]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/usbip/usbip_common.c
Merge tag 'char-misc-4.15-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/gregk...
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / usbip / usbip_common.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
05a1f28e
TH
2/*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
c7af4c22
IK
4 * Copyright (C) 2015-2016 Samsung Electronics
5 * Krzysztof Opasiak <k.opasiak@samsung.com>
05a1f28e
TH
6 */
7
7aaacb43 8#include <asm/byteorder.h>
05a1f28e 9#include <linux/file.h>
7aaacb43 10#include <linux/fs.h>
11#include <linux/kernel.h>
5a0e3ad6 12#include <linux/slab.h>
93efc55b 13#include <linux/stat.h>
45296236 14#include <linux/module.h>
93efc55b 15#include <linux/moduleparam.h>
7aaacb43 16#include <net/sock.h>
4ce0a41f 17
05a1f28e
TH
18#include "usbip_common.h"
19
4ce0a41f 20#define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
64e62426 21#define DRIVER_DESC "USB/IP Core"
05a1f28e 22
64e62426 23#ifdef CONFIG_USBIP_DEBUG
05a1f28e
TH
24unsigned long usbip_debug_flag = 0xffffffff;
25#else
26unsigned long usbip_debug_flag;
27#endif
28EXPORT_SYMBOL_GPL(usbip_debug_flag);
93efc55b
TK
29module_param(usbip_debug_flag, ulong, S_IRUGO|S_IWUSR);
30MODULE_PARM_DESC(usbip_debug_flag, "debug flags (defined in usbip_common.h)");
05a1f28e 31
05a1f28e
TH
32/* FIXME */
33struct device_attribute dev_attr_usbip_debug;
34EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
35
b1f56aca
GKH
36static ssize_t usbip_debug_show(struct device *dev,
37 struct device_attribute *attr, char *buf)
05a1f28e
TH
38{
39 return sprintf(buf, "%lx\n", usbip_debug_flag);
40}
41
b1f56aca
GKH
42static ssize_t usbip_debug_store(struct device *dev,
43 struct device_attribute *attr, const char *buf,
44 size_t count)
05a1f28e 45{
bf988c16
JG
46 if (sscanf(buf, "%lx", &usbip_debug_flag) != 1)
47 return -EINVAL;
05a1f28e
TH
48 return count;
49}
b1f56aca 50DEVICE_ATTR_RW(usbip_debug);
05a1f28e
TH
51
52static void usbip_dump_buffer(char *buff, int bufflen)
53{
1a4b6f66 54 print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4,
aad86577 55 buff, bufflen, false);
05a1f28e
TH
56}
57
58static void usbip_dump_pipe(unsigned int p)
59{
60 unsigned char type = usb_pipetype(p);
87352760 61 unsigned char ep = usb_pipeendpoint(p);
62 unsigned char dev = usb_pipedevice(p);
63 unsigned char dir = usb_pipein(p);
05a1f28e 64
1a4b6f66 65 pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
05a1f28e
TH
66
67 switch (type) {
68 case PIPE_ISOCHRONOUS:
1a4b6f66 69 pr_debug("ISO\n");
05a1f28e
TH
70 break;
71 case PIPE_INTERRUPT:
1a4b6f66 72 pr_debug("INT\n");
05a1f28e
TH
73 break;
74 case PIPE_CONTROL:
1a4b6f66 75 pr_debug("CTRL\n");
05a1f28e
TH
76 break;
77 case PIPE_BULK:
1a4b6f66 78 pr_debug("BULK\n");
05a1f28e
TH
79 break;
80 default:
1a4b6f66 81 pr_debug("ERR\n");
49aecefc 82 break;
05a1f28e 83 }
05a1f28e
TH
84}
85
86static void usbip_dump_usb_device(struct usb_device *udev)
87{
88 struct device *dev = &udev->dev;
89 int i;
90
09c8c8fb
SK
91 dev_dbg(dev, " devnum(%d) devpath(%s) usb speed(%s)",
92 udev->devnum, udev->devpath, usb_speed_string(udev->speed));
05a1f28e 93
1a4b6f66 94 pr_debug("tt %p, ttport %d\n", udev->tt, udev->ttport);
05a1f28e
TH
95
96 dev_dbg(dev, " ");
97 for (i = 0; i < 16; i++)
1a4b6f66 98 pr_debug(" %2u", i);
99 pr_debug("\n");
05a1f28e
TH
100
101 dev_dbg(dev, " toggle0(IN) :");
102 for (i = 0; i < 16; i++)
1a4b6f66 103 pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
104 pr_debug("\n");
05a1f28e
TH
105
106 dev_dbg(dev, " toggle1(OUT):");
107 for (i = 0; i < 16; i++)
1a4b6f66 108 pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
109 pr_debug("\n");
05a1f28e
TH
110
111 dev_dbg(dev, " epmaxp_in :");
112 for (i = 0; i < 16; i++) {
113 if (udev->ep_in[i])
1a4b6f66 114 pr_debug(" %2u",
115 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
05a1f28e 116 }
1a4b6f66 117 pr_debug("\n");
05a1f28e
TH
118
119 dev_dbg(dev, " epmaxp_out :");
120 for (i = 0; i < 16; i++) {
121 if (udev->ep_out[i])
1a4b6f66 122 pr_debug(" %2u",
123 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
05a1f28e 124 }
1a4b6f66 125 pr_debug("\n");
05a1f28e
TH
126
127 dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
128
b482da2b
HS
129 dev_dbg(dev,
130 "descriptor %p, config %p, actconfig %p, rawdescriptors %p\n",
131 &udev->descriptor, udev->config,
05a1f28e
TH
132 udev->actconfig, udev->rawdescriptors);
133
134 dev_dbg(dev, "have_langid %d, string_langid %d\n",
135 udev->have_langid, udev->string_langid);
136
ff823c79 137 dev_dbg(dev, "maxchild %d\n", udev->maxchild);
05a1f28e
TH
138}
139
140static void usbip_dump_request_type(__u8 rt)
141{
142 switch (rt & USB_RECIP_MASK) {
143 case USB_RECIP_DEVICE:
1a4b6f66 144 pr_debug("DEVICE");
05a1f28e
TH
145 break;
146 case USB_RECIP_INTERFACE:
1a4b6f66 147 pr_debug("INTERF");
05a1f28e
TH
148 break;
149 case USB_RECIP_ENDPOINT:
1a4b6f66 150 pr_debug("ENDPOI");
05a1f28e
TH
151 break;
152 case USB_RECIP_OTHER:
1a4b6f66 153 pr_debug("OTHER ");
05a1f28e
TH
154 break;
155 default:
1a4b6f66 156 pr_debug("------");
49aecefc 157 break;
05a1f28e
TH
158 }
159}
160
161static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
162{
163 if (!cmd) {
1a4b6f66 164 pr_debug(" : null pointer\n");
05a1f28e
TH
165 return;
166 }
167
1a4b6f66 168 pr_debug(" ");
6bb3ee69
CC
169 pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
170 cmd->bRequestType, cmd->bRequest,
1a4b6f66 171 cmd->wValue, cmd->wIndex, cmd->wLength);
172 pr_debug("\n ");
05a1f28e
TH
173
174 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1a4b6f66 175 pr_debug("STANDARD ");
05a1f28e
TH
176 switch (cmd->bRequest) {
177 case USB_REQ_GET_STATUS:
1a4b6f66 178 pr_debug("GET_STATUS\n");
05a1f28e
TH
179 break;
180 case USB_REQ_CLEAR_FEATURE:
1a4b6f66 181 pr_debug("CLEAR_FEAT\n");
05a1f28e
TH
182 break;
183 case USB_REQ_SET_FEATURE:
5ad7b85b 184 pr_debug("SET_FEAT\n");
05a1f28e
TH
185 break;
186 case USB_REQ_SET_ADDRESS:
1a4b6f66 187 pr_debug("SET_ADDRRS\n");
05a1f28e
TH
188 break;
189 case USB_REQ_GET_DESCRIPTOR:
1a4b6f66 190 pr_debug("GET_DESCRI\n");
05a1f28e
TH
191 break;
192 case USB_REQ_SET_DESCRIPTOR:
1a4b6f66 193 pr_debug("SET_DESCRI\n");
05a1f28e
TH
194 break;
195 case USB_REQ_GET_CONFIGURATION:
1a4b6f66 196 pr_debug("GET_CONFIG\n");
05a1f28e
TH
197 break;
198 case USB_REQ_SET_CONFIGURATION:
1a4b6f66 199 pr_debug("SET_CONFIG\n");
05a1f28e
TH
200 break;
201 case USB_REQ_GET_INTERFACE:
1a4b6f66 202 pr_debug("GET_INTERF\n");
05a1f28e
TH
203 break;
204 case USB_REQ_SET_INTERFACE:
1a4b6f66 205 pr_debug("SET_INTERF\n");
05a1f28e
TH
206 break;
207 case USB_REQ_SYNCH_FRAME:
1a4b6f66 208 pr_debug("SYNC_FRAME\n");
05a1f28e
TH
209 break;
210 default:
5ad7b85b 211 pr_debug("REQ(%02X)\n", cmd->bRequest);
49aecefc 212 break;
05a1f28e 213 }
05a1f28e 214 usbip_dump_request_type(cmd->bRequestType);
1a4b6f66 215 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
5ad7b85b 216 pr_debug("CLASS\n");
1a4b6f66 217 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
5ad7b85b 218 pr_debug("VENDOR\n");
1a4b6f66 219 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
220 pr_debug("RESERVED\n");
221 }
05a1f28e
TH
222}
223
224void usbip_dump_urb(struct urb *urb)
225{
226 struct device *dev;
227
228 if (!urb) {
1a4b6f66 229 pr_debug("urb: null pointer!!\n");
05a1f28e
TH
230 return;
231 }
232
233 if (!urb->dev) {
1a4b6f66 234 pr_debug("urb->dev: null pointer!!\n");
05a1f28e
TH
235 return;
236 }
1a4b6f66 237
05a1f28e
TH
238 dev = &urb->dev->dev;
239
240 dev_dbg(dev, " urb :%p\n", urb);
241 dev_dbg(dev, " dev :%p\n", urb->dev);
242
243 usbip_dump_usb_device(urb->dev);
244
245 dev_dbg(dev, " pipe :%08x ", urb->pipe);
246
247 usbip_dump_pipe(urb->pipe);
248
249 dev_dbg(dev, " status :%d\n", urb->status);
250 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
251 dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
b8868e45
BM
252 dev_dbg(dev, " transfer_buffer_length:%d\n",
253 urb->transfer_buffer_length);
05a1f28e
TH
254 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
255 dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
256
257 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
f9eacc98 258 usbip_dump_usb_ctrlrequest(
05a1f28e
TH
259 (struct usb_ctrlrequest *)urb->setup_packet);
260
261 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
262 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
263 dev_dbg(dev, " interval :%d\n", urb->interval);
264 dev_dbg(dev, " error_count :%d\n", urb->error_count);
265 dev_dbg(dev, " context :%p\n", urb->context);
266 dev_dbg(dev, " complete :%p\n", urb->complete);
267}
268EXPORT_SYMBOL_GPL(usbip_dump_urb);
269
270void usbip_dump_header(struct usbip_header *pdu)
271{
1a4b6f66 272 pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
273 pdu->base.command,
274 pdu->base.seqnum,
275 pdu->base.devid,
276 pdu->base.direction,
277 pdu->base.ep);
05a1f28e
TH
278
279 switch (pdu->base.command) {
280 case USBIP_CMD_SUBMIT:
6bb3ee69 281 pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n",
1a4b6f66 282 pdu->u.cmd_submit.transfer_flags,
283 pdu->u.cmd_submit.transfer_buffer_length,
284 pdu->u.cmd_submit.start_frame,
285 pdu->u.cmd_submit.number_of_packets,
286 pdu->u.cmd_submit.interval);
f9eacc98 287 break;
05a1f28e 288 case USBIP_CMD_UNLINK:
1a4b6f66 289 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
290 pdu->u.cmd_unlink.seqnum);
05a1f28e
TH
291 break;
292 case USBIP_RET_SUBMIT:
1a4b6f66 293 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
294 pdu->u.ret_submit.status,
295 pdu->u.ret_submit.actual_length,
296 pdu->u.ret_submit.start_frame,
297 pdu->u.ret_submit.number_of_packets,
298 pdu->u.ret_submit.error_count);
299 break;
05a1f28e 300 case USBIP_RET_UNLINK:
1a4b6f66 301 pr_debug("USBIP_RET_UNLINK: status %d\n",
302 pdu->u.ret_unlink.status);
05a1f28e
TH
303 break;
304 default:
305 /* NOT REACHED */
1a4b6f66 306 pr_err("unknown command\n");
49aecefc 307 break;
05a1f28e
TH
308 }
309}
310EXPORT_SYMBOL_GPL(usbip_dump_header);
311
5a08c526
BW
312/* Receive data over TCP/IP. */
313int usbip_recv(struct socket *sock, void *buf, int size)
05a1f28e
TH
314{
315 int result;
3995d161
AV
316 struct kvec iov = {.iov_base = buf, .iov_len = size};
317 struct msghdr msg = {.msg_flags = MSG_NOSIGNAL};
05a1f28e
TH
318 int total = 0;
319
90120d15
SK
320 if (!sock || !buf || !size)
321 return -EINVAL;
322
3995d161 323 iov_iter_kvec(&msg.msg_iter, READ|ITER_KVEC, &iov, 1, size);
05a1f28e 324
b8868e45 325 usbip_dbg_xmit("enter\n");
05a1f28e 326
05a1f28e 327 do {
90120d15 328 msg_data_left(&msg);
05a1f28e 329 sock->sk->sk_allocation = GFP_NOIO;
3995d161
AV
330
331 result = sock_recvmsg(sock, &msg, MSG_WAITALL);
90120d15 332 if (result <= 0)
05a1f28e 333 goto err;
05a1f28e 334
05a1f28e 335 total += result;
3995d161 336 } while (msg_data_left(&msg));
05a1f28e 337
b8868e45 338 if (usbip_dbg_flag_xmit) {
5a08c526
BW
339 if (!in_interrupt())
340 pr_debug("%-10s:", current->comm);
341 else
342 pr_debug("interrupt :");
05a1f28e 343
5a08c526 344 pr_debug("receiving....\n");
3995d161
AV
345 usbip_dump_buffer(buf, size);
346 pr_debug("received, osize %d ret %d size %zd total %d\n",
347 size, result, msg_data_left(&msg), total);
05a1f28e
TH
348 }
349
350 return total;
351
352err:
353 return result;
354}
5a08c526 355EXPORT_SYMBOL_GPL(usbip_recv);
05a1f28e 356
05a1f28e
TH
357/* there may be more cases to tweak the flags. */
358static unsigned int tweak_transfer_flags(unsigned int flags)
359{
85bcb5ee 360 flags &= ~URB_NO_TRANSFER_DMA_MAP;
05a1f28e
TH
361 return flags;
362}
363
364static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
f9eacc98 365 int pack)
05a1f28e
TH
366{
367 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
368
369 /*
370 * Some members are not still implemented in usbip. I hope this issue
371 * will be discussed when usbip is ported to other operating systems.
372 */
373 if (pack) {
05a1f28e 374 spdu->transfer_flags =
f9eacc98 375 tweak_transfer_flags(urb->transfer_flags);
05a1f28e
TH
376 spdu->transfer_buffer_length = urb->transfer_buffer_length;
377 spdu->start_frame = urb->start_frame;
378 spdu->number_of_packets = urb->number_of_packets;
379 spdu->interval = urb->interval;
380 } else {
05a1f28e 381 urb->transfer_flags = spdu->transfer_flags;
05a1f28e
TH
382 urb->transfer_buffer_length = spdu->transfer_buffer_length;
383 urb->start_frame = spdu->start_frame;
384 urb->number_of_packets = spdu->number_of_packets;
385 urb->interval = spdu->interval;
386 }
387}
388
389static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
f9eacc98 390 int pack)
05a1f28e
TH
391{
392 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
393
394 if (pack) {
05a1f28e
TH
395 rpdu->status = urb->status;
396 rpdu->actual_length = urb->actual_length;
397 rpdu->start_frame = urb->start_frame;
1325f85f 398 rpdu->number_of_packets = urb->number_of_packets;
05a1f28e
TH
399 rpdu->error_count = urb->error_count;
400 } else {
05a1f28e
TH
401 urb->status = rpdu->status;
402 urb->actual_length = rpdu->actual_length;
403 urb->start_frame = rpdu->start_frame;
1325f85f 404 urb->number_of_packets = rpdu->number_of_packets;
05a1f28e
TH
405 urb->error_count = rpdu->error_count;
406 }
407}
408
05a1f28e 409void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
f9eacc98 410 int pack)
05a1f28e
TH
411{
412 switch (cmd) {
413 case USBIP_CMD_SUBMIT:
414 usbip_pack_cmd_submit(pdu, urb, pack);
415 break;
416 case USBIP_RET_SUBMIT:
417 usbip_pack_ret_submit(pdu, urb, pack);
418 break;
419 default:
49aecefc 420 /* NOT REACHED */
1a4b6f66 421 pr_err("unknown command\n");
49aecefc 422 break;
05a1f28e
TH
423 }
424}
425EXPORT_SYMBOL_GPL(usbip_pack_pdu);
426
05a1f28e
TH
427static void correct_endian_basic(struct usbip_header_basic *base, int send)
428{
429 if (send) {
430 base->command = cpu_to_be32(base->command);
431 base->seqnum = cpu_to_be32(base->seqnum);
432 base->devid = cpu_to_be32(base->devid);
433 base->direction = cpu_to_be32(base->direction);
434 base->ep = cpu_to_be32(base->ep);
435 } else {
436 base->command = be32_to_cpu(base->command);
437 base->seqnum = be32_to_cpu(base->seqnum);
438 base->devid = be32_to_cpu(base->devid);
439 base->direction = be32_to_cpu(base->direction);
440 base->ep = be32_to_cpu(base->ep);
441 }
442}
443
444static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
f9eacc98 445 int send)
05a1f28e
TH
446{
447 if (send) {
448 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
449
450 cpu_to_be32s(&pdu->transfer_buffer_length);
451 cpu_to_be32s(&pdu->start_frame);
452 cpu_to_be32s(&pdu->number_of_packets);
453 cpu_to_be32s(&pdu->interval);
454 } else {
455 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
456
457 be32_to_cpus(&pdu->transfer_buffer_length);
458 be32_to_cpus(&pdu->start_frame);
459 be32_to_cpus(&pdu->number_of_packets);
460 be32_to_cpus(&pdu->interval);
461 }
462}
463
464static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
f9eacc98 465 int send)
05a1f28e
TH
466{
467 if (send) {
468 cpu_to_be32s(&pdu->status);
469 cpu_to_be32s(&pdu->actual_length);
470 cpu_to_be32s(&pdu->start_frame);
1325f85f 471 cpu_to_be32s(&pdu->number_of_packets);
05a1f28e
TH
472 cpu_to_be32s(&pdu->error_count);
473 } else {
474 be32_to_cpus(&pdu->status);
475 be32_to_cpus(&pdu->actual_length);
476 be32_to_cpus(&pdu->start_frame);
cacd18a8 477 be32_to_cpus(&pdu->number_of_packets);
05a1f28e
TH
478 be32_to_cpus(&pdu->error_count);
479 }
480}
481
482static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
f9eacc98 483 int send)
05a1f28e
TH
484{
485 if (send)
486 pdu->seqnum = cpu_to_be32(pdu->seqnum);
487 else
488 pdu->seqnum = be32_to_cpu(pdu->seqnum);
489}
490
491static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
f9eacc98 492 int send)
05a1f28e
TH
493{
494 if (send)
495 cpu_to_be32s(&pdu->status);
496 else
497 be32_to_cpus(&pdu->status);
498}
499
500void usbip_header_correct_endian(struct usbip_header *pdu, int send)
501{
502 __u32 cmd = 0;
503
504 if (send)
505 cmd = pdu->base.command;
506
507 correct_endian_basic(&pdu->base, send);
508
509 if (!send)
510 cmd = pdu->base.command;
511
512 switch (cmd) {
513 case USBIP_CMD_SUBMIT:
514 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
515 break;
516 case USBIP_RET_SUBMIT:
517 correct_endian_ret_submit(&pdu->u.ret_submit, send);
518 break;
519 case USBIP_CMD_UNLINK:
520 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
521 break;
522 case USBIP_RET_UNLINK:
523 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
524 break;
525 default:
49aecefc 526 /* NOT REACHED */
1a4b6f66 527 pr_err("unknown command\n");
49aecefc 528 break;
05a1f28e
TH
529 }
530}
531EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
532
2282e1fb 533static void usbip_iso_packet_correct_endian(
87352760 534 struct usbip_iso_packet_descriptor *iso, int send)
05a1f28e
TH
535{
536 /* does not need all members. but copy all simply. */
537 if (send) {
538 iso->offset = cpu_to_be32(iso->offset);
539 iso->length = cpu_to_be32(iso->length);
540 iso->status = cpu_to_be32(iso->status);
541 iso->actual_length = cpu_to_be32(iso->actual_length);
542 } else {
543 iso->offset = be32_to_cpu(iso->offset);
544 iso->length = be32_to_cpu(iso->length);
545 iso->status = be32_to_cpu(iso->status);
546 iso->actual_length = be32_to_cpu(iso->actual_length);
547 }
548}
549
550static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
f9eacc98 551 struct usb_iso_packet_descriptor *uiso, int pack)
05a1f28e
TH
552{
553 if (pack) {
554 iso->offset = uiso->offset;
555 iso->length = uiso->length;
556 iso->status = uiso->status;
557 iso->actual_length = uiso->actual_length;
558 } else {
559 uiso->offset = iso->offset;
560 uiso->length = iso->length;
561 uiso->status = iso->status;
562 uiso->actual_length = iso->actual_length;
563 }
564}
565
05a1f28e 566/* must free buffer */
36ac9b05
BW
567struct usbip_iso_packet_descriptor*
568usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
05a1f28e 569{
05a1f28e
TH
570 struct usbip_iso_packet_descriptor *iso;
571 int np = urb->number_of_packets;
572 ssize_t size = np * sizeof(*iso);
573 int i;
574
36ac9b05
BW
575 iso = kzalloc(size, GFP_KERNEL);
576 if (!iso)
05a1f28e
TH
577 return NULL;
578
579 for (i = 0; i < np; i++) {
36ac9b05
BW
580 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
581 usbip_iso_packet_correct_endian(&iso[i], 1);
05a1f28e
TH
582 }
583
584 *bufflen = size;
585
36ac9b05 586 return iso;
05a1f28e
TH
587}
588EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
589
590/* some members of urb must be substituted before. */
591int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
592{
593 void *buff;
594 struct usbip_iso_packet_descriptor *iso;
595 int np = urb->number_of_packets;
596 int size = np * sizeof(*iso);
597 int i;
598 int ret;
28276a28 599 int total_length = 0;
05a1f28e
TH
600
601 if (!usb_pipeisoc(urb->pipe))
602 return 0;
603
604 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
f14287b9 605 if (np == 0)
05a1f28e 606 return 0;
05a1f28e
TH
607
608 buff = kzalloc(size, GFP_KERNEL);
609 if (!buff)
610 return -ENOMEM;
611
5a08c526 612 ret = usbip_recv(ud->tcp_socket, buff, size);
05a1f28e
TH
613 if (ret != size) {
614 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
615 ret);
616 kfree(buff);
617
c7af4c22 618 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
05a1f28e
TH
619 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
620 else
621 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
622
623 return -EPIPE;
624 }
625
36ac9b05 626 iso = (struct usbip_iso_packet_descriptor *) buff;
05a1f28e 627 for (i = 0; i < np; i++) {
36ac9b05
BW
628 usbip_iso_packet_correct_endian(&iso[i], 0);
629 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
28276a28 630 total_length += urb->iso_frame_desc[i].actual_length;
05a1f28e
TH
631 }
632
05a1f28e
TH
633 kfree(buff);
634
28276a28
AM
635 if (total_length != urb->actual_length) {
636 dev_err(&urb->dev->dev,
6bb3ee69 637 "total length of iso packets %d not equal to actual length of buffer %d\n",
f9eacc98 638 total_length, urb->actual_length);
28276a28 639
c7af4c22 640 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
28276a28
AM
641 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
642 else
643 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
644
645 return -EPIPE;
646 }
647
05a1f28e
TH
648 return ret;
649}
650EXPORT_SYMBOL_GPL(usbip_recv_iso);
651
28276a28
AM
652/*
653 * This functions restores the padding which was removed for optimizing
654 * the bandwidth during transfer over tcp/ip
655 *
656 * buffer and iso packets need to be stored and be in propeper endian in urb
657 * before calling this function
658 */
ac2b41ac 659void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
28276a28
AM
660{
661 int np = urb->number_of_packets;
662 int i;
28276a28
AM
663 int actualoffset = urb->actual_length;
664
665 if (!usb_pipeisoc(urb->pipe))
ac2b41ac 666 return;
28276a28
AM
667
668 /* if no packets or length of data is 0, then nothing to unpack */
669 if (np == 0 || urb->actual_length == 0)
ac2b41ac 670 return;
28276a28
AM
671
672 /*
673 * if actual_length is transfer_buffer_length then no padding is
674 * present.
c7f00899 675 */
28276a28 676 if (urb->actual_length == urb->transfer_buffer_length)
ac2b41ac 677 return;
28276a28
AM
678
679 /*
9a284e5c 680 * loop over all packets from last to first (to prevent overwriting
28276a28
AM
681 * memory when padding) and move them into the proper place
682 */
683 for (i = np-1; i > 0; i--) {
684 actualoffset -= urb->iso_frame_desc[i].actual_length;
685 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
f9eacc98 686 urb->transfer_buffer + actualoffset,
687 urb->iso_frame_desc[i].actual_length);
28276a28 688 }
28276a28
AM
689}
690EXPORT_SYMBOL_GPL(usbip_pad_iso);
05a1f28e
TH
691
692/* some members of urb must be substituted before. */
693int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
694{
695 int ret;
696 int size;
697
c7af4c22 698 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
05a1f28e
TH
699 /* the direction of urb must be OUT. */
700 if (usb_pipein(urb->pipe))
701 return 0;
702
703 size = urb->transfer_buffer_length;
704 } else {
05a1f28e
TH
705 /* the direction of urb must be IN. */
706 if (usb_pipeout(urb->pipe))
707 return 0;
708
709 size = urb->actual_length;
710 }
711
712 /* no need to recv xbuff */
713 if (!(size > 0))
714 return 0;
715
b348d7dd
IK
716 if (size > urb->transfer_buffer_length) {
717 /* should not happen, probably malicious packet */
718 if (ud->side == USBIP_STUB) {
719 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
720 return 0;
721 } else {
722 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
723 return -EPIPE;
724 }
725 }
726
5a08c526 727 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
05a1f28e
TH
728 if (ret != size) {
729 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
c7af4c22 730 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
05a1f28e
TH
731 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
732 } else {
733 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
734 return -EPIPE;
735 }
736 }
737
738 return ret;
739}
740EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
741
3028d0ae 742static int __init usbip_core_init(void)
05a1f28e 743{
bb7871ad
NI
744 int ret;
745
bb7871ad
NI
746 ret = usbip_init_eh();
747 if (ret)
748 return ret;
749
05a1f28e
TH
750 return 0;
751}
752
3028d0ae 753static void __exit usbip_core_exit(void)
05a1f28e 754{
bb7871ad 755 usbip_finish_eh();
05a1f28e
TH
756 return;
757}
758
3028d0ae 759module_init(usbip_core_init);
760module_exit(usbip_core_exit);
05a1f28e
TH
761
762MODULE_AUTHOR(DRIVER_AUTHOR);
763MODULE_DESCRIPTION(DRIVER_DESC);
764MODULE_LICENSE("GPL");