storage_common.c 19 KB

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