Commit | Line | Data |
---|---|---|
b6058d0f MN |
1 | /* |
2 | * storage_common.c -- Common definitions for mass storage functionality | |
3 | * | |
4 | * Copyright (C) 2003-2008 Alan Stern | |
5 | * Copyeight (C) 2009 Samsung Electronics | |
54b8360f | 6 | * Author: Michal Nazarewicz (mina86@mina86.com) |
d6181702 MN |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
d6181702 MN |
12 | */ |
13 | ||
14 | ||
15 | /* | |
16 | * This file requires the following identifiers used in USB strings to | |
17 | * be defined (each of type pointer to char): | |
18 | * - fsg_string_manufacturer -- name of the manufacturer | |
19 | * - fsg_string_product -- name of the product | |
d6181702 MN |
20 | * - fsg_string_config -- name of the configuration |
21 | * - fsg_string_interface -- name of the interface | |
22 | * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS | |
23 | * macro is defined prior to including this file. | |
b6058d0f MN |
24 | */ |
25 | ||
93bcf12e MN |
26 | /* |
27 | * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and | |
28 | * fsg_hs_intr_in_desc objects as well as | |
29 | * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES | |
30 | * macros are not defined. | |
31 | * | |
32 | * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER, | |
33 | * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not | |
34 | * defined (as well as corresponding entries in string tables are | |
35 | * missing) and FSG_STRING_INTERFACE has value of zero. | |
36 | * | |
37 | * When FSG_NO_OTG is defined fsg_otg_desc won't be defined. | |
38 | */ | |
39 | ||
606206c2 MN |
40 | /* |
41 | * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included | |
42 | * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN | |
43 | * characters rather then a pointer to void. | |
44 | */ | |
45 | ||
6532c7fd PF |
46 | /* |
47 | * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers | |
48 | * sets the number of pipeline buffers (length of the fsg_buffhd array). | |
49 | * The valid range of num_buffers is: num >= 2 && num <= 4. | |
50 | */ | |
51 | ||
b6058d0f | 52 | |
3323b710 | 53 | #include <linux/usb/storage.h> |
0a6a717c MN |
54 | #include <scsi/scsi.h> |
55 | #include <asm/unaligned.h> | |
d6181702 | 56 | |
b6058d0f | 57 | |
d0893264 MN |
58 | /* |
59 | * Thanks to NetChip Technologies for donating this product ID. | |
93f93740 MN |
60 | * |
61 | * DO NOT REUSE THESE IDs with any other driver!! Ever!! | |
d0893264 MN |
62 | * Instead: allocate your own, using normal USB-IF procedures. |
63 | */ | |
d26a6aa0 MN |
64 | #define FSG_VENDOR_ID 0x0525 /* NetChip */ |
65 | #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */ | |
93f93740 MN |
66 | |
67 | ||
b6058d0f MN |
68 | /*-------------------------------------------------------------------------*/ |
69 | ||
70 | ||
71 | #ifndef DEBUG | |
72 | #undef VERBOSE_DEBUG | |
73 | #undef DUMP_MSGS | |
74 | #endif /* !DEBUG */ | |
75 | ||
76 | #ifdef VERBOSE_DEBUG | |
77 | #define VLDBG LDBG | |
78 | #else | |
79 | #define VLDBG(lun, fmt, args...) do { } while (0) | |
80 | #endif /* VERBOSE_DEBUG */ | |
81 | ||
82 | #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args) | |
83 | #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args) | |
84 | #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args) | |
85 | #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args) | |
86 | ||
d0893264 MN |
87 | /* |
88 | * Keep those macros in sync with those in | |
89 | * include/linux/usb/composite.h or else GCC will complain. If they | |
d23b0f08 MN |
90 | * are identical (the same names of arguments, white spaces in the |
91 | * same places) GCC will allow redefinition otherwise (even if some | |
d0893264 MN |
92 | * white space is removed or added) warning will be issued. |
93 | * | |
94 | * Those macros are needed here because File Storage Gadget does not | |
95 | * include the composite.h header. For composite gadgets those macros | |
96 | * are redundant since composite.h is included any way. | |
97 | * | |
98 | * One could check whether those macros are already defined (which | |
99 | * would indicate composite.h had been included) or not (which would | |
100 | * indicate we were in FSG) but this is not done because a warning is | |
101 | * desired if definitions here differ from the ones in composite.h. | |
102 | * | |
103 | * We want the definitions to match and be the same in File Storage | |
104 | * Gadget as well as Mass Storage Function (and so composite gadgets | |
105 | * using MSF). If someone changes them in composite.h it will produce | |
106 | * a warning in this file when building MSF. | |
107 | */ | |
d23b0f08 MN |
108 | #define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args) |
109 | #define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args) | |
110 | #define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args) | |
111 | #define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args) | |
112 | #define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args) | |
b6058d0f MN |
113 | |
114 | ||
115 | ||
116 | #ifdef DUMP_MSGS | |
117 | ||
118 | # define dump_msg(fsg, /* const char * */ label, \ | |
d26a6aa0 | 119 | /* const u8 * */ buf, /* unsigned */ length) do { \ |
b6058d0f MN |
120 | if (length < 512) { \ |
121 | DBG(fsg, "%s, length %u:\n", label, length); \ | |
122 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \ | |
123 | 16, 1, buf, length, 0); \ | |
124 | } \ | |
125 | } while (0) | |
126 | ||
127 | # define dump_cdb(fsg) do { } while (0) | |
128 | ||
129 | #else | |
130 | ||
131 | # define dump_msg(fsg, /* const char * */ label, \ | |
d26a6aa0 | 132 | /* const u8 * */ buf, /* unsigned */ length) do { } while (0) |
b6058d0f MN |
133 | |
134 | # ifdef VERBOSE_DEBUG | |
135 | ||
d26a6aa0 | 136 | # define dump_cdb(fsg) \ |
b6058d0f MN |
137 | print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \ |
138 | 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \ | |
139 | ||
140 | # else | |
141 | ||
142 | # define dump_cdb(fsg) do { } while (0) | |
143 | ||
144 | # endif /* VERBOSE_DEBUG */ | |
145 | ||
146 | #endif /* DUMP_MSGS */ | |
147 | ||
b6058d0f MN |
148 | /*-------------------------------------------------------------------------*/ |
149 | ||
b6058d0f MN |
150 | /* CBI Interrupt data structure */ |
151 | struct interrupt_data { | |
152 | u8 bType; | |
153 | u8 bValue; | |
154 | }; | |
155 | ||
156 | #define CBI_INTERRUPT_DATA_LEN 2 | |
157 | ||
158 | /* CBI Accept Device-Specific Command request */ | |
159 | #define USB_CBI_ADSC_REQUEST 0x00 | |
160 | ||
161 | ||
d26a6aa0 MN |
162 | /* Length of a SCSI Command Data Block */ |
163 | #define MAX_COMMAND_SIZE 16 | |
b6058d0f | 164 | |
b6058d0f MN |
165 | /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */ |
166 | #define SS_NO_SENSE 0 | |
167 | #define SS_COMMUNICATION_FAILURE 0x040800 | |
168 | #define SS_INVALID_COMMAND 0x052000 | |
169 | #define SS_INVALID_FIELD_IN_CDB 0x052400 | |
170 | #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100 | |
171 | #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500 | |
172 | #define SS_MEDIUM_NOT_PRESENT 0x023a00 | |
173 | #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302 | |
174 | #define SS_NOT_READY_TO_READY_TRANSITION 0x062800 | |
175 | #define SS_RESET_OCCURRED 0x062900 | |
176 | #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900 | |
177 | #define SS_UNRECOVERED_READ_ERROR 0x031100 | |
178 | #define SS_WRITE_ERROR 0x030c02 | |
179 | #define SS_WRITE_PROTECTED 0x072700 | |
180 | ||
d26a6aa0 | 181 | #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */ |
b6058d0f MN |
182 | #define ASC(x) ((u8) ((x) >> 8)) |
183 | #define ASCQ(x) ((u8) (x)) | |
184 | ||
185 | ||
186 | /*-------------------------------------------------------------------------*/ | |
187 | ||
188 | ||
d6181702 | 189 | struct fsg_lun { |
b6058d0f MN |
190 | struct file *filp; |
191 | loff_t file_length; | |
192 | loff_t num_sectors; | |
193 | ||
d26a6aa0 MN |
194 | unsigned int initially_ro:1; |
195 | unsigned int ro:1; | |
196 | unsigned int removable:1; | |
197 | unsigned int cdrom:1; | |
198 | unsigned int prevent_medium_removal:1; | |
199 | unsigned int registered:1; | |
200 | unsigned int info_valid:1; | |
a93917d3 | 201 | unsigned int nofua:1; |
b6058d0f MN |
202 | |
203 | u32 sense_data; | |
204 | u32 sense_data_info; | |
205 | u32 unit_attention_data; | |
206 | ||
3f565a36 PL |
207 | unsigned int blkbits; /* Bits of logical block size of bound block device */ |
208 | unsigned int blksize; /* logical block size of bound block device */ | |
b6058d0f MN |
209 | struct device dev; |
210 | }; | |
211 | ||
d6181702 | 212 | #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL) |
b6058d0f | 213 | |
d6181702 | 214 | static struct fsg_lun *fsg_lun_from_dev(struct device *dev) |
b6058d0f | 215 | { |
d6181702 | 216 | return container_of(dev, struct fsg_lun, dev); |
b6058d0f MN |
217 | } |
218 | ||
219 | ||
220 | /* Big enough to hold our biggest descriptor */ | |
221 | #define EP0_BUFSIZE 256 | |
d26a6aa0 | 222 | #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */ |
b6058d0f | 223 | |
6532c7fd PF |
224 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
225 | ||
226 | static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS; | |
227 | module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO); | |
228 | MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers"); | |
229 | ||
230 | #else | |
231 | ||
232 | /* | |
233 | * Number of buffers we will use. | |
234 | * 2 is usually enough for good buffering pipeline | |
235 | */ | |
236 | #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS | |
237 | ||
238 | #endif /* CONFIG_USB_DEBUG */ | |
239 | ||
240 | /* check if fsg_num_buffers is within a valid range */ | |
241 | static inline int fsg_num_buffers_validate(void) | |
242 | { | |
243 | if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4) | |
244 | return 0; | |
245 | pr_err("fsg_num_buffers %u is out of range (%d to %d)\n", | |
246 | fsg_num_buffers, 2 ,4); | |
247 | return -EINVAL; | |
248 | } | |
b6058d0f | 249 | |
93f93740 MN |
250 | /* Default size of buffer length. */ |
251 | #define FSG_BUFLEN ((u32)16384) | |
252 | ||
253 | /* Maximal number of LUNs supported in mass storage function */ | |
254 | #define FSG_MAX_LUNS 8 | |
255 | ||
b6058d0f MN |
256 | enum fsg_buffer_state { |
257 | BUF_STATE_EMPTY = 0, | |
258 | BUF_STATE_FULL, | |
259 | BUF_STATE_BUSY | |
260 | }; | |
261 | ||
262 | struct fsg_buffhd { | |
606206c2 MN |
263 | #ifdef FSG_BUFFHD_STATIC_BUFFER |
264 | char buf[FSG_BUFLEN]; | |
265 | #else | |
b6058d0f | 266 | void *buf; |
606206c2 | 267 | #endif |
b6058d0f MN |
268 | enum fsg_buffer_state state; |
269 | struct fsg_buffhd *next; | |
270 | ||
98346f7d GKH |
271 | /* |
272 | * The NetChip 2280 is faster, and handles some protocol faults | |
273 | * better, if we don't submit any short bulk-out read requests. | |
274 | * So we will record the intended request length here. | |
275 | */ | |
276 | unsigned int bulk_out_intended_length; | |
277 | ||
b6058d0f MN |
278 | struct usb_request *inreq; |
279 | int inreq_busy; | |
280 | struct usb_request *outreq; | |
281 | int outreq_busy; | |
282 | }; | |
283 | ||
284 | enum fsg_state { | |
d26a6aa0 MN |
285 | /* This one isn't used anywhere */ |
286 | FSG_STATE_COMMAND_PHASE = -10, | |
b6058d0f MN |
287 | FSG_STATE_DATA_PHASE, |
288 | FSG_STATE_STATUS_PHASE, | |
289 | ||
290 | FSG_STATE_IDLE = 0, | |
291 | FSG_STATE_ABORT_BULK_OUT, | |
292 | FSG_STATE_RESET, | |
293 | FSG_STATE_INTERFACE_CHANGE, | |
294 | FSG_STATE_CONFIG_CHANGE, | |
295 | FSG_STATE_DISCONNECT, | |
296 | FSG_STATE_EXIT, | |
297 | FSG_STATE_TERMINATED | |
298 | }; | |
299 | ||
300 | enum data_direction { | |
301 | DATA_DIR_UNKNOWN = 0, | |
302 | DATA_DIR_FROM_HOST, | |
303 | DATA_DIR_TO_HOST, | |
304 | DATA_DIR_NONE | |
305 | }; | |
306 | ||
307 | ||
308 | /*-------------------------------------------------------------------------*/ | |
309 | ||
310 | ||
311 | static inline u32 get_unaligned_be24(u8 *buf) | |
312 | { | |
313 | return 0xffffff & (u32) get_unaligned_be32(buf - 1); | |
314 | } | |
315 | ||
316 | ||
317 | /*-------------------------------------------------------------------------*/ | |
318 | ||
319 | ||
d6181702 | 320 | enum { |
93bcf12e | 321 | #ifndef FSG_NO_DEVICE_STRINGS |
d6181702 MN |
322 | FSG_STRING_MANUFACTURER = 1, |
323 | FSG_STRING_PRODUCT, | |
324 | FSG_STRING_SERIAL, | |
325 | FSG_STRING_CONFIG, | |
93bcf12e | 326 | #endif |
d6181702 MN |
327 | FSG_STRING_INTERFACE |
328 | }; | |
b6058d0f MN |
329 | |
330 | ||
93bcf12e | 331 | #ifndef FSG_NO_OTG |
b6058d0f | 332 | static struct usb_otg_descriptor |
d6181702 MN |
333 | fsg_otg_desc = { |
334 | .bLength = sizeof fsg_otg_desc, | |
b6058d0f MN |
335 | .bDescriptorType = USB_DT_OTG, |
336 | ||
337 | .bmAttributes = USB_OTG_SRP, | |
338 | }; | |
93bcf12e | 339 | #endif |
b6058d0f MN |
340 | |
341 | /* There is only one interface. */ | |
342 | ||
343 | static struct usb_interface_descriptor | |
d6181702 MN |
344 | fsg_intf_desc = { |
345 | .bLength = sizeof fsg_intf_desc, | |
b6058d0f MN |
346 | .bDescriptorType = USB_DT_INTERFACE, |
347 | ||
d26a6aa0 | 348 | .bNumEndpoints = 2, /* Adjusted during fsg_bind() */ |
b6058d0f | 349 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, |
d26a6aa0 MN |
350 | .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */ |
351 | .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */ | |
d6181702 | 352 | .iInterface = FSG_STRING_INTERFACE, |
b6058d0f MN |
353 | }; |
354 | ||
d0893264 MN |
355 | /* |
356 | * Three full-speed endpoint descriptors: bulk-in, bulk-out, and | |
357 | * interrupt-in. | |
358 | */ | |
b6058d0f MN |
359 | |
360 | static struct usb_endpoint_descriptor | |
d6181702 | 361 | fsg_fs_bulk_in_desc = { |
b6058d0f MN |
362 | .bLength = USB_DT_ENDPOINT_SIZE, |
363 | .bDescriptorType = USB_DT_ENDPOINT, | |
364 | ||
365 | .bEndpointAddress = USB_DIR_IN, | |
366 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
367 | /* wMaxPacketSize set by autoconfiguration */ | |
368 | }; | |
369 | ||
370 | static struct usb_endpoint_descriptor | |
d6181702 | 371 | fsg_fs_bulk_out_desc = { |
b6058d0f MN |
372 | .bLength = USB_DT_ENDPOINT_SIZE, |
373 | .bDescriptorType = USB_DT_ENDPOINT, | |
374 | ||
375 | .bEndpointAddress = USB_DIR_OUT, | |
376 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
377 | /* wMaxPacketSize set by autoconfiguration */ | |
378 | }; | |
379 | ||
93bcf12e MN |
380 | #ifndef FSG_NO_INTR_EP |
381 | ||
b6058d0f | 382 | static struct usb_endpoint_descriptor |
d6181702 | 383 | fsg_fs_intr_in_desc = { |
b6058d0f MN |
384 | .bLength = USB_DT_ENDPOINT_SIZE, |
385 | .bDescriptorType = USB_DT_ENDPOINT, | |
386 | ||
387 | .bEndpointAddress = USB_DIR_IN, | |
388 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
389 | .wMaxPacketSize = cpu_to_le16(2), | |
d26a6aa0 | 390 | .bInterval = 32, /* frames -> 32 ms */ |
b6058d0f MN |
391 | }; |
392 | ||
93bcf12e MN |
393 | #ifndef FSG_NO_OTG |
394 | # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2 | |
395 | #else | |
396 | # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1 | |
397 | #endif | |
398 | ||
399 | #endif | |
400 | ||
d23b0f08 | 401 | static struct usb_descriptor_header *fsg_fs_function[] = { |
93bcf12e | 402 | #ifndef FSG_NO_OTG |
d6181702 | 403 | (struct usb_descriptor_header *) &fsg_otg_desc, |
93bcf12e | 404 | #endif |
d6181702 MN |
405 | (struct usb_descriptor_header *) &fsg_intf_desc, |
406 | (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc, | |
407 | (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc, | |
93bcf12e | 408 | #ifndef FSG_NO_INTR_EP |
d6181702 | 409 | (struct usb_descriptor_header *) &fsg_fs_intr_in_desc, |
93bcf12e | 410 | #endif |
b6058d0f MN |
411 | NULL, |
412 | }; | |
b6058d0f MN |
413 | |
414 | ||
415 | /* | |
416 | * USB 2.0 devices need to expose both high speed and full speed | |
417 | * descriptors, unless they only run at full speed. | |
418 | * | |
419 | * That means alternate endpoint descriptors (bigger packets) | |
420 | * and a "device qualifier" ... plus more construction options | |
d0893264 | 421 | * for the configuration descriptor. |
b6058d0f MN |
422 | */ |
423 | static struct usb_endpoint_descriptor | |
d6181702 | 424 | fsg_hs_bulk_in_desc = { |
b6058d0f MN |
425 | .bLength = USB_DT_ENDPOINT_SIZE, |
426 | .bDescriptorType = USB_DT_ENDPOINT, | |
427 | ||
428 | /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */ | |
429 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
430 | .wMaxPacketSize = cpu_to_le16(512), | |
431 | }; | |
432 | ||
433 | static struct usb_endpoint_descriptor | |
d6181702 | 434 | fsg_hs_bulk_out_desc = { |
b6058d0f MN |
435 | .bLength = USB_DT_ENDPOINT_SIZE, |
436 | .bDescriptorType = USB_DT_ENDPOINT, | |
437 | ||
438 | /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */ | |
439 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
440 | .wMaxPacketSize = cpu_to_le16(512), | |
d26a6aa0 | 441 | .bInterval = 1, /* NAK every 1 uframe */ |
b6058d0f MN |
442 | }; |
443 | ||
93bcf12e MN |
444 | #ifndef FSG_NO_INTR_EP |
445 | ||
b6058d0f | 446 | static struct usb_endpoint_descriptor |
d6181702 | 447 | fsg_hs_intr_in_desc = { |
b6058d0f MN |
448 | .bLength = USB_DT_ENDPOINT_SIZE, |
449 | .bDescriptorType = USB_DT_ENDPOINT, | |
450 | ||
451 | /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */ | |
452 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
453 | .wMaxPacketSize = cpu_to_le16(2), | |
d26a6aa0 | 454 | .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */ |
b6058d0f MN |
455 | }; |
456 | ||
93bcf12e MN |
457 | #ifndef FSG_NO_OTG |
458 | # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2 | |
459 | #else | |
460 | # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1 | |
461 | #endif | |
462 | ||
463 | #endif | |
464 | ||
d23b0f08 | 465 | static struct usb_descriptor_header *fsg_hs_function[] = { |
93bcf12e | 466 | #ifndef FSG_NO_OTG |
d6181702 | 467 | (struct usb_descriptor_header *) &fsg_otg_desc, |
93bcf12e | 468 | #endif |
d6181702 MN |
469 | (struct usb_descriptor_header *) &fsg_intf_desc, |
470 | (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc, | |
471 | (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc, | |
93bcf12e | 472 | #ifndef FSG_NO_INTR_EP |
d6181702 | 473 | (struct usb_descriptor_header *) &fsg_hs_intr_in_desc, |
93bcf12e | 474 | #endif |
b6058d0f MN |
475 | NULL, |
476 | }; | |
b6058d0f | 477 | |
4bb99b7c FB |
478 | static struct usb_endpoint_descriptor |
479 | fsg_ss_bulk_in_desc = { | |
480 | .bLength = USB_DT_ENDPOINT_SIZE, | |
481 | .bDescriptorType = USB_DT_ENDPOINT, | |
482 | ||
483 | /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */ | |
484 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
485 | .wMaxPacketSize = cpu_to_le16(1024), | |
486 | }; | |
487 | ||
488 | static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = { | |
489 | .bLength = sizeof(fsg_ss_bulk_in_comp_desc), | |
490 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
491 | ||
492 | /*.bMaxBurst = DYNAMIC, */ | |
493 | }; | |
494 | ||
495 | static struct usb_endpoint_descriptor | |
496 | fsg_ss_bulk_out_desc = { | |
497 | .bLength = USB_DT_ENDPOINT_SIZE, | |
498 | .bDescriptorType = USB_DT_ENDPOINT, | |
499 | ||
500 | /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */ | |
501 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
502 | .wMaxPacketSize = cpu_to_le16(1024), | |
503 | }; | |
504 | ||
505 | static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = { | |
506 | .bLength = sizeof(fsg_ss_bulk_in_comp_desc), | |
507 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
508 | ||
509 | /*.bMaxBurst = DYNAMIC, */ | |
510 | }; | |
511 | ||
512 | #ifndef FSG_NO_INTR_EP | |
513 | ||
514 | static struct usb_endpoint_descriptor | |
515 | fsg_ss_intr_in_desc = { | |
516 | .bLength = USB_DT_ENDPOINT_SIZE, | |
517 | .bDescriptorType = USB_DT_ENDPOINT, | |
518 | ||
519 | /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */ | |
520 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
521 | .wMaxPacketSize = cpu_to_le16(2), | |
522 | .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */ | |
523 | }; | |
524 | ||
525 | static struct usb_ss_ep_comp_descriptor fsg_ss_intr_in_comp_desc = { | |
526 | .bLength = sizeof(fsg_ss_bulk_in_comp_desc), | |
527 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
528 | ||
529 | .wBytesPerInterval = cpu_to_le16(2), | |
530 | }; | |
531 | ||
532 | #ifndef FSG_NO_OTG | |
533 | # define FSG_SS_FUNCTION_PRE_EP_ENTRIES 2 | |
534 | #else | |
535 | # define FSG_SS_FUNCTION_PRE_EP_ENTRIES 1 | |
536 | #endif | |
537 | ||
538 | #endif | |
539 | ||
540 | static __maybe_unused struct usb_ext_cap_descriptor fsg_ext_cap_desc = { | |
541 | .bLength = USB_DT_USB_EXT_CAP_SIZE, | |
542 | .bDescriptorType = USB_DT_DEVICE_CAPABILITY, | |
543 | .bDevCapabilityType = USB_CAP_TYPE_EXT, | |
544 | ||
545 | .bmAttributes = cpu_to_le32(USB_LPM_SUPPORT), | |
546 | }; | |
547 | ||
548 | static __maybe_unused struct usb_ss_cap_descriptor fsg_ss_cap_desc = { | |
549 | .bLength = USB_DT_USB_SS_CAP_SIZE, | |
550 | .bDescriptorType = USB_DT_DEVICE_CAPABILITY, | |
551 | .bDevCapabilityType = USB_SS_CAP_TYPE, | |
552 | ||
553 | /* .bmAttributes = LTM is not supported yet */ | |
554 | ||
555 | .wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION | |
556 | | USB_FULL_SPEED_OPERATION | |
557 | | USB_HIGH_SPEED_OPERATION | |
558 | | USB_5GBPS_OPERATION), | |
559 | .bFunctionalitySupport = USB_LOW_SPEED_OPERATION, | |
560 | .bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT, | |
a8501639 | 561 | .bU2DevExitLat = cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT), |
4bb99b7c FB |
562 | }; |
563 | ||
564 | static __maybe_unused struct usb_bos_descriptor fsg_bos_desc = { | |
565 | .bLength = USB_DT_BOS_SIZE, | |
566 | .bDescriptorType = USB_DT_BOS, | |
567 | ||
a8501639 | 568 | .wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE |
4bb99b7c | 569 | + USB_DT_USB_EXT_CAP_SIZE |
a8501639 | 570 | + USB_DT_USB_SS_CAP_SIZE), |
4bb99b7c FB |
571 | |
572 | .bNumDeviceCaps = 2, | |
573 | }; | |
574 | ||
575 | static struct usb_descriptor_header *fsg_ss_function[] = { | |
576 | #ifndef FSG_NO_OTG | |
577 | (struct usb_descriptor_header *) &fsg_otg_desc, | |
578 | #endif | |
579 | (struct usb_descriptor_header *) &fsg_intf_desc, | |
580 | (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc, | |
581 | (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc, | |
582 | (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc, | |
583 | (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc, | |
584 | #ifndef FSG_NO_INTR_EP | |
585 | (struct usb_descriptor_header *) &fsg_ss_intr_in_desc, | |
586 | (struct usb_descriptor_header *) &fsg_ss_intr_in_comp_desc, | |
587 | #endif | |
588 | NULL, | |
589 | }; | |
590 | ||
b6058d0f | 591 | /* Maxpacket and other transfer characteristics vary by speed. */ |
bc8687db | 592 | static __maybe_unused struct usb_endpoint_descriptor * |
d6181702 | 593 | fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, |
4bb99b7c FB |
594 | struct usb_endpoint_descriptor *hs, |
595 | struct usb_endpoint_descriptor *ss) | |
b6058d0f | 596 | { |
4bb99b7c FB |
597 | if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER) |
598 | return ss; | |
599 | else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | |
b6058d0f MN |
600 | return hs; |
601 | return fs; | |
602 | } | |
603 | ||
604 | ||
b6058d0f | 605 | /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */ |
d6181702 | 606 | static struct usb_string fsg_strings[] = { |
93bcf12e | 607 | #ifndef FSG_NO_DEVICE_STRINGS |
d6181702 MN |
608 | {FSG_STRING_MANUFACTURER, fsg_string_manufacturer}, |
609 | {FSG_STRING_PRODUCT, fsg_string_product}, | |
d8087427 | 610 | {FSG_STRING_SERIAL, ""}, |
d6181702 | 611 | {FSG_STRING_CONFIG, fsg_string_config}, |
93bcf12e | 612 | #endif |
d6181702 | 613 | {FSG_STRING_INTERFACE, fsg_string_interface}, |
b6058d0f MN |
614 | {} |
615 | }; | |
616 | ||
d6181702 | 617 | static struct usb_gadget_strings fsg_stringtab = { |
d26a6aa0 | 618 | .language = 0x0409, /* en-us */ |
d6181702 | 619 | .strings = fsg_strings, |
b6058d0f MN |
620 | }; |
621 | ||
622 | ||
623 | /*-------------------------------------------------------------------------*/ | |
624 | ||
d0893264 MN |
625 | /* |
626 | * If the next two routines are called while the gadget is registered, | |
627 | * the caller must own fsg->filesem for writing. | |
628 | */ | |
b6058d0f | 629 | |
d6181702 | 630 | static int fsg_lun_open(struct fsg_lun *curlun, const char *filename) |
b6058d0f MN |
631 | { |
632 | int ro; | |
633 | struct file *filp = NULL; | |
634 | int rc = -EINVAL; | |
635 | struct inode *inode = NULL; | |
636 | loff_t size; | |
637 | loff_t num_sectors; | |
638 | loff_t min_sectors; | |
639 | ||
640 | /* R/W if we can, R/O if we must */ | |
e909ef5d | 641 | ro = curlun->initially_ro; |
b6058d0f MN |
642 | if (!ro) { |
643 | filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0); | |
75f1dc0d | 644 | if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES) |
b6058d0f MN |
645 | ro = 1; |
646 | } | |
647 | if (ro) | |
648 | filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0); | |
649 | if (IS_ERR(filp)) { | |
650 | LINFO(curlun, "unable to open backing file: %s\n", filename); | |
651 | return PTR_ERR(filp); | |
652 | } | |
653 | ||
654 | if (!(filp->f_mode & FMODE_WRITE)) | |
655 | ro = 1; | |
656 | ||
657 | if (filp->f_path.dentry) | |
658 | inode = filp->f_path.dentry->d_inode; | |
75f1dc0d | 659 | if (!inode || (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) { |
b6058d0f MN |
660 | LINFO(curlun, "invalid file type: %s\n", filename); |
661 | goto out; | |
662 | } | |
663 | ||
d0893264 MN |
664 | /* |
665 | * If we can't read the file, it's no good. | |
666 | * If we can't write the file, use it read-only. | |
667 | */ | |
b6058d0f MN |
668 | if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) { |
669 | LINFO(curlun, "file not readable: %s\n", filename); | |
670 | goto out; | |
671 | } | |
672 | if (!(filp->f_op->write || filp->f_op->aio_write)) | |
673 | ro = 1; | |
674 | ||
675 | size = i_size_read(inode->i_mapping->host); | |
676 | if (size < 0) { | |
677 | LINFO(curlun, "unable to find file size: %s\n", filename); | |
678 | rc = (int) size; | |
679 | goto out; | |
680 | } | |
3f565a36 PL |
681 | |
682 | if (curlun->cdrom) { | |
683 | curlun->blksize = 2048; | |
684 | curlun->blkbits = 11; | |
685 | } else if (inode->i_bdev) { | |
686 | curlun->blksize = bdev_logical_block_size(inode->i_bdev); | |
687 | curlun->blkbits = blksize_bits(curlun->blksize); | |
688 | } else { | |
689 | curlun->blksize = 512; | |
690 | curlun->blkbits = 9; | |
691 | } | |
692 | ||
693 | num_sectors = size >> curlun->blkbits; /* File size in logic-block-size blocks */ | |
b6058d0f | 694 | min_sectors = 1; |
e909ef5d | 695 | if (curlun->cdrom) { |
3f565a36 PL |
696 | min_sectors = 300; /* Smallest track is 300 frames */ |
697 | if (num_sectors >= 256*60*75) { | |
698 | num_sectors = 256*60*75 - 1; | |
b6058d0f MN |
699 | LINFO(curlun, "file too big: %s\n", filename); |
700 | LINFO(curlun, "using only first %d blocks\n", | |
701 | (int) num_sectors); | |
702 | } | |
703 | } | |
704 | if (num_sectors < min_sectors) { | |
705 | LINFO(curlun, "file too small: %s\n", filename); | |
706 | rc = -ETOOSMALL; | |
707 | goto out; | |
708 | } | |
709 | ||
710 | get_file(filp); | |
711 | curlun->ro = ro; | |
712 | curlun->filp = filp; | |
713 | curlun->file_length = size; | |
714 | curlun->num_sectors = num_sectors; | |
715 | LDBG(curlun, "open backing file: %s\n", filename); | |
716 | rc = 0; | |
717 | ||
718 | out: | |
719 | filp_close(filp, current->files); | |
720 | return rc; | |
721 | } | |
722 | ||
723 | ||
d6181702 | 724 | static void fsg_lun_close(struct fsg_lun *curlun) |
b6058d0f MN |
725 | { |
726 | if (curlun->filp) { | |
727 | LDBG(curlun, "close backing file\n"); | |
728 | fput(curlun->filp); | |
729 | curlun->filp = NULL; | |
730 | } | |
731 | } | |
732 | ||
733 | ||
734 | /*-------------------------------------------------------------------------*/ | |
735 | ||
d0893264 MN |
736 | /* |
737 | * Sync the file data, don't bother with the metadata. | |
738 | * This code was copied from fs/buffer.c:sys_fdatasync(). | |
739 | */ | |
d6181702 | 740 | static int fsg_lun_fsync_sub(struct fsg_lun *curlun) |
b6058d0f MN |
741 | { |
742 | struct file *filp = curlun->filp; | |
743 | ||
744 | if (curlun->ro || !filp) | |
745 | return 0; | |
8018ab05 | 746 | return vfs_fsync(filp, 1); |
b6058d0f MN |
747 | } |
748 | ||
749 | static void store_cdrom_address(u8 *dest, int msf, u32 addr) | |
750 | { | |
751 | if (msf) { | |
752 | /* Convert to Minutes-Seconds-Frames */ | |
753 | addr >>= 2; /* Convert to 2048-byte frames */ | |
754 | addr += 2*75; /* Lead-in occupies 2 seconds */ | |
755 | dest[3] = addr % 75; /* Frames */ | |
756 | addr /= 75; | |
757 | dest[2] = addr % 60; /* Seconds */ | |
758 | addr /= 60; | |
759 | dest[1] = addr; /* Minutes */ | |
760 | dest[0] = 0; /* Reserved */ | |
761 | } else { | |
762 | /* Absolute sector */ | |
763 | put_unaligned_be32(addr, dest); | |
764 | } | |
765 | } | |
93f93740 MN |
766 | |
767 | ||
768 | /*-------------------------------------------------------------------------*/ | |
769 | ||
770 | ||
771 | static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr, | |
772 | char *buf) | |
773 | { | |
774 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); | |
775 | ||
776 | return sprintf(buf, "%d\n", fsg_lun_is_open(curlun) | |
777 | ? curlun->ro | |
778 | : curlun->initially_ro); | |
779 | } | |
780 | ||
a93917d3 AS |
781 | static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr, |
782 | char *buf) | |
783 | { | |
784 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); | |
785 | ||
786 | return sprintf(buf, "%u\n", curlun->nofua); | |
787 | } | |
788 | ||
93f93740 MN |
789 | static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr, |
790 | char *buf) | |
791 | { | |
792 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); | |
793 | struct rw_semaphore *filesem = dev_get_drvdata(dev); | |
794 | char *p; | |
795 | ssize_t rc; | |
796 | ||
797 | down_read(filesem); | |
d26a6aa0 | 798 | if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */ |
93f93740 MN |
799 | p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1); |
800 | if (IS_ERR(p)) | |
801 | rc = PTR_ERR(p); | |
802 | else { | |
803 | rc = strlen(p); | |
804 | memmove(buf, p, rc); | |
d26a6aa0 | 805 | buf[rc] = '\n'; /* Add a newline */ |
93f93740 MN |
806 | buf[++rc] = 0; |
807 | } | |
d26a6aa0 | 808 | } else { /* No file, return 0 bytes */ |
93f93740 MN |
809 | *buf = 0; |
810 | rc = 0; | |
811 | } | |
812 | up_read(filesem); | |
813 | return rc; | |
814 | } | |
815 | ||
816 | ||
817 | static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr, | |
818 | const char *buf, size_t count) | |
819 | { | |
fd4477b0 | 820 | ssize_t rc; |
93f93740 MN |
821 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); |
822 | struct rw_semaphore *filesem = dev_get_drvdata(dev); | |
db8fa285 | 823 | unsigned ro; |
93f93740 | 824 | |
db8fa285 MN |
825 | rc = kstrtouint(buf, 2, &ro); |
826 | if (rc) | |
827 | return rc; | |
93f93740 | 828 | |
d0893264 MN |
829 | /* |
830 | * Allow the write-enable status to change only while the | |
831 | * backing file is closed. | |
832 | */ | |
93f93740 MN |
833 | down_read(filesem); |
834 | if (fsg_lun_is_open(curlun)) { | |
835 | LDBG(curlun, "read-only status change prevented\n"); | |
836 | rc = -EBUSY; | |
837 | } else { | |
8156d158 AS |
838 | curlun->ro = ro; |
839 | curlun->initially_ro = ro; | |
93f93740 | 840 | LDBG(curlun, "read-only status set to %d\n", curlun->ro); |
fd4477b0 | 841 | rc = count; |
93f93740 MN |
842 | } |
843 | up_read(filesem); | |
844 | return rc; | |
845 | } | |
846 | ||
a93917d3 AS |
847 | static ssize_t fsg_store_nofua(struct device *dev, |
848 | struct device_attribute *attr, | |
849 | const char *buf, size_t count) | |
850 | { | |
851 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); | |
db8fa285 MN |
852 | unsigned nofua; |
853 | int ret; | |
a93917d3 | 854 | |
db8fa285 MN |
855 | ret = kstrtouint(buf, 2, &nofua); |
856 | if (ret) | |
857 | return ret; | |
a93917d3 AS |
858 | |
859 | /* Sync data when switching from async mode to sync */ | |
860 | if (!nofua && curlun->nofua) | |
861 | fsg_lun_fsync_sub(curlun); | |
862 | ||
863 | curlun->nofua = nofua; | |
864 | ||
865 | return count; | |
866 | } | |
867 | ||
93f93740 MN |
868 | static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr, |
869 | const char *buf, size_t count) | |
870 | { | |
871 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); | |
872 | struct rw_semaphore *filesem = dev_get_drvdata(dev); | |
873 | int rc = 0; | |
874 | ||
875 | if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) { | |
876 | LDBG(curlun, "eject attempt prevented\n"); | |
d26a6aa0 | 877 | return -EBUSY; /* "Door is locked" */ |
93f93740 MN |
878 | } |
879 | ||
880 | /* Remove a trailing newline */ | |
881 | if (count > 0 && buf[count-1] == '\n') | |
d26a6aa0 | 882 | ((char *) buf)[count-1] = 0; /* Ugh! */ |
93f93740 MN |
883 | |
884 | /* Eject current medium */ | |
885 | down_write(filesem); | |
886 | if (fsg_lun_is_open(curlun)) { | |
887 | fsg_lun_close(curlun); | |
888 | curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT; | |
889 | } | |
890 | ||
891 | /* Load new medium */ | |
892 | if (count > 0 && buf[0]) { | |
893 | rc = fsg_lun_open(curlun, buf); | |
894 | if (rc == 0) | |
895 | curlun->unit_attention_data = | |
896 | SS_NOT_READY_TO_READY_TRANSITION; | |
897 | } | |
898 | up_write(filesem); | |
899 | return (rc < 0 ? rc : count); | |
900 | } |