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 <target/target_core_fabric_configfs.h>
23 #include <target/configfs_macros.h>
24 #include <asm/unaligned.h>
26 #include "tcm_usb_gadget.h"
28 USB_GADGET_COMPOSITE_OPTIONS();
30 static inline struct f_uas
*to_f_uas(struct usb_function
*f
)
32 return container_of(f
, struct f_uas
, function
);
35 static void usbg_cmd_release(struct kref
*);
37 static inline void usbg_cleanup_cmd(struct usbg_cmd
*cmd
)
39 kref_put(&cmd
->ref
, usbg_cmd_release
);
42 /* Start bot.c code */
44 static int bot_enqueue_cmd_cbw(struct f_uas
*fu
)
48 if (fu
->flags
& USBG_BOT_CMD_PEND
)
51 ret
= usb_ep_queue(fu
->ep_out
, fu
->cmd
.req
, GFP_ATOMIC
);
53 fu
->flags
|= USBG_BOT_CMD_PEND
;
57 static void bot_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
59 struct usbg_cmd
*cmd
= req
->context
;
60 struct f_uas
*fu
= cmd
->fu
;
62 usbg_cleanup_cmd(cmd
);
63 if (req
->status
< 0) {
64 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
68 /* CSW completed, wait for next CBW */
69 bot_enqueue_cmd_cbw(fu
);
72 static void bot_enqueue_sense_code(struct f_uas
*fu
, struct usbg_cmd
*cmd
)
74 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
77 unsigned int csw_stat
;
79 csw_stat
= cmd
->csw_code
;
82 * We can't send SENSE as a response. So we take ASC & ASCQ from our
83 * sense buffer and queue it and hope the host sends a REQUEST_SENSE
84 * command where it learns why we failed.
86 sense
= cmd
->sense_iu
.sense
;
88 csw
->Tag
= cmd
->bot_tag
;
89 csw
->Status
= csw_stat
;
90 fu
->bot_status
.req
->context
= cmd
;
91 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_status
.req
, GFP_ATOMIC
);
93 pr_err("%s(%d) ERR: %d\n", __func__
, __LINE__
, ret
);
96 static void bot_err_compl(struct usb_ep
*ep
, struct usb_request
*req
)
98 struct usbg_cmd
*cmd
= req
->context
;
99 struct f_uas
*fu
= cmd
->fu
;
102 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
105 if (cmd
->data_len
> ep
->maxpacket
) {
106 req
->length
= ep
->maxpacket
;
107 cmd
->data_len
-= ep
->maxpacket
;
109 req
->length
= cmd
->data_len
;
113 usb_ep_queue(ep
, req
, GFP_ATOMIC
);
116 bot_enqueue_sense_code(fu
, cmd
);
119 static void bot_send_bad_status(struct usbg_cmd
*cmd
)
121 struct f_uas
*fu
= cmd
->fu
;
122 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
123 struct usb_request
*req
;
126 csw
->Residue
= cpu_to_le32(cmd
->data_len
);
131 req
= fu
->bot_req_in
;
134 req
= fu
->bot_req_out
;
137 if (cmd
->data_len
> fu
->ep_in
->maxpacket
) {
138 req
->length
= ep
->maxpacket
;
139 cmd
->data_len
-= ep
->maxpacket
;
141 req
->length
= cmd
->data_len
;
144 req
->complete
= bot_err_compl
;
146 req
->buf
= fu
->cmd
.buf
;
147 usb_ep_queue(ep
, req
, GFP_KERNEL
);
149 bot_enqueue_sense_code(fu
, cmd
);
153 static int bot_send_status(struct usbg_cmd
*cmd
, bool moved_data
)
155 struct f_uas
*fu
= cmd
->fu
;
156 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
159 if (cmd
->se_cmd
.scsi_status
== SAM_STAT_GOOD
) {
160 if (!moved_data
&& cmd
->data_len
) {
162 * the host wants to move data, we don't. Fill / empty
163 * the pipe and then send the csw with reside set.
165 cmd
->csw_code
= US_BULK_STAT_OK
;
166 bot_send_bad_status(cmd
);
170 csw
->Tag
= cmd
->bot_tag
;
171 csw
->Residue
= cpu_to_le32(0);
172 csw
->Status
= US_BULK_STAT_OK
;
173 fu
->bot_status
.req
->context
= cmd
;
175 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_status
.req
, GFP_KERNEL
);
177 pr_err("%s(%d) ERR: %d\n", __func__
, __LINE__
, ret
);
179 cmd
->csw_code
= US_BULK_STAT_FAIL
;
180 bot_send_bad_status(cmd
);
186 * Called after command (no data transfer) or after the write (to device)
187 * operation is completed
189 static int bot_send_status_response(struct usbg_cmd
*cmd
)
191 bool moved_data
= false;
195 return bot_send_status(cmd
, moved_data
);
198 /* Read request completed, now we have to send the CSW */
199 static void bot_read_compl(struct usb_ep
*ep
, struct usb_request
*req
)
201 struct usbg_cmd
*cmd
= req
->context
;
204 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
206 bot_send_status(cmd
, true);
209 static int bot_send_read_response(struct usbg_cmd
*cmd
)
211 struct f_uas
*fu
= cmd
->fu
;
212 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
213 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
216 if (!cmd
->data_len
) {
217 cmd
->csw_code
= US_BULK_STAT_PHASE
;
218 bot_send_bad_status(cmd
);
222 if (!gadget
->sg_supported
) {
223 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
227 sg_copy_to_buffer(se_cmd
->t_data_sg
,
228 se_cmd
->t_data_nents
,
230 se_cmd
->data_length
);
232 fu
->bot_req_in
->buf
= cmd
->data_buf
;
234 fu
->bot_req_in
->buf
= NULL
;
235 fu
->bot_req_in
->num_sgs
= se_cmd
->t_data_nents
;
236 fu
->bot_req_in
->sg
= se_cmd
->t_data_sg
;
239 fu
->bot_req_in
->complete
= bot_read_compl
;
240 fu
->bot_req_in
->length
= se_cmd
->data_length
;
241 fu
->bot_req_in
->context
= cmd
;
242 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_req_in
, GFP_ATOMIC
);
244 pr_err("%s(%d)\n", __func__
, __LINE__
);
248 static void usbg_data_write_cmpl(struct usb_ep
*, struct usb_request
*);
249 static int usbg_prepare_w_request(struct usbg_cmd
*, struct usb_request
*);
251 static int bot_send_write_request(struct usbg_cmd
*cmd
)
253 struct f_uas
*fu
= cmd
->fu
;
254 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
255 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
258 init_completion(&cmd
->write_complete
);
261 if (!cmd
->data_len
) {
262 cmd
->csw_code
= US_BULK_STAT_PHASE
;
266 if (!gadget
->sg_supported
) {
267 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_KERNEL
);
271 fu
->bot_req_out
->buf
= cmd
->data_buf
;
273 fu
->bot_req_out
->buf
= NULL
;
274 fu
->bot_req_out
->num_sgs
= se_cmd
->t_data_nents
;
275 fu
->bot_req_out
->sg
= se_cmd
->t_data_sg
;
278 fu
->bot_req_out
->complete
= usbg_data_write_cmpl
;
279 fu
->bot_req_out
->length
= se_cmd
->data_length
;
280 fu
->bot_req_out
->context
= cmd
;
282 ret
= usbg_prepare_w_request(cmd
, fu
->bot_req_out
);
285 ret
= usb_ep_queue(fu
->ep_out
, fu
->bot_req_out
, GFP_KERNEL
);
287 pr_err("%s(%d)\n", __func__
, __LINE__
);
289 wait_for_completion(&cmd
->write_complete
);
290 target_execute_cmd(se_cmd
);
295 static int bot_submit_command(struct f_uas
*, void *, unsigned int);
297 static void bot_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
299 struct f_uas
*fu
= req
->context
;
302 fu
->flags
&= ~USBG_BOT_CMD_PEND
;
307 ret
= bot_submit_command(fu
, req
->buf
, req
->actual
);
309 pr_err("%s(%d): %d\n", __func__
, __LINE__
, ret
);
312 static int bot_prepare_reqs(struct f_uas
*fu
)
316 fu
->bot_req_in
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
320 fu
->bot_req_out
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
321 if (!fu
->bot_req_out
)
324 fu
->cmd
.req
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
328 fu
->bot_status
.req
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
329 if (!fu
->bot_status
.req
)
332 fu
->bot_status
.req
->buf
= &fu
->bot_status
.csw
;
333 fu
->bot_status
.req
->length
= US_BULK_CS_WRAP_LEN
;
334 fu
->bot_status
.req
->complete
= bot_status_complete
;
335 fu
->bot_status
.csw
.Signature
= cpu_to_le32(US_BULK_CS_SIGN
);
337 fu
->cmd
.buf
= kmalloc(fu
->ep_out
->maxpacket
, GFP_KERNEL
);
341 fu
->cmd
.req
->complete
= bot_cmd_complete
;
342 fu
->cmd
.req
->buf
= fu
->cmd
.buf
;
343 fu
->cmd
.req
->length
= fu
->ep_out
->maxpacket
;
344 fu
->cmd
.req
->context
= fu
;
346 ret
= bot_enqueue_cmd_cbw(fu
);
354 usb_ep_free_request(fu
->ep_in
, fu
->bot_status
.req
);
356 usb_ep_free_request(fu
->ep_out
, fu
->cmd
.req
);
359 usb_ep_free_request(fu
->ep_out
, fu
->bot_req_out
);
360 fu
->bot_req_out
= NULL
;
362 usb_ep_free_request(fu
->ep_in
, fu
->bot_req_in
);
363 fu
->bot_req_in
= NULL
;
365 pr_err("BOT: endpoint setup failed\n");
369 static void bot_cleanup_old_alt(struct f_uas
*fu
)
371 if (!(fu
->flags
& USBG_ENABLED
))
374 usb_ep_disable(fu
->ep_in
);
375 usb_ep_disable(fu
->ep_out
);
380 usb_ep_free_request(fu
->ep_in
, fu
->bot_req_in
);
381 usb_ep_free_request(fu
->ep_out
, fu
->bot_req_out
);
382 usb_ep_free_request(fu
->ep_out
, fu
->cmd
.req
);
383 usb_ep_free_request(fu
->ep_out
, fu
->bot_status
.req
);
387 fu
->bot_req_in
= NULL
;
388 fu
->bot_req_out
= NULL
;
390 fu
->bot_status
.req
= NULL
;
394 static void bot_set_alt(struct f_uas
*fu
)
396 struct usb_function
*f
= &fu
->function
;
397 struct usb_gadget
*gadget
= f
->config
->cdev
->gadget
;
400 fu
->flags
= USBG_IS_BOT
;
402 config_ep_by_speed(gadget
, f
, fu
->ep_in
);
403 ret
= usb_ep_enable(fu
->ep_in
);
407 config_ep_by_speed(gadget
, f
, fu
->ep_out
);
408 ret
= usb_ep_enable(fu
->ep_out
);
412 ret
= bot_prepare_reqs(fu
);
415 fu
->flags
|= USBG_ENABLED
;
416 pr_info("Using the BOT protocol\n");
419 usb_ep_disable(fu
->ep_out
);
421 usb_ep_disable(fu
->ep_in
);
423 fu
->flags
= USBG_IS_BOT
;
426 static int usbg_bot_setup(struct usb_function
*f
,
427 const struct usb_ctrlrequest
*ctrl
)
429 struct f_uas
*fu
= to_f_uas(f
);
430 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
431 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
432 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
436 switch (ctrl
->bRequest
) {
437 case US_BULK_GET_MAX_LUN
:
438 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_TYPE_CLASS
|
439 USB_RECIP_INTERFACE
))
446 luns
= atomic_read(&fu
->tpg
->tpg_port_count
);
448 pr_err("No LUNs configured?\n");
452 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
453 * accessed. The upper limit is 0xf
457 pr_info_once("Limiting the number of luns to 16\n");
460 ret_lun
= cdev
->req
->buf
;
462 cdev
->req
->length
= 1;
463 return usb_ep_queue(cdev
->gadget
->ep0
, cdev
->req
, GFP_ATOMIC
);
466 case US_BULK_RESET_REQUEST
:
467 /* XXX maybe we should remove previous requests for IN + OUT */
468 bot_enqueue_cmd_cbw(fu
);
475 /* Start uas.c code */
477 static void uasp_cleanup_one_stream(struct f_uas
*fu
, struct uas_stream
*stream
)
479 /* We have either all three allocated or none */
483 usb_ep_free_request(fu
->ep_in
, stream
->req_in
);
484 usb_ep_free_request(fu
->ep_out
, stream
->req_out
);
485 usb_ep_free_request(fu
->ep_status
, stream
->req_status
);
487 stream
->req_in
= NULL
;
488 stream
->req_out
= NULL
;
489 stream
->req_status
= NULL
;
492 static void uasp_free_cmdreq(struct f_uas
*fu
)
494 usb_ep_free_request(fu
->ep_cmd
, fu
->cmd
.req
);
500 static void uasp_cleanup_old_alt(struct f_uas
*fu
)
504 if (!(fu
->flags
& USBG_ENABLED
))
507 usb_ep_disable(fu
->ep_in
);
508 usb_ep_disable(fu
->ep_out
);
509 usb_ep_disable(fu
->ep_status
);
510 usb_ep_disable(fu
->ep_cmd
);
512 for (i
= 0; i
< UASP_SS_EP_COMP_NUM_STREAMS
; i
++)
513 uasp_cleanup_one_stream(fu
, &fu
->stream
[i
]);
514 uasp_free_cmdreq(fu
);
517 static void uasp_status_data_cmpl(struct usb_ep
*ep
, struct usb_request
*req
);
519 static int uasp_prepare_r_request(struct usbg_cmd
*cmd
)
521 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
522 struct f_uas
*fu
= cmd
->fu
;
523 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
524 struct uas_stream
*stream
= cmd
->stream
;
526 if (!gadget
->sg_supported
) {
527 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
531 sg_copy_to_buffer(se_cmd
->t_data_sg
,
532 se_cmd
->t_data_nents
,
534 se_cmd
->data_length
);
536 stream
->req_in
->buf
= cmd
->data_buf
;
538 stream
->req_in
->buf
= NULL
;
539 stream
->req_in
->num_sgs
= se_cmd
->t_data_nents
;
540 stream
->req_in
->sg
= se_cmd
->t_data_sg
;
543 stream
->req_in
->complete
= uasp_status_data_cmpl
;
544 stream
->req_in
->length
= se_cmd
->data_length
;
545 stream
->req_in
->context
= cmd
;
547 cmd
->state
= UASP_SEND_STATUS
;
551 static void uasp_prepare_status(struct usbg_cmd
*cmd
)
553 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
554 struct sense_iu
*iu
= &cmd
->sense_iu
;
555 struct uas_stream
*stream
= cmd
->stream
;
557 cmd
->state
= UASP_QUEUE_COMMAND
;
558 iu
->iu_id
= IU_ID_STATUS
;
559 iu
->tag
= cpu_to_be16(cmd
->tag
);
562 * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
564 iu
->len
= cpu_to_be16(se_cmd
->scsi_sense_length
);
565 iu
->status
= se_cmd
->scsi_status
;
566 stream
->req_status
->context
= cmd
;
567 stream
->req_status
->length
= se_cmd
->scsi_sense_length
+ 16;
568 stream
->req_status
->buf
= iu
;
569 stream
->req_status
->complete
= uasp_status_data_cmpl
;
572 static void uasp_status_data_cmpl(struct usb_ep
*ep
, struct usb_request
*req
)
574 struct usbg_cmd
*cmd
= req
->context
;
575 struct uas_stream
*stream
= cmd
->stream
;
576 struct f_uas
*fu
= cmd
->fu
;
582 switch (cmd
->state
) {
584 ret
= uasp_prepare_r_request(cmd
);
587 ret
= usb_ep_queue(fu
->ep_in
, stream
->req_in
, GFP_ATOMIC
);
589 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
592 case UASP_RECEIVE_DATA
:
593 ret
= usbg_prepare_w_request(cmd
, stream
->req_out
);
596 ret
= usb_ep_queue(fu
->ep_out
, stream
->req_out
, GFP_ATOMIC
);
598 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
601 case UASP_SEND_STATUS
:
602 uasp_prepare_status(cmd
);
603 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
606 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
609 case UASP_QUEUE_COMMAND
:
610 usbg_cleanup_cmd(cmd
);
611 usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
620 usbg_cleanup_cmd(cmd
);
623 static int uasp_send_status_response(struct usbg_cmd
*cmd
)
625 struct f_uas
*fu
= cmd
->fu
;
626 struct uas_stream
*stream
= cmd
->stream
;
627 struct sense_iu
*iu
= &cmd
->sense_iu
;
629 iu
->tag
= cpu_to_be16(cmd
->tag
);
630 stream
->req_status
->complete
= uasp_status_data_cmpl
;
631 stream
->req_status
->context
= cmd
;
633 uasp_prepare_status(cmd
);
634 return usb_ep_queue(fu
->ep_status
, stream
->req_status
, GFP_ATOMIC
);
637 static int uasp_send_read_response(struct usbg_cmd
*cmd
)
639 struct f_uas
*fu
= cmd
->fu
;
640 struct uas_stream
*stream
= cmd
->stream
;
641 struct sense_iu
*iu
= &cmd
->sense_iu
;
646 iu
->tag
= cpu_to_be16(cmd
->tag
);
647 if (fu
->flags
& USBG_USE_STREAMS
) {
649 ret
= uasp_prepare_r_request(cmd
);
652 ret
= usb_ep_queue(fu
->ep_in
, stream
->req_in
, GFP_ATOMIC
);
654 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
655 kfree(cmd
->data_buf
);
656 cmd
->data_buf
= NULL
;
661 iu
->iu_id
= IU_ID_READ_READY
;
662 iu
->tag
= cpu_to_be16(cmd
->tag
);
664 stream
->req_status
->complete
= uasp_status_data_cmpl
;
665 stream
->req_status
->context
= cmd
;
667 cmd
->state
= UASP_SEND_DATA
;
668 stream
->req_status
->buf
= iu
;
669 stream
->req_status
->length
= sizeof(struct iu
);
671 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
674 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
680 static int uasp_send_write_request(struct usbg_cmd
*cmd
)
682 struct f_uas
*fu
= cmd
->fu
;
683 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
684 struct uas_stream
*stream
= cmd
->stream
;
685 struct sense_iu
*iu
= &cmd
->sense_iu
;
688 init_completion(&cmd
->write_complete
);
691 iu
->tag
= cpu_to_be16(cmd
->tag
);
693 if (fu
->flags
& USBG_USE_STREAMS
) {
695 ret
= usbg_prepare_w_request(cmd
, stream
->req_out
);
698 ret
= usb_ep_queue(fu
->ep_out
, stream
->req_out
, GFP_ATOMIC
);
700 pr_err("%s(%d)\n", __func__
, __LINE__
);
704 iu
->iu_id
= IU_ID_WRITE_READY
;
705 iu
->tag
= cpu_to_be16(cmd
->tag
);
707 stream
->req_status
->complete
= uasp_status_data_cmpl
;
708 stream
->req_status
->context
= cmd
;
710 cmd
->state
= UASP_RECEIVE_DATA
;
711 stream
->req_status
->buf
= iu
;
712 stream
->req_status
->length
= sizeof(struct iu
);
714 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
717 pr_err("%s(%d)\n", __func__
, __LINE__
);
720 wait_for_completion(&cmd
->write_complete
);
721 target_execute_cmd(se_cmd
);
726 static int usbg_submit_command(struct f_uas
*, void *, unsigned int);
728 static void uasp_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
730 struct f_uas
*fu
= req
->context
;
736 ret
= usbg_submit_command(fu
, req
->buf
, req
->actual
);
738 * Once we tune for performance enqueue the command req here again so
739 * we can receive a second command while we processing this one. Pay
740 * attention to properly sync STAUS endpoint with DATA IN + OUT so you
745 usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
748 static int uasp_alloc_stream_res(struct f_uas
*fu
, struct uas_stream
*stream
)
750 stream
->req_in
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
754 stream
->req_out
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
755 if (!stream
->req_out
)
758 stream
->req_status
= usb_ep_alloc_request(fu
->ep_status
, GFP_KERNEL
);
759 if (!stream
->req_status
)
764 usb_ep_free_request(fu
->ep_status
, stream
->req_status
);
765 stream
->req_status
= NULL
;
767 usb_ep_free_request(fu
->ep_out
, stream
->req_out
);
768 stream
->req_out
= NULL
;
773 static int uasp_alloc_cmd(struct f_uas
*fu
)
775 fu
->cmd
.req
= usb_ep_alloc_request(fu
->ep_cmd
, GFP_KERNEL
);
779 fu
->cmd
.buf
= kmalloc(fu
->ep_cmd
->maxpacket
, GFP_KERNEL
);
783 fu
->cmd
.req
->complete
= uasp_cmd_complete
;
784 fu
->cmd
.req
->buf
= fu
->cmd
.buf
;
785 fu
->cmd
.req
->length
= fu
->ep_cmd
->maxpacket
;
786 fu
->cmd
.req
->context
= fu
;
790 usb_ep_free_request(fu
->ep_cmd
, fu
->cmd
.req
);
795 static void uasp_setup_stream_res(struct f_uas
*fu
, int max_streams
)
799 for (i
= 0; i
< max_streams
; i
++) {
800 struct uas_stream
*s
= &fu
->stream
[i
];
802 s
->req_in
->stream_id
= i
+ 1;
803 s
->req_out
->stream_id
= i
+ 1;
804 s
->req_status
->stream_id
= i
+ 1;
808 static int uasp_prepare_reqs(struct f_uas
*fu
)
814 if (fu
->flags
& USBG_USE_STREAMS
)
815 max_streams
= UASP_SS_EP_COMP_NUM_STREAMS
;
819 for (i
= 0; i
< max_streams
; i
++) {
820 ret
= uasp_alloc_stream_res(fu
, &fu
->stream
[i
]);
825 ret
= uasp_alloc_cmd(fu
);
827 goto err_free_stream
;
828 uasp_setup_stream_res(fu
, max_streams
);
830 ret
= usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
832 goto err_free_stream
;
837 uasp_free_cmdreq(fu
);
842 uasp_cleanup_one_stream(fu
, &fu
->stream
[i
- 1]);
846 pr_err("UASP: endpoint setup failed\n");
850 static void uasp_set_alt(struct f_uas
*fu
)
852 struct usb_function
*f
= &fu
->function
;
853 struct usb_gadget
*gadget
= f
->config
->cdev
->gadget
;
856 fu
->flags
= USBG_IS_UAS
;
858 if (gadget
->speed
== USB_SPEED_SUPER
)
859 fu
->flags
|= USBG_USE_STREAMS
;
861 config_ep_by_speed(gadget
, f
, fu
->ep_in
);
862 ret
= usb_ep_enable(fu
->ep_in
);
866 config_ep_by_speed(gadget
, f
, fu
->ep_out
);
867 ret
= usb_ep_enable(fu
->ep_out
);
871 config_ep_by_speed(gadget
, f
, fu
->ep_cmd
);
872 ret
= usb_ep_enable(fu
->ep_cmd
);
875 config_ep_by_speed(gadget
, f
, fu
->ep_status
);
876 ret
= usb_ep_enable(fu
->ep_status
);
880 ret
= uasp_prepare_reqs(fu
);
883 fu
->flags
|= USBG_ENABLED
;
885 pr_info("Using the UAS protocol\n");
888 usb_ep_disable(fu
->ep_status
);
890 usb_ep_disable(fu
->ep_cmd
);
892 usb_ep_disable(fu
->ep_out
);
894 usb_ep_disable(fu
->ep_in
);
899 static int get_cmd_dir(const unsigned char *cdb
)
911 case SERVICE_ACTION_IN_16
:
913 case PERSISTENT_RESERVE_IN
:
914 case SECURITY_PROTOCOL_IN
:
915 case ACCESS_CONTROL_IN
:
917 case READ_BLOCK_LIMITS
:
921 case READ_FORMAT_CAPACITIES
:
923 ret
= DMA_FROM_DEVICE
;
933 case WRITE_VERIFY_12
:
934 case PERSISTENT_RESERVE_OUT
:
935 case MAINTENANCE_OUT
:
936 case SECURITY_PROTOCOL_OUT
:
937 case ACCESS_CONTROL_OUT
:
940 case ALLOW_MEDIUM_REMOVAL
:
941 case TEST_UNIT_READY
:
942 case SYNCHRONIZE_CACHE
:
949 case WRITE_FILEMARKS
:
953 pr_warn("target: Unknown data direction for SCSI Opcode "
960 static void usbg_data_write_cmpl(struct usb_ep
*ep
, struct usb_request
*req
)
962 struct usbg_cmd
*cmd
= req
->context
;
963 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
965 if (req
->status
< 0) {
966 pr_err("%s() state %d transfer failed\n", __func__
, cmd
->state
);
970 if (req
->num_sgs
== 0) {
971 sg_copy_from_buffer(se_cmd
->t_data_sg
,
972 se_cmd
->t_data_nents
,
974 se_cmd
->data_length
);
977 complete(&cmd
->write_complete
);
981 usbg_cleanup_cmd(cmd
);
984 static int usbg_prepare_w_request(struct usbg_cmd
*cmd
, struct usb_request
*req
)
986 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
987 struct f_uas
*fu
= cmd
->fu
;
988 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
990 if (!gadget
->sg_supported
) {
991 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
995 req
->buf
= cmd
->data_buf
;
998 req
->num_sgs
= se_cmd
->t_data_nents
;
999 req
->sg
= se_cmd
->t_data_sg
;
1002 req
->complete
= usbg_data_write_cmpl
;
1003 req
->length
= se_cmd
->data_length
;
1008 static int usbg_send_status_response(struct se_cmd
*se_cmd
)
1010 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1012 struct f_uas
*fu
= cmd
->fu
;
1014 if (fu
->flags
& USBG_IS_BOT
)
1015 return bot_send_status_response(cmd
);
1017 return uasp_send_status_response(cmd
);
1020 static int usbg_send_write_request(struct se_cmd
*se_cmd
)
1022 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1024 struct f_uas
*fu
= cmd
->fu
;
1026 if (fu
->flags
& USBG_IS_BOT
)
1027 return bot_send_write_request(cmd
);
1029 return uasp_send_write_request(cmd
);
1032 static int usbg_send_read_response(struct se_cmd
*se_cmd
)
1034 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1036 struct f_uas
*fu
= cmd
->fu
;
1038 if (fu
->flags
& USBG_IS_BOT
)
1039 return bot_send_read_response(cmd
);
1041 return uasp_send_read_response(cmd
);
1044 static void usbg_cmd_work(struct work_struct
*work
)
1046 struct usbg_cmd
*cmd
= container_of(work
, struct usbg_cmd
, work
);
1047 struct se_cmd
*se_cmd
;
1048 struct tcm_usbg_nexus
*tv_nexus
;
1049 struct usbg_tpg
*tpg
;
1052 se_cmd
= &cmd
->se_cmd
;
1054 tv_nexus
= tpg
->tpg_nexus
;
1055 dir
= get_cmd_dir(cmd
->cmd_buf
);
1057 transport_init_se_cmd(se_cmd
,
1058 tv_nexus
->tvn_se_sess
->se_tpg
->se_tpg_tfo
,
1059 tv_nexus
->tvn_se_sess
, cmd
->data_len
, DMA_NONE
,
1060 cmd
->prio_attr
, cmd
->sense_iu
.sense
);
1064 if (target_submit_cmd(se_cmd
, tv_nexus
->tvn_se_sess
,
1065 cmd
->cmd_buf
, cmd
->sense_iu
.sense
, cmd
->unpacked_lun
,
1066 0, cmd
->prio_attr
, dir
, TARGET_SCF_UNKNOWN_SIZE
) < 0)
1072 transport_send_check_condition_and_sense(se_cmd
,
1073 TCM_UNSUPPORTED_SCSI_OPCODE
, 1);
1074 usbg_cleanup_cmd(cmd
);
1077 static int usbg_submit_command(struct f_uas
*fu
,
1078 void *cmdbuf
, unsigned int len
)
1080 struct command_iu
*cmd_iu
= cmdbuf
;
1081 struct usbg_cmd
*cmd
;
1082 struct usbg_tpg
*tpg
;
1083 struct se_cmd
*se_cmd
;
1084 struct tcm_usbg_nexus
*tv_nexus
;
1088 if (cmd_iu
->iu_id
!= IU_ID_COMMAND
) {
1089 pr_err("Unsupported type %d\n", cmd_iu
->iu_id
);
1093 cmd
= kzalloc(sizeof *cmd
, GFP_ATOMIC
);
1099 /* XXX until I figure out why I can't free in on complete */
1100 kref_init(&cmd
->ref
);
1101 kref_get(&cmd
->ref
);
1104 cmd_len
= (cmd_iu
->len
& ~0x3) + 16;
1105 if (cmd_len
> USBG_MAX_CMD
)
1108 memcpy(cmd
->cmd_buf
, cmd_iu
->cdb
, cmd_len
);
1110 cmd
->tag
= be16_to_cpup(&cmd_iu
->tag
);
1111 cmd
->se_cmd
.tag
= cmd
->tag
;
1112 if (fu
->flags
& USBG_USE_STREAMS
) {
1113 if (cmd
->tag
> UASP_SS_EP_COMP_NUM_STREAMS
)
1116 cmd
->stream
= &fu
->stream
[0];
1118 cmd
->stream
= &fu
->stream
[cmd
->tag
- 1];
1120 cmd
->stream
= &fu
->stream
[0];
1123 tv_nexus
= tpg
->tpg_nexus
;
1125 pr_err("Missing nexus, ignoring command\n");
1129 switch (cmd_iu
->prio_attr
& 0x7) {
1131 cmd
->prio_attr
= TCM_HEAD_TAG
;
1133 case UAS_ORDERED_TAG
:
1134 cmd
->prio_attr
= TCM_ORDERED_TAG
;
1137 cmd
->prio_attr
= TCM_ACA_TAG
;
1140 pr_debug_once("Unsupported prio_attr: %02x.\n",
1142 case UAS_SIMPLE_TAG
:
1143 cmd
->prio_attr
= TCM_SIMPLE_TAG
;
1147 se_cmd
= &cmd
->se_cmd
;
1148 cmd
->unpacked_lun
= scsilun_to_int(&cmd_iu
->lun
);
1150 INIT_WORK(&cmd
->work
, usbg_cmd_work
);
1151 ret
= queue_work(tpg
->workqueue
, &cmd
->work
);
1161 static void bot_cmd_work(struct work_struct
*work
)
1163 struct usbg_cmd
*cmd
= container_of(work
, struct usbg_cmd
, work
);
1164 struct se_cmd
*se_cmd
;
1165 struct tcm_usbg_nexus
*tv_nexus
;
1166 struct usbg_tpg
*tpg
;
1169 se_cmd
= &cmd
->se_cmd
;
1171 tv_nexus
= tpg
->tpg_nexus
;
1172 dir
= get_cmd_dir(cmd
->cmd_buf
);
1174 transport_init_se_cmd(se_cmd
,
1175 tv_nexus
->tvn_se_sess
->se_tpg
->se_tpg_tfo
,
1176 tv_nexus
->tvn_se_sess
, cmd
->data_len
, DMA_NONE
,
1177 cmd
->prio_attr
, cmd
->sense_iu
.sense
);
1181 if (target_submit_cmd(se_cmd
, tv_nexus
->tvn_se_sess
,
1182 cmd
->cmd_buf
, cmd
->sense_iu
.sense
, cmd
->unpacked_lun
,
1183 cmd
->data_len
, cmd
->prio_attr
, dir
, 0) < 0)
1189 transport_send_check_condition_and_sense(se_cmd
,
1190 TCM_UNSUPPORTED_SCSI_OPCODE
, 1);
1191 usbg_cleanup_cmd(cmd
);
1194 static int bot_submit_command(struct f_uas
*fu
,
1195 void *cmdbuf
, unsigned int len
)
1197 struct bulk_cb_wrap
*cbw
= cmdbuf
;
1198 struct usbg_cmd
*cmd
;
1199 struct usbg_tpg
*tpg
;
1200 struct se_cmd
*se_cmd
;
1201 struct tcm_usbg_nexus
*tv_nexus
;
1205 if (cbw
->Signature
!= cpu_to_le32(US_BULK_CB_SIGN
)) {
1206 pr_err("Wrong signature on CBW\n");
1210 pr_err("Wrong length for CBW\n");
1214 cmd_len
= cbw
->Length
;
1215 if (cmd_len
< 1 || cmd_len
> 16)
1218 cmd
= kzalloc(sizeof *cmd
, GFP_ATOMIC
);
1224 /* XXX until I figure out why I can't free in on complete */
1225 kref_init(&cmd
->ref
);
1226 kref_get(&cmd
->ref
);
1230 memcpy(cmd
->cmd_buf
, cbw
->CDB
, cmd_len
);
1232 cmd
->bot_tag
= cbw
->Tag
;
1234 tv_nexus
= tpg
->tpg_nexus
;
1236 pr_err("Missing nexus, ignoring command\n");
1240 cmd
->prio_attr
= TCM_SIMPLE_TAG
;
1241 se_cmd
= &cmd
->se_cmd
;
1242 cmd
->unpacked_lun
= cbw
->Lun
;
1243 cmd
->is_read
= cbw
->Flags
& US_BULK_FLAG_IN
? 1 : 0;
1244 cmd
->data_len
= le32_to_cpu(cbw
->DataTransferLength
);
1245 cmd
->se_cmd
.tag
= le32_to_cpu(cmd
->bot_tag
);
1247 INIT_WORK(&cmd
->work
, bot_cmd_work
);
1248 ret
= queue_work(tpg
->workqueue
, &cmd
->work
);
1258 /* Start fabric.c code */
1260 static int usbg_check_true(struct se_portal_group
*se_tpg
)
1265 static int usbg_check_false(struct se_portal_group
*se_tpg
)
1270 static char *usbg_get_fabric_name(void)
1272 return "usb_gadget";
1275 static char *usbg_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1277 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1278 struct usbg_tpg
, se_tpg
);
1279 struct usbg_tport
*tport
= tpg
->tport
;
1281 return &tport
->tport_name
[0];
1284 static u16
usbg_get_tag(struct se_portal_group
*se_tpg
)
1286 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1287 struct usbg_tpg
, se_tpg
);
1288 return tpg
->tport_tpgt
;
1291 static u32
usbg_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
1296 static void usbg_cmd_release(struct kref
*ref
)
1298 struct usbg_cmd
*cmd
= container_of(ref
, struct usbg_cmd
,
1301 transport_generic_free_cmd(&cmd
->se_cmd
, 0);
1304 static void usbg_release_cmd(struct se_cmd
*se_cmd
)
1306 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1308 kfree(cmd
->data_buf
);
1313 static int usbg_shutdown_session(struct se_session
*se_sess
)
1318 static void usbg_close_session(struct se_session
*se_sess
)
1323 static u32
usbg_sess_get_index(struct se_session
*se_sess
)
1329 * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
1331 static int usbg_write_pending_status(struct se_cmd
*se_cmd
)
1336 static void usbg_set_default_node_attrs(struct se_node_acl
*nacl
)
1341 static int usbg_get_cmd_state(struct se_cmd
*se_cmd
)
1346 static void usbg_queue_tm_rsp(struct se_cmd
*se_cmd
)
1350 static void usbg_aborted_task(struct se_cmd
*se_cmd
)
1355 static const char *usbg_check_wwn(const char *name
)
1360 n
= strstr(name
, "naa.");
1365 if (len
== 0 || len
> USBG_NAMELEN
- 1)
1370 static int usbg_init_nodeacl(struct se_node_acl
*se_nacl
, const char *name
)
1372 if (!usbg_check_wwn(name
))
1377 struct usbg_tpg
*the_only_tpg_I_currently_have
;
1379 static struct se_portal_group
*usbg_make_tpg(
1381 struct config_group
*group
,
1384 struct usbg_tport
*tport
= container_of(wwn
, struct usbg_tport
,
1386 struct usbg_tpg
*tpg
;
1390 if (strstr(name
, "tpgt_") != name
)
1391 return ERR_PTR(-EINVAL
);
1392 if (kstrtoul(name
+ 5, 0, &tpgt
) || tpgt
> UINT_MAX
)
1393 return ERR_PTR(-EINVAL
);
1394 if (the_only_tpg_I_currently_have
) {
1395 pr_err("Until the gadget framework can't handle multiple\n");
1396 pr_err("gadgets, you can't do this here.\n");
1397 return ERR_PTR(-EBUSY
);
1400 tpg
= kzalloc(sizeof(struct usbg_tpg
), GFP_KERNEL
);
1402 return ERR_PTR(-ENOMEM
);
1403 mutex_init(&tpg
->tpg_mutex
);
1404 atomic_set(&tpg
->tpg_port_count
, 0);
1405 tpg
->workqueue
= alloc_workqueue("tcm_usb_gadget", 0, 1);
1406 if (!tpg
->workqueue
) {
1412 tpg
->tport_tpgt
= tpgt
;
1415 * SPC doesn't assign a protocol identifier for USB-SCSI, so we
1416 * pretend to be SAS..
1418 ret
= core_tpg_register(wwn
, &tpg
->se_tpg
, SCSI_PROTOCOL_SAS
);
1420 destroy_workqueue(tpg
->workqueue
);
1424 the_only_tpg_I_currently_have
= tpg
;
1425 return &tpg
->se_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 core_tpg_deregister(se_tpg
);
1434 destroy_workqueue(tpg
->workqueue
);
1436 the_only_tpg_I_currently_have
= NULL
;
1439 static struct se_wwn
*usbg_make_tport(
1440 struct target_fabric_configfs
*tf
,
1441 struct config_group
*group
,
1444 struct usbg_tport
*tport
;
1445 const char *wnn_name
;
1448 wnn_name
= usbg_check_wwn(name
);
1450 return ERR_PTR(-EINVAL
);
1452 tport
= kzalloc(sizeof(struct usbg_tport
), GFP_KERNEL
);
1454 return ERR_PTR(-ENOMEM
);
1455 tport
->tport_wwpn
= wwpn
;
1456 snprintf(tport
->tport_name
, sizeof(tport
->tport_name
), "%s", wnn_name
);
1457 return &tport
->tport_wwn
;
1460 static void usbg_drop_tport(struct se_wwn
*wwn
)
1462 struct usbg_tport
*tport
= container_of(wwn
,
1463 struct usbg_tport
, tport_wwn
);
1468 * If somebody feels like dropping the version property, go ahead.
1470 static ssize_t
usbg_wwn_show_attr_version(
1471 struct target_fabric_configfs
*tf
,
1474 return sprintf(page
, "usb-gadget fabric module\n");
1476 TF_WWN_ATTR_RO(usbg
, version
);
1478 static struct configfs_attribute
*usbg_wwn_attrs
[] = {
1479 &usbg_wwn_version
.attr
,
1483 static ssize_t
tcm_usbg_tpg_show_enable(
1484 struct se_portal_group
*se_tpg
,
1487 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1489 return snprintf(page
, PAGE_SIZE
, "%u\n", tpg
->gadget_connect
);
1492 static int usbg_attach(struct usbg_tpg
*);
1493 static void usbg_detach(struct usbg_tpg
*);
1495 static ssize_t
tcm_usbg_tpg_store_enable(
1496 struct se_portal_group
*se_tpg
,
1500 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1504 ret
= kstrtoul(page
, 0, &op
);
1510 if (op
&& tpg
->gadget_connect
)
1512 if (!op
&& !tpg
->gadget_connect
)
1516 ret
= usbg_attach(tpg
);
1522 tpg
->gadget_connect
= op
;
1526 TF_TPG_BASE_ATTR(tcm_usbg
, enable
, S_IRUGO
| S_IWUSR
);
1528 static ssize_t
tcm_usbg_tpg_show_nexus(
1529 struct se_portal_group
*se_tpg
,
1532 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1533 struct tcm_usbg_nexus
*tv_nexus
;
1536 mutex_lock(&tpg
->tpg_mutex
);
1537 tv_nexus
= tpg
->tpg_nexus
;
1542 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1543 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1545 mutex_unlock(&tpg
->tpg_mutex
);
1549 static int tcm_usbg_make_nexus(struct usbg_tpg
*tpg
, char *name
)
1551 struct se_portal_group
*se_tpg
;
1552 struct tcm_usbg_nexus
*tv_nexus
;
1555 mutex_lock(&tpg
->tpg_mutex
);
1556 if (tpg
->tpg_nexus
) {
1558 pr_debug("tpg->tpg_nexus already exists\n");
1561 se_tpg
= &tpg
->se_tpg
;
1564 tv_nexus
= kzalloc(sizeof(*tv_nexus
), GFP_KERNEL
);
1567 tv_nexus
->tvn_se_sess
= transport_init_session(TARGET_PROT_NORMAL
);
1568 if (IS_ERR(tv_nexus
->tvn_se_sess
))
1572 * Since we are running in 'demo mode' this call with generate a
1573 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1574 * the SCSI Initiator port name of the passed configfs group 'name'.
1576 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1578 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1579 pr_debug("core_tpg_check_initiator_node_acl() failed"
1584 * Now register the TCM vHost virtual I_T Nexus as active.
1586 transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1587 tv_nexus
->tvn_se_sess
, tv_nexus
);
1588 tpg
->tpg_nexus
= tv_nexus
;
1589 mutex_unlock(&tpg
->tpg_mutex
);
1593 transport_free_session(tv_nexus
->tvn_se_sess
);
1597 mutex_unlock(&tpg
->tpg_mutex
);
1601 static int tcm_usbg_drop_nexus(struct usbg_tpg
*tpg
)
1603 struct se_session
*se_sess
;
1604 struct tcm_usbg_nexus
*tv_nexus
;
1607 mutex_lock(&tpg
->tpg_mutex
);
1608 tv_nexus
= tpg
->tpg_nexus
;
1612 se_sess
= tv_nexus
->tvn_se_sess
;
1616 if (atomic_read(&tpg
->tpg_port_count
)) {
1618 pr_err("Unable to remove Host I_T Nexus with"
1619 " active TPG port count: %d\n",
1620 atomic_read(&tpg
->tpg_port_count
));
1624 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1625 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1627 * Release the SCSI I_T Nexus to the emulated vHost Target Port
1629 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1630 tpg
->tpg_nexus
= NULL
;
1635 mutex_unlock(&tpg
->tpg_mutex
);
1639 static ssize_t
tcm_usbg_tpg_store_nexus(
1640 struct se_portal_group
*se_tpg
,
1644 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1645 unsigned char i_port
[USBG_NAMELEN
], *ptr
;
1648 if (!strncmp(page
, "NULL", 4)) {
1649 ret
= tcm_usbg_drop_nexus(tpg
);
1650 return (!ret
) ? count
: ret
;
1652 if (strlen(page
) >= USBG_NAMELEN
) {
1653 pr_err("Emulated NAA Sas Address: %s, exceeds"
1654 " max: %d\n", page
, USBG_NAMELEN
);
1657 snprintf(i_port
, USBG_NAMELEN
, "%s", page
);
1659 ptr
= strstr(i_port
, "naa.");
1661 pr_err("Missing 'naa.' prefix\n");
1665 if (i_port
[strlen(i_port
) - 1] == '\n')
1666 i_port
[strlen(i_port
) - 1] = '\0';
1668 ret
= tcm_usbg_make_nexus(tpg
, &i_port
[4]);
1673 TF_TPG_BASE_ATTR(tcm_usbg
, nexus
, S_IRUGO
| S_IWUSR
);
1675 static struct configfs_attribute
*usbg_base_attrs
[] = {
1676 &tcm_usbg_tpg_enable
.attr
,
1677 &tcm_usbg_tpg_nexus
.attr
,
1681 static int usbg_port_link(struct se_portal_group
*se_tpg
, struct se_lun
*lun
)
1683 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1685 atomic_inc(&tpg
->tpg_port_count
);
1686 smp_mb__after_atomic();
1690 static void usbg_port_unlink(struct se_portal_group
*se_tpg
,
1691 struct se_lun
*se_lun
)
1693 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1695 atomic_dec(&tpg
->tpg_port_count
);
1696 smp_mb__after_atomic();
1699 static int usbg_check_stop_free(struct se_cmd
*se_cmd
)
1701 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1704 kref_put(&cmd
->ref
, usbg_cmd_release
);
1708 static const struct target_core_fabric_ops usbg_ops
= {
1709 .module
= THIS_MODULE
,
1710 .name
= "usb_gadget",
1711 .get_fabric_name
= usbg_get_fabric_name
,
1712 .tpg_get_wwn
= usbg_get_fabric_wwn
,
1713 .tpg_get_tag
= usbg_get_tag
,
1714 .tpg_check_demo_mode
= usbg_check_true
,
1715 .tpg_check_demo_mode_cache
= usbg_check_false
,
1716 .tpg_check_demo_mode_write_protect
= usbg_check_false
,
1717 .tpg_check_prod_mode_write_protect
= usbg_check_false
,
1718 .tpg_get_inst_index
= usbg_tpg_get_inst_index
,
1719 .release_cmd
= usbg_release_cmd
,
1720 .shutdown_session
= usbg_shutdown_session
,
1721 .close_session
= usbg_close_session
,
1722 .sess_get_index
= usbg_sess_get_index
,
1723 .sess_get_initiator_sid
= NULL
,
1724 .write_pending
= usbg_send_write_request
,
1725 .write_pending_status
= usbg_write_pending_status
,
1726 .set_default_node_attributes
= usbg_set_default_node_attrs
,
1727 .get_cmd_state
= usbg_get_cmd_state
,
1728 .queue_data_in
= usbg_send_read_response
,
1729 .queue_status
= usbg_send_status_response
,
1730 .queue_tm_rsp
= usbg_queue_tm_rsp
,
1731 .aborted_task
= usbg_aborted_task
,
1732 .check_stop_free
= usbg_check_stop_free
,
1734 .fabric_make_wwn
= usbg_make_tport
,
1735 .fabric_drop_wwn
= usbg_drop_tport
,
1736 .fabric_make_tpg
= usbg_make_tpg
,
1737 .fabric_drop_tpg
= usbg_drop_tpg
,
1738 .fabric_post_link
= usbg_port_link
,
1739 .fabric_pre_unlink
= usbg_port_unlink
,
1740 .fabric_init_nodeacl
= usbg_init_nodeacl
,
1742 .tfc_wwn_attrs
= usbg_wwn_attrs
,
1743 .tfc_tpg_base_attrs
= usbg_base_attrs
,
1746 /* Start gadget.c code */
1748 static struct usb_interface_descriptor bot_intf_desc
= {
1749 .bLength
= sizeof(bot_intf_desc
),
1750 .bDescriptorType
= USB_DT_INTERFACE
,
1752 .bAlternateSetting
= USB_G_ALT_INT_BBB
,
1753 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
1754 .bInterfaceSubClass
= USB_SC_SCSI
,
1755 .bInterfaceProtocol
= USB_PR_BULK
,
1758 static struct usb_interface_descriptor uasp_intf_desc
= {
1759 .bLength
= sizeof(uasp_intf_desc
),
1760 .bDescriptorType
= USB_DT_INTERFACE
,
1762 .bAlternateSetting
= USB_G_ALT_INT_UAS
,
1763 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
1764 .bInterfaceSubClass
= USB_SC_SCSI
,
1765 .bInterfaceProtocol
= USB_PR_UAS
,
1768 static struct usb_endpoint_descriptor uasp_bi_desc
= {
1769 .bLength
= USB_DT_ENDPOINT_SIZE
,
1770 .bDescriptorType
= USB_DT_ENDPOINT
,
1771 .bEndpointAddress
= USB_DIR_IN
,
1772 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1773 .wMaxPacketSize
= cpu_to_le16(512),
1776 static struct usb_endpoint_descriptor uasp_fs_bi_desc
= {
1777 .bLength
= USB_DT_ENDPOINT_SIZE
,
1778 .bDescriptorType
= USB_DT_ENDPOINT
,
1779 .bEndpointAddress
= USB_DIR_IN
,
1780 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1783 static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc
= {
1784 .bLength
= sizeof(uasp_bi_pipe_desc
),
1785 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1786 .bPipeID
= DATA_IN_PIPE_ID
,
1789 static struct usb_endpoint_descriptor uasp_ss_bi_desc
= {
1790 .bLength
= USB_DT_ENDPOINT_SIZE
,
1791 .bDescriptorType
= USB_DT_ENDPOINT
,
1792 .bEndpointAddress
= USB_DIR_IN
,
1793 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1794 .wMaxPacketSize
= cpu_to_le16(1024),
1797 static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc
= {
1798 .bLength
= sizeof(uasp_bi_ep_comp_desc
),
1799 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1801 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
1802 .wBytesPerInterval
= 0,
1805 static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc
= {
1806 .bLength
= sizeof(bot_bi_ep_comp_desc
),
1807 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1811 static struct usb_endpoint_descriptor uasp_bo_desc
= {
1812 .bLength
= USB_DT_ENDPOINT_SIZE
,
1813 .bDescriptorType
= USB_DT_ENDPOINT
,
1814 .bEndpointAddress
= USB_DIR_OUT
,
1815 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1816 .wMaxPacketSize
= cpu_to_le16(512),
1819 static struct usb_endpoint_descriptor uasp_fs_bo_desc
= {
1820 .bLength
= USB_DT_ENDPOINT_SIZE
,
1821 .bDescriptorType
= USB_DT_ENDPOINT
,
1822 .bEndpointAddress
= USB_DIR_OUT
,
1823 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1826 static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc
= {
1827 .bLength
= sizeof(uasp_bo_pipe_desc
),
1828 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1829 .bPipeID
= DATA_OUT_PIPE_ID
,
1832 static struct usb_endpoint_descriptor uasp_ss_bo_desc
= {
1833 .bLength
= USB_DT_ENDPOINT_SIZE
,
1834 .bDescriptorType
= USB_DT_ENDPOINT
,
1835 .bEndpointAddress
= USB_DIR_OUT
,
1836 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1837 .wMaxPacketSize
= cpu_to_le16(0x400),
1840 static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc
= {
1841 .bLength
= sizeof(uasp_bo_ep_comp_desc
),
1842 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1843 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
1846 static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc
= {
1847 .bLength
= sizeof(bot_bo_ep_comp_desc
),
1848 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1851 static struct usb_endpoint_descriptor uasp_status_desc
= {
1852 .bLength
= USB_DT_ENDPOINT_SIZE
,
1853 .bDescriptorType
= USB_DT_ENDPOINT
,
1854 .bEndpointAddress
= USB_DIR_IN
,
1855 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1856 .wMaxPacketSize
= cpu_to_le16(512),
1859 static struct usb_endpoint_descriptor uasp_fs_status_desc
= {
1860 .bLength
= USB_DT_ENDPOINT_SIZE
,
1861 .bDescriptorType
= USB_DT_ENDPOINT
,
1862 .bEndpointAddress
= USB_DIR_IN
,
1863 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1866 static struct usb_pipe_usage_descriptor uasp_status_pipe_desc
= {
1867 .bLength
= sizeof(uasp_status_pipe_desc
),
1868 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1869 .bPipeID
= STATUS_PIPE_ID
,
1872 static struct usb_endpoint_descriptor uasp_ss_status_desc
= {
1873 .bLength
= USB_DT_ENDPOINT_SIZE
,
1874 .bDescriptorType
= USB_DT_ENDPOINT
,
1875 .bEndpointAddress
= USB_DIR_IN
,
1876 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1877 .wMaxPacketSize
= cpu_to_le16(1024),
1880 static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc
= {
1881 .bLength
= sizeof(uasp_status_in_ep_comp_desc
),
1882 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1883 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
1886 static struct usb_endpoint_descriptor uasp_cmd_desc
= {
1887 .bLength
= USB_DT_ENDPOINT_SIZE
,
1888 .bDescriptorType
= USB_DT_ENDPOINT
,
1889 .bEndpointAddress
= USB_DIR_OUT
,
1890 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1891 .wMaxPacketSize
= cpu_to_le16(512),
1894 static struct usb_endpoint_descriptor uasp_fs_cmd_desc
= {
1895 .bLength
= USB_DT_ENDPOINT_SIZE
,
1896 .bDescriptorType
= USB_DT_ENDPOINT
,
1897 .bEndpointAddress
= USB_DIR_OUT
,
1898 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1901 static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc
= {
1902 .bLength
= sizeof(uasp_cmd_pipe_desc
),
1903 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1904 .bPipeID
= CMD_PIPE_ID
,
1907 static struct usb_endpoint_descriptor uasp_ss_cmd_desc
= {
1908 .bLength
= USB_DT_ENDPOINT_SIZE
,
1909 .bDescriptorType
= USB_DT_ENDPOINT
,
1910 .bEndpointAddress
= USB_DIR_OUT
,
1911 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1912 .wMaxPacketSize
= cpu_to_le16(1024),
1915 static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc
= {
1916 .bLength
= sizeof(uasp_cmd_comp_desc
),
1917 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1920 static struct usb_descriptor_header
*uasp_fs_function_desc
[] = {
1921 (struct usb_descriptor_header
*) &bot_intf_desc
,
1922 (struct usb_descriptor_header
*) &uasp_fs_bi_desc
,
1923 (struct usb_descriptor_header
*) &uasp_fs_bo_desc
,
1925 (struct usb_descriptor_header
*) &uasp_intf_desc
,
1926 (struct usb_descriptor_header
*) &uasp_fs_bi_desc
,
1927 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
1928 (struct usb_descriptor_header
*) &uasp_fs_bo_desc
,
1929 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
1930 (struct usb_descriptor_header
*) &uasp_fs_status_desc
,
1931 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
1932 (struct usb_descriptor_header
*) &uasp_fs_cmd_desc
,
1933 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
1937 static struct usb_descriptor_header
*uasp_hs_function_desc
[] = {
1938 (struct usb_descriptor_header
*) &bot_intf_desc
,
1939 (struct usb_descriptor_header
*) &uasp_bi_desc
,
1940 (struct usb_descriptor_header
*) &uasp_bo_desc
,
1942 (struct usb_descriptor_header
*) &uasp_intf_desc
,
1943 (struct usb_descriptor_header
*) &uasp_bi_desc
,
1944 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
1945 (struct usb_descriptor_header
*) &uasp_bo_desc
,
1946 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
1947 (struct usb_descriptor_header
*) &uasp_status_desc
,
1948 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
1949 (struct usb_descriptor_header
*) &uasp_cmd_desc
,
1950 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
1954 static struct usb_descriptor_header
*uasp_ss_function_desc
[] = {
1955 (struct usb_descriptor_header
*) &bot_intf_desc
,
1956 (struct usb_descriptor_header
*) &uasp_ss_bi_desc
,
1957 (struct usb_descriptor_header
*) &bot_bi_ep_comp_desc
,
1958 (struct usb_descriptor_header
*) &uasp_ss_bo_desc
,
1959 (struct usb_descriptor_header
*) &bot_bo_ep_comp_desc
,
1961 (struct usb_descriptor_header
*) &uasp_intf_desc
,
1962 (struct usb_descriptor_header
*) &uasp_ss_bi_desc
,
1963 (struct usb_descriptor_header
*) &uasp_bi_ep_comp_desc
,
1964 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
1965 (struct usb_descriptor_header
*) &uasp_ss_bo_desc
,
1966 (struct usb_descriptor_header
*) &uasp_bo_ep_comp_desc
,
1967 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
1968 (struct usb_descriptor_header
*) &uasp_ss_status_desc
,
1969 (struct usb_descriptor_header
*) &uasp_status_in_ep_comp_desc
,
1970 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
1971 (struct usb_descriptor_header
*) &uasp_ss_cmd_desc
,
1972 (struct usb_descriptor_header
*) &uasp_cmd_comp_desc
,
1973 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
1977 #define UAS_VENDOR_ID 0x0525 /* NetChip */
1978 #define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
1980 static struct usb_device_descriptor usbg_device_desc
= {
1981 .bLength
= sizeof(usbg_device_desc
),
1982 .bDescriptorType
= USB_DT_DEVICE
,
1983 .bcdUSB
= cpu_to_le16(0x0200),
1984 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
1985 .idVendor
= cpu_to_le16(UAS_VENDOR_ID
),
1986 .idProduct
= cpu_to_le16(UAS_PRODUCT_ID
),
1987 .bNumConfigurations
= 1,
1990 static struct usb_string usbg_us_strings
[] = {
1991 [USB_GADGET_MANUFACTURER_IDX
].s
= "Target Manufactor",
1992 [USB_GADGET_PRODUCT_IDX
].s
= "Target Product",
1993 [USB_GADGET_SERIAL_IDX
].s
= "000000000001",
1994 [USB_G_STR_CONFIG
].s
= "default config",
1995 [USB_G_STR_INT_UAS
].s
= "USB Attached SCSI",
1996 [USB_G_STR_INT_BBB
].s
= "Bulk Only Transport",
2000 static struct usb_gadget_strings usbg_stringtab
= {
2002 .strings
= usbg_us_strings
,
2005 static struct usb_gadget_strings
*usbg_strings
[] = {
2010 static int guas_unbind(struct usb_composite_dev
*cdev
)
2015 static struct usb_configuration usbg_config_driver
= {
2016 .label
= "Linux Target",
2017 .bConfigurationValue
= 1,
2018 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
2021 static void give_back_ep(struct usb_ep
**pep
)
2023 struct usb_ep
*ep
= *pep
;
2026 ep
->driver_data
= NULL
;
2029 static int usbg_bind(struct usb_configuration
*c
, struct usb_function
*f
)
2031 struct f_uas
*fu
= to_f_uas(f
);
2032 struct usb_gadget
*gadget
= c
->cdev
->gadget
;
2037 iface
= usb_interface_id(c
, f
);
2041 bot_intf_desc
.bInterfaceNumber
= iface
;
2042 uasp_intf_desc
.bInterfaceNumber
= iface
;
2044 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_bi_desc
,
2045 &uasp_bi_ep_comp_desc
);
2049 ep
->driver_data
= fu
;
2052 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_bo_desc
,
2053 &uasp_bo_ep_comp_desc
);
2056 ep
->driver_data
= fu
;
2059 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_status_desc
,
2060 &uasp_status_in_ep_comp_desc
);
2063 ep
->driver_data
= fu
;
2066 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_cmd_desc
,
2067 &uasp_cmd_comp_desc
);
2070 ep
->driver_data
= fu
;
2073 /* Assume endpoint addresses are the same for both speeds */
2074 uasp_bi_desc
.bEndpointAddress
= uasp_ss_bi_desc
.bEndpointAddress
;
2075 uasp_bo_desc
.bEndpointAddress
= uasp_ss_bo_desc
.bEndpointAddress
;
2076 uasp_status_desc
.bEndpointAddress
=
2077 uasp_ss_status_desc
.bEndpointAddress
;
2078 uasp_cmd_desc
.bEndpointAddress
= uasp_ss_cmd_desc
.bEndpointAddress
;
2080 uasp_fs_bi_desc
.bEndpointAddress
= uasp_ss_bi_desc
.bEndpointAddress
;
2081 uasp_fs_bo_desc
.bEndpointAddress
= uasp_ss_bo_desc
.bEndpointAddress
;
2082 uasp_fs_status_desc
.bEndpointAddress
=
2083 uasp_ss_status_desc
.bEndpointAddress
;
2084 uasp_fs_cmd_desc
.bEndpointAddress
= uasp_ss_cmd_desc
.bEndpointAddress
;
2086 ret
= usb_assign_descriptors(f
, uasp_fs_function_desc
,
2087 uasp_hs_function_desc
, uasp_ss_function_desc
);
2093 pr_err("Can't claim all required eps\n");
2095 give_back_ep(&fu
->ep_in
);
2096 give_back_ep(&fu
->ep_out
);
2097 give_back_ep(&fu
->ep_status
);
2098 give_back_ep(&fu
->ep_cmd
);
2102 static void usbg_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
2104 struct f_uas
*fu
= to_f_uas(f
);
2106 usb_free_all_descriptors(f
);
2110 struct guas_setup_wq
{
2111 struct work_struct work
;
2116 static void usbg_delayed_set_alt(struct work_struct
*wq
)
2118 struct guas_setup_wq
*work
= container_of(wq
, struct guas_setup_wq
,
2120 struct f_uas
*fu
= work
->fu
;
2121 int alt
= work
->alt
;
2125 if (fu
->flags
& USBG_IS_BOT
)
2126 bot_cleanup_old_alt(fu
);
2127 if (fu
->flags
& USBG_IS_UAS
)
2128 uasp_cleanup_old_alt(fu
);
2130 if (alt
== USB_G_ALT_INT_BBB
)
2132 else if (alt
== USB_G_ALT_INT_UAS
)
2134 usb_composite_setup_continue(fu
->function
.config
->cdev
);
2137 static int usbg_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
2139 struct f_uas
*fu
= to_f_uas(f
);
2141 if ((alt
== USB_G_ALT_INT_BBB
) || (alt
== USB_G_ALT_INT_UAS
)) {
2142 struct guas_setup_wq
*work
;
2144 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
2147 INIT_WORK(&work
->work
, usbg_delayed_set_alt
);
2150 schedule_work(&work
->work
);
2151 return USB_GADGET_DELAYED_STATUS
;
2156 static void usbg_disable(struct usb_function
*f
)
2158 struct f_uas
*fu
= to_f_uas(f
);
2160 if (fu
->flags
& USBG_IS_UAS
)
2161 uasp_cleanup_old_alt(fu
);
2162 else if (fu
->flags
& USBG_IS_BOT
)
2163 bot_cleanup_old_alt(fu
);
2167 static int usbg_setup(struct usb_function
*f
,
2168 const struct usb_ctrlrequest
*ctrl
)
2170 struct f_uas
*fu
= to_f_uas(f
);
2172 if (!(fu
->flags
& USBG_IS_BOT
))
2175 return usbg_bot_setup(f
, ctrl
);
2178 static int usbg_cfg_bind(struct usb_configuration
*c
)
2183 fu
= kzalloc(sizeof(*fu
), GFP_KERNEL
);
2186 fu
->function
.name
= "Target Function";
2187 fu
->function
.bind
= usbg_bind
;
2188 fu
->function
.unbind
= usbg_unbind
;
2189 fu
->function
.set_alt
= usbg_set_alt
;
2190 fu
->function
.setup
= usbg_setup
;
2191 fu
->function
.disable
= usbg_disable
;
2192 fu
->tpg
= the_only_tpg_I_currently_have
;
2194 bot_intf_desc
.iInterface
= usbg_us_strings
[USB_G_STR_INT_BBB
].id
;
2195 uasp_intf_desc
.iInterface
= usbg_us_strings
[USB_G_STR_INT_UAS
].id
;
2197 ret
= usb_add_function(c
, &fu
->function
);
2207 static int usb_target_bind(struct usb_composite_dev
*cdev
)
2211 ret
= usb_string_ids_tab(cdev
, usbg_us_strings
);
2215 usbg_device_desc
.iManufacturer
=
2216 usbg_us_strings
[USB_GADGET_MANUFACTURER_IDX
].id
;
2217 usbg_device_desc
.iProduct
= usbg_us_strings
[USB_GADGET_PRODUCT_IDX
].id
;
2218 usbg_device_desc
.iSerialNumber
=
2219 usbg_us_strings
[USB_GADGET_SERIAL_IDX
].id
;
2220 usbg_config_driver
.iConfiguration
=
2221 usbg_us_strings
[USB_G_STR_CONFIG
].id
;
2223 ret
= usb_add_config(cdev
, &usbg_config_driver
,
2227 usb_composite_overwrite_options(cdev
, &coverwrite
);
2231 static struct usb_composite_driver usbg_driver
= {
2233 .dev
= &usbg_device_desc
,
2234 .strings
= usbg_strings
,
2235 .max_speed
= USB_SPEED_SUPER
,
2236 .bind
= usb_target_bind
,
2237 .unbind
= guas_unbind
,
2240 static int usbg_attach(struct usbg_tpg
*tpg
)
2242 return usb_composite_probe(&usbg_driver
);
2245 static void usbg_detach(struct usbg_tpg
*tpg
)
2247 usb_composite_unregister(&usbg_driver
);
2250 static int __init
usb_target_gadget_init(void)
2252 return target_register_template(&usbg_ops
);
2254 module_init(usb_target_gadget_init
);
2256 static void __exit
usb_target_gadget_exit(void)
2258 target_unregister_template(&usbg_ops
);
2260 module_exit(usb_target_gadget_exit
);
2262 MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
2263 MODULE_DESCRIPTION("usb-gadget fabric");
2264 MODULE_LICENSE("GPL v2");