storage_common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. /*-------------------------------------------------------------------------*/
  9. #ifndef DEBUG
  10. #undef VERBOSE_DEBUG
  11. #undef DUMP_MSGS
  12. #endif /* !DEBUG */
  13. #ifdef VERBOSE_DEBUG
  14. #define VLDBG LDBG
  15. #else
  16. #define VLDBG(lun, fmt, args...) do { } while (0)
  17. #endif /* VERBOSE_DEBUG */
  18. #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
  19. #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
  20. #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
  21. #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
  22. #define DBG(d, fmt, args...) dev_dbg (&(d)->gadget->dev, fmt, ## args)
  23. #define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev, fmt, ## args)
  24. #define ERROR(d, fmt, args...) dev_err (&(d)->gadget->dev, fmt, ## args)
  25. #define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev, fmt, ## args)
  26. #define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev, fmt, ## args)
  27. #ifdef DUMP_MSGS
  28. # define dump_msg(fsg, /* const char * */ label, \
  29. /* const u8 * */ buf, /* unsigned */ length) do { \
  30. if (length < 512) { \
  31. DBG(fsg, "%s, length %u:\n", label, length); \
  32. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
  33. 16, 1, buf, length, 0); \
  34. } \
  35. } while (0)
  36. # define dump_cdb(fsg) do { } while (0)
  37. #else
  38. # define dump_msg(fsg, /* const char * */ label, \
  39. /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
  40. # ifdef VERBOSE_DEBUG
  41. #define dump_cdb(fsg) \
  42. print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
  43. 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
  44. # else
  45. # define dump_cdb(fsg) do { } while (0)
  46. # endif /* VERBOSE_DEBUG */
  47. #endif /* DUMP_MSGS */
  48. /*-------------------------------------------------------------------------*/
  49. /* SCSI device types */
  50. #define TYPE_DISK 0x00
  51. #define TYPE_CDROM 0x05
  52. /* USB protocol value = the transport method */
  53. #define USB_PR_CBI 0x00 // Control/Bulk/Interrupt
  54. #define USB_PR_CB 0x01 // Control/Bulk w/o interrupt
  55. #define USB_PR_BULK 0x50 // Bulk-only
  56. /* USB subclass value = the protocol encapsulation */
  57. #define USB_SC_RBC 0x01 // Reduced Block Commands (flash)
  58. #define USB_SC_8020 0x02 // SFF-8020i, MMC-2, ATAPI (CD-ROM)
  59. #define USB_SC_QIC 0x03 // QIC-157 (tape)
  60. #define USB_SC_UFI 0x04 // UFI (floppy)
  61. #define USB_SC_8070 0x05 // SFF-8070i (removable)
  62. #define USB_SC_SCSI 0x06 // Transparent SCSI
  63. /* Bulk-only data structures */
  64. /* Command Block Wrapper */
  65. struct bulk_cb_wrap {
  66. __le32 Signature; // Contains 'USBC'
  67. u32 Tag; // Unique per command id
  68. __le32 DataTransferLength; // Size of the data
  69. u8 Flags; // Direction in bit 7
  70. u8 Lun; // LUN (normally 0)
  71. u8 Length; // Of the CDB, <= MAX_COMMAND_SIZE
  72. u8 CDB[16]; // Command Data Block
  73. };
  74. #define USB_BULK_CB_WRAP_LEN 31
  75. #define USB_BULK_CB_SIG 0x43425355 // Spells out USBC
  76. #define USB_BULK_IN_FLAG 0x80
  77. /* Command Status Wrapper */
  78. struct bulk_cs_wrap {
  79. __le32 Signature; // Should = 'USBS'
  80. u32 Tag; // Same as original command
  81. __le32 Residue; // Amount not transferred
  82. u8 Status; // See below
  83. };
  84. #define USB_BULK_CS_WRAP_LEN 13
  85. #define USB_BULK_CS_SIG 0x53425355 // Spells out 'USBS'
  86. #define USB_STATUS_PASS 0
  87. #define USB_STATUS_FAIL 1
  88. #define USB_STATUS_PHASE_ERROR 2
  89. /* Bulk-only class specific requests */
  90. #define USB_BULK_RESET_REQUEST 0xff
  91. #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
  92. /* CBI Interrupt data structure */
  93. struct interrupt_data {
  94. u8 bType;
  95. u8 bValue;
  96. };
  97. #define CBI_INTERRUPT_DATA_LEN 2
  98. /* CBI Accept Device-Specific Command request */
  99. #define USB_CBI_ADSC_REQUEST 0x00
  100. #define MAX_COMMAND_SIZE 16 // Length of a SCSI Command Data Block
  101. /* SCSI commands that we recognize */
  102. #define SC_FORMAT_UNIT 0x04
  103. #define SC_INQUIRY 0x12
  104. #define SC_MODE_SELECT_6 0x15
  105. #define SC_MODE_SELECT_10 0x55
  106. #define SC_MODE_SENSE_6 0x1a
  107. #define SC_MODE_SENSE_10 0x5a
  108. #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
  109. #define SC_READ_6 0x08
  110. #define SC_READ_10 0x28
  111. #define SC_READ_12 0xa8
  112. #define SC_READ_CAPACITY 0x25
  113. #define SC_READ_FORMAT_CAPACITIES 0x23
  114. #define SC_READ_HEADER 0x44
  115. #define SC_READ_TOC 0x43
  116. #define SC_RELEASE 0x17
  117. #define SC_REQUEST_SENSE 0x03
  118. #define SC_RESERVE 0x16
  119. #define SC_SEND_DIAGNOSTIC 0x1d
  120. #define SC_START_STOP_UNIT 0x1b
  121. #define SC_SYNCHRONIZE_CACHE 0x35
  122. #define SC_TEST_UNIT_READY 0x00
  123. #define SC_VERIFY 0x2f
  124. #define SC_WRITE_6 0x0a
  125. #define SC_WRITE_10 0x2a
  126. #define SC_WRITE_12 0xaa
  127. /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
  128. #define SS_NO_SENSE 0
  129. #define SS_COMMUNICATION_FAILURE 0x040800
  130. #define SS_INVALID_COMMAND 0x052000
  131. #define SS_INVALID_FIELD_IN_CDB 0x052400
  132. #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
  133. #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
  134. #define SS_MEDIUM_NOT_PRESENT 0x023a00
  135. #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
  136. #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
  137. #define SS_RESET_OCCURRED 0x062900
  138. #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
  139. #define SS_UNRECOVERED_READ_ERROR 0x031100
  140. #define SS_WRITE_ERROR 0x030c02
  141. #define SS_WRITE_PROTECTED 0x072700
  142. #define SK(x) ((u8) ((x) >> 16)) // Sense Key byte, etc.
  143. #define ASC(x) ((u8) ((x) >> 8))
  144. #define ASCQ(x) ((u8) (x))
  145. /*-------------------------------------------------------------------------*/
  146. struct lun {
  147. struct file *filp;
  148. loff_t file_length;
  149. loff_t num_sectors;
  150. unsigned int ro : 1;
  151. unsigned int prevent_medium_removal : 1;
  152. unsigned int registered : 1;
  153. unsigned int info_valid : 1;
  154. u32 sense_data;
  155. u32 sense_data_info;
  156. u32 unit_attention_data;
  157. struct device dev;
  158. };
  159. #define backing_file_is_open(curlun) ((curlun)->filp != NULL)
  160. static struct lun *dev_to_lun(struct device *dev)
  161. {
  162. return container_of(dev, struct lun, dev);
  163. }
  164. /* Big enough to hold our biggest descriptor */
  165. #define EP0_BUFSIZE 256
  166. #define DELAYED_STATUS (EP0_BUFSIZE + 999) // An impossibly large value
  167. /* Number of buffers we will use. 2 is enough for double-buffering */
  168. #define NUM_BUFFERS 2
  169. enum fsg_buffer_state {
  170. BUF_STATE_EMPTY = 0,
  171. BUF_STATE_FULL,
  172. BUF_STATE_BUSY
  173. };
  174. struct fsg_buffhd {
  175. void *buf;
  176. enum fsg_buffer_state state;
  177. struct fsg_buffhd *next;
  178. /* The NetChip 2280 is faster, and handles some protocol faults
  179. * better, if we don't submit any short bulk-out read requests.
  180. * So we will record the intended request length here. */
  181. unsigned int bulk_out_intended_length;
  182. struct usb_request *inreq;
  183. int inreq_busy;
  184. struct usb_request *outreq;
  185. int outreq_busy;
  186. };
  187. enum fsg_state {
  188. FSG_STATE_COMMAND_PHASE = -10, // This one isn't used anywhere
  189. FSG_STATE_DATA_PHASE,
  190. FSG_STATE_STATUS_PHASE,
  191. FSG_STATE_IDLE = 0,
  192. FSG_STATE_ABORT_BULK_OUT,
  193. FSG_STATE_RESET,
  194. FSG_STATE_INTERFACE_CHANGE,
  195. FSG_STATE_CONFIG_CHANGE,
  196. FSG_STATE_DISCONNECT,
  197. FSG_STATE_EXIT,
  198. FSG_STATE_TERMINATED
  199. };
  200. enum data_direction {
  201. DATA_DIR_UNKNOWN = 0,
  202. DATA_DIR_FROM_HOST,
  203. DATA_DIR_TO_HOST,
  204. DATA_DIR_NONE
  205. };
  206. /*-------------------------------------------------------------------------*/
  207. static inline u32 get_unaligned_be24(u8 *buf)
  208. {
  209. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  210. }
  211. /*-------------------------------------------------------------------------*/
  212. #define STRING_MANUFACTURER 1
  213. #define STRING_PRODUCT 2
  214. #define STRING_SERIAL 3
  215. #define STRING_CONFIG 4
  216. #define STRING_INTERFACE 5
  217. static struct usb_otg_descriptor
  218. otg_desc = {
  219. .bLength = sizeof(otg_desc),
  220. .bDescriptorType = USB_DT_OTG,
  221. .bmAttributes = USB_OTG_SRP,
  222. };
  223. /* There is only one interface. */
  224. static struct usb_interface_descriptor
  225. intf_desc = {
  226. .bLength = sizeof intf_desc,
  227. .bDescriptorType = USB_DT_INTERFACE,
  228. .bNumEndpoints = 2, // Adjusted during fsg_bind()
  229. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  230. .bInterfaceSubClass = USB_SC_SCSI, // Adjusted during fsg_bind()
  231. .bInterfaceProtocol = USB_PR_BULK, // Adjusted during fsg_bind()
  232. .iInterface = STRING_INTERFACE,
  233. };
  234. /* Three full-speed endpoint descriptors: bulk-in, bulk-out,
  235. * and interrupt-in. */
  236. static struct usb_endpoint_descriptor
  237. fs_bulk_in_desc = {
  238. .bLength = USB_DT_ENDPOINT_SIZE,
  239. .bDescriptorType = USB_DT_ENDPOINT,
  240. .bEndpointAddress = USB_DIR_IN,
  241. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  242. /* wMaxPacketSize set by autoconfiguration */
  243. };
  244. static struct usb_endpoint_descriptor
  245. fs_bulk_out_desc = {
  246. .bLength = USB_DT_ENDPOINT_SIZE,
  247. .bDescriptorType = USB_DT_ENDPOINT,
  248. .bEndpointAddress = USB_DIR_OUT,
  249. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  250. /* wMaxPacketSize set by autoconfiguration */
  251. };
  252. static struct usb_endpoint_descriptor
  253. fs_intr_in_desc = {
  254. .bLength = USB_DT_ENDPOINT_SIZE,
  255. .bDescriptorType = USB_DT_ENDPOINT,
  256. .bEndpointAddress = USB_DIR_IN,
  257. .bmAttributes = USB_ENDPOINT_XFER_INT,
  258. .wMaxPacketSize = cpu_to_le16(2),
  259. .bInterval = 32, // frames -> 32 ms
  260. };
  261. static const struct usb_descriptor_header *fs_function[] = {
  262. (struct usb_descriptor_header *) &otg_desc,
  263. (struct usb_descriptor_header *) &intf_desc,
  264. (struct usb_descriptor_header *) &fs_bulk_in_desc,
  265. (struct usb_descriptor_header *) &fs_bulk_out_desc,
  266. (struct usb_descriptor_header *) &fs_intr_in_desc,
  267. NULL,
  268. };
  269. #define FS_FUNCTION_PRE_EP_ENTRIES 2
  270. /*
  271. * USB 2.0 devices need to expose both high speed and full speed
  272. * descriptors, unless they only run at full speed.
  273. *
  274. * That means alternate endpoint descriptors (bigger packets)
  275. * and a "device qualifier" ... plus more construction options
  276. * for the config descriptor.
  277. */
  278. static struct usb_endpoint_descriptor
  279. hs_bulk_in_desc = {
  280. .bLength = USB_DT_ENDPOINT_SIZE,
  281. .bDescriptorType = USB_DT_ENDPOINT,
  282. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  283. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  284. .wMaxPacketSize = cpu_to_le16(512),
  285. };
  286. static struct usb_endpoint_descriptor
  287. hs_bulk_out_desc = {
  288. .bLength = USB_DT_ENDPOINT_SIZE,
  289. .bDescriptorType = USB_DT_ENDPOINT,
  290. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  291. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  292. .wMaxPacketSize = cpu_to_le16(512),
  293. .bInterval = 1, // NAK every 1 uframe
  294. };
  295. static struct usb_endpoint_descriptor
  296. hs_intr_in_desc = {
  297. .bLength = USB_DT_ENDPOINT_SIZE,
  298. .bDescriptorType = USB_DT_ENDPOINT,
  299. /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
  300. .bmAttributes = USB_ENDPOINT_XFER_INT,
  301. .wMaxPacketSize = cpu_to_le16(2),
  302. .bInterval = 9, // 2**(9-1) = 256 uframes -> 32 ms
  303. };
  304. static const struct usb_descriptor_header *hs_function[] = {
  305. (struct usb_descriptor_header *) &otg_desc,
  306. (struct usb_descriptor_header *) &intf_desc,
  307. (struct usb_descriptor_header *) &hs_bulk_in_desc,
  308. (struct usb_descriptor_header *) &hs_bulk_out_desc,
  309. (struct usb_descriptor_header *) &hs_intr_in_desc,
  310. NULL,
  311. };
  312. #define HS_FUNCTION_PRE_EP_ENTRIES 2
  313. /* Maxpacket and other transfer characteristics vary by speed. */
  314. static struct usb_endpoint_descriptor *
  315. ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
  316. struct usb_endpoint_descriptor *hs)
  317. {
  318. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  319. return hs;
  320. return fs;
  321. }
  322. /* The CBI specification limits the serial string to 12 uppercase hexadecimal
  323. * characters. */
  324. static char manufacturer[64];
  325. static char serial[13];
  326. /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
  327. static struct usb_string strings[] = {
  328. {STRING_MANUFACTURER, manufacturer},
  329. {STRING_PRODUCT, longname},
  330. {STRING_SERIAL, serial},
  331. {STRING_CONFIG, "Self-powered"},
  332. {STRING_INTERFACE, "Mass Storage"},
  333. {}
  334. };
  335. static struct usb_gadget_strings stringtab = {
  336. .language = 0x0409, // en-us
  337. .strings = strings,
  338. };
  339. /*-------------------------------------------------------------------------*/
  340. /* If the next two routines are called while the gadget is registered,
  341. * the caller must own fsg->filesem for writing. */
  342. static int open_backing_file(struct lun *curlun, const char *filename)
  343. {
  344. int ro;
  345. struct file *filp = NULL;
  346. int rc = -EINVAL;
  347. struct inode *inode = NULL;
  348. loff_t size;
  349. loff_t num_sectors;
  350. loff_t min_sectors;
  351. /* R/W if we can, R/O if we must */
  352. ro = curlun->ro;
  353. if (!ro) {
  354. filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
  355. if (-EROFS == PTR_ERR(filp))
  356. ro = 1;
  357. }
  358. if (ro)
  359. filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
  360. if (IS_ERR(filp)) {
  361. LINFO(curlun, "unable to open backing file: %s\n", filename);
  362. return PTR_ERR(filp);
  363. }
  364. if (!(filp->f_mode & FMODE_WRITE))
  365. ro = 1;
  366. if (filp->f_path.dentry)
  367. inode = filp->f_path.dentry->d_inode;
  368. if (inode && S_ISBLK(inode->i_mode)) {
  369. if (bdev_read_only(inode->i_bdev))
  370. ro = 1;
  371. } else if (!inode || !S_ISREG(inode->i_mode)) {
  372. LINFO(curlun, "invalid file type: %s\n", filename);
  373. goto out;
  374. }
  375. /* If we can't read the file, it's no good.
  376. * If we can't write the file, use it read-only. */
  377. if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
  378. LINFO(curlun, "file not readable: %s\n", filename);
  379. goto out;
  380. }
  381. if (!(filp->f_op->write || filp->f_op->aio_write))
  382. ro = 1;
  383. size = i_size_read(inode->i_mapping->host);
  384. if (size < 0) {
  385. LINFO(curlun, "unable to find file size: %s\n", filename);
  386. rc = (int) size;
  387. goto out;
  388. }
  389. num_sectors = size >> 9; // File size in 512-byte blocks
  390. min_sectors = 1;
  391. if (mod_data.cdrom) {
  392. num_sectors &= ~3; // Reduce to a multiple of 2048
  393. min_sectors = 300*4; // Smallest track is 300 frames
  394. if (num_sectors >= 256*60*75*4) {
  395. num_sectors = (256*60*75 - 1) * 4;
  396. LINFO(curlun, "file too big: %s\n", filename);
  397. LINFO(curlun, "using only first %d blocks\n",
  398. (int) num_sectors);
  399. }
  400. }
  401. if (num_sectors < min_sectors) {
  402. LINFO(curlun, "file too small: %s\n", filename);
  403. rc = -ETOOSMALL;
  404. goto out;
  405. }
  406. get_file(filp);
  407. curlun->ro = ro;
  408. curlun->filp = filp;
  409. curlun->file_length = size;
  410. curlun->num_sectors = num_sectors;
  411. LDBG(curlun, "open backing file: %s\n", filename);
  412. rc = 0;
  413. out:
  414. filp_close(filp, current->files);
  415. return rc;
  416. }
  417. static void close_backing_file(struct lun *curlun)
  418. {
  419. if (curlun->filp) {
  420. LDBG(curlun, "close backing file\n");
  421. fput(curlun->filp);
  422. curlun->filp = NULL;
  423. }
  424. }
  425. /*-------------------------------------------------------------------------*/
  426. /* Sync the file data, don't bother with the metadata.
  427. * This code was copied from fs/buffer.c:sys_fdatasync(). */
  428. static int fsync_sub(struct lun *curlun)
  429. {
  430. struct file *filp = curlun->filp;
  431. if (curlun->ro || !filp)
  432. return 0;
  433. return vfs_fsync(filp, filp->f_path.dentry, 1);
  434. }
  435. static void store_cdrom_address(u8 *dest, int msf, u32 addr)
  436. {
  437. if (msf) {
  438. /* Convert to Minutes-Seconds-Frames */
  439. addr >>= 2; /* Convert to 2048-byte frames */
  440. addr += 2*75; /* Lead-in occupies 2 seconds */
  441. dest[3] = addr % 75; /* Frames */
  442. addr /= 75;
  443. dest[2] = addr % 60; /* Seconds */
  444. addr /= 60;
  445. dest[1] = addr; /* Minutes */
  446. dest[0] = 0; /* Reserved */
  447. } else {
  448. /* Absolute sector */
  449. put_unaligned_be32(addr, dest);
  450. }
  451. }