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 "tcm_usb_gadget.h"
30 USB_GADGET_COMPOSITE_OPTIONS();
32 static struct target_fabric_configfs
*usbg_fabric_configfs
;
34 static inline struct f_uas
*to_f_uas(struct usb_function
*f
)
36 return container_of(f
, struct f_uas
, function
);
39 static void usbg_cmd_release(struct kref
*);
41 static inline void usbg_cleanup_cmd(struct usbg_cmd
*cmd
)
43 kref_put(&cmd
->ref
, usbg_cmd_release
);
46 /* Start bot.c code */
48 static int bot_enqueue_cmd_cbw(struct f_uas
*fu
)
52 if (fu
->flags
& USBG_BOT_CMD_PEND
)
55 ret
= usb_ep_queue(fu
->ep_out
, fu
->cmd
.req
, GFP_ATOMIC
);
57 fu
->flags
|= USBG_BOT_CMD_PEND
;
61 static void bot_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
63 struct usbg_cmd
*cmd
= req
->context
;
64 struct f_uas
*fu
= cmd
->fu
;
66 usbg_cleanup_cmd(cmd
);
67 if (req
->status
< 0) {
68 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
72 /* CSW completed, wait for next CBW */
73 bot_enqueue_cmd_cbw(fu
);
76 static void bot_enqueue_sense_code(struct f_uas
*fu
, struct usbg_cmd
*cmd
)
78 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
81 unsigned int csw_stat
;
83 csw_stat
= cmd
->csw_code
;
86 * We can't send SENSE as a response. So we take ASC & ASCQ from our
87 * sense buffer and queue it and hope the host sends a REQUEST_SENSE
88 * command where it learns why we failed.
90 sense
= cmd
->sense_iu
.sense
;
92 csw
->Tag
= cmd
->bot_tag
;
93 csw
->Status
= csw_stat
;
94 fu
->bot_status
.req
->context
= cmd
;
95 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_status
.req
, GFP_ATOMIC
);
97 pr_err("%s(%d) ERR: %d\n", __func__
, __LINE__
, ret
);
100 static void bot_err_compl(struct usb_ep
*ep
, struct usb_request
*req
)
102 struct usbg_cmd
*cmd
= req
->context
;
103 struct f_uas
*fu
= cmd
->fu
;
106 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
109 if (cmd
->data_len
> ep
->maxpacket
) {
110 req
->length
= ep
->maxpacket
;
111 cmd
->data_len
-= ep
->maxpacket
;
113 req
->length
= cmd
->data_len
;
117 usb_ep_queue(ep
, req
, GFP_ATOMIC
);
120 bot_enqueue_sense_code(fu
, cmd
);
123 static void bot_send_bad_status(struct usbg_cmd
*cmd
)
125 struct f_uas
*fu
= cmd
->fu
;
126 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
127 struct usb_request
*req
;
130 csw
->Residue
= cpu_to_le32(cmd
->data_len
);
135 req
= fu
->bot_req_in
;
138 req
= fu
->bot_req_out
;
141 if (cmd
->data_len
> fu
->ep_in
->maxpacket
) {
142 req
->length
= ep
->maxpacket
;
143 cmd
->data_len
-= ep
->maxpacket
;
145 req
->length
= cmd
->data_len
;
148 req
->complete
= bot_err_compl
;
150 req
->buf
= fu
->cmd
.buf
;
151 usb_ep_queue(ep
, req
, GFP_KERNEL
);
153 bot_enqueue_sense_code(fu
, cmd
);
157 static int bot_send_status(struct usbg_cmd
*cmd
, bool moved_data
)
159 struct f_uas
*fu
= cmd
->fu
;
160 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
163 if (cmd
->se_cmd
.scsi_status
== SAM_STAT_GOOD
) {
164 if (!moved_data
&& cmd
->data_len
) {
166 * the host wants to move data, we don't. Fill / empty
167 * the pipe and then send the csw with reside set.
169 cmd
->csw_code
= US_BULK_STAT_OK
;
170 bot_send_bad_status(cmd
);
174 csw
->Tag
= cmd
->bot_tag
;
175 csw
->Residue
= cpu_to_le32(0);
176 csw
->Status
= US_BULK_STAT_OK
;
177 fu
->bot_status
.req
->context
= cmd
;
179 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_status
.req
, GFP_KERNEL
);
181 pr_err("%s(%d) ERR: %d\n", __func__
, __LINE__
, ret
);
183 cmd
->csw_code
= US_BULK_STAT_FAIL
;
184 bot_send_bad_status(cmd
);
190 * Called after command (no data transfer) or after the write (to device)
191 * operation is completed
193 static int bot_send_status_response(struct usbg_cmd
*cmd
)
195 bool moved_data
= false;
199 return bot_send_status(cmd
, moved_data
);
202 /* Read request completed, now we have to send the CSW */
203 static void bot_read_compl(struct usb_ep
*ep
, struct usb_request
*req
)
205 struct usbg_cmd
*cmd
= req
->context
;
208 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
210 bot_send_status(cmd
, true);
213 static int bot_send_read_response(struct usbg_cmd
*cmd
)
215 struct f_uas
*fu
= cmd
->fu
;
216 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
217 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
220 if (!cmd
->data_len
) {
221 cmd
->csw_code
= US_BULK_STAT_PHASE
;
222 bot_send_bad_status(cmd
);
226 if (!gadget
->sg_supported
) {
227 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
231 sg_copy_to_buffer(se_cmd
->t_data_sg
,
232 se_cmd
->t_data_nents
,
234 se_cmd
->data_length
);
236 fu
->bot_req_in
->buf
= cmd
->data_buf
;
238 fu
->bot_req_in
->buf
= NULL
;
239 fu
->bot_req_in
->num_sgs
= se_cmd
->t_data_nents
;
240 fu
->bot_req_in
->sg
= se_cmd
->t_data_sg
;
243 fu
->bot_req_in
->complete
= bot_read_compl
;
244 fu
->bot_req_in
->length
= se_cmd
->data_length
;
245 fu
->bot_req_in
->context
= cmd
;
246 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_req_in
, GFP_ATOMIC
);
248 pr_err("%s(%d)\n", __func__
, __LINE__
);
252 static void usbg_data_write_cmpl(struct usb_ep
*, struct usb_request
*);
253 static int usbg_prepare_w_request(struct usbg_cmd
*, struct usb_request
*);
255 static int bot_send_write_request(struct usbg_cmd
*cmd
)
257 struct f_uas
*fu
= cmd
->fu
;
258 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
259 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
262 init_completion(&cmd
->write_complete
);
265 if (!cmd
->data_len
) {
266 cmd
->csw_code
= US_BULK_STAT_PHASE
;
270 if (!gadget
->sg_supported
) {
271 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_KERNEL
);
275 fu
->bot_req_out
->buf
= cmd
->data_buf
;
277 fu
->bot_req_out
->buf
= NULL
;
278 fu
->bot_req_out
->num_sgs
= se_cmd
->t_data_nents
;
279 fu
->bot_req_out
->sg
= se_cmd
->t_data_sg
;
282 fu
->bot_req_out
->complete
= usbg_data_write_cmpl
;
283 fu
->bot_req_out
->length
= se_cmd
->data_length
;
284 fu
->bot_req_out
->context
= cmd
;
286 ret
= usbg_prepare_w_request(cmd
, fu
->bot_req_out
);
289 ret
= usb_ep_queue(fu
->ep_out
, fu
->bot_req_out
, GFP_KERNEL
);
291 pr_err("%s(%d)\n", __func__
, __LINE__
);
293 wait_for_completion(&cmd
->write_complete
);
294 target_execute_cmd(se_cmd
);
299 static int bot_submit_command(struct f_uas
*, void *, unsigned int);
301 static void bot_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
303 struct f_uas
*fu
= req
->context
;
306 fu
->flags
&= ~USBG_BOT_CMD_PEND
;
311 ret
= bot_submit_command(fu
, req
->buf
, req
->actual
);
313 pr_err("%s(%d): %d\n", __func__
, __LINE__
, ret
);
316 static int bot_prepare_reqs(struct f_uas
*fu
)
320 fu
->bot_req_in
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
324 fu
->bot_req_out
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
325 if (!fu
->bot_req_out
)
328 fu
->cmd
.req
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
332 fu
->bot_status
.req
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
333 if (!fu
->bot_status
.req
)
336 fu
->bot_status
.req
->buf
= &fu
->bot_status
.csw
;
337 fu
->bot_status
.req
->length
= US_BULK_CS_WRAP_LEN
;
338 fu
->bot_status
.req
->complete
= bot_status_complete
;
339 fu
->bot_status
.csw
.Signature
= cpu_to_le32(US_BULK_CS_SIGN
);
341 fu
->cmd
.buf
= kmalloc(fu
->ep_out
->maxpacket
, GFP_KERNEL
);
345 fu
->cmd
.req
->complete
= bot_cmd_complete
;
346 fu
->cmd
.req
->buf
= fu
->cmd
.buf
;
347 fu
->cmd
.req
->length
= fu
->ep_out
->maxpacket
;
348 fu
->cmd
.req
->context
= fu
;
350 ret
= bot_enqueue_cmd_cbw(fu
);
358 usb_ep_free_request(fu
->ep_in
, fu
->bot_status
.req
);
360 usb_ep_free_request(fu
->ep_out
, fu
->cmd
.req
);
363 usb_ep_free_request(fu
->ep_out
, fu
->bot_req_out
);
364 fu
->bot_req_out
= NULL
;
366 usb_ep_free_request(fu
->ep_in
, fu
->bot_req_in
);
367 fu
->bot_req_in
= NULL
;
369 pr_err("BOT: endpoint setup failed\n");
373 static void bot_cleanup_old_alt(struct f_uas
*fu
)
375 if (!(fu
->flags
& USBG_ENABLED
))
378 usb_ep_disable(fu
->ep_in
);
379 usb_ep_disable(fu
->ep_out
);
384 usb_ep_free_request(fu
->ep_in
, fu
->bot_req_in
);
385 usb_ep_free_request(fu
->ep_out
, fu
->bot_req_out
);
386 usb_ep_free_request(fu
->ep_out
, fu
->cmd
.req
);
387 usb_ep_free_request(fu
->ep_out
, fu
->bot_status
.req
);
391 fu
->bot_req_in
= NULL
;
392 fu
->bot_req_out
= NULL
;
394 fu
->bot_status
.req
= NULL
;
398 static void bot_set_alt(struct f_uas
*fu
)
400 struct usb_function
*f
= &fu
->function
;
401 struct usb_gadget
*gadget
= f
->config
->cdev
->gadget
;
404 fu
->flags
= USBG_IS_BOT
;
406 config_ep_by_speed(gadget
, f
, fu
->ep_in
);
407 ret
= usb_ep_enable(fu
->ep_in
);
411 config_ep_by_speed(gadget
, f
, fu
->ep_out
);
412 ret
= usb_ep_enable(fu
->ep_out
);
416 ret
= bot_prepare_reqs(fu
);
419 fu
->flags
|= USBG_ENABLED
;
420 pr_info("Using the BOT protocol\n");
423 usb_ep_disable(fu
->ep_out
);
425 usb_ep_disable(fu
->ep_in
);
427 fu
->flags
= USBG_IS_BOT
;
430 static int usbg_bot_setup(struct usb_function
*f
,
431 const struct usb_ctrlrequest
*ctrl
)
433 struct f_uas
*fu
= to_f_uas(f
);
434 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
435 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
436 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
440 switch (ctrl
->bRequest
) {
441 case US_BULK_GET_MAX_LUN
:
442 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_TYPE_CLASS
|
443 USB_RECIP_INTERFACE
))
450 luns
= atomic_read(&fu
->tpg
->tpg_port_count
);
452 pr_err("No LUNs configured?\n");
456 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
457 * accessed. The upper limit is 0xf
461 pr_info_once("Limiting the number of luns to 16\n");
464 ret_lun
= cdev
->req
->buf
;
466 cdev
->req
->length
= 1;
467 return usb_ep_queue(cdev
->gadget
->ep0
, cdev
->req
, GFP_ATOMIC
);
470 case US_BULK_RESET_REQUEST
:
471 /* XXX maybe we should remove previous requests for IN + OUT */
472 bot_enqueue_cmd_cbw(fu
);
479 /* Start uas.c code */
481 static void uasp_cleanup_one_stream(struct f_uas
*fu
, struct uas_stream
*stream
)
483 /* We have either all three allocated or none */
487 usb_ep_free_request(fu
->ep_in
, stream
->req_in
);
488 usb_ep_free_request(fu
->ep_out
, stream
->req_out
);
489 usb_ep_free_request(fu
->ep_status
, stream
->req_status
);
491 stream
->req_in
= NULL
;
492 stream
->req_out
= NULL
;
493 stream
->req_status
= NULL
;
496 static void uasp_free_cmdreq(struct f_uas
*fu
)
498 usb_ep_free_request(fu
->ep_cmd
, fu
->cmd
.req
);
504 static void uasp_cleanup_old_alt(struct f_uas
*fu
)
508 if (!(fu
->flags
& USBG_ENABLED
))
511 usb_ep_disable(fu
->ep_in
);
512 usb_ep_disable(fu
->ep_out
);
513 usb_ep_disable(fu
->ep_status
);
514 usb_ep_disable(fu
->ep_cmd
);
516 for (i
= 0; i
< UASP_SS_EP_COMP_NUM_STREAMS
; i
++)
517 uasp_cleanup_one_stream(fu
, &fu
->stream
[i
]);
518 uasp_free_cmdreq(fu
);
521 static void uasp_status_data_cmpl(struct usb_ep
*ep
, struct usb_request
*req
);
523 static int uasp_prepare_r_request(struct usbg_cmd
*cmd
)
525 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
526 struct f_uas
*fu
= cmd
->fu
;
527 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
528 struct uas_stream
*stream
= cmd
->stream
;
530 if (!gadget
->sg_supported
) {
531 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
535 sg_copy_to_buffer(se_cmd
->t_data_sg
,
536 se_cmd
->t_data_nents
,
538 se_cmd
->data_length
);
540 stream
->req_in
->buf
= cmd
->data_buf
;
542 stream
->req_in
->buf
= NULL
;
543 stream
->req_in
->num_sgs
= se_cmd
->t_data_nents
;
544 stream
->req_in
->sg
= se_cmd
->t_data_sg
;
547 stream
->req_in
->complete
= uasp_status_data_cmpl
;
548 stream
->req_in
->length
= se_cmd
->data_length
;
549 stream
->req_in
->context
= cmd
;
551 cmd
->state
= UASP_SEND_STATUS
;
555 static void uasp_prepare_status(struct usbg_cmd
*cmd
)
557 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
558 struct sense_iu
*iu
= &cmd
->sense_iu
;
559 struct uas_stream
*stream
= cmd
->stream
;
561 cmd
->state
= UASP_QUEUE_COMMAND
;
562 iu
->iu_id
= IU_ID_STATUS
;
563 iu
->tag
= cpu_to_be16(cmd
->tag
);
566 * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
568 iu
->len
= cpu_to_be16(se_cmd
->scsi_sense_length
);
569 iu
->status
= se_cmd
->scsi_status
;
570 stream
->req_status
->context
= cmd
;
571 stream
->req_status
->length
= se_cmd
->scsi_sense_length
+ 16;
572 stream
->req_status
->buf
= iu
;
573 stream
->req_status
->complete
= uasp_status_data_cmpl
;
576 static void uasp_status_data_cmpl(struct usb_ep
*ep
, struct usb_request
*req
)
578 struct usbg_cmd
*cmd
= req
->context
;
579 struct uas_stream
*stream
= cmd
->stream
;
580 struct f_uas
*fu
= cmd
->fu
;
586 switch (cmd
->state
) {
588 ret
= uasp_prepare_r_request(cmd
);
591 ret
= usb_ep_queue(fu
->ep_in
, stream
->req_in
, GFP_ATOMIC
);
593 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
596 case UASP_RECEIVE_DATA
:
597 ret
= usbg_prepare_w_request(cmd
, stream
->req_out
);
600 ret
= usb_ep_queue(fu
->ep_out
, stream
->req_out
, GFP_ATOMIC
);
602 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
605 case UASP_SEND_STATUS
:
606 uasp_prepare_status(cmd
);
607 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
610 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
613 case UASP_QUEUE_COMMAND
:
614 usbg_cleanup_cmd(cmd
);
615 usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
624 usbg_cleanup_cmd(cmd
);
627 static int uasp_send_status_response(struct usbg_cmd
*cmd
)
629 struct f_uas
*fu
= cmd
->fu
;
630 struct uas_stream
*stream
= cmd
->stream
;
631 struct sense_iu
*iu
= &cmd
->sense_iu
;
633 iu
->tag
= cpu_to_be16(cmd
->tag
);
634 stream
->req_status
->complete
= uasp_status_data_cmpl
;
635 stream
->req_status
->context
= cmd
;
637 uasp_prepare_status(cmd
);
638 return usb_ep_queue(fu
->ep_status
, stream
->req_status
, GFP_ATOMIC
);
641 static int uasp_send_read_response(struct usbg_cmd
*cmd
)
643 struct f_uas
*fu
= cmd
->fu
;
644 struct uas_stream
*stream
= cmd
->stream
;
645 struct sense_iu
*iu
= &cmd
->sense_iu
;
650 iu
->tag
= cpu_to_be16(cmd
->tag
);
651 if (fu
->flags
& USBG_USE_STREAMS
) {
653 ret
= uasp_prepare_r_request(cmd
);
656 ret
= usb_ep_queue(fu
->ep_in
, stream
->req_in
, GFP_ATOMIC
);
658 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
659 kfree(cmd
->data_buf
);
660 cmd
->data_buf
= NULL
;
665 iu
->iu_id
= IU_ID_READ_READY
;
666 iu
->tag
= cpu_to_be16(cmd
->tag
);
668 stream
->req_status
->complete
= uasp_status_data_cmpl
;
669 stream
->req_status
->context
= cmd
;
671 cmd
->state
= UASP_SEND_DATA
;
672 stream
->req_status
->buf
= iu
;
673 stream
->req_status
->length
= sizeof(struct iu
);
675 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
678 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
684 static int uasp_send_write_request(struct usbg_cmd
*cmd
)
686 struct f_uas
*fu
= cmd
->fu
;
687 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
688 struct uas_stream
*stream
= cmd
->stream
;
689 struct sense_iu
*iu
= &cmd
->sense_iu
;
692 init_completion(&cmd
->write_complete
);
695 iu
->tag
= cpu_to_be16(cmd
->tag
);
697 if (fu
->flags
& USBG_USE_STREAMS
) {
699 ret
= usbg_prepare_w_request(cmd
, stream
->req_out
);
702 ret
= usb_ep_queue(fu
->ep_out
, stream
->req_out
, GFP_ATOMIC
);
704 pr_err("%s(%d)\n", __func__
, __LINE__
);
708 iu
->iu_id
= IU_ID_WRITE_READY
;
709 iu
->tag
= cpu_to_be16(cmd
->tag
);
711 stream
->req_status
->complete
= uasp_status_data_cmpl
;
712 stream
->req_status
->context
= cmd
;
714 cmd
->state
= UASP_RECEIVE_DATA
;
715 stream
->req_status
->buf
= iu
;
716 stream
->req_status
->length
= sizeof(struct iu
);
718 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
721 pr_err("%s(%d)\n", __func__
, __LINE__
);
724 wait_for_completion(&cmd
->write_complete
);
725 target_execute_cmd(se_cmd
);
730 static int usbg_submit_command(struct f_uas
*, void *, unsigned int);
732 static void uasp_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
734 struct f_uas
*fu
= req
->context
;
740 ret
= usbg_submit_command(fu
, req
->buf
, req
->actual
);
742 * Once we tune for performance enqueue the command req here again so
743 * we can receive a second command while we processing this one. Pay
744 * attention to properly sync STAUS endpoint with DATA IN + OUT so you
749 usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
752 static int uasp_alloc_stream_res(struct f_uas
*fu
, struct uas_stream
*stream
)
754 stream
->req_in
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
758 stream
->req_out
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
759 if (!stream
->req_out
)
762 stream
->req_status
= usb_ep_alloc_request(fu
->ep_status
, GFP_KERNEL
);
763 if (!stream
->req_status
)
768 usb_ep_free_request(fu
->ep_status
, stream
->req_status
);
769 stream
->req_status
= NULL
;
771 usb_ep_free_request(fu
->ep_out
, stream
->req_out
);
772 stream
->req_out
= NULL
;
777 static int uasp_alloc_cmd(struct f_uas
*fu
)
779 fu
->cmd
.req
= usb_ep_alloc_request(fu
->ep_cmd
, GFP_KERNEL
);
783 fu
->cmd
.buf
= kmalloc(fu
->ep_cmd
->maxpacket
, GFP_KERNEL
);
787 fu
->cmd
.req
->complete
= uasp_cmd_complete
;
788 fu
->cmd
.req
->buf
= fu
->cmd
.buf
;
789 fu
->cmd
.req
->length
= fu
->ep_cmd
->maxpacket
;
790 fu
->cmd
.req
->context
= fu
;
794 usb_ep_free_request(fu
->ep_cmd
, fu
->cmd
.req
);
799 static void uasp_setup_stream_res(struct f_uas
*fu
, int max_streams
)
803 for (i
= 0; i
< max_streams
; i
++) {
804 struct uas_stream
*s
= &fu
->stream
[i
];
806 s
->req_in
->stream_id
= i
+ 1;
807 s
->req_out
->stream_id
= i
+ 1;
808 s
->req_status
->stream_id
= i
+ 1;
812 static int uasp_prepare_reqs(struct f_uas
*fu
)
818 if (fu
->flags
& USBG_USE_STREAMS
)
819 max_streams
= UASP_SS_EP_COMP_NUM_STREAMS
;
823 for (i
= 0; i
< max_streams
; i
++) {
824 ret
= uasp_alloc_stream_res(fu
, &fu
->stream
[i
]);
829 ret
= uasp_alloc_cmd(fu
);
831 goto err_free_stream
;
832 uasp_setup_stream_res(fu
, max_streams
);
834 ret
= usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
836 goto err_free_stream
;
841 uasp_free_cmdreq(fu
);
846 uasp_cleanup_one_stream(fu
, &fu
->stream
[i
- 1]);
850 pr_err("UASP: endpoint setup failed\n");
854 static void uasp_set_alt(struct f_uas
*fu
)
856 struct usb_function
*f
= &fu
->function
;
857 struct usb_gadget
*gadget
= f
->config
->cdev
->gadget
;
860 fu
->flags
= USBG_IS_UAS
;
862 if (gadget
->speed
== USB_SPEED_SUPER
)
863 fu
->flags
|= USBG_USE_STREAMS
;
865 config_ep_by_speed(gadget
, f
, fu
->ep_in
);
866 ret
= usb_ep_enable(fu
->ep_in
);
870 config_ep_by_speed(gadget
, f
, fu
->ep_out
);
871 ret
= usb_ep_enable(fu
->ep_out
);
875 config_ep_by_speed(gadget
, f
, fu
->ep_cmd
);
876 ret
= usb_ep_enable(fu
->ep_cmd
);
879 config_ep_by_speed(gadget
, f
, fu
->ep_status
);
880 ret
= usb_ep_enable(fu
->ep_status
);
884 ret
= uasp_prepare_reqs(fu
);
887 fu
->flags
|= USBG_ENABLED
;
889 pr_info("Using the UAS protocol\n");
892 usb_ep_disable(fu
->ep_status
);
894 usb_ep_disable(fu
->ep_cmd
);
896 usb_ep_disable(fu
->ep_out
);
898 usb_ep_disable(fu
->ep_in
);
903 static int get_cmd_dir(const unsigned char *cdb
)
915 case SERVICE_ACTION_IN
:
917 case PERSISTENT_RESERVE_IN
:
918 case SECURITY_PROTOCOL_IN
:
919 case ACCESS_CONTROL_IN
:
921 case READ_BLOCK_LIMITS
:
925 case READ_FORMAT_CAPACITIES
:
927 ret
= DMA_FROM_DEVICE
;
937 case WRITE_VERIFY_12
:
938 case PERSISTENT_RESERVE_OUT
:
939 case MAINTENANCE_OUT
:
940 case SECURITY_PROTOCOL_OUT
:
941 case ACCESS_CONTROL_OUT
:
944 case ALLOW_MEDIUM_REMOVAL
:
945 case TEST_UNIT_READY
:
946 case SYNCHRONIZE_CACHE
:
953 case WRITE_FILEMARKS
:
957 pr_warn("target: Unknown data direction for SCSI Opcode "
964 static void usbg_data_write_cmpl(struct usb_ep
*ep
, struct usb_request
*req
)
966 struct usbg_cmd
*cmd
= req
->context
;
967 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
969 if (req
->status
< 0) {
970 pr_err("%s() state %d transfer failed\n", __func__
, cmd
->state
);
974 if (req
->num_sgs
== 0) {
975 sg_copy_from_buffer(se_cmd
->t_data_sg
,
976 se_cmd
->t_data_nents
,
978 se_cmd
->data_length
);
981 complete(&cmd
->write_complete
);
985 usbg_cleanup_cmd(cmd
);
988 static int usbg_prepare_w_request(struct usbg_cmd
*cmd
, struct usb_request
*req
)
990 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
991 struct f_uas
*fu
= cmd
->fu
;
992 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
994 if (!gadget
->sg_supported
) {
995 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
999 req
->buf
= cmd
->data_buf
;
1002 req
->num_sgs
= se_cmd
->t_data_nents
;
1003 req
->sg
= se_cmd
->t_data_sg
;
1006 req
->complete
= usbg_data_write_cmpl
;
1007 req
->length
= se_cmd
->data_length
;
1012 static int usbg_send_status_response(struct se_cmd
*se_cmd
)
1014 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1016 struct f_uas
*fu
= cmd
->fu
;
1018 if (fu
->flags
& USBG_IS_BOT
)
1019 return bot_send_status_response(cmd
);
1021 return uasp_send_status_response(cmd
);
1024 static int usbg_send_write_request(struct se_cmd
*se_cmd
)
1026 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1028 struct f_uas
*fu
= cmd
->fu
;
1030 if (fu
->flags
& USBG_IS_BOT
)
1031 return bot_send_write_request(cmd
);
1033 return uasp_send_write_request(cmd
);
1036 static int usbg_send_read_response(struct se_cmd
*se_cmd
)
1038 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1040 struct f_uas
*fu
= cmd
->fu
;
1042 if (fu
->flags
& USBG_IS_BOT
)
1043 return bot_send_read_response(cmd
);
1045 return uasp_send_read_response(cmd
);
1048 static void usbg_cmd_work(struct work_struct
*work
)
1050 struct usbg_cmd
*cmd
= container_of(work
, struct usbg_cmd
, work
);
1051 struct se_cmd
*se_cmd
;
1052 struct tcm_usbg_nexus
*tv_nexus
;
1053 struct usbg_tpg
*tpg
;
1056 se_cmd
= &cmd
->se_cmd
;
1058 tv_nexus
= tpg
->tpg_nexus
;
1059 dir
= get_cmd_dir(cmd
->cmd_buf
);
1061 transport_init_se_cmd(se_cmd
,
1062 tv_nexus
->tvn_se_sess
->se_tpg
->se_tpg_tfo
,
1063 tv_nexus
->tvn_se_sess
, cmd
->data_len
, DMA_NONE
,
1064 cmd
->prio_attr
, cmd
->sense_iu
.sense
);
1068 if (target_submit_cmd(se_cmd
, tv_nexus
->tvn_se_sess
,
1069 cmd
->cmd_buf
, cmd
->sense_iu
.sense
, cmd
->unpacked_lun
,
1070 0, cmd
->prio_attr
, dir
, TARGET_SCF_UNKNOWN_SIZE
) < 0)
1076 transport_send_check_condition_and_sense(se_cmd
,
1077 TCM_UNSUPPORTED_SCSI_OPCODE
, 1);
1078 usbg_cleanup_cmd(cmd
);
1081 static int usbg_submit_command(struct f_uas
*fu
,
1082 void *cmdbuf
, unsigned int len
)
1084 struct command_iu
*cmd_iu
= cmdbuf
;
1085 struct usbg_cmd
*cmd
;
1086 struct usbg_tpg
*tpg
;
1087 struct se_cmd
*se_cmd
;
1088 struct tcm_usbg_nexus
*tv_nexus
;
1092 if (cmd_iu
->iu_id
!= IU_ID_COMMAND
) {
1093 pr_err("Unsupported type %d\n", cmd_iu
->iu_id
);
1097 cmd
= kzalloc(sizeof *cmd
, GFP_ATOMIC
);
1103 /* XXX until I figure out why I can't free in on complete */
1104 kref_init(&cmd
->ref
);
1105 kref_get(&cmd
->ref
);
1108 cmd_len
= (cmd_iu
->len
& ~0x3) + 16;
1109 if (cmd_len
> USBG_MAX_CMD
)
1112 memcpy(cmd
->cmd_buf
, cmd_iu
->cdb
, cmd_len
);
1114 cmd
->tag
= be16_to_cpup(&cmd_iu
->tag
);
1115 if (fu
->flags
& USBG_USE_STREAMS
) {
1116 if (cmd
->tag
> UASP_SS_EP_COMP_NUM_STREAMS
)
1119 cmd
->stream
= &fu
->stream
[0];
1121 cmd
->stream
= &fu
->stream
[cmd
->tag
- 1];
1123 cmd
->stream
= &fu
->stream
[0];
1126 tv_nexus
= tpg
->tpg_nexus
;
1128 pr_err("Missing nexus, ignoring command\n");
1132 switch (cmd_iu
->prio_attr
& 0x7) {
1134 cmd
->prio_attr
= MSG_HEAD_TAG
;
1136 case UAS_ORDERED_TAG
:
1137 cmd
->prio_attr
= MSG_ORDERED_TAG
;
1140 cmd
->prio_attr
= MSG_ACA_TAG
;
1143 pr_debug_once("Unsupported prio_attr: %02x.\n",
1145 case UAS_SIMPLE_TAG
:
1146 cmd
->prio_attr
= MSG_SIMPLE_TAG
;
1150 se_cmd
= &cmd
->se_cmd
;
1151 cmd
->unpacked_lun
= scsilun_to_int(&cmd_iu
->lun
);
1153 INIT_WORK(&cmd
->work
, usbg_cmd_work
);
1154 ret
= queue_work(tpg
->workqueue
, &cmd
->work
);
1164 static void bot_cmd_work(struct work_struct
*work
)
1166 struct usbg_cmd
*cmd
= container_of(work
, struct usbg_cmd
, work
);
1167 struct se_cmd
*se_cmd
;
1168 struct tcm_usbg_nexus
*tv_nexus
;
1169 struct usbg_tpg
*tpg
;
1172 se_cmd
= &cmd
->se_cmd
;
1174 tv_nexus
= tpg
->tpg_nexus
;
1175 dir
= get_cmd_dir(cmd
->cmd_buf
);
1177 transport_init_se_cmd(se_cmd
,
1178 tv_nexus
->tvn_se_sess
->se_tpg
->se_tpg_tfo
,
1179 tv_nexus
->tvn_se_sess
, cmd
->data_len
, DMA_NONE
,
1180 cmd
->prio_attr
, cmd
->sense_iu
.sense
);
1184 if (target_submit_cmd(se_cmd
, tv_nexus
->tvn_se_sess
,
1185 cmd
->cmd_buf
, cmd
->sense_iu
.sense
, cmd
->unpacked_lun
,
1186 cmd
->data_len
, cmd
->prio_attr
, dir
, 0) < 0)
1192 transport_send_check_condition_and_sense(se_cmd
,
1193 TCM_UNSUPPORTED_SCSI_OPCODE
, 1);
1194 usbg_cleanup_cmd(cmd
);
1197 static int bot_submit_command(struct f_uas
*fu
,
1198 void *cmdbuf
, unsigned int len
)
1200 struct bulk_cb_wrap
*cbw
= cmdbuf
;
1201 struct usbg_cmd
*cmd
;
1202 struct usbg_tpg
*tpg
;
1203 struct se_cmd
*se_cmd
;
1204 struct tcm_usbg_nexus
*tv_nexus
;
1208 if (cbw
->Signature
!= cpu_to_le32(US_BULK_CB_SIGN
)) {
1209 pr_err("Wrong signature on CBW\n");
1213 pr_err("Wrong length for CBW\n");
1217 cmd_len
= cbw
->Length
;
1218 if (cmd_len
< 1 || cmd_len
> 16)
1221 cmd
= kzalloc(sizeof *cmd
, GFP_ATOMIC
);
1227 /* XXX until I figure out why I can't free in on complete */
1228 kref_init(&cmd
->ref
);
1229 kref_get(&cmd
->ref
);
1233 memcpy(cmd
->cmd_buf
, cbw
->CDB
, cmd_len
);
1235 cmd
->bot_tag
= cbw
->Tag
;
1237 tv_nexus
= tpg
->tpg_nexus
;
1239 pr_err("Missing nexus, ignoring command\n");
1243 cmd
->prio_attr
= MSG_SIMPLE_TAG
;
1244 se_cmd
= &cmd
->se_cmd
;
1245 cmd
->unpacked_lun
= cbw
->Lun
;
1246 cmd
->is_read
= cbw
->Flags
& US_BULK_FLAG_IN
? 1 : 0;
1247 cmd
->data_len
= le32_to_cpu(cbw
->DataTransferLength
);
1249 INIT_WORK(&cmd
->work
, bot_cmd_work
);
1250 ret
= queue_work(tpg
->workqueue
, &cmd
->work
);
1260 /* Start fabric.c code */
1262 static int usbg_check_true(struct se_portal_group
*se_tpg
)
1267 static int usbg_check_false(struct se_portal_group
*se_tpg
)
1272 static char *usbg_get_fabric_name(void)
1274 return "usb_gadget";
1277 static u8
usbg_get_fabric_proto_ident(struct se_portal_group
*se_tpg
)
1279 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1280 struct usbg_tpg
, se_tpg
);
1281 struct usbg_tport
*tport
= tpg
->tport
;
1284 switch (tport
->tport_proto_id
) {
1285 case SCSI_PROTOCOL_SAS
:
1287 proto_id
= sas_get_fabric_proto_ident(se_tpg
);
1294 static char *usbg_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1296 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1297 struct usbg_tpg
, se_tpg
);
1298 struct usbg_tport
*tport
= tpg
->tport
;
1300 return &tport
->tport_name
[0];
1303 static u16
usbg_get_tag(struct se_portal_group
*se_tpg
)
1305 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1306 struct usbg_tpg
, se_tpg
);
1307 return tpg
->tport_tpgt
;
1310 static u32
usbg_get_default_depth(struct se_portal_group
*se_tpg
)
1315 static u32
usbg_get_pr_transport_id(
1316 struct se_portal_group
*se_tpg
,
1317 struct se_node_acl
*se_nacl
,
1318 struct t10_pr_registration
*pr_reg
,
1322 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1323 struct usbg_tpg
, se_tpg
);
1324 struct usbg_tport
*tport
= tpg
->tport
;
1327 switch (tport
->tport_proto_id
) {
1328 case SCSI_PROTOCOL_SAS
:
1330 ret
= sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
1338 static u32
usbg_get_pr_transport_id_len(
1339 struct se_portal_group
*se_tpg
,
1340 struct se_node_acl
*se_nacl
,
1341 struct t10_pr_registration
*pr_reg
,
1344 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1345 struct usbg_tpg
, se_tpg
);
1346 struct usbg_tport
*tport
= tpg
->tport
;
1349 switch (tport
->tport_proto_id
) {
1350 case SCSI_PROTOCOL_SAS
:
1352 ret
= sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
1360 static char *usbg_parse_pr_out_transport_id(
1361 struct se_portal_group
*se_tpg
,
1364 char **port_nexus_ptr
)
1366 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1367 struct usbg_tpg
, se_tpg
);
1368 struct usbg_tport
*tport
= tpg
->tport
;
1371 switch (tport
->tport_proto_id
) {
1372 case SCSI_PROTOCOL_SAS
:
1374 tid
= sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
1381 static struct se_node_acl
*usbg_alloc_fabric_acl(struct se_portal_group
*se_tpg
)
1383 struct usbg_nacl
*nacl
;
1385 nacl
= kzalloc(sizeof(struct usbg_nacl
), GFP_KERNEL
);
1389 return &nacl
->se_node_acl
;
1392 static void usbg_release_fabric_acl(
1393 struct se_portal_group
*se_tpg
,
1394 struct se_node_acl
*se_nacl
)
1396 struct usbg_nacl
*nacl
= container_of(se_nacl
,
1397 struct usbg_nacl
, se_node_acl
);
1401 static u32
usbg_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
1406 static void usbg_cmd_release(struct kref
*ref
)
1408 struct usbg_cmd
*cmd
= container_of(ref
, struct usbg_cmd
,
1411 transport_generic_free_cmd(&cmd
->se_cmd
, 0);
1414 static void usbg_release_cmd(struct se_cmd
*se_cmd
)
1416 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1418 kfree(cmd
->data_buf
);
1423 static int usbg_shutdown_session(struct se_session
*se_sess
)
1428 static void usbg_close_session(struct se_session
*se_sess
)
1433 static u32
usbg_sess_get_index(struct se_session
*se_sess
)
1439 * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
1441 static int usbg_write_pending_status(struct se_cmd
*se_cmd
)
1446 static void usbg_set_default_node_attrs(struct se_node_acl
*nacl
)
1451 static u32
usbg_get_task_tag(struct se_cmd
*se_cmd
)
1453 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1455 struct f_uas
*fu
= cmd
->fu
;
1457 if (fu
->flags
& USBG_IS_BOT
)
1458 return le32_to_cpu(cmd
->bot_tag
);
1463 static int usbg_get_cmd_state(struct se_cmd
*se_cmd
)
1468 static void usbg_queue_tm_rsp(struct se_cmd
*se_cmd
)
1472 static void usbg_aborted_task(struct se_cmd
*se_cmd
)
1477 static const char *usbg_check_wwn(const char *name
)
1482 n
= strstr(name
, "naa.");
1487 if (len
== 0 || len
> USBG_NAMELEN
- 1)
1492 static struct se_node_acl
*usbg_make_nodeacl(
1493 struct se_portal_group
*se_tpg
,
1494 struct config_group
*group
,
1497 struct se_node_acl
*se_nacl
, *se_nacl_new
;
1498 struct usbg_nacl
*nacl
;
1501 const char *wnn_name
;
1503 wnn_name
= usbg_check_wwn(name
);
1505 return ERR_PTR(-EINVAL
);
1506 se_nacl_new
= usbg_alloc_fabric_acl(se_tpg
);
1508 return ERR_PTR(-ENOMEM
);
1512 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1513 * when converting a NodeACL from demo mode -> explict
1515 se_nacl
= core_tpg_add_initiator_node_acl(se_tpg
, se_nacl_new
,
1517 if (IS_ERR(se_nacl
)) {
1518 usbg_release_fabric_acl(se_tpg
, se_nacl_new
);
1522 * Locate our struct usbg_nacl and set the FC Nport WWPN
1524 nacl
= container_of(se_nacl
, struct usbg_nacl
, se_node_acl
);
1525 nacl
->iport_wwpn
= wwpn
;
1526 snprintf(nacl
->iport_name
, sizeof(nacl
->iport_name
), "%s", name
);
1530 static void usbg_drop_nodeacl(struct se_node_acl
*se_acl
)
1532 struct usbg_nacl
*nacl
= container_of(se_acl
,
1533 struct usbg_nacl
, se_node_acl
);
1534 core_tpg_del_initiator_node_acl(se_acl
->se_tpg
, se_acl
, 1);
1538 struct usbg_tpg
*the_only_tpg_I_currently_have
;
1540 static struct se_portal_group
*usbg_make_tpg(
1542 struct config_group
*group
,
1545 struct usbg_tport
*tport
= container_of(wwn
, struct usbg_tport
,
1547 struct usbg_tpg
*tpg
;
1551 if (strstr(name
, "tpgt_") != name
)
1552 return ERR_PTR(-EINVAL
);
1553 if (kstrtoul(name
+ 5, 0, &tpgt
) || tpgt
> UINT_MAX
)
1554 return ERR_PTR(-EINVAL
);
1555 if (the_only_tpg_I_currently_have
) {
1556 pr_err("Until the gadget framework can't handle multiple\n");
1557 pr_err("gadgets, you can't do this here.\n");
1558 return ERR_PTR(-EBUSY
);
1561 tpg
= kzalloc(sizeof(struct usbg_tpg
), GFP_KERNEL
);
1563 return ERR_PTR(-ENOMEM
);
1564 mutex_init(&tpg
->tpg_mutex
);
1565 atomic_set(&tpg
->tpg_port_count
, 0);
1566 tpg
->workqueue
= alloc_workqueue("tcm_usb_gadget", 0, 1);
1567 if (!tpg
->workqueue
) {
1573 tpg
->tport_tpgt
= tpgt
;
1575 ret
= core_tpg_register(&usbg_fabric_configfs
->tf_ops
, wwn
,
1577 TRANSPORT_TPG_TYPE_NORMAL
);
1579 destroy_workqueue(tpg
->workqueue
);
1583 the_only_tpg_I_currently_have
= tpg
;
1584 return &tpg
->se_tpg
;
1587 static void usbg_drop_tpg(struct se_portal_group
*se_tpg
)
1589 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1590 struct usbg_tpg
, se_tpg
);
1592 core_tpg_deregister(se_tpg
);
1593 destroy_workqueue(tpg
->workqueue
);
1595 the_only_tpg_I_currently_have
= NULL
;
1598 static struct se_wwn
*usbg_make_tport(
1599 struct target_fabric_configfs
*tf
,
1600 struct config_group
*group
,
1603 struct usbg_tport
*tport
;
1604 const char *wnn_name
;
1607 wnn_name
= usbg_check_wwn(name
);
1609 return ERR_PTR(-EINVAL
);
1611 tport
= kzalloc(sizeof(struct usbg_tport
), GFP_KERNEL
);
1613 return ERR_PTR(-ENOMEM
);
1614 tport
->tport_wwpn
= wwpn
;
1615 snprintf(tport
->tport_name
, sizeof(tport
->tport_name
), "%s", wnn_name
);
1616 return &tport
->tport_wwn
;
1619 static void usbg_drop_tport(struct se_wwn
*wwn
)
1621 struct usbg_tport
*tport
= container_of(wwn
,
1622 struct usbg_tport
, tport_wwn
);
1627 * If somebody feels like dropping the version property, go ahead.
1629 static ssize_t
usbg_wwn_show_attr_version(
1630 struct target_fabric_configfs
*tf
,
1633 return sprintf(page
, "usb-gadget fabric module\n");
1635 TF_WWN_ATTR_RO(usbg
, version
);
1637 static struct configfs_attribute
*usbg_wwn_attrs
[] = {
1638 &usbg_wwn_version
.attr
,
1642 static ssize_t
tcm_usbg_tpg_show_enable(
1643 struct se_portal_group
*se_tpg
,
1646 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1648 return snprintf(page
, PAGE_SIZE
, "%u\n", tpg
->gadget_connect
);
1651 static int usbg_attach(struct usbg_tpg
*);
1652 static void usbg_detach(struct usbg_tpg
*);
1654 static ssize_t
tcm_usbg_tpg_store_enable(
1655 struct se_portal_group
*se_tpg
,
1659 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1663 ret
= kstrtoul(page
, 0, &op
);
1669 if (op
&& tpg
->gadget_connect
)
1671 if (!op
&& !tpg
->gadget_connect
)
1675 ret
= usbg_attach(tpg
);
1681 tpg
->gadget_connect
= op
;
1685 TF_TPG_BASE_ATTR(tcm_usbg
, enable
, S_IRUGO
| S_IWUSR
);
1687 static ssize_t
tcm_usbg_tpg_show_nexus(
1688 struct se_portal_group
*se_tpg
,
1691 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1692 struct tcm_usbg_nexus
*tv_nexus
;
1695 mutex_lock(&tpg
->tpg_mutex
);
1696 tv_nexus
= tpg
->tpg_nexus
;
1701 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1702 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1704 mutex_unlock(&tpg
->tpg_mutex
);
1708 static int tcm_usbg_make_nexus(struct usbg_tpg
*tpg
, char *name
)
1710 struct se_portal_group
*se_tpg
;
1711 struct tcm_usbg_nexus
*tv_nexus
;
1714 mutex_lock(&tpg
->tpg_mutex
);
1715 if (tpg
->tpg_nexus
) {
1717 pr_debug("tpg->tpg_nexus already exists\n");
1720 se_tpg
= &tpg
->se_tpg
;
1723 tv_nexus
= kzalloc(sizeof(*tv_nexus
), GFP_KERNEL
);
1726 tv_nexus
->tvn_se_sess
= transport_init_session(TARGET_PROT_NORMAL
);
1727 if (IS_ERR(tv_nexus
->tvn_se_sess
))
1731 * Since we are running in 'demo mode' this call with generate a
1732 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1733 * the SCSI Initiator port name of the passed configfs group 'name'.
1735 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1737 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1738 pr_debug("core_tpg_check_initiator_node_acl() failed"
1743 * Now register the TCM vHost virtual I_T Nexus as active with the
1744 * call to __transport_register_session()
1746 __transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1747 tv_nexus
->tvn_se_sess
, tv_nexus
);
1748 tpg
->tpg_nexus
= tv_nexus
;
1749 mutex_unlock(&tpg
->tpg_mutex
);
1753 transport_free_session(tv_nexus
->tvn_se_sess
);
1757 mutex_unlock(&tpg
->tpg_mutex
);
1761 static int tcm_usbg_drop_nexus(struct usbg_tpg
*tpg
)
1763 struct se_session
*se_sess
;
1764 struct tcm_usbg_nexus
*tv_nexus
;
1767 mutex_lock(&tpg
->tpg_mutex
);
1768 tv_nexus
= tpg
->tpg_nexus
;
1772 se_sess
= tv_nexus
->tvn_se_sess
;
1776 if (atomic_read(&tpg
->tpg_port_count
)) {
1778 pr_err("Unable to remove Host I_T Nexus with"
1779 " active TPG port count: %d\n",
1780 atomic_read(&tpg
->tpg_port_count
));
1784 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1785 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1787 * Release the SCSI I_T Nexus to the emulated vHost Target Port
1789 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1790 tpg
->tpg_nexus
= NULL
;
1795 mutex_unlock(&tpg
->tpg_mutex
);
1799 static ssize_t
tcm_usbg_tpg_store_nexus(
1800 struct se_portal_group
*se_tpg
,
1804 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1805 unsigned char i_port
[USBG_NAMELEN
], *ptr
;
1808 if (!strncmp(page
, "NULL", 4)) {
1809 ret
= tcm_usbg_drop_nexus(tpg
);
1810 return (!ret
) ? count
: ret
;
1812 if (strlen(page
) >= USBG_NAMELEN
) {
1813 pr_err("Emulated NAA Sas Address: %s, exceeds"
1814 " max: %d\n", page
, USBG_NAMELEN
);
1817 snprintf(i_port
, USBG_NAMELEN
, "%s", page
);
1819 ptr
= strstr(i_port
, "naa.");
1821 pr_err("Missing 'naa.' prefix\n");
1825 if (i_port
[strlen(i_port
) - 1] == '\n')
1826 i_port
[strlen(i_port
) - 1] = '\0';
1828 ret
= tcm_usbg_make_nexus(tpg
, &i_port
[4]);
1833 TF_TPG_BASE_ATTR(tcm_usbg
, nexus
, S_IRUGO
| S_IWUSR
);
1835 static struct configfs_attribute
*usbg_base_attrs
[] = {
1836 &tcm_usbg_tpg_enable
.attr
,
1837 &tcm_usbg_tpg_nexus
.attr
,
1841 static int usbg_port_link(struct se_portal_group
*se_tpg
, struct se_lun
*lun
)
1843 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1845 atomic_inc(&tpg
->tpg_port_count
);
1846 smp_mb__after_atomic_inc();
1850 static void usbg_port_unlink(struct se_portal_group
*se_tpg
,
1851 struct se_lun
*se_lun
)
1853 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1855 atomic_dec(&tpg
->tpg_port_count
);
1856 smp_mb__after_atomic_dec();
1859 static int usbg_check_stop_free(struct se_cmd
*se_cmd
)
1861 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1864 kref_put(&cmd
->ref
, usbg_cmd_release
);
1868 static struct target_core_fabric_ops usbg_ops
= {
1869 .get_fabric_name
= usbg_get_fabric_name
,
1870 .get_fabric_proto_ident
= usbg_get_fabric_proto_ident
,
1871 .tpg_get_wwn
= usbg_get_fabric_wwn
,
1872 .tpg_get_tag
= usbg_get_tag
,
1873 .tpg_get_default_depth
= usbg_get_default_depth
,
1874 .tpg_get_pr_transport_id
= usbg_get_pr_transport_id
,
1875 .tpg_get_pr_transport_id_len
= usbg_get_pr_transport_id_len
,
1876 .tpg_parse_pr_out_transport_id
= usbg_parse_pr_out_transport_id
,
1877 .tpg_check_demo_mode
= usbg_check_true
,
1878 .tpg_check_demo_mode_cache
= usbg_check_false
,
1879 .tpg_check_demo_mode_write_protect
= usbg_check_false
,
1880 .tpg_check_prod_mode_write_protect
= usbg_check_false
,
1881 .tpg_alloc_fabric_acl
= usbg_alloc_fabric_acl
,
1882 .tpg_release_fabric_acl
= usbg_release_fabric_acl
,
1883 .tpg_get_inst_index
= usbg_tpg_get_inst_index
,
1884 .release_cmd
= usbg_release_cmd
,
1885 .shutdown_session
= usbg_shutdown_session
,
1886 .close_session
= usbg_close_session
,
1887 .sess_get_index
= usbg_sess_get_index
,
1888 .sess_get_initiator_sid
= NULL
,
1889 .write_pending
= usbg_send_write_request
,
1890 .write_pending_status
= usbg_write_pending_status
,
1891 .set_default_node_attributes
= usbg_set_default_node_attrs
,
1892 .get_task_tag
= usbg_get_task_tag
,
1893 .get_cmd_state
= usbg_get_cmd_state
,
1894 .queue_data_in
= usbg_send_read_response
,
1895 .queue_status
= usbg_send_status_response
,
1896 .queue_tm_rsp
= usbg_queue_tm_rsp
,
1897 .aborted_task
= usbg_aborted_task
,
1898 .check_stop_free
= usbg_check_stop_free
,
1900 .fabric_make_wwn
= usbg_make_tport
,
1901 .fabric_drop_wwn
= usbg_drop_tport
,
1902 .fabric_make_tpg
= usbg_make_tpg
,
1903 .fabric_drop_tpg
= usbg_drop_tpg
,
1904 .fabric_post_link
= usbg_port_link
,
1905 .fabric_pre_unlink
= usbg_port_unlink
,
1906 .fabric_make_np
= NULL
,
1907 .fabric_drop_np
= NULL
,
1908 .fabric_make_nodeacl
= usbg_make_nodeacl
,
1909 .fabric_drop_nodeacl
= usbg_drop_nodeacl
,
1912 static int usbg_register_configfs(void)
1914 struct target_fabric_configfs
*fabric
;
1917 fabric
= target_fabric_configfs_init(THIS_MODULE
, "usb_gadget");
1918 if (IS_ERR(fabric
)) {
1919 printk(KERN_ERR
"target_fabric_configfs_init() failed\n");
1920 return PTR_ERR(fabric
);
1923 fabric
->tf_ops
= usbg_ops
;
1924 fabric
->tf_cit_tmpl
.tfc_wwn_cit
.ct_attrs
= usbg_wwn_attrs
;
1925 fabric
->tf_cit_tmpl
.tfc_tpg_base_cit
.ct_attrs
= usbg_base_attrs
;
1926 fabric
->tf_cit_tmpl
.tfc_tpg_attrib_cit
.ct_attrs
= NULL
;
1927 fabric
->tf_cit_tmpl
.tfc_tpg_param_cit
.ct_attrs
= NULL
;
1928 fabric
->tf_cit_tmpl
.tfc_tpg_np_base_cit
.ct_attrs
= NULL
;
1929 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_base_cit
.ct_attrs
= NULL
;
1930 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_attrib_cit
.ct_attrs
= NULL
;
1931 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_auth_cit
.ct_attrs
= NULL
;
1932 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_param_cit
.ct_attrs
= NULL
;
1933 ret
= target_fabric_configfs_register(fabric
);
1935 printk(KERN_ERR
"target_fabric_configfs_register() failed"
1936 " for usb-gadget\n");
1939 usbg_fabric_configfs
= fabric
;
1943 static void usbg_deregister_configfs(void)
1945 if (!(usbg_fabric_configfs
))
1948 target_fabric_configfs_deregister(usbg_fabric_configfs
);
1949 usbg_fabric_configfs
= NULL
;
1952 /* Start gadget.c code */
1954 static struct usb_interface_descriptor bot_intf_desc
= {
1955 .bLength
= sizeof(bot_intf_desc
),
1956 .bDescriptorType
= USB_DT_INTERFACE
,
1958 .bAlternateSetting
= USB_G_ALT_INT_BBB
,
1959 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
1960 .bInterfaceSubClass
= USB_SC_SCSI
,
1961 .bInterfaceProtocol
= USB_PR_BULK
,
1964 static struct usb_interface_descriptor uasp_intf_desc
= {
1965 .bLength
= sizeof(uasp_intf_desc
),
1966 .bDescriptorType
= USB_DT_INTERFACE
,
1968 .bAlternateSetting
= USB_G_ALT_INT_UAS
,
1969 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
1970 .bInterfaceSubClass
= USB_SC_SCSI
,
1971 .bInterfaceProtocol
= USB_PR_UAS
,
1974 static struct usb_endpoint_descriptor uasp_bi_desc
= {
1975 .bLength
= USB_DT_ENDPOINT_SIZE
,
1976 .bDescriptorType
= USB_DT_ENDPOINT
,
1977 .bEndpointAddress
= USB_DIR_IN
,
1978 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1979 .wMaxPacketSize
= cpu_to_le16(512),
1982 static struct usb_endpoint_descriptor uasp_fs_bi_desc
= {
1983 .bLength
= USB_DT_ENDPOINT_SIZE
,
1984 .bDescriptorType
= USB_DT_ENDPOINT
,
1985 .bEndpointAddress
= USB_DIR_IN
,
1986 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1989 static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc
= {
1990 .bLength
= sizeof(uasp_bi_pipe_desc
),
1991 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1992 .bPipeID
= DATA_IN_PIPE_ID
,
1995 static struct usb_endpoint_descriptor uasp_ss_bi_desc
= {
1996 .bLength
= USB_DT_ENDPOINT_SIZE
,
1997 .bDescriptorType
= USB_DT_ENDPOINT
,
1998 .bEndpointAddress
= USB_DIR_IN
,
1999 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2000 .wMaxPacketSize
= cpu_to_le16(1024),
2003 static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc
= {
2004 .bLength
= sizeof(uasp_bi_ep_comp_desc
),
2005 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2007 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
2008 .wBytesPerInterval
= 0,
2011 static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc
= {
2012 .bLength
= sizeof(bot_bi_ep_comp_desc
),
2013 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2017 static struct usb_endpoint_descriptor uasp_bo_desc
= {
2018 .bLength
= USB_DT_ENDPOINT_SIZE
,
2019 .bDescriptorType
= USB_DT_ENDPOINT
,
2020 .bEndpointAddress
= USB_DIR_OUT
,
2021 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2022 .wMaxPacketSize
= cpu_to_le16(512),
2025 static struct usb_endpoint_descriptor uasp_fs_bo_desc
= {
2026 .bLength
= USB_DT_ENDPOINT_SIZE
,
2027 .bDescriptorType
= USB_DT_ENDPOINT
,
2028 .bEndpointAddress
= USB_DIR_OUT
,
2029 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2032 static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc
= {
2033 .bLength
= sizeof(uasp_bo_pipe_desc
),
2034 .bDescriptorType
= USB_DT_PIPE_USAGE
,
2035 .bPipeID
= DATA_OUT_PIPE_ID
,
2038 static struct usb_endpoint_descriptor uasp_ss_bo_desc
= {
2039 .bLength
= USB_DT_ENDPOINT_SIZE
,
2040 .bDescriptorType
= USB_DT_ENDPOINT
,
2041 .bEndpointAddress
= USB_DIR_OUT
,
2042 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2043 .wMaxPacketSize
= cpu_to_le16(0x400),
2046 static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc
= {
2047 .bLength
= sizeof(uasp_bo_ep_comp_desc
),
2048 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2049 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
2052 static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc
= {
2053 .bLength
= sizeof(bot_bo_ep_comp_desc
),
2054 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2057 static struct usb_endpoint_descriptor uasp_status_desc
= {
2058 .bLength
= USB_DT_ENDPOINT_SIZE
,
2059 .bDescriptorType
= USB_DT_ENDPOINT
,
2060 .bEndpointAddress
= USB_DIR_IN
,
2061 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2062 .wMaxPacketSize
= cpu_to_le16(512),
2065 static struct usb_endpoint_descriptor uasp_fs_status_desc
= {
2066 .bLength
= USB_DT_ENDPOINT_SIZE
,
2067 .bDescriptorType
= USB_DT_ENDPOINT
,
2068 .bEndpointAddress
= USB_DIR_IN
,
2069 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2072 static struct usb_pipe_usage_descriptor uasp_status_pipe_desc
= {
2073 .bLength
= sizeof(uasp_status_pipe_desc
),
2074 .bDescriptorType
= USB_DT_PIPE_USAGE
,
2075 .bPipeID
= STATUS_PIPE_ID
,
2078 static struct usb_endpoint_descriptor uasp_ss_status_desc
= {
2079 .bLength
= USB_DT_ENDPOINT_SIZE
,
2080 .bDescriptorType
= USB_DT_ENDPOINT
,
2081 .bEndpointAddress
= USB_DIR_IN
,
2082 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2083 .wMaxPacketSize
= cpu_to_le16(1024),
2086 static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc
= {
2087 .bLength
= sizeof(uasp_status_in_ep_comp_desc
),
2088 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2089 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
2092 static struct usb_endpoint_descriptor uasp_cmd_desc
= {
2093 .bLength
= USB_DT_ENDPOINT_SIZE
,
2094 .bDescriptorType
= USB_DT_ENDPOINT
,
2095 .bEndpointAddress
= USB_DIR_OUT
,
2096 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2097 .wMaxPacketSize
= cpu_to_le16(512),
2100 static struct usb_endpoint_descriptor uasp_fs_cmd_desc
= {
2101 .bLength
= USB_DT_ENDPOINT_SIZE
,
2102 .bDescriptorType
= USB_DT_ENDPOINT
,
2103 .bEndpointAddress
= USB_DIR_OUT
,
2104 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2107 static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc
= {
2108 .bLength
= sizeof(uasp_cmd_pipe_desc
),
2109 .bDescriptorType
= USB_DT_PIPE_USAGE
,
2110 .bPipeID
= CMD_PIPE_ID
,
2113 static struct usb_endpoint_descriptor uasp_ss_cmd_desc
= {
2114 .bLength
= USB_DT_ENDPOINT_SIZE
,
2115 .bDescriptorType
= USB_DT_ENDPOINT
,
2116 .bEndpointAddress
= USB_DIR_OUT
,
2117 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2118 .wMaxPacketSize
= cpu_to_le16(1024),
2121 static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc
= {
2122 .bLength
= sizeof(uasp_cmd_comp_desc
),
2123 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
2126 static struct usb_descriptor_header
*uasp_fs_function_desc
[] = {
2127 (struct usb_descriptor_header
*) &bot_intf_desc
,
2128 (struct usb_descriptor_header
*) &uasp_fs_bi_desc
,
2129 (struct usb_descriptor_header
*) &uasp_fs_bo_desc
,
2131 (struct usb_descriptor_header
*) &uasp_intf_desc
,
2132 (struct usb_descriptor_header
*) &uasp_fs_bi_desc
,
2133 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
2134 (struct usb_descriptor_header
*) &uasp_fs_bo_desc
,
2135 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
2136 (struct usb_descriptor_header
*) &uasp_fs_status_desc
,
2137 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
2138 (struct usb_descriptor_header
*) &uasp_fs_cmd_desc
,
2139 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
2143 static struct usb_descriptor_header
*uasp_hs_function_desc
[] = {
2144 (struct usb_descriptor_header
*) &bot_intf_desc
,
2145 (struct usb_descriptor_header
*) &uasp_bi_desc
,
2146 (struct usb_descriptor_header
*) &uasp_bo_desc
,
2148 (struct usb_descriptor_header
*) &uasp_intf_desc
,
2149 (struct usb_descriptor_header
*) &uasp_bi_desc
,
2150 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
2151 (struct usb_descriptor_header
*) &uasp_bo_desc
,
2152 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
2153 (struct usb_descriptor_header
*) &uasp_status_desc
,
2154 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
2155 (struct usb_descriptor_header
*) &uasp_cmd_desc
,
2156 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
2160 static struct usb_descriptor_header
*uasp_ss_function_desc
[] = {
2161 (struct usb_descriptor_header
*) &bot_intf_desc
,
2162 (struct usb_descriptor_header
*) &uasp_ss_bi_desc
,
2163 (struct usb_descriptor_header
*) &bot_bi_ep_comp_desc
,
2164 (struct usb_descriptor_header
*) &uasp_ss_bo_desc
,
2165 (struct usb_descriptor_header
*) &bot_bo_ep_comp_desc
,
2167 (struct usb_descriptor_header
*) &uasp_intf_desc
,
2168 (struct usb_descriptor_header
*) &uasp_ss_bi_desc
,
2169 (struct usb_descriptor_header
*) &uasp_bi_ep_comp_desc
,
2170 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
2171 (struct usb_descriptor_header
*) &uasp_ss_bo_desc
,
2172 (struct usb_descriptor_header
*) &uasp_bo_ep_comp_desc
,
2173 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
2174 (struct usb_descriptor_header
*) &uasp_ss_status_desc
,
2175 (struct usb_descriptor_header
*) &uasp_status_in_ep_comp_desc
,
2176 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
2177 (struct usb_descriptor_header
*) &uasp_ss_cmd_desc
,
2178 (struct usb_descriptor_header
*) &uasp_cmd_comp_desc
,
2179 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
2183 #define UAS_VENDOR_ID 0x0525 /* NetChip */
2184 #define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
2186 static struct usb_device_descriptor usbg_device_desc
= {
2187 .bLength
= sizeof(usbg_device_desc
),
2188 .bDescriptorType
= USB_DT_DEVICE
,
2189 .bcdUSB
= cpu_to_le16(0x0200),
2190 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
2191 .idVendor
= cpu_to_le16(UAS_VENDOR_ID
),
2192 .idProduct
= cpu_to_le16(UAS_PRODUCT_ID
),
2193 .bNumConfigurations
= 1,
2196 static struct usb_string usbg_us_strings
[] = {
2197 [USB_GADGET_MANUFACTURER_IDX
].s
= "Target Manufactor",
2198 [USB_GADGET_PRODUCT_IDX
].s
= "Target Product",
2199 [USB_GADGET_SERIAL_IDX
].s
= "000000000001",
2200 [USB_G_STR_CONFIG
].s
= "default config",
2201 [USB_G_STR_INT_UAS
].s
= "USB Attached SCSI",
2202 [USB_G_STR_INT_BBB
].s
= "Bulk Only Transport",
2206 static struct usb_gadget_strings usbg_stringtab
= {
2208 .strings
= usbg_us_strings
,
2211 static struct usb_gadget_strings
*usbg_strings
[] = {
2216 static int guas_unbind(struct usb_composite_dev
*cdev
)
2221 static struct usb_configuration usbg_config_driver
= {
2222 .label
= "Linux Target",
2223 .bConfigurationValue
= 1,
2224 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
2227 static void give_back_ep(struct usb_ep
**pep
)
2229 struct usb_ep
*ep
= *pep
;
2232 ep
->driver_data
= NULL
;
2235 static int usbg_bind(struct usb_configuration
*c
, struct usb_function
*f
)
2237 struct f_uas
*fu
= to_f_uas(f
);
2238 struct usb_gadget
*gadget
= c
->cdev
->gadget
;
2243 iface
= usb_interface_id(c
, f
);
2247 bot_intf_desc
.bInterfaceNumber
= iface
;
2248 uasp_intf_desc
.bInterfaceNumber
= iface
;
2250 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_bi_desc
,
2251 &uasp_bi_ep_comp_desc
);
2255 ep
->driver_data
= fu
;
2258 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_bo_desc
,
2259 &uasp_bo_ep_comp_desc
);
2262 ep
->driver_data
= fu
;
2265 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_status_desc
,
2266 &uasp_status_in_ep_comp_desc
);
2269 ep
->driver_data
= fu
;
2272 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_cmd_desc
,
2273 &uasp_cmd_comp_desc
);
2276 ep
->driver_data
= fu
;
2279 /* Assume endpoint addresses are the same for both speeds */
2280 uasp_bi_desc
.bEndpointAddress
= uasp_ss_bi_desc
.bEndpointAddress
;
2281 uasp_bo_desc
.bEndpointAddress
= uasp_ss_bo_desc
.bEndpointAddress
;
2282 uasp_status_desc
.bEndpointAddress
=
2283 uasp_ss_status_desc
.bEndpointAddress
;
2284 uasp_cmd_desc
.bEndpointAddress
= uasp_ss_cmd_desc
.bEndpointAddress
;
2286 uasp_fs_bi_desc
.bEndpointAddress
= uasp_ss_bi_desc
.bEndpointAddress
;
2287 uasp_fs_bo_desc
.bEndpointAddress
= uasp_ss_bo_desc
.bEndpointAddress
;
2288 uasp_fs_status_desc
.bEndpointAddress
=
2289 uasp_ss_status_desc
.bEndpointAddress
;
2290 uasp_fs_cmd_desc
.bEndpointAddress
= uasp_ss_cmd_desc
.bEndpointAddress
;
2292 ret
= usb_assign_descriptors(f
, uasp_fs_function_desc
,
2293 uasp_hs_function_desc
, uasp_ss_function_desc
);
2299 pr_err("Can't claim all required eps\n");
2301 give_back_ep(&fu
->ep_in
);
2302 give_back_ep(&fu
->ep_out
);
2303 give_back_ep(&fu
->ep_status
);
2304 give_back_ep(&fu
->ep_cmd
);
2308 static void usbg_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
2310 struct f_uas
*fu
= to_f_uas(f
);
2312 usb_free_all_descriptors(f
);
2316 struct guas_setup_wq
{
2317 struct work_struct work
;
2322 static void usbg_delayed_set_alt(struct work_struct
*wq
)
2324 struct guas_setup_wq
*work
= container_of(wq
, struct guas_setup_wq
,
2326 struct f_uas
*fu
= work
->fu
;
2327 int alt
= work
->alt
;
2331 if (fu
->flags
& USBG_IS_BOT
)
2332 bot_cleanup_old_alt(fu
);
2333 if (fu
->flags
& USBG_IS_UAS
)
2334 uasp_cleanup_old_alt(fu
);
2336 if (alt
== USB_G_ALT_INT_BBB
)
2338 else if (alt
== USB_G_ALT_INT_UAS
)
2340 usb_composite_setup_continue(fu
->function
.config
->cdev
);
2343 static int usbg_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
2345 struct f_uas
*fu
= to_f_uas(f
);
2347 if ((alt
== USB_G_ALT_INT_BBB
) || (alt
== USB_G_ALT_INT_UAS
)) {
2348 struct guas_setup_wq
*work
;
2350 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
2353 INIT_WORK(&work
->work
, usbg_delayed_set_alt
);
2356 schedule_work(&work
->work
);
2357 return USB_GADGET_DELAYED_STATUS
;
2362 static void usbg_disable(struct usb_function
*f
)
2364 struct f_uas
*fu
= to_f_uas(f
);
2366 if (fu
->flags
& USBG_IS_UAS
)
2367 uasp_cleanup_old_alt(fu
);
2368 else if (fu
->flags
& USBG_IS_BOT
)
2369 bot_cleanup_old_alt(fu
);
2373 static int usbg_setup(struct usb_function
*f
,
2374 const struct usb_ctrlrequest
*ctrl
)
2376 struct f_uas
*fu
= to_f_uas(f
);
2378 if (!(fu
->flags
& USBG_IS_BOT
))
2381 return usbg_bot_setup(f
, ctrl
);
2384 static int usbg_cfg_bind(struct usb_configuration
*c
)
2389 fu
= kzalloc(sizeof(*fu
), GFP_KERNEL
);
2392 fu
->function
.name
= "Target Function";
2393 fu
->function
.bind
= usbg_bind
;
2394 fu
->function
.unbind
= usbg_unbind
;
2395 fu
->function
.set_alt
= usbg_set_alt
;
2396 fu
->function
.setup
= usbg_setup
;
2397 fu
->function
.disable
= usbg_disable
;
2398 fu
->tpg
= the_only_tpg_I_currently_have
;
2400 bot_intf_desc
.iInterface
= usbg_us_strings
[USB_G_STR_INT_BBB
].id
;
2401 uasp_intf_desc
.iInterface
= usbg_us_strings
[USB_G_STR_INT_UAS
].id
;
2403 ret
= usb_add_function(c
, &fu
->function
);
2413 static int usb_target_bind(struct usb_composite_dev
*cdev
)
2417 ret
= usb_string_ids_tab(cdev
, usbg_us_strings
);
2421 usbg_device_desc
.iManufacturer
=
2422 usbg_us_strings
[USB_GADGET_MANUFACTURER_IDX
].id
;
2423 usbg_device_desc
.iProduct
= usbg_us_strings
[USB_GADGET_PRODUCT_IDX
].id
;
2424 usbg_device_desc
.iSerialNumber
=
2425 usbg_us_strings
[USB_GADGET_SERIAL_IDX
].id
;
2426 usbg_config_driver
.iConfiguration
=
2427 usbg_us_strings
[USB_G_STR_CONFIG
].id
;
2429 ret
= usb_add_config(cdev
, &usbg_config_driver
,
2433 usb_composite_overwrite_options(cdev
, &coverwrite
);
2437 static __refdata
struct usb_composite_driver usbg_driver
= {
2439 .dev
= &usbg_device_desc
,
2440 .strings
= usbg_strings
,
2441 .max_speed
= USB_SPEED_SUPER
,
2442 .bind
= usb_target_bind
,
2443 .unbind
= guas_unbind
,
2446 static int usbg_attach(struct usbg_tpg
*tpg
)
2448 return usb_composite_probe(&usbg_driver
);
2451 static void usbg_detach(struct usbg_tpg
*tpg
)
2453 usb_composite_unregister(&usbg_driver
);
2456 static int __init
usb_target_gadget_init(void)
2460 ret
= usbg_register_configfs();
2463 module_init(usb_target_gadget_init
);
2465 static void __exit
usb_target_gadget_exit(void)
2467 usbg_deregister_configfs();
2469 module_exit(usb_target_gadget_exit
);
2471 MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
2472 MODULE_DESCRIPTION("usb-gadget fabric");
2473 MODULE_LICENSE("GPL v2");