• Home
  • Raw
  • Download

Lines Matching refs:common

293 	struct fsg_common	*common;  member
307 static inline int __fsg_is_set(struct fsg_common *common, in __fsg_is_set() argument
310 if (common->fsg) in __fsg_is_set()
312 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line); in __fsg_is_set()
317 #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__)) argument
324 static int exception_in_progress(struct fsg_common *common) in exception_in_progress() argument
326 return common->state > FSG_STATE_NORMAL; in exception_in_progress()
330 static void set_bulk_out_req_length(struct fsg_common *common, in set_bulk_out_req_length() argument
336 rem = length % common->bulk_out_maxpacket; in set_bulk_out_req_length()
338 length += common->bulk_out_maxpacket - rem; in set_bulk_out_req_length()
364 static void __raise_exception(struct fsg_common *common, enum fsg_state new_state, in __raise_exception() argument
374 spin_lock_irqsave(&common->lock, flags); in __raise_exception()
375 if (common->state <= new_state) { in __raise_exception()
376 common->exception_req_tag = common->ep0_req_tag; in __raise_exception()
377 common->state = new_state; in __raise_exception()
378 common->exception_arg = arg; in __raise_exception()
379 if (common->thread_task) in __raise_exception()
381 common->thread_task); in __raise_exception()
383 spin_unlock_irqrestore(&common->lock, flags); in __raise_exception()
386 static void raise_exception(struct fsg_common *common, enum fsg_state new_state) in raise_exception() argument
388 __raise_exception(common, new_state, NULL); in raise_exception()
393 static int ep0_queue(struct fsg_common *common) in ep0_queue() argument
397 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC); in ep0_queue()
398 common->ep0->driver_data = common; in ep0_queue()
401 WARNING(common, "error in submission: %s --> %d\n", in ep0_queue()
402 common->ep0->name, rc); in ep0_queue()
414 struct fsg_common *common = ep->driver_data; in bulk_in_complete() local
418 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_in_complete()
425 wake_up(&common->io_wait); in bulk_in_complete()
430 struct fsg_common *common = ep->driver_data; in bulk_out_complete() local
433 dump_msg(common, "bulk-out", req->buf, req->actual); in bulk_out_complete()
435 DBG(common, "%s --> %d, %u/%u\n", __func__, in bulk_out_complete()
442 wake_up(&common->io_wait); in bulk_out_complete()
445 static int _fsg_common_get_max_lun(struct fsg_common *common) in _fsg_common_get_max_lun() argument
447 int i = ARRAY_SIZE(common->luns) - 1; in _fsg_common_get_max_lun()
449 while (i >= 0 && !common->luns[i]) in _fsg_common_get_max_lun()
459 struct usb_request *req = fsg->common->ep0req; in fsg_setup()
464 if (!fsg_is_set(fsg->common)) in fsg_setup()
467 ++fsg->common->ep0_req_tag; /* Record arrival of a new request */ in fsg_setup()
487 raise_exception(fsg->common, FSG_STATE_PROTOCOL_RESET); in fsg_setup()
498 *(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common); in fsg_setup()
502 return ep0_queue(fsg->common); in fsg_setup()
544 static bool start_in_transfer(struct fsg_common *common, struct fsg_buffhd *bh) in start_in_transfer() argument
548 if (!fsg_is_set(common)) in start_in_transfer()
551 rc = start_transfer(common->fsg, common->fsg->bulk_in, bh->inreq); in start_in_transfer()
555 common->running = 0; in start_in_transfer()
562 static bool start_out_transfer(struct fsg_common *common, struct fsg_buffhd *bh) in start_out_transfer() argument
566 if (!fsg_is_set(common)) in start_out_transfer()
569 rc = start_transfer(common->fsg, common->fsg->bulk_out, bh->outreq); in start_out_transfer()
573 common->running = 0; in start_out_transfer()
580 static int sleep_thread(struct fsg_common *common, bool can_freeze, in sleep_thread() argument
591 rc = wait_event_freezable(common->io_wait, in sleep_thread()
595 rc = wait_event_interruptible(common->io_wait, in sleep_thread()
604 static int do_read(struct fsg_common *common) in do_read() argument
606 struct fsg_lun *curlun = common->curlun; in do_read()
619 if (common->cmnd[0] == READ_6) in do_read()
620 lba = get_unaligned_be24(&common->cmnd[1]); in do_read()
622 lba = get_unaligned_be32(&common->cmnd[2]); in do_read()
629 if ((common->cmnd[1] & ~0x18) != 0) { in do_read()
641 amount_left = common->data_size_from_cmnd; in do_read()
657 bh = common->next_buffhd_to_fill; in do_read()
658 rc = sleep_thread(common, false, bh); in do_read()
696 common->residue -= nread; in do_read()
720 if (!start_in_transfer(common, bh)) in do_read()
723 common->next_buffhd_to_fill = bh->next; in do_read()
732 static int do_write(struct fsg_common *common) in do_write() argument
734 struct fsg_lun *curlun = common->curlun; in do_write()
756 if (common->cmnd[0] == WRITE_6) in do_write()
757 lba = get_unaligned_be24(&common->cmnd[1]); in do_write()
759 lba = get_unaligned_be32(&common->cmnd[2]); in do_write()
767 if (common->cmnd[1] & ~0x18) { in do_write()
771 if (!curlun->nofua && (common->cmnd[1] & 0x08)) { /* FUA */ in do_write()
785 amount_left_to_req = common->data_size_from_cmnd; in do_write()
786 amount_left_to_write = common->data_size_from_cmnd; in do_write()
791 bh = common->next_buffhd_to_fill; in do_write()
814 common->usb_amount_left -= amount; in do_write()
824 set_bulk_out_req_length(common, bh, amount); in do_write()
825 if (!start_out_transfer(common, bh)) in do_write()
828 common->next_buffhd_to_fill = bh->next; in do_write()
833 bh = common->next_buffhd_to_drain; in do_write()
838 rc = sleep_thread(common, false, bh); in do_write()
842 common->next_buffhd_to_drain = bh->next; in do_write()
893 common->residue -= nwritten; in do_write()
907 common->short_packet_received = 1; in do_write()
918 static int do_synchronize_cache(struct fsg_common *common) in do_synchronize_cache() argument
920 struct fsg_lun *curlun = common->curlun; in do_synchronize_cache()
944 static int do_verify(struct fsg_common *common) in do_verify() argument
946 struct fsg_lun *curlun = common->curlun; in do_verify()
949 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in do_verify()
959 lba = get_unaligned_be32(&common->cmnd[2]); in do_verify()
969 if (common->cmnd[1] & ~0x10) { in do_verify()
974 verification_length = get_unaligned_be16(&common->cmnd[7]); in do_verify()
1045 static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh) in do_inquiry() argument
1047 struct fsg_lun *curlun = common->curlun; in do_inquiry()
1051 common->bad_lun_okay = 1; in do_inquiry()
1070 memcpy(buf + 8, common->inquiry_string, in do_inquiry()
1071 sizeof(common->inquiry_string)); in do_inquiry()
1075 static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_request_sense() argument
1077 struct fsg_lun *curlun = common->curlun; in do_request_sense()
1105 common->bad_lun_okay = 1; in do_request_sense()
1128 static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_capacity() argument
1130 struct fsg_lun *curlun = common->curlun; in do_read_capacity()
1131 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_capacity()
1132 int pmi = common->cmnd[8]; in do_read_capacity()
1147 static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_header() argument
1149 struct fsg_lun *curlun = common->curlun; in do_read_header()
1150 int msf = common->cmnd[1] & 0x02; in do_read_header()
1151 u32 lba = get_unaligned_be32(&common->cmnd[2]); in do_read_header()
1154 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */ in do_read_header()
1169 static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh) in do_read_toc() argument
1171 struct fsg_lun *curlun = common->curlun; in do_read_toc()
1172 int msf = common->cmnd[1] & 0x02; in do_read_toc()
1173 int start_track = common->cmnd[6]; in do_read_toc()
1178 format = common->cmnd[2] & 0xf; in do_read_toc()
1180 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ in do_read_toc()
1192 format = (common->cmnd[9] >> 6) & 0x3; in do_read_toc()
1243 static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_sense() argument
1245 struct fsg_lun *curlun = common->curlun; in do_mode_sense()
1246 int mscmnd = common->cmnd[0]; in do_mode_sense()
1254 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */ in do_mode_sense()
1258 pc = common->cmnd[2] >> 6; in do_mode_sense()
1259 page_code = common->cmnd[2] & 0x3f; in do_mode_sense()
1329 static int do_start_stop(struct fsg_common *common) in do_start_stop() argument
1331 struct fsg_lun *curlun = common->curlun; in do_start_stop()
1339 } else if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */ in do_start_stop()
1340 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */ in do_start_stop()
1345 loej = common->cmnd[4] & 0x02; in do_start_stop()
1346 start = common->cmnd[4] & 0x01; in do_start_stop()
1370 up_read(&common->filesem); in do_start_stop()
1371 down_write(&common->filesem); in do_start_stop()
1373 up_write(&common->filesem); in do_start_stop()
1374 down_read(&common->filesem); in do_start_stop()
1379 static int do_prevent_allow(struct fsg_common *common) in do_prevent_allow() argument
1381 struct fsg_lun *curlun = common->curlun; in do_prevent_allow()
1384 if (!common->curlun) { in do_prevent_allow()
1386 } else if (!common->curlun->removable) { in do_prevent_allow()
1387 common->curlun->sense_data = SS_INVALID_COMMAND; in do_prevent_allow()
1391 prevent = common->cmnd[4] & 0x01; in do_prevent_allow()
1392 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */ in do_prevent_allow()
1403 static int do_read_format_capacities(struct fsg_common *common, in do_read_format_capacities() argument
1406 struct fsg_lun *curlun = common->curlun; in do_read_format_capacities()
1420 static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh) in do_mode_select() argument
1422 struct fsg_lun *curlun = common->curlun; in do_mode_select()
1478 static int throw_away_data(struct fsg_common *common) in throw_away_data() argument
1484 for (bh = common->next_buffhd_to_drain; in throw_away_data()
1485 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; in throw_away_data()
1486 bh = common->next_buffhd_to_drain) { in throw_away_data()
1489 bh2 = common->next_buffhd_to_fill; in throw_away_data()
1491 common->usb_amount_left > 0) { in throw_away_data()
1492 amount = min(common->usb_amount_left, FSG_BUFLEN); in throw_away_data()
1499 set_bulk_out_req_length(common, bh2, amount); in throw_away_data()
1500 if (!start_out_transfer(common, bh2)) in throw_away_data()
1503 common->next_buffhd_to_fill = bh2->next; in throw_away_data()
1504 common->usb_amount_left -= amount; in throw_away_data()
1509 rc = sleep_thread(common, false, bh); in throw_away_data()
1515 common->next_buffhd_to_drain = bh->next; in throw_away_data()
1520 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in throw_away_data()
1527 static int finish_reply(struct fsg_common *common) in finish_reply() argument
1529 struct fsg_buffhd *bh = common->next_buffhd_to_fill; in finish_reply()
1532 switch (common->data_dir) { in finish_reply()
1543 if (!common->can_stall) { in finish_reply()
1545 } else if (fsg_is_set(common)) { in finish_reply()
1546 fsg_set_halt(common->fsg, common->fsg->bulk_out); in finish_reply()
1547 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1556 if (common->data_size == 0) { in finish_reply()
1560 } else if (!fsg_is_set(common)) { in finish_reply()
1564 } else if (common->residue == 0) { in finish_reply()
1566 if (!start_in_transfer(common, bh)) in finish_reply()
1568 common->next_buffhd_to_fill = bh->next; in finish_reply()
1579 if (!start_in_transfer(common, bh)) in finish_reply()
1581 common->next_buffhd_to_fill = bh->next; in finish_reply()
1582 if (common->can_stall) in finish_reply()
1583 rc = halt_bulk_in_endpoint(common->fsg); in finish_reply()
1592 if (common->residue == 0) { in finish_reply()
1596 } else if (common->short_packet_received) { in finish_reply()
1597 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1609 } else if (common->can_stall) { in finish_reply()
1610 if (fsg_is_set(common)) in finish_reply()
1611 fsg_set_halt(common->fsg, in finish_reply()
1612 common->fsg->bulk_out); in finish_reply()
1613 raise_exception(common, FSG_STATE_ABORT_BULK_OUT); in finish_reply()
1622 rc = throw_away_data(common); in finish_reply()
1629 static void send_status(struct fsg_common *common) in send_status() argument
1631 struct fsg_lun *curlun = common->curlun; in send_status()
1639 bh = common->next_buffhd_to_fill; in send_status()
1640 rc = sleep_thread(common, false, bh); in send_status()
1647 } else if (common->bad_lun_okay) in send_status()
1652 if (common->phase_error) { in send_status()
1653 DBG(common, "sending phase-error status\n"); in send_status()
1657 DBG(common, "sending command-failure status\n"); in send_status()
1659 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;" in send_status()
1668 csw->Tag = common->tag; in send_status()
1669 csw->Residue = cpu_to_le32(common->residue); in send_status()
1674 if (!start_in_transfer(common, bh)) in send_status()
1678 common->next_buffhd_to_fill = bh->next; in send_status()
1689 static int check_command(struct fsg_common *common, int cmnd_size, in check_command() argument
1694 unsigned int lun = common->cmnd[1] >> 5; in check_command()
1700 if (common->data_dir != DATA_DIR_UNKNOWN) in check_command()
1701 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir], in check_command()
1702 common->data_size); in check_command()
1703 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", in check_command()
1705 common->data_size_from_cmnd, common->cmnd_size, hdlen); in check_command()
1711 if (common->data_size_from_cmnd == 0) in check_command()
1713 if (common->data_size < common->data_size_from_cmnd) { in check_command()
1719 common->data_size_from_cmnd = common->data_size; in check_command()
1720 common->phase_error = 1; in check_command()
1722 common->residue = common->data_size; in check_command()
1723 common->usb_amount_left = common->data_size; in check_command()
1726 if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) { in check_command()
1727 common->phase_error = 1; in check_command()
1732 if (cmnd_size != common->cmnd_size) { in check_command()
1747 if (cmnd_size <= common->cmnd_size) { in check_command()
1748 DBG(common, "%s is buggy! Expected length %d " in check_command()
1750 cmnd_size, common->cmnd_size); in check_command()
1751 cmnd_size = common->cmnd_size; in check_command()
1753 common->phase_error = 1; in check_command()
1759 if (common->lun != lun) in check_command()
1760 DBG(common, "using LUN %u from CBW, not LUN %u from CDB\n", in check_command()
1761 common->lun, lun); in check_command()
1764 curlun = common->curlun; in check_command()
1766 if (common->cmnd[0] != REQUEST_SENSE) { in check_command()
1772 common->bad_lun_okay = 0; in check_command()
1778 if (common->cmnd[0] != INQUIRY && in check_command()
1779 common->cmnd[0] != REQUEST_SENSE) { in check_command()
1780 DBG(common, "unsupported LUN %u\n", common->lun); in check_command()
1790 common->cmnd[0] != INQUIRY && in check_command()
1791 common->cmnd[0] != REQUEST_SENSE) { in check_command()
1798 common->cmnd[1] &= 0x1f; /* Mask away the LUN */ in check_command()
1800 if (common->cmnd[i] && !(mask & (1 << i))) { in check_command()
1818 static int check_command_size_in_blocks(struct fsg_common *common, in check_command_size_in_blocks() argument
1822 if (common->curlun) in check_command_size_in_blocks()
1823 common->data_size_from_cmnd <<= common->curlun->blkbits; in check_command_size_in_blocks()
1824 return check_command(common, cmnd_size, data_dir, in check_command_size_in_blocks()
1828 static int do_scsi_command(struct fsg_common *common) in do_scsi_command() argument
1836 dump_cdb(common); in do_scsi_command()
1839 bh = common->next_buffhd_to_fill; in do_scsi_command()
1840 common->next_buffhd_to_drain = bh; in do_scsi_command()
1841 rc = sleep_thread(common, false, bh); in do_scsi_command()
1845 common->phase_error = 0; in do_scsi_command()
1846 common->short_packet_received = 0; in do_scsi_command()
1848 down_read(&common->filesem); /* We're using the backing file */ in do_scsi_command()
1849 switch (common->cmnd[0]) { in do_scsi_command()
1852 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1853 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1857 reply = do_inquiry(common, bh); in do_scsi_command()
1861 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1862 reply = check_command(common, 6, DATA_DIR_FROM_HOST, in do_scsi_command()
1866 reply = do_mode_select(common, bh); in do_scsi_command()
1870 common->data_size_from_cmnd = in do_scsi_command()
1871 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1872 reply = check_command(common, 10, DATA_DIR_FROM_HOST, in do_scsi_command()
1876 reply = do_mode_select(common, bh); in do_scsi_command()
1880 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1881 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1885 reply = do_mode_sense(common, bh); in do_scsi_command()
1889 common->data_size_from_cmnd = in do_scsi_command()
1890 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1891 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1895 reply = do_mode_sense(common, bh); in do_scsi_command()
1899 common->data_size_from_cmnd = 0; in do_scsi_command()
1900 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1904 reply = do_prevent_allow(common); in do_scsi_command()
1908 i = common->cmnd[4]; in do_scsi_command()
1909 common->data_size_from_cmnd = (i == 0) ? 256 : i; in do_scsi_command()
1910 reply = check_command_size_in_blocks(common, 6, in do_scsi_command()
1915 reply = do_read(common); in do_scsi_command()
1919 common->data_size_from_cmnd = in do_scsi_command()
1920 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1921 reply = check_command_size_in_blocks(common, 10, in do_scsi_command()
1926 reply = do_read(common); in do_scsi_command()
1930 common->data_size_from_cmnd = in do_scsi_command()
1931 get_unaligned_be32(&common->cmnd[6]); in do_scsi_command()
1932 reply = check_command_size_in_blocks(common, 12, in do_scsi_command()
1937 reply = do_read(common); in do_scsi_command()
1941 common->data_size_from_cmnd = 8; in do_scsi_command()
1942 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1946 reply = do_read_capacity(common, bh); in do_scsi_command()
1950 if (!common->curlun || !common->curlun->cdrom) in do_scsi_command()
1952 common->data_size_from_cmnd = in do_scsi_command()
1953 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1954 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1958 reply = do_read_header(common, bh); in do_scsi_command()
1962 if (!common->curlun || !common->curlun->cdrom) in do_scsi_command()
1964 common->data_size_from_cmnd = in do_scsi_command()
1965 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1966 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1970 reply = do_read_toc(common, bh); in do_scsi_command()
1974 common->data_size_from_cmnd = in do_scsi_command()
1975 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
1976 reply = check_command(common, 10, DATA_DIR_TO_HOST, in do_scsi_command()
1980 reply = do_read_format_capacities(common, bh); in do_scsi_command()
1984 common->data_size_from_cmnd = common->cmnd[4]; in do_scsi_command()
1985 reply = check_command(common, 6, DATA_DIR_TO_HOST, in do_scsi_command()
1989 reply = do_request_sense(common, bh); in do_scsi_command()
1993 common->data_size_from_cmnd = 0; in do_scsi_command()
1994 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
1998 reply = do_start_stop(common); in do_scsi_command()
2002 common->data_size_from_cmnd = 0; in do_scsi_command()
2003 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
2007 reply = do_synchronize_cache(common); in do_scsi_command()
2011 common->data_size_from_cmnd = 0; in do_scsi_command()
2012 reply = check_command(common, 6, DATA_DIR_NONE, in do_scsi_command()
2022 common->data_size_from_cmnd = 0; in do_scsi_command()
2023 reply = check_command(common, 10, DATA_DIR_NONE, in do_scsi_command()
2027 reply = do_verify(common); in do_scsi_command()
2031 i = common->cmnd[4]; in do_scsi_command()
2032 common->data_size_from_cmnd = (i == 0) ? 256 : i; in do_scsi_command()
2033 reply = check_command_size_in_blocks(common, 6, in do_scsi_command()
2038 reply = do_write(common); in do_scsi_command()
2042 common->data_size_from_cmnd = in do_scsi_command()
2043 get_unaligned_be16(&common->cmnd[7]); in do_scsi_command()
2044 reply = check_command_size_in_blocks(common, 10, in do_scsi_command()
2049 reply = do_write(common); in do_scsi_command()
2053 common->data_size_from_cmnd = in do_scsi_command()
2054 get_unaligned_be32(&common->cmnd[6]); in do_scsi_command()
2055 reply = check_command_size_in_blocks(common, 12, in do_scsi_command()
2060 reply = do_write(common); in do_scsi_command()
2076 common->data_size_from_cmnd = 0; in do_scsi_command()
2077 sprintf(unknown, "Unknown x%02x", common->cmnd[0]); in do_scsi_command()
2078 reply = check_command(common, common->cmnd_size, in do_scsi_command()
2081 common->curlun->sense_data = SS_INVALID_COMMAND; in do_scsi_command()
2086 up_read(&common->filesem); in do_scsi_command()
2094 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) { in do_scsi_command()
2095 reply = min((u32)reply, common->data_size_from_cmnd); in do_scsi_command()
2098 common->residue -= reply; in do_scsi_command()
2111 struct fsg_common *common = fsg->common; in received_cbw() local
2142 if (cbw->Lun >= ARRAY_SIZE(common->luns) || in received_cbw()
2153 if (common->can_stall) { in received_cbw()
2161 common->cmnd_size = cbw->Length; in received_cbw()
2162 memcpy(common->cmnd, cbw->CDB, common->cmnd_size); in received_cbw()
2164 common->data_dir = DATA_DIR_TO_HOST; in received_cbw()
2166 common->data_dir = DATA_DIR_FROM_HOST; in received_cbw()
2167 common->data_size = le32_to_cpu(cbw->DataTransferLength); in received_cbw()
2168 if (common->data_size == 0) in received_cbw()
2169 common->data_dir = DATA_DIR_NONE; in received_cbw()
2170 common->lun = cbw->Lun; in received_cbw()
2171 if (common->lun < ARRAY_SIZE(common->luns)) in received_cbw()
2172 common->curlun = common->luns[common->lun]; in received_cbw()
2174 common->curlun = NULL; in received_cbw()
2175 common->tag = cbw->Tag; in received_cbw()
2179 static int get_next_command(struct fsg_common *common) in get_next_command() argument
2185 bh = common->next_buffhd_to_fill; in get_next_command()
2186 rc = sleep_thread(common, true, bh); in get_next_command()
2191 set_bulk_out_req_length(common, bh, US_BULK_CB_WRAP_LEN); in get_next_command()
2192 if (!start_out_transfer(common, bh)) in get_next_command()
2203 rc = sleep_thread(common, true, bh); in get_next_command()
2207 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; in get_next_command()
2216 static int alloc_request(struct fsg_common *common, struct usb_ep *ep, in alloc_request() argument
2222 ERROR(common, "can't allocate request for %s\n", ep->name); in alloc_request()
2227 static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg) in do_set_interface() argument
2232 if (common->running) in do_set_interface()
2233 DBG(common, "reset interface\n"); in do_set_interface()
2237 if (common->fsg) { in do_set_interface()
2238 fsg = common->fsg; in do_set_interface()
2240 for (i = 0; i < common->fsg_num_buffers; ++i) { in do_set_interface()
2241 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2263 common->fsg = NULL; in do_set_interface()
2264 wake_up(&common->fsg_wait); in do_set_interface()
2267 common->running = 0; in do_set_interface()
2271 common->fsg = new_fsg; in do_set_interface()
2272 fsg = common->fsg; in do_set_interface()
2275 rc = config_ep_by_speed(common->gadget, &(fsg->function), fsg->bulk_in); in do_set_interface()
2281 fsg->bulk_in->driver_data = common; in do_set_interface()
2284 rc = config_ep_by_speed(common->gadget, &(fsg->function), in do_set_interface()
2291 fsg->bulk_out->driver_data = common; in do_set_interface()
2293 common->bulk_out_maxpacket = usb_endpoint_maxp(fsg->bulk_out->desc); in do_set_interface()
2297 for (i = 0; i < common->fsg_num_buffers; ++i) { in do_set_interface()
2298 struct fsg_buffhd *bh = &common->buffhds[i]; in do_set_interface()
2300 rc = alloc_request(common, fsg->bulk_in, &bh->inreq); in do_set_interface()
2303 rc = alloc_request(common, fsg->bulk_out, &bh->outreq); in do_set_interface()
2312 common->running = 1; in do_set_interface()
2313 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) in do_set_interface()
2314 if (common->luns[i]) in do_set_interface()
2315 common->luns[i]->unit_attention_data = in do_set_interface()
2327 __raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE, fsg); in fsg_set_alt()
2345 __raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE, NULL); in fsg_disable()
2351 static void handle_exception(struct fsg_common *common) in handle_exception() argument
2369 spin_lock_irq(&common->lock); in handle_exception()
2370 if (common->state < FSG_STATE_EXIT) in handle_exception()
2371 DBG(common, "Main thread exiting on signal\n"); in handle_exception()
2372 common->state = FSG_STATE_EXIT; in handle_exception()
2373 spin_unlock_irq(&common->lock); in handle_exception()
2378 if (likely(common->fsg)) { in handle_exception()
2379 for (i = 0; i < common->fsg_num_buffers; ++i) { in handle_exception()
2380 bh = &common->buffhds[i]; in handle_exception()
2382 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq); in handle_exception()
2384 usb_ep_dequeue(common->fsg->bulk_out, in handle_exception()
2388 if (sleep_thread(common, false, bh)) in handle_exception()
2393 if (common->fsg->bulk_in_enabled) in handle_exception()
2394 usb_ep_fifo_flush(common->fsg->bulk_in); in handle_exception()
2395 if (common->fsg->bulk_out_enabled) in handle_exception()
2396 usb_ep_fifo_flush(common->fsg->bulk_out); in handle_exception()
2403 spin_lock_irq(&common->lock); in handle_exception()
2405 for (i = 0; i < common->fsg_num_buffers; ++i) { in handle_exception()
2406 bh = &common->buffhds[i]; in handle_exception()
2409 common->next_buffhd_to_fill = &common->buffhds[0]; in handle_exception()
2410 common->next_buffhd_to_drain = &common->buffhds[0]; in handle_exception()
2411 exception_req_tag = common->exception_req_tag; in handle_exception()
2412 new_fsg = common->exception_arg; in handle_exception()
2413 old_state = common->state; in handle_exception()
2414 common->state = FSG_STATE_NORMAL; in handle_exception()
2417 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) { in handle_exception()
2418 curlun = common->luns[i]; in handle_exception()
2428 spin_unlock_irq(&common->lock); in handle_exception()
2436 send_status(common); in handle_exception()
2445 if (!fsg_is_set(common)) in handle_exception()
2448 &common->fsg->atomic_bitflags)) in handle_exception()
2449 usb_ep_clear_halt(common->fsg->bulk_in); in handle_exception()
2451 if (common->ep0_req_tag == exception_req_tag) in handle_exception()
2452 ep0_queue(common); /* Complete the status stage */ in handle_exception()
2466 do_set_interface(common, new_fsg); in handle_exception()
2468 usb_composite_setup_continue(common->cdev); in handle_exception()
2472 do_set_interface(common, NULL); /* Free resources */ in handle_exception()
2473 spin_lock_irq(&common->lock); in handle_exception()
2474 common->state = FSG_STATE_TERMINATED; /* Stop the thread */ in handle_exception()
2475 spin_unlock_irq(&common->lock); in handle_exception()
2488 struct fsg_common *common = common_; in fsg_main_thread() local
2504 while (common->state != FSG_STATE_TERMINATED) { in fsg_main_thread()
2505 if (exception_in_progress(common) || signal_pending(current)) { in fsg_main_thread()
2506 handle_exception(common); in fsg_main_thread()
2510 if (!common->running) { in fsg_main_thread()
2511 sleep_thread(common, true, NULL); in fsg_main_thread()
2515 if (get_next_command(common) || exception_in_progress(common)) in fsg_main_thread()
2517 if (do_scsi_command(common) || exception_in_progress(common)) in fsg_main_thread()
2519 if (finish_reply(common) || exception_in_progress(common)) in fsg_main_thread()
2521 send_status(common); in fsg_main_thread()
2524 spin_lock_irq(&common->lock); in fsg_main_thread()
2525 common->thread_task = NULL; in fsg_main_thread()
2526 spin_unlock_irq(&common->lock); in fsg_main_thread()
2530 down_write(&common->filesem); in fsg_main_thread()
2531 for (i = 0; i < ARRAY_SIZE(common->luns); i++) { in fsg_main_thread()
2532 struct fsg_lun *curlun = common->luns[i]; in fsg_main_thread()
2537 up_write(&common->filesem); in fsg_main_thread()
2540 complete_and_exit(&common->thread_notifier, 0); in fsg_main_thread()
2608 static struct fsg_common *fsg_common_setup(struct fsg_common *common) in fsg_common_setup() argument
2610 if (!common) { in fsg_common_setup()
2611 common = kzalloc(sizeof(*common), GFP_KERNEL); in fsg_common_setup()
2612 if (!common) in fsg_common_setup()
2614 common->free_storage_on_release = 1; in fsg_common_setup()
2616 common->free_storage_on_release = 0; in fsg_common_setup()
2618 init_rwsem(&common->filesem); in fsg_common_setup()
2619 spin_lock_init(&common->lock); in fsg_common_setup()
2620 init_completion(&common->thread_notifier); in fsg_common_setup()
2621 init_waitqueue_head(&common->io_wait); in fsg_common_setup()
2622 init_waitqueue_head(&common->fsg_wait); in fsg_common_setup()
2623 common->state = FSG_STATE_TERMINATED; in fsg_common_setup()
2624 memset(common->luns, 0, sizeof(common->luns)); in fsg_common_setup()
2626 return common; in fsg_common_setup()
2629 void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs) in fsg_common_set_sysfs() argument
2631 common->sysfs = sysfs; in fsg_common_set_sysfs()
2647 int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n) in fsg_common_set_num_buffers() argument
2670 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_set_num_buffers()
2671 common->fsg_num_buffers = n; in fsg_common_set_num_buffers()
2672 common->buffhds = buffhds; in fsg_common_set_num_buffers()
2696 static void _fsg_common_remove_luns(struct fsg_common *common, int n) in _fsg_common_remove_luns() argument
2701 if (common->luns[i]) { in _fsg_common_remove_luns()
2702 fsg_common_remove_lun(common->luns[i]); in _fsg_common_remove_luns()
2703 common->luns[i] = NULL; in _fsg_common_remove_luns()
2707 void fsg_common_remove_luns(struct fsg_common *common) in fsg_common_remove_luns() argument
2709 _fsg_common_remove_luns(common, ARRAY_SIZE(common->luns)); in fsg_common_remove_luns()
2713 void fsg_common_free_buffers(struct fsg_common *common) in fsg_common_free_buffers() argument
2715 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_free_buffers()
2716 common->buffhds = NULL; in fsg_common_free_buffers()
2720 int fsg_common_set_cdev(struct fsg_common *common, in fsg_common_set_cdev() argument
2725 common->gadget = cdev->gadget; in fsg_common_set_cdev()
2726 common->ep0 = cdev->gadget->ep0; in fsg_common_set_cdev()
2727 common->ep0req = cdev->req; in fsg_common_set_cdev()
2728 common->cdev = cdev; in fsg_common_set_cdev()
2742 common->can_stall = can_stall && in fsg_common_set_cdev()
2743 gadget_is_stall_supported(common->gadget); in fsg_common_set_cdev()
2779 int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg, in fsg_common_create_lun() argument
2787 if (id >= ARRAY_SIZE(common->luns)) in fsg_common_create_lun()
2790 if (common->luns[id]) in fsg_common_create_lun()
2809 if (!common->sysfs) { in fsg_common_create_lun()
2814 lun->dev.parent = &common->gadget->dev; in fsg_common_create_lun()
2816 dev_set_drvdata(&lun->dev, &common->filesem); in fsg_common_create_lun()
2828 common->luns[id] = lun; in fsg_common_create_lun()
2859 common->luns[id] = NULL; in fsg_common_create_lun()
2866 int fsg_common_create_luns(struct fsg_common *common, struct fsg_config *cfg) in fsg_common_create_luns() argument
2871 fsg_common_remove_luns(common); in fsg_common_create_luns()
2875 rc = fsg_common_create_lun(common, &cfg->luns[i], i, buf, NULL); in fsg_common_create_luns()
2885 _fsg_common_remove_luns(common, i); in fsg_common_create_luns()
2890 void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn, in fsg_common_set_inquiry_string() argument
2897 snprintf(common->inquiry_string, sizeof(common->inquiry_string), in fsg_common_set_inquiry_string()
2900 pn ?: ((*common->luns)->cdrom in fsg_common_set_inquiry_string()
2907 static void fsg_common_release(struct fsg_common *common) in fsg_common_release() argument
2912 if (common->state != FSG_STATE_TERMINATED) { in fsg_common_release()
2913 raise_exception(common, FSG_STATE_EXIT); in fsg_common_release()
2914 wait_for_completion(&common->thread_notifier); in fsg_common_release()
2917 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) { in fsg_common_release()
2918 struct fsg_lun *lun = common->luns[i]; in fsg_common_release()
2927 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers); in fsg_common_release()
2928 if (common->free_storage_on_release) in fsg_common_release()
2929 kfree(common); in fsg_common_release()
2938 struct fsg_common *common = fsg->common; in fsg_bind() local
2947 ret = _fsg_common_get_max_lun(common); in fsg_bind()
2955 ret = fsg_common_set_cdev(fsg->common, c->cdev, in fsg_bind()
2956 fsg->common->can_stall); in fsg_bind()
2959 fsg_common_set_inquiry_string(fsg->common, NULL, NULL); in fsg_bind()
2962 if (!common->thread_task) { in fsg_bind()
2963 common->state = FSG_STATE_NORMAL; in fsg_bind()
2964 common->thread_task = in fsg_bind()
2965 kthread_create(fsg_main_thread, common, "file-storage"); in fsg_bind()
2966 if (IS_ERR(common->thread_task)) { in fsg_bind()
2967 ret = PTR_ERR(common->thread_task); in fsg_bind()
2968 common->thread_task = NULL; in fsg_bind()
2969 common->state = FSG_STATE_TERMINATED; in fsg_bind()
2972 DBG(common, "I/O thread pid: %d\n", in fsg_bind()
2973 task_pid_nr(common->thread_task)); in fsg_bind()
2974 wake_up_process(common->thread_task); in fsg_bind()
3026 if (fsg->common->state != FSG_STATE_TERMINATED) { in fsg_bind()
3027 raise_exception(fsg->common, FSG_STATE_EXIT); in fsg_bind()
3028 wait_for_completion(&fsg->common->thread_notifier); in fsg_bind()
3038 struct fsg_common *common = fsg->common; in fsg_unbind() local
3041 if (fsg->common->fsg == fsg) { in fsg_unbind()
3042 __raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE, NULL); in fsg_unbind()
3044 wait_event(common->fsg_wait, common->fsg != fsg); in fsg_unbind()
3078 return fsg_show_file(opts->lun, &fsg_opts->common->filesem, page); in fsg_lun_opts_file_show()
3087 return fsg_store_file(opts->lun, &fsg_opts->common->filesem, page, len); in fsg_lun_opts_file_store()
3103 return fsg_store_ro(opts->lun, &fsg_opts->common->filesem, page, len); in fsg_lun_opts_ro_store()
3133 return fsg_store_cdrom(opts->lun, &fsg_opts->common->filesem, page, in fsg_lun_opts_cdrom_store()
3209 if (fsg_opts->refcnt || fsg_opts->common->luns[num]) { in fsg_lun_make()
3223 ret = fsg_common_create_lun(fsg_opts->common, &config, num, name, in fsg_lun_make()
3229 opts->lun = fsg_opts->common->luns[num]; in fsg_lun_make()
3258 fsg_opts->common->luns[lun_opts->lun_id] = NULL; in fsg_lun_drop()
3282 result = sprintf(page, "%d", opts->common->can_stall); in fsg_opts_stall_show()
3304 opts->common->can_stall = stall; in fsg_opts_stall_store()
3322 result = sprintf(page, "%d", opts->common->fsg_num_buffers); in fsg_opts_num_buffers_show()
3344 ret = fsg_common_set_num_buffers(opts->common, num); in fsg_opts_num_buffers_store()
3382 fsg_common_release(opts->common); in fsg_free_inst()
3397 opts->common = fsg_common_setup(opts->common); in fsg_alloc_inst()
3398 if (IS_ERR(opts->common)) { in fsg_alloc_inst()
3399 rc = PTR_ERR(opts->common); in fsg_alloc_inst()
3403 rc = fsg_common_set_num_buffers(opts->common, in fsg_alloc_inst()
3412 rc = fsg_common_create_lun(opts->common, &config, 0, "lun.0", in fsg_alloc_inst()
3417 opts->lun0.lun = opts->common->luns[0]; in fsg_alloc_inst()
3428 fsg_common_free_buffers(opts->common); in fsg_alloc_inst()
3430 kfree(opts->common); in fsg_alloc_inst()
3454 struct fsg_common *common = opts->common; in fsg_alloc() local
3473 fsg->common = common; in fsg_alloc()