Lines Matching refs:common

295 	struct fsg_common	*common;  member
309 static inline int __fsg_is_set(struct fsg_common *common, in __fsg_is_set() argument
312 if (common->fsg) in __fsg_is_set()
314 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line); in __fsg_is_set()
319 #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__)) argument
326 static int exception_in_progress(struct fsg_common *common) in exception_in_progress() argument
328 return common->state > FSG_STATE_NORMAL; in exception_in_progress()
332 static void set_bulk_out_req_length(struct fsg_common *common, in set_bulk_out_req_length() argument
338 rem = length % common->bulk_out_maxpacket; in set_bulk_out_req_length()
340 length += common->bulk_out_maxpacket - rem; in set_bulk_out_req_length()
366 static void __raise_exception(struct fsg_common *common, enum fsg_state new_state, in __raise_exception() argument
376 spin_lock_irqsave(&common->lock, flags); in __raise_exception()
377 if (common->state <= new_state) { in __raise_exception()
378 common->exception_req_tag = common->ep0_req_tag; in __raise_exception()
379 common->state = new_state; in __raise_exception()
380 common->exception_arg = arg; in __raise_exception()
381 if (common->thread_task) in __raise_exception()
383 common->thread_task); in __raise_exception()
385 spin_unlock_irqrestore(&common->lock, flags); in __raise_exception()
388 static void raise_exception(struct fsg_common *common, enum fsg_state new_state) in raise_exception() argument
390 __raise_exception(common, new_state, NULL); in raise_exception()
395 static int ep0_queue(struct fsg_common *common) in ep0_queue() argument
399 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC); in ep0_queue()
400 common->ep0->driver_data = common; in ep0_queue()
403 WARNING(common, "error in submission: %s --> %d\n", in ep0_queue()
404 common->ep0->name, rc); in ep0_queue()
416 struct fsg_common *common = ep->driver_data; in bulk_in_complete() local
420 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_in_complete()
427 wake_up(&common->io_wait); in bulk_in_complete()
432 struct fsg_common *common = ep->driver_data; in bulk_out_complete() local
435 dump_msg(common, "bulk-out", req->buf, req->actual); in bulk_out_complete()
437 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_out_complete()
444 wake_up(&common->io_wait); in bulk_out_complete()
447 static int _fsg_common_get_max_lun(struct fsg_common *common) in _fsg_common_get_max_lun() argument
449 int i = ARRAY_SIZE(common->luns) - 1; in _fsg_common_get_max_lun()
451 while (i >= 0 && !common->luns[i]) in _fsg_common_get_max_lun()
461 struct usb_request *req = fsg->common->ep0req; in fsg_setup()
466 if (!fsg_is_set(fsg->common)) in fsg_setup()
469 ++fsg->common->ep0_req_tag; /* Record arrival of a new request */ in fsg_setup()
489 raise_exception(fsg->common, FSG_STATE_PROTOCOL_RESET); in fsg_setup()
500 *(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common); in fsg_setup()
504 return ep0_queue(fsg->common); in fsg_setup()
546 static bool start_in_transfer(struct fsg_common *common, struct fsg_buffhd *bh) in start_in_transfer() argument
550 if (!fsg_is_set(common)) in start_in_transfer()
553 rc = start_transfer(common->fsg, common->fsg->bulk_in, bh->inreq); in start_in_transfer()
557 common->running = 0; in start_in_transfer()
564 static bool start_out_transfer(struct fsg_common *common, struct fsg_buffhd *bh) in start_out_transfer() argument
568 if (!fsg_is_set(common)) in start_out_transfer()
571 rc = start_transfer(common->fsg, common->fsg->bulk_out, bh->outreq); in start_out_transfer()
575 common->running = 0; in start_out_transfer()
582 static int sleep_thread(struct fsg_common *common, bool can_freeze, in sleep_thread() argument
593 rc = wait_event_freezable(common->io_wait, in sleep_thread()
597 rc = wait_event_interruptible(common->io_wait, in sleep_thread()
606 static int do_read(struct fsg_common *common) in do_read() argument
608 struct fsg_lun *curlun = common->curlun; in do_read()
621 if (common->cmnd[0] == READ_6) in do_read()
622 lba = get_unaligned_be24(&common->cmnd[1]); in do_read()
624 if (common->cmnd[0] == READ_16) in do_read()
625 lba = get_unaligned_be64(&common->cmnd[2]); in do_read()
627 lba = get_unaligned_be32(&common->cmnd[2]); in do_read()
634 if ((common->cmnd[1] & ~0x18) != 0) { in do_read()
646 amount_left = common->data_size_from_cmnd; in do_read()
662 bh = common->next_buffhd_to_fill; in do_read()
663 rc = sleep_thread(common, false, bh); in do_read()
701 common->residue -= nread; in do_read()
725 if (!start_in_transfer(common, bh)) in do_read()
728 common->next_buffhd_to_fill = bh->next; in do_read()
737 static int do_write(struct fsg_common *common) in do_write() argument
739 struct fsg_lun *curlun = common->curlun; in do_write()
761 if (common->cmnd[0] == WRITE_6) in do_write()
762 lba = get_unaligned_be24(&common->cmnd[1]); in do_write()
764 if (common->cmnd[0] == WRITE_16) in do_write()
765 lba = get_unaligned_be64(&common->cmnd[2]); 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()
1159 static int do_read_capacity_16(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_capacity_16() argument
1161 struct fsg_lun *curlun = common->curlun; in do_read_capacity_16()
1162 u64 lba = get_unaligned_be64(&common->cmnd[2]); in do_read_capacity_16()
1163 int pmi = common->cmnd[14]; in do_read_capacity_16()
1181 static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_header() argument
1183 struct fsg_lun *curlun = common->curlun; in do_read_header()
1184 int msf = common->cmnd[1] & 0x02; in do_read_header()
1185 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_header()
1188 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */ in do_read_header()
1203 static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_toc() argument
1205 struct fsg_lun *curlun = common->curlun; in do_read_toc()
1206 int msf = common->cmnd[1] & 0x02; in do_read_toc()
1207 int start_track = common->cmnd[6]; in do_read_toc()
1212 format = common->cmnd[2] & 0xf; in do_read_toc()
1214 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ in do_read_toc()
1226 format = (common->cmnd[9] >> 6) & 0x3; in do_read_toc()
1277 static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_sense() argument
1279 struct fsg_lun *curlun = common->curlun; in do_mode_sense()
1280 int mscmnd = common->cmnd[0]; in do_mode_sense()
1288 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */ in do_mode_sense()
1292 pc = common->cmnd[2] >> 6; in do_mode_sense()
1293 page_code = common->cmnd[2] & 0x3f; in do_mode_sense()
1363 static int do_start_stop(struct fsg_common *common) in do_start_stop() argument
1365 struct fsg_lun *curlun = common->curlun; in do_start_stop()
1373 } else if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */ in do_start_stop()
1374 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */ in do_start_stop()
1379 loej = common->cmnd[4] & 0x02; in do_start_stop()
1380 start = common->cmnd[4] & 0x01; in do_start_stop()
1404 up_read(&common->filesem); in do_start_stop()
1405 down_write(&common->filesem); in do_start_stop()
1407 up_write(&common->filesem); in do_start_stop()
1408 down_read(&common->filesem); in do_start_stop()
1413 static int do_prevent_allow(struct fsg_common *common) in do_prevent_allow() argument
1415 struct fsg_lun *curlun = common->curlun; in do_prevent_allow()
1418 if (!common->curlun) { in do_prevent_allow()
1420 } else if (!common->curlun->removable) { in do_prevent_allow()
1421 common->curlun->sense_data = SS_INVALID_COMMAND; in do_prevent_allow()
1425 prevent = common->cmnd[4] & 0x01; in do_prevent_allow()
1426 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */ in do_prevent_allow()
1437 static int do_read_format_capacities(struct fsg_common *common, in do_read_format_capacities() argument
1440 struct fsg_lun *curlun = common->curlun; in do_read_format_capacities()
1454 static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_select() argument
1456 struct fsg_lun *curlun = common->curlun; in do_mode_select()
1512 static int throw_away_data(struct fsg_common *common) in throw_away_data() argument
1518 for (bh = common->next_buffhd_to_drain; in throw_away_data()
1519 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; in throw_away_data()
1520 bh = common->next_buffhd_to_drain) { in throw_away_data()
1523 bh2 = common->next_buffhd_to_fill; in throw_away_data()
1525 common->usb_amount_left > 0) { in throw_away_data()
1526 amount = min(common->usb_amount_left, FSG_BUFLEN); in throw_away_data()
1533 set_bulk_out_req_length(common, bh2, amount); in throw_away_data()
1534 if (!start_out_transfer(common, bh2)) in throw_away_data()
1537 common->next_buffhd_to_fill = bh2->next; in throw_away_data()
1538 common->usb_amount_left -= amount; in throw_away_data()
1543 rc = sleep_thread(common, false, bh); in throw_away_data()
1549 common->next_buffhd_to_drain = bh->next; in throw_away_data()
1554 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in throw_away_data()
1561 static int finish_reply(struct fsg_common *common) in finish_reply() argument
1563 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in finish_reply()
1566 switch (common->data_dir) { in finish_reply()
1577 if (!common->can_stall) { in finish_reply()
1579 } else if (fsg_is_set(common)) { in finish_reply()
1580 fsg_set_halt(common->fsg, common->fsg->bulk_out); in finish_reply()
1581 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1590 if (common->data_size == 0) { in finish_reply()
1594 } else if (!fsg_is_set(common)) { in finish_reply()
1598 } else if (common->residue == 0) { in finish_reply()
1600 if (!start_in_transfer(common, bh)) in finish_reply()
1602 common->next_buffhd_to_fill = bh->next; in finish_reply()
1613 if (!start_in_transfer(common, bh)) in finish_reply()
1615 common->next_buffhd_to_fill = bh->next; in finish_reply()
1616 if (common->can_stall) in finish_reply()
1617 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1626 if (common->residue == 0) { in finish_reply()
1630 } else if (common->short_packet_received) { in finish_reply()
1631 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1643 } else if (common->can_stall) { in finish_reply()
1644 if (fsg_is_set(common)) in finish_reply()
1645 fsg_set_halt(common->fsg, in finish_reply()
1646 common->fsg->bulk_out); in finish_reply()
1647 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1656 rc = throw_away_data(common); in finish_reply()
1663 static void send_status(struct fsg_common *common) in send_status() argument
1665 struct fsg_lun *curlun = common->curlun; in send_status()
1673 bh = common->next_buffhd_to_fill; in send_status()
1674 rc = sleep_thread(common, false, bh); in send_status()
1681 } else if (common->bad_lun_okay) in send_status()
1686 if (common->phase_error) { in send_status()
1687 DBG(common, "sending phase-error status\n"); in send_status()
1691 DBG(common, "sending command-failure status\n"); in send_status()
1693 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;" in send_status()
1702 csw->Tag = common->tag; in send_status()
1703 csw->Residue = cpu_to_le32(common->residue); in send_status()
1708 if (!start_in_transfer(common, bh)) in send_status()
1712 common->next_buffhd_to_fill = bh->next; in send_status()
1723 static int check_command(struct fsg_common *common, int cmnd_size, in check_command() argument
1728 unsigned int lun = common->cmnd[1] >> 5; in check_command()
1734 if (common->data_dir != DATA_DIR_UNKNOWN) in check_command()
1735 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir], in check_command()
1736 common->data_size); in check_command()
1737 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", in check_command()
1739 common->data_size_from_cmnd, common->cmnd_size, hdlen); in check_command()
1745 if (common->data_size_from_cmnd == 0) in check_command()
1747 if (common->data_size < common->data_size_from_cmnd) { in check_command()
1753 common->data_size_from_cmnd = common->data_size; in check_command()
1754 common->phase_error = 1; in check_command()
1756 common->residue = common->data_size; in check_command()
1757 common->usb_amount_left = common->data_size; in check_command()
1760 if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) { in check_command()
1761 common->phase_error = 1; in check_command()
1766 if (cmnd_size != common->cmnd_size) { in check_command()
1781 if (cmnd_size <= common->cmnd_size) { in check_command()
1782 DBG(common, "%s is buggy! Expected length %d " in check_command()
1784 cmnd_size, common->cmnd_size); in check_command()
1785 cmnd_size = common->cmnd_size; in check_command()
1787 common->phase_error = 1; in check_command()
1793 if (common->lun != lun) in check_command()
1794 DBG(common, "using LUN %u from CBW, not LUN %u from CDB\n", in check_command()
1795 common->lun, lun); in check_command()
1798 curlun = common->curlun; in check_command()
1800 if (common->cmnd[0] != REQUEST_SENSE) { in check_command()
1806 common->bad_lun_okay = 0; in check_command()
1812 if (common->cmnd[0] != INQUIRY && in check_command()
1813 common->cmnd[0] != REQUEST_SENSE) { in check_command()
1814 DBG(common, "unsupported LUN %u\n", common->lun); in check_command()
1824 common->cmnd[0] != INQUIRY && in check_command()
1825 common->cmnd[0] != REQUEST_SENSE) { in check_command()
1832 common->cmnd[1] &= 0x1f; /* Mask away the LUN */ in check_command()
1834 if (common->cmnd[i] && !(mask & (1 << i))) { in check_command()
1852 static int check_command_size_in_blocks(struct fsg_common *common, in check_command_size_in_blocks() argument
1856 if (common->curlun) in check_command_size_in_blocks()
1857 common->data_size_from_cmnd <<= common->curlun->blkbits; in check_command_size_in_blocks()
1858 return check_command(common, cmnd_size, data_dir, in check_command_size_in_blocks()
1862 static int do_scsi_command(struct fsg_common *common) in do_scsi_command() argument
1870 dump_cdb(common); in do_scsi_command()
1873 bh = common->next_buffhd_to_fill; in do_scsi_command()
1874 common->next_buffhd_to_drain = bh; in do_scsi_command()
1875 rc = sleep_thread(common, false, bh); in do_scsi_command()
1879 common->phase_error = 0; in do_scsi_command()
1880 common->short_packet_received = 0; in do_scsi_command()
1882 down_read(&common->filesem); /* We're using the backing file */ in do_scsi_command()
1883 switch (common->cmnd[0]) { in do_scsi_command()
1886 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1887 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1891 reply = do_inquiry(common, bh); in do_scsi_command()
1895 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1896 reply = check_command(common, 6, DATA_DIR_FROM_HOST, in do_scsi_command()
1900 reply = do_mode_select(common, bh); in do_scsi_command()
1904 common->data_size_from_cmnd = in do_scsi_command()
1905 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1906 reply = check_command(common, 10, DATA_DIR_FROM_HOST, in do_scsi_command()
1910 reply = do_mode_select(common, bh); in do_scsi_command()
1914 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1915 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1919 reply = do_mode_sense(common, bh); in do_scsi_command()
1923 common->data_size_from_cmnd = in do_scsi_command()
1924 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1925 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1929 reply = do_mode_sense(common, bh); in do_scsi_command()
1933 common->data_size_from_cmnd = 0; in do_scsi_command()
1934 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1938 reply = do_prevent_allow(common); in do_scsi_command()
1942 i = common->cmnd[4]; in do_scsi_command()
1943 common->data_size_from_cmnd = (i == 0) ? 256 : i; in do_scsi_command()
1944 reply = check_command_size_in_blocks(common, 6, in do_scsi_command()
1949 reply = do_read(common); in do_scsi_command()
1953 common->data_size_from_cmnd = in do_scsi_command()
1954 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1955 reply = check_command_size_in_blocks(common, 10, in do_scsi_command()
1960 reply = do_read(common); in do_scsi_command()
1964 common->data_size_from_cmnd = in do_scsi_command()
1965 get_unaligned_be32(&common->cmnd[6]); in do_scsi_command()
1966 reply = check_command_size_in_blocks(common, 12, in do_scsi_command()
1971 reply = do_read(common); in do_scsi_command()
1975 common->data_size_from_cmnd = in do_scsi_command()
1976 get_unaligned_be32(&common->cmnd[10]); in do_scsi_command()
1977 reply = check_command_size_in_blocks(common, 16, in do_scsi_command()
1982 reply = do_read(common); in do_scsi_command()
1986 common->data_size_from_cmnd = 8; in do_scsi_command()
1987 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1991 reply = do_read_capacity(common, bh); in do_scsi_command()
1995 if (!common->curlun || !common->curlun->cdrom) in do_scsi_command()
1997 common->data_size_from_cmnd = in do_scsi_command()
1998 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1999 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
2003 reply = do_read_header(common, bh); in do_scsi_command()
2007 if (!common->curlun || !common->curlun->cdrom) in do_scsi_command()
2009 common->data_size_from_cmnd = in do_scsi_command()
2010 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2011 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
2015 reply = do_read_toc(common, bh); in do_scsi_command()
2019 common->data_size_from_cmnd = in do_scsi_command()
2020 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2021 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
2025 reply = do_read_format_capacities(common, bh); in do_scsi_command()
2029 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
2030 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
2034 reply = do_request_sense(common, bh); in do_scsi_command()
2038 switch (common->cmnd[1] & 0x1f) { in do_scsi_command()
2041 common->data_size_from_cmnd = in do_scsi_command()
2042 get_unaligned_be32(&common->cmnd[10]); in do_scsi_command()
2043 reply = check_command(common, 16, DATA_DIR_TO_HOST, in do_scsi_command()
2048 reply = do_read_capacity_16(common, bh); in do_scsi_command()
2057 common->data_size_from_cmnd = 0; in do_scsi_command()
2058 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
2062 reply = do_start_stop(common); in do_scsi_command()
2066 common->data_size_from_cmnd = 0; in do_scsi_command()
2067 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
2071 reply = do_synchronize_cache(common); in do_scsi_command()
2075 common->data_size_from_cmnd = 0; in do_scsi_command()
2076 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
2086 common->data_size_from_cmnd = 0; in do_scsi_command()
2087 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
2091 reply = do_verify(common); in do_scsi_command()
2095 i = common->cmnd[4]; in do_scsi_command()
2096 common->data_size_from_cmnd = (i == 0) ? 256 : i; in do_scsi_command()
2097 reply = check_command_size_in_blocks(common, 6, in do_scsi_command()
2102 reply = do_write(common); in do_scsi_command()
2106 common->data_size_from_cmnd = in do_scsi_command()
2107 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2108 reply = check_command_size_in_blocks(common, 10, in do_scsi_command()
2113 reply = do_write(common); in do_scsi_command()
2117 common->data_size_from_cmnd = in do_scsi_command()
2118 get_unaligned_be32(&common->cmnd[6]); in do_scsi_command()
2119 reply = check_command_size_in_blocks(common, 12, in do_scsi_command()
2124 reply = do_write(common); in do_scsi_command()
2128 common->data_size_from_cmnd = in do_scsi_command()
2129 get_unaligned_be32(&common->cmnd[10]); in do_scsi_command()
2130 reply = check_command_size_in_blocks(common, 16, in do_scsi_command()
2135 reply = do_write(common); in do_scsi_command()
2151 common->data_size_from_cmnd = 0; in do_scsi_command()
2152 sprintf(unknown, "Unknown x%02x", common->cmnd[0]); in do_scsi_command()
2153 reply = check_command(common, common->cmnd_size, in do_scsi_command()
2156 common->curlun->sense_data = SS_INVALID_COMMAND; in do_scsi_command()
2161 up_read(&common->filesem); in do_scsi_command()
2169 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) { in do_scsi_command()
2170 reply = min_t(u32, reply, common->data_size_from_cmnd); in do_scsi_command()
2173 common->residue -= reply; in do_scsi_command()
2186 struct fsg_common *common = fsg->common; in received_cbw() local
2217 if (cbw->Lun >= ARRAY_SIZE(common->luns) || in received_cbw()
2228 if (common->can_stall) { in received_cbw()
2236 common->cmnd_size = cbw->Length; in received_cbw()
2237 memcpy(common->cmnd, cbw->CDB, common->cmnd_size); in received_cbw()
2239 common->data_dir = DATA_DIR_TO_HOST; in received_cbw()
2241 common->data_dir = DATA_DIR_FROM_HOST; in received_cbw()
2242 common->data_size = le32_to_cpu(cbw->DataTransferLength); in received_cbw()
2243 if (common->data_size == 0) in received_cbw()
2244 common->data_dir = DATA_DIR_NONE; in received_cbw()
2245 common->lun = cbw->Lun; in received_cbw()
2246 if (common->lun < ARRAY_SIZE(common->luns)) in received_cbw()
2247 common->curlun = common->luns[common->lun]; in received_cbw()
2249 common->curlun = NULL; in received_cbw()
2250 common->tag = cbw->Tag; in received_cbw()
2254 static int get_next_command(struct fsg_common *common) in get_next_command() argument
2260 bh = common->next_buffhd_to_fill; in get_next_command()
2261 rc = sleep_thread(common, true, bh); in get_next_command()
2266 set_bulk_out_req_length(common, bh, US_BULK_CB_WRAP_LEN); in get_next_command()
2267 if (!start_out_transfer(common, bh)) in get_next_command()
2278 rc = sleep_thread(common, true, bh); in get_next_command()
2282 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; in get_next_command()
2291 static int alloc_request(struct fsg_common *common, struct usb_ep *ep, in alloc_request() argument
2297 ERROR(common, "can't allocate request for %s\n", ep->name); in alloc_request()
2302 static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg) in do_set_interface() argument
2307 if (common->running) in do_set_interface()
2308 DBG(common, "reset interface\n"); in do_set_interface()
2312 if (common->fsg) { in do_set_interface()
2313 fsg = common->fsg; in do_set_interface()
2315 for (i = 0; i < common->fsg_num_buffers; ++i) { in do_set_interface()
2316 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2338 common->fsg = NULL; in do_set_interface()
2339 wake_up(&common->fsg_wait); in do_set_interface()
2342 common->running = 0; in do_set_interface()
2346 common->fsg = new_fsg; in do_set_interface()
2347 fsg = common->fsg; in do_set_interface()
2350 rc = config_ep_by_speed(common->gadget, &(fsg->function), fsg->bulk_in); in do_set_interface()
2356 fsg->bulk_in->driver_data = common; in do_set_interface()
2359 rc = config_ep_by_speed(common->gadget, &(fsg->function), in do_set_interface()
2366 fsg->bulk_out->driver_data = common; in do_set_interface()
2368 common->bulk_out_maxpacket = usb_endpoint_maxp(fsg->bulk_out->desc); in do_set_interface()
2372 for (i = 0; i < common->fsg_num_buffers; ++i) { in do_set_interface()
2373 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2375 rc = alloc_request(common, fsg->bulk_in, &bh->inreq); in do_set_interface()
2378 rc = alloc_request(common, fsg->bulk_out, &bh->outreq); in do_set_interface()
2387 common->running = 1; in do_set_interface()
2388 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) in do_set_interface()
2389 if (common->luns[i]) in do_set_interface()
2390 common->luns[i]->unit_attention_data = in do_set_interface()
2402 __raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE, fsg); in fsg_set_alt()
2420 __raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE, NULL); in fsg_disable()
2426 static void handle_exception(struct fsg_common *common) in handle_exception() argument
2444 spin_lock_irq(&common->lock); in handle_exception()
2445 if (common->state < FSG_STATE_EXIT) in handle_exception()
2446 DBG(common, "Main thread exiting on signal\n"); in handle_exception()
2447 common->state = FSG_STATE_EXIT; in handle_exception()
2448 spin_unlock_irq(&common->lock); in handle_exception()
2453 if (likely(common->fsg)) { in handle_exception()
2454 for (i = 0; i < common->fsg_num_buffers; ++i) { in handle_exception()
2455 bh = &common->buffhds[i]; in handle_exception()
2457 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq); in handle_exception()
2459 usb_ep_dequeue(common->fsg->bulk_out, in handle_exception()
2463 if (sleep_thread(common, false, bh)) in handle_exception()
2468 if (common->fsg->bulk_in_enabled) in handle_exception()
2469 usb_ep_fifo_flush(common->fsg->bulk_in); in handle_exception()
2470 if (common->fsg->bulk_out_enabled) in handle_exception()
2471 usb_ep_fifo_flush(common->fsg->bulk_out); in handle_exception()
2478 spin_lock_irq(&common->lock); in handle_exception()
2480 for (i = 0; i < common->fsg_num_buffers; ++i) { in handle_exception()
2481 bh = &common->buffhds[i]; in handle_exception()
2484 common->next_buffhd_to_fill = &common->buffhds[0]; in handle_exception()
2485 common->next_buffhd_to_drain = &common->buffhds[0]; in handle_exception()
2486 exception_req_tag = common->exception_req_tag; in handle_exception()
2487 new_fsg = common->exception_arg; in handle_exception()
2488 old_state = common->state; in handle_exception()
2489 common->state = FSG_STATE_NORMAL; in handle_exception()
2492 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) { in handle_exception()
2493 curlun = common->luns[i]; in handle_exception()
2503 spin_unlock_irq(&common->lock); in handle_exception()
2511 send_status(common); in handle_exception()
2520 if (!fsg_is_set(common)) in handle_exception()
2523 &common->fsg->atomic_bitflags)) in handle_exception()
2524 usb_ep_clear_halt(common->fsg->bulk_in); in handle_exception()
2526 if (common->ep0_req_tag == exception_req_tag) in handle_exception()
2527 ep0_queue(common); /* Complete the status stage */ in handle_exception()
2541 do_set_interface(common, new_fsg); in handle_exception()
2543 usb_composite_setup_continue(common->cdev); in handle_exception()
2547 do_set_interface(common, NULL); /* Free resources */ in handle_exception()
2548 spin_lock_irq(&common->lock); in handle_exception()
2549 common->state = FSG_STATE_TERMINATED; /* Stop the thread */ in handle_exception()
2550 spin_unlock_irq(&common->lock); in handle_exception()
2563 struct fsg_common *common = common_; in fsg_main_thread() local
2579 while (common->state != FSG_STATE_TERMINATED) { in fsg_main_thread()
2580 if (exception_in_progress(common) || signal_pending(current)) { in fsg_main_thread()
2581 handle_exception(common); in fsg_main_thread()
2585 if (!common->running) { in fsg_main_thread()
2586 sleep_thread(common, true, NULL); in fsg_main_thread()
2590 if (get_next_command(common) || exception_in_progress(common)) in fsg_main_thread()
2592 if (do_scsi_command(common) || exception_in_progress(common)) in fsg_main_thread()
2594 if (finish_reply(common) || exception_in_progress(common)) in fsg_main_thread()
2596 send_status(common); in fsg_main_thread()
2599 spin_lock_irq(&common->lock); in fsg_main_thread()
2600 common->thread_task = NULL; in fsg_main_thread()
2601 spin_unlock_irq(&common->lock); in fsg_main_thread()
2605 down_write(&common->filesem); in fsg_main_thread()
2606 for (i = 0; i < ARRAY_SIZE(common->luns); i++) { in fsg_main_thread()
2607 struct fsg_lun *curlun = common->luns[i]; in fsg_main_thread()
2612 up_write(&common->filesem); in fsg_main_thread()
2615 kthread_complete_and_exit(&common->thread_notifier, 0); in fsg_main_thread()
2699 static struct fsg_common *fsg_common_setup(struct fsg_common *common) in fsg_common_setup() argument
2701 if (!common) { in fsg_common_setup()
2702 common = kzalloc(sizeof(*common), GFP_KERNEL); in fsg_common_setup()
2703 if (!common) in fsg_common_setup()
2705 common->free_storage_on_release = 1; in fsg_common_setup()
2707 common->free_storage_on_release = 0; in fsg_common_setup()
2709 init_rwsem(&common->filesem); in fsg_common_setup()
2710 spin_lock_init(&common->lock); in fsg_common_setup()
2711 init_completion(&common->thread_notifier); in fsg_common_setup()
2712 init_waitqueue_head(&common->io_wait); in fsg_common_setup()
2713 init_waitqueue_head(&common->fsg_wait); in fsg_common_setup()
2714 common->state = FSG_STATE_TERMINATED; in fsg_common_setup()
2715 memset(common->luns, 0, sizeof(common->luns)); in fsg_common_setup()
2717 return common; in fsg_common_setup()
2720 void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs) in fsg_common_set_sysfs() argument
2722 common->sysfs = sysfs; in fsg_common_set_sysfs()
2738 int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n) in fsg_common_set_num_buffers() argument
2761 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_set_num_buffers()
2762 common->fsg_num_buffers = n; in fsg_common_set_num_buffers()
2763 common->buffhds = buffhds; in fsg_common_set_num_buffers()
2787 static void _fsg_common_remove_luns(struct fsg_common *common, int n) in _fsg_common_remove_luns() argument
2792 if (common->luns[i]) { in _fsg_common_remove_luns()
2793 fsg_common_remove_lun(common->luns[i]); in _fsg_common_remove_luns()
2794 common->luns[i] = NULL; in _fsg_common_remove_luns()
2798 void fsg_common_remove_luns(struct fsg_common *common) in fsg_common_remove_luns() argument
2800 _fsg_common_remove_luns(common, ARRAY_SIZE(common->luns)); in fsg_common_remove_luns()
2804 void fsg_common_free_buffers(struct fsg_common *common) in fsg_common_free_buffers() argument
2806 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_free_buffers()
2807 common->buffhds = NULL; in fsg_common_free_buffers()
2811 int fsg_common_set_cdev(struct fsg_common *common, in fsg_common_set_cdev() argument
2816 common->gadget = cdev->gadget; in fsg_common_set_cdev()
2817 common->ep0 = cdev->gadget->ep0; in fsg_common_set_cdev()
2818 common->ep0req = cdev->req; in fsg_common_set_cdev()
2819 common->cdev = cdev; in fsg_common_set_cdev()
2833 common->can_stall = can_stall && in fsg_common_set_cdev()
2834 gadget_is_stall_supported(common->gadget); in fsg_common_set_cdev()
2871 int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg, in fsg_common_create_lun() argument
2879 if (id >= ARRAY_SIZE(common->luns)) in fsg_common_create_lun()
2882 if (common->luns[id]) in fsg_common_create_lun()
2901 if (!common->sysfs) { in fsg_common_create_lun()
2906 lun->dev.parent = &common->gadget->dev; in fsg_common_create_lun()
2908 dev_set_drvdata(&lun->dev, &common->filesem); in fsg_common_create_lun()
2920 common->luns[id] = lun; in fsg_common_create_lun()
2947 common->luns[id] = NULL; in fsg_common_create_lun()
2954 int fsg_common_create_luns(struct fsg_common *common, struct fsg_config *cfg) in fsg_common_create_luns() argument
2959 fsg_common_remove_luns(common); in fsg_common_create_luns()
2963 rc = fsg_common_create_lun(common, &cfg->luns[i], i, buf, NULL); in fsg_common_create_luns()
2973 _fsg_common_remove_luns(common, i); in fsg_common_create_luns()
2978 void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn, in fsg_common_set_inquiry_string() argument
2985 snprintf(common->inquiry_string, sizeof(common->inquiry_string), in fsg_common_set_inquiry_string()
2988 pn ?: ((*common->luns)->cdrom in fsg_common_set_inquiry_string()
2995 static void fsg_common_release(struct fsg_common *common) in fsg_common_release() argument
3000 if (common->state != FSG_STATE_TERMINATED) { in fsg_common_release()
3001 raise_exception(common, FSG_STATE_EXIT); in fsg_common_release()
3002 wait_for_completion(&common->thread_notifier); in fsg_common_release()
3005 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) { in fsg_common_release()
3006 struct fsg_lun *lun = common->luns[i]; in fsg_common_release()
3015 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_release()
3016 if (common->free_storage_on_release) in fsg_common_release()
3017 kfree(common); in fsg_common_release()
3026 struct fsg_common *common = fsg->common; in fsg_bind() local
3035 ret = _fsg_common_get_max_lun(common); in fsg_bind()
3043 ret = fsg_common_set_cdev(fsg->common, c->cdev, in fsg_bind()
3044 fsg->common->can_stall); in fsg_bind()
3047 fsg_common_set_inquiry_string(fsg->common, NULL, NULL); in fsg_bind()
3050 if (!common->thread_task) { in fsg_bind()
3051 common->state = FSG_STATE_NORMAL; in fsg_bind()
3052 common->thread_task = in fsg_bind()
3053 kthread_run(fsg_main_thread, common, "file-storage"); in fsg_bind()
3054 if (IS_ERR(common->thread_task)) { in fsg_bind()
3055 ret = PTR_ERR(common->thread_task); in fsg_bind()
3056 common->thread_task = NULL; in fsg_bind()
3057 common->state = FSG_STATE_TERMINATED; in fsg_bind()
3060 DBG(common, "I/O thread pid: %d\n", in fsg_bind()
3061 task_pid_nr(common->thread_task)); in fsg_bind()
3113 if (fsg->common->state != FSG_STATE_TERMINATED) { in fsg_bind()
3114 raise_exception(fsg->common, FSG_STATE_EXIT); in fsg_bind()
3115 wait_for_completion(&fsg->common->thread_notifier); in fsg_bind()
3125 struct fsg_common *common = fsg->common; in fsg_unbind() local
3128 if (fsg->common->fsg == fsg) { in fsg_unbind()
3129 __raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE, NULL); in fsg_unbind()
3131 wait_event(common->fsg_wait, common->fsg != fsg); in fsg_unbind()
3165 return fsg_show_file(opts->lun, &fsg_opts->common->filesem, page); in fsg_lun_opts_file_show()
3174 return fsg_store_file(opts->lun, &fsg_opts->common->filesem, page, len); in fsg_lun_opts_file_store()
3190 return fsg_store_ro(opts->lun, &fsg_opts->common->filesem, page, len); in fsg_lun_opts_ro_store()
3220 return fsg_store_cdrom(opts->lun, &fsg_opts->common->filesem, page, in fsg_lun_opts_cdrom_store()
3259 return fsg_store_forced_eject(opts->lun, &fsg_opts->common->filesem, in fsg_lun_opts_forced_eject_store()
3309 if (fsg_opts->refcnt || fsg_opts->common->luns[num]) { in fsg_lun_make()
3323 ret = fsg_common_create_lun(fsg_opts->common, &config, num, name, in fsg_lun_make()
3329 opts->lun = fsg_opts->common->luns[num]; in fsg_lun_make()
3358 fsg_opts->common->luns[lun_opts->lun_id] = NULL; in fsg_lun_drop()
3382 result = sprintf(page, "%d", opts->common->can_stall); in fsg_opts_stall_show()
3404 opts->common->can_stall = stall; in fsg_opts_stall_store()
3422 result = sprintf(page, "%d", opts->common->fsg_num_buffers); in fsg_opts_num_buffers_show()
3444 ret = fsg_common_set_num_buffers(opts->common, num); in fsg_opts_num_buffers_store()
3482 fsg_common_release(opts->common); in fsg_free_inst()
3497 opts->common = fsg_common_setup(opts->common); in fsg_alloc_inst()
3498 if (IS_ERR(opts->common)) { in fsg_alloc_inst()
3499 rc = PTR_ERR(opts->common); in fsg_alloc_inst()
3503 rc = fsg_common_set_num_buffers(opts->common, in fsg_alloc_inst()
3512 rc = fsg_common_create_lun(opts->common, &config, 0, "lun.0", in fsg_alloc_inst()
3517 opts->lun0.lun = opts->common->luns[0]; in fsg_alloc_inst()
3528 fsg_common_free_buffers(opts->common); in fsg_alloc_inst()
3530 kfree(opts->common); in fsg_alloc_inst()
3554 struct fsg_common *common = opts->common; in fsg_alloc() local
3573 fsg->common = common; in fsg_alloc()