storage_common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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/module.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/file.h>
  26. #include <linux/fs.h>
  27. #include <linux/usb/composite.h>
  28. #include "storage_common.h"
  29. /* There is only one interface. */
  30. struct usb_interface_descriptor fsg_intf_desc = {
  31. .bLength = sizeof fsg_intf_desc,
  32. .bDescriptorType = USB_DT_INTERFACE,
  33. .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
  34. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  35. .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
  36. .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
  37. .iInterface = FSG_STRING_INTERFACE,
  38. };
  39. EXPORT_SYMBOL_GPL(fsg_intf_desc);
  40. /*
  41. * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
  42. * interrupt-in.
  43. */
  44. struct usb_endpoint_descriptor fsg_fs_bulk_in_desc = {
  45. .bLength = USB_DT_ENDPOINT_SIZE,
  46. .bDescriptorType = USB_DT_ENDPOINT,
  47. .bEndpointAddress = USB_DIR_IN,
  48. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  49. /* wMaxPacketSize set by autoconfiguration */
  50. };
  51. EXPORT_SYMBOL_GPL(fsg_fs_bulk_in_desc);
  52. struct usb_endpoint_descriptor fsg_fs_bulk_out_desc = {
  53. .bLength = USB_DT_ENDPOINT_SIZE,
  54. .bDescriptorType = USB_DT_ENDPOINT,
  55. .bEndpointAddress = USB_DIR_OUT,
  56. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  57. /* wMaxPacketSize set by autoconfiguration */
  58. };
  59. EXPORT_SYMBOL_GPL(fsg_fs_bulk_out_desc);
  60. struct usb_descriptor_header *fsg_fs_function[] = {
  61. (struct usb_descriptor_header *) &fsg_intf_desc,
  62. (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
  63. (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
  64. NULL,
  65. };
  66. EXPORT_SYMBOL_GPL(fsg_fs_function);
  67. /*
  68. * USB 2.0 devices need to expose both high speed and full speed
  69. * descriptors, unless they only run at full speed.
  70. *
  71. * That means alternate endpoint descriptors (bigger packets)
  72. * and a "device qualifier" ... plus more construction options
  73. * for the configuration descriptor.
  74. */
  75. struct usb_endpoint_descriptor fsg_hs_bulk_in_desc = {
  76. .bLength = USB_DT_ENDPOINT_SIZE,
  77. .bDescriptorType = USB_DT_ENDPOINT,
  78. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  79. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  80. .wMaxPacketSize = cpu_to_le16(512),
  81. };
  82. EXPORT_SYMBOL_GPL(fsg_hs_bulk_in_desc);
  83. struct usb_endpoint_descriptor fsg_hs_bulk_out_desc = {
  84. .bLength = USB_DT_ENDPOINT_SIZE,
  85. .bDescriptorType = USB_DT_ENDPOINT,
  86. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  87. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  88. .wMaxPacketSize = cpu_to_le16(512),
  89. .bInterval = 1, /* NAK every 1 uframe */
  90. };
  91. EXPORT_SYMBOL_GPL(fsg_hs_bulk_out_desc);
  92. struct usb_descriptor_header *fsg_hs_function[] = {
  93. (struct usb_descriptor_header *) &fsg_intf_desc,
  94. (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
  95. (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
  96. NULL,
  97. };
  98. EXPORT_SYMBOL_GPL(fsg_hs_function);
  99. struct usb_endpoint_descriptor fsg_ss_bulk_in_desc = {
  100. .bLength = USB_DT_ENDPOINT_SIZE,
  101. .bDescriptorType = USB_DT_ENDPOINT,
  102. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  103. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  104. .wMaxPacketSize = cpu_to_le16(1024),
  105. };
  106. EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_desc);
  107. struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
  108. .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
  109. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  110. /*.bMaxBurst = DYNAMIC, */
  111. };
  112. EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_comp_desc);
  113. struct usb_endpoint_descriptor fsg_ss_bulk_out_desc = {
  114. .bLength = USB_DT_ENDPOINT_SIZE,
  115. .bDescriptorType = USB_DT_ENDPOINT,
  116. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  117. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  118. .wMaxPacketSize = cpu_to_le16(1024),
  119. };
  120. EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_desc);
  121. struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
  122. .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
  123. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  124. /*.bMaxBurst = DYNAMIC, */
  125. };
  126. EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_comp_desc);
  127. struct usb_descriptor_header *fsg_ss_function[] = {
  128. (struct usb_descriptor_header *) &fsg_intf_desc,
  129. (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
  130. (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
  131. (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
  132. (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
  133. NULL,
  134. };
  135. EXPORT_SYMBOL_GPL(fsg_ss_function);
  136. /*-------------------------------------------------------------------------*/
  137. /*
  138. * If the next two routines are called while the gadget is registered,
  139. * the caller must own fsg->filesem for writing.
  140. */
  141. void fsg_lun_close(struct fsg_lun *curlun)
  142. {
  143. if (curlun->filp) {
  144. LDBG(curlun, "close backing file\n");
  145. fput(curlun->filp);
  146. curlun->filp = NULL;
  147. }
  148. }
  149. EXPORT_SYMBOL_GPL(fsg_lun_close);
  150. int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
  151. {
  152. int ro;
  153. struct file *filp = NULL;
  154. int rc = -EINVAL;
  155. struct inode *inode = NULL;
  156. loff_t size;
  157. loff_t num_sectors;
  158. loff_t min_sectors;
  159. unsigned int blkbits;
  160. unsigned int blksize;
  161. /* R/W if we can, R/O if we must */
  162. ro = curlun->initially_ro;
  163. if (!ro) {
  164. filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
  165. if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
  166. ro = 1;
  167. }
  168. if (ro)
  169. filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
  170. if (IS_ERR(filp)) {
  171. LINFO(curlun, "unable to open backing file: %s\n", filename);
  172. return PTR_ERR(filp);
  173. }
  174. if (!(filp->f_mode & FMODE_WRITE))
  175. ro = 1;
  176. inode = file_inode(filp);
  177. if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
  178. LINFO(curlun, "invalid file type: %s\n", filename);
  179. goto out;
  180. }
  181. /*
  182. * If we can't read the file, it's no good.
  183. * If we can't write the file, use it read-only.
  184. */
  185. if (!(filp->f_mode & FMODE_CAN_READ)) {
  186. LINFO(curlun, "file not readable: %s\n", filename);
  187. goto out;
  188. }
  189. if (!(filp->f_mode & FMODE_CAN_WRITE))
  190. ro = 1;
  191. size = i_size_read(inode->i_mapping->host);
  192. if (size < 0) {
  193. LINFO(curlun, "unable to find file size: %s\n", filename);
  194. rc = (int) size;
  195. goto out;
  196. }
  197. if (curlun->cdrom) {
  198. blksize = 2048;
  199. blkbits = 11;
  200. } else if (inode->i_bdev) {
  201. blksize = bdev_logical_block_size(inode->i_bdev);
  202. blkbits = blksize_bits(blksize);
  203. } else {
  204. blksize = 512;
  205. blkbits = 9;
  206. }
  207. num_sectors = size >> blkbits; /* File size in logic-block-size blocks */
  208. min_sectors = 1;
  209. if (curlun->cdrom) {
  210. min_sectors = 300; /* Smallest track is 300 frames */
  211. if (num_sectors >= 256*60*75) {
  212. num_sectors = 256*60*75 - 1;
  213. LINFO(curlun, "file too big: %s\n", filename);
  214. LINFO(curlun, "using only first %d blocks\n",
  215. (int) num_sectors);
  216. }
  217. }
  218. if (num_sectors < min_sectors) {
  219. LINFO(curlun, "file too small: %s\n", filename);
  220. rc = -ETOOSMALL;
  221. goto out;
  222. }
  223. if (fsg_lun_is_open(curlun))
  224. fsg_lun_close(curlun);
  225. curlun->blksize = blksize;
  226. curlun->blkbits = blkbits;
  227. curlun->ro = ro;
  228. curlun->filp = filp;
  229. curlun->file_length = size;
  230. curlun->num_sectors = num_sectors;
  231. LDBG(curlun, "open backing file: %s\n", filename);
  232. return 0;
  233. out:
  234. fput(filp);
  235. return rc;
  236. }
  237. EXPORT_SYMBOL_GPL(fsg_lun_open);
  238. /*-------------------------------------------------------------------------*/
  239. /*
  240. * Sync the file data, don't bother with the metadata.
  241. * This code was copied from fs/buffer.c:sys_fdatasync().
  242. */
  243. int fsg_lun_fsync_sub(struct fsg_lun *curlun)
  244. {
  245. struct file *filp = curlun->filp;
  246. if (curlun->ro || !filp)
  247. return 0;
  248. return vfs_fsync(filp, 1);
  249. }
  250. EXPORT_SYMBOL_GPL(fsg_lun_fsync_sub);
  251. void store_cdrom_address(u8 *dest, int msf, u32 addr)
  252. {
  253. if (msf) {
  254. /* Convert to Minutes-Seconds-Frames */
  255. addr >>= 2; /* Convert to 2048-byte frames */
  256. addr += 2*75; /* Lead-in occupies 2 seconds */
  257. dest[3] = addr % 75; /* Frames */
  258. addr /= 75;
  259. dest[2] = addr % 60; /* Seconds */
  260. addr /= 60;
  261. dest[1] = addr; /* Minutes */
  262. dest[0] = 0; /* Reserved */
  263. } else {
  264. /* Absolute sector */
  265. put_unaligned_be32(addr, dest);
  266. }
  267. }
  268. EXPORT_SYMBOL_GPL(store_cdrom_address);
  269. /*-------------------------------------------------------------------------*/
  270. ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf)
  271. {
  272. return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
  273. ? curlun->ro
  274. : curlun->initially_ro);
  275. }
  276. EXPORT_SYMBOL_GPL(fsg_show_ro);
  277. ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf)
  278. {
  279. return sprintf(buf, "%u\n", curlun->nofua);
  280. }
  281. EXPORT_SYMBOL_GPL(fsg_show_nofua);
  282. ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  283. char *buf)
  284. {
  285. char *p;
  286. ssize_t rc;
  287. down_read(filesem);
  288. if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
  289. p = file_path(curlun->filp, buf, PAGE_SIZE - 1);
  290. if (IS_ERR(p))
  291. rc = PTR_ERR(p);
  292. else {
  293. rc = strlen(p);
  294. memmove(buf, p, rc);
  295. buf[rc] = '\n'; /* Add a newline */
  296. buf[++rc] = 0;
  297. }
  298. } else { /* No file, return 0 bytes */
  299. *buf = 0;
  300. rc = 0;
  301. }
  302. up_read(filesem);
  303. return rc;
  304. }
  305. EXPORT_SYMBOL_GPL(fsg_show_file);
  306. ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf)
  307. {
  308. return sprintf(buf, "%u\n", curlun->cdrom);
  309. }
  310. EXPORT_SYMBOL_GPL(fsg_show_cdrom);
  311. ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf)
  312. {
  313. return sprintf(buf, "%u\n", curlun->removable);
  314. }
  315. EXPORT_SYMBOL_GPL(fsg_show_removable);
  316. /*
  317. * The caller must hold fsg->filesem for reading when calling this function.
  318. */
  319. static ssize_t _fsg_store_ro(struct fsg_lun *curlun, bool ro)
  320. {
  321. if (fsg_lun_is_open(curlun)) {
  322. LDBG(curlun, "read-only status change prevented\n");
  323. return -EBUSY;
  324. }
  325. curlun->ro = ro;
  326. curlun->initially_ro = ro;
  327. LDBG(curlun, "read-only status set to %d\n", curlun->ro);
  328. return 0;
  329. }
  330. ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  331. const char *buf, size_t count)
  332. {
  333. ssize_t rc;
  334. bool ro;
  335. rc = strtobool(buf, &ro);
  336. if (rc)
  337. return rc;
  338. /*
  339. * Allow the write-enable status to change only while the
  340. * backing file is closed.
  341. */
  342. down_read(filesem);
  343. rc = _fsg_store_ro(curlun, ro);
  344. if (!rc)
  345. rc = count;
  346. up_read(filesem);
  347. return rc;
  348. }
  349. EXPORT_SYMBOL_GPL(fsg_store_ro);
  350. ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count)
  351. {
  352. bool nofua;
  353. int ret;
  354. ret = strtobool(buf, &nofua);
  355. if (ret)
  356. return ret;
  357. /* Sync data when switching from async mode to sync */
  358. if (!nofua && curlun->nofua)
  359. fsg_lun_fsync_sub(curlun);
  360. curlun->nofua = nofua;
  361. return count;
  362. }
  363. EXPORT_SYMBOL_GPL(fsg_store_nofua);
  364. ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  365. const char *buf, size_t count)
  366. {
  367. int rc = 0;
  368. if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
  369. LDBG(curlun, "eject attempt prevented\n");
  370. return -EBUSY; /* "Door is locked" */
  371. }
  372. /* Remove a trailing newline */
  373. if (count > 0 && buf[count-1] == '\n')
  374. ((char *) buf)[count-1] = 0; /* Ugh! */
  375. /* Load new medium */
  376. down_write(filesem);
  377. if (count > 0 && buf[0]) {
  378. /* fsg_lun_open() will close existing file if any. */
  379. rc = fsg_lun_open(curlun, buf);
  380. if (rc == 0)
  381. curlun->unit_attention_data =
  382. SS_NOT_READY_TO_READY_TRANSITION;
  383. } else if (fsg_lun_is_open(curlun)) {
  384. fsg_lun_close(curlun);
  385. curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
  386. }
  387. up_write(filesem);
  388. return (rc < 0 ? rc : count);
  389. }
  390. EXPORT_SYMBOL_GPL(fsg_store_file);
  391. ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  392. const char *buf, size_t count)
  393. {
  394. bool cdrom;
  395. int ret;
  396. ret = strtobool(buf, &cdrom);
  397. if (ret)
  398. return ret;
  399. down_read(filesem);
  400. ret = cdrom ? _fsg_store_ro(curlun, true) : 0;
  401. if (!ret) {
  402. curlun->cdrom = cdrom;
  403. ret = count;
  404. }
  405. up_read(filesem);
  406. return ret;
  407. }
  408. EXPORT_SYMBOL_GPL(fsg_store_cdrom);
  409. ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
  410. size_t count)
  411. {
  412. bool removable;
  413. int ret;
  414. ret = strtobool(buf, &removable);
  415. if (ret)
  416. return ret;
  417. curlun->removable = removable;
  418. return count;
  419. }
  420. EXPORT_SYMBOL_GPL(fsg_store_removable);
  421. MODULE_LICENSE("GPL");