mm-only debug patch...
[mmotm.git] / drivers / staging / usbip / usbip_common.c
blobddb6f5fd04d56b024aa9d9f3e1ebe6c0e66f1adb
1 /*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
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.
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.
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.
20 #include <linux/kernel.h>
21 #include <linux/smp_lock.h>
22 #include <linux/file.h>
23 #include <linux/tcp.h>
24 #include <linux/in.h>
25 #include <linux/kthread.h>
26 #include "usbip_common.h"
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"
33 /*-------------------------------------------------------------------------*/
34 /* debug routines */
36 #ifdef CONFIG_USB_DEBUG
37 unsigned long usbip_debug_flag = 0xffffffff;
38 #else
39 unsigned long usbip_debug_flag;
40 #endif
41 EXPORT_SYMBOL_GPL(usbip_debug_flag);
44 /* FIXME */
45 struct device_attribute dev_attr_usbip_debug;
46 EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
49 static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
50 char *buf)
52 return sprintf(buf, "%lx\n", usbip_debug_flag);
55 static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
56 const char *buf, size_t count)
58 unsigned long flag;
60 sscanf(buf, "%lx", &flag);
61 usbip_debug_flag = flag;
63 return count;
65 DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
67 static void usbip_dump_buffer(char *buff, int bufflen)
69 int i;
71 if (bufflen > 128) {
72 for (i = 0; i < 128; i++) {
73 if (i%24 == 0)
74 printk(KERN_DEBUG " ");
75 printk(KERN_DEBUG "%02x ", (unsigned char) buff[i]);
76 if (i%4 == 3)
77 printk(KERN_DEBUG "| ");
78 if (i%24 == 23)
79 printk(KERN_DEBUG "\n");
81 printk(KERN_DEBUG "... (%d byte)\n", bufflen);
82 return;
85 for (i = 0; i < bufflen; i++) {
86 if (i%24 == 0)
87 printk(KERN_DEBUG " ");
88 printk(KERN_DEBUG "%02x ", (unsigned char) buff[i]);
89 if (i%4 == 3)
90 printk(KERN_DEBUG "| ");
91 if (i%24 == 23)
92 printk(KERN_DEBUG "\n");
94 printk(KERN_DEBUG "\n");
98 static void usbip_dump_pipe(unsigned int p)
100 unsigned char type = usb_pipetype(p);
101 unsigned char ep = usb_pipeendpoint(p);
102 unsigned char dev = usb_pipedevice(p);
103 unsigned char dir = usb_pipein(p);
105 printk(KERN_DEBUG "dev(%d) ", dev);
106 printk(KERN_DEBUG "ep(%d) ", ep);
107 printk(KERN_DEBUG "%s ", dir ? "IN" : "OUT");
109 switch (type) {
110 case PIPE_ISOCHRONOUS:
111 printk(KERN_DEBUG "%s ", "ISO");
112 break;
113 case PIPE_INTERRUPT:
114 printk(KERN_DEBUG "%s ", "INT");
115 break;
116 case PIPE_CONTROL:
117 printk(KERN_DEBUG "%s ", "CTL");
118 break;
119 case PIPE_BULK:
120 printk(KERN_DEBUG "%s ", "BLK");
121 break;
122 default:
123 printk(KERN_DEBUG "ERR");
126 printk(KERN_DEBUG "\n");
130 static void usbip_dump_usb_device(struct usb_device *udev)
132 struct device *dev = &udev->dev;
133 int i;
135 dev_dbg(dev, " devnum(%d) devpath(%s)",
136 udev->devnum, udev->devpath);
138 switch (udev->speed) {
139 case USB_SPEED_HIGH:
140 printk(KERN_DEBUG " SPD_HIGH");
141 break;
142 case USB_SPEED_FULL:
143 printk(KERN_DEBUG " SPD_FULL");
144 break;
145 case USB_SPEED_LOW:
146 printk(KERN_DEBUG " SPD_LOW");
147 break;
148 case USB_SPEED_UNKNOWN:
149 printk(KERN_DEBUG " SPD_UNKNOWN");
150 break;
151 default:
152 printk(KERN_DEBUG " SPD_ERROR");
155 printk(KERN_DEBUG " tt %p, ttport %d", udev->tt, udev->ttport);
156 printk(KERN_DEBUG "\n");
158 dev_dbg(dev, " ");
159 for (i = 0; i < 16; i++)
160 printk(KERN_DEBUG " %2u", i);
161 printk(KERN_DEBUG "\n");
163 dev_dbg(dev, " toggle0(IN) :");
164 for (i = 0; i < 16; i++)
165 printk(KERN_DEBUG " %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
166 printk(KERN_DEBUG "\n");
168 dev_dbg(dev, " toggle1(OUT):");
169 for (i = 0; i < 16; i++)
170 printk(KERN_DEBUG " %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
171 printk(KERN_DEBUG "\n");
174 dev_dbg(dev, " epmaxp_in :");
175 for (i = 0; i < 16; i++) {
176 if (udev->ep_in[i])
177 printk(KERN_DEBUG " %2u",
178 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
180 printk(KERN_DEBUG "\n");
182 dev_dbg(dev, " epmaxp_out :");
183 for (i = 0; i < 16; i++) {
184 if (udev->ep_out[i])
185 printk(KERN_DEBUG " %2u",
186 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
188 printk(KERN_DEBUG "\n");
190 dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
192 dev_dbg(dev, "descriptor %p, config %p, actconfig %p, "
193 "rawdescriptors %p\n", &udev->descriptor, udev->config,
194 udev->actconfig, udev->rawdescriptors);
196 dev_dbg(dev, "have_langid %d, string_langid %d\n",
197 udev->have_langid, udev->string_langid);
199 dev_dbg(dev, "maxchild %d, children %p\n",
200 udev->maxchild, udev->children);
203 static void usbip_dump_request_type(__u8 rt)
205 switch (rt & USB_RECIP_MASK) {
206 case USB_RECIP_DEVICE:
207 printk(KERN_DEBUG "DEVICE");
208 break;
209 case USB_RECIP_INTERFACE:
210 printk(KERN_DEBUG "INTERF");
211 break;
212 case USB_RECIP_ENDPOINT:
213 printk(KERN_DEBUG "ENDPOI");
214 break;
215 case USB_RECIP_OTHER:
216 printk(KERN_DEBUG "OTHER ");
217 break;
218 default:
219 printk(KERN_DEBUG "------");
223 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
225 if (!cmd) {
226 printk(KERN_DEBUG " %s : null pointer\n", __func__);
227 return;
230 printk(KERN_DEBUG " ");
231 printk(KERN_DEBUG "bRequestType(%02X) ", cmd->bRequestType);
232 printk(KERN_DEBUG "bRequest(%02X) " , cmd->bRequest);
233 printk(KERN_DEBUG "wValue(%04X) ", cmd->wValue);
234 printk(KERN_DEBUG "wIndex(%04X) ", cmd->wIndex);
235 printk(KERN_DEBUG "wLength(%04X) ", cmd->wLength);
237 printk(KERN_DEBUG "\n ");
239 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
240 printk(KERN_DEBUG "STANDARD ");
241 switch (cmd->bRequest) {
242 case USB_REQ_GET_STATUS:
243 printk(KERN_DEBUG "GET_STATUS");
244 break;
245 case USB_REQ_CLEAR_FEATURE:
246 printk(KERN_DEBUG "CLEAR_FEAT");
247 break;
248 case USB_REQ_SET_FEATURE:
249 printk(KERN_DEBUG "SET_FEAT ");
250 break;
251 case USB_REQ_SET_ADDRESS:
252 printk(KERN_DEBUG "SET_ADDRRS");
253 break;
254 case USB_REQ_GET_DESCRIPTOR:
255 printk(KERN_DEBUG "GET_DESCRI");
256 break;
257 case USB_REQ_SET_DESCRIPTOR:
258 printk(KERN_DEBUG "SET_DESCRI");
259 break;
260 case USB_REQ_GET_CONFIGURATION:
261 printk(KERN_DEBUG "GET_CONFIG");
262 break;
263 case USB_REQ_SET_CONFIGURATION:
264 printk(KERN_DEBUG "SET_CONFIG");
265 break;
266 case USB_REQ_GET_INTERFACE:
267 printk(KERN_DEBUG "GET_INTERF");
268 break;
269 case USB_REQ_SET_INTERFACE:
270 printk(KERN_DEBUG "SET_INTERF");
271 break;
272 case USB_REQ_SYNCH_FRAME:
273 printk(KERN_DEBUG "SYNC_FRAME");
274 break;
275 default:
276 printk(KERN_DEBUG "REQ(%02X) ", cmd->bRequest);
279 printk(KERN_DEBUG " ");
280 usbip_dump_request_type(cmd->bRequestType);
282 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
283 printk(KERN_DEBUG "CLASS ");
285 else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)
286 printk(KERN_DEBUG "VENDOR ");
288 else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED)
289 printk(KERN_DEBUG "RESERVED");
291 printk(KERN_DEBUG "\n");
294 void usbip_dump_urb(struct urb *urb)
296 struct device *dev;
298 if (!urb) {
299 printk(KERN_DEBUG KBUILD_MODNAME
300 ":%s: urb: null pointer!!\n", __func__);
301 return;
304 if (!urb->dev) {
305 printk(KERN_DEBUG KBUILD_MODNAME
306 ":%s: urb->dev: null pointer!!\n", __func__);
307 return;
309 dev = &urb->dev->dev;
311 dev_dbg(dev, " urb :%p\n", urb);
312 dev_dbg(dev, " dev :%p\n", urb->dev);
314 usbip_dump_usb_device(urb->dev);
316 dev_dbg(dev, " pipe :%08x ", urb->pipe);
318 usbip_dump_pipe(urb->pipe);
320 dev_dbg(dev, " status :%d\n", urb->status);
321 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
322 dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
323 dev_dbg(dev, " transfer_buffer_length:%d\n",
324 urb->transfer_buffer_length);
325 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
326 dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
328 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
329 usbip_dump_usb_ctrlrequest(
330 (struct usb_ctrlrequest *)urb->setup_packet);
332 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
333 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
334 dev_dbg(dev, " interval :%d\n", urb->interval);
335 dev_dbg(dev, " error_count :%d\n", urb->error_count);
336 dev_dbg(dev, " context :%p\n", urb->context);
337 dev_dbg(dev, " complete :%p\n", urb->complete);
339 EXPORT_SYMBOL_GPL(usbip_dump_urb);
341 void usbip_dump_header(struct usbip_header *pdu)
343 usbip_udbg("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
344 pdu->base.command,
345 pdu->base.seqnum,
346 pdu->base.devid,
347 pdu->base.direction,
348 pdu->base.ep);
350 switch (pdu->base.command) {
351 case USBIP_CMD_SUBMIT:
352 usbip_udbg("CMD_SUBMIT: "
353 "x_flags %u x_len %u sf %u #p %u iv %u\n",
354 pdu->u.cmd_submit.transfer_flags,
355 pdu->u.cmd_submit.transfer_buffer_length,
356 pdu->u.cmd_submit.start_frame,
357 pdu->u.cmd_submit.number_of_packets,
358 pdu->u.cmd_submit.interval);
359 break;
360 case USBIP_CMD_UNLINK:
361 usbip_udbg("CMD_UNLINK: seq %u\n", pdu->u.cmd_unlink.seqnum);
362 break;
363 case USBIP_RET_SUBMIT:
364 usbip_udbg("RET_SUBMIT: st %d al %u sf %d ec %d\n",
365 pdu->u.ret_submit.status,
366 pdu->u.ret_submit.actual_length,
367 pdu->u.ret_submit.start_frame,
368 pdu->u.ret_submit.error_count);
369 case USBIP_RET_UNLINK:
370 usbip_udbg("RET_UNLINK: status %d\n", pdu->u.ret_unlink.status);
371 break;
372 default:
373 /* NOT REACHED */
374 usbip_udbg("UNKNOWN\n");
377 EXPORT_SYMBOL_GPL(usbip_dump_header);
380 /*-------------------------------------------------------------------------*/
381 /* thread routines */
383 int usbip_thread(void *param)
385 struct usbip_task *ut = param;
387 if (!ut)
388 return -EINVAL;
390 lock_kernel();
391 daemonize(ut->name);
392 allow_signal(SIGKILL);
393 ut->thread = current;
394 unlock_kernel();
396 /* srv.rb must wait for rx_thread starting */
397 complete(&ut->thread_done);
399 /* start of while loop */
400 ut->loop_ops(ut);
402 /* end of loop */
403 ut->thread = NULL;
405 complete_and_exit(&ut->thread_done, 0);
408 int usbip_start_threads(struct usbip_device *ud)
411 * threads are invoked per one device (per one connection).
413 struct task_struct *th;
415 th = kthread_run(usbip_thread, (void *)&ud->tcp_rx, "usbip");
416 if (IS_ERR(th)) {
417 printk(KERN_WARNING
418 "Unable to start control thread\n");
419 return PTR_ERR(th);
421 th = kthread_run(usbip_thread, (void *)&ud->tcp_tx, "usbip");
422 if (IS_ERR(th)) {
423 printk(KERN_WARNING
424 "Unable to start control thread\n");
425 return PTR_ERR(th);
428 /* confirm threads are starting */
429 wait_for_completion(&ud->tcp_rx.thread_done);
430 wait_for_completion(&ud->tcp_tx.thread_done);
431 return 0;
433 EXPORT_SYMBOL_GPL(usbip_start_threads);
435 void usbip_stop_threads(struct usbip_device *ud)
437 /* kill threads related to this sdev, if v.c. exists */
438 if (ud->tcp_rx.thread != NULL) {
439 send_sig(SIGKILL, ud->tcp_rx.thread, 1);
440 wait_for_completion(&ud->tcp_rx.thread_done);
441 usbip_udbg("rx_thread for ud %p has finished\n", ud);
444 if (ud->tcp_tx.thread != NULL) {
445 send_sig(SIGKILL, ud->tcp_tx.thread, 1);
446 wait_for_completion(&ud->tcp_tx.thread_done);
447 usbip_udbg("tx_thread for ud %p has finished\n", ud);
450 EXPORT_SYMBOL_GPL(usbip_stop_threads);
452 void usbip_task_init(struct usbip_task *ut, char *name,
453 void (*loop_ops)(struct usbip_task *))
455 ut->thread = NULL;
456 init_completion(&ut->thread_done);
457 ut->name = name;
458 ut->loop_ops = loop_ops;
460 EXPORT_SYMBOL_GPL(usbip_task_init);
463 /*-------------------------------------------------------------------------*/
464 /* socket routines */
466 /* Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
467 int usbip_xmit(int send, struct socket *sock, char *buf,
468 int size, int msg_flags)
470 int result;
471 struct msghdr msg;
472 struct kvec iov;
473 int total = 0;
475 /* for blocks of if (usbip_dbg_flag_xmit) */
476 char *bp = buf;
477 int osize = size;
479 usbip_dbg_xmit("enter\n");
481 if (!sock || !buf || !size) {
482 printk(KERN_ERR "%s: invalid arg, sock %p buff %p size %d\n",
483 __func__, sock, buf, size);
484 return -EINVAL;
488 if (usbip_dbg_flag_xmit) {
489 if (send) {
490 if (!in_interrupt())
491 printk(KERN_DEBUG "%-10s:", current->comm);
492 else
493 printk(KERN_DEBUG "interrupt :");
495 printk(KERN_DEBUG "%s: sending... , sock %p, buf %p, "
496 "size %d, msg_flags %d\n", __func__,
497 sock, buf, size, msg_flags);
498 usbip_dump_buffer(buf, size);
503 do {
504 sock->sk->sk_allocation = GFP_NOIO;
505 iov.iov_base = buf;
506 iov.iov_len = size;
507 msg.msg_name = NULL;
508 msg.msg_namelen = 0;
509 msg.msg_control = NULL;
510 msg.msg_controllen = 0;
511 msg.msg_namelen = 0;
512 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
514 if (send)
515 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
516 else
517 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
518 MSG_WAITALL);
520 if (result <= 0) {
521 usbip_udbg("usbip_xmit: %s sock %p buf %p size %u ret "
522 "%d total %d\n",
523 send ? "send" : "receive", sock, buf,
524 size, result, total);
525 goto err;
528 size -= result;
529 buf += result;
530 total += result;
532 } while (size > 0);
535 if (usbip_dbg_flag_xmit) {
536 if (!send) {
537 if (!in_interrupt())
538 printk(KERN_DEBUG "%-10s:", current->comm);
539 else
540 printk(KERN_DEBUG "interrupt :");
542 printk(KERN_DEBUG "usbip_xmit: receiving....\n");
543 usbip_dump_buffer(bp, osize);
544 printk(KERN_DEBUG "usbip_xmit: received, osize %d ret "
545 "%d size %d total %d\n", osize, result,
546 size, total);
549 if (send)
550 printk(KERN_DEBUG "usbip_xmit: send, total %d\n",
551 total);
554 return total;
556 err:
557 return result;
559 EXPORT_SYMBOL_GPL(usbip_xmit);
562 /* now a usrland utility should set options. */
563 #if 0
564 int setquickack(struct socket *socket)
566 mm_segment_t oldfs;
567 int val = 1;
568 int ret;
570 oldfs = get_fs();
571 set_fs(get_ds());
572 ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_QUICKACK,
573 (char __user *) &val, sizeof(ret));
574 set_fs(oldfs);
576 return ret;
579 int setnodelay(struct socket *socket)
581 mm_segment_t oldfs;
582 int val = 1;
583 int ret;
585 oldfs = get_fs();
586 set_fs(get_ds());
587 ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_NODELAY,
588 (char __user *) &val, sizeof(ret));
589 set_fs(oldfs);
591 return ret;
594 int setkeepalive(struct socket *socket)
596 mm_segment_t oldfs;
597 int val = 1;
598 int ret;
600 oldfs = get_fs();
601 set_fs(get_ds());
602 ret = socket->ops->setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE,
603 (char __user *) &val, sizeof(ret));
604 set_fs(oldfs);
606 return ret;
609 void setreuse(struct socket *socket)
611 socket->sk->sk_reuse = 1;
613 #endif
615 struct socket *sockfd_to_socket(unsigned int sockfd)
617 struct socket *socket;
618 struct file *file;
619 struct inode *inode;
621 file = fget(sockfd);
622 if (!file) {
623 printk(KERN_ERR "%s: invalid sockfd\n", __func__);
624 return NULL;
627 inode = file->f_dentry->d_inode;
629 if (!inode || !S_ISSOCK(inode->i_mode))
630 return NULL;
632 socket = SOCKET_I(inode);
634 return socket;
636 EXPORT_SYMBOL_GPL(sockfd_to_socket);
640 /*-------------------------------------------------------------------------*/
641 /* pdu routines */
643 /* there may be more cases to tweak the flags. */
644 static unsigned int tweak_transfer_flags(unsigned int flags)
646 flags &= ~(URB_NO_TRANSFER_DMA_MAP|URB_NO_SETUP_DMA_MAP);
647 return flags;
650 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
651 int pack)
653 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
656 * Some members are not still implemented in usbip. I hope this issue
657 * will be discussed when usbip is ported to other operating systems.
659 if (pack) {
660 /* vhci_tx.c */
661 spdu->transfer_flags =
662 tweak_transfer_flags(urb->transfer_flags);
663 spdu->transfer_buffer_length = urb->transfer_buffer_length;
664 spdu->start_frame = urb->start_frame;
665 spdu->number_of_packets = urb->number_of_packets;
666 spdu->interval = urb->interval;
667 } else {
668 /* stub_rx.c */
669 urb->transfer_flags = spdu->transfer_flags;
671 urb->transfer_buffer_length = spdu->transfer_buffer_length;
672 urb->start_frame = spdu->start_frame;
673 urb->number_of_packets = spdu->number_of_packets;
674 urb->interval = spdu->interval;
678 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
679 int pack)
681 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
683 if (pack) {
684 /* stub_tx.c */
686 rpdu->status = urb->status;
687 rpdu->actual_length = urb->actual_length;
688 rpdu->start_frame = urb->start_frame;
689 rpdu->error_count = urb->error_count;
690 } else {
691 /* vhci_rx.c */
693 urb->status = rpdu->status;
694 urb->actual_length = rpdu->actual_length;
695 urb->start_frame = rpdu->start_frame;
696 urb->error_count = rpdu->error_count;
701 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
702 int pack)
704 switch (cmd) {
705 case USBIP_CMD_SUBMIT:
706 usbip_pack_cmd_submit(pdu, urb, pack);
707 break;
708 case USBIP_RET_SUBMIT:
709 usbip_pack_ret_submit(pdu, urb, pack);
710 break;
711 default:
712 err("unknown command");
713 /* NOTREACHED */
714 /* BUG(); */
717 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
720 static void correct_endian_basic(struct usbip_header_basic *base, int send)
722 if (send) {
723 base->command = cpu_to_be32(base->command);
724 base->seqnum = cpu_to_be32(base->seqnum);
725 base->devid = cpu_to_be32(base->devid);
726 base->direction = cpu_to_be32(base->direction);
727 base->ep = cpu_to_be32(base->ep);
728 } else {
729 base->command = be32_to_cpu(base->command);
730 base->seqnum = be32_to_cpu(base->seqnum);
731 base->devid = be32_to_cpu(base->devid);
732 base->direction = be32_to_cpu(base->direction);
733 base->ep = be32_to_cpu(base->ep);
737 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
738 int send)
740 if (send) {
741 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
743 cpu_to_be32s(&pdu->transfer_buffer_length);
744 cpu_to_be32s(&pdu->start_frame);
745 cpu_to_be32s(&pdu->number_of_packets);
746 cpu_to_be32s(&pdu->interval);
747 } else {
748 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
750 be32_to_cpus(&pdu->transfer_buffer_length);
751 be32_to_cpus(&pdu->start_frame);
752 be32_to_cpus(&pdu->number_of_packets);
753 be32_to_cpus(&pdu->interval);
757 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
758 int send)
760 if (send) {
761 cpu_to_be32s(&pdu->status);
762 cpu_to_be32s(&pdu->actual_length);
763 cpu_to_be32s(&pdu->start_frame);
764 cpu_to_be32s(&pdu->error_count);
765 } else {
766 be32_to_cpus(&pdu->status);
767 be32_to_cpus(&pdu->actual_length);
768 be32_to_cpus(&pdu->start_frame);
769 be32_to_cpus(&pdu->error_count);
773 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
774 int send)
776 if (send)
777 pdu->seqnum = cpu_to_be32(pdu->seqnum);
778 else
779 pdu->seqnum = be32_to_cpu(pdu->seqnum);
782 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
783 int send)
785 if (send)
786 cpu_to_be32s(&pdu->status);
787 else
788 be32_to_cpus(&pdu->status);
791 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
793 __u32 cmd = 0;
795 if (send)
796 cmd = pdu->base.command;
798 correct_endian_basic(&pdu->base, send);
800 if (!send)
801 cmd = pdu->base.command;
803 switch (cmd) {
804 case USBIP_CMD_SUBMIT:
805 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
806 break;
807 case USBIP_RET_SUBMIT:
808 correct_endian_ret_submit(&pdu->u.ret_submit, send);
809 break;
810 case USBIP_CMD_UNLINK:
811 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
812 break;
813 case USBIP_RET_UNLINK:
814 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
815 break;
816 default:
817 /* NOTREACHED */
818 err("unknown command in pdu header: %d", cmd);
819 /* BUG(); */
822 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
824 static void usbip_iso_pakcet_correct_endian(
825 struct usbip_iso_packet_descriptor *iso,
826 int send)
828 /* does not need all members. but copy all simply. */
829 if (send) {
830 iso->offset = cpu_to_be32(iso->offset);
831 iso->length = cpu_to_be32(iso->length);
832 iso->status = cpu_to_be32(iso->status);
833 iso->actual_length = cpu_to_be32(iso->actual_length);
834 } else {
835 iso->offset = be32_to_cpu(iso->offset);
836 iso->length = be32_to_cpu(iso->length);
837 iso->status = be32_to_cpu(iso->status);
838 iso->actual_length = be32_to_cpu(iso->actual_length);
842 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
843 struct usb_iso_packet_descriptor *uiso, int pack)
845 if (pack) {
846 iso->offset = uiso->offset;
847 iso->length = uiso->length;
848 iso->status = uiso->status;
849 iso->actual_length = uiso->actual_length;
850 } else {
851 uiso->offset = iso->offset;
852 uiso->length = iso->length;
853 uiso->status = iso->status;
854 uiso->actual_length = iso->actual_length;
859 /* must free buffer */
860 void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
862 void *buff;
863 struct usbip_iso_packet_descriptor *iso;
864 int np = urb->number_of_packets;
865 ssize_t size = np * sizeof(*iso);
866 int i;
868 buff = kzalloc(size, GFP_KERNEL);
869 if (!buff)
870 return NULL;
872 for (i = 0; i < np; i++) {
873 iso = buff + (i * sizeof(*iso));
875 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
876 usbip_iso_pakcet_correct_endian(iso, 1);
879 *bufflen = size;
881 return buff;
883 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
885 /* some members of urb must be substituted before. */
886 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
888 void *buff;
889 struct usbip_iso_packet_descriptor *iso;
890 int np = urb->number_of_packets;
891 int size = np * sizeof(*iso);
892 int i;
893 int ret;
895 if (!usb_pipeisoc(urb->pipe))
896 return 0;
898 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
899 if (np == 0) {
900 /* usbip_uinfo("iso np == 0\n"); */
901 /* usbip_dump_urb(urb); */
902 return 0;
905 buff = kzalloc(size, GFP_KERNEL);
906 if (!buff)
907 return -ENOMEM;
909 ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0);
910 if (ret != size) {
911 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
912 ret);
913 kfree(buff);
915 if (ud->side == USBIP_STUB)
916 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
917 else
918 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
920 return -EPIPE;
923 for (i = 0; i < np; i++) {
924 iso = buff + (i * sizeof(*iso));
926 usbip_iso_pakcet_correct_endian(iso, 0);
927 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
930 kfree(buff);
932 return ret;
934 EXPORT_SYMBOL_GPL(usbip_recv_iso);
937 /* some members of urb must be substituted before. */
938 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
940 int ret;
941 int size;
943 if (ud->side == USBIP_STUB) {
944 /* stub_rx.c */
945 /* the direction of urb must be OUT. */
946 if (usb_pipein(urb->pipe))
947 return 0;
949 size = urb->transfer_buffer_length;
950 } else {
951 /* vhci_rx.c */
952 /* the direction of urb must be IN. */
953 if (usb_pipeout(urb->pipe))
954 return 0;
956 size = urb->actual_length;
959 /* no need to recv xbuff */
960 if (!(size > 0))
961 return 0;
963 ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer,
964 size, 0);
965 if (ret != size) {
966 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
967 if (ud->side == USBIP_STUB) {
968 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
969 } else {
970 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
971 return -EPIPE;
975 return ret;
977 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
980 /*-------------------------------------------------------------------------*/
982 static int __init usbip_common_init(void)
984 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "" DRIVER_VERSION);
986 return 0;
989 static void __exit usbip_common_exit(void)
991 return;
997 module_init(usbip_common_init);
998 module_exit(usbip_common_exit);
1000 MODULE_AUTHOR(DRIVER_AUTHOR);
1001 MODULE_DESCRIPTION(DRIVER_DESC);
1002 MODULE_LICENSE("GPL");