blob: 7e81a2d898f01a51b5838a4f5277fd3a60f2bc54 [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 Nazarewicz93f93742009-10-28 16:57:17 +010060/* Thanks to NetChip Technologies for donating this product ID.
61 *
62 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
63 * Instead: allocate your own, using normal USB-IF procedures. */
64#define FSG_VENDOR_ID 0x0525 // NetChip
65#define FSG_PRODUCT_ID 0xa4a5 // Linux-USB File-backed Storage Gadget
66
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 Nazarewiczd23b0f02009-11-09 14:15:20 +010087/* Keep those macros in sync with thos in
88 * include/linux/ubs/composite.h or else GCC will complain. If they
89 * are identical (the same names of arguments, white spaces in the
90 * same places) GCC will allow redefinition otherwise (even if some
91 * white space is removed or added) warning will be issued. No
92 * checking if those symbols is defined is performed because warning
93 * is desired when those macros were defined by someone else to mean
94 * something else. */
95#define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
96#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
97#define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
98#define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
99#define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100100
101
102
103#ifdef DUMP_MSGS
104
105# define dump_msg(fsg, /* const char * */ label, \
106 /* const u8 * */ buf, /* unsigned */ length) do { \
107 if (length < 512) { \
108 DBG(fsg, "%s, length %u:\n", label, length); \
109 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
110 16, 1, buf, length, 0); \
111 } \
112} while (0)
113
114# define dump_cdb(fsg) do { } while (0)
115
116#else
117
118# define dump_msg(fsg, /* const char * */ label, \
119 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
120
121# ifdef VERBOSE_DEBUG
122
123#define dump_cdb(fsg) \
124 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
125 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
126
127# else
128
129# define dump_cdb(fsg) do { } while (0)
130
131# endif /* VERBOSE_DEBUG */
132
133#endif /* DUMP_MSGS */
134
135
136
137
138
139/*-------------------------------------------------------------------------*/
140
141/* SCSI device types */
142#define TYPE_DISK 0x00
143#define TYPE_CDROM 0x05
144
145/* USB protocol value = the transport method */
146#define USB_PR_CBI 0x00 // Control/Bulk/Interrupt
147#define USB_PR_CB 0x01 // Control/Bulk w/o interrupt
148#define USB_PR_BULK 0x50 // Bulk-only
149
150/* USB subclass value = the protocol encapsulation */
151#define USB_SC_RBC 0x01 // Reduced Block Commands (flash)
152#define USB_SC_8020 0x02 // SFF-8020i, MMC-2, ATAPI (CD-ROM)
153#define USB_SC_QIC 0x03 // QIC-157 (tape)
154#define USB_SC_UFI 0x04 // UFI (floppy)
155#define USB_SC_8070 0x05 // SFF-8070i (removable)
156#define USB_SC_SCSI 0x06 // Transparent SCSI
157
158/* Bulk-only data structures */
159
160/* Command Block Wrapper */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100161struct fsg_bulk_cb_wrap {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100162 __le32 Signature; // Contains 'USBC'
163 u32 Tag; // Unique per command id
164 __le32 DataTransferLength; // Size of the data
165 u8 Flags; // Direction in bit 7
166 u8 Lun; // LUN (normally 0)
167 u8 Length; // Of the CDB, <= MAX_COMMAND_SIZE
168 u8 CDB[16]; // Command Data Block
169};
170
171#define USB_BULK_CB_WRAP_LEN 31
172#define USB_BULK_CB_SIG 0x43425355 // Spells out USBC
173#define USB_BULK_IN_FLAG 0x80
174
175/* Command Status Wrapper */
176struct bulk_cs_wrap {
177 __le32 Signature; // Should = 'USBS'
178 u32 Tag; // Same as original command
179 __le32 Residue; // Amount not transferred
180 u8 Status; // See below
181};
182
183#define USB_BULK_CS_WRAP_LEN 13
184#define USB_BULK_CS_SIG 0x53425355 // Spells out 'USBS'
185#define USB_STATUS_PASS 0
186#define USB_STATUS_FAIL 1
187#define USB_STATUS_PHASE_ERROR 2
188
189/* Bulk-only class specific requests */
190#define USB_BULK_RESET_REQUEST 0xff
191#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
192
193
194/* CBI Interrupt data structure */
195struct interrupt_data {
196 u8 bType;
197 u8 bValue;
198};
199
200#define CBI_INTERRUPT_DATA_LEN 2
201
202/* CBI Accept Device-Specific Command request */
203#define USB_CBI_ADSC_REQUEST 0x00
204
205
206#define MAX_COMMAND_SIZE 16 // Length of a SCSI Command Data Block
207
208/* SCSI commands that we recognize */
209#define SC_FORMAT_UNIT 0x04
210#define SC_INQUIRY 0x12
211#define SC_MODE_SELECT_6 0x15
212#define SC_MODE_SELECT_10 0x55
213#define SC_MODE_SENSE_6 0x1a
214#define SC_MODE_SENSE_10 0x5a
215#define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
216#define SC_READ_6 0x08
217#define SC_READ_10 0x28
218#define SC_READ_12 0xa8
219#define SC_READ_CAPACITY 0x25
220#define SC_READ_FORMAT_CAPACITIES 0x23
221#define SC_READ_HEADER 0x44
222#define SC_READ_TOC 0x43
223#define SC_RELEASE 0x17
224#define SC_REQUEST_SENSE 0x03
225#define SC_RESERVE 0x16
226#define SC_SEND_DIAGNOSTIC 0x1d
227#define SC_START_STOP_UNIT 0x1b
228#define SC_SYNCHRONIZE_CACHE 0x35
229#define SC_TEST_UNIT_READY 0x00
230#define SC_VERIFY 0x2f
231#define SC_WRITE_6 0x0a
232#define SC_WRITE_10 0x2a
233#define SC_WRITE_12 0xaa
234
235/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
236#define SS_NO_SENSE 0
237#define SS_COMMUNICATION_FAILURE 0x040800
238#define SS_INVALID_COMMAND 0x052000
239#define SS_INVALID_FIELD_IN_CDB 0x052400
240#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
241#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
242#define SS_MEDIUM_NOT_PRESENT 0x023a00
243#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
244#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
245#define SS_RESET_OCCURRED 0x062900
246#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
247#define SS_UNRECOVERED_READ_ERROR 0x031100
248#define SS_WRITE_ERROR 0x030c02
249#define SS_WRITE_PROTECTED 0x072700
250
251#define SK(x) ((u8) ((x) >> 16)) // Sense Key byte, etc.
252#define ASC(x) ((u8) ((x) >> 8))
253#define ASCQ(x) ((u8) (x))
254
255
256/*-------------------------------------------------------------------------*/
257
258
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100259struct fsg_lun {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100260 struct file *filp;
261 loff_t file_length;
262 loff_t num_sectors;
263
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100264 unsigned int initially_ro : 1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100265 unsigned int ro : 1;
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100266 unsigned int removable : 1;
267 unsigned int cdrom : 1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100268 unsigned int prevent_medium_removal : 1;
269 unsigned int registered : 1;
270 unsigned int info_valid : 1;
271
272 u32 sense_data;
273 u32 sense_data_info;
274 u32 unit_attention_data;
275
276 struct device dev;
277};
278
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100279#define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100280
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100281static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100282{
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100283 return container_of(dev, struct fsg_lun, dev);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100284}
285
286
287/* Big enough to hold our biggest descriptor */
288#define EP0_BUFSIZE 256
289#define DELAYED_STATUS (EP0_BUFSIZE + 999) // An impossibly large value
290
291/* Number of buffers we will use. 2 is enough for double-buffering */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100292#define FSG_NUM_BUFFERS 2
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100293
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100294/* Default size of buffer length. */
295#define FSG_BUFLEN ((u32)16384)
296
297/* Maximal number of LUNs supported in mass storage function */
298#define FSG_MAX_LUNS 8
299
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100300enum fsg_buffer_state {
301 BUF_STATE_EMPTY = 0,
302 BUF_STATE_FULL,
303 BUF_STATE_BUSY
304};
305
306struct fsg_buffhd {
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100307#ifdef FSG_BUFFHD_STATIC_BUFFER
308 char buf[FSG_BUFLEN];
309#else
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100310 void *buf;
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100311#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100312 enum fsg_buffer_state state;
313 struct fsg_buffhd *next;
314
315 /* The NetChip 2280 is faster, and handles some protocol faults
316 * better, if we don't submit any short bulk-out read requests.
317 * So we will record the intended request length here. */
318 unsigned int bulk_out_intended_length;
319
320 struct usb_request *inreq;
321 int inreq_busy;
322 struct usb_request *outreq;
323 int outreq_busy;
324};
325
326enum fsg_state {
327 FSG_STATE_COMMAND_PHASE = -10, // This one isn't used anywhere
328 FSG_STATE_DATA_PHASE,
329 FSG_STATE_STATUS_PHASE,
330
331 FSG_STATE_IDLE = 0,
332 FSG_STATE_ABORT_BULK_OUT,
333 FSG_STATE_RESET,
334 FSG_STATE_INTERFACE_CHANGE,
335 FSG_STATE_CONFIG_CHANGE,
336 FSG_STATE_DISCONNECT,
337 FSG_STATE_EXIT,
338 FSG_STATE_TERMINATED
339};
340
341enum data_direction {
342 DATA_DIR_UNKNOWN = 0,
343 DATA_DIR_FROM_HOST,
344 DATA_DIR_TO_HOST,
345 DATA_DIR_NONE
346};
347
348
349/*-------------------------------------------------------------------------*/
350
351
352static inline u32 get_unaligned_be24(u8 *buf)
353{
354 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
355}
356
357
358/*-------------------------------------------------------------------------*/
359
360
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100361enum {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100362#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100363 FSG_STRING_MANUFACTURER = 1,
364 FSG_STRING_PRODUCT,
365 FSG_STRING_SERIAL,
366 FSG_STRING_CONFIG,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100367#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100368 FSG_STRING_INTERFACE
369};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100370
371
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100372#ifndef FSG_NO_OTG
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100373static struct usb_otg_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100374fsg_otg_desc = {
375 .bLength = sizeof fsg_otg_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100376 .bDescriptorType = USB_DT_OTG,
377
378 .bmAttributes = USB_OTG_SRP,
379};
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100380#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100381
382/* There is only one interface. */
383
384static struct usb_interface_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100385fsg_intf_desc = {
386 .bLength = sizeof fsg_intf_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100387 .bDescriptorType = USB_DT_INTERFACE,
388
389 .bNumEndpoints = 2, // Adjusted during fsg_bind()
390 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
391 .bInterfaceSubClass = USB_SC_SCSI, // Adjusted during fsg_bind()
392 .bInterfaceProtocol = USB_PR_BULK, // Adjusted during fsg_bind()
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100393 .iInterface = FSG_STRING_INTERFACE,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100394};
395
396/* Three full-speed endpoint descriptors: bulk-in, bulk-out,
397 * and interrupt-in. */
398
399static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100400fsg_fs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100401 .bLength = USB_DT_ENDPOINT_SIZE,
402 .bDescriptorType = USB_DT_ENDPOINT,
403
404 .bEndpointAddress = USB_DIR_IN,
405 .bmAttributes = USB_ENDPOINT_XFER_BULK,
406 /* wMaxPacketSize set by autoconfiguration */
407};
408
409static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100410fsg_fs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100411 .bLength = USB_DT_ENDPOINT_SIZE,
412 .bDescriptorType = USB_DT_ENDPOINT,
413
414 .bEndpointAddress = USB_DIR_OUT,
415 .bmAttributes = USB_ENDPOINT_XFER_BULK,
416 /* wMaxPacketSize set by autoconfiguration */
417};
418
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100419#ifndef FSG_NO_INTR_EP
420
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100421static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100422fsg_fs_intr_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_INT,
428 .wMaxPacketSize = cpu_to_le16(2),
429 .bInterval = 32, // frames -> 32 ms
430};
431
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100432#ifndef FSG_NO_OTG
433# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
434#else
435# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
436#endif
437
438#endif
439
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100440static struct usb_descriptor_header *fsg_fs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100441#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100442 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100443#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100444 (struct usb_descriptor_header *) &fsg_intf_desc,
445 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
446 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100447#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100448 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100449#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100450 NULL,
451};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100452
453
454/*
455 * USB 2.0 devices need to expose both high speed and full speed
456 * descriptors, unless they only run at full speed.
457 *
458 * That means alternate endpoint descriptors (bigger packets)
459 * and a "device qualifier" ... plus more construction options
460 * for the config descriptor.
461 */
462static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100463fsg_hs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100464 .bLength = USB_DT_ENDPOINT_SIZE,
465 .bDescriptorType = USB_DT_ENDPOINT,
466
467 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
468 .bmAttributes = USB_ENDPOINT_XFER_BULK,
469 .wMaxPacketSize = cpu_to_le16(512),
470};
471
472static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100473fsg_hs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100474 .bLength = USB_DT_ENDPOINT_SIZE,
475 .bDescriptorType = USB_DT_ENDPOINT,
476
477 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
478 .bmAttributes = USB_ENDPOINT_XFER_BULK,
479 .wMaxPacketSize = cpu_to_le16(512),
480 .bInterval = 1, // NAK every 1 uframe
481};
482
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100483#ifndef FSG_NO_INTR_EP
484
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100485static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100486fsg_hs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100487 .bLength = USB_DT_ENDPOINT_SIZE,
488 .bDescriptorType = USB_DT_ENDPOINT,
489
490 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
491 .bmAttributes = USB_ENDPOINT_XFER_INT,
492 .wMaxPacketSize = cpu_to_le16(2),
493 .bInterval = 9, // 2**(9-1) = 256 uframes -> 32 ms
494};
495
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100496#ifndef FSG_NO_OTG
497# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
498#else
499# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
500#endif
501
502#endif
503
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100504static struct usb_descriptor_header *fsg_hs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100505#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100506 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100507#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100508 (struct usb_descriptor_header *) &fsg_intf_desc,
509 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
510 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100511#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100512 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100513#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100514 NULL,
515};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100516
517/* Maxpacket and other transfer characteristics vary by speed. */
518static struct usb_endpoint_descriptor *
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100519fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100520 struct usb_endpoint_descriptor *hs)
521{
522 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
523 return hs;
524 return fs;
525}
526
527
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100528/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100529static struct usb_string fsg_strings[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100530#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100531 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
532 {FSG_STRING_PRODUCT, fsg_string_product},
533 {FSG_STRING_SERIAL, fsg_string_serial},
534 {FSG_STRING_CONFIG, fsg_string_config},
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100535#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100536 {FSG_STRING_INTERFACE, fsg_string_interface},
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100537 {}
538};
539
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100540static struct usb_gadget_strings fsg_stringtab = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100541 .language = 0x0409, // en-us
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100542 .strings = fsg_strings,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100543};
544
545
546 /*-------------------------------------------------------------------------*/
547
548/* If the next two routines are called while the gadget is registered,
549 * the caller must own fsg->filesem for writing. */
550
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100551static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100552{
553 int ro;
554 struct file *filp = NULL;
555 int rc = -EINVAL;
556 struct inode *inode = NULL;
557 loff_t size;
558 loff_t num_sectors;
559 loff_t min_sectors;
560
561 /* R/W if we can, R/O if we must */
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100562 ro = curlun->initially_ro;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100563 if (!ro) {
564 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
565 if (-EROFS == PTR_ERR(filp))
566 ro = 1;
567 }
568 if (ro)
569 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
570 if (IS_ERR(filp)) {
571 LINFO(curlun, "unable to open backing file: %s\n", filename);
572 return PTR_ERR(filp);
573 }
574
575 if (!(filp->f_mode & FMODE_WRITE))
576 ro = 1;
577
578 if (filp->f_path.dentry)
579 inode = filp->f_path.dentry->d_inode;
580 if (inode && S_ISBLK(inode->i_mode)) {
581 if (bdev_read_only(inode->i_bdev))
582 ro = 1;
583 } else if (!inode || !S_ISREG(inode->i_mode)) {
584 LINFO(curlun, "invalid file type: %s\n", filename);
585 goto out;
586 }
587
588 /* If we can't read the file, it's no good.
589 * If we can't write the file, use it read-only. */
590 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
591 LINFO(curlun, "file not readable: %s\n", filename);
592 goto out;
593 }
594 if (!(filp->f_op->write || filp->f_op->aio_write))
595 ro = 1;
596
597 size = i_size_read(inode->i_mapping->host);
598 if (size < 0) {
599 LINFO(curlun, "unable to find file size: %s\n", filename);
600 rc = (int) size;
601 goto out;
602 }
603 num_sectors = size >> 9; // File size in 512-byte blocks
604 min_sectors = 1;
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100605 if (curlun->cdrom) {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100606 num_sectors &= ~3; // Reduce to a multiple of 2048
607 min_sectors = 300*4; // Smallest track is 300 frames
608 if (num_sectors >= 256*60*75*4) {
609 num_sectors = (256*60*75 - 1) * 4;
610 LINFO(curlun, "file too big: %s\n", filename);
611 LINFO(curlun, "using only first %d blocks\n",
612 (int) num_sectors);
613 }
614 }
615 if (num_sectors < min_sectors) {
616 LINFO(curlun, "file too small: %s\n", filename);
617 rc = -ETOOSMALL;
618 goto out;
619 }
620
621 get_file(filp);
622 curlun->ro = ro;
623 curlun->filp = filp;
624 curlun->file_length = size;
625 curlun->num_sectors = num_sectors;
626 LDBG(curlun, "open backing file: %s\n", filename);
627 rc = 0;
628
629out:
630 filp_close(filp, current->files);
631 return rc;
632}
633
634
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100635static void fsg_lun_close(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100636{
637 if (curlun->filp) {
638 LDBG(curlun, "close backing file\n");
639 fput(curlun->filp);
640 curlun->filp = NULL;
641 }
642}
643
644
645/*-------------------------------------------------------------------------*/
646
647/* Sync the file data, don't bother with the metadata.
648 * This code was copied from fs/buffer.c:sys_fdatasync(). */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100649static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100650{
651 struct file *filp = curlun->filp;
652
653 if (curlun->ro || !filp)
654 return 0;
655 return vfs_fsync(filp, filp->f_path.dentry, 1);
656}
657
658static void store_cdrom_address(u8 *dest, int msf, u32 addr)
659{
660 if (msf) {
661 /* Convert to Minutes-Seconds-Frames */
662 addr >>= 2; /* Convert to 2048-byte frames */
663 addr += 2*75; /* Lead-in occupies 2 seconds */
664 dest[3] = addr % 75; /* Frames */
665 addr /= 75;
666 dest[2] = addr % 60; /* Seconds */
667 addr /= 60;
668 dest[1] = addr; /* Minutes */
669 dest[0] = 0; /* Reserved */
670 } else {
671 /* Absolute sector */
672 put_unaligned_be32(addr, dest);
673 }
674}
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100675
676
677/*-------------------------------------------------------------------------*/
678
679
680static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
681 char *buf)
682{
683 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
684
685 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
686 ? curlun->ro
687 : curlun->initially_ro);
688}
689
690static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
691 char *buf)
692{
693 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
694 struct rw_semaphore *filesem = dev_get_drvdata(dev);
695 char *p;
696 ssize_t rc;
697
698 down_read(filesem);
699 if (fsg_lun_is_open(curlun)) { // Get the complete pathname
700 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
701 if (IS_ERR(p))
702 rc = PTR_ERR(p);
703 else {
704 rc = strlen(p);
705 memmove(buf, p, rc);
706 buf[rc] = '\n'; // Add a newline
707 buf[++rc] = 0;
708 }
709 } else { // No file, return 0 bytes
710 *buf = 0;
711 rc = 0;
712 }
713 up_read(filesem);
714 return rc;
715}
716
717
718static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
719 const char *buf, size_t count)
720{
721 ssize_t rc = count;
722 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
723 struct rw_semaphore *filesem = dev_get_drvdata(dev);
724 int i;
725
726 if (sscanf(buf, "%d", &i) != 1)
727 return -EINVAL;
728
729 /* Allow the write-enable status to change only while the backing file
730 * is closed. */
731 down_read(filesem);
732 if (fsg_lun_is_open(curlun)) {
733 LDBG(curlun, "read-only status change prevented\n");
734 rc = -EBUSY;
735 } else {
736 curlun->ro = !!i;
737 curlun->initially_ro = !!i;
738 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
739 }
740 up_read(filesem);
741 return rc;
742}
743
744static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
745 const char *buf, size_t count)
746{
747 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
748 struct rw_semaphore *filesem = dev_get_drvdata(dev);
749 int rc = 0;
750
751 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
752 LDBG(curlun, "eject attempt prevented\n");
753 return -EBUSY; // "Door is locked"
754 }
755
756 /* Remove a trailing newline */
757 if (count > 0 && buf[count-1] == '\n')
758 ((char *) buf)[count-1] = 0; // Ugh!
759
760 /* Eject current medium */
761 down_write(filesem);
762 if (fsg_lun_is_open(curlun)) {
763 fsg_lun_close(curlun);
764 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
765 }
766
767 /* Load new medium */
768 if (count > 0 && buf[0]) {
769 rc = fsg_lun_open(curlun, buf);
770 if (rc == 0)
771 curlun->unit_attention_data =
772 SS_NOT_READY_TO_READY_TRANSITION;
773 }
774 up_write(filesem);
775 return (rc < 0 ? rc : count);
776}