storage_common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*
  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)
  7. *
  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
  21. */
  22. /*
  23. * This file requires the following identifiers used in USB strings to
  24. * be defined (each of type pointer to char):
  25. * - fsg_string_manufacturer -- name of the manufacturer
  26. * - fsg_string_product -- name of the product
  27. * - fsg_string_serial -- product's serial
  28. * - fsg_string_config -- name of the configuration
  29. * - fsg_string_interface -- name of the interface
  30. * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
  31. * macro is defined prior to including this file.
  32. */
  33. /*
  34. * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
  35. * fsg_hs_intr_in_desc objects as well as
  36. * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
  37. * macros are not defined.
  38. *
  39. * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
  40. * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
  41. * defined (as well as corresponding entries in string tables are
  42. * missing) and FSG_STRING_INTERFACE has value of zero.
  43. *
  44. * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
  45. */
  46. /*
  47. * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
  48. * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
  49. * characters rather then a pointer to void.
  50. */
  51. #include <asm/unaligned.h>
  52. /*
  53. * Thanks to NetChip Technologies for donating this product ID.
  54. *
  55. * DO NOT REUSE THESE IDs with any other driver!! Ever!!
  56. * Instead: allocate your own, using normal USB-IF procedures.
  57. */
  58. #define FSG_VENDOR_ID 0x0525 /* NetChip */
  59. #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
  60. /*-------------------------------------------------------------------------*/
  61. #ifndef DEBUG
  62. #undef VERBOSE_DEBUG
  63. #undef DUMP_MSGS
  64. #endif /* !DEBUG */
  65. #ifdef VERBOSE_DEBUG
  66. #define VLDBG LDBG
  67. #else
  68. #define VLDBG(lun, fmt, args...) do { } while (0)
  69. #endif /* VERBOSE_DEBUG */
  70. #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
  71. #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
  72. #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
  73. #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
  74. /*
  75. * Keep those macros in sync with those in
  76. * include/linux/usb/composite.h or else GCC will complain. If they
  77. * are identical (the same names of arguments, white spaces in the
  78. * same places) GCC will allow redefinition otherwise (even if some
  79. * white space is removed or added) warning will be issued.
  80. *
  81. * Those macros are needed here because File Storage Gadget does not
  82. * include the composite.h header. For composite gadgets those macros
  83. * are redundant since composite.h is included any way.
  84. *
  85. * One could check whether those macros are already defined (which
  86. * would indicate composite.h had been included) or not (which would
  87. * indicate we were in FSG) but this is not done because a warning is
  88. * desired if definitions here differ from the ones in composite.h.
  89. *
  90. * We want the definitions to match and be the same in File Storage
  91. * Gadget as well as Mass Storage Function (and so composite gadgets
  92. * using MSF). If someone changes them in composite.h it will produce
  93. * a warning in this file when building MSF.
  94. */
  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)
  100. #ifdef DUMP_MSGS
  101. # define dump_msg(fsg, /* const char * */ label, \
  102. /* const u8 * */ buf, /* unsigned */ length) do { \
  103. if (length < 512) { \
  104. DBG(fsg, "%s, length %u:\n", label, length); \
  105. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
  106. 16, 1, buf, length, 0); \
  107. } \
  108. } while (0)
  109. # define dump_cdb(fsg) do { } while (0)
  110. #else
  111. # define dump_msg(fsg, /* const char * */ label, \
  112. /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
  113. # ifdef VERBOSE_DEBUG
  114. # define dump_cdb(fsg) \
  115. print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
  116. 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
  117. # else
  118. # define dump_cdb(fsg) do { } while (0)
  119. # endif /* VERBOSE_DEBUG */
  120. #endif /* DUMP_MSGS */
  121. /*-------------------------------------------------------------------------*/
  122. /* SCSI device types */
  123. #define TYPE_DISK 0x00
  124. #define TYPE_CDROM 0x05
  125. /* USB protocol value = the transport method */
  126. #define USB_PR_CBI 0x00 /* Control/Bulk/Interrupt */
  127. #define USB_PR_CB 0x01 /* Control/Bulk w/o interrupt */
  128. #define USB_PR_BULK 0x50 /* Bulk-only */
  129. /* USB subclass value = the protocol encapsulation */
  130. #define USB_SC_RBC 0x01 /* Reduced Block Commands (flash) */
  131. #define USB_SC_8020 0x02 /* SFF-8020i, MMC-2, ATAPI (CD-ROM) */
  132. #define USB_SC_QIC 0x03 /* QIC-157 (tape) */
  133. #define USB_SC_UFI 0x04 /* UFI (floppy) */
  134. #define USB_SC_8070 0x05 /* SFF-8070i (removable) */
  135. #define USB_SC_SCSI 0x06 /* Transparent SCSI */
  136. /* Bulk-only data structures */
  137. /* Command Block Wrapper */
  138. struct fsg_bulk_cb_wrap {
  139. __le32 Signature; /* Contains 'USBC' */
  140. u32 Tag; /* Unique per command id */
  141. __le32 DataTransferLength; /* Size of the data */
  142. u8 Flags; /* Direction in bit 7 */
  143. u8 Lun; /* LUN (normally 0) */
  144. u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
  145. u8 CDB[16]; /* Command Data Block */
  146. };
  147. #define USB_BULK_CB_WRAP_LEN 31
  148. #define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
  149. #define USB_BULK_IN_FLAG 0x80
  150. /* Command Status Wrapper */
  151. struct bulk_cs_wrap {
  152. __le32 Signature; /* Should = 'USBS' */
  153. u32 Tag; /* Same as original command */
  154. __le32 Residue; /* Amount not transferred */
  155. u8 Status; /* See below */
  156. };
  157. #define USB_BULK_CS_WRAP_LEN 13
  158. #define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
  159. #define USB_STATUS_PASS 0
  160. #define USB_STATUS_FAIL 1
  161. #define USB_STATUS_PHASE_ERROR 2
  162. /* Bulk-only class specific requests */
  163. #define USB_BULK_RESET_REQUEST 0xff
  164. #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
  165. /* CBI Interrupt data structure */
  166. struct interrupt_data {
  167. u8 bType;
  168. u8 bValue;
  169. };
  170. #define CBI_INTERRUPT_DATA_LEN 2
  171. /* CBI Accept Device-Specific Command request */
  172. #define USB_CBI_ADSC_REQUEST 0x00
  173. /* Length of a SCSI Command Data Block */
  174. #define MAX_COMMAND_SIZE 16
  175. /* SCSI commands that we recognize */
  176. #define SC_FORMAT_UNIT 0x04
  177. #define SC_INQUIRY 0x12
  178. #define SC_MODE_SELECT_6 0x15
  179. #define SC_MODE_SELECT_10 0x55
  180. #define SC_MODE_SENSE_6 0x1a
  181. #define SC_MODE_SENSE_10 0x5a
  182. #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
  183. #define SC_READ_6 0x08
  184. #define SC_READ_10 0x28
  185. #define SC_READ_12 0xa8
  186. #define SC_READ_CAPACITY 0x25
  187. #define SC_READ_FORMAT_CAPACITIES 0x23
  188. #define SC_READ_HEADER 0x44
  189. #define SC_READ_TOC 0x43
  190. #define SC_RELEASE 0x17
  191. #define SC_REQUEST_SENSE 0x03
  192. #define SC_RESERVE 0x16
  193. #define SC_SEND_DIAGNOSTIC 0x1d
  194. #define SC_START_STOP_UNIT 0x1b
  195. #define SC_SYNCHRONIZE_CACHE 0x35
  196. #define SC_TEST_UNIT_READY 0x00
  197. #define SC_VERIFY 0x2f
  198. #define SC_WRITE_6 0x0a
  199. #define SC_WRITE_10 0x2a
  200. #define SC_WRITE_12 0xaa
  201. /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
  202. #define SS_NO_SENSE 0
  203. #define SS_COMMUNICATION_FAILURE 0x040800
  204. #define SS_INVALID_COMMAND 0x052000
  205. #define SS_INVALID_FIELD_IN_CDB 0x052400
  206. #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
  207. #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
  208. #define SS_MEDIUM_NOT_PRESENT 0x023a00
  209. #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
  210. #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
  211. #define SS_RESET_OCCURRED 0x062900
  212. #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
  213. #define SS_UNRECOVERED_READ_ERROR 0x031100
  214. #define SS_WRITE_ERROR 0x030c02
  215. #define SS_WRITE_PROTECTED 0x072700
  216. #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
  217. #define ASC(x) ((u8) ((x) >> 8))
  218. #define ASCQ(x) ((u8) (x))
  219. /*-------------------------------------------------------------------------*/
  220. struct fsg_lun {
  221. struct file *filp;
  222. loff_t file_length;
  223. loff_t num_sectors;
  224. unsigned int initially_ro:1;
  225. unsigned int ro:1;
  226. unsigned int removable:1;
  227. unsigned int cdrom:1;
  228. unsigned int prevent_medium_removal:1;
  229. unsigned int registered:1;
  230. unsigned int info_valid:1;
  231. unsigned int nofua:1;
  232. u32 sense_data;
  233. u32 sense_data_info;
  234. u32 unit_attention_data;
  235. struct device dev;
  236. };
  237. #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
  238. static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
  239. {
  240. return container_of(dev, struct fsg_lun, dev);
  241. }
  242. /* Big enough to hold our biggest descriptor */
  243. #define EP0_BUFSIZE 256
  244. #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
  245. /* Number of buffers we will use. 2 is enough for double-buffering */
  246. #define FSG_NUM_BUFFERS 2
  247. /* Default size of buffer length. */
  248. #define FSG_BUFLEN ((u32)16384)
  249. /* Maximal number of LUNs supported in mass storage function */
  250. #define FSG_MAX_LUNS 8
  251. enum fsg_buffer_state {
  252. BUF_STATE_EMPTY = 0,
  253. BUF_STATE_FULL,
  254. BUF_STATE_BUSY
  255. };
  256. struct fsg_buffhd {
  257. #ifdef FSG_BUFFHD_STATIC_BUFFER
  258. char buf[FSG_BUFLEN];
  259. #else
  260. void *buf;
  261. #endif
  262. enum fsg_buffer_state state;
  263. struct fsg_buffhd *next;
  264. /*
  265. * The NetChip 2280 is faster, and handles some protocol faults
  266. * better, if we don't submit any short bulk-out read requests.
  267. * So we will record the intended request length here.
  268. */
  269. unsigned int bulk_out_intended_length;
  270. struct usb_request *inreq;
  271. int inreq_busy;
  272. struct usb_request *outreq;
  273. int outreq_busy;
  274. };
  275. enum fsg_state {
  276. /* This one isn't used anywhere */
  277. FSG_STATE_COMMAND_PHASE = -10,
  278. FSG_STATE_DATA_PHASE,
  279. FSG_STATE_STATUS_PHASE,
  280. FSG_STATE_IDLE = 0,
  281. FSG_STATE_ABORT_BULK_OUT,
  282. FSG_STATE_RESET,
  283. FSG_STATE_INTERFACE_CHANGE,
  284. FSG_STATE_CONFIG_CHANGE,
  285. FSG_STATE_DISCONNECT,
  286. FSG_STATE_EXIT,
  287. FSG_STATE_TERMINATED
  288. };
  289. enum data_direction {
  290. DATA_DIR_UNKNOWN = 0,
  291. DATA_DIR_FROM_HOST,
  292. DATA_DIR_TO_HOST,
  293. DATA_DIR_NONE
  294. };
  295. /*-------------------------------------------------------------------------*/
  296. static inline u32 get_unaligned_be24(u8 *buf)
  297. {
  298. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  299. }
  300. /*-------------------------------------------------------------------------*/
  301. enum {
  302. #ifndef FSG_NO_DEVICE_STRINGS
  303. FSG_STRING_MANUFACTURER = 1,
  304. FSG_STRING_PRODUCT,
  305. FSG_STRING_SERIAL,
  306. FSG_STRING_CONFIG,
  307. #endif
  308. FSG_STRING_INTERFACE
  309. };
  310. #ifndef FSG_NO_OTG
  311. static struct usb_otg_descriptor
  312. fsg_otg_desc = {
  313. .bLength = sizeof fsg_otg_desc,
  314. .bDescriptorType = USB_DT_OTG,
  315. .bmAttributes = USB_OTG_SRP,
  316. };
  317. #endif
  318. /* There is only one interface. */
  319. static struct usb_interface_descriptor
  320. fsg_intf_desc = {
  321. .bLength = sizeof fsg_intf_desc,
  322. .bDescriptorType = USB_DT_INTERFACE,
  323. .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
  324. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  325. .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
  326. .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
  327. .iInterface = FSG_STRING_INTERFACE,
  328. };
  329. /*
  330. * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
  331. * interrupt-in.
  332. */
  333. static struct usb_endpoint_descriptor
  334. fsg_fs_bulk_in_desc = {
  335. .bLength = USB_DT_ENDPOINT_SIZE,
  336. .bDescriptorType = USB_DT_ENDPOINT,
  337. .bEndpointAddress = USB_DIR_IN,
  338. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  339. /* wMaxPacketSize set by autoconfiguration */
  340. };
  341. static struct usb_endpoint_descriptor
  342. fsg_fs_bulk_out_desc = {
  343. .bLength = USB_DT_ENDPOINT_SIZE,
  344. .bDescriptorType = USB_DT_ENDPOINT,
  345. .bEndpointAddress = USB_DIR_OUT,
  346. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  347. /* wMaxPacketSize set by autoconfiguration */
  348. };
  349. #ifndef FSG_NO_INTR_EP
  350. static struct usb_endpoint_descriptor
  351. fsg_fs_intr_in_desc = {
  352. .bLength = USB_DT_ENDPOINT_SIZE,
  353. .bDescriptorType = USB_DT_ENDPOINT,
  354. .bEndpointAddress = USB_DIR_IN,
  355. .bmAttributes = USB_ENDPOINT_XFER_INT,
  356. .wMaxPacketSize = cpu_to_le16(2),
  357. .bInterval = 32, /* frames -> 32 ms */
  358. };
  359. #ifndef FSG_NO_OTG
  360. # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
  361. #else
  362. # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
  363. #endif
  364. #endif
  365. static struct usb_descriptor_header *fsg_fs_function[] = {
  366. #ifndef FSG_NO_OTG
  367. (struct usb_descriptor_header *) &fsg_otg_desc,
  368. #endif
  369. (struct usb_descriptor_header *) &fsg_intf_desc,
  370. (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
  371. (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
  372. #ifndef FSG_NO_INTR_EP
  373. (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
  374. #endif
  375. NULL,
  376. };
  377. /*
  378. * USB 2.0 devices need to expose both high speed and full speed
  379. * descriptors, unless they only run at full speed.
  380. *
  381. * That means alternate endpoint descriptors (bigger packets)
  382. * and a "device qualifier" ... plus more construction options
  383. * for the configuration descriptor.
  384. */
  385. static struct usb_endpoint_descriptor
  386. fsg_hs_bulk_in_desc = {
  387. .bLength = USB_DT_ENDPOINT_SIZE,
  388. .bDescriptorType = USB_DT_ENDPOINT,
  389. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  390. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  391. .wMaxPacketSize = cpu_to_le16(512),
  392. };
  393. static struct usb_endpoint_descriptor
  394. fsg_hs_bulk_out_desc = {
  395. .bLength = USB_DT_ENDPOINT_SIZE,
  396. .bDescriptorType = USB_DT_ENDPOINT,
  397. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  398. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  399. .wMaxPacketSize = cpu_to_le16(512),
  400. .bInterval = 1, /* NAK every 1 uframe */
  401. };
  402. #ifndef FSG_NO_INTR_EP
  403. static struct usb_endpoint_descriptor
  404. fsg_hs_intr_in_desc = {
  405. .bLength = USB_DT_ENDPOINT_SIZE,
  406. .bDescriptorType = USB_DT_ENDPOINT,
  407. /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
  408. .bmAttributes = USB_ENDPOINT_XFER_INT,
  409. .wMaxPacketSize = cpu_to_le16(2),
  410. .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
  411. };
  412. #ifndef FSG_NO_OTG
  413. # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
  414. #else
  415. # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
  416. #endif
  417. #endif
  418. static struct usb_descriptor_header *fsg_hs_function[] = {
  419. #ifndef FSG_NO_OTG
  420. (struct usb_descriptor_header *) &fsg_otg_desc,
  421. #endif
  422. (struct usb_descriptor_header *) &fsg_intf_desc,
  423. (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
  424. (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
  425. #ifndef FSG_NO_INTR_EP
  426. (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
  427. #endif
  428. NULL,
  429. };
  430. /* Maxpacket and other transfer characteristics vary by speed. */
  431. static struct usb_endpoint_descriptor *
  432. fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
  433. struct usb_endpoint_descriptor *hs)
  434. {
  435. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  436. return hs;
  437. return fs;
  438. }
  439. /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
  440. static struct usb_string fsg_strings[] = {
  441. #ifndef FSG_NO_DEVICE_STRINGS
  442. {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
  443. {FSG_STRING_PRODUCT, fsg_string_product},
  444. {FSG_STRING_SERIAL, fsg_string_serial},
  445. {FSG_STRING_CONFIG, fsg_string_config},
  446. #endif
  447. {FSG_STRING_INTERFACE, fsg_string_interface},
  448. {}
  449. };
  450. static struct usb_gadget_strings fsg_stringtab = {
  451. .language = 0x0409, /* en-us */
  452. .strings = fsg_strings,
  453. };
  454. /*-------------------------------------------------------------------------*/
  455. /*
  456. * If the next two routines are called while the gadget is registered,
  457. * the caller must own fsg->filesem for writing.
  458. */
  459. static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
  460. {
  461. int ro;
  462. struct file *filp = NULL;
  463. int rc = -EINVAL;
  464. struct inode *inode = NULL;
  465. loff_t size;
  466. loff_t num_sectors;
  467. loff_t min_sectors;
  468. /* R/W if we can, R/O if we must */
  469. ro = curlun->initially_ro;
  470. if (!ro) {
  471. filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
  472. if (-EROFS == PTR_ERR(filp))
  473. ro = 1;
  474. }
  475. if (ro)
  476. filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
  477. if (IS_ERR(filp)) {
  478. LINFO(curlun, "unable to open backing file: %s\n", filename);
  479. return PTR_ERR(filp);
  480. }
  481. if (!(filp->f_mode & FMODE_WRITE))
  482. ro = 1;
  483. if (filp->f_path.dentry)
  484. inode = filp->f_path.dentry->d_inode;
  485. if (inode && S_ISBLK(inode->i_mode)) {
  486. if (bdev_read_only(inode->i_bdev))
  487. ro = 1;
  488. } else if (!inode || !S_ISREG(inode->i_mode)) {
  489. LINFO(curlun, "invalid file type: %s\n", filename);
  490. goto out;
  491. }
  492. /*
  493. * If we can't read the file, it's no good.
  494. * If we can't write the file, use it read-only.
  495. */
  496. if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
  497. LINFO(curlun, "file not readable: %s\n", filename);
  498. goto out;
  499. }
  500. if (!(filp->f_op->write || filp->f_op->aio_write))
  501. ro = 1;
  502. size = i_size_read(inode->i_mapping->host);
  503. if (size < 0) {
  504. LINFO(curlun, "unable to find file size: %s\n", filename);
  505. rc = (int) size;
  506. goto out;
  507. }
  508. num_sectors = size >> 9; /* File size in 512-byte blocks */
  509. min_sectors = 1;
  510. if (curlun->cdrom) {
  511. num_sectors &= ~3; /* Reduce to a multiple of 2048 */
  512. min_sectors = 300*4; /* Smallest track is 300 frames */
  513. if (num_sectors >= 256*60*75*4) {
  514. num_sectors = (256*60*75 - 1) * 4;
  515. LINFO(curlun, "file too big: %s\n", filename);
  516. LINFO(curlun, "using only first %d blocks\n",
  517. (int) num_sectors);
  518. }
  519. }
  520. if (num_sectors < min_sectors) {
  521. LINFO(curlun, "file too small: %s\n", filename);
  522. rc = -ETOOSMALL;
  523. goto out;
  524. }
  525. get_file(filp);
  526. curlun->ro = ro;
  527. curlun->filp = filp;
  528. curlun->file_length = size;
  529. curlun->num_sectors = num_sectors;
  530. LDBG(curlun, "open backing file: %s\n", filename);
  531. rc = 0;
  532. out:
  533. filp_close(filp, current->files);
  534. return rc;
  535. }
  536. static void fsg_lun_close(struct fsg_lun *curlun)
  537. {
  538. if (curlun->filp) {
  539. LDBG(curlun, "close backing file\n");
  540. fput(curlun->filp);
  541. curlun->filp = NULL;
  542. }
  543. }
  544. /*-------------------------------------------------------------------------*/
  545. /*
  546. * Sync the file data, don't bother with the metadata.
  547. * This code was copied from fs/buffer.c:sys_fdatasync().
  548. */
  549. static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
  550. {
  551. struct file *filp = curlun->filp;
  552. if (curlun->ro || !filp)
  553. return 0;
  554. return vfs_fsync(filp, 1);
  555. }
  556. static void store_cdrom_address(u8 *dest, int msf, u32 addr)
  557. {
  558. if (msf) {
  559. /* Convert to Minutes-Seconds-Frames */
  560. addr >>= 2; /* Convert to 2048-byte frames */
  561. addr += 2*75; /* Lead-in occupies 2 seconds */
  562. dest[3] = addr % 75; /* Frames */
  563. addr /= 75;
  564. dest[2] = addr % 60; /* Seconds */
  565. addr /= 60;
  566. dest[1] = addr; /* Minutes */
  567. dest[0] = 0; /* Reserved */
  568. } else {
  569. /* Absolute sector */
  570. put_unaligned_be32(addr, dest);
  571. }
  572. }
  573. /*-------------------------------------------------------------------------*/
  574. static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
  575. char *buf)
  576. {
  577. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  578. return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
  579. ? curlun->ro
  580. : curlun->initially_ro);
  581. }
  582. static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
  583. char *buf)
  584. {
  585. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  586. return sprintf(buf, "%u\n", curlun->nofua);
  587. }
  588. static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
  589. char *buf)
  590. {
  591. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  592. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  593. char *p;
  594. ssize_t rc;
  595. down_read(filesem);
  596. if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
  597. p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
  598. if (IS_ERR(p))
  599. rc = PTR_ERR(p);
  600. else {
  601. rc = strlen(p);
  602. memmove(buf, p, rc);
  603. buf[rc] = '\n'; /* Add a newline */
  604. buf[++rc] = 0;
  605. }
  606. } else { /* No file, return 0 bytes */
  607. *buf = 0;
  608. rc = 0;
  609. }
  610. up_read(filesem);
  611. return rc;
  612. }
  613. static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
  614. const char *buf, size_t count)
  615. {
  616. ssize_t rc = count;
  617. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  618. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  619. unsigned long ro;
  620. if (strict_strtoul(buf, 2, &ro))
  621. return -EINVAL;
  622. /*
  623. * Allow the write-enable status to change only while the
  624. * backing file is closed.
  625. */
  626. down_read(filesem);
  627. if (fsg_lun_is_open(curlun)) {
  628. LDBG(curlun, "read-only status change prevented\n");
  629. rc = -EBUSY;
  630. } else {
  631. curlun->ro = ro;
  632. curlun->initially_ro = ro;
  633. LDBG(curlun, "read-only status set to %d\n", curlun->ro);
  634. }
  635. up_read(filesem);
  636. return rc;
  637. }
  638. static ssize_t fsg_store_nofua(struct device *dev,
  639. struct device_attribute *attr,
  640. const char *buf, size_t count)
  641. {
  642. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  643. unsigned long nofua;
  644. if (strict_strtoul(buf, 2, &nofua))
  645. return -EINVAL;
  646. /* Sync data when switching from async mode to sync */
  647. if (!nofua && curlun->nofua)
  648. fsg_lun_fsync_sub(curlun);
  649. curlun->nofua = nofua;
  650. return count;
  651. }
  652. static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
  653. const char *buf, size_t count)
  654. {
  655. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  656. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  657. int rc = 0;
  658. if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
  659. LDBG(curlun, "eject attempt prevented\n");
  660. return -EBUSY; /* "Door is locked" */
  661. }
  662. /* Remove a trailing newline */
  663. if (count > 0 && buf[count-1] == '\n')
  664. ((char *) buf)[count-1] = 0; /* Ugh! */
  665. /* Eject current medium */
  666. down_write(filesem);
  667. if (fsg_lun_is_open(curlun)) {
  668. fsg_lun_close(curlun);
  669. curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
  670. }
  671. /* Load new medium */
  672. if (count > 0 && buf[0]) {
  673. rc = fsg_lun_open(curlun, buf);
  674. if (rc == 0)
  675. curlun->unit_attention_data =
  676. SS_NOT_READY_TO_READY_TRANSITION;
  677. }
  678. up_write(filesem);
  679. return (rc < 0 ? rc : count);
  680. }