1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "usbip_common.h"
21#include "stub.h"
22
23
24static void stub_free_priv_and_urb(struct stub_priv *priv)
25{
26 struct urb *urb = priv->urb;
27
28 kfree(urb->setup_packet);
29 kfree(urb->transfer_buffer);
30 list_del(&priv->list);
31 kmem_cache_free(stub_priv_cache, priv);
32 usb_free_urb(urb);
33}
34
35
36void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
37 __u32 status)
38{
39 struct stub_unlink *unlink;
40
41 unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
42 if (!unlink) {
43 dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
44 usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
45 return;
46 }
47
48 unlink->seqnum = seqnum;
49 unlink->status = status;
50
51 list_add_tail(&unlink->list, &sdev->unlink_tx);
52}
53
54
55
56
57
58
59
60
61
62
63void stub_complete(struct urb *urb)
64{
65 struct stub_priv *priv = (struct stub_priv *) urb->context;
66 struct stub_device *sdev = priv->sdev;
67 unsigned long flags;
68
69 usbip_dbg_stub_tx("complete! status %d\n", urb->status);
70
71
72 switch (urb->status) {
73 case 0:
74
75 break;
76 case -ENOENT:
77 usbip_uinfo("stopped by a call of usb_kill_urb() because of"
78 "cleaning up a virtual connection\n");
79 return;
80 case -ECONNRESET:
81 usbip_uinfo("unlinked by a call of usb_unlink_urb()\n");
82 break;
83 case -EPIPE:
84 usbip_uinfo("endpoint %d is stalled\n",
85 usb_pipeendpoint(urb->pipe));
86 break;
87 case -ESHUTDOWN:
88 usbip_uinfo("device removed?\n");
89 break;
90 default:
91 usbip_uinfo("urb completion with non-zero status %d\n",
92 urb->status);
93 }
94
95
96 spin_lock_irqsave(&sdev->priv_lock, flags);
97
98 if (priv->unlinking) {
99 stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
100 stub_free_priv_and_urb(priv);
101 } else
102 list_move_tail(&priv->list, &sdev->priv_tx);
103
104
105 spin_unlock_irqrestore(&sdev->priv_lock, flags);
106
107
108 wake_up(&sdev->tx_waitq);
109}
110
111
112
113
114
115static inline void setup_base_pdu(struct usbip_header_basic *base,
116 __u32 command, __u32 seqnum)
117{
118 base->command = command;
119 base->seqnum = seqnum;
120 base->devid = 0;
121 base->ep = 0;
122 base->direction = 0;
123}
124
125static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
126{
127 struct stub_priv *priv = (struct stub_priv *) urb->context;
128
129 setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
130
131 usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
132}
133
134static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
135 struct stub_unlink *unlink)
136{
137 setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
138
139 rpdu->u.ret_unlink.status = unlink->status;
140}
141
142
143
144
145
146static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
147{
148 unsigned long flags;
149 struct stub_priv *priv, *tmp;
150
151 spin_lock_irqsave(&sdev->priv_lock, flags);
152
153 list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
154 list_move_tail(&priv->list, &sdev->priv_free);
155 spin_unlock_irqrestore(&sdev->priv_lock, flags);
156 return priv;
157 }
158
159 spin_unlock_irqrestore(&sdev->priv_lock, flags);
160
161 return NULL;
162}
163
164static int stub_send_ret_submit(struct stub_device *sdev)
165{
166 unsigned long flags;
167 struct stub_priv *priv, *tmp;
168
169 struct msghdr msg;
170 struct kvec iov[3];
171 size_t txsize;
172
173 size_t total_size = 0;
174
175 while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
176 int ret;
177 struct urb *urb = priv->urb;
178 struct usbip_header pdu_header;
179 void *iso_buffer = NULL;
180
181 txsize = 0;
182 memset(&pdu_header, 0, sizeof(pdu_header));
183 memset(&msg, 0, sizeof(msg));
184 memset(&iov, 0, sizeof(iov));
185
186 usbip_dbg_stub_tx("setup txdata urb %p\n", urb);
187
188
189
190 setup_ret_submit_pdu(&pdu_header, urb);
191 usbip_header_correct_endian(&pdu_header, 1);
192
193 iov[0].iov_base = &pdu_header;
194 iov[0].iov_len = sizeof(pdu_header);
195 txsize += sizeof(pdu_header);
196
197
198 if (usb_pipein(urb->pipe) && urb->actual_length > 0) {
199 iov[1].iov_base = urb->transfer_buffer;
200 iov[1].iov_len = urb->actual_length;
201 txsize += urb->actual_length;
202 }
203
204
205 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
206 ssize_t len = 0;
207
208 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
209 if (!iso_buffer) {
210 usbip_event_add(&sdev->ud,
211 SDEV_EVENT_ERROR_MALLOC);
212 return -1;
213 }
214
215 iov[2].iov_base = iso_buffer;
216 iov[2].iov_len = len;
217 txsize += len;
218 }
219
220 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
221 3, txsize);
222 if (ret != txsize) {
223 dev_err(&sdev->interface->dev,
224 "sendmsg failed!, retval %d for %zd\n",
225 ret, txsize);
226 kfree(iso_buffer);
227 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
228 return -1;
229 }
230
231 kfree(iso_buffer);
232 usbip_dbg_stub_tx("send txdata\n");
233
234 total_size += txsize;
235 }
236
237
238 spin_lock_irqsave(&sdev->priv_lock, flags);
239
240 list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
241 stub_free_priv_and_urb(priv);
242 }
243
244 spin_unlock_irqrestore(&sdev->priv_lock, flags);
245
246 return total_size;
247}
248
249
250
251
252
253static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
254{
255 unsigned long flags;
256 struct stub_unlink *unlink, *tmp;
257
258 spin_lock_irqsave(&sdev->priv_lock, flags);
259
260 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
261 list_move_tail(&unlink->list, &sdev->unlink_free);
262 spin_unlock_irqrestore(&sdev->priv_lock, flags);
263 return unlink;
264 }
265
266 spin_unlock_irqrestore(&sdev->priv_lock, flags);
267
268 return NULL;
269}
270
271
272static int stub_send_ret_unlink(struct stub_device *sdev)
273{
274 unsigned long flags;
275 struct stub_unlink *unlink, *tmp;
276
277 struct msghdr msg;
278 struct kvec iov[1];
279 size_t txsize;
280
281 size_t total_size = 0;
282
283 while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
284 int ret;
285 struct usbip_header pdu_header;
286
287 txsize = 0;
288 memset(&pdu_header, 0, sizeof(pdu_header));
289 memset(&msg, 0, sizeof(msg));
290 memset(&iov, 0, sizeof(iov));
291
292 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
293
294
295 setup_ret_unlink_pdu(&pdu_header, unlink);
296 usbip_header_correct_endian(&pdu_header, 1);
297
298 iov[0].iov_base = &pdu_header;
299 iov[0].iov_len = sizeof(pdu_header);
300 txsize += sizeof(pdu_header);
301
302 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
303 1, txsize);
304 if (ret != txsize) {
305 dev_err(&sdev->interface->dev,
306 "sendmsg failed!, retval %d for %zd\n",
307 ret, txsize);
308 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
309 return -1;
310 }
311
312
313 usbip_dbg_stub_tx("send txdata\n");
314
315 total_size += txsize;
316 }
317
318
319 spin_lock_irqsave(&sdev->priv_lock, flags);
320
321 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
322 list_del(&unlink->list);
323 kfree(unlink);
324 }
325
326 spin_unlock_irqrestore(&sdev->priv_lock, flags);
327
328 return total_size;
329}
330
331
332
333
334void stub_tx_loop(struct usbip_task *ut)
335{
336 struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
337 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
338
339 while (1) {
340 if (signal_pending(current)) {
341 usbip_dbg_stub_tx("signal catched\n");
342 break;
343 }
344
345 if (usbip_event_happened(ud))
346 break;
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362 if (stub_send_ret_submit(sdev) < 0)
363 break;
364
365 if (stub_send_ret_unlink(sdev) < 0)
366 break;
367
368 wait_event_interruptible(sdev->tx_waitq,
369 (!list_empty(&sdev->priv_tx) ||
370 !list_empty(&sdev->unlink_tx)));
371 }
372}
373