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 (mina86@mina86.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
15 * This file requires the following identifiers used in USB strings to
16 * be defined (each of type pointer to char):
17 * - fsg_string_interface -- name of the interface
21 * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
22 * sets the number of pipeline buffers (length of the fsg_buffhd array).
23 * The valid range of num_buffers is: num >= 2 && num <= 4.
27 #include <linux/usb/storage.h>
28 #include <scsi/scsi.h>
29 #include <asm/unaligned.h>
33 * Thanks to NetChip Technologies for donating this product ID.
35 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
36 * Instead: allocate your own, using normal USB-IF procedures.
38 #define FSG_VENDOR_ID 0x0525 /* NetChip */
39 #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
42 /*-------------------------------------------------------------------------*/
53 #define VLDBG(lun, fmt, args...) do { } while (0)
54 #endif /* VERBOSE_DEBUG */
56 #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
57 #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
58 #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
59 #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
64 # define dump_msg(fsg, /* const char * */ label, \
65 /* const u8 * */ buf, /* unsigned */ length) do { \
67 DBG(fsg, "%s, length %u:\n", label, length); \
68 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
69 16, 1, buf, length, 0); \
73 # define dump_cdb(fsg) do { } while (0)
77 # define dump_msg(fsg, /* const char * */ label, \
78 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
82 # define dump_cdb(fsg) \
83 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
84 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
88 # define dump_cdb(fsg) do { } while (0)
90 # endif /* VERBOSE_DEBUG */
92 #endif /* DUMP_MSGS */
94 /*-------------------------------------------------------------------------*/
96 /* Length of a SCSI Command Data Block */
97 #define MAX_COMMAND_SIZE 16
99 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
100 #define SS_NO_SENSE 0
101 #define SS_COMMUNICATION_FAILURE 0x040800
102 #define SS_INVALID_COMMAND 0x052000
103 #define SS_INVALID_FIELD_IN_CDB 0x052400
104 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
105 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
106 #define SS_MEDIUM_NOT_PRESENT 0x023a00
107 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
108 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
109 #define SS_RESET_OCCURRED 0x062900
110 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
111 #define SS_UNRECOVERED_READ_ERROR 0x031100
112 #define SS_WRITE_ERROR 0x030c02
113 #define SS_WRITE_PROTECTED 0x072700
115 #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
116 #define ASC(x) ((u8) ((x) >> 8))
117 #define ASCQ(x) ((u8) (x))
120 /*-------------------------------------------------------------------------*/
128 unsigned int initially_ro
:1;
130 unsigned int removable
:1;
131 unsigned int cdrom
:1;
132 unsigned int prevent_medium_removal
:1;
133 unsigned int registered
:1;
134 unsigned int info_valid
:1;
135 unsigned int nofua
:1;
139 u32 unit_attention_data
;
141 unsigned int blkbits
; /* Bits of logical block size of bound block device */
142 unsigned int blksize
; /* logical block size of bound block device */
146 static inline bool fsg_lun_is_open(struct fsg_lun
*curlun
)
148 return curlun
->filp
!= NULL
;
151 static inline struct fsg_lun
*fsg_lun_from_dev(struct device
*dev
)
153 return container_of(dev
, struct fsg_lun
, dev
);
157 /* Big enough to hold our biggest descriptor */
158 #define EP0_BUFSIZE 256
159 #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
161 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
163 static unsigned int fsg_num_buffers
= CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
;
164 module_param_named(num_buffers
, fsg_num_buffers
, uint
, S_IRUGO
);
165 MODULE_PARM_DESC(num_buffers
, "Number of pipeline buffers");
170 * Number of buffers we will use.
171 * 2 is usually enough for good buffering pipeline
173 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
175 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
177 /* check if fsg_num_buffers is within a valid range */
178 static inline int fsg_num_buffers_validate(void)
180 if (fsg_num_buffers
>= 2 && fsg_num_buffers
<= 4)
182 pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
183 fsg_num_buffers
, 2 ,4);
187 /* Default size of buffer length. */
188 #define FSG_BUFLEN ((u32)16384)
190 /* Maximal number of LUNs supported in mass storage function */
191 #define FSG_MAX_LUNS 8
193 enum fsg_buffer_state
{
201 enum fsg_buffer_state state
;
202 struct fsg_buffhd
*next
;
205 * The NetChip 2280 is faster, and handles some protocol faults
206 * better, if we don't submit any short bulk-out read requests.
207 * So we will record the intended request length here.
209 unsigned int bulk_out_intended_length
;
211 struct usb_request
*inreq
;
213 struct usb_request
*outreq
;
218 /* This one isn't used anywhere */
219 FSG_STATE_COMMAND_PHASE
= -10,
220 FSG_STATE_DATA_PHASE
,
221 FSG_STATE_STATUS_PHASE
,
224 FSG_STATE_ABORT_BULK_OUT
,
226 FSG_STATE_INTERFACE_CHANGE
,
227 FSG_STATE_CONFIG_CHANGE
,
228 FSG_STATE_DISCONNECT
,
233 enum data_direction
{
234 DATA_DIR_UNKNOWN
= 0,
241 /*-------------------------------------------------------------------------*/
244 static inline u32
get_unaligned_be24(u8
*buf
)
246 return 0xffffff & (u32
) get_unaligned_be32(buf
- 1);
250 /*-------------------------------------------------------------------------*/
258 /* There is only one interface. */
260 static struct usb_interface_descriptor
262 .bLength
= sizeof fsg_intf_desc
,
263 .bDescriptorType
= USB_DT_INTERFACE
,
265 .bNumEndpoints
= 2, /* Adjusted during fsg_bind() */
266 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
267 .bInterfaceSubClass
= USB_SC_SCSI
, /* Adjusted during fsg_bind() */
268 .bInterfaceProtocol
= USB_PR_BULK
, /* Adjusted during fsg_bind() */
269 .iInterface
= FSG_STRING_INTERFACE
,
273 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
277 static struct usb_endpoint_descriptor
278 fsg_fs_bulk_in_desc
= {
279 .bLength
= USB_DT_ENDPOINT_SIZE
,
280 .bDescriptorType
= USB_DT_ENDPOINT
,
282 .bEndpointAddress
= USB_DIR_IN
,
283 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
284 /* wMaxPacketSize set by autoconfiguration */
287 static struct usb_endpoint_descriptor
288 fsg_fs_bulk_out_desc
= {
289 .bLength
= USB_DT_ENDPOINT_SIZE
,
290 .bDescriptorType
= USB_DT_ENDPOINT
,
292 .bEndpointAddress
= USB_DIR_OUT
,
293 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
294 /* wMaxPacketSize set by autoconfiguration */
297 static struct usb_descriptor_header
*fsg_fs_function
[] = {
298 (struct usb_descriptor_header
*) &fsg_intf_desc
,
299 (struct usb_descriptor_header
*) &fsg_fs_bulk_in_desc
,
300 (struct usb_descriptor_header
*) &fsg_fs_bulk_out_desc
,
306 * USB 2.0 devices need to expose both high speed and full speed
307 * descriptors, unless they only run at full speed.
309 * That means alternate endpoint descriptors (bigger packets)
310 * and a "device qualifier" ... plus more construction options
311 * for the configuration descriptor.
313 static struct usb_endpoint_descriptor
314 fsg_hs_bulk_in_desc
= {
315 .bLength
= USB_DT_ENDPOINT_SIZE
,
316 .bDescriptorType
= USB_DT_ENDPOINT
,
318 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
319 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
320 .wMaxPacketSize
= cpu_to_le16(512),
323 static struct usb_endpoint_descriptor
324 fsg_hs_bulk_out_desc
= {
325 .bLength
= USB_DT_ENDPOINT_SIZE
,
326 .bDescriptorType
= USB_DT_ENDPOINT
,
328 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
329 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
330 .wMaxPacketSize
= cpu_to_le16(512),
331 .bInterval
= 1, /* NAK every 1 uframe */
335 static struct usb_descriptor_header
*fsg_hs_function
[] = {
336 (struct usb_descriptor_header
*) &fsg_intf_desc
,
337 (struct usb_descriptor_header
*) &fsg_hs_bulk_in_desc
,
338 (struct usb_descriptor_header
*) &fsg_hs_bulk_out_desc
,
342 static struct usb_endpoint_descriptor
343 fsg_ss_bulk_in_desc
= {
344 .bLength
= USB_DT_ENDPOINT_SIZE
,
345 .bDescriptorType
= USB_DT_ENDPOINT
,
347 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
348 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
349 .wMaxPacketSize
= cpu_to_le16(1024),
352 static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc
= {
353 .bLength
= sizeof(fsg_ss_bulk_in_comp_desc
),
354 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
356 /*.bMaxBurst = DYNAMIC, */
359 static struct usb_endpoint_descriptor
360 fsg_ss_bulk_out_desc
= {
361 .bLength
= USB_DT_ENDPOINT_SIZE
,
362 .bDescriptorType
= USB_DT_ENDPOINT
,
364 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
365 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
366 .wMaxPacketSize
= cpu_to_le16(1024),
369 static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc
= {
370 .bLength
= sizeof(fsg_ss_bulk_in_comp_desc
),
371 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
373 /*.bMaxBurst = DYNAMIC, */
376 static struct usb_descriptor_header
*fsg_ss_function
[] = {
377 (struct usb_descriptor_header
*) &fsg_intf_desc
,
378 (struct usb_descriptor_header
*) &fsg_ss_bulk_in_desc
,
379 (struct usb_descriptor_header
*) &fsg_ss_bulk_in_comp_desc
,
380 (struct usb_descriptor_header
*) &fsg_ss_bulk_out_desc
,
381 (struct usb_descriptor_header
*) &fsg_ss_bulk_out_comp_desc
,
385 /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
386 static struct usb_string fsg_strings
[] = {
387 {FSG_STRING_INTERFACE
, fsg_string_interface
},
391 static struct usb_gadget_strings fsg_stringtab
= {
392 .language
= 0x0409, /* en-us */
393 .strings
= fsg_strings
,
397 /*-------------------------------------------------------------------------*/
400 * If the next two routines are called while the gadget is registered,
401 * the caller must own fsg->filesem for writing.
404 static void fsg_lun_close(struct fsg_lun
*curlun
)
407 LDBG(curlun
, "close backing file\n");
414 static int fsg_lun_open(struct fsg_lun
*curlun
, const char *filename
)
417 struct file
*filp
= NULL
;
419 struct inode
*inode
= NULL
;
423 unsigned int blkbits
;
424 unsigned int blksize
;
426 /* R/W if we can, R/O if we must */
427 ro
= curlun
->initially_ro
;
429 filp
= filp_open(filename
, O_RDWR
| O_LARGEFILE
, 0);
430 if (PTR_ERR(filp
) == -EROFS
|| PTR_ERR(filp
) == -EACCES
)
434 filp
= filp_open(filename
, O_RDONLY
| O_LARGEFILE
, 0);
436 LINFO(curlun
, "unable to open backing file: %s\n", filename
);
437 return PTR_ERR(filp
);
440 if (!(filp
->f_mode
& FMODE_WRITE
))
443 inode
= file_inode(filp
);
444 if ((!S_ISREG(inode
->i_mode
) && !S_ISBLK(inode
->i_mode
))) {
445 LINFO(curlun
, "invalid file type: %s\n", filename
);
450 * If we can't read the file, it's no good.
451 * If we can't write the file, use it read-only.
453 if (!(filp
->f_op
->read
|| filp
->f_op
->aio_read
)) {
454 LINFO(curlun
, "file not readable: %s\n", filename
);
457 if (!(filp
->f_op
->write
|| filp
->f_op
->aio_write
))
460 size
= i_size_read(inode
->i_mapping
->host
);
462 LINFO(curlun
, "unable to find file size: %s\n", filename
);
470 } else if (inode
->i_bdev
) {
471 blksize
= bdev_logical_block_size(inode
->i_bdev
);
472 blkbits
= blksize_bits(blksize
);
478 num_sectors
= size
>> blkbits
; /* File size in logic-block-size blocks */
481 min_sectors
= 300; /* Smallest track is 300 frames */
482 if (num_sectors
>= 256*60*75) {
483 num_sectors
= 256*60*75 - 1;
484 LINFO(curlun
, "file too big: %s\n", filename
);
485 LINFO(curlun
, "using only first %d blocks\n",
489 if (num_sectors
< min_sectors
) {
490 LINFO(curlun
, "file too small: %s\n", filename
);
495 if (fsg_lun_is_open(curlun
))
496 fsg_lun_close(curlun
);
498 curlun
->blksize
= blksize
;
499 curlun
->blkbits
= blkbits
;
502 curlun
->file_length
= size
;
503 curlun
->num_sectors
= num_sectors
;
504 LDBG(curlun
, "open backing file: %s\n", filename
);
513 /*-------------------------------------------------------------------------*/
516 * Sync the file data, don't bother with the metadata.
517 * This code was copied from fs/buffer.c:sys_fdatasync().
519 static int fsg_lun_fsync_sub(struct fsg_lun
*curlun
)
521 struct file
*filp
= curlun
->filp
;
523 if (curlun
->ro
|| !filp
)
525 return vfs_fsync(filp
, 1);
528 static void store_cdrom_address(u8
*dest
, int msf
, u32 addr
)
531 /* Convert to Minutes-Seconds-Frames */
532 addr
>>= 2; /* Convert to 2048-byte frames */
533 addr
+= 2*75; /* Lead-in occupies 2 seconds */
534 dest
[3] = addr
% 75; /* Frames */
536 dest
[2] = addr
% 60; /* Seconds */
538 dest
[1] = addr
; /* Minutes */
539 dest
[0] = 0; /* Reserved */
541 /* Absolute sector */
542 put_unaligned_be32(addr
, dest
);
547 /*-------------------------------------------------------------------------*/
550 static ssize_t
ro_show(struct device
*dev
, struct device_attribute
*attr
,
553 struct fsg_lun
*curlun
= fsg_lun_from_dev(dev
);
555 return sprintf(buf
, "%d\n", fsg_lun_is_open(curlun
)
557 : curlun
->initially_ro
);
560 static ssize_t
nofua_show(struct device
*dev
, struct device_attribute
*attr
,
563 struct fsg_lun
*curlun
= fsg_lun_from_dev(dev
);
565 return sprintf(buf
, "%u\n", curlun
->nofua
);
568 static ssize_t
file_show(struct device
*dev
, struct device_attribute
*attr
,
571 struct fsg_lun
*curlun
= fsg_lun_from_dev(dev
);
572 struct rw_semaphore
*filesem
= dev_get_drvdata(dev
);
577 if (fsg_lun_is_open(curlun
)) { /* Get the complete pathname */
578 p
= d_path(&curlun
->filp
->f_path
, buf
, PAGE_SIZE
- 1);
584 buf
[rc
] = '\n'; /* Add a newline */
587 } else { /* No file, return 0 bytes */
596 static ssize_t
ro_store(struct device
*dev
, struct device_attribute
*attr
,
597 const char *buf
, size_t count
)
600 struct fsg_lun
*curlun
= fsg_lun_from_dev(dev
);
601 struct rw_semaphore
*filesem
= dev_get_drvdata(dev
);
604 rc
= kstrtouint(buf
, 2, &ro
);
609 * Allow the write-enable status to change only while the
610 * backing file is closed.
613 if (fsg_lun_is_open(curlun
)) {
614 LDBG(curlun
, "read-only status change prevented\n");
618 curlun
->initially_ro
= ro
;
619 LDBG(curlun
, "read-only status set to %d\n", curlun
->ro
);
626 static ssize_t
nofua_store(struct device
*dev
, struct device_attribute
*attr
,
627 const char *buf
, size_t count
)
629 struct fsg_lun
*curlun
= fsg_lun_from_dev(dev
);
633 ret
= kstrtouint(buf
, 2, &nofua
);
637 /* Sync data when switching from async mode to sync */
638 if (!nofua
&& curlun
->nofua
)
639 fsg_lun_fsync_sub(curlun
);
641 curlun
->nofua
= nofua
;
646 static ssize_t
file_store(struct device
*dev
, struct device_attribute
*attr
,
647 const char *buf
, size_t count
)
649 struct fsg_lun
*curlun
= fsg_lun_from_dev(dev
);
650 struct rw_semaphore
*filesem
= dev_get_drvdata(dev
);
653 if (curlun
->prevent_medium_removal
&& fsg_lun_is_open(curlun
)) {
654 LDBG(curlun
, "eject attempt prevented\n");
655 return -EBUSY
; /* "Door is locked" */
658 /* Remove a trailing newline */
659 if (count
> 0 && buf
[count
-1] == '\n')
660 ((char *) buf
)[count
-1] = 0; /* Ugh! */
662 /* Load new medium */
664 if (count
> 0 && buf
[0]) {
665 /* fsg_lun_open() will close existing file if any. */
666 rc
= fsg_lun_open(curlun
, buf
);
668 curlun
->unit_attention_data
=
669 SS_NOT_READY_TO_READY_TRANSITION
;
670 } else if (fsg_lun_is_open(curlun
)) {
671 fsg_lun_close(curlun
);
672 curlun
->unit_attention_data
= SS_MEDIUM_NOT_PRESENT
;
675 return (rc
< 0 ? rc
: count
);