]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/gadget/function/storage_common.h
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / gadget / function / storage_common.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
6fdc5dd2
AP
2#ifndef USB_STORAGE_COMMON_H
3#define USB_STORAGE_COMMON_H
4
5#include <linux/device.h>
6#include <linux/usb/storage.h>
7#include <scsi/scsi.h>
8#include <asm/unaligned.h>
9
10#ifndef DEBUG
11#undef VERBOSE_DEBUG
12#undef DUMP_MSGS
13#endif /* !DEBUG */
14
15#ifdef VERBOSE_DEBUG
16#define VLDBG LDBG
17#else
18#define VLDBG(lun, fmt, args...) do { } while (0)
19#endif /* VERBOSE_DEBUG */
20
bd528d4e
AP
21#define _LMSG(func, lun, fmt, args...) \
22 do { \
23 if ((lun)->name_pfx && *(lun)->name_pfx) \
24 func("%s/%s: " fmt, *(lun)->name_pfx, \
25 (lun)->name, ## args); \
26 else \
27 func("%s: " fmt, (lun)->name, ## args); \
28 } while (0)
29
30#define LDBG(lun, fmt, args...) _LMSG(pr_debug, lun, fmt, ## args)
31#define LERROR(lun, fmt, args...) _LMSG(pr_err, lun, fmt, ## args)
32#define LWARN(lun, fmt, args...) _LMSG(pr_warn, lun, fmt, ## args)
33#define LINFO(lun, fmt, args...) _LMSG(pr_info, lun, fmt, ## args)
34
6fdc5dd2
AP
35
36#ifdef DUMP_MSGS
37
38# define dump_msg(fsg, /* const char * */ label, \
39 /* const u8 * */ buf, /* unsigned */ length) \
40do { \
41 if (length < 512) { \
42 DBG(fsg, "%s, length %u:\n", label, length); \
43 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
44 16, 1, buf, length, 0); \
45 } \
46} while (0)
47
48# define dump_cdb(fsg) do { } while (0)
49
50#else
51
52# define dump_msg(fsg, /* const char * */ label, \
53 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
54
55# ifdef VERBOSE_DEBUG
56
57# define dump_cdb(fsg) \
58 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
59 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
60
61# else
62
63# define dump_cdb(fsg) do { } while (0)
64
65# endif /* VERBOSE_DEBUG */
66
67#endif /* DUMP_MSGS */
68
69/* Length of a SCSI Command Data Block */
70#define MAX_COMMAND_SIZE 16
71
72/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
73#define SS_NO_SENSE 0
74#define SS_COMMUNICATION_FAILURE 0x040800
75#define SS_INVALID_COMMAND 0x052000
76#define SS_INVALID_FIELD_IN_CDB 0x052400
77#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
78#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
79#define SS_MEDIUM_NOT_PRESENT 0x023a00
80#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
81#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
82#define SS_RESET_OCCURRED 0x062900
83#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
84#define SS_UNRECOVERED_READ_ERROR 0x031100
85#define SS_WRITE_ERROR 0x030c02
86#define SS_WRITE_PROTECTED 0x072700
87
88#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
89#define ASC(x) ((u8) ((x) >> 8))
90#define ASCQ(x) ((u8) (x))
91
6ac47090
PG
92/*
93 * Vendor (8 chars), product (16 chars), release (4 hexadecimal digits) and NUL
94 * byte
95 */
96#define INQUIRY_STRING_LEN ((size_t) (8 + 16 + 4 + 1))
97
6fdc5dd2
AP
98struct fsg_lun {
99 struct file *filp;
100 loff_t file_length;
101 loff_t num_sectors;
102
103 unsigned int initially_ro:1;
104 unsigned int ro:1;
105 unsigned int removable:1;
106 unsigned int cdrom:1;
107 unsigned int prevent_medium_removal:1;
108 unsigned int registered:1;
109 unsigned int info_valid:1;
110 unsigned int nofua:1;
111
112 u32 sense_data;
113 u32 sense_data_info;
114 u32 unit_attention_data;
115
116 unsigned int blkbits; /* Bits of logical block size
117 of bound block device */
118 unsigned int blksize; /* logical block size of bound block device */
119 struct device dev;
bd528d4e
AP
120 const char *name; /* "lun.name" */
121 const char **name_pfx; /* "function.name" */
6ac47090 122 char inquiry_string[INQUIRY_STRING_LEN];
6fdc5dd2
AP
123};
124
125static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
126{
127 return curlun->filp != NULL;
128}
129
6fdc5dd2
AP
130/* Default size of buffer length. */
131#define FSG_BUFLEN ((u32)16384)
132
133/* Maximal number of LUNs supported in mass storage function */
75ddead2 134#define FSG_MAX_LUNS 16
6fdc5dd2
AP
135
136enum fsg_buffer_state {
225785ae
AS
137 BUF_STATE_SENDING = -2,
138 BUF_STATE_RECEIVING,
6fdc5dd2 139 BUF_STATE_EMPTY = 0,
225785ae 140 BUF_STATE_FULL
6fdc5dd2
AP
141};
142
143struct fsg_buffhd {
144 void *buf;
145 enum fsg_buffer_state state;
146 struct fsg_buffhd *next;
147
148 /*
149 * The NetChip 2280 is faster, and handles some protocol faults
150 * better, if we don't submit any short bulk-out read requests.
151 * So we will record the intended request length here.
152 */
153 unsigned int bulk_out_intended_length;
154
155 struct usb_request *inreq;
6fdc5dd2 156 struct usb_request *outreq;
6fdc5dd2
AP
157};
158
159enum fsg_state {
78db441d 160 FSG_STATE_NORMAL,
6fdc5dd2 161 FSG_STATE_ABORT_BULK_OUT,
78db441d 162 FSG_STATE_PROTOCOL_RESET,
6fdc5dd2 163 FSG_STATE_CONFIG_CHANGE,
6fdc5dd2
AP
164 FSG_STATE_EXIT,
165 FSG_STATE_TERMINATED
166};
167
168enum data_direction {
169 DATA_DIR_UNKNOWN = 0,
170 DATA_DIR_FROM_HOST,
171 DATA_DIR_TO_HOST,
172 DATA_DIR_NONE
173};
174
175static inline u32 get_unaligned_be24(u8 *buf)
176{
177 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
178}
179
77850ae2
AP
180static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
181{
182 return container_of(dev, struct fsg_lun, dev);
183}
184
6fdc5dd2
AP
185enum {
186 FSG_STRING_INTERFACE
187};
188
189extern struct usb_interface_descriptor fsg_intf_desc;
190
191extern struct usb_endpoint_descriptor fsg_fs_bulk_in_desc;
192extern struct usb_endpoint_descriptor fsg_fs_bulk_out_desc;
193extern struct usb_descriptor_header *fsg_fs_function[];
194
195extern struct usb_endpoint_descriptor fsg_hs_bulk_in_desc;
196extern struct usb_endpoint_descriptor fsg_hs_bulk_out_desc;
197extern struct usb_descriptor_header *fsg_hs_function[];
198
199extern struct usb_endpoint_descriptor fsg_ss_bulk_in_desc;
200extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc;
201extern struct usb_endpoint_descriptor fsg_ss_bulk_out_desc;
202extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc;
203extern struct usb_descriptor_header *fsg_ss_function[];
204
205void fsg_lun_close(struct fsg_lun *curlun);
206int fsg_lun_open(struct fsg_lun *curlun, const char *filename);
207int fsg_lun_fsync_sub(struct fsg_lun *curlun);
208void store_cdrom_address(u8 *dest, int msf, u32 addr);
77850ae2
AP
209ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf);
210ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf);
211ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
6fdc5dd2 212 char *buf);
6ac47090 213ssize_t fsg_show_inquiry_string(struct fsg_lun *curlun, char *buf);
864328ef
AP
214ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf);
215ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf);
77850ae2 216ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
6fdc5dd2 217 const char *buf, size_t count);
77850ae2
AP
218ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count);
219ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
6fdc5dd2 220 const char *buf, size_t count);
b8798636
AP
221ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
222 const char *buf, size_t count);
864328ef
AP
223ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
224 size_t count);
6ac47090
PG
225ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
226 size_t count);
6fdc5dd2
AP
227
228#endif /* USB_STORAGE_COMMON_H */