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
60#define FSG_VENDOR_ID 0x0525
61#define FSG_PRODUCT_ID 0xa4a5
62
63
64
65#ifndef DEBUG
66#undef VERBOSE_DEBUG
67#undef DUMP_MSGS
68#endif
69
70#ifdef VERBOSE_DEBUG
71#define VLDBG LDBG
72#else
73#define VLDBG(lun, fmt, args...) do { } while (0)
74#endif
75
76
77
78
79
80
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110#define DBG(d, fmt, args...) debug(fmt , ## args)
111#define VDBG(d, fmt, args...) debug(fmt , ## args)
112
113
114
115
116
117
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
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(KERN_DEBUG, "", 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, label, \
138 buf, length) do { } while (0)
139
140# ifdef VERBOSE_DEBUG
141
142# define dump_cdb(fsg) \
143 print_hex_dump(KERN_DEBUG, "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
151
152#endif
153
154
155
156
157#define TYPE_DISK 0x00
158#define TYPE_CDROM 0x05
159
160
161#define USB_PR_CBI 0x00
162#define USB_PR_CB 0x01
163#define USB_PR_BULK 0x50
164
165
166#define USB_SC_RBC 0x01
167#define USB_SC_8020 0x02
168#define USB_SC_QIC 0x03
169#define USB_SC_UFI 0x04
170#define USB_SC_8070 0x05
171#define USB_SC_SCSI 0x06
172
173
174
175
176struct fsg_bulk_cb_wrap {
177 __le32 Signature;
178 u32 Tag;
179 __le32 DataTransferLength;
180 u8 Flags;
181 u8 Lun;
182 u8 Length;
183 u8 CDB[16];
184};
185
186#define USB_BULK_CB_WRAP_LEN 31
187#define USB_BULK_CB_SIG 0x43425355
188#define USB_BULK_IN_FLAG 0x80
189
190
191struct bulk_cs_wrap {
192 __le32 Signature;
193 u32 Tag;
194 __le32 Residue;
195 u8 Status;
196};
197
198#define USB_BULK_CS_WRAP_LEN 13
199#define USB_BULK_CS_SIG 0x53425355
200#define USB_STATUS_PASS 0
201#define USB_STATUS_FAIL 1
202#define USB_STATUS_PHASE_ERROR 2
203
204
205#define USB_BULK_RESET_REQUEST 0xff
206#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
207
208
209struct interrupt_data {
210 u8 bType;
211 u8 bValue;
212};
213
214#define CBI_INTERRUPT_DATA_LEN 2
215
216
217#define USB_CBI_ADSC_REQUEST 0x00
218
219
220#define MAX_COMMAND_SIZE 16
221
222
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
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))
266#define ASC(x) ((u8) ((x) >> 8))
267#define ASCQ(x) ((u8) (x))
268
269struct device_attribute { int i; };
270#define ETOOSMALL 525
271
272#include <usb_mass_storage.h>
273
274
275
276struct 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
298static 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
305#define EP0_BUFSIZE 256
306#define DELAYED_STATUS (EP0_BUFSIZE + 999)
307
308
309#define FSG_NUM_BUFFERS 2
310
311
312#define FSG_BUFLEN ((u32)16384)
313
314
315#define FSG_MAX_LUNS 8
316
317enum fsg_buffer_state {
318 BUF_STATE_EMPTY = 0,
319 BUF_STATE_FULL,
320 BUF_STATE_BUSY
321};
322
323struct fsg_buffhd {
324#ifdef FSG_BUFFHD_STATIC_BUFFER
325 char buf[FSG_BUFLEN];
326#else
327 void *buf;
328#endif
329 enum fsg_buffer_state state;
330 struct fsg_buffhd *next;
331
332
333
334
335
336
337 unsigned int bulk_out_intended_length;
338
339 struct usb_request *inreq;
340 int inreq_busy;
341 struct usb_request *outreq;
342 int outreq_busy;
343};
344
345enum fsg_state {
346
347 FSG_STATE_COMMAND_PHASE = -10,
348 FSG_STATE_DATA_PHASE,
349 FSG_STATE_STATUS_PHASE,
350
351 FSG_STATE_IDLE = 0,
352 FSG_STATE_ABORT_BULK_OUT,
353 FSG_STATE_RESET,
354 FSG_STATE_INTERFACE_CHANGE,
355 FSG_STATE_CONFIG_CHANGE,
356 FSG_STATE_DISCONNECT,
357 FSG_STATE_EXIT,
358 FSG_STATE_TERMINATED
359};
360
361enum data_direction {
362 DATA_DIR_UNKNOWN = 0,
363 DATA_DIR_FROM_HOST,
364 DATA_DIR_TO_HOST,
365 DATA_DIR_NONE
366};
367
368
369
370static inline u32 get_unaligned_be24(u8 *buf)
371{
372 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
373}
374
375
376
377enum {
378#ifndef FSG_NO_DEVICE_STRINGS
379 FSG_STRING_MANUFACTURER = 1,
380 FSG_STRING_PRODUCT,
381 FSG_STRING_SERIAL,
382 FSG_STRING_CONFIG,
383#endif
384 FSG_STRING_INTERFACE
385};
386
387#ifndef FSG_NO_OTG
388static struct usb_otg_descriptor
389fsg_otg_desc = {
390 .bLength = sizeof fsg_otg_desc,
391 .bDescriptorType = USB_DT_OTG,
392
393 .bmAttributes = USB_OTG_SRP,
394};
395#endif
396
397
398
399static struct usb_interface_descriptor
400fsg_intf_desc = {
401 .bLength = sizeof fsg_intf_desc,
402 .bDescriptorType = USB_DT_INTERFACE,
403
404 .bNumEndpoints = 2,
405 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
406 .bInterfaceSubClass = USB_SC_SCSI,
407 .bInterfaceProtocol = USB_PR_BULK,
408 .iInterface = FSG_STRING_INTERFACE,
409};
410
411
412
413
414
415
416static struct usb_endpoint_descriptor
417fsg_fs_bulk_in_desc = {
418 .bLength = USB_DT_ENDPOINT_SIZE,
419 .bDescriptorType = USB_DT_ENDPOINT,
420
421 .bEndpointAddress = USB_DIR_IN,
422 .bmAttributes = USB_ENDPOINT_XFER_BULK,
423
424};
425
426static struct usb_endpoint_descriptor
427fsg_fs_bulk_out_desc = {
428 .bLength = USB_DT_ENDPOINT_SIZE,
429 .bDescriptorType = USB_DT_ENDPOINT,
430
431 .bEndpointAddress = USB_DIR_OUT,
432 .bmAttributes = USB_ENDPOINT_XFER_BULK,
433
434};
435
436#ifndef FSG_NO_INTR_EP
437
438static struct usb_endpoint_descriptor
439fsg_fs_intr_in_desc = {
440 .bLength = USB_DT_ENDPOINT_SIZE,
441 .bDescriptorType = USB_DT_ENDPOINT,
442
443 .bEndpointAddress = USB_DIR_IN,
444 .bmAttributes = USB_ENDPOINT_XFER_INT,
445 .wMaxPacketSize = cpu_to_le16(2),
446 .bInterval = 32,
447};
448
449#ifndef FSG_NO_OTG
450# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
451#else
452# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
453#endif
454
455#endif
456
457static struct usb_descriptor_header *fsg_fs_function[] = {
458#ifndef FSG_NO_OTG
459 (struct usb_descriptor_header *) &fsg_otg_desc,
460#endif
461 (struct usb_descriptor_header *) &fsg_intf_desc,
462 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
463 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
464#ifndef FSG_NO_INTR_EP
465 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
466#endif
467 NULL,
468};
469
470
471
472
473
474
475
476
477
478static struct usb_endpoint_descriptor
479fsg_hs_bulk_in_desc = {
480 .bLength = USB_DT_ENDPOINT_SIZE,
481 .bDescriptorType = USB_DT_ENDPOINT,
482
483
484 .bmAttributes = USB_ENDPOINT_XFER_BULK,
485 .wMaxPacketSize = cpu_to_le16(512),
486};
487
488static struct usb_endpoint_descriptor
489fsg_hs_bulk_out_desc = {
490 .bLength = USB_DT_ENDPOINT_SIZE,
491 .bDescriptorType = USB_DT_ENDPOINT,
492
493
494 .bmAttributes = USB_ENDPOINT_XFER_BULK,
495 .wMaxPacketSize = cpu_to_le16(512),
496 .bInterval = 1,
497};
498
499#ifndef FSG_NO_INTR_EP
500
501static struct usb_endpoint_descriptor
502fsg_hs_intr_in_desc = {
503 .bLength = USB_DT_ENDPOINT_SIZE,
504 .bDescriptorType = USB_DT_ENDPOINT,
505
506
507 .bmAttributes = USB_ENDPOINT_XFER_INT,
508 .wMaxPacketSize = cpu_to_le16(2),
509 .bInterval = 9,
510};
511
512#ifndef FSG_NO_OTG
513# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
514#else
515# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
516#endif
517
518#endif
519
520static struct usb_descriptor_header *fsg_hs_function[] = {
521#ifndef FSG_NO_OTG
522 (struct usb_descriptor_header *) &fsg_otg_desc,
523#endif
524 (struct usb_descriptor_header *) &fsg_intf_desc,
525 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
526 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
527#ifndef FSG_NO_INTR_EP
528 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
529#endif
530 NULL,
531};
532
533
534static struct usb_endpoint_descriptor *
535fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
536 struct usb_endpoint_descriptor *hs)
537{
538 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
539 return hs;
540 return fs;
541}
542
543
544static struct usb_string fsg_strings[] = {
545#ifndef FSG_NO_DEVICE_STRINGS
546 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
547 {FSG_STRING_PRODUCT, fsg_string_product},
548 {FSG_STRING_SERIAL, fsg_string_serial},
549 {FSG_STRING_CONFIG, fsg_string_config},
550#endif
551 {FSG_STRING_INTERFACE, fsg_string_interface},
552 {}
553};
554
555static struct usb_gadget_strings fsg_stringtab = {
556 .language = 0x0409,
557 .strings = fsg_strings,
558};
559
560
561
562
563
564
565
566
567static int fsg_lun_open(struct fsg_lun *curlun, unsigned int num_sectors,
568 const char *filename)
569{
570 int ro;
571
572
573 ro = curlun->initially_ro;
574
575 curlun->ro = ro;
576 curlun->file_length = num_sectors << 9;
577 curlun->num_sectors = num_sectors;
578 debug("open backing file: %s\n", filename);
579
580 return 0;
581}
582
583static void fsg_lun_close(struct fsg_lun *curlun)
584{
585}
586
587
588
589
590
591
592
593static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
594{
595 return 0;
596}
597
598static void store_cdrom_address(u8 *dest, int msf, u32 addr)
599{
600 if (msf) {
601
602 addr >>= 2;
603 addr += 2*75;
604 dest[3] = addr % 75;
605 addr /= 75;
606 dest[2] = addr % 60;
607 addr /= 60;
608 dest[1] = addr;
609 dest[0] = 0;
610 } else {
611
612 put_unaligned_be32(addr, dest);
613 }
614}
615
616
617