storage_common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. };
  110. static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
  111. {
  112. return curlun->filp != NULL;
  113. }
  114. static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
  115. {
  116. return container_of(dev, struct fsg_lun, dev);
  117. }
  118. /* Big enough to hold our biggest descriptor */
  119. #define EP0_BUFSIZE 256
  120. #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
  121. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  122. static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
  123. module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);
  124. MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers");
  125. #else
  126. /*
  127. * Number of buffers we will use.
  128. * 2 is usually enough for good buffering pipeline
  129. */
  130. #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
  131. #endif /* CONFIG_USB_DEBUG */
  132. /* check if fsg_num_buffers is within a valid range */
  133. static inline int fsg_num_buffers_validate(void)
  134. {
  135. if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4)
  136. return 0;
  137. pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
  138. fsg_num_buffers, 2 ,4);
  139. return -EINVAL;
  140. }
  141. /* Default size of buffer length. */
  142. #define FSG_BUFLEN ((u32)16384)
  143. /* Maximal number of LUNs supported in mass storage function */
  144. #define FSG_MAX_LUNS 8
  145. enum fsg_buffer_state {
  146. BUF_STATE_EMPTY = 0,
  147. BUF_STATE_FULL,
  148. BUF_STATE_BUSY
  149. };
  150. struct fsg_buffhd {
  151. void *buf;
  152. enum fsg_buffer_state state;
  153. struct fsg_buffhd *next;
  154. /*
  155. * The NetChip 2280 is faster, and handles some protocol faults
  156. * better, if we don't submit any short bulk-out read requests.
  157. * So we will record the intended request length here.
  158. */
  159. unsigned int bulk_out_intended_length;
  160. struct usb_request *inreq;
  161. int inreq_busy;
  162. struct usb_request *outreq;
  163. int outreq_busy;
  164. };
  165. enum fsg_state {
  166. /* This one isn't used anywhere */
  167. FSG_STATE_COMMAND_PHASE = -10,
  168. FSG_STATE_DATA_PHASE,
  169. FSG_STATE_STATUS_PHASE,
  170. FSG_STATE_IDLE = 0,
  171. FSG_STATE_ABORT_BULK_OUT,
  172. FSG_STATE_RESET,
  173. FSG_STATE_INTERFACE_CHANGE,
  174. FSG_STATE_CONFIG_CHANGE,
  175. FSG_STATE_DISCONNECT,
  176. FSG_STATE_EXIT,
  177. FSG_STATE_TERMINATED
  178. };
  179. enum data_direction {
  180. DATA_DIR_UNKNOWN = 0,
  181. DATA_DIR_FROM_HOST,
  182. DATA_DIR_TO_HOST,
  183. DATA_DIR_NONE
  184. };
  185. /*-------------------------------------------------------------------------*/
  186. static inline u32 get_unaligned_be24(u8 *buf)
  187. {
  188. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  189. }
  190. /*-------------------------------------------------------------------------*/
  191. enum {
  192. FSG_STRING_INTERFACE
  193. };
  194. /* There is only one interface. */
  195. static struct usb_interface_descriptor
  196. fsg_intf_desc = {
  197. .bLength = sizeof fsg_intf_desc,
  198. .bDescriptorType = USB_DT_INTERFACE,
  199. .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
  200. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  201. .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
  202. .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
  203. .iInterface = FSG_STRING_INTERFACE,
  204. };
  205. /*
  206. * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
  207. * interrupt-in.
  208. */
  209. static struct usb_endpoint_descriptor
  210. fsg_fs_bulk_in_desc = {
  211. .bLength = USB_DT_ENDPOINT_SIZE,
  212. .bDescriptorType = USB_DT_ENDPOINT,
  213. .bEndpointAddress = USB_DIR_IN,
  214. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  215. /* wMaxPacketSize set by autoconfiguration */
  216. };
  217. static struct usb_endpoint_descriptor
  218. fsg_fs_bulk_out_desc = {
  219. .bLength = USB_DT_ENDPOINT_SIZE,
  220. .bDescriptorType = USB_DT_ENDPOINT,
  221. .bEndpointAddress = USB_DIR_OUT,
  222. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  223. /* wMaxPacketSize set by autoconfiguration */
  224. };
  225. static struct usb_descriptor_header *fsg_fs_function[] = {
  226. (struct usb_descriptor_header *) &fsg_intf_desc,
  227. (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
  228. (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
  229. NULL,
  230. };
  231. /*
  232. * USB 2.0 devices need to expose both high speed and full speed
  233. * descriptors, unless they only run at full speed.
  234. *
  235. * That means alternate endpoint descriptors (bigger packets)
  236. * and a "device qualifier" ... plus more construction options
  237. * for the configuration descriptor.
  238. */
  239. static struct usb_endpoint_descriptor
  240. fsg_hs_bulk_in_desc = {
  241. .bLength = USB_DT_ENDPOINT_SIZE,
  242. .bDescriptorType = USB_DT_ENDPOINT,
  243. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  244. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  245. .wMaxPacketSize = cpu_to_le16(512),
  246. };
  247. static struct usb_endpoint_descriptor
  248. fsg_hs_bulk_out_desc = {
  249. .bLength = USB_DT_ENDPOINT_SIZE,
  250. .bDescriptorType = USB_DT_ENDPOINT,
  251. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  252. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  253. .wMaxPacketSize = cpu_to_le16(512),
  254. .bInterval = 1, /* NAK every 1 uframe */
  255. };
  256. static struct usb_descriptor_header *fsg_hs_function[] = {
  257. (struct usb_descriptor_header *) &fsg_intf_desc,
  258. (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
  259. (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
  260. NULL,
  261. };
  262. static struct usb_endpoint_descriptor
  263. fsg_ss_bulk_in_desc = {
  264. .bLength = USB_DT_ENDPOINT_SIZE,
  265. .bDescriptorType = USB_DT_ENDPOINT,
  266. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  267. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  268. .wMaxPacketSize = cpu_to_le16(1024),
  269. };
  270. static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
  271. .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
  272. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  273. /*.bMaxBurst = DYNAMIC, */
  274. };
  275. static struct usb_endpoint_descriptor
  276. fsg_ss_bulk_out_desc = {
  277. .bLength = USB_DT_ENDPOINT_SIZE,
  278. .bDescriptorType = USB_DT_ENDPOINT,
  279. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  280. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  281. .wMaxPacketSize = cpu_to_le16(1024),
  282. };
  283. static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
  284. .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
  285. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  286. /*.bMaxBurst = DYNAMIC, */
  287. };
  288. static struct usb_descriptor_header *fsg_ss_function[] = {
  289. (struct usb_descriptor_header *) &fsg_intf_desc,
  290. (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
  291. (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
  292. (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
  293. (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
  294. NULL,
  295. };
  296. /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
  297. static struct usb_string fsg_strings[] = {
  298. {FSG_STRING_INTERFACE, fsg_string_interface},
  299. {}
  300. };
  301. static struct usb_gadget_strings fsg_stringtab = {
  302. .language = 0x0409, /* en-us */
  303. .strings = fsg_strings,
  304. };
  305. /*-------------------------------------------------------------------------*/
  306. /*
  307. * If the next two routines are called while the gadget is registered,
  308. * the caller must own fsg->filesem for writing.
  309. */
  310. static void fsg_lun_close(struct fsg_lun *curlun)
  311. {
  312. if (curlun->filp) {
  313. LDBG(curlun, "close backing file\n");
  314. fput(curlun->filp);
  315. curlun->filp = NULL;
  316. }
  317. }
  318. static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
  319. {
  320. int ro;
  321. struct file *filp = NULL;
  322. int rc = -EINVAL;
  323. struct inode *inode = NULL;
  324. loff_t size;
  325. loff_t num_sectors;
  326. loff_t min_sectors;
  327. unsigned int blkbits;
  328. unsigned int blksize;
  329. /* R/W if we can, R/O if we must */
  330. ro = curlun->initially_ro;
  331. if (!ro) {
  332. filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
  333. if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
  334. ro = 1;
  335. }
  336. if (ro)
  337. filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
  338. if (IS_ERR(filp)) {
  339. LINFO(curlun, "unable to open backing file: %s\n", filename);
  340. return PTR_ERR(filp);
  341. }
  342. if (!(filp->f_mode & FMODE_WRITE))
  343. ro = 1;
  344. inode = file_inode(filp);
  345. if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
  346. LINFO(curlun, "invalid file type: %s\n", filename);
  347. goto out;
  348. }
  349. /*
  350. * If we can't read the file, it's no good.
  351. * If we can't write the file, use it read-only.
  352. */
  353. if (!(filp->f_op->read || filp->f_op->aio_read)) {
  354. LINFO(curlun, "file not readable: %s\n", filename);
  355. goto out;
  356. }
  357. if (!(filp->f_op->write || filp->f_op->aio_write))
  358. ro = 1;
  359. size = i_size_read(inode->i_mapping->host);
  360. if (size < 0) {
  361. LINFO(curlun, "unable to find file size: %s\n", filename);
  362. rc = (int) size;
  363. goto out;
  364. }
  365. if (curlun->cdrom) {
  366. blksize = 2048;
  367. blkbits = 11;
  368. } else if (inode->i_bdev) {
  369. blksize = bdev_logical_block_size(inode->i_bdev);
  370. blkbits = blksize_bits(blksize);
  371. } else {
  372. blksize = 512;
  373. blkbits = 9;
  374. }
  375. num_sectors = size >> blkbits; /* File size in logic-block-size blocks */
  376. min_sectors = 1;
  377. if (curlun->cdrom) {
  378. min_sectors = 300; /* Smallest track is 300 frames */
  379. if (num_sectors >= 256*60*75) {
  380. num_sectors = 256*60*75 - 1;
  381. LINFO(curlun, "file too big: %s\n", filename);
  382. LINFO(curlun, "using only first %d blocks\n",
  383. (int) num_sectors);
  384. }
  385. }
  386. if (num_sectors < min_sectors) {
  387. LINFO(curlun, "file too small: %s\n", filename);
  388. rc = -ETOOSMALL;
  389. goto out;
  390. }
  391. if (fsg_lun_is_open(curlun))
  392. fsg_lun_close(curlun);
  393. curlun->blksize = blksize;
  394. curlun->blkbits = blkbits;
  395. curlun->ro = ro;
  396. curlun->filp = filp;
  397. curlun->file_length = size;
  398. curlun->num_sectors = num_sectors;
  399. LDBG(curlun, "open backing file: %s\n", filename);
  400. return 0;
  401. out:
  402. fput(filp);
  403. return rc;
  404. }
  405. /*-------------------------------------------------------------------------*/
  406. /*
  407. * Sync the file data, don't bother with the metadata.
  408. * This code was copied from fs/buffer.c:sys_fdatasync().
  409. */
  410. static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
  411. {
  412. struct file *filp = curlun->filp;
  413. if (curlun->ro || !filp)
  414. return 0;
  415. return vfs_fsync(filp, 1);
  416. }
  417. static void store_cdrom_address(u8 *dest, int msf, u32 addr)
  418. {
  419. if (msf) {
  420. /* Convert to Minutes-Seconds-Frames */
  421. addr >>= 2; /* Convert to 2048-byte frames */
  422. addr += 2*75; /* Lead-in occupies 2 seconds */
  423. dest[3] = addr % 75; /* Frames */
  424. addr /= 75;
  425. dest[2] = addr % 60; /* Seconds */
  426. addr /= 60;
  427. dest[1] = addr; /* Minutes */
  428. dest[0] = 0; /* Reserved */
  429. } else {
  430. /* Absolute sector */
  431. put_unaligned_be32(addr, dest);
  432. }
  433. }
  434. /*-------------------------------------------------------------------------*/
  435. static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
  436. char *buf)
  437. {
  438. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  439. return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
  440. ? curlun->ro
  441. : curlun->initially_ro);
  442. }
  443. static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
  444. char *buf)
  445. {
  446. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  447. return sprintf(buf, "%u\n", curlun->nofua);
  448. }
  449. static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
  450. char *buf)
  451. {
  452. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  453. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  454. char *p;
  455. ssize_t rc;
  456. down_read(filesem);
  457. if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
  458. p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
  459. if (IS_ERR(p))
  460. rc = PTR_ERR(p);
  461. else {
  462. rc = strlen(p);
  463. memmove(buf, p, rc);
  464. buf[rc] = '\n'; /* Add a newline */
  465. buf[++rc] = 0;
  466. }
  467. } else { /* No file, return 0 bytes */
  468. *buf = 0;
  469. rc = 0;
  470. }
  471. up_read(filesem);
  472. return rc;
  473. }
  474. static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
  475. const char *buf, size_t count)
  476. {
  477. ssize_t rc;
  478. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  479. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  480. unsigned ro;
  481. rc = kstrtouint(buf, 2, &ro);
  482. if (rc)
  483. return rc;
  484. /*
  485. * Allow the write-enable status to change only while the
  486. * backing file is closed.
  487. */
  488. down_read(filesem);
  489. if (fsg_lun_is_open(curlun)) {
  490. LDBG(curlun, "read-only status change prevented\n");
  491. rc = -EBUSY;
  492. } else {
  493. curlun->ro = ro;
  494. curlun->initially_ro = ro;
  495. LDBG(curlun, "read-only status set to %d\n", curlun->ro);
  496. rc = count;
  497. }
  498. up_read(filesem);
  499. return rc;
  500. }
  501. static ssize_t fsg_store_nofua(struct device *dev,
  502. struct device_attribute *attr,
  503. const char *buf, size_t count)
  504. {
  505. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  506. unsigned nofua;
  507. int ret;
  508. ret = kstrtouint(buf, 2, &nofua);
  509. if (ret)
  510. return ret;
  511. /* Sync data when switching from async mode to sync */
  512. if (!nofua && curlun->nofua)
  513. fsg_lun_fsync_sub(curlun);
  514. curlun->nofua = nofua;
  515. return count;
  516. }
  517. static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
  518. const char *buf, size_t count)
  519. {
  520. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  521. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  522. int rc = 0;
  523. if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
  524. LDBG(curlun, "eject attempt prevented\n");
  525. return -EBUSY; /* "Door is locked" */
  526. }
  527. /* Remove a trailing newline */
  528. if (count > 0 && buf[count-1] == '\n')
  529. ((char *) buf)[count-1] = 0; /* Ugh! */
  530. /* Load new medium */
  531. down_write(filesem);
  532. if (count > 0 && buf[0]) {
  533. /* fsg_lun_open() will close existing file if any. */
  534. rc = fsg_lun_open(curlun, buf);
  535. if (rc == 0)
  536. curlun->unit_attention_data =
  537. SS_NOT_READY_TO_READY_TRANSITION;
  538. } else if (fsg_lun_is_open(curlun)) {
  539. fsg_lun_close(curlun);
  540. curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
  541. }
  542. up_write(filesem);
  543. return (rc < 0 ? rc : count);
  544. }