import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / gadget / storage_common.c
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 (mina86@mina86.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14 /*
15 * This file requires the following identifiers used in USB strings to
16 * be defined (each of type pointer to char):
17 * - fsg_string_interface -- name of the interface
18 */
19
20 /*
21 * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
22 * sets the number of pipeline buffers (length of the fsg_buffhd array).
23 * The valid range of num_buffers is: num >= 2 && num <= 4.
24 */
25
26
27 #include <linux/usb/storage.h>
28 #include <scsi/scsi.h>
29 #include <asm/unaligned.h>
30
31
32 /*
33 * Thanks to NetChip Technologies for donating this product ID.
34 *
35 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
36 * Instead: allocate your own, using normal USB-IF procedures.
37 */
38 #define FSG_VENDOR_ID 0x0525 /* NetChip */
39 #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
40
41
42 /*-------------------------------------------------------------------------*/
43
44
45 #ifndef DEBUG
46 #undef VERBOSE_DEBUG
47 #undef DUMP_MSGS
48 #endif /* !DEBUG */
49
50 #ifdef VERBOSE_DEBUG
51 #define VLDBG LDBG
52 #else
53 #define VLDBG(lun, fmt, args...) do { } while (0)
54 #endif /* VERBOSE_DEBUG */
55
56 #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
57 #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
58 #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
59 #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
60
61
62 #ifdef DUMP_MSGS
63
64 # define dump_msg(fsg, /* const char * */ label, \
65 /* const u8 * */ buf, /* unsigned */ length) do { \
66 if (length < 512) { \
67 DBG(fsg, "%s, length %u:\n", label, length); \
68 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
69 16, 1, buf, length, 0); \
70 } \
71 } while (0)
72
73 # define dump_cdb(fsg) do { } while (0)
74
75 #else
76
77 # define dump_msg(fsg, /* const char * */ label, \
78 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
79
80 # ifdef VERBOSE_DEBUG
81
82 # define dump_cdb(fsg) \
83 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
84 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
85
86 # else
87
88 # define dump_cdb(fsg) do { } while (0)
89
90 # endif /* VERBOSE_DEBUG */
91
92 #endif /* DUMP_MSGS */
93
94 /*-------------------------------------------------------------------------*/
95
96 /* Length of a SCSI Command Data Block */
97 #define MAX_COMMAND_SIZE 16
98
99 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
100 #define SS_NO_SENSE 0
101 #define SS_COMMUNICATION_FAILURE 0x040800
102 #define SS_INVALID_COMMAND 0x052000
103 #define SS_INVALID_FIELD_IN_CDB 0x052400
104 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
105 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
106 #define SS_MEDIUM_NOT_PRESENT 0x023a00
107 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
108 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
109 #define SS_RESET_OCCURRED 0x062900
110 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
111 #define SS_UNRECOVERED_READ_ERROR 0x031100
112 #define SS_WRITE_ERROR 0x030c02
113 #define SS_WRITE_PROTECTED 0x072700
114
115 #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
116 #define ASC(x) ((u8) ((x) >> 8))
117 #define ASCQ(x) ((u8) (x))
118
119
120 /*-------------------------------------------------------------------------*/
121
122
123 struct fsg_lun {
124 struct file *filp;
125 loff_t file_length;
126 loff_t num_sectors;
127
128 unsigned int initially_ro:1;
129 unsigned int ro:1;
130 unsigned int removable:1;
131 unsigned int cdrom:1;
132 unsigned int prevent_medium_removal:1;
133 unsigned int registered:1;
134 unsigned int info_valid:1;
135 unsigned int nofua:1;
136
137 u32 sense_data;
138 u32 sense_data_info;
139 u32 unit_attention_data;
140
141 unsigned int blkbits; /* Bits of logical block size of bound block device */
142 unsigned int blksize; /* logical block size of bound block device */
143 struct device dev;
144 #ifdef CONFIG_MTK_ICUSB_SUPPORT
145 char isICUSB;
146 #endif
147 };
148
149 static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
150 {
151 return curlun->filp != NULL;
152 }
153
154 static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
155 {
156 return container_of(dev, struct fsg_lun, dev);
157 }
158
159
160 /* Big enough to hold our biggest descriptor */
161 #define EP0_BUFSIZE 256
162 #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
163
164 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
165
166 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
167 module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);
168 MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers");
169
170 #else
171
172 /*
173 * Number of buffers we will use.
174 * 2 is usually enough for good buffering pipeline
175 */
176 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
177
178 #endif /* CONFIG_USB_DEBUG */
179
180 /* check if fsg_num_buffers is within a valid range */
181 static inline int fsg_num_buffers_validate(void)
182 {
183 if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4)
184 return 0;
185 pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
186 fsg_num_buffers, 2 ,4);
187 return -EINVAL;
188 }
189
190 /* Default size of buffer length. */
191 #define FSG_BUFLEN ((u32)16384)
192
193 /* Maximal number of LUNs supported in mass storage function */
194 #define FSG_MAX_LUNS 8
195
196 enum fsg_buffer_state {
197 BUF_STATE_EMPTY = 0,
198 BUF_STATE_FULL,
199 BUF_STATE_BUSY
200 };
201
202 struct fsg_buffhd {
203 void *buf;
204 enum fsg_buffer_state state;
205 struct fsg_buffhd *next;
206
207 /*
208 * The NetChip 2280 is faster, and handles some protocol faults
209 * better, if we don't submit any short bulk-out read requests.
210 * So we will record the intended request length here.
211 */
212 unsigned int bulk_out_intended_length;
213
214 struct usb_request *inreq;
215 int inreq_busy;
216 struct usb_request *outreq;
217 int outreq_busy;
218 };
219
220 enum fsg_state {
221 /* This one isn't used anywhere */
222 FSG_STATE_COMMAND_PHASE = -10,
223 FSG_STATE_DATA_PHASE,
224 FSG_STATE_STATUS_PHASE,
225
226 FSG_STATE_IDLE = 0,
227 FSG_STATE_ABORT_BULK_OUT,
228 FSG_STATE_RESET,
229 FSG_STATE_INTERFACE_CHANGE,
230 FSG_STATE_CONFIG_CHANGE,
231 FSG_STATE_DISCONNECT,
232 FSG_STATE_EXIT,
233 FSG_STATE_TERMINATED
234 };
235
236 enum data_direction {
237 DATA_DIR_UNKNOWN = 0,
238 DATA_DIR_FROM_HOST,
239 DATA_DIR_TO_HOST,
240 DATA_DIR_NONE
241 };
242
243
244 /*-------------------------------------------------------------------------*/
245
246
247 static inline u32 get_unaligned_be24(u8 *buf)
248 {
249 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
250 }
251
252
253 /*-------------------------------------------------------------------------*/
254
255
256 enum {
257 FSG_STRING_INTERFACE
258 };
259
260
261 #ifdef CONFIG_USBIF_COMPLIANCE
262 static struct usb_otg_descriptor
263 fsg_otg_desc = {
264 .bLength = sizeof fsg_otg_desc,
265 .bDescriptorType = USB_DT_OTG,
266 /* OTG 2.0: */
267 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
268 .bcdOTG = cpu_to_le16(0x200),
269 };
270 #endif
271
272 /* There is only one interface. */
273
274 static struct usb_interface_descriptor
275 fsg_intf_desc = {
276 .bLength = sizeof fsg_intf_desc,
277 .bDescriptorType = USB_DT_INTERFACE,
278
279 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
280 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
281 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
282 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
283 .iInterface = FSG_STRING_INTERFACE,
284 };
285
286 /*
287 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
288 * interrupt-in.
289 */
290
291 static struct usb_endpoint_descriptor
292 fsg_fs_bulk_in_desc = {
293 .bLength = USB_DT_ENDPOINT_SIZE,
294 .bDescriptorType = USB_DT_ENDPOINT,
295
296 .bEndpointAddress = USB_DIR_IN,
297 .bmAttributes = USB_ENDPOINT_XFER_BULK,
298 /* wMaxPacketSize set by autoconfiguration */
299 };
300
301 static struct usb_endpoint_descriptor
302 fsg_fs_bulk_out_desc = {
303 .bLength = USB_DT_ENDPOINT_SIZE,
304 .bDescriptorType = USB_DT_ENDPOINT,
305
306 .bEndpointAddress = USB_DIR_OUT,
307 .bmAttributes = USB_ENDPOINT_XFER_BULK,
308 /* wMaxPacketSize set by autoconfiguration */
309 };
310
311 static struct usb_descriptor_header *fsg_fs_function[] = {
312 #ifdef CONFIG_USBIF_COMPLIANCE
313 (struct usb_descriptor_header *) &fsg_otg_desc,
314 #endif
315 (struct usb_descriptor_header *) &fsg_intf_desc,
316 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
317 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
318 NULL,
319 };
320
321
322 /*
323 * USB 2.0 devices need to expose both high speed and full speed
324 * descriptors, unless they only run at full speed.
325 *
326 * That means alternate endpoint descriptors (bigger packets)
327 * and a "device qualifier" ... plus more construction options
328 * for the configuration descriptor.
329 */
330 static struct usb_endpoint_descriptor
331 fsg_hs_bulk_in_desc = {
332 .bLength = USB_DT_ENDPOINT_SIZE,
333 .bDescriptorType = USB_DT_ENDPOINT,
334
335 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
336 .bmAttributes = USB_ENDPOINT_XFER_BULK,
337 .wMaxPacketSize = cpu_to_le16(512),
338 };
339
340 static struct usb_endpoint_descriptor
341 fsg_hs_bulk_out_desc = {
342 .bLength = USB_DT_ENDPOINT_SIZE,
343 .bDescriptorType = USB_DT_ENDPOINT,
344
345 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
346 .bmAttributes = USB_ENDPOINT_XFER_BULK,
347 .wMaxPacketSize = cpu_to_le16(512),
348 .bInterval = 1, /* NAK every 1 uframe */
349 };
350
351
352 static struct usb_descriptor_header *fsg_hs_function[] = {
353 #ifdef CONFIG_USBIF_COMPLIANCE
354 (struct usb_descriptor_header *) &fsg_otg_desc,
355 #endif
356 (struct usb_descriptor_header *) &fsg_intf_desc,
357 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
358 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
359 NULL,
360 };
361
362 static struct usb_endpoint_descriptor
363 fsg_ss_bulk_in_desc = {
364 .bLength = USB_DT_ENDPOINT_SIZE,
365 .bDescriptorType = USB_DT_ENDPOINT,
366
367 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
368 .bmAttributes = USB_ENDPOINT_XFER_BULK,
369 .wMaxPacketSize = cpu_to_le16(1024),
370 };
371
372 static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
373 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
374 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
375
376 /*.bMaxBurst = DYNAMIC, */
377 };
378
379 static struct usb_endpoint_descriptor
380 fsg_ss_bulk_out_desc = {
381 .bLength = USB_DT_ENDPOINT_SIZE,
382 .bDescriptorType = USB_DT_ENDPOINT,
383
384 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
385 .bmAttributes = USB_ENDPOINT_XFER_BULK,
386 .wMaxPacketSize = cpu_to_le16(1024),
387 };
388
389 static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
390 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
391 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
392
393 /*.bMaxBurst = DYNAMIC, */
394 };
395
396 static struct usb_descriptor_header *fsg_ss_function[] = {
397 #ifdef CONFIG_USBIF_COMPLIANCE
398 (struct usb_descriptor_header *) &fsg_otg_desc,
399 #endif
400 (struct usb_descriptor_header *) &fsg_intf_desc,
401 (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
402 (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
403 (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
404 (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
405 NULL,
406 };
407
408 /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
409 static struct usb_string fsg_strings[] = {
410 {FSG_STRING_INTERFACE, fsg_string_interface},
411 {}
412 };
413
414 static struct usb_gadget_strings fsg_stringtab = {
415 .language = 0x0409, /* en-us */
416 .strings = fsg_strings,
417 };
418
419
420 /*-------------------------------------------------------------------------*/
421
422 /*
423 * If the next two routines are called while the gadget is registered,
424 * the caller must own fsg->filesem for writing.
425 */
426
427 static void fsg_lun_close(struct fsg_lun *curlun)
428 {
429 if (curlun->filp) {
430 LDBG(curlun, "close backing file\n");
431 fput(curlun->filp);
432 curlun->filp = NULL;
433 }
434 }
435
436
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 unsigned int blkbits;
447 unsigned int blksize;
448
449 #ifdef CONFIG_MTK_ICUSB_SUPPORT
450 #define ICUSB_STORAGE_LABEL "/dev/block/vold/8:"
451 if(strstr(filename, ICUSB_STORAGE_LABEL))
452 {
453 printk(KERN_WARNING "filename : %s, set isICUSB to 0\n", filename);
454 curlun->isICUSB = 1;
455 }
456 else
457 {
458 printk(KERN_WARNING "filename : %s, set isICUSB to 1\n", filename);
459 curlun->isICUSB = 0;
460 }
461 #endif
462
463 /* R/W if we can, R/O if we must */
464 ro = curlun->initially_ro;
465 if (!ro) {
466 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
467 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
468 ro = 1;
469 }
470 if (ro)
471 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
472 if (IS_ERR(filp)) {
473 LINFO(curlun, "unable to open backing file: %s\n", filename);
474 return PTR_ERR(filp);
475 }
476
477 if (!(filp->f_mode & FMODE_WRITE))
478 ro = 1;
479
480 inode = file_inode(filp);
481 if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
482 LINFO(curlun, "invalid file type: %s\n", filename);
483 goto out;
484 }
485
486 /*
487 * If we can't read the file, it's no good.
488 * If we can't write the file, use it read-only.
489 */
490 if (!(filp->f_op->read || filp->f_op->aio_read)) {
491 LINFO(curlun, "file not readable: %s\n", filename);
492 goto out;
493 }
494 if (!(filp->f_op->write || filp->f_op->aio_write))
495 ro = 1;
496
497 size = i_size_read(inode->i_mapping->host);
498 if (size < 0) {
499 LINFO(curlun, "unable to find file size: %s\n", filename);
500 rc = (int) size;
501 goto out;
502 }
503
504 if (curlun->cdrom) {
505 blksize = 2048;
506 blkbits = 11;
507 } else if (inode->i_bdev) {
508 blksize = bdev_logical_block_size(inode->i_bdev);
509 blkbits = blksize_bits(blksize);
510 } else {
511 blksize = 512;
512 blkbits = 9;
513 }
514
515 num_sectors = size >> blkbits; /* File size in logic-block-size blocks */
516 min_sectors = 1;
517 if (curlun->cdrom) {
518 min_sectors = 300; /* Smallest track is 300 frames */
519 if (num_sectors >= 256*60*75) {
520 num_sectors = 256*60*75 - 1;
521 LINFO(curlun, "file too big: %s\n", filename);
522 LINFO(curlun, "using only first %d blocks\n",
523 (int) num_sectors);
524 }
525 }
526 if (num_sectors < min_sectors) {
527 LINFO(curlun, "file too small: %s\n", filename);
528 rc = -ETOOSMALL;
529 goto out;
530 }
531
532 if (fsg_lun_is_open(curlun))
533 fsg_lun_close(curlun);
534
535 curlun->blksize = blksize;
536 curlun->blkbits = blkbits;
537 curlun->ro = ro;
538 curlun->filp = filp;
539 curlun->file_length = size;
540 curlun->num_sectors = num_sectors;
541 LDBG(curlun, "open backing file: %s\n", filename);
542 return 0;
543
544 out:
545 fput(filp);
546 return rc;
547 }
548
549
550 /*-------------------------------------------------------------------------*/
551
552 /*
553 * Sync the file data, don't bother with the metadata.
554 * This code was copied from fs/buffer.c:sys_fdatasync().
555 */
556 static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
557 {
558 struct file *filp = curlun->filp;
559
560 if (curlun->ro || !filp)
561 return 0;
562 return vfs_fsync(filp, 1);
563 }
564
565 static void store_cdrom_address(u8 *dest, int msf, u32 addr)
566 {
567 if (msf) {
568 /* Convert to Minutes-Seconds-Frames */
569 addr >>= 2; /* Convert to 2048-byte frames */
570 addr += 2*75; /* Lead-in occupies 2 seconds */
571 dest[3] = addr % 75; /* Frames */
572 addr /= 75;
573 dest[2] = addr % 60; /* Seconds */
574 addr /= 60;
575 dest[1] = addr; /* Minutes */
576 dest[0] = 0; /* Reserved */
577 } else {
578 /* Absolute sector */
579 put_unaligned_be32(addr, dest);
580 }
581 }
582
583 /**
584 * fsg_get_toc() - Builds a TOC with required format @format.
585 * @curlun: The LUN for which the TOC has to be built
586 * @msf: Min Sec Frame format or LBA format for address
587 * @format: TOC format code
588 * @buf: The buffer into which the TOC is built
589 *
590 * Builds a Table of Content which can be used as data for READ_TOC command.
591 * The TOC simulates a single session, single track CD-ROM mode 1 disc.
592 *
593 * Returns number of bytes written to @buf, -EINVAL if format not supported.
594 */
595 static int fsg_get_toc(struct fsg_lun *curlun, int msf, int format, u8 *buf)
596 {
597 int i, len;
598 switch (format) {
599
600 case 0:
601 /* Formatted TOC */
602 len = 4 + 2*8; /* 4 byte header + 2 descriptors */
603 memset(buf, 0, len);
604 buf[1] = len - 2; /* TOC Length excludes length field */
605
606 buf[2] = 1; /* First track number */
607 buf[3] = 1; /* Last track number */
608 buf[5] = 0x16; /* Data track, copying allowed */
609 buf[6] = 0x01; /* Only track is number 1 */
610 store_cdrom_address(&buf[8], msf, 0);
611
612 buf[13] = 0x16; /* Lead-out track is data */
613 buf[14] = 0xAA; /* Lead-out track number */
614 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
615 break;
616
617 case 2:
618 /* Raw TOC */
619 len = 4 + 3*11; /* 4 byte header + 3 descriptors */
620 memset(buf, 0, len); /* Header + A0, A1 & A2 descriptors */
621 buf[1] = len - 2; /* TOC Length excludes length field */
622 buf[2] = 1; /* First complete session */
623 buf[3] = 1; /* Last complete session */
624
625 buf += 4;
626 /* fill in A0, A1 and A2 points */
627 for (i = 0; i < 3; i++) {
628 buf[0] = 1; /* Session number */
629 buf[1] = 0x16; /* Data track, copying allowed */
630 /* 2 - Track number 0 -> TOC */
631 buf[3] = 0xA0 + i; /* A0, A1, A2 point */
632 /* 4, 5, 6 - Min, sec, frame is zero */
633 buf[8] = 1; /* Pmin: last track number */
634 buf += 11; /* go to next track descriptor */
635 }
636 buf -= 11; /* go back to A2 descriptor */
637
638 /* For A2, 7, 8, 9, 10 - zero, Pmin, Psec, Pframe of Lead out */
639 store_cdrom_address(&buf[7], msf, curlun->num_sectors);
640 break;
641
642 default:
643 /* Multi-session, PMA, ATIP, CD-TEXT not supported/required */
644 len = -EINVAL;
645 break;
646 }
647 return len;
648 }
649
650 /*-------------------------------------------------------------------------*/
651
652
653 static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
654 char *buf)
655 {
656 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
657
658 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
659 ? curlun->ro
660 : curlun->initially_ro);
661 }
662
663 static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
664 char *buf)
665 {
666 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
667
668 return sprintf(buf, "%u\n", curlun->nofua);
669 }
670
671 static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
672 char *buf)
673 {
674 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
675 struct rw_semaphore *filesem = dev_get_drvdata(dev);
676 char *p;
677 ssize_t rc;
678
679 down_read(filesem);
680 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
681 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
682 if (IS_ERR(p))
683 rc = PTR_ERR(p);
684 else {
685 rc = strlen(p);
686 memmove(buf, p, rc);
687 buf[rc] = '\n'; /* Add a newline */
688 buf[++rc] = 0;
689 }
690 } else { /* No file, return 0 bytes */
691 *buf = 0;
692 rc = 0;
693 }
694 up_read(filesem);
695 return rc;
696 }
697
698
699 static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
700 const char *buf, size_t count)
701 {
702 ssize_t rc;
703 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
704 struct rw_semaphore *filesem = dev_get_drvdata(dev);
705 unsigned ro;
706
707 rc = kstrtouint(buf, 2, &ro);
708 if (rc)
709 return rc;
710
711 /*
712 * Allow the write-enable status to change only while the
713 * backing file is closed.
714 */
715 down_read(filesem);
716 if (fsg_lun_is_open(curlun)) {
717 LDBG(curlun, "read-only status change prevented\n");
718 rc = -EBUSY;
719 } else {
720 curlun->ro = ro;
721 curlun->initially_ro = ro;
722 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
723 rc = count;
724 }
725 up_read(filesem);
726 return rc;
727 }
728
729 static ssize_t fsg_store_nofua(struct device *dev,
730 struct device_attribute *attr,
731 const char *buf, size_t count)
732 {
733 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
734 unsigned nofua;
735 int ret;
736
737 ret = kstrtouint(buf, 2, &nofua);
738 if (ret)
739 return ret;
740
741 /* Sync data when switching from async mode to sync */
742 if (!nofua && curlun->nofua)
743 fsg_lun_fsync_sub(curlun);
744
745 curlun->nofua = nofua;
746
747 return count;
748 }
749
750 static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
751 const char *buf, size_t count)
752 {
753 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
754 struct rw_semaphore *filesem = dev_get_drvdata(dev);
755 int rc = 0;
756
757
758 #if !defined(CONFIG_USB_G_ANDROID)
759 /* disabled in android because we need to allow closing the backing file
760 * if the media was removed
761 */
762 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
763 LDBG(curlun, "eject attempt prevented\n");
764 return -EBUSY; /* "Door is locked" */
765 }
766 #endif
767
768 printk("fsg_store_file file=%s, count=%d, curlun->cdrom=%d\n", buf, (int)count, curlun->cdrom);
769
770 /*
771 * WORKAROUND:VOLD would clean the file path after switching to bicr.
772 * So when the lun is being a CD-ROM a.k.a. BICR. Dont clean the file path to empty.
773 */
774 if (curlun->cdrom == 1 && count == 1)
775 return count;
776
777 /* Remove a trailing newline */
778 if (count > 0 && buf[count-1] == '\n')
779 ((char *) buf)[count-1] = 0; /* Ugh! */
780
781 /* Load new medium */
782 down_write(filesem);
783 if (count > 0 && buf[0]) {
784 /* fsg_lun_open() will close existing file if any. */
785 rc = fsg_lun_open(curlun, buf);
786 if (rc == 0)
787 curlun->unit_attention_data =
788 SS_NOT_READY_TO_READY_TRANSITION;
789 } else if (fsg_lun_is_open(curlun)) {
790 fsg_lun_close(curlun);
791 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
792 }
793 up_write(filesem);
794 return (rc < 0 ? rc : count);
795 }