239 #include <linux/device.h>
240 #include <linux/fcntl.h>
242 #include <linux/fs.h>
246 #include <linux/module.h>
248 #include <linux/slab.h>
250 #include <linux/string.h>
252 #include <linux/utsname.h>
255 #include <linux/usb/ch9.h>
260 #define DRIVER_DESC "File-backed Storage Gadget"
261 #define DRIVER_NAME "g_file_storage"
262 #define DRIVER_VERSION "1 September 2010"
264 static char fsg_string_manufacturer[64];
265 static const char fsg_string_product[] =
DRIVER_DESC;
266 static const char fsg_string_config[] =
"Self-powered";
267 static const char fsg_string_interface[] =
"Mass Storage";
316 .transport_parm =
"BBB",
317 .protocol_parm =
"SCSI",
356 #ifdef CONFIG_USB_FILE_STORAGE_TEST
387 #ifdef CONFIG_USB_FILE_STORAGE_TEST
389 #define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
390 #define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
391 #define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
395 #define transport_is_bbb() 1
396 #define transport_is_cbi() 0
397 #define protocol_is_scsi() 1
441 #define IGNORE_BULK_OUT 1
482 static int exception_in_progress(
struct fsg_dev *fsg)
488 static void set_bulk_out_req_length(
struct fsg_dev *fsg,
500 static struct fsg_dev *the_fsg;
506 static int fsg_set_halt(
struct fsg_dev *fsg,
struct usb_ep *ep)
516 DBG(fsg,
"%s set halt\n", name);
517 return usb_ep_set_halt(ep);
530 #define CONFIG_VALUE 1
534 .bLength =
sizeof device_desc,
548 .bNumConfigurations = 1,
553 .bLength =
sizeof config_desc,
561 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
567 .bLength =
sizeof dev_qualifier,
573 .bNumConfigurations = 1,
595 static int populate_config_buf(
struct usb_gadget *gadget,
612 if (!gadget_is_otg(gadget))
629 fsg->thread_wakeup_needed = 1;
630 if (fsg->thread_task)
643 if (fsg->
state <= new_state) {
650 spin_unlock_irqrestore(&fsg->
lock, flags);
661 static void fsg_disconnect(
struct usb_gadget *gadget)
663 struct fsg_dev *fsg = get_gadget_data(gadget);
665 DBG(fsg,
"disconnect or port reset\n");
670 static int ep0_queue(
struct fsg_dev *fsg)
678 WARNING(fsg,
"error in submission: %s --> %d\n",
691 DBG(fsg,
"%s --> %d, %u/%u\n", __func__,
694 usb_ep_fifo_flush(ep);
712 DBG(fsg,
"%s --> %d, %u/%u\n", __func__,
715 usb_ep_fifo_flush(ep);
719 spin_lock(&fsg->
lock);
723 spin_unlock(&fsg->
lock);
733 DBG(fsg,
"%s --> %d, %u/%u\n", __func__,
737 usb_ep_fifo_flush(ep);
741 spin_lock(&fsg->
lock);
745 spin_unlock(&fsg->
lock);
749 #ifdef CONFIG_USB_FILE_STORAGE_TEST
756 DBG(fsg,
"%s --> %d, %u/%u\n", __func__,
759 usb_ep_fifo_flush(ep);
763 spin_lock(&fsg->
lock);
767 spin_unlock(&fsg->
lock);
780 #ifdef CONFIG_USB_FILE_STORAGE_TEST
784 static u8 cbi_reset_cmnd[6] = {
793 fsg_set_halt(fsg, fsg->
ep0);
798 if (req->
actual >=
sizeof cbi_reset_cmnd &&
800 sizeof cbi_reset_cmnd) == 0) {
804 DBG(fsg,
"cbi reset request\n");
809 VDBG(fsg,
"CB[I] accept device-specific command\n");
810 spin_lock(&fsg->
lock);
814 WARNING(fsg,
"CB[I] overwriting previous command\n");
819 spin_unlock(&fsg->
lock);
828 static int class_setup_req(
struct fsg_dev *fsg,
848 if (w_index != 0 || w_value != 0 || w_length != 0) {
855 DBG(fsg,
"bulk reset request\n");
864 if (w_index != 0 || w_value != 0 || w_length != 1) {
868 VDBG(fsg,
"get max LUN\n");
883 if (w_index != 0 || w_value != 0) {
892 fsg->
ep0req->context = received_cbi_adsc;
899 "unknown class-specific control req "
900 "%02x.%02x v%04x i%04x l%u\n",
911 static int standard_setup_req(
struct fsg_dev *fsg,
927 switch (w_value >> 8) {
930 VDBG(fsg,
"get device descriptor\n");
932 value =
sizeof device_desc;
936 VDBG(fsg,
"get device qualifier\n");
937 if (!gadget_is_dualspeed(fsg->
gadget) ||
945 value =
sizeof dev_qualifier;
950 VDBG(fsg,
"get other-speed config descriptor\n");
951 if (!gadget_is_dualspeed(fsg->
gadget) ||
956 VDBG(fsg,
"get configuration descriptor\n");
958 value = populate_config_buf(fsg->
gadget,
965 VDBG(fsg,
"get string descriptor\n");
969 w_value & 0xff, req->
buf);
973 VDBG(fsg,
"get bos descriptor\n");
975 if (gadget_is_superspeed(fsg->
gadget))
976 value = populate_bos(fsg, req->
buf);
987 VDBG(fsg,
"set configuration\n");
1001 VDBG(fsg,
"get configuration\n");
1010 if (fsg->
config && w_index == 0) {
1029 VDBG(fsg,
"get interface\n");
1030 *(
u8 *) req->
buf = 0;
1036 "unknown control req %02x.%02x v%04x i%04x l%u\n",
1045 static int fsg_setup(
struct usb_gadget *gadget,
1048 struct fsg_dev *fsg = get_gadget_data(gadget);
1055 dump_msg(fsg,
"ep0-setup", (
u8 *) ctrl,
sizeof(*ctrl));
1058 rc = class_setup_req(fsg, ctrl);
1060 rc = standard_setup_req(fsg, ctrl);
1064 rc =
min(rc, w_length);
1068 "ep0-in" :
"ep0-out");
1069 rc = ep0_queue(fsg);
1083 static void start_transfer(
struct fsg_dev *fsg,
struct usb_ep *ep,
1094 spin_lock_irq(&fsg->
lock);
1097 spin_unlock_irq(&fsg->
lock);
1109 WARNING(fsg,
"error in submission: %s --> %d\n",
1115 static int sleep_thread(
struct fsg_dev *fsg)
1123 if (signal_pending(
current)) {
1147 unsigned int amount;
1153 lba = get_unaligned_be24(&fsg->
cmnd[1]);
1160 if ((fsg->
cmnd[1] & ~0x18) != 0) {
1169 file_offset = ((loff_t) lba) << curlun->
blkbits;
1183 amount =
min((
unsigned int) amount_left, mod_data.buflen);
1184 amount =
min((loff_t) amount,
1190 rc = sleep_thread(fsg);
1202 bh->
inreq->length = 0;
1210 (
char __user *) bh->
buf,
1211 amount, &file_offset_tmp);
1212 VLDBG(curlun,
"file read %u @ %llu -> %d\n", amount,
1213 (
unsigned long long) file_offset,
1219 LDBG(curlun,
"error in file read: %d\n",
1222 }
else if (nread < amount) {
1223 LDBG(curlun,
"partial file read: %d/%u\n",
1224 (
int) nread, amount);
1227 file_offset += nread;
1228 amount_left -= nread;
1235 bh->
inreq->length = nread;
1239 if (nread < amount) {
1246 if (amount_left == 0)
1250 bh->
inreq->zero = 0;
1268 u32 amount_left_to_req, amount_left_to_write;
1270 unsigned int amount;
1278 spin_lock(&curlun->
filp->f_lock);
1280 spin_unlock(&curlun->
filp->f_lock);
1285 lba = get_unaligned_be24(&fsg->
cmnd[1]);
1293 if ((fsg->
cmnd[1] & ~0x18) != 0) {
1298 if (!curlun->
nofua && (fsg->
cmnd[1] & 0x08)) {
1299 spin_lock(&curlun->
filp->f_lock);
1301 spin_unlock(&curlun->
filp->f_lock);
1311 file_offset = usb_offset = ((loff_t) lba) << curlun->
blkbits;
1314 while (amount_left_to_write > 0) {
1324 amount =
min(amount_left_to_req, mod_data.buflen);
1337 usb_offset += amount;
1339 amount_left_to_req -= amount;
1340 if (amount_left_to_req == 0)
1347 set_bulk_out_req_length(fsg, bh, amount);
1364 if (bh->
outreq->status != 0) {
1371 amount = bh->
outreq->actual;
1374 "write %u @ %llu beyond end %llu\n",
1375 amount, (
unsigned long long) file_offset,
1393 (
char __user *) bh->
buf,
1394 amount, &file_offset_tmp);
1395 VLDBG(curlun,
"file write %u @ %llu -> %d\n", amount,
1396 (
unsigned long long) file_offset,
1402 LDBG(curlun,
"error in file write: %d\n",
1405 }
else if (nwritten < amount) {
1406 LDBG(curlun,
"partial file write: %d/%u\n",
1407 (
int) nwritten, amount);
1410 file_offset += nwritten;
1411 amount_left_to_write -= nwritten;
1415 if (nwritten < amount) {
1432 rc = sleep_thread(fsg);
1443 static int do_synchronize_cache(
struct fsg_dev *fsg)
1450 rc = fsg_lun_fsync_sub(curlun);
1459 static void invalidate_sub(
struct fsg_lun *curlun)
1466 VLDBG(curlun,
"invalidate_mapping_pages -> %ld\n", rc);
1469 static int do_verify(
struct fsg_dev *fsg)
1473 u32 verification_length;
1477 unsigned int amount;
1490 if ((fsg->
cmnd[1] & ~0x10) != 0) {
1495 verification_length = get_unaligned_be16(&fsg->
cmnd[7]);
1496 if (
unlikely(verification_length == 0))
1500 amount_left = verification_length << curlun->
blkbits;
1501 file_offset = ((loff_t) lba) << curlun->
blkbits;
1504 fsg_lun_fsync_sub(curlun);
1508 invalidate_sub(curlun);
1513 while (amount_left > 0) {
1520 amount =
min((
unsigned int) amount_left, mod_data.buflen);
1521 amount =
min((loff_t) amount,
1534 (
char __user *) bh->
buf,
1535 amount, &file_offset_tmp);
1536 VLDBG(curlun,
"file read %u @ %llu -> %d\n", amount,
1537 (
unsigned long long) file_offset,
1543 LDBG(curlun,
"error in file verify: %d\n",
1546 }
else if (nread < amount) {
1547 LDBG(curlun,
"partial file verify: %d/%u\n",
1548 (
int) nread, amount);
1557 file_offset += nread;
1558 amount_left -= nread;
1571 static char product_disk_id[] =
"File-Stor Gadget";
1572 static char product_cdrom_id[] =
"File-CD Gadget ";
1584 if (mod_data.removable)
1591 (mod_data.cdrom ? product_cdrom_id :
1642 buf[0] =
valid | 0x70;
1656 int pmi = fsg->
cmnd[8];
1660 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1675 int msf = fsg->
cmnd[1] & 0x02;
1679 if ((fsg->
cmnd[1] & ~0x02) != 0) {
1690 store_cdrom_address(&buf[4], msf, lba);
1698 int msf = fsg->
cmnd[1] & 0x02;
1699 int start_track = fsg->
cmnd[6];
1702 if ((fsg->
cmnd[1] & ~0x02) != 0 ||
1714 store_cdrom_address(&buf[8], msf, 0);
1718 store_cdrom_address(&buf[16], msf, curlun->
num_sectors);
1726 int mscmnd = fsg->
cmnd[0];
1730 int changeable_values, all_pages;
1734 if ((fsg->
cmnd[1] & ~0x08) != 0) {
1738 pc = fsg->
cmnd[2] >> 6;
1744 changeable_values = (pc == 1);
1753 buf[2] = (curlun->
ro ? 0x80 : 0x00);
1757 buf[3] = (curlun->
ro ? 0x80 : 0x00);
1772 if (!changeable_values) {
1776 put_unaligned_be16(0xffff, &buf[4]);
1779 put_unaligned_be16(0xffff, &buf[8]);
1781 put_unaligned_be16(0xffff, &buf[10]);
1790 if (!valid_page || len >
limit) {
1799 put_unaligned_be16(len - 2, buf0);
1804 static int do_start_stop(
struct fsg_dev *fsg)
1809 if (!mod_data.removable) {
1815 loej = fsg->
cmnd[4] & 0x02;
1816 start = fsg->
cmnd[4] & 0x01;
1818 #ifdef CONFIG_USB_FILE_STORAGE_TEST
1819 if ((fsg->
cmnd[1] & ~0x01) != 0 ||
1820 (fsg->
cmnd[4] & ~0x03) != 0) {
1829 LDBG(curlun,
"unload attempt prevented\n");
1836 fsg_lun_close(curlun);
1854 static int do_prevent_allow(
struct fsg_dev *fsg)
1859 if (!mod_data.removable) {
1864 prevent = fsg->
cmnd[4] & 0x01;
1865 if ((fsg->
cmnd[4] & ~0x01) != 0) {
1871 fsg_lun_fsync_sub(curlun);
1877 static int do_read_format_capacities(
struct fsg_dev *fsg,
1883 buf[0] = buf[1] = buf[2] = 0;
1907 static int halt_bulk_in_endpoint(
struct fsg_dev *fsg)
1911 rc = fsg_set_halt(fsg, fsg->
bulk_in);
1913 VDBG(fsg,
"delayed bulk-in endpoint halt\n");
1916 WARNING(fsg,
"usb_ep_set_halt -> %d\n", rc);
1924 rc = usb_ep_set_halt(fsg->
bulk_in);
1929 static int wedge_bulk_in_endpoint(
struct fsg_dev *fsg)
1933 DBG(fsg,
"bulk-in set wedge\n");
1934 rc = usb_ep_set_wedge(fsg->
bulk_in);
1936 VDBG(fsg,
"delayed bulk-in endpoint wedge\n");
1939 WARNING(fsg,
"usb_ep_set_wedge -> %d\n", rc);
1947 rc = usb_ep_set_wedge(fsg->
bulk_in);
1952 static int throw_away_data(
struct fsg_dev *fsg)
1969 bh->
outreq->status != 0) {
1980 (
u32) mod_data.buflen);
1986 set_bulk_out_req_length(fsg, bh, amount);
1995 rc = sleep_thread(fsg);
2003 static int finish_reply(
struct fsg_dev *fsg)
2017 if (mod_data.can_stall) {
2019 rc = halt_bulk_in_endpoint(fsg);
2030 bh->
inreq->zero = 0;
2043 if (mod_data.can_stall &&
2047 rc = halt_bulk_in_endpoint(fsg);
2049 bh->
inreq->zero = 1;
2064 bh->
inreq->zero = 1;
2068 if (mod_data.can_stall)
2069 rc = halt_bulk_in_endpoint(fsg);
2092 else if (mod_data.can_stall) {
2102 rc = throw_away_data(fsg);
2109 static int send_status(
struct fsg_dev *fsg)
2120 rc = sleep_thread(fsg);
2134 DBG(fsg,
"sending phase-error status\n");
2138 DBG(fsg,
"sending command-failure status\n");
2140 VDBG(fsg,
" sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
2155 bh->
inreq->zero = 0;
2159 }
else if (mod_data.transport_type ==
USB_PR_CB) {
2181 fsg->
intreq->context = bh;
2195 static int check_command(
struct fsg_dev *fsg,
int cmnd_size,
2197 int needs_medium,
const char *name)
2201 static const char dirletter[4] = {
'u',
'o',
'i',
'n'};
2212 else if (mod_data.protocol_type ==
USB_SC_RBC) {
2224 VDBG(fsg,
"SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
2225 name, cmnd_size, dirletter[(
int) data_dir],
2269 if (cmnd_size <= fsg->cmnd_size) {
2270 DBG(fsg,
"%s is buggy! Expected length %d "
2271 "but we got %d\n", name,
2282 if (fsg->
lun != lun)
2283 DBG(fsg,
"using LUN %d from CBW, "
2284 "not LUN %d from CDB\n",
2303 DBG(fsg,
"unsupported LUN %d\n", fsg->
lun);
2319 fsg->
cmnd[1] &= 0x1f;
2320 for (i = 1; i < cmnd_size; ++
i) {
2321 if (fsg->
cmnd[i] && !(mask & (1 << i))) {
2339 static int check_command_size_in_blocks(
struct fsg_dev *fsg,
int cmnd_size,
2341 int needs_medium,
const char *name)
2345 return check_command(fsg, cmnd_size, data_dir,
2346 mask, needs_medium, name);
2349 static int do_scsi_command(
struct fsg_dev *fsg)
2362 rc = sleep_thread(fsg);
2370 switch (fsg->
cmnd[0]) {
2377 reply = do_inquiry(fsg, bh);
2384 "MODE SELECT(6)")) == 0)
2385 reply = do_mode_select(fsg, bh);
2392 "MODE SELECT(10)")) == 0)
2393 reply = do_mode_select(fsg, bh);
2399 (1<<1) | (1<<2) | (1<<4), 0,
2400 "MODE SENSE(6)")) == 0)
2401 reply = do_mode_sense(fsg, bh);
2407 (1<<1) | (1<<2) | (3<<7), 0,
2408 "MODE SENSE(10)")) == 0)
2409 reply = do_mode_sense(fsg, bh);
2416 "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
2417 reply = do_prevent_allow(fsg);
2423 if ((reply = check_command_size_in_blocks(fsg, 6,
2432 if ((reply = check_command_size_in_blocks(fsg, 10,
2434 (1<<1) | (0xf<<2) | (3<<7), 1,
2441 if ((reply = check_command_size_in_blocks(fsg, 12,
2443 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2451 (0xf<<2) | (1<<8), 1,
2452 "READ CAPACITY")) == 0)
2453 reply = do_read_capacity(fsg, bh);
2457 if (!mod_data.cdrom)
2461 (3<<7) | (0x1f<<1), 1,
2462 "READ HEADER")) == 0)
2463 reply = do_read_header(fsg, bh);
2467 if (!mod_data.cdrom)
2473 reply = do_read_toc(fsg, bh);
2480 "READ FORMAT CAPACITIES")) == 0)
2481 reply = do_read_format_capacities(fsg, bh);
2488 "REQUEST SENSE")) == 0)
2489 reply = do_request_sense(fsg, bh);
2496 "START-STOP UNIT")) == 0)
2497 reply = do_start_stop(fsg);
2503 (0xf<<2) | (3<<7), 1,
2504 "SYNCHRONIZE CACHE")) == 0)
2505 reply = do_synchronize_cache(fsg);
2520 (1<<1) | (0xf<<2) | (3<<7), 1,
2522 reply = do_verify(fsg);
2528 if ((reply = check_command_size_in_blocks(fsg, 6,
2537 if ((reply = check_command_size_in_blocks(fsg, 10,
2539 (1<<1) | (0xf<<2) | (3<<7), 1,
2546 if ((reply = check_command_size_in_blocks(fsg, 12,
2548 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2567 if ((reply = check_command(fsg, fsg->
cmnd_size,
2584 bh->
inreq->length = reply;
2608 DBG(fsg,
"invalid CBW: len %u sig 0x%x\n",
2621 wedge_bulk_in_endpoint(fsg);
2629 DBG(fsg,
"non-meaningful CBW: lun = %u, flags = 0x%x, "
2635 if (mod_data.can_stall) {
2637 halt_bulk_in_endpoint(fsg);
2658 static int get_next_command(
struct fsg_dev *fsg)
2668 rc = sleep_thread(fsg);
2684 rc = sleep_thread(fsg);
2689 rc = received_cbw(fsg, bh);
2696 rc = sleep_thread(fsg);
2709 spin_lock_irq(&fsg->
lock);
2713 spin_unlock_irq(&fsg->
lock);
2731 static int enable_endpoint(
struct fsg_dev *fsg,
struct usb_ep *ep,
2738 rc = usb_ep_enable(ep);
2740 ERROR(fsg,
"can't enable %s, result %d\n", ep->
name, rc);
2744 static int alloc_request(
struct fsg_dev *fsg,
struct usb_ep *ep,
2747 *preq = usb_ep_alloc_request(ep,
GFP_ATOMIC);
2750 ERROR(fsg,
"can't allocate request for %s\n", ep->
name);
2759 static int do_set_interface(
struct fsg_dev *fsg,
int altsetting)
2766 DBG(fsg,
"reset interface\n");
2802 if (altsetting < 0 || rc != 0)
2805 DBG(fsg,
"set interface %d\n", altsetting);
2808 d = fsg_ep_desc(fsg->
gadget,
2809 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
2810 &fsg_ss_bulk_in_desc);
2811 if ((rc = enable_endpoint(fsg, fsg->
bulk_in, d)) != 0)
2815 d = fsg_ep_desc(fsg->
gadget,
2816 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
2817 &fsg_ss_bulk_out_desc);
2818 if ((rc = enable_endpoint(fsg, fsg->
bulk_out, d)) != 0)
2825 d = fsg_ep_desc(fsg->
gadget,
2826 &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
2827 &fsg_ss_intr_in_desc);
2828 if ((rc = enable_endpoint(fsg, fsg->
intr_in, d)) != 0)
2837 if ((rc = alloc_request(fsg, fsg->
bulk_in, &bh->
inreq)) != 0)
2843 bh->
inreq->complete = bulk_in_complete;
2844 bh->
outreq->complete = bulk_out_complete;
2847 if ((rc = alloc_request(fsg, fsg->
intr_in, &fsg->
intreq)) != 0)
2849 fsg->
intreq->complete = intr_in_complete;
2853 for (i = 0; i < fsg->
nluns; ++
i)
2867 static int do_set_config(
struct fsg_dev *fsg,
u8 new_config)
2873 DBG(fsg,
"reset config\n");
2875 rc = do_set_interface(fsg, -1);
2879 if (new_config != 0) {
2880 fsg->
config = new_config;
2881 if ((rc = do_set_interface(fsg, 0)) != 0)
2884 INFO(fsg,
"%s config #%d\n",
2904 unsigned int exception_req_tag;
2915 DBG(fsg,
"Main thread exiting on signal\n");
2938 if (num_active == 0)
2940 if (sleep_thread(fsg))
2946 usb_ep_fifo_flush(fsg->
bulk_in);
2950 usb_ep_fifo_flush(fsg->
intr_in);
2954 spin_lock_irq(&fsg->
lock);
2965 old_state = fsg->
state;
2970 for (i = 0; i < fsg->
nluns; ++
i) {
2971 curlun = &fsg->
luns[
i];
2980 spin_unlock_irq(&fsg->
lock);
2983 switch (old_state) {
2989 spin_lock_irq(&fsg->
lock);
2992 spin_unlock_irq(&fsg->
lock);
3000 usb_ep_clear_halt(fsg->
bulk_in);
3017 rc = do_set_interface(fsg, 0);
3021 fsg_set_halt(fsg, fsg->
ep0);
3027 rc = do_set_config(fsg, new_config);
3031 fsg_set_halt(fsg, fsg->
ep0);
3037 for (i = 0; i < fsg->
nluns; ++
i)
3038 fsg_lun_fsync_sub(fsg->
luns + i);
3039 do_set_config(fsg, 0);
3044 do_set_config(fsg, 0);
3045 spin_lock_irq(&fsg->
lock);
3047 spin_unlock_irq(&fsg->
lock);
3055 static int fsg_main_thread(
void *fsg_)
3076 if (exception_in_progress(fsg) || signal_pending(
current)) {
3086 if (get_next_command(fsg))
3089 spin_lock_irq(&fsg->
lock);
3090 if (!exception_in_progress(fsg))
3092 spin_unlock_irq(&fsg->
lock);
3094 if (do_scsi_command(fsg) || finish_reply(fsg))
3097 spin_lock_irq(&fsg->
lock);
3098 if (!exception_in_progress(fsg))
3100 spin_unlock_irq(&fsg->
lock);
3102 if (send_status(fsg))
3105 spin_lock_irq(&fsg->
lock);
3106 if (!exception_in_progress(fsg))
3108 spin_unlock_irq(&fsg->
lock);
3111 spin_lock_irq(&fsg->
lock);
3113 spin_unlock_irq(&fsg->
lock);
3136 static void fsg_release(
struct kref *
ref)
3144 static void lun_release(
struct device *
dev)
3150 kref_put(&fsg->
ref, fsg_release);
3153 static void fsg_unbind(
struct usb_gadget *gadget)
3155 struct fsg_dev *fsg = get_gadget_data(gadget);
3160 DBG(fsg,
"unbind\n");
3173 for (i = 0; i < fsg->
nluns; ++
i) {
3174 curlun = &fsg->
luns[
i];
3179 fsg_lun_close(curlun);
3192 usb_ep_free_request(fsg->
ep0, req);
3195 set_gadget_data(gadget,
NULL);
3205 mod_data.transport_name =
"Bulk-only";
3207 mod_data.protocol_name =
"Transparent SCSI";
3214 mod_data.can_stall = 0;
3216 if (mod_data.release == 0xffff)
3217 mod_data.release = get_default_bcdDevice();
3221 #ifdef CONFIG_USB_FILE_STORAGE_TEST
3222 if (
strnicmp(mod_data.transport_parm,
"BBB", 10) == 0) {
3224 }
else if (
strnicmp(mod_data.transport_parm,
"CB", 10) == 0) {
3226 mod_data.transport_name =
"Control-Bulk";
3227 }
else if (
strnicmp(mod_data.transport_parm,
"CBI", 10) == 0) {
3229 mod_data.transport_name =
"Control-Bulk-Interrupt";
3231 ERROR(fsg,
"invalid transport: %s\n", mod_data.transport_parm);
3235 if (
strnicmp(mod_data.protocol_parm,
"SCSI", 10) == 0 ||
3238 }
else if (
strnicmp(mod_data.protocol_parm,
"RBC", 10) == 0 ||
3241 mod_data.protocol_name =
"RBC";
3242 }
else if (
strnicmp(mod_data.protocol_parm,
"8020", 4) == 0 ||
3243 strnicmp(mod_data.protocol_parm,
"ATAPI", 10) == 0 ||
3246 mod_data.protocol_name =
"8020i (ATAPI)";
3247 }
else if (
strnicmp(mod_data.protocol_parm,
"QIC", 3) == 0 ||
3250 mod_data.protocol_name =
"QIC-157";
3251 }
else if (
strnicmp(mod_data.protocol_parm,
"UFI", 10) == 0 ||
3254 mod_data.protocol_name =
"UFI";
3255 }
else if (
strnicmp(mod_data.protocol_parm,
"8070", 4) == 0 ||
3258 mod_data.protocol_name =
"8070i";
3260 ERROR(fsg,
"invalid protocol: %s\n", mod_data.protocol_parm);
3265 if (mod_data.buflen <= 0) {
3266 ERROR(fsg,
"invalid buflen\n");
3275 if (mod_data.serial) {
3284 for (ch = mod_data.serial; *ch; ++ch) {
3286 if ((*ch < '0' || *ch >
'9') &&
3287 (*ch < 'A' || *ch >
'F')) {
3289 "Invalid serial string character: %c\n",
3295 (mod_data.transport_type ==
USB_PR_BULK && len < 12) ||
3296 (mod_data.transport_type !=
USB_PR_BULK && len > 12)) {
3297 WARNING(fsg,
"Invalid serial string length!\n");
3302 WARNING(fsg,
"No serial-number string provided!\n");
3314 struct fsg_dev *fsg = the_fsg;
3323 set_gadget_data(gadget, fsg);
3325 fsg->
ep0->driver_data = fsg;
3327 if ((rc = check_parameters(fsg)) != 0)
3330 if (mod_data.removable) {
3331 dev_attr_file.attr.mode = 0644;
3332 dev_attr_file.store = fsg_store_file;
3333 if (!mod_data.cdrom) {
3334 dev_attr_ro.attr.mode = 0644;
3335 dev_attr_ro.store = fsg_store_ro;
3340 dev_attr_nofua.attr.mode = 0644;
3341 dev_attr_nofua.store = fsg_store_nofua;
3346 i =
max(mod_data.num_filenames, 1
u);
3348 ERROR(fsg,
"invalid number of LUNs: %d\n", i);
3362 for (i = 0; i < fsg->
nluns; ++
i) {
3363 curlun = &fsg->
luns[
i];
3364 curlun->
cdrom = !!mod_data.cdrom;
3365 curlun->
ro = mod_data.cdrom || mod_data.ro[
i];
3368 curlun->
nofua = mod_data.nofua[
i];
3369 curlun->
dev.release = lun_release;
3370 curlun->
dev.parent = &gadget->
dev;
3371 curlun->
dev.driver = &fsg_driver.driver;
3374 dev_name(&gadget->
dev), i);
3376 kref_get(&fsg->
ref);
3379 INFO(fsg,
"failed to register LUN%d: %d\n", i, rc);
3395 if (mod_data.file[i] && *mod_data.file[i]) {
3396 rc = fsg_lun_open(curlun, mod_data.file[i]);
3399 }
else if (!mod_data.removable) {
3400 ERROR(fsg,
"no file given for LUN%d\n", i);
3434 fsg_intf_desc.bNumEndpoints =
i;
3435 fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
3436 fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
3439 if (gadget_is_dualspeed(gadget)) {
3443 fsg_hs_bulk_in_desc.bEndpointAddress =
3444 fsg_fs_bulk_in_desc.bEndpointAddress;
3445 fsg_hs_bulk_out_desc.bEndpointAddress =
3446 fsg_fs_bulk_out_desc.bEndpointAddress;
3447 fsg_hs_intr_in_desc.bEndpointAddress =
3448 fsg_fs_intr_in_desc.bEndpointAddress;
3451 if (gadget_is_superspeed(gadget)) {
3457 max_burst =
min_t(
unsigned, mod_data.buflen / 1024, 15);
3460 fsg_ss_bulk_in_desc.bEndpointAddress =
3461 fsg_fs_bulk_in_desc.bEndpointAddress;
3462 fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
3464 fsg_ss_bulk_out_desc.bEndpointAddress =
3465 fsg_fs_bulk_out_desc.bEndpointAddress;
3466 fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
3469 if (gadget_is_otg(gadget))
3498 usb_gadget_set_selfpowered(gadget);
3500 snprintf(fsg_string_manufacturer,
sizeof fsg_string_manufacturer,
3502 init_utsname()->sysname, init_utsname()->
release,
3506 "file-storage-gadget");
3513 INFO(fsg,
"NOTE: This driver is deprecated. "
3514 "Consider using g_mass_storage instead.\n");
3515 INFO(fsg,
"Number of LUNs=%d\n", fsg->
nluns);
3518 for (i = 0; i < fsg->
nluns; ++
i) {
3519 curlun = &fsg->
luns[
i];
3528 LINFO(curlun,
"ro=%d, nofua=%d, file: %s\n",
3529 curlun->
ro, curlun->
nofua, (p ? p :
"(error)"));
3534 DBG(fsg,
"transport=%s (x%02x)\n",
3535 mod_data.transport_name, mod_data.transport_type);
3536 DBG(fsg,
"protocol=%s (x%02x)\n",
3537 mod_data.protocol_name, mod_data.protocol_type);
3538 DBG(fsg,
"VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
3539 mod_data.vendor, mod_data.product, mod_data.release);
3540 DBG(fsg,
"removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
3541 mod_data.removable, mod_data.can_stall,
3542 mod_data.cdrom, mod_data.buflen);
3552 ERROR(fsg,
"unable to autoconfigure all endpoints\n");
3565 static void fsg_suspend(
struct usb_gadget *gadget)
3567 struct fsg_dev *fsg = get_gadget_data(gadget);
3569 DBG(fsg,
"suspend\n");
3573 static void fsg_resume(
struct usb_gadget *gadget)
3575 struct fsg_dev *fsg = get_gadget_data(gadget);
3577 DBG(fsg,
"resume\n");
3586 .function = (
char *) fsg_string_product,
3604 static int __init fsg_alloc(
void)
3608 fsg = kzalloc(
sizeof *fsg +
3615 kref_init(&fsg->
ref);
3623 static int __init fsg_init(
void)
3628 rc = fsg_num_buffers_validate();
3632 if ((rc = fsg_alloc()) != 0)
3637 kref_put(&fsg->
ref, fsg_release);
3643 static void __exit fsg_cleanup(
void)
3645 struct fsg_dev *fsg = the_fsg;
3654 kref_put(&fsg->
ref, fsg_release);