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  * @{
25  */
26 
27 #include <kernel.h>
28 #include <zephyr/types.h>
29 #include <sys/dlist.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /**
36  * @brief Possible Cmd Codes for disk_ioctl()
37  */
38 
39 /** Get the number of sectors in the disk  */
40 #define DISK_IOCTL_GET_SECTOR_COUNT		1
41 /** Get the size of a disk SECTOR in bytes */
42 #define DISK_IOCTL_GET_SECTOR_SIZE		2
43 /** reserved. It used to be DISK_IOCTL_GET_DISK_SIZE */
44 #define DISK_IOCTL_RESERVED			3
45 /** How many  sectors constitute a FLASH Erase block */
46 #define DISK_IOCTL_GET_ERASE_BLOCK_SZ		4
47 /** Commit any cached read/writes to disk */
48 #define DISK_IOCTL_CTRL_SYNC			5
49 
50 /**
51  * @brief Possible return bitmasks for disk_status()
52  */
53 
54 /** Disk status okay */
55 #define DISK_STATUS_OK			0x00
56 /** Disk status uninitialized */
57 #define DISK_STATUS_UNINIT		0x01
58 /** Disk status no media */
59 #define DISK_STATUS_NOMEDIA		0x02
60 /** Disk status write protected */
61 #define DISK_STATUS_WR_PROTECT		0x04
62 
63 struct disk_operations;
64 
65 /**
66  * @brief Disk info
67  */
68 struct disk_info {
69 	/** Internally used list node */
70 	sys_dnode_t node;
71 	/** Disk name */
72 	char *name;
73 	/** Disk operations */
74 	const struct disk_operations *ops;
75 	/** Device associated to this disk */
76 	const struct device *dev;
77 };
78 
79 /**
80  * @brief Disk operations
81  */
82 struct disk_operations {
83 	int (*init)(struct disk_info *disk);
84 	int (*status)(struct disk_info *disk);
85 	int (*read)(struct disk_info *disk, uint8_t *data_buf,
86 		    uint32_t start_sector, uint32_t num_sector);
87 	int (*write)(struct disk_info *disk, const uint8_t *data_buf,
88 		     uint32_t start_sector, uint32_t num_sector);
89 	int (*ioctl)(struct disk_info *disk, uint8_t cmd, void *buff);
90 };
91 
92 /**
93  * @brief Register disk
94  *
95  * @param[in] disk Pointer to the disk info structure
96  *
97  * @return 0 on success, negative errno code on fail
98  */
99 int disk_access_register(struct disk_info *disk);
100 
101 /**
102  * @brief Unregister disk
103  *
104  * @param[in] disk Pointer to the disk info structure
105  *
106  * @return 0 on success, negative errno code on fail
107  */
108 int disk_access_unregister(struct disk_info *disk);
109 
110 #ifdef __cplusplus
111 }
112 #endif
113 
114 /**
115  * @}
116  */
117 
118 #endif /* ZEPHYR_INCLUDE_DRIVERS_DISK_H_ */
119