storage_common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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_interface -- name of the interface
  17. */
  18. /*
  19. * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
  20. * sets the number of pipeline buffers (length of the fsg_buffhd array).
  21. * The valid range of num_buffers is: num >= 2 && num <= 4.
  22. */
  23. #include <linux/usb/storage.h>
  24. #include <scsi/scsi.h>
  25. #include <asm/unaligned.h>
  26. /*
  27. * Thanks to NetChip Technologies for donating this product ID.
  28. *
  29. * DO NOT REUSE THESE IDs with any other driver!! Ever!!
  30. * Instead: allocate your own, using normal USB-IF procedures.
  31. */
  32. #define FSG_VENDOR_ID 0x0525 /* NetChip */
  33. #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
  34. /*-------------------------------------------------------------------------*/
  35. #ifndef DEBUG
  36. #undef VERBOSE_DEBUG
  37. #undef DUMP_MSGS
  38. #endif /* !DEBUG */
  39. #ifdef VERBOSE_DEBUG
  40. #define VLDBG LDBG
  41. #else
  42. #define VLDBG(lun, fmt, args...) do { } while (0)
  43. #endif /* VERBOSE_DEBUG */
  44. #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
  45. #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
  46. #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
  47. #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
  48. #ifdef DUMP_MSGS
  49. # define dump_msg(fsg, /* const char * */ label, \
  50. /* const u8 * */ buf, /* unsigned */ length) do { \
  51. if (length < 512) { \
  52. DBG(fsg, "%s, length %u:\n", label, length); \
  53. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
  54. 16, 1, buf, length, 0); \
  55. } \
  56. } while (0)
  57. # define dump_cdb(fsg) do { } while (0)
  58. #else
  59. # define dump_msg(fsg, /* const char * */ label, \
  60. /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
  61. # ifdef VERBOSE_DEBUG
  62. # define dump_cdb(fsg) \
  63. print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
  64. 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
  65. # else
  66. # define dump_cdb(fsg) do { } while (0)
  67. # endif /* VERBOSE_DEBUG */
  68. #endif /* DUMP_MSGS */
  69. /*-------------------------------------------------------------------------*/
  70. /* Length of a SCSI Command Data Block */
  71. #define MAX_COMMAND_SIZE 16
  72. /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
  73. #define SS_NO_SENSE 0
  74. #define SS_COMMUNICATION_FAILURE 0x040800
  75. #define SS_INVALID_COMMAND 0x052000
  76. #define SS_INVALID_FIELD_IN_CDB 0x052400
  77. #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
  78. #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
  79. #define SS_MEDIUM_NOT_PRESENT 0x023a00
  80. #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
  81. #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
  82. #define SS_RESET_OCCURRED 0x062900
  83. #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
  84. #define SS_UNRECOVERED_READ_ERROR 0x031100
  85. #define SS_WRITE_ERROR 0x030c02
  86. #define SS_WRITE_PROTECTED 0x072700
  87. #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
  88. #define ASC(x) ((u8) ((x) >> 8))
  89. #define ASCQ(x) ((u8) (x))
  90. /*-------------------------------------------------------------------------*/
  91. struct fsg_lun {
  92. struct file *filp;
  93. loff_t file_length;
  94. loff_t num_sectors;
  95. unsigned int initially_ro:1;
  96. unsigned int ro:1;
  97. unsigned int removable:1;
  98. unsigned int cdrom:1;
  99. unsigned int prevent_medium_removal:1;
  100. unsigned int registered:1;
  101. unsigned int info_valid:1;
  102. unsigned int nofua:1;
  103. u32 sense_data;
  104. u32 sense_data_info;
  105. u32 unit_attention_data;
  106. unsigned int blkbits; /* Bits of logical block size of bound block device */
  107. unsigned int blksize; /* logical block size of bound block device */
  108. struct device dev;
  109. #ifdef CONFIG_MTK_ICUSB_SUPPORT
  110. char isICUSB;
  111. #endif
  112. };
  113. static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
  114. {
  115. return curlun->filp != NULL;
  116. }
  117. static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
  118. {
  119. return container_of(dev, struct fsg_lun, dev);
  120. }
  121. /* Big enough to hold our biggest descriptor */
  122. #define EP0_BUFSIZE 256
  123. #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
  124. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  125. static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
  126. module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);
  127. MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers");
  128. #else
  129. /*
  130. * Number of buffers we will use.
  131. * 2 is usually enough for good buffering pipeline
  132. */
  133. #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
  134. #endif /* CONFIG_USB_DEBUG */
  135. /* check if fsg_num_buffers is within a valid range */
  136. static inline int fsg_num_buffers_validate(void)
  137. {
  138. if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4)
  139. return 0;
  140. pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
  141. fsg_num_buffers, 2 ,4);
  142. return -EINVAL;
  143. }
  144. /* Default size of buffer length. */
  145. #define FSG_BUFLEN ((u32)16384)
  146. /* Maximal number of LUNs supported in mass storage function */
  147. #define FSG_MAX_LUNS 8
  148. enum fsg_buffer_state {
  149. BUF_STATE_EMPTY = 0,
  150. BUF_STATE_FULL,
  151. BUF_STATE_BUSY
  152. };
  153. struct fsg_buffhd {
  154. void *buf;
  155. enum fsg_buffer_state state;
  156. struct fsg_buffhd *next;
  157. /*
  158. * The NetChip 2280 is faster, and handles some protocol faults
  159. * better, if we don't submit any short bulk-out read requests.
  160. * So we will record the intended request length here.
  161. */
  162. unsigned int bulk_out_intended_length;
  163. struct usb_request *inreq;
  164. int inreq_busy;
  165. struct usb_request *outreq;
  166. int outreq_busy;
  167. };
  168. enum fsg_state {
  169. /* This one isn't used anywhere */
  170. FSG_STATE_COMMAND_PHASE = -10,
  171. FSG_STATE_DATA_PHASE,
  172. FSG_STATE_STATUS_PHASE,
  173. FSG_STATE_IDLE = 0,
  174. FSG_STATE_ABORT_BULK_OUT,
  175. FSG_STATE_RESET,
  176. FSG_STATE_INTERFACE_CHANGE,
  177. FSG_STATE_CONFIG_CHANGE,
  178. FSG_STATE_DISCONNECT,
  179. FSG_STATE_EXIT,
  180. FSG_STATE_TERMINATED
  181. };
  182. enum data_direction {
  183. DATA_DIR_UNKNOWN = 0,
  184. DATA_DIR_FROM_HOST,
  185. DATA_DIR_TO_HOST,
  186. DATA_DIR_NONE
  187. };
  188. /*-------------------------------------------------------------------------*/
  189. static inline u32 get_unaligned_be24(u8 *buf)
  190. {
  191. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  192. }
  193. /*-------------------------------------------------------------------------*/
  194. enum {
  195. FSG_STRING_INTERFACE
  196. };
  197. #ifdef CONFIG_USBIF_COMPLIANCE
  198. static struct usb_otg_descriptor
  199. fsg_otg_desc = {
  200. .bLength = sizeof fsg_otg_desc,
  201. .bDescriptorType = USB_DT_OTG,
  202. /* OTG 2.0: */
  203. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  204. .bcdOTG = cpu_to_le16(0x200),
  205. };
  206. #endif
  207. /* There is only one interface. */
  208. static struct usb_interface_descriptor
  209. fsg_intf_desc = {
  210. .bLength = sizeof fsg_intf_desc,
  211. .bDescriptorType = USB_DT_INTERFACE,
  212. .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
  213. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  214. .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
  215. .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
  216. .iInterface = FSG_STRING_INTERFACE,
  217. };
  218. /*
  219. * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
  220. * interrupt-in.
  221. */
  222. static struct usb_endpoint_descriptor
  223. fsg_fs_bulk_in_desc = {
  224. .bLength = USB_DT_ENDPOINT_SIZE,
  225. .bDescriptorType = USB_DT_ENDPOINT,
  226. .bEndpointAddress = USB_DIR_IN,
  227. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  228. /* wMaxPacketSize set by autoconfiguration */
  229. };
  230. static struct usb_endpoint_descriptor
  231. fsg_fs_bulk_out_desc = {
  232. .bLength = USB_DT_ENDPOINT_SIZE,
  233. .bDescriptorType = USB_DT_ENDPOINT,
  234. .bEndpointAddress = USB_DIR_OUT,
  235. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  236. /* wMaxPacketSize set by autoconfiguration */
  237. };
  238. static struct usb_descriptor_header *fsg_fs_function[] = {
  239. #ifdef CONFIG_USBIF_COMPLIANCE
  240. (struct usb_descriptor_header *) &fsg_otg_desc,
  241. #endif
  242. (struct usb_descriptor_header *) &fsg_intf_desc,
  243. (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
  244. (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
  245. NULL,
  246. };
  247. /*
  248. * USB 2.0 devices need to expose both high speed and full speed
  249. * descriptors, unless they only run at full speed.
  250. *
  251. * That means alternate endpoint descriptors (bigger packets)
  252. * and a "device qualifier" ... plus more construction options
  253. * for the configuration descriptor.
  254. */
  255. static struct usb_endpoint_descriptor
  256. fsg_hs_bulk_in_desc = {
  257. .bLength = USB_DT_ENDPOINT_SIZE,
  258. .bDescriptorType = USB_DT_ENDPOINT,
  259. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  260. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  261. .wMaxPacketSize = cpu_to_le16(512),
  262. };
  263. static struct usb_endpoint_descriptor
  264. fsg_hs_bulk_out_desc = {
  265. .bLength = USB_DT_ENDPOINT_SIZE,
  266. .bDescriptorType = USB_DT_ENDPOINT,
  267. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  268. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  269. .wMaxPacketSize = cpu_to_le16(512),
  270. .bInterval = 1, /* NAK every 1 uframe */
  271. };
  272. static struct usb_descriptor_header *fsg_hs_function[] = {
  273. #ifdef CONFIG_USBIF_COMPLIANCE
  274. (struct usb_descriptor_header *) &fsg_otg_desc,
  275. #endif
  276. (struct usb_descriptor_header *) &fsg_intf_desc,
  277. (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
  278. (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
  279. NULL,
  280. };
  281. static struct usb_endpoint_descriptor
  282. fsg_ss_bulk_in_desc = {
  283. .bLength = USB_DT_ENDPOINT_SIZE,
  284. .bDescriptorType = USB_DT_ENDPOINT,
  285. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  286. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  287. .wMaxPacketSize = cpu_to_le16(1024),
  288. };
  289. static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
  290. .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
  291. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  292. /*.bMaxBurst = DYNAMIC, */
  293. };
  294. static struct usb_endpoint_descriptor
  295. fsg_ss_bulk_out_desc = {
  296. .bLength = USB_DT_ENDPOINT_SIZE,
  297. .bDescriptorType = USB_DT_ENDPOINT,
  298. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  299. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  300. .wMaxPacketSize = cpu_to_le16(1024),
  301. };
  302. static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
  303. .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
  304. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  305. /*.bMaxBurst = DYNAMIC, */
  306. };
  307. static struct usb_descriptor_header *fsg_ss_function[] = {
  308. #ifdef CONFIG_USBIF_COMPLIANCE
  309. (struct usb_descriptor_header *) &fsg_otg_desc,
  310. #endif
  311. (struct usb_descriptor_header *) &fsg_intf_desc,
  312. (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
  313. (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
  314. (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
  315. (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
  316. NULL,
  317. };
  318. /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
  319. static struct usb_string fsg_strings[] = {
  320. {FSG_STRING_INTERFACE, fsg_string_interface},
  321. {}
  322. };
  323. static struct usb_gadget_strings fsg_stringtab = {
  324. .language = 0x0409, /* en-us */
  325. .strings = fsg_strings,
  326. };
  327. /*-------------------------------------------------------------------------*/
  328. /*
  329. * If the next two routines are called while the gadget is registered,
  330. * the caller must own fsg->filesem for writing.
  331. */
  332. static void fsg_lun_close(struct fsg_lun *curlun)
  333. {
  334. if (curlun->filp) {
  335. LDBG(curlun, "close backing file\n");
  336. fput(curlun->filp);
  337. curlun->filp = NULL;
  338. }
  339. }
  340. static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
  341. {
  342. int ro;
  343. struct file *filp = NULL;
  344. int rc = -EINVAL;
  345. struct inode *inode = NULL;
  346. loff_t size;
  347. loff_t num_sectors;
  348. loff_t min_sectors;
  349. unsigned int blkbits;
  350. unsigned int blksize;
  351. #ifdef CONFIG_MTK_ICUSB_SUPPORT
  352. #define ICUSB_STORAGE_LABEL "/dev/block/vold/8:"
  353. if(strstr(filename, ICUSB_STORAGE_LABEL))
  354. {
  355. printk(KERN_WARNING "filename : %s, set isICUSB to 0\n", filename);
  356. curlun->isICUSB = 1;
  357. }
  358. else
  359. {
  360. printk(KERN_WARNING "filename : %s, set isICUSB to 1\n", filename);
  361. curlun->isICUSB = 0;
  362. }
  363. #endif
  364. /* R/W if we can, R/O if we must */
  365. ro = curlun->initially_ro;
  366. if (!ro) {
  367. filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
  368. if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
  369. ro = 1;
  370. }
  371. if (ro)
  372. filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
  373. if (IS_ERR(filp)) {
  374. LINFO(curlun, "unable to open backing file: %s\n", filename);
  375. return PTR_ERR(filp);
  376. }
  377. if (!(filp->f_mode & FMODE_WRITE))
  378. ro = 1;
  379. inode = file_inode(filp);
  380. if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
  381. LINFO(curlun, "invalid file type: %s\n", filename);
  382. goto out;
  383. }
  384. /*
  385. * If we can't read the file, it's no good.
  386. * If we can't write the file, use it read-only.
  387. */
  388. if (!(filp->f_op->read || filp->f_op->aio_read)) {
  389. LINFO(curlun, "file not readable: %s\n", filename);
  390. goto out;
  391. }
  392. if (!(filp->f_op->write || filp->f_op->aio_write))
  393. ro = 1;
  394. size = i_size_read(inode->i_mapping->host);
  395. if (size < 0) {
  396. LINFO(curlun, "unable to find file size: %s\n", filename);
  397. rc = (int) size;
  398. goto out;
  399. }
  400. if (curlun->cdrom) {
  401. blksize = 2048;
  402. blkbits = 11;
  403. } else if (inode->i_bdev) {
  404. blksize = bdev_logical_block_size(inode->i_bdev);
  405. blkbits = blksize_bits(blksize);
  406. } else {
  407. blksize = 512;
  408. blkbits = 9;
  409. }
  410. num_sectors = size >> blkbits; /* File size in logic-block-size blocks */
  411. min_sectors = 1;
  412. if (curlun->cdrom) {
  413. min_sectors = 300; /* Smallest track is 300 frames */
  414. if (num_sectors >= 256*60*75) {
  415. num_sectors = 256*60*75 - 1;
  416. LINFO(curlun, "file too big: %s\n", filename);
  417. LINFO(curlun, "using only first %d blocks\n",
  418. (int) num_sectors);
  419. }
  420. }
  421. if (num_sectors < min_sectors) {
  422. LINFO(curlun, "file too small: %s\n", filename);
  423. rc = -ETOOSMALL;
  424. goto out;
  425. }
  426. if (fsg_lun_is_open(curlun))
  427. fsg_lun_close(curlun);
  428. curlun->blksize = blksize;
  429. curlun->blkbits = blkbits;
  430. curlun->ro = ro;
  431. curlun->filp = filp;
  432. curlun->file_length = size;
  433. curlun->num_sectors = num_sectors;
  434. LDBG(curlun, "open backing file: %s\n", filename);
  435. return 0;
  436. out:
  437. fput(filp);
  438. return rc;
  439. }
  440. /*-------------------------------------------------------------------------*/
  441. /*
  442. * Sync the file data, don't bother with the metadata.
  443. * This code was copied from fs/buffer.c:sys_fdatasync().
  444. */
  445. static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
  446. {
  447. struct file *filp = curlun->filp;
  448. if (curlun->ro || !filp)
  449. return 0;
  450. return vfs_fsync(filp, 1);
  451. }
  452. static void store_cdrom_address(u8 *dest, int msf, u32 addr)
  453. {
  454. if (msf) {
  455. /* Convert to Minutes-Seconds-Frames */
  456. addr >>= 2; /* Convert to 2048-byte frames */
  457. addr += 2*75; /* Lead-in occupies 2 seconds */
  458. dest[3] = addr % 75; /* Frames */
  459. addr /= 75;
  460. dest[2] = addr % 60; /* Seconds */
  461. addr /= 60;
  462. dest[1] = addr; /* Minutes */
  463. dest[0] = 0; /* Reserved */
  464. } else {
  465. /* Absolute sector */
  466. put_unaligned_be32(addr, dest);
  467. }
  468. }
  469. /**
  470. * fsg_get_toc() - Builds a TOC with required format @format.
  471. * @curlun: The LUN for which the TOC has to be built
  472. * @msf: Min Sec Frame format or LBA format for address
  473. * @format: TOC format code
  474. * @buf: The buffer into which the TOC is built
  475. *
  476. * Builds a Table of Content which can be used as data for READ_TOC command.
  477. * The TOC simulates a single session, single track CD-ROM mode 1 disc.
  478. *
  479. * Returns number of bytes written to @buf, -EINVAL if format not supported.
  480. */
  481. static int fsg_get_toc(struct fsg_lun *curlun, int msf, int format, u8 *buf)
  482. {
  483. int i, len;
  484. switch (format) {
  485. case 0:
  486. /* Formatted TOC */
  487. len = 4 + 2*8; /* 4 byte header + 2 descriptors */
  488. memset(buf, 0, len);
  489. buf[1] = len - 2; /* TOC Length excludes length field */
  490. buf[2] = 1; /* First track number */
  491. buf[3] = 1; /* Last track number */
  492. buf[5] = 0x16; /* Data track, copying allowed */
  493. buf[6] = 0x01; /* Only track is number 1 */
  494. store_cdrom_address(&buf[8], msf, 0);
  495. buf[13] = 0x16; /* Lead-out track is data */
  496. buf[14] = 0xAA; /* Lead-out track number */
  497. store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  498. break;
  499. case 2:
  500. /* Raw TOC */
  501. len = 4 + 3*11; /* 4 byte header + 3 descriptors */
  502. memset(buf, 0, len); /* Header + A0, A1 & A2 descriptors */
  503. buf[1] = len - 2; /* TOC Length excludes length field */
  504. buf[2] = 1; /* First complete session */
  505. buf[3] = 1; /* Last complete session */
  506. buf += 4;
  507. /* fill in A0, A1 and A2 points */
  508. for (i = 0; i < 3; i++) {
  509. buf[0] = 1; /* Session number */
  510. buf[1] = 0x16; /* Data track, copying allowed */
  511. /* 2 - Track number 0 -> TOC */
  512. buf[3] = 0xA0 + i; /* A0, A1, A2 point */
  513. /* 4, 5, 6 - Min, sec, frame is zero */
  514. buf[8] = 1; /* Pmin: last track number */
  515. buf += 11; /* go to next track descriptor */
  516. }
  517. buf -= 11; /* go back to A2 descriptor */
  518. /* For A2, 7, 8, 9, 10 - zero, Pmin, Psec, Pframe of Lead out */
  519. store_cdrom_address(&buf[7], msf, curlun->num_sectors);
  520. break;
  521. default:
  522. /* Multi-session, PMA, ATIP, CD-TEXT not supported/required */
  523. len = -EINVAL;
  524. break;
  525. }
  526. return len;
  527. }
  528. /*-------------------------------------------------------------------------*/
  529. static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
  530. char *buf)
  531. {
  532. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  533. return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
  534. ? curlun->ro
  535. : curlun->initially_ro);
  536. }
  537. static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
  538. char *buf)
  539. {
  540. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  541. return sprintf(buf, "%u\n", curlun->nofua);
  542. }
  543. static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
  544. char *buf)
  545. {
  546. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  547. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  548. char *p;
  549. ssize_t rc;
  550. down_read(filesem);
  551. if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
  552. p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
  553. if (IS_ERR(p))
  554. rc = PTR_ERR(p);
  555. else {
  556. rc = strlen(p);
  557. memmove(buf, p, rc);
  558. buf[rc] = '\n'; /* Add a newline */
  559. buf[++rc] = 0;
  560. }
  561. } else { /* No file, return 0 bytes */
  562. *buf = 0;
  563. rc = 0;
  564. }
  565. up_read(filesem);
  566. return rc;
  567. }
  568. static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
  569. const char *buf, size_t count)
  570. {
  571. ssize_t rc;
  572. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  573. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  574. unsigned ro;
  575. rc = kstrtouint(buf, 2, &ro);
  576. if (rc)
  577. return rc;
  578. /*
  579. * Allow the write-enable status to change only while the
  580. * backing file is closed.
  581. */
  582. down_read(filesem);
  583. if (fsg_lun_is_open(curlun)) {
  584. LDBG(curlun, "read-only status change prevented\n");
  585. rc = -EBUSY;
  586. } else {
  587. curlun->ro = ro;
  588. curlun->initially_ro = ro;
  589. LDBG(curlun, "read-only status set to %d\n", curlun->ro);
  590. rc = count;
  591. }
  592. up_read(filesem);
  593. return rc;
  594. }
  595. static ssize_t fsg_store_nofua(struct device *dev,
  596. struct device_attribute *attr,
  597. const char *buf, size_t count)
  598. {
  599. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  600. unsigned nofua;
  601. int ret;
  602. ret = kstrtouint(buf, 2, &nofua);
  603. if (ret)
  604. return ret;
  605. /* Sync data when switching from async mode to sync */
  606. if (!nofua && curlun->nofua)
  607. fsg_lun_fsync_sub(curlun);
  608. curlun->nofua = nofua;
  609. return count;
  610. }
  611. static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
  612. const char *buf, size_t count)
  613. {
  614. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  615. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  616. int rc = 0;
  617. #if !defined(CONFIG_USB_G_ANDROID)
  618. /* disabled in android because we need to allow closing the backing file
  619. * if the media was removed
  620. */
  621. if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
  622. LDBG(curlun, "eject attempt prevented\n");
  623. return -EBUSY; /* "Door is locked" */
  624. }
  625. #endif
  626. printk("fsg_store_file file=%s, count=%d, curlun->cdrom=%d\n", buf, (int)count, curlun->cdrom);
  627. /*
  628. * WORKAROUND:VOLD would clean the file path after switching to bicr.
  629. * So when the lun is being a CD-ROM a.k.a. BICR. Dont clean the file path to empty.
  630. */
  631. if (curlun->cdrom == 1 && count == 1)
  632. return count;
  633. /*
  634. * WORKAROUND:Should be closed the fsg lun for virtual cd-rom, when switch to
  635. * other usb functions. Use the special keyword "off", because the init can
  636. * not parse the char '\n' in rc file and write into the sysfs.
  637. */
  638. if (count == 3 &&
  639. buf[0] == 'o' && buf[1] == 'f' && buf[2] == 'f' &&
  640. fsg_lun_is_open(curlun)) {
  641. ((char *) buf)[0] = 0;
  642. }
  643. /* Remove a trailing newline */
  644. if (count > 0 && buf[count-1] == '\n')
  645. ((char *) buf)[count-1] = 0; /* Ugh! */
  646. /* Load new medium */
  647. down_write(filesem);
  648. if (count > 0 && buf[0]) {
  649. /* fsg_lun_open() will close existing file if any. */
  650. rc = fsg_lun_open(curlun, buf);
  651. if (rc == 0)
  652. curlun->unit_attention_data =
  653. SS_NOT_READY_TO_READY_TRANSITION;
  654. } else if (fsg_lun_is_open(curlun)) {
  655. fsg_lun_close(curlun);
  656. curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
  657. }
  658. up_write(filesem);
  659. return (rc < 0 ? rc : count);
  660. }