storage_common.c 19 KB

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