diff options
author | Andrea Bastoni <bastoni@cs.unc.edu> | 2010-05-30 19:16:45 -0400 |
---|---|---|
committer | Andrea Bastoni <bastoni@cs.unc.edu> | 2010-05-30 19:16:45 -0400 |
commit | ada47b5fe13d89735805b566185f4885f5a3f750 (patch) | |
tree | 644b88f8a71896307d71438e9b3af49126ffb22b /drivers/usb/gadget/file_storage.c | |
parent | 43e98717ad40a4ae64545b5ba047c7b86aa44f4f (diff) | |
parent | 3280f21d43ee541f97f8cda5792150d2dbec20d5 (diff) |
Merge branch 'wip-2.6.34' into old-private-masterarchived-private-master
Diffstat (limited to 'drivers/usb/gadget/file_storage.c')
-rw-r--r-- | drivers/usb/gadget/file_storage.c | 891 |
1 files changed, 114 insertions, 777 deletions
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c index 1e6aa504d58a..b49d86e3e45b 100644 --- a/drivers/usb/gadget/file_storage.c +++ b/drivers/usb/gadget/file_storage.c | |||
@@ -248,8 +248,6 @@ | |||
248 | #include <linux/freezer.h> | 248 | #include <linux/freezer.h> |
249 | #include <linux/utsname.h> | 249 | #include <linux/utsname.h> |
250 | 250 | ||
251 | #include <asm/unaligned.h> | ||
252 | |||
253 | #include <linux/usb/ch9.h> | 251 | #include <linux/usb/ch9.h> |
254 | #include <linux/usb/gadget.h> | 252 | #include <linux/usb/gadget.h> |
255 | 253 | ||
@@ -274,21 +272,20 @@ | |||
274 | #define DRIVER_NAME "g_file_storage" | 272 | #define DRIVER_NAME "g_file_storage" |
275 | #define DRIVER_VERSION "20 November 2008" | 273 | #define DRIVER_VERSION "20 November 2008" |
276 | 274 | ||
277 | static const char longname[] = DRIVER_DESC; | 275 | static char fsg_string_manufacturer[64]; |
278 | static const char shortname[] = DRIVER_NAME; | 276 | static const char fsg_string_product[] = DRIVER_DESC; |
277 | static char fsg_string_serial[13]; | ||
278 | static const char fsg_string_config[] = "Self-powered"; | ||
279 | static const char fsg_string_interface[] = "Mass Storage"; | ||
280 | |||
281 | |||
282 | #include "storage_common.c" | ||
283 | |||
279 | 284 | ||
280 | MODULE_DESCRIPTION(DRIVER_DESC); | 285 | MODULE_DESCRIPTION(DRIVER_DESC); |
281 | MODULE_AUTHOR("Alan Stern"); | 286 | MODULE_AUTHOR("Alan Stern"); |
282 | MODULE_LICENSE("Dual BSD/GPL"); | 287 | MODULE_LICENSE("Dual BSD/GPL"); |
283 | 288 | ||
284 | /* Thanks to NetChip Technologies for donating this product ID. | ||
285 | * | ||
286 | * DO NOT REUSE THESE IDs with any other driver!! Ever!! | ||
287 | * Instead: allocate your own, using normal USB-IF procedures. */ | ||
288 | #define DRIVER_VENDOR_ID 0x0525 // NetChip | ||
289 | #define DRIVER_PRODUCT_ID 0xa4a5 // Linux-USB File-backed Storage Gadget | ||
290 | |||
291 | |||
292 | /* | 289 | /* |
293 | * This driver assumes self-powered hardware and has no way for users to | 290 | * This driver assumes self-powered hardware and has no way for users to |
294 | * trigger remote wakeup. It uses autoconfiguration to select endpoints | 291 | * trigger remote wakeup. It uses autoconfiguration to select endpoints |
@@ -298,54 +295,12 @@ MODULE_LICENSE("Dual BSD/GPL"); | |||
298 | 295 | ||
299 | /*-------------------------------------------------------------------------*/ | 296 | /*-------------------------------------------------------------------------*/ |
300 | 297 | ||
301 | #define LDBG(lun,fmt,args...) \ | ||
302 | dev_dbg(&(lun)->dev , fmt , ## args) | ||
303 | #define MDBG(fmt,args...) \ | ||
304 | pr_debug(DRIVER_NAME ": " fmt , ## args) | ||
305 | |||
306 | #ifndef DEBUG | ||
307 | #undef VERBOSE_DEBUG | ||
308 | #undef DUMP_MSGS | ||
309 | #endif /* !DEBUG */ | ||
310 | |||
311 | #ifdef VERBOSE_DEBUG | ||
312 | #define VLDBG LDBG | ||
313 | #else | ||
314 | #define VLDBG(lun,fmt,args...) \ | ||
315 | do { } while (0) | ||
316 | #endif /* VERBOSE_DEBUG */ | ||
317 | |||
318 | #define LERROR(lun,fmt,args...) \ | ||
319 | dev_err(&(lun)->dev , fmt , ## args) | ||
320 | #define LWARN(lun,fmt,args...) \ | ||
321 | dev_warn(&(lun)->dev , fmt , ## args) | ||
322 | #define LINFO(lun,fmt,args...) \ | ||
323 | dev_info(&(lun)->dev , fmt , ## args) | ||
324 | |||
325 | #define MINFO(fmt,args...) \ | ||
326 | pr_info(DRIVER_NAME ": " fmt , ## args) | ||
327 | |||
328 | #define DBG(d, fmt, args...) \ | ||
329 | dev_dbg(&(d)->gadget->dev , fmt , ## args) | ||
330 | #define VDBG(d, fmt, args...) \ | ||
331 | dev_vdbg(&(d)->gadget->dev , fmt , ## args) | ||
332 | #define ERROR(d, fmt, args...) \ | ||
333 | dev_err(&(d)->gadget->dev , fmt , ## args) | ||
334 | #define WARNING(d, fmt, args...) \ | ||
335 | dev_warn(&(d)->gadget->dev , fmt , ## args) | ||
336 | #define INFO(d, fmt, args...) \ | ||
337 | dev_info(&(d)->gadget->dev , fmt , ## args) | ||
338 | |||
339 | |||
340 | /*-------------------------------------------------------------------------*/ | ||
341 | 298 | ||
342 | /* Encapsulate the module parameter settings */ | 299 | /* Encapsulate the module parameter settings */ |
343 | 300 | ||
344 | #define MAX_LUNS 8 | ||
345 | |||
346 | static struct { | 301 | static struct { |
347 | char *file[MAX_LUNS]; | 302 | char *file[FSG_MAX_LUNS]; |
348 | int ro[MAX_LUNS]; | 303 | int ro[FSG_MAX_LUNS]; |
349 | unsigned int num_filenames; | 304 | unsigned int num_filenames; |
350 | unsigned int num_ros; | 305 | unsigned int num_ros; |
351 | unsigned int nluns; | 306 | unsigned int nluns; |
@@ -372,8 +327,8 @@ static struct { | |||
372 | .removable = 0, | 327 | .removable = 0, |
373 | .can_stall = 1, | 328 | .can_stall = 1, |
374 | .cdrom = 0, | 329 | .cdrom = 0, |
375 | .vendor = DRIVER_VENDOR_ID, | 330 | .vendor = FSG_VENDOR_ID, |
376 | .product = DRIVER_PRODUCT_ID, | 331 | .product = FSG_PRODUCT_ID, |
377 | .release = 0xffff, // Use controller chip type | 332 | .release = 0xffff, // Use controller chip type |
378 | .buflen = 16384, | 333 | .buflen = 16384, |
379 | }; | 334 | }; |
@@ -425,125 +380,6 @@ MODULE_PARM_DESC(buflen, "I/O buffer size"); | |||
425 | #endif /* CONFIG_USB_FILE_STORAGE_TEST */ | 380 | #endif /* CONFIG_USB_FILE_STORAGE_TEST */ |
426 | 381 | ||
427 | 382 | ||
428 | /*-------------------------------------------------------------------------*/ | ||
429 | |||
430 | /* SCSI device types */ | ||
431 | #define TYPE_DISK 0x00 | ||
432 | #define TYPE_CDROM 0x05 | ||
433 | |||
434 | /* USB protocol value = the transport method */ | ||
435 | #define USB_PR_CBI 0x00 // Control/Bulk/Interrupt | ||
436 | #define USB_PR_CB 0x01 // Control/Bulk w/o interrupt | ||
437 | #define USB_PR_BULK 0x50 // Bulk-only | ||
438 | |||
439 | /* USB subclass value = the protocol encapsulation */ | ||
440 | #define USB_SC_RBC 0x01 // Reduced Block Commands (flash) | ||
441 | #define USB_SC_8020 0x02 // SFF-8020i, MMC-2, ATAPI (CD-ROM) | ||
442 | #define USB_SC_QIC 0x03 // QIC-157 (tape) | ||
443 | #define USB_SC_UFI 0x04 // UFI (floppy) | ||
444 | #define USB_SC_8070 0x05 // SFF-8070i (removable) | ||
445 | #define USB_SC_SCSI 0x06 // Transparent SCSI | ||
446 | |||
447 | /* Bulk-only data structures */ | ||
448 | |||
449 | /* Command Block Wrapper */ | ||
450 | struct bulk_cb_wrap { | ||
451 | __le32 Signature; // Contains 'USBC' | ||
452 | u32 Tag; // Unique per command id | ||
453 | __le32 DataTransferLength; // Size of the data | ||
454 | u8 Flags; // Direction in bit 7 | ||
455 | u8 Lun; // LUN (normally 0) | ||
456 | u8 Length; // Of the CDB, <= MAX_COMMAND_SIZE | ||
457 | u8 CDB[16]; // Command Data Block | ||
458 | }; | ||
459 | |||
460 | #define USB_BULK_CB_WRAP_LEN 31 | ||
461 | #define USB_BULK_CB_SIG 0x43425355 // Spells out USBC | ||
462 | #define USB_BULK_IN_FLAG 0x80 | ||
463 | |||
464 | /* Command Status Wrapper */ | ||
465 | struct bulk_cs_wrap { | ||
466 | __le32 Signature; // Should = 'USBS' | ||
467 | u32 Tag; // Same as original command | ||
468 | __le32 Residue; // Amount not transferred | ||
469 | u8 Status; // See below | ||
470 | }; | ||
471 | |||
472 | #define USB_BULK_CS_WRAP_LEN 13 | ||
473 | #define USB_BULK_CS_SIG 0x53425355 // Spells out 'USBS' | ||
474 | #define USB_STATUS_PASS 0 | ||
475 | #define USB_STATUS_FAIL 1 | ||
476 | #define USB_STATUS_PHASE_ERROR 2 | ||
477 | |||
478 | /* Bulk-only class specific requests */ | ||
479 | #define USB_BULK_RESET_REQUEST 0xff | ||
480 | #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe | ||
481 | |||
482 | |||
483 | /* CBI Interrupt data structure */ | ||
484 | struct interrupt_data { | ||
485 | u8 bType; | ||
486 | u8 bValue; | ||
487 | }; | ||
488 | |||
489 | #define CBI_INTERRUPT_DATA_LEN 2 | ||
490 | |||
491 | /* CBI Accept Device-Specific Command request */ | ||
492 | #define USB_CBI_ADSC_REQUEST 0x00 | ||
493 | |||
494 | |||
495 | #define MAX_COMMAND_SIZE 16 // Length of a SCSI Command Data Block | ||
496 | |||
497 | /* SCSI commands that we recognize */ | ||
498 | #define SC_FORMAT_UNIT 0x04 | ||
499 | #define SC_INQUIRY 0x12 | ||
500 | #define SC_MODE_SELECT_6 0x15 | ||
501 | #define SC_MODE_SELECT_10 0x55 | ||
502 | #define SC_MODE_SENSE_6 0x1a | ||
503 | #define SC_MODE_SENSE_10 0x5a | ||
504 | #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e | ||
505 | #define SC_READ_6 0x08 | ||
506 | #define SC_READ_10 0x28 | ||
507 | #define SC_READ_12 0xa8 | ||
508 | #define SC_READ_CAPACITY 0x25 | ||
509 | #define SC_READ_FORMAT_CAPACITIES 0x23 | ||
510 | #define SC_READ_HEADER 0x44 | ||
511 | #define SC_READ_TOC 0x43 | ||
512 | #define SC_RELEASE 0x17 | ||
513 | #define SC_REQUEST_SENSE 0x03 | ||
514 | #define SC_RESERVE 0x16 | ||
515 | #define SC_SEND_DIAGNOSTIC 0x1d | ||
516 | #define SC_START_STOP_UNIT 0x1b | ||
517 | #define SC_SYNCHRONIZE_CACHE 0x35 | ||
518 | #define SC_TEST_UNIT_READY 0x00 | ||
519 | #define SC_VERIFY 0x2f | ||
520 | #define SC_WRITE_6 0x0a | ||
521 | #define SC_WRITE_10 0x2a | ||
522 | #define SC_WRITE_12 0xaa | ||
523 | |||
524 | /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */ | ||
525 | #define SS_NO_SENSE 0 | ||
526 | #define SS_COMMUNICATION_FAILURE 0x040800 | ||
527 | #define SS_INVALID_COMMAND 0x052000 | ||
528 | #define SS_INVALID_FIELD_IN_CDB 0x052400 | ||
529 | #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100 | ||
530 | #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500 | ||
531 | #define SS_MEDIUM_NOT_PRESENT 0x023a00 | ||
532 | #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302 | ||
533 | #define SS_NOT_READY_TO_READY_TRANSITION 0x062800 | ||
534 | #define SS_RESET_OCCURRED 0x062900 | ||
535 | #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900 | ||
536 | #define SS_UNRECOVERED_READ_ERROR 0x031100 | ||
537 | #define SS_WRITE_ERROR 0x030c02 | ||
538 | #define SS_WRITE_PROTECTED 0x072700 | ||
539 | |||
540 | #define SK(x) ((u8) ((x) >> 16)) // Sense Key byte, etc. | ||
541 | #define ASC(x) ((u8) ((x) >> 8)) | ||
542 | #define ASCQ(x) ((u8) (x)) | ||
543 | |||
544 | |||
545 | /*-------------------------------------------------------------------------*/ | ||
546 | |||
547 | /* | 383 | /* |
548 | * These definitions will permit the compiler to avoid generating code for | 384 | * These definitions will permit the compiler to avoid generating code for |
549 | * parts of the driver that aren't used in the non-TEST version. Even gcc | 385 | * parts of the driver that aren't used in the non-TEST version. Even gcc |
@@ -566,81 +402,8 @@ struct interrupt_data { | |||
566 | #endif /* CONFIG_USB_FILE_STORAGE_TEST */ | 402 | #endif /* CONFIG_USB_FILE_STORAGE_TEST */ |
567 | 403 | ||
568 | 404 | ||
569 | struct lun { | 405 | /*-------------------------------------------------------------------------*/ |
570 | struct file *filp; | ||
571 | loff_t file_length; | ||
572 | loff_t num_sectors; | ||
573 | |||
574 | unsigned int ro : 1; | ||
575 | unsigned int prevent_medium_removal : 1; | ||
576 | unsigned int registered : 1; | ||
577 | unsigned int info_valid : 1; | ||
578 | |||
579 | u32 sense_data; | ||
580 | u32 sense_data_info; | ||
581 | u32 unit_attention_data; | ||
582 | |||
583 | struct device dev; | ||
584 | }; | ||
585 | |||
586 | #define backing_file_is_open(curlun) ((curlun)->filp != NULL) | ||
587 | |||
588 | static struct lun *dev_to_lun(struct device *dev) | ||
589 | { | ||
590 | return container_of(dev, struct lun, dev); | ||
591 | } | ||
592 | |||
593 | |||
594 | /* Big enough to hold our biggest descriptor */ | ||
595 | #define EP0_BUFSIZE 256 | ||
596 | #define DELAYED_STATUS (EP0_BUFSIZE + 999) // An impossibly large value | ||
597 | |||
598 | /* Number of buffers we will use. 2 is enough for double-buffering */ | ||
599 | #define NUM_BUFFERS 2 | ||
600 | |||
601 | enum fsg_buffer_state { | ||
602 | BUF_STATE_EMPTY = 0, | ||
603 | BUF_STATE_FULL, | ||
604 | BUF_STATE_BUSY | ||
605 | }; | ||
606 | |||
607 | struct fsg_buffhd { | ||
608 | void *buf; | ||
609 | enum fsg_buffer_state state; | ||
610 | struct fsg_buffhd *next; | ||
611 | |||
612 | /* The NetChip 2280 is faster, and handles some protocol faults | ||
613 | * better, if we don't submit any short bulk-out read requests. | ||
614 | * So we will record the intended request length here. */ | ||
615 | unsigned int bulk_out_intended_length; | ||
616 | |||
617 | struct usb_request *inreq; | ||
618 | int inreq_busy; | ||
619 | struct usb_request *outreq; | ||
620 | int outreq_busy; | ||
621 | }; | ||
622 | |||
623 | enum fsg_state { | ||
624 | FSG_STATE_COMMAND_PHASE = -10, // This one isn't used anywhere | ||
625 | FSG_STATE_DATA_PHASE, | ||
626 | FSG_STATE_STATUS_PHASE, | ||
627 | |||
628 | FSG_STATE_IDLE = 0, | ||
629 | FSG_STATE_ABORT_BULK_OUT, | ||
630 | FSG_STATE_RESET, | ||
631 | FSG_STATE_INTERFACE_CHANGE, | ||
632 | FSG_STATE_CONFIG_CHANGE, | ||
633 | FSG_STATE_DISCONNECT, | ||
634 | FSG_STATE_EXIT, | ||
635 | FSG_STATE_TERMINATED | ||
636 | }; | ||
637 | 406 | ||
638 | enum data_direction { | ||
639 | DATA_DIR_UNKNOWN = 0, | ||
640 | DATA_DIR_FROM_HOST, | ||
641 | DATA_DIR_TO_HOST, | ||
642 | DATA_DIR_NONE | ||
643 | }; | ||
644 | 407 | ||
645 | struct fsg_dev { | 408 | struct fsg_dev { |
646 | /* lock protects: state, all the req_busy's, and cbbuf_cmnd */ | 409 | /* lock protects: state, all the req_busy's, and cbbuf_cmnd */ |
@@ -662,7 +425,7 @@ struct fsg_dev { | |||
662 | int intreq_busy; | 425 | int intreq_busy; |
663 | struct fsg_buffhd *intr_buffhd; | 426 | struct fsg_buffhd *intr_buffhd; |
664 | 427 | ||
665 | unsigned int bulk_out_maxpacket; | 428 | unsigned int bulk_out_maxpacket; |
666 | enum fsg_state state; // For exception handling | 429 | enum fsg_state state; // For exception handling |
667 | unsigned int exception_req_tag; | 430 | unsigned int exception_req_tag; |
668 | 431 | ||
@@ -687,7 +450,7 @@ struct fsg_dev { | |||
687 | 450 | ||
688 | struct fsg_buffhd *next_buffhd_to_fill; | 451 | struct fsg_buffhd *next_buffhd_to_fill; |
689 | struct fsg_buffhd *next_buffhd_to_drain; | 452 | struct fsg_buffhd *next_buffhd_to_drain; |
690 | struct fsg_buffhd buffhds[NUM_BUFFERS]; | 453 | struct fsg_buffhd buffhds[FSG_NUM_BUFFERS]; |
691 | 454 | ||
692 | int thread_wakeup_needed; | 455 | int thread_wakeup_needed; |
693 | struct completion thread_notifier; | 456 | struct completion thread_notifier; |
@@ -712,8 +475,8 @@ struct fsg_dev { | |||
712 | u8 cbbuf_cmnd[MAX_COMMAND_SIZE]; | 475 | u8 cbbuf_cmnd[MAX_COMMAND_SIZE]; |
713 | 476 | ||
714 | unsigned int nluns; | 477 | unsigned int nluns; |
715 | struct lun *luns; | 478 | struct fsg_lun *luns; |
716 | struct lun *curlun; | 479 | struct fsg_lun *curlun; |
717 | }; | 480 | }; |
718 | 481 | ||
719 | typedef void (*fsg_routine_t)(struct fsg_dev *); | 482 | typedef void (*fsg_routine_t)(struct fsg_dev *); |
@@ -739,49 +502,9 @@ static void set_bulk_out_req_length(struct fsg_dev *fsg, | |||
739 | static struct fsg_dev *the_fsg; | 502 | static struct fsg_dev *the_fsg; |
740 | static struct usb_gadget_driver fsg_driver; | 503 | static struct usb_gadget_driver fsg_driver; |
741 | 504 | ||
742 | static void close_backing_file(struct lun *curlun); | ||
743 | |||
744 | 505 | ||
745 | /*-------------------------------------------------------------------------*/ | 506 | /*-------------------------------------------------------------------------*/ |
746 | 507 | ||
747 | #ifdef DUMP_MSGS | ||
748 | |||
749 | static void dump_msg(struct fsg_dev *fsg, const char *label, | ||
750 | const u8 *buf, unsigned int length) | ||
751 | { | ||
752 | if (length < 512) { | ||
753 | DBG(fsg, "%s, length %u:\n", label, length); | ||
754 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, | ||
755 | 16, 1, buf, length, 0); | ||
756 | } | ||
757 | } | ||
758 | |||
759 | static void dump_cdb(struct fsg_dev *fsg) | ||
760 | {} | ||
761 | |||
762 | #else | ||
763 | |||
764 | static void dump_msg(struct fsg_dev *fsg, const char *label, | ||
765 | const u8 *buf, unsigned int length) | ||
766 | {} | ||
767 | |||
768 | #ifdef VERBOSE_DEBUG | ||
769 | |||
770 | static void dump_cdb(struct fsg_dev *fsg) | ||
771 | { | ||
772 | print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, | ||
773 | 16, 1, fsg->cmnd, fsg->cmnd_size, 0); | ||
774 | } | ||
775 | |||
776 | #else | ||
777 | |||
778 | static void dump_cdb(struct fsg_dev *fsg) | ||
779 | {} | ||
780 | |||
781 | #endif /* VERBOSE_DEBUG */ | ||
782 | #endif /* DUMP_MSGS */ | ||
783 | |||
784 | |||
785 | static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep) | 508 | static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep) |
786 | { | 509 | { |
787 | const char *name; | 510 | const char *name; |
@@ -799,26 +522,11 @@ static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep) | |||
799 | 522 | ||
800 | /*-------------------------------------------------------------------------*/ | 523 | /*-------------------------------------------------------------------------*/ |
801 | 524 | ||
802 | /* Routines for unaligned data access */ | ||
803 | |||
804 | static u32 get_unaligned_be24(u8 *buf) | ||
805 | { | ||
806 | return 0xffffff & (u32) get_unaligned_be32(buf - 1); | ||
807 | } | ||
808 | |||
809 | |||
810 | /*-------------------------------------------------------------------------*/ | ||
811 | |||
812 | /* | 525 | /* |
813 | * DESCRIPTORS ... most are static, but strings and (full) configuration | 526 | * DESCRIPTORS ... most are static, but strings and (full) configuration |
814 | * descriptors are built on demand. Also the (static) config and interface | 527 | * descriptors are built on demand. Also the (static) config and interface |
815 | * descriptors are adjusted during fsg_bind(). | 528 | * descriptors are adjusted during fsg_bind(). |
816 | */ | 529 | */ |
817 | #define STRING_MANUFACTURER 1 | ||
818 | #define STRING_PRODUCT 2 | ||
819 | #define STRING_SERIAL 3 | ||
820 | #define STRING_CONFIG 4 | ||
821 | #define STRING_INTERFACE 5 | ||
822 | 530 | ||
823 | /* There is only one configuration. */ | 531 | /* There is only one configuration. */ |
824 | #define CONFIG_VALUE 1 | 532 | #define CONFIG_VALUE 1 |
@@ -832,13 +540,13 @@ device_desc = { | |||
832 | .bDeviceClass = USB_CLASS_PER_INTERFACE, | 540 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
833 | 541 | ||
834 | /* The next three values can be overridden by module parameters */ | 542 | /* The next three values can be overridden by module parameters */ |
835 | .idVendor = cpu_to_le16(DRIVER_VENDOR_ID), | 543 | .idVendor = cpu_to_le16(FSG_VENDOR_ID), |
836 | .idProduct = cpu_to_le16(DRIVER_PRODUCT_ID), | 544 | .idProduct = cpu_to_le16(FSG_PRODUCT_ID), |
837 | .bcdDevice = cpu_to_le16(0xffff), | 545 | .bcdDevice = cpu_to_le16(0xffff), |
838 | 546 | ||
839 | .iManufacturer = STRING_MANUFACTURER, | 547 | .iManufacturer = FSG_STRING_MANUFACTURER, |
840 | .iProduct = STRING_PRODUCT, | 548 | .iProduct = FSG_STRING_PRODUCT, |
841 | .iSerialNumber = STRING_SERIAL, | 549 | .iSerialNumber = FSG_STRING_SERIAL, |
842 | .bNumConfigurations = 1, | 550 | .bNumConfigurations = 1, |
843 | }; | 551 | }; |
844 | 552 | ||
@@ -850,86 +558,12 @@ config_desc = { | |||
850 | /* wTotalLength computed by usb_gadget_config_buf() */ | 558 | /* wTotalLength computed by usb_gadget_config_buf() */ |
851 | .bNumInterfaces = 1, | 559 | .bNumInterfaces = 1, |
852 | .bConfigurationValue = CONFIG_VALUE, | 560 | .bConfigurationValue = CONFIG_VALUE, |
853 | .iConfiguration = STRING_CONFIG, | 561 | .iConfiguration = FSG_STRING_CONFIG, |
854 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | 562 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
855 | .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2, | 563 | .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2, |
856 | }; | 564 | }; |
857 | 565 | ||
858 | static struct usb_otg_descriptor | ||
859 | otg_desc = { | ||
860 | .bLength = sizeof(otg_desc), | ||
861 | .bDescriptorType = USB_DT_OTG, | ||
862 | |||
863 | .bmAttributes = USB_OTG_SRP, | ||
864 | }; | ||
865 | |||
866 | /* There is only one interface. */ | ||
867 | |||
868 | static struct usb_interface_descriptor | ||
869 | intf_desc = { | ||
870 | .bLength = sizeof intf_desc, | ||
871 | .bDescriptorType = USB_DT_INTERFACE, | ||
872 | |||
873 | .bNumEndpoints = 2, // Adjusted during fsg_bind() | ||
874 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, | ||
875 | .bInterfaceSubClass = USB_SC_SCSI, // Adjusted during fsg_bind() | ||
876 | .bInterfaceProtocol = USB_PR_BULK, // Adjusted during fsg_bind() | ||
877 | .iInterface = STRING_INTERFACE, | ||
878 | }; | ||
879 | |||
880 | /* Three full-speed endpoint descriptors: bulk-in, bulk-out, | ||
881 | * and interrupt-in. */ | ||
882 | |||
883 | static struct usb_endpoint_descriptor | ||
884 | fs_bulk_in_desc = { | ||
885 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
886 | .bDescriptorType = USB_DT_ENDPOINT, | ||
887 | |||
888 | .bEndpointAddress = USB_DIR_IN, | ||
889 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
890 | /* wMaxPacketSize set by autoconfiguration */ | ||
891 | }; | ||
892 | 566 | ||
893 | static struct usb_endpoint_descriptor | ||
894 | fs_bulk_out_desc = { | ||
895 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
896 | .bDescriptorType = USB_DT_ENDPOINT, | ||
897 | |||
898 | .bEndpointAddress = USB_DIR_OUT, | ||
899 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
900 | /* wMaxPacketSize set by autoconfiguration */ | ||
901 | }; | ||
902 | |||
903 | static struct usb_endpoint_descriptor | ||
904 | fs_intr_in_desc = { | ||
905 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
906 | .bDescriptorType = USB_DT_ENDPOINT, | ||
907 | |||
908 | .bEndpointAddress = USB_DIR_IN, | ||
909 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
910 | .wMaxPacketSize = cpu_to_le16(2), | ||
911 | .bInterval = 32, // frames -> 32 ms | ||
912 | }; | ||
913 | |||
914 | static const struct usb_descriptor_header *fs_function[] = { | ||
915 | (struct usb_descriptor_header *) &otg_desc, | ||
916 | (struct usb_descriptor_header *) &intf_desc, | ||
917 | (struct usb_descriptor_header *) &fs_bulk_in_desc, | ||
918 | (struct usb_descriptor_header *) &fs_bulk_out_desc, | ||
919 | (struct usb_descriptor_header *) &fs_intr_in_desc, | ||
920 | NULL, | ||
921 | }; | ||
922 | #define FS_FUNCTION_PRE_EP_ENTRIES 2 | ||
923 | |||
924 | |||
925 | /* | ||
926 | * USB 2.0 devices need to expose both high speed and full speed | ||
927 | * descriptors, unless they only run at full speed. | ||
928 | * | ||
929 | * That means alternate endpoint descriptors (bigger packets) | ||
930 | * and a "device qualifier" ... plus more construction options | ||
931 | * for the config descriptor. | ||
932 | */ | ||
933 | static struct usb_qualifier_descriptor | 567 | static struct usb_qualifier_descriptor |
934 | dev_qualifier = { | 568 | dev_qualifier = { |
935 | .bLength = sizeof dev_qualifier, | 569 | .bLength = sizeof dev_qualifier, |
@@ -941,78 +575,6 @@ dev_qualifier = { | |||
941 | .bNumConfigurations = 1, | 575 | .bNumConfigurations = 1, |
942 | }; | 576 | }; |
943 | 577 | ||
944 | static struct usb_endpoint_descriptor | ||
945 | hs_bulk_in_desc = { | ||
946 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
947 | .bDescriptorType = USB_DT_ENDPOINT, | ||
948 | |||
949 | /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */ | ||
950 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
951 | .wMaxPacketSize = cpu_to_le16(512), | ||
952 | }; | ||
953 | |||
954 | static struct usb_endpoint_descriptor | ||
955 | hs_bulk_out_desc = { | ||
956 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
957 | .bDescriptorType = USB_DT_ENDPOINT, | ||
958 | |||
959 | /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */ | ||
960 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
961 | .wMaxPacketSize = cpu_to_le16(512), | ||
962 | .bInterval = 1, // NAK every 1 uframe | ||
963 | }; | ||
964 | |||
965 | static struct usb_endpoint_descriptor | ||
966 | hs_intr_in_desc = { | ||
967 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
968 | .bDescriptorType = USB_DT_ENDPOINT, | ||
969 | |||
970 | /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */ | ||
971 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
972 | .wMaxPacketSize = cpu_to_le16(2), | ||
973 | .bInterval = 9, // 2**(9-1) = 256 uframes -> 32 ms | ||
974 | }; | ||
975 | |||
976 | static const struct usb_descriptor_header *hs_function[] = { | ||
977 | (struct usb_descriptor_header *) &otg_desc, | ||
978 | (struct usb_descriptor_header *) &intf_desc, | ||
979 | (struct usb_descriptor_header *) &hs_bulk_in_desc, | ||
980 | (struct usb_descriptor_header *) &hs_bulk_out_desc, | ||
981 | (struct usb_descriptor_header *) &hs_intr_in_desc, | ||
982 | NULL, | ||
983 | }; | ||
984 | #define HS_FUNCTION_PRE_EP_ENTRIES 2 | ||
985 | |||
986 | /* Maxpacket and other transfer characteristics vary by speed. */ | ||
987 | static struct usb_endpoint_descriptor * | ||
988 | ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, | ||
989 | struct usb_endpoint_descriptor *hs) | ||
990 | { | ||
991 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | ||
992 | return hs; | ||
993 | return fs; | ||
994 | } | ||
995 | |||
996 | |||
997 | /* The CBI specification limits the serial string to 12 uppercase hexadecimal | ||
998 | * characters. */ | ||
999 | static char manufacturer[64]; | ||
1000 | static char serial[13]; | ||
1001 | |||
1002 | /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */ | ||
1003 | static struct usb_string strings[] = { | ||
1004 | {STRING_MANUFACTURER, manufacturer}, | ||
1005 | {STRING_PRODUCT, longname}, | ||
1006 | {STRING_SERIAL, serial}, | ||
1007 | {STRING_CONFIG, "Self-powered"}, | ||
1008 | {STRING_INTERFACE, "Mass Storage"}, | ||
1009 | {} | ||
1010 | }; | ||
1011 | |||
1012 | static struct usb_gadget_strings stringtab = { | ||
1013 | .language = 0x0409, // en-us | ||
1014 | .strings = strings, | ||
1015 | }; | ||
1016 | 578 | ||
1017 | 579 | ||
1018 | /* | 580 | /* |
@@ -1032,10 +594,9 @@ static int populate_config_buf(struct usb_gadget *gadget, | |||
1032 | 594 | ||
1033 | if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG) | 595 | if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG) |
1034 | speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed; | 596 | speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed; |
1035 | if (gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH) | 597 | function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH |
1036 | function = hs_function; | 598 | ? (const struct usb_descriptor_header **)fsg_hs_function |
1037 | else | 599 | : (const struct usb_descriptor_header **)fsg_fs_function; |
1038 | function = fs_function; | ||
1039 | 600 | ||
1040 | /* for now, don't advertise srp-only devices */ | 601 | /* for now, don't advertise srp-only devices */ |
1041 | if (!gadget_is_otg(gadget)) | 602 | if (!gadget_is_otg(gadget)) |
@@ -1386,7 +947,7 @@ get_config: | |||
1386 | VDBG(fsg, "get string descriptor\n"); | 947 | VDBG(fsg, "get string descriptor\n"); |
1387 | 948 | ||
1388 | /* wIndex == language code */ | 949 | /* wIndex == language code */ |
1389 | value = usb_gadget_get_string(&stringtab, | 950 | value = usb_gadget_get_string(&fsg_stringtab, |
1390 | w_value & 0xff, req->buf); | 951 | w_value & 0xff, req->buf); |
1391 | break; | 952 | break; |
1392 | } | 953 | } |
@@ -1551,7 +1112,7 @@ static int sleep_thread(struct fsg_dev *fsg) | |||
1551 | 1112 | ||
1552 | static int do_read(struct fsg_dev *fsg) | 1113 | static int do_read(struct fsg_dev *fsg) |
1553 | { | 1114 | { |
1554 | struct lun *curlun = fsg->curlun; | 1115 | struct fsg_lun *curlun = fsg->curlun; |
1555 | u32 lba; | 1116 | u32 lba; |
1556 | struct fsg_buffhd *bh; | 1117 | struct fsg_buffhd *bh; |
1557 | int rc; | 1118 | int rc; |
@@ -1677,7 +1238,7 @@ static int do_read(struct fsg_dev *fsg) | |||
1677 | 1238 | ||
1678 | static int do_write(struct fsg_dev *fsg) | 1239 | static int do_write(struct fsg_dev *fsg) |
1679 | { | 1240 | { |
1680 | struct lun *curlun = fsg->curlun; | 1241 | struct fsg_lun *curlun = fsg->curlun; |
1681 | u32 lba; | 1242 | u32 lba; |
1682 | struct fsg_buffhd *bh; | 1243 | struct fsg_buffhd *bh; |
1683 | int get_some_more; | 1244 | int get_some_more; |
@@ -1713,7 +1274,7 @@ static int do_write(struct fsg_dev *fsg) | |||
1713 | } | 1274 | } |
1714 | if (fsg->cmnd[1] & 0x08) { // FUA | 1275 | if (fsg->cmnd[1] & 0x08) { // FUA |
1715 | spin_lock(&curlun->filp->f_lock); | 1276 | spin_lock(&curlun->filp->f_lock); |
1716 | curlun->filp->f_flags |= O_SYNC; | 1277 | curlun->filp->f_flags |= O_DSYNC; |
1717 | spin_unlock(&curlun->filp->f_lock); | 1278 | spin_unlock(&curlun->filp->f_lock); |
1718 | } | 1279 | } |
1719 | } | 1280 | } |
@@ -1864,33 +1425,14 @@ static int do_write(struct fsg_dev *fsg) | |||
1864 | 1425 | ||
1865 | /*-------------------------------------------------------------------------*/ | 1426 | /*-------------------------------------------------------------------------*/ |
1866 | 1427 | ||
1867 | /* Sync the file data, don't bother with the metadata. | ||
1868 | * This code was copied from fs/buffer.c:sys_fdatasync(). */ | ||
1869 | static int fsync_sub(struct lun *curlun) | ||
1870 | { | ||
1871 | struct file *filp = curlun->filp; | ||
1872 | |||
1873 | if (curlun->ro || !filp) | ||
1874 | return 0; | ||
1875 | return vfs_fsync(filp, filp->f_path.dentry, 1); | ||
1876 | } | ||
1877 | |||
1878 | static void fsync_all(struct fsg_dev *fsg) | ||
1879 | { | ||
1880 | int i; | ||
1881 | |||
1882 | for (i = 0; i < fsg->nluns; ++i) | ||
1883 | fsync_sub(&fsg->luns[i]); | ||
1884 | } | ||
1885 | |||
1886 | static int do_synchronize_cache(struct fsg_dev *fsg) | 1428 | static int do_synchronize_cache(struct fsg_dev *fsg) |
1887 | { | 1429 | { |
1888 | struct lun *curlun = fsg->curlun; | 1430 | struct fsg_lun *curlun = fsg->curlun; |
1889 | int rc; | 1431 | int rc; |
1890 | 1432 | ||
1891 | /* We ignore the requested LBA and write out all file's | 1433 | /* We ignore the requested LBA and write out all file's |
1892 | * dirty data buffers. */ | 1434 | * dirty data buffers. */ |
1893 | rc = fsync_sub(curlun); | 1435 | rc = fsg_lun_fsync_sub(curlun); |
1894 | if (rc) | 1436 | if (rc) |
1895 | curlun->sense_data = SS_WRITE_ERROR; | 1437 | curlun->sense_data = SS_WRITE_ERROR; |
1896 | return 0; | 1438 | return 0; |
@@ -1899,19 +1441,19 @@ static int do_synchronize_cache(struct fsg_dev *fsg) | |||
1899 | 1441 | ||
1900 | /*-------------------------------------------------------------------------*/ | 1442 | /*-------------------------------------------------------------------------*/ |
1901 | 1443 | ||
1902 | static void invalidate_sub(struct lun *curlun) | 1444 | static void invalidate_sub(struct fsg_lun *curlun) |
1903 | { | 1445 | { |
1904 | struct file *filp = curlun->filp; | 1446 | struct file *filp = curlun->filp; |
1905 | struct inode *inode = filp->f_path.dentry->d_inode; | 1447 | struct inode *inode = filp->f_path.dentry->d_inode; |
1906 | unsigned long rc; | 1448 | unsigned long rc; |
1907 | 1449 | ||
1908 | rc = invalidate_mapping_pages(inode->i_mapping, 0, -1); | 1450 | rc = invalidate_mapping_pages(inode->i_mapping, 0, -1); |
1909 | VLDBG(curlun, "invalidate_inode_pages -> %ld\n", rc); | 1451 | VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc); |
1910 | } | 1452 | } |
1911 | 1453 | ||
1912 | static int do_verify(struct fsg_dev *fsg) | 1454 | static int do_verify(struct fsg_dev *fsg) |
1913 | { | 1455 | { |
1914 | struct lun *curlun = fsg->curlun; | 1456 | struct fsg_lun *curlun = fsg->curlun; |
1915 | u32 lba; | 1457 | u32 lba; |
1916 | u32 verification_length; | 1458 | u32 verification_length; |
1917 | struct fsg_buffhd *bh = fsg->next_buffhd_to_fill; | 1459 | struct fsg_buffhd *bh = fsg->next_buffhd_to_fill; |
@@ -1944,7 +1486,7 @@ static int do_verify(struct fsg_dev *fsg) | |||
1944 | file_offset = ((loff_t) lba) << 9; | 1486 | file_offset = ((loff_t) lba) << 9; |
1945 | 1487 | ||
1946 | /* Write out all the dirty buffers before invalidating them */ | 1488 | /* Write out all the dirty buffers before invalidating them */ |
1947 | fsync_sub(curlun); | 1489 | fsg_lun_fsync_sub(curlun); |
1948 | if (signal_pending(current)) | 1490 | if (signal_pending(current)) |
1949 | return -EINTR; | 1491 | return -EINTR; |
1950 | 1492 | ||
@@ -2041,7 +1583,7 @@ static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
2041 | 1583 | ||
2042 | static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1584 | static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
2043 | { | 1585 | { |
2044 | struct lun *curlun = fsg->curlun; | 1586 | struct fsg_lun *curlun = fsg->curlun; |
2045 | u8 *buf = (u8 *) bh->buf; | 1587 | u8 *buf = (u8 *) bh->buf; |
2046 | u32 sd, sdinfo; | 1588 | u32 sd, sdinfo; |
2047 | int valid; | 1589 | int valid; |
@@ -2095,7 +1637,7 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
2095 | 1637 | ||
2096 | static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1638 | static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
2097 | { | 1639 | { |
2098 | struct lun *curlun = fsg->curlun; | 1640 | struct fsg_lun *curlun = fsg->curlun; |
2099 | u32 lba = get_unaligned_be32(&fsg->cmnd[2]); | 1641 | u32 lba = get_unaligned_be32(&fsg->cmnd[2]); |
2100 | int pmi = fsg->cmnd[8]; | 1642 | int pmi = fsg->cmnd[8]; |
2101 | u8 *buf = (u8 *) bh->buf; | 1643 | u8 *buf = (u8 *) bh->buf; |
@@ -2113,27 +1655,9 @@ static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
2113 | } | 1655 | } |
2114 | 1656 | ||
2115 | 1657 | ||
2116 | static void store_cdrom_address(u8 *dest, int msf, u32 addr) | ||
2117 | { | ||
2118 | if (msf) { | ||
2119 | /* Convert to Minutes-Seconds-Frames */ | ||
2120 | addr >>= 2; /* Convert to 2048-byte frames */ | ||
2121 | addr += 2*75; /* Lead-in occupies 2 seconds */ | ||
2122 | dest[3] = addr % 75; /* Frames */ | ||
2123 | addr /= 75; | ||
2124 | dest[2] = addr % 60; /* Seconds */ | ||
2125 | addr /= 60; | ||
2126 | dest[1] = addr; /* Minutes */ | ||
2127 | dest[0] = 0; /* Reserved */ | ||
2128 | } else { | ||
2129 | /* Absolute sector */ | ||
2130 | put_unaligned_be32(addr, dest); | ||
2131 | } | ||
2132 | } | ||
2133 | |||
2134 | static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1658 | static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
2135 | { | 1659 | { |
2136 | struct lun *curlun = fsg->curlun; | 1660 | struct fsg_lun *curlun = fsg->curlun; |
2137 | int msf = fsg->cmnd[1] & 0x02; | 1661 | int msf = fsg->cmnd[1] & 0x02; |
2138 | u32 lba = get_unaligned_be32(&fsg->cmnd[2]); | 1662 | u32 lba = get_unaligned_be32(&fsg->cmnd[2]); |
2139 | u8 *buf = (u8 *) bh->buf; | 1663 | u8 *buf = (u8 *) bh->buf; |
@@ -2156,7 +1680,7 @@ static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
2156 | 1680 | ||
2157 | static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1681 | static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
2158 | { | 1682 | { |
2159 | struct lun *curlun = fsg->curlun; | 1683 | struct fsg_lun *curlun = fsg->curlun; |
2160 | int msf = fsg->cmnd[1] & 0x02; | 1684 | int msf = fsg->cmnd[1] & 0x02; |
2161 | int start_track = fsg->cmnd[6]; | 1685 | int start_track = fsg->cmnd[6]; |
2162 | u8 *buf = (u8 *) bh->buf; | 1686 | u8 *buf = (u8 *) bh->buf; |
@@ -2184,7 +1708,7 @@ static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
2184 | 1708 | ||
2185 | static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1709 | static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
2186 | { | 1710 | { |
2187 | struct lun *curlun = fsg->curlun; | 1711 | struct fsg_lun *curlun = fsg->curlun; |
2188 | int mscmnd = fsg->cmnd[0]; | 1712 | int mscmnd = fsg->cmnd[0]; |
2189 | u8 *buf = (u8 *) bh->buf; | 1713 | u8 *buf = (u8 *) bh->buf; |
2190 | u8 *buf0 = buf; | 1714 | u8 *buf0 = buf; |
@@ -2265,7 +1789,7 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
2265 | 1789 | ||
2266 | static int do_start_stop(struct fsg_dev *fsg) | 1790 | static int do_start_stop(struct fsg_dev *fsg) |
2267 | { | 1791 | { |
2268 | struct lun *curlun = fsg->curlun; | 1792 | struct fsg_lun *curlun = fsg->curlun; |
2269 | int loej, start; | 1793 | int loej, start; |
2270 | 1794 | ||
2271 | if (!mod_data.removable) { | 1795 | if (!mod_data.removable) { |
@@ -2295,7 +1819,7 @@ static int do_start_stop(struct fsg_dev *fsg) | |||
2295 | if (loej) { // Simulate an unload/eject | 1819 | if (loej) { // Simulate an unload/eject |
2296 | up_read(&fsg->filesem); | 1820 | up_read(&fsg->filesem); |
2297 | down_write(&fsg->filesem); | 1821 | down_write(&fsg->filesem); |
2298 | close_backing_file(curlun); | 1822 | fsg_lun_close(curlun); |
2299 | up_write(&fsg->filesem); | 1823 | up_write(&fsg->filesem); |
2300 | down_read(&fsg->filesem); | 1824 | down_read(&fsg->filesem); |
2301 | } | 1825 | } |
@@ -2303,7 +1827,7 @@ static int do_start_stop(struct fsg_dev *fsg) | |||
2303 | 1827 | ||
2304 | /* Our emulation doesn't support mounting; the medium is | 1828 | /* Our emulation doesn't support mounting; the medium is |
2305 | * available for use as soon as it is loaded. */ | 1829 | * available for use as soon as it is loaded. */ |
2306 | if (!backing_file_is_open(curlun)) { | 1830 | if (!fsg_lun_is_open(curlun)) { |
2307 | curlun->sense_data = SS_MEDIUM_NOT_PRESENT; | 1831 | curlun->sense_data = SS_MEDIUM_NOT_PRESENT; |
2308 | return -EINVAL; | 1832 | return -EINVAL; |
2309 | } | 1833 | } |
@@ -2315,7 +1839,7 @@ static int do_start_stop(struct fsg_dev *fsg) | |||
2315 | 1839 | ||
2316 | static int do_prevent_allow(struct fsg_dev *fsg) | 1840 | static int do_prevent_allow(struct fsg_dev *fsg) |
2317 | { | 1841 | { |
2318 | struct lun *curlun = fsg->curlun; | 1842 | struct fsg_lun *curlun = fsg->curlun; |
2319 | int prevent; | 1843 | int prevent; |
2320 | 1844 | ||
2321 | if (!mod_data.removable) { | 1845 | if (!mod_data.removable) { |
@@ -2330,7 +1854,7 @@ static int do_prevent_allow(struct fsg_dev *fsg) | |||
2330 | } | 1854 | } |
2331 | 1855 | ||
2332 | if (curlun->prevent_medium_removal && !prevent) | 1856 | if (curlun->prevent_medium_removal && !prevent) |
2333 | fsync_sub(curlun); | 1857 | fsg_lun_fsync_sub(curlun); |
2334 | curlun->prevent_medium_removal = prevent; | 1858 | curlun->prevent_medium_removal = prevent; |
2335 | return 0; | 1859 | return 0; |
2336 | } | 1860 | } |
@@ -2339,7 +1863,7 @@ static int do_prevent_allow(struct fsg_dev *fsg) | |||
2339 | static int do_read_format_capacities(struct fsg_dev *fsg, | 1863 | static int do_read_format_capacities(struct fsg_dev *fsg, |
2340 | struct fsg_buffhd *bh) | 1864 | struct fsg_buffhd *bh) |
2341 | { | 1865 | { |
2342 | struct lun *curlun = fsg->curlun; | 1866 | struct fsg_lun *curlun = fsg->curlun; |
2343 | u8 *buf = (u8 *) bh->buf; | 1867 | u8 *buf = (u8 *) bh->buf; |
2344 | 1868 | ||
2345 | buf[0] = buf[1] = buf[2] = 0; | 1869 | buf[0] = buf[1] = buf[2] = 0; |
@@ -2356,7 +1880,7 @@ static int do_read_format_capacities(struct fsg_dev *fsg, | |||
2356 | 1880 | ||
2357 | static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1881 | static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
2358 | { | 1882 | { |
2359 | struct lun *curlun = fsg->curlun; | 1883 | struct fsg_lun *curlun = fsg->curlun; |
2360 | 1884 | ||
2361 | /* We don't support MODE SELECT */ | 1885 | /* We don't support MODE SELECT */ |
2362 | curlun->sense_data = SS_INVALID_COMMAND; | 1886 | curlun->sense_data = SS_INVALID_COMMAND; |
@@ -2599,7 +2123,7 @@ static int finish_reply(struct fsg_dev *fsg) | |||
2599 | 2123 | ||
2600 | static int send_status(struct fsg_dev *fsg) | 2124 | static int send_status(struct fsg_dev *fsg) |
2601 | { | 2125 | { |
2602 | struct lun *curlun = fsg->curlun; | 2126 | struct fsg_lun *curlun = fsg->curlun; |
2603 | struct fsg_buffhd *bh; | 2127 | struct fsg_buffhd *bh; |
2604 | int rc; | 2128 | int rc; |
2605 | u8 status = USB_STATUS_PASS; | 2129 | u8 status = USB_STATUS_PASS; |
@@ -2691,7 +2215,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size, | |||
2691 | int lun = fsg->cmnd[1] >> 5; | 2215 | int lun = fsg->cmnd[1] >> 5; |
2692 | static const char dirletter[4] = {'u', 'o', 'i', 'n'}; | 2216 | static const char dirletter[4] = {'u', 'o', 'i', 'n'}; |
2693 | char hdlen[20]; | 2217 | char hdlen[20]; |
2694 | struct lun *curlun; | 2218 | struct fsg_lun *curlun; |
2695 | 2219 | ||
2696 | /* Adjust the expected cmnd_size for protocol encapsulation padding. | 2220 | /* Adjust the expected cmnd_size for protocol encapsulation padding. |
2697 | * Transparent SCSI doesn't pad. */ | 2221 | * Transparent SCSI doesn't pad. */ |
@@ -2820,7 +2344,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size, | |||
2820 | 2344 | ||
2821 | /* If the medium isn't mounted and the command needs to access | 2345 | /* If the medium isn't mounted and the command needs to access |
2822 | * it, return an error. */ | 2346 | * it, return an error. */ |
2823 | if (curlun && !backing_file_is_open(curlun) && needs_medium) { | 2347 | if (curlun && !fsg_lun_is_open(curlun) && needs_medium) { |
2824 | curlun->sense_data = SS_MEDIUM_NOT_PRESENT; | 2348 | curlun->sense_data = SS_MEDIUM_NOT_PRESENT; |
2825 | return -EINVAL; | 2349 | return -EINVAL; |
2826 | } | 2350 | } |
@@ -3075,8 +2599,8 @@ static int do_scsi_command(struct fsg_dev *fsg) | |||
3075 | 2599 | ||
3076 | static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 2600 | static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
3077 | { | 2601 | { |
3078 | struct usb_request *req = bh->outreq; | 2602 | struct usb_request *req = bh->outreq; |
3079 | struct bulk_cb_wrap *cbw = req->buf; | 2603 | struct fsg_bulk_cb_wrap *cbw = req->buf; |
3080 | 2604 | ||
3081 | /* Was this a real packet? Should it be ignored? */ | 2605 | /* Was this a real packet? Should it be ignored? */ |
3082 | if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags)) | 2606 | if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags)) |
@@ -3105,7 +2629,7 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
3105 | } | 2629 | } |
3106 | 2630 | ||
3107 | /* Is the CBW meaningful? */ | 2631 | /* Is the CBW meaningful? */ |
3108 | if (cbw->Lun >= MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG || | 2632 | if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG || |
3109 | cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) { | 2633 | cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) { |
3110 | DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, " | 2634 | DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, " |
3111 | "cmdlen %u\n", | 2635 | "cmdlen %u\n", |
@@ -3238,7 +2762,7 @@ static int do_set_interface(struct fsg_dev *fsg, int altsetting) | |||
3238 | 2762 | ||
3239 | reset: | 2763 | reset: |
3240 | /* Deallocate the requests */ | 2764 | /* Deallocate the requests */ |
3241 | for (i = 0; i < NUM_BUFFERS; ++i) { | 2765 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
3242 | struct fsg_buffhd *bh = &fsg->buffhds[i]; | 2766 | struct fsg_buffhd *bh = &fsg->buffhds[i]; |
3243 | 2767 | ||
3244 | if (bh->inreq) { | 2768 | if (bh->inreq) { |
@@ -3276,12 +2800,14 @@ reset: | |||
3276 | DBG(fsg, "set interface %d\n", altsetting); | 2800 | DBG(fsg, "set interface %d\n", altsetting); |
3277 | 2801 | ||
3278 | /* Enable the endpoints */ | 2802 | /* Enable the endpoints */ |
3279 | d = ep_desc(fsg->gadget, &fs_bulk_in_desc, &hs_bulk_in_desc); | 2803 | d = fsg_ep_desc(fsg->gadget, |
2804 | &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc); | ||
3280 | if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0) | 2805 | if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0) |
3281 | goto reset; | 2806 | goto reset; |
3282 | fsg->bulk_in_enabled = 1; | 2807 | fsg->bulk_in_enabled = 1; |
3283 | 2808 | ||
3284 | d = ep_desc(fsg->gadget, &fs_bulk_out_desc, &hs_bulk_out_desc); | 2809 | d = fsg_ep_desc(fsg->gadget, |
2810 | &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc); | ||
3285 | if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0) | 2811 | if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0) |
3286 | goto reset; | 2812 | goto reset; |
3287 | fsg->bulk_out_enabled = 1; | 2813 | fsg->bulk_out_enabled = 1; |
@@ -3289,14 +2815,15 @@ reset: | |||
3289 | clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); | 2815 | clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); |
3290 | 2816 | ||
3291 | if (transport_is_cbi()) { | 2817 | if (transport_is_cbi()) { |
3292 | d = ep_desc(fsg->gadget, &fs_intr_in_desc, &hs_intr_in_desc); | 2818 | d = fsg_ep_desc(fsg->gadget, |
2819 | &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc); | ||
3293 | if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0) | 2820 | if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0) |
3294 | goto reset; | 2821 | goto reset; |
3295 | fsg->intr_in_enabled = 1; | 2822 | fsg->intr_in_enabled = 1; |
3296 | } | 2823 | } |
3297 | 2824 | ||
3298 | /* Allocate the requests */ | 2825 | /* Allocate the requests */ |
3299 | for (i = 0; i < NUM_BUFFERS; ++i) { | 2826 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
3300 | struct fsg_buffhd *bh = &fsg->buffhds[i]; | 2827 | struct fsg_buffhd *bh = &fsg->buffhds[i]; |
3301 | 2828 | ||
3302 | if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0) | 2829 | if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0) |
@@ -3372,7 +2899,7 @@ static void handle_exception(struct fsg_dev *fsg) | |||
3372 | struct fsg_buffhd *bh; | 2899 | struct fsg_buffhd *bh; |
3373 | enum fsg_state old_state; | 2900 | enum fsg_state old_state; |
3374 | u8 new_config; | 2901 | u8 new_config; |
3375 | struct lun *curlun; | 2902 | struct fsg_lun *curlun; |
3376 | unsigned int exception_req_tag; | 2903 | unsigned int exception_req_tag; |
3377 | int rc; | 2904 | int rc; |
3378 | 2905 | ||
@@ -3392,7 +2919,7 @@ static void handle_exception(struct fsg_dev *fsg) | |||
3392 | /* Cancel all the pending transfers */ | 2919 | /* Cancel all the pending transfers */ |
3393 | if (fsg->intreq_busy) | 2920 | if (fsg->intreq_busy) |
3394 | usb_ep_dequeue(fsg->intr_in, fsg->intreq); | 2921 | usb_ep_dequeue(fsg->intr_in, fsg->intreq); |
3395 | for (i = 0; i < NUM_BUFFERS; ++i) { | 2922 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
3396 | bh = &fsg->buffhds[i]; | 2923 | bh = &fsg->buffhds[i]; |
3397 | if (bh->inreq_busy) | 2924 | if (bh->inreq_busy) |
3398 | usb_ep_dequeue(fsg->bulk_in, bh->inreq); | 2925 | usb_ep_dequeue(fsg->bulk_in, bh->inreq); |
@@ -3403,7 +2930,7 @@ static void handle_exception(struct fsg_dev *fsg) | |||
3403 | /* Wait until everything is idle */ | 2930 | /* Wait until everything is idle */ |
3404 | for (;;) { | 2931 | for (;;) { |
3405 | num_active = fsg->intreq_busy; | 2932 | num_active = fsg->intreq_busy; |
3406 | for (i = 0; i < NUM_BUFFERS; ++i) { | 2933 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
3407 | bh = &fsg->buffhds[i]; | 2934 | bh = &fsg->buffhds[i]; |
3408 | num_active += bh->inreq_busy + bh->outreq_busy; | 2935 | num_active += bh->inreq_busy + bh->outreq_busy; |
3409 | } | 2936 | } |
@@ -3425,7 +2952,7 @@ static void handle_exception(struct fsg_dev *fsg) | |||
3425 | * state, and the exception. Then invoke the handler. */ | 2952 | * state, and the exception. Then invoke the handler. */ |
3426 | spin_lock_irq(&fsg->lock); | 2953 | spin_lock_irq(&fsg->lock); |
3427 | 2954 | ||
3428 | for (i = 0; i < NUM_BUFFERS; ++i) { | 2955 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
3429 | bh = &fsg->buffhds[i]; | 2956 | bh = &fsg->buffhds[i]; |
3430 | bh->state = BUF_STATE_EMPTY; | 2957 | bh->state = BUF_STATE_EMPTY; |
3431 | } | 2958 | } |
@@ -3506,7 +3033,8 @@ static void handle_exception(struct fsg_dev *fsg) | |||
3506 | break; | 3033 | break; |
3507 | 3034 | ||
3508 | case FSG_STATE_DISCONNECT: | 3035 | case FSG_STATE_DISCONNECT: |
3509 | fsync_all(fsg); | 3036 | for (i = 0; i < fsg->nluns; ++i) |
3037 | fsg_lun_fsync_sub(fsg->luns + i); | ||
3510 | do_set_config(fsg, 0); // Unconfigured state | 3038 | do_set_config(fsg, 0); // Unconfigured state |
3511 | break; | 3039 | break; |
3512 | 3040 | ||
@@ -3595,201 +3123,10 @@ static int fsg_main_thread(void *fsg_) | |||
3595 | 3123 | ||
3596 | /*-------------------------------------------------------------------------*/ | 3124 | /*-------------------------------------------------------------------------*/ |
3597 | 3125 | ||
3598 | /* If the next two routines are called while the gadget is registered, | ||
3599 | * the caller must own fsg->filesem for writing. */ | ||
3600 | |||
3601 | static int open_backing_file(struct lun *curlun, const char *filename) | ||
3602 | { | ||
3603 | int ro; | ||
3604 | struct file *filp = NULL; | ||
3605 | int rc = -EINVAL; | ||
3606 | struct inode *inode = NULL; | ||
3607 | loff_t size; | ||
3608 | loff_t num_sectors; | ||
3609 | loff_t min_sectors; | ||
3610 | |||
3611 | /* R/W if we can, R/O if we must */ | ||
3612 | ro = curlun->ro; | ||
3613 | if (!ro) { | ||
3614 | filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0); | ||
3615 | if (-EROFS == PTR_ERR(filp)) | ||
3616 | ro = 1; | ||
3617 | } | ||
3618 | if (ro) | ||
3619 | filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0); | ||
3620 | if (IS_ERR(filp)) { | ||
3621 | LINFO(curlun, "unable to open backing file: %s\n", filename); | ||
3622 | return PTR_ERR(filp); | ||
3623 | } | ||
3624 | |||
3625 | if (!(filp->f_mode & FMODE_WRITE)) | ||
3626 | ro = 1; | ||
3627 | |||
3628 | if (filp->f_path.dentry) | ||
3629 | inode = filp->f_path.dentry->d_inode; | ||
3630 | if (inode && S_ISBLK(inode->i_mode)) { | ||
3631 | if (bdev_read_only(inode->i_bdev)) | ||
3632 | ro = 1; | ||
3633 | } else if (!inode || !S_ISREG(inode->i_mode)) { | ||
3634 | LINFO(curlun, "invalid file type: %s\n", filename); | ||
3635 | goto out; | ||
3636 | } | ||
3637 | |||
3638 | /* If we can't read the file, it's no good. | ||
3639 | * If we can't write the file, use it read-only. */ | ||
3640 | if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) { | ||
3641 | LINFO(curlun, "file not readable: %s\n", filename); | ||
3642 | goto out; | ||
3643 | } | ||
3644 | if (!(filp->f_op->write || filp->f_op->aio_write)) | ||
3645 | ro = 1; | ||
3646 | |||
3647 | size = i_size_read(inode->i_mapping->host); | ||
3648 | if (size < 0) { | ||
3649 | LINFO(curlun, "unable to find file size: %s\n", filename); | ||
3650 | rc = (int) size; | ||
3651 | goto out; | ||
3652 | } | ||
3653 | num_sectors = size >> 9; // File size in 512-byte blocks | ||
3654 | min_sectors = 1; | ||
3655 | if (mod_data.cdrom) { | ||
3656 | num_sectors &= ~3; // Reduce to a multiple of 2048 | ||
3657 | min_sectors = 300*4; // Smallest track is 300 frames | ||
3658 | if (num_sectors >= 256*60*75*4) { | ||
3659 | num_sectors = (256*60*75 - 1) * 4; | ||
3660 | LINFO(curlun, "file too big: %s\n", filename); | ||
3661 | LINFO(curlun, "using only first %d blocks\n", | ||
3662 | (int) num_sectors); | ||
3663 | } | ||
3664 | } | ||
3665 | if (num_sectors < min_sectors) { | ||
3666 | LINFO(curlun, "file too small: %s\n", filename); | ||
3667 | rc = -ETOOSMALL; | ||
3668 | goto out; | ||
3669 | } | ||
3670 | |||
3671 | get_file(filp); | ||
3672 | curlun->ro = ro; | ||
3673 | curlun->filp = filp; | ||
3674 | curlun->file_length = size; | ||
3675 | curlun->num_sectors = num_sectors; | ||
3676 | LDBG(curlun, "open backing file: %s\n", filename); | ||
3677 | rc = 0; | ||
3678 | |||
3679 | out: | ||
3680 | filp_close(filp, current->files); | ||
3681 | return rc; | ||
3682 | } | ||
3683 | |||
3684 | |||
3685 | static void close_backing_file(struct lun *curlun) | ||
3686 | { | ||
3687 | if (curlun->filp) { | ||
3688 | LDBG(curlun, "close backing file\n"); | ||
3689 | fput(curlun->filp); | ||
3690 | curlun->filp = NULL; | ||
3691 | } | ||
3692 | } | ||
3693 | |||
3694 | |||
3695 | static ssize_t show_ro(struct device *dev, struct device_attribute *attr, char *buf) | ||
3696 | { | ||
3697 | struct lun *curlun = dev_to_lun(dev); | ||
3698 | |||
3699 | return sprintf(buf, "%d\n", curlun->ro); | ||
3700 | } | ||
3701 | |||
3702 | static ssize_t show_file(struct device *dev, struct device_attribute *attr, | ||
3703 | char *buf) | ||
3704 | { | ||
3705 | struct lun *curlun = dev_to_lun(dev); | ||
3706 | struct fsg_dev *fsg = dev_get_drvdata(dev); | ||
3707 | char *p; | ||
3708 | ssize_t rc; | ||
3709 | |||
3710 | down_read(&fsg->filesem); | ||
3711 | if (backing_file_is_open(curlun)) { // Get the complete pathname | ||
3712 | p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1); | ||
3713 | if (IS_ERR(p)) | ||
3714 | rc = PTR_ERR(p); | ||
3715 | else { | ||
3716 | rc = strlen(p); | ||
3717 | memmove(buf, p, rc); | ||
3718 | buf[rc] = '\n'; // Add a newline | ||
3719 | buf[++rc] = 0; | ||
3720 | } | ||
3721 | } else { // No file, return 0 bytes | ||
3722 | *buf = 0; | ||
3723 | rc = 0; | ||
3724 | } | ||
3725 | up_read(&fsg->filesem); | ||
3726 | return rc; | ||
3727 | } | ||
3728 | |||
3729 | |||
3730 | static ssize_t store_ro(struct device *dev, struct device_attribute *attr, | ||
3731 | const char *buf, size_t count) | ||
3732 | { | ||
3733 | ssize_t rc = count; | ||
3734 | struct lun *curlun = dev_to_lun(dev); | ||
3735 | struct fsg_dev *fsg = dev_get_drvdata(dev); | ||
3736 | int i; | ||
3737 | |||
3738 | if (sscanf(buf, "%d", &i) != 1) | ||
3739 | return -EINVAL; | ||
3740 | |||
3741 | /* Allow the write-enable status to change only while the backing file | ||
3742 | * is closed. */ | ||
3743 | down_read(&fsg->filesem); | ||
3744 | if (backing_file_is_open(curlun)) { | ||
3745 | LDBG(curlun, "read-only status change prevented\n"); | ||
3746 | rc = -EBUSY; | ||
3747 | } else { | ||
3748 | curlun->ro = !!i; | ||
3749 | LDBG(curlun, "read-only status set to %d\n", curlun->ro); | ||
3750 | } | ||
3751 | up_read(&fsg->filesem); | ||
3752 | return rc; | ||
3753 | } | ||
3754 | |||
3755 | static ssize_t store_file(struct device *dev, struct device_attribute *attr, | ||
3756 | const char *buf, size_t count) | ||
3757 | { | ||
3758 | struct lun *curlun = dev_to_lun(dev); | ||
3759 | struct fsg_dev *fsg = dev_get_drvdata(dev); | ||
3760 | int rc = 0; | ||
3761 | |||
3762 | if (curlun->prevent_medium_removal && backing_file_is_open(curlun)) { | ||
3763 | LDBG(curlun, "eject attempt prevented\n"); | ||
3764 | return -EBUSY; // "Door is locked" | ||
3765 | } | ||
3766 | |||
3767 | /* Remove a trailing newline */ | ||
3768 | if (count > 0 && buf[count-1] == '\n') | ||
3769 | ((char *) buf)[count-1] = 0; // Ugh! | ||
3770 | |||
3771 | /* Eject current medium */ | ||
3772 | down_write(&fsg->filesem); | ||
3773 | if (backing_file_is_open(curlun)) { | ||
3774 | close_backing_file(curlun); | ||
3775 | curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT; | ||
3776 | } | ||
3777 | |||
3778 | /* Load new medium */ | ||
3779 | if (count > 0 && buf[0]) { | ||
3780 | rc = open_backing_file(curlun, buf); | ||
3781 | if (rc == 0) | ||
3782 | curlun->unit_attention_data = | ||
3783 | SS_NOT_READY_TO_READY_TRANSITION; | ||
3784 | } | ||
3785 | up_write(&fsg->filesem); | ||
3786 | return (rc < 0 ? rc : count); | ||
3787 | } | ||
3788 | |||
3789 | 3126 | ||
3790 | /* The write permissions and store_xxx pointers are set in fsg_bind() */ | 3127 | /* The write permissions and store_xxx pointers are set in fsg_bind() */ |
3791 | static DEVICE_ATTR(ro, 0444, show_ro, NULL); | 3128 | static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL); |
3792 | static DEVICE_ATTR(file, 0444, show_file, NULL); | 3129 | static DEVICE_ATTR(file, 0444, fsg_show_file, NULL); |
3793 | 3130 | ||
3794 | 3131 | ||
3795 | /*-------------------------------------------------------------------------*/ | 3132 | /*-------------------------------------------------------------------------*/ |
@@ -3804,7 +3141,9 @@ static void fsg_release(struct kref *ref) | |||
3804 | 3141 | ||
3805 | static void lun_release(struct device *dev) | 3142 | static void lun_release(struct device *dev) |
3806 | { | 3143 | { |
3807 | struct fsg_dev *fsg = dev_get_drvdata(dev); | 3144 | struct rw_semaphore *filesem = dev_get_drvdata(dev); |
3145 | struct fsg_dev *fsg = | ||
3146 | container_of(filesem, struct fsg_dev, filesem); | ||
3808 | 3147 | ||
3809 | kref_put(&fsg->ref, fsg_release); | 3148 | kref_put(&fsg->ref, fsg_release); |
3810 | } | 3149 | } |
@@ -3813,7 +3152,7 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget) | |||
3813 | { | 3152 | { |
3814 | struct fsg_dev *fsg = get_gadget_data(gadget); | 3153 | struct fsg_dev *fsg = get_gadget_data(gadget); |
3815 | int i; | 3154 | int i; |
3816 | struct lun *curlun; | 3155 | struct fsg_lun *curlun; |
3817 | struct usb_request *req = fsg->ep0req; | 3156 | struct usb_request *req = fsg->ep0req; |
3818 | 3157 | ||
3819 | DBG(fsg, "unbind\n"); | 3158 | DBG(fsg, "unbind\n"); |
@@ -3825,7 +3164,7 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget) | |||
3825 | if (curlun->registered) { | 3164 | if (curlun->registered) { |
3826 | device_remove_file(&curlun->dev, &dev_attr_ro); | 3165 | device_remove_file(&curlun->dev, &dev_attr_ro); |
3827 | device_remove_file(&curlun->dev, &dev_attr_file); | 3166 | device_remove_file(&curlun->dev, &dev_attr_file); |
3828 | close_backing_file(curlun); | 3167 | fsg_lun_close(curlun); |
3829 | device_unregister(&curlun->dev); | 3168 | device_unregister(&curlun->dev); |
3830 | curlun->registered = 0; | 3169 | curlun->registered = 0; |
3831 | } | 3170 | } |
@@ -3841,7 +3180,7 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget) | |||
3841 | } | 3180 | } |
3842 | 3181 | ||
3843 | /* Free the data buffers */ | 3182 | /* Free the data buffers */ |
3844 | for (i = 0; i < NUM_BUFFERS; ++i) | 3183 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) |
3845 | kfree(fsg->buffhds[i].buf); | 3184 | kfree(fsg->buffhds[i].buf); |
3846 | 3185 | ||
3847 | /* Free the request and buffer for endpoint 0 */ | 3186 | /* Free the request and buffer for endpoint 0 */ |
@@ -3869,15 +3208,11 @@ static int __init check_parameters(struct fsg_dev *fsg) | |||
3869 | * halt bulk endpoints correctly. If one of them is present, | 3208 | * halt bulk endpoints correctly. If one of them is present, |
3870 | * disable stalls. | 3209 | * disable stalls. |
3871 | */ | 3210 | */ |
3872 | if (gadget_is_sh(fsg->gadget) || gadget_is_at91(fsg->gadget)) | 3211 | if (gadget_is_at91(fsg->gadget)) |
3873 | mod_data.can_stall = 0; | 3212 | mod_data.can_stall = 0; |
3874 | 3213 | ||
3875 | if (mod_data.release == 0xffff) { // Parameter wasn't set | 3214 | if (mod_data.release == 0xffff) { // Parameter wasn't set |
3876 | /* The sa1100 controller is not supported */ | 3215 | gcnum = usb_gadget_controller_number(fsg->gadget); |
3877 | if (gadget_is_sa1100(fsg->gadget)) | ||
3878 | gcnum = -1; | ||
3879 | else | ||
3880 | gcnum = usb_gadget_controller_number(fsg->gadget); | ||
3881 | if (gcnum >= 0) | 3216 | if (gcnum >= 0) |
3882 | mod_data.release = 0x0300 + gcnum; | 3217 | mod_data.release = 0x0300 + gcnum; |
3883 | else { | 3218 | else { |
@@ -3948,7 +3283,7 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
3948 | struct fsg_dev *fsg = the_fsg; | 3283 | struct fsg_dev *fsg = the_fsg; |
3949 | int rc; | 3284 | int rc; |
3950 | int i; | 3285 | int i; |
3951 | struct lun *curlun; | 3286 | struct fsg_lun *curlun; |
3952 | struct usb_ep *ep; | 3287 | struct usb_ep *ep; |
3953 | struct usb_request *req; | 3288 | struct usb_request *req; |
3954 | char *pathbuf, *p; | 3289 | char *pathbuf, *p; |
@@ -3963,10 +3298,10 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
3963 | 3298 | ||
3964 | if (mod_data.removable) { // Enable the store_xxx attributes | 3299 | if (mod_data.removable) { // Enable the store_xxx attributes |
3965 | dev_attr_file.attr.mode = 0644; | 3300 | dev_attr_file.attr.mode = 0644; |
3966 | dev_attr_file.store = store_file; | 3301 | dev_attr_file.store = fsg_store_file; |
3967 | if (!mod_data.cdrom) { | 3302 | if (!mod_data.cdrom) { |
3968 | dev_attr_ro.attr.mode = 0644; | 3303 | dev_attr_ro.attr.mode = 0644; |
3969 | dev_attr_ro.store = store_ro; | 3304 | dev_attr_ro.store = fsg_store_ro; |
3970 | } | 3305 | } |
3971 | } | 3306 | } |
3972 | 3307 | ||
@@ -3974,7 +3309,7 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
3974 | i = mod_data.nluns; | 3309 | i = mod_data.nluns; |
3975 | if (i == 0) | 3310 | if (i == 0) |
3976 | i = max(mod_data.num_filenames, 1u); | 3311 | i = max(mod_data.num_filenames, 1u); |
3977 | if (i > MAX_LUNS) { | 3312 | if (i > FSG_MAX_LUNS) { |
3978 | ERROR(fsg, "invalid number of LUNs: %d\n", i); | 3313 | ERROR(fsg, "invalid number of LUNs: %d\n", i); |
3979 | rc = -EINVAL; | 3314 | rc = -EINVAL; |
3980 | goto out; | 3315 | goto out; |
@@ -3982,7 +3317,7 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
3982 | 3317 | ||
3983 | /* Create the LUNs, open their backing files, and register the | 3318 | /* Create the LUNs, open their backing files, and register the |
3984 | * LUN devices in sysfs. */ | 3319 | * LUN devices in sysfs. */ |
3985 | fsg->luns = kzalloc(i * sizeof(struct lun), GFP_KERNEL); | 3320 | fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL); |
3986 | if (!fsg->luns) { | 3321 | if (!fsg->luns) { |
3987 | rc = -ENOMEM; | 3322 | rc = -ENOMEM; |
3988 | goto out; | 3323 | goto out; |
@@ -3991,13 +3326,14 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
3991 | 3326 | ||
3992 | for (i = 0; i < fsg->nluns; ++i) { | 3327 | for (i = 0; i < fsg->nluns; ++i) { |
3993 | curlun = &fsg->luns[i]; | 3328 | curlun = &fsg->luns[i]; |
3994 | curlun->ro = mod_data.ro[i]; | 3329 | curlun->cdrom = !!mod_data.cdrom; |
3995 | if (mod_data.cdrom) | 3330 | curlun->ro = mod_data.cdrom || mod_data.ro[i]; |
3996 | curlun->ro = 1; | 3331 | curlun->initially_ro = curlun->ro; |
3332 | curlun->removable = mod_data.removable; | ||
3997 | curlun->dev.release = lun_release; | 3333 | curlun->dev.release = lun_release; |
3998 | curlun->dev.parent = &gadget->dev; | 3334 | curlun->dev.parent = &gadget->dev; |
3999 | curlun->dev.driver = &fsg_driver.driver; | 3335 | curlun->dev.driver = &fsg_driver.driver; |
4000 | dev_set_drvdata(&curlun->dev, fsg); | 3336 | dev_set_drvdata(&curlun->dev, &fsg->filesem); |
4001 | dev_set_name(&curlun->dev,"%s-lun%d", | 3337 | dev_set_name(&curlun->dev,"%s-lun%d", |
4002 | dev_name(&gadget->dev), i); | 3338 | dev_name(&gadget->dev), i); |
4003 | 3339 | ||
@@ -4016,7 +3352,7 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
4016 | kref_get(&fsg->ref); | 3352 | kref_get(&fsg->ref); |
4017 | 3353 | ||
4018 | if (mod_data.file[i] && *mod_data.file[i]) { | 3354 | if (mod_data.file[i] && *mod_data.file[i]) { |
4019 | if ((rc = open_backing_file(curlun, | 3355 | if ((rc = fsg_lun_open(curlun, |
4020 | mod_data.file[i])) != 0) | 3356 | mod_data.file[i])) != 0) |
4021 | goto out; | 3357 | goto out; |
4022 | } else if (!mod_data.removable) { | 3358 | } else if (!mod_data.removable) { |
@@ -4028,20 +3364,20 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
4028 | 3364 | ||
4029 | /* Find all the endpoints we will use */ | 3365 | /* Find all the endpoints we will use */ |
4030 | usb_ep_autoconfig_reset(gadget); | 3366 | usb_ep_autoconfig_reset(gadget); |
4031 | ep = usb_ep_autoconfig(gadget, &fs_bulk_in_desc); | 3367 | ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc); |
4032 | if (!ep) | 3368 | if (!ep) |
4033 | goto autoconf_fail; | 3369 | goto autoconf_fail; |
4034 | ep->driver_data = fsg; // claim the endpoint | 3370 | ep->driver_data = fsg; // claim the endpoint |
4035 | fsg->bulk_in = ep; | 3371 | fsg->bulk_in = ep; |
4036 | 3372 | ||
4037 | ep = usb_ep_autoconfig(gadget, &fs_bulk_out_desc); | 3373 | ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc); |
4038 | if (!ep) | 3374 | if (!ep) |
4039 | goto autoconf_fail; | 3375 | goto autoconf_fail; |
4040 | ep->driver_data = fsg; // claim the endpoint | 3376 | ep->driver_data = fsg; // claim the endpoint |
4041 | fsg->bulk_out = ep; | 3377 | fsg->bulk_out = ep; |
4042 | 3378 | ||
4043 | if (transport_is_cbi()) { | 3379 | if (transport_is_cbi()) { |
4044 | ep = usb_ep_autoconfig(gadget, &fs_intr_in_desc); | 3380 | ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc); |
4045 | if (!ep) | 3381 | if (!ep) |
4046 | goto autoconf_fail; | 3382 | goto autoconf_fail; |
4047 | ep->driver_data = fsg; // claim the endpoint | 3383 | ep->driver_data = fsg; // claim the endpoint |
@@ -4055,28 +3391,28 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
4055 | device_desc.bcdDevice = cpu_to_le16(mod_data.release); | 3391 | device_desc.bcdDevice = cpu_to_le16(mod_data.release); |
4056 | 3392 | ||
4057 | i = (transport_is_cbi() ? 3 : 2); // Number of endpoints | 3393 | i = (transport_is_cbi() ? 3 : 2); // Number of endpoints |
4058 | intf_desc.bNumEndpoints = i; | 3394 | fsg_intf_desc.bNumEndpoints = i; |
4059 | intf_desc.bInterfaceSubClass = mod_data.protocol_type; | 3395 | fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type; |
4060 | intf_desc.bInterfaceProtocol = mod_data.transport_type; | 3396 | fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type; |
4061 | fs_function[i + FS_FUNCTION_PRE_EP_ENTRIES] = NULL; | 3397 | fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL; |
4062 | 3398 | ||
4063 | if (gadget_is_dualspeed(gadget)) { | 3399 | if (gadget_is_dualspeed(gadget)) { |
4064 | hs_function[i + HS_FUNCTION_PRE_EP_ENTRIES] = NULL; | 3400 | fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL; |
4065 | 3401 | ||
4066 | /* Assume ep0 uses the same maxpacket value for both speeds */ | 3402 | /* Assume ep0 uses the same maxpacket value for both speeds */ |
4067 | dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket; | 3403 | dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket; |
4068 | 3404 | ||
4069 | /* Assume endpoint addresses are the same for both speeds */ | 3405 | /* Assume endpoint addresses are the same for both speeds */ |
4070 | hs_bulk_in_desc.bEndpointAddress = | 3406 | fsg_hs_bulk_in_desc.bEndpointAddress = |
4071 | fs_bulk_in_desc.bEndpointAddress; | 3407 | fsg_fs_bulk_in_desc.bEndpointAddress; |
4072 | hs_bulk_out_desc.bEndpointAddress = | 3408 | fsg_hs_bulk_out_desc.bEndpointAddress = |
4073 | fs_bulk_out_desc.bEndpointAddress; | 3409 | fsg_fs_bulk_out_desc.bEndpointAddress; |
4074 | hs_intr_in_desc.bEndpointAddress = | 3410 | fsg_hs_intr_in_desc.bEndpointAddress = |
4075 | fs_intr_in_desc.bEndpointAddress; | 3411 | fsg_fs_intr_in_desc.bEndpointAddress; |
4076 | } | 3412 | } |
4077 | 3413 | ||
4078 | if (gadget_is_otg(gadget)) | 3414 | if (gadget_is_otg(gadget)) |
4079 | otg_desc.bmAttributes |= USB_OTG_HNP; | 3415 | fsg_otg_desc.bmAttributes |= USB_OTG_HNP; |
4080 | 3416 | ||
4081 | rc = -ENOMEM; | 3417 | rc = -ENOMEM; |
4082 | 3418 | ||
@@ -4090,7 +3426,7 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
4090 | req->complete = ep0_complete; | 3426 | req->complete = ep0_complete; |
4091 | 3427 | ||
4092 | /* Allocate the data buffers */ | 3428 | /* Allocate the data buffers */ |
4093 | for (i = 0; i < NUM_BUFFERS; ++i) { | 3429 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
4094 | struct fsg_buffhd *bh = &fsg->buffhds[i]; | 3430 | struct fsg_buffhd *bh = &fsg->buffhds[i]; |
4095 | 3431 | ||
4096 | /* Allocate for the bulk-in endpoint. We assume that | 3432 | /* Allocate for the bulk-in endpoint. We assume that |
@@ -4101,23 +3437,24 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
4101 | goto out; | 3437 | goto out; |
4102 | bh->next = bh + 1; | 3438 | bh->next = bh + 1; |
4103 | } | 3439 | } |
4104 | fsg->buffhds[NUM_BUFFERS - 1].next = &fsg->buffhds[0]; | 3440 | fsg->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->buffhds[0]; |
4105 | 3441 | ||
4106 | /* This should reflect the actual gadget power source */ | 3442 | /* This should reflect the actual gadget power source */ |
4107 | usb_gadget_set_selfpowered(gadget); | 3443 | usb_gadget_set_selfpowered(gadget); |
4108 | 3444 | ||
4109 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", | 3445 | snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer, |
3446 | "%s %s with %s", | ||
4110 | init_utsname()->sysname, init_utsname()->release, | 3447 | init_utsname()->sysname, init_utsname()->release, |
4111 | gadget->name); | 3448 | gadget->name); |
4112 | 3449 | ||
4113 | /* On a real device, serial[] would be loaded from permanent | 3450 | /* On a real device, serial[] would be loaded from permanent |
4114 | * storage. We just encode it from the driver version string. */ | 3451 | * storage. We just encode it from the driver version string. */ |
4115 | for (i = 0; i < sizeof(serial) - 2; i += 2) { | 3452 | for (i = 0; i < sizeof fsg_string_serial - 2; i += 2) { |
4116 | unsigned char c = DRIVER_VERSION[i / 2]; | 3453 | unsigned char c = DRIVER_VERSION[i / 2]; |
4117 | 3454 | ||
4118 | if (!c) | 3455 | if (!c) |
4119 | break; | 3456 | break; |
4120 | sprintf(&serial[i], "%02X", c); | 3457 | sprintf(&fsg_string_serial[i], "%02X", c); |
4121 | } | 3458 | } |
4122 | 3459 | ||
4123 | fsg->thread_task = kthread_create(fsg_main_thread, fsg, | 3460 | fsg->thread_task = kthread_create(fsg_main_thread, fsg, |
@@ -4133,7 +3470,7 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
4133 | pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); | 3470 | pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); |
4134 | for (i = 0; i < fsg->nluns; ++i) { | 3471 | for (i = 0; i < fsg->nluns; ++i) { |
4135 | curlun = &fsg->luns[i]; | 3472 | curlun = &fsg->luns[i]; |
4136 | if (backing_file_is_open(curlun)) { | 3473 | if (fsg_lun_is_open(curlun)) { |
4137 | p = NULL; | 3474 | p = NULL; |
4138 | if (pathbuf) { | 3475 | if (pathbuf) { |
4139 | p = d_path(&curlun->filp->f_path, | 3476 | p = d_path(&curlun->filp->f_path, |
@@ -4203,7 +3540,7 @@ static struct usb_gadget_driver fsg_driver = { | |||
4203 | #else | 3540 | #else |
4204 | .speed = USB_SPEED_FULL, | 3541 | .speed = USB_SPEED_FULL, |
4205 | #endif | 3542 | #endif |
4206 | .function = (char *) longname, | 3543 | .function = (char *) fsg_string_product, |
4207 | .bind = fsg_bind, | 3544 | .bind = fsg_bind, |
4208 | .unbind = fsg_unbind, | 3545 | .unbind = fsg_unbind, |
4209 | .disconnect = fsg_disconnect, | 3546 | .disconnect = fsg_disconnect, |
@@ -4212,7 +3549,7 @@ static struct usb_gadget_driver fsg_driver = { | |||
4212 | .resume = fsg_resume, | 3549 | .resume = fsg_resume, |
4213 | 3550 | ||
4214 | .driver = { | 3551 | .driver = { |
4215 | .name = (char *) shortname, | 3552 | .name = DRIVER_NAME, |
4216 | .owner = THIS_MODULE, | 3553 | .owner = THIS_MODULE, |
4217 | // .release = ... | 3554 | // .release = ... |
4218 | // .suspend = ... | 3555 | // .suspend = ... |