blob: 523626274c0f9bfc0f490eb6f575ece95677718c [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.
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010012 */
13
14
Michal Nazarewiczd6181702009-10-28 16:57:15 +010015/*
16 * This file requires the following identifiers used in USB strings to
17 * be defined (each of type pointer to char):
18 * - fsg_string_manufacturer -- name of the manufacturer
19 * - fsg_string_product -- name of the product
Michal Nazarewiczd6181702009-10-28 16:57:15 +010020 * - fsg_string_config -- name of the configuration
21 * - fsg_string_interface -- name of the interface
22 * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
23 * macro is defined prior to including this file.
24 */
25
Michal Nazarewicz93bcf122009-10-28 16:57:19 +010026/*
27 * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
28 * fsg_hs_intr_in_desc objects as well as
29 * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
30 * macros are not defined.
31 *
32 * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
33 * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
34 * defined (as well as corresponding entries in string tables are
35 * missing) and FSG_STRING_INTERFACE has value of zero.
36 *
37 * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
38 */
39
Michal Nazarewicz606206c2009-10-28 16:57:21 +010040/*
41 * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
42 * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
43 * characters rather then a pointer to void.
44 */
45
Per Forlin6532c7f2011-08-19 21:21:27 +020046/*
47 * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
48 * sets the number of pipeline buffers (length of the fsg_buffhd array).
49 * The valid range of num_buffers is: num >= 2 && num <= 4.
50 */
51
Michal Nazarewiczd6181702009-10-28 16:57:15 +010052
Michal Nazarewicz3323b712010-10-07 13:05:24 +020053#include <linux/usb/storage.h>
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +020054#include <scsi/scsi.h>
55#include <asm/unaligned.h>
Michal Nazarewiczd6181702009-10-28 16:57:15 +010056
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010057
Michal Nazarewiczd0893262010-07-05 16:38:04 +020058/*
59 * Thanks to NetChip Technologies for donating this product ID.
Michal Nazarewicz93f93742009-10-28 16:57:17 +010060 *
61 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
Michal Nazarewiczd0893262010-07-05 16:38:04 +020062 * Instead: allocate your own, using normal USB-IF procedures.
63 */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +010064#define FSG_VENDOR_ID 0x0525 /* NetChip */
65#define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
Michal Nazarewicz93f93742009-10-28 16:57:17 +010066
67
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010068/*-------------------------------------------------------------------------*/
69
70
71#ifndef DEBUG
72#undef VERBOSE_DEBUG
73#undef DUMP_MSGS
74#endif /* !DEBUG */
75
76#ifdef VERBOSE_DEBUG
77#define VLDBG LDBG
78#else
79#define VLDBG(lun, fmt, args...) do { } while (0)
80#endif /* VERBOSE_DEBUG */
81
82#define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
83#define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
84#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
85#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
86
Michal Nazarewiczd0893262010-07-05 16:38:04 +020087/*
88 * Keep those macros in sync with those in
89 * include/linux/usb/composite.h or else GCC will complain. If they
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010090 * are identical (the same names of arguments, white spaces in the
91 * same places) GCC will allow redefinition otherwise (even if some
Michal Nazarewiczd0893262010-07-05 16:38:04 +020092 * white space is removed or added) warning will be issued.
93 *
94 * Those macros are needed here because File Storage Gadget does not
95 * include the composite.h header. For composite gadgets those macros
96 * are redundant since composite.h is included any way.
97 *
98 * One could check whether those macros are already defined (which
99 * would indicate composite.h had been included) or not (which would
100 * indicate we were in FSG) but this is not done because a warning is
101 * desired if definitions here differ from the ones in composite.h.
102 *
103 * We want the definitions to match and be the same in File Storage
104 * Gadget as well as Mass Storage Function (and so composite gadgets
105 * using MSF). If someone changes them in composite.h it will produce
106 * a warning in this file when building MSF.
107 */
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100108#define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
109#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
110#define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
111#define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
112#define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100113
114
115
116#ifdef DUMP_MSGS
117
118# define dump_msg(fsg, /* const char * */ label, \
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100119 /* const u8 * */ buf, /* unsigned */ length) do { \
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100120 if (length < 512) { \
121 DBG(fsg, "%s, length %u:\n", label, length); \
122 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
123 16, 1, buf, length, 0); \
124 } \
125} while (0)
126
127# define dump_cdb(fsg) do { } while (0)
128
129#else
130
131# define dump_msg(fsg, /* const char * */ label, \
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100132 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100133
134# ifdef VERBOSE_DEBUG
135
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100136# define dump_cdb(fsg) \
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100137 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
138 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
139
140# else
141
142# define dump_cdb(fsg) do { } while (0)
143
144# endif /* VERBOSE_DEBUG */
145
146#endif /* DUMP_MSGS */
147
148
149
150
151
152/*-------------------------------------------------------------------------*/
153
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100154/* Bulk-only data structures */
155
156/* Command Block Wrapper */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100157struct fsg_bulk_cb_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100158 __le32 Signature; /* Contains 'USBC' */
159 u32 Tag; /* Unique per command id */
160 __le32 DataTransferLength; /* Size of the data */
161 u8 Flags; /* Direction in bit 7 */
162 u8 Lun; /* LUN (normally 0) */
163 u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
164 u8 CDB[16]; /* Command Data Block */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100165};
166
167#define USB_BULK_CB_WRAP_LEN 31
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100168#define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100169#define USB_BULK_IN_FLAG 0x80
170
171/* Command Status Wrapper */
172struct bulk_cs_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100173 __le32 Signature; /* Should = 'USBS' */
174 u32 Tag; /* Same as original command */
175 __le32 Residue; /* Amount not transferred */
176 u8 Status; /* See below */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100177};
178
179#define USB_BULK_CS_WRAP_LEN 13
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100180#define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100181#define USB_STATUS_PASS 0
182#define USB_STATUS_FAIL 1
183#define USB_STATUS_PHASE_ERROR 2
184
185/* Bulk-only class specific requests */
186#define USB_BULK_RESET_REQUEST 0xff
187#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
188
189
190/* CBI Interrupt data structure */
191struct interrupt_data {
192 u8 bType;
193 u8 bValue;
194};
195
196#define CBI_INTERRUPT_DATA_LEN 2
197
198/* CBI Accept Device-Specific Command request */
199#define USB_CBI_ADSC_REQUEST 0x00
200
201
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100202/* Length of a SCSI Command Data Block */
203#define MAX_COMMAND_SIZE 16
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100204
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100205/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
206#define SS_NO_SENSE 0
207#define SS_COMMUNICATION_FAILURE 0x040800
208#define SS_INVALID_COMMAND 0x052000
209#define SS_INVALID_FIELD_IN_CDB 0x052400
210#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
211#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
212#define SS_MEDIUM_NOT_PRESENT 0x023a00
213#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
214#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
215#define SS_RESET_OCCURRED 0x062900
216#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
217#define SS_UNRECOVERED_READ_ERROR 0x031100
218#define SS_WRITE_ERROR 0x030c02
219#define SS_WRITE_PROTECTED 0x072700
220
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100221#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100222#define ASC(x) ((u8) ((x) >> 8))
223#define ASCQ(x) ((u8) (x))
224
225
226/*-------------------------------------------------------------------------*/
227
228
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100229struct fsg_lun {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100230 struct file *filp;
231 loff_t file_length;
232 loff_t num_sectors;
233
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100234 unsigned int initially_ro:1;
235 unsigned int ro:1;
236 unsigned int removable:1;
237 unsigned int cdrom:1;
238 unsigned int prevent_medium_removal:1;
239 unsigned int registered:1;
240 unsigned int info_valid:1;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300241 unsigned int nofua:1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100242
243 u32 sense_data;
244 u32 sense_data_info;
245 u32 unit_attention_data;
246
Peiyu Li3f565a32011-08-17 22:52:59 -0700247 unsigned int blkbits; /* Bits of logical block size of bound block device */
248 unsigned int blksize; /* logical block size of bound block device */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100249 struct device dev;
250};
251
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100252#define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100253
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100254static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100255{
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100256 return container_of(dev, struct fsg_lun, dev);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100257}
258
259
260/* Big enough to hold our biggest descriptor */
261#define EP0_BUFSIZE 256
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100262#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100263
Per Forlin6532c7f2011-08-19 21:21:27 +0200264#ifdef CONFIG_USB_GADGET_DEBUG_FILES
265
266static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
267module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);
268MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers");
269
270#else
271
272/*
273 * Number of buffers we will use.
274 * 2 is usually enough for good buffering pipeline
275 */
276#define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
277
278#endif /* CONFIG_USB_DEBUG */
279
280/* check if fsg_num_buffers is within a valid range */
281static inline int fsg_num_buffers_validate(void)
282{
283 if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4)
284 return 0;
285 pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
286 fsg_num_buffers, 2 ,4);
287 return -EINVAL;
288}
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100289
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100290/* Default size of buffer length. */
291#define FSG_BUFLEN ((u32)16384)
292
293/* Maximal number of LUNs supported in mass storage function */
294#define FSG_MAX_LUNS 8
295
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100296enum fsg_buffer_state {
297 BUF_STATE_EMPTY = 0,
298 BUF_STATE_FULL,
299 BUF_STATE_BUSY
300};
301
302struct fsg_buffhd {
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100303#ifdef FSG_BUFFHD_STATIC_BUFFER
304 char buf[FSG_BUFLEN];
305#else
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100306 void *buf;
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100307#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100308 enum fsg_buffer_state state;
309 struct fsg_buffhd *next;
310
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -0700311 /*
312 * The NetChip 2280 is faster, and handles some protocol faults
313 * better, if we don't submit any short bulk-out read requests.
314 * So we will record the intended request length here.
315 */
316 unsigned int bulk_out_intended_length;
317
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100318 struct usb_request *inreq;
319 int inreq_busy;
320 struct usb_request *outreq;
321 int outreq_busy;
322};
323
324enum fsg_state {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100325 /* This one isn't used anywhere */
326 FSG_STATE_COMMAND_PHASE = -10,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100327 FSG_STATE_DATA_PHASE,
328 FSG_STATE_STATUS_PHASE,
329
330 FSG_STATE_IDLE = 0,
331 FSG_STATE_ABORT_BULK_OUT,
332 FSG_STATE_RESET,
333 FSG_STATE_INTERFACE_CHANGE,
334 FSG_STATE_CONFIG_CHANGE,
335 FSG_STATE_DISCONNECT,
336 FSG_STATE_EXIT,
337 FSG_STATE_TERMINATED
338};
339
340enum data_direction {
341 DATA_DIR_UNKNOWN = 0,
342 DATA_DIR_FROM_HOST,
343 DATA_DIR_TO_HOST,
344 DATA_DIR_NONE
345};
346
347
348/*-------------------------------------------------------------------------*/
349
350
351static inline u32 get_unaligned_be24(u8 *buf)
352{
353 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
354}
355
356
357/*-------------------------------------------------------------------------*/
358
359
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100360enum {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100361#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100362 FSG_STRING_MANUFACTURER = 1,
363 FSG_STRING_PRODUCT,
364 FSG_STRING_SERIAL,
365 FSG_STRING_CONFIG,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100366#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100367 FSG_STRING_INTERFACE
368};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100369
370
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100371#ifndef FSG_NO_OTG
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100372static struct usb_otg_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100373fsg_otg_desc = {
374 .bLength = sizeof fsg_otg_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100375 .bDescriptorType = USB_DT_OTG,
376
377 .bmAttributes = USB_OTG_SRP,
378};
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100379#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100380
381/* There is only one interface. */
382
383static struct usb_interface_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100384fsg_intf_desc = {
385 .bLength = sizeof fsg_intf_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100386 .bDescriptorType = USB_DT_INTERFACE,
387
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100388 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100389 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100390 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
391 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100392 .iInterface = FSG_STRING_INTERFACE,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100393};
394
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200395/*
396 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
397 * interrupt-in.
398 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100399
400static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100401fsg_fs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100402 .bLength = USB_DT_ENDPOINT_SIZE,
403 .bDescriptorType = USB_DT_ENDPOINT,
404
405 .bEndpointAddress = USB_DIR_IN,
406 .bmAttributes = USB_ENDPOINT_XFER_BULK,
407 /* wMaxPacketSize set by autoconfiguration */
408};
409
410static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100411fsg_fs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100412 .bLength = USB_DT_ENDPOINT_SIZE,
413 .bDescriptorType = USB_DT_ENDPOINT,
414
415 .bEndpointAddress = USB_DIR_OUT,
416 .bmAttributes = USB_ENDPOINT_XFER_BULK,
417 /* wMaxPacketSize set by autoconfiguration */
418};
419
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100420#ifndef FSG_NO_INTR_EP
421
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100422static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100423fsg_fs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100424 .bLength = USB_DT_ENDPOINT_SIZE,
425 .bDescriptorType = USB_DT_ENDPOINT,
426
427 .bEndpointAddress = USB_DIR_IN,
428 .bmAttributes = USB_ENDPOINT_XFER_INT,
429 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100430 .bInterval = 32, /* frames -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100431};
432
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100433#ifndef FSG_NO_OTG
434# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
435#else
436# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
437#endif
438
439#endif
440
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100441static struct usb_descriptor_header *fsg_fs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100442#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100443 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100444#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100445 (struct usb_descriptor_header *) &fsg_intf_desc,
446 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
447 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100448#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100449 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100450#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100451 NULL,
452};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100453
454
455/*
456 * USB 2.0 devices need to expose both high speed and full speed
457 * descriptors, unless they only run at full speed.
458 *
459 * That means alternate endpoint descriptors (bigger packets)
460 * and a "device qualifier" ... plus more construction options
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200461 * for the configuration descriptor.
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100462 */
463static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100464fsg_hs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100465 .bLength = USB_DT_ENDPOINT_SIZE,
466 .bDescriptorType = USB_DT_ENDPOINT,
467
468 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
469 .bmAttributes = USB_ENDPOINT_XFER_BULK,
470 .wMaxPacketSize = cpu_to_le16(512),
471};
472
473static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100474fsg_hs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100475 .bLength = USB_DT_ENDPOINT_SIZE,
476 .bDescriptorType = USB_DT_ENDPOINT,
477
478 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
479 .bmAttributes = USB_ENDPOINT_XFER_BULK,
480 .wMaxPacketSize = cpu_to_le16(512),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100481 .bInterval = 1, /* NAK every 1 uframe */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100482};
483
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100484#ifndef FSG_NO_INTR_EP
485
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100486static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100487fsg_hs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100488 .bLength = USB_DT_ENDPOINT_SIZE,
489 .bDescriptorType = USB_DT_ENDPOINT,
490
491 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
492 .bmAttributes = USB_ENDPOINT_XFER_INT,
493 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100494 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100495};
496
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100497#ifndef FSG_NO_OTG
498# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
499#else
500# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
501#endif
502
503#endif
504
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100505static struct usb_descriptor_header *fsg_hs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100506#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100507 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100508#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100509 (struct usb_descriptor_header *) &fsg_intf_desc,
510 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
511 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100512#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100513 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100514#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100515 NULL,
516};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100517
518/* Maxpacket and other transfer characteristics vary by speed. */
Tatyana Brokhmanbc8687d2011-06-30 08:44:42 +0300519static __maybe_unused struct usb_endpoint_descriptor *
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100520fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100521 struct usb_endpoint_descriptor *hs)
522{
523 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
524 return hs;
525 return fs;
526}
527
528
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100529/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100530static struct usb_string fsg_strings[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100531#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100532 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
533 {FSG_STRING_PRODUCT, fsg_string_product},
Alan Sternd8087422010-09-03 11:15:41 -0400534 {FSG_STRING_SERIAL, ""},
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100535 {FSG_STRING_CONFIG, fsg_string_config},
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100536#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100537 {FSG_STRING_INTERFACE, fsg_string_interface},
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100538 {}
539};
540
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100541static struct usb_gadget_strings fsg_stringtab = {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100542 .language = 0x0409, /* en-us */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100543 .strings = fsg_strings,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100544};
545
546
547 /*-------------------------------------------------------------------------*/
548
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200549/*
550 * If the next two routines are called while the gadget is registered,
551 * the caller must own fsg->filesem for writing.
552 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100553
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100554static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100555{
556 int ro;
557 struct file *filp = NULL;
558 int rc = -EINVAL;
559 struct inode *inode = NULL;
560 loff_t size;
561 loff_t num_sectors;
562 loff_t min_sectors;
563
564 /* R/W if we can, R/O if we must */
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100565 ro = curlun->initially_ro;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100566 if (!ro) {
567 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
Tejun Heo75f1dc02010-11-13 11:55:17 +0100568 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100569 ro = 1;
570 }
571 if (ro)
572 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
573 if (IS_ERR(filp)) {
574 LINFO(curlun, "unable to open backing file: %s\n", filename);
575 return PTR_ERR(filp);
576 }
577
578 if (!(filp->f_mode & FMODE_WRITE))
579 ro = 1;
580
581 if (filp->f_path.dentry)
582 inode = filp->f_path.dentry->d_inode;
Tejun Heo75f1dc02010-11-13 11:55:17 +0100583 if (!inode || (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100584 LINFO(curlun, "invalid file type: %s\n", filename);
585 goto out;
586 }
587
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200588 /*
589 * If we can't read the file, it's no good.
590 * If we can't write the file, use it read-only.
591 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100592 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
593 LINFO(curlun, "file not readable: %s\n", filename);
594 goto out;
595 }
596 if (!(filp->f_op->write || filp->f_op->aio_write))
597 ro = 1;
598
599 size = i_size_read(inode->i_mapping->host);
600 if (size < 0) {
601 LINFO(curlun, "unable to find file size: %s\n", filename);
602 rc = (int) size;
603 goto out;
604 }
Peiyu Li3f565a32011-08-17 22:52:59 -0700605
606 if (curlun->cdrom) {
607 curlun->blksize = 2048;
608 curlun->blkbits = 11;
609 } else if (inode->i_bdev) {
610 curlun->blksize = bdev_logical_block_size(inode->i_bdev);
611 curlun->blkbits = blksize_bits(curlun->blksize);
612 } else {
613 curlun->blksize = 512;
614 curlun->blkbits = 9;
615 }
616
617 num_sectors = size >> curlun->blkbits; /* File size in logic-block-size blocks */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100618 min_sectors = 1;
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100619 if (curlun->cdrom) {
Peiyu Li3f565a32011-08-17 22:52:59 -0700620 min_sectors = 300; /* Smallest track is 300 frames */
621 if (num_sectors >= 256*60*75) {
622 num_sectors = 256*60*75 - 1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100623 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{
Michal Nazarewiczfd4477b2011-04-14 11:55:43 +0200744 ssize_t rc;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100745 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
746 struct rw_semaphore *filesem = dev_get_drvdata(dev);
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200747 unsigned ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100748
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200749 rc = kstrtouint(buf, 2, &ro);
750 if (rc)
751 return rc;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100752
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200753 /*
754 * Allow the write-enable status to change only while the
755 * backing file is closed.
756 */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100757 down_read(filesem);
758 if (fsg_lun_is_open(curlun)) {
759 LDBG(curlun, "read-only status change prevented\n");
760 rc = -EBUSY;
761 } else {
Andy Shevchenko8156d152010-07-22 11:58:47 +0300762 curlun->ro = ro;
763 curlun->initially_ro = ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100764 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
Michal Nazarewiczfd4477b2011-04-14 11:55:43 +0200765 rc = count;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100766 }
767 up_read(filesem);
768 return rc;
769}
770
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300771static ssize_t fsg_store_nofua(struct device *dev,
772 struct device_attribute *attr,
773 const char *buf, size_t count)
774{
775 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200776 unsigned nofua;
777 int ret;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300778
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200779 ret = kstrtouint(buf, 2, &nofua);
780 if (ret)
781 return ret;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300782
783 /* Sync data when switching from async mode to sync */
784 if (!nofua && curlun->nofua)
785 fsg_lun_fsync_sub(curlun);
786
787 curlun->nofua = nofua;
788
789 return count;
790}
791
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100792static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
793 const char *buf, size_t count)
794{
795 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
796 struct rw_semaphore *filesem = dev_get_drvdata(dev);
797 int rc = 0;
798
799 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
800 LDBG(curlun, "eject attempt prevented\n");
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100801 return -EBUSY; /* "Door is locked" */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100802 }
803
804 /* Remove a trailing newline */
805 if (count > 0 && buf[count-1] == '\n')
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100806 ((char *) buf)[count-1] = 0; /* Ugh! */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100807
808 /* Eject current medium */
809 down_write(filesem);
810 if (fsg_lun_is_open(curlun)) {
811 fsg_lun_close(curlun);
812 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
813 }
814
815 /* Load new medium */
816 if (count > 0 && buf[0]) {
817 rc = fsg_lun_open(curlun, buf);
818 if (rc == 0)
819 curlun->unit_attention_data =
820 SS_NOT_READY_TO_READY_TRANSITION;
821 }
822 up_write(filesem);
823 return (rc < 0 ? rc : count);
824}