storage_common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * storage_common.c -- Common definitions for mass storage functionality
  4. *
  5. * Copyright (C) 2003-2008 Alan Stern
  6. * Copyeight (C) 2009 Samsung Electronics
  7. * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
  8. *
  9. * Ported to u-boot:
  10. * Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  11. *
  12. * Code refactoring & cleanup:
  13. * Łukasz Majewski <l.majewski@samsung.com>
  14. */
  15. /*
  16. * This file requires the following identifiers used in USB strings to
  17. * be defined (each of type pointer to char):
  18. * - fsg_string_manufacturer -- name of the manufacturer
  19. * - fsg_string_product -- name of the product
  20. * - fsg_string_serial -- product's serial
  21. * - fsg_string_config -- name of the configuration
  22. * - fsg_string_interface -- name of the interface
  23. * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
  24. * macro is defined prior to including this file.
  25. */
  26. /*
  27. * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
  28. * fsg_hs_intr_in_desc objects as well as
  29. * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
  30. * macros are not defined.
  31. *
  32. * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
  33. * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
  34. * defined (as well as corresponding entries in string tables are
  35. * missing) and FSG_STRING_INTERFACE has value of zero.
  36. *
  37. * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
  38. */
  39. /*
  40. * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
  41. * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
  42. * characters rather then a pointer to void.
  43. */
  44. /* #include <asm/unaligned.h> */
  45. /*
  46. * Thanks to NetChip Technologies for donating this product ID.
  47. *
  48. * DO NOT REUSE THESE IDs with any other driver!! Ever!!
  49. * Instead: allocate your own, using normal USB-IF procedures.
  50. */
  51. #define FSG_VENDOR_ID 0x0525 /* NetChip */
  52. #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
  53. /*-------------------------------------------------------------------------*/
  54. #ifndef DEBUG
  55. #undef VERBOSE_DEBUG
  56. #undef DUMP_MSGS
  57. #endif /* !DEBUG */
  58. #ifdef VERBOSE_DEBUG
  59. #define VLDBG LDBG
  60. #else
  61. #define VLDBG(lun, fmt, args...) do { } while (0)
  62. #endif /* VERBOSE_DEBUG */
  63. /*
  64. #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
  65. #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
  66. #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
  67. #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
  68. */
  69. #define LDBG(lun, fmt, args...) do { } while (0)
  70. #define LERROR(lun, fmt, args...) do { } while (0)
  71. #define LWARN(lun, fmt, args...) do { } while (0)
  72. #define LINFO(lun, fmt, args...) do { } while (0)
  73. /*
  74. * Keep those macros in sync with those in
  75. * include/linux/usb/composite.h or else GCC will complain. If they
  76. * are identical (the same names of arguments, white spaces in the
  77. * same places) GCC will allow redefinition otherwise (even if some
  78. * white space is removed or added) warning will be issued.
  79. *
  80. * Those macros are needed here because File Storage Gadget does not
  81. * include the composite.h header. For composite gadgets those macros
  82. * are redundant since composite.h is included any way.
  83. *
  84. * One could check whether those macros are already defined (which
  85. * would indicate composite.h had been included) or not (which would
  86. * indicate we were in FSG) but this is not done because a warning is
  87. * desired if definitions here differ from the ones in composite.h.
  88. *
  89. * We want the definitions to match and be the same in File Storage
  90. * Gadget as well as Mass Storage Function (and so composite gadgets
  91. * using MSF). If someone changes them in composite.h it will produce
  92. * a warning in this file when building MSF.
  93. */
  94. #define DBG(d, fmt, args...) debug(fmt , ## args)
  95. #define VDBG(d, fmt, args...) debug(fmt , ## args)
  96. /* #define ERROR(d, fmt, args...) printf(fmt , ## args) */
  97. /* #define WARNING(d, fmt, args...) printf(fmt , ## args) */
  98. /* #define INFO(d, fmt, args...) printf(fmt , ## args) */
  99. /* #define DBG(d, fmt, args...) do { } while (0) */
  100. /* #define VDBG(d, fmt, args...) do { } while (0) */
  101. #define ERROR(d, fmt, args...) do { } while (0)
  102. #define WARNING(d, fmt, args...) do { } while (0)
  103. #define INFO(d, fmt, args...) do { } while (0)
  104. #ifdef DUMP_MSGS
  105. /* dump_msg(fsg, const char * label, const u8 * buf, unsigned length); */
  106. # define dump_msg(fsg, label, buf, length) do { \
  107. if (length < 512) { \
  108. DBG(fsg, "%s, length %u:\n", label, length); \
  109. print_hex_dump("", DUMP_PREFIX_OFFSET, \
  110. 16, 1, buf, length, 0); \
  111. } \
  112. } while (0)
  113. # define dump_cdb(fsg) do { } while (0)
  114. #else
  115. # define dump_msg(fsg, /* const char * */ label, \
  116. /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
  117. # ifdef VERBOSE_DEBUG
  118. # define dump_cdb(fsg) \
  119. print_hex_dump("SCSI CDB: ", DUMP_PREFIX_NONE, \
  120. 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
  121. # else
  122. # define dump_cdb(fsg) do { } while (0)
  123. # endif /* VERBOSE_DEBUG */
  124. #endif /* DUMP_MSGS */
  125. /*-------------------------------------------------------------------------*/
  126. /* SCSI device types */
  127. #define TYPE_DISK 0x00
  128. #define TYPE_CDROM 0x05
  129. /* USB protocol value = the transport method */
  130. #define USB_PR_CBI 0x00 /* Control/Bulk/Interrupt */
  131. #define USB_PR_CB 0x01 /* Control/Bulk w/o interrupt */
  132. #define USB_PR_BULK 0x50 /* Bulk-only */
  133. /* USB subclass value = the protocol encapsulation */
  134. #define USB_SC_RBC 0x01 /* Reduced Block Commands (flash) */
  135. #define USB_SC_8020 0x02 /* SFF-8020i, MMC-2, ATAPI (CD-ROM) */
  136. #define USB_SC_QIC 0x03 /* QIC-157 (tape) */
  137. #define USB_SC_UFI 0x04 /* UFI (floppy) */
  138. #define USB_SC_8070 0x05 /* SFF-8070i (removable) */
  139. #define USB_SC_SCSI 0x06 /* Transparent SCSI */
  140. /* Bulk-only data structures */
  141. /* Command Block Wrapper */
  142. struct fsg_bulk_cb_wrap {
  143. __le32 Signature; /* Contains 'USBC' */
  144. u32 Tag; /* Unique per command id */
  145. __le32 DataTransferLength; /* Size of the data */
  146. u8 Flags; /* Direction in bit 7 */
  147. u8 Lun; /* LUN (normally 0) */
  148. u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
  149. u8 CDB[16]; /* Command Data Block */
  150. };
  151. #define USB_BULK_CB_WRAP_LEN 31
  152. #define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
  153. #define USB_BULK_IN_FLAG 0x80
  154. /* Command Status Wrapper */
  155. struct bulk_cs_wrap {
  156. __le32 Signature; /* Should = 'USBS' */
  157. u32 Tag; /* Same as original command */
  158. __le32 Residue; /* Amount not transferred */
  159. u8 Status; /* See below */
  160. };
  161. #define USB_BULK_CS_WRAP_LEN 13
  162. #define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
  163. #define USB_STATUS_PASS 0
  164. #define USB_STATUS_FAIL 1
  165. #define USB_STATUS_PHASE_ERROR 2
  166. /* Bulk-only class specific requests */
  167. #define USB_BULK_RESET_REQUEST 0xff
  168. #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
  169. /* CBI Interrupt data structure */
  170. struct interrupt_data {
  171. u8 bType;
  172. u8 bValue;
  173. };
  174. #define CBI_INTERRUPT_DATA_LEN 2
  175. /* CBI Accept Device-Specific Command request */
  176. #define USB_CBI_ADSC_REQUEST 0x00
  177. /* Length of a SCSI Command Data Block */
  178. #define MAX_COMMAND_SIZE 16
  179. /* SCSI commands that we recognize */
  180. #define SC_FORMAT_UNIT 0x04
  181. #define SC_INQUIRY 0x12
  182. #define SC_MODE_SELECT_6 0x15
  183. #define SC_MODE_SELECT_10 0x55
  184. #define SC_MODE_SENSE_6 0x1a
  185. #define SC_MODE_SENSE_10 0x5a
  186. #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
  187. #define SC_READ_6 0x08
  188. #define SC_READ_10 0x28
  189. #define SC_READ_12 0xa8
  190. #define SC_READ_CAPACITY 0x25
  191. #define SC_READ_FORMAT_CAPACITIES 0x23
  192. #define SC_READ_HEADER 0x44
  193. #define SC_READ_TOC 0x43
  194. #define SC_RELEASE 0x17
  195. #define SC_REQUEST_SENSE 0x03
  196. #define SC_RESERVE 0x16
  197. #define SC_SEND_DIAGNOSTIC 0x1d
  198. #define SC_START_STOP_UNIT 0x1b
  199. #define SC_SYNCHRONIZE_CACHE 0x35
  200. #define SC_TEST_UNIT_READY 0x00
  201. #define SC_VERIFY 0x2f
  202. #define SC_WRITE_6 0x0a
  203. #define SC_WRITE_10 0x2a
  204. #define SC_WRITE_12 0xaa
  205. /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
  206. #define SS_NO_SENSE 0
  207. #define SS_COMMUNICATION_FAILURE 0x040800
  208. #define SS_INVALID_COMMAND 0x052000
  209. #define SS_INVALID_FIELD_IN_CDB 0x052400
  210. #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
  211. #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
  212. #define SS_MEDIUM_NOT_PRESENT 0x023a00
  213. #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
  214. #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
  215. #define SS_RESET_OCCURRED 0x062900
  216. #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
  217. #define SS_UNRECOVERED_READ_ERROR 0x031100
  218. #define SS_WRITE_ERROR 0x030c02
  219. #define SS_WRITE_PROTECTED 0x072700
  220. #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
  221. #define ASC(x) ((u8) ((x) >> 8))
  222. #define ASCQ(x) ((u8) (x))
  223. struct device_attribute { int i; };
  224. #define ETOOSMALL 525
  225. #include <usb_mass_storage.h>
  226. /*-------------------------------------------------------------------------*/
  227. struct fsg_lun {
  228. loff_t file_length;
  229. loff_t num_sectors;
  230. unsigned int initially_ro:1;
  231. unsigned int ro:1;
  232. unsigned int removable:1;
  233. unsigned int cdrom:1;
  234. unsigned int prevent_medium_removal:1;
  235. unsigned int registered:1;
  236. unsigned int info_valid:1;
  237. unsigned int nofua:1;
  238. u32 sense_data;
  239. u32 sense_data_info;
  240. u32 unit_attention_data;
  241. struct device dev;
  242. };
  243. #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
  244. #if 0
  245. static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
  246. {
  247. return container_of(dev, struct fsg_lun, dev);
  248. }
  249. #endif
  250. /* Big enough to hold our biggest descriptor */
  251. #define EP0_BUFSIZE 256
  252. #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
  253. /* Number of buffers we will use. 2 is enough for double-buffering */
  254. #define FSG_NUM_BUFFERS 2
  255. /* Default size of buffer length. */
  256. #define FSG_BUFLEN ((u32)131072)
  257. /* Maximal number of LUNs supported in mass storage function */
  258. #define FSG_MAX_LUNS 8
  259. enum fsg_buffer_state {
  260. BUF_STATE_EMPTY = 0,
  261. BUF_STATE_FULL,
  262. BUF_STATE_BUSY
  263. };
  264. struct fsg_buffhd {
  265. #ifdef FSG_BUFFHD_STATIC_BUFFER
  266. char buf[FSG_BUFLEN];
  267. #else
  268. void *buf;
  269. #endif
  270. enum fsg_buffer_state state;
  271. struct fsg_buffhd *next;
  272. /*
  273. * The NetChip 2280 is faster, and handles some protocol faults
  274. * better, if we don't submit any short bulk-out read requests.
  275. * So we will record the intended request length here.
  276. */
  277. unsigned int bulk_out_intended_length;
  278. struct usb_request *inreq;
  279. int inreq_busy;
  280. struct usb_request *outreq;
  281. int outreq_busy;
  282. };
  283. enum fsg_state {
  284. /* This one isn't used anywhere */
  285. FSG_STATE_COMMAND_PHASE = -10,
  286. FSG_STATE_DATA_PHASE,
  287. FSG_STATE_STATUS_PHASE,
  288. FSG_STATE_IDLE = 0,
  289. FSG_STATE_ABORT_BULK_OUT,
  290. FSG_STATE_RESET,
  291. FSG_STATE_INTERFACE_CHANGE,
  292. FSG_STATE_CONFIG_CHANGE,
  293. FSG_STATE_DISCONNECT,
  294. FSG_STATE_EXIT,
  295. FSG_STATE_TERMINATED
  296. };
  297. enum data_direction {
  298. DATA_DIR_UNKNOWN = 0,
  299. DATA_DIR_FROM_HOST,
  300. DATA_DIR_TO_HOST,
  301. DATA_DIR_NONE
  302. };
  303. /*-------------------------------------------------------------------------*/
  304. static inline u32 get_unaligned_be24(u8 *buf)
  305. {
  306. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  307. }
  308. /*-------------------------------------------------------------------------*/
  309. enum {
  310. #ifndef FSG_NO_DEVICE_STRINGS
  311. FSG_STRING_MANUFACTURER = 1,
  312. FSG_STRING_PRODUCT,
  313. FSG_STRING_SERIAL,
  314. FSG_STRING_CONFIG,
  315. #endif
  316. FSG_STRING_INTERFACE
  317. };
  318. #ifndef FSG_NO_OTG
  319. static struct usb_otg_descriptor
  320. fsg_otg_desc = {
  321. .bLength = sizeof fsg_otg_desc,
  322. .bDescriptorType = USB_DT_OTG,
  323. .bmAttributes = USB_OTG_SRP,
  324. };
  325. #endif
  326. /* There is only one interface. */
  327. static struct usb_interface_descriptor
  328. fsg_intf_desc = {
  329. .bLength = sizeof fsg_intf_desc,
  330. .bDescriptorType = USB_DT_INTERFACE,
  331. .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
  332. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  333. .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
  334. .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
  335. .iInterface = FSG_STRING_INTERFACE,
  336. };
  337. /*
  338. * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
  339. * interrupt-in.
  340. */
  341. static struct usb_endpoint_descriptor
  342. fsg_fs_bulk_in_desc = {
  343. .bLength = USB_DT_ENDPOINT_SIZE,
  344. .bDescriptorType = USB_DT_ENDPOINT,
  345. .bEndpointAddress = USB_DIR_IN,
  346. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  347. /* wMaxPacketSize set by autoconfiguration */
  348. };
  349. static struct usb_endpoint_descriptor
  350. fsg_fs_bulk_out_desc = {
  351. .bLength = USB_DT_ENDPOINT_SIZE,
  352. .bDescriptorType = USB_DT_ENDPOINT,
  353. .bEndpointAddress = USB_DIR_OUT,
  354. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  355. /* wMaxPacketSize set by autoconfiguration */
  356. };
  357. #ifndef FSG_NO_INTR_EP
  358. static struct usb_endpoint_descriptor
  359. fsg_fs_intr_in_desc = {
  360. .bLength = USB_DT_ENDPOINT_SIZE,
  361. .bDescriptorType = USB_DT_ENDPOINT,
  362. .bEndpointAddress = USB_DIR_IN,
  363. .bmAttributes = USB_ENDPOINT_XFER_INT,
  364. .wMaxPacketSize = cpu_to_le16(2),
  365. .bInterval = 32, /* frames -> 32 ms */
  366. };
  367. #ifndef FSG_NO_OTG
  368. # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
  369. #else
  370. # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
  371. #endif
  372. #endif
  373. static struct usb_descriptor_header *fsg_fs_function[] = {
  374. #ifndef FSG_NO_OTG
  375. (struct usb_descriptor_header *) &fsg_otg_desc,
  376. #endif
  377. (struct usb_descriptor_header *) &fsg_intf_desc,
  378. (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
  379. (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
  380. #ifndef FSG_NO_INTR_EP
  381. (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
  382. #endif
  383. NULL,
  384. };
  385. /*
  386. * USB 2.0 devices need to expose both high speed and full speed
  387. * descriptors, unless they only run at full speed.
  388. *
  389. * That means alternate endpoint descriptors (bigger packets)
  390. * and a "device qualifier" ... plus more construction options
  391. * for the configuration descriptor.
  392. */
  393. static struct usb_endpoint_descriptor
  394. fsg_hs_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(512),
  400. };
  401. static struct usb_endpoint_descriptor
  402. fsg_hs_bulk_out_desc = {
  403. .bLength = USB_DT_ENDPOINT_SIZE,
  404. .bDescriptorType = USB_DT_ENDPOINT,
  405. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  406. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  407. .wMaxPacketSize = cpu_to_le16(512),
  408. .bInterval = 1, /* NAK every 1 uframe */
  409. };
  410. #ifndef FSG_NO_INTR_EP
  411. static struct usb_endpoint_descriptor
  412. fsg_hs_intr_in_desc = {
  413. .bLength = USB_DT_ENDPOINT_SIZE,
  414. .bDescriptorType = USB_DT_ENDPOINT,
  415. /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
  416. .bmAttributes = USB_ENDPOINT_XFER_INT,
  417. .wMaxPacketSize = cpu_to_le16(2),
  418. .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
  419. };
  420. #ifndef FSG_NO_OTG
  421. # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
  422. #else
  423. # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
  424. #endif
  425. #endif
  426. static struct usb_descriptor_header *fsg_hs_function[] = {
  427. #ifndef FSG_NO_OTG
  428. (struct usb_descriptor_header *) &fsg_otg_desc,
  429. #endif
  430. (struct usb_descriptor_header *) &fsg_intf_desc,
  431. (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
  432. (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
  433. #ifndef FSG_NO_INTR_EP
  434. (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
  435. #endif
  436. NULL,
  437. };
  438. /* Maxpacket and other transfer characteristics vary by speed. */
  439. static struct usb_endpoint_descriptor *
  440. fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
  441. struct usb_endpoint_descriptor *hs)
  442. {
  443. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  444. return hs;
  445. return fs;
  446. }
  447. /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
  448. static struct usb_string fsg_strings[] = {
  449. #ifndef FSG_NO_DEVICE_STRINGS
  450. {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
  451. {FSG_STRING_PRODUCT, fsg_string_product},
  452. {FSG_STRING_SERIAL, fsg_string_serial},
  453. {FSG_STRING_CONFIG, fsg_string_config},
  454. #endif
  455. {FSG_STRING_INTERFACE, fsg_string_interface},
  456. {}
  457. };
  458. static struct usb_gadget_strings fsg_stringtab = {
  459. .language = 0x0409, /* en-us */
  460. .strings = fsg_strings,
  461. };
  462. /*-------------------------------------------------------------------------*/
  463. /*
  464. * If the next two routines are called while the gadget is registered,
  465. * the caller must own fsg->filesem for writing.
  466. */
  467. static int fsg_lun_open(struct fsg_lun *curlun, unsigned int num_sectors,
  468. const char *filename)
  469. {
  470. int ro;
  471. /* R/W if we can, R/O if we must */
  472. ro = curlun->initially_ro;
  473. curlun->ro = ro;
  474. curlun->file_length = num_sectors << 9;
  475. curlun->num_sectors = num_sectors;
  476. debug("open backing file: %s\n", filename);
  477. return 0;
  478. }
  479. static void fsg_lun_close(struct fsg_lun *curlun)
  480. {
  481. }
  482. /*-------------------------------------------------------------------------*/
  483. /*
  484. * Sync the file data, don't bother with the metadata.
  485. * This code was copied from fs/buffer.c:sys_fdatasync().
  486. */
  487. static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
  488. {
  489. return 0;
  490. }
  491. static void store_cdrom_address(u8 *dest, int msf, u32 addr)
  492. {
  493. if (msf) {
  494. /* Convert to Minutes-Seconds-Frames */
  495. addr >>= 2; /* Convert to 2048-byte frames */
  496. addr += 2*75; /* Lead-in occupies 2 seconds */
  497. dest[3] = addr % 75; /* Frames */
  498. addr /= 75;
  499. dest[2] = addr % 60; /* Seconds */
  500. addr /= 60;
  501. dest[1] = addr; /* Minutes */
  502. dest[0] = 0; /* Reserved */
  503. } else {
  504. /* Absolute sector */
  505. put_unaligned_be32(addr, dest);
  506. }
  507. }
  508. /*-------------------------------------------------------------------------*/