Lines Matching refs:common
327 struct fsg_common *common; member
341 static inline int __fsg_is_set(struct fsg_common *common, in __fsg_is_set() argument
344 if (common->fsg) in __fsg_is_set()
346 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line); in __fsg_is_set()
351 #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__)) argument
360 static int exception_in_progress(struct fsg_common *common) in exception_in_progress() argument
362 return common->state > FSG_STATE_IDLE; in exception_in_progress()
366 static void set_bulk_out_req_length(struct fsg_common *common, in set_bulk_out_req_length() argument
372 rem = length % common->bulk_out_maxpacket; in set_bulk_out_req_length()
374 length += common->bulk_out_maxpacket - rem; in set_bulk_out_req_length()
401 static void wakeup_thread(struct fsg_common *common) in wakeup_thread() argument
409 common->thread_wakeup_needed = 1; in wakeup_thread()
410 if (common->thread_task) in wakeup_thread()
411 wake_up_process(common->thread_task); in wakeup_thread()
414 static void raise_exception(struct fsg_common *common, enum fsg_state new_state) in raise_exception() argument
423 spin_lock_irqsave(&common->lock, flags); in raise_exception()
424 if (common->state <= new_state) { in raise_exception()
425 common->exception_req_tag = common->ep0_req_tag; in raise_exception()
426 common->state = new_state; in raise_exception()
427 if (common->thread_task) in raise_exception()
429 common->thread_task); in raise_exception()
431 spin_unlock_irqrestore(&common->lock, flags); in raise_exception()
437 static int ep0_queue(struct fsg_common *common) in ep0_queue() argument
441 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC); in ep0_queue()
442 common->ep0->driver_data = common; in ep0_queue()
445 WARNING(common, "error in submission: %s --> %d\n", in ep0_queue()
446 common->ep0->name, rc); in ep0_queue()
458 struct fsg_common *common = ep->driver_data; in bulk_in_complete() local
462 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_in_complete()
469 spin_lock(&common->lock); in bulk_in_complete()
472 wakeup_thread(common); in bulk_in_complete()
473 spin_unlock(&common->lock); in bulk_in_complete()
478 struct fsg_common *common = ep->driver_data; in bulk_out_complete() local
481 dump_msg(common, "bulk-out", req->buf, req->actual); in bulk_out_complete()
483 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_out_complete()
490 spin_lock(&common->lock); in bulk_out_complete()
493 wakeup_thread(common); in bulk_out_complete()
494 spin_unlock(&common->lock); in bulk_out_complete()
501 struct usb_request *req = fsg->common->ep0req; in fsg_setup()
506 if (!fsg_is_set(fsg->common)) in fsg_setup()
509 ++fsg->common->ep0_req_tag; /* Record arrival of a new request */ in fsg_setup()
529 raise_exception(fsg->common, FSG_STATE_RESET); in fsg_setup()
540 *(u8 *)req->buf = fsg->common->nluns - 1; in fsg_setup()
544 return ep0_queue(fsg->common); in fsg_setup()
569 spin_lock_irq(&fsg->common->lock); in start_transfer()
572 spin_unlock_irq(&fsg->common->lock); in start_transfer()
591 static bool start_in_transfer(struct fsg_common *common, struct fsg_buffhd *bh) in start_in_transfer() argument
593 if (!fsg_is_set(common)) in start_in_transfer()
595 start_transfer(common->fsg, common->fsg->bulk_in, in start_in_transfer()
600 static bool start_out_transfer(struct fsg_common *common, struct fsg_buffhd *bh) in start_out_transfer() argument
602 if (!fsg_is_set(common)) in start_out_transfer()
604 start_transfer(common->fsg, common->fsg->bulk_out, in start_out_transfer()
609 static int sleep_thread(struct fsg_common *common, bool can_freeze) in sleep_thread() argument
622 if (common->thread_wakeup_needed) in sleep_thread()
627 common->thread_wakeup_needed = 0; in sleep_thread()
640 static int do_read(struct fsg_common *common) in do_read() argument
642 struct fsg_lun *curlun = common->curlun; in do_read()
655 if (common->cmnd[0] == READ_6) in do_read()
656 lba = get_unaligned_be24(&common->cmnd[1]); in do_read()
658 lba = get_unaligned_be32(&common->cmnd[2]); in do_read()
665 if ((common->cmnd[1] & ~0x18) != 0) { in do_read()
677 amount_left = common->data_size_from_cmnd; in do_read()
693 bh = common->next_buffhd_to_fill; in do_read()
695 rc = sleep_thread(common, false); in do_read()
735 common->residue -= nread; in do_read()
759 if (!start_in_transfer(common, bh)) in do_read()
762 common->next_buffhd_to_fill = bh->next; in do_read()
771 static int do_write(struct fsg_common *common) in do_write() argument
773 struct fsg_lun *curlun = common->curlun; in do_write()
795 if (common->cmnd[0] == WRITE_6) in do_write()
796 lba = get_unaligned_be24(&common->cmnd[1]); in do_write()
798 lba = get_unaligned_be32(&common->cmnd[2]); in do_write()
806 if (common->cmnd[1] & ~0x18) { in do_write()
810 if (!curlun->nofua && (common->cmnd[1] & 0x08)) { /* FUA */ in do_write()
824 amount_left_to_req = common->data_size_from_cmnd; in do_write()
825 amount_left_to_write = common->data_size_from_cmnd; in do_write()
830 bh = common->next_buffhd_to_fill; in do_write()
853 common->usb_amount_left -= amount; in do_write()
863 set_bulk_out_req_length(common, bh, amount); in do_write()
864 if (!start_out_transfer(common, bh)) in do_write()
867 common->next_buffhd_to_fill = bh->next; in do_write()
872 bh = common->next_buffhd_to_drain; in do_write()
877 common->next_buffhd_to_drain = bh->next; in do_write()
929 common->residue -= nwritten; in do_write()
943 common->short_packet_received = 1; in do_write()
950 rc = sleep_thread(common, false); in do_write()
961 static int do_synchronize_cache(struct fsg_common *common) in do_synchronize_cache() argument
963 struct fsg_lun *curlun = common->curlun; in do_synchronize_cache()
987 static int do_verify(struct fsg_common *common) in do_verify() argument
989 struct fsg_lun *curlun = common->curlun; in do_verify()
992 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in do_verify()
1002 lba = get_unaligned_be32(&common->cmnd[2]); in do_verify()
1012 if (common->cmnd[1] & ~0x10) { in do_verify()
1017 verification_length = get_unaligned_be16(&common->cmnd[7]); in do_verify()
1089 static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh) in do_inquiry() argument
1091 struct fsg_lun *curlun = common->curlun; in do_inquiry()
1095 common->bad_lun_okay = 1; in do_inquiry()
1110 memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string); in do_inquiry()
1114 static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_request_sense() argument
1116 struct fsg_lun *curlun = common->curlun; in do_request_sense()
1144 common->bad_lun_okay = 1; in do_request_sense()
1167 static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_capacity() argument
1169 struct fsg_lun *curlun = common->curlun; in do_read_capacity()
1170 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_capacity()
1171 int pmi = common->cmnd[8]; in do_read_capacity()
1186 static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_header() argument
1188 struct fsg_lun *curlun = common->curlun; in do_read_header()
1189 int msf = common->cmnd[1] & 0x02; in do_read_header()
1190 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_header()
1193 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */ in do_read_header()
1208 static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_toc() argument
1210 struct fsg_lun *curlun = common->curlun; in do_read_toc()
1211 int msf = common->cmnd[1] & 0x02; in do_read_toc()
1212 int start_track = common->cmnd[6]; in do_read_toc()
1215 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ in do_read_toc()
1235 static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_sense() argument
1237 struct fsg_lun *curlun = common->curlun; in do_mode_sense()
1238 int mscmnd = common->cmnd[0]; in do_mode_sense()
1246 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */ in do_mode_sense()
1250 pc = common->cmnd[2] >> 6; in do_mode_sense()
1251 page_code = common->cmnd[2] & 0x3f; in do_mode_sense()
1321 static int do_start_stop(struct fsg_common *common) in do_start_stop() argument
1323 struct fsg_lun *curlun = common->curlun; in do_start_stop()
1331 } else if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */ in do_start_stop()
1332 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */ in do_start_stop()
1337 loej = common->cmnd[4] & 0x02; in do_start_stop()
1338 start = common->cmnd[4] & 0x01; in do_start_stop()
1362 up_read(&common->filesem); in do_start_stop()
1363 down_write(&common->filesem); in do_start_stop()
1365 up_write(&common->filesem); in do_start_stop()
1366 down_read(&common->filesem); in do_start_stop()
1371 static int do_prevent_allow(struct fsg_common *common) in do_prevent_allow() argument
1373 struct fsg_lun *curlun = common->curlun; in do_prevent_allow()
1376 if (!common->curlun) { in do_prevent_allow()
1378 } else if (!common->curlun->removable) { in do_prevent_allow()
1379 common->curlun->sense_data = SS_INVALID_COMMAND; in do_prevent_allow()
1383 prevent = common->cmnd[4] & 0x01; in do_prevent_allow()
1384 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */ in do_prevent_allow()
1395 static int do_read_format_capacities(struct fsg_common *common, in do_read_format_capacities() argument
1398 struct fsg_lun *curlun = common->curlun; in do_read_format_capacities()
1412 static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_select() argument
1414 struct fsg_lun *curlun = common->curlun; in do_mode_select()
1470 static int throw_away_data(struct fsg_common *common) in throw_away_data() argument
1476 for (bh = common->next_buffhd_to_drain; in throw_away_data()
1477 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; in throw_away_data()
1478 bh = common->next_buffhd_to_drain) { in throw_away_data()
1484 common->next_buffhd_to_drain = bh->next; in throw_away_data()
1489 raise_exception(common, in throw_away_data()
1497 bh = common->next_buffhd_to_fill; in throw_away_data()
1499 && common->usb_amount_left > 0) { in throw_away_data()
1500 amount = min(common->usb_amount_left, FSG_BUFLEN); in throw_away_data()
1507 set_bulk_out_req_length(common, bh, amount); in throw_away_data()
1508 if (!start_out_transfer(common, bh)) in throw_away_data()
1511 common->next_buffhd_to_fill = bh->next; in throw_away_data()
1512 common->usb_amount_left -= amount; in throw_away_data()
1517 rc = sleep_thread(common, true); in throw_away_data()
1524 static int finish_reply(struct fsg_common *common) in finish_reply() argument
1526 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in finish_reply()
1529 switch (common->data_dir) { in finish_reply()
1540 if (!common->can_stall) { in finish_reply()
1542 } else if (fsg_is_set(common)) { in finish_reply()
1543 fsg_set_halt(common->fsg, common->fsg->bulk_out); in finish_reply()
1544 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1553 if (common->data_size == 0) { in finish_reply()
1557 } else if (!fsg_is_set(common)) { in finish_reply()
1561 } else if (common->residue == 0) { in finish_reply()
1563 if (!start_in_transfer(common, bh)) in finish_reply()
1565 common->next_buffhd_to_fill = bh->next; in finish_reply()
1576 if (!start_in_transfer(common, bh)) in finish_reply()
1578 common->next_buffhd_to_fill = bh->next; in finish_reply()
1579 if (common->can_stall) in finish_reply()
1580 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1589 if (common->residue == 0) { in finish_reply()
1593 } else if (common->short_packet_received) { in finish_reply()
1594 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1606 } else if (common->can_stall) { in finish_reply()
1607 if (fsg_is_set(common)) in finish_reply()
1608 fsg_set_halt(common->fsg, in finish_reply()
1609 common->fsg->bulk_out); in finish_reply()
1610 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1619 rc = throw_away_data(common); in finish_reply()
1626 static int send_status(struct fsg_common *common) in send_status() argument
1628 struct fsg_lun *curlun = common->curlun; in send_status()
1636 bh = common->next_buffhd_to_fill; in send_status()
1638 rc = sleep_thread(common, true); in send_status()
1646 } else if (common->bad_lun_okay) in send_status()
1651 if (common->phase_error) { in send_status()
1652 DBG(common, "sending phase-error status\n"); in send_status()
1656 DBG(common, "sending command-failure status\n"); in send_status()
1658 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;" in send_status()
1667 csw->Tag = common->tag; in send_status()
1668 csw->Residue = cpu_to_le32(common->residue); in send_status()
1673 if (!start_in_transfer(common, bh)) in send_status()
1677 common->next_buffhd_to_fill = bh->next; in send_status()
1688 static int check_command(struct fsg_common *common, int cmnd_size, in check_command() argument
1693 unsigned int lun = common->cmnd[1] >> 5; in check_command()
1699 if (common->data_dir != DATA_DIR_UNKNOWN) in check_command()
1700 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir], in check_command()
1701 common->data_size); in check_command()
1702 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", in check_command()
1704 common->data_size_from_cmnd, common->cmnd_size, hdlen); in check_command()
1710 if (common->data_size_from_cmnd == 0) in check_command()
1712 if (common->data_size < common->data_size_from_cmnd) { in check_command()
1718 common->data_size_from_cmnd = common->data_size; in check_command()
1719 common->phase_error = 1; in check_command()
1721 common->residue = common->data_size; in check_command()
1722 common->usb_amount_left = common->data_size; in check_command()
1725 if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) { in check_command()
1726 common->phase_error = 1; in check_command()
1731 if (cmnd_size != common->cmnd_size) { in check_command()
1746 if (cmnd_size <= common->cmnd_size) { in check_command()
1747 DBG(common, "%s is buggy! Expected length %d " in check_command()
1749 cmnd_size, common->cmnd_size); in check_command()
1750 cmnd_size = common->cmnd_size; in check_command()
1752 common->phase_error = 1; in check_command()
1758 if (common->lun != lun) in check_command()
1759 DBG(common, "using LUN %u from CBW, not LUN %u from CDB\n", in check_command()
1760 common->lun, lun); in check_command()
1763 curlun = common->curlun; in check_command()
1765 if (common->cmnd[0] != REQUEST_SENSE) { in check_command()
1771 common->bad_lun_okay = 0; in check_command()
1777 if (common->cmnd[0] != INQUIRY && in check_command()
1778 common->cmnd[0] != REQUEST_SENSE) { in check_command()
1779 DBG(common, "unsupported LUN %u\n", common->lun); in check_command()
1789 common->cmnd[0] != INQUIRY && in check_command()
1790 common->cmnd[0] != REQUEST_SENSE) { in check_command()
1797 common->cmnd[1] &= 0x1f; /* Mask away the LUN */ in check_command()
1799 if (common->cmnd[i] && !(mask & (1 << i))) { in check_command()
1817 static int check_command_size_in_blocks(struct fsg_common *common, in check_command_size_in_blocks() argument
1821 if (common->curlun) in check_command_size_in_blocks()
1822 common->data_size_from_cmnd <<= common->curlun->blkbits; in check_command_size_in_blocks()
1823 return check_command(common, cmnd_size, data_dir, in check_command_size_in_blocks()
1827 static int do_scsi_command(struct fsg_common *common) in do_scsi_command() argument
1835 dump_cdb(common); in do_scsi_command()
1838 bh = common->next_buffhd_to_fill; in do_scsi_command()
1839 common->next_buffhd_to_drain = bh; in do_scsi_command()
1841 rc = sleep_thread(common, true); in do_scsi_command()
1845 common->phase_error = 0; in do_scsi_command()
1846 common->short_packet_received = 0; in do_scsi_command()
1848 down_read(&common->filesem); /* We're using the backing file */ in do_scsi_command()
1849 switch (common->cmnd[0]) { in do_scsi_command()
1852 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1853 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1857 reply = do_inquiry(common, bh); in do_scsi_command()
1861 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1862 reply = check_command(common, 6, DATA_DIR_FROM_HOST, in do_scsi_command()
1866 reply = do_mode_select(common, bh); in do_scsi_command()
1870 common->data_size_from_cmnd = in do_scsi_command()
1871 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1872 reply = check_command(common, 10, DATA_DIR_FROM_HOST, in do_scsi_command()
1876 reply = do_mode_select(common, bh); in do_scsi_command()
1880 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1881 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1885 reply = do_mode_sense(common, bh); in do_scsi_command()
1889 common->data_size_from_cmnd = in do_scsi_command()
1890 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1891 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1895 reply = do_mode_sense(common, bh); in do_scsi_command()
1899 common->data_size_from_cmnd = 0; in do_scsi_command()
1900 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1904 reply = do_prevent_allow(common); in do_scsi_command()
1908 i = common->cmnd[4]; in do_scsi_command()
1909 common->data_size_from_cmnd = (i == 0) ? 256 : i; in do_scsi_command()
1910 reply = check_command_size_in_blocks(common, 6, in do_scsi_command()
1915 reply = do_read(common); in do_scsi_command()
1919 common->data_size_from_cmnd = in do_scsi_command()
1920 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1921 reply = check_command_size_in_blocks(common, 10, in do_scsi_command()
1926 reply = do_read(common); in do_scsi_command()
1930 common->data_size_from_cmnd = in do_scsi_command()
1931 get_unaligned_be32(&common->cmnd[6]); in do_scsi_command()
1932 reply = check_command_size_in_blocks(common, 12, in do_scsi_command()
1937 reply = do_read(common); in do_scsi_command()
1941 common->data_size_from_cmnd = 8; in do_scsi_command()
1942 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1946 reply = do_read_capacity(common, bh); in do_scsi_command()
1950 if (!common->curlun || !common->curlun->cdrom) in do_scsi_command()
1952 common->data_size_from_cmnd = in do_scsi_command()
1953 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1954 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1958 reply = do_read_header(common, bh); in do_scsi_command()
1962 if (!common->curlun || !common->curlun->cdrom) in do_scsi_command()
1964 common->data_size_from_cmnd = in do_scsi_command()
1965 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1966 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1970 reply = do_read_toc(common, bh); in do_scsi_command()
1974 common->data_size_from_cmnd = in do_scsi_command()
1975 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1976 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1980 reply = do_read_format_capacities(common, bh); in do_scsi_command()
1984 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1985 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1989 reply = do_request_sense(common, bh); in do_scsi_command()
1993 common->data_size_from_cmnd = 0; in do_scsi_command()
1994 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1998 reply = do_start_stop(common); in do_scsi_command()
2002 common->data_size_from_cmnd = 0; in do_scsi_command()
2003 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
2007 reply = do_synchronize_cache(common); in do_scsi_command()
2011 common->data_size_from_cmnd = 0; in do_scsi_command()
2012 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
2022 common->data_size_from_cmnd = 0; in do_scsi_command()
2023 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
2027 reply = do_verify(common); in do_scsi_command()
2031 i = common->cmnd[4]; in do_scsi_command()
2032 common->data_size_from_cmnd = (i == 0) ? 256 : i; in do_scsi_command()
2033 reply = check_command_size_in_blocks(common, 6, in do_scsi_command()
2038 reply = do_write(common); in do_scsi_command()
2042 common->data_size_from_cmnd = in do_scsi_command()
2043 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2044 reply = check_command_size_in_blocks(common, 10, in do_scsi_command()
2049 reply = do_write(common); in do_scsi_command()
2053 common->data_size_from_cmnd = in do_scsi_command()
2054 get_unaligned_be32(&common->cmnd[6]); in do_scsi_command()
2055 reply = check_command_size_in_blocks(common, 12, in do_scsi_command()
2060 reply = do_write(common); in do_scsi_command()
2077 common->data_size_from_cmnd = 0; in do_scsi_command()
2078 sprintf(unknown, "Unknown x%02x", common->cmnd[0]); in do_scsi_command()
2079 reply = check_command(common, common->cmnd_size, in do_scsi_command()
2082 common->curlun->sense_data = SS_INVALID_COMMAND; in do_scsi_command()
2087 up_read(&common->filesem); in do_scsi_command()
2095 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) { in do_scsi_command()
2096 reply = min((u32)reply, common->data_size_from_cmnd); in do_scsi_command()
2099 common->residue -= reply; in do_scsi_command()
2112 struct fsg_common *common = fsg->common; in received_cbw() local
2153 if (common->can_stall) { in received_cbw()
2161 common->cmnd_size = cbw->Length; in received_cbw()
2162 memcpy(common->cmnd, cbw->CDB, common->cmnd_size); in received_cbw()
2164 common->data_dir = DATA_DIR_TO_HOST; in received_cbw()
2166 common->data_dir = DATA_DIR_FROM_HOST; in received_cbw()
2167 common->data_size = le32_to_cpu(cbw->DataTransferLength); in received_cbw()
2168 if (common->data_size == 0) in received_cbw()
2169 common->data_dir = DATA_DIR_NONE; in received_cbw()
2170 common->lun = cbw->Lun; in received_cbw()
2171 if (common->lun < common->nluns) in received_cbw()
2172 common->curlun = common->luns[common->lun]; in received_cbw()
2174 common->curlun = NULL; in received_cbw()
2175 common->tag = cbw->Tag; in received_cbw()
2179 static int get_next_command(struct fsg_common *common) in get_next_command() argument
2185 bh = common->next_buffhd_to_fill; in get_next_command()
2187 rc = sleep_thread(common, true); in get_next_command()
2193 set_bulk_out_req_length(common, bh, US_BULK_CB_WRAP_LEN); in get_next_command()
2194 if (!start_out_transfer(common, bh)) in get_next_command()
2206 rc = sleep_thread(common, true); in get_next_command()
2211 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; in get_next_command()
2220 static int alloc_request(struct fsg_common *common, struct usb_ep *ep, in alloc_request() argument
2226 ERROR(common, "can't allocate request for %s\n", ep->name); in alloc_request()
2231 static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg) in do_set_interface() argument
2236 if (common->running) in do_set_interface()
2237 DBG(common, "reset interface\n"); in do_set_interface()
2241 if (common->fsg) { in do_set_interface()
2242 fsg = common->fsg; in do_set_interface()
2244 for (i = 0; i < common->fsg_num_buffers; ++i) { in do_set_interface()
2245 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2269 common->fsg = NULL; in do_set_interface()
2270 wake_up(&common->fsg_wait); in do_set_interface()
2273 common->running = 0; in do_set_interface()
2277 common->fsg = new_fsg; in do_set_interface()
2278 fsg = common->fsg; in do_set_interface()
2281 rc = config_ep_by_speed(common->gadget, &(fsg->function), fsg->bulk_in); in do_set_interface()
2287 fsg->bulk_in->driver_data = common; in do_set_interface()
2290 rc = config_ep_by_speed(common->gadget, &(fsg->function), in do_set_interface()
2297 fsg->bulk_out->driver_data = common; in do_set_interface()
2299 common->bulk_out_maxpacket = usb_endpoint_maxp(fsg->bulk_out->desc); in do_set_interface()
2303 for (i = 0; i < common->fsg_num_buffers; ++i) { in do_set_interface()
2304 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2306 rc = alloc_request(common, fsg->bulk_in, &bh->inreq); in do_set_interface()
2309 rc = alloc_request(common, fsg->bulk_out, &bh->outreq); in do_set_interface()
2318 common->running = 1; in do_set_interface()
2319 for (i = 0; i < common->nluns; ++i) in do_set_interface()
2320 if (common->luns[i]) in do_set_interface()
2321 common->luns[i]->unit_attention_data = in do_set_interface()
2332 fsg->common->new_fsg = fsg; in fsg_set_alt()
2333 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_set_alt()
2340 fsg->common->new_fsg = NULL; in fsg_disable()
2341 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_disable()
2347 static void handle_exception(struct fsg_common *common) in handle_exception() argument
2366 if (common->state < FSG_STATE_EXIT) in handle_exception()
2367 DBG(common, "Main thread exiting on signal\n"); in handle_exception()
2368 raise_exception(common, FSG_STATE_EXIT); in handle_exception()
2373 if (likely(common->fsg)) { in handle_exception()
2374 for (i = 0; i < common->fsg_num_buffers; ++i) { in handle_exception()
2375 bh = &common->buffhds[i]; in handle_exception()
2377 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq); in handle_exception()
2379 usb_ep_dequeue(common->fsg->bulk_out, in handle_exception()
2386 for (i = 0; i < common->fsg_num_buffers; ++i) { in handle_exception()
2387 bh = &common->buffhds[i]; in handle_exception()
2392 if (sleep_thread(common, true)) in handle_exception()
2397 if (common->fsg->bulk_in_enabled) in handle_exception()
2398 usb_ep_fifo_flush(common->fsg->bulk_in); in handle_exception()
2399 if (common->fsg->bulk_out_enabled) in handle_exception()
2400 usb_ep_fifo_flush(common->fsg->bulk_out); in handle_exception()
2407 spin_lock_irq(&common->lock); in handle_exception()
2409 for (i = 0; i < common->fsg_num_buffers; ++i) { in handle_exception()
2410 bh = &common->buffhds[i]; in handle_exception()
2413 common->next_buffhd_to_fill = &common->buffhds[0]; in handle_exception()
2414 common->next_buffhd_to_drain = &common->buffhds[0]; in handle_exception()
2415 exception_req_tag = common->exception_req_tag; in handle_exception()
2416 old_state = common->state; in handle_exception()
2419 common->state = FSG_STATE_STATUS_PHASE; in handle_exception()
2421 for (i = 0; i < common->nluns; ++i) { in handle_exception()
2422 curlun = common->luns[i]; in handle_exception()
2431 common->state = FSG_STATE_IDLE; in handle_exception()
2433 spin_unlock_irq(&common->lock); in handle_exception()
2438 send_status(common); in handle_exception()
2439 spin_lock_irq(&common->lock); in handle_exception()
2440 if (common->state == FSG_STATE_STATUS_PHASE) in handle_exception()
2441 common->state = FSG_STATE_IDLE; in handle_exception()
2442 spin_unlock_irq(&common->lock); in handle_exception()
2451 if (!fsg_is_set(common)) in handle_exception()
2454 &common->fsg->atomic_bitflags)) in handle_exception()
2455 usb_ep_clear_halt(common->fsg->bulk_in); in handle_exception()
2457 if (common->ep0_req_tag == exception_req_tag) in handle_exception()
2458 ep0_queue(common); /* Complete the status stage */ in handle_exception()
2472 do_set_interface(common, common->new_fsg); in handle_exception()
2473 if (common->new_fsg) in handle_exception()
2474 usb_composite_setup_continue(common->cdev); in handle_exception()
2479 do_set_interface(common, NULL); /* Free resources */ in handle_exception()
2480 spin_lock_irq(&common->lock); in handle_exception()
2481 common->state = FSG_STATE_TERMINATED; /* Stop the thread */ in handle_exception()
2482 spin_unlock_irq(&common->lock); in handle_exception()
2500 struct fsg_common *common = common_; in fsg_main_thread() local
2522 while (common->state != FSG_STATE_TERMINATED) { in fsg_main_thread()
2523 if (exception_in_progress(common) || signal_pending(current)) { in fsg_main_thread()
2524 handle_exception(common); in fsg_main_thread()
2528 if (!common->running) { in fsg_main_thread()
2529 sleep_thread(common, true); in fsg_main_thread()
2533 if (get_next_command(common)) in fsg_main_thread()
2536 spin_lock_irq(&common->lock); in fsg_main_thread()
2537 if (!exception_in_progress(common)) in fsg_main_thread()
2538 common->state = FSG_STATE_DATA_PHASE; in fsg_main_thread()
2539 spin_unlock_irq(&common->lock); in fsg_main_thread()
2541 if (do_scsi_command(common) || finish_reply(common)) in fsg_main_thread()
2544 spin_lock_irq(&common->lock); in fsg_main_thread()
2545 if (!exception_in_progress(common)) in fsg_main_thread()
2546 common->state = FSG_STATE_STATUS_PHASE; in fsg_main_thread()
2547 spin_unlock_irq(&common->lock); in fsg_main_thread()
2549 if (send_status(common)) in fsg_main_thread()
2552 spin_lock_irq(&common->lock); in fsg_main_thread()
2553 if (!exception_in_progress(common)) in fsg_main_thread()
2554 common->state = FSG_STATE_IDLE; in fsg_main_thread()
2555 spin_unlock_irq(&common->lock); in fsg_main_thread()
2558 spin_lock_irq(&common->lock); in fsg_main_thread()
2559 common->thread_task = NULL; in fsg_main_thread()
2560 spin_unlock_irq(&common->lock); in fsg_main_thread()
2562 if (!common->ops || !common->ops->thread_exits in fsg_main_thread()
2563 || common->ops->thread_exits(common) < 0) { in fsg_main_thread()
2564 struct fsg_lun **curlun_it = common->luns; in fsg_main_thread()
2565 unsigned i = common->nluns; in fsg_main_thread()
2567 down_write(&common->filesem); in fsg_main_thread()
2576 up_write(&common->filesem); in fsg_main_thread()
2580 complete_and_exit(&common->thread_notifier, 0); in fsg_main_thread()
2653 void fsg_common_get(struct fsg_common *common) in fsg_common_get() argument
2655 kref_get(&common->ref); in fsg_common_get()
2659 void fsg_common_put(struct fsg_common *common) in fsg_common_put() argument
2661 kref_put(&common->ref, fsg_common_release); in fsg_common_put()
2675 static struct fsg_common *fsg_common_setup(struct fsg_common *common) in fsg_common_setup() argument
2677 if (!common) { in fsg_common_setup()
2678 common = kzalloc(sizeof(*common), GFP_KERNEL); in fsg_common_setup()
2679 if (!common) in fsg_common_setup()
2681 common->free_storage_on_release = 1; in fsg_common_setup()
2683 common->free_storage_on_release = 0; in fsg_common_setup()
2685 init_rwsem(&common->filesem); in fsg_common_setup()
2686 spin_lock_init(&common->lock); in fsg_common_setup()
2687 kref_init(&common->ref); in fsg_common_setup()
2688 init_completion(&common->thread_notifier); in fsg_common_setup()
2689 init_waitqueue_head(&common->fsg_wait); in fsg_common_setup()
2690 common->state = FSG_STATE_TERMINATED; in fsg_common_setup()
2692 return common; in fsg_common_setup()
2695 void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs) in fsg_common_set_sysfs() argument
2697 common->sysfs = sysfs; in fsg_common_set_sysfs()
2713 int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n) in fsg_common_set_num_buffers() argument
2740 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_set_num_buffers()
2741 common->fsg_num_buffers = n; in fsg_common_set_num_buffers()
2742 common->buffhds = buffhds; in fsg_common_set_num_buffers()
2796 static void _fsg_common_remove_luns(struct fsg_common *common, int n) in _fsg_common_remove_luns() argument
2801 if (common->luns[i]) { in _fsg_common_remove_luns()
2802 fsg_common_remove_lun(common->luns[i], common->sysfs); in _fsg_common_remove_luns()
2803 common->luns[i] = NULL; in _fsg_common_remove_luns()
2808 void fsg_common_remove_luns(struct fsg_common *common) in fsg_common_remove_luns() argument
2810 _fsg_common_remove_luns(common, common->nluns); in fsg_common_remove_luns()
2813 void fsg_common_free_luns(struct fsg_common *common) in fsg_common_free_luns() argument
2815 fsg_common_remove_luns(common); in fsg_common_free_luns()
2816 kfree(common->luns); in fsg_common_free_luns()
2817 common->luns = NULL; in fsg_common_free_luns()
2821 int fsg_common_set_nluns(struct fsg_common *common, int nluns) in fsg_common_set_nluns() argument
2835 if (common->luns) in fsg_common_set_nluns()
2836 fsg_common_free_luns(common); in fsg_common_set_nluns()
2838 common->luns = curlun; in fsg_common_set_nluns()
2839 common->nluns = nluns; in fsg_common_set_nluns()
2841 pr_info("Number of LUNs=%d\n", common->nluns); in fsg_common_set_nluns()
2847 void fsg_common_set_ops(struct fsg_common *common, in fsg_common_set_ops() argument
2850 common->ops = ops; in fsg_common_set_ops()
2854 void fsg_common_free_buffers(struct fsg_common *common) in fsg_common_free_buffers() argument
2856 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_free_buffers()
2857 common->buffhds = NULL; in fsg_common_free_buffers()
2861 int fsg_common_set_cdev(struct fsg_common *common, in fsg_common_set_cdev() argument
2866 common->gadget = cdev->gadget; in fsg_common_set_cdev()
2867 common->ep0 = cdev->gadget->ep0; in fsg_common_set_cdev()
2868 common->ep0req = cdev->req; in fsg_common_set_cdev()
2869 common->cdev = cdev; in fsg_common_set_cdev()
2883 common->can_stall = can_stall && !(gadget_is_at91(common->gadget)); in fsg_common_set_cdev()
2889 static inline int fsg_common_add_sysfs(struct fsg_common *common, in fsg_common_add_sysfs() argument
2925 int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg, in fsg_common_create_lun() argument
2933 if (!common->nluns || !common->luns) in fsg_common_create_lun()
2936 if (common->luns[id]) in fsg_common_create_lun()
2955 if (!common->sysfs) { in fsg_common_create_lun()
2960 lun->dev.parent = &common->gadget->dev; in fsg_common_create_lun()
2961 dev_set_drvdata(&lun->dev, &common->filesem); in fsg_common_create_lun()
2965 rc = fsg_common_add_sysfs(common, lun); in fsg_common_create_lun()
2972 common->luns[id] = lun; in fsg_common_create_lun()
3000 if (common->sysfs) { in fsg_common_create_lun()
3005 common->luns[id] = NULL; in fsg_common_create_lun()
3012 int fsg_common_create_luns(struct fsg_common *common, struct fsg_config *cfg) in fsg_common_create_luns() argument
3017 for (i = 0; i < common->nluns; ++i) { in fsg_common_create_luns()
3019 rc = fsg_common_create_lun(common, &cfg->luns[i], i, buf, NULL); in fsg_common_create_luns()
3024 pr_info("Number of LUNs=%d\n", common->nluns); in fsg_common_create_luns()
3029 _fsg_common_remove_luns(common, i); in fsg_common_create_luns()
3034 void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn, in fsg_common_set_inquiry_string() argument
3041 snprintf(common->inquiry_string, sizeof(common->inquiry_string), in fsg_common_set_inquiry_string()
3044 pn ?: ((*common->luns)->cdrom in fsg_common_set_inquiry_string()
3051 int fsg_common_run_thread(struct fsg_common *common) in fsg_common_run_thread() argument
3053 common->state = FSG_STATE_IDLE; in fsg_common_run_thread()
3055 common->thread_task = in fsg_common_run_thread()
3056 kthread_create(fsg_main_thread, common, "file-storage"); in fsg_common_run_thread()
3057 if (IS_ERR(common->thread_task)) { in fsg_common_run_thread()
3058 common->state = FSG_STATE_TERMINATED; in fsg_common_run_thread()
3059 return PTR_ERR(common->thread_task); in fsg_common_run_thread()
3062 DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task)); in fsg_common_run_thread()
3064 wake_up_process(common->thread_task); in fsg_common_run_thread()
3072 struct fsg_common *common = container_of(ref, struct fsg_common, ref); in fsg_common_release() local
3075 if (common->state != FSG_STATE_TERMINATED) { in fsg_common_release()
3076 raise_exception(common, FSG_STATE_EXIT); in fsg_common_release()
3077 wait_for_completion(&common->thread_notifier); in fsg_common_release()
3080 if (likely(common->luns)) { in fsg_common_release()
3081 struct fsg_lun **lun_it = common->luns; in fsg_common_release()
3082 unsigned i = common->nluns; in fsg_common_release()
3089 if (common->sysfs) in fsg_common_release()
3092 if (common->sysfs) in fsg_common_release()
3097 kfree(common->luns); in fsg_common_release()
3100 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_release()
3101 if (common->free_storage_on_release) in fsg_common_release()
3102 kfree(common); in fsg_common_release()
3120 ret = fsg_common_set_cdev(fsg->common, c->cdev, in fsg_bind()
3121 fsg->common->can_stall); in fsg_bind()
3124 fsg_common_set_inquiry_string(fsg->common, NULL, NULL); in fsg_bind()
3125 ret = fsg_common_run_thread(fsg->common); in fsg_bind()
3143 ep->driver_data = fsg->common; /* claim the endpoint */ in fsg_bind()
3149 ep->driver_data = fsg->common; /* claim the endpoint */ in fsg_bind()
3186 struct fsg_common *common = fsg->common; in fsg_unbind() local
3189 if (fsg->common->fsg == fsg) { in fsg_unbind()
3190 fsg->common->new_fsg = NULL; in fsg_unbind()
3191 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_unbind()
3193 wait_event(common->fsg_wait, common->fsg != fsg); in fsg_unbind()
3233 return fsg_show_file(opts->lun, &fsg_opts->common->filesem, page); in fsg_lun_opts_file_show()
3243 return fsg_store_file(opts->lun, &fsg_opts->common->filesem, page, len); in fsg_lun_opts_file_store()
3262 return fsg_store_ro(opts->lun, &fsg_opts->common->filesem, page, len); in fsg_lun_opts_ro_store()
3298 return fsg_store_cdrom(opts->lun, &fsg_opts->common->filesem, page, in fsg_lun_opts_cdrom_store()
3362 if (fsg_opts->refcnt || fsg_opts->common->luns[num]) { in fsg_lun_make()
3376 ret = fsg_common_create_lun(fsg_opts->common, &config, num, name, in fsg_lun_make()
3382 opts->lun = fsg_opts->common->luns[num]; in fsg_lun_make()
3410 fsg_common_remove_lun(lun_opts->lun, fsg_opts->common->sysfs); in fsg_lun_drop()
3411 fsg_opts->common->luns[lun_opts->lun_id] = NULL; in fsg_lun_drop()
3439 result = sprintf(page, "%d", opts->common->can_stall); in fsg_opts_stall_show()
3460 opts->common->can_stall = stall; in fsg_opts_stall_store()
3479 result = sprintf(page, "%d", opts->common->fsg_num_buffers); in fsg_opts_num_buffers_show()
3504 fsg_common_set_num_buffers(opts->common, num); in fsg_opts_num_buffers_store()
3544 fsg_common_put(opts->common); in fsg_free_inst()
3559 opts->common = fsg_common_setup(opts->common); in fsg_alloc_inst()
3560 if (IS_ERR(opts->common)) { in fsg_alloc_inst()
3561 rc = PTR_ERR(opts->common); in fsg_alloc_inst()
3564 rc = fsg_common_set_nluns(opts->common, FSG_MAX_LUNS); in fsg_alloc_inst()
3568 rc = fsg_common_set_num_buffers(opts->common, in fsg_alloc_inst()
3577 rc = fsg_common_create_lun(opts->common, &config, 0, "lun.0", in fsg_alloc_inst()
3579 opts->lun0.lun = opts->common->luns[0]; in fsg_alloc_inst()
3590 kfree(opts->common->luns); in fsg_alloc_inst()
3614 struct fsg_common *common = opts->common; in fsg_alloc() local
3632 fsg->common = common; in fsg_alloc()