Lines Matching full:common
85 * set to 512 rather than the more common value 2048.
245 #include <common.h>
372 struct fsg_common *common; member
387 static inline int __fsg_is_set(struct fsg_common *common, in __fsg_is_set() argument
390 if (common->fsg) in __fsg_is_set()
392 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line); in __fsg_is_set()
397 #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__)) argument
408 static int exception_in_progress(struct fsg_common *common) in exception_in_progress() argument
410 return common->state > FSG_STATE_IDLE; in exception_in_progress()
414 static void set_bulk_out_req_length(struct fsg_common *common, in set_bulk_out_req_length() argument
420 rem = length % common->bulk_out_maxpacket; in set_bulk_out_req_length()
422 length += common->bulk_out_maxpacket - rem; in set_bulk_out_req_length()
451 static void wakeup_thread(struct fsg_common *common) in wakeup_thread() argument
453 common->thread_wakeup_needed = 1; in wakeup_thread()
456 static void raise_exception(struct fsg_common *common, enum fsg_state new_state) in raise_exception() argument
461 if (common->state <= new_state) { in raise_exception()
462 common->exception_req_tag = common->ep0_req_tag; in raise_exception()
463 common->state = new_state; in raise_exception()
464 common->thread_wakeup_needed = 1; in raise_exception()
470 static int ep0_queue(struct fsg_common *common) in ep0_queue() argument
474 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC); in ep0_queue()
475 common->ep0->driver_data = common; in ep0_queue()
478 WARNING(common, "error in submission: %s --> %d\n", in ep0_queue()
479 common->ep0->name, rc); in ep0_queue()
491 struct fsg_common *common = ep->driver_data; in bulk_in_complete() local
495 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_in_complete()
503 wakeup_thread(common); in bulk_in_complete()
508 struct fsg_common *common = ep->driver_data; in bulk_out_complete() local
511 dump_msg(common, "bulk-out", req->buf, req->actual); in bulk_out_complete()
513 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_out_complete()
522 wakeup_thread(common); in bulk_out_complete()
533 struct usb_request *req = fsg->common->ep0req; in fsg_setup()
538 if (!fsg_is_set(fsg->common)) in fsg_setup()
553 raise_exception(fsg->common, FSG_STATE_RESET); in fsg_setup()
563 *(u8 *) req->buf = fsg->common->nluns - 1; in fsg_setup()
567 return ep0_queue(fsg->common); in fsg_setup()
610 #define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \ argument
611 if (fsg_is_set(common)) \
612 start_transfer((common)->fsg, (common)->fsg->ep_name, \
616 #define START_TRANSFER(common, ep_name, req, pbusy, state) \ argument
617 START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0
647 static int sleep_thread(struct fsg_common *common) in sleep_thread() argument
654 if (common->thread_wakeup_needed) in sleep_thread()
677 common->thread_wakeup_needed = 0; in sleep_thread()
683 static int do_read(struct fsg_common *common) in do_read() argument
685 struct fsg_lun *curlun = &common->luns[common->lun]; in do_read()
697 if (common->cmnd[0] == SC_READ_6) in do_read()
698 lba = get_unaligned_be24(&common->cmnd[1]); in do_read()
700 lba = get_unaligned_be32(&common->cmnd[2]); in do_read()
705 if ((common->cmnd[1] & ~0x18) != 0) { in do_read()
717 amount_left = common->data_size_from_cmnd; in do_read()
738 bh = common->next_buffhd_to_fill; in do_read()
740 rc = sleep_thread(common); in do_read()
757 rc = ums[common->lun].read_sector(&ums[common->lun], in do_read()
781 common->residue -= nread; in do_read()
797 START_TRANSFER_OR(common, bulk_in, bh->inreq, in do_read()
800 * common->fsg is NULL */ in do_read()
802 common->next_buffhd_to_fill = bh->next; in do_read()
810 static int do_write(struct fsg_common *common) in do_write() argument
812 struct fsg_lun *curlun = &common->luns[common->lun]; in do_write()
830 if (common->cmnd[0] == SC_WRITE_6) in do_write()
831 lba = get_unaligned_be24(&common->cmnd[1]); in do_write()
833 lba = get_unaligned_be32(&common->cmnd[2]); in do_write()
839 if (common->cmnd[1] & ~0x18) { in do_write()
852 amount_left_to_req = common->data_size_from_cmnd; in do_write()
853 amount_left_to_write = common->data_size_from_cmnd; in do_write()
858 bh = common->next_buffhd_to_fill; in do_write()
894 common->usb_amount_left -= amount; in do_write()
904 START_TRANSFER_OR(common, bulk_out, bh->outreq, in do_write()
907 * common->fsg is NULL */ in do_write()
909 common->next_buffhd_to_fill = bh->next; in do_write()
914 bh = common->next_buffhd_to_drain; in do_write()
918 common->next_buffhd_to_drain = bh->next; in do_write()
931 rc = ums[common->lun].write_sector(&ums[common->lun], in do_write()
955 common->residue -= nwritten; in do_write()
968 common->short_packet_received = 1; in do_write()
975 rc = sleep_thread(common); in do_write()
985 static int do_synchronize_cache(struct fsg_common *common) in do_synchronize_cache() argument
992 static int do_verify(struct fsg_common *common) in do_verify() argument
994 struct fsg_lun *curlun = &common->luns[common->lun]; in do_verify()
997 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in do_verify()
1006 lba = get_unaligned_be32(&common->cmnd[2]); in do_verify()
1014 if (common->cmnd[1] & ~0x10) { in do_verify()
1019 verification_length = get_unaligned_be16(&common->cmnd[7]); in do_verify()
1047 rc = ums[common->lun].read_sector(&ums[common->lun], in do_verify()
1080 static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh) in do_inquiry() argument
1082 struct fsg_lun *curlun = &common->luns[common->lun]; in do_inquiry()
1087 common->bad_lun_okay = 1; in do_inquiry()
1102 ums[common->lun].name, (u16) 0xffff); in do_inquiry()
1108 static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_request_sense() argument
1110 struct fsg_lun *curlun = &common->luns[common->lun]; in do_request_sense()
1138 common->bad_lun_okay = 1; in do_request_sense()
1159 static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_capacity() argument
1161 struct fsg_lun *curlun = &common->luns[common->lun]; in do_read_capacity()
1162 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_capacity()
1163 int pmi = common->cmnd[8]; in do_read_capacity()
1178 static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_header() argument
1180 struct fsg_lun *curlun = &common->luns[common->lun]; in do_read_header()
1181 int msf = common->cmnd[1] & 0x02; in do_read_header()
1182 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_header()
1185 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */ in do_read_header()
1201 static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_toc() argument
1203 struct fsg_lun *curlun = &common->luns[common->lun]; in do_read_toc()
1204 int msf = common->cmnd[1] & 0x02; in do_read_toc()
1205 int start_track = common->cmnd[6]; in do_read_toc()
1208 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ in do_read_toc()
1229 static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_sense() argument
1231 struct fsg_lun *curlun = &common->luns[common->lun]; in do_mode_sense()
1232 int mscmnd = common->cmnd[0]; in do_mode_sense()
1240 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */ in do_mode_sense()
1244 pc = common->cmnd[2] >> 6; in do_mode_sense()
1245 page_code = common->cmnd[2] & 0x3f; in do_mode_sense()
1310 static int do_start_stop(struct fsg_common *common) in do_start_stop() argument
1312 struct fsg_lun *curlun = &common->luns[common->lun]; in do_start_stop()
1324 static int do_prevent_allow(struct fsg_common *common) in do_prevent_allow() argument
1326 struct fsg_lun *curlun = &common->luns[common->lun]; in do_prevent_allow()
1334 prevent = common->cmnd[4] & 0x01; in do_prevent_allow()
1335 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */ in do_prevent_allow()
1347 static int do_read_format_capacities(struct fsg_common *common, in do_read_format_capacities() argument
1350 struct fsg_lun *curlun = &common->luns[common->lun]; in do_read_format_capacities()
1365 static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_select() argument
1367 struct fsg_lun *curlun = &common->luns[common->lun]; in do_mode_select()
1417 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill; in pad_with_zeros()
1423 fsg->common->usb_amount_left = nkeep + fsg->common->residue; in pad_with_zeros()
1424 while (fsg->common->usb_amount_left > 0) { in pad_with_zeros()
1428 rc = sleep_thread(fsg->common); in pad_with_zeros()
1433 nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN); in pad_with_zeros()
1439 bh = fsg->common->next_buffhd_to_fill = bh->next; in pad_with_zeros()
1440 fsg->common->usb_amount_left -= nsend; in pad_with_zeros()
1446 static int throw_away_data(struct fsg_common *common) in throw_away_data() argument
1452 for (bh = common->next_buffhd_to_drain; in throw_away_data()
1453 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; in throw_away_data()
1454 bh = common->next_buffhd_to_drain) { in throw_away_data()
1459 common->next_buffhd_to_drain = bh->next; in throw_away_data()
1464 raise_exception(common, in throw_away_data()
1472 bh = common->next_buffhd_to_fill; in throw_away_data()
1474 && common->usb_amount_left > 0) { in throw_away_data()
1475 amount = min(common->usb_amount_left, FSG_BUFLEN); in throw_away_data()
1482 START_TRANSFER_OR(common, bulk_out, bh->outreq, in throw_away_data()
1485 * common->fsg is NULL */ in throw_away_data()
1487 common->next_buffhd_to_fill = bh->next; in throw_away_data()
1488 common->usb_amount_left -= amount; in throw_away_data()
1493 rc = sleep_thread(common); in throw_away_data()
1501 static int finish_reply(struct fsg_common *common) in finish_reply() argument
1503 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in finish_reply()
1506 switch (common->data_dir) { in finish_reply()
1515 if (!common->can_stall) { in finish_reply()
1517 } else if (fsg_is_set(common)) { in finish_reply()
1518 fsg_set_halt(common->fsg, common->fsg->bulk_out); in finish_reply()
1519 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1521 /* Don't know what to do if common->fsg is NULL */ in finish_reply()
1528 if (common->data_size == 0) { in finish_reply()
1532 } else if (common->residue == 0) { in finish_reply()
1534 START_TRANSFER_OR(common, bulk_in, bh->inreq, in finish_reply()
1537 common->next_buffhd_to_fill = bh->next; in finish_reply()
1542 } else if (common->can_stall) { in finish_reply()
1544 START_TRANSFER_OR(common, bulk_in, bh->inreq, in finish_reply()
1547 * common->fsg is NULL */ in finish_reply()
1549 common->next_buffhd_to_fill = bh->next; in finish_reply()
1550 if (common->fsg) in finish_reply()
1551 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1552 } else if (fsg_is_set(common)) { in finish_reply()
1553 rc = pad_with_zeros(common->fsg); in finish_reply()
1555 /* Don't know what to do if common->fsg is NULL */ in finish_reply()
1563 if (common->residue == 0) { in finish_reply()
1567 } else if (common->short_packet_received) { in finish_reply()
1568 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1578 } else if (common->can_stall) { in finish_reply()
1579 if (fsg_is_set(common)) in finish_reply()
1580 fsg_set_halt(common->fsg, in finish_reply()
1581 common->fsg->bulk_out); in finish_reply()
1582 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1589 rc = throw_away_data(common); in finish_reply()
1597 static int send_status(struct fsg_common *common) in send_status() argument
1599 struct fsg_lun *curlun = &common->luns[common->lun]; in send_status()
1607 bh = common->next_buffhd_to_fill; in send_status()
1609 rc = sleep_thread(common); in send_status()
1616 else if (common->bad_lun_okay) in send_status()
1621 if (common->phase_error) { in send_status()
1622 DBG(common, "sending phase-error status\n"); in send_status()
1626 DBG(common, "sending command-failure status\n"); in send_status()
1628 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;" in send_status()
1637 csw->Tag = common->tag; in send_status()
1638 csw->Residue = cpu_to_le32(common->residue); in send_status()
1643 START_TRANSFER_OR(common, bulk_in, bh->inreq, in send_status()
1645 /* Don't know what to do if common->fsg is NULL */ in send_status()
1648 common->next_buffhd_to_fill = bh->next; in send_status()
1657 static int check_command(struct fsg_common *common, int cmnd_size, in check_command() argument
1662 int lun = common->cmnd[1] >> 5; in check_command()
1668 if (common->data_dir != DATA_DIR_UNKNOWN) in check_command()
1669 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir], in check_command()
1670 common->data_size); in check_command()
1671 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", in check_command()
1673 common->data_size_from_cmnd, common->cmnd_size, hdlen); in check_command()
1677 if (common->data_size_from_cmnd == 0) in check_command()
1679 if (common->data_size < common->data_size_from_cmnd) { in check_command()
1683 common->data_size_from_cmnd = common->data_size; in check_command()
1684 common->phase_error = 1; in check_command()
1686 common->residue = common->data_size; in check_command()
1687 common->usb_amount_left = common->data_size; in check_command()
1690 if (common->data_dir != data_dir in check_command()
1691 && common->data_size_from_cmnd > 0) { in check_command()
1692 common->phase_error = 1; in check_command()
1697 if (cmnd_size != common->cmnd_size) { in check_command()
1711 if (cmnd_size <= common->cmnd_size) { in check_command()
1712 DBG(common, "%s is buggy! Expected length %d " in check_command()
1714 cmnd_size, common->cmnd_size); in check_command()
1715 cmnd_size = common->cmnd_size; in check_command()
1717 common->phase_error = 1; in check_command()
1723 if (common->lun != lun) in check_command()
1724 DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n", in check_command()
1725 common->lun, lun); in check_command()
1728 if (common->lun < common->nluns) { in check_command()
1729 curlun = &common->luns[common->lun]; in check_command()
1730 if (common->cmnd[0] != SC_REQUEST_SENSE) { in check_command()
1736 common->bad_lun_okay = 0; in check_command()
1740 if (common->cmnd[0] != SC_INQUIRY && in check_command()
1741 common->cmnd[0] != SC_REQUEST_SENSE) { in check_command()
1742 DBG(common, "unsupported LUN %d\n", common->lun); in check_command()
1750 common->cmnd[0] != SC_INQUIRY && in check_command()
1751 common->cmnd[0] != SC_REQUEST_SENSE) { in check_command()
1758 common->cmnd[1] &= 0x1f; /* Mask away the LUN */ in check_command()
1760 if (common->cmnd[i] && !(mask & (1 << i))) { in check_command()
1771 static int do_scsi_command(struct fsg_common *common) in do_scsi_command() argument
1778 struct fsg_lun *curlun = &common->luns[common->lun]; in do_scsi_command()
1780 dump_cdb(common); in do_scsi_command()
1783 bh = common->next_buffhd_to_fill; in do_scsi_command()
1784 common->next_buffhd_to_drain = bh; in do_scsi_command()
1786 rc = sleep_thread(common); in do_scsi_command()
1790 common->phase_error = 0; in do_scsi_command()
1791 common->short_packet_received = 0; in do_scsi_command()
1793 down_read(&common->filesem); /* We're using the backing file */ in do_scsi_command()
1794 switch (common->cmnd[0]) { in do_scsi_command()
1797 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1798 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1802 reply = do_inquiry(common, bh); in do_scsi_command()
1806 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1807 reply = check_command(common, 6, DATA_DIR_FROM_HOST, in do_scsi_command()
1811 reply = do_mode_select(common, bh); in do_scsi_command()
1815 common->data_size_from_cmnd = in do_scsi_command()
1816 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1817 reply = check_command(common, 10, DATA_DIR_FROM_HOST, in do_scsi_command()
1821 reply = do_mode_select(common, bh); in do_scsi_command()
1825 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1826 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1830 reply = do_mode_sense(common, bh); in do_scsi_command()
1834 common->data_size_from_cmnd = in do_scsi_command()
1835 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1836 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1840 reply = do_mode_sense(common, bh); in do_scsi_command()
1844 common->data_size_from_cmnd = 0; in do_scsi_command()
1845 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1849 reply = do_prevent_allow(common); in do_scsi_command()
1853 i = common->cmnd[4]; in do_scsi_command()
1854 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; in do_scsi_command()
1855 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1859 reply = do_read(common); in do_scsi_command()
1863 common->data_size_from_cmnd = in do_scsi_command()
1864 get_unaligned_be16(&common->cmnd[7]) << 9; in do_scsi_command()
1865 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1869 reply = do_read(common); in do_scsi_command()
1873 common->data_size_from_cmnd = in do_scsi_command()
1874 get_unaligned_be32(&common->cmnd[6]) << 9; in do_scsi_command()
1875 reply = check_command(common, 12, DATA_DIR_TO_HOST, in do_scsi_command()
1879 reply = do_read(common); in do_scsi_command()
1883 common->data_size_from_cmnd = 8; in do_scsi_command()
1884 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1888 reply = do_read_capacity(common, bh); in do_scsi_command()
1892 if (!common->luns[common->lun].cdrom) in do_scsi_command()
1894 common->data_size_from_cmnd = in do_scsi_command()
1895 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1896 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1900 reply = do_read_header(common, bh); in do_scsi_command()
1904 if (!common->luns[common->lun].cdrom) in do_scsi_command()
1906 common->data_size_from_cmnd = in do_scsi_command()
1907 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1908 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1912 reply = do_read_toc(common, bh); in do_scsi_command()
1916 common->data_size_from_cmnd = in do_scsi_command()
1917 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1918 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1922 reply = do_read_format_capacities(common, bh); in do_scsi_command()
1926 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1927 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1931 reply = do_request_sense(common, bh); in do_scsi_command()
1935 common->data_size_from_cmnd = 0; in do_scsi_command()
1936 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1940 reply = do_start_stop(common); in do_scsi_command()
1944 common->data_size_from_cmnd = 0; in do_scsi_command()
1945 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
1949 reply = do_synchronize_cache(common); in do_scsi_command()
1953 common->data_size_from_cmnd = 0; in do_scsi_command()
1954 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1962 common->data_size_from_cmnd = 0; in do_scsi_command()
1963 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
1967 reply = do_verify(common); in do_scsi_command()
1971 i = common->cmnd[4]; in do_scsi_command()
1972 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; in do_scsi_command()
1973 reply = check_command(common, 6, DATA_DIR_FROM_HOST, in do_scsi_command()
1977 reply = do_write(common); in do_scsi_command()
1981 common->data_size_from_cmnd = in do_scsi_command()
1982 get_unaligned_be16(&common->cmnd[7]) << 9; in do_scsi_command()
1983 reply = check_command(common, 10, DATA_DIR_FROM_HOST, in do_scsi_command()
1987 reply = do_write(common); in do_scsi_command()
1991 common->data_size_from_cmnd = in do_scsi_command()
1992 get_unaligned_be32(&common->cmnd[6]) << 9; in do_scsi_command()
1993 reply = check_command(common, 12, DATA_DIR_FROM_HOST, in do_scsi_command()
1997 reply = do_write(common); in do_scsi_command()
2012 common->data_size_from_cmnd = 0; in do_scsi_command()
2013 sprintf(unknown, "Unknown x%02x", common->cmnd[0]); in do_scsi_command()
2014 reply = check_command(common, common->cmnd_size, in do_scsi_command()
2022 up_read(&common->filesem); in do_scsi_command()
2030 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) { in do_scsi_command()
2031 reply = min((u32) reply, common->data_size_from_cmnd); in do_scsi_command()
2034 common->residue -= reply; in do_scsi_command()
2046 struct fsg_common *common = fsg->common; in received_cbw() local
2083 if (common->can_stall) { in received_cbw()
2091 common->cmnd_size = cbw->Length; in received_cbw()
2092 memcpy(common->cmnd, cbw->CDB, common->cmnd_size); in received_cbw()
2094 common->data_dir = DATA_DIR_TO_HOST; in received_cbw()
2096 common->data_dir = DATA_DIR_FROM_HOST; in received_cbw()
2097 common->data_size = le32_to_cpu(cbw->DataTransferLength); in received_cbw()
2098 if (common->data_size == 0) in received_cbw()
2099 common->data_dir = DATA_DIR_NONE; in received_cbw()
2100 common->lun = cbw->Lun; in received_cbw()
2101 common->tag = cbw->Tag; in received_cbw()
2106 static int get_next_command(struct fsg_common *common) in get_next_command() argument
2112 bh = common->next_buffhd_to_fill; in get_next_command()
2114 rc = sleep_thread(common); in get_next_command()
2120 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN); in get_next_command()
2122 START_TRANSFER_OR(common, bulk_out, bh->outreq, in get_next_command()
2124 /* Don't know what to do if common->fsg is NULL */ in get_next_command()
2133 rc = sleep_thread(common); in get_next_command()
2138 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; in get_next_command()
2147 static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep, in enable_endpoint() argument
2152 ep->driver_data = common; in enable_endpoint()
2155 ERROR(common, "can't enable %s, result %d\n", ep->name, rc); in enable_endpoint()
2159 static int alloc_request(struct fsg_common *common, struct usb_ep *ep, in alloc_request() argument
2165 ERROR(common, "can't allocate request for %s\n", ep->name); in alloc_request()
2170 static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg) in do_set_interface() argument
2176 if (common->running) in do_set_interface()
2177 DBG(common, "reset interface\n"); in do_set_interface()
2181 if (common->fsg) { in do_set_interface()
2182 fsg = common->fsg; in do_set_interface()
2185 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2207 common->fsg = NULL; in do_set_interface()
2208 /* wake_up(&common->fsg_wait); */ in do_set_interface()
2211 common->running = 0; in do_set_interface()
2215 common->fsg = new_fsg; in do_set_interface()
2216 fsg = common->fsg; in do_set_interface()
2219 d = fsg_ep_desc(common->gadget, in do_set_interface()
2221 rc = enable_endpoint(common, fsg->bulk_in, d); in do_set_interface()
2226 d = fsg_ep_desc(common->gadget, in do_set_interface()
2228 rc = enable_endpoint(common, fsg->bulk_out, d); in do_set_interface()
2232 common->bulk_out_maxpacket = in do_set_interface()
2238 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2240 rc = alloc_request(common, fsg->bulk_in, &bh->inreq); in do_set_interface()
2243 rc = alloc_request(common, fsg->bulk_out, &bh->outreq); in do_set_interface()
2252 common->running = 1; in do_set_interface()
2264 fsg->common->new_fsg = fsg; in fsg_set_alt()
2265 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_set_alt()
2272 fsg->common->new_fsg = NULL; in fsg_disable()
2273 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_disable()
2278 static void handle_exception(struct fsg_common *common) in handle_exception() argument
2287 if (common->fsg) { in handle_exception()
2289 bh = &common->buffhds[i]; in handle_exception()
2291 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq); in handle_exception()
2293 usb_ep_dequeue(common->fsg->bulk_out, in handle_exception()
2301 bh = &common->buffhds[i]; in handle_exception()
2306 if (sleep_thread(common)) in handle_exception()
2311 if (common->fsg->bulk_in_enabled) in handle_exception()
2312 usb_ep_fifo_flush(common->fsg->bulk_in); in handle_exception()
2313 if (common->fsg->bulk_out_enabled) in handle_exception()
2314 usb_ep_fifo_flush(common->fsg->bulk_out); in handle_exception()
2321 bh = &common->buffhds[i]; in handle_exception()
2324 common->next_buffhd_to_fill = &common->buffhds[0]; in handle_exception()
2325 common->next_buffhd_to_drain = &common->buffhds[0]; in handle_exception()
2326 exception_req_tag = common->exception_req_tag; in handle_exception()
2327 old_state = common->state; in handle_exception()
2330 common->state = FSG_STATE_STATUS_PHASE; in handle_exception()
2332 for (i = 0; i < common->nluns; ++i) { in handle_exception()
2333 curlun = &common->luns[i]; in handle_exception()
2337 common->state = FSG_STATE_IDLE; in handle_exception()
2343 send_status(common); in handle_exception()
2345 if (common->state == FSG_STATE_STATUS_PHASE) in handle_exception()
2346 common->state = FSG_STATE_IDLE; in handle_exception()
2353 if (!fsg_is_set(common)) in handle_exception()
2356 &common->fsg->atomic_bitflags)) in handle_exception()
2357 usb_ep_clear_halt(common->fsg->bulk_in); in handle_exception()
2359 if (common->ep0_req_tag == exception_req_tag) in handle_exception()
2360 ep0_queue(common); /* Complete the status stage */ in handle_exception()
2365 do_set_interface(common, common->new_fsg); in handle_exception()
2370 do_set_interface(common, NULL); /* Free resources */ in handle_exception()
2371 common->state = FSG_STATE_TERMINATED; /* Stop the thread */ in handle_exception()
2389 struct fsg_common *common = the_fsg_common; in fsg_main_thread() local
2392 if (exception_in_progress(common)) { in fsg_main_thread()
2393 handle_exception(common); in fsg_main_thread()
2397 if (!common->running) { in fsg_main_thread()
2398 ret = sleep_thread(common); in fsg_main_thread()
2405 ret = get_next_command(common); in fsg_main_thread()
2409 if (!exception_in_progress(common)) in fsg_main_thread()
2410 common->state = FSG_STATE_DATA_PHASE; in fsg_main_thread()
2412 if (do_scsi_command(common) || finish_reply(common)) in fsg_main_thread()
2415 if (!exception_in_progress(common)) in fsg_main_thread()
2416 common->state = FSG_STATE_STATUS_PHASE; in fsg_main_thread()
2418 if (send_status(common)) in fsg_main_thread()
2421 if (!exception_in_progress(common)) in fsg_main_thread()
2422 common->state = FSG_STATE_IDLE; in fsg_main_thread()
2425 common->thread_task = NULL; in fsg_main_thread()
2432 static struct fsg_common *fsg_common_init(struct fsg_common *common, in fsg_common_init() argument
2448 if (!common) { in fsg_common_init()
2449 common = calloc(sizeof(*common), 1); in fsg_common_init()
2450 if (!common) in fsg_common_init()
2452 common->free_storage_on_release = 1; in fsg_common_init()
2454 memset(common, 0, sizeof(*common)); in fsg_common_init()
2455 common->free_storage_on_release = 0; in fsg_common_init()
2458 common->ops = NULL; in fsg_common_init()
2459 common->private_data = NULL; in fsg_common_init()
2461 common->gadget = gadget; in fsg_common_init()
2462 common->ep0 = gadget->ep0; in fsg_common_init()
2463 common->ep0req = cdev->req; in fsg_common_init()
2481 common->nluns = nluns; in fsg_common_init()
2484 common->luns[i].removable = 1; in fsg_common_init()
2486 rc = fsg_lun_open(&common->luns[i], ums[i].num_sectors, ""); in fsg_common_init()
2490 common->lun = 0; in fsg_common_init()
2493 bh = common->buffhds; in fsg_common_init()
2509 bh->next = common->buffhds; in fsg_common_init()
2511 snprintf(common->inquiry_string, sizeof common->inquiry_string, in fsg_common_init()
2523 common->thread_task = in fsg_common_init()
2524 kthread_create(fsg_main_thread, common, in fsg_common_init()
2526 if (IS_ERR(common->thread_task)) { in fsg_common_init()
2527 rc = PTR_ERR(common->thread_task); in fsg_common_init()
2533 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n"); in fsg_common_init()
2534 INFO(common, "Number of LUNs=%d\n", common->nluns); in fsg_common_init()
2536 return common; in fsg_common_init()
2539 common->nluns = i + 1; in fsg_common_init()
2541 common->state = FSG_STATE_TERMINATED; /* The thread is dead */ in fsg_common_init()
2544 fsg_common_release(&common->ref); in fsg_common_init()
2550 struct fsg_common *common = container_of(ref, struct fsg_common, ref); in fsg_common_release() local
2553 if (common->state != FSG_STATE_TERMINATED) { in fsg_common_release()
2554 raise_exception(common, FSG_STATE_EXIT); in fsg_common_release()
2555 wait_for_completion(&common->thread_notifier); in fsg_common_release()
2558 if (likely(common->luns)) { in fsg_common_release()
2559 struct fsg_lun *lun = common->luns; in fsg_common_release()
2560 unsigned i = common->nluns; in fsg_common_release()
2562 /* In error recovery common->nluns may be zero. */ in fsg_common_release()
2566 kfree(common->luns); in fsg_common_release()
2570 struct fsg_buffhd *bh = common->buffhds; in fsg_common_release()
2577 if (common->free_storage_on_release) in fsg_common_release()
2578 kfree(common); in fsg_common_release()
2638 if (fsg->common->fsg == fsg) { in fsg_unbind()
2639 fsg->common->new_fsg = NULL; in fsg_unbind()
2640 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_unbind()
2667 ep->driver_data = fsg->common; /* claim the endpoint */ in fsg_bind()
2673 ep->driver_data = fsg->common; /* claim the endpoint */ in fsg_bind()
2710 struct fsg_common *common) in fsg_bind_config() argument
2726 fsg->common = common; in fsg_bind_config()
2727 common->fsg = fsg; in fsg_bind_config()
2728 /* Our caller holds a reference to common structure so we in fsg_bind_config()