1 /*
2  * Copyright (c) 2013 Corey Tabaka
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 
9 #include <lk/err.h>
10 #include <dev/class/block.h>
11 
class_block_get_size(struct device * dev)12 ssize_t class_block_get_size(struct device *dev) {
13     struct block_ops *ops = device_get_driver_ops(dev, struct block_ops, std);
14     if (!ops)
15         return ERR_NOT_CONFIGURED;
16 
17     if (ops->get_block_size)
18         return ops->get_block_size(dev);
19     else
20         return ERR_NOT_SUPPORTED;
21 }
22 
class_block_get_count(struct device * dev)23 ssize_t class_block_get_count(struct device *dev) {
24     struct block_ops *ops = device_get_driver_ops(dev, struct block_ops, std);
25     if (!ops)
26         return ERR_NOT_CONFIGURED;
27 
28     if (ops->get_block_count)
29         return ops->get_block_count(dev);
30     else
31         return ERR_NOT_SUPPORTED;
32 }
33 
class_block_write(struct device * dev,off_t offset,const void * buf,size_t count)34 ssize_t class_block_write(struct device *dev, off_t offset, const void *buf, size_t count) {
35     struct block_ops *ops = device_get_driver_ops(dev, struct block_ops, std);
36     if (!ops)
37         return ERR_NOT_CONFIGURED;
38 
39     if (ops->write)
40         return ops->write(dev, offset, buf, count);
41     else
42         return ERR_NOT_SUPPORTED;
43 }
44 
class_block_read(struct device * dev,off_t offset,void * buf,size_t count)45 ssize_t class_block_read(struct device *dev, off_t offset, void *buf, size_t count) {
46     struct block_ops *ops = device_get_driver_ops(dev, struct block_ops, std);
47     if (!ops)
48         return ERR_NOT_CONFIGURED;
49 
50     if (ops->read)
51         return ops->read(dev, offset, buf, count);
52     else
53         return ERR_NOT_SUPPORTED;
54 }
55 
class_block_flush(struct device * dev)56 status_t class_block_flush(struct device *dev) {
57     struct block_ops *ops = device_get_driver_ops(dev, struct block_ops, std);
58     if (!ops)
59         return ERR_NOT_CONFIGURED;
60 
61     if (ops->flush)
62         return ops->flush(dev);
63     else
64         return ERR_NOT_SUPPORTED;
65 }
66 
67