2 * storage_common.c -- Common definitions for mass storage functionality
4 * Copyright (C) 2003-2008 Alan Stern
5 * Copyeight (C) 2009 Samsung Electronics
6 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
11 /*-------------------------------------------------------------------------*/
22 #define VLDBG(lun, fmt, args...) do { } while (0)
23 #endif /* VERBOSE_DEBUG */
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)
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)
40 # define dump_msg(fsg, /* const char * */ label, \
41 /* const u8 * */ buf, /* unsigned */ length) do { \
43 DBG(fsg, "%s, length %u:\n", label, length); \
44 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
45 16, 1, buf, length, 0); \
49 # define dump_cdb(fsg) do { } while (0)
53 # define dump_msg(fsg, /* const char * */ label, \
54 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
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) \
64 # define dump_cdb(fsg) do { } while (0)
66 # endif /* VERBOSE_DEBUG */
68 #endif /* DUMP_MSGS */
74 /*-------------------------------------------------------------------------*/
76 /* SCSI device types */
77 #define TYPE_DISK 0x00
78 #define TYPE_CDROM 0x05
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
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
93 /* Bulk-only data structures */
95 /* Command Block Wrapper */
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
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
110 /* Command Status Wrapper */
111 struct 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
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
124 /* Bulk-only class specific requests */
125 #define USB_BULK_RESET_REQUEST 0xff
126 #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
129 /* CBI Interrupt data structure */
130 struct interrupt_data {
135 #define CBI_INTERRUPT_DATA_LEN 2
137 /* CBI Accept Device-Specific Command request */
138 #define USB_CBI_ADSC_REQUEST 0x00
141 #define MAX_COMMAND_SIZE 16 // Length of a SCSI Command Data Block
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
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
186 #define SK(x) ((u8) ((x) >> 16)) // Sense Key byte, etc.
187 #define ASC(x) ((u8) ((x) >> 8))
188 #define ASCQ(x) ((u8) (x))
191 /*-------------------------------------------------------------------------*/
200 unsigned int prevent_medium_removal : 1;
201 unsigned int registered : 1;
202 unsigned int info_valid : 1;
206 u32 unit_attention_data;
211 #define backing_file_is_open(curlun) ((curlun)->filp != NULL)
213 static struct lun *dev_to_lun(struct device *dev)
215 return container_of(dev, struct lun, dev);
219 /* Big enough to hold our biggest descriptor */
220 #define EP0_BUFSIZE 256
221 #define DELAYED_STATUS (EP0_BUFSIZE + 999) // An impossibly large value
223 /* Number of buffers we will use. 2 is enough for double-buffering */
224 #define NUM_BUFFERS 2
226 enum fsg_buffer_state {
234 enum fsg_buffer_state state;
235 struct fsg_buffhd *next;
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;
242 struct usb_request *inreq;
244 struct usb_request *outreq;
249 FSG_STATE_COMMAND_PHASE = -10, // This one isn't used anywhere
250 FSG_STATE_DATA_PHASE,
251 FSG_STATE_STATUS_PHASE,
254 FSG_STATE_ABORT_BULK_OUT,
256 FSG_STATE_INTERFACE_CHANGE,
257 FSG_STATE_CONFIG_CHANGE,
258 FSG_STATE_DISCONNECT,
263 enum data_direction {
264 DATA_DIR_UNKNOWN = 0,
271 /*-------------------------------------------------------------------------*/
274 static inline u32 get_unaligned_be24(u8 *buf)
276 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
280 /*-------------------------------------------------------------------------*/
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
290 static struct usb_otg_descriptor
292 .bLength = sizeof(otg_desc),
293 .bDescriptorType = USB_DT_OTG,
295 .bmAttributes = USB_OTG_SRP,
298 /* There is only one interface. */
300 static struct usb_interface_descriptor
302 .bLength = sizeof intf_desc,
303 .bDescriptorType = USB_DT_INTERFACE,
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,
312 /* Three full-speed endpoint descriptors: bulk-in, bulk-out,
313 * and interrupt-in. */
315 static struct usb_endpoint_descriptor
317 .bLength = USB_DT_ENDPOINT_SIZE,
318 .bDescriptorType = USB_DT_ENDPOINT,
320 .bEndpointAddress = USB_DIR_IN,
321 .bmAttributes = USB_ENDPOINT_XFER_BULK,
322 /* wMaxPacketSize set by autoconfiguration */
325 static struct usb_endpoint_descriptor
327 .bLength = USB_DT_ENDPOINT_SIZE,
328 .bDescriptorType = USB_DT_ENDPOINT,
330 .bEndpointAddress = USB_DIR_OUT,
331 .bmAttributes = USB_ENDPOINT_XFER_BULK,
332 /* wMaxPacketSize set by autoconfiguration */
335 static struct usb_endpoint_descriptor
337 .bLength = USB_DT_ENDPOINT_SIZE,
338 .bDescriptorType = USB_DT_ENDPOINT,
340 .bEndpointAddress = USB_DIR_IN,
341 .bmAttributes = USB_ENDPOINT_XFER_INT,
342 .wMaxPacketSize = cpu_to_le16(2),
343 .bInterval = 32, // frames -> 32 ms
346 static 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,
354 #define FS_FUNCTION_PRE_EP_ENTRIES 2
358 * USB 2.0 devices need to expose both high speed and full speed
359 * descriptors, unless they only run at full speed.
361 * That means alternate endpoint descriptors (bigger packets)
362 * and a "device qualifier" ... plus more construction options
363 * for the config descriptor.
365 static struct usb_endpoint_descriptor
367 .bLength = USB_DT_ENDPOINT_SIZE,
368 .bDescriptorType = USB_DT_ENDPOINT,
370 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
371 .bmAttributes = USB_ENDPOINT_XFER_BULK,
372 .wMaxPacketSize = cpu_to_le16(512),
375 static struct usb_endpoint_descriptor
377 .bLength = USB_DT_ENDPOINT_SIZE,
378 .bDescriptorType = USB_DT_ENDPOINT,
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
386 static struct usb_endpoint_descriptor
388 .bLength = USB_DT_ENDPOINT_SIZE,
389 .bDescriptorType = USB_DT_ENDPOINT,
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
397 static 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,
405 #define HS_FUNCTION_PRE_EP_ENTRIES 2
407 /* Maxpacket and other transfer characteristics vary by speed. */
408 static struct usb_endpoint_descriptor *
409 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
410 struct usb_endpoint_descriptor *hs)
412 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
418 /* The CBI specification limits the serial string to 12 uppercase hexadecimal
420 static char manufacturer[64];
421 static char serial[13];
423 /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
424 static 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"},
433 static struct usb_gadget_strings stringtab = {
434 .language = 0x0409, // en-us
439 /*-------------------------------------------------------------------------*/
441 /* If the next two routines are called while the gadget is registered,
442 * the caller must own fsg->filesem for writing. */
444 static int open_backing_file(struct lun *curlun, const char *filename)
447 struct file *filp = NULL;
449 struct inode *inode = NULL;
454 /* R/W if we can, R/O if we must */
457 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
458 if (-EROFS == PTR_ERR(filp))
462 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
464 LINFO(curlun, "unable to open backing file: %s\n", filename);
465 return PTR_ERR(filp);
468 if (!(filp->f_mode & FMODE_WRITE))
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))
476 } else if (!inode || !S_ISREG(inode->i_mode)) {
477 LINFO(curlun, "invalid file type: %s\n", filename);
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);
487 if (!(filp->f_op->write || filp->f_op->aio_write))
490 size = i_size_read(inode->i_mapping->host);
492 LINFO(curlun, "unable to find file size: %s\n", filename);
496 num_sectors = size >> 9; // File size in 512-byte blocks
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",
508 if (num_sectors < min_sectors) {
509 LINFO(curlun, "file too small: %s\n", filename);
517 curlun->file_length = size;
518 curlun->num_sectors = num_sectors;
519 LDBG(curlun, "open backing file: %s\n", filename);
523 filp_close(filp, current->files);
528 static void close_backing_file(struct lun *curlun)
531 LDBG(curlun, "close backing file\n");
538 /*-------------------------------------------------------------------------*/
540 /* Sync the file data, don't bother with the metadata.
541 * This code was copied from fs/buffer.c:sys_fdatasync(). */
542 static int fsync_sub(struct lun *curlun)
544 struct file *filp = curlun->filp;
546 if (curlun->ro || !filp)
548 return vfs_fsync(filp, filp->f_path.dentry, 1);
551 static void store_cdrom_address(u8 *dest, int msf, u32 addr)
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 */
559 dest[2] = addr % 60; /* Seconds */
561 dest[1] = addr; /* Minutes */
562 dest[0] = 0; /* Reserved */
564 /* Absolute sector */
565 put_unaligned_be32(addr, dest);