storage_common.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 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. /* CBI Interrupt data structure */
  120. struct interrupt_data {
  121. u8 bType;
  122. u8 bValue;
  123. };
  124. #define CBI_INTERRUPT_DATA_LEN 2
  125. /* CBI Accept Device-Specific Command request */
  126. #define USB_CBI_ADSC_REQUEST 0x00
  127. /* Length of a SCSI Command Data Block */
  128. #define MAX_COMMAND_SIZE 16
  129. /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
  130. #define SS_NO_SENSE 0
  131. #define SS_COMMUNICATION_FAILURE 0x040800
  132. #define SS_INVALID_COMMAND 0x052000
  133. #define SS_INVALID_FIELD_IN_CDB 0x052400
  134. #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
  135. #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
  136. #define SS_MEDIUM_NOT_PRESENT 0x023a00
  137. #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
  138. #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
  139. #define SS_RESET_OCCURRED 0x062900
  140. #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
  141. #define SS_UNRECOVERED_READ_ERROR 0x031100
  142. #define SS_WRITE_ERROR 0x030c02
  143. #define SS_WRITE_PROTECTED 0x072700
  144. #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
  145. #define ASC(x) ((u8) ((x) >> 8))
  146. #define ASCQ(x) ((u8) (x))
  147. /*-------------------------------------------------------------------------*/
  148. struct fsg_lun {
  149. struct file *filp;
  150. loff_t file_length;
  151. loff_t num_sectors;
  152. unsigned int initially_ro:1;
  153. unsigned int ro:1;
  154. unsigned int removable:1;
  155. unsigned int cdrom:1;
  156. unsigned int prevent_medium_removal:1;
  157. unsigned int registered:1;
  158. unsigned int info_valid:1;
  159. unsigned int nofua:1;
  160. u32 sense_data;
  161. u32 sense_data_info;
  162. u32 unit_attention_data;
  163. unsigned int blkbits; /* Bits of logical block size of bound block device */
  164. unsigned int blksize; /* logical block size of bound block device */
  165. struct device dev;
  166. };
  167. #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
  168. static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
  169. {
  170. return container_of(dev, struct fsg_lun, dev);
  171. }
  172. /* Big enough to hold our biggest descriptor */
  173. #define EP0_BUFSIZE 256
  174. #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
  175. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  176. static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
  177. module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);
  178. MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers");
  179. #else
  180. /*
  181. * Number of buffers we will use.
  182. * 2 is usually enough for good buffering pipeline
  183. */
  184. #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
  185. #endif /* CONFIG_USB_DEBUG */
  186. /* check if fsg_num_buffers is within a valid range */
  187. static inline int fsg_num_buffers_validate(void)
  188. {
  189. if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4)
  190. return 0;
  191. pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
  192. fsg_num_buffers, 2 ,4);
  193. return -EINVAL;
  194. }
  195. /* Default size of buffer length. */
  196. #define FSG_BUFLEN ((u32)16384)
  197. /* Maximal number of LUNs supported in mass storage function */
  198. #define FSG_MAX_LUNS 8
  199. enum fsg_buffer_state {
  200. BUF_STATE_EMPTY = 0,
  201. BUF_STATE_FULL,
  202. BUF_STATE_BUSY
  203. };
  204. struct fsg_buffhd {
  205. #ifdef FSG_BUFFHD_STATIC_BUFFER
  206. char buf[FSG_BUFLEN];
  207. #else
  208. void *buf;
  209. #endif
  210. enum fsg_buffer_state state;
  211. struct fsg_buffhd *next;
  212. /*
  213. * The NetChip 2280 is faster, and handles some protocol faults
  214. * better, if we don't submit any short bulk-out read requests.
  215. * So we will record the intended request length here.
  216. */
  217. unsigned int bulk_out_intended_length;
  218. struct usb_request *inreq;
  219. int inreq_busy;
  220. struct usb_request *outreq;
  221. int outreq_busy;
  222. };
  223. enum fsg_state {
  224. /* This one isn't used anywhere */
  225. FSG_STATE_COMMAND_PHASE = -10,
  226. FSG_STATE_DATA_PHASE,
  227. FSG_STATE_STATUS_PHASE,
  228. FSG_STATE_IDLE = 0,
  229. FSG_STATE_ABORT_BULK_OUT,
  230. FSG_STATE_RESET,
  231. FSG_STATE_INTERFACE_CHANGE,
  232. FSG_STATE_CONFIG_CHANGE,
  233. FSG_STATE_DISCONNECT,
  234. FSG_STATE_EXIT,
  235. FSG_STATE_TERMINATED
  236. };
  237. enum data_direction {
  238. DATA_DIR_UNKNOWN = 0,
  239. DATA_DIR_FROM_HOST,
  240. DATA_DIR_TO_HOST,
  241. DATA_DIR_NONE
  242. };
  243. /*-------------------------------------------------------------------------*/
  244. static inline u32 get_unaligned_be24(u8 *buf)
  245. {
  246. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  247. }
  248. /*-------------------------------------------------------------------------*/
  249. enum {
  250. #ifndef FSG_NO_DEVICE_STRINGS
  251. FSG_STRING_MANUFACTURER = 1,
  252. FSG_STRING_PRODUCT,
  253. FSG_STRING_SERIAL,
  254. FSG_STRING_CONFIG,
  255. #endif
  256. FSG_STRING_INTERFACE
  257. };
  258. #ifndef FSG_NO_OTG
  259. static struct usb_otg_descriptor
  260. fsg_otg_desc = {
  261. .bLength = sizeof fsg_otg_desc,
  262. .bDescriptorType = USB_DT_OTG,
  263. .bmAttributes = USB_OTG_SRP,
  264. };
  265. #endif
  266. /* There is only one interface. */
  267. static struct usb_interface_descriptor
  268. fsg_intf_desc = {
  269. .bLength = sizeof fsg_intf_desc,
  270. .bDescriptorType = USB_DT_INTERFACE,
  271. .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
  272. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  273. .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
  274. .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
  275. .iInterface = FSG_STRING_INTERFACE,
  276. };
  277. /*
  278. * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
  279. * interrupt-in.
  280. */
  281. static struct usb_endpoint_descriptor
  282. fsg_fs_bulk_in_desc = {
  283. .bLength = USB_DT_ENDPOINT_SIZE,
  284. .bDescriptorType = USB_DT_ENDPOINT,
  285. .bEndpointAddress = USB_DIR_IN,
  286. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  287. /* wMaxPacketSize set by autoconfiguration */
  288. };
  289. static struct usb_endpoint_descriptor
  290. fsg_fs_bulk_out_desc = {
  291. .bLength = USB_DT_ENDPOINT_SIZE,
  292. .bDescriptorType = USB_DT_ENDPOINT,
  293. .bEndpointAddress = USB_DIR_OUT,
  294. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  295. /* wMaxPacketSize set by autoconfiguration */
  296. };
  297. #ifndef FSG_NO_INTR_EP
  298. static struct usb_endpoint_descriptor
  299. fsg_fs_intr_in_desc = {
  300. .bLength = USB_DT_ENDPOINT_SIZE,
  301. .bDescriptorType = USB_DT_ENDPOINT,
  302. .bEndpointAddress = USB_DIR_IN,
  303. .bmAttributes = USB_ENDPOINT_XFER_INT,
  304. .wMaxPacketSize = cpu_to_le16(2),
  305. .bInterval = 32, /* frames -> 32 ms */
  306. };
  307. #ifndef FSG_NO_OTG
  308. # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
  309. #else
  310. # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
  311. #endif
  312. #endif
  313. static struct usb_descriptor_header *fsg_fs_function[] = {
  314. #ifndef FSG_NO_OTG
  315. (struct usb_descriptor_header *) &fsg_otg_desc,
  316. #endif
  317. (struct usb_descriptor_header *) &fsg_intf_desc,
  318. (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
  319. (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
  320. #ifndef FSG_NO_INTR_EP
  321. (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
  322. #endif
  323. NULL,
  324. };
  325. /*
  326. * USB 2.0 devices need to expose both high speed and full speed
  327. * descriptors, unless they only run at full speed.
  328. *
  329. * That means alternate endpoint descriptors (bigger packets)
  330. * and a "device qualifier" ... plus more construction options
  331. * for the configuration descriptor.
  332. */
  333. static struct usb_endpoint_descriptor
  334. fsg_hs_bulk_in_desc = {
  335. .bLength = USB_DT_ENDPOINT_SIZE,
  336. .bDescriptorType = USB_DT_ENDPOINT,
  337. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  338. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  339. .wMaxPacketSize = cpu_to_le16(512),
  340. };
  341. static struct usb_endpoint_descriptor
  342. fsg_hs_bulk_out_desc = {
  343. .bLength = USB_DT_ENDPOINT_SIZE,
  344. .bDescriptorType = USB_DT_ENDPOINT,
  345. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  346. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  347. .wMaxPacketSize = cpu_to_le16(512),
  348. .bInterval = 1, /* NAK every 1 uframe */
  349. };
  350. #ifndef FSG_NO_INTR_EP
  351. static struct usb_endpoint_descriptor
  352. fsg_hs_intr_in_desc = {
  353. .bLength = USB_DT_ENDPOINT_SIZE,
  354. .bDescriptorType = USB_DT_ENDPOINT,
  355. /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
  356. .bmAttributes = USB_ENDPOINT_XFER_INT,
  357. .wMaxPacketSize = cpu_to_le16(2),
  358. .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
  359. };
  360. #ifndef FSG_NO_OTG
  361. # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
  362. #else
  363. # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
  364. #endif
  365. #endif
  366. static struct usb_descriptor_header *fsg_hs_function[] = {
  367. #ifndef FSG_NO_OTG
  368. (struct usb_descriptor_header *) &fsg_otg_desc,
  369. #endif
  370. (struct usb_descriptor_header *) &fsg_intf_desc,
  371. (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
  372. (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
  373. #ifndef FSG_NO_INTR_EP
  374. (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
  375. #endif
  376. NULL,
  377. };
  378. static struct usb_endpoint_descriptor
  379. fsg_ss_bulk_in_desc = {
  380. .bLength = USB_DT_ENDPOINT_SIZE,
  381. .bDescriptorType = USB_DT_ENDPOINT,
  382. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  383. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  384. .wMaxPacketSize = cpu_to_le16(1024),
  385. };
  386. static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
  387. .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
  388. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  389. /*.bMaxBurst = DYNAMIC, */
  390. };
  391. static struct usb_endpoint_descriptor
  392. fsg_ss_bulk_out_desc = {
  393. .bLength = USB_DT_ENDPOINT_SIZE,
  394. .bDescriptorType = USB_DT_ENDPOINT,
  395. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  396. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  397. .wMaxPacketSize = cpu_to_le16(1024),
  398. };
  399. static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
  400. .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
  401. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  402. /*.bMaxBurst = DYNAMIC, */
  403. };
  404. #ifndef FSG_NO_INTR_EP
  405. static struct usb_endpoint_descriptor
  406. fsg_ss_intr_in_desc = {
  407. .bLength = USB_DT_ENDPOINT_SIZE,
  408. .bDescriptorType = USB_DT_ENDPOINT,
  409. /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
  410. .bmAttributes = USB_ENDPOINT_XFER_INT,
  411. .wMaxPacketSize = cpu_to_le16(2),
  412. .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
  413. };
  414. static struct usb_ss_ep_comp_descriptor fsg_ss_intr_in_comp_desc = {
  415. .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
  416. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  417. .wBytesPerInterval = cpu_to_le16(2),
  418. };
  419. #ifndef FSG_NO_OTG
  420. # define FSG_SS_FUNCTION_PRE_EP_ENTRIES 2
  421. #else
  422. # define FSG_SS_FUNCTION_PRE_EP_ENTRIES 1
  423. #endif
  424. #endif
  425. static __maybe_unused struct usb_ext_cap_descriptor fsg_ext_cap_desc = {
  426. .bLength = USB_DT_USB_EXT_CAP_SIZE,
  427. .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
  428. .bDevCapabilityType = USB_CAP_TYPE_EXT,
  429. .bmAttributes = cpu_to_le32(USB_LPM_SUPPORT),
  430. };
  431. static __maybe_unused struct usb_ss_cap_descriptor fsg_ss_cap_desc = {
  432. .bLength = USB_DT_USB_SS_CAP_SIZE,
  433. .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
  434. .bDevCapabilityType = USB_SS_CAP_TYPE,
  435. /* .bmAttributes = LTM is not supported yet */
  436. .wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION
  437. | USB_FULL_SPEED_OPERATION
  438. | USB_HIGH_SPEED_OPERATION
  439. | USB_5GBPS_OPERATION),
  440. .bFunctionalitySupport = USB_LOW_SPEED_OPERATION,
  441. .bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT,
  442. .bU2DevExitLat = cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT),
  443. };
  444. static __maybe_unused struct usb_bos_descriptor fsg_bos_desc = {
  445. .bLength = USB_DT_BOS_SIZE,
  446. .bDescriptorType = USB_DT_BOS,
  447. .wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE
  448. + USB_DT_USB_EXT_CAP_SIZE
  449. + USB_DT_USB_SS_CAP_SIZE),
  450. .bNumDeviceCaps = 2,
  451. };
  452. static struct usb_descriptor_header *fsg_ss_function[] = {
  453. #ifndef FSG_NO_OTG
  454. (struct usb_descriptor_header *) &fsg_otg_desc,
  455. #endif
  456. (struct usb_descriptor_header *) &fsg_intf_desc,
  457. (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
  458. (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
  459. (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
  460. (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
  461. #ifndef FSG_NO_INTR_EP
  462. (struct usb_descriptor_header *) &fsg_ss_intr_in_desc,
  463. (struct usb_descriptor_header *) &fsg_ss_intr_in_comp_desc,
  464. #endif
  465. NULL,
  466. };
  467. /* Maxpacket and other transfer characteristics vary by speed. */
  468. static __maybe_unused struct usb_endpoint_descriptor *
  469. fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
  470. struct usb_endpoint_descriptor *hs,
  471. struct usb_endpoint_descriptor *ss)
  472. {
  473. if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
  474. return ss;
  475. else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  476. return hs;
  477. return fs;
  478. }
  479. /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
  480. static struct usb_string fsg_strings[] = {
  481. #ifndef FSG_NO_DEVICE_STRINGS
  482. {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
  483. {FSG_STRING_PRODUCT, fsg_string_product},
  484. {FSG_STRING_SERIAL, ""},
  485. {FSG_STRING_CONFIG, fsg_string_config},
  486. #endif
  487. {FSG_STRING_INTERFACE, fsg_string_interface},
  488. {}
  489. };
  490. static struct usb_gadget_strings fsg_stringtab = {
  491. .language = 0x0409, /* en-us */
  492. .strings = fsg_strings,
  493. };
  494. /*-------------------------------------------------------------------------*/
  495. /*
  496. * If the next two routines are called while the gadget is registered,
  497. * the caller must own fsg->filesem for writing.
  498. */
  499. static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
  500. {
  501. int ro;
  502. struct file *filp = NULL;
  503. int rc = -EINVAL;
  504. struct inode *inode = NULL;
  505. loff_t size;
  506. loff_t num_sectors;
  507. loff_t min_sectors;
  508. /* R/W if we can, R/O if we must */
  509. ro = curlun->initially_ro;
  510. if (!ro) {
  511. filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
  512. if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
  513. ro = 1;
  514. }
  515. if (ro)
  516. filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
  517. if (IS_ERR(filp)) {
  518. LINFO(curlun, "unable to open backing file: %s\n", filename);
  519. return PTR_ERR(filp);
  520. }
  521. if (!(filp->f_mode & FMODE_WRITE))
  522. ro = 1;
  523. if (filp->f_path.dentry)
  524. inode = filp->f_path.dentry->d_inode;
  525. if (!inode || (!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 || !(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. curlun->blksize = 2048;
  547. curlun->blkbits = 11;
  548. } else if (inode->i_bdev) {
  549. curlun->blksize = bdev_logical_block_size(inode->i_bdev);
  550. curlun->blkbits = blksize_bits(curlun->blksize);
  551. } else {
  552. curlun->blksize = 512;
  553. curlun->blkbits = 9;
  554. }
  555. num_sectors = size >> curlun->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. get_file(filp);
  572. curlun->ro = ro;
  573. curlun->filp = filp;
  574. curlun->file_length = size;
  575. curlun->num_sectors = num_sectors;
  576. LDBG(curlun, "open backing file: %s\n", filename);
  577. rc = 0;
  578. out:
  579. filp_close(filp, current->files);
  580. return rc;
  581. }
  582. static void fsg_lun_close(struct fsg_lun *curlun)
  583. {
  584. if (curlun->filp) {
  585. LDBG(curlun, "close backing file\n");
  586. fput(curlun->filp);
  587. curlun->filp = NULL;
  588. }
  589. }
  590. /*-------------------------------------------------------------------------*/
  591. /*
  592. * Sync the file data, don't bother with the metadata.
  593. * This code was copied from fs/buffer.c:sys_fdatasync().
  594. */
  595. static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
  596. {
  597. struct file *filp = curlun->filp;
  598. if (curlun->ro || !filp)
  599. return 0;
  600. return vfs_fsync(filp, 1);
  601. }
  602. static void store_cdrom_address(u8 *dest, int msf, u32 addr)
  603. {
  604. if (msf) {
  605. /* Convert to Minutes-Seconds-Frames */
  606. addr >>= 2; /* Convert to 2048-byte frames */
  607. addr += 2*75; /* Lead-in occupies 2 seconds */
  608. dest[3] = addr % 75; /* Frames */
  609. addr /= 75;
  610. dest[2] = addr % 60; /* Seconds */
  611. addr /= 60;
  612. dest[1] = addr; /* Minutes */
  613. dest[0] = 0; /* Reserved */
  614. } else {
  615. /* Absolute sector */
  616. put_unaligned_be32(addr, dest);
  617. }
  618. }
  619. /*-------------------------------------------------------------------------*/
  620. static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
  621. char *buf)
  622. {
  623. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  624. return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
  625. ? curlun->ro
  626. : curlun->initially_ro);
  627. }
  628. static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
  629. char *buf)
  630. {
  631. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  632. return sprintf(buf, "%u\n", curlun->nofua);
  633. }
  634. static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
  635. char *buf)
  636. {
  637. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  638. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  639. char *p;
  640. ssize_t rc;
  641. down_read(filesem);
  642. if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
  643. p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
  644. if (IS_ERR(p))
  645. rc = PTR_ERR(p);
  646. else {
  647. rc = strlen(p);
  648. memmove(buf, p, rc);
  649. buf[rc] = '\n'; /* Add a newline */
  650. buf[++rc] = 0;
  651. }
  652. } else { /* No file, return 0 bytes */
  653. *buf = 0;
  654. rc = 0;
  655. }
  656. up_read(filesem);
  657. return rc;
  658. }
  659. static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
  660. const char *buf, size_t count)
  661. {
  662. ssize_t rc;
  663. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  664. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  665. unsigned ro;
  666. rc = kstrtouint(buf, 2, &ro);
  667. if (rc)
  668. return rc;
  669. /*
  670. * Allow the write-enable status to change only while the
  671. * backing file is closed.
  672. */
  673. down_read(filesem);
  674. if (fsg_lun_is_open(curlun)) {
  675. LDBG(curlun, "read-only status change prevented\n");
  676. rc = -EBUSY;
  677. } else {
  678. curlun->ro = ro;
  679. curlun->initially_ro = ro;
  680. LDBG(curlun, "read-only status set to %d\n", curlun->ro);
  681. rc = count;
  682. }
  683. up_read(filesem);
  684. return rc;
  685. }
  686. static ssize_t fsg_store_nofua(struct device *dev,
  687. struct device_attribute *attr,
  688. const char *buf, size_t count)
  689. {
  690. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  691. unsigned nofua;
  692. int ret;
  693. ret = kstrtouint(buf, 2, &nofua);
  694. if (ret)
  695. return ret;
  696. /* Sync data when switching from async mode to sync */
  697. if (!nofua && curlun->nofua)
  698. fsg_lun_fsync_sub(curlun);
  699. curlun->nofua = nofua;
  700. return count;
  701. }
  702. static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
  703. const char *buf, size_t count)
  704. {
  705. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  706. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  707. int rc = 0;
  708. if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
  709. LDBG(curlun, "eject attempt prevented\n");
  710. return -EBUSY; /* "Door is locked" */
  711. }
  712. /* Remove a trailing newline */
  713. if (count > 0 && buf[count-1] == '\n')
  714. ((char *) buf)[count-1] = 0; /* Ugh! */
  715. /* Eject current medium */
  716. down_write(filesem);
  717. if (fsg_lun_is_open(curlun)) {
  718. fsg_lun_close(curlun);
  719. curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
  720. }
  721. /* Load new medium */
  722. if (count > 0 && buf[0]) {
  723. rc = fsg_lun_open(curlun, buf);
  724. if (rc == 0)
  725. curlun->unit_attention_data =
  726. SS_NOT_READY_TO_READY_TRANSITION;
  727. }
  728. up_write(filesem);
  729. return (rc < 0 ? rc : count);
  730. }