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