blob: 3179b8bb6ceda3ed3f1dab2760ed8ad603c5f367 [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
250 struct device dev;
251};
252
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100253#define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100254
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100255static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100256{
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100257 return container_of(dev, struct fsg_lun, dev);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100258}
259
260
261/* Big enough to hold our biggest descriptor */
262#define EP0_BUFSIZE 256
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100263#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100264
265/* Number of buffers we will use. 2 is enough for double-buffering */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100266#define FSG_NUM_BUFFERS 2
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100267
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100268/* Default size of buffer length. */
269#define FSG_BUFLEN ((u32)16384)
270
271/* Maximal number of LUNs supported in mass storage function */
272#define FSG_MAX_LUNS 8
273
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100274enum fsg_buffer_state {
275 BUF_STATE_EMPTY = 0,
276 BUF_STATE_FULL,
277 BUF_STATE_BUSY
278};
279
280struct fsg_buffhd {
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100281#ifdef FSG_BUFFHD_STATIC_BUFFER
282 char buf[FSG_BUFLEN];
283#else
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100284 void *buf;
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100285#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100286 enum fsg_buffer_state state;
287 struct fsg_buffhd *next;
288
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100289 struct usb_request *inreq;
290 int inreq_busy;
291 struct usb_request *outreq;
292 int outreq_busy;
293};
294
295enum fsg_state {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100296 /* This one isn't used anywhere */
297 FSG_STATE_COMMAND_PHASE = -10,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100298 FSG_STATE_DATA_PHASE,
299 FSG_STATE_STATUS_PHASE,
300
301 FSG_STATE_IDLE = 0,
302 FSG_STATE_ABORT_BULK_OUT,
303 FSG_STATE_RESET,
304 FSG_STATE_INTERFACE_CHANGE,
305 FSG_STATE_CONFIG_CHANGE,
306 FSG_STATE_DISCONNECT,
307 FSG_STATE_EXIT,
308 FSG_STATE_TERMINATED
309};
310
311enum data_direction {
312 DATA_DIR_UNKNOWN = 0,
313 DATA_DIR_FROM_HOST,
314 DATA_DIR_TO_HOST,
315 DATA_DIR_NONE
316};
317
318
319/*-------------------------------------------------------------------------*/
320
321
322static inline u32 get_unaligned_be24(u8 *buf)
323{
324 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
325}
326
327
328/*-------------------------------------------------------------------------*/
329
330
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100331enum {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100332#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100333 FSG_STRING_MANUFACTURER = 1,
334 FSG_STRING_PRODUCT,
335 FSG_STRING_SERIAL,
336 FSG_STRING_CONFIG,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100337#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100338 FSG_STRING_INTERFACE
339};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100340
341
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100342#ifndef FSG_NO_OTG
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100343static struct usb_otg_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100344fsg_otg_desc = {
345 .bLength = sizeof fsg_otg_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100346 .bDescriptorType = USB_DT_OTG,
347
348 .bmAttributes = USB_OTG_SRP,
349};
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100350#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100351
352/* There is only one interface. */
353
354static struct usb_interface_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100355fsg_intf_desc = {
356 .bLength = sizeof fsg_intf_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100357 .bDescriptorType = USB_DT_INTERFACE,
358
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100359 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100360 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100361 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
362 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100363 .iInterface = FSG_STRING_INTERFACE,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100364};
365
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200366/*
367 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
368 * interrupt-in.
369 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100370
371static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100372fsg_fs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100373 .bLength = USB_DT_ENDPOINT_SIZE,
374 .bDescriptorType = USB_DT_ENDPOINT,
375
376 .bEndpointAddress = USB_DIR_IN,
377 .bmAttributes = USB_ENDPOINT_XFER_BULK,
378 /* wMaxPacketSize set by autoconfiguration */
379};
380
381static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100382fsg_fs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100383 .bLength = USB_DT_ENDPOINT_SIZE,
384 .bDescriptorType = USB_DT_ENDPOINT,
385
386 .bEndpointAddress = USB_DIR_OUT,
387 .bmAttributes = USB_ENDPOINT_XFER_BULK,
388 /* wMaxPacketSize set by autoconfiguration */
389};
390
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100391#ifndef FSG_NO_INTR_EP
392
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100393static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100394fsg_fs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100395 .bLength = USB_DT_ENDPOINT_SIZE,
396 .bDescriptorType = USB_DT_ENDPOINT,
397
398 .bEndpointAddress = USB_DIR_IN,
399 .bmAttributes = USB_ENDPOINT_XFER_INT,
400 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100401 .bInterval = 32, /* frames -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100402};
403
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100404#ifndef FSG_NO_OTG
405# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
406#else
407# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
408#endif
409
410#endif
411
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100412static struct usb_descriptor_header *fsg_fs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100413#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100414 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100415#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100416 (struct usb_descriptor_header *) &fsg_intf_desc,
417 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
418 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100419#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100420 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100421#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100422 NULL,
423};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100424
425
426/*
427 * USB 2.0 devices need to expose both high speed and full speed
428 * descriptors, unless they only run at full speed.
429 *
430 * That means alternate endpoint descriptors (bigger packets)
431 * and a "device qualifier" ... plus more construction options
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200432 * for the configuration descriptor.
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100433 */
434static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100435fsg_hs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100436 .bLength = USB_DT_ENDPOINT_SIZE,
437 .bDescriptorType = USB_DT_ENDPOINT,
438
439 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
440 .bmAttributes = USB_ENDPOINT_XFER_BULK,
441 .wMaxPacketSize = cpu_to_le16(512),
442};
443
444static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100445fsg_hs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100446 .bLength = USB_DT_ENDPOINT_SIZE,
447 .bDescriptorType = USB_DT_ENDPOINT,
448
449 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
450 .bmAttributes = USB_ENDPOINT_XFER_BULK,
451 .wMaxPacketSize = cpu_to_le16(512),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100452 .bInterval = 1, /* NAK every 1 uframe */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100453};
454
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100455#ifndef FSG_NO_INTR_EP
456
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100457static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100458fsg_hs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100459 .bLength = USB_DT_ENDPOINT_SIZE,
460 .bDescriptorType = USB_DT_ENDPOINT,
461
462 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
463 .bmAttributes = USB_ENDPOINT_XFER_INT,
464 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100465 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100466};
467
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100468#ifndef FSG_NO_OTG
469# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
470#else
471# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
472#endif
473
474#endif
475
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100476static struct usb_descriptor_header *fsg_hs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100477#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100478 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100479#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100480 (struct usb_descriptor_header *) &fsg_intf_desc,
481 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
482 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100483#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100484 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100485#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100486 NULL,
487};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100488
489/* Maxpacket and other transfer characteristics vary by speed. */
490static struct usb_endpoint_descriptor *
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100491fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100492 struct usb_endpoint_descriptor *hs)
493{
494 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
495 return hs;
496 return fs;
497}
498
499
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100500/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100501static struct usb_string fsg_strings[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100502#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100503 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
504 {FSG_STRING_PRODUCT, fsg_string_product},
Alan Sternd8087422010-09-03 11:15:41 -0400505 {FSG_STRING_SERIAL, ""},
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100506 {FSG_STRING_CONFIG, fsg_string_config},
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100507#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100508 {FSG_STRING_INTERFACE, fsg_string_interface},
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100509 {}
510};
511
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100512static struct usb_gadget_strings fsg_stringtab = {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100513 .language = 0x0409, /* en-us */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100514 .strings = fsg_strings,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100515};
516
517
518 /*-------------------------------------------------------------------------*/
519
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200520/*
521 * If the next two routines are called while the gadget is registered,
522 * the caller must own fsg->filesem for writing.
523 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100524
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100525static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100526{
527 int ro;
528 struct file *filp = NULL;
529 int rc = -EINVAL;
530 struct inode *inode = NULL;
531 loff_t size;
532 loff_t num_sectors;
533 loff_t min_sectors;
534
535 /* R/W if we can, R/O if we must */
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100536 ro = curlun->initially_ro;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100537 if (!ro) {
538 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
Tejun Heo75f1dc02010-11-13 11:55:17 +0100539 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100540 ro = 1;
541 }
542 if (ro)
543 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
544 if (IS_ERR(filp)) {
545 LINFO(curlun, "unable to open backing file: %s\n", filename);
546 return PTR_ERR(filp);
547 }
548
549 if (!(filp->f_mode & FMODE_WRITE))
550 ro = 1;
551
552 if (filp->f_path.dentry)
553 inode = filp->f_path.dentry->d_inode;
Tejun Heo75f1dc02010-11-13 11:55:17 +0100554 if (!inode || (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100555 LINFO(curlun, "invalid file type: %s\n", filename);
556 goto out;
557 }
558
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200559 /*
560 * If we can't read the file, it's no good.
561 * If we can't write the file, use it read-only.
562 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100563 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
564 LINFO(curlun, "file not readable: %s\n", filename);
565 goto out;
566 }
567 if (!(filp->f_op->write || filp->f_op->aio_write))
568 ro = 1;
569
570 size = i_size_read(inode->i_mapping->host);
571 if (size < 0) {
572 LINFO(curlun, "unable to find file size: %s\n", filename);
573 rc = (int) size;
574 goto out;
575 }
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100576 num_sectors = size >> 9; /* File size in 512-byte blocks */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100577 min_sectors = 1;
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100578 if (curlun->cdrom) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100579 num_sectors &= ~3; /* Reduce to a multiple of 2048 */
580 min_sectors = 300*4; /* Smallest track is 300 frames */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100581 if (num_sectors >= 256*60*75*4) {
582 num_sectors = (256*60*75 - 1) * 4;
583 LINFO(curlun, "file too big: %s\n", filename);
584 LINFO(curlun, "using only first %d blocks\n",
585 (int) num_sectors);
586 }
587 }
588 if (num_sectors < min_sectors) {
589 LINFO(curlun, "file too small: %s\n", filename);
590 rc = -ETOOSMALL;
591 goto out;
592 }
593
594 get_file(filp);
595 curlun->ro = ro;
596 curlun->filp = filp;
597 curlun->file_length = size;
598 curlun->num_sectors = num_sectors;
599 LDBG(curlun, "open backing file: %s\n", filename);
600 rc = 0;
601
602out:
603 filp_close(filp, current->files);
604 return rc;
605}
606
607
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100608static void fsg_lun_close(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100609{
610 if (curlun->filp) {
611 LDBG(curlun, "close backing file\n");
612 fput(curlun->filp);
613 curlun->filp = NULL;
614 }
615}
616
617
618/*-------------------------------------------------------------------------*/
619
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200620/*
621 * Sync the file data, don't bother with the metadata.
622 * This code was copied from fs/buffer.c:sys_fdatasync().
623 */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100624static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100625{
626 struct file *filp = curlun->filp;
627
628 if (curlun->ro || !filp)
629 return 0;
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100630 return vfs_fsync(filp, 1);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100631}
632
633static void store_cdrom_address(u8 *dest, int msf, u32 addr)
634{
635 if (msf) {
636 /* Convert to Minutes-Seconds-Frames */
637 addr >>= 2; /* Convert to 2048-byte frames */
638 addr += 2*75; /* Lead-in occupies 2 seconds */
639 dest[3] = addr % 75; /* Frames */
640 addr /= 75;
641 dest[2] = addr % 60; /* Seconds */
642 addr /= 60;
643 dest[1] = addr; /* Minutes */
644 dest[0] = 0; /* Reserved */
645 } else {
646 /* Absolute sector */
647 put_unaligned_be32(addr, dest);
648 }
649}
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100650
651
652/*-------------------------------------------------------------------------*/
653
654
655static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
656 char *buf)
657{
658 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
659
660 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
661 ? curlun->ro
662 : curlun->initially_ro);
663}
664
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300665static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
666 char *buf)
667{
668 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
669
670 return sprintf(buf, "%u\n", curlun->nofua);
671}
672
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100673static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
674 char *buf)
675{
676 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
677 struct rw_semaphore *filesem = dev_get_drvdata(dev);
678 char *p;
679 ssize_t rc;
680
681 down_read(filesem);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100682 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100683 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
684 if (IS_ERR(p))
685 rc = PTR_ERR(p);
686 else {
687 rc = strlen(p);
688 memmove(buf, p, rc);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100689 buf[rc] = '\n'; /* Add a newline */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100690 buf[++rc] = 0;
691 }
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100692 } else { /* No file, return 0 bytes */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100693 *buf = 0;
694 rc = 0;
695 }
696 up_read(filesem);
697 return rc;
698}
699
700
701static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
702 const char *buf, size_t count)
703{
704 ssize_t rc = count;
705 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
706 struct rw_semaphore *filesem = dev_get_drvdata(dev);
Andy Shevchenko8156d152010-07-22 11:58:47 +0300707 unsigned long ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100708
Andy Shevchenko8156d152010-07-22 11:58:47 +0300709 if (strict_strtoul(buf, 2, &ro))
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100710 return -EINVAL;
711
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200712 /*
713 * Allow the write-enable status to change only while the
714 * backing file is closed.
715 */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100716 down_read(filesem);
717 if (fsg_lun_is_open(curlun)) {
718 LDBG(curlun, "read-only status change prevented\n");
719 rc = -EBUSY;
720 } else {
Andy Shevchenko8156d152010-07-22 11:58:47 +0300721 curlun->ro = ro;
722 curlun->initially_ro = ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100723 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
724 }
725 up_read(filesem);
726 return rc;
727}
728
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300729static ssize_t fsg_store_nofua(struct device *dev,
730 struct device_attribute *attr,
731 const char *buf, size_t count)
732{
733 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
734 unsigned long nofua;
735
736 if (strict_strtoul(buf, 2, &nofua))
737 return -EINVAL;
738
739 /* Sync data when switching from async mode to sync */
740 if (!nofua && curlun->nofua)
741 fsg_lun_fsync_sub(curlun);
742
743 curlun->nofua = nofua;
744
745 return count;
746}
747
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100748static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
749 const char *buf, size_t count)
750{
751 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
752 struct rw_semaphore *filesem = dev_get_drvdata(dev);
753 int rc = 0;
754
755 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
756 LDBG(curlun, "eject attempt prevented\n");
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100757 return -EBUSY; /* "Door is locked" */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100758 }
759
760 /* Remove a trailing newline */
761 if (count > 0 && buf[count-1] == '\n')
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100762 ((char *) buf)[count-1] = 0; /* Ugh! */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100763
764 /* Eject current medium */
765 down_write(filesem);
766 if (fsg_lun_is_open(curlun)) {
767 fsg_lun_close(curlun);
768 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
769 }
770
771 /* Load new medium */
772 if (count > 0 && buf[0]) {
773 rc = fsg_lun_open(curlun, buf);
774 if (rc == 0)
775 curlun->unit_attention_data =
776 SS_NOT_READY_TO_READY_TRANSITION;
777 }
778 up_write(filesem);
779 return (rc < 0 ? rc : count);
780}