1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59#define FSG_VENDOR_ID 0x0525
60#define FSG_PRODUCT_ID 0xa4a5
61
62
63
64#ifndef DEBUG
65#undef VERBOSE_DEBUG
66#undef DUMP_MSGS
67#endif
68
69#ifdef VERBOSE_DEBUG
70#define VLDBG LDBG
71#else
72#define VLDBG(lun, fmt, args...) do { } while (0)
73#endif
74
75
76
77
78
79
80
81
82#define LDBG(lun, fmt, args...) do { } while (0)
83#define LERROR(lun, fmt, args...) do { } while (0)
84#define LWARN(lun, fmt, args...) do { } while (0)
85#define LINFO(lun, fmt, args...) do { } while (0)
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109#define DBG(d, fmt, args...) debug(fmt , ## args)
110#define VDBG(d, fmt, args...) debug(fmt , ## args)
111
112
113
114
115
116
117#define ERROR(d, fmt, args...) do { } while (0)
118#define WARNING(d, fmt, args...) do { } while (0)
119#define INFO(d, fmt, args...) do { } while (0)
120
121#ifdef DUMP_MSGS
122
123
124# define dump_msg(fsg, label, buf, length) do { \
125 if (length < 512) { \
126 DBG(fsg, "%s, length %u:\n", label, length); \
127 print_hex_dump("", DUMP_PREFIX_OFFSET, \
128 16, 1, buf, length, 0); \
129 } \
130} while (0)
131
132# define dump_cdb(fsg) do { } while (0)
133
134#else
135
136# define dump_msg(fsg, label, \
137 buf, length) do { } while (0)
138
139# ifdef VERBOSE_DEBUG
140
141# define dump_cdb(fsg) \
142 print_hex_dump("SCSI CDB: ", DUMP_PREFIX_NONE, \
143 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
144
145# else
146
147# define dump_cdb(fsg) do { } while (0)
148
149# endif
150
151#endif
152
153
154
155
156#define TYPE_DISK 0x00
157#define TYPE_CDROM 0x05
158
159
160#define USB_PR_CBI 0x00
161#define USB_PR_CB 0x01
162#define USB_PR_BULK 0x50
163
164
165#define USB_SC_RBC 0x01
166#define USB_SC_8020 0x02
167#define USB_SC_QIC 0x03
168#define USB_SC_UFI 0x04
169#define USB_SC_8070 0x05
170#define USB_SC_SCSI 0x06
171
172
173
174
175struct fsg_bulk_cb_wrap {
176 __le32 Signature;
177 u32 Tag;
178 __le32 DataTransferLength;
179 u8 Flags;
180 u8 Lun;
181 u8 Length;
182 u8 CDB[16];
183};
184
185#define USB_BULK_CB_WRAP_LEN 31
186#define USB_BULK_CB_SIG 0x43425355
187#define USB_BULK_IN_FLAG 0x80
188
189
190struct bulk_cs_wrap {
191 __le32 Signature;
192 u32 Tag;
193 __le32 Residue;
194 u8 Status;
195};
196
197#define USB_BULK_CS_WRAP_LEN 13
198#define USB_BULK_CS_SIG 0x53425355
199#define USB_STATUS_PASS 0
200#define USB_STATUS_FAIL 1
201#define USB_STATUS_PHASE_ERROR 2
202
203
204#define USB_BULK_RESET_REQUEST 0xff
205#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
206
207
208struct interrupt_data {
209 u8 bType;
210 u8 bValue;
211};
212
213#define CBI_INTERRUPT_DATA_LEN 2
214
215
216#define USB_CBI_ADSC_REQUEST 0x00
217
218
219#define MAX_COMMAND_SIZE 16
220
221
222#define SC_FORMAT_UNIT 0x04
223#define SC_INQUIRY 0x12
224#define SC_MODE_SELECT_6 0x15
225#define SC_MODE_SELECT_10 0x55
226#define SC_MODE_SENSE_6 0x1a
227#define SC_MODE_SENSE_10 0x5a
228#define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
229#define SC_READ_6 0x08
230#define SC_READ_10 0x28
231#define SC_READ_12 0xa8
232#define SC_READ_CAPACITY 0x25
233#define SC_READ_FORMAT_CAPACITIES 0x23
234#define SC_READ_HEADER 0x44
235#define SC_READ_TOC 0x43
236#define SC_RELEASE 0x17
237#define SC_REQUEST_SENSE 0x03
238#define SC_RESERVE 0x16
239#define SC_SEND_DIAGNOSTIC 0x1d
240#define SC_START_STOP_UNIT 0x1b
241#define SC_SYNCHRONIZE_CACHE 0x35
242#define SC_TEST_UNIT_READY 0x00
243#define SC_VERIFY 0x2f
244#define SC_WRITE_6 0x0a
245#define SC_WRITE_10 0x2a
246#define SC_WRITE_12 0xaa
247
248
249#define SS_NO_SENSE 0
250#define SS_COMMUNICATION_FAILURE 0x040800
251#define SS_INVALID_COMMAND 0x052000
252#define SS_INVALID_FIELD_IN_CDB 0x052400
253#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
254#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
255#define SS_MEDIUM_NOT_PRESENT 0x023a00
256#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
257#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
258#define SS_RESET_OCCURRED 0x062900
259#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
260#define SS_UNRECOVERED_READ_ERROR 0x031100
261#define SS_WRITE_ERROR 0x030c02
262#define SS_WRITE_PROTECTED 0x072700
263
264#define SK(x) ((u8) ((x) >> 16))
265#define ASC(x) ((u8) ((x) >> 8))
266#define ASCQ(x) ((u8) (x))
267
268struct device_attribute { int i; };
269#define ETOOSMALL 525
270
271#include <log.h>
272#include <usb_mass_storage.h>
273#include <dm/device_compat.h>
274
275
276
277struct fsg_lun {
278 loff_t file_length;
279 loff_t num_sectors;
280
281 unsigned int initially_ro:1;
282 unsigned int ro:1;
283 unsigned int removable:1;
284 unsigned int cdrom:1;
285 unsigned int prevent_medium_removal:1;
286 unsigned int registered:1;
287 unsigned int info_valid:1;
288 unsigned int nofua:1;
289
290 u32 sense_data;
291 u32 sense_data_info;
292 u32 unit_attention_data;
293
294 struct device dev;
295};
296
297#define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
298#if 0
299static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
300{
301 return container_of(dev, struct fsg_lun, dev);
302}
303#endif
304
305
306#define EP0_BUFSIZE 256
307#define DELAYED_STATUS (EP0_BUFSIZE + 999)
308
309
310#define FSG_NUM_BUFFERS 2
311
312
313#define FSG_BUFLEN ((u32)131072)
314
315
316#define FSG_MAX_LUNS 8
317
318enum fsg_buffer_state {
319 BUF_STATE_EMPTY = 0,
320 BUF_STATE_FULL,
321 BUF_STATE_BUSY
322};
323
324struct fsg_buffhd {
325#ifdef FSG_BUFFHD_STATIC_BUFFER
326 char buf[FSG_BUFLEN];
327#else
328 void *buf;
329#endif
330 enum fsg_buffer_state state;
331 struct fsg_buffhd *next;
332
333
334
335
336
337
338 unsigned int bulk_out_intended_length;
339
340 struct usb_request *inreq;
341 int inreq_busy;
342 struct usb_request *outreq;
343 int outreq_busy;
344};
345
346enum fsg_state {
347
348 FSG_STATE_COMMAND_PHASE = -10,
349 FSG_STATE_DATA_PHASE,
350 FSG_STATE_STATUS_PHASE,
351
352 FSG_STATE_IDLE = 0,
353 FSG_STATE_ABORT_BULK_OUT,
354 FSG_STATE_RESET,
355 FSG_STATE_INTERFACE_CHANGE,
356 FSG_STATE_CONFIG_CHANGE,
357 FSG_STATE_DISCONNECT,
358 FSG_STATE_EXIT,
359 FSG_STATE_TERMINATED
360};
361
362enum data_direction {
363 DATA_DIR_UNKNOWN = 0,
364 DATA_DIR_FROM_HOST,
365 DATA_DIR_TO_HOST,
366 DATA_DIR_NONE
367};
368
369
370
371static inline u32 get_unaligned_be24(u8 *buf)
372{
373 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
374}
375
376
377
378enum {
379#ifndef FSG_NO_DEVICE_STRINGS
380 FSG_STRING_MANUFACTURER = 1,
381 FSG_STRING_PRODUCT,
382 FSG_STRING_SERIAL,
383 FSG_STRING_CONFIG,
384#endif
385 FSG_STRING_INTERFACE
386};
387
388#ifndef FSG_NO_OTG
389static struct usb_otg_descriptor
390fsg_otg_desc = {
391 .bLength = sizeof fsg_otg_desc,
392 .bDescriptorType = USB_DT_OTG,
393
394 .bmAttributes = USB_OTG_SRP,
395};
396#endif
397
398
399
400static struct usb_interface_descriptor
401fsg_intf_desc = {
402 .bLength = sizeof fsg_intf_desc,
403 .bDescriptorType = USB_DT_INTERFACE,
404
405 .bNumEndpoints = 2,
406 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
407 .bInterfaceSubClass = USB_SC_SCSI,
408 .bInterfaceProtocol = USB_PR_BULK,
409 .iInterface = FSG_STRING_INTERFACE,
410};
411
412
413
414
415
416
417static struct usb_endpoint_descriptor
418fsg_fs_bulk_in_desc = {
419 .bLength = USB_DT_ENDPOINT_SIZE,
420 .bDescriptorType = USB_DT_ENDPOINT,
421
422 .bEndpointAddress = USB_DIR_IN,
423 .bmAttributes = USB_ENDPOINT_XFER_BULK,
424
425};
426
427static struct usb_endpoint_descriptor
428fsg_fs_bulk_out_desc = {
429 .bLength = USB_DT_ENDPOINT_SIZE,
430 .bDescriptorType = USB_DT_ENDPOINT,
431
432 .bEndpointAddress = USB_DIR_OUT,
433 .bmAttributes = USB_ENDPOINT_XFER_BULK,
434
435};
436
437#ifndef FSG_NO_INTR_EP
438
439static struct usb_endpoint_descriptor
440fsg_fs_intr_in_desc = {
441 .bLength = USB_DT_ENDPOINT_SIZE,
442 .bDescriptorType = USB_DT_ENDPOINT,
443
444 .bEndpointAddress = USB_DIR_IN,
445 .bmAttributes = USB_ENDPOINT_XFER_INT,
446 .wMaxPacketSize = cpu_to_le16(2),
447 .bInterval = 32,
448};
449
450#ifndef FSG_NO_OTG
451# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
452#else
453# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
454#endif
455
456#endif
457
458static struct usb_descriptor_header *fsg_fs_function[] = {
459#ifndef FSG_NO_OTG
460 (struct usb_descriptor_header *) &fsg_otg_desc,
461#endif
462 (struct usb_descriptor_header *) &fsg_intf_desc,
463 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
464 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
465#ifndef FSG_NO_INTR_EP
466 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
467#endif
468 NULL,
469};
470
471
472
473
474
475
476
477
478
479static struct usb_endpoint_descriptor
480fsg_hs_bulk_in_desc = {
481 .bLength = USB_DT_ENDPOINT_SIZE,
482 .bDescriptorType = USB_DT_ENDPOINT,
483
484
485 .bmAttributes = USB_ENDPOINT_XFER_BULK,
486 .wMaxPacketSize = cpu_to_le16(512),
487};
488
489static struct usb_endpoint_descriptor
490fsg_hs_bulk_out_desc = {
491 .bLength = USB_DT_ENDPOINT_SIZE,
492 .bDescriptorType = USB_DT_ENDPOINT,
493
494
495 .bmAttributes = USB_ENDPOINT_XFER_BULK,
496 .wMaxPacketSize = cpu_to_le16(512),
497 .bInterval = 1,
498};
499
500#ifndef FSG_NO_INTR_EP
501
502static struct usb_endpoint_descriptor
503fsg_hs_intr_in_desc = {
504 .bLength = USB_DT_ENDPOINT_SIZE,
505 .bDescriptorType = USB_DT_ENDPOINT,
506
507
508 .bmAttributes = USB_ENDPOINT_XFER_INT,
509 .wMaxPacketSize = cpu_to_le16(2),
510 .bInterval = 9,
511};
512
513#ifndef FSG_NO_OTG
514# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
515#else
516# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
517#endif
518
519#endif
520
521static struct usb_descriptor_header *fsg_hs_function[] = {
522#ifndef FSG_NO_OTG
523 (struct usb_descriptor_header *) &fsg_otg_desc,
524#endif
525 (struct usb_descriptor_header *) &fsg_intf_desc,
526 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
527 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
528#ifndef FSG_NO_INTR_EP
529 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
530#endif
531 NULL,
532};
533
534
535static struct usb_endpoint_descriptor *
536fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
537 struct usb_endpoint_descriptor *hs)
538{
539 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
540 return hs;
541 return fs;
542}
543
544
545static struct usb_string fsg_strings[] = {
546#ifndef FSG_NO_DEVICE_STRINGS
547 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
548 {FSG_STRING_PRODUCT, fsg_string_product},
549 {FSG_STRING_SERIAL, fsg_string_serial},
550 {FSG_STRING_CONFIG, fsg_string_config},
551#endif
552 {FSG_STRING_INTERFACE, fsg_string_interface},
553 {}
554};
555
556static struct usb_gadget_strings fsg_stringtab = {
557 .language = 0x0409,
558 .strings = fsg_strings,
559};
560
561
562
563
564
565
566
567
568static int fsg_lun_open(struct fsg_lun *curlun, unsigned int num_sectors,
569 const char *filename)
570{
571 int ro;
572
573
574 ro = curlun->initially_ro;
575
576 curlun->ro = ro;
577 curlun->file_length = num_sectors << 9;
578 curlun->num_sectors = num_sectors;
579 debug("open backing file: %s\n", filename);
580
581 return 0;
582}
583
584static void fsg_lun_close(struct fsg_lun *curlun)
585{
586}
587
588
589
590
591
592
593
594static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
595{
596 return 0;
597}
598
599static void store_cdrom_address(u8 *dest, int msf, u32 addr)
600{
601 if (msf) {
602
603 addr >>= 2;
604 addr += 2*75;
605 dest[3] = addr % 75;
606 addr /= 75;
607 dest[2] = addr % 60;
608 addr /= 60;
609 dest[1] = addr;
610 dest[0] = 0;
611 } else {
612
613 put_unaligned_be32(addr, dest);
614 }
615}
616
617
618