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
6 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
7 *
8 * Ported to u-boot:
9 * Andrzej Pietrasiewicz <andrzej.p@samsung.com>
10 *
11 * Code refactoring & cleanup:
12 * Łukasz Majewski <l.majewski@samsung.com>
13 *
14 * SPDX-License-Identifier: GPL-2.0+
15 */
16
17
18 /*
19 * This file requires the following identifiers used in USB strings to
20 * be defined (each of type pointer to char):
21 * - fsg_string_manufacturer -- name of the manufacturer
22 * - fsg_string_product -- name of the product
23 * - fsg_string_serial -- product's serial
24 * - fsg_string_config -- name of the configuration
25 * - fsg_string_interface -- name of the interface
26 * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
27 * macro is defined prior to including this file.
28 */
29
30 /*
31 * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
32 * fsg_hs_intr_in_desc objects as well as
33 * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
34 * macros are not defined.
35 *
36 * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
37 * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
38 * defined (as well as corresponding entries in string tables are
39 * missing) and FSG_STRING_INTERFACE has value of zero.
40 *
41 * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
42 */
43
44 /*
45 * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
46 * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
47 * characters rather then a pointer to void.
48 */
49
50
51 /* #include <asm/unaligned.h> */
52
53
54 /*
55 * Thanks to NetChip Technologies for donating this product ID.
56 *
57 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
58 * Instead: allocate your own, using normal USB-IF procedures.
59 */
60 #define FSG_VENDOR_ID 0x0525 /* NetChip */
61 #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
62
63 /*-------------------------------------------------------------------------*/
64
65 #ifndef DEBUG
66 #undef VERBOSE_DEBUG
67 #undef DUMP_MSGS
68 #endif /* !DEBUG */
69
70 #ifdef VERBOSE_DEBUG
71 #define VLDBG LDBG
72 #else
73 #define VLDBG(lun, fmt, args...) do { } while (0)
74 #endif /* VERBOSE_DEBUG */
75
76 /*
77 #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
78 #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
79 #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
80 #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
81 */
82
83 #define LDBG(lun, fmt, args...) do { } while (0)
84 #define LERROR(lun, fmt, args...) do { } while (0)
85 #define LWARN(lun, fmt, args...) do { } while (0)
86 #define LINFO(lun, fmt, args...) do { } while (0)
87
88 /*
89 * Keep those macros in sync with those in
90 * include/linux/usb/composite.h or else GCC will complain. If they
91 * are identical (the same names of arguments, white spaces in the
92 * same places) GCC will allow redefinition otherwise (even if some
93 * white space is removed or added) warning will be issued.
94 *
95 * Those macros are needed here because File Storage Gadget does not
96 * include the composite.h header. For composite gadgets those macros
97 * are redundant since composite.h is included any way.
98 *
99 * One could check whether those macros are already defined (which
100 * would indicate composite.h had been included) or not (which would
101 * indicate we were in FSG) but this is not done because a warning is
102 * desired if definitions here differ from the ones in composite.h.
103 *
104 * We want the definitions to match and be the same in File Storage
105 * Gadget as well as Mass Storage Function (and so composite gadgets
106 * using MSF). If someone changes them in composite.h it will produce
107 * a warning in this file when building MSF.
108 */
109
110 #define DBG(d, fmt, args...) debug(fmt , ## args)
111 #define VDBG(d, fmt, args...) debug(fmt , ## args)
112 /* #define ERROR(d, fmt, args...) printf(fmt , ## args) */
113 /* #define WARNING(d, fmt, args...) printf(fmt , ## args) */
114 /* #define INFO(d, fmt, args...) printf(fmt , ## args) */
115
116 /* #define DBG(d, fmt, args...) do { } while (0) */
117 /* #define VDBG(d, fmt, args...) do { } while (0) */
118 #define ERROR(d, fmt, args...) do { } while (0)
119 #define WARNING(d, fmt, args...) do { } while (0)
120 #define INFO(d, fmt, args...) do { } while (0)
121
122 #ifdef DUMP_MSGS
123
124 /* dump_msg(fsg, const char * label, const u8 * buf, unsigned length); */
125 # define dump_msg(fsg, label, buf, length) do { \
126 if (length < 512) { \
127 DBG(fsg, "%s, length %u:\n", label, length); \
128 print_hex_dump("", DUMP_PREFIX_OFFSET, \
129 16, 1, buf, length, 0); \
130 } \
131 } while (0)
132
133 # define dump_cdb(fsg) do { } while (0)
134
135 #else
136
137 # define dump_msg(fsg, /* const char * */ label, \
138 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
139
140 # ifdef VERBOSE_DEBUG
141
142 # define dump_cdb(fsg) \
143 print_hex_dump("SCSI CDB: ", DUMP_PREFIX_NONE, \
144 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
145
146 # else
147
148 # define dump_cdb(fsg) do { } while (0)
149
150 # endif /* VERBOSE_DEBUG */
151
152 #endif /* DUMP_MSGS */
153
154 /*-------------------------------------------------------------------------*/
155
156 /* SCSI device types */
157 #define TYPE_DISK 0x00
158 #define TYPE_CDROM 0x05
159
160 /* USB protocol value = the transport method */
161 #define USB_PR_CBI 0x00 /* Control/Bulk/Interrupt */
162 #define USB_PR_CB 0x01 /* Control/Bulk w/o interrupt */
163 #define USB_PR_BULK 0x50 /* Bulk-only */
164
165 /* USB subclass value = the protocol encapsulation */
166 #define USB_SC_RBC 0x01 /* Reduced Block Commands (flash) */
167 #define USB_SC_8020 0x02 /* SFF-8020i, MMC-2, ATAPI (CD-ROM) */
168 #define USB_SC_QIC 0x03 /* QIC-157 (tape) */
169 #define USB_SC_UFI 0x04 /* UFI (floppy) */
170 #define USB_SC_8070 0x05 /* SFF-8070i (removable) */
171 #define USB_SC_SCSI 0x06 /* Transparent SCSI */
172
173 /* Bulk-only data structures */
174
175 /* Command Block Wrapper */
176 struct fsg_bulk_cb_wrap {
177 __le32 Signature; /* Contains 'USBC' */
178 u32 Tag; /* Unique per command id */
179 __le32 DataTransferLength; /* Size of the data */
180 u8 Flags; /* Direction in bit 7 */
181 u8 Lun; /* LUN (normally 0) */
182 u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
183 u8 CDB[16]; /* Command Data Block */
184 };
185
186 #define USB_BULK_CB_WRAP_LEN 31
187 #define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
188 #define USB_BULK_IN_FLAG 0x80
189
190 /* Command Status Wrapper */
191 struct bulk_cs_wrap {
192 __le32 Signature; /* Should = 'USBS' */
193 u32 Tag; /* Same as original command */
194 __le32 Residue; /* Amount not transferred */
195 u8 Status; /* See below */
196 };
197
198 #define USB_BULK_CS_WRAP_LEN 13
199 #define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
200 #define USB_STATUS_PASS 0
201 #define USB_STATUS_FAIL 1
202 #define USB_STATUS_PHASE_ERROR 2
203
204 /* Bulk-only class specific requests */
205 #define USB_BULK_RESET_REQUEST 0xff
206 #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
207
208 /* CBI Interrupt data structure */
209 struct interrupt_data {
210 u8 bType;
211 u8 bValue;
212 };
213
214 #define CBI_INTERRUPT_DATA_LEN 2
215
216 /* CBI Accept Device-Specific Command request */
217 #define USB_CBI_ADSC_REQUEST 0x00
218
219 /* Length of a SCSI Command Data Block */
220 #define MAX_COMMAND_SIZE 16
221
222 /* SCSI commands that we recognize */
223 #define SC_FORMAT_UNIT 0x04
224 #define SC_INQUIRY 0x12
225 #define SC_MODE_SELECT_6 0x15
226 #define SC_MODE_SELECT_10 0x55
227 #define SC_MODE_SENSE_6 0x1a
228 #define SC_MODE_SENSE_10 0x5a
229 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
230 #define SC_READ_6 0x08
231 #define SC_READ_10 0x28
232 #define SC_READ_12 0xa8
233 #define SC_READ_CAPACITY 0x25
234 #define SC_READ_FORMAT_CAPACITIES 0x23
235 #define SC_READ_HEADER 0x44
236 #define SC_READ_TOC 0x43
237 #define SC_RELEASE 0x17
238 #define SC_REQUEST_SENSE 0x03
239 #define SC_RESERVE 0x16
240 #define SC_SEND_DIAGNOSTIC 0x1d
241 #define SC_START_STOP_UNIT 0x1b
242 #define SC_SYNCHRONIZE_CACHE 0x35
243 #define SC_TEST_UNIT_READY 0x00
244 #define SC_VERIFY 0x2f
245 #define SC_WRITE_6 0x0a
246 #define SC_WRITE_10 0x2a
247 #define SC_WRITE_12 0xaa
248
249 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
250 #define SS_NO_SENSE 0
251 #define SS_COMMUNICATION_FAILURE 0x040800
252 #define SS_INVALID_COMMAND 0x052000
253 #define SS_INVALID_FIELD_IN_CDB 0x052400
254 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
255 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
256 #define SS_MEDIUM_NOT_PRESENT 0x023a00
257 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
258 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
259 #define SS_RESET_OCCURRED 0x062900
260 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
261 #define SS_UNRECOVERED_READ_ERROR 0x031100
262 #define SS_WRITE_ERROR 0x030c02
263 #define SS_WRITE_PROTECTED 0x072700
264
265 #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
266 #define ASC(x) ((u8) ((x) >> 8))
267 #define ASCQ(x) ((u8) (x))
268
269 struct device_attribute { int i; };
270 #define ETOOSMALL 525
271
272 #include <usb_mass_storage.h>
273
274 /*-------------------------------------------------------------------------*/
275
276 struct fsg_lun {
277 loff_t file_length;
278 loff_t num_sectors;
279
280 unsigned int initially_ro:1;
281 unsigned int ro:1;
282 unsigned int removable:1;
283 unsigned int cdrom:1;
284 unsigned int prevent_medium_removal:1;
285 unsigned int registered:1;
286 unsigned int info_valid:1;
287 unsigned int nofua:1;
288
289 u32 sense_data;
290 u32 sense_data_info;
291 u32 unit_attention_data;
292
293 struct device dev;
294 };
295
296 #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
297 #if 0
298 static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
299 {
300 return container_of(dev, struct fsg_lun, dev);
301 }
302 #endif
303
304 /* Big enough to hold our biggest descriptor */
305 #define EP0_BUFSIZE 256
306 #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
307
308 /* Number of buffers we will use. 2 is enough for double-buffering */
309 #define FSG_NUM_BUFFERS 2
310
311 #if defined(CONFIG_USB_DWC3_GADGET) && defined(ROCKUSB_FSG_BUFLEN)
312 #define FSG_BUFLEN ((u32)ROCKUSB_FSG_BUFLEN)
313 #else
314 /* Default size of buffer length. */
315 #define FSG_BUFLEN ((u32)262144)
316 #endif
317
318 /* Maximal number of LUNs supported in mass storage function */
319 #define FSG_MAX_LUNS 8
320
321 enum fsg_buffer_state {
322 BUF_STATE_EMPTY = 0,
323 BUF_STATE_FULL,
324 BUF_STATE_BUSY
325 };
326
327 struct fsg_buffhd {
328 #ifdef FSG_BUFFHD_STATIC_BUFFER
329 char buf[FSG_BUFLEN];
330 #else
331 void *buf;
332 #endif
333 enum fsg_buffer_state state;
334 struct fsg_buffhd *next;
335
336 /*
337 * The NetChip 2280 is faster, and handles some protocol faults
338 * better, if we don't submit any short bulk-out read requests.
339 * So we will record the intended request length here.
340 */
341 unsigned int bulk_out_intended_length;
342
343 struct usb_request *inreq;
344 int inreq_busy;
345 struct usb_request *outreq;
346 int outreq_busy;
347 };
348
349 enum fsg_state {
350 /* This one isn't used anywhere */
351 FSG_STATE_COMMAND_PHASE = -10,
352 FSG_STATE_DATA_PHASE,
353 FSG_STATE_STATUS_PHASE,
354
355 FSG_STATE_IDLE = 0,
356 FSG_STATE_ABORT_BULK_OUT,
357 FSG_STATE_RESET,
358 FSG_STATE_INTERFACE_CHANGE,
359 FSG_STATE_CONFIG_CHANGE,
360 FSG_STATE_DISCONNECT,
361 FSG_STATE_EXIT,
362 FSG_STATE_TERMINATED
363 };
364
365 enum data_direction {
366 DATA_DIR_UNKNOWN = 0,
367 DATA_DIR_FROM_HOST,
368 DATA_DIR_TO_HOST,
369 DATA_DIR_NONE
370 };
371
372 /*-------------------------------------------------------------------------*/
373
get_unaligned_be24(u8 * buf)374 static inline u32 get_unaligned_be24(u8 *buf)
375 {
376 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
377 }
378
379 /*-------------------------------------------------------------------------*/
380
381 enum {
382 #ifndef FSG_NO_DEVICE_STRINGS
383 FSG_STRING_MANUFACTURER = 1,
384 FSG_STRING_PRODUCT,
385 FSG_STRING_SERIAL,
386 FSG_STRING_CONFIG,
387 #endif
388 FSG_STRING_INTERFACE
389 };
390
391 #ifndef FSG_NO_OTG
392 static struct usb_otg_descriptor
393 fsg_otg_desc = {
394 .bLength = sizeof fsg_otg_desc,
395 .bDescriptorType = USB_DT_OTG,
396
397 .bmAttributes = USB_OTG_SRP,
398 };
399 #endif
400
401 /* There is only one interface. */
402
403 static struct usb_interface_descriptor
404 fsg_intf_desc = {
405 .bLength = sizeof fsg_intf_desc,
406 .bDescriptorType = USB_DT_INTERFACE,
407
408 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
409 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
410 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
411 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
412 .iInterface = FSG_STRING_INTERFACE,
413 };
414
415 /*
416 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
417 * interrupt-in.
418 */
419
420 static struct usb_endpoint_descriptor
421 fsg_fs_bulk_in_desc = {
422 .bLength = USB_DT_ENDPOINT_SIZE,
423 .bDescriptorType = USB_DT_ENDPOINT,
424
425 .bEndpointAddress = USB_DIR_IN,
426 .bmAttributes = USB_ENDPOINT_XFER_BULK,
427 /* wMaxPacketSize set by autoconfiguration */
428 };
429
430 static struct usb_endpoint_descriptor
431 fsg_fs_bulk_out_desc = {
432 .bLength = USB_DT_ENDPOINT_SIZE,
433 .bDescriptorType = USB_DT_ENDPOINT,
434
435 .bEndpointAddress = USB_DIR_OUT,
436 .bmAttributes = USB_ENDPOINT_XFER_BULK,
437 /* wMaxPacketSize set by autoconfiguration */
438 };
439
440 #ifndef FSG_NO_INTR_EP
441
442 static struct usb_endpoint_descriptor
443 fsg_fs_intr_in_desc = {
444 .bLength = USB_DT_ENDPOINT_SIZE,
445 .bDescriptorType = USB_DT_ENDPOINT,
446
447 .bEndpointAddress = USB_DIR_IN,
448 .bmAttributes = USB_ENDPOINT_XFER_INT,
449 .wMaxPacketSize = cpu_to_le16(2),
450 .bInterval = 32, /* frames -> 32 ms */
451 };
452
453 #ifndef FSG_NO_OTG
454 # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
455 #else
456 # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
457 #endif
458
459 #endif
460
461 static struct usb_descriptor_header *fsg_fs_function[] = {
462 #ifndef FSG_NO_OTG
463 (struct usb_descriptor_header *) &fsg_otg_desc,
464 #endif
465 (struct usb_descriptor_header *) &fsg_intf_desc,
466 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
467 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
468 #ifndef FSG_NO_INTR_EP
469 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
470 #endif
471 NULL,
472 };
473
474 /*
475 * USB 2.0 devices need to expose both high speed and full speed
476 * descriptors, unless they only run at full speed.
477 *
478 * That means alternate endpoint descriptors (bigger packets)
479 * and a "device qualifier" ... plus more construction options
480 * for the configuration descriptor.
481 */
482 static struct usb_endpoint_descriptor
483 fsg_hs_bulk_in_desc = {
484 .bLength = USB_DT_ENDPOINT_SIZE,
485 .bDescriptorType = USB_DT_ENDPOINT,
486
487 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
488 .bmAttributes = USB_ENDPOINT_XFER_BULK,
489 .wMaxPacketSize = cpu_to_le16(512),
490 };
491
492 static struct usb_endpoint_descriptor
493 fsg_hs_bulk_out_desc = {
494 .bLength = USB_DT_ENDPOINT_SIZE,
495 .bDescriptorType = USB_DT_ENDPOINT,
496
497 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
498 .bmAttributes = USB_ENDPOINT_XFER_BULK,
499 .wMaxPacketSize = cpu_to_le16(512),
500 .bInterval = 1, /* NAK every 1 uframe */
501 };
502
503 #ifndef FSG_NO_INTR_EP
504
505 static struct usb_endpoint_descriptor
506 fsg_hs_intr_in_desc = {
507 .bLength = USB_DT_ENDPOINT_SIZE,
508 .bDescriptorType = USB_DT_ENDPOINT,
509
510 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
511 .bmAttributes = USB_ENDPOINT_XFER_INT,
512 .wMaxPacketSize = cpu_to_le16(2),
513 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
514 };
515
516 #ifndef FSG_NO_OTG
517 # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
518 #else
519 # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
520 #endif
521
522 #endif
523
524 static struct usb_descriptor_header *fsg_hs_function[] = {
525 #ifndef FSG_NO_OTG
526 (struct usb_descriptor_header *) &fsg_otg_desc,
527 #endif
528 (struct usb_descriptor_header *) &fsg_intf_desc,
529 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
530 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
531 #ifndef FSG_NO_INTR_EP
532 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
533 #endif
534 NULL,
535 };
536
537 static struct usb_endpoint_descriptor
538 fsg_ss_bulk_in_desc = {
539 .bLength = USB_DT_ENDPOINT_SIZE,
540 .bDescriptorType = USB_DT_ENDPOINT,
541
542 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
543 .bmAttributes = USB_ENDPOINT_XFER_BULK,
544 .wMaxPacketSize = cpu_to_le16(1024),
545 };
546
547 static struct usb_ss_ep_comp_descriptor
548 fsg_ss_bulk_in_comp_desc = {
549 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
550 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
551 /* .bMaxBurst = DYNAMIC, */
552 };
553
554 static struct usb_endpoint_descriptor
555 fsg_ss_bulk_out_desc = {
556 .bLength = USB_DT_ENDPOINT_SIZE,
557 .bDescriptorType = USB_DT_ENDPOINT,
558
559 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
560 .bmAttributes = USB_ENDPOINT_XFER_BULK,
561 .wMaxPacketSize = cpu_to_le16(1024),
562 };
563
564 static struct usb_ss_ep_comp_descriptor
565 fsg_ss_bulk_out_comp_desc = {
566 .bLength = sizeof(fsg_ss_bulk_out_comp_desc),
567 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
568 /* .bMaxBurst = DYNAMIC, */
569 };
570
571 /* Maxpacket and other transfer characteristics vary by speed. */
572 static struct usb_endpoint_descriptor *
fsg_ep_desc(struct usb_gadget * g,struct usb_endpoint_descriptor * fs,struct usb_endpoint_descriptor * hs,struct usb_endpoint_descriptor * ss,struct usb_ss_ep_comp_descriptor * comp_desc,struct usb_ep * ep)573 fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
574 struct usb_endpoint_descriptor *hs,
575 struct usb_endpoint_descriptor *ss,
576 struct usb_ss_ep_comp_descriptor *comp_desc,
577 struct usb_ep *ep)
578 {
579 struct usb_endpoint_descriptor *speed_desc = NULL;
580
581 /* select desired speed */
582 switch (g->speed) {
583 case USB_SPEED_SUPER:
584 if (gadget_is_superspeed(g)) {
585 speed_desc = ss;
586 ep->comp_desc = comp_desc;
587 break;
588 }
589 /* else: Fall trough */
590 case USB_SPEED_HIGH:
591 if (gadget_is_dualspeed(g)) {
592 speed_desc = hs;
593 break;
594 }
595 /* else: fall through */
596 default:
597 speed_desc = fs;
598 }
599
600 /*
601 * Config the ep maxpacket according to the right descriptors
602 * for a given endpoint.
603 */
604 ep->maxpacket = usb_endpoint_maxp(speed_desc) & USB_ENDPOINT_MAXP_MASK;
605
606 return speed_desc;
607 }
608
609 /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
610 static struct usb_string fsg_strings[] = {
611 #ifndef FSG_NO_DEVICE_STRINGS
612 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
613 {FSG_STRING_PRODUCT, fsg_string_product},
614 {FSG_STRING_SERIAL, fsg_string_serial},
615 {FSG_STRING_CONFIG, fsg_string_config},
616 #endif
617 {FSG_STRING_INTERFACE, fsg_string_interface},
618 {}
619 };
620
621 static struct usb_gadget_strings fsg_stringtab = {
622 .language = 0x0409, /* en-us */
623 .strings = fsg_strings,
624 };
625
626 /*-------------------------------------------------------------------------*/
627
628 /*
629 * If the next two routines are called while the gadget is registered,
630 * the caller must own fsg->filesem for writing.
631 */
632
fsg_lun_open(struct fsg_lun * curlun,unsigned int num_sectors,const char * filename)633 static int fsg_lun_open(struct fsg_lun *curlun, unsigned int num_sectors,
634 const char *filename)
635 {
636 int ro;
637
638 /* R/W if we can, R/O if we must */
639 ro = curlun->initially_ro;
640
641 curlun->ro = ro;
642 curlun->file_length = num_sectors << 9;
643 curlun->num_sectors = num_sectors;
644 debug("open backing file: %s\n", filename);
645
646 return 0;
647 }
648
fsg_lun_close(struct fsg_lun * curlun)649 static void fsg_lun_close(struct fsg_lun *curlun)
650 {
651 }
652
653 /*-------------------------------------------------------------------------*/
654
655 /*
656 * Sync the file data, don't bother with the metadata.
657 * This code was copied from fs/buffer.c:sys_fdatasync().
658 */
fsg_lun_fsync_sub(struct fsg_lun * curlun)659 static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
660 {
661 return 0;
662 }
663
store_cdrom_address(u8 * dest,int msf,u32 addr)664 static void store_cdrom_address(u8 *dest, int msf, u32 addr)
665 {
666 if (msf) {
667 /* Convert to Minutes-Seconds-Frames */
668 addr >>= 2; /* Convert to 2048-byte frames */
669 addr += 2*75; /* Lead-in occupies 2 seconds */
670 dest[3] = addr % 75; /* Frames */
671 addr /= 75;
672 dest[2] = addr % 60; /* Seconds */
673 addr /= 60;
674 dest[1] = addr; /* Minutes */
675 dest[0] = 0; /* Reserved */
676 } else {
677 /* Absolute sector */
678 put_unaligned_be32(addr, dest);
679 }
680 }
681
682 /*-------------------------------------------------------------------------*/
683