blob: 0a7df3db71f1d0dd20f4e5bd419eedf94799a9d5 [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
56#include <asm/unaligned.h>
Michal Nazarewicz3323b712010-10-07 13:05:24 +020057#include <linux/usb/storage.h>
Michal Nazarewiczd6181702009-10-28 16:57:15 +010058
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010059
Michal Nazarewiczd0893262010-07-05 16:38:04 +020060/*
61 * Thanks to NetChip Technologies for donating this product ID.
Michal Nazarewicz93f93742009-10-28 16:57:17 +010062 *
63 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
Michal Nazarewiczd0893262010-07-05 16:38:04 +020064 * Instead: allocate your own, using normal USB-IF procedures.
65 */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +010066#define FSG_VENDOR_ID 0x0525 /* NetChip */
67#define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
Michal Nazarewicz93f93742009-10-28 16:57:17 +010068
69
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010070/*-------------------------------------------------------------------------*/
71
72
73#ifndef DEBUG
74#undef VERBOSE_DEBUG
75#undef DUMP_MSGS
76#endif /* !DEBUG */
77
78#ifdef VERBOSE_DEBUG
79#define VLDBG LDBG
80#else
81#define VLDBG(lun, fmt, args...) do { } while (0)
82#endif /* VERBOSE_DEBUG */
83
84#define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
85#define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
86#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
87#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
88
Michal Nazarewiczd0893262010-07-05 16:38:04 +020089/*
90 * Keep those macros in sync with those in
91 * include/linux/usb/composite.h or else GCC will complain. If they
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010092 * are identical (the same names of arguments, white spaces in the
93 * same places) GCC will allow redefinition otherwise (even if some
Michal Nazarewiczd0893262010-07-05 16:38:04 +020094 * white space is removed or added) warning will be issued.
95 *
96 * Those macros are needed here because File Storage Gadget does not
97 * include the composite.h header. For composite gadgets those macros
98 * are redundant since composite.h is included any way.
99 *
100 * One could check whether those macros are already defined (which
101 * would indicate composite.h had been included) or not (which would
102 * indicate we were in FSG) but this is not done because a warning is
103 * desired if definitions here differ from the ones in composite.h.
104 *
105 * We want the definitions to match and be the same in File Storage
106 * Gadget as well as Mass Storage Function (and so composite gadgets
107 * using MSF). If someone changes them in composite.h it will produce
108 * a warning in this file when building MSF.
109 */
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100110#define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
111#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
112#define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
113#define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
114#define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100115
116
117
118#ifdef DUMP_MSGS
119
120# define dump_msg(fsg, /* const char * */ label, \
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100121 /* const u8 * */ buf, /* unsigned */ length) do { \
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100122 if (length < 512) { \
123 DBG(fsg, "%s, length %u:\n", label, length); \
124 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
125 16, 1, buf, length, 0); \
126 } \
127} while (0)
128
129# define dump_cdb(fsg) do { } while (0)
130
131#else
132
133# define dump_msg(fsg, /* const char * */ label, \
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100134 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100135
136# ifdef VERBOSE_DEBUG
137
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100138# define dump_cdb(fsg) \
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100139 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
140 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
141
142# else
143
144# define dump_cdb(fsg) do { } while (0)
145
146# endif /* VERBOSE_DEBUG */
147
148#endif /* DUMP_MSGS */
149
150
151
152
153
154/*-------------------------------------------------------------------------*/
155
156/* SCSI device types */
157#define TYPE_DISK 0x00
158#define TYPE_CDROM 0x05
159
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100160/* Bulk-only data structures */
161
162/* Command Block Wrapper */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100163struct fsg_bulk_cb_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100164 __le32 Signature; /* Contains 'USBC' */
165 u32 Tag; /* Unique per command id */
166 __le32 DataTransferLength; /* Size of the data */
167 u8 Flags; /* Direction in bit 7 */
168 u8 Lun; /* LUN (normally 0) */
169 u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
170 u8 CDB[16]; /* Command Data Block */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100171};
172
173#define USB_BULK_CB_WRAP_LEN 31
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100174#define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100175#define USB_BULK_IN_FLAG 0x80
176
177/* Command Status Wrapper */
178struct bulk_cs_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100179 __le32 Signature; /* Should = 'USBS' */
180 u32 Tag; /* Same as original command */
181 __le32 Residue; /* Amount not transferred */
182 u8 Status; /* See below */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100183};
184
185#define USB_BULK_CS_WRAP_LEN 13
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100186#define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100187#define USB_STATUS_PASS 0
188#define USB_STATUS_FAIL 1
189#define USB_STATUS_PHASE_ERROR 2
190
191/* Bulk-only class specific requests */
192#define USB_BULK_RESET_REQUEST 0xff
193#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
194
195
196/* CBI Interrupt data structure */
197struct interrupt_data {
198 u8 bType;
199 u8 bValue;
200};
201
202#define CBI_INTERRUPT_DATA_LEN 2
203
204/* CBI Accept Device-Specific Command request */
205#define USB_CBI_ADSC_REQUEST 0x00
206
207
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100208/* Length of a SCSI Command Data Block */
209#define MAX_COMMAND_SIZE 16
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100210
211/* SCSI commands that we recognize */
212#define SC_FORMAT_UNIT 0x04
213#define SC_INQUIRY 0x12
214#define SC_MODE_SELECT_6 0x15
215#define SC_MODE_SELECT_10 0x55
216#define SC_MODE_SENSE_6 0x1a
217#define SC_MODE_SENSE_10 0x5a
218#define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
219#define SC_READ_6 0x08
220#define SC_READ_10 0x28
221#define SC_READ_12 0xa8
222#define SC_READ_CAPACITY 0x25
223#define SC_READ_FORMAT_CAPACITIES 0x23
224#define SC_READ_HEADER 0x44
225#define SC_READ_TOC 0x43
226#define SC_RELEASE 0x17
227#define SC_REQUEST_SENSE 0x03
228#define SC_RESERVE 0x16
229#define SC_SEND_DIAGNOSTIC 0x1d
230#define SC_START_STOP_UNIT 0x1b
231#define SC_SYNCHRONIZE_CACHE 0x35
232#define SC_TEST_UNIT_READY 0x00
233#define SC_VERIFY 0x2f
234#define SC_WRITE_6 0x0a
235#define SC_WRITE_10 0x2a
236#define SC_WRITE_12 0xaa
237
238/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
239#define SS_NO_SENSE 0
240#define SS_COMMUNICATION_FAILURE 0x040800
241#define SS_INVALID_COMMAND 0x052000
242#define SS_INVALID_FIELD_IN_CDB 0x052400
243#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
244#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
245#define SS_MEDIUM_NOT_PRESENT 0x023a00
246#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
247#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
248#define SS_RESET_OCCURRED 0x062900
249#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
250#define SS_UNRECOVERED_READ_ERROR 0x031100
251#define SS_WRITE_ERROR 0x030c02
252#define SS_WRITE_PROTECTED 0x072700
253
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100254#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100255#define ASC(x) ((u8) ((x) >> 8))
256#define ASCQ(x) ((u8) (x))
257
258
259/*-------------------------------------------------------------------------*/
260
261
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100262struct fsg_lun {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100263 struct file *filp;
264 loff_t file_length;
265 loff_t num_sectors;
266
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100267 unsigned int initially_ro:1;
268 unsigned int ro:1;
269 unsigned int removable:1;
270 unsigned int cdrom:1;
271 unsigned int prevent_medium_removal:1;
272 unsigned int registered:1;
273 unsigned int info_valid:1;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300274 unsigned int nofua:1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100275
276 u32 sense_data;
277 u32 sense_data_info;
278 u32 unit_attention_data;
279
280 struct device dev;
281};
282
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100283#define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100284
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100285static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100286{
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100287 return container_of(dev, struct fsg_lun, dev);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100288}
289
290
291/* Big enough to hold our biggest descriptor */
292#define EP0_BUFSIZE 256
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100293#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100294
295/* Number of buffers we will use. 2 is enough for double-buffering */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100296#define FSG_NUM_BUFFERS 2
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100297
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100298/* Default size of buffer length. */
299#define FSG_BUFLEN ((u32)16384)
300
301/* Maximal number of LUNs supported in mass storage function */
302#define FSG_MAX_LUNS 8
303
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100304enum fsg_buffer_state {
305 BUF_STATE_EMPTY = 0,
306 BUF_STATE_FULL,
307 BUF_STATE_BUSY
308};
309
310struct fsg_buffhd {
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100311#ifdef FSG_BUFFHD_STATIC_BUFFER
312 char buf[FSG_BUFLEN];
313#else
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100314 void *buf;
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100315#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100316 enum fsg_buffer_state state;
317 struct fsg_buffhd *next;
318
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200319 /*
320 * The NetChip 2280 is faster, and handles some protocol faults
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100321 * better, if we don't submit any short bulk-out read requests.
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200322 * So we will record the intended request length here.
323 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100324 unsigned int bulk_out_intended_length;
325
326 struct usb_request *inreq;
327 int inreq_busy;
328 struct usb_request *outreq;
329 int outreq_busy;
330};
331
332enum fsg_state {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100333 /* This one isn't used anywhere */
334 FSG_STATE_COMMAND_PHASE = -10,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100335 FSG_STATE_DATA_PHASE,
336 FSG_STATE_STATUS_PHASE,
337
338 FSG_STATE_IDLE = 0,
339 FSG_STATE_ABORT_BULK_OUT,
340 FSG_STATE_RESET,
341 FSG_STATE_INTERFACE_CHANGE,
342 FSG_STATE_CONFIG_CHANGE,
343 FSG_STATE_DISCONNECT,
344 FSG_STATE_EXIT,
345 FSG_STATE_TERMINATED
346};
347
348enum data_direction {
349 DATA_DIR_UNKNOWN = 0,
350 DATA_DIR_FROM_HOST,
351 DATA_DIR_TO_HOST,
352 DATA_DIR_NONE
353};
354
355
356/*-------------------------------------------------------------------------*/
357
358
359static inline u32 get_unaligned_be24(u8 *buf)
360{
361 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
362}
363
364
365/*-------------------------------------------------------------------------*/
366
367
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100368enum {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100369#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100370 FSG_STRING_MANUFACTURER = 1,
371 FSG_STRING_PRODUCT,
372 FSG_STRING_SERIAL,
373 FSG_STRING_CONFIG,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100374#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100375 FSG_STRING_INTERFACE
376};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100377
378
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100379#ifndef FSG_NO_OTG
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100380static struct usb_otg_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100381fsg_otg_desc = {
382 .bLength = sizeof fsg_otg_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100383 .bDescriptorType = USB_DT_OTG,
384
385 .bmAttributes = USB_OTG_SRP,
386};
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100387#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100388
389/* There is only one interface. */
390
391static struct usb_interface_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100392fsg_intf_desc = {
393 .bLength = sizeof fsg_intf_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100394 .bDescriptorType = USB_DT_INTERFACE,
395
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100396 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100397 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100398 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
399 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100400 .iInterface = FSG_STRING_INTERFACE,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100401};
402
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200403/*
404 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
405 * interrupt-in.
406 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100407
408static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100409fsg_fs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100410 .bLength = USB_DT_ENDPOINT_SIZE,
411 .bDescriptorType = USB_DT_ENDPOINT,
412
413 .bEndpointAddress = USB_DIR_IN,
414 .bmAttributes = USB_ENDPOINT_XFER_BULK,
415 /* wMaxPacketSize set by autoconfiguration */
416};
417
418static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100419fsg_fs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100420 .bLength = USB_DT_ENDPOINT_SIZE,
421 .bDescriptorType = USB_DT_ENDPOINT,
422
423 .bEndpointAddress = USB_DIR_OUT,
424 .bmAttributes = USB_ENDPOINT_XFER_BULK,
425 /* wMaxPacketSize set by autoconfiguration */
426};
427
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100428#ifndef FSG_NO_INTR_EP
429
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100430static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100431fsg_fs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100432 .bLength = USB_DT_ENDPOINT_SIZE,
433 .bDescriptorType = USB_DT_ENDPOINT,
434
435 .bEndpointAddress = USB_DIR_IN,
436 .bmAttributes = USB_ENDPOINT_XFER_INT,
437 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100438 .bInterval = 32, /* frames -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100439};
440
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100441#ifndef FSG_NO_OTG
442# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
443#else
444# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
445#endif
446
447#endif
448
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100449static struct usb_descriptor_header *fsg_fs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100450#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100451 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100452#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100453 (struct usb_descriptor_header *) &fsg_intf_desc,
454 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
455 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100456#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100457 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100458#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100459 NULL,
460};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100461
462
463/*
464 * USB 2.0 devices need to expose both high speed and full speed
465 * descriptors, unless they only run at full speed.
466 *
467 * That means alternate endpoint descriptors (bigger packets)
468 * and a "device qualifier" ... plus more construction options
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200469 * for the configuration descriptor.
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100470 */
471static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100472fsg_hs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100473 .bLength = USB_DT_ENDPOINT_SIZE,
474 .bDescriptorType = USB_DT_ENDPOINT,
475
476 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
477 .bmAttributes = USB_ENDPOINT_XFER_BULK,
478 .wMaxPacketSize = cpu_to_le16(512),
479};
480
481static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100482fsg_hs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100483 .bLength = USB_DT_ENDPOINT_SIZE,
484 .bDescriptorType = USB_DT_ENDPOINT,
485
486 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
487 .bmAttributes = USB_ENDPOINT_XFER_BULK,
488 .wMaxPacketSize = cpu_to_le16(512),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100489 .bInterval = 1, /* NAK every 1 uframe */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100490};
491
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100492#ifndef FSG_NO_INTR_EP
493
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100494static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100495fsg_hs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100496 .bLength = USB_DT_ENDPOINT_SIZE,
497 .bDescriptorType = USB_DT_ENDPOINT,
498
499 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
500 .bmAttributes = USB_ENDPOINT_XFER_INT,
501 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100502 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100503};
504
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100505#ifndef FSG_NO_OTG
506# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
507#else
508# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
509#endif
510
511#endif
512
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100513static struct usb_descriptor_header *fsg_hs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100514#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100515 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100516#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100517 (struct usb_descriptor_header *) &fsg_intf_desc,
518 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
519 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100520#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100521 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100522#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100523 NULL,
524};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100525
526/* Maxpacket and other transfer characteristics vary by speed. */
527static struct usb_endpoint_descriptor *
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100528fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100529 struct usb_endpoint_descriptor *hs)
530{
531 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
532 return hs;
533 return fs;
534}
535
536
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100537/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100538static struct usb_string fsg_strings[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100539#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100540 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
541 {FSG_STRING_PRODUCT, fsg_string_product},
Alan Sternd8087422010-09-03 11:15:41 -0400542 {FSG_STRING_SERIAL, ""},
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100543 {FSG_STRING_CONFIG, fsg_string_config},
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100544#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100545 {FSG_STRING_INTERFACE, fsg_string_interface},
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100546 {}
547};
548
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100549static struct usb_gadget_strings fsg_stringtab = {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100550 .language = 0x0409, /* en-us */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100551 .strings = fsg_strings,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100552};
553
554
555 /*-------------------------------------------------------------------------*/
556
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200557/*
558 * If the next two routines are called while the gadget is registered,
559 * the caller must own fsg->filesem for writing.
560 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100561
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100562static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100563{
564 int ro;
565 struct file *filp = NULL;
566 int rc = -EINVAL;
567 struct inode *inode = NULL;
568 loff_t size;
569 loff_t num_sectors;
570 loff_t min_sectors;
571
572 /* R/W if we can, R/O if we must */
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100573 ro = curlun->initially_ro;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100574 if (!ro) {
575 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
576 if (-EROFS == PTR_ERR(filp))
577 ro = 1;
578 }
579 if (ro)
580 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
581 if (IS_ERR(filp)) {
582 LINFO(curlun, "unable to open backing file: %s\n", filename);
583 return PTR_ERR(filp);
584 }
585
586 if (!(filp->f_mode & FMODE_WRITE))
587 ro = 1;
588
589 if (filp->f_path.dentry)
590 inode = filp->f_path.dentry->d_inode;
591 if (inode && S_ISBLK(inode->i_mode)) {
592 if (bdev_read_only(inode->i_bdev))
593 ro = 1;
594 } else if (!inode || !S_ISREG(inode->i_mode)) {
595 LINFO(curlun, "invalid file type: %s\n", filename);
596 goto out;
597 }
598
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200599 /*
600 * If we can't read the file, it's no good.
601 * If we can't write the file, use it read-only.
602 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100603 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
604 LINFO(curlun, "file not readable: %s\n", filename);
605 goto out;
606 }
607 if (!(filp->f_op->write || filp->f_op->aio_write))
608 ro = 1;
609
610 size = i_size_read(inode->i_mapping->host);
611 if (size < 0) {
612 LINFO(curlun, "unable to find file size: %s\n", filename);
613 rc = (int) size;
614 goto out;
615 }
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100616 num_sectors = size >> 9; /* File size in 512-byte blocks */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100617 min_sectors = 1;
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100618 if (curlun->cdrom) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100619 num_sectors &= ~3; /* Reduce to a multiple of 2048 */
620 min_sectors = 300*4; /* Smallest track is 300 frames */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100621 if (num_sectors >= 256*60*75*4) {
622 num_sectors = (256*60*75 - 1) * 4;
623 LINFO(curlun, "file too big: %s\n", filename);
624 LINFO(curlun, "using only first %d blocks\n",
625 (int) num_sectors);
626 }
627 }
628 if (num_sectors < min_sectors) {
629 LINFO(curlun, "file too small: %s\n", filename);
630 rc = -ETOOSMALL;
631 goto out;
632 }
633
634 get_file(filp);
635 curlun->ro = ro;
636 curlun->filp = filp;
637 curlun->file_length = size;
638 curlun->num_sectors = num_sectors;
639 LDBG(curlun, "open backing file: %s\n", filename);
640 rc = 0;
641
642out:
643 filp_close(filp, current->files);
644 return rc;
645}
646
647
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100648static void fsg_lun_close(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100649{
650 if (curlun->filp) {
651 LDBG(curlun, "close backing file\n");
652 fput(curlun->filp);
653 curlun->filp = NULL;
654 }
655}
656
657
658/*-------------------------------------------------------------------------*/
659
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200660/*
661 * Sync the file data, don't bother with the metadata.
662 * This code was copied from fs/buffer.c:sys_fdatasync().
663 */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100664static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100665{
666 struct file *filp = curlun->filp;
667
668 if (curlun->ro || !filp)
669 return 0;
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100670 return vfs_fsync(filp, 1);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100671}
672
673static void store_cdrom_address(u8 *dest, int msf, u32 addr)
674{
675 if (msf) {
676 /* Convert to Minutes-Seconds-Frames */
677 addr >>= 2; /* Convert to 2048-byte frames */
678 addr += 2*75; /* Lead-in occupies 2 seconds */
679 dest[3] = addr % 75; /* Frames */
680 addr /= 75;
681 dest[2] = addr % 60; /* Seconds */
682 addr /= 60;
683 dest[1] = addr; /* Minutes */
684 dest[0] = 0; /* Reserved */
685 } else {
686 /* Absolute sector */
687 put_unaligned_be32(addr, dest);
688 }
689}
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100690
691
692/*-------------------------------------------------------------------------*/
693
694
695static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
696 char *buf)
697{
698 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
699
700 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
701 ? curlun->ro
702 : curlun->initially_ro);
703}
704
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300705static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
706 char *buf)
707{
708 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
709
710 return sprintf(buf, "%u\n", curlun->nofua);
711}
712
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100713static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
714 char *buf)
715{
716 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
717 struct rw_semaphore *filesem = dev_get_drvdata(dev);
718 char *p;
719 ssize_t rc;
720
721 down_read(filesem);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100722 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100723 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
724 if (IS_ERR(p))
725 rc = PTR_ERR(p);
726 else {
727 rc = strlen(p);
728 memmove(buf, p, rc);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100729 buf[rc] = '\n'; /* Add a newline */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100730 buf[++rc] = 0;
731 }
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100732 } else { /* No file, return 0 bytes */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100733 *buf = 0;
734 rc = 0;
735 }
736 up_read(filesem);
737 return rc;
738}
739
740
741static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
742 const char *buf, size_t count)
743{
744 ssize_t rc = count;
745 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
746 struct rw_semaphore *filesem = dev_get_drvdata(dev);
Andy Shevchenko8156d152010-07-22 11:58:47 +0300747 unsigned long ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100748
Andy Shevchenko8156d152010-07-22 11:58:47 +0300749 if (strict_strtoul(buf, 2, &ro))
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100750 return -EINVAL;
751
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200752 /*
753 * Allow the write-enable status to change only while the
754 * backing file is closed.
755 */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100756 down_read(filesem);
757 if (fsg_lun_is_open(curlun)) {
758 LDBG(curlun, "read-only status change prevented\n");
759 rc = -EBUSY;
760 } else {
Andy Shevchenko8156d152010-07-22 11:58:47 +0300761 curlun->ro = ro;
762 curlun->initially_ro = ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100763 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
764 }
765 up_read(filesem);
766 return rc;
767}
768
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300769static ssize_t fsg_store_nofua(struct device *dev,
770 struct device_attribute *attr,
771 const char *buf, size_t count)
772{
773 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
774 unsigned long nofua;
775
776 if (strict_strtoul(buf, 2, &nofua))
777 return -EINVAL;
778
779 /* Sync data when switching from async mode to sync */
780 if (!nofua && curlun->nofua)
781 fsg_lun_fsync_sub(curlun);
782
783 curlun->nofua = nofua;
784
785 return count;
786}
787
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100788static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
789 const char *buf, size_t count)
790{
791 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
792 struct rw_semaphore *filesem = dev_get_drvdata(dev);
793 int rc = 0;
794
795 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
796 LDBG(curlun, "eject attempt prevented\n");
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100797 return -EBUSY; /* "Door is locked" */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100798 }
799
800 /* Remove a trailing newline */
801 if (count > 0 && buf[count-1] == '\n')
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100802 ((char *) buf)[count-1] = 0; /* Ugh! */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100803
804 /* Eject current medium */
805 down_write(filesem);
806 if (fsg_lun_is_open(curlun)) {
807 fsg_lun_close(curlun);
808 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
809 }
810
811 /* Load new medium */
812 if (count > 0 && buf[0]) {
813 rc = fsg_lun_open(curlun, buf);
814 if (rc == 0)
815 curlun->unit_attention_data =
816 SS_NOT_READY_TO_READY_TRANSITION;
817 }
818 up_write(filesem);
819 return (rc < 0 ? rc : count);
820}