Lines Matching defs:fsg
76 * fsg->state; it won't try to stop the thread if the state is already
90 * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
399 static int exception_in_progress(struct fsg_dev *fsg)
401 return (fsg->state > FSG_STATE_IDLE);
405 static void set_bulk_out_req_length(struct fsg_dev *fsg,
411 rem = length % fsg->bulk_out_maxpacket;
413 length += fsg->bulk_out_maxpacket - rem;
423 static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
427 if (ep == fsg->bulk_in)
429 else if (ep == fsg->bulk_out)
433 DBG(fsg, "%s set halt\n", name);
530 /* Caller must hold fsg->lock */
531 static void wakeup_thread(struct fsg_dev *fsg)
534 fsg->thread_wakeup_needed = 1;
535 if (fsg->thread_task)
536 wake_up_process(fsg->thread_task);
540 static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
547 spin_lock_irqsave(&fsg->lock, flags);
548 if (fsg->state <= new_state) {
549 fsg->exception_req_tag = fsg->ep0_req_tag;
550 fsg->state = new_state;
551 if (fsg->thread_task)
553 fsg->thread_task);
555 spin_unlock_irqrestore(&fsg->lock, flags);
568 struct fsg_dev *fsg = get_gadget_data(gadget);
570 DBG(fsg, "disconnect or port reset\n");
571 raise_exception(fsg, FSG_STATE_DISCONNECT);
575 static int ep0_queue(struct fsg_dev *fsg)
579 rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
583 WARNING(fsg, "error in submission: %s --> %d\n",
584 fsg->ep0->name, rc);
591 struct fsg_dev *fsg = ep->driver_data;
594 dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
596 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
602 ((fsg_routine_t) (req->context))(fsg);
613 struct fsg_dev *fsg = ep->driver_data;
617 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
624 spin_lock(&fsg->lock);
627 wakeup_thread(fsg);
628 spin_unlock(&fsg->lock);
633 struct fsg_dev *fsg = ep->driver_data;
636 dump_msg(fsg, "bulk-out", req->buf, req->actual);
638 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
646 spin_lock(&fsg->lock);
649 wakeup_thread(fsg);
650 spin_unlock(&fsg->lock);
657 struct fsg_dev *fsg = ep->driver_data;
661 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
668 spin_lock(&fsg->lock);
669 fsg->intreq_busy = 0;
671 wakeup_thread(fsg);
672 spin_unlock(&fsg->lock);
686 static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
688 struct usb_request *req = fsg->ep0req;
698 fsg_set_halt(fsg, fsg->ep0);
709 DBG(fsg, "cbi reset request\n");
710 raise_exception(fsg, FSG_STATE_RESET);
714 VDBG(fsg, "CB[I] accept device-specific command\n");
715 spin_lock(&fsg->lock);
718 if (fsg->cbbuf_cmnd_size)
719 WARNING(fsg, "CB[I] overwriting previous command\n");
720 fsg->cbbuf_cmnd_size = req->actual;
721 memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
723 wakeup_thread(fsg);
724 spin_unlock(&fsg->lock);
728 static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
733 static int class_setup_req(struct fsg_dev *fsg,
736 struct usb_request *req = fsg->ep0req;
742 if (!fsg->config)
760 DBG(fsg, "bulk reset request\n");
761 raise_exception(fsg, FSG_STATE_RESET);
773 VDBG(fsg, "get max LUN\n");
774 *(u8 *) req->buf = fsg->nluns - 1;
797 fsg->ep0req->context = received_cbi_adsc;
803 VDBG(fsg,
816 static int standard_setup_req(struct fsg_dev *fsg,
819 struct usb_request *req = fsg->ep0req;
835 VDBG(fsg, "get device descriptor\n");
840 VDBG(fsg, "get device qualifier\n");
841 if (!gadget_is_dualspeed(fsg->gadget))
848 VDBG(fsg, "get other-speed config descriptor\n");
849 if (!gadget_is_dualspeed(fsg->gadget))
853 VDBG(fsg, "get configuration descriptor\n");
855 value = populate_config_buf(fsg->gadget,
862 VDBG(fsg, "get string descriptor\n");
876 VDBG(fsg, "set configuration\n");
878 fsg->new_config = w_value;
882 raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
890 VDBG(fsg, "get configuration\n");
891 *(u8 *) req->buf = fsg->config;
899 if (fsg->config && w_index == 0) {
904 raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
912 if (!fsg->config)
918 VDBG(fsg, "get interface\n");
924 VDBG(fsg,
937 struct fsg_dev *fsg = get_gadget_data(gadget);
941 ++fsg->ep0_req_tag; // Record arrival of a new request
942 fsg->ep0req->context = NULL;
943 fsg->ep0req->length = 0;
944 dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
947 rc = class_setup_req(fsg, ctrl);
949 rc = standard_setup_req(fsg, ctrl);
954 fsg->ep0req->length = rc;
955 fsg->ep0req->zero = rc < w_length;
956 fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
958 rc = ep0_queue(fsg);
972 static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
978 if (ep == fsg->bulk_in)
979 dump_msg(fsg, "bulk-in", req->buf, req->length);
980 else if (ep == fsg->intr_in)
981 dump_msg(fsg, "intr-in", req->buf, req->length);
983 spin_lock_irq(&fsg->lock);
986 spin_unlock_irq(&fsg->lock);
998 WARNING(fsg, "error in submission: %s --> %d\n",
1004 static int sleep_thread(struct fsg_dev *fsg)
1016 if (fsg->thread_wakeup_needed)
1021 fsg->thread_wakeup_needed = 0;
1028 static int do_read(struct fsg_dev *fsg)
1030 struct fsg_lun *curlun = fsg->curlun;
1042 if (fsg->cmnd[0] == SC_READ_6)
1043 lba = get_unaligned_be24(&fsg->cmnd[1]);
1045 lba = get_unaligned_be32(&fsg->cmnd[2]);
1050 if ((fsg->cmnd[1] & ~0x18) != 0) {
1062 amount_left = fsg->data_size_from_cmnd;
1085 bh = fsg->next_buffhd_to_fill;
1087 rc = sleep_thread(fsg);
1126 fsg->residue -= nread;
1143 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1145 fsg->next_buffhd_to_fill = bh->next;
1154 static int do_write(struct fsg_dev *fsg)
1156 struct fsg_lun *curlun = fsg->curlun;
1177 if (fsg->cmnd[0] == SC_WRITE_6)
1178 lba = get_unaligned_be24(&fsg->cmnd[1]);
1180 lba = get_unaligned_be32(&fsg->cmnd[2]);
1186 if ((fsg->cmnd[1] & ~0x18) != 0) {
1191 if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
1205 amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
1210 bh = fsg->next_buffhd_to_fill;
1249 fsg->usb_amount_left -= amount;
1259 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1261 fsg->next_buffhd_to_fill = bh->next;
1266 bh = fsg->next_buffhd_to_drain;
1271 fsg->next_buffhd_to_drain = bh->next;
1314 fsg->residue -= nwritten;
1326 fsg->short_packet_received = 1;
1333 rc = sleep_thread(fsg);
1344 static int do_synchronize_cache(struct fsg_dev *fsg)
1346 struct fsg_lun *curlun = fsg->curlun;
1370 static int do_verify(struct fsg_dev *fsg)
1372 struct fsg_lun *curlun = fsg->curlun;
1375 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1383 lba = get_unaligned_be32(&fsg->cmnd[2]);
1391 if ((fsg->cmnd[1] & ~0x10) != 0) {
1396 verification_length = get_unaligned_be16(&fsg->cmnd[7]);
1468 static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1476 if (!fsg->curlun) { // Unsupported LUNs are okay
1477 fsg->bad_lun_okay = 1;
1500 static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1502 struct fsg_lun *curlun = fsg->curlun;
1524 fsg->bad_lun_okay = 1;
1548 static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1550 struct fsg_lun *curlun = fsg->curlun;
1551 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
1552 int pmi = fsg->cmnd[8];
1568 static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1570 struct fsg_lun *curlun = fsg->curlun;
1571 int msf = fsg->cmnd[1] & 0x02;
1572 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
1575 if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
1591 static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1593 struct fsg_lun *curlun = fsg->curlun;
1594 int msf = fsg->cmnd[1] & 0x02;
1595 int start_track = fsg->cmnd[6];
1598 if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1619 static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1621 struct fsg_lun *curlun = fsg->curlun;
1622 int mscmnd = fsg->cmnd[0];
1630 if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
1634 pc = fsg->cmnd[2] >> 6;
1635 page_code = fsg->cmnd[2] & 0x3f;
1700 static int do_start_stop(struct fsg_dev *fsg)
1702 struct fsg_lun *curlun = fsg->curlun;
1710 // int immed = fsg->cmnd[1] & 0x01;
1711 loej = fsg->cmnd[4] & 0x02;
1712 start = fsg->cmnd[4] & 0x01;
1715 if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
1716 (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
1730 up_read(&fsg->filesem);
1731 down_write(&fsg->filesem);
1733 up_write(&fsg->filesem);
1734 down_read(&fsg->filesem);
1750 static int do_prevent_allow(struct fsg_dev *fsg)
1752 struct fsg_lun *curlun = fsg->curlun;
1760 prevent = fsg->cmnd[4] & 0x01;
1761 if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
1773 static int do_read_format_capacities(struct fsg_dev *fsg,
1776 struct fsg_lun *curlun = fsg->curlun;
1791 static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1793 struct fsg_lun *curlun = fsg->curlun;
1803 static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1807 rc = fsg_set_halt(fsg, fsg->bulk_in);
1809 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1812 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1820 rc = usb_ep_set_halt(fsg->bulk_in);
1825 static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1829 DBG(fsg, "bulk-in set wedge\n");
1830 rc = usb_ep_set_wedge(fsg->bulk_in);
1832 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1835 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1843 rc = usb_ep_set_wedge(fsg->bulk_in);
1848 static int pad_with_zeros(struct fsg_dev *fsg)
1850 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1856 fsg->usb_amount_left = nkeep + fsg->residue;
1857 while (fsg->usb_amount_left > 0) {
1861 rc = sleep_thread(fsg);
1866 nsend = min(fsg->usb_amount_left, (u32) mod_data.buflen);
1870 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1872 bh = fsg->next_buffhd_to_fill = bh->next;
1873 fsg->usb_amount_left -= nsend;
1879 static int throw_away_data(struct fsg_dev *fsg)
1885 while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
1886 fsg->usb_amount_left > 0) {
1892 fsg->next_buffhd_to_drain = bh->next;
1897 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
1904 bh = fsg->next_buffhd_to_fill;
1905 if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
1906 amount = min(fsg->usb_amount_left,
1914 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1916 fsg->next_buffhd_to_fill = bh->next;
1917 fsg->usb_amount_left -= amount;
1922 rc = sleep_thread(fsg);
1930 static int finish_reply(struct fsg_dev *fsg)
1932 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1935 switch (fsg->data_dir) {
1945 fsg_set_halt(fsg, fsg->bulk_out);
1946 rc = halt_bulk_in_endpoint(fsg);
1952 if (fsg->data_size == 0)
1956 else if (fsg->residue == 0) {
1958 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1960 fsg->next_buffhd_to_fill = bh->next;
1971 fsg->residue == fsg->data_size &&
1972 (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
1974 rc = halt_bulk_in_endpoint(fsg);
1977 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1979 fsg->next_buffhd_to_fill = bh->next;
1989 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1991 fsg->next_buffhd_to_fill = bh->next;
1992 rc = halt_bulk_in_endpoint(fsg);
1994 rc = pad_with_zeros(fsg);
2001 if (fsg->residue == 0)
2005 else if (fsg->short_packet_received) {
2006 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2020 rc = throw_away_data(fsg);
2027 static int send_status(struct fsg_dev *fsg)
2029 struct fsg_lun *curlun = fsg->curlun;
2036 bh = fsg->next_buffhd_to_fill;
2038 rc = sleep_thread(fsg);
2046 } else if (fsg->bad_lun_okay)
2051 if (fsg->phase_error) {
2052 DBG(fsg, "sending phase-error status\n");
2056 DBG(fsg, "sending command-failure status\n");
2058 VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
2068 csw->Tag = fsg->tag;
2069 csw->Residue = cpu_to_le32(fsg->residue);
2074 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2095 fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
2097 fsg->intr_buffhd = bh; // Point to the right buffhd
2098 fsg->intreq->buf = bh->inreq->buf;
2099 fsg->intreq->context = bh;
2100 start_transfer(fsg, fsg->intr_in, fsg->intreq,
2101 &fsg->intreq_busy, &bh->state);
2104 fsg->next_buffhd_to_fill = bh->next;
2113 static int check_command(struct fsg_dev *fsg, int cmnd_size,
2118 int lun = fsg->cmnd[1] >> 5;
2131 if (fsg->cmnd_size == 12)
2139 if (fsg->data_dir != DATA_DIR_UNKNOWN)
2140 sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
2141 fsg->data_size);
2142 VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
2144 fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
2148 if (fsg->data_size_from_cmnd == 0)
2150 if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
2151 fsg->data_dir = data_dir;
2152 fsg->data_size = fsg->data_size_from_cmnd;
2155 if (fsg->data_size < fsg->data_size_from_cmnd) {
2160 fsg->data_size_from_cmnd = fsg->data_size;
2161 fsg->phase_error = 1;
2164 fsg->residue = fsg->usb_amount_left = fsg->data_size;
2167 if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
2168 fsg->phase_error = 1;
2173 if (cmnd_size != fsg->cmnd_size) {
2175 if (cmnd_size <= fsg->cmnd_size) {
2176 DBG(fsg, "%s is buggy! Expected length %d "
2178 cmnd_size, fsg->cmnd_size);
2179 cmnd_size = fsg->cmnd_size;
2181 fsg->phase_error = 1;
2188 if (fsg->lun != lun)
2189 DBG(fsg, "using LUN %d from CBW, "
2191 fsg->lun, lun);
2193 fsg->lun = lun; // Use LUN from the command
2196 if (fsg->lun >= 0 && fsg->lun < fsg->nluns) {
2197 fsg->curlun = curlun = &fsg->luns[fsg->lun];
2198 if (fsg->cmnd[0] != SC_REQUEST_SENSE) {
2204 fsg->curlun = curlun = NULL;
2205 fsg->bad_lun_okay = 0;
2209 if (fsg->cmnd[0] != SC_INQUIRY &&
2210 fsg->cmnd[0] != SC_REQUEST_SENSE) {
2211 DBG(fsg, "unsupported LUN %d\n", fsg->lun);
2219 fsg->cmnd[0] != SC_INQUIRY &&
2220 fsg->cmnd[0] != SC_REQUEST_SENSE) {
2227 fsg->cmnd[1] &= 0x1f; // Mask away the LUN
2229 if (fsg->cmnd[i] && !(mask & (1 << i))) {
2247 static int do_scsi_command(struct fsg_dev *fsg)
2255 dump_cdb(fsg);
2258 bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
2260 rc = sleep_thread(fsg);
2264 fsg->phase_error = 0;
2265 fsg->short_packet_received = 0;
2267 down_read(&fsg->filesem); // We're using the backing file
2268 switch (fsg->cmnd[0]) {
2271 fsg->data_size_from_cmnd = fsg->cmnd[4];
2272 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2275 reply = do_inquiry(fsg, bh);
2279 fsg->data_size_from_cmnd = fsg->cmnd[4];
2280 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2283 reply = do_mode_select(fsg, bh);
2287 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2288 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2291 reply = do_mode_select(fsg, bh);
2295 fsg->data_size_from_cmnd = fsg->cmnd[4];
2296 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2299 reply = do_mode_sense(fsg, bh);
2303 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2304 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2307 reply = do_mode_sense(fsg, bh);
2311 fsg->data_size_from_cmnd = 0;
2312 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2315 reply = do_prevent_allow(fsg);
2319 i = fsg->cmnd[4];
2320 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2321 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2324 reply = do_read(fsg);
2328 fsg->data_size_from_cmnd =
2329 get_unaligned_be16(&fsg->cmnd[7]) << 9;
2330 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2333 reply = do_read(fsg);
2337 fsg->data_size_from_cmnd =
2338 get_unaligned_be32(&fsg->cmnd[6]) << 9;
2339 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
2342 reply = do_read(fsg);
2346 fsg->data_size_from_cmnd = 8;
2347 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2350 reply = do_read_capacity(fsg, bh);
2356 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2357 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2360 reply = do_read_header(fsg, bh);
2366 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2367 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2370 reply = do_read_toc(fsg, bh);
2374 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2375 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2378 reply = do_read_format_capacities(fsg, bh);
2382 fsg->data_size_from_cmnd = fsg->cmnd[4];
2383 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2386 reply = do_request_sense(fsg, bh);
2390 fsg->data_size_from_cmnd = 0;
2391 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2394 reply = do_start_stop(fsg);
2398 fsg->data_size_from_cmnd = 0;
2399 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2402 reply = do_synchronize_cache(fsg);
2406 fsg->data_size_from_cmnd = 0;
2407 reply = check_command(fsg, 6, DATA_DIR_NONE,
2415 fsg->data_size_from_cmnd = 0;
2416 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2419 reply = do_verify(fsg);
2423 i = fsg->cmnd[4];
2424 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2425 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2428 reply = do_write(fsg);
2432 fsg->data_size_from_cmnd =
2433 get_unaligned_be16(&fsg->cmnd[7]) << 9;
2434 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2437 reply = do_write(fsg);
2441 fsg->data_size_from_cmnd =
2442 get_unaligned_be32(&fsg->cmnd[6]) << 9;
2443 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
2446 reply = do_write(fsg);
2461 fsg->data_size_from_cmnd = 0;
2462 sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
2463 if ((reply = check_command(fsg, fsg->cmnd_size,
2465 fsg->curlun->sense_data = SS_INVALID_COMMAND;
2470 up_read(&fsg->filesem);
2478 if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
2479 reply = min((u32) reply, fsg->data_size_from_cmnd);
2482 fsg->residue -= reply;
2491 static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2497 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2504 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2517 wedge_bulk_in_endpoint(fsg);
2518 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2525 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2532 fsg_set_halt(fsg, fsg->bulk_out);
2533 halt_bulk_in_endpoint(fsg);
2539 fsg->cmnd_size = cbw->Length;
2540 memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
2542 fsg->data_dir = DATA_DIR_TO_HOST;
2544 fsg->data_dir = DATA_DIR_FROM_HOST;
2545 fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
2546 if (fsg->data_size == 0)
2547 fsg->data_dir = DATA_DIR_NONE;
2548 fsg->lun = cbw->Lun;
2549 fsg->tag = cbw->Tag;
2554 static int get_next_command(struct fsg_dev *fsg)
2562 bh = fsg->next_buffhd_to_fill;
2564 rc = sleep_thread(fsg);
2570 set_bulk_out_req_length(fsg, bh, USB_BULK_CB_WRAP_LEN);
2572 start_transfer(fsg, fsg->bulk_out, bh->outreq,
2581 rc = sleep_thread(fsg);
2586 rc = received_cbw(fsg, bh);
2592 while (fsg->cbbuf_cmnd_size == 0) {
2593 rc = sleep_thread(fsg);
2601 if (fsg->intreq_busy)
2602 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
2605 fsg->data_dir = DATA_DIR_UNKNOWN;
2606 spin_lock_irq(&fsg->lock);
2607 fsg->cmnd_size = fsg->cbbuf_cmnd_size;
2608 memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
2609 fsg->cbbuf_cmnd_size = 0;
2610 spin_unlock_irq(&fsg->lock);
2618 static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
2623 ep->driver_data = fsg;
2626 ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
2630 static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
2636 ERROR(fsg, "can't allocate request for %s\n", ep->name);
2645 static int do_set_interface(struct fsg_dev *fsg, int altsetting)
2651 if (fsg->running)
2652 DBG(fsg, "reset interface\n");
2657 struct fsg_buffhd *bh = &fsg->buffhds[i];
2660 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2664 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2668 if (fsg->intreq) {
2669 usb_ep_free_request(fsg->intr_in, fsg->intreq);
2670 fsg->intreq = NULL;
2674 if (fsg->bulk_in_enabled) {
2675 usb_ep_disable(fsg->bulk_in);
2676 fsg->bulk_in_enabled = 0;
2678 if (fsg->bulk_out_enabled) {
2679 usb_ep_disable(fsg->bulk_out);
2680 fsg->bulk_out_enabled = 0;
2682 if (fsg->intr_in_enabled) {
2683 usb_ep_disable(fsg->intr_in);
2684 fsg->intr_in_enabled = 0;
2687 fsg->running = 0;
2691 DBG(fsg, "set interface %d\n", altsetting);
2694 d = fsg_ep_desc(fsg->gadget,
2696 if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
2698 fsg->bulk_in_enabled = 1;
2700 d = fsg_ep_desc(fsg->gadget,
2702 if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
2704 fsg->bulk_out_enabled = 1;
2705 fsg->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
2706 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2709 d = fsg_ep_desc(fsg->gadget,
2711 if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
2713 fsg->intr_in_enabled = 1;
2718 struct fsg_buffhd *bh = &fsg->buffhds[i];
2720 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
2722 if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
2730 if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
2732 fsg->intreq->complete = intr_in_complete;
2735 fsg->running = 1;
2736 for (i = 0; i < fsg->nluns; ++i)
2737 fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2750 static int do_set_config(struct fsg_dev *fsg, u8 new_config)
2755 if (fsg->config != 0) {
2756 DBG(fsg, "reset config\n");
2757 fsg->config = 0;
2758 rc = do_set_interface(fsg, -1);
2763 fsg->config = new_config;
2764 if ((rc = do_set_interface(fsg, 0)) != 0)
2765 fsg->config = 0; // Reset on errors
2769 switch (fsg->gadget->speed) {
2775 INFO(fsg, "%s speed config #%d\n", speed, fsg->config);
2784 static void handle_exception(struct fsg_dev *fsg)
2804 if (fsg->state < FSG_STATE_EXIT)
2805 DBG(fsg, "Main thread exiting on signal\n");
2806 raise_exception(fsg, FSG_STATE_EXIT);
2811 if (fsg->intreq_busy)
2812 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
2814 bh = &fsg->buffhds[i];
2816 usb_ep_dequeue(fsg->bulk_in, bh->inreq);
2818 usb_ep_dequeue(fsg->bulk_out, bh->outreq);
2823 num_active = fsg->intreq_busy;
2825 bh = &fsg->buffhds[i];
2830 if (sleep_thread(fsg))
2835 if (fsg->bulk_in_enabled)
2836 usb_ep_fifo_flush(fsg->bulk_in);
2837 if (fsg->bulk_out_enabled)
2838 usb_ep_fifo_flush(fsg->bulk_out);
2839 if (fsg->intr_in_enabled)
2840 usb_ep_fifo_flush(fsg->intr_in);
2844 spin_lock_irq(&fsg->lock);
2847 bh = &fsg->buffhds[i];
2850 fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
2851 &fsg->buffhds[0];
2853 exception_req_tag = fsg->exception_req_tag;
2854 new_config = fsg->new_config;
2855 old_state = fsg->state;
2858 fsg->state = FSG_STATE_STATUS_PHASE;
2860 for (i = 0; i < fsg->nluns; ++i) {
2861 curlun = &fsg->luns[i];
2868 fsg->state = FSG_STATE_IDLE;
2870 spin_unlock_irq(&fsg->lock);
2878 send_status(fsg);
2879 spin_lock_irq(&fsg->lock);
2880 if (fsg->state == FSG_STATE_STATUS_PHASE)
2881 fsg->state = FSG_STATE_IDLE;
2882 spin_unlock_irq(&fsg->lock);
2889 if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2890 usb_ep_clear_halt(fsg->bulk_in);
2893 if (fsg->ep0_req_tag == exception_req_tag)
2894 ep0_queue(fsg); // Complete the status stage
2897 send_status(fsg); // Status by interrupt pipe
2902 // for (i = 0; i < fsg->nluns; ++i)
2903 // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2907 rc = do_set_interface(fsg, 0);
2908 if (fsg->ep0_req_tag != exception_req_tag)
2911 fsg_set_halt(fsg, fsg->ep0);
2913 ep0_queue(fsg);
2917 rc = do_set_config(fsg, new_config);
2918 if (fsg->ep0_req_tag != exception_req_tag)
2921 fsg_set_halt(fsg, fsg->ep0);
2923 ep0_queue(fsg);
2927 for (i = 0; i < fsg->nluns; ++i)
2928 fsg_lun_fsync_sub(fsg->luns + i);
2929 do_set_config(fsg, 0); // Unconfigured state
2934 do_set_config(fsg, 0); // Free resources
2935 spin_lock_irq(&fsg->lock);
2936 fsg->state = FSG_STATE_TERMINATED; // Stop the thread
2937 spin_unlock_irq(&fsg->lock);
2947 struct fsg_dev *fsg = fsg_;
2965 while (fsg->state != FSG_STATE_TERMINATED) {
2966 if (exception_in_progress(fsg) || signal_pending(current)) {
2967 handle_exception(fsg);
2971 if (!fsg->running) {
2972 sleep_thread(fsg);
2976 if (get_next_command(fsg))
2979 spin_lock_irq(&fsg->lock);
2980 if (!exception_in_progress(fsg))
2981 fsg->state = FSG_STATE_DATA_PHASE;
2982 spin_unlock_irq(&fsg->lock);
2984 if (do_scsi_command(fsg) || finish_reply(fsg))
2987 spin_lock_irq(&fsg->lock);
2988 if (!exception_in_progress(fsg))
2989 fsg->state = FSG_STATE_STATUS_PHASE;
2990 spin_unlock_irq(&fsg->lock);
2992 if (send_status(fsg))
2995 spin_lock_irq(&fsg->lock);
2996 if (!exception_in_progress(fsg))
2997 fsg->state = FSG_STATE_IDLE;
2998 spin_unlock_irq(&fsg->lock);
3001 spin_lock_irq(&fsg->lock);
3002 fsg->thread_task = NULL;
3003 spin_unlock_irq(&fsg->lock);
3007 if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
3011 complete_and_exit(&fsg->thread_notifier, 0);
3028 struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
3030 kfree(fsg->luns);
3031 kfree(fsg);
3037 struct fsg_dev *fsg =
3040 kref_put(&fsg->ref, fsg_release);
3045 struct fsg_dev *fsg = get_gadget_data(gadget);
3048 struct usb_request *req = fsg->ep0req;
3050 DBG(fsg, "unbind\n");
3051 clear_bit(REGISTERED, &fsg->atomic_bitflags);
3054 for (i = 0; i < fsg->nluns; ++i) {
3055 curlun = &fsg->luns[i];
3066 if (fsg->state != FSG_STATE_TERMINATED) {
3067 raise_exception(fsg, FSG_STATE_EXIT);
3068 wait_for_completion(&fsg->thread_notifier);
3071 complete(&fsg->thread_notifier);
3076 kfree(fsg->buffhds[i].buf);
3081 usb_ep_free_request(fsg->ep0, req);
3088 static int __init check_parameters(struct fsg_dev *fsg)
3104 if (gadget_is_at91(fsg->gadget))
3108 gcnum = usb_gadget_controller_number(fsg->gadget);
3112 WARNING(fsg, "controller '%s' not recognized\n",
3113 fsg->gadget->name);
3130 ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
3159 ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
3165 ERROR(fsg, "invalid buflen\n");
3187 WARNING(fsg,
3197 WARNING(fsg,
3204 WARNING(fsg,
3228 struct fsg_dev *fsg = the_fsg;
3236 fsg->gadget = gadget;
3237 set_gadget_data(gadget, fsg);
3238 fsg->ep0 = gadget->ep0;
3239 fsg->ep0->driver_data = fsg;
3241 if ((rc = check_parameters(fsg)) != 0)
3262 ERROR(fsg, "invalid number of LUNs: %d\n", i);
3269 fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
3270 if (!fsg->luns) {
3274 fsg->nluns = i;
3276 for (i = 0; i < fsg->nluns; ++i) {
3277 curlun = &fsg->luns[i];
3286 dev_set_drvdata(&curlun->dev, &fsg->filesem);
3291 INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
3304 kref_get(&fsg->ref);
3311 ERROR(fsg, "no file given for LUN%d\n", i);
3322 ep->driver_data = fsg; // claim the endpoint
3323 fsg->bulk_in = ep;
3328 ep->driver_data = fsg; // claim the endpoint
3329 fsg->bulk_out = ep;
3335 ep->driver_data = fsg; // claim the endpoint
3336 fsg->intr_in = ep;
3340 device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
3355 dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
3372 fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
3382 struct fsg_buffhd *bh = &fsg->buffhds[i];
3392 fsg->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->buffhds[0];
3402 fsg->thread_task = kthread_create(fsg_main_thread, fsg,
3404 if (IS_ERR(fsg->thread_task)) {
3405 rc = PTR_ERR(fsg->thread_task);
3409 INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
3410 INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
3413 for (i = 0; i < fsg->nluns; ++i) {
3414 curlun = &fsg->luns[i];
3429 DBG(fsg, "transport=%s (x%02x)\n",
3431 DBG(fsg, "protocol=%s (x%02x)\n",
3433 DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
3435 DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
3438 DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
3440 set_bit(REGISTERED, &fsg->atomic_bitflags);
3443 wake_up_process(fsg->thread_task);
3447 ERROR(fsg, "unable to autoconfigure all endpoints\n");
3451 fsg->state = FSG_STATE_TERMINATED; // The thread is dead
3453 complete(&fsg->thread_notifier);
3462 struct fsg_dev *fsg = get_gadget_data(gadget);
3464 DBG(fsg, "suspend\n");
3465 set_bit(SUSPENDED, &fsg->atomic_bitflags);
3470 struct fsg_dev *fsg = get_gadget_data(gadget);
3472 DBG(fsg, "resume\n");
3473 clear_bit(SUSPENDED, &fsg->atomic_bitflags);
3505 struct fsg_dev *fsg;
3507 fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
3508 if (!fsg)
3510 spin_lock_init(&fsg->lock);
3511 init_rwsem(&fsg->filesem);
3512 kref_init(&fsg->ref);
3513 init_completion(&fsg->thread_notifier);
3515 the_fsg = fsg;
3523 struct fsg_dev *fsg;
3527 fsg = the_fsg;
3529 kref_put(&fsg->ref, fsg_release);
3537 struct fsg_dev *fsg = the_fsg;
3540 if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
3544 wait_for_completion(&fsg->thread_notifier);
3546 kref_put(&fsg->ref, fsg_release);