storage_common.c 21 KB

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