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_tcq.h>
20 #include <target/target_core_base.h>
21 #include <target/target_core_fabric.h>
22 #include <asm/unaligned.h>
24 #include "tcm_usb_gadget.h"
26 USB_GADGET_COMPOSITE_OPTIONS();
28 static inline struct f_uas
*to_f_uas(struct usb_function
*f
)
30 return container_of(f
, struct f_uas
, function
);
33 static void usbg_cmd_release(struct kref
*);
35 static inline void usbg_cleanup_cmd(struct usbg_cmd
*cmd
)
37 kref_put(&cmd
->ref
, usbg_cmd_release
);
40 /* Start bot.c code */
42 static int bot_enqueue_cmd_cbw(struct f_uas
*fu
)
46 if (fu
->flags
& USBG_BOT_CMD_PEND
)
49 ret
= usb_ep_queue(fu
->ep_out
, fu
->cmd
.req
, GFP_ATOMIC
);
51 fu
->flags
|= USBG_BOT_CMD_PEND
;
55 static void bot_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
57 struct usbg_cmd
*cmd
= req
->context
;
58 struct f_uas
*fu
= cmd
->fu
;
60 usbg_cleanup_cmd(cmd
);
61 if (req
->status
< 0) {
62 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
66 /* CSW completed, wait for next CBW */
67 bot_enqueue_cmd_cbw(fu
);
70 static void bot_enqueue_sense_code(struct f_uas
*fu
, struct usbg_cmd
*cmd
)
72 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
75 unsigned int csw_stat
;
77 csw_stat
= cmd
->csw_code
;
80 * We can't send SENSE as a response. So we take ASC & ASCQ from our
81 * sense buffer and queue it and hope the host sends a REQUEST_SENSE
82 * command where it learns why we failed.
84 sense
= cmd
->sense_iu
.sense
;
86 csw
->Tag
= cmd
->bot_tag
;
87 csw
->Status
= csw_stat
;
88 fu
->bot_status
.req
->context
= cmd
;
89 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_status
.req
, GFP_ATOMIC
);
91 pr_err("%s(%d) ERR: %d\n", __func__
, __LINE__
, ret
);
94 static void bot_err_compl(struct usb_ep
*ep
, struct usb_request
*req
)
96 struct usbg_cmd
*cmd
= req
->context
;
97 struct f_uas
*fu
= cmd
->fu
;
100 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
103 if (cmd
->data_len
> ep
->maxpacket
) {
104 req
->length
= ep
->maxpacket
;
105 cmd
->data_len
-= ep
->maxpacket
;
107 req
->length
= cmd
->data_len
;
111 usb_ep_queue(ep
, req
, GFP_ATOMIC
);
114 bot_enqueue_sense_code(fu
, cmd
);
117 static void bot_send_bad_status(struct usbg_cmd
*cmd
)
119 struct f_uas
*fu
= cmd
->fu
;
120 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
121 struct usb_request
*req
;
124 csw
->Residue
= cpu_to_le32(cmd
->data_len
);
129 req
= fu
->bot_req_in
;
132 req
= fu
->bot_req_out
;
135 if (cmd
->data_len
> fu
->ep_in
->maxpacket
) {
136 req
->length
= ep
->maxpacket
;
137 cmd
->data_len
-= ep
->maxpacket
;
139 req
->length
= cmd
->data_len
;
142 req
->complete
= bot_err_compl
;
144 req
->buf
= fu
->cmd
.buf
;
145 usb_ep_queue(ep
, req
, GFP_KERNEL
);
147 bot_enqueue_sense_code(fu
, cmd
);
151 static int bot_send_status(struct usbg_cmd
*cmd
, bool moved_data
)
153 struct f_uas
*fu
= cmd
->fu
;
154 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
157 if (cmd
->se_cmd
.scsi_status
== SAM_STAT_GOOD
) {
158 if (!moved_data
&& cmd
->data_len
) {
160 * the host wants to move data, we don't. Fill / empty
161 * the pipe and then send the csw with reside set.
163 cmd
->csw_code
= US_BULK_STAT_OK
;
164 bot_send_bad_status(cmd
);
168 csw
->Tag
= cmd
->bot_tag
;
169 csw
->Residue
= cpu_to_le32(0);
170 csw
->Status
= US_BULK_STAT_OK
;
171 fu
->bot_status
.req
->context
= cmd
;
173 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_status
.req
, GFP_KERNEL
);
175 pr_err("%s(%d) ERR: %d\n", __func__
, __LINE__
, ret
);
177 cmd
->csw_code
= US_BULK_STAT_FAIL
;
178 bot_send_bad_status(cmd
);
184 * Called after command (no data transfer) or after the write (to device)
185 * operation is completed
187 static int bot_send_status_response(struct usbg_cmd
*cmd
)
189 bool moved_data
= false;
193 return bot_send_status(cmd
, moved_data
);
196 /* Read request completed, now we have to send the CSW */
197 static void bot_read_compl(struct usb_ep
*ep
, struct usb_request
*req
)
199 struct usbg_cmd
*cmd
= req
->context
;
202 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
204 bot_send_status(cmd
, true);
207 static int bot_send_read_response(struct usbg_cmd
*cmd
)
209 struct f_uas
*fu
= cmd
->fu
;
210 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
211 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
214 if (!cmd
->data_len
) {
215 cmd
->csw_code
= US_BULK_STAT_PHASE
;
216 bot_send_bad_status(cmd
);
220 if (!gadget
->sg_supported
) {
221 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
225 sg_copy_to_buffer(se_cmd
->t_data_sg
,
226 se_cmd
->t_data_nents
,
228 se_cmd
->data_length
);
230 fu
->bot_req_in
->buf
= cmd
->data_buf
;
232 fu
->bot_req_in
->buf
= NULL
;
233 fu
->bot_req_in
->num_sgs
= se_cmd
->t_data_nents
;
234 fu
->bot_req_in
->sg
= se_cmd
->t_data_sg
;
237 fu
->bot_req_in
->complete
= bot_read_compl
;
238 fu
->bot_req_in
->length
= se_cmd
->data_length
;
239 fu
->bot_req_in
->context
= cmd
;
240 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_req_in
, GFP_ATOMIC
);
242 pr_err("%s(%d)\n", __func__
, __LINE__
);
246 static void usbg_data_write_cmpl(struct usb_ep
*, struct usb_request
*);
247 static int usbg_prepare_w_request(struct usbg_cmd
*, struct usb_request
*);
249 static int bot_send_write_request(struct usbg_cmd
*cmd
)
251 struct f_uas
*fu
= cmd
->fu
;
252 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
253 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
256 init_completion(&cmd
->write_complete
);
259 if (!cmd
->data_len
) {
260 cmd
->csw_code
= US_BULK_STAT_PHASE
;
264 if (!gadget
->sg_supported
) {
265 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_KERNEL
);
269 fu
->bot_req_out
->buf
= cmd
->data_buf
;
271 fu
->bot_req_out
->buf
= NULL
;
272 fu
->bot_req_out
->num_sgs
= se_cmd
->t_data_nents
;
273 fu
->bot_req_out
->sg
= se_cmd
->t_data_sg
;
276 fu
->bot_req_out
->complete
= usbg_data_write_cmpl
;
277 fu
->bot_req_out
->length
= se_cmd
->data_length
;
278 fu
->bot_req_out
->context
= cmd
;
280 ret
= usbg_prepare_w_request(cmd
, fu
->bot_req_out
);
283 ret
= usb_ep_queue(fu
->ep_out
, fu
->bot_req_out
, GFP_KERNEL
);
285 pr_err("%s(%d)\n", __func__
, __LINE__
);
287 wait_for_completion(&cmd
->write_complete
);
288 target_execute_cmd(se_cmd
);
293 static int bot_submit_command(struct f_uas
*, void *, unsigned int);
295 static void bot_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
297 struct f_uas
*fu
= req
->context
;
300 fu
->flags
&= ~USBG_BOT_CMD_PEND
;
305 ret
= bot_submit_command(fu
, req
->buf
, req
->actual
);
307 pr_err("%s(%d): %d\n", __func__
, __LINE__
, ret
);
310 static int bot_prepare_reqs(struct f_uas
*fu
)
314 fu
->bot_req_in
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
318 fu
->bot_req_out
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
319 if (!fu
->bot_req_out
)
322 fu
->cmd
.req
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
326 fu
->bot_status
.req
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
327 if (!fu
->bot_status
.req
)
330 fu
->bot_status
.req
->buf
= &fu
->bot_status
.csw
;
331 fu
->bot_status
.req
->length
= US_BULK_CS_WRAP_LEN
;
332 fu
->bot_status
.req
->complete
= bot_status_complete
;
333 fu
->bot_status
.csw
.Signature
= cpu_to_le32(US_BULK_CS_SIGN
);
335 fu
->cmd
.buf
= kmalloc(fu
->ep_out
->maxpacket
, GFP_KERNEL
);
339 fu
->cmd
.req
->complete
= bot_cmd_complete
;
340 fu
->cmd
.req
->buf
= fu
->cmd
.buf
;
341 fu
->cmd
.req
->length
= fu
->ep_out
->maxpacket
;
342 fu
->cmd
.req
->context
= fu
;
344 ret
= bot_enqueue_cmd_cbw(fu
);
352 usb_ep_free_request(fu
->ep_in
, fu
->bot_status
.req
);
354 usb_ep_free_request(fu
->ep_out
, fu
->cmd
.req
);
357 usb_ep_free_request(fu
->ep_out
, fu
->bot_req_out
);
358 fu
->bot_req_out
= NULL
;
360 usb_ep_free_request(fu
->ep_in
, fu
->bot_req_in
);
361 fu
->bot_req_in
= NULL
;
363 pr_err("BOT: endpoint setup failed\n");
367 static void bot_cleanup_old_alt(struct f_uas
*fu
)
369 if (!(fu
->flags
& USBG_ENABLED
))
372 usb_ep_disable(fu
->ep_in
);
373 usb_ep_disable(fu
->ep_out
);
378 usb_ep_free_request(fu
->ep_in
, fu
->bot_req_in
);
379 usb_ep_free_request(fu
->ep_out
, fu
->bot_req_out
);
380 usb_ep_free_request(fu
->ep_out
, fu
->cmd
.req
);
381 usb_ep_free_request(fu
->ep_out
, fu
->bot_status
.req
);
385 fu
->bot_req_in
= NULL
;
386 fu
->bot_req_out
= NULL
;
388 fu
->bot_status
.req
= NULL
;
392 static void bot_set_alt(struct f_uas
*fu
)
394 struct usb_function
*f
= &fu
->function
;
395 struct usb_gadget
*gadget
= f
->config
->cdev
->gadget
;
398 fu
->flags
= USBG_IS_BOT
;
400 config_ep_by_speed(gadget
, f
, fu
->ep_in
);
401 ret
= usb_ep_enable(fu
->ep_in
);
405 config_ep_by_speed(gadget
, f
, fu
->ep_out
);
406 ret
= usb_ep_enable(fu
->ep_out
);
410 ret
= bot_prepare_reqs(fu
);
413 fu
->flags
|= USBG_ENABLED
;
414 pr_info("Using the BOT protocol\n");
417 usb_ep_disable(fu
->ep_out
);
419 usb_ep_disable(fu
->ep_in
);
421 fu
->flags
= USBG_IS_BOT
;
424 static int usbg_bot_setup(struct usb_function
*f
,
425 const struct usb_ctrlrequest
*ctrl
)
427 struct f_uas
*fu
= to_f_uas(f
);
428 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
429 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
430 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
434 switch (ctrl
->bRequest
) {
435 case US_BULK_GET_MAX_LUN
:
436 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_TYPE_CLASS
|
437 USB_RECIP_INTERFACE
))
444 luns
= atomic_read(&fu
->tpg
->tpg_port_count
);
446 pr_err("No LUNs configured?\n");
450 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
451 * accessed. The upper limit is 0xf
455 pr_info_once("Limiting the number of luns to 16\n");
458 ret_lun
= cdev
->req
->buf
;
460 cdev
->req
->length
= 1;
461 return usb_ep_queue(cdev
->gadget
->ep0
, cdev
->req
, GFP_ATOMIC
);
464 case US_BULK_RESET_REQUEST
:
465 /* XXX maybe we should remove previous requests for IN + OUT */
466 bot_enqueue_cmd_cbw(fu
);
473 /* Start uas.c code */
475 static void uasp_cleanup_one_stream(struct f_uas
*fu
, struct uas_stream
*stream
)
477 /* We have either all three allocated or none */
481 usb_ep_free_request(fu
->ep_in
, stream
->req_in
);
482 usb_ep_free_request(fu
->ep_out
, stream
->req_out
);
483 usb_ep_free_request(fu
->ep_status
, stream
->req_status
);
485 stream
->req_in
= NULL
;
486 stream
->req_out
= NULL
;
487 stream
->req_status
= NULL
;
490 static void uasp_free_cmdreq(struct f_uas
*fu
)
492 usb_ep_free_request(fu
->ep_cmd
, fu
->cmd
.req
);
498 static void uasp_cleanup_old_alt(struct f_uas
*fu
)
502 if (!(fu
->flags
& USBG_ENABLED
))
505 usb_ep_disable(fu
->ep_in
);
506 usb_ep_disable(fu
->ep_out
);
507 usb_ep_disable(fu
->ep_status
);
508 usb_ep_disable(fu
->ep_cmd
);
510 for (i
= 0; i
< UASP_SS_EP_COMP_NUM_STREAMS
; i
++)
511 uasp_cleanup_one_stream(fu
, &fu
->stream
[i
]);
512 uasp_free_cmdreq(fu
);
515 static void uasp_status_data_cmpl(struct usb_ep
*ep
, struct usb_request
*req
);
517 static int uasp_prepare_r_request(struct usbg_cmd
*cmd
)
519 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
520 struct f_uas
*fu
= cmd
->fu
;
521 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
522 struct uas_stream
*stream
= cmd
->stream
;
524 if (!gadget
->sg_supported
) {
525 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
529 sg_copy_to_buffer(se_cmd
->t_data_sg
,
530 se_cmd
->t_data_nents
,
532 se_cmd
->data_length
);
534 stream
->req_in
->buf
= cmd
->data_buf
;
536 stream
->req_in
->buf
= NULL
;
537 stream
->req_in
->num_sgs
= se_cmd
->t_data_nents
;
538 stream
->req_in
->sg
= se_cmd
->t_data_sg
;
541 stream
->req_in
->complete
= uasp_status_data_cmpl
;
542 stream
->req_in
->length
= se_cmd
->data_length
;
543 stream
->req_in
->context
= cmd
;
545 cmd
->state
= UASP_SEND_STATUS
;
549 static void uasp_prepare_status(struct usbg_cmd
*cmd
)
551 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
552 struct sense_iu
*iu
= &cmd
->sense_iu
;
553 struct uas_stream
*stream
= cmd
->stream
;
555 cmd
->state
= UASP_QUEUE_COMMAND
;
556 iu
->iu_id
= IU_ID_STATUS
;
557 iu
->tag
= cpu_to_be16(cmd
->tag
);
560 * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
562 iu
->len
= cpu_to_be16(se_cmd
->scsi_sense_length
);
563 iu
->status
= se_cmd
->scsi_status
;
564 stream
->req_status
->context
= cmd
;
565 stream
->req_status
->length
= se_cmd
->scsi_sense_length
+ 16;
566 stream
->req_status
->buf
= iu
;
567 stream
->req_status
->complete
= uasp_status_data_cmpl
;
570 static void uasp_status_data_cmpl(struct usb_ep
*ep
, struct usb_request
*req
)
572 struct usbg_cmd
*cmd
= req
->context
;
573 struct uas_stream
*stream
= cmd
->stream
;
574 struct f_uas
*fu
= cmd
->fu
;
580 switch (cmd
->state
) {
582 ret
= uasp_prepare_r_request(cmd
);
585 ret
= usb_ep_queue(fu
->ep_in
, stream
->req_in
, GFP_ATOMIC
);
587 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
590 case UASP_RECEIVE_DATA
:
591 ret
= usbg_prepare_w_request(cmd
, stream
->req_out
);
594 ret
= usb_ep_queue(fu
->ep_out
, stream
->req_out
, GFP_ATOMIC
);
596 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
599 case UASP_SEND_STATUS
:
600 uasp_prepare_status(cmd
);
601 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
604 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
607 case UASP_QUEUE_COMMAND
:
608 usbg_cleanup_cmd(cmd
);
609 usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
618 usbg_cleanup_cmd(cmd
);
621 static int uasp_send_status_response(struct usbg_cmd
*cmd
)
623 struct f_uas
*fu
= cmd
->fu
;
624 struct uas_stream
*stream
= cmd
->stream
;
625 struct sense_iu
*iu
= &cmd
->sense_iu
;
627 iu
->tag
= cpu_to_be16(cmd
->tag
);
628 stream
->req_status
->complete
= uasp_status_data_cmpl
;
629 stream
->req_status
->context
= cmd
;
631 uasp_prepare_status(cmd
);
632 return usb_ep_queue(fu
->ep_status
, stream
->req_status
, GFP_ATOMIC
);
635 static int uasp_send_read_response(struct usbg_cmd
*cmd
)
637 struct f_uas
*fu
= cmd
->fu
;
638 struct uas_stream
*stream
= cmd
->stream
;
639 struct sense_iu
*iu
= &cmd
->sense_iu
;
644 iu
->tag
= cpu_to_be16(cmd
->tag
);
645 if (fu
->flags
& USBG_USE_STREAMS
) {
647 ret
= uasp_prepare_r_request(cmd
);
650 ret
= usb_ep_queue(fu
->ep_in
, stream
->req_in
, GFP_ATOMIC
);
652 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
653 kfree(cmd
->data_buf
);
654 cmd
->data_buf
= NULL
;
659 iu
->iu_id
= IU_ID_READ_READY
;
660 iu
->tag
= cpu_to_be16(cmd
->tag
);
662 stream
->req_status
->complete
= uasp_status_data_cmpl
;
663 stream
->req_status
->context
= cmd
;
665 cmd
->state
= UASP_SEND_DATA
;
666 stream
->req_status
->buf
= iu
;
667 stream
->req_status
->length
= sizeof(struct iu
);
669 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
672 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
678 static int uasp_send_write_request(struct usbg_cmd
*cmd
)
680 struct f_uas
*fu
= cmd
->fu
;
681 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
682 struct uas_stream
*stream
= cmd
->stream
;
683 struct sense_iu
*iu
= &cmd
->sense_iu
;
686 init_completion(&cmd
->write_complete
);
689 iu
->tag
= cpu_to_be16(cmd
->tag
);
691 if (fu
->flags
& USBG_USE_STREAMS
) {
693 ret
= usbg_prepare_w_request(cmd
, stream
->req_out
);
696 ret
= usb_ep_queue(fu
->ep_out
, stream
->req_out
, GFP_ATOMIC
);
698 pr_err("%s(%d)\n", __func__
, __LINE__
);
702 iu
->iu_id
= IU_ID_WRITE_READY
;
703 iu
->tag
= cpu_to_be16(cmd
->tag
);
705 stream
->req_status
->complete
= uasp_status_data_cmpl
;
706 stream
->req_status
->context
= cmd
;
708 cmd
->state
= UASP_RECEIVE_DATA
;
709 stream
->req_status
->buf
= iu
;
710 stream
->req_status
->length
= sizeof(struct iu
);
712 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
715 pr_err("%s(%d)\n", __func__
, __LINE__
);
718 wait_for_completion(&cmd
->write_complete
);
719 target_execute_cmd(se_cmd
);
724 static int usbg_submit_command(struct f_uas
*, void *, unsigned int);
726 static void uasp_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
728 struct f_uas
*fu
= req
->context
;
734 ret
= usbg_submit_command(fu
, req
->buf
, req
->actual
);
736 * Once we tune for performance enqueue the command req here again so
737 * we can receive a second command while we processing this one. Pay
738 * attention to properly sync STAUS endpoint with DATA IN + OUT so you
743 usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
746 static int uasp_alloc_stream_res(struct f_uas
*fu
, struct uas_stream
*stream
)
748 stream
->req_in
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
752 stream
->req_out
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
753 if (!stream
->req_out
)
756 stream
->req_status
= usb_ep_alloc_request(fu
->ep_status
, GFP_KERNEL
);
757 if (!stream
->req_status
)
762 usb_ep_free_request(fu
->ep_status
, stream
->req_status
);
763 stream
->req_status
= NULL
;
765 usb_ep_free_request(fu
->ep_out
, stream
->req_out
);
766 stream
->req_out
= NULL
;
771 static int uasp_alloc_cmd(struct f_uas
*fu
)
773 fu
->cmd
.req
= usb_ep_alloc_request(fu
->ep_cmd
, GFP_KERNEL
);
777 fu
->cmd
.buf
= kmalloc(fu
->ep_cmd
->maxpacket
, GFP_KERNEL
);
781 fu
->cmd
.req
->complete
= uasp_cmd_complete
;
782 fu
->cmd
.req
->buf
= fu
->cmd
.buf
;
783 fu
->cmd
.req
->length
= fu
->ep_cmd
->maxpacket
;
784 fu
->cmd
.req
->context
= fu
;
788 usb_ep_free_request(fu
->ep_cmd
, fu
->cmd
.req
);
793 static void uasp_setup_stream_res(struct f_uas
*fu
, int max_streams
)
797 for (i
= 0; i
< max_streams
; i
++) {
798 struct uas_stream
*s
= &fu
->stream
[i
];
800 s
->req_in
->stream_id
= i
+ 1;
801 s
->req_out
->stream_id
= i
+ 1;
802 s
->req_status
->stream_id
= i
+ 1;
806 static int uasp_prepare_reqs(struct f_uas
*fu
)
812 if (fu
->flags
& USBG_USE_STREAMS
)
813 max_streams
= UASP_SS_EP_COMP_NUM_STREAMS
;
817 for (i
= 0; i
< max_streams
; i
++) {
818 ret
= uasp_alloc_stream_res(fu
, &fu
->stream
[i
]);
823 ret
= uasp_alloc_cmd(fu
);
825 goto err_free_stream
;
826 uasp_setup_stream_res(fu
, max_streams
);
828 ret
= usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
830 goto err_free_stream
;
835 uasp_free_cmdreq(fu
);
840 uasp_cleanup_one_stream(fu
, &fu
->stream
[i
- 1]);
844 pr_err("UASP: endpoint setup failed\n");
848 static void uasp_set_alt(struct f_uas
*fu
)
850 struct usb_function
*f
= &fu
->function
;
851 struct usb_gadget
*gadget
= f
->config
->cdev
->gadget
;
854 fu
->flags
= USBG_IS_UAS
;
856 if (gadget
->speed
== USB_SPEED_SUPER
)
857 fu
->flags
|= USBG_USE_STREAMS
;
859 config_ep_by_speed(gadget
, f
, fu
->ep_in
);
860 ret
= usb_ep_enable(fu
->ep_in
);
864 config_ep_by_speed(gadget
, f
, fu
->ep_out
);
865 ret
= usb_ep_enable(fu
->ep_out
);
869 config_ep_by_speed(gadget
, f
, fu
->ep_cmd
);
870 ret
= usb_ep_enable(fu
->ep_cmd
);
873 config_ep_by_speed(gadget
, f
, fu
->ep_status
);
874 ret
= usb_ep_enable(fu
->ep_status
);
878 ret
= uasp_prepare_reqs(fu
);
881 fu
->flags
|= USBG_ENABLED
;
883 pr_info("Using the UAS protocol\n");
886 usb_ep_disable(fu
->ep_status
);
888 usb_ep_disable(fu
->ep_cmd
);
890 usb_ep_disable(fu
->ep_out
);
892 usb_ep_disable(fu
->ep_in
);
897 static int get_cmd_dir(const unsigned char *cdb
)
909 case SERVICE_ACTION_IN_16
:
911 case PERSISTENT_RESERVE_IN
:
912 case SECURITY_PROTOCOL_IN
:
913 case ACCESS_CONTROL_IN
:
915 case READ_BLOCK_LIMITS
:
919 case READ_FORMAT_CAPACITIES
:
921 ret
= DMA_FROM_DEVICE
;
931 case WRITE_VERIFY_12
:
932 case PERSISTENT_RESERVE_OUT
:
933 case MAINTENANCE_OUT
:
934 case SECURITY_PROTOCOL_OUT
:
935 case ACCESS_CONTROL_OUT
:
938 case ALLOW_MEDIUM_REMOVAL
:
939 case TEST_UNIT_READY
:
940 case SYNCHRONIZE_CACHE
:
947 case WRITE_FILEMARKS
:
951 pr_warn("target: Unknown data direction for SCSI Opcode "
958 static void usbg_data_write_cmpl(struct usb_ep
*ep
, struct usb_request
*req
)
960 struct usbg_cmd
*cmd
= req
->context
;
961 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
963 if (req
->status
< 0) {
964 pr_err("%s() state %d transfer failed\n", __func__
, cmd
->state
);
968 if (req
->num_sgs
== 0) {
969 sg_copy_from_buffer(se_cmd
->t_data_sg
,
970 se_cmd
->t_data_nents
,
972 se_cmd
->data_length
);
975 complete(&cmd
->write_complete
);
979 usbg_cleanup_cmd(cmd
);
982 static int usbg_prepare_w_request(struct usbg_cmd
*cmd
, struct usb_request
*req
)
984 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
985 struct f_uas
*fu
= cmd
->fu
;
986 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
988 if (!gadget
->sg_supported
) {
989 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
993 req
->buf
= cmd
->data_buf
;
996 req
->num_sgs
= se_cmd
->t_data_nents
;
997 req
->sg
= se_cmd
->t_data_sg
;
1000 req
->complete
= usbg_data_write_cmpl
;
1001 req
->length
= se_cmd
->data_length
;
1006 static int usbg_send_status_response(struct se_cmd
*se_cmd
)
1008 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1010 struct f_uas
*fu
= cmd
->fu
;
1012 if (fu
->flags
& USBG_IS_BOT
)
1013 return bot_send_status_response(cmd
);
1015 return uasp_send_status_response(cmd
);
1018 static int usbg_send_write_request(struct se_cmd
*se_cmd
)
1020 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1022 struct f_uas
*fu
= cmd
->fu
;
1024 if (fu
->flags
& USBG_IS_BOT
)
1025 return bot_send_write_request(cmd
);
1027 return uasp_send_write_request(cmd
);
1030 static int usbg_send_read_response(struct se_cmd
*se_cmd
)
1032 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1034 struct f_uas
*fu
= cmd
->fu
;
1036 if (fu
->flags
& USBG_IS_BOT
)
1037 return bot_send_read_response(cmd
);
1039 return uasp_send_read_response(cmd
);
1042 static void usbg_cmd_work(struct work_struct
*work
)
1044 struct usbg_cmd
*cmd
= container_of(work
, struct usbg_cmd
, work
);
1045 struct se_cmd
*se_cmd
;
1046 struct tcm_usbg_nexus
*tv_nexus
;
1047 struct usbg_tpg
*tpg
;
1050 se_cmd
= &cmd
->se_cmd
;
1052 tv_nexus
= tpg
->tpg_nexus
;
1053 dir
= get_cmd_dir(cmd
->cmd_buf
);
1055 transport_init_se_cmd(se_cmd
,
1056 tv_nexus
->tvn_se_sess
->se_tpg
->se_tpg_tfo
,
1057 tv_nexus
->tvn_se_sess
, cmd
->data_len
, DMA_NONE
,
1058 cmd
->prio_attr
, cmd
->sense_iu
.sense
);
1062 if (target_submit_cmd(se_cmd
, tv_nexus
->tvn_se_sess
,
1063 cmd
->cmd_buf
, cmd
->sense_iu
.sense
, cmd
->unpacked_lun
,
1064 0, cmd
->prio_attr
, dir
, TARGET_SCF_UNKNOWN_SIZE
) < 0)
1070 transport_send_check_condition_and_sense(se_cmd
,
1071 TCM_UNSUPPORTED_SCSI_OPCODE
, 1);
1072 usbg_cleanup_cmd(cmd
);
1075 static int usbg_submit_command(struct f_uas
*fu
,
1076 void *cmdbuf
, unsigned int len
)
1078 struct command_iu
*cmd_iu
= cmdbuf
;
1079 struct usbg_cmd
*cmd
;
1080 struct usbg_tpg
*tpg
;
1081 struct se_cmd
*se_cmd
;
1082 struct tcm_usbg_nexus
*tv_nexus
;
1086 if (cmd_iu
->iu_id
!= IU_ID_COMMAND
) {
1087 pr_err("Unsupported type %d\n", cmd_iu
->iu_id
);
1091 cmd
= kzalloc(sizeof *cmd
, GFP_ATOMIC
);
1097 /* XXX until I figure out why I can't free in on complete */
1098 kref_init(&cmd
->ref
);
1099 kref_get(&cmd
->ref
);
1102 cmd_len
= (cmd_iu
->len
& ~0x3) + 16;
1103 if (cmd_len
> USBG_MAX_CMD
)
1106 memcpy(cmd
->cmd_buf
, cmd_iu
->cdb
, cmd_len
);
1108 cmd
->tag
= be16_to_cpup(&cmd_iu
->tag
);
1109 cmd
->se_cmd
.tag
= cmd
->tag
;
1110 if (fu
->flags
& USBG_USE_STREAMS
) {
1111 if (cmd
->tag
> UASP_SS_EP_COMP_NUM_STREAMS
)
1114 cmd
->stream
= &fu
->stream
[0];
1116 cmd
->stream
= &fu
->stream
[cmd
->tag
- 1];
1118 cmd
->stream
= &fu
->stream
[0];
1121 tv_nexus
= tpg
->tpg_nexus
;
1123 pr_err("Missing nexus, ignoring command\n");
1127 switch (cmd_iu
->prio_attr
& 0x7) {
1129 cmd
->prio_attr
= TCM_HEAD_TAG
;
1131 case UAS_ORDERED_TAG
:
1132 cmd
->prio_attr
= TCM_ORDERED_TAG
;
1135 cmd
->prio_attr
= TCM_ACA_TAG
;
1138 pr_debug_once("Unsupported prio_attr: %02x.\n",
1140 case UAS_SIMPLE_TAG
:
1141 cmd
->prio_attr
= TCM_SIMPLE_TAG
;
1145 se_cmd
= &cmd
->se_cmd
;
1146 cmd
->unpacked_lun
= scsilun_to_int(&cmd_iu
->lun
);
1148 INIT_WORK(&cmd
->work
, usbg_cmd_work
);
1149 ret
= queue_work(tpg
->workqueue
, &cmd
->work
);
1159 static void bot_cmd_work(struct work_struct
*work
)
1161 struct usbg_cmd
*cmd
= container_of(work
, struct usbg_cmd
, work
);
1162 struct se_cmd
*se_cmd
;
1163 struct tcm_usbg_nexus
*tv_nexus
;
1164 struct usbg_tpg
*tpg
;
1167 se_cmd
= &cmd
->se_cmd
;
1169 tv_nexus
= tpg
->tpg_nexus
;
1170 dir
= get_cmd_dir(cmd
->cmd_buf
);
1172 transport_init_se_cmd(se_cmd
,
1173 tv_nexus
->tvn_se_sess
->se_tpg
->se_tpg_tfo
,
1174 tv_nexus
->tvn_se_sess
, cmd
->data_len
, DMA_NONE
,
1175 cmd
->prio_attr
, cmd
->sense_iu
.sense
);
1179 if (target_submit_cmd(se_cmd
, tv_nexus
->tvn_se_sess
,
1180 cmd
->cmd_buf
, cmd
->sense_iu
.sense
, cmd
->unpacked_lun
,
1181 cmd
->data_len
, cmd
->prio_attr
, dir
, 0) < 0)
1187 transport_send_check_condition_and_sense(se_cmd
,
1188 TCM_UNSUPPORTED_SCSI_OPCODE
, 1);
1189 usbg_cleanup_cmd(cmd
);
1192 static int bot_submit_command(struct f_uas
*fu
,
1193 void *cmdbuf
, unsigned int len
)
1195 struct bulk_cb_wrap
*cbw
= cmdbuf
;
1196 struct usbg_cmd
*cmd
;
1197 struct usbg_tpg
*tpg
;
1198 struct se_cmd
*se_cmd
;
1199 struct tcm_usbg_nexus
*tv_nexus
;
1203 if (cbw
->Signature
!= cpu_to_le32(US_BULK_CB_SIGN
)) {
1204 pr_err("Wrong signature on CBW\n");
1208 pr_err("Wrong length for CBW\n");
1212 cmd_len
= cbw
->Length
;
1213 if (cmd_len
< 1 || cmd_len
> 16)
1216 cmd
= kzalloc(sizeof *cmd
, GFP_ATOMIC
);
1222 /* XXX until I figure out why I can't free in on complete */
1223 kref_init(&cmd
->ref
);
1224 kref_get(&cmd
->ref
);
1228 memcpy(cmd
->cmd_buf
, cbw
->CDB
, cmd_len
);
1230 cmd
->bot_tag
= cbw
->Tag
;
1232 tv_nexus
= tpg
->tpg_nexus
;
1234 pr_err("Missing nexus, ignoring command\n");
1238 cmd
->prio_attr
= TCM_SIMPLE_TAG
;
1239 se_cmd
= &cmd
->se_cmd
;
1240 cmd
->unpacked_lun
= cbw
->Lun
;
1241 cmd
->is_read
= cbw
->Flags
& US_BULK_FLAG_IN
? 1 : 0;
1242 cmd
->data_len
= le32_to_cpu(cbw
->DataTransferLength
);
1243 cmd
->se_cmd
.tag
= le32_to_cpu(cmd
->bot_tag
);
1245 INIT_WORK(&cmd
->work
, bot_cmd_work
);
1246 ret
= queue_work(tpg
->workqueue
, &cmd
->work
);
1256 /* Start fabric.c code */
1258 static int usbg_check_true(struct se_portal_group
*se_tpg
)
1263 static int usbg_check_false(struct se_portal_group
*se_tpg
)
1268 static char *usbg_get_fabric_name(void)
1270 return "usb_gadget";
1273 static char *usbg_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1275 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1276 struct usbg_tpg
, se_tpg
);
1277 struct usbg_tport
*tport
= tpg
->tport
;
1279 return &tport
->tport_name
[0];
1282 static u16
usbg_get_tag(struct se_portal_group
*se_tpg
)
1284 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1285 struct usbg_tpg
, se_tpg
);
1286 return tpg
->tport_tpgt
;
1289 static u32
usbg_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
1294 static void usbg_cmd_release(struct kref
*ref
)
1296 struct usbg_cmd
*cmd
= container_of(ref
, struct usbg_cmd
,
1299 transport_generic_free_cmd(&cmd
->se_cmd
, 0);
1302 static void usbg_release_cmd(struct se_cmd
*se_cmd
)
1304 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1306 kfree(cmd
->data_buf
);
1311 static int usbg_shutdown_session(struct se_session
*se_sess
)
1316 static void usbg_close_session(struct se_session
*se_sess
)
1321 static u32
usbg_sess_get_index(struct se_session
*se_sess
)
1327 * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
1329 static int usbg_write_pending_status(struct se_cmd
*se_cmd
)
1334 static void usbg_set_default_node_attrs(struct se_node_acl
*nacl
)
1339 static int usbg_get_cmd_state(struct se_cmd
*se_cmd
)
1344 static void usbg_queue_tm_rsp(struct se_cmd
*se_cmd
)
1348 static void usbg_aborted_task(struct se_cmd
*se_cmd
)
1353 static const char *usbg_check_wwn(const char *name
)
1358 n
= strstr(name
, "naa.");
1363 if (len
== 0 || len
> USBG_NAMELEN
- 1)
1368 static int usbg_init_nodeacl(struct se_node_acl
*se_nacl
, const char *name
)
1370 if (!usbg_check_wwn(name
))
1375 struct usbg_tpg
*the_only_tpg_I_currently_have
;
1377 static struct se_portal_group
*usbg_make_tpg(
1379 struct config_group
*group
,
1382 struct usbg_tport
*tport
= container_of(wwn
, struct usbg_tport
,
1384 struct usbg_tpg
*tpg
;
1388 if (strstr(name
, "tpgt_") != name
)
1389 return ERR_PTR(-EINVAL
);
1390 if (kstrtoul(name
+ 5, 0, &tpgt
) || tpgt
> UINT_MAX
)
1391 return ERR_PTR(-EINVAL
);
1392 if (the_only_tpg_I_currently_have
) {
1393 pr_err("Until the gadget framework can't handle multiple\n");
1394 pr_err("gadgets, you can't do this here.\n");
1395 return ERR_PTR(-EBUSY
);
1398 tpg
= kzalloc(sizeof(struct usbg_tpg
), GFP_KERNEL
);
1400 return ERR_PTR(-ENOMEM
);
1401 mutex_init(&tpg
->tpg_mutex
);
1402 atomic_set(&tpg
->tpg_port_count
, 0);
1403 tpg
->workqueue
= alloc_workqueue("tcm_usb_gadget", 0, 1);
1404 if (!tpg
->workqueue
) {
1410 tpg
->tport_tpgt
= tpgt
;
1413 * SPC doesn't assign a protocol identifier for USB-SCSI, so we
1414 * pretend to be SAS..
1416 ret
= core_tpg_register(wwn
, &tpg
->se_tpg
, SCSI_PROTOCOL_SAS
);
1418 destroy_workqueue(tpg
->workqueue
);
1422 the_only_tpg_I_currently_have
= tpg
;
1423 return &tpg
->se_tpg
;
1426 static int tcm_usbg_drop_nexus(struct usbg_tpg
*);
1428 static void usbg_drop_tpg(struct se_portal_group
*se_tpg
)
1430 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1431 struct usbg_tpg
, se_tpg
);
1433 tcm_usbg_drop_nexus(tpg
);
1434 core_tpg_deregister(se_tpg
);
1435 destroy_workqueue(tpg
->workqueue
);
1437 the_only_tpg_I_currently_have
= NULL
;
1440 static struct se_wwn
*usbg_make_tport(
1441 struct target_fabric_configfs
*tf
,
1442 struct config_group
*group
,
1445 struct usbg_tport
*tport
;
1446 const char *wnn_name
;
1449 wnn_name
= usbg_check_wwn(name
);
1451 return ERR_PTR(-EINVAL
);
1453 tport
= kzalloc(sizeof(struct usbg_tport
), GFP_KERNEL
);
1455 return ERR_PTR(-ENOMEM
);
1456 tport
->tport_wwpn
= wwpn
;
1457 snprintf(tport
->tport_name
, sizeof(tport
->tport_name
), "%s", wnn_name
);
1458 return &tport
->tport_wwn
;
1461 static void usbg_drop_tport(struct se_wwn
*wwn
)
1463 struct usbg_tport
*tport
= container_of(wwn
,
1464 struct usbg_tport
, tport_wwn
);
1469 * If somebody feels like dropping the version property, go ahead.
1471 static ssize_t
usbg_wwn_version_show(struct config_item
*item
, char *page
)
1473 return sprintf(page
, "usb-gadget fabric module\n");
1476 CONFIGFS_ATTR_RO(usbg_wwn_
, version
);
1478 static struct configfs_attribute
*usbg_wwn_attrs
[] = {
1479 &usbg_wwn_attr_version
,
1483 static ssize_t
tcm_usbg_tpg_enable_show(struct config_item
*item
, char *page
)
1485 struct se_portal_group
*se_tpg
= to_tpg(item
);
1486 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1488 return snprintf(page
, PAGE_SIZE
, "%u\n", tpg
->gadget_connect
);
1491 static int usbg_attach(struct usbg_tpg
*);
1492 static void usbg_detach(struct usbg_tpg
*);
1494 static ssize_t
tcm_usbg_tpg_enable_store(struct config_item
*item
,
1495 const char *page
, size_t count
)
1497 struct se_portal_group
*se_tpg
= to_tpg(item
);
1498 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1502 ret
= kstrtoul(page
, 0, &op
);
1508 if (op
&& tpg
->gadget_connect
) {
1512 if (!op
&& !tpg
->gadget_connect
) {
1518 ret
= usbg_attach(tpg
);
1524 tpg
->gadget_connect
= op
;
1531 static ssize_t
tcm_usbg_tpg_nexus_show(struct config_item
*item
, char *page
)
1533 struct se_portal_group
*se_tpg
= to_tpg(item
);
1534 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1535 struct tcm_usbg_nexus
*tv_nexus
;
1538 mutex_lock(&tpg
->tpg_mutex
);
1539 tv_nexus
= tpg
->tpg_nexus
;
1544 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1545 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1547 mutex_unlock(&tpg
->tpg_mutex
);
1551 static int tcm_usbg_make_nexus(struct usbg_tpg
*tpg
, char *name
)
1553 struct se_portal_group
*se_tpg
;
1554 struct tcm_usbg_nexus
*tv_nexus
;
1557 mutex_lock(&tpg
->tpg_mutex
);
1558 if (tpg
->tpg_nexus
) {
1560 pr_debug("tpg->tpg_nexus already exists\n");
1563 se_tpg
= &tpg
->se_tpg
;
1566 tv_nexus
= kzalloc(sizeof(*tv_nexus
), GFP_KERNEL
);
1569 tv_nexus
->tvn_se_sess
= transport_init_session(TARGET_PROT_NORMAL
);
1570 if (IS_ERR(tv_nexus
->tvn_se_sess
))
1574 * Since we are running in 'demo mode' this call with generate a
1575 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1576 * the SCSI Initiator port name of the passed configfs group 'name'.
1578 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1580 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1581 pr_debug("core_tpg_check_initiator_node_acl() failed"
1586 * Now register the TCM vHost virtual I_T Nexus as active.
1588 transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1589 tv_nexus
->tvn_se_sess
, tv_nexus
);
1590 tpg
->tpg_nexus
= tv_nexus
;
1591 mutex_unlock(&tpg
->tpg_mutex
);
1595 transport_free_session(tv_nexus
->tvn_se_sess
);
1599 mutex_unlock(&tpg
->tpg_mutex
);
1603 static int tcm_usbg_drop_nexus(struct usbg_tpg
*tpg
)
1605 struct se_session
*se_sess
;
1606 struct tcm_usbg_nexus
*tv_nexus
;
1609 mutex_lock(&tpg
->tpg_mutex
);
1610 tv_nexus
= tpg
->tpg_nexus
;
1614 se_sess
= tv_nexus
->tvn_se_sess
;
1618 if (atomic_read(&tpg
->tpg_port_count
)) {
1620 pr_err("Unable to remove Host I_T Nexus with"
1621 " active TPG port count: %d\n",
1622 atomic_read(&tpg
->tpg_port_count
));
1626 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1627 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1629 * Release the SCSI I_T Nexus to the emulated vHost Target Port
1631 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1632 tpg
->tpg_nexus
= NULL
;
1637 mutex_unlock(&tpg
->tpg_mutex
);
1641 static ssize_t
tcm_usbg_tpg_nexus_store(struct config_item
*item
,
1642 const char *page
, size_t count
)
1644 struct se_portal_group
*se_tpg
= to_tpg(item
);
1645 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1646 unsigned char i_port
[USBG_NAMELEN
], *ptr
;
1649 if (!strncmp(page
, "NULL", 4)) {
1650 ret
= tcm_usbg_drop_nexus(tpg
);
1651 return (!ret
) ? count
: ret
;
1653 if (strlen(page
) >= USBG_NAMELEN
) {
1654 pr_err("Emulated NAA Sas Address: %s, exceeds"
1655 " max: %d\n", page
, USBG_NAMELEN
);
1658 snprintf(i_port
, USBG_NAMELEN
, "%s", page
);
1660 ptr
= strstr(i_port
, "naa.");
1662 pr_err("Missing 'naa.' prefix\n");
1666 if (i_port
[strlen(i_port
) - 1] == '\n')
1667 i_port
[strlen(i_port
) - 1] = '\0';
1669 ret
= tcm_usbg_make_nexus(tpg
, &i_port
[0]);
1675 CONFIGFS_ATTR(tcm_usbg_tpg_
, enable
);
1676 CONFIGFS_ATTR(tcm_usbg_tpg_
, nexus
);
1678 static struct configfs_attribute
*usbg_base_attrs
[] = {
1679 &tcm_usbg_tpg_attr_enable
,
1680 &tcm_usbg_tpg_attr_nexus
,
1684 static int usbg_port_link(struct se_portal_group
*se_tpg
, struct se_lun
*lun
)
1686 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1688 atomic_inc(&tpg
->tpg_port_count
);
1689 smp_mb__after_atomic();
1693 static void usbg_port_unlink(struct se_portal_group
*se_tpg
,
1694 struct se_lun
*se_lun
)
1696 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1698 atomic_dec(&tpg
->tpg_port_count
);
1699 smp_mb__after_atomic();
1702 static int usbg_check_stop_free(struct se_cmd
*se_cmd
)
1704 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1707 kref_put(&cmd
->ref
, usbg_cmd_release
);
1711 static const struct target_core_fabric_ops usbg_ops
= {
1712 .module
= THIS_MODULE
,
1713 .name
= "usb_gadget",
1714 .get_fabric_name
= usbg_get_fabric_name
,
1715 .tpg_get_wwn
= usbg_get_fabric_wwn
,
1716 .tpg_get_tag
= usbg_get_tag
,
1717 .tpg_check_demo_mode
= usbg_check_true
,
1718 .tpg_check_demo_mode_cache
= usbg_check_false
,
1719 .tpg_check_demo_mode_write_protect
= usbg_check_false
,
1720 .tpg_check_prod_mode_write_protect
= usbg_check_false
,
1721 .tpg_get_inst_index
= usbg_tpg_get_inst_index
,
1722 .release_cmd
= usbg_release_cmd
,
1723 .shutdown_session
= usbg_shutdown_session
,
1724 .close_session
= usbg_close_session
,
1725 .sess_get_index
= usbg_sess_get_index
,
1726 .sess_get_initiator_sid
= NULL
,
1727 .write_pending
= usbg_send_write_request
,
1728 .write_pending_status
= usbg_write_pending_status
,
1729 .set_default_node_attributes
= usbg_set_default_node_attrs
,
1730 .get_cmd_state
= usbg_get_cmd_state
,
1731 .queue_data_in
= usbg_send_read_response
,
1732 .queue_status
= usbg_send_status_response
,
1733 .queue_tm_rsp
= usbg_queue_tm_rsp
,
1734 .aborted_task
= usbg_aborted_task
,
1735 .check_stop_free
= usbg_check_stop_free
,
1737 .fabric_make_wwn
= usbg_make_tport
,
1738 .fabric_drop_wwn
= usbg_drop_tport
,
1739 .fabric_make_tpg
= usbg_make_tpg
,
1740 .fabric_drop_tpg
= usbg_drop_tpg
,
1741 .fabric_post_link
= usbg_port_link
,
1742 .fabric_pre_unlink
= usbg_port_unlink
,
1743 .fabric_init_nodeacl
= usbg_init_nodeacl
,
1745 .tfc_wwn_attrs
= usbg_wwn_attrs
,
1746 .tfc_tpg_base_attrs
= usbg_base_attrs
,
1749 /* Start gadget.c code */
1751 static struct usb_interface_descriptor bot_intf_desc
= {
1752 .bLength
= sizeof(bot_intf_desc
),
1753 .bDescriptorType
= USB_DT_INTERFACE
,
1755 .bAlternateSetting
= USB_G_ALT_INT_BBB
,
1756 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
1757 .bInterfaceSubClass
= USB_SC_SCSI
,
1758 .bInterfaceProtocol
= USB_PR_BULK
,
1761 static struct usb_interface_descriptor uasp_intf_desc
= {
1762 .bLength
= sizeof(uasp_intf_desc
),
1763 .bDescriptorType
= USB_DT_INTERFACE
,
1765 .bAlternateSetting
= USB_G_ALT_INT_UAS
,
1766 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
1767 .bInterfaceSubClass
= USB_SC_SCSI
,
1768 .bInterfaceProtocol
= USB_PR_UAS
,
1771 static struct usb_endpoint_descriptor uasp_bi_desc
= {
1772 .bLength
= USB_DT_ENDPOINT_SIZE
,
1773 .bDescriptorType
= USB_DT_ENDPOINT
,
1774 .bEndpointAddress
= USB_DIR_IN
,
1775 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1776 .wMaxPacketSize
= cpu_to_le16(512),
1779 static struct usb_endpoint_descriptor uasp_fs_bi_desc
= {
1780 .bLength
= USB_DT_ENDPOINT_SIZE
,
1781 .bDescriptorType
= USB_DT_ENDPOINT
,
1782 .bEndpointAddress
= USB_DIR_IN
,
1783 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1786 static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc
= {
1787 .bLength
= sizeof(uasp_bi_pipe_desc
),
1788 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1789 .bPipeID
= DATA_IN_PIPE_ID
,
1792 static struct usb_endpoint_descriptor uasp_ss_bi_desc
= {
1793 .bLength
= USB_DT_ENDPOINT_SIZE
,
1794 .bDescriptorType
= USB_DT_ENDPOINT
,
1795 .bEndpointAddress
= USB_DIR_IN
,
1796 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1797 .wMaxPacketSize
= cpu_to_le16(1024),
1800 static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc
= {
1801 .bLength
= sizeof(uasp_bi_ep_comp_desc
),
1802 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1804 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
1805 .wBytesPerInterval
= 0,
1808 static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc
= {
1809 .bLength
= sizeof(bot_bi_ep_comp_desc
),
1810 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1814 static struct usb_endpoint_descriptor uasp_bo_desc
= {
1815 .bLength
= USB_DT_ENDPOINT_SIZE
,
1816 .bDescriptorType
= USB_DT_ENDPOINT
,
1817 .bEndpointAddress
= USB_DIR_OUT
,
1818 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1819 .wMaxPacketSize
= cpu_to_le16(512),
1822 static struct usb_endpoint_descriptor uasp_fs_bo_desc
= {
1823 .bLength
= USB_DT_ENDPOINT_SIZE
,
1824 .bDescriptorType
= USB_DT_ENDPOINT
,
1825 .bEndpointAddress
= USB_DIR_OUT
,
1826 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1829 static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc
= {
1830 .bLength
= sizeof(uasp_bo_pipe_desc
),
1831 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1832 .bPipeID
= DATA_OUT_PIPE_ID
,
1835 static struct usb_endpoint_descriptor uasp_ss_bo_desc
= {
1836 .bLength
= USB_DT_ENDPOINT_SIZE
,
1837 .bDescriptorType
= USB_DT_ENDPOINT
,
1838 .bEndpointAddress
= USB_DIR_OUT
,
1839 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1840 .wMaxPacketSize
= cpu_to_le16(0x400),
1843 static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc
= {
1844 .bLength
= sizeof(uasp_bo_ep_comp_desc
),
1845 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1846 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
1849 static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc
= {
1850 .bLength
= sizeof(bot_bo_ep_comp_desc
),
1851 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1854 static struct usb_endpoint_descriptor uasp_status_desc
= {
1855 .bLength
= USB_DT_ENDPOINT_SIZE
,
1856 .bDescriptorType
= USB_DT_ENDPOINT
,
1857 .bEndpointAddress
= USB_DIR_IN
,
1858 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1859 .wMaxPacketSize
= cpu_to_le16(512),
1862 static struct usb_endpoint_descriptor uasp_fs_status_desc
= {
1863 .bLength
= USB_DT_ENDPOINT_SIZE
,
1864 .bDescriptorType
= USB_DT_ENDPOINT
,
1865 .bEndpointAddress
= USB_DIR_IN
,
1866 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1869 static struct usb_pipe_usage_descriptor uasp_status_pipe_desc
= {
1870 .bLength
= sizeof(uasp_status_pipe_desc
),
1871 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1872 .bPipeID
= STATUS_PIPE_ID
,
1875 static struct usb_endpoint_descriptor uasp_ss_status_desc
= {
1876 .bLength
= USB_DT_ENDPOINT_SIZE
,
1877 .bDescriptorType
= USB_DT_ENDPOINT
,
1878 .bEndpointAddress
= USB_DIR_IN
,
1879 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1880 .wMaxPacketSize
= cpu_to_le16(1024),
1883 static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc
= {
1884 .bLength
= sizeof(uasp_status_in_ep_comp_desc
),
1885 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1886 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
1889 static struct usb_endpoint_descriptor uasp_cmd_desc
= {
1890 .bLength
= USB_DT_ENDPOINT_SIZE
,
1891 .bDescriptorType
= USB_DT_ENDPOINT
,
1892 .bEndpointAddress
= USB_DIR_OUT
,
1893 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1894 .wMaxPacketSize
= cpu_to_le16(512),
1897 static struct usb_endpoint_descriptor uasp_fs_cmd_desc
= {
1898 .bLength
= USB_DT_ENDPOINT_SIZE
,
1899 .bDescriptorType
= USB_DT_ENDPOINT
,
1900 .bEndpointAddress
= USB_DIR_OUT
,
1901 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1904 static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc
= {
1905 .bLength
= sizeof(uasp_cmd_pipe_desc
),
1906 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1907 .bPipeID
= CMD_PIPE_ID
,
1910 static struct usb_endpoint_descriptor uasp_ss_cmd_desc
= {
1911 .bLength
= USB_DT_ENDPOINT_SIZE
,
1912 .bDescriptorType
= USB_DT_ENDPOINT
,
1913 .bEndpointAddress
= USB_DIR_OUT
,
1914 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1915 .wMaxPacketSize
= cpu_to_le16(1024),
1918 static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc
= {
1919 .bLength
= sizeof(uasp_cmd_comp_desc
),
1920 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1923 static struct usb_descriptor_header
*uasp_fs_function_desc
[] = {
1924 (struct usb_descriptor_header
*) &bot_intf_desc
,
1925 (struct usb_descriptor_header
*) &uasp_fs_bi_desc
,
1926 (struct usb_descriptor_header
*) &uasp_fs_bo_desc
,
1928 (struct usb_descriptor_header
*) &uasp_intf_desc
,
1929 (struct usb_descriptor_header
*) &uasp_fs_bi_desc
,
1930 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
1931 (struct usb_descriptor_header
*) &uasp_fs_bo_desc
,
1932 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
1933 (struct usb_descriptor_header
*) &uasp_fs_status_desc
,
1934 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
1935 (struct usb_descriptor_header
*) &uasp_fs_cmd_desc
,
1936 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
1940 static struct usb_descriptor_header
*uasp_hs_function_desc
[] = {
1941 (struct usb_descriptor_header
*) &bot_intf_desc
,
1942 (struct usb_descriptor_header
*) &uasp_bi_desc
,
1943 (struct usb_descriptor_header
*) &uasp_bo_desc
,
1945 (struct usb_descriptor_header
*) &uasp_intf_desc
,
1946 (struct usb_descriptor_header
*) &uasp_bi_desc
,
1947 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
1948 (struct usb_descriptor_header
*) &uasp_bo_desc
,
1949 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
1950 (struct usb_descriptor_header
*) &uasp_status_desc
,
1951 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
1952 (struct usb_descriptor_header
*) &uasp_cmd_desc
,
1953 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
1957 static struct usb_descriptor_header
*uasp_ss_function_desc
[] = {
1958 (struct usb_descriptor_header
*) &bot_intf_desc
,
1959 (struct usb_descriptor_header
*) &uasp_ss_bi_desc
,
1960 (struct usb_descriptor_header
*) &bot_bi_ep_comp_desc
,
1961 (struct usb_descriptor_header
*) &uasp_ss_bo_desc
,
1962 (struct usb_descriptor_header
*) &bot_bo_ep_comp_desc
,
1964 (struct usb_descriptor_header
*) &uasp_intf_desc
,
1965 (struct usb_descriptor_header
*) &uasp_ss_bi_desc
,
1966 (struct usb_descriptor_header
*) &uasp_bi_ep_comp_desc
,
1967 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
1968 (struct usb_descriptor_header
*) &uasp_ss_bo_desc
,
1969 (struct usb_descriptor_header
*) &uasp_bo_ep_comp_desc
,
1970 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
1971 (struct usb_descriptor_header
*) &uasp_ss_status_desc
,
1972 (struct usb_descriptor_header
*) &uasp_status_in_ep_comp_desc
,
1973 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
1974 (struct usb_descriptor_header
*) &uasp_ss_cmd_desc
,
1975 (struct usb_descriptor_header
*) &uasp_cmd_comp_desc
,
1976 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
1980 #define UAS_VENDOR_ID 0x0525 /* NetChip */
1981 #define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
1983 static struct usb_device_descriptor usbg_device_desc
= {
1984 .bLength
= sizeof(usbg_device_desc
),
1985 .bDescriptorType
= USB_DT_DEVICE
,
1986 .bcdUSB
= cpu_to_le16(0x0200),
1987 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
1988 .idVendor
= cpu_to_le16(UAS_VENDOR_ID
),
1989 .idProduct
= cpu_to_le16(UAS_PRODUCT_ID
),
1990 .bNumConfigurations
= 1,
1993 static struct usb_string usbg_us_strings
[] = {
1994 [USB_GADGET_MANUFACTURER_IDX
].s
= "Target Manufactor",
1995 [USB_GADGET_PRODUCT_IDX
].s
= "Target Product",
1996 [USB_GADGET_SERIAL_IDX
].s
= "000000000001",
1997 [USB_G_STR_CONFIG
].s
= "default config",
1998 [USB_G_STR_INT_UAS
].s
= "USB Attached SCSI",
1999 [USB_G_STR_INT_BBB
].s
= "Bulk Only Transport",
2003 static struct usb_gadget_strings usbg_stringtab
= {
2005 .strings
= usbg_us_strings
,
2008 static struct usb_gadget_strings
*usbg_strings
[] = {
2013 static int guas_unbind(struct usb_composite_dev
*cdev
)
2018 static struct usb_configuration usbg_config_driver
= {
2019 .label
= "Linux Target",
2020 .bConfigurationValue
= 1,
2021 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
2024 static int usbg_bind(struct usb_configuration
*c
, struct usb_function
*f
)
2026 struct f_uas
*fu
= to_f_uas(f
);
2027 struct usb_gadget
*gadget
= c
->cdev
->gadget
;
2032 iface
= usb_interface_id(c
, f
);
2036 bot_intf_desc
.bInterfaceNumber
= iface
;
2037 uasp_intf_desc
.bInterfaceNumber
= iface
;
2039 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_bi_desc
,
2040 &uasp_bi_ep_comp_desc
);
2045 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_bo_desc
,
2046 &uasp_bo_ep_comp_desc
);
2051 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_status_desc
,
2052 &uasp_status_in_ep_comp_desc
);
2057 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_cmd_desc
,
2058 &uasp_cmd_comp_desc
);
2063 /* Assume endpoint addresses are the same for both speeds */
2064 uasp_bi_desc
.bEndpointAddress
= uasp_ss_bi_desc
.bEndpointAddress
;
2065 uasp_bo_desc
.bEndpointAddress
= uasp_ss_bo_desc
.bEndpointAddress
;
2066 uasp_status_desc
.bEndpointAddress
=
2067 uasp_ss_status_desc
.bEndpointAddress
;
2068 uasp_cmd_desc
.bEndpointAddress
= uasp_ss_cmd_desc
.bEndpointAddress
;
2070 uasp_fs_bi_desc
.bEndpointAddress
= uasp_ss_bi_desc
.bEndpointAddress
;
2071 uasp_fs_bo_desc
.bEndpointAddress
= uasp_ss_bo_desc
.bEndpointAddress
;
2072 uasp_fs_status_desc
.bEndpointAddress
=
2073 uasp_ss_status_desc
.bEndpointAddress
;
2074 uasp_fs_cmd_desc
.bEndpointAddress
= uasp_ss_cmd_desc
.bEndpointAddress
;
2076 ret
= usb_assign_descriptors(f
, uasp_fs_function_desc
,
2077 uasp_hs_function_desc
, uasp_ss_function_desc
);
2083 pr_err("Can't claim all required eps\n");
2087 static void usbg_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
2089 struct f_uas
*fu
= to_f_uas(f
);
2091 usb_free_all_descriptors(f
);
2095 struct guas_setup_wq
{
2096 struct work_struct work
;
2101 static void usbg_delayed_set_alt(struct work_struct
*wq
)
2103 struct guas_setup_wq
*work
= container_of(wq
, struct guas_setup_wq
,
2105 struct f_uas
*fu
= work
->fu
;
2106 int alt
= work
->alt
;
2110 if (fu
->flags
& USBG_IS_BOT
)
2111 bot_cleanup_old_alt(fu
);
2112 if (fu
->flags
& USBG_IS_UAS
)
2113 uasp_cleanup_old_alt(fu
);
2115 if (alt
== USB_G_ALT_INT_BBB
)
2117 else if (alt
== USB_G_ALT_INT_UAS
)
2119 usb_composite_setup_continue(fu
->function
.config
->cdev
);
2122 static int usbg_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
2124 struct f_uas
*fu
= to_f_uas(f
);
2126 if ((alt
== USB_G_ALT_INT_BBB
) || (alt
== USB_G_ALT_INT_UAS
)) {
2127 struct guas_setup_wq
*work
;
2129 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
2132 INIT_WORK(&work
->work
, usbg_delayed_set_alt
);
2135 schedule_work(&work
->work
);
2136 return USB_GADGET_DELAYED_STATUS
;
2141 static void usbg_disable(struct usb_function
*f
)
2143 struct f_uas
*fu
= to_f_uas(f
);
2145 if (fu
->flags
& USBG_IS_UAS
)
2146 uasp_cleanup_old_alt(fu
);
2147 else if (fu
->flags
& USBG_IS_BOT
)
2148 bot_cleanup_old_alt(fu
);
2152 static int usbg_setup(struct usb_function
*f
,
2153 const struct usb_ctrlrequest
*ctrl
)
2155 struct f_uas
*fu
= to_f_uas(f
);
2157 if (!(fu
->flags
& USBG_IS_BOT
))
2160 return usbg_bot_setup(f
, ctrl
);
2163 static int usbg_cfg_bind(struct usb_configuration
*c
)
2168 fu
= kzalloc(sizeof(*fu
), GFP_KERNEL
);
2171 fu
->function
.name
= "Target Function";
2172 fu
->function
.bind
= usbg_bind
;
2173 fu
->function
.unbind
= usbg_unbind
;
2174 fu
->function
.set_alt
= usbg_set_alt
;
2175 fu
->function
.setup
= usbg_setup
;
2176 fu
->function
.disable
= usbg_disable
;
2177 fu
->tpg
= the_only_tpg_I_currently_have
;
2179 bot_intf_desc
.iInterface
= usbg_us_strings
[USB_G_STR_INT_BBB
].id
;
2180 uasp_intf_desc
.iInterface
= usbg_us_strings
[USB_G_STR_INT_UAS
].id
;
2182 ret
= usb_add_function(c
, &fu
->function
);
2192 static int usb_target_bind(struct usb_composite_dev
*cdev
)
2196 ret
= usb_string_ids_tab(cdev
, usbg_us_strings
);
2200 usbg_device_desc
.iManufacturer
=
2201 usbg_us_strings
[USB_GADGET_MANUFACTURER_IDX
].id
;
2202 usbg_device_desc
.iProduct
= usbg_us_strings
[USB_GADGET_PRODUCT_IDX
].id
;
2203 usbg_device_desc
.iSerialNumber
=
2204 usbg_us_strings
[USB_GADGET_SERIAL_IDX
].id
;
2205 usbg_config_driver
.iConfiguration
=
2206 usbg_us_strings
[USB_G_STR_CONFIG
].id
;
2208 ret
= usb_add_config(cdev
, &usbg_config_driver
,
2212 usb_composite_overwrite_options(cdev
, &coverwrite
);
2216 static struct usb_composite_driver usbg_driver
= {
2218 .dev
= &usbg_device_desc
,
2219 .strings
= usbg_strings
,
2220 .max_speed
= USB_SPEED_SUPER
,
2221 .bind
= usb_target_bind
,
2222 .unbind
= guas_unbind
,
2225 static int usbg_attach(struct usbg_tpg
*tpg
)
2227 return usb_composite_probe(&usbg_driver
);
2230 static void usbg_detach(struct usbg_tpg
*tpg
)
2232 usb_composite_unregister(&usbg_driver
);
2235 static int __init
usb_target_gadget_init(void)
2237 return target_register_template(&usbg_ops
);
2239 module_init(usb_target_gadget_init
);
2241 static void __exit
usb_target_gadget_exit(void)
2243 target_unregister_template(&usbg_ops
);
2245 module_exit(usb_target_gadget_exit
);
2247 MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
2248 MODULE_DESCRIPTION("usb-gadget fabric");
2249 MODULE_LICENSE("GPL v2");