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