2 * Copyright (C) 2015 Karol Kosik <karo9@interia.eu>
3 * Copyright (C) 2015-2016 Samsung Electronics
4 * Igor Kotrasinski <i.kotrasinsk@samsung.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/list.h>
22 #include <linux/kthread.h>
24 #include "usbip_common.h"
27 static inline void setup_base_pdu(struct usbip_header_basic
*base
,
28 __u32 command
, __u32 seqnum
)
30 base
->command
= command
;
31 base
->seqnum
= seqnum
;
37 static void setup_ret_submit_pdu(struct usbip_header
*rpdu
, struct urbp
*urb_p
)
39 setup_base_pdu(&rpdu
->base
, USBIP_RET_SUBMIT
, urb_p
->seqnum
);
40 usbip_pack_pdu(rpdu
, urb_p
->urb
, USBIP_RET_SUBMIT
, 1);
43 static void setup_ret_unlink_pdu(struct usbip_header
*rpdu
,
44 struct v_unlink
*unlink
)
46 setup_base_pdu(&rpdu
->base
, USBIP_RET_UNLINK
, unlink
->seqnum
);
47 rpdu
->u
.ret_unlink
.status
= unlink
->status
;
50 static int v_send_ret_unlink(struct vudc
*udc
, struct v_unlink
*unlink
)
57 struct usbip_header pdu_header
;
60 memset(&pdu_header
, 0, sizeof(pdu_header
));
61 memset(&msg
, 0, sizeof(msg
));
62 memset(&iov
, 0, sizeof(iov
));
64 /* 1. setup usbip_header */
65 setup_ret_unlink_pdu(&pdu_header
, unlink
);
66 usbip_header_correct_endian(&pdu_header
, 1);
68 iov
[0].iov_base
= &pdu_header
;
69 iov
[0].iov_len
= sizeof(pdu_header
);
70 txsize
+= sizeof(pdu_header
);
72 ret
= kernel_sendmsg(udc
->ud
.tcp_socket
, &msg
, iov
,
75 usbip_event_add(&udc
->ud
, VUDC_EVENT_ERROR_TCP
);
85 static int v_send_ret_submit(struct vudc
*udc
, struct urbp
*urb_p
)
87 struct urb
*urb
= urb_p
->urb
;
88 struct usbip_header pdu_header
;
89 struct usbip_iso_packet_descriptor
*iso_buffer
= NULL
;
90 struct kvec
*iov
= NULL
;
97 memset(&pdu_header
, 0, sizeof(pdu_header
));
98 memset(&msg
, 0, sizeof(msg
));
100 if (urb_p
->type
== USB_ENDPOINT_XFER_ISOC
)
101 iovnum
= 2 + urb
->number_of_packets
;
105 iov
= kcalloc(iovnum
, sizeof(*iov
), GFP_KERNEL
);
107 usbip_event_add(&udc
->ud
, VUDC_EVENT_ERROR_MALLOC
);
113 /* 1. setup usbip_header */
114 setup_ret_submit_pdu(&pdu_header
, urb_p
);
115 usbip_dbg_stub_tx("setup txdata seqnum: %d urb: %p\n",
116 pdu_header
.base
.seqnum
, urb
);
117 usbip_header_correct_endian(&pdu_header
, 1);
119 iov
[iovnum
].iov_base
= &pdu_header
;
120 iov
[iovnum
].iov_len
= sizeof(pdu_header
);
122 txsize
+= sizeof(pdu_header
);
124 /* 2. setup transfer buffer */
125 if (urb_p
->type
!= USB_ENDPOINT_XFER_ISOC
&&
126 usb_pipein(urb
->pipe
) && urb
->actual_length
> 0) {
127 iov
[iovnum
].iov_base
= urb
->transfer_buffer
;
128 iov
[iovnum
].iov_len
= urb
->actual_length
;
130 txsize
+= urb
->actual_length
;
131 } else if (urb_p
->type
== USB_ENDPOINT_XFER_ISOC
&&
132 usb_pipein(urb
->pipe
)) {
133 /* FIXME - copypasted from stub_tx, refactor */
136 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
137 iov
[iovnum
].iov_base
= urb
->transfer_buffer
+
138 urb
->iso_frame_desc
[i
].offset
;
139 iov
[iovnum
].iov_len
=
140 urb
->iso_frame_desc
[i
].actual_length
;
142 txsize
+= urb
->iso_frame_desc
[i
].actual_length
;
145 if (txsize
!= sizeof(pdu_header
) + urb
->actual_length
) {
146 usbip_event_add(&udc
->ud
, VUDC_EVENT_ERROR_TCP
);
151 /* else - no buffer to send */
153 /* 3. setup iso_packet_descriptor */
154 if (urb_p
->type
== USB_ENDPOINT_XFER_ISOC
) {
157 iso_buffer
= usbip_alloc_iso_desc_pdu(urb
, &len
);
159 usbip_event_add(&udc
->ud
,
160 VUDC_EVENT_ERROR_MALLOC
);
165 iov
[iovnum
].iov_base
= iso_buffer
;
166 iov
[iovnum
].iov_len
= len
;
171 ret
= kernel_sendmsg(udc
->ud
.tcp_socket
, &msg
,
172 iov
, iovnum
, txsize
);
174 usbip_event_add(&udc
->ud
, VUDC_EVENT_ERROR_TCP
);
183 free_urbp_and_urb(urb_p
);
189 static int v_send_ret(struct vudc
*udc
)
193 size_t total_size
= 0;
196 spin_lock_irqsave(&udc
->lock_tx
, flags
);
197 while (!list_empty(&udc
->tx_queue
)) {
198 txi
= list_first_entry(&udc
->tx_queue
, struct tx_item
,
200 list_del(&txi
->tx_entry
);
201 spin_unlock_irqrestore(&udc
->lock_tx
, flags
);
205 ret
= v_send_ret_submit(udc
, txi
->s
);
208 ret
= v_send_ret_unlink(udc
, txi
->u
);
218 spin_lock_irqsave(&udc
->lock_tx
, flags
);
221 spin_unlock_irqrestore(&udc
->lock_tx
, flags
);
226 int v_tx_loop(void *data
)
228 struct usbip_device
*ud
= (struct usbip_device
*) data
;
229 struct vudc
*udc
= container_of(ud
, struct vudc
, ud
);
232 while (!kthread_should_stop()) {
233 if (usbip_event_happened(&udc
->ud
))
235 ret
= v_send_ret(udc
);
237 pr_warn("v_tx exit with error %d", ret
);
240 wait_event_interruptible(udc
->tx_waitq
,
241 (!list_empty(&udc
->tx_queue
) ||
242 kthread_should_stop()));
248 /* called with spinlocks held */
249 void v_enqueue_ret_unlink(struct vudc
*udc
, __u32 seqnum
, __u32 status
)
252 struct v_unlink
*unlink
;
254 txi
= kzalloc(sizeof(*txi
), GFP_ATOMIC
);
256 usbip_event_add(&udc
->ud
, VDEV_EVENT_ERROR_MALLOC
);
259 unlink
= kzalloc(sizeof(*unlink
), GFP_ATOMIC
);
262 usbip_event_add(&udc
->ud
, VDEV_EVENT_ERROR_MALLOC
);
266 unlink
->seqnum
= seqnum
;
267 unlink
->status
= status
;
268 txi
->type
= TX_UNLINK
;
271 list_add_tail(&txi
->tx_entry
, &udc
->tx_queue
);
274 /* called with spinlocks held */
275 void v_enqueue_ret_submit(struct vudc
*udc
, struct urbp
*urb_p
)
279 txi
= kzalloc(sizeof(*txi
), GFP_ATOMIC
);
281 usbip_event_add(&udc
->ud
, VDEV_EVENT_ERROR_MALLOC
);
285 txi
->type
= TX_SUBMIT
;
288 list_add_tail(&txi
->tx_entry
, &udc
->tx_queue
);