Lines Matching refs:common
375 struct fsg_common *common; member
390 static inline int __fsg_is_set(struct fsg_common *common, in __fsg_is_set() argument
393 if (common->fsg) in __fsg_is_set()
395 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line); in __fsg_is_set()
400 #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__)) argument
411 static int exception_in_progress(struct fsg_common *common) in exception_in_progress() argument
413 return common->state > FSG_STATE_IDLE; in exception_in_progress()
417 static void set_bulk_out_req_length(struct fsg_common *common, in set_bulk_out_req_length() argument
423 rem = length % common->bulk_out_maxpacket; in set_bulk_out_req_length()
425 length += common->bulk_out_maxpacket - rem; in set_bulk_out_req_length()
454 static void wakeup_thread(struct fsg_common *common) in wakeup_thread() argument
456 common->thread_wakeup_needed = 1; in wakeup_thread()
459 static void raise_exception(struct fsg_common *common, enum fsg_state new_state) in raise_exception() argument
464 if (common->state <= new_state) { in raise_exception()
465 common->exception_req_tag = common->ep0_req_tag; in raise_exception()
466 common->state = new_state; in raise_exception()
467 common->thread_wakeup_needed = 1; in raise_exception()
473 static int ep0_queue(struct fsg_common *common) in ep0_queue() argument
477 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC); in ep0_queue()
478 common->ep0->driver_data = common; in ep0_queue()
481 WARNING(common, "error in submission: %s --> %d\n", in ep0_queue()
482 common->ep0->name, rc); in ep0_queue()
494 struct fsg_common *common = ep->driver_data; in bulk_in_complete() local
498 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_in_complete()
506 wakeup_thread(common); in bulk_in_complete()
511 struct fsg_common *common = ep->driver_data; in bulk_out_complete() local
514 dump_msg(common, "bulk-out", req->buf, req->actual); in bulk_out_complete()
516 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_out_complete()
525 wakeup_thread(common); in bulk_out_complete()
536 struct usb_request *req = fsg->common->ep0req; in fsg_setup()
541 if (!fsg_is_set(fsg->common)) in fsg_setup()
556 raise_exception(fsg->common, FSG_STATE_RESET); in fsg_setup()
566 *(u8 *) req->buf = fsg->common->nluns - 1; in fsg_setup()
570 return ep0_queue(fsg->common); in fsg_setup()
613 #define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \ argument
614 if (fsg_is_set(common)) \
615 start_transfer((common)->fsg, (common)->fsg->ep_name, \
619 #define START_TRANSFER(common, ep_name, req, pbusy, state) \ argument
620 START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0
650 static int sleep_thread(struct fsg_common *common) in sleep_thread() argument
657 if (common->thread_wakeup_needed) in sleep_thread()
686 common->thread_wakeup_needed = 0; in sleep_thread()
692 static int do_read(struct fsg_common *common) in do_read() argument
694 struct fsg_lun *curlun = &common->luns[common->lun]; in do_read()
706 if (common->cmnd[0] == SC_READ_6) in do_read()
707 lba = get_unaligned_be24(&common->cmnd[1]); in do_read()
709 lba = get_unaligned_be32(&common->cmnd[2]); in do_read()
714 if ((common->cmnd[1] & ~0x18) != 0) { in do_read()
726 amount_left = common->data_size_from_cmnd; in do_read()
740 amount = min(amount_left, common->usb_trb_size); in do_read()
747 bh = common->next_buffhd_to_fill; in do_read()
749 rc = sleep_thread(common); in do_read()
766 rc = ums[common->lun].read_sector(&ums[common->lun], in do_read()
790 common->residue -= nread; in do_read()
806 START_TRANSFER_OR(common, bulk_in, bh->inreq, in do_read()
811 common->next_buffhd_to_fill = bh->next; in do_read()
819 static int do_write(struct fsg_common *common) in do_write() argument
821 struct fsg_lun *curlun = &common->luns[common->lun]; in do_write()
840 if (common->cmnd[0] == SC_WRITE_6) in do_write()
841 lba = get_unaligned_be24(&common->cmnd[1]); in do_write()
843 lba = get_unaligned_be32(&common->cmnd[2]); in do_write()
849 if (common->cmnd[1] & ~0x18) { in do_write()
862 amount_left_to_req = common->data_size_from_cmnd; in do_write()
863 amount_left_to_write = common->data_size_from_cmnd; in do_write()
868 bh = common->next_buffhd_to_fill; in do_write()
880 amount = min(amount_left_to_req, common->usb_trb_size); in do_write()
904 common->usb_amount_left -= amount; in do_write()
914 START_TRANSFER_OR(common, bulk_out, bh->outreq, in do_write()
919 common->next_buffhd_to_fill = bh->next; in do_write()
924 bh = common->next_buffhd_to_drain; in do_write()
928 common->next_buffhd_to_drain = bh->next; in do_write()
941 rc = ums[common->lun].write_sector(&ums[common->lun], in do_write()
965 common->residue -= nwritten; in do_write()
978 common->short_packet_received = 1; in do_write()
985 rc = sleep_thread(common); in do_write()
990 cdev_name = common->fsg->function.config->cdev->driver->name; in do_write()
992 rkusb_do_check_parity(common); in do_write()
999 static int do_synchronize_cache(struct fsg_common *common) in do_synchronize_cache() argument
1006 static int do_verify(struct fsg_common *common) in do_verify() argument
1008 struct fsg_lun *curlun = &common->luns[common->lun]; in do_verify()
1011 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in do_verify()
1020 lba = get_unaligned_be32(&common->cmnd[2]); in do_verify()
1028 if (common->cmnd[1] & ~0x10) { in do_verify()
1033 verification_length = get_unaligned_be16(&common->cmnd[7]); in do_verify()
1052 amount = min(amount_left, common->usb_trb_size); in do_verify()
1061 rc = ums[common->lun].read_sector(&ums[common->lun], in do_verify()
1094 static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh) in do_inquiry() argument
1096 struct fsg_lun *curlun = &common->luns[common->lun]; in do_inquiry()
1101 common->bad_lun_okay = 1; in do_inquiry()
1116 ums[common->lun].name, (u16) 0xffff); in do_inquiry()
1122 static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_request_sense() argument
1124 struct fsg_lun *curlun = &common->luns[common->lun]; in do_request_sense()
1152 common->bad_lun_okay = 1; in do_request_sense()
1173 static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_capacity() argument
1175 struct fsg_lun *curlun = &common->luns[common->lun]; in do_read_capacity()
1176 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_capacity()
1177 int pmi = common->cmnd[8]; in do_read_capacity()
1192 static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_header() argument
1194 struct fsg_lun *curlun = &common->luns[common->lun]; in do_read_header()
1195 int msf = common->cmnd[1] & 0x02; in do_read_header()
1196 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_header()
1199 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */ in do_read_header()
1215 static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_toc() argument
1217 struct fsg_lun *curlun = &common->luns[common->lun]; in do_read_toc()
1218 int msf = common->cmnd[1] & 0x02; in do_read_toc()
1219 int start_track = common->cmnd[6]; in do_read_toc()
1222 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ in do_read_toc()
1243 static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_sense() argument
1245 struct fsg_lun *curlun = &common->luns[common->lun]; in do_mode_sense()
1246 int mscmnd = common->cmnd[0]; in do_mode_sense()
1254 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */ in do_mode_sense()
1258 pc = common->cmnd[2] >> 6; in do_mode_sense()
1259 page_code = common->cmnd[2] & 0x3f; in do_mode_sense()
1324 static int do_start_stop(struct fsg_common *common) in do_start_stop() argument
1326 struct fsg_lun *curlun = &common->luns[common->lun]; in do_start_stop()
1338 static int do_prevent_allow(struct fsg_common *common) in do_prevent_allow() argument
1340 struct fsg_lun *curlun = &common->luns[common->lun]; in do_prevent_allow()
1348 prevent = common->cmnd[4] & 0x01; in do_prevent_allow()
1349 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */ in do_prevent_allow()
1361 static int do_read_format_capacities(struct fsg_common *common, in do_read_format_capacities() argument
1364 struct fsg_lun *curlun = &common->luns[common->lun]; in do_read_format_capacities()
1379 static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_select() argument
1381 struct fsg_lun *curlun = &common->luns[common->lun]; in do_mode_select()
1431 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill; in pad_with_zeros()
1437 fsg->common->usb_amount_left = nkeep + fsg->common->residue; in pad_with_zeros()
1438 while (fsg->common->usb_amount_left > 0) { in pad_with_zeros()
1442 rc = sleep_thread(fsg->common); in pad_with_zeros()
1447 nsend = min(fsg->common->usb_amount_left, in pad_with_zeros()
1448 fsg->common->usb_trb_size); in pad_with_zeros()
1454 bh = fsg->common->next_buffhd_to_fill = bh->next; in pad_with_zeros()
1455 fsg->common->usb_amount_left -= nsend; in pad_with_zeros()
1461 static int throw_away_data(struct fsg_common *common) in throw_away_data() argument
1467 for (bh = common->next_buffhd_to_drain; in throw_away_data()
1468 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; in throw_away_data()
1469 bh = common->next_buffhd_to_drain) { in throw_away_data()
1474 common->next_buffhd_to_drain = bh->next; in throw_away_data()
1479 raise_exception(common, in throw_away_data()
1487 bh = common->next_buffhd_to_fill; in throw_away_data()
1489 && common->usb_amount_left > 0) { in throw_away_data()
1490 amount = min(common->usb_amount_left, in throw_away_data()
1491 common->usb_trb_size); in throw_away_data()
1498 START_TRANSFER_OR(common, bulk_out, bh->outreq, in throw_away_data()
1503 common->next_buffhd_to_fill = bh->next; in throw_away_data()
1504 common->usb_amount_left -= amount; in throw_away_data()
1509 rc = sleep_thread(common); in throw_away_data()
1517 static int finish_reply(struct fsg_common *common) in finish_reply() argument
1519 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in finish_reply()
1522 switch (common->data_dir) { in finish_reply()
1531 if (!common->can_stall) { in finish_reply()
1533 } else if (fsg_is_set(common)) { in finish_reply()
1534 fsg_set_halt(common->fsg, common->fsg->bulk_out); in finish_reply()
1535 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1544 if (common->data_size == 0) { in finish_reply()
1548 } else if (common->residue == 0) { in finish_reply()
1550 START_TRANSFER_OR(common, bulk_in, bh->inreq, in finish_reply()
1553 common->next_buffhd_to_fill = bh->next; in finish_reply()
1558 } else if (common->can_stall) { in finish_reply()
1560 START_TRANSFER_OR(common, bulk_in, bh->inreq, in finish_reply()
1565 common->next_buffhd_to_fill = bh->next; in finish_reply()
1566 if (common->fsg) in finish_reply()
1567 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1568 } else if (fsg_is_set(common)) { in finish_reply()
1569 rc = pad_with_zeros(common->fsg); in finish_reply()
1579 if (common->residue == 0) { in finish_reply()
1583 } else if (common->short_packet_received) { in finish_reply()
1584 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1594 } else if (common->can_stall) { in finish_reply()
1595 if (fsg_is_set(common)) in finish_reply()
1596 fsg_set_halt(common->fsg, in finish_reply()
1597 common->fsg->bulk_out); in finish_reply()
1598 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1605 rc = throw_away_data(common); in finish_reply()
1613 static int send_status(struct fsg_common *common) in send_status() argument
1615 struct fsg_lun *curlun = &common->luns[common->lun]; in send_status()
1623 bh = common->next_buffhd_to_fill; in send_status()
1625 rc = sleep_thread(common); in send_status()
1632 else if (common->bad_lun_okay) in send_status()
1637 if (common->phase_error) { in send_status()
1638 DBG(common, "sending phase-error status\n"); in send_status()
1642 DBG(common, "sending command-failure status\n"); in send_status()
1644 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;" in send_status()
1653 csw->Tag = common->tag; in send_status()
1654 csw->Residue = cpu_to_le32(common->residue); in send_status()
1659 START_TRANSFER_OR(common, bulk_in, bh->inreq, in send_status()
1664 common->next_buffhd_to_fill = bh->next; in send_status()
1676 static int check_command(struct fsg_common *common, int cmnd_size, in check_command() argument
1681 int lun = common->cmnd[1] >> 5; in check_command()
1687 if (common->data_dir != DATA_DIR_UNKNOWN) in check_command()
1688 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir], in check_command()
1689 common->data_size); in check_command()
1690 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", in check_command()
1692 common->data_size_from_cmnd, common->cmnd_size, hdlen); in check_command()
1696 if (common->data_size_from_cmnd == 0) in check_command()
1698 if (common->data_size < common->data_size_from_cmnd) { in check_command()
1702 common->data_size_from_cmnd = common->data_size; in check_command()
1703 common->phase_error = 1; in check_command()
1705 common->residue = common->data_size; in check_command()
1706 common->usb_amount_left = common->data_size; in check_command()
1709 if (common->data_dir != data_dir in check_command()
1710 && common->data_size_from_cmnd > 0) { in check_command()
1711 common->phase_error = 1; in check_command()
1716 if (cmnd_size != common->cmnd_size) { in check_command()
1730 if (cmnd_size <= common->cmnd_size) { in check_command()
1731 DBG(common, "%s is buggy! Expected length %d " in check_command()
1733 cmnd_size, common->cmnd_size); in check_command()
1734 cmnd_size = common->cmnd_size; in check_command()
1736 common->phase_error = 1; in check_command()
1742 if (common->lun != lun) in check_command()
1743 DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n", in check_command()
1744 common->lun, lun); in check_command()
1747 if (common->lun < common->nluns) { in check_command()
1748 curlun = &common->luns[common->lun]; in check_command()
1749 if (common->cmnd[0] != SC_REQUEST_SENSE) { in check_command()
1755 common->bad_lun_okay = 0; in check_command()
1759 if (common->cmnd[0] != SC_INQUIRY && in check_command()
1760 common->cmnd[0] != SC_REQUEST_SENSE) { in check_command()
1761 DBG(common, "unsupported LUN %d\n", common->lun); in check_command()
1769 common->cmnd[0] != SC_INQUIRY && in check_command()
1770 common->cmnd[0] != SC_REQUEST_SENSE) { in check_command()
1777 common->cmnd[1] &= 0x1f; /* Mask away the LUN */ in check_command()
1779 if (common->cmnd[i] && !(mask & (1 << i))) { in check_command()
1790 static int do_scsi_command(struct fsg_common *common) in do_scsi_command() argument
1797 struct fsg_lun *curlun = &common->luns[common->lun]; in do_scsi_command()
1800 dump_cdb(common); in do_scsi_command()
1803 bh = common->next_buffhd_to_fill; in do_scsi_command()
1804 common->next_buffhd_to_drain = bh; in do_scsi_command()
1806 rc = sleep_thread(common); in do_scsi_command()
1810 common->phase_error = 0; in do_scsi_command()
1811 common->short_packet_received = 0; in do_scsi_command()
1813 down_read(&common->filesem); /* We're using the backing file */ in do_scsi_command()
1815 cdev_name = common->fsg->function.config->cdev->driver->name; in do_scsi_command()
1817 rc = rkusb_cmd_process(common, bh, &reply); in do_scsi_command()
1824 switch (common->cmnd[0]) { in do_scsi_command()
1827 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1828 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1832 reply = do_inquiry(common, bh); in do_scsi_command()
1836 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1837 reply = check_command(common, 6, DATA_DIR_FROM_HOST, in do_scsi_command()
1841 reply = do_mode_select(common, bh); in do_scsi_command()
1845 common->data_size_from_cmnd = in do_scsi_command()
1846 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1847 reply = check_command(common, 10, DATA_DIR_FROM_HOST, in do_scsi_command()
1851 reply = do_mode_select(common, bh); in do_scsi_command()
1855 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1856 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1860 reply = do_mode_sense(common, bh); in do_scsi_command()
1864 common->data_size_from_cmnd = in do_scsi_command()
1865 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1866 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1870 reply = do_mode_sense(common, bh); in do_scsi_command()
1874 common->data_size_from_cmnd = 0; in do_scsi_command()
1875 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1879 reply = do_prevent_allow(common); in do_scsi_command()
1883 i = common->cmnd[4]; in do_scsi_command()
1884 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; in do_scsi_command()
1885 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1889 reply = do_read(common); in do_scsi_command()
1893 common->data_size_from_cmnd = in do_scsi_command()
1894 get_unaligned_be16(&common->cmnd[7]) << 9; in do_scsi_command()
1895 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1899 reply = do_read(common); in do_scsi_command()
1903 common->data_size_from_cmnd = in do_scsi_command()
1904 get_unaligned_be32(&common->cmnd[6]) << 9; in do_scsi_command()
1905 reply = check_command(common, 12, DATA_DIR_TO_HOST, in do_scsi_command()
1909 reply = do_read(common); in do_scsi_command()
1913 common->data_size_from_cmnd = 8; in do_scsi_command()
1914 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1918 reply = do_read_capacity(common, bh); in do_scsi_command()
1922 if (!common->luns[common->lun].cdrom) in do_scsi_command()
1924 common->data_size_from_cmnd = in do_scsi_command()
1925 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1926 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1930 reply = do_read_header(common, bh); in do_scsi_command()
1934 if (!common->luns[common->lun].cdrom) in do_scsi_command()
1936 common->data_size_from_cmnd = in do_scsi_command()
1937 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1938 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1942 reply = do_read_toc(common, bh); in do_scsi_command()
1946 common->data_size_from_cmnd = in do_scsi_command()
1947 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1948 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1952 reply = do_read_format_capacities(common, bh); in do_scsi_command()
1956 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1957 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1961 reply = do_request_sense(common, bh); in do_scsi_command()
1965 common->data_size_from_cmnd = 0; in do_scsi_command()
1966 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1970 reply = do_start_stop(common); in do_scsi_command()
1974 common->data_size_from_cmnd = 0; in do_scsi_command()
1975 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
1979 reply = do_synchronize_cache(common); in do_scsi_command()
1983 common->data_size_from_cmnd = 0; in do_scsi_command()
1984 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1992 common->data_size_from_cmnd = 0; in do_scsi_command()
1993 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
1997 reply = do_verify(common); in do_scsi_command()
2001 i = common->cmnd[4]; in do_scsi_command()
2002 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; in do_scsi_command()
2003 reply = check_command(common, 6, DATA_DIR_FROM_HOST, in do_scsi_command()
2007 reply = do_write(common); in do_scsi_command()
2011 common->data_size_from_cmnd = in do_scsi_command()
2012 get_unaligned_be16(&common->cmnd[7]) << 9; in do_scsi_command()
2015 reply = check_command(common, common->cmnd_size, DATA_DIR_FROM_HOST, in do_scsi_command()
2019 reply = check_command(common, 10, DATA_DIR_FROM_HOST, in do_scsi_command()
2025 reply = do_write(common); in do_scsi_command()
2029 common->data_size_from_cmnd = in do_scsi_command()
2030 get_unaligned_be32(&common->cmnd[6]) << 9; in do_scsi_command()
2031 reply = check_command(common, 12, DATA_DIR_FROM_HOST, in do_scsi_command()
2035 reply = do_write(common); in do_scsi_command()
2050 common->data_size_from_cmnd = 0; in do_scsi_command()
2051 sprintf(unknown, "Unknown x%02x", common->cmnd[0]); in do_scsi_command()
2052 reply = check_command(common, common->cmnd_size, in do_scsi_command()
2062 up_read(&common->filesem); in do_scsi_command()
2070 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) { in do_scsi_command()
2071 reply = min((u32) reply, common->data_size_from_cmnd); in do_scsi_command()
2074 common->residue -= reply; in do_scsi_command()
2086 struct fsg_common *common = fsg->common; in received_cbw() local
2123 if (common->can_stall) { in received_cbw()
2131 common->cmnd_size = cbw->Length; in received_cbw()
2132 memcpy(common->cmnd, cbw->CDB, common->cmnd_size); in received_cbw()
2134 common->data_dir = DATA_DIR_TO_HOST; in received_cbw()
2136 common->data_dir = DATA_DIR_FROM_HOST; in received_cbw()
2137 common->data_size = le32_to_cpu(cbw->DataTransferLength); in received_cbw()
2138 if (common->data_size == 0) in received_cbw()
2139 common->data_dir = DATA_DIR_NONE; in received_cbw()
2140 common->lun = cbw->Lun; in received_cbw()
2141 common->tag = cbw->Tag; in received_cbw()
2146 static int get_next_command(struct fsg_common *common) in get_next_command() argument
2152 bh = common->next_buffhd_to_fill; in get_next_command()
2154 rc = sleep_thread(common); in get_next_command()
2160 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN); in get_next_command()
2162 START_TRANSFER_OR(common, bulk_out, bh->outreq, in get_next_command()
2173 rc = sleep_thread(common); in get_next_command()
2178 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; in get_next_command()
2187 static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep, in enable_endpoint() argument
2192 ep->driver_data = common; in enable_endpoint()
2195 ERROR(common, "can't enable %s, result %d\n", ep->name, rc); in enable_endpoint()
2199 static int alloc_request(struct fsg_common *common, struct usb_ep *ep, in alloc_request() argument
2205 ERROR(common, "can't allocate request for %s\n", ep->name); in alloc_request()
2210 static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg) in do_set_interface() argument
2216 if (common->running) in do_set_interface()
2217 DBG(common, "reset interface\n"); in do_set_interface()
2221 if (common->fsg) { in do_set_interface()
2222 fsg = common->fsg; in do_set_interface()
2225 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2247 common->fsg = NULL; in do_set_interface()
2251 common->running = 0; in do_set_interface()
2255 common->fsg = new_fsg; in do_set_interface()
2256 fsg = common->fsg; in do_set_interface()
2259 d = fsg_ep_desc(common->gadget, in do_set_interface()
2263 rc = enable_endpoint(common, fsg->bulk_in, d); in do_set_interface()
2268 d = fsg_ep_desc(common->gadget, in do_set_interface()
2272 rc = enable_endpoint(common, fsg->bulk_out, d); in do_set_interface()
2276 common->bulk_out_maxpacket = in do_set_interface()
2282 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2284 rc = alloc_request(common, fsg->bulk_in, &bh->inreq); in do_set_interface()
2287 rc = alloc_request(common, fsg->bulk_out, &bh->outreq); in do_set_interface()
2296 common->running = 1; in do_set_interface()
2308 fsg->common->new_fsg = fsg; in fsg_set_alt()
2309 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_set_alt()
2316 fsg->common->new_fsg = NULL; in fsg_disable()
2317 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_disable()
2322 static void handle_exception(struct fsg_common *common) in handle_exception() argument
2331 if (common->fsg) { in handle_exception()
2333 bh = &common->buffhds[i]; in handle_exception()
2335 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq); in handle_exception()
2337 usb_ep_dequeue(common->fsg->bulk_out, in handle_exception()
2345 bh = &common->buffhds[i]; in handle_exception()
2350 if (sleep_thread(common)) in handle_exception()
2355 if (common->fsg->bulk_in_enabled) in handle_exception()
2356 usb_ep_fifo_flush(common->fsg->bulk_in); in handle_exception()
2357 if (common->fsg->bulk_out_enabled) in handle_exception()
2358 usb_ep_fifo_flush(common->fsg->bulk_out); in handle_exception()
2365 bh = &common->buffhds[i]; in handle_exception()
2368 common->next_buffhd_to_fill = &common->buffhds[0]; in handle_exception()
2369 common->next_buffhd_to_drain = &common->buffhds[0]; in handle_exception()
2370 exception_req_tag = common->exception_req_tag; in handle_exception()
2371 old_state = common->state; in handle_exception()
2374 common->state = FSG_STATE_STATUS_PHASE; in handle_exception()
2376 for (i = 0; i < common->nluns; ++i) { in handle_exception()
2377 curlun = &common->luns[i]; in handle_exception()
2381 common->state = FSG_STATE_IDLE; in handle_exception()
2387 send_status(common); in handle_exception()
2389 if (common->state == FSG_STATE_STATUS_PHASE) in handle_exception()
2390 common->state = FSG_STATE_IDLE; in handle_exception()
2397 if (!fsg_is_set(common)) in handle_exception()
2400 &common->fsg->atomic_bitflags)) in handle_exception()
2401 usb_ep_clear_halt(common->fsg->bulk_in); in handle_exception()
2403 if (common->ep0_req_tag == exception_req_tag) in handle_exception()
2404 ep0_queue(common); /* Complete the status stage */ in handle_exception()
2409 do_set_interface(common, common->new_fsg); in handle_exception()
2414 do_set_interface(common, NULL); /* Free resources */ in handle_exception()
2415 common->state = FSG_STATE_TERMINATED; /* Stop the thread */ in handle_exception()
2433 struct fsg_common *common = the_fsg_common; in fsg_main_thread() local
2436 if (exception_in_progress(common)) { in fsg_main_thread()
2437 handle_exception(common); in fsg_main_thread()
2441 if (!common->running) { in fsg_main_thread()
2442 ret = sleep_thread(common); in fsg_main_thread()
2449 ret = get_next_command(common); in fsg_main_thread()
2453 if (!exception_in_progress(common)) in fsg_main_thread()
2454 common->state = FSG_STATE_DATA_PHASE; in fsg_main_thread()
2456 if (do_scsi_command(common) || finish_reply(common)) in fsg_main_thread()
2459 if (!exception_in_progress(common)) in fsg_main_thread()
2460 common->state = FSG_STATE_STATUS_PHASE; in fsg_main_thread()
2462 if (send_status(common)) in fsg_main_thread()
2465 if (!exception_in_progress(common)) in fsg_main_thread()
2466 common->state = FSG_STATE_IDLE; in fsg_main_thread()
2469 common->thread_task = NULL; in fsg_main_thread()
2476 static struct fsg_common *fsg_common_init(struct fsg_common *common, in fsg_common_init() argument
2492 if (!common) { in fsg_common_init()
2493 common = calloc(sizeof(*common), 1); in fsg_common_init()
2494 if (!common) in fsg_common_init()
2496 common->free_storage_on_release = 1; in fsg_common_init()
2498 memset(common, 0, sizeof(*common)); in fsg_common_init()
2499 common->free_storage_on_release = 0; in fsg_common_init()
2502 common->ops = NULL; in fsg_common_init()
2503 common->private_data = NULL; in fsg_common_init()
2505 common->gadget = gadget; in fsg_common_init()
2506 common->ep0 = gadget->ep0; in fsg_common_init()
2507 common->ep0req = cdev->req; in fsg_common_init()
2508 common->usb_trb_size = FSG_BUFLEN; in fsg_common_init()
2526 common->nluns = nluns; in fsg_common_init()
2529 common->luns[i].removable = 1; in fsg_common_init()
2531 rc = fsg_lun_open(&common->luns[i], ums[i].num_sectors, ""); in fsg_common_init()
2535 common->lun = 0; in fsg_common_init()
2538 bh = common->buffhds; in fsg_common_init()
2554 bh->next = common->buffhds; in fsg_common_init()
2556 snprintf(common->inquiry_string, sizeof common->inquiry_string, in fsg_common_init()
2568 common->thread_task = in fsg_common_init()
2569 kthread_create(fsg_main_thread, common, in fsg_common_init()
2571 if (IS_ERR(common->thread_task)) { in fsg_common_init()
2572 rc = PTR_ERR(common->thread_task); in fsg_common_init()
2578 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n"); in fsg_common_init()
2579 INFO(common, "Number of LUNs=%d\n", common->nluns); in fsg_common_init()
2581 return common; in fsg_common_init()
2584 common->nluns = i + 1; in fsg_common_init()
2586 common->state = FSG_STATE_TERMINATED; /* The thread is dead */ in fsg_common_init()
2589 fsg_common_release(&common->ref); in fsg_common_init()
2595 struct fsg_common *common = container_of(ref, struct fsg_common, ref); in fsg_common_release() local
2598 if (common->state != FSG_STATE_TERMINATED) { in fsg_common_release()
2599 raise_exception(common, FSG_STATE_EXIT); in fsg_common_release()
2600 wait_for_completion(&common->thread_notifier); in fsg_common_release()
2603 if (likely(common->luns)) { in fsg_common_release()
2604 struct fsg_lun *lun = common->luns; in fsg_common_release()
2605 unsigned i = common->nluns; in fsg_common_release()
2611 kfree(common->luns); in fsg_common_release()
2615 struct fsg_buffhd *bh = common->buffhds; in fsg_common_release()
2622 if (common->free_storage_on_release) in fsg_common_release()
2623 kfree(common); in fsg_common_release()
2683 if (fsg->common->fsg == fsg) { in fsg_unbind()
2684 fsg->common->new_fsg = NULL; in fsg_unbind()
2685 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_unbind()
2712 ep->driver_data = fsg->common; /* claim the endpoint */ in fsg_bind()
2718 ep->driver_data = fsg->common; /* claim the endpoint */ in fsg_bind()
2783 struct fsg_common *common) in fsg_bind_config() argument
2799 fsg->common = common; in fsg_bind_config()
2800 common->fsg = fsg; in fsg_bind_config()