blob: 3ea70d8549efa64600ed9c86005806dc6edd6a77 [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
Michal Nazarewiczd6181702009-10-28 16:57:15 +010055
Michal Nazarewicz3323b712010-10-07 13:05:24 +020056#include <linux/usb/storage.h>
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +020057#include <scsi/scsi.h>
58#include <asm/unaligned.h>
Michal Nazarewiczd6181702009-10-28 16:57:15 +010059
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010060
Michal Nazarewiczd0893262010-07-05 16:38:04 +020061/*
62 * Thanks to NetChip Technologies for donating this product ID.
Michal Nazarewicz93f93742009-10-28 16:57:17 +010063 *
64 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
Michal Nazarewiczd0893262010-07-05 16:38:04 +020065 * Instead: allocate your own, using normal USB-IF procedures.
66 */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +010067#define FSG_VENDOR_ID 0x0525 /* NetChip */
68#define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
Michal Nazarewicz93f93742009-10-28 16:57:17 +010069
70
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010071/*-------------------------------------------------------------------------*/
72
73
74#ifndef DEBUG
75#undef VERBOSE_DEBUG
76#undef DUMP_MSGS
77#endif /* !DEBUG */
78
79#ifdef VERBOSE_DEBUG
80#define VLDBG LDBG
81#else
82#define VLDBG(lun, fmt, args...) do { } while (0)
83#endif /* VERBOSE_DEBUG */
84
85#define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
86#define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
87#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
88#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
89
Michal Nazarewiczd0893262010-07-05 16:38:04 +020090/*
91 * Keep those macros in sync with those in
92 * include/linux/usb/composite.h or else GCC will complain. If they
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010093 * are identical (the same names of arguments, white spaces in the
94 * same places) GCC will allow redefinition otherwise (even if some
Michal Nazarewiczd0893262010-07-05 16:38:04 +020095 * white space is removed or added) warning will be issued.
96 *
97 * Those macros are needed here because File Storage Gadget does not
98 * include the composite.h header. For composite gadgets those macros
99 * are redundant since composite.h is included any way.
100 *
101 * One could check whether those macros are already defined (which
102 * would indicate composite.h had been included) or not (which would
103 * indicate we were in FSG) but this is not done because a warning is
104 * desired if definitions here differ from the ones in composite.h.
105 *
106 * We want the definitions to match and be the same in File Storage
107 * Gadget as well as Mass Storage Function (and so composite gadgets
108 * using MSF). If someone changes them in composite.h it will produce
109 * a warning in this file when building MSF.
110 */
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100111#define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
112#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
113#define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
114#define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
115#define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100116
117
118
119#ifdef DUMP_MSGS
120
121# define dump_msg(fsg, /* const char * */ label, \
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100122 /* const u8 * */ buf, /* unsigned */ length) do { \
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100123 if (length < 512) { \
124 DBG(fsg, "%s, length %u:\n", label, length); \
125 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
126 16, 1, buf, length, 0); \
127 } \
128} while (0)
129
130# define dump_cdb(fsg) do { } while (0)
131
132#else
133
134# define dump_msg(fsg, /* const char * */ label, \
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100135 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100136
137# ifdef VERBOSE_DEBUG
138
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100139# define dump_cdb(fsg) \
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100140 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
141 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
142
143# else
144
145# define dump_cdb(fsg) do { } while (0)
146
147# endif /* VERBOSE_DEBUG */
148
149#endif /* DUMP_MSGS */
150
151
152
153
154
155/*-------------------------------------------------------------------------*/
156
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100157/* Bulk-only data structures */
158
159/* Command Block Wrapper */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100160struct fsg_bulk_cb_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100161 __le32 Signature; /* Contains 'USBC' */
162 u32 Tag; /* Unique per command id */
163 __le32 DataTransferLength; /* Size of the data */
164 u8 Flags; /* Direction in bit 7 */
165 u8 Lun; /* LUN (normally 0) */
166 u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
167 u8 CDB[16]; /* Command Data Block */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100168};
169
170#define USB_BULK_CB_WRAP_LEN 31
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100171#define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100172#define USB_BULK_IN_FLAG 0x80
173
174/* Command Status Wrapper */
175struct bulk_cs_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100176 __le32 Signature; /* Should = 'USBS' */
177 u32 Tag; /* Same as original command */
178 __le32 Residue; /* Amount not transferred */
179 u8 Status; /* See below */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100180};
181
182#define USB_BULK_CS_WRAP_LEN 13
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100183#define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100184#define USB_STATUS_PASS 0
185#define USB_STATUS_FAIL 1
186#define USB_STATUS_PHASE_ERROR 2
187
188/* Bulk-only class specific requests */
189#define USB_BULK_RESET_REQUEST 0xff
190#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
191
192
193/* CBI Interrupt data structure */
194struct interrupt_data {
195 u8 bType;
196 u8 bValue;
197};
198
199#define CBI_INTERRUPT_DATA_LEN 2
200
201/* CBI Accept Device-Specific Command request */
202#define USB_CBI_ADSC_REQUEST 0x00
203
204
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100205/* Length of a SCSI Command Data Block */
206#define MAX_COMMAND_SIZE 16
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100207
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100208/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
209#define SS_NO_SENSE 0
210#define SS_COMMUNICATION_FAILURE 0x040800
211#define SS_INVALID_COMMAND 0x052000
212#define SS_INVALID_FIELD_IN_CDB 0x052400
213#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
214#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
215#define SS_MEDIUM_NOT_PRESENT 0x023a00
216#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
217#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
218#define SS_RESET_OCCURRED 0x062900
219#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
220#define SS_UNRECOVERED_READ_ERROR 0x031100
221#define SS_WRITE_ERROR 0x030c02
222#define SS_WRITE_PROTECTED 0x072700
223
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100224#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100225#define ASC(x) ((u8) ((x) >> 8))
226#define ASCQ(x) ((u8) (x))
227
228
229/*-------------------------------------------------------------------------*/
230
231
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100232struct fsg_lun {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100233 struct file *filp;
234 loff_t file_length;
235 loff_t num_sectors;
236
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100237 unsigned int initially_ro:1;
238 unsigned int ro:1;
239 unsigned int removable:1;
240 unsigned int cdrom:1;
241 unsigned int prevent_medium_removal:1;
242 unsigned int registered:1;
243 unsigned int info_valid:1;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300244 unsigned int nofua:1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100245
246 u32 sense_data;
247 u32 sense_data_info;
248 u32 unit_attention_data;
249
Peiyu Li3f565a32011-08-17 22:52:59 -0700250 unsigned int blkbits; /* Bits of logical block size of bound block device */
251 unsigned int blksize; /* logical block size of bound block device */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100252 struct device dev;
253};
254
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100255#define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100256
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100257static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100258{
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100259 return container_of(dev, struct fsg_lun, dev);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100260}
261
262
263/* Big enough to hold our biggest descriptor */
264#define EP0_BUFSIZE 256
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100265#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100266
267/* Number of buffers we will use. 2 is enough for double-buffering */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100268#define FSG_NUM_BUFFERS 2
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100269
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100270/* Default size of buffer length. */
271#define FSG_BUFLEN ((u32)16384)
272
273/* Maximal number of LUNs supported in mass storage function */
274#define FSG_MAX_LUNS 8
275
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100276enum fsg_buffer_state {
277 BUF_STATE_EMPTY = 0,
278 BUF_STATE_FULL,
279 BUF_STATE_BUSY
280};
281
282struct fsg_buffhd {
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100283#ifdef FSG_BUFFHD_STATIC_BUFFER
284 char buf[FSG_BUFLEN];
285#else
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100286 void *buf;
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100287#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100288 enum fsg_buffer_state state;
289 struct fsg_buffhd *next;
290
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -0700291 /*
292 * The NetChip 2280 is faster, and handles some protocol faults
293 * better, if we don't submit any short bulk-out read requests.
294 * So we will record the intended request length here.
295 */
296 unsigned int bulk_out_intended_length;
297
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100298 struct usb_request *inreq;
299 int inreq_busy;
300 struct usb_request *outreq;
301 int outreq_busy;
302};
303
304enum fsg_state {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100305 /* This one isn't used anywhere */
306 FSG_STATE_COMMAND_PHASE = -10,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100307 FSG_STATE_DATA_PHASE,
308 FSG_STATE_STATUS_PHASE,
309
310 FSG_STATE_IDLE = 0,
311 FSG_STATE_ABORT_BULK_OUT,
312 FSG_STATE_RESET,
313 FSG_STATE_INTERFACE_CHANGE,
314 FSG_STATE_CONFIG_CHANGE,
315 FSG_STATE_DISCONNECT,
316 FSG_STATE_EXIT,
317 FSG_STATE_TERMINATED
318};
319
320enum data_direction {
321 DATA_DIR_UNKNOWN = 0,
322 DATA_DIR_FROM_HOST,
323 DATA_DIR_TO_HOST,
324 DATA_DIR_NONE
325};
326
327
328/*-------------------------------------------------------------------------*/
329
330
331static inline u32 get_unaligned_be24(u8 *buf)
332{
333 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
334}
335
336
337/*-------------------------------------------------------------------------*/
338
339
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100340enum {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100341#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100342 FSG_STRING_MANUFACTURER = 1,
343 FSG_STRING_PRODUCT,
344 FSG_STRING_SERIAL,
345 FSG_STRING_CONFIG,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100346#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100347 FSG_STRING_INTERFACE
348};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100349
350
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100351#ifndef FSG_NO_OTG
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100352static struct usb_otg_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100353fsg_otg_desc = {
354 .bLength = sizeof fsg_otg_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100355 .bDescriptorType = USB_DT_OTG,
356
357 .bmAttributes = USB_OTG_SRP,
358};
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100359#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100360
361/* There is only one interface. */
362
363static struct usb_interface_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100364fsg_intf_desc = {
365 .bLength = sizeof fsg_intf_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100366 .bDescriptorType = USB_DT_INTERFACE,
367
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100368 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100369 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100370 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
371 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100372 .iInterface = FSG_STRING_INTERFACE,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100373};
374
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200375/*
376 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
377 * interrupt-in.
378 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100379
380static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100381fsg_fs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100382 .bLength = USB_DT_ENDPOINT_SIZE,
383 .bDescriptorType = USB_DT_ENDPOINT,
384
385 .bEndpointAddress = USB_DIR_IN,
386 .bmAttributes = USB_ENDPOINT_XFER_BULK,
387 /* wMaxPacketSize set by autoconfiguration */
388};
389
390static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100391fsg_fs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100392 .bLength = USB_DT_ENDPOINT_SIZE,
393 .bDescriptorType = USB_DT_ENDPOINT,
394
395 .bEndpointAddress = USB_DIR_OUT,
396 .bmAttributes = USB_ENDPOINT_XFER_BULK,
397 /* wMaxPacketSize set by autoconfiguration */
398};
399
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100400#ifndef FSG_NO_INTR_EP
401
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100402static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100403fsg_fs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100404 .bLength = USB_DT_ENDPOINT_SIZE,
405 .bDescriptorType = USB_DT_ENDPOINT,
406
407 .bEndpointAddress = USB_DIR_IN,
408 .bmAttributes = USB_ENDPOINT_XFER_INT,
409 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100410 .bInterval = 32, /* frames -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100411};
412
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100413#ifndef FSG_NO_OTG
414# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
415#else
416# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
417#endif
418
419#endif
420
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100421static struct usb_descriptor_header *fsg_fs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100422#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100423 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100424#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100425 (struct usb_descriptor_header *) &fsg_intf_desc,
426 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
427 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100428#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100429 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100430#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100431 NULL,
432};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100433
434
435/*
436 * USB 2.0 devices need to expose both high speed and full speed
437 * descriptors, unless they only run at full speed.
438 *
439 * That means alternate endpoint descriptors (bigger packets)
440 * and a "device qualifier" ... plus more construction options
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200441 * for the configuration descriptor.
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100442 */
443static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100444fsg_hs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100445 .bLength = USB_DT_ENDPOINT_SIZE,
446 .bDescriptorType = USB_DT_ENDPOINT,
447
448 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
449 .bmAttributes = USB_ENDPOINT_XFER_BULK,
450 .wMaxPacketSize = cpu_to_le16(512),
451};
452
453static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100454fsg_hs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100455 .bLength = USB_DT_ENDPOINT_SIZE,
456 .bDescriptorType = USB_DT_ENDPOINT,
457
458 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
459 .bmAttributes = USB_ENDPOINT_XFER_BULK,
460 .wMaxPacketSize = cpu_to_le16(512),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100461 .bInterval = 1, /* NAK every 1 uframe */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100462};
463
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100464#ifndef FSG_NO_INTR_EP
465
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100466static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100467fsg_hs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100468 .bLength = USB_DT_ENDPOINT_SIZE,
469 .bDescriptorType = USB_DT_ENDPOINT,
470
471 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
472 .bmAttributes = USB_ENDPOINT_XFER_INT,
473 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100474 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100475};
476
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100477#ifndef FSG_NO_OTG
478# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
479#else
480# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
481#endif
482
483#endif
484
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100485static struct usb_descriptor_header *fsg_hs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100486#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100487 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100488#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100489 (struct usb_descriptor_header *) &fsg_intf_desc,
490 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
491 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100492#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100493 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100494#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100495 NULL,
496};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100497
498/* Maxpacket and other transfer characteristics vary by speed. */
Tatyana Brokhmanbc8687d2011-06-30 08:44:42 +0300499static __maybe_unused struct usb_endpoint_descriptor *
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100500fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100501 struct usb_endpoint_descriptor *hs)
502{
503 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
504 return hs;
505 return fs;
506}
507
508
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100509/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100510static struct usb_string fsg_strings[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100511#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100512 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
513 {FSG_STRING_PRODUCT, fsg_string_product},
Alan Sternd8087422010-09-03 11:15:41 -0400514 {FSG_STRING_SERIAL, ""},
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100515 {FSG_STRING_CONFIG, fsg_string_config},
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100516#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100517 {FSG_STRING_INTERFACE, fsg_string_interface},
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100518 {}
519};
520
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100521static struct usb_gadget_strings fsg_stringtab = {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100522 .language = 0x0409, /* en-us */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100523 .strings = fsg_strings,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100524};
525
526
527 /*-------------------------------------------------------------------------*/
528
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200529/*
530 * If the next two routines are called while the gadget is registered,
531 * the caller must own fsg->filesem for writing.
532 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100533
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100534static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100535{
536 int ro;
537 struct file *filp = NULL;
538 int rc = -EINVAL;
539 struct inode *inode = NULL;
540 loff_t size;
541 loff_t num_sectors;
542 loff_t min_sectors;
543
544 /* R/W if we can, R/O if we must */
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100545 ro = curlun->initially_ro;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100546 if (!ro) {
547 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
Tejun Heo75f1dc02010-11-13 11:55:17 +0100548 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100549 ro = 1;
550 }
551 if (ro)
552 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
553 if (IS_ERR(filp)) {
554 LINFO(curlun, "unable to open backing file: %s\n", filename);
555 return PTR_ERR(filp);
556 }
557
558 if (!(filp->f_mode & FMODE_WRITE))
559 ro = 1;
560
561 if (filp->f_path.dentry)
562 inode = filp->f_path.dentry->d_inode;
Tejun Heo75f1dc02010-11-13 11:55:17 +0100563 if (!inode || (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100564 LINFO(curlun, "invalid file type: %s\n", filename);
565 goto out;
566 }
567
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200568 /*
569 * If we can't read the file, it's no good.
570 * If we can't write the file, use it read-only.
571 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100572 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
573 LINFO(curlun, "file not readable: %s\n", filename);
574 goto out;
575 }
576 if (!(filp->f_op->write || filp->f_op->aio_write))
577 ro = 1;
578
579 size = i_size_read(inode->i_mapping->host);
580 if (size < 0) {
581 LINFO(curlun, "unable to find file size: %s\n", filename);
582 rc = (int) size;
583 goto out;
584 }
Peiyu Li3f565a32011-08-17 22:52:59 -0700585
586 if (curlun->cdrom) {
587 curlun->blksize = 2048;
588 curlun->blkbits = 11;
589 } else if (inode->i_bdev) {
590 curlun->blksize = bdev_logical_block_size(inode->i_bdev);
591 curlun->blkbits = blksize_bits(curlun->blksize);
592 } else {
593 curlun->blksize = 512;
594 curlun->blkbits = 9;
595 }
596
597 num_sectors = size >> curlun->blkbits; /* File size in logic-block-size blocks */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100598 min_sectors = 1;
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100599 if (curlun->cdrom) {
Peiyu Li3f565a32011-08-17 22:52:59 -0700600 min_sectors = 300; /* Smallest track is 300 frames */
601 if (num_sectors >= 256*60*75) {
602 num_sectors = 256*60*75 - 1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100603 LINFO(curlun, "file too big: %s\n", filename);
604 LINFO(curlun, "using only first %d blocks\n",
605 (int) num_sectors);
606 }
607 }
608 if (num_sectors < min_sectors) {
609 LINFO(curlun, "file too small: %s\n", filename);
610 rc = -ETOOSMALL;
611 goto out;
612 }
613
614 get_file(filp);
615 curlun->ro = ro;
616 curlun->filp = filp;
617 curlun->file_length = size;
618 curlun->num_sectors = num_sectors;
619 LDBG(curlun, "open backing file: %s\n", filename);
620 rc = 0;
621
622out:
623 filp_close(filp, current->files);
624 return rc;
625}
626
627
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100628static void fsg_lun_close(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100629{
630 if (curlun->filp) {
631 LDBG(curlun, "close backing file\n");
632 fput(curlun->filp);
633 curlun->filp = NULL;
634 }
635}
636
637
638/*-------------------------------------------------------------------------*/
639
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200640/*
641 * Sync the file data, don't bother with the metadata.
642 * This code was copied from fs/buffer.c:sys_fdatasync().
643 */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100644static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100645{
646 struct file *filp = curlun->filp;
647
648 if (curlun->ro || !filp)
649 return 0;
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100650 return vfs_fsync(filp, 1);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100651}
652
653static void store_cdrom_address(u8 *dest, int msf, u32 addr)
654{
655 if (msf) {
656 /* Convert to Minutes-Seconds-Frames */
657 addr >>= 2; /* Convert to 2048-byte frames */
658 addr += 2*75; /* Lead-in occupies 2 seconds */
659 dest[3] = addr % 75; /* Frames */
660 addr /= 75;
661 dest[2] = addr % 60; /* Seconds */
662 addr /= 60;
663 dest[1] = addr; /* Minutes */
664 dest[0] = 0; /* Reserved */
665 } else {
666 /* Absolute sector */
667 put_unaligned_be32(addr, dest);
668 }
669}
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100670
671
672/*-------------------------------------------------------------------------*/
673
674
675static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
676 char *buf)
677{
678 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
679
680 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
681 ? curlun->ro
682 : curlun->initially_ro);
683}
684
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300685static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
686 char *buf)
687{
688 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
689
690 return sprintf(buf, "%u\n", curlun->nofua);
691}
692
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100693static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
694 char *buf)
695{
696 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
697 struct rw_semaphore *filesem = dev_get_drvdata(dev);
698 char *p;
699 ssize_t rc;
700
701 down_read(filesem);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100702 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100703 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
704 if (IS_ERR(p))
705 rc = PTR_ERR(p);
706 else {
707 rc = strlen(p);
708 memmove(buf, p, rc);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100709 buf[rc] = '\n'; /* Add a newline */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100710 buf[++rc] = 0;
711 }
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100712 } else { /* No file, return 0 bytes */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100713 *buf = 0;
714 rc = 0;
715 }
716 up_read(filesem);
717 return rc;
718}
719
720
721static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
722 const char *buf, size_t count)
723{
Michal Nazarewiczfd4477b2011-04-14 11:55:43 +0200724 ssize_t rc;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100725 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
726 struct rw_semaphore *filesem = dev_get_drvdata(dev);
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200727 unsigned ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100728
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200729 rc = kstrtouint(buf, 2, &ro);
730 if (rc)
731 return rc;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100732
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200733 /*
734 * Allow the write-enable status to change only while the
735 * backing file is closed.
736 */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100737 down_read(filesem);
738 if (fsg_lun_is_open(curlun)) {
739 LDBG(curlun, "read-only status change prevented\n");
740 rc = -EBUSY;
741 } else {
Andy Shevchenko8156d152010-07-22 11:58:47 +0300742 curlun->ro = ro;
743 curlun->initially_ro = ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100744 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
Michal Nazarewiczfd4477b2011-04-14 11:55:43 +0200745 rc = count;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100746 }
747 up_read(filesem);
748 return rc;
749}
750
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300751static ssize_t fsg_store_nofua(struct device *dev,
752 struct device_attribute *attr,
753 const char *buf, size_t count)
754{
755 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200756 unsigned nofua;
757 int ret;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300758
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200759 ret = kstrtouint(buf, 2, &nofua);
760 if (ret)
761 return ret;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300762
763 /* Sync data when switching from async mode to sync */
764 if (!nofua && curlun->nofua)
765 fsg_lun_fsync_sub(curlun);
766
767 curlun->nofua = nofua;
768
769 return count;
770}
771
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100772static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
773 const char *buf, size_t count)
774{
775 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
776 struct rw_semaphore *filesem = dev_get_drvdata(dev);
777 int rc = 0;
778
779 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
780 LDBG(curlun, "eject attempt prevented\n");
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100781 return -EBUSY; /* "Door is locked" */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100782 }
783
784 /* Remove a trailing newline */
785 if (count > 0 && buf[count-1] == '\n')
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100786 ((char *) buf)[count-1] = 0; /* Ugh! */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100787
788 /* Eject current medium */
789 down_write(filesem);
790 if (fsg_lun_is_open(curlun)) {
791 fsg_lun_close(curlun);
792 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
793 }
794
795 /* Load new medium */
796 if (count > 0 && buf[0]) {
797 rc = fsg_lun_open(curlun, buf);
798 if (rc == 0)
799 curlun->unit_attention_data =
800 SS_NOT_READY_TO_READY_TRANSITION;
801 }
802 up_write(filesem);
803 return (rc < 0 ? rc : count);
804}