]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/usbip/usbip_common.c
22f93dd0ba034ac741dcba70642fbf5272957070
[mv-sheeva.git] / drivers / staging / usbip / usbip_common.c
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 "usbip_common.h"
25
26 /* version information */
27 #define DRIVER_VERSION "1.0"
28 #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi _at_ users.sourceforge.net>"
29 #define DRIVER_DESC "usbip common driver"
30
31 /*-------------------------------------------------------------------------*/
32 /* debug routines */
33
34 #ifdef CONFIG_USB_DEBUG
35 unsigned long usbip_debug_flag = 0xffffffff;
36 #else
37 unsigned long usbip_debug_flag;
38 #endif
39 EXPORT_SYMBOL_GPL(usbip_debug_flag);
40
41
42 /* FIXME */
43 struct device_attribute dev_attr_usbip_debug;
44 EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
45
46
47 static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
48                                                                 char *buf)
49 {
50         return sprintf(buf, "%lx\n", usbip_debug_flag);
51 }
52
53 static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
54                 const char *buf, size_t count)
55 {
56         unsigned long flag;
57
58         sscanf(buf, "%lx", &flag);
59         usbip_debug_flag = flag;
60
61         return count;
62 }
63 DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
64
65 static void usbip_dump_buffer(char *buff, int bufflen)
66 {
67         int i;
68
69         if (bufflen > 128) {
70                 for (i = 0; i < 128; i++) {
71                         if (i%24 == 0)
72                                 printk("   ");
73                         printk("%02x ", (unsigned char) buff[i]);
74                         if (i%4 == 3)
75                                 printk("| ");
76                         if (i%24 == 23)
77                                 printk("\n");
78                 }
79                 printk("... (%d byte)\n", bufflen);
80                 return;
81         }
82
83         for (i = 0; i < bufflen; i++) {
84                 if (i%24 == 0)
85                         printk("   ");
86                 printk("%02x ", (unsigned char) buff[i]);
87                 if (i%4 == 3)
88                         printk("| ");
89                 if (i%24 == 23)
90                         printk("\n");
91         }
92         printk("\n");
93
94 }
95
96 static void usbip_dump_pipe(unsigned int p)
97 {
98         unsigned char type = usb_pipetype(p);
99         unsigned char ep = usb_pipeendpoint(p);
100         unsigned char dev = usb_pipedevice(p);
101         unsigned char dir = usb_pipein(p);
102
103         printk("dev(%d) ", dev);
104         printk("ep(%d) ",  ep);
105         printk("%s ", dir ? "IN" : "OUT");
106
107         switch (type) {
108         case PIPE_ISOCHRONOUS:
109                 printk("%s ", "ISO");
110                 break;
111         case PIPE_INTERRUPT:
112                 printk("%s ", "INT");
113                 break;
114         case PIPE_CONTROL:
115                 printk("%s ", "CTL");
116                 break;
117         case PIPE_BULK:
118                 printk("%s ", "BLK");
119                 break;
120         default:
121                 printk("ERR");
122         }
123
124         printk("\n");
125
126 }
127
128 static void usbip_dump_usb_device(struct usb_device *udev)
129 {
130         struct device *dev = &udev->dev;
131         int i;
132
133         dev_dbg(dev, "       devnum(%d) devpath(%s)",
134                 udev->devnum, udev->devpath);
135
136         switch (udev->speed) {
137         case USB_SPEED_HIGH:
138                 printk(" SPD_HIGH");
139                 break;
140         case USB_SPEED_FULL:
141                 printk(" SPD_FULL");
142                 break;
143         case USB_SPEED_LOW:
144                 printk(" SPD_LOW");
145                 break;
146         case USB_SPEED_UNKNOWN:
147                 printk(" SPD_UNKNOWN");
148                 break;
149         default:
150                 printk(" SPD_ERROR");
151         }
152
153         printk(" tt %p, ttport %d", udev->tt, udev->ttport);
154         printk("\n");
155
156         dev_dbg(dev, "                    ");
157         for (i = 0; i < 16; i++)
158                 printk(" %2u", i);
159         printk("\n");
160
161         dev_dbg(dev, "       toggle0(IN) :");
162         for (i = 0; i < 16; i++)
163                 printk(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
164         printk("\n");
165
166         dev_dbg(dev, "       toggle1(OUT):");
167         for (i = 0; i < 16; i++)
168                 printk(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
169         printk("\n");
170
171
172         dev_dbg(dev, "       epmaxp_in   :");
173         for (i = 0; i < 16; i++) {
174                 if (udev->ep_in[i])
175                         printk(" %2u",
176                              le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
177         }
178         printk("\n");
179
180         dev_dbg(dev, "       epmaxp_out  :");
181         for (i = 0; i < 16; i++) {
182                 if (udev->ep_out[i])
183                         printk(" %2u",
184                              le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
185         }
186         printk("\n");
187
188         dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
189
190         dev_dbg(dev, "descriptor %p, config %p, actconfig %p, "
191                 "rawdescriptors %p\n", &udev->descriptor, udev->config,
192                 udev->actconfig, udev->rawdescriptors);
193
194         dev_dbg(dev, "have_langid %d, string_langid %d\n",
195                 udev->have_langid, udev->string_langid);
196
197         dev_dbg(dev, "maxchild %d, children %p\n",
198                 udev->maxchild, udev->children);
199 }
200
201 static void usbip_dump_request_type(__u8 rt)
202 {
203         switch (rt & USB_RECIP_MASK) {
204         case USB_RECIP_DEVICE:
205                 printk("DEVICE");
206                 break;
207         case USB_RECIP_INTERFACE:
208                 printk("INTERF");
209                 break;
210         case USB_RECIP_ENDPOINT:
211                 printk("ENDPOI");
212                 break;
213         case USB_RECIP_OTHER:
214                 printk("OTHER ");
215                 break;
216         default:
217                 printk("------");
218         }
219 }
220
221 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
222 {
223         if (!cmd) {
224                 printk("      %s : null pointer\n", __func__);
225                 return;
226         }
227
228         printk("       ");
229         printk("bRequestType(%02X) ", cmd->bRequestType);
230         printk("bRequest(%02X) " , cmd->bRequest);
231         printk("wValue(%04X) ", cmd->wValue);
232         printk("wIndex(%04X) ", cmd->wIndex);
233         printk("wLength(%04X) ", cmd->wLength);
234
235         printk("\n       ");
236
237         if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
238                 printk("STANDARD ");
239                 switch (cmd->bRequest) {
240                 case USB_REQ_GET_STATUS:
241                         printk("GET_STATUS");
242                         break;
243                 case USB_REQ_CLEAR_FEATURE:
244                         printk("CLEAR_FEAT");
245                         break;
246                 case USB_REQ_SET_FEATURE:
247                         printk("SET_FEAT  ");
248                         break;
249                 case USB_REQ_SET_ADDRESS:
250                         printk("SET_ADDRRS");
251                         break;
252                 case USB_REQ_GET_DESCRIPTOR:
253                         printk("GET_DESCRI");
254                         break;
255                 case USB_REQ_SET_DESCRIPTOR:
256                         printk("SET_DESCRI");
257                         break;
258                 case USB_REQ_GET_CONFIGURATION:
259                         printk("GET_CONFIG");
260                         break;
261                 case USB_REQ_SET_CONFIGURATION:
262                         printk("SET_CONFIG");
263                         break;
264                 case USB_REQ_GET_INTERFACE:
265                         printk("GET_INTERF");
266                         break;
267                 case USB_REQ_SET_INTERFACE:
268                         printk("SET_INTERF");
269                         break;
270                 case USB_REQ_SYNCH_FRAME:
271                         printk("SYNC_FRAME");
272                         break;
273                 default:
274                         printk("REQ(%02X) ", cmd->bRequest);
275                 }
276
277                 printk(" ");
278                 usbip_dump_request_type(cmd->bRequestType);
279
280         } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
281                 printk("CLASS   ");
282
283         else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)
284                 printk("VENDOR  ");
285
286         else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED)
287                 printk("RESERVED");
288
289         printk("\n");
290 }
291
292 void usbip_dump_urb(struct urb *urb)
293 {
294         struct device *dev;
295
296         if (!urb) {
297                 printk(KERN_DEBUG KBUILD_MODNAME
298                        ":%s: urb: null pointer!!\n", __func__);
299                 return;
300         }
301
302         if (!urb->dev) {
303                 printk(KERN_DEBUG KBUILD_MODNAME
304                        ":%s: urb->dev: null pointer!!\n", __func__);
305                 return;
306         }
307         dev = &urb->dev->dev;
308
309         dev_dbg(dev, "   urb                   :%p\n", urb);
310         dev_dbg(dev, "   dev                   :%p\n", urb->dev);
311
312         usbip_dump_usb_device(urb->dev);
313
314         dev_dbg(dev, "   pipe                  :%08x ", urb->pipe);
315
316         usbip_dump_pipe(urb->pipe);
317
318         dev_dbg(dev, "   status                :%d\n", urb->status);
319         dev_dbg(dev, "   transfer_flags        :%08X\n", urb->transfer_flags);
320         dev_dbg(dev, "   transfer_buffer       :%p\n", urb->transfer_buffer);
321         dev_dbg(dev, "   transfer_buffer_length:%d\n", urb->transfer_buffer_length);
322         dev_dbg(dev, "   actual_length         :%d\n", urb->actual_length);
323         dev_dbg(dev, "   setup_packet          :%p\n", urb->setup_packet);
324
325         if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
326                         usbip_dump_usb_ctrlrequest(
327                         (struct usb_ctrlrequest *)urb->setup_packet);
328
329         dev_dbg(dev, "   start_frame           :%d\n", urb->start_frame);
330         dev_dbg(dev, "   number_of_packets     :%d\n", urb->number_of_packets);
331         dev_dbg(dev, "   interval              :%d\n", urb->interval);
332         dev_dbg(dev, "   error_count           :%d\n", urb->error_count);
333         dev_dbg(dev, "   context               :%p\n", urb->context);
334         dev_dbg(dev, "   complete              :%p\n", urb->complete);
335 }
336 EXPORT_SYMBOL_GPL(usbip_dump_urb);
337
338 void usbip_dump_header(struct usbip_header *pdu)
339 {
340         udbg("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
341                         pdu->base.command,
342                         pdu->base.seqnum,
343                         pdu->base.devid,
344                         pdu->base.direction,
345                         pdu->base.ep);
346
347         switch (pdu->base.command) {
348         case USBIP_CMD_SUBMIT:
349                 udbg("CMD_SUBMIT: x_flags %u x_len %u sf %u #p %u iv %u\n",
350                                 pdu->u.cmd_submit.transfer_flags,
351                                 pdu->u.cmd_submit.transfer_buffer_length,
352                                 pdu->u.cmd_submit.start_frame,
353                                 pdu->u.cmd_submit.number_of_packets,
354                                 pdu->u.cmd_submit.interval);
355                                 break;
356         case USBIP_CMD_UNLINK:
357                 udbg("CMD_UNLINK: seq %u\n", pdu->u.cmd_unlink.seqnum);
358                 break;
359         case USBIP_RET_SUBMIT:
360                 udbg("RET_SUBMIT: st %d al %u sf %d ec %d\n",
361                                 pdu->u.ret_submit.status,
362                                 pdu->u.ret_submit.actual_length,
363                                 pdu->u.ret_submit.start_frame,
364                                 pdu->u.ret_submit.error_count);
365         case USBIP_RET_UNLINK:
366                 udbg("RET_UNLINK: status %d\n", pdu->u.ret_unlink.status);
367                 break;
368         default:
369                 /* NOT REACHED */
370                 udbg("UNKNOWN\n");
371         }
372 }
373 EXPORT_SYMBOL_GPL(usbip_dump_header);
374
375
376 /*-------------------------------------------------------------------------*/
377 /* thread routines */
378
379 int usbip_thread(void *param)
380 {
381         struct usbip_task *ut = param;
382
383         if (!ut)
384                 return -EINVAL;
385
386         lock_kernel();
387         daemonize(ut->name);
388         allow_signal(SIGKILL);
389         ut->thread = current;
390         unlock_kernel();
391
392         /* srv.rb must wait for rx_thread starting */
393         complete(&ut->thread_done);
394
395         /* start of while loop */
396         ut->loop_ops(ut);
397
398         /* end of loop */
399         ut->thread = NULL;
400
401         complete_and_exit(&ut->thread_done, 0);
402 }
403
404 void usbip_start_threads(struct usbip_device *ud)
405 {
406         /*
407          * threads are invoked per one device (per one connection).
408          */
409         int retval;
410
411         retval = kernel_thread(usbip_thread, (void *)&ud->tcp_rx, 0);
412         if (retval < 0) {
413                 printk(KERN_ERR "Creating tcp_rx thread for ud %p failed.\n",
414                                 ud);
415                 return;
416         }
417         retval = kernel_thread(usbip_thread, (void *)&ud->tcp_tx, 0);
418         if (retval < 0) {
419                 printk(KERN_ERR "Creating tcp_tx thread for ud %p failed.\n",
420                                 ud);
421                 return;
422         }
423
424         /* confirm threads are starting */
425         wait_for_completion(&ud->tcp_rx.thread_done);
426         wait_for_completion(&ud->tcp_tx.thread_done);
427 }
428 EXPORT_SYMBOL_GPL(usbip_start_threads);
429
430 void usbip_stop_threads(struct usbip_device *ud)
431 {
432         /* kill threads related to this sdev, if v.c. exists */
433         if (ud->tcp_rx.thread != NULL) {
434                 send_sig(SIGKILL, ud->tcp_rx.thread, 1);
435                 wait_for_completion(&ud->tcp_rx.thread_done);
436                 udbg("rx_thread for ud %p has finished\n", ud);
437         }
438
439         if (ud->tcp_tx.thread != NULL) {
440                 send_sig(SIGKILL, ud->tcp_tx.thread, 1);
441                 wait_for_completion(&ud->tcp_tx.thread_done);
442                 udbg("tx_thread for ud %p has finished\n", ud);
443         }
444 }
445 EXPORT_SYMBOL_GPL(usbip_stop_threads);
446
447 void usbip_task_init(struct usbip_task *ut, char *name,
448                 void (*loop_ops)(struct usbip_task *))
449 {
450         ut->thread = NULL;
451         init_completion(&ut->thread_done);
452         ut->name = name;
453         ut->loop_ops = loop_ops;
454 }
455 EXPORT_SYMBOL_GPL(usbip_task_init);
456
457
458 /*-------------------------------------------------------------------------*/
459 /* socket routines */
460
461  /*  Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
462 int usbip_xmit(int send, struct socket *sock, char *buf,
463                int size, int msg_flags)
464 {
465         int result;
466         struct msghdr msg;
467         struct kvec iov;
468         int total = 0;
469
470         /* for blocks of if (dbg_flag_xmit) */
471         char *bp = buf;
472         int osize = size;
473
474         dbg_xmit("enter\n");
475
476         if (!sock || !buf || !size) {
477                 printk(KERN_ERR "%s: invalid arg, sock %p buff %p size %d\n",
478                        __func__, sock, buf, size);
479                 return -EINVAL;
480         }
481
482
483         if (dbg_flag_xmit) {
484                 if (send) {
485                         if (!in_interrupt())
486                                 printk(KERN_DEBUG "%-10s:", current->comm);
487                         else
488                                 printk(KERN_DEBUG "interupt  :");
489
490                         printk("%s: sending... , sock %p, buf %p, "
491                                "size %d, msg_flags %d\n", __func__,
492                                sock, buf, size, msg_flags);
493                         usbip_dump_buffer(buf, size);
494                 }
495         }
496
497
498         do {
499                 sock->sk->sk_allocation = GFP_NOIO;
500                 iov.iov_base    = buf;
501                 iov.iov_len     = size;
502                 msg.msg_name    = NULL;
503                 msg.msg_namelen = 0;
504                 msg.msg_control = NULL;
505                 msg.msg_controllen = 0;
506                 msg.msg_namelen    = 0;
507                 msg.msg_flags      = msg_flags | MSG_NOSIGNAL;
508
509                 if (send)
510                         result = kernel_sendmsg(sock, &msg, &iov, 1, size);
511                 else
512                         result = kernel_recvmsg(sock, &msg, &iov, 1, size,
513                                                                 MSG_WAITALL);
514
515                 if (result <= 0) {
516                         udbg("usbip_xmit: %s sock %p buf %p size %u ret %d"
517                                         " total %d\n",
518                                         send ? "send" : "receive", sock, buf,
519                                         size, result, total);
520                         goto err;
521                 }
522
523                 size -= result;
524                 buf += result;
525                 total += result;
526
527         } while (size > 0);
528
529
530         if (dbg_flag_xmit) {
531                 if (!send) {
532                         if (!in_interrupt())
533                                 printk(KERN_DEBUG "%-10s:", current->comm);
534                         else
535                                 printk(KERN_DEBUG "interupt  :");
536
537                         printk("usbip_xmit: receiving....\n");
538                         usbip_dump_buffer(bp, osize);
539                         printk("usbip_xmit: received, osize %d ret %d size %d "
540                                         "total %d\n", osize, result, size,
541                                         total);
542                 }
543
544                 if (send)
545                         printk("usbip_xmit: send, total %d\n", total);
546         }
547
548         return total;
549
550 err:
551         return result;
552 }
553 EXPORT_SYMBOL_GPL(usbip_xmit);
554
555
556 /* now a usrland utility should set options. */
557 #if 0
558 int setquickack(struct socket *socket)
559 {
560         mm_segment_t oldfs;
561         int val = 1;
562         int ret;
563
564         oldfs = get_fs();
565         set_fs(get_ds());
566         ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_QUICKACK,
567                         (char __user *) &val, sizeof(ret));
568         set_fs(oldfs);
569
570         return ret;
571 }
572
573 int setnodelay(struct socket *socket)
574 {
575         mm_segment_t oldfs;
576         int val = 1;
577         int ret;
578
579         oldfs = get_fs();
580         set_fs(get_ds());
581         ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_NODELAY,
582                         (char __user *) &val, sizeof(ret));
583         set_fs(oldfs);
584
585         return ret;
586 }
587
588 int setkeepalive(struct socket *socket)
589 {
590         mm_segment_t oldfs;
591         int val = 1;
592         int ret;
593
594         oldfs = get_fs();
595         set_fs(get_ds());
596         ret = socket->ops->setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE,
597                         (char __user *) &val, sizeof(ret));
598         set_fs(oldfs);
599
600         return ret;
601 }
602
603 void setreuse(struct socket *socket)
604 {
605         socket->sk->sk_reuse = 1;
606 }
607 #endif
608
609 struct socket *sockfd_to_socket(unsigned int sockfd)
610 {
611         struct socket *socket;
612         struct file *file;
613         struct inode *inode;
614
615         file = fget(sockfd);
616         if (!file) {
617                 printk(KERN_ERR "%s: invalid sockfd\n", __func__);
618                 return NULL;
619         }
620
621         inode = file->f_dentry->d_inode;
622
623         if (!inode || !S_ISSOCK(inode->i_mode))
624                 return NULL;
625
626         socket = SOCKET_I(inode);
627
628         return socket;
629 }
630 EXPORT_SYMBOL_GPL(sockfd_to_socket);
631
632
633
634 /*-------------------------------------------------------------------------*/
635 /* pdu routines */
636
637 /* there may be more cases to tweak the flags. */
638 static unsigned int tweak_transfer_flags(unsigned int flags)
639 {
640
641         if (flags & URB_NO_TRANSFER_DMA_MAP)
642                 /*
643                  * vhci_hcd does not provide DMA-mapped I/O. The upper
644                  * driver does not need to set this flag. The remote
645                  * usbip.ko does not still perform DMA-mapped I/O for
646                  * DMA-caplable host controllers. So, clear this flag.
647                  */
648                 flags &= ~URB_NO_TRANSFER_DMA_MAP;
649
650         if (flags & URB_NO_SETUP_DMA_MAP)
651                 flags &= ~URB_NO_SETUP_DMA_MAP;
652
653         return flags;
654 }
655
656 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
657                                                                 int pack)
658 {
659         struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
660
661         /*
662          * Some members are not still implemented in usbip. I hope this issue
663          * will be discussed when usbip is ported to other operating systems.
664          */
665         if (pack) {
666                 /* vhci_tx.c */
667                 spdu->transfer_flags =
668                                 tweak_transfer_flags(urb->transfer_flags);
669                 spdu->transfer_buffer_length    = urb->transfer_buffer_length;
670                 spdu->start_frame               = urb->start_frame;
671                 spdu->number_of_packets         = urb->number_of_packets;
672                 spdu->interval                  = urb->interval;
673         } else  {
674                 /* stub_rx.c */
675                 urb->transfer_flags         = spdu->transfer_flags;
676
677                 urb->transfer_buffer_length = spdu->transfer_buffer_length;
678                 urb->start_frame            = spdu->start_frame;
679                 urb->number_of_packets      = spdu->number_of_packets;
680                 urb->interval               = spdu->interval;
681         }
682 }
683
684 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
685                                                                 int pack)
686 {
687         struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
688
689         if (pack) {
690                 /* stub_tx.c */
691
692                 rpdu->status            = urb->status;
693                 rpdu->actual_length     = urb->actual_length;
694                 rpdu->start_frame       = urb->start_frame;
695                 rpdu->error_count       = urb->error_count;
696         } else {
697                 /* vhci_rx.c */
698
699                 urb->status             = rpdu->status;
700                 urb->actual_length      = rpdu->actual_length;
701                 urb->start_frame        = rpdu->start_frame;
702                 urb->error_count        = rpdu->error_count;
703         }
704 }
705
706
707 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
708                                                                 int pack)
709 {
710         switch (cmd) {
711         case USBIP_CMD_SUBMIT:
712                 usbip_pack_cmd_submit(pdu, urb, pack);
713                 break;
714         case USBIP_RET_SUBMIT:
715                 usbip_pack_ret_submit(pdu, urb, pack);
716                 break;
717         default:
718                 err("unknown command");
719                 /* NOTREACHED */
720                 /* BUG(); */
721         }
722 }
723 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
724
725
726 static void correct_endian_basic(struct usbip_header_basic *base, int send)
727 {
728         if (send) {
729                 base->command   = cpu_to_be32(base->command);
730                 base->seqnum    = cpu_to_be32(base->seqnum);
731                 base->devid     = cpu_to_be32(base->devid);
732                 base->direction = cpu_to_be32(base->direction);
733                 base->ep        = cpu_to_be32(base->ep);
734         } else {
735                 base->command   = be32_to_cpu(base->command);
736                 base->seqnum    = be32_to_cpu(base->seqnum);
737                 base->devid     = be32_to_cpu(base->devid);
738                 base->direction = be32_to_cpu(base->direction);
739                 base->ep        = be32_to_cpu(base->ep);
740         }
741 }
742
743 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
744                                                                 int send)
745 {
746         if (send) {
747                 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
748
749                 cpu_to_be32s(&pdu->transfer_buffer_length);
750                 cpu_to_be32s(&pdu->start_frame);
751                 cpu_to_be32s(&pdu->number_of_packets);
752                 cpu_to_be32s(&pdu->interval);
753         } else {
754                 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
755
756                 be32_to_cpus(&pdu->transfer_buffer_length);
757                 be32_to_cpus(&pdu->start_frame);
758                 be32_to_cpus(&pdu->number_of_packets);
759                 be32_to_cpus(&pdu->interval);
760         }
761 }
762
763 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
764                                                                 int send)
765 {
766         if (send) {
767                 cpu_to_be32s(&pdu->status);
768                 cpu_to_be32s(&pdu->actual_length);
769                 cpu_to_be32s(&pdu->start_frame);
770                 cpu_to_be32s(&pdu->error_count);
771         } else {
772                 be32_to_cpus(&pdu->status);
773                 be32_to_cpus(&pdu->actual_length);
774                 be32_to_cpus(&pdu->start_frame);
775                 be32_to_cpus(&pdu->error_count);
776         }
777 }
778
779 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
780                                                                 int send)
781 {
782         if (send)
783                 pdu->seqnum = cpu_to_be32(pdu->seqnum);
784         else
785                 pdu->seqnum = be32_to_cpu(pdu->seqnum);
786 }
787
788 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
789                                                                 int send)
790 {
791         if (send)
792                 cpu_to_be32s(&pdu->status);
793         else
794                 be32_to_cpus(&pdu->status);
795 }
796
797 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
798 {
799         __u32 cmd = 0;
800
801         if (send)
802                 cmd = pdu->base.command;
803
804         correct_endian_basic(&pdu->base, send);
805
806         if (!send)
807                 cmd = pdu->base.command;
808
809         switch (cmd) {
810         case USBIP_CMD_SUBMIT:
811                 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
812                 break;
813         case USBIP_RET_SUBMIT:
814                 correct_endian_ret_submit(&pdu->u.ret_submit, send);
815                 break;
816         case USBIP_CMD_UNLINK:
817                 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
818                 break;
819         case USBIP_RET_UNLINK:
820                 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
821                 break;
822         default:
823                 /* NOTREACHED */
824                 err("unknown command in pdu header: %d", cmd);
825                 /* BUG(); */
826         }
827 }
828 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
829
830 static void usbip_iso_pakcet_correct_endian(
831                                 struct usbip_iso_packet_descriptor *iso,
832                                 int send)
833 {
834         /* does not need all members. but copy all simply. */
835         if (send) {
836                 iso->offset     = cpu_to_be32(iso->offset);
837                 iso->length     = cpu_to_be32(iso->length);
838                 iso->status     = cpu_to_be32(iso->status);
839                 iso->actual_length = cpu_to_be32(iso->actual_length);
840         } else {
841                 iso->offset     = be32_to_cpu(iso->offset);
842                 iso->length     = be32_to_cpu(iso->length);
843                 iso->status     = be32_to_cpu(iso->status);
844                 iso->actual_length = be32_to_cpu(iso->actual_length);
845         }
846 }
847
848 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
849                 struct usb_iso_packet_descriptor *uiso, int pack)
850 {
851         if (pack) {
852                 iso->offset             = uiso->offset;
853                 iso->length             = uiso->length;
854                 iso->status             = uiso->status;
855                 iso->actual_length      = uiso->actual_length;
856         } else {
857                 uiso->offset            = iso->offset;
858                 uiso->length            = iso->length;
859                 uiso->status            = iso->status;
860                 uiso->actual_length     = iso->actual_length;
861         }
862 }
863
864
865 /* must free buffer */
866 void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
867 {
868         void *buff;
869         struct usbip_iso_packet_descriptor *iso;
870         int np = urb->number_of_packets;
871         ssize_t size = np * sizeof(*iso);
872         int i;
873
874         buff = kzalloc(size, GFP_KERNEL);
875         if (!buff)
876                 return NULL;
877
878         for (i = 0; i < np; i++) {
879                 iso = buff + (i * sizeof(*iso));
880
881                 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
882                 usbip_iso_pakcet_correct_endian(iso, 1);
883         }
884
885         *bufflen = size;
886
887         return buff;
888 }
889 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
890
891 /* some members of urb must be substituted before. */
892 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
893 {
894         void *buff;
895         struct usbip_iso_packet_descriptor *iso;
896         int np = urb->number_of_packets;
897         int size = np * sizeof(*iso);
898         int i;
899         int ret;
900
901         if (!usb_pipeisoc(urb->pipe))
902                 return 0;
903
904         /* my Bluetooth dongle gets ISO URBs which are np = 0 */
905         if (np == 0) {
906                 /* uinfo("iso np == 0\n"); */
907                 /* usbip_dump_urb(urb); */
908                 return 0;
909         }
910
911         buff = kzalloc(size, GFP_KERNEL);
912         if (!buff)
913                 return -ENOMEM;
914
915         ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0);
916         if (ret != size) {
917                 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
918                         ret);
919                 kfree(buff);
920
921                 if (ud->side == USBIP_STUB)
922                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
923                 else
924                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
925
926                 return -EPIPE;
927         }
928
929         for (i = 0; i < np; i++) {
930                 iso = buff + (i * sizeof(*iso));
931
932                 usbip_iso_pakcet_correct_endian(iso, 0);
933                 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
934         }
935
936
937         kfree(buff);
938
939         return ret;
940 }
941 EXPORT_SYMBOL_GPL(usbip_recv_iso);
942
943
944 /* some members of urb must be substituted before. */
945 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
946 {
947         int ret;
948         int size;
949
950         if (ud->side == USBIP_STUB) {
951                 /* stub_rx.c */
952                 /* the direction of urb must be OUT. */
953                 if (usb_pipein(urb->pipe))
954                         return 0;
955
956                 size = urb->transfer_buffer_length;
957         } else {
958                 /* vhci_rx.c */
959                 /* the direction of urb must be IN. */
960                 if (usb_pipeout(urb->pipe))
961                         return 0;
962
963                 size = urb->actual_length;
964         }
965
966         /* no need to recv xbuff */
967         if (!(size > 0))
968                 return 0;
969
970         ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer,
971                          size, 0);
972         if (ret != size) {
973                 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
974                 if (ud->side == USBIP_STUB) {
975                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
976                 } else {
977                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
978                         return -EPIPE;
979                 }
980         }
981
982         return ret;
983 }
984 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
985
986
987 /*-------------------------------------------------------------------------*/
988
989 static int __init usbip_common_init(void)
990 {
991         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "" DRIVER_VERSION);
992
993         return 0;
994 }
995
996 static void __exit usbip_common_exit(void)
997 {
998         return;
999 }
1000
1001
1002
1003
1004 module_init(usbip_common_init);
1005 module_exit(usbip_common_exit);
1006
1007 MODULE_AUTHOR(DRIVER_AUTHOR);
1008 MODULE_DESCRIPTION(DRIVER_DESC);
1009 MODULE_LICENSE("GPL");