1 // SPDX-License-Identifier: GPL-2.0
2 /* Target based USB-Gadget
4 * UAS protocol handling, target callbacks, configfs handling,
5 * BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling.
7 * Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/configfs.h>
14 #include <linux/ctype.h>
15 #include <linux/usb/ch9.h>
16 #include <linux/usb/composite.h>
17 #include <linux/usb/gadget.h>
18 #include <linux/usb/storage.h>
19 #include <scsi/scsi_tcq.h>
20 #include <target/target_core_base.h>
21 #include <target/target_core_fabric.h>
22 #include <asm/unaligned.h>
28 #define TPG_INSTANCES 1
31 struct usb_function_instance
*func_inst
;
35 static struct tpg_instance tpg_instances
[TPG_INSTANCES
];
37 static DEFINE_MUTEX(tpg_instances_lock
);
39 static inline struct f_uas
*to_f_uas(struct usb_function
*f
)
41 return container_of(f
, struct f_uas
, function
);
44 /* Start bot.c code */
46 static int bot_enqueue_cmd_cbw(struct f_uas
*fu
)
50 if (fu
->flags
& USBG_BOT_CMD_PEND
)
53 ret
= usb_ep_queue(fu
->ep_out
, fu
->cmd
.req
, GFP_ATOMIC
);
55 fu
->flags
|= USBG_BOT_CMD_PEND
;
59 static void bot_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
61 struct usbg_cmd
*cmd
= req
->context
;
62 struct f_uas
*fu
= cmd
->fu
;
64 transport_generic_free_cmd(&cmd
->se_cmd
, 0);
65 if (req
->status
< 0) {
66 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
70 /* CSW completed, wait for next CBW */
71 bot_enqueue_cmd_cbw(fu
);
74 static void bot_enqueue_sense_code(struct f_uas
*fu
, struct usbg_cmd
*cmd
)
76 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
78 unsigned int csw_stat
;
80 csw_stat
= cmd
->csw_code
;
81 csw
->Tag
= cmd
->bot_tag
;
82 csw
->Status
= csw_stat
;
83 fu
->bot_status
.req
->context
= cmd
;
84 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_status
.req
, GFP_ATOMIC
);
86 pr_err("%s(%d) ERR: %d\n", __func__
, __LINE__
, ret
);
89 static void bot_err_compl(struct usb_ep
*ep
, struct usb_request
*req
)
91 struct usbg_cmd
*cmd
= req
->context
;
92 struct f_uas
*fu
= cmd
->fu
;
95 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
98 if (cmd
->data_len
> ep
->maxpacket
) {
99 req
->length
= ep
->maxpacket
;
100 cmd
->data_len
-= ep
->maxpacket
;
102 req
->length
= cmd
->data_len
;
106 usb_ep_queue(ep
, req
, GFP_ATOMIC
);
109 bot_enqueue_sense_code(fu
, cmd
);
112 static void bot_send_bad_status(struct usbg_cmd
*cmd
)
114 struct f_uas
*fu
= cmd
->fu
;
115 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
116 struct usb_request
*req
;
119 csw
->Residue
= cpu_to_le32(cmd
->data_len
);
124 req
= fu
->bot_req_in
;
127 req
= fu
->bot_req_out
;
130 if (cmd
->data_len
> fu
->ep_in
->maxpacket
) {
131 req
->length
= ep
->maxpacket
;
132 cmd
->data_len
-= ep
->maxpacket
;
134 req
->length
= cmd
->data_len
;
137 req
->complete
= bot_err_compl
;
139 req
->buf
= fu
->cmd
.buf
;
140 usb_ep_queue(ep
, req
, GFP_KERNEL
);
142 bot_enqueue_sense_code(fu
, cmd
);
146 static int bot_send_status(struct usbg_cmd
*cmd
, bool moved_data
)
148 struct f_uas
*fu
= cmd
->fu
;
149 struct bulk_cs_wrap
*csw
= &fu
->bot_status
.csw
;
152 if (cmd
->se_cmd
.scsi_status
== SAM_STAT_GOOD
) {
153 if (!moved_data
&& cmd
->data_len
) {
155 * the host wants to move data, we don't. Fill / empty
156 * the pipe and then send the csw with reside set.
158 cmd
->csw_code
= US_BULK_STAT_OK
;
159 bot_send_bad_status(cmd
);
163 csw
->Tag
= cmd
->bot_tag
;
164 csw
->Residue
= cpu_to_le32(0);
165 csw
->Status
= US_BULK_STAT_OK
;
166 fu
->bot_status
.req
->context
= cmd
;
168 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_status
.req
, GFP_KERNEL
);
170 pr_err("%s(%d) ERR: %d\n", __func__
, __LINE__
, ret
);
172 cmd
->csw_code
= US_BULK_STAT_FAIL
;
173 bot_send_bad_status(cmd
);
179 * Called after command (no data transfer) or after the write (to device)
180 * operation is completed
182 static int bot_send_status_response(struct usbg_cmd
*cmd
)
184 bool moved_data
= false;
188 return bot_send_status(cmd
, moved_data
);
191 /* Read request completed, now we have to send the CSW */
192 static void bot_read_compl(struct usb_ep
*ep
, struct usb_request
*req
)
194 struct usbg_cmd
*cmd
= req
->context
;
197 pr_err("ERR %s(%d)\n", __func__
, __LINE__
);
199 bot_send_status(cmd
, true);
202 static int bot_send_read_response(struct usbg_cmd
*cmd
)
204 struct f_uas
*fu
= cmd
->fu
;
205 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
206 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
209 if (!cmd
->data_len
) {
210 cmd
->csw_code
= US_BULK_STAT_PHASE
;
211 bot_send_bad_status(cmd
);
215 if (!gadget
->sg_supported
) {
216 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
220 sg_copy_to_buffer(se_cmd
->t_data_sg
,
221 se_cmd
->t_data_nents
,
223 se_cmd
->data_length
);
225 fu
->bot_req_in
->buf
= cmd
->data_buf
;
227 fu
->bot_req_in
->buf
= NULL
;
228 fu
->bot_req_in
->num_sgs
= se_cmd
->t_data_nents
;
229 fu
->bot_req_in
->sg
= se_cmd
->t_data_sg
;
232 fu
->bot_req_in
->complete
= bot_read_compl
;
233 fu
->bot_req_in
->length
= se_cmd
->data_length
;
234 fu
->bot_req_in
->context
= cmd
;
235 ret
= usb_ep_queue(fu
->ep_in
, fu
->bot_req_in
, GFP_ATOMIC
);
237 pr_err("%s(%d)\n", __func__
, __LINE__
);
241 static void usbg_data_write_cmpl(struct usb_ep
*, struct usb_request
*);
242 static int usbg_prepare_w_request(struct usbg_cmd
*, struct usb_request
*);
244 static int bot_send_write_request(struct usbg_cmd
*cmd
)
246 struct f_uas
*fu
= cmd
->fu
;
247 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
248 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
251 init_completion(&cmd
->write_complete
);
254 if (!cmd
->data_len
) {
255 cmd
->csw_code
= US_BULK_STAT_PHASE
;
259 if (!gadget
->sg_supported
) {
260 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_KERNEL
);
264 fu
->bot_req_out
->buf
= cmd
->data_buf
;
266 fu
->bot_req_out
->buf
= NULL
;
267 fu
->bot_req_out
->num_sgs
= se_cmd
->t_data_nents
;
268 fu
->bot_req_out
->sg
= se_cmd
->t_data_sg
;
271 fu
->bot_req_out
->complete
= usbg_data_write_cmpl
;
272 fu
->bot_req_out
->length
= se_cmd
->data_length
;
273 fu
->bot_req_out
->context
= cmd
;
275 ret
= usbg_prepare_w_request(cmd
, fu
->bot_req_out
);
278 ret
= usb_ep_queue(fu
->ep_out
, fu
->bot_req_out
, GFP_KERNEL
);
280 pr_err("%s(%d)\n", __func__
, __LINE__
);
282 wait_for_completion(&cmd
->write_complete
);
283 target_execute_cmd(se_cmd
);
288 static int bot_submit_command(struct f_uas
*, void *, unsigned int);
290 static void bot_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
292 struct f_uas
*fu
= req
->context
;
295 fu
->flags
&= ~USBG_BOT_CMD_PEND
;
300 ret
= bot_submit_command(fu
, req
->buf
, req
->actual
);
302 pr_err("%s(%d): %d\n", __func__
, __LINE__
, ret
);
305 static int bot_prepare_reqs(struct f_uas
*fu
)
309 fu
->bot_req_in
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
313 fu
->bot_req_out
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
314 if (!fu
->bot_req_out
)
317 fu
->cmd
.req
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
321 fu
->bot_status
.req
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
322 if (!fu
->bot_status
.req
)
325 fu
->bot_status
.req
->buf
= &fu
->bot_status
.csw
;
326 fu
->bot_status
.req
->length
= US_BULK_CS_WRAP_LEN
;
327 fu
->bot_status
.req
->complete
= bot_status_complete
;
328 fu
->bot_status
.csw
.Signature
= cpu_to_le32(US_BULK_CS_SIGN
);
330 fu
->cmd
.buf
= kmalloc(fu
->ep_out
->maxpacket
, GFP_KERNEL
);
334 fu
->cmd
.req
->complete
= bot_cmd_complete
;
335 fu
->cmd
.req
->buf
= fu
->cmd
.buf
;
336 fu
->cmd
.req
->length
= fu
->ep_out
->maxpacket
;
337 fu
->cmd
.req
->context
= fu
;
339 ret
= bot_enqueue_cmd_cbw(fu
);
347 usb_ep_free_request(fu
->ep_in
, fu
->bot_status
.req
);
349 usb_ep_free_request(fu
->ep_out
, fu
->cmd
.req
);
352 usb_ep_free_request(fu
->ep_out
, fu
->bot_req_out
);
353 fu
->bot_req_out
= NULL
;
355 usb_ep_free_request(fu
->ep_in
, fu
->bot_req_in
);
356 fu
->bot_req_in
= NULL
;
358 pr_err("BOT: endpoint setup failed\n");
362 static void bot_cleanup_old_alt(struct f_uas
*fu
)
364 if (!(fu
->flags
& USBG_ENABLED
))
367 usb_ep_disable(fu
->ep_in
);
368 usb_ep_disable(fu
->ep_out
);
373 usb_ep_free_request(fu
->ep_in
, fu
->bot_req_in
);
374 usb_ep_free_request(fu
->ep_out
, fu
->bot_req_out
);
375 usb_ep_free_request(fu
->ep_out
, fu
->cmd
.req
);
376 usb_ep_free_request(fu
->ep_in
, fu
->bot_status
.req
);
380 fu
->bot_req_in
= NULL
;
381 fu
->bot_req_out
= NULL
;
383 fu
->bot_status
.req
= NULL
;
387 static void bot_set_alt(struct f_uas
*fu
)
389 struct usb_function
*f
= &fu
->function
;
390 struct usb_gadget
*gadget
= f
->config
->cdev
->gadget
;
393 fu
->flags
= USBG_IS_BOT
;
395 config_ep_by_speed(gadget
, f
, fu
->ep_in
);
396 ret
= usb_ep_enable(fu
->ep_in
);
400 config_ep_by_speed(gadget
, f
, fu
->ep_out
);
401 ret
= usb_ep_enable(fu
->ep_out
);
405 ret
= bot_prepare_reqs(fu
);
408 fu
->flags
|= USBG_ENABLED
;
409 pr_info("Using the BOT protocol\n");
412 usb_ep_disable(fu
->ep_out
);
414 usb_ep_disable(fu
->ep_in
);
416 fu
->flags
= USBG_IS_BOT
;
419 static int usbg_bot_setup(struct usb_function
*f
,
420 const struct usb_ctrlrequest
*ctrl
)
422 struct f_uas
*fu
= to_f_uas(f
);
423 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
424 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
425 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
429 switch (ctrl
->bRequest
) {
430 case US_BULK_GET_MAX_LUN
:
431 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_TYPE_CLASS
|
432 USB_RECIP_INTERFACE
))
439 luns
= atomic_read(&fu
->tpg
->tpg_port_count
);
441 pr_err("No LUNs configured?\n");
445 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
446 * accessed. The upper limit is 0xf
450 pr_info_once("Limiting the number of luns to 16\n");
453 ret_lun
= cdev
->req
->buf
;
455 cdev
->req
->length
= 1;
456 return usb_ep_queue(cdev
->gadget
->ep0
, cdev
->req
, GFP_ATOMIC
);
458 case US_BULK_RESET_REQUEST
:
459 /* XXX maybe we should remove previous requests for IN + OUT */
460 bot_enqueue_cmd_cbw(fu
);
466 /* Start uas.c code */
468 static void uasp_cleanup_one_stream(struct f_uas
*fu
, struct uas_stream
*stream
)
470 /* We have either all three allocated or none */
474 usb_ep_free_request(fu
->ep_in
, stream
->req_in
);
475 usb_ep_free_request(fu
->ep_out
, stream
->req_out
);
476 usb_ep_free_request(fu
->ep_status
, stream
->req_status
);
478 stream
->req_in
= NULL
;
479 stream
->req_out
= NULL
;
480 stream
->req_status
= NULL
;
483 static void uasp_free_cmdreq(struct f_uas
*fu
)
485 usb_ep_free_request(fu
->ep_cmd
, fu
->cmd
.req
);
491 static void uasp_cleanup_old_alt(struct f_uas
*fu
)
495 if (!(fu
->flags
& USBG_ENABLED
))
498 usb_ep_disable(fu
->ep_in
);
499 usb_ep_disable(fu
->ep_out
);
500 usb_ep_disable(fu
->ep_status
);
501 usb_ep_disable(fu
->ep_cmd
);
503 for (i
= 0; i
< UASP_SS_EP_COMP_NUM_STREAMS
; i
++)
504 uasp_cleanup_one_stream(fu
, &fu
->stream
[i
]);
505 uasp_free_cmdreq(fu
);
508 static void uasp_status_data_cmpl(struct usb_ep
*ep
, struct usb_request
*req
);
510 static int uasp_prepare_r_request(struct usbg_cmd
*cmd
)
512 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
513 struct f_uas
*fu
= cmd
->fu
;
514 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
515 struct uas_stream
*stream
= cmd
->stream
;
517 if (!gadget
->sg_supported
) {
518 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
522 sg_copy_to_buffer(se_cmd
->t_data_sg
,
523 se_cmd
->t_data_nents
,
525 se_cmd
->data_length
);
527 stream
->req_in
->buf
= cmd
->data_buf
;
529 stream
->req_in
->buf
= NULL
;
530 stream
->req_in
->num_sgs
= se_cmd
->t_data_nents
;
531 stream
->req_in
->sg
= se_cmd
->t_data_sg
;
534 stream
->req_in
->complete
= uasp_status_data_cmpl
;
535 stream
->req_in
->length
= se_cmd
->data_length
;
536 stream
->req_in
->context
= cmd
;
538 cmd
->state
= UASP_SEND_STATUS
;
542 static void uasp_prepare_status(struct usbg_cmd
*cmd
)
544 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
545 struct sense_iu
*iu
= &cmd
->sense_iu
;
546 struct uas_stream
*stream
= cmd
->stream
;
548 cmd
->state
= UASP_QUEUE_COMMAND
;
549 iu
->iu_id
= IU_ID_STATUS
;
550 iu
->tag
= cpu_to_be16(cmd
->tag
);
553 * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
555 iu
->len
= cpu_to_be16(se_cmd
->scsi_sense_length
);
556 iu
->status
= se_cmd
->scsi_status
;
557 stream
->req_status
->context
= cmd
;
558 stream
->req_status
->length
= se_cmd
->scsi_sense_length
+ 16;
559 stream
->req_status
->buf
= iu
;
560 stream
->req_status
->complete
= uasp_status_data_cmpl
;
563 static void uasp_status_data_cmpl(struct usb_ep
*ep
, struct usb_request
*req
)
565 struct usbg_cmd
*cmd
= req
->context
;
566 struct uas_stream
*stream
= cmd
->stream
;
567 struct f_uas
*fu
= cmd
->fu
;
573 switch (cmd
->state
) {
575 ret
= uasp_prepare_r_request(cmd
);
578 ret
= usb_ep_queue(fu
->ep_in
, stream
->req_in
, GFP_ATOMIC
);
580 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
583 case UASP_RECEIVE_DATA
:
584 ret
= usbg_prepare_w_request(cmd
, stream
->req_out
);
587 ret
= usb_ep_queue(fu
->ep_out
, stream
->req_out
, GFP_ATOMIC
);
589 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
592 case UASP_SEND_STATUS
:
593 uasp_prepare_status(cmd
);
594 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
597 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
600 case UASP_QUEUE_COMMAND
:
601 transport_generic_free_cmd(&cmd
->se_cmd
, 0);
602 usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
611 transport_generic_free_cmd(&cmd
->se_cmd
, 0);
614 static int uasp_send_status_response(struct usbg_cmd
*cmd
)
616 struct f_uas
*fu
= cmd
->fu
;
617 struct uas_stream
*stream
= cmd
->stream
;
618 struct sense_iu
*iu
= &cmd
->sense_iu
;
620 iu
->tag
= cpu_to_be16(cmd
->tag
);
621 stream
->req_status
->complete
= uasp_status_data_cmpl
;
622 stream
->req_status
->context
= cmd
;
624 uasp_prepare_status(cmd
);
625 return usb_ep_queue(fu
->ep_status
, stream
->req_status
, GFP_ATOMIC
);
628 static int uasp_send_read_response(struct usbg_cmd
*cmd
)
630 struct f_uas
*fu
= cmd
->fu
;
631 struct uas_stream
*stream
= cmd
->stream
;
632 struct sense_iu
*iu
= &cmd
->sense_iu
;
637 iu
->tag
= cpu_to_be16(cmd
->tag
);
638 if (fu
->flags
& USBG_USE_STREAMS
) {
640 ret
= uasp_prepare_r_request(cmd
);
643 ret
= usb_ep_queue(fu
->ep_in
, stream
->req_in
, GFP_ATOMIC
);
645 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
646 kfree(cmd
->data_buf
);
647 cmd
->data_buf
= NULL
;
652 iu
->iu_id
= IU_ID_READ_READY
;
653 iu
->tag
= cpu_to_be16(cmd
->tag
);
655 stream
->req_status
->complete
= uasp_status_data_cmpl
;
656 stream
->req_status
->context
= cmd
;
658 cmd
->state
= UASP_SEND_DATA
;
659 stream
->req_status
->buf
= iu
;
660 stream
->req_status
->length
= sizeof(struct iu
);
662 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
665 pr_err("%s(%d) => %d\n", __func__
, __LINE__
, ret
);
671 static int uasp_send_write_request(struct usbg_cmd
*cmd
)
673 struct f_uas
*fu
= cmd
->fu
;
674 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
675 struct uas_stream
*stream
= cmd
->stream
;
676 struct sense_iu
*iu
= &cmd
->sense_iu
;
679 init_completion(&cmd
->write_complete
);
682 iu
->tag
= cpu_to_be16(cmd
->tag
);
684 if (fu
->flags
& USBG_USE_STREAMS
) {
686 ret
= usbg_prepare_w_request(cmd
, stream
->req_out
);
689 ret
= usb_ep_queue(fu
->ep_out
, stream
->req_out
, GFP_ATOMIC
);
691 pr_err("%s(%d)\n", __func__
, __LINE__
);
695 iu
->iu_id
= IU_ID_WRITE_READY
;
696 iu
->tag
= cpu_to_be16(cmd
->tag
);
698 stream
->req_status
->complete
= uasp_status_data_cmpl
;
699 stream
->req_status
->context
= cmd
;
701 cmd
->state
= UASP_RECEIVE_DATA
;
702 stream
->req_status
->buf
= iu
;
703 stream
->req_status
->length
= sizeof(struct iu
);
705 ret
= usb_ep_queue(fu
->ep_status
, stream
->req_status
,
708 pr_err("%s(%d)\n", __func__
, __LINE__
);
711 wait_for_completion(&cmd
->write_complete
);
712 target_execute_cmd(se_cmd
);
717 static int usbg_submit_command(struct f_uas
*, void *, unsigned int);
719 static void uasp_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
721 struct f_uas
*fu
= req
->context
;
727 ret
= usbg_submit_command(fu
, req
->buf
, req
->actual
);
729 * Once we tune for performance enqueue the command req here again so
730 * we can receive a second command while we processing this one. Pay
731 * attention to properly sync STAUS endpoint with DATA IN + OUT so you
736 usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
739 static int uasp_alloc_stream_res(struct f_uas
*fu
, struct uas_stream
*stream
)
741 stream
->req_in
= usb_ep_alloc_request(fu
->ep_in
, GFP_KERNEL
);
745 stream
->req_out
= usb_ep_alloc_request(fu
->ep_out
, GFP_KERNEL
);
746 if (!stream
->req_out
)
749 stream
->req_status
= usb_ep_alloc_request(fu
->ep_status
, GFP_KERNEL
);
750 if (!stream
->req_status
)
755 usb_ep_free_request(fu
->ep_status
, stream
->req_status
);
756 stream
->req_status
= NULL
;
758 usb_ep_free_request(fu
->ep_out
, stream
->req_out
);
759 stream
->req_out
= NULL
;
764 static int uasp_alloc_cmd(struct f_uas
*fu
)
766 fu
->cmd
.req
= usb_ep_alloc_request(fu
->ep_cmd
, GFP_KERNEL
);
770 fu
->cmd
.buf
= kmalloc(fu
->ep_cmd
->maxpacket
, GFP_KERNEL
);
774 fu
->cmd
.req
->complete
= uasp_cmd_complete
;
775 fu
->cmd
.req
->buf
= fu
->cmd
.buf
;
776 fu
->cmd
.req
->length
= fu
->ep_cmd
->maxpacket
;
777 fu
->cmd
.req
->context
= fu
;
781 usb_ep_free_request(fu
->ep_cmd
, fu
->cmd
.req
);
786 static void uasp_setup_stream_res(struct f_uas
*fu
, int max_streams
)
790 for (i
= 0; i
< max_streams
; i
++) {
791 struct uas_stream
*s
= &fu
->stream
[i
];
793 s
->req_in
->stream_id
= i
+ 1;
794 s
->req_out
->stream_id
= i
+ 1;
795 s
->req_status
->stream_id
= i
+ 1;
799 static int uasp_prepare_reqs(struct f_uas
*fu
)
805 if (fu
->flags
& USBG_USE_STREAMS
)
806 max_streams
= UASP_SS_EP_COMP_NUM_STREAMS
;
810 for (i
= 0; i
< max_streams
; i
++) {
811 ret
= uasp_alloc_stream_res(fu
, &fu
->stream
[i
]);
816 ret
= uasp_alloc_cmd(fu
);
818 goto err_free_stream
;
819 uasp_setup_stream_res(fu
, max_streams
);
821 ret
= usb_ep_queue(fu
->ep_cmd
, fu
->cmd
.req
, GFP_ATOMIC
);
823 goto err_free_stream
;
828 uasp_free_cmdreq(fu
);
833 uasp_cleanup_one_stream(fu
, &fu
->stream
[i
- 1]);
837 pr_err("UASP: endpoint setup failed\n");
841 static void uasp_set_alt(struct f_uas
*fu
)
843 struct usb_function
*f
= &fu
->function
;
844 struct usb_gadget
*gadget
= f
->config
->cdev
->gadget
;
847 fu
->flags
= USBG_IS_UAS
;
849 if (gadget
->speed
== USB_SPEED_SUPER
)
850 fu
->flags
|= USBG_USE_STREAMS
;
852 config_ep_by_speed(gadget
, f
, fu
->ep_in
);
853 ret
= usb_ep_enable(fu
->ep_in
);
857 config_ep_by_speed(gadget
, f
, fu
->ep_out
);
858 ret
= usb_ep_enable(fu
->ep_out
);
862 config_ep_by_speed(gadget
, f
, fu
->ep_cmd
);
863 ret
= usb_ep_enable(fu
->ep_cmd
);
866 config_ep_by_speed(gadget
, f
, fu
->ep_status
);
867 ret
= usb_ep_enable(fu
->ep_status
);
871 ret
= uasp_prepare_reqs(fu
);
874 fu
->flags
|= USBG_ENABLED
;
876 pr_info("Using the UAS protocol\n");
879 usb_ep_disable(fu
->ep_status
);
881 usb_ep_disable(fu
->ep_cmd
);
883 usb_ep_disable(fu
->ep_out
);
885 usb_ep_disable(fu
->ep_in
);
890 static int get_cmd_dir(const unsigned char *cdb
)
902 case SERVICE_ACTION_IN_16
:
904 case PERSISTENT_RESERVE_IN
:
905 case SECURITY_PROTOCOL_IN
:
906 case ACCESS_CONTROL_IN
:
908 case READ_BLOCK_LIMITS
:
912 case READ_FORMAT_CAPACITIES
:
914 ret
= DMA_FROM_DEVICE
;
924 case WRITE_VERIFY_12
:
925 case PERSISTENT_RESERVE_OUT
:
926 case MAINTENANCE_OUT
:
927 case SECURITY_PROTOCOL_OUT
:
928 case ACCESS_CONTROL_OUT
:
931 case ALLOW_MEDIUM_REMOVAL
:
932 case TEST_UNIT_READY
:
933 case SYNCHRONIZE_CACHE
:
940 case WRITE_FILEMARKS
:
944 #define CMD_DIR_MSG "target: Unknown data direction for SCSI Opcode 0x%02x\n"
945 pr_warn(CMD_DIR_MSG
, cdb
[0]);
952 static void usbg_data_write_cmpl(struct usb_ep
*ep
, struct usb_request
*req
)
954 struct usbg_cmd
*cmd
= req
->context
;
955 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
957 if (req
->status
< 0) {
958 pr_err("%s() state %d transfer failed\n", __func__
, cmd
->state
);
962 if (req
->num_sgs
== 0) {
963 sg_copy_from_buffer(se_cmd
->t_data_sg
,
964 se_cmd
->t_data_nents
,
966 se_cmd
->data_length
);
969 complete(&cmd
->write_complete
);
973 transport_generic_free_cmd(&cmd
->se_cmd
, 0);
976 static int usbg_prepare_w_request(struct usbg_cmd
*cmd
, struct usb_request
*req
)
978 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
979 struct f_uas
*fu
= cmd
->fu
;
980 struct usb_gadget
*gadget
= fuas_to_gadget(fu
);
982 if (!gadget
->sg_supported
) {
983 cmd
->data_buf
= kmalloc(se_cmd
->data_length
, GFP_ATOMIC
);
987 req
->buf
= cmd
->data_buf
;
990 req
->num_sgs
= se_cmd
->t_data_nents
;
991 req
->sg
= se_cmd
->t_data_sg
;
994 req
->complete
= usbg_data_write_cmpl
;
995 req
->length
= se_cmd
->data_length
;
1000 static int usbg_send_status_response(struct se_cmd
*se_cmd
)
1002 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1004 struct f_uas
*fu
= cmd
->fu
;
1006 if (fu
->flags
& USBG_IS_BOT
)
1007 return bot_send_status_response(cmd
);
1009 return uasp_send_status_response(cmd
);
1012 static int usbg_send_write_request(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_write_request(cmd
);
1021 return uasp_send_write_request(cmd
);
1024 static int usbg_send_read_response(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_read_response(cmd
);
1033 return uasp_send_read_response(cmd
);
1036 static void usbg_cmd_work(struct work_struct
*work
)
1038 struct usbg_cmd
*cmd
= container_of(work
, struct usbg_cmd
, work
);
1039 struct se_cmd
*se_cmd
;
1040 struct tcm_usbg_nexus
*tv_nexus
;
1041 struct usbg_tpg
*tpg
;
1042 int dir
, flags
= (TARGET_SCF_UNKNOWN_SIZE
| TARGET_SCF_ACK_KREF
);
1044 se_cmd
= &cmd
->se_cmd
;
1046 tv_nexus
= tpg
->tpg_nexus
;
1047 dir
= get_cmd_dir(cmd
->cmd_buf
);
1049 transport_init_se_cmd(se_cmd
,
1050 tv_nexus
->tvn_se_sess
->se_tpg
->se_tpg_tfo
,
1051 tv_nexus
->tvn_se_sess
, cmd
->data_len
, DMA_NONE
,
1052 cmd
->prio_attr
, cmd
->sense_iu
.sense
);
1056 if (target_submit_cmd(se_cmd
, tv_nexus
->tvn_se_sess
, cmd
->cmd_buf
,
1057 cmd
->sense_iu
.sense
, cmd
->unpacked_lun
, 0,
1058 cmd
->prio_attr
, dir
, flags
) < 0)
1064 transport_send_check_condition_and_sense(se_cmd
,
1065 TCM_UNSUPPORTED_SCSI_OPCODE
, 1);
1066 transport_generic_free_cmd(&cmd
->se_cmd
, 0);
1069 static struct usbg_cmd
*usbg_get_cmd(struct f_uas
*fu
,
1070 struct tcm_usbg_nexus
*tv_nexus
, u32 scsi_tag
)
1072 struct se_session
*se_sess
= tv_nexus
->tvn_se_sess
;
1073 struct usbg_cmd
*cmd
;
1076 tag
= percpu_ida_alloc(&se_sess
->sess_tag_pool
, TASK_RUNNING
);
1078 return ERR_PTR(-ENOMEM
);
1080 cmd
= &((struct usbg_cmd
*)se_sess
->sess_cmd_map
)[tag
];
1081 memset(cmd
, 0, sizeof(*cmd
));
1082 cmd
->se_cmd
.map_tag
= tag
;
1083 cmd
->se_cmd
.tag
= cmd
->tag
= scsi_tag
;
1089 static void usbg_release_cmd(struct se_cmd
*);
1091 static int usbg_submit_command(struct f_uas
*fu
,
1092 void *cmdbuf
, unsigned int len
)
1094 struct command_iu
*cmd_iu
= cmdbuf
;
1095 struct usbg_cmd
*cmd
;
1096 struct usbg_tpg
*tpg
= fu
->tpg
;
1097 struct tcm_usbg_nexus
*tv_nexus
= tpg
->tpg_nexus
;
1101 if (cmd_iu
->iu_id
!= IU_ID_COMMAND
) {
1102 pr_err("Unsupported type %d\n", cmd_iu
->iu_id
);
1106 tv_nexus
= tpg
->tpg_nexus
;
1108 pr_err("Missing nexus, ignoring command\n");
1112 cmd_len
= (cmd_iu
->len
& ~0x3) + 16;
1113 if (cmd_len
> USBG_MAX_CMD
)
1116 scsi_tag
= be16_to_cpup(&cmd_iu
->tag
);
1117 cmd
= usbg_get_cmd(fu
, tv_nexus
, scsi_tag
);
1119 pr_err("usbg_get_cmd failed\n");
1122 memcpy(cmd
->cmd_buf
, cmd_iu
->cdb
, cmd_len
);
1124 if (fu
->flags
& USBG_USE_STREAMS
) {
1125 if (cmd
->tag
> UASP_SS_EP_COMP_NUM_STREAMS
)
1128 cmd
->stream
= &fu
->stream
[0];
1130 cmd
->stream
= &fu
->stream
[cmd
->tag
- 1];
1132 cmd
->stream
= &fu
->stream
[0];
1135 switch (cmd_iu
->prio_attr
& 0x7) {
1137 cmd
->prio_attr
= TCM_HEAD_TAG
;
1139 case UAS_ORDERED_TAG
:
1140 cmd
->prio_attr
= TCM_ORDERED_TAG
;
1143 cmd
->prio_attr
= TCM_ACA_TAG
;
1146 pr_debug_once("Unsupported prio_attr: %02x.\n",
1149 case UAS_SIMPLE_TAG
:
1150 cmd
->prio_attr
= TCM_SIMPLE_TAG
;
1154 cmd
->unpacked_lun
= scsilun_to_int(&cmd_iu
->lun
);
1156 INIT_WORK(&cmd
->work
, usbg_cmd_work
);
1157 queue_work(tpg
->workqueue
, &cmd
->work
);
1161 usbg_release_cmd(&cmd
->se_cmd
);
1165 static void bot_cmd_work(struct work_struct
*work
)
1167 struct usbg_cmd
*cmd
= container_of(work
, struct usbg_cmd
, work
);
1168 struct se_cmd
*se_cmd
;
1169 struct tcm_usbg_nexus
*tv_nexus
;
1170 struct usbg_tpg
*tpg
;
1173 se_cmd
= &cmd
->se_cmd
;
1175 tv_nexus
= tpg
->tpg_nexus
;
1176 dir
= get_cmd_dir(cmd
->cmd_buf
);
1178 transport_init_se_cmd(se_cmd
,
1179 tv_nexus
->tvn_se_sess
->se_tpg
->se_tpg_tfo
,
1180 tv_nexus
->tvn_se_sess
, cmd
->data_len
, DMA_NONE
,
1181 cmd
->prio_attr
, cmd
->sense_iu
.sense
);
1185 if (target_submit_cmd(se_cmd
, tv_nexus
->tvn_se_sess
,
1186 cmd
->cmd_buf
, cmd
->sense_iu
.sense
, cmd
->unpacked_lun
,
1187 cmd
->data_len
, cmd
->prio_attr
, dir
, 0) < 0)
1193 transport_send_check_condition_and_sense(se_cmd
,
1194 TCM_UNSUPPORTED_SCSI_OPCODE
, 1);
1195 transport_generic_free_cmd(&cmd
->se_cmd
, 0);
1198 static int bot_submit_command(struct f_uas
*fu
,
1199 void *cmdbuf
, unsigned int len
)
1201 struct bulk_cb_wrap
*cbw
= cmdbuf
;
1202 struct usbg_cmd
*cmd
;
1203 struct usbg_tpg
*tpg
= fu
->tpg
;
1204 struct tcm_usbg_nexus
*tv_nexus
;
1207 if (cbw
->Signature
!= cpu_to_le32(US_BULK_CB_SIGN
)) {
1208 pr_err("Wrong signature on CBW\n");
1212 pr_err("Wrong length for CBW\n");
1216 cmd_len
= cbw
->Length
;
1217 if (cmd_len
< 1 || cmd_len
> 16)
1220 tv_nexus
= tpg
->tpg_nexus
;
1222 pr_err("Missing nexus, ignoring command\n");
1226 cmd
= usbg_get_cmd(fu
, tv_nexus
, cbw
->Tag
);
1228 pr_err("usbg_get_cmd failed\n");
1231 memcpy(cmd
->cmd_buf
, cbw
->CDB
, cmd_len
);
1233 cmd
->bot_tag
= cbw
->Tag
;
1234 cmd
->prio_attr
= TCM_SIMPLE_TAG
;
1235 cmd
->unpacked_lun
= cbw
->Lun
;
1236 cmd
->is_read
= cbw
->Flags
& US_BULK_FLAG_IN
? 1 : 0;
1237 cmd
->data_len
= le32_to_cpu(cbw
->DataTransferLength
);
1238 cmd
->se_cmd
.tag
= le32_to_cpu(cmd
->bot_tag
);
1240 INIT_WORK(&cmd
->work
, bot_cmd_work
);
1241 queue_work(tpg
->workqueue
, &cmd
->work
);
1246 /* Start fabric.c code */
1248 static int usbg_check_true(struct se_portal_group
*se_tpg
)
1253 static int usbg_check_false(struct se_portal_group
*se_tpg
)
1258 static char *usbg_get_fabric_name(void)
1260 return "usb_gadget";
1263 static char *usbg_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1265 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1266 struct usbg_tpg
, se_tpg
);
1267 struct usbg_tport
*tport
= tpg
->tport
;
1269 return &tport
->tport_name
[0];
1272 static u16
usbg_get_tag(struct se_portal_group
*se_tpg
)
1274 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1275 struct usbg_tpg
, se_tpg
);
1276 return tpg
->tport_tpgt
;
1279 static u32
usbg_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
1284 static void usbg_release_cmd(struct se_cmd
*se_cmd
)
1286 struct usbg_cmd
*cmd
= container_of(se_cmd
, struct usbg_cmd
,
1288 struct se_session
*se_sess
= se_cmd
->se_sess
;
1290 kfree(cmd
->data_buf
);
1291 percpu_ida_free(&se_sess
->sess_tag_pool
, se_cmd
->map_tag
);
1294 static u32
usbg_sess_get_index(struct se_session
*se_sess
)
1300 * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
1302 static int usbg_write_pending_status(struct se_cmd
*se_cmd
)
1307 static void usbg_set_default_node_attrs(struct se_node_acl
*nacl
)
1311 static int usbg_get_cmd_state(struct se_cmd
*se_cmd
)
1316 static void usbg_queue_tm_rsp(struct se_cmd
*se_cmd
)
1320 static void usbg_aborted_task(struct se_cmd
*se_cmd
)
1324 static const char *usbg_check_wwn(const char *name
)
1329 n
= strstr(name
, "naa.");
1334 if (len
== 0 || len
> USBG_NAMELEN
- 1)
1339 static int usbg_init_nodeacl(struct se_node_acl
*se_nacl
, const char *name
)
1341 if (!usbg_check_wwn(name
))
1346 static struct se_portal_group
*usbg_make_tpg(
1348 struct config_group
*group
,
1351 struct usbg_tport
*tport
= container_of(wwn
, struct usbg_tport
,
1353 struct usbg_tpg
*tpg
;
1356 struct f_tcm_opts
*opts
;
1359 if (strstr(name
, "tpgt_") != name
)
1360 return ERR_PTR(-EINVAL
);
1361 if (kstrtoul(name
+ 5, 0, &tpgt
) || tpgt
> UINT_MAX
)
1362 return ERR_PTR(-EINVAL
);
1364 mutex_lock(&tpg_instances_lock
);
1365 for (i
= 0; i
< TPG_INSTANCES
; ++i
)
1366 if (tpg_instances
[i
].func_inst
&& !tpg_instances
[i
].tpg
)
1368 if (i
== TPG_INSTANCES
)
1371 opts
= container_of(tpg_instances
[i
].func_inst
, struct f_tcm_opts
,
1373 mutex_lock(&opts
->dep_lock
);
1377 if (opts
->has_dep
) {
1378 if (!try_module_get(opts
->dependent
))
1381 ret
= configfs_depend_item_unlocked(
1383 &opts
->func_inst
.group
.cg_item
);
1388 tpg
= kzalloc(sizeof(struct usbg_tpg
), GFP_KERNEL
);
1392 mutex_init(&tpg
->tpg_mutex
);
1393 atomic_set(&tpg
->tpg_port_count
, 0);
1394 tpg
->workqueue
= alloc_workqueue("tcm_usb_gadget", 0, 1);
1395 if (!tpg
->workqueue
)
1399 tpg
->tport_tpgt
= tpgt
;
1402 * SPC doesn't assign a protocol identifier for USB-SCSI, so we
1403 * pretend to be SAS..
1405 ret
= core_tpg_register(wwn
, &tpg
->se_tpg
, SCSI_PROTOCOL_SAS
);
1407 goto free_workqueue
;
1409 tpg_instances
[i
].tpg
= tpg
;
1410 tpg
->fi
= tpg_instances
[i
].func_inst
;
1411 mutex_unlock(&opts
->dep_lock
);
1412 mutex_unlock(&tpg_instances_lock
);
1413 return &tpg
->se_tpg
;
1416 destroy_workqueue(tpg
->workqueue
);
1421 module_put(opts
->dependent
);
1423 configfs_undepend_item_unlocked(&opts
->func_inst
.group
.cg_item
);
1425 mutex_unlock(&opts
->dep_lock
);
1427 mutex_unlock(&tpg_instances_lock
);
1429 return ERR_PTR(ret
);
1432 static int tcm_usbg_drop_nexus(struct usbg_tpg
*);
1434 static void usbg_drop_tpg(struct se_portal_group
*se_tpg
)
1436 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1437 struct usbg_tpg
, se_tpg
);
1439 struct f_tcm_opts
*opts
;
1441 tcm_usbg_drop_nexus(tpg
);
1442 core_tpg_deregister(se_tpg
);
1443 destroy_workqueue(tpg
->workqueue
);
1445 mutex_lock(&tpg_instances_lock
);
1446 for (i
= 0; i
< TPG_INSTANCES
; ++i
)
1447 if (tpg_instances
[i
].tpg
== tpg
)
1449 if (i
< TPG_INSTANCES
) {
1450 tpg_instances
[i
].tpg
= NULL
;
1451 opts
= container_of(tpg_instances
[i
].func_inst
,
1452 struct f_tcm_opts
, func_inst
);
1453 mutex_lock(&opts
->dep_lock
);
1455 module_put(opts
->dependent
);
1457 configfs_undepend_item_unlocked(
1458 &opts
->func_inst
.group
.cg_item
);
1459 mutex_unlock(&opts
->dep_lock
);
1461 mutex_unlock(&tpg_instances_lock
);
1466 static struct se_wwn
*usbg_make_tport(
1467 struct target_fabric_configfs
*tf
,
1468 struct config_group
*group
,
1471 struct usbg_tport
*tport
;
1472 const char *wnn_name
;
1475 wnn_name
= usbg_check_wwn(name
);
1477 return ERR_PTR(-EINVAL
);
1479 tport
= kzalloc(sizeof(struct usbg_tport
), GFP_KERNEL
);
1481 return ERR_PTR(-ENOMEM
);
1483 tport
->tport_wwpn
= wwpn
;
1484 snprintf(tport
->tport_name
, sizeof(tport
->tport_name
), "%s", wnn_name
);
1485 return &tport
->tport_wwn
;
1488 static void usbg_drop_tport(struct se_wwn
*wwn
)
1490 struct usbg_tport
*tport
= container_of(wwn
,
1491 struct usbg_tport
, tport_wwn
);
1496 * If somebody feels like dropping the version property, go ahead.
1498 static ssize_t
usbg_wwn_version_show(struct config_item
*item
, char *page
)
1500 return sprintf(page
, "usb-gadget fabric module\n");
1503 CONFIGFS_ATTR_RO(usbg_wwn_
, version
);
1505 static struct configfs_attribute
*usbg_wwn_attrs
[] = {
1506 &usbg_wwn_attr_version
,
1510 static ssize_t
tcm_usbg_tpg_enable_show(struct config_item
*item
, char *page
)
1512 struct se_portal_group
*se_tpg
= to_tpg(item
);
1513 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1515 return snprintf(page
, PAGE_SIZE
, "%u\n", tpg
->gadget_connect
);
1518 static int usbg_attach(struct usbg_tpg
*);
1519 static void usbg_detach(struct usbg_tpg
*);
1521 static ssize_t
tcm_usbg_tpg_enable_store(struct config_item
*item
,
1522 const char *page
, size_t count
)
1524 struct se_portal_group
*se_tpg
= to_tpg(item
);
1525 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1529 ret
= strtobool(page
, &op
);
1533 if ((op
&& tpg
->gadget_connect
) || (!op
&& !tpg
->gadget_connect
))
1537 ret
= usbg_attach(tpg
);
1543 tpg
->gadget_connect
= op
;
1548 static ssize_t
tcm_usbg_tpg_nexus_show(struct config_item
*item
, char *page
)
1550 struct se_portal_group
*se_tpg
= to_tpg(item
);
1551 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1552 struct tcm_usbg_nexus
*tv_nexus
;
1555 mutex_lock(&tpg
->tpg_mutex
);
1556 tv_nexus
= tpg
->tpg_nexus
;
1561 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1562 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1564 mutex_unlock(&tpg
->tpg_mutex
);
1568 static int usbg_alloc_sess_cb(struct se_portal_group
*se_tpg
,
1569 struct se_session
*se_sess
, void *p
)
1571 struct usbg_tpg
*tpg
= container_of(se_tpg
,
1572 struct usbg_tpg
, se_tpg
);
1578 static int tcm_usbg_make_nexus(struct usbg_tpg
*tpg
, char *name
)
1580 struct tcm_usbg_nexus
*tv_nexus
;
1583 mutex_lock(&tpg
->tpg_mutex
);
1584 if (tpg
->tpg_nexus
) {
1586 pr_debug("tpg->tpg_nexus already exists\n");
1590 tv_nexus
= kzalloc(sizeof(*tv_nexus
), GFP_KERNEL
);
1596 tv_nexus
->tvn_se_sess
= target_alloc_session(&tpg
->se_tpg
,
1597 USB_G_DEFAULT_SESSION_TAGS
,
1598 sizeof(struct usbg_cmd
),
1599 TARGET_PROT_NORMAL
, name
,
1600 tv_nexus
, usbg_alloc_sess_cb
);
1601 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1602 #define MAKE_NEXUS_MSG "core_tpg_check_initiator_node_acl() failed for %s\n"
1603 pr_debug(MAKE_NEXUS_MSG
, name
);
1604 #undef MAKE_NEXUS_MSG
1605 ret
= PTR_ERR(tv_nexus
->tvn_se_sess
);
1610 mutex_unlock(&tpg
->tpg_mutex
);
1614 static int tcm_usbg_drop_nexus(struct usbg_tpg
*tpg
)
1616 struct se_session
*se_sess
;
1617 struct tcm_usbg_nexus
*tv_nexus
;
1620 mutex_lock(&tpg
->tpg_mutex
);
1621 tv_nexus
= tpg
->tpg_nexus
;
1625 se_sess
= tv_nexus
->tvn_se_sess
;
1629 if (atomic_read(&tpg
->tpg_port_count
)) {
1631 #define MSG "Unable to remove Host I_T Nexus with active TPG port count: %d\n"
1632 pr_err(MSG
, atomic_read(&tpg
->tpg_port_count
));
1637 pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
1638 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1640 * Release the SCSI I_T Nexus to the emulated vHost Target Port
1642 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1643 tpg
->tpg_nexus
= NULL
;
1648 mutex_unlock(&tpg
->tpg_mutex
);
1652 static ssize_t
tcm_usbg_tpg_nexus_store(struct config_item
*item
,
1653 const char *page
, size_t count
)
1655 struct se_portal_group
*se_tpg
= to_tpg(item
);
1656 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1657 unsigned char i_port
[USBG_NAMELEN
], *ptr
;
1660 if (!strncmp(page
, "NULL", 4)) {
1661 ret
= tcm_usbg_drop_nexus(tpg
);
1662 return (!ret
) ? count
: ret
;
1664 if (strlen(page
) >= USBG_NAMELEN
) {
1666 #define NEXUS_STORE_MSG "Emulated NAA Sas Address: %s, exceeds max: %d\n"
1667 pr_err(NEXUS_STORE_MSG
, page
, USBG_NAMELEN
);
1668 #undef NEXUS_STORE_MSG
1671 snprintf(i_port
, USBG_NAMELEN
, "%s", page
);
1673 ptr
= strstr(i_port
, "naa.");
1675 pr_err("Missing 'naa.' prefix\n");
1679 if (i_port
[strlen(i_port
) - 1] == '\n')
1680 i_port
[strlen(i_port
) - 1] = '\0';
1682 ret
= tcm_usbg_make_nexus(tpg
, &i_port
[0]);
1688 CONFIGFS_ATTR(tcm_usbg_tpg_
, enable
);
1689 CONFIGFS_ATTR(tcm_usbg_tpg_
, nexus
);
1691 static struct configfs_attribute
*usbg_base_attrs
[] = {
1692 &tcm_usbg_tpg_attr_enable
,
1693 &tcm_usbg_tpg_attr_nexus
,
1697 static int usbg_port_link(struct se_portal_group
*se_tpg
, struct se_lun
*lun
)
1699 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1701 atomic_inc(&tpg
->tpg_port_count
);
1702 smp_mb__after_atomic();
1706 static void usbg_port_unlink(struct se_portal_group
*se_tpg
,
1707 struct se_lun
*se_lun
)
1709 struct usbg_tpg
*tpg
= container_of(se_tpg
, struct usbg_tpg
, se_tpg
);
1711 atomic_dec(&tpg
->tpg_port_count
);
1712 smp_mb__after_atomic();
1715 static int usbg_check_stop_free(struct se_cmd
*se_cmd
)
1717 return target_put_sess_cmd(se_cmd
);
1720 static const struct target_core_fabric_ops usbg_ops
= {
1721 .module
= THIS_MODULE
,
1722 .name
= "usb_gadget",
1723 .get_fabric_name
= usbg_get_fabric_name
,
1724 .tpg_get_wwn
= usbg_get_fabric_wwn
,
1725 .tpg_get_tag
= usbg_get_tag
,
1726 .tpg_check_demo_mode
= usbg_check_true
,
1727 .tpg_check_demo_mode_cache
= usbg_check_false
,
1728 .tpg_check_demo_mode_write_protect
= usbg_check_false
,
1729 .tpg_check_prod_mode_write_protect
= usbg_check_false
,
1730 .tpg_get_inst_index
= usbg_tpg_get_inst_index
,
1731 .release_cmd
= usbg_release_cmd
,
1732 .sess_get_index
= usbg_sess_get_index
,
1733 .sess_get_initiator_sid
= NULL
,
1734 .write_pending
= usbg_send_write_request
,
1735 .write_pending_status
= usbg_write_pending_status
,
1736 .set_default_node_attributes
= usbg_set_default_node_attrs
,
1737 .get_cmd_state
= usbg_get_cmd_state
,
1738 .queue_data_in
= usbg_send_read_response
,
1739 .queue_status
= usbg_send_status_response
,
1740 .queue_tm_rsp
= usbg_queue_tm_rsp
,
1741 .aborted_task
= usbg_aborted_task
,
1742 .check_stop_free
= usbg_check_stop_free
,
1744 .fabric_make_wwn
= usbg_make_tport
,
1745 .fabric_drop_wwn
= usbg_drop_tport
,
1746 .fabric_make_tpg
= usbg_make_tpg
,
1747 .fabric_drop_tpg
= usbg_drop_tpg
,
1748 .fabric_post_link
= usbg_port_link
,
1749 .fabric_pre_unlink
= usbg_port_unlink
,
1750 .fabric_init_nodeacl
= usbg_init_nodeacl
,
1752 .tfc_wwn_attrs
= usbg_wwn_attrs
,
1753 .tfc_tpg_base_attrs
= usbg_base_attrs
,
1756 /* Start gadget.c code */
1758 static struct usb_interface_descriptor bot_intf_desc
= {
1759 .bLength
= sizeof(bot_intf_desc
),
1760 .bDescriptorType
= USB_DT_INTERFACE
,
1762 .bAlternateSetting
= USB_G_ALT_INT_BBB
,
1763 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
1764 .bInterfaceSubClass
= USB_SC_SCSI
,
1765 .bInterfaceProtocol
= USB_PR_BULK
,
1768 static struct usb_interface_descriptor uasp_intf_desc
= {
1769 .bLength
= sizeof(uasp_intf_desc
),
1770 .bDescriptorType
= USB_DT_INTERFACE
,
1772 .bAlternateSetting
= USB_G_ALT_INT_UAS
,
1773 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
1774 .bInterfaceSubClass
= USB_SC_SCSI
,
1775 .bInterfaceProtocol
= USB_PR_UAS
,
1778 static struct usb_endpoint_descriptor uasp_bi_desc
= {
1779 .bLength
= USB_DT_ENDPOINT_SIZE
,
1780 .bDescriptorType
= USB_DT_ENDPOINT
,
1781 .bEndpointAddress
= USB_DIR_IN
,
1782 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1783 .wMaxPacketSize
= cpu_to_le16(512),
1786 static struct usb_endpoint_descriptor uasp_fs_bi_desc
= {
1787 .bLength
= USB_DT_ENDPOINT_SIZE
,
1788 .bDescriptorType
= USB_DT_ENDPOINT
,
1789 .bEndpointAddress
= USB_DIR_IN
,
1790 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1793 static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc
= {
1794 .bLength
= sizeof(uasp_bi_pipe_desc
),
1795 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1796 .bPipeID
= DATA_IN_PIPE_ID
,
1799 static struct usb_endpoint_descriptor uasp_ss_bi_desc
= {
1800 .bLength
= USB_DT_ENDPOINT_SIZE
,
1801 .bDescriptorType
= USB_DT_ENDPOINT
,
1802 .bEndpointAddress
= USB_DIR_IN
,
1803 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1804 .wMaxPacketSize
= cpu_to_le16(1024),
1807 static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc
= {
1808 .bLength
= sizeof(uasp_bi_ep_comp_desc
),
1809 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1811 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
1812 .wBytesPerInterval
= 0,
1815 static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc
= {
1816 .bLength
= sizeof(bot_bi_ep_comp_desc
),
1817 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1821 static struct usb_endpoint_descriptor uasp_bo_desc
= {
1822 .bLength
= USB_DT_ENDPOINT_SIZE
,
1823 .bDescriptorType
= USB_DT_ENDPOINT
,
1824 .bEndpointAddress
= USB_DIR_OUT
,
1825 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1826 .wMaxPacketSize
= cpu_to_le16(512),
1829 static struct usb_endpoint_descriptor uasp_fs_bo_desc
= {
1830 .bLength
= USB_DT_ENDPOINT_SIZE
,
1831 .bDescriptorType
= USB_DT_ENDPOINT
,
1832 .bEndpointAddress
= USB_DIR_OUT
,
1833 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1836 static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc
= {
1837 .bLength
= sizeof(uasp_bo_pipe_desc
),
1838 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1839 .bPipeID
= DATA_OUT_PIPE_ID
,
1842 static struct usb_endpoint_descriptor uasp_ss_bo_desc
= {
1843 .bLength
= USB_DT_ENDPOINT_SIZE
,
1844 .bDescriptorType
= USB_DT_ENDPOINT
,
1845 .bEndpointAddress
= USB_DIR_OUT
,
1846 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1847 .wMaxPacketSize
= cpu_to_le16(0x400),
1850 static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc
= {
1851 .bLength
= sizeof(uasp_bo_ep_comp_desc
),
1852 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1853 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
1856 static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc
= {
1857 .bLength
= sizeof(bot_bo_ep_comp_desc
),
1858 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1861 static struct usb_endpoint_descriptor uasp_status_desc
= {
1862 .bLength
= USB_DT_ENDPOINT_SIZE
,
1863 .bDescriptorType
= USB_DT_ENDPOINT
,
1864 .bEndpointAddress
= USB_DIR_IN
,
1865 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1866 .wMaxPacketSize
= cpu_to_le16(512),
1869 static struct usb_endpoint_descriptor uasp_fs_status_desc
= {
1870 .bLength
= USB_DT_ENDPOINT_SIZE
,
1871 .bDescriptorType
= USB_DT_ENDPOINT
,
1872 .bEndpointAddress
= USB_DIR_IN
,
1873 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1876 static struct usb_pipe_usage_descriptor uasp_status_pipe_desc
= {
1877 .bLength
= sizeof(uasp_status_pipe_desc
),
1878 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1879 .bPipeID
= STATUS_PIPE_ID
,
1882 static struct usb_endpoint_descriptor uasp_ss_status_desc
= {
1883 .bLength
= USB_DT_ENDPOINT_SIZE
,
1884 .bDescriptorType
= USB_DT_ENDPOINT
,
1885 .bEndpointAddress
= USB_DIR_IN
,
1886 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1887 .wMaxPacketSize
= cpu_to_le16(1024),
1890 static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc
= {
1891 .bLength
= sizeof(uasp_status_in_ep_comp_desc
),
1892 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1893 .bmAttributes
= UASP_SS_EP_COMP_LOG_STREAMS
,
1896 static struct usb_endpoint_descriptor uasp_cmd_desc
= {
1897 .bLength
= USB_DT_ENDPOINT_SIZE
,
1898 .bDescriptorType
= USB_DT_ENDPOINT
,
1899 .bEndpointAddress
= USB_DIR_OUT
,
1900 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1901 .wMaxPacketSize
= cpu_to_le16(512),
1904 static struct usb_endpoint_descriptor uasp_fs_cmd_desc
= {
1905 .bLength
= USB_DT_ENDPOINT_SIZE
,
1906 .bDescriptorType
= USB_DT_ENDPOINT
,
1907 .bEndpointAddress
= USB_DIR_OUT
,
1908 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1911 static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc
= {
1912 .bLength
= sizeof(uasp_cmd_pipe_desc
),
1913 .bDescriptorType
= USB_DT_PIPE_USAGE
,
1914 .bPipeID
= CMD_PIPE_ID
,
1917 static struct usb_endpoint_descriptor uasp_ss_cmd_desc
= {
1918 .bLength
= USB_DT_ENDPOINT_SIZE
,
1919 .bDescriptorType
= USB_DT_ENDPOINT
,
1920 .bEndpointAddress
= USB_DIR_OUT
,
1921 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1922 .wMaxPacketSize
= cpu_to_le16(1024),
1925 static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc
= {
1926 .bLength
= sizeof(uasp_cmd_comp_desc
),
1927 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
1930 static struct usb_descriptor_header
*uasp_fs_function_desc
[] = {
1931 (struct usb_descriptor_header
*) &bot_intf_desc
,
1932 (struct usb_descriptor_header
*) &uasp_fs_bi_desc
,
1933 (struct usb_descriptor_header
*) &uasp_fs_bo_desc
,
1935 (struct usb_descriptor_header
*) &uasp_intf_desc
,
1936 (struct usb_descriptor_header
*) &uasp_fs_bi_desc
,
1937 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
1938 (struct usb_descriptor_header
*) &uasp_fs_bo_desc
,
1939 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
1940 (struct usb_descriptor_header
*) &uasp_fs_status_desc
,
1941 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
1942 (struct usb_descriptor_header
*) &uasp_fs_cmd_desc
,
1943 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
1947 static struct usb_descriptor_header
*uasp_hs_function_desc
[] = {
1948 (struct usb_descriptor_header
*) &bot_intf_desc
,
1949 (struct usb_descriptor_header
*) &uasp_bi_desc
,
1950 (struct usb_descriptor_header
*) &uasp_bo_desc
,
1952 (struct usb_descriptor_header
*) &uasp_intf_desc
,
1953 (struct usb_descriptor_header
*) &uasp_bi_desc
,
1954 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
1955 (struct usb_descriptor_header
*) &uasp_bo_desc
,
1956 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
1957 (struct usb_descriptor_header
*) &uasp_status_desc
,
1958 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
1959 (struct usb_descriptor_header
*) &uasp_cmd_desc
,
1960 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
1964 static struct usb_descriptor_header
*uasp_ss_function_desc
[] = {
1965 (struct usb_descriptor_header
*) &bot_intf_desc
,
1966 (struct usb_descriptor_header
*) &uasp_ss_bi_desc
,
1967 (struct usb_descriptor_header
*) &bot_bi_ep_comp_desc
,
1968 (struct usb_descriptor_header
*) &uasp_ss_bo_desc
,
1969 (struct usb_descriptor_header
*) &bot_bo_ep_comp_desc
,
1971 (struct usb_descriptor_header
*) &uasp_intf_desc
,
1972 (struct usb_descriptor_header
*) &uasp_ss_bi_desc
,
1973 (struct usb_descriptor_header
*) &uasp_bi_ep_comp_desc
,
1974 (struct usb_descriptor_header
*) &uasp_bi_pipe_desc
,
1975 (struct usb_descriptor_header
*) &uasp_ss_bo_desc
,
1976 (struct usb_descriptor_header
*) &uasp_bo_ep_comp_desc
,
1977 (struct usb_descriptor_header
*) &uasp_bo_pipe_desc
,
1978 (struct usb_descriptor_header
*) &uasp_ss_status_desc
,
1979 (struct usb_descriptor_header
*) &uasp_status_in_ep_comp_desc
,
1980 (struct usb_descriptor_header
*) &uasp_status_pipe_desc
,
1981 (struct usb_descriptor_header
*) &uasp_ss_cmd_desc
,
1982 (struct usb_descriptor_header
*) &uasp_cmd_comp_desc
,
1983 (struct usb_descriptor_header
*) &uasp_cmd_pipe_desc
,
1987 static struct usb_string tcm_us_strings
[] = {
1988 [USB_G_STR_INT_UAS
].s
= "USB Attached SCSI",
1989 [USB_G_STR_INT_BBB
].s
= "Bulk Only Transport",
1993 static struct usb_gadget_strings tcm_stringtab
= {
1995 .strings
= tcm_us_strings
,
1998 static struct usb_gadget_strings
*tcm_strings
[] = {
2003 static int tcm_bind(struct usb_configuration
*c
, struct usb_function
*f
)
2005 struct f_uas
*fu
= to_f_uas(f
);
2006 struct usb_string
*us
;
2007 struct usb_gadget
*gadget
= c
->cdev
->gadget
;
2009 struct f_tcm_opts
*opts
;
2013 opts
= container_of(f
->fi
, struct f_tcm_opts
, func_inst
);
2015 mutex_lock(&opts
->dep_lock
);
2016 if (!opts
->can_attach
) {
2017 mutex_unlock(&opts
->dep_lock
);
2020 mutex_unlock(&opts
->dep_lock
);
2021 us
= usb_gstrings_attach(c
->cdev
, tcm_strings
,
2022 ARRAY_SIZE(tcm_us_strings
));
2025 bot_intf_desc
.iInterface
= us
[USB_G_STR_INT_BBB
].id
;
2026 uasp_intf_desc
.iInterface
= us
[USB_G_STR_INT_UAS
].id
;
2028 iface
= usb_interface_id(c
, f
);
2032 bot_intf_desc
.bInterfaceNumber
= iface
;
2033 uasp_intf_desc
.bInterfaceNumber
= iface
;
2035 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_bi_desc
,
2036 &uasp_bi_ep_comp_desc
);
2042 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_bo_desc
,
2043 &uasp_bo_ep_comp_desc
);
2048 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_status_desc
,
2049 &uasp_status_in_ep_comp_desc
);
2054 ep
= usb_ep_autoconfig_ss(gadget
, &uasp_ss_cmd_desc
,
2055 &uasp_cmd_comp_desc
);
2060 /* Assume endpoint addresses are the same for both speeds */
2061 uasp_bi_desc
.bEndpointAddress
= uasp_ss_bi_desc
.bEndpointAddress
;
2062 uasp_bo_desc
.bEndpointAddress
= uasp_ss_bo_desc
.bEndpointAddress
;
2063 uasp_status_desc
.bEndpointAddress
=
2064 uasp_ss_status_desc
.bEndpointAddress
;
2065 uasp_cmd_desc
.bEndpointAddress
= uasp_ss_cmd_desc
.bEndpointAddress
;
2067 uasp_fs_bi_desc
.bEndpointAddress
= uasp_ss_bi_desc
.bEndpointAddress
;
2068 uasp_fs_bo_desc
.bEndpointAddress
= uasp_ss_bo_desc
.bEndpointAddress
;
2069 uasp_fs_status_desc
.bEndpointAddress
=
2070 uasp_ss_status_desc
.bEndpointAddress
;
2071 uasp_fs_cmd_desc
.bEndpointAddress
= uasp_ss_cmd_desc
.bEndpointAddress
;
2073 ret
= usb_assign_descriptors(f
, uasp_fs_function_desc
,
2074 uasp_hs_function_desc
, uasp_ss_function_desc
, NULL
);
2080 pr_err("Can't claim all required eps\n");
2085 struct guas_setup_wq
{
2086 struct work_struct work
;
2091 static void tcm_delayed_set_alt(struct work_struct
*wq
)
2093 struct guas_setup_wq
*work
= container_of(wq
, struct guas_setup_wq
,
2095 struct f_uas
*fu
= work
->fu
;
2096 int alt
= work
->alt
;
2100 if (fu
->flags
& USBG_IS_BOT
)
2101 bot_cleanup_old_alt(fu
);
2102 if (fu
->flags
& USBG_IS_UAS
)
2103 uasp_cleanup_old_alt(fu
);
2105 if (alt
== USB_G_ALT_INT_BBB
)
2107 else if (alt
== USB_G_ALT_INT_UAS
)
2109 usb_composite_setup_continue(fu
->function
.config
->cdev
);
2112 static int tcm_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
2114 struct f_uas
*fu
= to_f_uas(f
);
2116 if ((alt
== USB_G_ALT_INT_BBB
) || (alt
== USB_G_ALT_INT_UAS
)) {
2117 struct guas_setup_wq
*work
;
2119 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
2122 INIT_WORK(&work
->work
, tcm_delayed_set_alt
);
2125 schedule_work(&work
->work
);
2126 return USB_GADGET_DELAYED_STATUS
;
2131 static void tcm_disable(struct usb_function
*f
)
2133 struct f_uas
*fu
= to_f_uas(f
);
2135 if (fu
->flags
& USBG_IS_UAS
)
2136 uasp_cleanup_old_alt(fu
);
2137 else if (fu
->flags
& USBG_IS_BOT
)
2138 bot_cleanup_old_alt(fu
);
2142 static int tcm_setup(struct usb_function
*f
,
2143 const struct usb_ctrlrequest
*ctrl
)
2145 struct f_uas
*fu
= to_f_uas(f
);
2147 if (!(fu
->flags
& USBG_IS_BOT
))
2150 return usbg_bot_setup(f
, ctrl
);
2153 static inline struct f_tcm_opts
*to_f_tcm_opts(struct config_item
*item
)
2155 return container_of(to_config_group(item
), struct f_tcm_opts
,
2159 static void tcm_attr_release(struct config_item
*item
)
2161 struct f_tcm_opts
*opts
= to_f_tcm_opts(item
);
2163 usb_put_function_instance(&opts
->func_inst
);
2166 static struct configfs_item_operations tcm_item_ops
= {
2167 .release
= tcm_attr_release
,
2170 static const struct config_item_type tcm_func_type
= {
2171 .ct_item_ops
= &tcm_item_ops
,
2172 .ct_owner
= THIS_MODULE
,
2175 static void tcm_free_inst(struct usb_function_instance
*f
)
2177 struct f_tcm_opts
*opts
;
2180 opts
= container_of(f
, struct f_tcm_opts
, func_inst
);
2182 mutex_lock(&tpg_instances_lock
);
2183 for (i
= 0; i
< TPG_INSTANCES
; ++i
)
2184 if (tpg_instances
[i
].func_inst
== f
)
2186 if (i
< TPG_INSTANCES
)
2187 tpg_instances
[i
].func_inst
= NULL
;
2188 mutex_unlock(&tpg_instances_lock
);
2193 static int tcm_register_callback(struct usb_function_instance
*f
)
2195 struct f_tcm_opts
*opts
= container_of(f
, struct f_tcm_opts
, func_inst
);
2197 mutex_lock(&opts
->dep_lock
);
2198 opts
->can_attach
= true;
2199 mutex_unlock(&opts
->dep_lock
);
2204 static void tcm_unregister_callback(struct usb_function_instance
*f
)
2206 struct f_tcm_opts
*opts
= container_of(f
, struct f_tcm_opts
, func_inst
);
2208 mutex_lock(&opts
->dep_lock
);
2209 unregister_gadget_item(opts
->
2210 func_inst
.group
.cg_item
.ci_parent
->ci_parent
);
2211 opts
->can_attach
= false;
2212 mutex_unlock(&opts
->dep_lock
);
2215 static int usbg_attach(struct usbg_tpg
*tpg
)
2217 struct usb_function_instance
*f
= tpg
->fi
;
2218 struct f_tcm_opts
*opts
= container_of(f
, struct f_tcm_opts
, func_inst
);
2220 if (opts
->tcm_register_callback
)
2221 return opts
->tcm_register_callback(f
);
2226 static void usbg_detach(struct usbg_tpg
*tpg
)
2228 struct usb_function_instance
*f
= tpg
->fi
;
2229 struct f_tcm_opts
*opts
= container_of(f
, struct f_tcm_opts
, func_inst
);
2231 if (opts
->tcm_unregister_callback
)
2232 opts
->tcm_unregister_callback(f
);
2235 static int tcm_set_name(struct usb_function_instance
*f
, const char *name
)
2237 struct f_tcm_opts
*opts
= container_of(f
, struct f_tcm_opts
, func_inst
);
2239 pr_debug("tcm: Activating %s\n", name
);
2241 mutex_lock(&opts
->dep_lock
);
2243 mutex_unlock(&opts
->dep_lock
);
2248 static struct usb_function_instance
*tcm_alloc_inst(void)
2250 struct f_tcm_opts
*opts
;
2254 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
2256 return ERR_PTR(-ENOMEM
);
2258 mutex_lock(&tpg_instances_lock
);
2259 for (i
= 0; i
< TPG_INSTANCES
; ++i
)
2260 if (!tpg_instances
[i
].func_inst
)
2263 if (i
== TPG_INSTANCES
) {
2264 mutex_unlock(&tpg_instances_lock
);
2266 return ERR_PTR(-EBUSY
);
2268 tpg_instances
[i
].func_inst
= &opts
->func_inst
;
2269 mutex_unlock(&tpg_instances_lock
);
2271 mutex_init(&opts
->dep_lock
);
2272 opts
->func_inst
.set_inst_name
= tcm_set_name
;
2273 opts
->func_inst
.free_func_inst
= tcm_free_inst
;
2274 opts
->tcm_register_callback
= tcm_register_callback
;
2275 opts
->tcm_unregister_callback
= tcm_unregister_callback
;
2277 config_group_init_type_name(&opts
->func_inst
.group
, "",
2280 return &opts
->func_inst
;
2283 static void tcm_free(struct usb_function
*f
)
2285 struct f_uas
*tcm
= to_f_uas(f
);
2290 static void tcm_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
2292 usb_free_all_descriptors(f
);
2295 static struct usb_function
*tcm_alloc(struct usb_function_instance
*fi
)
2300 mutex_lock(&tpg_instances_lock
);
2301 for (i
= 0; i
< TPG_INSTANCES
; ++i
)
2302 if (tpg_instances
[i
].func_inst
== fi
)
2304 if (i
== TPG_INSTANCES
) {
2305 mutex_unlock(&tpg_instances_lock
);
2306 return ERR_PTR(-ENODEV
);
2309 fu
= kzalloc(sizeof(*fu
), GFP_KERNEL
);
2311 mutex_unlock(&tpg_instances_lock
);
2312 return ERR_PTR(-ENOMEM
);
2315 fu
->function
.name
= "Target Function";
2316 fu
->function
.bind
= tcm_bind
;
2317 fu
->function
.unbind
= tcm_unbind
;
2318 fu
->function
.set_alt
= tcm_set_alt
;
2319 fu
->function
.setup
= tcm_setup
;
2320 fu
->function
.disable
= tcm_disable
;
2321 fu
->function
.free_func
= tcm_free
;
2322 fu
->tpg
= tpg_instances
[i
].tpg
;
2323 mutex_unlock(&tpg_instances_lock
);
2325 return &fu
->function
;
2328 DECLARE_USB_FUNCTION(tcm
, tcm_alloc_inst
, tcm_alloc
);
2330 static int tcm_init(void)
2334 ret
= usb_function_register(&tcmusb_func
);
2338 ret
= target_register_template(&usbg_ops
);
2340 usb_function_unregister(&tcmusb_func
);
2344 module_init(tcm_init
);
2346 static void tcm_exit(void)
2348 target_unregister_template(&usbg_ops
);
2349 usb_function_unregister(&tcmusb_func
);
2351 module_exit(tcm_exit
);
2353 MODULE_LICENSE("GPL");
2354 MODULE_AUTHOR("Sebastian Andrzej Siewior");