storage_common.c 25 KB

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