storage_common.c 26 KB

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