Lines Matching refs:common
321 struct fsg_common *common; member
335 static inline int __fsg_is_set(struct fsg_common *common, in __fsg_is_set() argument
338 if (common->fsg) in __fsg_is_set()
340 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line); in __fsg_is_set()
345 #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__)) argument
354 static int exception_in_progress(struct fsg_common *common) in exception_in_progress() argument
356 return common->state > FSG_STATE_NORMAL; in exception_in_progress()
360 static void set_bulk_out_req_length(struct fsg_common *common, in set_bulk_out_req_length() argument
366 rem = length % common->bulk_out_maxpacket; in set_bulk_out_req_length()
368 length += common->bulk_out_maxpacket - rem; in set_bulk_out_req_length()
394 static void raise_exception(struct fsg_common *common, enum fsg_state new_state) in raise_exception() argument
403 spin_lock_irqsave(&common->lock, flags); in raise_exception()
404 if (common->state <= new_state) { in raise_exception()
405 common->exception_req_tag = common->ep0_req_tag; in raise_exception()
406 common->state = new_state; in raise_exception()
407 if (common->thread_task) in raise_exception()
409 common->thread_task); in raise_exception()
411 spin_unlock_irqrestore(&common->lock, flags); in raise_exception()
417 static int ep0_queue(struct fsg_common *common) in ep0_queue() argument
421 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC); in ep0_queue()
422 common->ep0->driver_data = common; in ep0_queue()
425 WARNING(common, "error in submission: %s --> %d\n", in ep0_queue()
426 common->ep0->name, rc); in ep0_queue()
438 struct fsg_common *common = ep->driver_data; in bulk_in_complete() local
442 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_in_complete()
449 wake_up(&common->io_wait); in bulk_in_complete()
454 struct fsg_common *common = ep->driver_data; in bulk_out_complete() local
457 dump_msg(common, "bulk-out", req->buf, req->actual); in bulk_out_complete()
459 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_out_complete()
466 wake_up(&common->io_wait); in bulk_out_complete()
469 static int _fsg_common_get_max_lun(struct fsg_common *common) in _fsg_common_get_max_lun() argument
471 int i = ARRAY_SIZE(common->luns) - 1; in _fsg_common_get_max_lun()
473 while (i >= 0 && !common->luns[i]) in _fsg_common_get_max_lun()
483 struct usb_request *req = fsg->common->ep0req; in fsg_setup()
488 if (!fsg_is_set(fsg->common)) in fsg_setup()
491 ++fsg->common->ep0_req_tag; /* Record arrival of a new request */ in fsg_setup()
511 raise_exception(fsg->common, FSG_STATE_PROTOCOL_RESET); in fsg_setup()
522 *(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common); in fsg_setup()
526 return ep0_queue(fsg->common); in fsg_setup()
568 static bool start_in_transfer(struct fsg_common *common, struct fsg_buffhd *bh) in start_in_transfer() argument
570 if (!fsg_is_set(common)) in start_in_transfer()
573 if (start_transfer(common->fsg, common->fsg->bulk_in, bh->inreq)) in start_in_transfer()
578 static bool start_out_transfer(struct fsg_common *common, struct fsg_buffhd *bh) in start_out_transfer() argument
580 if (!fsg_is_set(common)) in start_out_transfer()
583 if (start_transfer(common->fsg, common->fsg->bulk_out, bh->outreq)) in start_out_transfer()
588 static int sleep_thread(struct fsg_common *common, bool can_freeze, in sleep_thread() argument
599 rc = wait_event_freezable(common->io_wait, in sleep_thread()
603 rc = wait_event_interruptible(common->io_wait, in sleep_thread()
612 static int do_read(struct fsg_common *common) in do_read() argument
614 struct fsg_lun *curlun = common->curlun; in do_read()
627 if (common->cmnd[0] == READ_6) in do_read()
628 lba = get_unaligned_be24(&common->cmnd[1]); in do_read()
630 lba = get_unaligned_be32(&common->cmnd[2]); in do_read()
637 if ((common->cmnd[1] & ~0x18) != 0) { in do_read()
649 amount_left = common->data_size_from_cmnd; in do_read()
665 bh = common->next_buffhd_to_fill; in do_read()
666 rc = sleep_thread(common, false, bh); in do_read()
704 common->residue -= nread; in do_read()
728 if (!start_in_transfer(common, bh)) in do_read()
731 common->next_buffhd_to_fill = bh->next; in do_read()
740 static int do_write(struct fsg_common *common) in do_write() argument
742 struct fsg_lun *curlun = common->curlun; in do_write()
764 if (common->cmnd[0] == WRITE_6) in do_write()
765 lba = get_unaligned_be24(&common->cmnd[1]); in do_write()
767 lba = get_unaligned_be32(&common->cmnd[2]); in do_write()
775 if (common->cmnd[1] & ~0x18) { in do_write()
779 if (!curlun->nofua && (common->cmnd[1] & 0x08)) { /* FUA */ in do_write()
793 amount_left_to_req = common->data_size_from_cmnd; in do_write()
794 amount_left_to_write = common->data_size_from_cmnd; in do_write()
799 bh = common->next_buffhd_to_fill; in do_write()
822 common->usb_amount_left -= amount; in do_write()
832 set_bulk_out_req_length(common, bh, amount); in do_write()
833 if (!start_out_transfer(common, bh)) in do_write()
836 common->next_buffhd_to_fill = bh->next; in do_write()
841 bh = common->next_buffhd_to_drain; in do_write()
846 rc = sleep_thread(common, false, bh); in do_write()
850 common->next_buffhd_to_drain = bh->next; in do_write()
901 common->residue -= nwritten; in do_write()
915 common->short_packet_received = 1; in do_write()
926 static int do_synchronize_cache(struct fsg_common *common) in do_synchronize_cache() argument
928 struct fsg_lun *curlun = common->curlun; in do_synchronize_cache()
952 static int do_verify(struct fsg_common *common) in do_verify() argument
954 struct fsg_lun *curlun = common->curlun; in do_verify()
957 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in do_verify()
967 lba = get_unaligned_be32(&common->cmnd[2]); in do_verify()
977 if (common->cmnd[1] & ~0x10) { in do_verify()
982 verification_length = get_unaligned_be16(&common->cmnd[7]); in do_verify()
1053 static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh) in do_inquiry() argument
1055 struct fsg_lun *curlun = common->curlun; in do_inquiry()
1059 common->bad_lun_okay = 1; in do_inquiry()
1078 memcpy(buf + 8, common->inquiry_string, in do_inquiry()
1079 sizeof(common->inquiry_string)); in do_inquiry()
1083 static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_request_sense() argument
1085 struct fsg_lun *curlun = common->curlun; in do_request_sense()
1113 common->bad_lun_okay = 1; in do_request_sense()
1136 static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_capacity() argument
1138 struct fsg_lun *curlun = common->curlun; in do_read_capacity()
1139 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_capacity()
1140 int pmi = common->cmnd[8]; in do_read_capacity()
1155 static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_header() argument
1157 struct fsg_lun *curlun = common->curlun; in do_read_header()
1158 int msf = common->cmnd[1] & 0x02; in do_read_header()
1159 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_header()
1162 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */ in do_read_header()
1177 static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_toc() argument
1179 struct fsg_lun *curlun = common->curlun; in do_read_toc()
1180 int msf = common->cmnd[1] & 0x02; in do_read_toc()
1181 int start_track = common->cmnd[6]; in do_read_toc()
1184 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ in do_read_toc()
1204 static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_sense() argument
1206 struct fsg_lun *curlun = common->curlun; in do_mode_sense()
1207 int mscmnd = common->cmnd[0]; in do_mode_sense()
1215 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */ in do_mode_sense()
1219 pc = common->cmnd[2] >> 6; in do_mode_sense()
1220 page_code = common->cmnd[2] & 0x3f; in do_mode_sense()
1290 static int do_start_stop(struct fsg_common *common) in do_start_stop() argument
1292 struct fsg_lun *curlun = common->curlun; in do_start_stop()
1300 } else if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */ in do_start_stop()
1301 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */ in do_start_stop()
1306 loej = common->cmnd[4] & 0x02; in do_start_stop()
1307 start = common->cmnd[4] & 0x01; in do_start_stop()
1331 up_read(&common->filesem); in do_start_stop()
1332 down_write(&common->filesem); in do_start_stop()
1334 up_write(&common->filesem); in do_start_stop()
1335 down_read(&common->filesem); in do_start_stop()
1340 static int do_prevent_allow(struct fsg_common *common) in do_prevent_allow() argument
1342 struct fsg_lun *curlun = common->curlun; in do_prevent_allow()
1345 if (!common->curlun) { in do_prevent_allow()
1347 } else if (!common->curlun->removable) { in do_prevent_allow()
1348 common->curlun->sense_data = SS_INVALID_COMMAND; in do_prevent_allow()
1352 prevent = common->cmnd[4] & 0x01; in do_prevent_allow()
1353 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */ in do_prevent_allow()
1364 static int do_read_format_capacities(struct fsg_common *common, in do_read_format_capacities() argument
1367 struct fsg_lun *curlun = common->curlun; in do_read_format_capacities()
1381 static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_select() argument
1383 struct fsg_lun *curlun = common->curlun; in do_mode_select()
1439 static int throw_away_data(struct fsg_common *common) in throw_away_data() argument
1445 for (bh = common->next_buffhd_to_drain; in throw_away_data()
1446 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; in throw_away_data()
1447 bh = common->next_buffhd_to_drain) { in throw_away_data()
1450 bh2 = common->next_buffhd_to_fill; in throw_away_data()
1452 common->usb_amount_left > 0) { in throw_away_data()
1453 amount = min(common->usb_amount_left, FSG_BUFLEN); in throw_away_data()
1460 set_bulk_out_req_length(common, bh2, amount); in throw_away_data()
1461 if (!start_out_transfer(common, bh2)) in throw_away_data()
1464 common->next_buffhd_to_fill = bh2->next; in throw_away_data()
1465 common->usb_amount_left -= amount; in throw_away_data()
1470 rc = sleep_thread(common, false, bh); in throw_away_data()
1476 common->next_buffhd_to_drain = bh->next; in throw_away_data()
1481 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in throw_away_data()
1488 static int finish_reply(struct fsg_common *common) in finish_reply() argument
1490 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in finish_reply()
1493 switch (common->data_dir) { in finish_reply()
1504 if (!common->can_stall) { in finish_reply()
1506 } else if (fsg_is_set(common)) { in finish_reply()
1507 fsg_set_halt(common->fsg, common->fsg->bulk_out); in finish_reply()
1508 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1517 if (common->data_size == 0) { in finish_reply()
1521 } else if (!fsg_is_set(common)) { in finish_reply()
1525 } else if (common->residue == 0) { in finish_reply()
1527 if (!start_in_transfer(common, bh)) in finish_reply()
1529 common->next_buffhd_to_fill = bh->next; in finish_reply()
1540 if (!start_in_transfer(common, bh)) in finish_reply()
1542 common->next_buffhd_to_fill = bh->next; in finish_reply()
1543 if (common->can_stall) in finish_reply()
1544 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1553 if (common->residue == 0) { in finish_reply()
1557 } else if (common->short_packet_received) { in finish_reply()
1558 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1570 } else if (common->can_stall) { in finish_reply()
1571 if (fsg_is_set(common)) in finish_reply()
1572 fsg_set_halt(common->fsg, in finish_reply()
1573 common->fsg->bulk_out); in finish_reply()
1574 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1583 rc = throw_away_data(common); in finish_reply()
1590 static void send_status(struct fsg_common *common) in send_status() argument
1592 struct fsg_lun *curlun = common->curlun; in send_status()
1600 bh = common->next_buffhd_to_fill; in send_status()
1601 rc = sleep_thread(common, false, bh); in send_status()
1608 } else if (common->bad_lun_okay) in send_status()
1613 if (common->phase_error) { in send_status()
1614 DBG(common, "sending phase-error status\n"); in send_status()
1618 DBG(common, "sending command-failure status\n"); in send_status()
1620 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;" in send_status()
1629 csw->Tag = common->tag; in send_status()
1630 csw->Residue = cpu_to_le32(common->residue); in send_status()
1635 if (!start_in_transfer(common, bh)) in send_status()
1639 common->next_buffhd_to_fill = bh->next; in send_status()
1650 static int check_command(struct fsg_common *common, int cmnd_size, in check_command() argument
1655 unsigned int lun = common->cmnd[1] >> 5; in check_command()
1661 if (common->data_dir != DATA_DIR_UNKNOWN) in check_command()
1662 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir], in check_command()
1663 common->data_size); in check_command()
1664 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", in check_command()
1666 common->data_size_from_cmnd, common->cmnd_size, hdlen); in check_command()
1672 if (common->data_size_from_cmnd == 0) in check_command()
1674 if (common->data_size < common->data_size_from_cmnd) { in check_command()
1680 common->data_size_from_cmnd = common->data_size; in check_command()
1681 common->phase_error = 1; in check_command()
1683 common->residue = common->data_size; in check_command()
1684 common->usb_amount_left = common->data_size; in check_command()
1687 if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) { in check_command()
1688 common->phase_error = 1; in check_command()
1693 if (cmnd_size != common->cmnd_size) { in check_command()
1708 if (cmnd_size <= common->cmnd_size) { in check_command()
1709 DBG(common, "%s is buggy! Expected length %d " in check_command()
1711 cmnd_size, common->cmnd_size); in check_command()
1712 cmnd_size = common->cmnd_size; in check_command()
1714 common->phase_error = 1; in check_command()
1720 if (common->lun != lun) in check_command()
1721 DBG(common, "using LUN %u from CBW, not LUN %u from CDB\n", in check_command()
1722 common->lun, lun); in check_command()
1725 curlun = common->curlun; in check_command()
1727 if (common->cmnd[0] != REQUEST_SENSE) { in check_command()
1733 common->bad_lun_okay = 0; in check_command()
1739 if (common->cmnd[0] != INQUIRY && in check_command()
1740 common->cmnd[0] != REQUEST_SENSE) { in check_command()
1741 DBG(common, "unsupported LUN %u\n", common->lun); in check_command()
1751 common->cmnd[0] != INQUIRY && in check_command()
1752 common->cmnd[0] != REQUEST_SENSE) { in check_command()
1759 common->cmnd[1] &= 0x1f; /* Mask away the LUN */ in check_command()
1761 if (common->cmnd[i] && !(mask & (1 << i))) { in check_command()
1779 static int check_command_size_in_blocks(struct fsg_common *common, in check_command_size_in_blocks() argument
1783 if (common->curlun) in check_command_size_in_blocks()
1784 common->data_size_from_cmnd <<= common->curlun->blkbits; in check_command_size_in_blocks()
1785 return check_command(common, cmnd_size, data_dir, in check_command_size_in_blocks()
1789 static int do_scsi_command(struct fsg_common *common) in do_scsi_command() argument
1797 dump_cdb(common); in do_scsi_command()
1800 bh = common->next_buffhd_to_fill; in do_scsi_command()
1801 common->next_buffhd_to_drain = bh; in do_scsi_command()
1802 rc = sleep_thread(common, false, bh); in do_scsi_command()
1806 common->phase_error = 0; in do_scsi_command()
1807 common->short_packet_received = 0; in do_scsi_command()
1809 down_read(&common->filesem); /* We're using the backing file */ in do_scsi_command()
1810 switch (common->cmnd[0]) { in do_scsi_command()
1813 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1814 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1818 reply = do_inquiry(common, bh); in do_scsi_command()
1822 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1823 reply = check_command(common, 6, DATA_DIR_FROM_HOST, in do_scsi_command()
1827 reply = do_mode_select(common, bh); in do_scsi_command()
1831 common->data_size_from_cmnd = in do_scsi_command()
1832 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1833 reply = check_command(common, 10, DATA_DIR_FROM_HOST, in do_scsi_command()
1837 reply = do_mode_select(common, bh); in do_scsi_command()
1841 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1842 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1846 reply = do_mode_sense(common, bh); in do_scsi_command()
1850 common->data_size_from_cmnd = in do_scsi_command()
1851 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1852 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1856 reply = do_mode_sense(common, bh); in do_scsi_command()
1860 common->data_size_from_cmnd = 0; in do_scsi_command()
1861 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1865 reply = do_prevent_allow(common); in do_scsi_command()
1869 i = common->cmnd[4]; in do_scsi_command()
1870 common->data_size_from_cmnd = (i == 0) ? 256 : i; in do_scsi_command()
1871 reply = check_command_size_in_blocks(common, 6, in do_scsi_command()
1876 reply = do_read(common); in do_scsi_command()
1880 common->data_size_from_cmnd = in do_scsi_command()
1881 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1882 reply = check_command_size_in_blocks(common, 10, in do_scsi_command()
1887 reply = do_read(common); in do_scsi_command()
1891 common->data_size_from_cmnd = in do_scsi_command()
1892 get_unaligned_be32(&common->cmnd[6]); in do_scsi_command()
1893 reply = check_command_size_in_blocks(common, 12, in do_scsi_command()
1898 reply = do_read(common); in do_scsi_command()
1902 common->data_size_from_cmnd = 8; in do_scsi_command()
1903 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1907 reply = do_read_capacity(common, bh); in do_scsi_command()
1911 if (!common->curlun || !common->curlun->cdrom) in do_scsi_command()
1913 common->data_size_from_cmnd = in do_scsi_command()
1914 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1915 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1919 reply = do_read_header(common, bh); in do_scsi_command()
1923 if (!common->curlun || !common->curlun->cdrom) in do_scsi_command()
1925 common->data_size_from_cmnd = in do_scsi_command()
1926 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1927 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1931 reply = do_read_toc(common, bh); in do_scsi_command()
1935 common->data_size_from_cmnd = in do_scsi_command()
1936 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1937 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1941 reply = do_read_format_capacities(common, bh); in do_scsi_command()
1945 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1946 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1950 reply = do_request_sense(common, bh); in do_scsi_command()
1954 common->data_size_from_cmnd = 0; in do_scsi_command()
1955 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1959 reply = do_start_stop(common); in do_scsi_command()
1963 common->data_size_from_cmnd = 0; in do_scsi_command()
1964 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
1968 reply = do_synchronize_cache(common); in do_scsi_command()
1972 common->data_size_from_cmnd = 0; in do_scsi_command()
1973 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1983 common->data_size_from_cmnd = 0; in do_scsi_command()
1984 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
1988 reply = do_verify(common); in do_scsi_command()
1992 i = common->cmnd[4]; in do_scsi_command()
1993 common->data_size_from_cmnd = (i == 0) ? 256 : i; in do_scsi_command()
1994 reply = check_command_size_in_blocks(common, 6, in do_scsi_command()
1999 reply = do_write(common); in do_scsi_command()
2003 common->data_size_from_cmnd = in do_scsi_command()
2004 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2005 reply = check_command_size_in_blocks(common, 10, in do_scsi_command()
2010 reply = do_write(common); in do_scsi_command()
2014 common->data_size_from_cmnd = in do_scsi_command()
2015 get_unaligned_be32(&common->cmnd[6]); in do_scsi_command()
2016 reply = check_command_size_in_blocks(common, 12, in do_scsi_command()
2021 reply = do_write(common); in do_scsi_command()
2038 common->data_size_from_cmnd = 0; in do_scsi_command()
2039 sprintf(unknown, "Unknown x%02x", common->cmnd[0]); in do_scsi_command()
2040 reply = check_command(common, common->cmnd_size, in do_scsi_command()
2043 common->curlun->sense_data = SS_INVALID_COMMAND; in do_scsi_command()
2048 up_read(&common->filesem); in do_scsi_command()
2056 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) { in do_scsi_command()
2057 reply = min((u32)reply, common->data_size_from_cmnd); in do_scsi_command()
2060 common->residue -= reply; in do_scsi_command()
2073 struct fsg_common *common = fsg->common; in received_cbw() local
2104 if (cbw->Lun >= ARRAY_SIZE(common->luns) || in received_cbw()
2115 if (common->can_stall) { in received_cbw()
2123 common->cmnd_size = cbw->Length; in received_cbw()
2124 memcpy(common->cmnd, cbw->CDB, common->cmnd_size); in received_cbw()
2126 common->data_dir = DATA_DIR_TO_HOST; in received_cbw()
2128 common->data_dir = DATA_DIR_FROM_HOST; in received_cbw()
2129 common->data_size = le32_to_cpu(cbw->DataTransferLength); in received_cbw()
2130 if (common->data_size == 0) in received_cbw()
2131 common->data_dir = DATA_DIR_NONE; in received_cbw()
2132 common->lun = cbw->Lun; in received_cbw()
2133 if (common->lun < ARRAY_SIZE(common->luns)) in received_cbw()
2134 common->curlun = common->luns[common->lun]; in received_cbw()
2136 common->curlun = NULL; in received_cbw()
2137 common->tag = cbw->Tag; in received_cbw()
2141 static int get_next_command(struct fsg_common *common) in get_next_command() argument
2147 bh = common->next_buffhd_to_fill; in get_next_command()
2148 rc = sleep_thread(common, true, bh); in get_next_command()
2153 set_bulk_out_req_length(common, bh, US_BULK_CB_WRAP_LEN); in get_next_command()
2154 if (!start_out_transfer(common, bh)) in get_next_command()
2165 rc = sleep_thread(common, true, bh); in get_next_command()
2169 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; in get_next_command()
2178 static int alloc_request(struct fsg_common *common, struct usb_ep *ep, in alloc_request() argument
2184 ERROR(common, "can't allocate request for %s\n", ep->name); in alloc_request()
2189 static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg) in do_set_interface() argument
2194 if (common->running) in do_set_interface()
2195 DBG(common, "reset interface\n"); in do_set_interface()
2199 if (common->fsg) { in do_set_interface()
2200 fsg = common->fsg; in do_set_interface()
2202 for (i = 0; i < common->fsg_num_buffers; ++i) { in do_set_interface()
2203 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2225 common->fsg = NULL; in do_set_interface()
2226 wake_up(&common->fsg_wait); in do_set_interface()
2229 common->running = 0; in do_set_interface()
2233 common->fsg = new_fsg; in do_set_interface()
2234 fsg = common->fsg; in do_set_interface()
2237 rc = config_ep_by_speed(common->gadget, &(fsg->function), fsg->bulk_in); in do_set_interface()
2243 fsg->bulk_in->driver_data = common; in do_set_interface()
2246 rc = config_ep_by_speed(common->gadget, &(fsg->function), in do_set_interface()
2253 fsg->bulk_out->driver_data = common; in do_set_interface()
2255 common->bulk_out_maxpacket = usb_endpoint_maxp(fsg->bulk_out->desc); in do_set_interface()
2259 for (i = 0; i < common->fsg_num_buffers; ++i) { in do_set_interface()
2260 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2262 rc = alloc_request(common, fsg->bulk_in, &bh->inreq); in do_set_interface()
2265 rc = alloc_request(common, fsg->bulk_out, &bh->outreq); in do_set_interface()
2274 common->running = 1; in do_set_interface()
2275 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) in do_set_interface()
2276 if (common->luns[i]) in do_set_interface()
2277 common->luns[i]->unit_attention_data = in do_set_interface()
2288 fsg->common->new_fsg = fsg; in fsg_set_alt()
2289 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_set_alt()
2296 fsg->common->new_fsg = NULL; in fsg_disable()
2297 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_disable()
2303 static void handle_exception(struct fsg_common *common) in handle_exception() argument
2320 spin_lock_irq(&common->lock); in handle_exception()
2321 if (common->state < FSG_STATE_EXIT) in handle_exception()
2322 DBG(common, "Main thread exiting on signal\n"); in handle_exception()
2323 common->state = FSG_STATE_EXIT; in handle_exception()
2324 spin_unlock_irq(&common->lock); in handle_exception()
2329 if (likely(common->fsg)) { in handle_exception()
2330 for (i = 0; i < common->fsg_num_buffers; ++i) { in handle_exception()
2331 bh = &common->buffhds[i]; in handle_exception()
2333 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq); in handle_exception()
2335 usb_ep_dequeue(common->fsg->bulk_out, in handle_exception()
2339 if (sleep_thread(common, false, bh)) in handle_exception()
2344 if (common->fsg->bulk_in_enabled) in handle_exception()
2345 usb_ep_fifo_flush(common->fsg->bulk_in); in handle_exception()
2346 if (common->fsg->bulk_out_enabled) in handle_exception()
2347 usb_ep_fifo_flush(common->fsg->bulk_out); in handle_exception()
2354 spin_lock_irq(&common->lock); in handle_exception()
2356 for (i = 0; i < common->fsg_num_buffers; ++i) { in handle_exception()
2357 bh = &common->buffhds[i]; in handle_exception()
2360 common->next_buffhd_to_fill = &common->buffhds[0]; in handle_exception()
2361 common->next_buffhd_to_drain = &common->buffhds[0]; in handle_exception()
2362 exception_req_tag = common->exception_req_tag; in handle_exception()
2363 old_state = common->state; in handle_exception()
2364 common->state = FSG_STATE_NORMAL; in handle_exception()
2367 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) { in handle_exception()
2368 curlun = common->luns[i]; in handle_exception()
2378 spin_unlock_irq(&common->lock); in handle_exception()
2386 send_status(common); in handle_exception()
2395 if (!fsg_is_set(common)) in handle_exception()
2398 &common->fsg->atomic_bitflags)) in handle_exception()
2399 usb_ep_clear_halt(common->fsg->bulk_in); in handle_exception()
2401 if (common->ep0_req_tag == exception_req_tag) in handle_exception()
2402 ep0_queue(common); /* Complete the status stage */ in handle_exception()
2416 do_set_interface(common, common->new_fsg); in handle_exception()
2417 if (common->new_fsg) in handle_exception()
2418 usb_composite_setup_continue(common->cdev); in handle_exception()
2422 do_set_interface(common, NULL); /* Free resources */ in handle_exception()
2423 spin_lock_irq(&common->lock); in handle_exception()
2424 common->state = FSG_STATE_TERMINATED; /* Stop the thread */ in handle_exception()
2425 spin_unlock_irq(&common->lock); in handle_exception()
2438 struct fsg_common *common = common_; in fsg_main_thread() local
2454 while (common->state != FSG_STATE_TERMINATED) { in fsg_main_thread()
2455 if (exception_in_progress(common) || signal_pending(current)) { in fsg_main_thread()
2456 handle_exception(common); in fsg_main_thread()
2460 if (!common->running) { in fsg_main_thread()
2461 sleep_thread(common, true, NULL); in fsg_main_thread()
2465 if (get_next_command(common) || exception_in_progress(common)) in fsg_main_thread()
2467 if (do_scsi_command(common) || exception_in_progress(common)) in fsg_main_thread()
2469 if (finish_reply(common) || exception_in_progress(common)) in fsg_main_thread()
2471 send_status(common); in fsg_main_thread()
2474 spin_lock_irq(&common->lock); in fsg_main_thread()
2475 common->thread_task = NULL; in fsg_main_thread()
2476 spin_unlock_irq(&common->lock); in fsg_main_thread()
2480 down_write(&common->filesem); in fsg_main_thread()
2481 for (i = 0; i < ARRAY_SIZE(common->luns); i++) { in fsg_main_thread()
2482 struct fsg_lun *curlun = common->luns[i]; in fsg_main_thread()
2487 up_write(&common->filesem); in fsg_main_thread()
2490 complete_and_exit(&common->thread_notifier, 0); in fsg_main_thread()
2558 static struct fsg_common *fsg_common_setup(struct fsg_common *common) in fsg_common_setup() argument
2560 if (!common) { in fsg_common_setup()
2561 common = kzalloc(sizeof(*common), GFP_KERNEL); in fsg_common_setup()
2562 if (!common) in fsg_common_setup()
2564 common->free_storage_on_release = 1; in fsg_common_setup()
2566 common->free_storage_on_release = 0; in fsg_common_setup()
2568 init_rwsem(&common->filesem); in fsg_common_setup()
2569 spin_lock_init(&common->lock); in fsg_common_setup()
2570 init_completion(&common->thread_notifier); in fsg_common_setup()
2571 init_waitqueue_head(&common->io_wait); in fsg_common_setup()
2572 init_waitqueue_head(&common->fsg_wait); in fsg_common_setup()
2573 common->state = FSG_STATE_TERMINATED; in fsg_common_setup()
2574 memset(common->luns, 0, sizeof(common->luns)); in fsg_common_setup()
2576 return common; in fsg_common_setup()
2579 void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs) in fsg_common_set_sysfs() argument
2581 common->sysfs = sysfs; in fsg_common_set_sysfs()
2597 int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n) in fsg_common_set_num_buffers() argument
2620 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_set_num_buffers()
2621 common->fsg_num_buffers = n; in fsg_common_set_num_buffers()
2622 common->buffhds = buffhds; in fsg_common_set_num_buffers()
2646 static void _fsg_common_remove_luns(struct fsg_common *common, int n) in _fsg_common_remove_luns() argument
2651 if (common->luns[i]) { in _fsg_common_remove_luns()
2652 fsg_common_remove_lun(common->luns[i]); in _fsg_common_remove_luns()
2653 common->luns[i] = NULL; in _fsg_common_remove_luns()
2657 void fsg_common_remove_luns(struct fsg_common *common) in fsg_common_remove_luns() argument
2659 _fsg_common_remove_luns(common, ARRAY_SIZE(common->luns)); in fsg_common_remove_luns()
2663 void fsg_common_free_buffers(struct fsg_common *common) in fsg_common_free_buffers() argument
2665 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_free_buffers()
2666 common->buffhds = NULL; in fsg_common_free_buffers()
2670 int fsg_common_set_cdev(struct fsg_common *common, in fsg_common_set_cdev() argument
2675 common->gadget = cdev->gadget; in fsg_common_set_cdev()
2676 common->ep0 = cdev->gadget->ep0; in fsg_common_set_cdev()
2677 common->ep0req = cdev->req; in fsg_common_set_cdev()
2678 common->cdev = cdev; in fsg_common_set_cdev()
2692 common->can_stall = can_stall && in fsg_common_set_cdev()
2693 gadget_is_stall_supported(common->gadget); in fsg_common_set_cdev()
2729 int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg, in fsg_common_create_lun() argument
2737 if (id >= ARRAY_SIZE(common->luns)) in fsg_common_create_lun()
2740 if (common->luns[id]) in fsg_common_create_lun()
2759 if (!common->sysfs) { in fsg_common_create_lun()
2764 lun->dev.parent = &common->gadget->dev; in fsg_common_create_lun()
2766 dev_set_drvdata(&lun->dev, &common->filesem); in fsg_common_create_lun()
2778 common->luns[id] = lun; in fsg_common_create_lun()
2809 common->luns[id] = NULL; in fsg_common_create_lun()
2816 int fsg_common_create_luns(struct fsg_common *common, struct fsg_config *cfg) in fsg_common_create_luns() argument
2821 fsg_common_remove_luns(common); in fsg_common_create_luns()
2825 rc = fsg_common_create_lun(common, &cfg->luns[i], i, buf, NULL); in fsg_common_create_luns()
2835 _fsg_common_remove_luns(common, i); in fsg_common_create_luns()
2840 void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn, in fsg_common_set_inquiry_string() argument
2847 snprintf(common->inquiry_string, sizeof(common->inquiry_string), in fsg_common_set_inquiry_string()
2850 pn ?: ((*common->luns)->cdrom in fsg_common_set_inquiry_string()
2857 static void fsg_common_release(struct fsg_common *common) in fsg_common_release() argument
2862 if (common->state != FSG_STATE_TERMINATED) { in fsg_common_release()
2863 raise_exception(common, FSG_STATE_EXIT); in fsg_common_release()
2864 wait_for_completion(&common->thread_notifier); in fsg_common_release()
2867 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) { in fsg_common_release()
2868 struct fsg_lun *lun = common->luns[i]; in fsg_common_release()
2877 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_release()
2878 if (common->free_storage_on_release) in fsg_common_release()
2879 kfree(common); in fsg_common_release()
2888 struct fsg_common *common = fsg->common; in fsg_bind() local
2897 ret = _fsg_common_get_max_lun(common); in fsg_bind()
2905 ret = fsg_common_set_cdev(fsg->common, c->cdev, in fsg_bind()
2906 fsg->common->can_stall); in fsg_bind()
2909 fsg_common_set_inquiry_string(fsg->common, NULL, NULL); in fsg_bind()
2912 if (!common->thread_task) { in fsg_bind()
2913 common->state = FSG_STATE_NORMAL; in fsg_bind()
2914 common->thread_task = in fsg_bind()
2915 kthread_create(fsg_main_thread, common, "file-storage"); in fsg_bind()
2916 if (IS_ERR(common->thread_task)) { in fsg_bind()
2917 ret = PTR_ERR(common->thread_task); in fsg_bind()
2918 common->thread_task = NULL; in fsg_bind()
2919 common->state = FSG_STATE_TERMINATED; in fsg_bind()
2922 DBG(common, "I/O thread pid: %d\n", in fsg_bind()
2923 task_pid_nr(common->thread_task)); in fsg_bind()
2924 wake_up_process(common->thread_task); in fsg_bind()
2976 if (fsg->common->state != FSG_STATE_TERMINATED) { in fsg_bind()
2977 raise_exception(fsg->common, FSG_STATE_EXIT); in fsg_bind()
2978 wait_for_completion(&fsg->common->thread_notifier); in fsg_bind()
2988 struct fsg_common *common = fsg->common; in fsg_unbind() local
2991 if (fsg->common->fsg == fsg) { in fsg_unbind()
2992 fsg->common->new_fsg = NULL; in fsg_unbind()
2993 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); in fsg_unbind()
2995 wait_event(common->fsg_wait, common->fsg != fsg); in fsg_unbind()
3029 return fsg_show_file(opts->lun, &fsg_opts->common->filesem, page); in fsg_lun_opts_file_show()
3038 return fsg_store_file(opts->lun, &fsg_opts->common->filesem, page, len); in fsg_lun_opts_file_store()
3054 return fsg_store_ro(opts->lun, &fsg_opts->common->filesem, page, len); in fsg_lun_opts_ro_store()
3084 return fsg_store_cdrom(opts->lun, &fsg_opts->common->filesem, page, in fsg_lun_opts_cdrom_store()
3160 if (fsg_opts->refcnt || fsg_opts->common->luns[num]) { in fsg_lun_make()
3174 ret = fsg_common_create_lun(fsg_opts->common, &config, num, name, in fsg_lun_make()
3180 opts->lun = fsg_opts->common->luns[num]; in fsg_lun_make()
3209 fsg_opts->common->luns[lun_opts->lun_id] = NULL; in fsg_lun_drop()
3233 result = sprintf(page, "%d", opts->common->can_stall); in fsg_opts_stall_show()
3255 opts->common->can_stall = stall; in fsg_opts_stall_store()
3273 result = sprintf(page, "%d", opts->common->fsg_num_buffers); in fsg_opts_num_buffers_show()
3295 ret = fsg_common_set_num_buffers(opts->common, num); in fsg_opts_num_buffers_store()
3333 fsg_common_release(opts->common); in fsg_free_inst()
3348 opts->common = fsg_common_setup(opts->common); in fsg_alloc_inst()
3349 if (IS_ERR(opts->common)) { in fsg_alloc_inst()
3350 rc = PTR_ERR(opts->common); in fsg_alloc_inst()
3354 rc = fsg_common_set_num_buffers(opts->common, in fsg_alloc_inst()
3363 rc = fsg_common_create_lun(opts->common, &config, 0, "lun.0", in fsg_alloc_inst()
3368 opts->lun0.lun = opts->common->luns[0]; in fsg_alloc_inst()
3379 fsg_common_free_buffers(opts->common); in fsg_alloc_inst()
3381 kfree(opts->common); in fsg_alloc_inst()
3405 struct fsg_common *common = opts->common; in fsg_alloc() local
3424 fsg->common = common; in fsg_alloc()