storage_common.c 24 KB

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