blob: 9fd379998608d1f72c3b33c9a4f240cb9475e951 [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)
Michal Nazarewiczd6181702009-10-28 16:57:15 +01007 *
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.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010021 */
22
23
Michal Nazarewiczd6181702009-10-28 16:57:15 +010024/*
25 * This file requires the following identifiers used in USB strings to
26 * be defined (each of type pointer to char):
27 * - fsg_string_manufacturer -- name of the manufacturer
28 * - fsg_string_product -- name of the product
Michal Nazarewiczd6181702009-10-28 16:57:15 +010029 * - fsg_string_config -- name of the configuration
30 * - fsg_string_interface -- name of the interface
31 * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
32 * macro is defined prior to including this file.
33 */
34
Michal Nazarewicz93bcf122009-10-28 16:57:19 +010035/*
36 * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
37 * fsg_hs_intr_in_desc objects as well as
38 * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
39 * macros are not defined.
40 *
41 * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
42 * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
43 * defined (as well as corresponding entries in string tables are
44 * missing) and FSG_STRING_INTERFACE has value of zero.
45 *
46 * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
47 */
48
Michal Nazarewicz606206c2009-10-28 16:57:21 +010049/*
50 * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
51 * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
52 * characters rather then a pointer to void.
53 */
54
Per Forlin6532c7f2011-08-19 21:21:27 +020055/*
56 * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
57 * sets the number of pipeline buffers (length of the fsg_buffhd array).
58 * The valid range of num_buffers is: num >= 2 && num <= 4.
59 */
60
Michal Nazarewiczd6181702009-10-28 16:57:15 +010061
Michal Nazarewicz3323b712010-10-07 13:05:24 +020062#include <linux/usb/storage.h>
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +020063#include <scsi/scsi.h>
64#include <asm/unaligned.h>
Michal Nazarewiczd6181702009-10-28 16:57:15 +010065
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010066
Michal Nazarewiczd0893262010-07-05 16:38:04 +020067/*
68 * Thanks to NetChip Technologies for donating this product ID.
Michal Nazarewicz93f93742009-10-28 16:57:17 +010069 *
70 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
Michal Nazarewiczd0893262010-07-05 16:38:04 +020071 * Instead: allocate your own, using normal USB-IF procedures.
72 */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +010073#define FSG_VENDOR_ID 0x0525 /* NetChip */
74#define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
Michal Nazarewicz93f93742009-10-28 16:57:17 +010075
76
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010077/*-------------------------------------------------------------------------*/
78
79
80#ifndef DEBUG
81#undef VERBOSE_DEBUG
82#undef DUMP_MSGS
83#endif /* !DEBUG */
84
85#ifdef VERBOSE_DEBUG
86#define VLDBG LDBG
87#else
88#define VLDBG(lun, fmt, args...) do { } while (0)
89#endif /* VERBOSE_DEBUG */
90
91#define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
92#define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
93#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
94#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
95
Michal Nazarewiczd0893262010-07-05 16:38:04 +020096/*
97 * Keep those macros in sync with those in
98 * include/linux/usb/composite.h or else GCC will complain. If they
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010099 * are identical (the same names of arguments, white spaces in the
100 * same places) GCC will allow redefinition otherwise (even if some
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200101 * white space is removed or added) warning will be issued.
102 *
103 * Those macros are needed here because File Storage Gadget does not
104 * include the composite.h header. For composite gadgets those macros
105 * are redundant since composite.h is included any way.
106 *
107 * One could check whether those macros are already defined (which
108 * would indicate composite.h had been included) or not (which would
109 * indicate we were in FSG) but this is not done because a warning is
110 * desired if definitions here differ from the ones in composite.h.
111 *
112 * We want the definitions to match and be the same in File Storage
113 * Gadget as well as Mass Storage Function (and so composite gadgets
114 * using MSF). If someone changes them in composite.h it will produce
115 * a warning in this file when building MSF.
116 */
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100117#define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
118#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
119#define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
120#define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
121#define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100122
123
124
125#ifdef DUMP_MSGS
126
127# define dump_msg(fsg, /* const char * */ label, \
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100128 /* const u8 * */ buf, /* unsigned */ length) do { \
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100129 if (length < 512) { \
130 DBG(fsg, "%s, length %u:\n", label, length); \
131 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
132 16, 1, buf, length, 0); \
133 } \
134} while (0)
135
136# define dump_cdb(fsg) do { } while (0)
137
138#else
139
140# define dump_msg(fsg, /* const char * */ label, \
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100141 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100142
143# ifdef VERBOSE_DEBUG
144
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100145# define dump_cdb(fsg) \
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100146 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
147 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
148
149# else
150
151# define dump_cdb(fsg) do { } while (0)
152
153# endif /* VERBOSE_DEBUG */
154
155#endif /* DUMP_MSGS */
156
157
158
159
160
161/*-------------------------------------------------------------------------*/
162
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100163/* Bulk-only data structures */
164
165/* Command Block Wrapper */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100166struct fsg_bulk_cb_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100167 __le32 Signature; /* Contains 'USBC' */
168 u32 Tag; /* Unique per command id */
169 __le32 DataTransferLength; /* Size of the data */
170 u8 Flags; /* Direction in bit 7 */
171 u8 Lun; /* LUN (normally 0) */
172 u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
173 u8 CDB[16]; /* Command Data Block */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100174};
175
176#define USB_BULK_CB_WRAP_LEN 31
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100177#define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100178#define USB_BULK_IN_FLAG 0x80
179
180/* Command Status Wrapper */
181struct bulk_cs_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100182 __le32 Signature; /* Should = 'USBS' */
183 u32 Tag; /* Same as original command */
184 __le32 Residue; /* Amount not transferred */
185 u8 Status; /* See below */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100186};
187
188#define USB_BULK_CS_WRAP_LEN 13
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100189#define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100190#define USB_STATUS_PASS 0
191#define USB_STATUS_FAIL 1
192#define USB_STATUS_PHASE_ERROR 2
193
194/* Bulk-only class specific requests */
195#define USB_BULK_RESET_REQUEST 0xff
196#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
197
198
199/* CBI Interrupt data structure */
200struct interrupt_data {
201 u8 bType;
202 u8 bValue;
203};
204
205#define CBI_INTERRUPT_DATA_LEN 2
206
207/* CBI Accept Device-Specific Command request */
208#define USB_CBI_ADSC_REQUEST 0x00
209
210
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100211/* Length of a SCSI Command Data Block */
212#define MAX_COMMAND_SIZE 16
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100213
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100214/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
215#define SS_NO_SENSE 0
216#define SS_COMMUNICATION_FAILURE 0x040800
217#define SS_INVALID_COMMAND 0x052000
218#define SS_INVALID_FIELD_IN_CDB 0x052400
219#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
220#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
221#define SS_MEDIUM_NOT_PRESENT 0x023a00
222#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
223#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
224#define SS_RESET_OCCURRED 0x062900
225#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
226#define SS_UNRECOVERED_READ_ERROR 0x031100
227#define SS_WRITE_ERROR 0x030c02
228#define SS_WRITE_PROTECTED 0x072700
229
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100230#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100231#define ASC(x) ((u8) ((x) >> 8))
232#define ASCQ(x) ((u8) (x))
233
234
235/*-------------------------------------------------------------------------*/
236
237
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100238struct fsg_lun {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100239 struct file *filp;
240 loff_t file_length;
241 loff_t num_sectors;
242
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100243 unsigned int initially_ro:1;
244 unsigned int ro:1;
245 unsigned int removable:1;
246 unsigned int cdrom:1;
247 unsigned int prevent_medium_removal:1;
248 unsigned int registered:1;
249 unsigned int info_valid:1;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300250 unsigned int nofua:1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100251
252 u32 sense_data;
253 u32 sense_data_info;
254 u32 unit_attention_data;
255
Peiyu Li3f565a32011-08-17 22:52:59 -0700256 unsigned int blkbits; /* Bits of logical block size of bound block device */
257 unsigned int blksize; /* logical block size of bound block device */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100258 struct device dev;
259};
260
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100261#define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100262
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100263static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100264{
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100265 return container_of(dev, struct fsg_lun, dev);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100266}
267
268
269/* Big enough to hold our biggest descriptor */
270#define EP0_BUFSIZE 256
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100271#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100272
Per Forlin6532c7f2011-08-19 21:21:27 +0200273#ifdef CONFIG_USB_GADGET_DEBUG_FILES
274
275static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
276module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);
277MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers");
278
279#else
280
281/*
282 * Number of buffers we will use.
283 * 2 is usually enough for good buffering pipeline
284 */
285#define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
286
287#endif /* CONFIG_USB_DEBUG */
288
289/* check if fsg_num_buffers is within a valid range */
290static inline int fsg_num_buffers_validate(void)
291{
292 if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4)
293 return 0;
294 pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
295 fsg_num_buffers, 2 ,4);
296 return -EINVAL;
297}
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100298
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100299/* Default size of buffer length. */
300#define FSG_BUFLEN ((u32)16384)
301
302/* Maximal number of LUNs supported in mass storage function */
303#define FSG_MAX_LUNS 8
304
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100305enum fsg_buffer_state {
306 BUF_STATE_EMPTY = 0,
307 BUF_STATE_FULL,
308 BUF_STATE_BUSY
309};
310
311struct fsg_buffhd {
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100312#ifdef FSG_BUFFHD_STATIC_BUFFER
313 char buf[FSG_BUFLEN];
314#else
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100315 void *buf;
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100316#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100317 enum fsg_buffer_state state;
318 struct fsg_buffhd *next;
319
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -0700320 /*
321 * The NetChip 2280 is faster, and handles some protocol faults
322 * better, if we don't submit any short bulk-out read requests.
323 * So we will record the intended request length here.
324 */
325 unsigned int bulk_out_intended_length;
326
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100327 struct usb_request *inreq;
328 int inreq_busy;
329 struct usb_request *outreq;
330 int outreq_busy;
331};
332
333enum fsg_state {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100334 /* This one isn't used anywhere */
335 FSG_STATE_COMMAND_PHASE = -10,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100336 FSG_STATE_DATA_PHASE,
337 FSG_STATE_STATUS_PHASE,
338
339 FSG_STATE_IDLE = 0,
340 FSG_STATE_ABORT_BULK_OUT,
341 FSG_STATE_RESET,
342 FSG_STATE_INTERFACE_CHANGE,
343 FSG_STATE_CONFIG_CHANGE,
344 FSG_STATE_DISCONNECT,
345 FSG_STATE_EXIT,
346 FSG_STATE_TERMINATED
347};
348
349enum data_direction {
350 DATA_DIR_UNKNOWN = 0,
351 DATA_DIR_FROM_HOST,
352 DATA_DIR_TO_HOST,
353 DATA_DIR_NONE
354};
355
356
357/*-------------------------------------------------------------------------*/
358
359
360static inline u32 get_unaligned_be24(u8 *buf)
361{
362 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
363}
364
365
366/*-------------------------------------------------------------------------*/
367
368
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100369enum {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100370#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100371 FSG_STRING_MANUFACTURER = 1,
372 FSG_STRING_PRODUCT,
373 FSG_STRING_SERIAL,
374 FSG_STRING_CONFIG,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100375#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100376 FSG_STRING_INTERFACE
377};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100378
379
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100380#ifndef FSG_NO_OTG
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100381static struct usb_otg_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100382fsg_otg_desc = {
383 .bLength = sizeof fsg_otg_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100384 .bDescriptorType = USB_DT_OTG,
385
386 .bmAttributes = USB_OTG_SRP,
387};
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100388#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100389
390/* There is only one interface. */
391
392static struct usb_interface_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100393fsg_intf_desc = {
394 .bLength = sizeof fsg_intf_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100395 .bDescriptorType = USB_DT_INTERFACE,
396
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100397 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100398 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100399 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
400 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100401 .iInterface = FSG_STRING_INTERFACE,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100402};
403
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200404/*
405 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
406 * interrupt-in.
407 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100408
409static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100410fsg_fs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100411 .bLength = USB_DT_ENDPOINT_SIZE,
412 .bDescriptorType = USB_DT_ENDPOINT,
413
414 .bEndpointAddress = USB_DIR_IN,
415 .bmAttributes = USB_ENDPOINT_XFER_BULK,
416 /* wMaxPacketSize set by autoconfiguration */
417};
418
419static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100420fsg_fs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100421 .bLength = USB_DT_ENDPOINT_SIZE,
422 .bDescriptorType = USB_DT_ENDPOINT,
423
424 .bEndpointAddress = USB_DIR_OUT,
425 .bmAttributes = USB_ENDPOINT_XFER_BULK,
426 /* wMaxPacketSize set by autoconfiguration */
427};
428
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100429#ifndef FSG_NO_INTR_EP
430
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100431static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100432fsg_fs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100433 .bLength = USB_DT_ENDPOINT_SIZE,
434 .bDescriptorType = USB_DT_ENDPOINT,
435
436 .bEndpointAddress = USB_DIR_IN,
437 .bmAttributes = USB_ENDPOINT_XFER_INT,
438 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100439 .bInterval = 32, /* frames -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100440};
441
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100442#ifndef FSG_NO_OTG
443# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
444#else
445# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
446#endif
447
448#endif
449
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100450static struct usb_descriptor_header *fsg_fs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100451#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100452 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100453#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100454 (struct usb_descriptor_header *) &fsg_intf_desc,
455 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
456 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100457#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100458 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100459#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100460 NULL,
461};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100462
463
464/*
465 * USB 2.0 devices need to expose both high speed and full speed
466 * descriptors, unless they only run at full speed.
467 *
468 * That means alternate endpoint descriptors (bigger packets)
469 * and a "device qualifier" ... plus more construction options
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200470 * for the configuration descriptor.
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100471 */
472static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100473fsg_hs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100474 .bLength = USB_DT_ENDPOINT_SIZE,
475 .bDescriptorType = USB_DT_ENDPOINT,
476
477 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
478 .bmAttributes = USB_ENDPOINT_XFER_BULK,
479 .wMaxPacketSize = cpu_to_le16(512),
480};
481
482static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100483fsg_hs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100484 .bLength = USB_DT_ENDPOINT_SIZE,
485 .bDescriptorType = USB_DT_ENDPOINT,
486
487 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
488 .bmAttributes = USB_ENDPOINT_XFER_BULK,
489 .wMaxPacketSize = cpu_to_le16(512),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100490 .bInterval = 1, /* NAK every 1 uframe */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100491};
492
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100493#ifndef FSG_NO_INTR_EP
494
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100495static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100496fsg_hs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100497 .bLength = USB_DT_ENDPOINT_SIZE,
498 .bDescriptorType = USB_DT_ENDPOINT,
499
500 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
501 .bmAttributes = USB_ENDPOINT_XFER_INT,
502 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100503 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100504};
505
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100506#ifndef FSG_NO_OTG
507# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
508#else
509# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
510#endif
511
512#endif
513
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100514static struct usb_descriptor_header *fsg_hs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100515#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100516 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100517#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100518 (struct usb_descriptor_header *) &fsg_intf_desc,
519 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
520 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100521#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100522 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100523#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100524 NULL,
525};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100526
527/* Maxpacket and other transfer characteristics vary by speed. */
Tatyana Brokhmanbc8687d2011-06-30 08:44:42 +0300528static __maybe_unused struct usb_endpoint_descriptor *
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100529fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100530 struct usb_endpoint_descriptor *hs)
531{
532 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
533 return hs;
534 return fs;
535}
536
537
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100538/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100539static struct usb_string fsg_strings[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100540#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100541 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
542 {FSG_STRING_PRODUCT, fsg_string_product},
Alan Sternd8087422010-09-03 11:15:41 -0400543 {FSG_STRING_SERIAL, ""},
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100544 {FSG_STRING_CONFIG, fsg_string_config},
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100545#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100546 {FSG_STRING_INTERFACE, fsg_string_interface},
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100547 {}
548};
549
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100550static struct usb_gadget_strings fsg_stringtab = {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100551 .language = 0x0409, /* en-us */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100552 .strings = fsg_strings,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100553};
554
555
556 /*-------------------------------------------------------------------------*/
557
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200558/*
559 * If the next two routines are called while the gadget is registered,
560 * the caller must own fsg->filesem for writing.
561 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100562
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100563static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100564{
565 int ro;
566 struct file *filp = NULL;
567 int rc = -EINVAL;
568 struct inode *inode = NULL;
569 loff_t size;
570 loff_t num_sectors;
571 loff_t min_sectors;
572
573 /* R/W if we can, R/O if we must */
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100574 ro = curlun->initially_ro;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100575 if (!ro) {
576 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
Tejun Heo75f1dc02010-11-13 11:55:17 +0100577 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100578 ro = 1;
579 }
580 if (ro)
581 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
582 if (IS_ERR(filp)) {
583 LINFO(curlun, "unable to open backing file: %s\n", filename);
584 return PTR_ERR(filp);
585 }
586
587 if (!(filp->f_mode & FMODE_WRITE))
588 ro = 1;
589
590 if (filp->f_path.dentry)
591 inode = filp->f_path.dentry->d_inode;
Tejun Heo75f1dc02010-11-13 11:55:17 +0100592 if (!inode || (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100593 LINFO(curlun, "invalid file type: %s\n", filename);
594 goto out;
595 }
596
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200597 /*
598 * If we can't read the file, it's no good.
599 * If we can't write the file, use it read-only.
600 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100601 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
602 LINFO(curlun, "file not readable: %s\n", filename);
603 goto out;
604 }
605 if (!(filp->f_op->write || filp->f_op->aio_write))
606 ro = 1;
607
608 size = i_size_read(inode->i_mapping->host);
609 if (size < 0) {
610 LINFO(curlun, "unable to find file size: %s\n", filename);
611 rc = (int) size;
612 goto out;
613 }
Peiyu Li3f565a32011-08-17 22:52:59 -0700614
615 if (curlun->cdrom) {
616 curlun->blksize = 2048;
617 curlun->blkbits = 11;
618 } else if (inode->i_bdev) {
619 curlun->blksize = bdev_logical_block_size(inode->i_bdev);
620 curlun->blkbits = blksize_bits(curlun->blksize);
621 } else {
622 curlun->blksize = 512;
623 curlun->blkbits = 9;
624 }
625
626 num_sectors = size >> curlun->blkbits; /* File size in logic-block-size blocks */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100627 min_sectors = 1;
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100628 if (curlun->cdrom) {
Peiyu Li3f565a32011-08-17 22:52:59 -0700629 min_sectors = 300; /* Smallest track is 300 frames */
630 if (num_sectors >= 256*60*75) {
631 num_sectors = 256*60*75 - 1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100632 LINFO(curlun, "file too big: %s\n", filename);
633 LINFO(curlun, "using only first %d blocks\n",
634 (int) num_sectors);
635 }
636 }
637 if (num_sectors < min_sectors) {
638 LINFO(curlun, "file too small: %s\n", filename);
639 rc = -ETOOSMALL;
640 goto out;
641 }
642
643 get_file(filp);
644 curlun->ro = ro;
645 curlun->filp = filp;
646 curlun->file_length = size;
647 curlun->num_sectors = num_sectors;
648 LDBG(curlun, "open backing file: %s\n", filename);
649 rc = 0;
650
651out:
652 filp_close(filp, current->files);
653 return rc;
654}
655
656
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100657static void fsg_lun_close(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100658{
659 if (curlun->filp) {
660 LDBG(curlun, "close backing file\n");
661 fput(curlun->filp);
662 curlun->filp = NULL;
663 }
664}
665
666
667/*-------------------------------------------------------------------------*/
668
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200669/*
670 * Sync the file data, don't bother with the metadata.
671 * This code was copied from fs/buffer.c:sys_fdatasync().
672 */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100673static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100674{
675 struct file *filp = curlun->filp;
676
677 if (curlun->ro || !filp)
678 return 0;
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100679 return vfs_fsync(filp, 1);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100680}
681
682static void store_cdrom_address(u8 *dest, int msf, u32 addr)
683{
684 if (msf) {
685 /* Convert to Minutes-Seconds-Frames */
686 addr >>= 2; /* Convert to 2048-byte frames */
687 addr += 2*75; /* Lead-in occupies 2 seconds */
688 dest[3] = addr % 75; /* Frames */
689 addr /= 75;
690 dest[2] = addr % 60; /* Seconds */
691 addr /= 60;
692 dest[1] = addr; /* Minutes */
693 dest[0] = 0; /* Reserved */
694 } else {
695 /* Absolute sector */
696 put_unaligned_be32(addr, dest);
697 }
698}
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100699
700
701/*-------------------------------------------------------------------------*/
702
703
704static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
705 char *buf)
706{
707 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
708
709 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
710 ? curlun->ro
711 : curlun->initially_ro);
712}
713
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300714static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
715 char *buf)
716{
717 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
718
719 return sprintf(buf, "%u\n", curlun->nofua);
720}
721
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100722static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
723 char *buf)
724{
725 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
726 struct rw_semaphore *filesem = dev_get_drvdata(dev);
727 char *p;
728 ssize_t rc;
729
730 down_read(filesem);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100731 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100732 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
733 if (IS_ERR(p))
734 rc = PTR_ERR(p);
735 else {
736 rc = strlen(p);
737 memmove(buf, p, rc);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100738 buf[rc] = '\n'; /* Add a newline */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100739 buf[++rc] = 0;
740 }
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100741 } else { /* No file, return 0 bytes */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100742 *buf = 0;
743 rc = 0;
744 }
745 up_read(filesem);
746 return rc;
747}
748
749
750static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
751 const char *buf, size_t count)
752{
Michal Nazarewiczfd4477b2011-04-14 11:55:43 +0200753 ssize_t rc;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100754 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
755 struct rw_semaphore *filesem = dev_get_drvdata(dev);
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200756 unsigned ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100757
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200758 rc = kstrtouint(buf, 2, &ro);
759 if (rc)
760 return rc;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100761
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200762 /*
763 * Allow the write-enable status to change only while the
764 * backing file is closed.
765 */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100766 down_read(filesem);
767 if (fsg_lun_is_open(curlun)) {
768 LDBG(curlun, "read-only status change prevented\n");
769 rc = -EBUSY;
770 } else {
Andy Shevchenko8156d152010-07-22 11:58:47 +0300771 curlun->ro = ro;
772 curlun->initially_ro = ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100773 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
Michal Nazarewiczfd4477b2011-04-14 11:55:43 +0200774 rc = count;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100775 }
776 up_read(filesem);
777 return rc;
778}
779
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300780static ssize_t fsg_store_nofua(struct device *dev,
781 struct device_attribute *attr,
782 const char *buf, size_t count)
783{
784 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200785 unsigned nofua;
786 int ret;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300787
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200788 ret = kstrtouint(buf, 2, &nofua);
789 if (ret)
790 return ret;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300791
792 /* Sync data when switching from async mode to sync */
793 if (!nofua && curlun->nofua)
794 fsg_lun_fsync_sub(curlun);
795
796 curlun->nofua = nofua;
797
798 return count;
799}
800
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100801static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
802 const char *buf, size_t count)
803{
804 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
805 struct rw_semaphore *filesem = dev_get_drvdata(dev);
806 int rc = 0;
807
808 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
809 LDBG(curlun, "eject attempt prevented\n");
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100810 return -EBUSY; /* "Door is locked" */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100811 }
812
813 /* Remove a trailing newline */
814 if (count > 0 && buf[count-1] == '\n')
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100815 ((char *) buf)[count-1] = 0; /* Ugh! */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100816
817 /* Eject current medium */
818 down_write(filesem);
819 if (fsg_lun_is_open(curlun)) {
820 fsg_lun_close(curlun);
821 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
822 }
823
824 /* Load new medium */
825 if (count > 0 && buf[0]) {
826 rc = fsg_lun_open(curlun, buf);
827 if (rc == 0)
828 curlun->unit_attention_data =
829 SS_NOT_READY_TO_READY_TRANSITION;
830 }
831 up_write(filesem);
832 return (rc < 0 ? rc : count);
833}