storage_common.c 13 KB

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