1 /* Target based USB-Gadget
3 * UAS protocol handling, target callbacks, configfs handling,
4 * BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling.
6 * Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de>
7 * License: GPLv2 as published by FSF.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/configfs.h>
14 #include <linux/ctype.h>
15 #include <linux/usb/ch9.h>
16 #include <linux/usb/composite.h>
17 #include <linux/usb/gadget.h>
18 #include <linux/usb/storage.h>
19 #include <scsi/scsi.h>
20 #include <scsi/scsi_tcq.h>
21 #include <target/target_core_base.h>
22 #include <target/target_core_fabric.h>
23 #include <target/target_core_fabric_configfs.h>
24 #include <target/configfs_macros.h>
25 #include <asm/unaligned.h>
27 #include "tcm_usb_gadget.h"
29 USB_GADGET_COMPOSITE_OPTIONS();
31 static const struct target_core_fabric_ops usbg_ops;
33 static inline struct f_uas *to_f_uas(struct usb_function *f)
35 return container_of(f, struct f_uas, function);
38 static void usbg_cmd_release(struct kref *);
40 static inline void usbg_cleanup_cmd(struct usbg_cmd *cmd)
42 kref_put(&cmd->ref, usbg_cmd_release);
45 /* Start bot.c code */
47 static int bot_enqueue_cmd_cbw(struct f_uas *fu)
51 if (fu->flags & USBG_BOT_CMD_PEND)
54 ret = usb_ep_queue(fu->ep_out, fu->cmd.req, GFP_ATOMIC);
56 fu->flags |= USBG_BOT_CMD_PEND;
60 static void bot_status_complete(struct usb_ep *ep, struct usb_request *req)
62 struct usbg_cmd *cmd = req->context;
63 struct f_uas *fu = cmd->fu;
65 usbg_cleanup_cmd(cmd);
66 if (req->status < 0) {
67 pr_err("ERR %s(%d)\n", __func__, __LINE__);
71 /* CSW completed, wait for next CBW */
72 bot_enqueue_cmd_cbw(fu);
75 static void bot_enqueue_sense_code(struct f_uas *fu, struct usbg_cmd *cmd)
77 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
80 unsigned int csw_stat;
82 csw_stat = cmd->csw_code;
85 * We can't send SENSE as a response. So we take ASC & ASCQ from our
86 * sense buffer and queue it and hope the host sends a REQUEST_SENSE
87 * command where it learns why we failed.
89 sense = cmd->sense_iu.sense;
91 csw->Tag = cmd->bot_tag;
92 csw->Status = csw_stat;
93 fu->bot_status.req->context = cmd;
94 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_ATOMIC);
96 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
99 static void bot_err_compl(struct usb_ep *ep, struct usb_request *req)
101 struct usbg_cmd *cmd = req->context;
102 struct f_uas *fu = cmd->fu;
105 pr_err("ERR %s(%d)\n", __func__, __LINE__);
108 if (cmd->data_len > ep->maxpacket) {
109 req->length = ep->maxpacket;
110 cmd->data_len -= ep->maxpacket;
112 req->length = cmd->data_len;
116 usb_ep_queue(ep, req, GFP_ATOMIC);
119 bot_enqueue_sense_code(fu, cmd);
122 static void bot_send_bad_status(struct usbg_cmd *cmd)
124 struct f_uas *fu = cmd->fu;
125 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
126 struct usb_request *req;
129 csw->Residue = cpu_to_le32(cmd->data_len);
134 req = fu->bot_req_in;
137 req = fu->bot_req_out;
140 if (cmd->data_len > fu->ep_in->maxpacket) {
141 req->length = ep->maxpacket;
142 cmd->data_len -= ep->maxpacket;
144 req->length = cmd->data_len;
147 req->complete = bot_err_compl;
149 req->buf = fu->cmd.buf;
150 usb_ep_queue(ep, req, GFP_KERNEL);
152 bot_enqueue_sense_code(fu, cmd);
156 static int bot_send_status(struct usbg_cmd *cmd, bool moved_data)
158 struct f_uas *fu = cmd->fu;
159 struct bulk_cs_wrap *csw = &fu->bot_status.csw;
162 if (cmd->se_cmd.scsi_status == SAM_STAT_GOOD) {
163 if (!moved_data && cmd->data_len) {
165 * the host wants to move data, we don't. Fill / empty
166 * the pipe and then send the csw with reside set.
168 cmd->csw_code = US_BULK_STAT_OK;
169 bot_send_bad_status(cmd);
173 csw->Tag = cmd->bot_tag;
174 csw->Residue = cpu_to_le32(0);
175 csw->Status = US_BULK_STAT_OK;
176 fu->bot_status.req->context = cmd;
178 ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_KERNEL);
180 pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
182 cmd->csw_code = US_BULK_STAT_FAIL;
183 bot_send_bad_status(cmd);
189 * Called after command (no data transfer) or after the write (to device)
190 * operation is completed
192 static int bot_send_status_response(struct usbg_cmd *cmd)
194 bool moved_data = false;
198 return bot_send_status(cmd, moved_data);
201 /* Read request completed, now we have to send the CSW */
202 static void bot_read_compl(struct usb_ep *ep, struct usb_request *req)
204 struct usbg_cmd *cmd = req->context;
207 pr_err("ERR %s(%d)\n", __func__, __LINE__);
209 bot_send_status(cmd, true);
212 static int bot_send_read_response(struct usbg_cmd *cmd)
214 struct f_uas *fu = cmd->fu;
215 struct se_cmd *se_cmd = &cmd->se_cmd;
216 struct usb_gadget *gadget = fuas_to_gadget(fu);
219 if (!cmd->data_len) {
220 cmd->csw_code = US_BULK_STAT_PHASE;
221 bot_send_bad_status(cmd);
225 if (!gadget->sg_supported) {
226 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
230 sg_copy_to_buffer(se_cmd->t_data_sg,
231 se_cmd->t_data_nents,
233 se_cmd->data_length);
235 fu->bot_req_in->buf = cmd->data_buf;
237 fu->bot_req_in->buf = NULL;
238 fu->bot_req_in->num_sgs = se_cmd->t_data_nents;
239 fu->bot_req_in->sg = se_cmd->t_data_sg;
242 fu->bot_req_in->complete = bot_read_compl;
243 fu->bot_req_in->length = se_cmd->data_length;
244 fu->bot_req_in->context = cmd;
245 ret = usb_ep_queue(fu->ep_in, fu->bot_req_in, GFP_ATOMIC);
247 pr_err("%s(%d)\n", __func__, __LINE__);
251 static void usbg_data_write_cmpl(struct usb_ep *, struct usb_request *);
252 static int usbg_prepare_w_request(struct usbg_cmd *, struct usb_request *);
254 static int bot_send_write_request(struct usbg_cmd *cmd)
256 struct f_uas *fu = cmd->fu;
257 struct se_cmd *se_cmd = &cmd->se_cmd;
258 struct usb_gadget *gadget = fuas_to_gadget(fu);
261 init_completion(&cmd->write_complete);
264 if (!cmd->data_len) {
265 cmd->csw_code = US_BULK_STAT_PHASE;
269 if (!gadget->sg_supported) {
270 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_KERNEL);
274 fu->bot_req_out->buf = cmd->data_buf;
276 fu->bot_req_out->buf = NULL;
277 fu->bot_req_out->num_sgs = se_cmd->t_data_nents;
278 fu->bot_req_out->sg = se_cmd->t_data_sg;
281 fu->bot_req_out->complete = usbg_data_write_cmpl;
282 fu->bot_req_out->length = se_cmd->data_length;
283 fu->bot_req_out->context = cmd;
285 ret = usbg_prepare_w_request(cmd, fu->bot_req_out);
288 ret = usb_ep_queue(fu->ep_out, fu->bot_req_out, GFP_KERNEL);
290 pr_err("%s(%d)\n", __func__, __LINE__);
292 wait_for_completion(&cmd->write_complete);
293 target_execute_cmd(se_cmd);
298 static int bot_submit_command(struct f_uas *, void *, unsigned int);
300 static void bot_cmd_complete(struct usb_ep *ep, struct usb_request *req)
302 struct f_uas *fu = req->context;
305 fu->flags &= ~USBG_BOT_CMD_PEND;
310 ret = bot_submit_command(fu, req->buf, req->actual);
312 pr_err("%s(%d): %d\n", __func__, __LINE__, ret);
315 static int bot_prepare_reqs(struct f_uas *fu)
319 fu->bot_req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
323 fu->bot_req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
324 if (!fu->bot_req_out)
327 fu->cmd.req = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
331 fu->bot_status.req = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
332 if (!fu->bot_status.req)
335 fu->bot_status.req->buf = &fu->bot_status.csw;
336 fu->bot_status.req->length = US_BULK_CS_WRAP_LEN;
337 fu->bot_status.req->complete = bot_status_complete;
338 fu->bot_status.csw.Signature = cpu_to_le32(US_BULK_CS_SIGN);
340 fu->cmd.buf = kmalloc(fu->ep_out->maxpacket, GFP_KERNEL);
344 fu->cmd.req->complete = bot_cmd_complete;
345 fu->cmd.req->buf = fu->cmd.buf;
346 fu->cmd.req->length = fu->ep_out->maxpacket;
347 fu->cmd.req->context = fu;
349 ret = bot_enqueue_cmd_cbw(fu);
357 usb_ep_free_request(fu->ep_in, fu->bot_status.req);
359 usb_ep_free_request(fu->ep_out, fu->cmd.req);
362 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
363 fu->bot_req_out = NULL;
365 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
366 fu->bot_req_in = NULL;
368 pr_err("BOT: endpoint setup failed\n");
372 static void bot_cleanup_old_alt(struct f_uas *fu)
374 if (!(fu->flags & USBG_ENABLED))
377 usb_ep_disable(fu->ep_in);
378 usb_ep_disable(fu->ep_out);
383 usb_ep_free_request(fu->ep_in, fu->bot_req_in);
384 usb_ep_free_request(fu->ep_out, fu->bot_req_out);
385 usb_ep_free_request(fu->ep_out, fu->cmd.req);
386 usb_ep_free_request(fu->ep_out, fu->bot_status.req);
390 fu->bot_req_in = NULL;
391 fu->bot_req_out = NULL;
393 fu->bot_status.req = NULL;
397 static void bot_set_alt(struct f_uas *fu)
399 struct usb_function *f = &fu->function;
400 struct usb_gadget *gadget = f->config->cdev->gadget;
403 fu->flags = USBG_IS_BOT;
405 config_ep_by_speed(gadget, f, fu->ep_in);
406 ret = usb_ep_enable(fu->ep_in);
410 config_ep_by_speed(gadget, f, fu->ep_out);
411 ret = usb_ep_enable(fu->ep_out);
415 ret = bot_prepare_reqs(fu);
418 fu->flags |= USBG_ENABLED;
419 pr_info("Using the BOT protocol\n");
422 usb_ep_disable(fu->ep_out);
424 usb_ep_disable(fu->ep_in);
426 fu->flags = USBG_IS_BOT;
429 static int usbg_bot_setup(struct usb_function *f,
430 const struct usb_ctrlrequest *ctrl)
432 struct f_uas *fu = to_f_uas(f);
433 struct usb_composite_dev *cdev = f->config->cdev;
434 u16 w_value = le16_to_cpu(ctrl->wValue);
435 u16 w_length = le16_to_cpu(ctrl->wLength);
439 switch (ctrl->bRequest) {
440 case US_BULK_GET_MAX_LUN:
441 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_CLASS |
442 USB_RECIP_INTERFACE))
449 luns = atomic_read(&fu->tpg->tpg_port_count);
451 pr_err("No LUNs configured?\n");
455 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
456 * accessed. The upper limit is 0xf
460 pr_info_once("Limiting the number of luns to 16\n");
463 ret_lun = cdev->req->buf;
465 cdev->req->length = 1;
466 return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
469 case US_BULK_RESET_REQUEST:
470 /* XXX maybe we should remove previous requests for IN + OUT */
471 bot_enqueue_cmd_cbw(fu);
478 /* Start uas.c code */
480 static void uasp_cleanup_one_stream(struct f_uas *fu, struct uas_stream *stream)
482 /* We have either all three allocated or none */
486 usb_ep_free_request(fu->ep_in, stream->req_in);
487 usb_ep_free_request(fu->ep_out, stream->req_out);
488 usb_ep_free_request(fu->ep_status, stream->req_status);
490 stream->req_in = NULL;
491 stream->req_out = NULL;
492 stream->req_status = NULL;
495 static void uasp_free_cmdreq(struct f_uas *fu)
497 usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
503 static void uasp_cleanup_old_alt(struct f_uas *fu)
507 if (!(fu->flags & USBG_ENABLED))
510 usb_ep_disable(fu->ep_in);
511 usb_ep_disable(fu->ep_out);
512 usb_ep_disable(fu->ep_status);
513 usb_ep_disable(fu->ep_cmd);
515 for (i = 0; i < UASP_SS_EP_COMP_NUM_STREAMS; i++)
516 uasp_cleanup_one_stream(fu, &fu->stream[i]);
517 uasp_free_cmdreq(fu);
520 static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req);
522 static int uasp_prepare_r_request(struct usbg_cmd *cmd)
524 struct se_cmd *se_cmd = &cmd->se_cmd;
525 struct f_uas *fu = cmd->fu;
526 struct usb_gadget *gadget = fuas_to_gadget(fu);
527 struct uas_stream *stream = cmd->stream;
529 if (!gadget->sg_supported) {
530 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
534 sg_copy_to_buffer(se_cmd->t_data_sg,
535 se_cmd->t_data_nents,
537 se_cmd->data_length);
539 stream->req_in->buf = cmd->data_buf;
541 stream->req_in->buf = NULL;
542 stream->req_in->num_sgs = se_cmd->t_data_nents;
543 stream->req_in->sg = se_cmd->t_data_sg;
546 stream->req_in->complete = uasp_status_data_cmpl;
547 stream->req_in->length = se_cmd->data_length;
548 stream->req_in->context = cmd;
550 cmd->state = UASP_SEND_STATUS;
554 static void uasp_prepare_status(struct usbg_cmd *cmd)
556 struct se_cmd *se_cmd = &cmd->se_cmd;
557 struct sense_iu *iu = &cmd->sense_iu;
558 struct uas_stream *stream = cmd->stream;
560 cmd->state = UASP_QUEUE_COMMAND;
561 iu->iu_id = IU_ID_STATUS;
562 iu->tag = cpu_to_be16(cmd->tag);
565 * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
567 iu->len = cpu_to_be16(se_cmd->scsi_sense_length);
568 iu->status = se_cmd->scsi_status;
569 stream->req_status->context = cmd;
570 stream->req_status->length = se_cmd->scsi_sense_length + 16;
571 stream->req_status->buf = iu;
572 stream->req_status->complete = uasp_status_data_cmpl;
575 static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req)
577 struct usbg_cmd *cmd = req->context;
578 struct uas_stream *stream = cmd->stream;
579 struct f_uas *fu = cmd->fu;
585 switch (cmd->state) {
587 ret = uasp_prepare_r_request(cmd);
590 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
592 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
595 case UASP_RECEIVE_DATA:
596 ret = usbg_prepare_w_request(cmd, stream->req_out);
599 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
601 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
604 case UASP_SEND_STATUS:
605 uasp_prepare_status(cmd);
606 ret = usb_ep_queue(fu->ep_status, stream->req_status,
609 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
612 case UASP_QUEUE_COMMAND:
613 usbg_cleanup_cmd(cmd);
614 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
623 usbg_cleanup_cmd(cmd);
626 static int uasp_send_status_response(struct usbg_cmd *cmd)
628 struct f_uas *fu = cmd->fu;
629 struct uas_stream *stream = cmd->stream;
630 struct sense_iu *iu = &cmd->sense_iu;
632 iu->tag = cpu_to_be16(cmd->tag);
633 stream->req_status->complete = uasp_status_data_cmpl;
634 stream->req_status->context = cmd;
636 uasp_prepare_status(cmd);
637 return usb_ep_queue(fu->ep_status, stream->req_status, GFP_ATOMIC);
640 static int uasp_send_read_response(struct usbg_cmd *cmd)
642 struct f_uas *fu = cmd->fu;
643 struct uas_stream *stream = cmd->stream;
644 struct sense_iu *iu = &cmd->sense_iu;
649 iu->tag = cpu_to_be16(cmd->tag);
650 if (fu->flags & USBG_USE_STREAMS) {
652 ret = uasp_prepare_r_request(cmd);
655 ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
657 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
658 kfree(cmd->data_buf);
659 cmd->data_buf = NULL;
664 iu->iu_id = IU_ID_READ_READY;
665 iu->tag = cpu_to_be16(cmd->tag);
667 stream->req_status->complete = uasp_status_data_cmpl;
668 stream->req_status->context = cmd;
670 cmd->state = UASP_SEND_DATA;
671 stream->req_status->buf = iu;
672 stream->req_status->length = sizeof(struct iu);
674 ret = usb_ep_queue(fu->ep_status, stream->req_status,
677 pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
683 static int uasp_send_write_request(struct usbg_cmd *cmd)
685 struct f_uas *fu = cmd->fu;
686 struct se_cmd *se_cmd = &cmd->se_cmd;
687 struct uas_stream *stream = cmd->stream;
688 struct sense_iu *iu = &cmd->sense_iu;
691 init_completion(&cmd->write_complete);
694 iu->tag = cpu_to_be16(cmd->tag);
696 if (fu->flags & USBG_USE_STREAMS) {
698 ret = usbg_prepare_w_request(cmd, stream->req_out);
701 ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
703 pr_err("%s(%d)\n", __func__, __LINE__);
707 iu->iu_id = IU_ID_WRITE_READY;
708 iu->tag = cpu_to_be16(cmd->tag);
710 stream->req_status->complete = uasp_status_data_cmpl;
711 stream->req_status->context = cmd;
713 cmd->state = UASP_RECEIVE_DATA;
714 stream->req_status->buf = iu;
715 stream->req_status->length = sizeof(struct iu);
717 ret = usb_ep_queue(fu->ep_status, stream->req_status,
720 pr_err("%s(%d)\n", __func__, __LINE__);
723 wait_for_completion(&cmd->write_complete);
724 target_execute_cmd(se_cmd);
729 static int usbg_submit_command(struct f_uas *, void *, unsigned int);
731 static void uasp_cmd_complete(struct usb_ep *ep, struct usb_request *req)
733 struct f_uas *fu = req->context;
739 ret = usbg_submit_command(fu, req->buf, req->actual);
741 * Once we tune for performance enqueue the command req here again so
742 * we can receive a second command while we processing this one. Pay
743 * attention to properly sync STAUS endpoint with DATA IN + OUT so you
748 usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
751 static int uasp_alloc_stream_res(struct f_uas *fu, struct uas_stream *stream)
753 stream->req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
757 stream->req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
758 if (!stream->req_out)
761 stream->req_status = usb_ep_alloc_request(fu->ep_status, GFP_KERNEL);
762 if (!stream->req_status)
767 usb_ep_free_request(fu->ep_status, stream->req_status);
768 stream->req_status = NULL;
770 usb_ep_free_request(fu->ep_out, stream->req_out);
771 stream->req_out = NULL;
776 static int uasp_alloc_cmd(struct f_uas *fu)
778 fu->cmd.req = usb_ep_alloc_request(fu->ep_cmd, GFP_KERNEL);
782 fu->cmd.buf = kmalloc(fu->ep_cmd->maxpacket, GFP_KERNEL);
786 fu->cmd.req->complete = uasp_cmd_complete;
787 fu->cmd.req->buf = fu->cmd.buf;
788 fu->cmd.req->length = fu->ep_cmd->maxpacket;
789 fu->cmd.req->context = fu;
793 usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
798 static void uasp_setup_stream_res(struct f_uas *fu, int max_streams)
802 for (i = 0; i < max_streams; i++) {
803 struct uas_stream *s = &fu->stream[i];
805 s->req_in->stream_id = i + 1;
806 s->req_out->stream_id = i + 1;
807 s->req_status->stream_id = i + 1;
811 static int uasp_prepare_reqs(struct f_uas *fu)
817 if (fu->flags & USBG_USE_STREAMS)
818 max_streams = UASP_SS_EP_COMP_NUM_STREAMS;
822 for (i = 0; i < max_streams; i++) {
823 ret = uasp_alloc_stream_res(fu, &fu->stream[i]);
828 ret = uasp_alloc_cmd(fu);
830 goto err_free_stream;
831 uasp_setup_stream_res(fu, max_streams);
833 ret = usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
835 goto err_free_stream;
840 uasp_free_cmdreq(fu);
845 uasp_cleanup_one_stream(fu, &fu->stream[i - 1]);
849 pr_err("UASP: endpoint setup failed\n");
853 static void uasp_set_alt(struct f_uas *fu)
855 struct usb_function *f = &fu->function;
856 struct usb_gadget *gadget = f->config->cdev->gadget;
859 fu->flags = USBG_IS_UAS;
861 if (gadget->speed == USB_SPEED_SUPER)
862 fu->flags |= USBG_USE_STREAMS;
864 config_ep_by_speed(gadget, f, fu->ep_in);
865 ret = usb_ep_enable(fu->ep_in);
869 config_ep_by_speed(gadget, f, fu->ep_out);
870 ret = usb_ep_enable(fu->ep_out);
874 config_ep_by_speed(gadget, f, fu->ep_cmd);
875 ret = usb_ep_enable(fu->ep_cmd);
878 config_ep_by_speed(gadget, f, fu->ep_status);
879 ret = usb_ep_enable(fu->ep_status);
883 ret = uasp_prepare_reqs(fu);
886 fu->flags |= USBG_ENABLED;
888 pr_info("Using the UAS protocol\n");
891 usb_ep_disable(fu->ep_status);
893 usb_ep_disable(fu->ep_cmd);
895 usb_ep_disable(fu->ep_out);
897 usb_ep_disable(fu->ep_in);
902 static int get_cmd_dir(const unsigned char *cdb)
914 case SERVICE_ACTION_IN_16:
916 case PERSISTENT_RESERVE_IN:
917 case SECURITY_PROTOCOL_IN:
918 case ACCESS_CONTROL_IN:
920 case READ_BLOCK_LIMITS:
924 case READ_FORMAT_CAPACITIES:
926 ret = DMA_FROM_DEVICE;
936 case WRITE_VERIFY_12:
937 case PERSISTENT_RESERVE_OUT:
938 case MAINTENANCE_OUT:
939 case SECURITY_PROTOCOL_OUT:
940 case ACCESS_CONTROL_OUT:
943 case ALLOW_MEDIUM_REMOVAL:
944 case TEST_UNIT_READY:
945 case SYNCHRONIZE_CACHE:
952 case WRITE_FILEMARKS:
956 pr_warn("target: Unknown data direction for SCSI Opcode "
963 static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req)
965 struct usbg_cmd *cmd = req->context;
966 struct se_cmd *se_cmd = &cmd->se_cmd;
968 if (req->status < 0) {
969 pr_err("%s() state %d transfer failed\n", __func__, cmd->state);
973 if (req->num_sgs == 0) {
974 sg_copy_from_buffer(se_cmd->t_data_sg,
975 se_cmd->t_data_nents,
977 se_cmd->data_length);
980 complete(&cmd->write_complete);
984 usbg_cleanup_cmd(cmd);
987 static int usbg_prepare_w_request(struct usbg_cmd *cmd, struct usb_request *req)
989 struct se_cmd *se_cmd = &cmd->se_cmd;
990 struct f_uas *fu = cmd->fu;
991 struct usb_gadget *gadget = fuas_to_gadget(fu);
993 if (!gadget->sg_supported) {
994 cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
998 req->buf = cmd->data_buf;
1001 req->num_sgs = se_cmd->t_data_nents;
1002 req->sg = se_cmd->t_data_sg;
1005 req->complete = usbg_data_write_cmpl;
1006 req->length = se_cmd->data_length;
1011 static int usbg_send_status_response(struct se_cmd *se_cmd)
1013 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1015 struct f_uas *fu = cmd->fu;
1017 if (fu->flags & USBG_IS_BOT)
1018 return bot_send_status_response(cmd);
1020 return uasp_send_status_response(cmd);
1023 static int usbg_send_write_request(struct se_cmd *se_cmd)
1025 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1027 struct f_uas *fu = cmd->fu;
1029 if (fu->flags & USBG_IS_BOT)
1030 return bot_send_write_request(cmd);
1032 return uasp_send_write_request(cmd);
1035 static int usbg_send_read_response(struct se_cmd *se_cmd)
1037 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1039 struct f_uas *fu = cmd->fu;
1041 if (fu->flags & USBG_IS_BOT)
1042 return bot_send_read_response(cmd);
1044 return uasp_send_read_response(cmd);
1047 static void usbg_cmd_work(struct work_struct *work)
1049 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1050 struct se_cmd *se_cmd;
1051 struct tcm_usbg_nexus *tv_nexus;
1052 struct usbg_tpg *tpg;
1055 se_cmd = &cmd->se_cmd;
1057 tv_nexus = tpg->tpg_nexus;
1058 dir = get_cmd_dir(cmd->cmd_buf);
1060 transport_init_se_cmd(se_cmd,
1061 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1062 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1063 cmd->prio_attr, cmd->sense_iu.sense);
1067 if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
1068 cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
1069 0, cmd->prio_attr, dir, TARGET_SCF_UNKNOWN_SIZE) < 0)
1075 transport_send_check_condition_and_sense(se_cmd,
1076 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1077 usbg_cleanup_cmd(cmd);
1080 static int usbg_submit_command(struct f_uas *fu,
1081 void *cmdbuf, unsigned int len)
1083 struct command_iu *cmd_iu = cmdbuf;
1084 struct usbg_cmd *cmd;
1085 struct usbg_tpg *tpg;
1086 struct se_cmd *se_cmd;
1087 struct tcm_usbg_nexus *tv_nexus;
1091 if (cmd_iu->iu_id != IU_ID_COMMAND) {
1092 pr_err("Unsupported type %d\n", cmd_iu->iu_id);
1096 cmd = kzalloc(sizeof *cmd, GFP_ATOMIC);
1102 /* XXX until I figure out why I can't free in on complete */
1103 kref_init(&cmd->ref);
1104 kref_get(&cmd->ref);
1107 cmd_len = (cmd_iu->len & ~0x3) + 16;
1108 if (cmd_len > USBG_MAX_CMD)
1111 memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len);
1113 cmd->tag = be16_to_cpup(&cmd_iu->tag);
1114 cmd->se_cmd.tag = cmd->tag;
1115 if (fu->flags & USBG_USE_STREAMS) {
1116 if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS)
1119 cmd->stream = &fu->stream[0];
1121 cmd->stream = &fu->stream[cmd->tag - 1];
1123 cmd->stream = &fu->stream[0];
1126 tv_nexus = tpg->tpg_nexus;
1128 pr_err("Missing nexus, ignoring command\n");
1132 switch (cmd_iu->prio_attr & 0x7) {
1134 cmd->prio_attr = TCM_HEAD_TAG;
1136 case UAS_ORDERED_TAG:
1137 cmd->prio_attr = TCM_ORDERED_TAG;
1140 cmd->prio_attr = TCM_ACA_TAG;
1143 pr_debug_once("Unsupported prio_attr: %02x.\n",
1145 case UAS_SIMPLE_TAG:
1146 cmd->prio_attr = TCM_SIMPLE_TAG;
1150 se_cmd = &cmd->se_cmd;
1151 cmd->unpacked_lun = scsilun_to_int(&cmd_iu->lun);
1153 INIT_WORK(&cmd->work, usbg_cmd_work);
1154 ret = queue_work(tpg->workqueue, &cmd->work);
1164 static void bot_cmd_work(struct work_struct *work)
1166 struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
1167 struct se_cmd *se_cmd;
1168 struct tcm_usbg_nexus *tv_nexus;
1169 struct usbg_tpg *tpg;
1172 se_cmd = &cmd->se_cmd;
1174 tv_nexus = tpg->tpg_nexus;
1175 dir = get_cmd_dir(cmd->cmd_buf);
1177 transport_init_se_cmd(se_cmd,
1178 tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
1179 tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
1180 cmd->prio_attr, cmd->sense_iu.sense);
1184 if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
1185 cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
1186 cmd->data_len, cmd->prio_attr, dir, 0) < 0)
1192 transport_send_check_condition_and_sense(se_cmd,
1193 TCM_UNSUPPORTED_SCSI_OPCODE, 1);
1194 usbg_cleanup_cmd(cmd);
1197 static int bot_submit_command(struct f_uas *fu,
1198 void *cmdbuf, unsigned int len)
1200 struct bulk_cb_wrap *cbw = cmdbuf;
1201 struct usbg_cmd *cmd;
1202 struct usbg_tpg *tpg;
1203 struct se_cmd *se_cmd;
1204 struct tcm_usbg_nexus *tv_nexus;
1208 if (cbw->Signature != cpu_to_le32(US_BULK_CB_SIGN)) {
1209 pr_err("Wrong signature on CBW\n");
1213 pr_err("Wrong length for CBW\n");
1217 cmd_len = cbw->Length;
1218 if (cmd_len < 1 || cmd_len > 16)
1221 cmd = kzalloc(sizeof *cmd, GFP_ATOMIC);
1227 /* XXX until I figure out why I can't free in on complete */
1228 kref_init(&cmd->ref);
1229 kref_get(&cmd->ref);
1233 memcpy(cmd->cmd_buf, cbw->CDB, cmd_len);
1235 cmd->bot_tag = cbw->Tag;
1237 tv_nexus = tpg->tpg_nexus;
1239 pr_err("Missing nexus, ignoring command\n");
1243 cmd->prio_attr = TCM_SIMPLE_TAG;
1244 se_cmd = &cmd->se_cmd;
1245 cmd->unpacked_lun = cbw->Lun;
1246 cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0;
1247 cmd->data_len = le32_to_cpu(cbw->DataTransferLength);
1248 cmd->se_cmd.tag = le32_to_cpu(cmd->bot_tag);
1250 INIT_WORK(&cmd->work, bot_cmd_work);
1251 ret = queue_work(tpg->workqueue, &cmd->work);
1261 /* Start fabric.c code */
1263 static int usbg_check_true(struct se_portal_group *se_tpg)
1268 static int usbg_check_false(struct se_portal_group *se_tpg)
1273 static char *usbg_get_fabric_name(void)
1275 return "usb_gadget";
1278 static char *usbg_get_fabric_wwn(struct se_portal_group *se_tpg)
1280 struct usbg_tpg *tpg = container_of(se_tpg,
1281 struct usbg_tpg, se_tpg);
1282 struct usbg_tport *tport = tpg->tport;
1284 return &tport->tport_name[0];
1287 static u16 usbg_get_tag(struct se_portal_group *se_tpg)
1289 struct usbg_tpg *tpg = container_of(se_tpg,
1290 struct usbg_tpg, se_tpg);
1291 return tpg->tport_tpgt;
1294 static u32 usbg_tpg_get_inst_index(struct se_portal_group *se_tpg)
1299 static void usbg_cmd_release(struct kref *ref)
1301 struct usbg_cmd *cmd = container_of(ref, struct usbg_cmd,
1304 transport_generic_free_cmd(&cmd->se_cmd, 0);
1307 static void usbg_release_cmd(struct se_cmd *se_cmd)
1309 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1311 kfree(cmd->data_buf);
1316 static int usbg_shutdown_session(struct se_session *se_sess)
1321 static void usbg_close_session(struct se_session *se_sess)
1326 static u32 usbg_sess_get_index(struct se_session *se_sess)
1332 * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
1334 static int usbg_write_pending_status(struct se_cmd *se_cmd)
1339 static void usbg_set_default_node_attrs(struct se_node_acl *nacl)
1344 static int usbg_get_cmd_state(struct se_cmd *se_cmd)
1349 static void usbg_queue_tm_rsp(struct se_cmd *se_cmd)
1353 static void usbg_aborted_task(struct se_cmd *se_cmd)
1358 static const char *usbg_check_wwn(const char *name)
1363 n = strstr(name, "naa.");
1368 if (len == 0 || len > USBG_NAMELEN - 1)
1373 static int usbg_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
1375 if (!usbg_check_wwn(name))
1380 struct usbg_tpg *the_only_tpg_I_currently_have;
1382 static struct se_portal_group *usbg_make_tpg(
1384 struct config_group *group,
1387 struct usbg_tport *tport = container_of(wwn, struct usbg_tport,
1389 struct usbg_tpg *tpg;
1393 if (strstr(name, "tpgt_") != name)
1394 return ERR_PTR(-EINVAL);
1395 if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX)
1396 return ERR_PTR(-EINVAL);
1397 if (the_only_tpg_I_currently_have) {
1398 pr_err("Until the gadget framework can't handle multiple\n");
1399 pr_err("gadgets, you can't do this here.\n");
1400 return ERR_PTR(-EBUSY);
1403 tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL);
1405 return ERR_PTR(-ENOMEM);
1406 mutex_init(&tpg->tpg_mutex);
1407 atomic_set(&tpg->tpg_port_count, 0);
1408 tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1);
1409 if (!tpg->workqueue) {
1415 tpg->tport_tpgt = tpgt;
1418 * SPC doesn't assign a protocol identifier for USB-SCSI, so we
1419 * pretend to be SAS..
1421 ret = core_tpg_register(&usbg_ops, wwn, &tpg->se_tpg,
1424 destroy_workqueue(tpg->workqueue);
1428 the_only_tpg_I_currently_have = tpg;
1429 return &tpg->se_tpg;
1432 static void usbg_drop_tpg(struct se_portal_group *se_tpg)
1434 struct usbg_tpg *tpg = container_of(se_tpg,
1435 struct usbg_tpg, se_tpg);
1437 core_tpg_deregister(se_tpg);
1438 destroy_workqueue(tpg->workqueue);
1440 the_only_tpg_I_currently_have = NULL;
1443 static struct se_wwn *usbg_make_tport(
1444 struct target_fabric_configfs *tf,
1445 struct config_group *group,
1448 struct usbg_tport *tport;
1449 const char *wnn_name;
1452 wnn_name = usbg_check_wwn(name);
1454 return ERR_PTR(-EINVAL);
1456 tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL);
1458 return ERR_PTR(-ENOMEM);
1459 tport->tport_wwpn = wwpn;
1460 snprintf(tport->tport_name, sizeof(tport->tport_name), "%s", wnn_name);
1461 return &tport->tport_wwn;
1464 static void usbg_drop_tport(struct se_wwn *wwn)
1466 struct usbg_tport *tport = container_of(wwn,
1467 struct usbg_tport, tport_wwn);
1472 * If somebody feels like dropping the version property, go ahead.
1474 static ssize_t usbg_wwn_show_attr_version(
1475 struct target_fabric_configfs *tf,
1478 return sprintf(page, "usb-gadget fabric module\n");
1480 TF_WWN_ATTR_RO(usbg, version);
1482 static struct configfs_attribute *usbg_wwn_attrs[] = {
1483 &usbg_wwn_version.attr,
1487 static ssize_t tcm_usbg_tpg_show_enable(
1488 struct se_portal_group *se_tpg,
1491 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1493 return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect);
1496 static int usbg_attach(struct usbg_tpg *);
1497 static void usbg_detach(struct usbg_tpg *);
1499 static ssize_t tcm_usbg_tpg_store_enable(
1500 struct se_portal_group *se_tpg,
1504 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1508 ret = kstrtoul(page, 0, &op);
1514 if (op && tpg->gadget_connect)
1516 if (!op && !tpg->gadget_connect)
1520 ret = usbg_attach(tpg);
1526 tpg->gadget_connect = op;
1530 TF_TPG_BASE_ATTR(tcm_usbg, enable, S_IRUGO | S_IWUSR);
1532 static ssize_t tcm_usbg_tpg_show_nexus(
1533 struct se_portal_group *se_tpg,
1536 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1537 struct tcm_usbg_nexus *tv_nexus;
1540 mutex_lock(&tpg->tpg_mutex);
1541 tv_nexus = tpg->tpg_nexus;
1546 ret = snprintf(page, PAGE_SIZE, "%s\n",
1547 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1549 mutex_unlock(&tpg->tpg_mutex);
1553 static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
1555 struct se_portal_group *se_tpg;
1556 struct tcm_usbg_nexus *tv_nexus;
1559 mutex_lock(&tpg->tpg_mutex);
1560 if (tpg->tpg_nexus) {
1562 pr_debug("tpg->tpg_nexus already exists\n");
1565 se_tpg = &tpg->se_tpg;
1568 tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
1571 tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
1572 if (IS_ERR(tv_nexus->tvn_se_sess))
1576 * Since we are running in 'demo mode' this call with generate a
1577 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1578 * the SCSI Initiator port name of the passed configfs group 'name'.
1580 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1582 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1583 pr_debug("core_tpg_check_initiator_node_acl() failed"
1588 * Now register the TCM vHost virtual I_T Nexus as active.
1590 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1591 tv_nexus->tvn_se_sess, tv_nexus);
1592 tpg->tpg_nexus = tv_nexus;
1593 mutex_unlock(&tpg->tpg_mutex);
1597 transport_free_session(tv_nexus->tvn_se_sess);
1601 mutex_unlock(&tpg->tpg_mutex);
1605 static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg)
1607 struct se_session *se_sess;
1608 struct tcm_usbg_nexus *tv_nexus;
1611 mutex_lock(&tpg->tpg_mutex);
1612 tv_nexus = tpg->tpg_nexus;
1616 se_sess = tv_nexus->tvn_se_sess;
1620 if (atomic_read(&tpg->tpg_port_count)) {
1622 pr_err("Unable to remove Host I_T Nexus with"
1623 " active TPG port count: %d\n",
1624 atomic_read(&tpg->tpg_port_count));
1628 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1629 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1631 * Release the SCSI I_T Nexus to the emulated vHost Target Port
1633 transport_deregister_session(tv_nexus->tvn_se_sess);
1634 tpg->tpg_nexus = NULL;
1639 mutex_unlock(&tpg->tpg_mutex);
1643 static ssize_t tcm_usbg_tpg_store_nexus(
1644 struct se_portal_group *se_tpg,
1648 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1649 unsigned char i_port[USBG_NAMELEN], *ptr;
1652 if (!strncmp(page, "NULL", 4)) {
1653 ret = tcm_usbg_drop_nexus(tpg);
1654 return (!ret) ? count : ret;
1656 if (strlen(page) >= USBG_NAMELEN) {
1657 pr_err("Emulated NAA Sas Address: %s, exceeds"
1658 " max: %d\n", page, USBG_NAMELEN);
1661 snprintf(i_port, USBG_NAMELEN, "%s", page);
1663 ptr = strstr(i_port, "naa.");
1665 pr_err("Missing 'naa.' prefix\n");
1669 if (i_port[strlen(i_port) - 1] == '\n')
1670 i_port[strlen(i_port) - 1] = '\0';
1672 ret = tcm_usbg_make_nexus(tpg, &i_port[4]);
1677 TF_TPG_BASE_ATTR(tcm_usbg, nexus, S_IRUGO | S_IWUSR);
1679 static struct configfs_attribute *usbg_base_attrs[] = {
1680 &tcm_usbg_tpg_enable.attr,
1681 &tcm_usbg_tpg_nexus.attr,
1685 static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun)
1687 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1689 atomic_inc(&tpg->tpg_port_count);
1690 smp_mb__after_atomic();
1694 static void usbg_port_unlink(struct se_portal_group *se_tpg,
1695 struct se_lun *se_lun)
1697 struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
1699 atomic_dec(&tpg->tpg_port_count);
1700 smp_mb__after_atomic();
1703 static int usbg_check_stop_free(struct se_cmd *se_cmd)
1705 struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
1708 kref_put(&cmd->ref, usbg_cmd_release);
1712 static const struct target_core_fabric_ops usbg_ops = {
1713 .module = THIS_MODULE,
1714 .name = "usb_gadget",
1715 .get_fabric_name = usbg_get_fabric_name,
1716 .tpg_get_wwn = usbg_get_fabric_wwn,
1717 .tpg_get_tag = usbg_get_tag,
1718 .tpg_check_demo_mode = usbg_check_true,
1719 .tpg_check_demo_mode_cache = usbg_check_false,
1720 .tpg_check_demo_mode_write_protect = usbg_check_false,
1721 .tpg_check_prod_mode_write_protect = usbg_check_false,
1722 .tpg_get_inst_index = usbg_tpg_get_inst_index,
1723 .release_cmd = usbg_release_cmd,
1724 .shutdown_session = usbg_shutdown_session,
1725 .close_session = usbg_close_session,
1726 .sess_get_index = usbg_sess_get_index,
1727 .sess_get_initiator_sid = NULL,
1728 .write_pending = usbg_send_write_request,
1729 .write_pending_status = usbg_write_pending_status,
1730 .set_default_node_attributes = usbg_set_default_node_attrs,
1731 .get_cmd_state = usbg_get_cmd_state,
1732 .queue_data_in = usbg_send_read_response,
1733 .queue_status = usbg_send_status_response,
1734 .queue_tm_rsp = usbg_queue_tm_rsp,
1735 .aborted_task = usbg_aborted_task,
1736 .check_stop_free = usbg_check_stop_free,
1738 .fabric_make_wwn = usbg_make_tport,
1739 .fabric_drop_wwn = usbg_drop_tport,
1740 .fabric_make_tpg = usbg_make_tpg,
1741 .fabric_drop_tpg = usbg_drop_tpg,
1742 .fabric_post_link = usbg_port_link,
1743 .fabric_pre_unlink = usbg_port_unlink,
1744 .fabric_init_nodeacl = usbg_init_nodeacl,
1746 .tfc_wwn_attrs = usbg_wwn_attrs,
1747 .tfc_tpg_base_attrs = usbg_base_attrs,
1750 /* Start gadget.c code */
1752 static struct usb_interface_descriptor bot_intf_desc = {
1753 .bLength = sizeof(bot_intf_desc),
1754 .bDescriptorType = USB_DT_INTERFACE,
1756 .bAlternateSetting = USB_G_ALT_INT_BBB,
1757 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1758 .bInterfaceSubClass = USB_SC_SCSI,
1759 .bInterfaceProtocol = USB_PR_BULK,
1762 static struct usb_interface_descriptor uasp_intf_desc = {
1763 .bLength = sizeof(uasp_intf_desc),
1764 .bDescriptorType = USB_DT_INTERFACE,
1766 .bAlternateSetting = USB_G_ALT_INT_UAS,
1767 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
1768 .bInterfaceSubClass = USB_SC_SCSI,
1769 .bInterfaceProtocol = USB_PR_UAS,
1772 static struct usb_endpoint_descriptor uasp_bi_desc = {
1773 .bLength = USB_DT_ENDPOINT_SIZE,
1774 .bDescriptorType = USB_DT_ENDPOINT,
1775 .bEndpointAddress = USB_DIR_IN,
1776 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1777 .wMaxPacketSize = cpu_to_le16(512),
1780 static struct usb_endpoint_descriptor uasp_fs_bi_desc = {
1781 .bLength = USB_DT_ENDPOINT_SIZE,
1782 .bDescriptorType = USB_DT_ENDPOINT,
1783 .bEndpointAddress = USB_DIR_IN,
1784 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1787 static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = {
1788 .bLength = sizeof(uasp_bi_pipe_desc),
1789 .bDescriptorType = USB_DT_PIPE_USAGE,
1790 .bPipeID = DATA_IN_PIPE_ID,
1793 static struct usb_endpoint_descriptor uasp_ss_bi_desc = {
1794 .bLength = USB_DT_ENDPOINT_SIZE,
1795 .bDescriptorType = USB_DT_ENDPOINT,
1796 .bEndpointAddress = USB_DIR_IN,
1797 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1798 .wMaxPacketSize = cpu_to_le16(1024),
1801 static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = {
1802 .bLength = sizeof(uasp_bi_ep_comp_desc),
1803 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1805 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1806 .wBytesPerInterval = 0,
1809 static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = {
1810 .bLength = sizeof(bot_bi_ep_comp_desc),
1811 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1815 static struct usb_endpoint_descriptor uasp_bo_desc = {
1816 .bLength = USB_DT_ENDPOINT_SIZE,
1817 .bDescriptorType = USB_DT_ENDPOINT,
1818 .bEndpointAddress = USB_DIR_OUT,
1819 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1820 .wMaxPacketSize = cpu_to_le16(512),
1823 static struct usb_endpoint_descriptor uasp_fs_bo_desc = {
1824 .bLength = USB_DT_ENDPOINT_SIZE,
1825 .bDescriptorType = USB_DT_ENDPOINT,
1826 .bEndpointAddress = USB_DIR_OUT,
1827 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1830 static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = {
1831 .bLength = sizeof(uasp_bo_pipe_desc),
1832 .bDescriptorType = USB_DT_PIPE_USAGE,
1833 .bPipeID = DATA_OUT_PIPE_ID,
1836 static struct usb_endpoint_descriptor uasp_ss_bo_desc = {
1837 .bLength = USB_DT_ENDPOINT_SIZE,
1838 .bDescriptorType = USB_DT_ENDPOINT,
1839 .bEndpointAddress = USB_DIR_OUT,
1840 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1841 .wMaxPacketSize = cpu_to_le16(0x400),
1844 static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = {
1845 .bLength = sizeof(uasp_bo_ep_comp_desc),
1846 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1847 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1850 static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = {
1851 .bLength = sizeof(bot_bo_ep_comp_desc),
1852 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1855 static struct usb_endpoint_descriptor uasp_status_desc = {
1856 .bLength = USB_DT_ENDPOINT_SIZE,
1857 .bDescriptorType = USB_DT_ENDPOINT,
1858 .bEndpointAddress = USB_DIR_IN,
1859 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1860 .wMaxPacketSize = cpu_to_le16(512),
1863 static struct usb_endpoint_descriptor uasp_fs_status_desc = {
1864 .bLength = USB_DT_ENDPOINT_SIZE,
1865 .bDescriptorType = USB_DT_ENDPOINT,
1866 .bEndpointAddress = USB_DIR_IN,
1867 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1870 static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = {
1871 .bLength = sizeof(uasp_status_pipe_desc),
1872 .bDescriptorType = USB_DT_PIPE_USAGE,
1873 .bPipeID = STATUS_PIPE_ID,
1876 static struct usb_endpoint_descriptor uasp_ss_status_desc = {
1877 .bLength = USB_DT_ENDPOINT_SIZE,
1878 .bDescriptorType = USB_DT_ENDPOINT,
1879 .bEndpointAddress = USB_DIR_IN,
1880 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1881 .wMaxPacketSize = cpu_to_le16(1024),
1884 static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = {
1885 .bLength = sizeof(uasp_status_in_ep_comp_desc),
1886 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1887 .bmAttributes = UASP_SS_EP_COMP_LOG_STREAMS,
1890 static struct usb_endpoint_descriptor uasp_cmd_desc = {
1891 .bLength = USB_DT_ENDPOINT_SIZE,
1892 .bDescriptorType = USB_DT_ENDPOINT,
1893 .bEndpointAddress = USB_DIR_OUT,
1894 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1895 .wMaxPacketSize = cpu_to_le16(512),
1898 static struct usb_endpoint_descriptor uasp_fs_cmd_desc = {
1899 .bLength = USB_DT_ENDPOINT_SIZE,
1900 .bDescriptorType = USB_DT_ENDPOINT,
1901 .bEndpointAddress = USB_DIR_OUT,
1902 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1905 static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = {
1906 .bLength = sizeof(uasp_cmd_pipe_desc),
1907 .bDescriptorType = USB_DT_PIPE_USAGE,
1908 .bPipeID = CMD_PIPE_ID,
1911 static struct usb_endpoint_descriptor uasp_ss_cmd_desc = {
1912 .bLength = USB_DT_ENDPOINT_SIZE,
1913 .bDescriptorType = USB_DT_ENDPOINT,
1914 .bEndpointAddress = USB_DIR_OUT,
1915 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1916 .wMaxPacketSize = cpu_to_le16(1024),
1919 static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = {
1920 .bLength = sizeof(uasp_cmd_comp_desc),
1921 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
1924 static struct usb_descriptor_header *uasp_fs_function_desc[] = {
1925 (struct usb_descriptor_header *) &bot_intf_desc,
1926 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
1927 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
1929 (struct usb_descriptor_header *) &uasp_intf_desc,
1930 (struct usb_descriptor_header *) &uasp_fs_bi_desc,
1931 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1932 (struct usb_descriptor_header *) &uasp_fs_bo_desc,
1933 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1934 (struct usb_descriptor_header *) &uasp_fs_status_desc,
1935 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1936 (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
1937 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1941 static struct usb_descriptor_header *uasp_hs_function_desc[] = {
1942 (struct usb_descriptor_header *) &bot_intf_desc,
1943 (struct usb_descriptor_header *) &uasp_bi_desc,
1944 (struct usb_descriptor_header *) &uasp_bo_desc,
1946 (struct usb_descriptor_header *) &uasp_intf_desc,
1947 (struct usb_descriptor_header *) &uasp_bi_desc,
1948 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1949 (struct usb_descriptor_header *) &uasp_bo_desc,
1950 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1951 (struct usb_descriptor_header *) &uasp_status_desc,
1952 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1953 (struct usb_descriptor_header *) &uasp_cmd_desc,
1954 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1958 static struct usb_descriptor_header *uasp_ss_function_desc[] = {
1959 (struct usb_descriptor_header *) &bot_intf_desc,
1960 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
1961 (struct usb_descriptor_header *) &bot_bi_ep_comp_desc,
1962 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
1963 (struct usb_descriptor_header *) &bot_bo_ep_comp_desc,
1965 (struct usb_descriptor_header *) &uasp_intf_desc,
1966 (struct usb_descriptor_header *) &uasp_ss_bi_desc,
1967 (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc,
1968 (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
1969 (struct usb_descriptor_header *) &uasp_ss_bo_desc,
1970 (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc,
1971 (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
1972 (struct usb_descriptor_header *) &uasp_ss_status_desc,
1973 (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc,
1974 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
1975 (struct usb_descriptor_header *) &uasp_ss_cmd_desc,
1976 (struct usb_descriptor_header *) &uasp_cmd_comp_desc,
1977 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
1981 #define UAS_VENDOR_ID 0x0525 /* NetChip */
1982 #define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
1984 static struct usb_device_descriptor usbg_device_desc = {
1985 .bLength = sizeof(usbg_device_desc),
1986 .bDescriptorType = USB_DT_DEVICE,
1987 .bcdUSB = cpu_to_le16(0x0200),
1988 .bDeviceClass = USB_CLASS_PER_INTERFACE,
1989 .idVendor = cpu_to_le16(UAS_VENDOR_ID),
1990 .idProduct = cpu_to_le16(UAS_PRODUCT_ID),
1991 .bNumConfigurations = 1,
1994 static struct usb_string usbg_us_strings[] = {
1995 [USB_GADGET_MANUFACTURER_IDX].s = "Target Manufactor",
1996 [USB_GADGET_PRODUCT_IDX].s = "Target Product",
1997 [USB_GADGET_SERIAL_IDX].s = "000000000001",
1998 [USB_G_STR_CONFIG].s = "default config",
1999 [USB_G_STR_INT_UAS].s = "USB Attached SCSI",
2000 [USB_G_STR_INT_BBB].s = "Bulk Only Transport",
2004 static struct usb_gadget_strings usbg_stringtab = {
2006 .strings = usbg_us_strings,
2009 static struct usb_gadget_strings *usbg_strings[] = {
2014 static int guas_unbind(struct usb_composite_dev *cdev)
2019 static struct usb_configuration usbg_config_driver = {
2020 .label = "Linux Target",
2021 .bConfigurationValue = 1,
2022 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
2025 static void give_back_ep(struct usb_ep **pep)
2027 struct usb_ep *ep = *pep;
2030 ep->driver_data = NULL;
2033 static int usbg_bind(struct usb_configuration *c, struct usb_function *f)
2035 struct f_uas *fu = to_f_uas(f);
2036 struct usb_gadget *gadget = c->cdev->gadget;
2041 iface = usb_interface_id(c, f);
2045 bot_intf_desc.bInterfaceNumber = iface;
2046 uasp_intf_desc.bInterfaceNumber = iface;
2048 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc,
2049 &uasp_bi_ep_comp_desc);
2053 ep->driver_data = fu;
2056 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc,
2057 &uasp_bo_ep_comp_desc);
2060 ep->driver_data = fu;
2063 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc,
2064 &uasp_status_in_ep_comp_desc);
2067 ep->driver_data = fu;
2070 ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc,
2071 &uasp_cmd_comp_desc);
2074 ep->driver_data = fu;
2077 /* Assume endpoint addresses are the same for both speeds */
2078 uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2079 uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2080 uasp_status_desc.bEndpointAddress =
2081 uasp_ss_status_desc.bEndpointAddress;
2082 uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2084 uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
2085 uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
2086 uasp_fs_status_desc.bEndpointAddress =
2087 uasp_ss_status_desc.bEndpointAddress;
2088 uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2090 ret = usb_assign_descriptors(f, uasp_fs_function_desc,
2091 uasp_hs_function_desc, uasp_ss_function_desc);
2097 pr_err("Can't claim all required eps\n");
2099 give_back_ep(&fu->ep_in);
2100 give_back_ep(&fu->ep_out);
2101 give_back_ep(&fu->ep_status);
2102 give_back_ep(&fu->ep_cmd);
2106 static void usbg_unbind(struct usb_configuration *c, struct usb_function *f)
2108 struct f_uas *fu = to_f_uas(f);
2110 usb_free_all_descriptors(f);
2114 struct guas_setup_wq {
2115 struct work_struct work;
2120 static void usbg_delayed_set_alt(struct work_struct *wq)
2122 struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq,
2124 struct f_uas *fu = work->fu;
2125 int alt = work->alt;
2129 if (fu->flags & USBG_IS_BOT)
2130 bot_cleanup_old_alt(fu);
2131 if (fu->flags & USBG_IS_UAS)
2132 uasp_cleanup_old_alt(fu);
2134 if (alt == USB_G_ALT_INT_BBB)
2136 else if (alt == USB_G_ALT_INT_UAS)
2138 usb_composite_setup_continue(fu->function.config->cdev);
2141 static int usbg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2143 struct f_uas *fu = to_f_uas(f);
2145 if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) {
2146 struct guas_setup_wq *work;
2148 work = kmalloc(sizeof(*work), GFP_ATOMIC);
2151 INIT_WORK(&work->work, usbg_delayed_set_alt);
2154 schedule_work(&work->work);
2155 return USB_GADGET_DELAYED_STATUS;
2160 static void usbg_disable(struct usb_function *f)
2162 struct f_uas *fu = to_f_uas(f);
2164 if (fu->flags & USBG_IS_UAS)
2165 uasp_cleanup_old_alt(fu);
2166 else if (fu->flags & USBG_IS_BOT)
2167 bot_cleanup_old_alt(fu);
2171 static int usbg_setup(struct usb_function *f,
2172 const struct usb_ctrlrequest *ctrl)
2174 struct f_uas *fu = to_f_uas(f);
2176 if (!(fu->flags & USBG_IS_BOT))
2179 return usbg_bot_setup(f, ctrl);
2182 static int usbg_cfg_bind(struct usb_configuration *c)
2187 fu = kzalloc(sizeof(*fu), GFP_KERNEL);
2190 fu->function.name = "Target Function";
2191 fu->function.bind = usbg_bind;
2192 fu->function.unbind = usbg_unbind;
2193 fu->function.set_alt = usbg_set_alt;
2194 fu->function.setup = usbg_setup;
2195 fu->function.disable = usbg_disable;
2196 fu->tpg = the_only_tpg_I_currently_have;
2198 bot_intf_desc.iInterface = usbg_us_strings[USB_G_STR_INT_BBB].id;
2199 uasp_intf_desc.iInterface = usbg_us_strings[USB_G_STR_INT_UAS].id;
2201 ret = usb_add_function(c, &fu->function);
2211 static int usb_target_bind(struct usb_composite_dev *cdev)
2215 ret = usb_string_ids_tab(cdev, usbg_us_strings);
2219 usbg_device_desc.iManufacturer =
2220 usbg_us_strings[USB_GADGET_MANUFACTURER_IDX].id;
2221 usbg_device_desc.iProduct = usbg_us_strings[USB_GADGET_PRODUCT_IDX].id;
2222 usbg_device_desc.iSerialNumber =
2223 usbg_us_strings[USB_GADGET_SERIAL_IDX].id;
2224 usbg_config_driver.iConfiguration =
2225 usbg_us_strings[USB_G_STR_CONFIG].id;
2227 ret = usb_add_config(cdev, &usbg_config_driver,
2231 usb_composite_overwrite_options(cdev, &coverwrite);
2235 static __refdata struct usb_composite_driver usbg_driver = {
2237 .dev = &usbg_device_desc,
2238 .strings = usbg_strings,
2239 .max_speed = USB_SPEED_SUPER,
2240 .bind = usb_target_bind,
2241 .unbind = guas_unbind,
2244 static int usbg_attach(struct usbg_tpg *tpg)
2246 return usb_composite_probe(&usbg_driver);
2249 static void usbg_detach(struct usbg_tpg *tpg)
2251 usb_composite_unregister(&usbg_driver);
2254 static int __init usb_target_gadget_init(void)
2256 return target_register_template(&usbg_ops);
2258 module_init(usb_target_gadget_init);
2260 static void __exit usb_target_gadget_exit(void)
2262 target_unregister_template(&usbg_ops);
2264 module_exit(usb_target_gadget_exit);
2266 MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
2267 MODULE_DESCRIPTION("usb-gadget fabric");
2268 MODULE_LICENSE("GPL v2");