1 /*
2  * Copyright (c) 2016 Intel Corporation.
3  * Copyright (c) 2021 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /**
9  * @file
10  * @brief Disk Driver Interface
11  *
12  * This file contains interface for disk access. Apart from disks, various
13  * other storage media like Flash and RAM disks may implement this interface to
14  * be used by various higher layers(consumers) like USB Mass storage
15  * and Filesystems.
16  */
17 
18 #ifndef ZEPHYR_INCLUDE_DRIVERS_DISK_H_
19 #define ZEPHYR_INCLUDE_DRIVERS_DISK_H_
20 
21 /**
22  * @brief Disk Driver Interface
23  * @defgroup disk_driver_interface Disk Driver Interface
24  * @ingroup io_interfaces
25  * @{
26  */
27 
28 #include <zephyr/kernel.h>
29 #include <zephyr/types.h>
30 #include <zephyr/sys/dlist.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /**
37  * @brief Possible Cmd Codes for disk_ioctl()
38  */
39 
40 /** Get the number of sectors in the disk  */
41 #define DISK_IOCTL_GET_SECTOR_COUNT		1
42 /** Get the size of a disk SECTOR in bytes */
43 #define DISK_IOCTL_GET_SECTOR_SIZE		2
44 /** reserved. It used to be DISK_IOCTL_GET_DISK_SIZE */
45 #define DISK_IOCTL_RESERVED			3
46 /** How many  sectors constitute a FLASH Erase block */
47 #define DISK_IOCTL_GET_ERASE_BLOCK_SZ		4
48 /** Commit any cached read/writes to disk */
49 #define DISK_IOCTL_CTRL_SYNC			5
50 
51 /**
52  * @brief Possible return bitmasks for disk_status()
53  */
54 
55 /** Disk status okay */
56 #define DISK_STATUS_OK			0x00
57 /** Disk status uninitialized */
58 #define DISK_STATUS_UNINIT		0x01
59 /** Disk status no media */
60 #define DISK_STATUS_NOMEDIA		0x02
61 /** Disk status write protected */
62 #define DISK_STATUS_WR_PROTECT		0x04
63 
64 struct disk_operations;
65 
66 /**
67  * @brief Disk info
68  */
69 struct disk_info {
70 	/** Internally used list node */
71 	sys_dnode_t node;
72 	/** Disk name */
73 	char *name;
74 	/** Disk operations */
75 	const struct disk_operations *ops;
76 	/** Device associated to this disk */
77 	const struct device *dev;
78 };
79 
80 /**
81  * @brief Disk operations
82  */
83 struct disk_operations {
84 	int (*init)(struct disk_info *disk);
85 	int (*status)(struct disk_info *disk);
86 	int (*read)(struct disk_info *disk, uint8_t *data_buf,
87 		    uint32_t start_sector, uint32_t num_sector);
88 	int (*write)(struct disk_info *disk, const uint8_t *data_buf,
89 		     uint32_t start_sector, uint32_t num_sector);
90 	int (*ioctl)(struct disk_info *disk, uint8_t cmd, void *buff);
91 };
92 
93 /**
94  * @brief Register disk
95  *
96  * @param[in] disk Pointer to the disk info structure
97  *
98  * @return 0 on success, negative errno code on fail
99  */
100 int disk_access_register(struct disk_info *disk);
101 
102 /**
103  * @brief Unregister disk
104  *
105  * @param[in] disk Pointer to the disk info structure
106  *
107  * @return 0 on success, negative errno code on fail
108  */
109 int disk_access_unregister(struct disk_info *disk);
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 /**
116  * @}
117  */
118 
119 #endif /* ZEPHYR_INCLUDE_DRIVERS_DISK_H_ */
120