storage_common.c 23 KB

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