storage_common.c 21 KB

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