storage_common.c 21 KB

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