storage_common.c 17 KB

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