storage_common.c 18 KB

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