1 /* Target based USB-Gadget
3 * UAS protocol handling, target callbacks, configfs handling,
4 * BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling.
6 * Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de>
7 * License: GPLv2 as published by FSF.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/configfs.h>
14 #include <linux/ctype.h>
15 #include <linux/usb/ch9.h>
16 #include <linux/usb/composite.h>
17 #include <linux/usb/gadget.h>
18 #include <linux/usb/storage.h>
19 #include <scsi/scsi.h>
20 #include <scsi/scsi_tcq.h>
21 #include <target/target_core_base.h>
22 #include <target/target_core_fabric.h>
23 #include <target/target_core_fabric_configfs.h>
24 #include <target/target_core_configfs.h>
25 #include <target/configfs_macros.h>
26 #include <asm/unaligned.h>
28 #include "usbstring.c"
29 #include "epautoconf.c"
31 #include "composite.c"
33 #include "tcm_usb_gadget.h"
35 static struct target_fabric_configfs
*usbg_fabric_configfs
;
37 static inline struct f_uas
*to_f_uas(struct usb_function
*f
)
39 return container_of(f
, struct f_uas
, function
);
42 static void usbg_cmd_release(struct kref
*);
44 static inline void usbg_cleanup_cmd(struct usbg_cmd
*cmd
)
46 kref_put(&cmd
->ref
, usbg_cmd_release
);
49 /* Start bot.c code */
51 static int bot_enqueue_cmd_cbw(struct f_uas
*fu
)
55 if (fu
->flags
& USBG_BOT_CMD_PEND
)
58 ret
= usb_ep_queue(fu
->ep_out
, fu
->cmd
.req
, GFP_ATOMIC
);
60 fu
->flags
|= USBG_BOT_CMD_PEND
;
64 static void bot_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
66 struct usbg_cmd
*cmd
= req
->context
;
67 struct f_uas
*fu
= cmd
->fu
;
69 usbg_cleanup_cmd(cmd
);
70 if (req
->status
< 0) {
71 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
75 /* CSW completed, wait for next CBW */
76 bot_enqueue_cmd_cbw(fu
);
79 static void bot_enqueue_sense_code(struct f_uas
*fu
, struct usbg_cmd
*cmd
)
81 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
84 unsigned int csw_stat
;
86 csw_stat
= cmd
->csw_code
;
89 * We can't send SENSE as a response. So we take ASC & ASCQ from our
90 * sense buffer and queue it and hope the host sends a REQUEST_SENSE
91 * command where it learns why we failed.
93 sense
= cmd
->sense_iu
.sense
;
95 csw
->Tag
= cmd
->bot_tag
;
96 csw
->Status
= csw_stat
;
97 fu
->bot_status
.req
->context
= cmd
;
98 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_status
.req
, GFP_ATOMIC
);
100 pr_err("%s(%d) ERR: %d\n", __func__
, __LINE__
, ret
);
103 static void bot_err_compl(struct usb_ep
*ep
, struct usb_request
*req
)
105 struct usbg_cmd
*cmd
= req
->context
;
106 struct f_uas
*fu
= cmd
->fu
;
109 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
112 if (cmd
->data_len
> ep
->maxpacket
) {
113 req
->length
= ep
->maxpacket
;
114 cmd
->data_len
-= ep
->maxpacket
;
116 req
->length
= cmd
->data_len
;
120 usb_ep_queue(ep
, req
, GFP_ATOMIC
);
123 bot_enqueue_sense_code(fu
, cmd
);
126 static void bot_send_bad_status(struct usbg_cmd
*cmd
)
128 struct f_uas
*fu
= cmd
->fu
;
129 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
130 struct usb_request
*req
;
133 csw
->Residue
= cpu_to_le32(cmd
->data_len
);
138 req
= fu
->bot_req_in
;
141 req
= fu
->bot_req_out
;
144 if (cmd
->data_len
> fu
->ep_in
->maxpacket
) {
145 req
->length
= ep
->maxpacket
;
146 cmd
->data_len
-= ep
->maxpacket
;
148 req
->length
= cmd
->data_len
;
151 req
->complete
= bot_err_compl
;
153 req
->buf
= fu
->cmd
.buf
;
154 usb_ep_queue(ep
, req
, GFP_KERNEL
);
156 bot_enqueue_sense_code(fu
, cmd
);
160 static int bot_send_status(struct usbg_cmd
*cmd
, bool moved_data
)
162 struct f_uas
*fu
= cmd
->fu
;
163 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
166 if (cmd
->se_cmd
.scsi_status
== SAM_STAT_GOOD
) {
167 if (!moved_data
&& cmd
->data_len
) {
169 * the host wants to move data, we don't. Fill / empty
170 * the pipe and then send the csw with reside set.
172 cmd
->csw_code
= US_BULK_STAT_OK
;
173 bot_send_bad_status(cmd
);
177 csw
->Tag
= cmd
->bot_tag
;
178 csw
->Residue
= cpu_to_le32(0);
179 csw
->Status
= US_BULK_STAT_OK
;
180 fu
->bot_status
.req
->context
= cmd
;
182 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_status
.req
, GFP_KERNEL
);
184 pr_err("%s(%d) ERR: %d\n", __func__
, __LINE__
, ret
);
186 cmd
->csw_code
= US_BULK_STAT_FAIL
;
187 bot_send_bad_status(cmd
);
193 * Called after command (no data transfer) or after the write (to device)
194 * operation is completed
196 static int bot_send_status_response(struct usbg_cmd
*cmd
)
198 bool moved_data
= false;
202 return bot_send_status(cmd
, moved_data
);
205 /* Read request completed, now we have to send the CSW */
206 static void bot_read_compl(struct usb_ep
*ep
, struct usb_request
*req
)
208 struct usbg_cmd
*cmd
= req
->context
;
211 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
213 bot_send_status(cmd
, true);
216 static int bot_send_read_response(struct usbg_cmd
*cmd
)
218 struct f_uas
*fu
= cmd
->fu
;
219 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
220 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
223 if (!cmd
->data_len
) {
224 cmd
->csw_code
= US_BULK_STAT_PHASE
;
225 bot_send_bad_status(cmd
);
229 if (!gadget
->sg_supported
) {
230 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
234 sg_copy_to_buffer(se_cmd
->t_data_sg
,
235 se_cmd
->t_data_nents
,
237 se_cmd
->data_length
);
239 fu
->bot_req_in
->buf
= cmd
->data_buf
;
241 fu
->bot_req_in
->buf
= NULL
;
242 fu
->bot_req_in
->num_sgs
= se_cmd
->t_data_nents
;
243 fu
->bot_req_in
->sg
= se_cmd
->t_data_sg
;
246 fu
->bot_req_in
->complete
= bot_read_compl
;
247 fu
->bot_req_in
->length
= se_cmd
->data_length
;
248 fu
->bot_req_in
->context
= cmd
;
249 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_req_in
, GFP_ATOMIC
);
251 pr_err("%s(%d)\n", __func__
, __LINE__
);
255 static void usbg_data_write_cmpl(struct usb_ep
*, struct usb_request
*);
256 static int usbg_prepare_w_request(struct usbg_cmd
*, struct usb_request
*);
258 static int bot_send_write_request(struct usbg_cmd
*cmd
)
260 struct f_uas
*fu
= cmd
->fu
;
261 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
262 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
265 init_completion(&cmd
->write_complete
);
268 if (!cmd
->data_len
) {
269 cmd
->csw_code
= US_BULK_STAT_PHASE
;
273 if (!gadget
->sg_supported
) {
274 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_KERNEL
);
278 fu
->bot_req_out
->buf
= cmd
->data_buf
;
280 fu
->bot_req_out
->buf
= NULL
;
281 fu
->bot_req_out
->num_sgs
= se_cmd
->t_data_nents
;
282 fu
->bot_req_out
->sg
= se_cmd
->t_data_sg
;
285 fu
->bot_req_out
->complete
= usbg_data_write_cmpl
;
286 fu
->bot_req_out
->length
= se_cmd
->data_length
;
287 fu
->bot_req_out
->context
= cmd
;
289 ret
= usbg_prepare_w_request(cmd
, fu
->bot_req_out
);
292 ret
= usb_ep_queue(fu
->ep_out
, fu
->bot_req_out
, GFP_KERNEL
);
294 pr_err("%s(%d)\n", __func__
, __LINE__
);
296 wait_for_completion(&cmd
->write_complete
);
297 target_execute_cmd(se_cmd
);
302 static int bot_submit_command(struct f_uas
*, void *, unsigned int);
304 static void bot_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
306 struct f_uas
*fu
= req
->context
;
309 fu
->flags
&= ~USBG_BOT_CMD_PEND
;
314 ret
= bot_submit_command(fu
, req
->buf
, req
->actual
);
316 pr_err("%s(%d): %d\n", __func__
, __LINE__
, ret
);
319 static int bot_prepare_reqs(struct f_uas
*fu
)
323 fu
->bot_req_in
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
327 fu
->bot_req_out
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
328 if (!fu
->bot_req_out
)
331 fu
->cmd
.req
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
335 fu
->bot_status
.req
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
336 if (!fu
->bot_status
.req
)
339 fu
->bot_status
.req
->buf
= &fu
->bot_status
.csw
;
340 fu
->bot_status
.req
->length
= US_BULK_CS_WRAP_LEN
;
341 fu
->bot_status
.req
->complete
= bot_status_complete
;
342 fu
->bot_status
.csw
.Signature
= cpu_to_le32(US_BULK_CS_SIGN
);
344 fu
->cmd
.buf
= kmalloc(fu
->ep_out
->maxpacket
, GFP_KERNEL
);
348 fu
->cmd
.req
->complete
= bot_cmd_complete
;
349 fu
->cmd
.req
->buf
= fu
->cmd
.buf
;
350 fu
->cmd
.req
->length
= fu
->ep_out
->maxpacket
;
351 fu
->cmd
.req
->context
= fu
;
353 ret
= bot_enqueue_cmd_cbw(fu
);
361 usb_ep_free_request(fu
->ep_in
, fu
->bot_status
.req
);
363 usb_ep_free_request(fu
->ep_out
, fu
->cmd
.req
);
366 usb_ep_free_request(fu
->ep_out
, fu
->bot_req_out
);
367 fu
->bot_req_out
= NULL
;
369 usb_ep_free_request(fu
->ep_in
, fu
->bot_req_in
);
370 fu
->bot_req_in
= NULL
;
372 pr_err("BOT: endpoint setup failed\n");
376 void bot_cleanup_old_alt(struct f_uas
*fu
)
378 if (!(fu
->flags
& USBG_ENABLED
))
381 usb_ep_disable(fu
->ep_in
);
382 usb_ep_disable(fu
->ep_out
);
387 usb_ep_free_request(fu
->ep_in
, fu
->bot_req_in
);
388 usb_ep_free_request(fu
->ep_out
, fu
->bot_req_out
);
389 usb_ep_free_request(fu
->ep_out
, fu
->cmd
.req
);
390 usb_ep_free_request(fu
->ep_out
, fu
->bot_status
.req
);
394 fu
->bot_req_in
= NULL
;
395 fu
->bot_req_out
= NULL
;
397 fu
->bot_status
.req
= NULL
;
401 static void bot_set_alt(struct f_uas
*fu
)
403 struct usb_function
*f
= &fu
->function
;
404 struct usb_gadget
*gadget
= f
->config
->cdev
->gadget
;
407 fu
->flags
= USBG_IS_BOT
;
409 config_ep_by_speed(gadget
, f
, fu
->ep_in
);
410 ret
= usb_ep_enable(fu
->ep_in
);
414 config_ep_by_speed(gadget
, f
, fu
->ep_out
);
415 ret
= usb_ep_enable(fu
->ep_out
);
419 ret
= bot_prepare_reqs(fu
);
422 fu
->flags
|= USBG_ENABLED
;
423 pr_info("Using the BOT protocol\n");
426 usb_ep_disable(fu
->ep_out
);
428 usb_ep_disable(fu
->ep_in
);
430 fu
->flags
= USBG_IS_BOT
;
433 static int usbg_bot_setup(struct usb_function
*f
,
434 const struct usb_ctrlrequest
*ctrl
)
436 struct f_uas
*fu
= to_f_uas(f
);
437 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
438 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
439 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
443 switch (ctrl
->bRequest
) {
444 case US_BULK_GET_MAX_LUN
:
445 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_TYPE_CLASS
|
446 USB_RECIP_INTERFACE
))
453 luns
= atomic_read(&fu
->tpg
->tpg_port_count
);
455 pr_err("No LUNs configured?\n");
459 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
460 * accessed. The upper limit is 0xf
464 pr_info_once("Limiting the number of luns to 16\n");
467 ret_lun
= cdev
->req
->buf
;
469 cdev
->req
->length
= 1;
470 return usb_ep_queue(cdev
->gadget
->ep0
, cdev
->req
, GFP_ATOMIC
);
473 case US_BULK_RESET_REQUEST
:
474 /* XXX maybe we should remove previous requests for IN + OUT */
475 bot_enqueue_cmd_cbw(fu
);
482 /* Start uas.c code */
484 static void uasp_cleanup_one_stream(struct f_uas
*fu
, struct uas_stream
*stream
)
486 /* We have either all three allocated or none */
490 usb_ep_free_request(fu
->ep_in
, stream
->req_in
);
491 usb_ep_free_request(fu
->ep_out
, stream
->req_out
);
492 usb_ep_free_request(fu
->ep_status
, stream
->req_status
);
494 stream
->req_in
= NULL
;
495 stream
->req_out
= NULL
;
496 stream
->req_status
= NULL
;
499 static void uasp_free_cmdreq(struct f_uas
*fu
)
501 usb_ep_free_request(fu
->ep_cmd
, fu
->cmd
.req
);
507 static void uasp_cleanup_old_alt(struct f_uas
*fu
)
511 if (!(fu
->flags
& USBG_ENABLED
))
514 usb_ep_disable(fu
->ep_in
);
515 usb_ep_disable(fu
->ep_out
);
516 usb_ep_disable(fu
->ep_status
);
517 usb_ep_disable(fu
->ep_cmd
);
519 for (i
= 0; i
< UASP_SS_EP_COMP_NUM_STREAMS
; i
++)
520 uasp_cleanup_one_stream(fu
, &fu
->stream
[i
]);
521 uasp_free_cmdreq(fu
);
524 static void uasp_status_data_cmpl(struct usb_ep
*ep
, struct usb_request
*req
);
526 static int uasp_prepare_r_request(struct usbg_cmd
*cmd
)
528 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
529 struct f_uas
*fu
= cmd
->fu
;
530 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
531 struct uas_stream
*stream
= cmd
->stream
;
533 if (!gadget
->sg_supported
) {
534 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
538 sg_copy_to_buffer(se_cmd
->t_data_sg
,
539 se_cmd
->t_data_nents
,
541 se_cmd
->data_length
);
543 stream
->req_in
->buf
= cmd
->data_buf
;
545 stream
->req_in
->buf
= NULL
;
546 stream
->req_in
->num_sgs
= se_cmd
->t_data_nents
;
547 stream
->req_in
->sg
= se_cmd
->t_data_sg
;
550 stream
->req_in
->complete
= uasp_status_data_cmpl
;
551 stream
->req_in
->length
= se_cmd
->data_length
;
552 stream
->req_in
->context
= cmd
;
554 cmd
->state
= UASP_SEND_STATUS
;
558 static void uasp_prepare_status(struct usbg_cmd
*cmd
)
560 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
561 struct sense_iu
*iu
= &cmd
->sense_iu
;
562 struct uas_stream
*stream
= cmd
->stream
;
564 cmd
->state
= UASP_QUEUE_COMMAND
;
565 iu
->iu_id
= IU_ID_STATUS
;
566 iu
->tag
= cpu_to_be16(cmd
->tag
);
569 * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
571 iu
->len
= cpu_to_be16(se_cmd
->scsi_sense_length
);
572 iu
->status
= se_cmd
->scsi_status
;
573 stream
->req_status
->context
= cmd
;
574 stream
->req_status
->length
= se_cmd
->scsi_sense_length
+ 16;
575 stream
->req_status
->buf
= iu
;
576 stream
->req_status
->complete
= uasp_status_data_cmpl
;
579 static void uasp_status_data_cmpl(struct usb_ep
*ep
, struct usb_request
*req
)
581 struct usbg_cmd
*cmd
= req
->context
;
582 struct uas_stream
*stream
= cmd
->stream
;
583 struct f_uas
*fu
= cmd
->fu
;
589 switch (cmd
->state
) {
591 ret
= uasp_prepare_r_request(cmd
);
594 ret
= usb_ep_queue(fu
->ep_in
, stream
->req_in
, GFP_ATOMIC
);
596 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
599 case UASP_RECEIVE_DATA
:
600 ret
= usbg_prepare_w_request(cmd
, stream
->req_out
);
603 ret
= usb_ep_queue(fu
->ep_out
, stream
->req_out
, GFP_ATOMIC
);
605 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
608 case UASP_SEND_STATUS
:
609 uasp_prepare_status(cmd
);
610 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
613 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
616 case UASP_QUEUE_COMMAND
:
617 usbg_cleanup_cmd(cmd
);
618 usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
627 usbg_cleanup_cmd(cmd
);
630 static int uasp_send_status_response(struct usbg_cmd
*cmd
)
632 struct f_uas
*fu
= cmd
->fu
;
633 struct uas_stream
*stream
= cmd
->stream
;
634 struct sense_iu
*iu
= &cmd
->sense_iu
;
636 iu
->tag
= cpu_to_be16(cmd
->tag
);
637 stream
->req_status
->complete
= uasp_status_data_cmpl
;
638 stream
->req_status
->context
= cmd
;
640 uasp_prepare_status(cmd
);
641 return usb_ep_queue(fu
->ep_status
, stream
->req_status
, GFP_ATOMIC
);
644 static int uasp_send_read_response(struct usbg_cmd
*cmd
)
646 struct f_uas
*fu
= cmd
->fu
;
647 struct uas_stream
*stream
= cmd
->stream
;
648 struct sense_iu
*iu
= &cmd
->sense_iu
;
653 iu
->tag
= cpu_to_be16(cmd
->tag
);
654 if (fu
->flags
& USBG_USE_STREAMS
) {
656 ret
= uasp_prepare_r_request(cmd
);
659 ret
= usb_ep_queue(fu
->ep_in
, stream
->req_in
, GFP_ATOMIC
);
661 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
662 kfree(cmd
->data_buf
);
663 cmd
->data_buf
= NULL
;
668 iu
->iu_id
= IU_ID_READ_READY
;
669 iu
->tag
= cpu_to_be16(cmd
->tag
);
671 stream
->req_status
->complete
= uasp_status_data_cmpl
;
672 stream
->req_status
->context
= cmd
;
674 cmd
->state
= UASP_SEND_DATA
;
675 stream
->req_status
->buf
= iu
;
676 stream
->req_status
->length
= sizeof(struct iu
);
678 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
681 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
687 static int uasp_send_write_request(struct usbg_cmd
*cmd
)
689 struct f_uas
*fu
= cmd
->fu
;
690 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
691 struct uas_stream
*stream
= cmd
->stream
;
692 struct sense_iu
*iu
= &cmd
->sense_iu
;
695 init_completion(&cmd
->write_complete
);
698 iu
->tag
= cpu_to_be16(cmd
->tag
);
700 if (fu
->flags
& USBG_USE_STREAMS
) {
702 ret
= usbg_prepare_w_request(cmd
, stream
->req_out
);
705 ret
= usb_ep_queue(fu
->ep_out
, stream
->req_out
, GFP_ATOMIC
);
707 pr_err("%s(%d)\n", __func__
, __LINE__
);
711 iu
->iu_id
= IU_ID_WRITE_READY
;
712 iu
->tag
= cpu_to_be16(cmd
->tag
);
714 stream
->req_status
->complete
= uasp_status_data_cmpl
;
715 stream
->req_status
->context
= cmd
;
717 cmd
->state
= UASP_RECEIVE_DATA
;
718 stream
->req_status
->buf
= iu
;
719 stream
->req_status
->length
= sizeof(struct iu
);
721 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
724 pr_err("%s(%d)\n", __func__
, __LINE__
);
727 wait_for_completion(&cmd
->write_complete
);
728 target_execute_cmd(se_cmd
);
733 static int usbg_submit_command(struct f_uas
*, void *, unsigned int);
735 static void uasp_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
737 struct f_uas
*fu
= req
->context
;
743 ret
= usbg_submit_command(fu
, req
->buf
, req
->actual
);
745 * Once we tune for performance enqueue the command req here again so
746 * we can receive a second command while we processing this one. Pay
747 * attention to properly sync STAUS endpoint with DATA IN + OUT so you
752 usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
755 static int uasp_alloc_stream_res(struct f_uas
*fu
, struct uas_stream
*stream
)
757 stream
->req_in
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
761 stream
->req_out
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
762 if (!stream
->req_out
)
765 stream
->req_status
= usb_ep_alloc_request(fu
->ep_status
, GFP_KERNEL
);
766 if (!stream
->req_status
)
771 usb_ep_free_request(fu
->ep_status
, stream
->req_status
);
772 stream
->req_status
= NULL
;
774 usb_ep_free_request(fu
->ep_out
, stream
->req_out
);
775 stream
->req_out
= NULL
;
780 static int uasp_alloc_cmd(struct f_uas
*fu
)
782 fu
->cmd
.req
= usb_ep_alloc_request(fu
->ep_cmd
, GFP_KERNEL
);
786 fu
->cmd
.buf
= kmalloc(fu
->ep_cmd
->maxpacket
, GFP_KERNEL
);
790 fu
->cmd
.req
->complete
= uasp_cmd_complete
;
791 fu
->cmd
.req
->buf
= fu
->cmd
.buf
;
792 fu
->cmd
.req
->length
= fu
->ep_cmd
->maxpacket
;
793 fu
->cmd
.req
->context
= fu
;
797 usb_ep_free_request(fu
->ep_cmd
, fu
->cmd
.req
);
802 static void uasp_setup_stream_res(struct f_uas
*fu
, int max_streams
)
806 for (i
= 0; i
< max_streams
; i
++) {
807 struct uas_stream
*s
= &fu
->stream
[i
];
809 s
->req_in
->stream_id
= i
+ 1;
810 s
->req_out
->stream_id
= i
+ 1;
811 s
->req_status
->stream_id
= i
+ 1;
815 static int uasp_prepare_reqs(struct f_uas
*fu
)
821 if (fu
->flags
& USBG_USE_STREAMS
)
822 max_streams
= UASP_SS_EP_COMP_NUM_STREAMS
;
826 for (i
= 0; i
< max_streams
; i
++) {
827 ret
= uasp_alloc_stream_res(fu
, &fu
->stream
[i
]);
832 ret
= uasp_alloc_cmd(fu
);
834 goto err_free_stream
;
835 uasp_setup_stream_res(fu
, max_streams
);
837 ret
= usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
839 goto err_free_stream
;
844 uasp_free_cmdreq(fu
);
849 uasp_cleanup_one_stream(fu
, &fu
->stream
[i
- 1]);
853 pr_err("UASP: endpoint setup failed\n");
857 static void uasp_set_alt(struct f_uas
*fu
)
859 struct usb_function
*f
= &fu
->function
;
860 struct usb_gadget
*gadget
= f
->config
->cdev
->gadget
;
863 fu
->flags
= USBG_IS_UAS
;
865 if (gadget
->speed
== USB_SPEED_SUPER
)
866 fu
->flags
|= USBG_USE_STREAMS
;
868 config_ep_by_speed(gadget
, f
, fu
->ep_in
);
869 ret
= usb_ep_enable(fu
->ep_in
);
873 config_ep_by_speed(gadget
, f
, fu
->ep_out
);
874 ret
= usb_ep_enable(fu
->ep_out
);
878 config_ep_by_speed(gadget
, f
, fu
->ep_cmd
);
879 ret
= usb_ep_enable(fu
->ep_cmd
);
882 config_ep_by_speed(gadget
, f
, fu
->ep_status
);
883 ret
= usb_ep_enable(fu
->ep_status
);
887 ret
= uasp_prepare_reqs(fu
);
890 fu
->flags
|= USBG_ENABLED
;
892 pr_info("Using the UAS protocol\n");
895 usb_ep_disable(fu
->ep_status
);
897 usb_ep_disable(fu
->ep_cmd
);
899 usb_ep_disable(fu
->ep_out
);
901 usb_ep_disable(fu
->ep_in
);
906 static int get_cmd_dir(const unsigned char *cdb
)
918 case SERVICE_ACTION_IN
:
920 case PERSISTENT_RESERVE_IN
:
921 case SECURITY_PROTOCOL_IN
:
922 case ACCESS_CONTROL_IN
:
924 case READ_BLOCK_LIMITS
:
928 case READ_FORMAT_CAPACITIES
:
930 ret
= DMA_FROM_DEVICE
;
940 case WRITE_VERIFY_12
:
941 case PERSISTENT_RESERVE_OUT
:
942 case MAINTENANCE_OUT
:
943 case SECURITY_PROTOCOL_OUT
:
944 case ACCESS_CONTROL_OUT
:
947 case ALLOW_MEDIUM_REMOVAL
:
948 case TEST_UNIT_READY
:
949 case SYNCHRONIZE_CACHE
:
956 case WRITE_FILEMARKS
:
960 pr_warn("target: Unknown data direction for SCSI Opcode "
967 static void usbg_data_write_cmpl(struct usb_ep
*ep
, struct usb_request
*req
)
969 struct usbg_cmd
*cmd
= req
->context
;
970 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
972 if (req
->status
< 0) {
973 pr_err("%s() state %d transfer failed\n", __func__
, cmd
->state
);
977 if (req
->num_sgs
== 0) {
978 sg_copy_from_buffer(se_cmd
->t_data_sg
,
979 se_cmd
->t_data_nents
,
981 se_cmd
->data_length
);
984 complete(&cmd
->write_complete
);
988 usbg_cleanup_cmd(cmd
);
991 static int usbg_prepare_w_request(struct usbg_cmd
*cmd
, struct usb_request
*req
)
993 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
994 struct f_uas
*fu
= cmd
->fu
;
995 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
997 if (!gadget
->sg_supported
) {
998 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
1002 req
->buf
= cmd
->data_buf
;
1005 req
->num_sgs
= se_cmd
->t_data_nents
;
1006 req
->sg
= se_cmd
->t_data_sg
;
1009 req
->complete
= usbg_data_write_cmpl
;
1010 req
->length
= se_cmd
->data_length
;
1015 static int usbg_send_status_response(struct se_cmd
*se_cmd
)
1017 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1019 struct f_uas
*fu
= cmd
->fu
;
1021 if (fu
->flags
& USBG_IS_BOT
)
1022 return bot_send_status_response(cmd
);
1024 return uasp_send_status_response(cmd
);
1027 static int usbg_send_write_request(struct se_cmd
*se_cmd
)
1029 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1031 struct f_uas
*fu
= cmd
->fu
;
1033 if (fu
->flags
& USBG_IS_BOT
)
1034 return bot_send_write_request(cmd
);
1036 return uasp_send_write_request(cmd
);
1039 static int usbg_send_read_response(struct se_cmd
*se_cmd
)
1041 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1043 struct f_uas
*fu
= cmd
->fu
;
1045 if (fu
->flags
& USBG_IS_BOT
)
1046 return bot_send_read_response(cmd
);
1048 return uasp_send_read_response(cmd
);
1051 static void usbg_cmd_work(struct work_struct
*work
)
1053 struct usbg_cmd
*cmd
= container_of(work
, struct usbg_cmd
, work
);
1054 struct se_cmd
*se_cmd
;
1055 struct tcm_usbg_nexus
*tv_nexus
;
1056 struct usbg_tpg
*tpg
;
1059 se_cmd
= &cmd
->se_cmd
;
1061 tv_nexus
= tpg
->tpg_nexus
;
1062 dir
= get_cmd_dir(cmd
->cmd_buf
);
1064 transport_init_se_cmd(se_cmd
,
1065 tv_nexus
->tvn_se_sess
->se_tpg
->se_tpg_tfo
,
1066 tv_nexus
->tvn_se_sess
, cmd
->data_len
, DMA_NONE
,
1067 cmd
->prio_attr
, cmd
->sense_iu
.sense
);
1071 if (target_submit_cmd(se_cmd
, tv_nexus
->tvn_se_sess
,
1072 cmd
->cmd_buf
, cmd
->sense_iu
.sense
, cmd
->unpacked_lun
,
1073 0, cmd
->prio_attr
, dir
, TARGET_SCF_UNKNOWN_SIZE
) < 0)
1079 transport_send_check_condition_and_sense(se_cmd
,
1080 TCM_UNSUPPORTED_SCSI_OPCODE
, 1);
1081 usbg_cleanup_cmd(cmd
);
1084 static int usbg_submit_command(struct f_uas
*fu
,
1085 void *cmdbuf
, unsigned int len
)
1087 struct command_iu
*cmd_iu
= cmdbuf
;
1088 struct usbg_cmd
*cmd
;
1089 struct usbg_tpg
*tpg
;
1090 struct se_cmd
*se_cmd
;
1091 struct tcm_usbg_nexus
*tv_nexus
;
1095 if (cmd_iu
->iu_id
!= IU_ID_COMMAND
) {
1096 pr_err("Unsupported type %d\n", cmd_iu
->iu_id
);
1100 cmd
= kzalloc(sizeof *cmd
, GFP_ATOMIC
);
1106 /* XXX until I figure out why I can't free in on complete */
1107 kref_init(&cmd
->ref
);
1108 kref_get(&cmd
->ref
);
1111 cmd_len
= (cmd_iu
->len
& ~0x3) + 16;
1112 if (cmd_len
> USBG_MAX_CMD
)
1115 memcpy(cmd
->cmd_buf
, cmd_iu
->cdb
, cmd_len
);
1117 cmd
->tag
= be16_to_cpup(&cmd_iu
->tag
);
1118 if (fu
->flags
& USBG_USE_STREAMS
) {
1119 if (cmd
->tag
> UASP_SS_EP_COMP_NUM_STREAMS
)
1122 cmd
->stream
= &fu
->stream
[0];
1124 cmd
->stream
= &fu
->stream
[cmd
->tag
- 1];
1126 cmd
->stream
= &fu
->stream
[0];
1129 tv_nexus
= tpg
->tpg_nexus
;
1131 pr_err("Missing nexus, ignoring command\n");
1135 switch (cmd_iu
->prio_attr
& 0x7) {
1137 cmd
->prio_attr
= MSG_HEAD_TAG
;
1139 case UAS_ORDERED_TAG
:
1140 cmd
->prio_attr
= MSG_ORDERED_TAG
;
1143 cmd
->prio_attr
= MSG_ACA_TAG
;
1146 pr_debug_once("Unsupported prio_attr: %02x.\n",
1148 case UAS_SIMPLE_TAG
:
1149 cmd
->prio_attr
= MSG_SIMPLE_TAG
;
1153 se_cmd
= &cmd
->se_cmd
;
1154 cmd
->unpacked_lun
= scsilun_to_int(&cmd_iu
->lun
);
1156 INIT_WORK(&cmd
->work
, usbg_cmd_work
);
1157 ret
= queue_work(tpg
->workqueue
, &cmd
->work
);
1167 static void bot_cmd_work(struct work_struct
*work
)
1169 struct usbg_cmd
*cmd
= container_of(work
, struct usbg_cmd
, work
);
1170 struct se_cmd
*se_cmd
;
1171 struct tcm_usbg_nexus
*tv_nexus
;
1172 struct usbg_tpg
*tpg
;
1175 se_cmd
= &cmd
->se_cmd
;
1177 tv_nexus
= tpg
->tpg_nexus
;
1178 dir
= get_cmd_dir(cmd
->cmd_buf
);
1180 transport_init_se_cmd(se_cmd
,
1181 tv_nexus
->tvn_se_sess
->se_tpg
->se_tpg_tfo
,
1182 tv_nexus
->tvn_se_sess
, cmd
->data_len
, DMA_NONE
,
1183 cmd
->prio_attr
, cmd
->sense_iu
.sense
);
1187 if (target_submit_cmd(se_cmd
, tv_nexus
->tvn_se_sess
,
1188 cmd
->cmd_buf
, cmd
->sense_iu
.sense
, cmd
->unpacked_lun
,
1189 cmd
->data_len
, cmd
->prio_attr
, dir
, 0) < 0)
1195 transport_send_check_condition_and_sense(se_cmd
,
1196 TCM_UNSUPPORTED_SCSI_OPCODE
, 1);
1197 usbg_cleanup_cmd(cmd
);
1200 static int bot_submit_command(struct f_uas
*fu
,
1201 void *cmdbuf
, unsigned int len
)
1203 struct bulk_cb_wrap
*cbw
= cmdbuf
;
1204 struct usbg_cmd
*cmd
;
1205 struct usbg_tpg
*tpg
;
1206 struct se_cmd
*se_cmd
;
1207 struct tcm_usbg_nexus
*tv_nexus
;
1211 if (cbw
->Signature
!= cpu_to_le32(US_BULK_CB_SIGN
)) {
1212 pr_err("Wrong signature on CBW\n");
1216 pr_err("Wrong length for CBW\n");
1220 cmd_len
= cbw
->Length
;
1221 if (cmd_len
< 1 || cmd_len
> 16)
1224 cmd
= kzalloc(sizeof *cmd
, GFP_ATOMIC
);
1230 /* XXX until I figure out why I can't free in on complete */
1231 kref_init(&cmd
->ref
);
1232 kref_get(&cmd
->ref
);
1236 memcpy(cmd
->cmd_buf
, cbw
->CDB
, cmd_len
);
1238 cmd
->bot_tag
= cbw
->Tag
;
1240 tv_nexus
= tpg
->tpg_nexus
;
1242 pr_err("Missing nexus, ignoring command\n");
1246 cmd
->prio_attr
= MSG_SIMPLE_TAG
;
1247 se_cmd
= &cmd
->se_cmd
;
1248 cmd
->unpacked_lun
= cbw
->Lun
;
1249 cmd
->is_read
= cbw
->Flags
& US_BULK_FLAG_IN
? 1 : 0;
1250 cmd
->data_len
= le32_to_cpu(cbw
->DataTransferLength
);
1252 INIT_WORK(&cmd
->work
, bot_cmd_work
);
1253 ret
= queue_work(tpg
->workqueue
, &cmd
->work
);
1263 /* Start fabric.c code */
1265 static int usbg_check_true(struct se_portal_group
*se_tpg
)
1270 static int usbg_check_false(struct se_portal_group
*se_tpg
)
1275 static char *usbg_get_fabric_name(void)
1277 return "usb_gadget";
1280 static u8
usbg_get_fabric_proto_ident(struct se_portal_group
*se_tpg
)
1282 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1283 struct usbg_tpg
, se_tpg
);
1284 struct usbg_tport
*tport
= tpg
->tport
;
1287 switch (tport
->tport_proto_id
) {
1288 case SCSI_PROTOCOL_SAS
:
1290 proto_id
= sas_get_fabric_proto_ident(se_tpg
);
1297 static char *usbg_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1299 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1300 struct usbg_tpg
, se_tpg
);
1301 struct usbg_tport
*tport
= tpg
->tport
;
1303 return &tport
->tport_name
[0];
1306 static u16
usbg_get_tag(struct se_portal_group
*se_tpg
)
1308 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1309 struct usbg_tpg
, se_tpg
);
1310 return tpg
->tport_tpgt
;
1313 static u32
usbg_get_default_depth(struct se_portal_group
*se_tpg
)
1318 static u32
usbg_get_pr_transport_id(
1319 struct se_portal_group
*se_tpg
,
1320 struct se_node_acl
*se_nacl
,
1321 struct t10_pr_registration
*pr_reg
,
1325 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1326 struct usbg_tpg
, se_tpg
);
1327 struct usbg_tport
*tport
= tpg
->tport
;
1330 switch (tport
->tport_proto_id
) {
1331 case SCSI_PROTOCOL_SAS
:
1333 ret
= sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
1341 static u32
usbg_get_pr_transport_id_len(
1342 struct se_portal_group
*se_tpg
,
1343 struct se_node_acl
*se_nacl
,
1344 struct t10_pr_registration
*pr_reg
,
1347 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1348 struct usbg_tpg
, se_tpg
);
1349 struct usbg_tport
*tport
= tpg
->tport
;
1352 switch (tport
->tport_proto_id
) {
1353 case SCSI_PROTOCOL_SAS
:
1355 ret
= sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
1363 static char *usbg_parse_pr_out_transport_id(
1364 struct se_portal_group
*se_tpg
,
1367 char **port_nexus_ptr
)
1369 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1370 struct usbg_tpg
, se_tpg
);
1371 struct usbg_tport
*tport
= tpg
->tport
;
1374 switch (tport
->tport_proto_id
) {
1375 case SCSI_PROTOCOL_SAS
:
1377 tid
= sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
1384 static struct se_node_acl
*usbg_alloc_fabric_acl(struct se_portal_group
*se_tpg
)
1386 struct usbg_nacl
*nacl
;
1388 nacl
= kzalloc(sizeof(struct usbg_nacl
), GFP_KERNEL
);
1390 printk(KERN_ERR
"Unable to alocate struct usbg_nacl\n");
1394 return &nacl
->se_node_acl
;
1397 static void usbg_release_fabric_acl(
1398 struct se_portal_group
*se_tpg
,
1399 struct se_node_acl
*se_nacl
)
1401 struct usbg_nacl
*nacl
= container_of(se_nacl
,
1402 struct usbg_nacl
, se_node_acl
);
1406 static u32
usbg_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
1411 static void usbg_cmd_release(struct kref
*ref
)
1413 struct usbg_cmd
*cmd
= container_of(ref
, struct usbg_cmd
,
1416 transport_generic_free_cmd(&cmd
->se_cmd
, 0);
1419 static void usbg_release_cmd(struct se_cmd
*se_cmd
)
1421 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1423 kfree(cmd
->data_buf
);
1428 static int usbg_shutdown_session(struct se_session
*se_sess
)
1433 static void usbg_close_session(struct se_session
*se_sess
)
1438 static u32
usbg_sess_get_index(struct se_session
*se_sess
)
1444 * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
1446 static int usbg_write_pending_status(struct se_cmd
*se_cmd
)
1451 static void usbg_set_default_node_attrs(struct se_node_acl
*nacl
)
1456 static u32
usbg_get_task_tag(struct se_cmd
*se_cmd
)
1458 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1460 struct f_uas
*fu
= cmd
->fu
;
1462 if (fu
->flags
& USBG_IS_BOT
)
1463 return le32_to_cpu(cmd
->bot_tag
);
1468 static int usbg_get_cmd_state(struct se_cmd
*se_cmd
)
1473 static int usbg_queue_tm_rsp(struct se_cmd
*se_cmd
)
1478 static u16
usbg_set_fabric_sense_len(struct se_cmd
*se_cmd
, u32 sense_length
)
1483 static u16
usbg_get_fabric_sense_len(void)
1488 static const char *usbg_check_wwn(const char *name
)
1493 n
= strstr(name
, "naa.");
1498 if (len
== 0 || len
> USBG_NAMELEN
- 1)
1503 static struct se_node_acl
*usbg_make_nodeacl(
1504 struct se_portal_group
*se_tpg
,
1505 struct config_group
*group
,
1508 struct se_node_acl
*se_nacl
, *se_nacl_new
;
1509 struct usbg_nacl
*nacl
;
1512 const char *wnn_name
;
1514 wnn_name
= usbg_check_wwn(name
);
1516 return ERR_PTR(-EINVAL
);
1517 se_nacl_new
= usbg_alloc_fabric_acl(se_tpg
);
1519 return ERR_PTR(-ENOMEM
);
1523 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1524 * when converting a NodeACL from demo mode -> explict
1526 se_nacl
= core_tpg_add_initiator_node_acl(se_tpg
, se_nacl_new
,
1528 if (IS_ERR(se_nacl
)) {
1529 usbg_release_fabric_acl(se_tpg
, se_nacl_new
);
1533 * Locate our struct usbg_nacl and set the FC Nport WWPN
1535 nacl
= container_of(se_nacl
, struct usbg_nacl
, se_node_acl
);
1536 nacl
->iport_wwpn
= wwpn
;
1537 snprintf(nacl
->iport_name
, sizeof(nacl
->iport_name
), "%s", name
);
1541 static void usbg_drop_nodeacl(struct se_node_acl
*se_acl
)
1543 struct usbg_nacl
*nacl
= container_of(se_acl
,
1544 struct usbg_nacl
, se_node_acl
);
1545 core_tpg_del_initiator_node_acl(se_acl
->se_tpg
, se_acl
, 1);
1549 struct usbg_tpg
*the_only_tpg_I_currently_have
;
1551 static struct se_portal_group
*usbg_make_tpg(
1553 struct config_group
*group
,
1556 struct usbg_tport
*tport
= container_of(wwn
, struct usbg_tport
,
1558 struct usbg_tpg
*tpg
;
1562 if (strstr(name
, "tpgt_") != name
)
1563 return ERR_PTR(-EINVAL
);
1564 if (kstrtoul(name
+ 5, 0, &tpgt
) || tpgt
> UINT_MAX
)
1565 return ERR_PTR(-EINVAL
);
1566 if (the_only_tpg_I_currently_have
) {
1567 pr_err("Until the gadget framework can't handle multiple\n");
1568 pr_err("gadgets, you can't do this here.\n");
1569 return ERR_PTR(-EBUSY
);
1572 tpg
= kzalloc(sizeof(struct usbg_tpg
), GFP_KERNEL
);
1574 printk(KERN_ERR
"Unable to allocate struct usbg_tpg");
1575 return ERR_PTR(-ENOMEM
);
1577 mutex_init(&tpg
->tpg_mutex
);
1578 atomic_set(&tpg
->tpg_port_count
, 0);
1579 tpg
->workqueue
= alloc_workqueue("tcm_usb_gadget", 0, 1);
1580 if (!tpg
->workqueue
) {
1586 tpg
->tport_tpgt
= tpgt
;
1588 ret
= core_tpg_register(&usbg_fabric_configfs
->tf_ops
, wwn
,
1590 TRANSPORT_TPG_TYPE_NORMAL
);
1592 destroy_workqueue(tpg
->workqueue
);
1596 the_only_tpg_I_currently_have
= tpg
;
1597 return &tpg
->se_tpg
;
1600 static void usbg_drop_tpg(struct se_portal_group
*se_tpg
)
1602 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1603 struct usbg_tpg
, se_tpg
);
1605 core_tpg_deregister(se_tpg
);
1606 destroy_workqueue(tpg
->workqueue
);
1608 the_only_tpg_I_currently_have
= NULL
;
1611 static struct se_wwn
*usbg_make_tport(
1612 struct target_fabric_configfs
*tf
,
1613 struct config_group
*group
,
1616 struct usbg_tport
*tport
;
1617 const char *wnn_name
;
1620 wnn_name
= usbg_check_wwn(name
);
1622 return ERR_PTR(-EINVAL
);
1624 tport
= kzalloc(sizeof(struct usbg_tport
), GFP_KERNEL
);
1626 printk(KERN_ERR
"Unable to allocate struct usbg_tport");
1627 return ERR_PTR(-ENOMEM
);
1629 tport
->tport_wwpn
= wwpn
;
1630 snprintf(tport
->tport_name
, sizeof(tport
->tport_name
), wnn_name
);
1631 return &tport
->tport_wwn
;
1634 static void usbg_drop_tport(struct se_wwn
*wwn
)
1636 struct usbg_tport
*tport
= container_of(wwn
,
1637 struct usbg_tport
, tport_wwn
);
1642 * If somebody feels like dropping the version property, go ahead.
1644 static ssize_t
usbg_wwn_show_attr_version(
1645 struct target_fabric_configfs
*tf
,
1648 return sprintf(page
, "usb-gadget fabric module\n");
1650 TF_WWN_ATTR_RO(usbg
, version
);
1652 static struct configfs_attribute
*usbg_wwn_attrs
[] = {
1653 &usbg_wwn_version
.attr
,
1657 static ssize_t
tcm_usbg_tpg_show_enable(
1658 struct se_portal_group
*se_tpg
,
1661 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1663 return snprintf(page
, PAGE_SIZE
, "%u\n", tpg
->gadget_connect
);
1666 static int usbg_attach(struct usbg_tpg
*);
1667 static void usbg_detach(struct usbg_tpg
*);
1669 static ssize_t
tcm_usbg_tpg_store_enable(
1670 struct se_portal_group
*se_tpg
,
1674 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1678 ret
= kstrtoul(page
, 0, &op
);
1684 if (op
&& tpg
->gadget_connect
)
1686 if (!op
&& !tpg
->gadget_connect
)
1690 ret
= usbg_attach(tpg
);
1696 tpg
->gadget_connect
= op
;
1700 TF_TPG_BASE_ATTR(tcm_usbg
, enable
, S_IRUGO
| S_IWUSR
);
1702 static ssize_t
tcm_usbg_tpg_show_nexus(
1703 struct se_portal_group
*se_tpg
,
1706 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1707 struct tcm_usbg_nexus
*tv_nexus
;
1710 mutex_lock(&tpg
->tpg_mutex
);
1711 tv_nexus
= tpg
->tpg_nexus
;
1716 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1717 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1719 mutex_unlock(&tpg
->tpg_mutex
);
1723 static int tcm_usbg_make_nexus(struct usbg_tpg
*tpg
, char *name
)
1725 struct se_portal_group
*se_tpg
;
1726 struct tcm_usbg_nexus
*tv_nexus
;
1729 mutex_lock(&tpg
->tpg_mutex
);
1730 if (tpg
->tpg_nexus
) {
1732 pr_debug("tpg->tpg_nexus already exists\n");
1735 se_tpg
= &tpg
->se_tpg
;
1738 tv_nexus
= kzalloc(sizeof(*tv_nexus
), GFP_KERNEL
);
1740 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1743 tv_nexus
->tvn_se_sess
= transport_init_session();
1744 if (IS_ERR(tv_nexus
->tvn_se_sess
))
1748 * Since we are running in 'demo mode' this call with generate a
1749 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1750 * the SCSI Initiator port name of the passed configfs group 'name'.
1752 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1754 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1755 pr_debug("core_tpg_check_initiator_node_acl() failed"
1760 * Now register the TCM vHost virtual I_T Nexus as active with the
1761 * call to __transport_register_session()
1763 __transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1764 tv_nexus
->tvn_se_sess
, tv_nexus
);
1765 tpg
->tpg_nexus
= tv_nexus
;
1766 mutex_unlock(&tpg
->tpg_mutex
);
1770 transport_free_session(tv_nexus
->tvn_se_sess
);
1774 mutex_unlock(&tpg
->tpg_mutex
);
1778 static int tcm_usbg_drop_nexus(struct usbg_tpg
*tpg
)
1780 struct se_session
*se_sess
;
1781 struct tcm_usbg_nexus
*tv_nexus
;
1784 mutex_lock(&tpg
->tpg_mutex
);
1785 tv_nexus
= tpg
->tpg_nexus
;
1789 se_sess
= tv_nexus
->tvn_se_sess
;
1793 if (atomic_read(&tpg
->tpg_port_count
)) {
1795 pr_err("Unable to remove Host I_T Nexus with"
1796 " active TPG port count: %d\n",
1797 atomic_read(&tpg
->tpg_port_count
));
1801 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1802 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1804 * Release the SCSI I_T Nexus to the emulated vHost Target Port
1806 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1807 tpg
->tpg_nexus
= NULL
;
1811 mutex_unlock(&tpg
->tpg_mutex
);
1815 static ssize_t
tcm_usbg_tpg_store_nexus(
1816 struct se_portal_group
*se_tpg
,
1820 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1821 unsigned char i_port
[USBG_NAMELEN
], *ptr
;
1824 if (!strncmp(page
, "NULL", 4)) {
1825 ret
= tcm_usbg_drop_nexus(tpg
);
1826 return (!ret
) ? count
: ret
;
1828 if (strlen(page
) > USBG_NAMELEN
) {
1829 pr_err("Emulated NAA Sas Address: %s, exceeds"
1830 " max: %d\n", page
, USBG_NAMELEN
);
1833 snprintf(i_port
, USBG_NAMELEN
, "%s", page
);
1835 ptr
= strstr(i_port
, "naa.");
1837 pr_err("Missing 'naa.' prefix\n");
1841 if (i_port
[strlen(i_port
) - 1] == '\n')
1842 i_port
[strlen(i_port
) - 1] = '\0';
1844 ret
= tcm_usbg_make_nexus(tpg
, &i_port
[4]);
1849 TF_TPG_BASE_ATTR(tcm_usbg
, nexus
, S_IRUGO
| S_IWUSR
);
1851 static struct configfs_attribute
*usbg_base_attrs
[] = {
1852 &tcm_usbg_tpg_enable
.attr
,
1853 &tcm_usbg_tpg_nexus
.attr
,
1857 static int usbg_port_link(struct se_portal_group
*se_tpg
, struct se_lun
*lun
)
1859 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1861 atomic_inc(&tpg
->tpg_port_count
);
1862 smp_mb__after_atomic_inc();
1866 static void usbg_port_unlink(struct se_portal_group
*se_tpg
,
1867 struct se_lun
*se_lun
)
1869 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1871 atomic_dec(&tpg
->tpg_port_count
);
1872 smp_mb__after_atomic_dec();
1875 static int usbg_check_stop_free(struct se_cmd
*se_cmd
)
1877 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1880 kref_put(&cmd
->ref
, usbg_cmd_release
);
1884 static struct target_core_fabric_ops usbg_ops
= {
1885 .get_fabric_name
= usbg_get_fabric_name
,
1886 .get_fabric_proto_ident
= usbg_get_fabric_proto_ident
,
1887 .tpg_get_wwn
= usbg_get_fabric_wwn
,
1888 .tpg_get_tag
= usbg_get_tag
,
1889 .tpg_get_default_depth
= usbg_get_default_depth
,
1890 .tpg_get_pr_transport_id
= usbg_get_pr_transport_id
,
1891 .tpg_get_pr_transport_id_len
= usbg_get_pr_transport_id_len
,
1892 .tpg_parse_pr_out_transport_id
= usbg_parse_pr_out_transport_id
,
1893 .tpg_check_demo_mode
= usbg_check_true
,
1894 .tpg_check_demo_mode_cache
= usbg_check_false
,
1895 .tpg_check_demo_mode_write_protect
= usbg_check_false
,
1896 .tpg_check_prod_mode_write_protect
= usbg_check_false
,
1897 .tpg_alloc_fabric_acl
= usbg_alloc_fabric_acl
,
1898 .tpg_release_fabric_acl
= usbg_release_fabric_acl
,
1899 .tpg_get_inst_index
= usbg_tpg_get_inst_index
,
1900 .release_cmd
= usbg_release_cmd
,
1901 .shutdown_session
= usbg_shutdown_session
,
1902 .close_session
= usbg_close_session
,
1903 .sess_get_index
= usbg_sess_get_index
,
1904 .sess_get_initiator_sid
= NULL
,
1905 .write_pending
= usbg_send_write_request
,
1906 .write_pending_status
= usbg_write_pending_status
,
1907 .set_default_node_attributes
= usbg_set_default_node_attrs
,
1908 .get_task_tag
= usbg_get_task_tag
,
1909 .get_cmd_state
= usbg_get_cmd_state
,
1910 .queue_data_in
= usbg_send_read_response
,
1911 .queue_status
= usbg_send_status_response
,
1912 .queue_tm_rsp
= usbg_queue_tm_rsp
,
1913 .get_fabric_sense_len
= usbg_get_fabric_sense_len
,
1914 .set_fabric_sense_len
= usbg_set_fabric_sense_len
,
1915 .check_stop_free
= usbg_check_stop_free
,
1917 .fabric_make_wwn
= usbg_make_tport
,
1918 .fabric_drop_wwn
= usbg_drop_tport
,
1919 .fabric_make_tpg
= usbg_make_tpg
,
1920 .fabric_drop_tpg
= usbg_drop_tpg
,
1921 .fabric_post_link
= usbg_port_link
,
1922 .fabric_pre_unlink
= usbg_port_unlink
,
1923 .fabric_make_np
= NULL
,
1924 .fabric_drop_np
= NULL
,
1925 .fabric_make_nodeacl
= usbg_make_nodeacl
,
1926 .fabric_drop_nodeacl
= usbg_drop_nodeacl
,
1929 static int usbg_register_configfs(void)
1931 struct target_fabric_configfs
*fabric
;
1934 fabric
= target_fabric_configfs_init(THIS_MODULE
, "usb_gadget");
1935 if (IS_ERR(fabric
)) {
1936 printk(KERN_ERR
"target_fabric_configfs_init() failed\n");
1937 return PTR_ERR(fabric
);
1940 fabric
->tf_ops
= usbg_ops
;
1941 TF_CIT_TMPL(fabric
)->tfc_wwn_cit
.ct_attrs
= usbg_wwn_attrs
;
1942 TF_CIT_TMPL(fabric
)->tfc_tpg_base_cit
.ct_attrs
= usbg_base_attrs
;
1943 TF_CIT_TMPL(fabric
)->tfc_tpg_attrib_cit
.ct_attrs
= NULL
;
1944 TF_CIT_TMPL(fabric
)->tfc_tpg_param_cit
.ct_attrs
= NULL
;
1945 TF_CIT_TMPL(fabric
)->tfc_tpg_np_base_cit
.ct_attrs
= NULL
;
1946 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_base_cit
.ct_attrs
= NULL
;
1947 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_attrib_cit
.ct_attrs
= NULL
;
1948 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_auth_cit
.ct_attrs
= NULL
;
1949 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_param_cit
.ct_attrs
= NULL
;
1950 ret
= target_fabric_configfs_register(fabric
);
1952 printk(KERN_ERR
"target_fabric_configfs_register() failed"
1953 " for usb-gadget\n");
1956 usbg_fabric_configfs
= fabric
;
1960 static void usbg_deregister_configfs(void)
1962 if (!(usbg_fabric_configfs
))
1965 target_fabric_configfs_deregister(usbg_fabric_configfs
);
1966 usbg_fabric_configfs
= NULL
;
1969 /* Start gadget.c code */
1971 static struct usb_interface_descriptor bot_intf_desc
= {
1972 .bLength
= sizeof(bot_intf_desc
),
1973 .bDescriptorType
= USB_DT_INTERFACE
,
1974 .bAlternateSetting
= 0,
1976 .bAlternateSetting
= USB_G_ALT_INT_BBB
,
1977 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
1978 .bInterfaceSubClass
= USB_SC_SCSI
,
1979 .bInterfaceProtocol
= USB_PR_BULK
,
1980 .iInterface
= USB_G_STR_INT_UAS
,
1983 static struct usb_interface_descriptor uasp_intf_desc
= {
1984 .bLength
= sizeof(uasp_intf_desc
),
1985 .bDescriptorType
= USB_DT_INTERFACE
,
1987 .bAlternateSetting
= USB_G_ALT_INT_UAS
,
1988 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
1989 .bInterfaceSubClass
= USB_SC_SCSI
,
1990 .bInterfaceProtocol
= USB_PR_UAS
,
1991 .iInterface
= USB_G_STR_INT_BBB
,
1994 static struct usb_endpoint_descriptor uasp_bi_desc
= {
1995 .bLength
= USB_DT_ENDPOINT_SIZE
,
1996 .bDescriptorType
= USB_DT_ENDPOINT
,
1997 .bEndpointAddress
= USB_DIR_IN
,
1998 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1999 .wMaxPacketSize
= cpu_to_le16(512),
2002 static struct usb_endpoint_descriptor uasp_fs_bi_desc
= {
2003 .bLength
= USB_DT_ENDPOINT_SIZE
,
2004 .bDescriptorType
= USB_DT_ENDPOINT
,
2005 .bEndpointAddress
= USB_DIR_IN
,
2006 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2009 static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc
= {
2010 .bLength
= sizeof(uasp_bi_pipe_desc
),
2011 .bDescriptorType
= USB_DT_PIPE_USAGE
,
2012 .bPipeID
= DATA_IN_PIPE_ID
,
2015 static struct usb_endpoint_descriptor uasp_ss_bi_desc
= {
2016 .bLength
= USB_DT_ENDPOINT_SIZE
,
2017 .bDescriptorType
= USB_DT_ENDPOINT
,
2018 .bEndpointAddress
= USB_DIR_IN
,
2019 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2020 .wMaxPacketSize
= cpu_to_le16(1024),
2023 static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc
= {
2024 .bLength
= sizeof(uasp_bi_ep_comp_desc
),
2025 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2027 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
2028 .wBytesPerInterval
= 0,
2031 static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc
= {
2032 .bLength
= sizeof(bot_bi_ep_comp_desc
),
2033 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2037 static struct usb_endpoint_descriptor uasp_bo_desc
= {
2038 .bLength
= USB_DT_ENDPOINT_SIZE
,
2039 .bDescriptorType
= USB_DT_ENDPOINT
,
2040 .bEndpointAddress
= USB_DIR_OUT
,
2041 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2042 .wMaxPacketSize
= cpu_to_le16(512),
2045 static struct usb_endpoint_descriptor uasp_fs_bo_desc
= {
2046 .bLength
= USB_DT_ENDPOINT_SIZE
,
2047 .bDescriptorType
= USB_DT_ENDPOINT
,
2048 .bEndpointAddress
= USB_DIR_OUT
,
2049 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2052 static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc
= {
2053 .bLength
= sizeof(uasp_bo_pipe_desc
),
2054 .bDescriptorType
= USB_DT_PIPE_USAGE
,
2055 .bPipeID
= DATA_OUT_PIPE_ID
,
2058 static struct usb_endpoint_descriptor uasp_ss_bo_desc
= {
2059 .bLength
= USB_DT_ENDPOINT_SIZE
,
2060 .bDescriptorType
= USB_DT_ENDPOINT
,
2061 .bEndpointAddress
= USB_DIR_OUT
,
2062 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2063 .wMaxPacketSize
= cpu_to_le16(0x400),
2066 static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc
= {
2067 .bLength
= sizeof(uasp_bo_ep_comp_desc
),
2068 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2069 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
2072 static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc
= {
2073 .bLength
= sizeof(bot_bo_ep_comp_desc
),
2074 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2077 static struct usb_endpoint_descriptor uasp_status_desc
= {
2078 .bLength
= USB_DT_ENDPOINT_SIZE
,
2079 .bDescriptorType
= USB_DT_ENDPOINT
,
2080 .bEndpointAddress
= USB_DIR_IN
,
2081 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2082 .wMaxPacketSize
= cpu_to_le16(512),
2085 static struct usb_endpoint_descriptor uasp_fs_status_desc
= {
2086 .bLength
= USB_DT_ENDPOINT_SIZE
,
2087 .bDescriptorType
= USB_DT_ENDPOINT
,
2088 .bEndpointAddress
= USB_DIR_IN
,
2089 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2092 static struct usb_pipe_usage_descriptor uasp_status_pipe_desc
= {
2093 .bLength
= sizeof(uasp_status_pipe_desc
),
2094 .bDescriptorType
= USB_DT_PIPE_USAGE
,
2095 .bPipeID
= STATUS_PIPE_ID
,
2098 static struct usb_endpoint_descriptor uasp_ss_status_desc
= {
2099 .bLength
= USB_DT_ENDPOINT_SIZE
,
2100 .bDescriptorType
= USB_DT_ENDPOINT
,
2101 .bEndpointAddress
= USB_DIR_IN
,
2102 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2103 .wMaxPacketSize
= cpu_to_le16(1024),
2106 static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc
= {
2107 .bLength
= sizeof(uasp_status_in_ep_comp_desc
),
2108 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2109 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
2112 static struct usb_endpoint_descriptor uasp_cmd_desc
= {
2113 .bLength
= USB_DT_ENDPOINT_SIZE
,
2114 .bDescriptorType
= USB_DT_ENDPOINT
,
2115 .bEndpointAddress
= USB_DIR_OUT
,
2116 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2117 .wMaxPacketSize
= cpu_to_le16(512),
2120 static struct usb_endpoint_descriptor uasp_fs_cmd_desc
= {
2121 .bLength
= USB_DT_ENDPOINT_SIZE
,
2122 .bDescriptorType
= USB_DT_ENDPOINT
,
2123 .bEndpointAddress
= USB_DIR_OUT
,
2124 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2127 static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc
= {
2128 .bLength
= sizeof(uasp_cmd_pipe_desc
),
2129 .bDescriptorType
= USB_DT_PIPE_USAGE
,
2130 .bPipeID
= CMD_PIPE_ID
,
2133 static struct usb_endpoint_descriptor uasp_ss_cmd_desc
= {
2134 .bLength
= USB_DT_ENDPOINT_SIZE
,
2135 .bDescriptorType
= USB_DT_ENDPOINT
,
2136 .bEndpointAddress
= USB_DIR_OUT
,
2137 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2138 .wMaxPacketSize
= cpu_to_le16(1024),
2141 static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc
= {
2142 .bLength
= sizeof(uasp_cmd_comp_desc
),
2143 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2146 static struct usb_descriptor_header
*uasp_fs_function_desc
[] = {
2147 (struct usb_descriptor_header
*) &bot_intf_desc
,
2148 (struct usb_descriptor_header
*) &uasp_fs_bi_desc
,
2149 (struct usb_descriptor_header
*) &uasp_fs_bo_desc
,
2151 (struct usb_descriptor_header
*) &uasp_intf_desc
,
2152 (struct usb_descriptor_header
*) &uasp_fs_bi_desc
,
2153 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
2154 (struct usb_descriptor_header
*) &uasp_fs_bo_desc
,
2155 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
2156 (struct usb_descriptor_header
*) &uasp_fs_status_desc
,
2157 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
2158 (struct usb_descriptor_header
*) &uasp_fs_cmd_desc
,
2159 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
2162 static struct usb_descriptor_header
*uasp_hs_function_desc
[] = {
2163 (struct usb_descriptor_header
*) &bot_intf_desc
,
2164 (struct usb_descriptor_header
*) &uasp_bi_desc
,
2165 (struct usb_descriptor_header
*) &uasp_bo_desc
,
2167 (struct usb_descriptor_header
*) &uasp_intf_desc
,
2168 (struct usb_descriptor_header
*) &uasp_bi_desc
,
2169 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
2170 (struct usb_descriptor_header
*) &uasp_bo_desc
,
2171 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
2172 (struct usb_descriptor_header
*) &uasp_status_desc
,
2173 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
2174 (struct usb_descriptor_header
*) &uasp_cmd_desc
,
2175 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
2179 static struct usb_descriptor_header
*uasp_ss_function_desc
[] = {
2180 (struct usb_descriptor_header
*) &bot_intf_desc
,
2181 (struct usb_descriptor_header
*) &uasp_ss_bi_desc
,
2182 (struct usb_descriptor_header
*) &bot_bi_ep_comp_desc
,
2183 (struct usb_descriptor_header
*) &uasp_ss_bo_desc
,
2184 (struct usb_descriptor_header
*) &bot_bo_ep_comp_desc
,
2186 (struct usb_descriptor_header
*) &uasp_intf_desc
,
2187 (struct usb_descriptor_header
*) &uasp_ss_bi_desc
,
2188 (struct usb_descriptor_header
*) &uasp_bi_ep_comp_desc
,
2189 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
2190 (struct usb_descriptor_header
*) &uasp_ss_bo_desc
,
2191 (struct usb_descriptor_header
*) &uasp_bo_ep_comp_desc
,
2192 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
2193 (struct usb_descriptor_header
*) &uasp_ss_status_desc
,
2194 (struct usb_descriptor_header
*) &uasp_status_in_ep_comp_desc
,
2195 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
2196 (struct usb_descriptor_header
*) &uasp_ss_cmd_desc
,
2197 (struct usb_descriptor_header
*) &uasp_cmd_comp_desc
,
2198 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
2202 #define UAS_VENDOR_ID 0x0525 /* NetChip */
2203 #define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
2205 static struct usb_device_descriptor usbg_device_desc
= {
2206 .bLength
= sizeof(usbg_device_desc
),
2207 .bDescriptorType
= USB_DT_DEVICE
,
2208 .bcdUSB
= cpu_to_le16(0x0200),
2209 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
2210 .idVendor
= cpu_to_le16(UAS_VENDOR_ID
),
2211 .idProduct
= cpu_to_le16(UAS_PRODUCT_ID
),
2212 .iManufacturer
= USB_G_STR_MANUFACTOR
,
2213 .iProduct
= USB_G_STR_PRODUCT
,
2214 .iSerialNumber
= USB_G_STR_SERIAL
,
2216 .bNumConfigurations
= 1,
2219 static struct usb_string usbg_us_strings
[] = {
2220 { USB_G_STR_MANUFACTOR
, "Target Manufactor"},
2221 { USB_G_STR_PRODUCT
, "Target Product"},
2222 { USB_G_STR_SERIAL
, "000000000001"},
2223 { USB_G_STR_CONFIG
, "default config"},
2224 { USB_G_STR_INT_UAS
, "USB Attached SCSI"},
2225 { USB_G_STR_INT_BBB
, "Bulk Only Transport"},
2229 static struct usb_gadget_strings usbg_stringtab
= {
2231 .strings
= usbg_us_strings
,
2234 static struct usb_gadget_strings
*usbg_strings
[] = {
2239 static int guas_unbind(struct usb_composite_dev
*cdev
)
2244 static struct usb_configuration usbg_config_driver
= {
2245 .label
= "Linux Target",
2246 .bConfigurationValue
= 1,
2247 .iConfiguration
= USB_G_STR_CONFIG
,
2248 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
2251 static void give_back_ep(struct usb_ep
**pep
)
2253 struct usb_ep
*ep
= *pep
;
2256 ep
->driver_data
= NULL
;
2259 static int usbg_bind(struct usb_configuration
*c
, struct usb_function
*f
)
2261 struct f_uas
*fu
= to_f_uas(f
);
2262 struct usb_gadget
*gadget
= c
->cdev
->gadget
;
2266 iface
= usb_interface_id(c
, f
);
2270 bot_intf_desc
.bInterfaceNumber
= iface
;
2271 uasp_intf_desc
.bInterfaceNumber
= iface
;
2273 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_bi_desc
,
2274 &uasp_bi_ep_comp_desc
);
2278 ep
->driver_data
= fu
;
2281 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_bo_desc
,
2282 &uasp_bo_ep_comp_desc
);
2285 ep
->driver_data
= fu
;
2288 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_status_desc
,
2289 &uasp_status_in_ep_comp_desc
);
2292 ep
->driver_data
= fu
;
2295 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_cmd_desc
,
2296 &uasp_cmd_comp_desc
);
2299 ep
->driver_data
= fu
;
2302 /* Assume endpoint addresses are the same for both speeds */
2303 uasp_bi_desc
.bEndpointAddress
= uasp_ss_bi_desc
.bEndpointAddress
;
2304 uasp_bo_desc
.bEndpointAddress
= uasp_ss_bo_desc
.bEndpointAddress
;
2305 uasp_status_desc
.bEndpointAddress
=
2306 uasp_ss_status_desc
.bEndpointAddress
;
2307 uasp_cmd_desc
.bEndpointAddress
= uasp_ss_cmd_desc
.bEndpointAddress
;
2309 uasp_fs_bi_desc
.bEndpointAddress
= uasp_ss_bi_desc
.bEndpointAddress
;
2310 uasp_fs_bo_desc
.bEndpointAddress
= uasp_ss_bo_desc
.bEndpointAddress
;
2311 uasp_fs_status_desc
.bEndpointAddress
=
2312 uasp_ss_status_desc
.bEndpointAddress
;
2313 uasp_fs_cmd_desc
.bEndpointAddress
= uasp_ss_cmd_desc
.bEndpointAddress
;
2317 pr_err("Can't claim all required eps\n");
2319 give_back_ep(&fu
->ep_in
);
2320 give_back_ep(&fu
->ep_out
);
2321 give_back_ep(&fu
->ep_status
);
2322 give_back_ep(&fu
->ep_cmd
);
2326 static void usbg_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
2328 struct f_uas
*fu
= to_f_uas(f
);
2333 struct guas_setup_wq
{
2334 struct work_struct work
;
2339 static void usbg_delayed_set_alt(struct work_struct
*wq
)
2341 struct guas_setup_wq
*work
= container_of(wq
, struct guas_setup_wq
,
2343 struct f_uas
*fu
= work
->fu
;
2344 int alt
= work
->alt
;
2348 if (fu
->flags
& USBG_IS_BOT
)
2349 bot_cleanup_old_alt(fu
);
2350 if (fu
->flags
& USBG_IS_UAS
)
2351 uasp_cleanup_old_alt(fu
);
2353 if (alt
== USB_G_ALT_INT_BBB
)
2355 else if (alt
== USB_G_ALT_INT_UAS
)
2357 usb_composite_setup_continue(fu
->function
.config
->cdev
);
2360 static int usbg_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
2362 struct f_uas
*fu
= to_f_uas(f
);
2364 if ((alt
== USB_G_ALT_INT_BBB
) || (alt
== USB_G_ALT_INT_UAS
)) {
2365 struct guas_setup_wq
*work
;
2367 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
2370 INIT_WORK(&work
->work
, usbg_delayed_set_alt
);
2373 schedule_work(&work
->work
);
2374 return USB_GADGET_DELAYED_STATUS
;
2379 static void usbg_disable(struct usb_function
*f
)
2381 struct f_uas
*fu
= to_f_uas(f
);
2383 if (fu
->flags
& USBG_IS_UAS
)
2384 uasp_cleanup_old_alt(fu
);
2385 else if (fu
->flags
& USBG_IS_BOT
)
2386 bot_cleanup_old_alt(fu
);
2390 static int usbg_setup(struct usb_function
*f
,
2391 const struct usb_ctrlrequest
*ctrl
)
2393 struct f_uas
*fu
= to_f_uas(f
);
2395 if (!(fu
->flags
& USBG_IS_BOT
))
2398 return usbg_bot_setup(f
, ctrl
);
2401 static int usbg_cfg_bind(struct usb_configuration
*c
)
2406 fu
= kzalloc(sizeof(*fu
), GFP_KERNEL
);
2409 fu
->function
.name
= "Target Function";
2410 fu
->function
.descriptors
= uasp_fs_function_desc
;
2411 fu
->function
.hs_descriptors
= uasp_hs_function_desc
;
2412 fu
->function
.ss_descriptors
= uasp_ss_function_desc
;
2413 fu
->function
.bind
= usbg_bind
;
2414 fu
->function
.unbind
= usbg_unbind
;
2415 fu
->function
.set_alt
= usbg_set_alt
;
2416 fu
->function
.setup
= usbg_setup
;
2417 fu
->function
.disable
= usbg_disable
;
2418 fu
->tpg
= the_only_tpg_I_currently_have
;
2420 ret
= usb_add_function(c
, &fu
->function
);
2430 static int usb_target_bind(struct usb_composite_dev
*cdev
)
2434 ret
= usb_add_config(cdev
, &usbg_config_driver
,
2439 static struct usb_composite_driver usbg_driver
= {
2441 .dev
= &usbg_device_desc
,
2442 .strings
= usbg_strings
,
2443 .max_speed
= USB_SPEED_SUPER
,
2444 .unbind
= guas_unbind
,
2447 static int usbg_attach(struct usbg_tpg
*tpg
)
2449 return usb_composite_probe(&usbg_driver
, usb_target_bind
);
2452 static void usbg_detach(struct usbg_tpg
*tpg
)
2454 usb_composite_unregister(&usbg_driver
);
2457 static int __init
usb_target_gadget_init(void)
2461 ret
= usbg_register_configfs();
2464 module_init(usb_target_gadget_init
);
2466 static void __exit
usb_target_gadget_exit(void)
2468 usbg_deregister_configfs();
2470 module_exit(usb_target_gadget_exit
);
2472 MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
2473 MODULE_DESCRIPTION("usb-gadget fabric");
2474 MODULE_LICENSE("GPL v2");