storage_common.c 19 KB

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