Lines Matching full:common

329 	int (*thread_exits)(struct fsg_common *common);
336 int (*pre_eject)(struct fsg_common *common,
342 int (*post_eject)(struct fsg_common *common,
439 struct fsg_common *common; member
453 static inline int __fsg_is_set(struct fsg_common *common, in __fsg_is_set() argument
456 if (common->fsg) in __fsg_is_set()
458 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line); in __fsg_is_set()
463 #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__)) argument
472 static int exception_in_progress(struct fsg_common *common) in exception_in_progress() argument
474 return common->state > FSG_STATE_IDLE; in exception_in_progress()
478 static void set_bulk_out_req_length(struct fsg_common *common, in set_bulk_out_req_length() argument
484 rem = length % common->bulk_out_maxpacket; in set_bulk_out_req_length()
486 length += common->bulk_out_maxpacket - rem; in set_bulk_out_req_length()
513 static void wakeup_thread(struct fsg_common *common) in wakeup_thread() argument
516 common->thread_wakeup_needed = 1; in wakeup_thread()
517 if (common->thread_task) in wakeup_thread()
518 wake_up_process(common->thread_task); in wakeup_thread()
521 static void raise_exception(struct fsg_common *common, enum fsg_state new_state) in raise_exception() argument
530 spin_lock_irqsave(&common->lock, flags); in raise_exception()
531 if (common->state <= new_state) { in raise_exception()
532 common->exception_req_tag = common->ep0_req_tag; in raise_exception()
533 common->state = new_state; in raise_exception()
534 if (common->thread_task) in raise_exception()
536 common->thread_task); in raise_exception()
538 spin_unlock_irqrestore(&common->lock, flags); in raise_exception()
544 static int ep0_queue(struct fsg_common *common) in ep0_queue() argument
548 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC); in ep0_queue()
549 common->ep0->driver_data = common; in ep0_queue()
552 WARNING(common, "error in submission: %s --> %d\n", in ep0_queue()
553 common->ep0->name, rc); in ep0_queue()
565 struct fsg_common *common = ep->driver_data; in bulk_in_complete() local
569 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_in_complete()
576 spin_lock(&common->lock); in bulk_in_complete()
579 wakeup_thread(common); in bulk_in_complete()
580 spin_unlock(&common->lock); in bulk_in_complete()
585 struct fsg_common *common = ep->driver_data; in bulk_out_complete() local
588 dump_msg(common, "bulk-out", req->buf, req->actual); in bulk_out_complete()
590 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_out_complete()
597 spin_lock(&common->lock); in bulk_out_complete()
600 wakeup_thread(common); in bulk_out_complete()
601 spin_unlock(&common->lock); in bulk_out_complete()
608 struct usb_request *req = fsg->common->ep0req; in fsg_setup()
613 if (!fsg_is_set(fsg->common)) in fsg_setup()
616 ++fsg->common->ep0_req_tag; /* Record arrival of a new request */ in fsg_setup()
636 raise_exception(fsg->common, FSG_STATE_RESET); in fsg_setup()
647 *(u8 *)req->buf = fsg->common->nluns - 1; in fsg_setup()
651 return ep0_queue(fsg->common); in fsg_setup()
676 spin_lock_irq(&fsg->common->lock); in start_transfer()
679 spin_unlock_irq(&fsg->common->lock); in start_transfer()
698 static bool start_in_transfer(struct fsg_common *common, struct fsg_buffhd *bh) in start_in_transfer() argument
700 if (!fsg_is_set(common)) in start_in_transfer()
702 start_transfer(common->fsg, common->fsg->bulk_in, in start_in_transfer()
707 static bool start_out_transfer(struct fsg_common *common, struct fsg_buffhd *bh) in start_out_transfer() argument
709 if (!fsg_is_set(common)) in start_out_transfer()
711 start_transfer(common->fsg, common->fsg->bulk_out, in start_out_transfer()
716 static int sleep_thread(struct fsg_common *common) in sleep_thread() argument
728 if (common->thread_wakeup_needed) in sleep_thread()
733 common->thread_wakeup_needed = 0; in sleep_thread()
740 static int do_read(struct fsg_common *common) in do_read() argument
742 struct fsg_lun *curlun = common->curlun; in do_read()
755 if (common->cmnd[0] == READ_6) in do_read()
756 lba = get_unaligned_be24(&common->cmnd[1]); in do_read()
758 lba = get_unaligned_be32(&common->cmnd[2]); in do_read()
765 if ((common->cmnd[1] & ~0x18) != 0) { in do_read()
777 amount_left = common->data_size_from_cmnd; in do_read()
793 bh = common->next_buffhd_to_fill; in do_read()
795 rc = sleep_thread(common); in do_read()
835 common->residue -= nread; in do_read()
859 if (!start_in_transfer(common, bh)) in do_read()
860 /* Don't know what to do if common->fsg is NULL */ in do_read()
862 common->next_buffhd_to_fill = bh->next; in do_read()
871 static int do_write(struct fsg_common *common) in do_write() argument
873 struct fsg_lun *curlun = common->curlun; in do_write()
895 if (common->cmnd[0] == WRITE_6) in do_write()
896 lba = get_unaligned_be24(&common->cmnd[1]); in do_write()
898 lba = get_unaligned_be32(&common->cmnd[2]); in do_write()
906 if (common->cmnd[1] & ~0x18) { in do_write()
910 if (!curlun->nofua && (common->cmnd[1] & 0x08)) { /* FUA */ in do_write()
924 amount_left_to_req = common->data_size_from_cmnd; in do_write()
925 amount_left_to_write = common->data_size_from_cmnd; in do_write()
930 bh = common->next_buffhd_to_fill; in do_write()
953 common->usb_amount_left -= amount; in do_write()
963 set_bulk_out_req_length(common, bh, amount); in do_write()
964 if (!start_out_transfer(common, bh)) in do_write()
965 /* Dunno what to do if common->fsg is NULL */ in do_write()
967 common->next_buffhd_to_fill = bh->next; in do_write()
972 bh = common->next_buffhd_to_drain; in do_write()
977 common->next_buffhd_to_drain = bh->next; in do_write()
1029 common->residue -= nwritten; in do_write()
1043 common->short_packet_received = 1; in do_write()
1050 rc = sleep_thread(common); in do_write()
1061 static int do_synchronize_cache(struct fsg_common *common) in do_synchronize_cache() argument
1063 struct fsg_lun *curlun = common->curlun; in do_synchronize_cache()
1087 static int do_verify(struct fsg_common *common) in do_verify() argument
1089 struct fsg_lun *curlun = common->curlun; in do_verify()
1092 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in do_verify()
1102 lba = get_unaligned_be32(&common->cmnd[2]); in do_verify()
1112 if (common->cmnd[1] & ~0x10) { in do_verify()
1117 verification_length = get_unaligned_be16(&common->cmnd[7]); in do_verify()
1189 static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh) in do_inquiry() argument
1191 struct fsg_lun *curlun = common->curlun; in do_inquiry()
1195 common->bad_lun_okay = 1; in do_inquiry()
1210 memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string); in do_inquiry()
1214 static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_request_sense() argument
1216 struct fsg_lun *curlun = common->curlun; in do_request_sense()
1244 common->bad_lun_okay = 1; in do_request_sense()
1267 static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_capacity() argument
1269 struct fsg_lun *curlun = common->curlun; in do_read_capacity()
1270 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_capacity()
1271 int pmi = common->cmnd[8]; in do_read_capacity()
1286 static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_header() argument
1288 struct fsg_lun *curlun = common->curlun; in do_read_header()
1289 int msf = common->cmnd[1] & 0x02; in do_read_header()
1290 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_header()
1293 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */ in do_read_header()
1308 static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_toc() argument
1310 struct fsg_lun *curlun = common->curlun; in do_read_toc()
1311 int msf = common->cmnd[1] & 0x02; in do_read_toc()
1312 int start_track = common->cmnd[6]; in do_read_toc()
1315 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ in do_read_toc()
1335 static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_sense() argument
1337 struct fsg_lun *curlun = common->curlun; in do_mode_sense()
1338 int mscmnd = common->cmnd[0]; in do_mode_sense()
1346 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */ in do_mode_sense()
1350 pc = common->cmnd[2] >> 6; in do_mode_sense()
1351 page_code = common->cmnd[2] & 0x3f; in do_mode_sense()
1421 static int do_start_stop(struct fsg_common *common) in do_start_stop() argument
1423 struct fsg_lun *curlun = common->curlun; in do_start_stop()
1431 } else if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */ in do_start_stop()
1432 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */ in do_start_stop()
1437 loej = common->cmnd[4] & 0x02; in do_start_stop()
1438 start = common->cmnd[4] & 0x01; in do_start_stop()
1463 if (common->ops && common->ops->pre_eject) { in do_start_stop()
1464 int r = common->ops->pre_eject(common, curlun, in do_start_stop()
1465 curlun - common->luns); in do_start_stop()
1472 up_read(&common->filesem); in do_start_stop()
1473 down_write(&common->filesem); in do_start_stop()
1475 up_write(&common->filesem); in do_start_stop()
1476 down_read(&common->filesem); in do_start_stop()
1478 return common->ops && common->ops->post_eject in do_start_stop()
1479 ? min(0, common->ops->post_eject(common, curlun, in do_start_stop()
1480 curlun - common->luns)) in do_start_stop()
1484 static int do_prevent_allow(struct fsg_common *common) in do_prevent_allow() argument
1486 struct fsg_lun *curlun = common->curlun; in do_prevent_allow()
1489 if (!common->curlun) { in do_prevent_allow()
1491 } else if (!common->curlun->removable) { in do_prevent_allow()
1492 common->curlun->sense_data = SS_INVALID_COMMAND; in do_prevent_allow()
1496 prevent = common->cmnd[4] & 0x01; in do_prevent_allow()
1497 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */ in do_prevent_allow()
1508 static int do_read_format_capacities(struct fsg_common *common, in do_read_format_capacities() argument
1511 struct fsg_lun *curlun = common->curlun; in do_read_format_capacities()
1525 static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_select() argument
1527 struct fsg_lun *curlun = common->curlun; in do_mode_select()
1583 static int throw_away_data(struct fsg_common *common) in throw_away_data() argument
1589 for (bh = common->next_buffhd_to_drain; in throw_away_data()
1590 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; in throw_away_data()
1591 bh = common->next_buffhd_to_drain) { in throw_away_data()
1597 common->next_buffhd_to_drain = bh->next; in throw_away_data()
1602 raise_exception(common, in throw_away_data()
1610 bh = common->next_buffhd_to_fill; in throw_away_data()
1612 && common->usb_amount_left > 0) { in throw_away_data()
1613 amount = min(common->usb_amount_left, FSG_BUFLEN); in throw_away_data()
1620 set_bulk_out_req_length(common, bh, amount); in throw_away_data()
1621 if (!start_out_transfer(common, bh)) in throw_away_data()
1622 /* Dunno what to do if common->fsg is NULL */ in throw_away_data()
1624 common->next_buffhd_to_fill = bh->next; in throw_away_data()
1625 common->usb_amount_left -= amount; in throw_away_data()
1630 rc = sleep_thread(common); in throw_away_data()
1637 static int finish_reply(struct fsg_common *common) in finish_reply() argument
1639 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in finish_reply()
1642 switch (common->data_dir) { in finish_reply()
1653 if (!common->can_stall) { in finish_reply()
1655 } else if (fsg_is_set(common)) { in finish_reply()
1656 fsg_set_halt(common->fsg, common->fsg->bulk_out); in finish_reply()
1657 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1659 /* Don't know what to do if common->fsg is NULL */ in finish_reply()
1666 if (common->data_size == 0) { in finish_reply()
1669 /* Don't know what to do if common->fsg is NULL */ in finish_reply()
1670 } else if (!fsg_is_set(common)) { in finish_reply()
1674 } else if (common->residue == 0) { in finish_reply()
1676 if (!start_in_transfer(common, bh)) in finish_reply()
1678 common->next_buffhd_to_fill = bh->next; in finish_reply()
1689 if (!start_in_transfer(common, bh)) in finish_reply()
1691 common->next_buffhd_to_fill = bh->next; in finish_reply()
1692 if (common->can_stall) in finish_reply()
1693 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1702 if (common->residue == 0) { in finish_reply()
1706 } else if (common->short_packet_received) { in finish_reply()
1707 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1719 } else if (common->can_stall) { in finish_reply()
1720 if (fsg_is_set(common)) in finish_reply()
1721 fsg_set_halt(common->fsg, in finish_reply()
1722 common->fsg->bulk_out); in finish_reply()
1723 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1732 rc = throw_away_data(common); in finish_reply()
1739 static int send_status(struct fsg_common *common) in send_status() argument
1741 struct fsg_lun *curlun = common->curlun; in send_status()
1749 bh = common->next_buffhd_to_fill; in send_status()
1751 rc = sleep_thread(common); in send_status()
1759 } else if (common->bad_lun_okay) in send_status()
1764 if (common->phase_error) { in send_status()
1765 DBG(common, "sending phase-error status\n"); in send_status()
1769 DBG(common, "sending command-failure status\n"); in send_status()
1771 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;" in send_status()
1780 csw->Tag = common->tag; in send_status()
1781 csw->Residue = cpu_to_le32(common->residue); in send_status()
1786 if (!start_in_transfer(common, bh)) in send_status()
1787 /* Don't know what to do if common->fsg is NULL */ in send_status()
1790 common->next_buffhd_to_fill = bh->next; in send_status()
1801 static int check_command(struct fsg_common *common, int cmnd_size, in check_command() argument
1806 int lun = common->cmnd[1] >> 5; in check_command()
1812 if (common->data_dir != DATA_DIR_UNKNOWN) in check_command()
1813 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir], in check_command()
1814 common->data_size); in check_command()
1815 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", in check_command()
1817 common->data_size_from_cmnd, common->cmnd_size, hdlen); in check_command()
1823 if (common->data_size_from_cmnd == 0) in check_command()
1825 if (common->data_size < common->data_size_from_cmnd) { in check_command()
1831 common->data_size_from_cmnd = common->data_size; in check_command()
1832 common->phase_error = 1; in check_command()
1834 common->residue = common->data_size; in check_command()
1835 common->usb_amount_left = common->data_size; in check_command()
1838 if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) { in check_command()
1839 common->phase_error = 1; in check_command()
1844 if (cmnd_size != common->cmnd_size) { in check_command()
1859 if (cmnd_size <= common->cmnd_size) { in check_command()
1860 DBG(common, "%s is buggy! Expected length %d " in check_command()
1862 cmnd_size, common->cmnd_size); in check_command()
1863 cmnd_size = common->cmnd_size; in check_command()
1865 common->phase_error = 1; in check_command()
1871 if (common->lun != lun) in check_command()
1872 DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n", in check_command()
1873 common->lun, lun); in check_command()
1876 curlun = common->curlun; in check_command()
1878 if (common->cmnd[0] != REQUEST_SENSE) { in check_command()
1884 common->bad_lun_okay = 0; in check_command()
1890 if (common->cmnd[0] != INQUIRY && in check_command()
1891 common->cmnd[0] != REQUEST_SENSE) { in check_command()
1892 DBG(common, "unsupported LUN %d\n", common->lun); in check_command()
1902 common->cmnd[0] != INQUIRY && in check_command()
1903 common->cmnd[0] != REQUEST_SENSE) { in check_command()
1910 common->cmnd[1] &= 0x1f; /* Mask away the LUN */ in check_command()
1912 if (common->cmnd[i] && !(mask & (1 << i))) { in check_command()
1930 static int check_command_size_in_blocks(struct fsg_common *common, in check_command_size_in_blocks() argument
1934 if (common->curlun) in check_command_size_in_blocks()
1935 common->data_size_from_cmnd <<= common->curlun->blkbits; in check_command_size_in_blocks()
1936 return check_command(common, cmnd_size, data_dir, in check_command_size_in_blocks()
1940 static int do_scsi_command(struct fsg_common *common) in do_scsi_command() argument
1948 dump_cdb(common); in do_scsi_command()
1951 bh = common->next_buffhd_to_fill; in do_scsi_command()
1952 common->next_buffhd_to_drain = bh; in do_scsi_command()
1954 rc = sleep_thread(common); in do_scsi_command()
1958 common->phase_error = 0; in do_scsi_command()
1959 common->short_packet_received = 0; in do_scsi_command()
1961 down_read(&common->filesem); /* We're using the backing file */ in do_scsi_command()
1962 switch (common->cmnd[0]) { in do_scsi_command()
1965 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1966 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1970 reply = do_inquiry(common, bh); in do_scsi_command()
1974 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1975 reply = check_command(common, 6, DATA_DIR_FROM_HOST, in do_scsi_command()
1979 reply = do_mode_select(common, bh); in do_scsi_command()
1983 common->data_size_from_cmnd = in do_scsi_command()
1984 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1985 reply = check_command(common, 10, DATA_DIR_FROM_HOST, in do_scsi_command()
1989 reply = do_mode_select(common, bh); in do_scsi_command()
1993 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1994 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1998 reply = do_mode_sense(common, bh); in do_scsi_command()
2002 common->data_size_from_cmnd = in do_scsi_command()
2003 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2004 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
2008 reply = do_mode_sense(common, bh); in do_scsi_command()
2012 common->data_size_from_cmnd = 0; in do_scsi_command()
2013 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
2017 reply = do_prevent_allow(common); in do_scsi_command()
2021 i = common->cmnd[4]; in do_scsi_command()
2022 common->data_size_from_cmnd = (i == 0) ? 256 : i; in do_scsi_command()
2023 reply = check_command_size_in_blocks(common, 6, in do_scsi_command()
2028 reply = do_read(common); in do_scsi_command()
2032 common->data_size_from_cmnd = in do_scsi_command()
2033 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2034 reply = check_command_size_in_blocks(common, 10, in do_scsi_command()
2039 reply = do_read(common); in do_scsi_command()
2043 common->data_size_from_cmnd = in do_scsi_command()
2044 get_unaligned_be32(&common->cmnd[6]); in do_scsi_command()
2045 reply = check_command_size_in_blocks(common, 12, in do_scsi_command()
2050 reply = do_read(common); in do_scsi_command()
2054 common->data_size_from_cmnd = 8; in do_scsi_command()
2055 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
2059 reply = do_read_capacity(common, bh); in do_scsi_command()
2063 if (!common->curlun || !common->curlun->cdrom) in do_scsi_command()
2065 common->data_size_from_cmnd = in do_scsi_command()
2066 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2067 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
2071 reply = do_read_header(common, bh); in do_scsi_command()
2075 if (!common->curlun || !common->curlun->cdrom) in do_scsi_command()
2077 common->data_size_from_cmnd = in do_scsi_command()
2078 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2079 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
2083 reply = do_read_toc(common, bh); in do_scsi_command()
2087 common->data_size_from_cmnd = in do_scsi_command()
2088 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2089 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
2093 reply = do_read_format_capacities(common, bh); in do_scsi_command()
2097 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
2098 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
2102 reply = do_request_sense(common, bh); in do_scsi_command()
2106 common->data_size_from_cmnd = 0; in do_scsi_command()
2107 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
2111 reply = do_start_stop(common); in do_scsi_command()
2115 common->data_size_from_cmnd = 0; in do_scsi_command()
2116 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
2120 reply = do_synchronize_cache(common); in do_scsi_command()
2124 common->data_size_from_cmnd = 0; in do_scsi_command()
2125 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
2135 common->data_size_from_cmnd = 0; in do_scsi_command()
2136 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
2140 reply = do_verify(common); in do_scsi_command()
2144 i = common->cmnd[4]; in do_scsi_command()
2145 common->data_size_from_cmnd = (i == 0) ? 256 : i; in do_scsi_command()
2146 reply = check_command_size_in_blocks(common, 6, in do_scsi_command()
2151 reply = do_write(common); in do_scsi_command()
2155 common->data_size_from_cmnd = in do_scsi_command()
2156 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2157 reply = check_command_size_in_blocks(common, 10, in do_scsi_command()
2162 reply = do_write(common); in do_scsi_command()
2166 common->data_size_from_cmnd = in do_scsi_command()
2167 get_unaligned_be32(&common->cmnd[6]); in do_scsi_command()
2168 reply = check_command_size_in_blocks(common, 12, in do_scsi_command()
2173 reply = do_write(common); in do_scsi_command()
2190 common->data_size_from_cmnd = 0; in do_scsi_command()
2191 sprintf(unknown, "Unknown x%02x", common->cmnd[0]); in do_scsi_command()
2192 reply = check_command(common, common->cmnd_size, in do_scsi_command()
2195 common->curlun->sense_data = SS_INVALID_COMMAND; in do_scsi_command()
2200 up_read(&common->filesem); in do_scsi_command()
2208 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) { in do_scsi_command()
2209 reply = min((u32)reply, common->data_size_from_cmnd); in do_scsi_command()
2212 common->residue -= reply; in do_scsi_command()
2225 struct fsg_common *common = fsg->common; in received_cbw() local
2266 if (common->can_stall) { in received_cbw()
2274 common->cmnd_size = cbw->Length; in received_cbw()
2275 memcpy(common->cmnd, cbw->CDB, common->cmnd_size); in received_cbw()
2277 common->data_dir = DATA_DIR_TO_HOST; in received_cbw()
2279 common->data_dir = DATA_DIR_FROM_HOST; in received_cbw()
2280 common->data_size = le32_to_cpu(cbw->DataTransferLength); in received_cbw()
2281 if (common->data_size == 0) in received_cbw()
2282 common->data_dir = DATA_DIR_NONE; in received_cbw()
2283 common->lun = cbw->Lun; in received_cbw()
2284 if (common->lun >= 0 && common->lun < common->nluns) in received_cbw()
2285 common->curlun = &common->luns[common->lun]; in received_cbw()
2287 common->curlun = NULL; in received_cbw()
2288 common->tag = cbw->Tag; in received_cbw()
2292 static int get_next_command(struct fsg_common *common) in get_next_command() argument
2298 bh = common->next_buffhd_to_fill; in get_next_command()
2300 rc = sleep_thread(common); in get_next_command()
2306 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN); in get_next_command()
2307 if (!start_out_transfer(common, bh)) in get_next_command()
2308 /* Don't know what to do if common->fsg is NULL */ in get_next_command()
2319 rc = sleep_thread(common); in get_next_command()
2324 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; in get_next_command()
2333 static int alloc_request(struct fsg_common *common, struct usb_ep *ep, in alloc_request() argument
2339 ERROR(common, "can't allocate request for %s\n", ep->name); in alloc_request()
2344 static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg) in do_set_interface() argument
2349 if (common->running) in do_set_interface()
2350 DBG(common, "reset interface\n"); in do_set_interface()
2354 if (common->fsg) { in do_set_interface()
2355 fsg = common->fsg; in do_set_interface()
2358 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2380 common->fsg = NULL; in do_set_interface()
2381 wake_up(&common->fsg_wait); in do_set_interface()
2384 common->running = 0; in do_set_interface()
2388 common->fsg = new_fsg; in do_set_interface()
2389 fsg = common->fsg; in do_set_interface()
2392 rc = config_ep_by_speed(common->gadget, &(fsg->function), fsg->bulk_in); in do_set_interface()
2398 fsg->bulk_in->driver_data = common; in do_set_interface()
2401 rc = config_ep_by_speed(common->gadget, &(fsg->function), in do_set_interface()
2408 fsg->bulk_out->driver_data = common; in do_set_interface()
2410 common->bulk_out_maxpacket = usb_endpoint_maxp(fsg->bulk_out->desc); in do_set_interface()
2415 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2417 rc = alloc_request(common, fsg->bulk_in, &bh->inreq); in do_set_interface()
2420 rc = alloc_request(common, fsg->bulk_out, &bh->outreq); in do_set_interface()
2429 common->running = 1; in do_set_interface()
2430 for (i = 0; i < common->nluns; ++i) in do_set_interface()
2431 common->luns[i].unit_attention_data = SS_RESET_OCCURRED; in do_set_interface()
2441 fsg->common->new_fsg = fsg; in fsg_set_alt()
2442 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_set_alt()
2449 fsg->common->new_fsg = NULL; in fsg_disable()
2450 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_disable()
2456 static void handle_exception(struct fsg_common *common) in handle_exception() argument
2475 if (common->state < FSG_STATE_EXIT) in handle_exception()
2476 DBG(common, "Main thread exiting on signal\n"); in handle_exception()
2477 raise_exception(common, FSG_STATE_EXIT); in handle_exception()
2482 if (likely(common->fsg)) { in handle_exception()
2484 bh = &common->buffhds[i]; in handle_exception()
2486 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq); in handle_exception()
2488 usb_ep_dequeue(common->fsg->bulk_out, in handle_exception()
2496 bh = &common->buffhds[i]; in handle_exception()
2501 if (sleep_thread(common)) in handle_exception()
2506 if (common->fsg->bulk_in_enabled) in handle_exception()
2507 usb_ep_fifo_flush(common->fsg->bulk_in); in handle_exception()
2508 if (common->fsg->bulk_out_enabled) in handle_exception()
2509 usb_ep_fifo_flush(common->fsg->bulk_out); in handle_exception()
2516 spin_lock_irq(&common->lock); in handle_exception()
2519 bh = &common->buffhds[i]; in handle_exception()
2522 common->next_buffhd_to_fill = &common->buffhds[0]; in handle_exception()
2523 common->next_buffhd_to_drain = &common->buffhds[0]; in handle_exception()
2524 exception_req_tag = common->exception_req_tag; in handle_exception()
2525 old_state = common->state; in handle_exception()
2528 common->state = FSG_STATE_STATUS_PHASE; in handle_exception()
2530 for (i = 0; i < common->nluns; ++i) { in handle_exception()
2531 curlun = &common->luns[i]; in handle_exception()
2538 common->state = FSG_STATE_IDLE; in handle_exception()
2540 spin_unlock_irq(&common->lock); in handle_exception()
2545 send_status(common); in handle_exception()
2546 spin_lock_irq(&common->lock); in handle_exception()
2547 if (common->state == FSG_STATE_STATUS_PHASE) in handle_exception()
2548 common->state = FSG_STATE_IDLE; in handle_exception()
2549 spin_unlock_irq(&common->lock); in handle_exception()
2558 if (!fsg_is_set(common)) in handle_exception()
2561 &common->fsg->atomic_bitflags)) in handle_exception()
2562 usb_ep_clear_halt(common->fsg->bulk_in); in handle_exception()
2564 if (common->ep0_req_tag == exception_req_tag) in handle_exception()
2565 ep0_queue(common); /* Complete the status stage */ in handle_exception()
2572 /* for (i = 0; i < common->nluns; ++i) */ in handle_exception()
2573 /* common->luns[i].unit_attention_data = */ in handle_exception()
2578 do_set_interface(common, common->new_fsg); in handle_exception()
2579 if (common->new_fsg) in handle_exception()
2580 usb_composite_setup_continue(common->cdev); in handle_exception()
2585 do_set_interface(common, NULL); /* Free resources */ in handle_exception()
2586 spin_lock_irq(&common->lock); in handle_exception()
2587 common->state = FSG_STATE_TERMINATED; /* Stop the thread */ in handle_exception()
2588 spin_unlock_irq(&common->lock); in handle_exception()
2606 struct fsg_common *common = common_; in fsg_main_thread() local
2628 while (common->state != FSG_STATE_TERMINATED) { in fsg_main_thread()
2629 if (exception_in_progress(common) || signal_pending(current)) { in fsg_main_thread()
2630 handle_exception(common); in fsg_main_thread()
2634 if (!common->running) { in fsg_main_thread()
2635 sleep_thread(common); in fsg_main_thread()
2639 if (get_next_command(common)) in fsg_main_thread()
2642 spin_lock_irq(&common->lock); in fsg_main_thread()
2643 if (!exception_in_progress(common)) in fsg_main_thread()
2644 common->state = FSG_STATE_DATA_PHASE; in fsg_main_thread()
2645 spin_unlock_irq(&common->lock); in fsg_main_thread()
2647 if (do_scsi_command(common) || finish_reply(common)) in fsg_main_thread()
2650 spin_lock_irq(&common->lock); in fsg_main_thread()
2651 if (!exception_in_progress(common)) in fsg_main_thread()
2652 common->state = FSG_STATE_STATUS_PHASE; in fsg_main_thread()
2653 spin_unlock_irq(&common->lock); in fsg_main_thread()
2655 if (send_status(common)) in fsg_main_thread()
2658 spin_lock_irq(&common->lock); in fsg_main_thread()
2659 if (!exception_in_progress(common)) in fsg_main_thread()
2660 common->state = FSG_STATE_IDLE; in fsg_main_thread()
2661 spin_unlock_irq(&common->lock); in fsg_main_thread()
2664 spin_lock_irq(&common->lock); in fsg_main_thread()
2665 common->thread_task = NULL; in fsg_main_thread()
2666 spin_unlock_irq(&common->lock); in fsg_main_thread()
2668 if (!common->ops || !common->ops->thread_exits in fsg_main_thread()
2669 || common->ops->thread_exits(common) < 0) { in fsg_main_thread()
2670 struct fsg_lun *curlun = common->luns; in fsg_main_thread()
2671 unsigned i = common->nluns; in fsg_main_thread()
2673 down_write(&common->filesem); in fsg_main_thread()
2681 up_write(&common->filesem); in fsg_main_thread()
2685 complete_and_exit(&common->thread_notifier, 0); in fsg_main_thread()
2697 /****************************** FSG COMMON ******************************/
2706 static inline void fsg_common_get(struct fsg_common *common) in fsg_common_get() argument
2708 kref_get(&common->ref); in fsg_common_get()
2711 static inline void fsg_common_put(struct fsg_common *common) in fsg_common_put() argument
2713 kref_put(&common->ref, fsg_common_release); in fsg_common_put()
2716 static struct fsg_common *fsg_common_init(struct fsg_common *common, in fsg_common_init() argument
2739 if (!common) { in fsg_common_init()
2740 common = kzalloc(sizeof *common, GFP_KERNEL); in fsg_common_init()
2741 if (!common) in fsg_common_init()
2743 common->free_storage_on_release = 1; in fsg_common_init()
2745 memset(common, 0, sizeof *common); in fsg_common_init()
2746 common->free_storage_on_release = 0; in fsg_common_init()
2749 common->buffhds = kcalloc(fsg_num_buffers, in fsg_common_init()
2750 sizeof *(common->buffhds), GFP_KERNEL); in fsg_common_init()
2751 if (!common->buffhds) { in fsg_common_init()
2752 if (common->free_storage_on_release) in fsg_common_init()
2753 kfree(common); in fsg_common_init()
2757 common->ops = cfg->ops; in fsg_common_init()
2758 common->private_data = cfg->private_data; in fsg_common_init()
2760 common->gadget = gadget; in fsg_common_init()
2761 common->ep0 = gadget->ep0; in fsg_common_init()
2762 common->ep0req = cdev->req; in fsg_common_init()
2763 common->cdev = cdev; in fsg_common_init()
2783 common->luns = curlun; in fsg_common_init()
2785 init_rwsem(&common->filesem); in fsg_common_init()
2795 dev_set_drvdata(&curlun->dev, &common->filesem); in fsg_common_init()
2804 INFO(common, "failed to register LUN%d: %d\n", i, rc); in fsg_common_init()
2805 common->nluns = i; in fsg_common_init()
2825 ERROR(common, "no file given for LUN%d\n", i); in fsg_common_init()
2830 common->nluns = nluns; in fsg_common_init()
2833 bh = common->buffhds; in fsg_common_init()
2846 bh->next = common->buffhds; in fsg_common_init()
2856 WARNING(common, "controller '%s' not recognized\n", in fsg_common_init()
2861 snprintf(common->inquiry_string, sizeof common->inquiry_string, in fsg_common_init()
2864 cfg->product_name ?: (common->luns->cdrom in fsg_common_init()
2874 common->can_stall = cfg->can_stall && in fsg_common_init()
2875 !(gadget_is_at91(common->gadget)); in fsg_common_init()
2877 spin_lock_init(&common->lock); in fsg_common_init()
2878 kref_init(&common->ref); in fsg_common_init()
2881 common->thread_task = in fsg_common_init()
2882 kthread_create(fsg_main_thread, common, in fsg_common_init()
2884 if (IS_ERR(common->thread_task)) { in fsg_common_init()
2885 rc = PTR_ERR(common->thread_task); in fsg_common_init()
2888 init_completion(&common->thread_notifier); in fsg_common_init()
2889 init_waitqueue_head(&common->fsg_wait); in fsg_common_init()
2892 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n"); in fsg_common_init()
2893 INFO(common, "Number of LUNs=%d\n", common->nluns); in fsg_common_init()
2896 for (i = 0, nluns = common->nluns, curlun = common->luns; in fsg_common_init()
2917 DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task)); in fsg_common_init()
2919 wake_up_process(common->thread_task); in fsg_common_init()
2921 return common; in fsg_common_init()
2924 common->nluns = i + 1; in fsg_common_init()
2926 common->state = FSG_STATE_TERMINATED; /* The thread is dead */ in fsg_common_init()
2928 fsg_common_release(&common->ref); in fsg_common_init()
2934 struct fsg_common *common = container_of(ref, struct fsg_common, ref); in fsg_common_release() local
2937 if (common->state != FSG_STATE_TERMINATED) { in fsg_common_release()
2938 raise_exception(common, FSG_STATE_EXIT); in fsg_common_release()
2939 wait_for_completion(&common->thread_notifier); in fsg_common_release()
2942 if (likely(common->luns)) { in fsg_common_release()
2943 struct fsg_lun *lun = common->luns; in fsg_common_release()
2944 unsigned i = common->nluns; in fsg_common_release()
2946 /* In error recovery common->nluns may be zero. */ in fsg_common_release()
2955 kfree(common->luns); in fsg_common_release()
2959 struct fsg_buffhd *bh = common->buffhds; in fsg_common_release()
2966 kfree(common->buffhds); in fsg_common_release()
2967 if (common->free_storage_on_release) in fsg_common_release()
2968 kfree(common); in fsg_common_release()
2977 struct fsg_common *common = fsg->common; in fsg_unbind() local
2980 if (fsg->common->fsg == fsg) { in fsg_unbind()
2981 fsg->common->new_fsg = NULL; in fsg_unbind()
2982 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_unbind()
2984 wait_event(common->fsg_wait, common->fsg != fsg); in fsg_unbind()
2987 fsg_common_put(common); in fsg_unbind()
3014 ep->driver_data = fsg->common; /* claim the endpoint */ in fsg_bind()
3020 ep->driver_data = fsg->common; /* claim the endpoint */ in fsg_bind()
3080 struct fsg_common *common) in fsg_bind_config() argument
3097 fsg->common = common; in fsg_bind_config()
3099 * Our caller holds a reference to common structure so we in fsg_bind_config()
3110 fsg_common_get(fsg->common); in fsg_bind_config()
3116 struct fsg_common *common) in fsg_add() argument
3118 return fsg_bind_config(cdev, c, common); in fsg_add()
3201 fsg_common_from_params(struct fsg_common *common,
3206 fsg_common_from_params(struct fsg_common *common, in fsg_common_from_params() argument
3212 return fsg_common_init(common, cdev, &cfg); in fsg_common_from_params()