aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
authorMichal Nazarewicz <m.nazarewicz@samsung.com>2009-11-09 08:15:23 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2009-12-11 14:55:22 -0500
commitd26a6aa08b9f12b44fb1ee65625e7480d3d5bb81 (patch)
tree9c86cc33a03c6ea0f9f7e6d2453b84b973e936fa /drivers/usb/gadget
parente8b6f8c5aa001235423878ef70bda919652e10be (diff)
USB: g_mass_storage: code cleaned up and comments updated
Fixed most of the errors and warnings in f_mass_storage.c and storage_common.c reported by checkpatch.pl as well as updated comments. Signed-off-by: Michal Nazarewicz <m.nazarewicz@samsung.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/f_mass_storage.c567
-rw-r--r--drivers/usb/gadget/mass_storage.c6
-rw-r--r--drivers/usb/gadget/storage_common.c108
3 files changed, 387 insertions, 294 deletions
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index 837aab624a4..5eaf22db7fc 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -1,7 +1,9 @@
1/* 1/*
2 * file_storage.c -- File-backed USB Storage Gadget, for USB development 2 * f_mass_storage.c -- Mass Storage USB Composite Function
3 * 3 *
4 * Copyright (C) 2003-2008 Alan Stern 4 * Copyright (C) 2003-2008 Alan Stern
5 * Copyright (C) 2009 Samsung Electronics
6 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
5 * All rights reserved. 7 * All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
@@ -37,37 +39,112 @@
37 39
38 40
39/* 41/*
40 * The File-backed Storage Gadget acts as a USB Mass Storage device, 42 * The Mass Storage Function acts as a USB Mass Storage device,
41 * appearing to the host as a disk drive or as a CD-ROM drive. In addition 43 * appearing to the host as a disk drive or as a CD-ROM drive. In
42 * to providing an example of a genuinely useful gadget driver for a USB 44 * addition to providing an example of a genuinely useful composite
43 * device, it also illustrates a technique of double-buffering for increased 45 * function for a USB device, it also illustrates a technique of
44 * throughput. Last but not least, it gives an easy way to probe the 46 * double-buffering for increased throughput.
45 * behavior of the Mass Storage drivers in a USB host.
46 * 47 *
47 * Backing storage is provided by a regular file or a block device, specified 48 * Function supports multiple logical units (LUNs). Backing storage
48 * by the "file" module parameter. Access can be limited to read-only by 49 * for each LUN is provided by a regular file or a block device.
49 * setting the optional "ro" module parameter. (For CD-ROM emulation, 50 * Access for each LUN can be limited to read-only. Moreover, the
50 * access is always read-only.) The gadget will indicate that it has 51 * function can indicate that LUN is removable and/or CD-ROM. (The
51 * removable media if the optional "removable" module parameter is set. 52 * later implies read-only access.)
53 *
54 * MSF is configured by specifying a fsg_config structure. It has the
55 * following fields:
56 *
57 * nluns Number of LUNs function have (anywhere from 1
58 * to FSG_MAX_LUNS which is 8).
59 * luns An array of LUN configuration values. This
60 * should be filled for each LUN that
61 * function will include (ie. for "nluns"
62 * LUNs). Each element of the array has
63 * the following fields:
64 * ->filename The path to the backing file for the LUN.
65 * Required if LUN is not marked as
66 * removable.
67 * ->ro Flag specifying access to the LUN shall be
68 * read-only. This is implied if CD-ROM
69 * emulation is enabled as well as when
70 * it was impossible to open "filename"
71 * in R/W mode.
72 * ->removable Flag specifying that LUN shall be indicated as
73 * being removable.
74 * ->cdrom Flag specifying that LUN shall be reported as
75 * being a CD-ROM.
76 *
77 * lun_name_format A printf-like format for names of the LUN
78 * devices. This determines how the
79 * directory in sysfs will be named.
80 * Unless you are using several MSFs in
81 * a single gadget (as opposed to single
82 * MSF in many configurations) you may
83 * leave it as NULL (in which case
84 * "lun%d" will be used). In the format
85 * you can use "%d" to index LUNs for
86 * MSF's with more than one LUN. (Beware
87 * that there is only one integer given
88 * as an argument for the format and
89 * specifying invalid format may cause
90 * unspecified behaviour.)
91 * thread_name Name of the kernel thread process used by the
92 * MSF. You can safely set it to NULL
93 * (in which case default "file-storage"
94 * will be used).
95 *
96 * vendor_name
97 * product_name
98 * release Information used as a reply to INQUIRY
99 * request. To use default set to NULL,
100 * NULL, 0xffff respectively. The first
101 * field should be 8 and the second 16
102 * characters or less.
103 *
104 * can_stall Set to permit function to halt bulk endpoints.
105 * Disabled on some USB devices known not
106 * to work correctly. You should set it
107 * to true.
108 *
109 * If "removable" is not set for a LUN then a backing file must be
110 * specified. If it is set, then NULL filename means the LUN's medium
111 * is not loaded (an empty string as "filename" in the fsg_config
112 * structure causes error). The CD-ROM emulation includes a single
113 * data track and no audio tracks; hence there need be only one
114 * backing file per LUN. Note also that the CD-ROM block length is
115 * set to 512 rather than the more common value 2048.
116 *
117 *
118 * MSF includes support for module parameters. If gadget using it
119 * decides to use it, the following module parameters will be
120 * available:
121 *
122 * file=filename[,filename...]
123 * Names of the files or block devices used for
124 * backing storage.
125 * ro=b[,b...] Default false, boolean for read-only access.
126 * removable=b[,b...]
127 * Default true, boolean for removable media.
128 * cdrom=b[,b...] Default false, boolean for whether to emulate
129 * a CD-ROM drive.
130 * luns=N Default N = number of filenames, number of
131 * LUNs to support.
132 * stall Default determined according to the type of
133 * USB device controller (usually true),
134 * boolean to permit the driver to halt
135 * bulk endpoints.
136 *
137 * The module parameters may be prefixed with some string. You need
138 * to consult gadget's documentation or source to verify whether it is
139 * using those module parameters and if it does what are the prefixes
140 * (look for FSG_MODULE_PARAMETERS() macro usage, what's inside it is
141 * the prefix).
52 * 142 *
53 * There is support for multiple logical units (LUNs), each of which has
54 * its own backing file. The number of LUNs can be set using the optional
55 * "luns" module parameter (anywhere from 1 to 8), and the corresponding
56 * files are specified using comma-separated lists for "file" and "ro".
57 * The default number of LUNs is taken from the number of "file" elements;
58 * it is 1 if "file" is not given. If "removable" is not set then a backing
59 * file must be specified for each LUN. If it is set, then an unspecified
60 * or empty backing filename means the LUN's medium is not loaded. Ideally
61 * each LUN would be settable independently as a disk drive or a CD-ROM
62 * drive, but currently all LUNs have to be the same type. The CD-ROM
63 * emulation includes a single data track and no audio tracks; hence there
64 * need be only one backing file per LUN. Note also that the CD-ROM block
65 * length is set to 512 rather than the more common value 2048.
66 * 143 *
67 * Requirements are modest; only a bulk-in and a bulk-out endpoint are 144 * Requirements are modest; only a bulk-in and a bulk-out endpoint are
68 * needed (an interrupt-out endpoint is also needed for CBI). The memory 145 * needed. The memory requirement amounts to two 16K buffers, size
69 * requirement amounts to two 16K buffers, size configurable by a parameter. 146 * configurable by a parameter. Support is included for both
70 * Support is included for both full-speed and high-speed operation. 147 * full-speed and high-speed operation.
71 * 148 *
72 * Note that the driver is slightly non-portable in that it assumes a 149 * Note that the driver is slightly non-portable in that it assumes a
73 * single memory/DMA buffer will be useable for bulk-in, bulk-out, and 150 * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
@@ -75,39 +152,28 @@
75 * issue, but there may be some with hardware restrictions that prevent 152 * issue, but there may be some with hardware restrictions that prevent
76 * a buffer from being used by more than one endpoint. 153 * a buffer from being used by more than one endpoint.
77 * 154 *
78 * Module options:
79 * 155 *
80 * file=filename[,filename...] 156 * The pathnames of the backing files and the ro settings are
81 * Required if "removable" is not set, names of 157 * available in the attribute files "file" and "ro" in the lun<n> (or
82 * the files or block devices used for 158 * to be more precise in a directory which name comes from
83 * backing storage 159 * "lun_name_format" option!) subdirectory of the gadget's sysfs
84 * ro=b[,b...] Default false, booleans for read-only access 160 * directory. If the "removable" option is set, writing to these
85 * removable Default false, boolean for removable media 161 * files will simulate ejecting/loading the medium (writing an empty
86 * luns=N Default N = number of filenames, number of 162 * line means eject) and adjusting a write-enable tab. Changes to the
87 * LUNs to support 163 * ro setting are not allowed when the medium is loaded or if CD-ROM
88 * stall Default determined according to the type of 164 * emulation is being used.
89 * USB device controller (usually true),
90 * boolean to permit the driver to halt
91 * bulk endpoints
92 * cdrom Default false, boolean for whether to emulate
93 * a CD-ROM drive
94 * 165 *
95 * The pathnames of the backing files and the ro settings are available in
96 * the attribute files "file" and "ro" in the lun<n> subdirectory of the
97 * gadget's sysfs directory. If the "removable" option is set, writing to
98 * these files will simulate ejecting/loading the medium (writing an empty
99 * line means eject) and adjusting a write-enable tab. Changes to the ro
100 * setting are not allowed when the medium is loaded or if CD-ROM emulation
101 * is being used.
102 * 166 *
103 * This gadget driver is heavily based on "Gadget Zero" by David Brownell. 167 * This function is heavily based on "File-backed Storage Gadget" by
104 * The driver's SCSI command interface was based on the "Information 168 * Alan Stern which in turn is heavily based on "Gadget Zero" by David
105 * technology - Small Computer System Interface - 2" document from 169 * Brownell. The driver's SCSI command interface was based on the
106 * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at 170 * "Information technology - Small Computer System Interface - 2"
107 * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception 171 * document from X3T9.2 Project 375D, Revision 10L, 7-SEP-93,
108 * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the 172 * available at <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>.
109 * "Universal Serial Bus Mass Storage Class UFI Command Specification" 173 * The single exception is opcode 0x23 (READ FORMAT CAPACITIES), which
110 * document, Revision 1.0, December 14, 1998, available at 174 * was based on the "Universal Serial Bus Mass Storage Class UFI
175 * Command Specification" document, Revision 1.0, December 14, 1998,
176 * available at
111 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>. 177 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
112 */ 178 */
113 179
@@ -115,7 +181,7 @@
115/* 181/*
116 * Driver Design 182 * Driver Design
117 * 183 *
118 * The FSG driver is fairly straightforward. There is a main kernel 184 * The MSF is fairly straightforward. There is a main kernel
119 * thread that handles most of the work. Interrupt routines field 185 * thread that handles most of the work. Interrupt routines field
120 * callbacks from the controller driver: bulk- and interrupt-request 186 * callbacks from the controller driver: bulk- and interrupt-request
121 * completion notifications, endpoint-0 events, and disconnect events. 187 * completion notifications, endpoint-0 events, and disconnect events.
@@ -138,17 +204,11 @@
138 * an EXIT exception. 204 * an EXIT exception.
139 * 205 *
140 * In normal operation the main thread is started during the gadget's 206 * In normal operation the main thread is started during the gadget's
141 * fsg_bind() callback and stopped during fsg_unbind(). But it can also 207 * fsg_bind() callback and stopped during fsg_unbind(). But it can
142 * exit when it receives a signal, and there's no point leaving the 208 * also exit when it receives a signal, and there's no point leaving
143 * gadget running when the thread is dead. So just before the thread 209 * the gadget running when the thread is dead. At of this moment, MSF
144 * exits, it deregisters the gadget driver. This makes things a little 210 * provides no way to deregister the gadget when thread dies -- maybe
145 * tricky: The driver is deregistered at two places, and the exiting 211 * a callback functions is needed.
146 * thread can indirectly call fsg_unbind() which in turn can tell the
147 * thread to exit. The first problem is resolved through the use of the
148 * REGISTERED atomic bitflag; the driver will only be deregistered once.
149 * The second problem is resolved by having fsg_unbind() check
150 * fsg->state; it won't try to stop the thread if the state is already
151 * FSG_STATE_TERMINATED.
152 * 212 *
153 * To provide maximum throughput, the driver uses a circular pipeline of 213 * To provide maximum throughput, the driver uses a circular pipeline of
154 * buffer heads (struct fsg_buffhd). In principle the pipeline can be 214 * buffer heads (struct fsg_buffhd). In principle the pipeline can be
@@ -236,7 +296,7 @@
236/*------------------------------------------------------------------------*/ 296/*------------------------------------------------------------------------*/
237 297
238#define FSG_DRIVER_DESC "Mass Storage Function" 298#define FSG_DRIVER_DESC "Mass Storage Function"
239#define FSG_DRIVER_VERSION "20 November 2008" 299#define FSG_DRIVER_VERSION "2009/09/11"
240 300
241static const char fsg_string_interface[] = "Mass Storage"; 301static const char fsg_string_interface[] = "Mass Storage";
242 302
@@ -307,7 +367,7 @@ struct fsg_config {
307 367
308struct fsg_dev { 368struct fsg_dev {
309 struct usb_function function; 369 struct usb_function function;
310 struct usb_composite_dev*cdev; 370 struct usb_composite_dev *cdev;
311 struct usb_gadget *gadget; /* Copy of cdev->gadget */ 371 struct usb_gadget *gadget; /* Copy of cdev->gadget */
312 struct fsg_common *common; 372 struct fsg_common *common;
313 373
@@ -322,18 +382,18 @@ struct fsg_dev {
322 const char *ep0req_name; 382 const char *ep0req_name;
323 383
324 unsigned int bulk_out_maxpacket; 384 unsigned int bulk_out_maxpacket;
325 enum fsg_state state; // For exception handling 385 enum fsg_state state; /* For exception handling */
326 unsigned int exception_req_tag; 386 unsigned int exception_req_tag;
327 387
328 u8 config, new_config; 388 u8 config, new_config;
329 389
330 unsigned int running : 1; 390 unsigned int running:1;
331 unsigned int bulk_in_enabled : 1; 391 unsigned int bulk_in_enabled:1;
332 unsigned int bulk_out_enabled : 1; 392 unsigned int bulk_out_enabled:1;
333 unsigned int phase_error : 1; 393 unsigned int phase_error:1;
334 unsigned int short_packet_received : 1; 394 unsigned int short_packet_received:1;
335 unsigned int bad_lun_okay : 1; 395 unsigned int bad_lun_okay:1;
336 unsigned int can_stall : 1; 396 unsigned int can_stall:1;
337 397
338 unsigned long atomic_bitflags; 398 unsigned long atomic_bitflags;
339#define REGISTERED 0 399#define REGISTERED 0
@@ -461,7 +521,7 @@ static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
461 if (req->status || req->actual != req->length) 521 if (req->status || req->actual != req->length)
462 DBG(fsg, "%s --> %d, %u/%u\n", __func__, 522 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
463 req->status, req->actual, req->length); 523 req->status, req->actual, req->length);
464 if (req->status == -ECONNRESET) // Request was cancelled 524 if (req->status == -ECONNRESET) /* Request was cancelled */
465 usb_ep_fifo_flush(ep); 525 usb_ep_fifo_flush(ep);
466 526
467 /* Hold the lock while we update the request and buffer states */ 527 /* Hold the lock while we update the request and buffer states */
@@ -483,7 +543,7 @@ static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
483 DBG(fsg, "%s --> %d, %u/%u\n", __func__, 543 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
484 req->status, req->actual, 544 req->status, req->actual,
485 bh->bulk_out_intended_length); 545 bh->bulk_out_intended_length);
486 if (req->status == -ECONNRESET) // Request was cancelled 546 if (req->status == -ECONNRESET) /* Request was cancelled */
487 usb_ep_fifo_flush(ep); 547 usb_ep_fifo_flush(ep);
488 548
489 /* Hold the lock while we update the request and buffer states */ 549 /* Hold the lock while we update the request and buffer states */
@@ -643,7 +703,7 @@ static int do_read(struct fsg_dev *fsg)
643 /* Carry out the file reads */ 703 /* Carry out the file reads */
644 amount_left = fsg->data_size_from_cmnd; 704 amount_left = fsg->data_size_from_cmnd;
645 if (unlikely(amount_left == 0)) 705 if (unlikely(amount_left == 0))
646 return -EIO; // No default reply 706 return -EIO; /* No default reply */
647 707
648 for (;;) { 708 for (;;) {
649 709
@@ -701,7 +761,7 @@ static int do_read(struct fsg_dev *fsg)
701 } else if (nread < amount) { 761 } else if (nread < amount) {
702 LDBG(curlun, "partial file read: %d/%u\n", 762 LDBG(curlun, "partial file read: %d/%u\n",
703 (int) nread, amount); 763 (int) nread, amount);
704 nread -= (nread & 511); // Round down to a block 764 nread -= (nread & 511); /* Round down to a block */
705 } 765 }
706 file_offset += nread; 766 file_offset += nread;
707 amount_left -= nread; 767 amount_left -= nread;
@@ -718,7 +778,7 @@ static int do_read(struct fsg_dev *fsg)
718 } 778 }
719 779
720 if (amount_left == 0) 780 if (amount_left == 0)
721 break; // No more left to read 781 break; /* No more left to read */
722 782
723 /* Send this buffer and go read some more */ 783 /* Send this buffer and go read some more */
724 bh->inreq->zero = 0; 784 bh->inreq->zero = 0;
@@ -727,7 +787,7 @@ static int do_read(struct fsg_dev *fsg)
727 fsg->common->next_buffhd_to_fill = bh->next; 787 fsg->common->next_buffhd_to_fill = bh->next;
728 } 788 }
729 789
730 return -EIO; // No default reply 790 return -EIO; /* No default reply */
731} 791}
732 792
733 793
@@ -751,7 +811,7 @@ static int do_write(struct fsg_dev *fsg)
751 return -EINVAL; 811 return -EINVAL;
752 } 812 }
753 spin_lock(&curlun->filp->f_lock); 813 spin_lock(&curlun->filp->f_lock);
754 curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait 814 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
755 spin_unlock(&curlun->filp->f_lock); 815 spin_unlock(&curlun->filp->f_lock);
756 816
757 /* Get the starting Logical Block Address and check that it's 817 /* Get the starting Logical Block Address and check that it's
@@ -769,7 +829,7 @@ static int do_write(struct fsg_dev *fsg)
769 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 829 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
770 return -EINVAL; 830 return -EINVAL;
771 } 831 }
772 if (fsg->common->cmnd[1] & 0x08) { // FUA 832 if (fsg->common->cmnd[1] & 0x08) { /* FUA */
773 spin_lock(&curlun->filp->f_lock); 833 spin_lock(&curlun->filp->f_lock);
774 curlun->filp->f_flags |= O_SYNC; 834 curlun->filp->f_flags |= O_SYNC;
775 spin_unlock(&curlun->filp->f_lock); 835 spin_unlock(&curlun->filp->f_lock);
@@ -834,8 +894,8 @@ static int do_write(struct fsg_dev *fsg)
834 894
835 /* amount is always divisible by 512, hence by 895 /* amount is always divisible by 512, hence by
836 * the bulk-out maxpacket size */ 896 * the bulk-out maxpacket size */
837 bh->outreq->length = bh->bulk_out_intended_length = 897 bh->outreq->length = amount;
838 amount; 898 bh->bulk_out_intended_length = amount;
839 bh->outreq->short_not_ok = 1; 899 bh->outreq->short_not_ok = 1;
840 start_transfer(fsg, fsg->bulk_out, bh->outreq, 900 start_transfer(fsg, fsg->bulk_out, bh->outreq,
841 &bh->outreq_busy, &bh->state); 901 &bh->outreq_busy, &bh->state);
@@ -846,7 +906,7 @@ static int do_write(struct fsg_dev *fsg)
846 /* Write the received data to the backing file */ 906 /* Write the received data to the backing file */
847 bh = fsg->common->next_buffhd_to_drain; 907 bh = fsg->common->next_buffhd_to_drain;
848 if (bh->state == BUF_STATE_EMPTY && !get_some_more) 908 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
849 break; // We stopped early 909 break; /* We stopped early */
850 if (bh->state == BUF_STATE_FULL) { 910 if (bh->state == BUF_STATE_FULL) {
851 smp_rmb(); 911 smp_rmb();
852 fsg->common->next_buffhd_to_drain = bh->next; 912 fsg->common->next_buffhd_to_drain = bh->next;
@@ -878,7 +938,7 @@ static int do_write(struct fsg_dev *fsg)
878 (unsigned long long) file_offset, 938 (unsigned long long) file_offset,
879 (int) nwritten); 939 (int) nwritten);
880 if (signal_pending(current)) 940 if (signal_pending(current))
881 return -EINTR; // Interrupted! 941 return -EINTR; /* Interrupted! */
882 942
883 if (nwritten < 0) { 943 if (nwritten < 0) {
884 LDBG(curlun, "error in file write: %d\n", 944 LDBG(curlun, "error in file write: %d\n",
@@ -888,7 +948,7 @@ static int do_write(struct fsg_dev *fsg)
888 LDBG(curlun, "partial file write: %d/%u\n", 948 LDBG(curlun, "partial file write: %d/%u\n",
889 (int) nwritten, amount); 949 (int) nwritten, amount);
890 nwritten -= (nwritten & 511); 950 nwritten -= (nwritten & 511);
891 // Round down to a block 951 /* Round down to a block */
892 } 952 }
893 file_offset += nwritten; 953 file_offset += nwritten;
894 amount_left_to_write -= nwritten; 954 amount_left_to_write -= nwritten;
@@ -916,7 +976,7 @@ static int do_write(struct fsg_dev *fsg)
916 return rc; 976 return rc;
917 } 977 }
918 978
919 return -EIO; // No default reply 979 return -EIO; /* No default reply */
920} 980}
921 981
922 982
@@ -976,7 +1036,7 @@ static int do_verify(struct fsg_dev *fsg)
976 1036
977 verification_length = get_unaligned_be16(&fsg->common->cmnd[7]); 1037 verification_length = get_unaligned_be16(&fsg->common->cmnd[7]);
978 if (unlikely(verification_length == 0)) 1038 if (unlikely(verification_length == 0))
979 return -EIO; // No default reply 1039 return -EIO; /* No default reply */
980 1040
981 /* Prepare to carry out the file verify */ 1041 /* Prepare to carry out the file verify */
982 amount_left = verification_length << 9; 1042 amount_left = verification_length << 9;
@@ -1029,7 +1089,7 @@ static int do_verify(struct fsg_dev *fsg)
1029 } else if (nread < amount) { 1089 } else if (nread < amount) {
1030 LDBG(curlun, "partial file verify: %d/%u\n", 1090 LDBG(curlun, "partial file verify: %d/%u\n",
1031 (int) nread, amount); 1091 (int) nread, amount);
1032 nread -= (nread & 511); // Round down to a sector 1092 nread -= (nread & 511); /* Round down to a sector */
1033 } 1093 }
1034 if (nread == 0) { 1094 if (nread == 0) {
1035 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 1095 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
@@ -1054,17 +1114,17 @@ static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1054 if (!curlun) { /* Unsupported LUNs are okay */ 1114 if (!curlun) { /* Unsupported LUNs are okay */
1055 fsg->bad_lun_okay = 1; 1115 fsg->bad_lun_okay = 1;
1056 memset(buf, 0, 36); 1116 memset(buf, 0, 36);
1057 buf[0] = 0x7f; // Unsupported, no device-type 1117 buf[0] = 0x7f; /* Unsupported, no device-type */
1058 buf[4] = 31; // Additional length 1118 buf[4] = 31; /* Additional length */
1059 return 36; 1119 return 36;
1060 } 1120 }
1061 1121
1062 buf[0] = curlun->cdrom ? TYPE_CDROM : TYPE_DISK; 1122 buf[0] = curlun->cdrom ? TYPE_CDROM : TYPE_DISK;
1063 buf[1] = curlun->removable ? 0x80 : 0; 1123 buf[1] = curlun->removable ? 0x80 : 0;
1064 buf[2] = 2; // ANSI SCSI level 2 1124 buf[2] = 2; /* ANSI SCSI level 2 */
1065 buf[3] = 2; // SCSI-2 INQUIRY data format 1125 buf[3] = 2; /* SCSI-2 INQUIRY data format */
1066 buf[4] = 31; // Additional length 1126 buf[4] = 31; /* Additional length */
1067 buf[5] = 0; // No special options 1127 buf[5] = 0; /* No special options */
1068 buf[6] = 0; 1128 buf[6] = 0;
1069 buf[7] = 0; 1129 buf[7] = 0;
1070 memcpy(buf + 8, fsg->common->inquiry_string, 1130 memcpy(buf + 8, fsg->common->inquiry_string,
@@ -1102,7 +1162,7 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1102 } 1162 }
1103#endif 1163#endif
1104 1164
1105 if (!curlun) { // Unsupported LUNs are okay 1165 if (!curlun) { /* Unsupported LUNs are okay */
1106 fsg->bad_lun_okay = 1; 1166 fsg->bad_lun_okay = 1;
1107 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED; 1167 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1108 sdinfo = 0; 1168 sdinfo = 0;
@@ -1117,10 +1177,10 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1117 } 1177 }
1118 1178
1119 memset(buf, 0, 18); 1179 memset(buf, 0, 18);
1120 buf[0] = valid | 0x70; // Valid, current error 1180 buf[0] = valid | 0x70; /* Valid, current error */
1121 buf[2] = SK(sd); 1181 buf[2] = SK(sd);
1122 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */ 1182 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
1123 buf[7] = 18 - 8; // Additional sense length 1183 buf[7] = 18 - 8; /* Additional sense length */
1124 buf[12] = ASC(sd); 1184 buf[12] = ASC(sd);
1125 buf[13] = ASCQ(sd); 1185 buf[13] = ASCQ(sd);
1126 return 18; 1186 return 18;
@@ -1209,7 +1269,7 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1209 int valid_page = 0; 1269 int valid_page = 0;
1210 int len, limit; 1270 int len, limit;
1211 1271
1212 if ((fsg->common->cmnd[1] & ~0x08) != 0) { // Mask away DBD 1272 if ((fsg->common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
1213 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1273 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1214 return -EINVAL; 1274 return -EINVAL;
1215 } 1275 }
@@ -1228,13 +1288,13 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1228 * the mode data length later. */ 1288 * the mode data length later. */
1229 memset(buf, 0, 8); 1289 memset(buf, 0, 8);
1230 if (mscmnd == SC_MODE_SENSE_6) { 1290 if (mscmnd == SC_MODE_SENSE_6) {
1231 buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA 1291 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
1232 buf += 4; 1292 buf += 4;
1233 limit = 255; 1293 limit = 255;
1234 } else { // SC_MODE_SENSE_10 1294 } else { /* SC_MODE_SENSE_10 */
1235 buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA 1295 buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
1236 buf += 8; 1296 buf += 8;
1237 limit = 65535; // Should really be FSG_BUFLEN 1297 limit = 65535; /* Should really be FSG_BUFLEN */
1238 } 1298 }
1239 1299
1240 /* No block descriptors */ 1300 /* No block descriptors */
@@ -1243,14 +1303,14 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1243 * is the Caching page. */ 1303 * is the Caching page. */
1244 if (page_code == 0x08 || all_pages) { 1304 if (page_code == 0x08 || all_pages) {
1245 valid_page = 1; 1305 valid_page = 1;
1246 buf[0] = 0x08; // Page code 1306 buf[0] = 0x08; /* Page code */
1247 buf[1] = 10; // Page length 1307 buf[1] = 10; /* Page length */
1248 memset(buf+2, 0, 10); // None of the fields are changeable 1308 memset(buf+2, 0, 10); /* None of the fields are changeable */
1249 1309
1250 if (!changeable_values) { 1310 if (!changeable_values) {
1251 buf[2] = 0x04; // Write cache enable, 1311 buf[2] = 0x04; /* Write cache enable, */
1252 // Read cache not disabled 1312 /* Read cache not disabled */
1253 // No cache retention priorities 1313 /* No cache retention priorities */
1254 put_unaligned_be16(0xffff, &buf[4]); 1314 put_unaligned_be16(0xffff, &buf[4]);
1255 /* Don't disable prefetch */ 1315 /* Don't disable prefetch */
1256 /* Minimum prefetch = 0 */ 1316 /* Minimum prefetch = 0 */
@@ -1304,7 +1364,7 @@ static int do_prevent_allow(struct fsg_dev *fsg)
1304 } 1364 }
1305 1365
1306 prevent = fsg->common->cmnd[4] & 0x01; 1366 prevent = fsg->common->cmnd[4] & 0x01;
1307 if ((fsg->common->cmnd[4] & ~0x01) != 0) { // Mask away Prevent 1367 if ((fsg->common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
1308 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1368 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1309 return -EINVAL; 1369 return -EINVAL;
1310 } 1370 }
@@ -1323,7 +1383,7 @@ static int do_read_format_capacities(struct fsg_dev *fsg,
1323 u8 *buf = (u8 *) bh->buf; 1383 u8 *buf = (u8 *) bh->buf;
1324 1384
1325 buf[0] = buf[1] = buf[2] = 0; 1385 buf[0] = buf[1] = buf[2] = 0;
1326 buf[3] = 8; // Only the Current/Maximum Capacity Descriptor 1386 buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
1327 buf += 4; 1387 buf += 4;
1328 1388
1329 put_unaligned_be32(curlun->num_sectors, &buf[0]); 1389 put_unaligned_be32(curlun->num_sectors, &buf[0]);
@@ -1398,7 +1458,7 @@ static int pad_with_zeros(struct fsg_dev *fsg)
1398 u32 nsend; 1458 u32 nsend;
1399 int rc; 1459 int rc;
1400 1460
1401 bh->state = BUF_STATE_EMPTY; // For the first iteration 1461 bh->state = BUF_STATE_EMPTY; /* For the first iteration */
1402 fsg->usb_amount_left = nkeep + fsg->residue; 1462 fsg->usb_amount_left = nkeep + fsg->residue;
1403 while (fsg->usb_amount_left > 0) { 1463 while (fsg->usb_amount_left > 0) {
1404 1464
@@ -1454,8 +1514,8 @@ static int throw_away_data(struct fsg_dev *fsg)
1454 1514
1455 /* amount is always divisible by 512, hence by 1515 /* amount is always divisible by 512, hence by
1456 * the bulk-out maxpacket size */ 1516 * the bulk-out maxpacket size */
1457 bh->outreq->length = bh->bulk_out_intended_length = 1517 bh->outreq->length = amount;
1458 amount; 1518 bh->bulk_out_intended_length = amount;
1459 bh->outreq->short_not_ok = 1; 1519 bh->outreq->short_not_ok = 1;
1460 start_transfer(fsg, fsg->bulk_out, bh->outreq, 1520 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1461 &bh->outreq_busy, &bh->state); 1521 &bh->outreq_busy, &bh->state);
@@ -1480,7 +1540,7 @@ static int finish_reply(struct fsg_dev *fsg)
1480 1540
1481 switch (fsg->data_dir) { 1541 switch (fsg->data_dir) {
1482 case DATA_DIR_NONE: 1542 case DATA_DIR_NONE:
1483 break; // Nothing to send 1543 break; /* Nothing to send */
1484 1544
1485 /* If we don't know whether the host wants to read or write, 1545 /* If we don't know whether the host wants to read or write,
1486 * this must be CB or CBI with an unknown command. We mustn't 1546 * this must be CB or CBI with an unknown command. We mustn't
@@ -1522,14 +1582,13 @@ static int finish_reply(struct fsg_dev *fsg)
1522 /* We have processed all we want from the data the host has sent. 1582 /* We have processed all we want from the data the host has sent.
1523 * There may still be outstanding bulk-out requests. */ 1583 * There may still be outstanding bulk-out requests. */
1524 case DATA_DIR_FROM_HOST: 1584 case DATA_DIR_FROM_HOST:
1525 if (fsg->residue == 0) 1585 if (fsg->residue == 0) {
1526 ; // Nothing to receive 1586 /* Nothing to receive */
1527 1587
1528 /* Did the host stop sending unexpectedly early? */ 1588 /* Did the host stop sending unexpectedly early? */
1529 else if (fsg->short_packet_received) { 1589 } else if (fsg->short_packet_received) {
1530 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT); 1590 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
1531 rc = -EINTR; 1591 rc = -EINTR;
1532 }
1533 1592
1534 /* We haven't processed all the incoming data. Even though 1593 /* We haven't processed all the incoming data. Even though
1535 * we may be allowed to stall, doing so would cause a race. 1594 * we may be allowed to stall, doing so would cause a race.
@@ -1538,17 +1597,17 @@ static int finish_reply(struct fsg_dev *fsg)
1538 * STALL. Not realizing the endpoint was halted, it wouldn't 1597 * STALL. Not realizing the endpoint was halted, it wouldn't
1539 * clear the halt -- leading to problems later on. */ 1598 * clear the halt -- leading to problems later on. */
1540#if 0 1599#if 0
1541 else if (fsg->can_stall) { 1600 } else if (fsg->can_stall) {
1542 fsg_set_halt(fsg, fsg->bulk_out); 1601 fsg_set_halt(fsg, fsg->bulk_out);
1543 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT); 1602 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
1544 rc = -EINTR; 1603 rc = -EINTR;
1545 }
1546#endif 1604#endif
1547 1605
1548 /* We can't stall. Read in the excess data and throw it 1606 /* We can't stall. Read in the excess data and throw it
1549 * all away. */ 1607 * all away. */
1550 else 1608 } else {
1551 rc = throw_away_data(fsg); 1609 rc = throw_away_data(fsg);
1610 }
1552 break; 1611 break;
1553 } 1612 }
1554 return rc; 1613 return rc;
@@ -1593,7 +1652,7 @@ static int send_status(struct fsg_dev *fsg)
1593 } 1652 }
1594 1653
1595 /* Store and send the Bulk-only CSW */ 1654 /* Store and send the Bulk-only CSW */
1596 csw = (void*)bh->buf; 1655 csw = (void *)bh->buf;
1597 1656
1598 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG); 1657 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
1599 csw->Tag = fsg->tag; 1658 csw->Tag = fsg->tag;
@@ -1629,18 +1688,18 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
1629 sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir], 1688 sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
1630 fsg->data_size); 1689 fsg->data_size);
1631 VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", 1690 VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
1632 name, cmnd_size, dirletter[(int) data_dir], 1691 name, cmnd_size, dirletter[(int) data_dir],
1633 fsg->data_size_from_cmnd, fsg->common->cmnd_size, hdlen); 1692 fsg->data_size_from_cmnd, fsg->common->cmnd_size, hdlen);
1634 1693
1635 /* We can't reply at all until we know the correct data direction 1694 /* We can't reply at all until we know the correct data direction
1636 * and size. */ 1695 * and size. */
1637 if (fsg->data_size_from_cmnd == 0) 1696 if (fsg->data_size_from_cmnd == 0)
1638 data_dir = DATA_DIR_NONE; 1697 data_dir = DATA_DIR_NONE;
1639 if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI 1698 if (fsg->data_dir == DATA_DIR_UNKNOWN) { /* CB or CBI */
1640 fsg->data_dir = data_dir; 1699 fsg->data_dir = data_dir;
1641 fsg->data_size = fsg->data_size_from_cmnd; 1700 fsg->data_size = fsg->data_size_from_cmnd;
1642 1701
1643 } else { // Bulk-only 1702 } else { /* Bulk-only */
1644 if (fsg->data_size < fsg->data_size_from_cmnd) { 1703 if (fsg->data_size < fsg->data_size_from_cmnd) {
1645 1704
1646 /* Host data size < Device data size is a phase error. 1705 /* Host data size < Device data size is a phase error.
@@ -1691,7 +1750,8 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
1691 1750
1692 /* Check the LUN */ 1751 /* Check the LUN */
1693 if (fsg->common->lun >= 0 && fsg->common->lun < fsg->common->nluns) { 1752 if (fsg->common->lun >= 0 && fsg->common->lun < fsg->common->nluns) {
1694 fsg->common->curlun = curlun = &fsg->common->luns[fsg->common->lun]; 1753 curlun = &fsg->common->luns[fsg->common->lun];
1754 fsg->common->curlun = curlun;
1695 if (fsg->common->cmnd[0] != SC_REQUEST_SENSE) { 1755 if (fsg->common->cmnd[0] != SC_REQUEST_SENSE) {
1696 curlun->sense_data = SS_NO_SENSE; 1756 curlun->sense_data = SS_NO_SENSE;
1697 curlun->sense_data_info = 0; 1757 curlun->sense_data_info = 0;
@@ -1721,7 +1781,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
1721 } 1781 }
1722 1782
1723 /* Check that only command bytes listed in the mask are non-zero */ 1783 /* Check that only command bytes listed in the mask are non-zero */
1724 fsg->common->cmnd[1] &= 0x1f; // Mask away the LUN 1784 fsg->common->cmnd[1] &= 0x1f; /* Mask away the LUN */
1725 for (i = 1; i < cmnd_size; ++i) { 1785 for (i = 1; i < cmnd_size; ++i) {
1726 if (fsg->common->cmnd[i] && !(mask & (1 << i))) { 1786 if (fsg->common->cmnd[i] && !(mask & (1 << i))) {
1727 if (curlun) 1787 if (curlun)
@@ -1752,7 +1812,8 @@ static int do_scsi_command(struct fsg_dev *fsg)
1752 dump_cdb(fsg->common); 1812 dump_cdb(fsg->common);
1753 1813
1754 /* Wait for the next buffer to become available for data or status */ 1814 /* Wait for the next buffer to become available for data or status */
1755 bh = fsg->common->next_buffhd_to_drain = fsg->common->next_buffhd_to_fill; 1815 bh = fsg->common->next_buffhd_to_fill;
1816 fsg->common->next_buffhd_to_drain = bh;
1756 while (bh->state != BUF_STATE_EMPTY) { 1817 while (bh->state != BUF_STATE_EMPTY) {
1757 rc = sleep_thread(fsg); 1818 rc = sleep_thread(fsg);
1758 if (rc) 1819 if (rc)
@@ -1761,141 +1822,163 @@ static int do_scsi_command(struct fsg_dev *fsg)
1761 fsg->phase_error = 0; 1822 fsg->phase_error = 0;
1762 fsg->short_packet_received = 0; 1823 fsg->short_packet_received = 0;
1763 1824
1764 down_read(&fsg->common->filesem); // We're using the backing file 1825 /* We're using the backing file */
1826 down_read(&fsg->common->filesem);
1765 switch (fsg->common->cmnd[0]) { 1827 switch (fsg->common->cmnd[0]) {
1766 1828
1767 case SC_INQUIRY: 1829 case SC_INQUIRY:
1768 fsg->data_size_from_cmnd = fsg->common->cmnd[4]; 1830 fsg->data_size_from_cmnd = fsg->common->cmnd[4];
1769 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 1831 reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
1770 (1<<4), 0, 1832 (1<<4), 0,
1771 "INQUIRY")) == 0) 1833 "INQUIRY");
1834 if (reply == 0)
1772 reply = do_inquiry(fsg, bh); 1835 reply = do_inquiry(fsg, bh);
1773 break; 1836 break;
1774 1837
1775 case SC_MODE_SELECT_6: 1838 case SC_MODE_SELECT_6:
1776 fsg->data_size_from_cmnd = fsg->common->cmnd[4]; 1839 fsg->data_size_from_cmnd = fsg->common->cmnd[4];
1777 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, 1840 reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
1778 (1<<1) | (1<<4), 0, 1841 (1<<1) | (1<<4), 0,
1779 "MODE SELECT(6)")) == 0) 1842 "MODE SELECT(6)");
1843 if (reply == 0)
1780 reply = do_mode_select(fsg, bh); 1844 reply = do_mode_select(fsg, bh);
1781 break; 1845 break;
1782 1846
1783 case SC_MODE_SELECT_10: 1847 case SC_MODE_SELECT_10:
1784 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]); 1848 fsg->data_size_from_cmnd =
1785 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, 1849 get_unaligned_be16(&fsg->common->cmnd[7]);
1786 (1<<1) | (3<<7), 0, 1850 reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
1787 "MODE SELECT(10)")) == 0) 1851 (1<<1) | (3<<7), 0,
1852 "MODE SELECT(10)");
1853 if (reply == 0)
1788 reply = do_mode_select(fsg, bh); 1854 reply = do_mode_select(fsg, bh);
1789 break; 1855 break;
1790 1856
1791 case SC_MODE_SENSE_6: 1857 case SC_MODE_SENSE_6:
1792 fsg->data_size_from_cmnd = fsg->common->cmnd[4]; 1858 fsg->data_size_from_cmnd = fsg->common->cmnd[4];
1793 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 1859 reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
1794 (1<<1) | (1<<2) | (1<<4), 0, 1860 (1<<1) | (1<<2) | (1<<4), 0,
1795 "MODE SENSE(6)")) == 0) 1861 "MODE SENSE(6)");
1862 if (reply == 0)
1796 reply = do_mode_sense(fsg, bh); 1863 reply = do_mode_sense(fsg, bh);
1797 break; 1864 break;
1798 1865
1799 case SC_MODE_SENSE_10: 1866 case SC_MODE_SENSE_10:
1800 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]); 1867 fsg->data_size_from_cmnd =
1801 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 1868 get_unaligned_be16(&fsg->common->cmnd[7]);
1802 (1<<1) | (1<<2) | (3<<7), 0, 1869 reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
1803 "MODE SENSE(10)")) == 0) 1870 (1<<1) | (1<<2) | (3<<7), 0,
1871 "MODE SENSE(10)");
1872 if (reply == 0)
1804 reply = do_mode_sense(fsg, bh); 1873 reply = do_mode_sense(fsg, bh);
1805 break; 1874 break;
1806 1875
1807 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL: 1876 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1808 fsg->data_size_from_cmnd = 0; 1877 fsg->data_size_from_cmnd = 0;
1809 if ((reply = check_command(fsg, 6, DATA_DIR_NONE, 1878 reply = check_command(fsg, 6, DATA_DIR_NONE,
1810 (1<<4), 0, 1879 (1<<4), 0,
1811 "PREVENT-ALLOW MEDIUM REMOVAL")) == 0) 1880 "PREVENT-ALLOW MEDIUM REMOVAL");
1881 if (reply == 0)
1812 reply = do_prevent_allow(fsg); 1882 reply = do_prevent_allow(fsg);
1813 break; 1883 break;
1814 1884
1815 case SC_READ_6: 1885 case SC_READ_6:
1816 i = fsg->common->cmnd[4]; 1886 i = fsg->common->cmnd[4];
1817 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 1887 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
1818 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 1888 reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
1819 (7<<1) | (1<<4), 1, 1889 (7<<1) | (1<<4), 1,
1820 "READ(6)")) == 0) 1890 "READ(6)");
1891 if (reply == 0)
1821 reply = do_read(fsg); 1892 reply = do_read(fsg);
1822 break; 1893 break;
1823 1894
1824 case SC_READ_10: 1895 case SC_READ_10:
1825 fsg->data_size_from_cmnd = 1896 fsg->data_size_from_cmnd =
1826 get_unaligned_be16(&fsg->common->cmnd[7]) << 9; 1897 get_unaligned_be16(&fsg->common->cmnd[7]) << 9;
1827 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 1898 reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
1828 (1<<1) | (0xf<<2) | (3<<7), 1, 1899 (1<<1) | (0xf<<2) | (3<<7), 1,
1829 "READ(10)")) == 0) 1900 "READ(10)");
1901 if (reply == 0)
1830 reply = do_read(fsg); 1902 reply = do_read(fsg);
1831 break; 1903 break;
1832 1904
1833 case SC_READ_12: 1905 case SC_READ_12:
1834 fsg->data_size_from_cmnd = 1906 fsg->data_size_from_cmnd =
1835 get_unaligned_be32(&fsg->common->cmnd[6]) << 9; 1907 get_unaligned_be32(&fsg->common->cmnd[6]) << 9;
1836 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST, 1908 reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
1837 (1<<1) | (0xf<<2) | (0xf<<6), 1, 1909 (1<<1) | (0xf<<2) | (0xf<<6), 1,
1838 "READ(12)")) == 0) 1910 "READ(12)");
1911 if (reply == 0)
1839 reply = do_read(fsg); 1912 reply = do_read(fsg);
1840 break; 1913 break;
1841 1914
1842 case SC_READ_CAPACITY: 1915 case SC_READ_CAPACITY:
1843 fsg->data_size_from_cmnd = 8; 1916 fsg->data_size_from_cmnd = 8;
1844 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 1917 reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
1845 (0xf<<2) | (1<<8), 1, 1918 (0xf<<2) | (1<<8), 1,
1846 "READ CAPACITY")) == 0) 1919 "READ CAPACITY");
1920 if (reply == 0)
1847 reply = do_read_capacity(fsg, bh); 1921 reply = do_read_capacity(fsg, bh);
1848 break; 1922 break;
1849 1923
1850 case SC_READ_HEADER: 1924 case SC_READ_HEADER:
1851 if (!fsg->common->curlun || !fsg->common->curlun->cdrom) 1925 if (!fsg->common->curlun || !fsg->common->curlun->cdrom)
1852 goto unknown_cmnd; 1926 goto unknown_cmnd;
1853 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]); 1927 fsg->data_size_from_cmnd =
1854 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 1928 get_unaligned_be16(&fsg->common->cmnd[7]);
1855 (3<<7) | (0x1f<<1), 1, 1929 reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
1856 "READ HEADER")) == 0) 1930 (3<<7) | (0x1f<<1), 1,
1931 "READ HEADER");
1932 if (reply == 0)
1857 reply = do_read_header(fsg, bh); 1933 reply = do_read_header(fsg, bh);
1858 break; 1934 break;
1859 1935
1860 case SC_READ_TOC: 1936 case SC_READ_TOC:
1861 if (!fsg->common->curlun || !fsg->common->curlun->cdrom) 1937 if (!fsg->common->curlun || !fsg->common->curlun->cdrom)
1862 goto unknown_cmnd; 1938 goto unknown_cmnd;
1863 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]); 1939 fsg->data_size_from_cmnd =
1864 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 1940 get_unaligned_be16(&fsg->common->cmnd[7]);
1865 (7<<6) | (1<<1), 1, 1941 reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
1866 "READ TOC")) == 0) 1942 (7<<6) | (1<<1), 1,
1943 "READ TOC");
1944 if (reply == 0)
1867 reply = do_read_toc(fsg, bh); 1945 reply = do_read_toc(fsg, bh);
1868 break; 1946 break;
1869 1947
1870 case SC_READ_FORMAT_CAPACITIES: 1948 case SC_READ_FORMAT_CAPACITIES:
1871 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]); 1949 fsg->data_size_from_cmnd =
1872 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 1950 get_unaligned_be16(&fsg->common->cmnd[7]);
1873 (3<<7), 1, 1951 reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
1874 "READ FORMAT CAPACITIES")) == 0) 1952 (3<<7), 1,
1953 "READ FORMAT CAPACITIES");
1954 if (reply == 0)
1875 reply = do_read_format_capacities(fsg, bh); 1955 reply = do_read_format_capacities(fsg, bh);
1876 break; 1956 break;
1877 1957
1878 case SC_REQUEST_SENSE: 1958 case SC_REQUEST_SENSE:
1879 fsg->data_size_from_cmnd = fsg->common->cmnd[4]; 1959 fsg->data_size_from_cmnd = fsg->common->cmnd[4];
1880 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 1960 reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
1881 (1<<4), 0, 1961 (1<<4), 0,
1882 "REQUEST SENSE")) == 0) 1962 "REQUEST SENSE");
1963 if (reply == 0)
1883 reply = do_request_sense(fsg, bh); 1964 reply = do_request_sense(fsg, bh);
1884 break; 1965 break;
1885 1966
1886 case SC_START_STOP_UNIT: 1967 case SC_START_STOP_UNIT:
1887 fsg->data_size_from_cmnd = 0; 1968 fsg->data_size_from_cmnd = 0;
1888 if ((reply = check_command(fsg, 6, DATA_DIR_NONE, 1969 reply = check_command(fsg, 6, DATA_DIR_NONE,
1889 (1<<1) | (1<<4), 0, 1970 (1<<1) | (1<<4), 0,
1890 "START-STOP UNIT")) == 0) 1971 "START-STOP UNIT");
1972 if (reply == 0)
1891 reply = do_start_stop(fsg); 1973 reply = do_start_stop(fsg);
1892 break; 1974 break;
1893 1975
1894 case SC_SYNCHRONIZE_CACHE: 1976 case SC_SYNCHRONIZE_CACHE:
1895 fsg->data_size_from_cmnd = 0; 1977 fsg->data_size_from_cmnd = 0;
1896 if ((reply = check_command(fsg, 10, DATA_DIR_NONE, 1978 reply = check_command(fsg, 10, DATA_DIR_NONE,
1897 (0xf<<2) | (3<<7), 1, 1979 (0xf<<2) | (3<<7), 1,
1898 "SYNCHRONIZE CACHE")) == 0) 1980 "SYNCHRONIZE CACHE");
1981 if (reply == 0)
1899 reply = do_synchronize_cache(fsg); 1982 reply = do_synchronize_cache(fsg);
1900 break; 1983 break;
1901 1984
@@ -1910,36 +1993,40 @@ static int do_scsi_command(struct fsg_dev *fsg)
1910 * support a minimal version: BytChk must be 0. */ 1993 * support a minimal version: BytChk must be 0. */
1911 case SC_VERIFY: 1994 case SC_VERIFY:
1912 fsg->data_size_from_cmnd = 0; 1995 fsg->data_size_from_cmnd = 0;
1913 if ((reply = check_command(fsg, 10, DATA_DIR_NONE, 1996 reply = check_command(fsg, 10, DATA_DIR_NONE,
1914 (1<<1) | (0xf<<2) | (3<<7), 1, 1997 (1<<1) | (0xf<<2) | (3<<7), 1,
1915 "VERIFY")) == 0) 1998 "VERIFY");
1999 if (reply == 0)
1916 reply = do_verify(fsg); 2000 reply = do_verify(fsg);
1917 break; 2001 break;
1918 2002
1919 case SC_WRITE_6: 2003 case SC_WRITE_6:
1920 i = fsg->common->cmnd[4]; 2004 i = fsg->common->cmnd[4];
1921 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 2005 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
1922 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, 2006 reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
1923 (7<<1) | (1<<4), 1, 2007 (7<<1) | (1<<4), 1,
1924 "WRITE(6)")) == 0) 2008 "WRITE(6)");
2009 if (reply == 0)
1925 reply = do_write(fsg); 2010 reply = do_write(fsg);
1926 break; 2011 break;
1927 2012
1928 case SC_WRITE_10: 2013 case SC_WRITE_10:
1929 fsg->data_size_from_cmnd = 2014 fsg->data_size_from_cmnd =
1930 get_unaligned_be16(&fsg->common->cmnd[7]) << 9; 2015 get_unaligned_be16(&fsg->common->cmnd[7]) << 9;
1931 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, 2016 reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
1932 (1<<1) | (0xf<<2) | (3<<7), 1, 2017 (1<<1) | (0xf<<2) | (3<<7), 1,
1933 "WRITE(10)")) == 0) 2018 "WRITE(10)");
2019 if (reply == 0)
1934 reply = do_write(fsg); 2020 reply = do_write(fsg);
1935 break; 2021 break;
1936 2022
1937 case SC_WRITE_12: 2023 case SC_WRITE_12:
1938 fsg->data_size_from_cmnd = 2024 fsg->data_size_from_cmnd =
1939 get_unaligned_be32(&fsg->common->cmnd[6]) << 9; 2025 get_unaligned_be32(&fsg->common->cmnd[6]) << 9;
1940 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST, 2026 reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
1941 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2027 (1<<1) | (0xf<<2) | (0xf<<6), 1,
1942 "WRITE(12)")) == 0) 2028 "WRITE(12)");
2029 if (reply == 0)
1943 reply = do_write(fsg); 2030 reply = do_write(fsg);
1944 break; 2031 break;
1945 2032
@@ -1951,14 +2038,15 @@ static int do_scsi_command(struct fsg_dev *fsg)
1951 case SC_RELEASE: 2038 case SC_RELEASE:
1952 case SC_RESERVE: 2039 case SC_RESERVE:
1953 case SC_SEND_DIAGNOSTIC: 2040 case SC_SEND_DIAGNOSTIC:
1954 // Fall through 2041 /* Fall through */
1955 2042
1956 default: 2043 default:
1957 unknown_cmnd: 2044unknown_cmnd:
1958 fsg->data_size_from_cmnd = 0; 2045 fsg->data_size_from_cmnd = 0;
1959 sprintf(unknown, "Unknown x%02x", fsg->common->cmnd[0]); 2046 sprintf(unknown, "Unknown x%02x", fsg->common->cmnd[0]);
1960 if ((reply = check_command(fsg, fsg->common->cmnd_size, 2047 reply = check_command(fsg, fsg->common->cmnd_size,
1961 DATA_DIR_UNKNOWN, 0xff, 0, unknown)) == 0) { 2048 DATA_DIR_UNKNOWN, 0xff, 0, unknown);
2049 if (reply == 0) {
1962 fsg->common->curlun->sense_data = SS_INVALID_COMMAND; 2050 fsg->common->curlun->sense_data = SS_INVALID_COMMAND;
1963 reply = -EINVAL; 2051 reply = -EINVAL;
1964 } 2052 }
@@ -1971,13 +2059,13 @@ static int do_scsi_command(struct fsg_dev *fsg)
1971 2059
1972 /* Set up the single reply buffer for finish_reply() */ 2060 /* Set up the single reply buffer for finish_reply() */
1973 if (reply == -EINVAL) 2061 if (reply == -EINVAL)
1974 reply = 0; // Error reply length 2062 reply = 0; /* Error reply length */
1975 if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) { 2063 if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
1976 reply = min((u32) reply, fsg->data_size_from_cmnd); 2064 reply = min((u32) reply, fsg->data_size_from_cmnd);
1977 bh->inreq->length = reply; 2065 bh->inreq->length = reply;
1978 bh->state = BUF_STATE_FULL; 2066 bh->state = BUF_STATE_FULL;
1979 fsg->residue -= reply; 2067 fsg->residue -= reply;
1980 } // Otherwise it's already set 2068 } /* Otherwise it's already set */
1981 2069
1982 return 0; 2070 return 0;
1983} 2071}
@@ -2157,13 +2245,15 @@ reset:
2157 /* Enable the endpoints */ 2245 /* Enable the endpoints */
2158 d = fsg_ep_desc(fsg->gadget, 2246 d = fsg_ep_desc(fsg->gadget,
2159 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc); 2247 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
2160 if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0) 2248 rc = enable_endpoint(fsg, fsg->bulk_in, d);
2249 if (rc != 0)
2161 goto reset; 2250 goto reset;
2162 fsg->bulk_in_enabled = 1; 2251 fsg->bulk_in_enabled = 1;
2163 2252
2164 d = fsg_ep_desc(fsg->gadget, 2253 d = fsg_ep_desc(fsg->gadget,
2165 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc); 2254 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
2166 if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0) 2255 rc = enable_endpoint(fsg, fsg->bulk_out, d);
2256 if (rc != 0)
2167 goto reset; 2257 goto reset;
2168 fsg->bulk_out_enabled = 1; 2258 fsg->bulk_out_enabled = 1;
2169 fsg->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize); 2259 fsg->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
@@ -2173,9 +2263,11 @@ reset:
2173 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2263 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2174 struct fsg_buffhd *bh = &fsg->common->buffhds[i]; 2264 struct fsg_buffhd *bh = &fsg->common->buffhds[i];
2175 2265
2176 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0) 2266 rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq);
2267 if (rc != 0)
2177 goto reset; 2268 goto reset;
2178 if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0) 2269 rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq);
2270 if (rc != 0)
2179 goto reset; 2271 goto reset;
2180 bh->inreq->buf = bh->outreq->buf = bh->buf; 2272 bh->inreq->buf = bh->outreq->buf = bh->buf;
2181 bh->inreq->context = bh->outreq->context = bh; 2273 bh->inreq->context = bh->outreq->context = bh;
@@ -2302,9 +2394,8 @@ static void handle_exception(struct fsg_dev *fsg)
2302 bh = &fsg->common->buffhds[i]; 2394 bh = &fsg->common->buffhds[i];
2303 bh->state = BUF_STATE_EMPTY; 2395 bh->state = BUF_STATE_EMPTY;
2304 } 2396 }
2305 fsg->common->next_buffhd_to_fill = fsg->common->next_buffhd_to_drain = 2397 fsg->common->next_buffhd_to_fill = &fsg->common->buffhds[0];
2306 &fsg->common->buffhds[0]; 2398 fsg->common->next_buffhd_to_drain = &fsg->common->buffhds[0];
2307
2308 exception_req_tag = fsg->exception_req_tag; 2399 exception_req_tag = fsg->exception_req_tag;
2309 new_config = fsg->new_config; 2400 new_config = fsg->new_config;
2310 old_state = fsg->state; 2401 old_state = fsg->state;
@@ -2315,8 +2406,8 @@ static void handle_exception(struct fsg_dev *fsg)
2315 for (i = 0; i < fsg->common->nluns; ++i) { 2406 for (i = 0; i < fsg->common->nluns; ++i) {
2316 curlun = &fsg->common->luns[i]; 2407 curlun = &fsg->common->luns[i];
2317 curlun->prevent_medium_removal = 0; 2408 curlun->prevent_medium_removal = 0;
2318 curlun->sense_data = curlun->unit_attention_data = 2409 curlun->sense_data = SS_NO_SENSE;
2319 SS_NO_SENSE; 2410 curlun->unit_attention_data = SS_NO_SENSE;
2320 curlun->sense_data_info = 0; 2411 curlun->sense_data_info = 0;
2321 curlun->info_valid = 0; 2412 curlun->info_valid = 0;
2322 } 2413 }
@@ -2342,30 +2433,31 @@ static void handle_exception(struct fsg_dev *fsg)
2342 usb_ep_clear_halt(fsg->bulk_in); 2433 usb_ep_clear_halt(fsg->bulk_in);
2343 2434
2344 if (fsg->ep0_req_tag == exception_req_tag) 2435 if (fsg->ep0_req_tag == exception_req_tag)
2345 ep0_queue(fsg); // Complete the status stage 2436 ep0_queue(fsg); /* Complete the status stage */
2346 2437
2347 /* Technically this should go here, but it would only be 2438 /* Technically this should go here, but it would only be
2348 * a waste of time. Ditto for the INTERFACE_CHANGE and 2439 * a waste of time. Ditto for the INTERFACE_CHANGE and
2349 * CONFIG_CHANGE cases. */ 2440 * CONFIG_CHANGE cases. */
2350 // for (i = 0; i < fsg->common->nluns; ++i) 2441 /* for (i = 0; i < fsg->common->nluns; ++i) */
2351 // fsg->common->luns[i].unit_attention_data = SS_RESET_OCCURRED; 2442 /* fsg->common->luns[i].unit_attention_data = */
2443 /* SS_RESET_OCCURRED; */
2352 break; 2444 break;
2353 2445
2354 case FSG_STATE_CONFIG_CHANGE: 2446 case FSG_STATE_CONFIG_CHANGE:
2355 rc = do_set_config(fsg, new_config); 2447 rc = do_set_config(fsg, new_config);
2356 if (fsg->ep0_req_tag != exception_req_tag) 2448 if (fsg->ep0_req_tag != exception_req_tag)
2357 break; 2449 break;
2358 if (rc != 0) // STALL on errors 2450 if (rc != 0) /* STALL on errors */
2359 fsg_set_halt(fsg, fsg->ep0); 2451 fsg_set_halt(fsg, fsg->ep0);
2360 else // Complete the status stage 2452 else /* Complete the status stage */
2361 ep0_queue(fsg); 2453 ep0_queue(fsg);
2362 break; 2454 break;
2363 2455
2364 case FSG_STATE_EXIT: 2456 case FSG_STATE_EXIT:
2365 case FSG_STATE_TERMINATED: 2457 case FSG_STATE_TERMINATED:
2366 do_set_config(fsg, 0); // Free resources 2458 do_set_config(fsg, 0); /* Free resources */
2367 spin_lock_irq(&fsg->lock); 2459 spin_lock_irq(&fsg->lock);
2368 fsg->state = FSG_STATE_TERMINATED; // Stop the thread 2460 fsg->state = FSG_STATE_TERMINATED; /* Stop the thread */
2369 spin_unlock_irq(&fsg->lock); 2461 spin_unlock_irq(&fsg->lock);
2370 break; 2462 break;
2371 2463
@@ -2645,7 +2737,8 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2645error_luns: 2737error_luns:
2646 common->nluns = i + 1; 2738 common->nluns = i + 1;
2647error_release: 2739error_release:
2648 /* Call fsg_common_release() directly, ref is not initialised */ 2740 /* Call fsg_common_release() directly, ref might be not
2741 * initialised */
2649 fsg_common_release(&common->ref); 2742 fsg_common_release(&common->ref);
2650 return ERR_PTR(rc); 2743 return ERR_PTR(rc);
2651} 2744}
@@ -2721,13 +2814,13 @@ static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
2721 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc); 2814 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
2722 if (!ep) 2815 if (!ep)
2723 goto autoconf_fail; 2816 goto autoconf_fail;
2724 ep->driver_data = fsg; // claim the endpoint 2817 ep->driver_data = fsg; /* claim the endpoint */
2725 fsg->bulk_in = ep; 2818 fsg->bulk_in = ep;
2726 2819
2727 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc); 2820 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
2728 if (!ep) 2821 if (!ep)
2729 goto autoconf_fail; 2822 goto autoconf_fail;
2730 ep->driver_data = fsg; // claim the endpoint 2823 ep->driver_data = fsg; /* claim the endpoint */
2731 fsg->bulk_out = ep; 2824 fsg->bulk_out = ep;
2732 2825
2733 if (gadget_is_dualspeed(gadget)) { 2826 if (gadget_is_dualspeed(gadget)) {
@@ -2770,7 +2863,7 @@ autoconf_fail:
2770 rc = -ENOTSUPP; 2863 rc = -ENOTSUPP;
2771 2864
2772out: 2865out:
2773 fsg->state = FSG_STATE_TERMINATED; // The thread is dead 2866 fsg->state = FSG_STATE_TERMINATED; /* The thread is dead */
2774 fsg_unbind(c, f); 2867 fsg_unbind(c, f);
2775 complete(&fsg->thread_notifier); 2868 complete(&fsg->thread_notifier);
2776 return rc; 2869 return rc;
@@ -2874,18 +2967,16 @@ fsg_config_from_params(struct fsg_config *cfg,
2874 const struct fsg_module_parameters *params) 2967 const struct fsg_module_parameters *params)
2875{ 2968{
2876 struct fsg_lun_config *lun; 2969 struct fsg_lun_config *lun;
2877 unsigned i, nluns; 2970 unsigned i;
2878 2971
2879 /* Configure LUNs */ 2972 /* Configure LUNs */
2880 nluns = cfg->nluns = !params->luns 2973 cfg->nluns =
2881 ? params->file_count ? params->file_count : 1 2974 min(params->luns ?: (params->file_count ?: 1u),
2882 : params->luns; 2975 (unsigned)FSG_MAX_LUNS);
2883 for (i = 0, lun = cfg->luns; 2976 for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) {
2884 i < FSG_MAX_LUNS && i < nluns;
2885 ++i, ++lun) {
2886 lun->ro = !!params->ro[i]; 2977 lun->ro = !!params->ro[i];
2887 lun->cdrom = !!params->cdrom[i]; 2978 lun->cdrom = !!params->cdrom[i];
2888 lun->removable = 2979 lun->removable = /* Removable by default */
2889 params->removable_count <= i || params->removable[i]; 2980 params->removable_count <= i || params->removable[i];
2890 lun->filename = 2981 lun->filename =
2891 params->file_count > i && params->file[i][0] 2982 params->file_count > i && params->file[i][0]
@@ -2893,7 +2984,7 @@ fsg_config_from_params(struct fsg_config *cfg,
2893 : 0; 2984 : 0;
2894 } 2985 }
2895 2986
2896 /* Let FSG use defaults */ 2987 /* Let MSF use defaults */
2897 cfg->lun_name_format = 0; 2988 cfg->lun_name_format = 0;
2898 cfg->thread_name = 0; 2989 cfg->thread_name = 0;
2899 cfg->vendor_name = 0; 2990 cfg->vendor_name = 0;
diff --git a/drivers/usb/gadget/mass_storage.c b/drivers/usb/gadget/mass_storage.c
index 00396a36d01..e43ca8f4be1 100644
--- a/drivers/usb/gadget/mass_storage.c
+++ b/drivers/usb/gadget/mass_storage.c
@@ -1,10 +1,10 @@
1/* 1/*
2 * mass_storage.c -- File-backed USB Storage Gadget, for USB development 2 * mass_storage.c -- Mass Storage USB Gadget
3 * 3 *
4 * Copyright (C) 2003-2008 Alan Stern 4 * Copyright (C) 2003-2008 Alan Stern
5 *
6 * Copyright (C) 2009 Samsung Electronics 5 * Copyright (C) 2009 Samsung Electronics
7 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com> 6 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
7 * All rights reserved.
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
@@ -45,7 +45,7 @@
45/*-------------------------------------------------------------------------*/ 45/*-------------------------------------------------------------------------*/
46 46
47#define DRIVER_DESC "Mass Storage Gadget" 47#define DRIVER_DESC "Mass Storage Gadget"
48#define DRIVER_VERSION "2009/07/21" 48#define DRIVER_VERSION "2009/09/11"
49 49
50/*-------------------------------------------------------------------------*/ 50/*-------------------------------------------------------------------------*/
51 51
diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c
index 7e81a2d898f..868d8ee8675 100644
--- a/drivers/usb/gadget/storage_common.c
+++ b/drivers/usb/gadget/storage_common.c
@@ -61,8 +61,8 @@
61 * 61 *
62 * DO NOT REUSE THESE IDs with any other driver!! Ever!! 62 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
63 * Instead: allocate your own, using normal USB-IF procedures. */ 63 * Instead: allocate your own, using normal USB-IF procedures. */
64#define FSG_VENDOR_ID 0x0525 // NetChip 64#define FSG_VENDOR_ID 0x0525 /* NetChip */
65#define FSG_PRODUCT_ID 0xa4a5 // Linux-USB File-backed Storage Gadget 65#define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
66 66
67 67
68/*-------------------------------------------------------------------------*/ 68/*-------------------------------------------------------------------------*/
@@ -103,7 +103,7 @@
103#ifdef DUMP_MSGS 103#ifdef DUMP_MSGS
104 104
105# define dump_msg(fsg, /* const char * */ label, \ 105# define dump_msg(fsg, /* const char * */ label, \
106 /* const u8 * */ buf, /* unsigned */ length) do { \ 106 /* const u8 * */ buf, /* unsigned */ length) do { \
107 if (length < 512) { \ 107 if (length < 512) { \
108 DBG(fsg, "%s, length %u:\n", label, length); \ 108 DBG(fsg, "%s, length %u:\n", label, length); \
109 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \ 109 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
@@ -116,11 +116,11 @@
116#else 116#else
117 117
118# define dump_msg(fsg, /* const char * */ label, \ 118# define dump_msg(fsg, /* const char * */ label, \
119 /* const u8 * */ buf, /* unsigned */ length) do { } while (0) 119 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
120 120
121# ifdef VERBOSE_DEBUG 121# ifdef VERBOSE_DEBUG
122 122
123#define dump_cdb(fsg) \ 123# define dump_cdb(fsg) \
124 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \ 124 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
125 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \ 125 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
126 126
@@ -143,45 +143,45 @@
143#define TYPE_CDROM 0x05 143#define TYPE_CDROM 0x05
144 144
145/* USB protocol value = the transport method */ 145/* USB protocol value = the transport method */
146#define USB_PR_CBI 0x00 // Control/Bulk/Interrupt 146#define USB_PR_CBI 0x00 /* Control/Bulk/Interrupt */
147#define USB_PR_CB 0x01 // Control/Bulk w/o interrupt 147#define USB_PR_CB 0x01 /* Control/Bulk w/o interrupt */
148#define USB_PR_BULK 0x50 // Bulk-only 148#define USB_PR_BULK 0x50 /* Bulk-only */
149 149
150/* USB subclass value = the protocol encapsulation */ 150/* USB subclass value = the protocol encapsulation */
151#define USB_SC_RBC 0x01 // Reduced Block Commands (flash) 151#define USB_SC_RBC 0x01 /* Reduced Block Commands (flash) */
152#define USB_SC_8020 0x02 // SFF-8020i, MMC-2, ATAPI (CD-ROM) 152#define USB_SC_8020 0x02 /* SFF-8020i, MMC-2, ATAPI (CD-ROM) */
153#define USB_SC_QIC 0x03 // QIC-157 (tape) 153#define USB_SC_QIC 0x03 /* QIC-157 (tape) */
154#define USB_SC_UFI 0x04 // UFI (floppy) 154#define USB_SC_UFI 0x04 /* UFI (floppy) */
155#define USB_SC_8070 0x05 // SFF-8070i (removable) 155#define USB_SC_8070 0x05 /* SFF-8070i (removable) */
156#define USB_SC_SCSI 0x06 // Transparent SCSI 156#define USB_SC_SCSI 0x06 /* Transparent SCSI */
157 157
158/* Bulk-only data structures */ 158/* Bulk-only data structures */
159 159
160/* Command Block Wrapper */ 160/* Command Block Wrapper */
161struct fsg_bulk_cb_wrap { 161struct fsg_bulk_cb_wrap {
162 __le32 Signature; // Contains 'USBC' 162 __le32 Signature; /* Contains 'USBC' */
163 u32 Tag; // Unique per command id 163 u32 Tag; /* Unique per command id */
164 __le32 DataTransferLength; // Size of the data 164 __le32 DataTransferLength; /* Size of the data */
165 u8 Flags; // Direction in bit 7 165 u8 Flags; /* Direction in bit 7 */
166 u8 Lun; // LUN (normally 0) 166 u8 Lun; /* LUN (normally 0) */
167 u8 Length; // Of the CDB, <= MAX_COMMAND_SIZE 167 u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
168 u8 CDB[16]; // Command Data Block 168 u8 CDB[16]; /* Command Data Block */
169}; 169};
170 170
171#define USB_BULK_CB_WRAP_LEN 31 171#define USB_BULK_CB_WRAP_LEN 31
172#define USB_BULK_CB_SIG 0x43425355 // Spells out USBC 172#define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
173#define USB_BULK_IN_FLAG 0x80 173#define USB_BULK_IN_FLAG 0x80
174 174
175/* Command Status Wrapper */ 175/* Command Status Wrapper */
176struct bulk_cs_wrap { 176struct bulk_cs_wrap {
177 __le32 Signature; // Should = 'USBS' 177 __le32 Signature; /* Should = 'USBS' */
178 u32 Tag; // Same as original command 178 u32 Tag; /* Same as original command */
179 __le32 Residue; // Amount not transferred 179 __le32 Residue; /* Amount not transferred */
180 u8 Status; // See below 180 u8 Status; /* See below */
181}; 181};
182 182
183#define USB_BULK_CS_WRAP_LEN 13 183#define USB_BULK_CS_WRAP_LEN 13
184#define USB_BULK_CS_SIG 0x53425355 // Spells out 'USBS' 184#define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
185#define USB_STATUS_PASS 0 185#define USB_STATUS_PASS 0
186#define USB_STATUS_FAIL 1 186#define USB_STATUS_FAIL 1
187#define USB_STATUS_PHASE_ERROR 2 187#define USB_STATUS_PHASE_ERROR 2
@@ -203,7 +203,8 @@ struct interrupt_data {
203#define USB_CBI_ADSC_REQUEST 0x00 203#define USB_CBI_ADSC_REQUEST 0x00
204 204
205 205
206#define MAX_COMMAND_SIZE 16 // Length of a SCSI Command Data Block 206/* Length of a SCSI Command Data Block */
207#define MAX_COMMAND_SIZE 16
207 208
208/* SCSI commands that we recognize */ 209/* SCSI commands that we recognize */
209#define SC_FORMAT_UNIT 0x04 210#define SC_FORMAT_UNIT 0x04
@@ -248,7 +249,7 @@ struct interrupt_data {
248#define SS_WRITE_ERROR 0x030c02 249#define SS_WRITE_ERROR 0x030c02
249#define SS_WRITE_PROTECTED 0x072700 250#define SS_WRITE_PROTECTED 0x072700
250 251
251#define SK(x) ((u8) ((x) >> 16)) // Sense Key byte, etc. 252#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
252#define ASC(x) ((u8) ((x) >> 8)) 253#define ASC(x) ((u8) ((x) >> 8))
253#define ASCQ(x) ((u8) (x)) 254#define ASCQ(x) ((u8) (x))
254 255
@@ -261,13 +262,13 @@ struct fsg_lun {
261 loff_t file_length; 262 loff_t file_length;
262 loff_t num_sectors; 263 loff_t num_sectors;
263 264
264 unsigned int initially_ro : 1; 265 unsigned int initially_ro:1;
265 unsigned int ro : 1; 266 unsigned int ro:1;
266 unsigned int removable : 1; 267 unsigned int removable:1;
267 unsigned int cdrom : 1; 268 unsigned int cdrom:1;
268 unsigned int prevent_medium_removal : 1; 269 unsigned int prevent_medium_removal:1;
269 unsigned int registered : 1; 270 unsigned int registered:1;
270 unsigned int info_valid : 1; 271 unsigned int info_valid:1;
271 272
272 u32 sense_data; 273 u32 sense_data;
273 u32 sense_data_info; 274 u32 sense_data_info;
@@ -286,7 +287,7 @@ static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
286 287
287/* Big enough to hold our biggest descriptor */ 288/* Big enough to hold our biggest descriptor */
288#define EP0_BUFSIZE 256 289#define EP0_BUFSIZE 256
289#define DELAYED_STATUS (EP0_BUFSIZE + 999) // An impossibly large value 290#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
290 291
291/* Number of buffers we will use. 2 is enough for double-buffering */ 292/* Number of buffers we will use. 2 is enough for double-buffering */
292#define FSG_NUM_BUFFERS 2 293#define FSG_NUM_BUFFERS 2
@@ -324,7 +325,8 @@ struct fsg_buffhd {
324}; 325};
325 326
326enum fsg_state { 327enum fsg_state {
327 FSG_STATE_COMMAND_PHASE = -10, // This one isn't used anywhere 328 /* This one isn't used anywhere */
329 FSG_STATE_COMMAND_PHASE = -10,
328 FSG_STATE_DATA_PHASE, 330 FSG_STATE_DATA_PHASE,
329 FSG_STATE_STATUS_PHASE, 331 FSG_STATE_STATUS_PHASE,
330 332
@@ -386,10 +388,10 @@ fsg_intf_desc = {
386 .bLength = sizeof fsg_intf_desc, 388 .bLength = sizeof fsg_intf_desc,
387 .bDescriptorType = USB_DT_INTERFACE, 389 .bDescriptorType = USB_DT_INTERFACE,
388 390
389 .bNumEndpoints = 2, // Adjusted during fsg_bind() 391 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
390 .bInterfaceClass = USB_CLASS_MASS_STORAGE, 392 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
391 .bInterfaceSubClass = USB_SC_SCSI, // Adjusted during fsg_bind() 393 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
392 .bInterfaceProtocol = USB_PR_BULK, // Adjusted during fsg_bind() 394 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
393 .iInterface = FSG_STRING_INTERFACE, 395 .iInterface = FSG_STRING_INTERFACE,
394}; 396};
395 397
@@ -426,7 +428,7 @@ fsg_fs_intr_in_desc = {
426 .bEndpointAddress = USB_DIR_IN, 428 .bEndpointAddress = USB_DIR_IN,
427 .bmAttributes = USB_ENDPOINT_XFER_INT, 429 .bmAttributes = USB_ENDPOINT_XFER_INT,
428 .wMaxPacketSize = cpu_to_le16(2), 430 .wMaxPacketSize = cpu_to_le16(2),
429 .bInterval = 32, // frames -> 32 ms 431 .bInterval = 32, /* frames -> 32 ms */
430}; 432};
431 433
432#ifndef FSG_NO_OTG 434#ifndef FSG_NO_OTG
@@ -477,7 +479,7 @@ fsg_hs_bulk_out_desc = {
477 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */ 479 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
478 .bmAttributes = USB_ENDPOINT_XFER_BULK, 480 .bmAttributes = USB_ENDPOINT_XFER_BULK,
479 .wMaxPacketSize = cpu_to_le16(512), 481 .wMaxPacketSize = cpu_to_le16(512),
480 .bInterval = 1, // NAK every 1 uframe 482 .bInterval = 1, /* NAK every 1 uframe */
481}; 483};
482 484
483#ifndef FSG_NO_INTR_EP 485#ifndef FSG_NO_INTR_EP
@@ -490,7 +492,7 @@ fsg_hs_intr_in_desc = {
490 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */ 492 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
491 .bmAttributes = USB_ENDPOINT_XFER_INT, 493 .bmAttributes = USB_ENDPOINT_XFER_INT,
492 .wMaxPacketSize = cpu_to_le16(2), 494 .wMaxPacketSize = cpu_to_le16(2),
493 .bInterval = 9, // 2**(9-1) = 256 uframes -> 32 ms 495 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
494}; 496};
495 497
496#ifndef FSG_NO_OTG 498#ifndef FSG_NO_OTG
@@ -538,7 +540,7 @@ static struct usb_string fsg_strings[] = {
538}; 540};
539 541
540static struct usb_gadget_strings fsg_stringtab = { 542static struct usb_gadget_strings fsg_stringtab = {
541 .language = 0x0409, // en-us 543 .language = 0x0409, /* en-us */
542 .strings = fsg_strings, 544 .strings = fsg_strings,
543}; 545};
544 546
@@ -600,11 +602,11 @@ static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
600 rc = (int) size; 602 rc = (int) size;
601 goto out; 603 goto out;
602 } 604 }
603 num_sectors = size >> 9; // File size in 512-byte blocks 605 num_sectors = size >> 9; /* File size in 512-byte blocks */
604 min_sectors = 1; 606 min_sectors = 1;
605 if (curlun->cdrom) { 607 if (curlun->cdrom) {
606 num_sectors &= ~3; // Reduce to a multiple of 2048 608 num_sectors &= ~3; /* Reduce to a multiple of 2048 */
607 min_sectors = 300*4; // Smallest track is 300 frames 609 min_sectors = 300*4; /* Smallest track is 300 frames */
608 if (num_sectors >= 256*60*75*4) { 610 if (num_sectors >= 256*60*75*4) {
609 num_sectors = (256*60*75 - 1) * 4; 611 num_sectors = (256*60*75 - 1) * 4;
610 LINFO(curlun, "file too big: %s\n", filename); 612 LINFO(curlun, "file too big: %s\n", filename);
@@ -696,17 +698,17 @@ static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
696 ssize_t rc; 698 ssize_t rc;
697 699
698 down_read(filesem); 700 down_read(filesem);
699 if (fsg_lun_is_open(curlun)) { // Get the complete pathname 701 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
700 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1); 702 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
701 if (IS_ERR(p)) 703 if (IS_ERR(p))
702 rc = PTR_ERR(p); 704 rc = PTR_ERR(p);
703 else { 705 else {
704 rc = strlen(p); 706 rc = strlen(p);
705 memmove(buf, p, rc); 707 memmove(buf, p, rc);
706 buf[rc] = '\n'; // Add a newline 708 buf[rc] = '\n'; /* Add a newline */
707 buf[++rc] = 0; 709 buf[++rc] = 0;
708 } 710 }
709 } else { // No file, return 0 bytes 711 } else { /* No file, return 0 bytes */
710 *buf = 0; 712 *buf = 0;
711 rc = 0; 713 rc = 0;
712 } 714 }
@@ -750,12 +752,12 @@ static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
750 752
751 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) { 753 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
752 LDBG(curlun, "eject attempt prevented\n"); 754 LDBG(curlun, "eject attempt prevented\n");
753 return -EBUSY; // "Door is locked" 755 return -EBUSY; /* "Door is locked" */
754 } 756 }
755 757
756 /* Remove a trailing newline */ 758 /* Remove a trailing newline */
757 if (count > 0 && buf[count-1] == '\n') 759 if (count > 0 && buf[count-1] == '\n')
758 ((char *) buf)[count-1] = 0; // Ugh! 760 ((char *) buf)[count-1] = 0; /* Ugh! */
759 761
760 /* Eject current medium */ 762 /* Eject current medium */
761 down_write(filesem); 763 down_write(filesem);