storage_common.c 21 KB

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