storage_common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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 (m.nazarewicz@samsung.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_manufacturer -- name of the manufacturer
  17. * - fsg_string_product -- name of the product
  18. * - fsg_string_config -- name of the configuration
  19. * - fsg_string_interface -- name of the interface
  20. * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
  21. * macro is defined prior to including this file.
  22. */
  23. /*
  24. * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
  25. * fsg_hs_intr_in_desc objects as well as
  26. * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
  27. * macros are not defined.
  28. *
  29. * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
  30. * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
  31. * defined (as well as corresponding entries in string tables are
  32. * missing) and FSG_STRING_INTERFACE has value of zero.
  33. *
  34. * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
  35. */
  36. /*
  37. * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
  38. * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
  39. * characters rather then a pointer to void.
  40. */
  41. #include <linux/usb/storage.h>
  42. #include <scsi/scsi.h>
  43. #include <asm/unaligned.h>
  44. /*
  45. * Thanks to NetChip Technologies for donating this product ID.
  46. *
  47. * DO NOT REUSE THESE IDs with any other driver!! Ever!!
  48. * Instead: allocate your own, using normal USB-IF procedures.
  49. */
  50. #define FSG_VENDOR_ID 0x0525 /* NetChip */
  51. #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
  52. /*-------------------------------------------------------------------------*/
  53. #ifndef DEBUG
  54. #undef VERBOSE_DEBUG
  55. #undef DUMP_MSGS
  56. #endif /* !DEBUG */
  57. #ifdef VERBOSE_DEBUG
  58. #define VLDBG LDBG
  59. #else
  60. #define VLDBG(lun, fmt, args...) do { } while (0)
  61. #endif /* VERBOSE_DEBUG */
  62. #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
  63. #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
  64. #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
  65. #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
  66. /*
  67. * Keep those macros in sync with those in
  68. * include/linux/usb/composite.h or else GCC will complain. If they
  69. * are identical (the same names of arguments, white spaces in the
  70. * same places) GCC will allow redefinition otherwise (even if some
  71. * white space is removed or added) warning will be issued.
  72. *
  73. * Those macros are needed here because File Storage Gadget does not
  74. * include the composite.h header. For composite gadgets those macros
  75. * are redundant since composite.h is included any way.
  76. *
  77. * One could check whether those macros are already defined (which
  78. * would indicate composite.h had been included) or not (which would
  79. * indicate we were in FSG) but this is not done because a warning is
  80. * desired if definitions here differ from the ones in composite.h.
  81. *
  82. * We want the definitions to match and be the same in File Storage
  83. * Gadget as well as Mass Storage Function (and so composite gadgets
  84. * using MSF). If someone changes them in composite.h it will produce
  85. * a warning in this file when building MSF.
  86. */
  87. #define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
  88. #define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
  89. #define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
  90. #define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
  91. #define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
  92. #ifdef DUMP_MSGS
  93. # define dump_msg(fsg, /* const char * */ label, \
  94. /* const u8 * */ buf, /* unsigned */ length) do { \
  95. if (length < 512) { \
  96. DBG(fsg, "%s, length %u:\n", label, length); \
  97. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
  98. 16, 1, buf, length, 0); \
  99. } \
  100. } while (0)
  101. # define dump_cdb(fsg) do { } while (0)
  102. #else
  103. # define dump_msg(fsg, /* const char * */ label, \
  104. /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
  105. # ifdef VERBOSE_DEBUG
  106. # define dump_cdb(fsg) \
  107. print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
  108. 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
  109. # else
  110. # define dump_cdb(fsg) do { } while (0)
  111. # endif /* VERBOSE_DEBUG */
  112. #endif /* DUMP_MSGS */
  113. /*-------------------------------------------------------------------------*/
  114. /* Bulk-only data structures */
  115. /* Command Block Wrapper */
  116. struct fsg_bulk_cb_wrap {
  117. __le32 Signature; /* Contains 'USBC' */
  118. u32 Tag; /* Unique per command id */
  119. __le32 DataTransferLength; /* Size of the data */
  120. u8 Flags; /* Direction in bit 7 */
  121. u8 Lun; /* LUN (normally 0) */
  122. u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
  123. u8 CDB[16]; /* Command Data Block */
  124. };
  125. #define USB_BULK_CB_WRAP_LEN 31
  126. #define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
  127. #define USB_BULK_IN_FLAG 0x80
  128. /* Command Status Wrapper */
  129. struct bulk_cs_wrap {
  130. __le32 Signature; /* Should = 'USBS' */
  131. u32 Tag; /* Same as original command */
  132. __le32 Residue; /* Amount not transferred */
  133. u8 Status; /* See below */
  134. };
  135. #define USB_BULK_CS_WRAP_LEN 13
  136. #define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
  137. #define USB_STATUS_PASS 0
  138. #define USB_STATUS_FAIL 1
  139. #define USB_STATUS_PHASE_ERROR 2
  140. /* Bulk-only class specific requests */
  141. #define USB_BULK_RESET_REQUEST 0xff
  142. #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
  143. /* CBI Interrupt data structure */
  144. struct interrupt_data {
  145. u8 bType;
  146. u8 bValue;
  147. };
  148. #define CBI_INTERRUPT_DATA_LEN 2
  149. /* CBI Accept Device-Specific Command request */
  150. #define USB_CBI_ADSC_REQUEST 0x00
  151. /* Length of a SCSI Command Data Block */
  152. #define MAX_COMMAND_SIZE 16
  153. /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
  154. #define SS_NO_SENSE 0
  155. #define SS_COMMUNICATION_FAILURE 0x040800
  156. #define SS_INVALID_COMMAND 0x052000
  157. #define SS_INVALID_FIELD_IN_CDB 0x052400
  158. #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
  159. #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
  160. #define SS_MEDIUM_NOT_PRESENT 0x023a00
  161. #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
  162. #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
  163. #define SS_RESET_OCCURRED 0x062900
  164. #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
  165. #define SS_UNRECOVERED_READ_ERROR 0x031100
  166. #define SS_WRITE_ERROR 0x030c02
  167. #define SS_WRITE_PROTECTED 0x072700
  168. #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
  169. #define ASC(x) ((u8) ((x) >> 8))
  170. #define ASCQ(x) ((u8) (x))
  171. /*-------------------------------------------------------------------------*/
  172. struct fsg_lun {
  173. struct file *filp;
  174. loff_t file_length;
  175. loff_t num_sectors;
  176. unsigned int initially_ro:1;
  177. unsigned int ro:1;
  178. unsigned int removable:1;
  179. unsigned int cdrom:1;
  180. unsigned int prevent_medium_removal:1;
  181. unsigned int registered:1;
  182. unsigned int info_valid:1;
  183. unsigned int nofua:1;
  184. u32 sense_data;
  185. u32 sense_data_info;
  186. u32 unit_attention_data;
  187. struct device dev;
  188. };
  189. #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
  190. static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
  191. {
  192. return container_of(dev, struct fsg_lun, dev);
  193. }
  194. /* Big enough to hold our biggest descriptor */
  195. #define EP0_BUFSIZE 256
  196. #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
  197. /* Number of buffers we will use. 2 is enough for double-buffering */
  198. #define FSG_NUM_BUFFERS 2
  199. /* Default size of buffer length. */
  200. #define FSG_BUFLEN ((u32)16384)
  201. /* Maximal number of LUNs supported in mass storage function */
  202. #define FSG_MAX_LUNS 8
  203. enum fsg_buffer_state {
  204. BUF_STATE_EMPTY = 0,
  205. BUF_STATE_FULL,
  206. BUF_STATE_BUSY
  207. };
  208. struct fsg_buffhd {
  209. #ifdef FSG_BUFFHD_STATIC_BUFFER
  210. char buf[FSG_BUFLEN];
  211. #else
  212. void *buf;
  213. #endif
  214. enum fsg_buffer_state state;
  215. struct fsg_buffhd *next;
  216. /*
  217. * The NetChip 2280 is faster, and handles some protocol faults
  218. * better, if we don't submit any short bulk-out read requests.
  219. * So we will record the intended request length here.
  220. */
  221. unsigned int bulk_out_intended_length;
  222. struct usb_request *inreq;
  223. int inreq_busy;
  224. struct usb_request *outreq;
  225. int outreq_busy;
  226. };
  227. enum fsg_state {
  228. /* This one isn't used anywhere */
  229. FSG_STATE_COMMAND_PHASE = -10,
  230. FSG_STATE_DATA_PHASE,
  231. FSG_STATE_STATUS_PHASE,
  232. FSG_STATE_IDLE = 0,
  233. FSG_STATE_ABORT_BULK_OUT,
  234. FSG_STATE_RESET,
  235. FSG_STATE_INTERFACE_CHANGE,
  236. FSG_STATE_CONFIG_CHANGE,
  237. FSG_STATE_DISCONNECT,
  238. FSG_STATE_EXIT,
  239. FSG_STATE_TERMINATED
  240. };
  241. enum data_direction {
  242. DATA_DIR_UNKNOWN = 0,
  243. DATA_DIR_FROM_HOST,
  244. DATA_DIR_TO_HOST,
  245. DATA_DIR_NONE
  246. };
  247. /*-------------------------------------------------------------------------*/
  248. static inline u32 get_unaligned_be24(u8 *buf)
  249. {
  250. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  251. }
  252. /*-------------------------------------------------------------------------*/
  253. enum {
  254. #ifndef FSG_NO_DEVICE_STRINGS
  255. FSG_STRING_MANUFACTURER = 1,
  256. FSG_STRING_PRODUCT,
  257. FSG_STRING_SERIAL,
  258. FSG_STRING_CONFIG,
  259. #endif
  260. FSG_STRING_INTERFACE
  261. };
  262. #ifndef FSG_NO_OTG
  263. static struct usb_otg_descriptor
  264. fsg_otg_desc = {
  265. .bLength = sizeof fsg_otg_desc,
  266. .bDescriptorType = USB_DT_OTG,
  267. .bmAttributes = USB_OTG_SRP,
  268. };
  269. #endif
  270. /* There is only one interface. */
  271. static struct usb_interface_descriptor
  272. fsg_intf_desc = {
  273. .bLength = sizeof fsg_intf_desc,
  274. .bDescriptorType = USB_DT_INTERFACE,
  275. .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
  276. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  277. .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
  278. .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
  279. .iInterface = FSG_STRING_INTERFACE,
  280. };
  281. /*
  282. * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
  283. * interrupt-in.
  284. */
  285. static struct usb_endpoint_descriptor
  286. fsg_fs_bulk_in_desc = {
  287. .bLength = USB_DT_ENDPOINT_SIZE,
  288. .bDescriptorType = USB_DT_ENDPOINT,
  289. .bEndpointAddress = USB_DIR_IN,
  290. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  291. /* wMaxPacketSize set by autoconfiguration */
  292. };
  293. static struct usb_endpoint_descriptor
  294. fsg_fs_bulk_out_desc = {
  295. .bLength = USB_DT_ENDPOINT_SIZE,
  296. .bDescriptorType = USB_DT_ENDPOINT,
  297. .bEndpointAddress = USB_DIR_OUT,
  298. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  299. /* wMaxPacketSize set by autoconfiguration */
  300. };
  301. #ifndef FSG_NO_INTR_EP
  302. static struct usb_endpoint_descriptor
  303. fsg_fs_intr_in_desc = {
  304. .bLength = USB_DT_ENDPOINT_SIZE,
  305. .bDescriptorType = USB_DT_ENDPOINT,
  306. .bEndpointAddress = USB_DIR_IN,
  307. .bmAttributes = USB_ENDPOINT_XFER_INT,
  308. .wMaxPacketSize = cpu_to_le16(2),
  309. .bInterval = 32, /* frames -> 32 ms */
  310. };
  311. #ifndef FSG_NO_OTG
  312. # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
  313. #else
  314. # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
  315. #endif
  316. #endif
  317. static struct usb_descriptor_header *fsg_fs_function[] = {
  318. #ifndef FSG_NO_OTG
  319. (struct usb_descriptor_header *) &fsg_otg_desc,
  320. #endif
  321. (struct usb_descriptor_header *) &fsg_intf_desc,
  322. (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
  323. (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
  324. #ifndef FSG_NO_INTR_EP
  325. (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
  326. #endif
  327. NULL,
  328. };
  329. /*
  330. * USB 2.0 devices need to expose both high speed and full speed
  331. * descriptors, unless they only run at full speed.
  332. *
  333. * That means alternate endpoint descriptors (bigger packets)
  334. * and a "device qualifier" ... plus more construction options
  335. * for the configuration descriptor.
  336. */
  337. static struct usb_endpoint_descriptor
  338. fsg_hs_bulk_in_desc = {
  339. .bLength = USB_DT_ENDPOINT_SIZE,
  340. .bDescriptorType = USB_DT_ENDPOINT,
  341. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  342. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  343. .wMaxPacketSize = cpu_to_le16(512),
  344. };
  345. static struct usb_endpoint_descriptor
  346. fsg_hs_bulk_out_desc = {
  347. .bLength = USB_DT_ENDPOINT_SIZE,
  348. .bDescriptorType = USB_DT_ENDPOINT,
  349. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  350. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  351. .wMaxPacketSize = cpu_to_le16(512),
  352. .bInterval = 1, /* NAK every 1 uframe */
  353. };
  354. #ifndef FSG_NO_INTR_EP
  355. static struct usb_endpoint_descriptor
  356. fsg_hs_intr_in_desc = {
  357. .bLength = USB_DT_ENDPOINT_SIZE,
  358. .bDescriptorType = USB_DT_ENDPOINT,
  359. /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
  360. .bmAttributes = USB_ENDPOINT_XFER_INT,
  361. .wMaxPacketSize = cpu_to_le16(2),
  362. .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
  363. };
  364. #ifndef FSG_NO_OTG
  365. # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
  366. #else
  367. # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
  368. #endif
  369. #endif
  370. static struct usb_descriptor_header *fsg_hs_function[] = {
  371. #ifndef FSG_NO_OTG
  372. (struct usb_descriptor_header *) &fsg_otg_desc,
  373. #endif
  374. (struct usb_descriptor_header *) &fsg_intf_desc,
  375. (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
  376. (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
  377. #ifndef FSG_NO_INTR_EP
  378. (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
  379. #endif
  380. NULL,
  381. };
  382. /* Maxpacket and other transfer characteristics vary by speed. */
  383. static __maybe_unused struct usb_endpoint_descriptor *
  384. fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
  385. struct usb_endpoint_descriptor *hs)
  386. {
  387. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  388. return hs;
  389. return fs;
  390. }
  391. /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
  392. static struct usb_string fsg_strings[] = {
  393. #ifndef FSG_NO_DEVICE_STRINGS
  394. {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
  395. {FSG_STRING_PRODUCT, fsg_string_product},
  396. {FSG_STRING_SERIAL, ""},
  397. {FSG_STRING_CONFIG, fsg_string_config},
  398. #endif
  399. {FSG_STRING_INTERFACE, fsg_string_interface},
  400. {}
  401. };
  402. static struct usb_gadget_strings fsg_stringtab = {
  403. .language = 0x0409, /* en-us */
  404. .strings = fsg_strings,
  405. };
  406. /*-------------------------------------------------------------------------*/
  407. /*
  408. * If the next two routines are called while the gadget is registered,
  409. * the caller must own fsg->filesem for writing.
  410. */
  411. static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
  412. {
  413. int ro;
  414. struct file *filp = NULL;
  415. int rc = -EINVAL;
  416. struct inode *inode = NULL;
  417. loff_t size;
  418. loff_t num_sectors;
  419. loff_t min_sectors;
  420. /* R/W if we can, R/O if we must */
  421. ro = curlun->initially_ro;
  422. if (!ro) {
  423. filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
  424. if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
  425. ro = 1;
  426. }
  427. if (ro)
  428. filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
  429. if (IS_ERR(filp)) {
  430. LINFO(curlun, "unable to open backing file: %s\n", filename);
  431. return PTR_ERR(filp);
  432. }
  433. if (!(filp->f_mode & FMODE_WRITE))
  434. ro = 1;
  435. if (filp->f_path.dentry)
  436. inode = filp->f_path.dentry->d_inode;
  437. if (!inode || (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
  438. LINFO(curlun, "invalid file type: %s\n", filename);
  439. goto out;
  440. }
  441. /*
  442. * If we can't read the file, it's no good.
  443. * If we can't write the file, use it read-only.
  444. */
  445. if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
  446. LINFO(curlun, "file not readable: %s\n", filename);
  447. goto out;
  448. }
  449. if (!(filp->f_op->write || filp->f_op->aio_write))
  450. ro = 1;
  451. size = i_size_read(inode->i_mapping->host);
  452. if (size < 0) {
  453. LINFO(curlun, "unable to find file size: %s\n", filename);
  454. rc = (int) size;
  455. goto out;
  456. }
  457. num_sectors = size >> 9; /* File size in 512-byte blocks */
  458. min_sectors = 1;
  459. if (curlun->cdrom) {
  460. num_sectors &= ~3; /* Reduce to a multiple of 2048 */
  461. min_sectors = 300*4; /* Smallest track is 300 frames */
  462. if (num_sectors >= 256*60*75*4) {
  463. num_sectors = (256*60*75 - 1) * 4;
  464. LINFO(curlun, "file too big: %s\n", filename);
  465. LINFO(curlun, "using only first %d blocks\n",
  466. (int) num_sectors);
  467. }
  468. }
  469. if (num_sectors < min_sectors) {
  470. LINFO(curlun, "file too small: %s\n", filename);
  471. rc = -ETOOSMALL;
  472. goto out;
  473. }
  474. get_file(filp);
  475. curlun->ro = ro;
  476. curlun->filp = filp;
  477. curlun->file_length = size;
  478. curlun->num_sectors = num_sectors;
  479. LDBG(curlun, "open backing file: %s\n", filename);
  480. rc = 0;
  481. out:
  482. filp_close(filp, current->files);
  483. return rc;
  484. }
  485. static void fsg_lun_close(struct fsg_lun *curlun)
  486. {
  487. if (curlun->filp) {
  488. LDBG(curlun, "close backing file\n");
  489. fput(curlun->filp);
  490. curlun->filp = NULL;
  491. }
  492. }
  493. /*-------------------------------------------------------------------------*/
  494. /*
  495. * Sync the file data, don't bother with the metadata.
  496. * This code was copied from fs/buffer.c:sys_fdatasync().
  497. */
  498. static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
  499. {
  500. struct file *filp = curlun->filp;
  501. if (curlun->ro || !filp)
  502. return 0;
  503. return vfs_fsync(filp, 1);
  504. }
  505. static void store_cdrom_address(u8 *dest, int msf, u32 addr)
  506. {
  507. if (msf) {
  508. /* Convert to Minutes-Seconds-Frames */
  509. addr >>= 2; /* Convert to 2048-byte frames */
  510. addr += 2*75; /* Lead-in occupies 2 seconds */
  511. dest[3] = addr % 75; /* Frames */
  512. addr /= 75;
  513. dest[2] = addr % 60; /* Seconds */
  514. addr /= 60;
  515. dest[1] = addr; /* Minutes */
  516. dest[0] = 0; /* Reserved */
  517. } else {
  518. /* Absolute sector */
  519. put_unaligned_be32(addr, dest);
  520. }
  521. }
  522. /*-------------------------------------------------------------------------*/
  523. static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
  524. char *buf)
  525. {
  526. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  527. return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
  528. ? curlun->ro
  529. : curlun->initially_ro);
  530. }
  531. static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
  532. char *buf)
  533. {
  534. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  535. return sprintf(buf, "%u\n", curlun->nofua);
  536. }
  537. static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
  538. char *buf)
  539. {
  540. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  541. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  542. char *p;
  543. ssize_t rc;
  544. down_read(filesem);
  545. if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
  546. p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
  547. if (IS_ERR(p))
  548. rc = PTR_ERR(p);
  549. else {
  550. rc = strlen(p);
  551. memmove(buf, p, rc);
  552. buf[rc] = '\n'; /* Add a newline */
  553. buf[++rc] = 0;
  554. }
  555. } else { /* No file, return 0 bytes */
  556. *buf = 0;
  557. rc = 0;
  558. }
  559. up_read(filesem);
  560. return rc;
  561. }
  562. static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
  563. const char *buf, size_t count)
  564. {
  565. ssize_t rc;
  566. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  567. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  568. unsigned ro;
  569. rc = kstrtouint(buf, 2, &ro);
  570. if (rc)
  571. return rc;
  572. /*
  573. * Allow the write-enable status to change only while the
  574. * backing file is closed.
  575. */
  576. down_read(filesem);
  577. if (fsg_lun_is_open(curlun)) {
  578. LDBG(curlun, "read-only status change prevented\n");
  579. rc = -EBUSY;
  580. } else {
  581. curlun->ro = ro;
  582. curlun->initially_ro = ro;
  583. LDBG(curlun, "read-only status set to %d\n", curlun->ro);
  584. rc = count;
  585. }
  586. up_read(filesem);
  587. return rc;
  588. }
  589. static ssize_t fsg_store_nofua(struct device *dev,
  590. struct device_attribute *attr,
  591. const char *buf, size_t count)
  592. {
  593. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  594. unsigned nofua;
  595. int ret;
  596. ret = kstrtouint(buf, 2, &nofua);
  597. if (ret)
  598. return ret;
  599. /* Sync data when switching from async mode to sync */
  600. if (!nofua && curlun->nofua)
  601. fsg_lun_fsync_sub(curlun);
  602. curlun->nofua = nofua;
  603. return count;
  604. }
  605. static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
  606. const char *buf, size_t count)
  607. {
  608. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  609. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  610. int rc = 0;
  611. if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
  612. LDBG(curlun, "eject attempt prevented\n");
  613. return -EBUSY; /* "Door is locked" */
  614. }
  615. /* Remove a trailing newline */
  616. if (count > 0 && buf[count-1] == '\n')
  617. ((char *) buf)[count-1] = 0; /* Ugh! */
  618. /* Eject current medium */
  619. down_write(filesem);
  620. if (fsg_lun_is_open(curlun)) {
  621. fsg_lun_close(curlun);
  622. curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
  623. }
  624. /* Load new medium */
  625. if (count > 0 && buf[0]) {
  626. rc = fsg_lun_open(curlun, buf);
  627. if (rc == 0)
  628. curlun->unit_attention_data =
  629. SS_NOT_READY_TO_READY_TRANSITION;
  630. }
  631. up_write(filesem);
  632. return (rc < 0 ? rc : count);
  633. }