blob: fa86fdaafc1d15eb17eef412dee8df52705f2860 [file] [log] [blame]
Michal Nazarewiczb6058d02009-10-28 16:57:14 +01001/*
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
9
10
11/*-------------------------------------------------------------------------*/
12
13
14#ifndef DEBUG
15#undef VERBOSE_DEBUG
16#undef DUMP_MSGS
17#endif /* !DEBUG */
18
19#ifdef VERBOSE_DEBUG
20#define VLDBG LDBG
21#else
22#define VLDBG(lun, fmt, args...) do { } while (0)
23#endif /* VERBOSE_DEBUG */
24
25#define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
26#define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
27#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
28#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
29
30#define DBG(d, fmt, args...) dev_dbg (&(d)->gadget->dev, fmt, ## args)
31#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev, fmt, ## args)
32#define ERROR(d, fmt, args...) dev_err (&(d)->gadget->dev, fmt, ## args)
33#define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev, fmt, ## args)
34#define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev, fmt, ## args)
35
36
37
38#ifdef DUMP_MSGS
39
40# define dump_msg(fsg, /* const char * */ label, \
41 /* const u8 * */ buf, /* unsigned */ length) do { \
42 if (length < 512) { \
43 DBG(fsg, "%s, length %u:\n", label, length); \
44 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
45 16, 1, buf, length, 0); \
46 } \
47} while (0)
48
49# define dump_cdb(fsg) do { } while (0)
50
51#else
52
53# define dump_msg(fsg, /* const char * */ label, \
54 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
55
56# ifdef VERBOSE_DEBUG
57
58#define dump_cdb(fsg) \
59 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
60 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
61
62# else
63
64# define dump_cdb(fsg) do { } while (0)
65
66# endif /* VERBOSE_DEBUG */
67
68#endif /* DUMP_MSGS */
69
70
71
72
73
74/*-------------------------------------------------------------------------*/
75
76/* SCSI device types */
77#define TYPE_DISK 0x00
78#define TYPE_CDROM 0x05
79
80/* USB protocol value = the transport method */
81#define USB_PR_CBI 0x00 // Control/Bulk/Interrupt
82#define USB_PR_CB 0x01 // Control/Bulk w/o interrupt
83#define USB_PR_BULK 0x50 // Bulk-only
84
85/* USB subclass value = the protocol encapsulation */
86#define USB_SC_RBC 0x01 // Reduced Block Commands (flash)
87#define USB_SC_8020 0x02 // SFF-8020i, MMC-2, ATAPI (CD-ROM)
88#define USB_SC_QIC 0x03 // QIC-157 (tape)
89#define USB_SC_UFI 0x04 // UFI (floppy)
90#define USB_SC_8070 0x05 // SFF-8070i (removable)
91#define USB_SC_SCSI 0x06 // Transparent SCSI
92
93/* Bulk-only data structures */
94
95/* Command Block Wrapper */
96struct bulk_cb_wrap {
97 __le32 Signature; // Contains 'USBC'
98 u32 Tag; // Unique per command id
99 __le32 DataTransferLength; // Size of the data
100 u8 Flags; // Direction in bit 7
101 u8 Lun; // LUN (normally 0)
102 u8 Length; // Of the CDB, <= MAX_COMMAND_SIZE
103 u8 CDB[16]; // Command Data Block
104};
105
106#define USB_BULK_CB_WRAP_LEN 31
107#define USB_BULK_CB_SIG 0x43425355 // Spells out USBC
108#define USB_BULK_IN_FLAG 0x80
109
110/* Command Status Wrapper */
111struct bulk_cs_wrap {
112 __le32 Signature; // Should = 'USBS'
113 u32 Tag; // Same as original command
114 __le32 Residue; // Amount not transferred
115 u8 Status; // See below
116};
117
118#define USB_BULK_CS_WRAP_LEN 13
119#define USB_BULK_CS_SIG 0x53425355 // Spells out 'USBS'
120#define USB_STATUS_PASS 0
121#define USB_STATUS_FAIL 1
122#define USB_STATUS_PHASE_ERROR 2
123
124/* Bulk-only class specific requests */
125#define USB_BULK_RESET_REQUEST 0xff
126#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
127
128
129/* CBI Interrupt data structure */
130struct interrupt_data {
131 u8 bType;
132 u8 bValue;
133};
134
135#define CBI_INTERRUPT_DATA_LEN 2
136
137/* CBI Accept Device-Specific Command request */
138#define USB_CBI_ADSC_REQUEST 0x00
139
140
141#define MAX_COMMAND_SIZE 16 // Length of a SCSI Command Data Block
142
143/* SCSI commands that we recognize */
144#define SC_FORMAT_UNIT 0x04
145#define SC_INQUIRY 0x12
146#define SC_MODE_SELECT_6 0x15
147#define SC_MODE_SELECT_10 0x55
148#define SC_MODE_SENSE_6 0x1a
149#define SC_MODE_SENSE_10 0x5a
150#define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
151#define SC_READ_6 0x08
152#define SC_READ_10 0x28
153#define SC_READ_12 0xa8
154#define SC_READ_CAPACITY 0x25
155#define SC_READ_FORMAT_CAPACITIES 0x23
156#define SC_READ_HEADER 0x44
157#define SC_READ_TOC 0x43
158#define SC_RELEASE 0x17
159#define SC_REQUEST_SENSE 0x03
160#define SC_RESERVE 0x16
161#define SC_SEND_DIAGNOSTIC 0x1d
162#define SC_START_STOP_UNIT 0x1b
163#define SC_SYNCHRONIZE_CACHE 0x35
164#define SC_TEST_UNIT_READY 0x00
165#define SC_VERIFY 0x2f
166#define SC_WRITE_6 0x0a
167#define SC_WRITE_10 0x2a
168#define SC_WRITE_12 0xaa
169
170/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
171#define SS_NO_SENSE 0
172#define SS_COMMUNICATION_FAILURE 0x040800
173#define SS_INVALID_COMMAND 0x052000
174#define SS_INVALID_FIELD_IN_CDB 0x052400
175#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
176#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
177#define SS_MEDIUM_NOT_PRESENT 0x023a00
178#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
179#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
180#define SS_RESET_OCCURRED 0x062900
181#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
182#define SS_UNRECOVERED_READ_ERROR 0x031100
183#define SS_WRITE_ERROR 0x030c02
184#define SS_WRITE_PROTECTED 0x072700
185
186#define SK(x) ((u8) ((x) >> 16)) // Sense Key byte, etc.
187#define ASC(x) ((u8) ((x) >> 8))
188#define ASCQ(x) ((u8) (x))
189
190
191/*-------------------------------------------------------------------------*/
192
193
194struct lun {
195 struct file *filp;
196 loff_t file_length;
197 loff_t num_sectors;
198
199 unsigned int ro : 1;
200 unsigned int prevent_medium_removal : 1;
201 unsigned int registered : 1;
202 unsigned int info_valid : 1;
203
204 u32 sense_data;
205 u32 sense_data_info;
206 u32 unit_attention_data;
207
208 struct device dev;
209};
210
211#define backing_file_is_open(curlun) ((curlun)->filp != NULL)
212
213static struct lun *dev_to_lun(struct device *dev)
214{
215 return container_of(dev, struct lun, dev);
216}
217
218
219/* Big enough to hold our biggest descriptor */
220#define EP0_BUFSIZE 256
221#define DELAYED_STATUS (EP0_BUFSIZE + 999) // An impossibly large value
222
223/* Number of buffers we will use. 2 is enough for double-buffering */
224#define NUM_BUFFERS 2
225
226enum fsg_buffer_state {
227 BUF_STATE_EMPTY = 0,
228 BUF_STATE_FULL,
229 BUF_STATE_BUSY
230};
231
232struct fsg_buffhd {
233 void *buf;
234 enum fsg_buffer_state state;
235 struct fsg_buffhd *next;
236
237 /* The NetChip 2280 is faster, and handles some protocol faults
238 * better, if we don't submit any short bulk-out read requests.
239 * So we will record the intended request length here. */
240 unsigned int bulk_out_intended_length;
241
242 struct usb_request *inreq;
243 int inreq_busy;
244 struct usb_request *outreq;
245 int outreq_busy;
246};
247
248enum fsg_state {
249 FSG_STATE_COMMAND_PHASE = -10, // This one isn't used anywhere
250 FSG_STATE_DATA_PHASE,
251 FSG_STATE_STATUS_PHASE,
252
253 FSG_STATE_IDLE = 0,
254 FSG_STATE_ABORT_BULK_OUT,
255 FSG_STATE_RESET,
256 FSG_STATE_INTERFACE_CHANGE,
257 FSG_STATE_CONFIG_CHANGE,
258 FSG_STATE_DISCONNECT,
259 FSG_STATE_EXIT,
260 FSG_STATE_TERMINATED
261};
262
263enum data_direction {
264 DATA_DIR_UNKNOWN = 0,
265 DATA_DIR_FROM_HOST,
266 DATA_DIR_TO_HOST,
267 DATA_DIR_NONE
268};
269
270
271/*-------------------------------------------------------------------------*/
272
273
274static inline u32 get_unaligned_be24(u8 *buf)
275{
276 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
277}
278
279
280/*-------------------------------------------------------------------------*/
281
282
283#define STRING_MANUFACTURER 1
284#define STRING_PRODUCT 2
285#define STRING_SERIAL 3
286#define STRING_CONFIG 4
287#define STRING_INTERFACE 5
288
289
290static struct usb_otg_descriptor
291otg_desc = {
292 .bLength = sizeof(otg_desc),
293 .bDescriptorType = USB_DT_OTG,
294
295 .bmAttributes = USB_OTG_SRP,
296};
297
298/* There is only one interface. */
299
300static struct usb_interface_descriptor
301intf_desc = {
302 .bLength = sizeof intf_desc,
303 .bDescriptorType = USB_DT_INTERFACE,
304
305 .bNumEndpoints = 2, // Adjusted during fsg_bind()
306 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
307 .bInterfaceSubClass = USB_SC_SCSI, // Adjusted during fsg_bind()
308 .bInterfaceProtocol = USB_PR_BULK, // Adjusted during fsg_bind()
309 .iInterface = STRING_INTERFACE,
310};
311
312/* Three full-speed endpoint descriptors: bulk-in, bulk-out,
313 * and interrupt-in. */
314
315static struct usb_endpoint_descriptor
316fs_bulk_in_desc = {
317 .bLength = USB_DT_ENDPOINT_SIZE,
318 .bDescriptorType = USB_DT_ENDPOINT,
319
320 .bEndpointAddress = USB_DIR_IN,
321 .bmAttributes = USB_ENDPOINT_XFER_BULK,
322 /* wMaxPacketSize set by autoconfiguration */
323};
324
325static struct usb_endpoint_descriptor
326fs_bulk_out_desc = {
327 .bLength = USB_DT_ENDPOINT_SIZE,
328 .bDescriptorType = USB_DT_ENDPOINT,
329
330 .bEndpointAddress = USB_DIR_OUT,
331 .bmAttributes = USB_ENDPOINT_XFER_BULK,
332 /* wMaxPacketSize set by autoconfiguration */
333};
334
335static struct usb_endpoint_descriptor
336fs_intr_in_desc = {
337 .bLength = USB_DT_ENDPOINT_SIZE,
338 .bDescriptorType = USB_DT_ENDPOINT,
339
340 .bEndpointAddress = USB_DIR_IN,
341 .bmAttributes = USB_ENDPOINT_XFER_INT,
342 .wMaxPacketSize = cpu_to_le16(2),
343 .bInterval = 32, // frames -> 32 ms
344};
345
346static const struct usb_descriptor_header *fs_function[] = {
347 (struct usb_descriptor_header *) &otg_desc,
348 (struct usb_descriptor_header *) &intf_desc,
349 (struct usb_descriptor_header *) &fs_bulk_in_desc,
350 (struct usb_descriptor_header *) &fs_bulk_out_desc,
351 (struct usb_descriptor_header *) &fs_intr_in_desc,
352 NULL,
353};
354#define FS_FUNCTION_PRE_EP_ENTRIES 2
355
356
357/*
358 * USB 2.0 devices need to expose both high speed and full speed
359 * descriptors, unless they only run at full speed.
360 *
361 * That means alternate endpoint descriptors (bigger packets)
362 * and a "device qualifier" ... plus more construction options
363 * for the config descriptor.
364 */
365static struct usb_endpoint_descriptor
366hs_bulk_in_desc = {
367 .bLength = USB_DT_ENDPOINT_SIZE,
368 .bDescriptorType = USB_DT_ENDPOINT,
369
370 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
371 .bmAttributes = USB_ENDPOINT_XFER_BULK,
372 .wMaxPacketSize = cpu_to_le16(512),
373};
374
375static struct usb_endpoint_descriptor
376hs_bulk_out_desc = {
377 .bLength = USB_DT_ENDPOINT_SIZE,
378 .bDescriptorType = USB_DT_ENDPOINT,
379
380 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
381 .bmAttributes = USB_ENDPOINT_XFER_BULK,
382 .wMaxPacketSize = cpu_to_le16(512),
383 .bInterval = 1, // NAK every 1 uframe
384};
385
386static struct usb_endpoint_descriptor
387hs_intr_in_desc = {
388 .bLength = USB_DT_ENDPOINT_SIZE,
389 .bDescriptorType = USB_DT_ENDPOINT,
390
391 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
392 .bmAttributes = USB_ENDPOINT_XFER_INT,
393 .wMaxPacketSize = cpu_to_le16(2),
394 .bInterval = 9, // 2**(9-1) = 256 uframes -> 32 ms
395};
396
397static const struct usb_descriptor_header *hs_function[] = {
398 (struct usb_descriptor_header *) &otg_desc,
399 (struct usb_descriptor_header *) &intf_desc,
400 (struct usb_descriptor_header *) &hs_bulk_in_desc,
401 (struct usb_descriptor_header *) &hs_bulk_out_desc,
402 (struct usb_descriptor_header *) &hs_intr_in_desc,
403 NULL,
404};
405#define HS_FUNCTION_PRE_EP_ENTRIES 2
406
407/* Maxpacket and other transfer characteristics vary by speed. */
408static struct usb_endpoint_descriptor *
409ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
410 struct usb_endpoint_descriptor *hs)
411{
412 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
413 return hs;
414 return fs;
415}
416
417
418/* The CBI specification limits the serial string to 12 uppercase hexadecimal
419 * characters. */
420static char manufacturer[64];
421static char serial[13];
422
423/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
424static struct usb_string strings[] = {
425 {STRING_MANUFACTURER, manufacturer},
426 {STRING_PRODUCT, longname},
427 {STRING_SERIAL, serial},
428 {STRING_CONFIG, "Self-powered"},
429 {STRING_INTERFACE, "Mass Storage"},
430 {}
431};
432
433static struct usb_gadget_strings stringtab = {
434 .language = 0x0409, // en-us
435 .strings = strings,
436};
437
438
439 /*-------------------------------------------------------------------------*/
440
441/* If the next two routines are called while the gadget is registered,
442 * the caller must own fsg->filesem for writing. */
443
444static int open_backing_file(struct lun *curlun, const char *filename)
445{
446 int ro;
447 struct file *filp = NULL;
448 int rc = -EINVAL;
449 struct inode *inode = NULL;
450 loff_t size;
451 loff_t num_sectors;
452 loff_t min_sectors;
453
454 /* R/W if we can, R/O if we must */
455 ro = curlun->ro;
456 if (!ro) {
457 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
458 if (-EROFS == PTR_ERR(filp))
459 ro = 1;
460 }
461 if (ro)
462 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
463 if (IS_ERR(filp)) {
464 LINFO(curlun, "unable to open backing file: %s\n", filename);
465 return PTR_ERR(filp);
466 }
467
468 if (!(filp->f_mode & FMODE_WRITE))
469 ro = 1;
470
471 if (filp->f_path.dentry)
472 inode = filp->f_path.dentry->d_inode;
473 if (inode && S_ISBLK(inode->i_mode)) {
474 if (bdev_read_only(inode->i_bdev))
475 ro = 1;
476 } else if (!inode || !S_ISREG(inode->i_mode)) {
477 LINFO(curlun, "invalid file type: %s\n", filename);
478 goto out;
479 }
480
481 /* If we can't read the file, it's no good.
482 * If we can't write the file, use it read-only. */
483 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
484 LINFO(curlun, "file not readable: %s\n", filename);
485 goto out;
486 }
487 if (!(filp->f_op->write || filp->f_op->aio_write))
488 ro = 1;
489
490 size = i_size_read(inode->i_mapping->host);
491 if (size < 0) {
492 LINFO(curlun, "unable to find file size: %s\n", filename);
493 rc = (int) size;
494 goto out;
495 }
496 num_sectors = size >> 9; // File size in 512-byte blocks
497 min_sectors = 1;
498 if (mod_data.cdrom) {
499 num_sectors &= ~3; // Reduce to a multiple of 2048
500 min_sectors = 300*4; // Smallest track is 300 frames
501 if (num_sectors >= 256*60*75*4) {
502 num_sectors = (256*60*75 - 1) * 4;
503 LINFO(curlun, "file too big: %s\n", filename);
504 LINFO(curlun, "using only first %d blocks\n",
505 (int) num_sectors);
506 }
507 }
508 if (num_sectors < min_sectors) {
509 LINFO(curlun, "file too small: %s\n", filename);
510 rc = -ETOOSMALL;
511 goto out;
512 }
513
514 get_file(filp);
515 curlun->ro = ro;
516 curlun->filp = filp;
517 curlun->file_length = size;
518 curlun->num_sectors = num_sectors;
519 LDBG(curlun, "open backing file: %s\n", filename);
520 rc = 0;
521
522out:
523 filp_close(filp, current->files);
524 return rc;
525}
526
527
528static void close_backing_file(struct lun *curlun)
529{
530 if (curlun->filp) {
531 LDBG(curlun, "close backing file\n");
532 fput(curlun->filp);
533 curlun->filp = NULL;
534 }
535}
536
537
538/*-------------------------------------------------------------------------*/
539
540/* Sync the file data, don't bother with the metadata.
541 * This code was copied from fs/buffer.c:sys_fdatasync(). */
542static int fsync_sub(struct lun *curlun)
543{
544 struct file *filp = curlun->filp;
545
546 if (curlun->ro || !filp)
547 return 0;
548 return vfs_fsync(filp, filp->f_path.dentry, 1);
549}
550
551static void store_cdrom_address(u8 *dest, int msf, u32 addr)
552{
553 if (msf) {
554 /* Convert to Minutes-Seconds-Frames */
555 addr >>= 2; /* Convert to 2048-byte frames */
556 addr += 2*75; /* Lead-in occupies 2 seconds */
557 dest[3] = addr % 75; /* Frames */
558 addr /= 75;
559 dest[2] = addr % 60; /* Seconds */
560 addr /= 60;
561 dest[1] = addr; /* Minutes */
562 dest[0] = 0; /* Reserved */
563 } else {
564 /* Absolute sector */
565 put_unaligned_be32(addr, dest);
566 }
567}