blob: 484acfb1a7c5a999e9b2ef5f48c4ab54d1c0a549 [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
29 * - fsg_string_serial -- product's serial
30 * - fsg_string_config -- name of the configuration
31 * - fsg_string_interface -- name of the interface
32 * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
33 * macro is defined prior to including this file.
34 */
35
Michal Nazarewicz93bcf122009-10-28 16:57:19 +010036/*
37 * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
38 * fsg_hs_intr_in_desc objects as well as
39 * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
40 * macros are not defined.
41 *
42 * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
43 * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
44 * defined (as well as corresponding entries in string tables are
45 * missing) and FSG_STRING_INTERFACE has value of zero.
46 *
47 * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
48 */
49
Michal Nazarewicz606206c2009-10-28 16:57:21 +010050/*
51 * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
52 * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
53 * characters rather then a pointer to void.
54 */
55
Michal Nazarewiczd6181702009-10-28 16:57:15 +010056
57#include <asm/unaligned.h>
58
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
160/* USB protocol value = the transport method */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100161#define USB_PR_CBI 0x00 /* Control/Bulk/Interrupt */
162#define USB_PR_CB 0x01 /* Control/Bulk w/o interrupt */
163#define USB_PR_BULK 0x50 /* Bulk-only */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100164
165/* USB subclass value = the protocol encapsulation */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100166#define USB_SC_RBC 0x01 /* Reduced Block Commands (flash) */
167#define USB_SC_8020 0x02 /* SFF-8020i, MMC-2, ATAPI (CD-ROM) */
168#define USB_SC_QIC 0x03 /* QIC-157 (tape) */
169#define USB_SC_UFI 0x04 /* UFI (floppy) */
170#define USB_SC_8070 0x05 /* SFF-8070i (removable) */
171#define USB_SC_SCSI 0x06 /* Transparent SCSI */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100172
173/* Bulk-only data structures */
174
175/* Command Block Wrapper */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100176struct fsg_bulk_cb_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100177 __le32 Signature; /* Contains 'USBC' */
178 u32 Tag; /* Unique per command id */
179 __le32 DataTransferLength; /* Size of the data */
180 u8 Flags; /* Direction in bit 7 */
181 u8 Lun; /* LUN (normally 0) */
182 u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
183 u8 CDB[16]; /* Command Data Block */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100184};
185
186#define USB_BULK_CB_WRAP_LEN 31
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100187#define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100188#define USB_BULK_IN_FLAG 0x80
189
190/* Command Status Wrapper */
191struct bulk_cs_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100192 __le32 Signature; /* Should = 'USBS' */
193 u32 Tag; /* Same as original command */
194 __le32 Residue; /* Amount not transferred */
195 u8 Status; /* See below */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100196};
197
198#define USB_BULK_CS_WRAP_LEN 13
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100199#define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100200#define USB_STATUS_PASS 0
201#define USB_STATUS_FAIL 1
202#define USB_STATUS_PHASE_ERROR 2
203
204/* Bulk-only class specific requests */
205#define USB_BULK_RESET_REQUEST 0xff
206#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
207
208
209/* CBI Interrupt data structure */
210struct interrupt_data {
211 u8 bType;
212 u8 bValue;
213};
214
215#define CBI_INTERRUPT_DATA_LEN 2
216
217/* CBI Accept Device-Specific Command request */
218#define USB_CBI_ADSC_REQUEST 0x00
219
220
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100221/* Length of a SCSI Command Data Block */
222#define MAX_COMMAND_SIZE 16
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100223
224/* SCSI commands that we recognize */
225#define SC_FORMAT_UNIT 0x04
226#define SC_INQUIRY 0x12
227#define SC_MODE_SELECT_6 0x15
228#define SC_MODE_SELECT_10 0x55
229#define SC_MODE_SENSE_6 0x1a
230#define SC_MODE_SENSE_10 0x5a
231#define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
232#define SC_READ_6 0x08
233#define SC_READ_10 0x28
234#define SC_READ_12 0xa8
235#define SC_READ_CAPACITY 0x25
236#define SC_READ_FORMAT_CAPACITIES 0x23
237#define SC_READ_HEADER 0x44
238#define SC_READ_TOC 0x43
239#define SC_RELEASE 0x17
240#define SC_REQUEST_SENSE 0x03
241#define SC_RESERVE 0x16
242#define SC_SEND_DIAGNOSTIC 0x1d
243#define SC_START_STOP_UNIT 0x1b
244#define SC_SYNCHRONIZE_CACHE 0x35
245#define SC_TEST_UNIT_READY 0x00
246#define SC_VERIFY 0x2f
247#define SC_WRITE_6 0x0a
248#define SC_WRITE_10 0x2a
249#define SC_WRITE_12 0xaa
250
251/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
252#define SS_NO_SENSE 0
253#define SS_COMMUNICATION_FAILURE 0x040800
254#define SS_INVALID_COMMAND 0x052000
255#define SS_INVALID_FIELD_IN_CDB 0x052400
256#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
257#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
258#define SS_MEDIUM_NOT_PRESENT 0x023a00
259#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
260#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
261#define SS_RESET_OCCURRED 0x062900
262#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
263#define SS_UNRECOVERED_READ_ERROR 0x031100
264#define SS_WRITE_ERROR 0x030c02
265#define SS_WRITE_PROTECTED 0x072700
266
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100267#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100268#define ASC(x) ((u8) ((x) >> 8))
269#define ASCQ(x) ((u8) (x))
270
271
272/*-------------------------------------------------------------------------*/
273
274
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100275struct fsg_lun {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100276 struct file *filp;
277 loff_t file_length;
278 loff_t num_sectors;
279
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100280 unsigned int initially_ro:1;
281 unsigned int ro:1;
282 unsigned int removable:1;
283 unsigned int cdrom:1;
284 unsigned int prevent_medium_removal:1;
285 unsigned int registered:1;
286 unsigned int info_valid:1;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300287 unsigned int nofua:1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100288
289 u32 sense_data;
290 u32 sense_data_info;
291 u32 unit_attention_data;
292
293 struct device dev;
294};
295
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100296#define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100297
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100298static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100299{
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100300 return container_of(dev, struct fsg_lun, dev);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100301}
302
303
304/* Big enough to hold our biggest descriptor */
305#define EP0_BUFSIZE 256
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100306#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100307
308/* Number of buffers we will use. 2 is enough for double-buffering */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100309#define FSG_NUM_BUFFERS 2
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100310
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100311/* Default size of buffer length. */
312#define FSG_BUFLEN ((u32)16384)
313
314/* Maximal number of LUNs supported in mass storage function */
315#define FSG_MAX_LUNS 8
316
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100317enum fsg_buffer_state {
318 BUF_STATE_EMPTY = 0,
319 BUF_STATE_FULL,
320 BUF_STATE_BUSY
321};
322
323struct fsg_buffhd {
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100324#ifdef FSG_BUFFHD_STATIC_BUFFER
325 char buf[FSG_BUFLEN];
326#else
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100327 void *buf;
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100328#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100329 enum fsg_buffer_state state;
330 struct fsg_buffhd *next;
331
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200332 /*
333 * The NetChip 2280 is faster, and handles some protocol faults
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100334 * better, if we don't submit any short bulk-out read requests.
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200335 * So we will record the intended request length here.
336 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100337 unsigned int bulk_out_intended_length;
338
339 struct usb_request *inreq;
340 int inreq_busy;
341 struct usb_request *outreq;
342 int outreq_busy;
343};
344
345enum fsg_state {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100346 /* This one isn't used anywhere */
347 FSG_STATE_COMMAND_PHASE = -10,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100348 FSG_STATE_DATA_PHASE,
349 FSG_STATE_STATUS_PHASE,
350
351 FSG_STATE_IDLE = 0,
352 FSG_STATE_ABORT_BULK_OUT,
353 FSG_STATE_RESET,
354 FSG_STATE_INTERFACE_CHANGE,
355 FSG_STATE_CONFIG_CHANGE,
356 FSG_STATE_DISCONNECT,
357 FSG_STATE_EXIT,
358 FSG_STATE_TERMINATED
359};
360
361enum data_direction {
362 DATA_DIR_UNKNOWN = 0,
363 DATA_DIR_FROM_HOST,
364 DATA_DIR_TO_HOST,
365 DATA_DIR_NONE
366};
367
368
369/*-------------------------------------------------------------------------*/
370
371
372static inline u32 get_unaligned_be24(u8 *buf)
373{
374 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
375}
376
377
378/*-------------------------------------------------------------------------*/
379
380
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100381enum {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100382#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100383 FSG_STRING_MANUFACTURER = 1,
384 FSG_STRING_PRODUCT,
385 FSG_STRING_SERIAL,
386 FSG_STRING_CONFIG,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100387#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100388 FSG_STRING_INTERFACE
389};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100390
391
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100392#ifndef FSG_NO_OTG
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100393static struct usb_otg_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100394fsg_otg_desc = {
395 .bLength = sizeof fsg_otg_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100396 .bDescriptorType = USB_DT_OTG,
397
398 .bmAttributes = USB_OTG_SRP,
399};
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100400#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100401
402/* There is only one interface. */
403
404static struct usb_interface_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100405fsg_intf_desc = {
406 .bLength = sizeof fsg_intf_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100407 .bDescriptorType = USB_DT_INTERFACE,
408
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100409 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100410 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100411 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
412 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100413 .iInterface = FSG_STRING_INTERFACE,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100414};
415
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200416/*
417 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
418 * interrupt-in.
419 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100420
421static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100422fsg_fs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100423 .bLength = USB_DT_ENDPOINT_SIZE,
424 .bDescriptorType = USB_DT_ENDPOINT,
425
426 .bEndpointAddress = USB_DIR_IN,
427 .bmAttributes = USB_ENDPOINT_XFER_BULK,
428 /* wMaxPacketSize set by autoconfiguration */
429};
430
431static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100432fsg_fs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100433 .bLength = USB_DT_ENDPOINT_SIZE,
434 .bDescriptorType = USB_DT_ENDPOINT,
435
436 .bEndpointAddress = USB_DIR_OUT,
437 .bmAttributes = USB_ENDPOINT_XFER_BULK,
438 /* wMaxPacketSize set by autoconfiguration */
439};
440
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100441#ifndef FSG_NO_INTR_EP
442
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100443static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100444fsg_fs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100445 .bLength = USB_DT_ENDPOINT_SIZE,
446 .bDescriptorType = USB_DT_ENDPOINT,
447
448 .bEndpointAddress = USB_DIR_IN,
449 .bmAttributes = USB_ENDPOINT_XFER_INT,
450 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100451 .bInterval = 32, /* frames -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100452};
453
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100454#ifndef FSG_NO_OTG
455# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
456#else
457# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
458#endif
459
460#endif
461
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100462static struct usb_descriptor_header *fsg_fs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100463#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100464 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100465#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100466 (struct usb_descriptor_header *) &fsg_intf_desc,
467 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
468 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100469#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100470 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100471#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100472 NULL,
473};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100474
475
476/*
477 * USB 2.0 devices need to expose both high speed and full speed
478 * descriptors, unless they only run at full speed.
479 *
480 * That means alternate endpoint descriptors (bigger packets)
481 * and a "device qualifier" ... plus more construction options
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200482 * for the configuration descriptor.
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100483 */
484static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100485fsg_hs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100486 .bLength = USB_DT_ENDPOINT_SIZE,
487 .bDescriptorType = USB_DT_ENDPOINT,
488
489 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
490 .bmAttributes = USB_ENDPOINT_XFER_BULK,
491 .wMaxPacketSize = cpu_to_le16(512),
492};
493
494static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100495fsg_hs_bulk_out_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_bulk_out_desc during fsg_bind() */
500 .bmAttributes = USB_ENDPOINT_XFER_BULK,
501 .wMaxPacketSize = cpu_to_le16(512),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100502 .bInterval = 1, /* NAK every 1 uframe */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100503};
504
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100505#ifndef FSG_NO_INTR_EP
506
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100507static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100508fsg_hs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100509 .bLength = USB_DT_ENDPOINT_SIZE,
510 .bDescriptorType = USB_DT_ENDPOINT,
511
512 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
513 .bmAttributes = USB_ENDPOINT_XFER_INT,
514 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100515 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100516};
517
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100518#ifndef FSG_NO_OTG
519# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
520#else
521# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
522#endif
523
524#endif
525
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100526static struct usb_descriptor_header *fsg_hs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100527#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100528 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100529#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100530 (struct usb_descriptor_header *) &fsg_intf_desc,
531 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
532 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100533#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100534 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100535#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100536 NULL,
537};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100538
539/* Maxpacket and other transfer characteristics vary by speed. */
540static struct usb_endpoint_descriptor *
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100541fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100542 struct usb_endpoint_descriptor *hs)
543{
544 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
545 return hs;
546 return fs;
547}
548
549
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100550/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100551static struct usb_string fsg_strings[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100552#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100553 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
554 {FSG_STRING_PRODUCT, fsg_string_product},
555 {FSG_STRING_SERIAL, fsg_string_serial},
556 {FSG_STRING_CONFIG, fsg_string_config},
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100557#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100558 {FSG_STRING_INTERFACE, fsg_string_interface},
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100559 {}
560};
561
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100562static struct usb_gadget_strings fsg_stringtab = {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100563 .language = 0x0409, /* en-us */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100564 .strings = fsg_strings,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100565};
566
567
568 /*-------------------------------------------------------------------------*/
569
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200570/*
571 * If the next two routines are called while the gadget is registered,
572 * the caller must own fsg->filesem for writing.
573 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100574
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100575static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100576{
577 int ro;
578 struct file *filp = NULL;
579 int rc = -EINVAL;
580 struct inode *inode = NULL;
581 loff_t size;
582 loff_t num_sectors;
583 loff_t min_sectors;
584
585 /* R/W if we can, R/O if we must */
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100586 ro = curlun->initially_ro;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100587 if (!ro) {
588 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
589 if (-EROFS == PTR_ERR(filp))
590 ro = 1;
591 }
592 if (ro)
593 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
594 if (IS_ERR(filp)) {
595 LINFO(curlun, "unable to open backing file: %s\n", filename);
596 return PTR_ERR(filp);
597 }
598
599 if (!(filp->f_mode & FMODE_WRITE))
600 ro = 1;
601
602 if (filp->f_path.dentry)
603 inode = filp->f_path.dentry->d_inode;
604 if (inode && S_ISBLK(inode->i_mode)) {
605 if (bdev_read_only(inode->i_bdev))
606 ro = 1;
607 } else if (!inode || !S_ISREG(inode->i_mode)) {
608 LINFO(curlun, "invalid file type: %s\n", filename);
609 goto out;
610 }
611
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200612 /*
613 * If we can't read the file, it's no good.
614 * If we can't write the file, use it read-only.
615 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100616 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
617 LINFO(curlun, "file not readable: %s\n", filename);
618 goto out;
619 }
620 if (!(filp->f_op->write || filp->f_op->aio_write))
621 ro = 1;
622
623 size = i_size_read(inode->i_mapping->host);
624 if (size < 0) {
625 LINFO(curlun, "unable to find file size: %s\n", filename);
626 rc = (int) size;
627 goto out;
628 }
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100629 num_sectors = size >> 9; /* File size in 512-byte blocks */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100630 min_sectors = 1;
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100631 if (curlun->cdrom) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100632 num_sectors &= ~3; /* Reduce to a multiple of 2048 */
633 min_sectors = 300*4; /* Smallest track is 300 frames */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100634 if (num_sectors >= 256*60*75*4) {
635 num_sectors = (256*60*75 - 1) * 4;
636 LINFO(curlun, "file too big: %s\n", filename);
637 LINFO(curlun, "using only first %d blocks\n",
638 (int) num_sectors);
639 }
640 }
641 if (num_sectors < min_sectors) {
642 LINFO(curlun, "file too small: %s\n", filename);
643 rc = -ETOOSMALL;
644 goto out;
645 }
646
647 get_file(filp);
648 curlun->ro = ro;
649 curlun->filp = filp;
650 curlun->file_length = size;
651 curlun->num_sectors = num_sectors;
652 LDBG(curlun, "open backing file: %s\n", filename);
653 rc = 0;
654
655out:
656 filp_close(filp, current->files);
657 return rc;
658}
659
660
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100661static void fsg_lun_close(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100662{
663 if (curlun->filp) {
664 LDBG(curlun, "close backing file\n");
665 fput(curlun->filp);
666 curlun->filp = NULL;
667 }
668}
669
670
671/*-------------------------------------------------------------------------*/
672
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200673/*
674 * Sync the file data, don't bother with the metadata.
675 * This code was copied from fs/buffer.c:sys_fdatasync().
676 */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100677static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100678{
679 struct file *filp = curlun->filp;
680
681 if (curlun->ro || !filp)
682 return 0;
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100683 return vfs_fsync(filp, 1);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100684}
685
686static void store_cdrom_address(u8 *dest, int msf, u32 addr)
687{
688 if (msf) {
689 /* Convert to Minutes-Seconds-Frames */
690 addr >>= 2; /* Convert to 2048-byte frames */
691 addr += 2*75; /* Lead-in occupies 2 seconds */
692 dest[3] = addr % 75; /* Frames */
693 addr /= 75;
694 dest[2] = addr % 60; /* Seconds */
695 addr /= 60;
696 dest[1] = addr; /* Minutes */
697 dest[0] = 0; /* Reserved */
698 } else {
699 /* Absolute sector */
700 put_unaligned_be32(addr, dest);
701 }
702}
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100703
704
705/*-------------------------------------------------------------------------*/
706
707
708static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
709 char *buf)
710{
711 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
712
713 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
714 ? curlun->ro
715 : curlun->initially_ro);
716}
717
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300718static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
719 char *buf)
720{
721 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
722
723 return sprintf(buf, "%u\n", curlun->nofua);
724}
725
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100726static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
727 char *buf)
728{
729 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
730 struct rw_semaphore *filesem = dev_get_drvdata(dev);
731 char *p;
732 ssize_t rc;
733
734 down_read(filesem);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100735 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100736 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
737 if (IS_ERR(p))
738 rc = PTR_ERR(p);
739 else {
740 rc = strlen(p);
741 memmove(buf, p, rc);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100742 buf[rc] = '\n'; /* Add a newline */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100743 buf[++rc] = 0;
744 }
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100745 } else { /* No file, return 0 bytes */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100746 *buf = 0;
747 rc = 0;
748 }
749 up_read(filesem);
750 return rc;
751}
752
753
754static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
755 const char *buf, size_t count)
756{
757 ssize_t rc = count;
758 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
759 struct rw_semaphore *filesem = dev_get_drvdata(dev);
Andy Shevchenko8156d152010-07-22 11:58:47 +0300760 unsigned long ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100761
Andy Shevchenko8156d152010-07-22 11:58:47 +0300762 if (strict_strtoul(buf, 2, &ro))
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100763 return -EINVAL;
764
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200765 /*
766 * Allow the write-enable status to change only while the
767 * backing file is closed.
768 */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100769 down_read(filesem);
770 if (fsg_lun_is_open(curlun)) {
771 LDBG(curlun, "read-only status change prevented\n");
772 rc = -EBUSY;
773 } else {
Andy Shevchenko8156d152010-07-22 11:58:47 +0300774 curlun->ro = ro;
775 curlun->initially_ro = ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100776 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
777 }
778 up_read(filesem);
779 return rc;
780}
781
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300782static ssize_t fsg_store_nofua(struct device *dev,
783 struct device_attribute *attr,
784 const char *buf, size_t count)
785{
786 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
787 unsigned long nofua;
788
789 if (strict_strtoul(buf, 2, &nofua))
790 return -EINVAL;
791
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}