staging: struct device - replace bus_id with dev_name(), dev_set_name()
[linux-2.6] / 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         kernel_thread(usbip_thread, (void *)&ud->tcp_rx, 0);
410         kernel_thread(usbip_thread, (void *)&ud->tcp_tx, 0);
411
412         /* confirm threads are starting */
413         wait_for_completion(&ud->tcp_rx.thread_done);
414         wait_for_completion(&ud->tcp_tx.thread_done);
415 }
416 EXPORT_SYMBOL_GPL(usbip_start_threads);
417
418 void usbip_stop_threads(struct usbip_device *ud)
419 {
420         /* kill threads related to this sdev, if v.c. exists */
421         if (ud->tcp_rx.thread != NULL) {
422                 send_sig(SIGKILL, ud->tcp_rx.thread, 1);
423                 wait_for_completion(&ud->tcp_rx.thread_done);
424                 udbg("rx_thread for ud %p has finished\n", ud);
425         }
426
427         if (ud->tcp_tx.thread != NULL) {
428                 send_sig(SIGKILL, ud->tcp_tx.thread, 1);
429                 wait_for_completion(&ud->tcp_tx.thread_done);
430                 udbg("tx_thread for ud %p has finished\n", ud);
431         }
432 }
433 EXPORT_SYMBOL_GPL(usbip_stop_threads);
434
435 void usbip_task_init(struct usbip_task *ut, char *name,
436                 void (*loop_ops)(struct usbip_task *))
437 {
438         ut->thread = NULL;
439         init_completion(&ut->thread_done);
440         ut->name = name;
441         ut->loop_ops = loop_ops;
442 }
443 EXPORT_SYMBOL_GPL(usbip_task_init);
444
445
446 /*-------------------------------------------------------------------------*/
447 /* socket routines */
448
449  /*  Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
450 int usbip_xmit(int send, struct socket *sock, char *buf,
451                int size, int msg_flags)
452 {
453         int result;
454         struct msghdr msg;
455         struct kvec iov;
456         int total = 0;
457
458         /* for blocks of if (dbg_flag_xmit) */
459         char *bp = buf;
460         int osize = size;
461
462         dbg_xmit("enter\n");
463
464         if (!sock || !buf || !size) {
465                 printk(KERN_ERR "%s: invalid arg, sock %p buff %p size %d\n",
466                        __func__, sock, buf, size);
467                 return -EINVAL;
468         }
469
470
471         if (dbg_flag_xmit) {
472                 if (send) {
473                         if (!in_interrupt())
474                                 printk(KERN_DEBUG "%-10s:", current->comm);
475                         else
476                                 printk(KERN_DEBUG "interupt  :");
477
478                         printk("%s: sending... , sock %p, buf %p, "
479                                "size %d, msg_flags %d\n", __func__,
480                                sock, buf, size, msg_flags);
481                         usbip_dump_buffer(buf, size);
482                 }
483         }
484
485
486         do {
487                 sock->sk->sk_allocation = GFP_NOIO;
488                 iov.iov_base    = buf;
489                 iov.iov_len     = size;
490                 msg.msg_name    = NULL;
491                 msg.msg_namelen = 0;
492                 msg.msg_control = NULL;
493                 msg.msg_controllen = 0;
494                 msg.msg_namelen    = 0;
495                 msg.msg_flags      = msg_flags | MSG_NOSIGNAL;
496
497                 if (send)
498                         result = kernel_sendmsg(sock, &msg, &iov, 1, size);
499                 else
500                         result = kernel_recvmsg(sock, &msg, &iov, 1, size,
501                                                                 MSG_WAITALL);
502
503                 if (result <= 0) {
504                         udbg("usbip_xmit: %s sock %p buf %p size %u ret %d"
505                                         " total %d\n",
506                                         send ? "send" : "receive", sock, buf,
507                                         size, result, total);
508                         goto err;
509                 }
510
511                 size -= result;
512                 buf += result;
513                 total += result;
514
515         } while (size > 0);
516
517
518         if (dbg_flag_xmit) {
519                 if (!send) {
520                         if (!in_interrupt())
521                                 printk(KERN_DEBUG "%-10s:", current->comm);
522                         else
523                                 printk(KERN_DEBUG "interupt  :");
524
525                         printk("usbip_xmit: receiving....\n");
526                         usbip_dump_buffer(bp, osize);
527                         printk("usbip_xmit: received, osize %d ret %d size %d "
528                                         "total %d\n", osize, result, size,
529                                         total);
530                 }
531
532                 if (send)
533                         printk("usbip_xmit: send, total %d\n", total);
534         }
535
536         return total;
537
538 err:
539         return result;
540 }
541 EXPORT_SYMBOL_GPL(usbip_xmit);
542
543
544 /* now a usrland utility should set options. */
545 #if 0
546 int setquickack(struct socket *socket)
547 {
548         mm_segment_t oldfs;
549         int val = 1;
550         int ret;
551
552         oldfs = get_fs();
553         set_fs(get_ds());
554         ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_QUICKACK,
555                         (char __user *) &val, sizeof(ret));
556         set_fs(oldfs);
557
558         return ret;
559 }
560
561 int setnodelay(struct socket *socket)
562 {
563         mm_segment_t oldfs;
564         int val = 1;
565         int ret;
566
567         oldfs = get_fs();
568         set_fs(get_ds());
569         ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_NODELAY,
570                         (char __user *) &val, sizeof(ret));
571         set_fs(oldfs);
572
573         return ret;
574 }
575
576 int setkeepalive(struct socket *socket)
577 {
578         mm_segment_t oldfs;
579         int val = 1;
580         int ret;
581
582         oldfs = get_fs();
583         set_fs(get_ds());
584         ret = socket->ops->setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE,
585                         (char __user *) &val, sizeof(ret));
586         set_fs(oldfs);
587
588         return ret;
589 }
590
591 void setreuse(struct socket *socket)
592 {
593         socket->sk->sk_reuse = 1;
594 }
595 #endif
596
597 struct socket *sockfd_to_socket(unsigned int sockfd)
598 {
599         struct socket *socket;
600         struct file *file;
601         struct inode *inode;
602
603         file = fget(sockfd);
604         if (!file) {
605                 printk(KERN_ERR "%s: invalid sockfd\n", __func__);
606                 return NULL;
607         }
608
609         inode = file->f_dentry->d_inode;
610
611         if (!inode || !S_ISSOCK(inode->i_mode))
612                 return NULL;
613
614         socket = SOCKET_I(inode);
615
616         return socket;
617 }
618 EXPORT_SYMBOL_GPL(sockfd_to_socket);
619
620
621
622 /*-------------------------------------------------------------------------*/
623 /* pdu routines */
624
625 /* there may be more cases to tweak the flags. */
626 static unsigned int tweak_transfer_flags(unsigned int flags)
627 {
628
629         if (flags & URB_NO_TRANSFER_DMA_MAP)
630                 /*
631                  * vhci_hcd does not provide DMA-mapped I/O. The upper
632                  * driver does not need to set this flag. The remote
633                  * usbip.ko does not still perform DMA-mapped I/O for
634                  * DMA-caplable host controllers. So, clear this flag.
635                  */
636                 flags &= ~URB_NO_TRANSFER_DMA_MAP;
637
638         if (flags & URB_NO_SETUP_DMA_MAP)
639                 flags &= ~URB_NO_SETUP_DMA_MAP;
640
641         return flags;
642 }
643
644 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
645                                                                 int pack)
646 {
647         struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
648
649         /*
650          * Some members are not still implemented in usbip. I hope this issue
651          * will be discussed when usbip is ported to other operating systems.
652          */
653         if (pack) {
654                 /* vhci_tx.c */
655                 spdu->transfer_flags =
656                                 tweak_transfer_flags(urb->transfer_flags);
657                 spdu->transfer_buffer_length    = urb->transfer_buffer_length;
658                 spdu->start_frame               = urb->start_frame;
659                 spdu->number_of_packets         = urb->number_of_packets;
660                 spdu->interval                  = urb->interval;
661         } else  {
662                 /* stub_rx.c */
663                 urb->transfer_flags         = spdu->transfer_flags;
664
665                 urb->transfer_buffer_length = spdu->transfer_buffer_length;
666                 urb->start_frame            = spdu->start_frame;
667                 urb->number_of_packets      = spdu->number_of_packets;
668                 urb->interval               = spdu->interval;
669         }
670 }
671
672 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
673                                                                 int pack)
674 {
675         struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
676
677         if (pack) {
678                 /* stub_tx.c */
679
680                 rpdu->status            = urb->status;
681                 rpdu->actual_length     = urb->actual_length;
682                 rpdu->start_frame       = urb->start_frame;
683                 rpdu->error_count       = urb->error_count;
684         } else {
685                 /* vhci_rx.c */
686
687                 urb->status             = rpdu->status;
688                 urb->actual_length      = rpdu->actual_length;
689                 urb->start_frame        = rpdu->start_frame;
690                 urb->error_count        = rpdu->error_count;
691         }
692 }
693
694
695 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
696                                                                 int pack)
697 {
698         switch (cmd) {
699         case USBIP_CMD_SUBMIT:
700                 usbip_pack_cmd_submit(pdu, urb, pack);
701                 break;
702         case USBIP_RET_SUBMIT:
703                 usbip_pack_ret_submit(pdu, urb, pack);
704                 break;
705         default:
706                 err("unknown command");
707                 /* NOTREACHED */
708                 /* BUG(); */
709         }
710 }
711 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
712
713
714 static void correct_endian_basic(struct usbip_header_basic *base, int send)
715 {
716         if (send) {
717                 base->command   = cpu_to_be32(base->command);
718                 base->seqnum    = cpu_to_be32(base->seqnum);
719                 base->devid     = cpu_to_be32(base->devid);
720                 base->direction = cpu_to_be32(base->direction);
721                 base->ep        = cpu_to_be32(base->ep);
722         } else {
723                 base->command   = be32_to_cpu(base->command);
724                 base->seqnum    = be32_to_cpu(base->seqnum);
725                 base->devid     = be32_to_cpu(base->devid);
726                 base->direction = be32_to_cpu(base->direction);
727                 base->ep        = be32_to_cpu(base->ep);
728         }
729 }
730
731 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
732                                                                 int send)
733 {
734         if (send) {
735                 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
736
737                 cpu_to_be32s(&pdu->transfer_buffer_length);
738                 cpu_to_be32s(&pdu->start_frame);
739                 cpu_to_be32s(&pdu->number_of_packets);
740                 cpu_to_be32s(&pdu->interval);
741         } else {
742                 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
743
744                 be32_to_cpus(&pdu->transfer_buffer_length);
745                 be32_to_cpus(&pdu->start_frame);
746                 be32_to_cpus(&pdu->number_of_packets);
747                 be32_to_cpus(&pdu->interval);
748         }
749 }
750
751 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
752                                                                 int send)
753 {
754         if (send) {
755                 cpu_to_be32s(&pdu->status);
756                 cpu_to_be32s(&pdu->actual_length);
757                 cpu_to_be32s(&pdu->start_frame);
758                 cpu_to_be32s(&pdu->error_count);
759         } else {
760                 be32_to_cpus(&pdu->status);
761                 be32_to_cpus(&pdu->actual_length);
762                 be32_to_cpus(&pdu->start_frame);
763                 be32_to_cpus(&pdu->error_count);
764         }
765 }
766
767 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
768                                                                 int send)
769 {
770         if (send)
771                 pdu->seqnum = cpu_to_be32(pdu->seqnum);
772         else
773                 pdu->seqnum = be32_to_cpu(pdu->seqnum);
774 }
775
776 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
777                                                                 int send)
778 {
779         if (send)
780                 cpu_to_be32s(&pdu->status);
781         else
782                 be32_to_cpus(&pdu->status);
783 }
784
785 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
786 {
787         __u32 cmd = 0;
788
789         if (send)
790                 cmd = pdu->base.command;
791
792         correct_endian_basic(&pdu->base, send);
793
794         if (!send)
795                 cmd = pdu->base.command;
796
797         switch (cmd) {
798         case USBIP_CMD_SUBMIT:
799                 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
800                 break;
801         case USBIP_RET_SUBMIT:
802                 correct_endian_ret_submit(&pdu->u.ret_submit, send);
803                 break;
804         case USBIP_CMD_UNLINK:
805                 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
806                 break;
807         case USBIP_RET_UNLINK:
808                 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
809                 break;
810         default:
811                 /* NOTREACHED */
812                 err("unknown command in pdu header: %d", cmd);
813                 /* BUG(); */
814         }
815 }
816 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
817
818 static void usbip_iso_pakcet_correct_endian(
819                                 struct usbip_iso_packet_descriptor *iso,
820                                 int send)
821 {
822         /* does not need all members. but copy all simply. */
823         if (send) {
824                 iso->offset     = cpu_to_be32(iso->offset);
825                 iso->length     = cpu_to_be32(iso->length);
826                 iso->status     = cpu_to_be32(iso->status);
827                 iso->actual_length = cpu_to_be32(iso->actual_length);
828         } else {
829                 iso->offset     = be32_to_cpu(iso->offset);
830                 iso->length     = be32_to_cpu(iso->length);
831                 iso->status     = be32_to_cpu(iso->status);
832                 iso->actual_length = be32_to_cpu(iso->actual_length);
833         }
834 }
835
836 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
837                 struct usb_iso_packet_descriptor *uiso, int pack)
838 {
839         if (pack) {
840                 iso->offset             = uiso->offset;
841                 iso->length             = uiso->length;
842                 iso->status             = uiso->status;
843                 iso->actual_length      = uiso->actual_length;
844         } else {
845                 uiso->offset            = iso->offset;
846                 uiso->length            = iso->length;
847                 uiso->status            = iso->status;
848                 uiso->actual_length     = iso->actual_length;
849         }
850 }
851
852
853 /* must free buffer */
854 void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
855 {
856         void *buff;
857         struct usbip_iso_packet_descriptor *iso;
858         int np = urb->number_of_packets;
859         ssize_t size = np * sizeof(*iso);
860         int i;
861
862         buff = kzalloc(size, GFP_KERNEL);
863         if (!buff)
864                 return NULL;
865
866         for (i = 0; i < np; i++) {
867                 iso = buff + (i * sizeof(*iso));
868
869                 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
870                 usbip_iso_pakcet_correct_endian(iso, 1);
871         }
872
873         *bufflen = size;
874
875         return buff;
876 }
877 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
878
879 /* some members of urb must be substituted before. */
880 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
881 {
882         void *buff;
883         struct usbip_iso_packet_descriptor *iso;
884         int np = urb->number_of_packets;
885         int size = np * sizeof(*iso);
886         int i;
887         int ret;
888
889         if (!usb_pipeisoc(urb->pipe))
890                 return 0;
891
892         /* my Bluetooth dongle gets ISO URBs which are np = 0 */
893         if (np == 0) {
894                 /* uinfo("iso np == 0\n"); */
895                 /* usbip_dump_urb(urb); */
896                 return 0;
897         }
898
899         buff = kzalloc(size, GFP_KERNEL);
900         if (!buff)
901                 return -ENOMEM;
902
903         ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0);
904         if (ret != size) {
905                 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
906                         ret);
907                 kfree(buff);
908
909                 if (ud->side == USBIP_STUB)
910                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
911                 else
912                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
913
914                 return -EPIPE;
915         }
916
917         for (i = 0; i < np; i++) {
918                 iso = buff + (i * sizeof(*iso));
919
920                 usbip_iso_pakcet_correct_endian(iso, 0);
921                 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
922         }
923
924
925         kfree(buff);
926
927         return ret;
928 }
929 EXPORT_SYMBOL_GPL(usbip_recv_iso);
930
931
932 /* some members of urb must be substituted before. */
933 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
934 {
935         int ret;
936         int size;
937
938         if (ud->side == USBIP_STUB) {
939                 /* stub_rx.c */
940                 /* the direction of urb must be OUT. */
941                 if (usb_pipein(urb->pipe))
942                         return 0;
943
944                 size = urb->transfer_buffer_length;
945         } else {
946                 /* vhci_rx.c */
947                 /* the direction of urb must be IN. */
948                 if (usb_pipeout(urb->pipe))
949                         return 0;
950
951                 size = urb->actual_length;
952         }
953
954         /* no need to recv xbuff */
955         if (!(size > 0))
956                 return 0;
957
958         ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer,
959                          size, 0);
960         if (ret != size) {
961                 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
962                 if (ud->side == USBIP_STUB) {
963                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
964                 } else {
965                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
966                         return -EPIPE;
967                 }
968         }
969
970         return ret;
971 }
972 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
973
974
975 /*-------------------------------------------------------------------------*/
976
977 static int __init usbip_common_init(void)
978 {
979         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "" DRIVER_VERSION);
980
981         return 0;
982 }
983
984 static void __exit usbip_common_exit(void)
985 {
986         return;
987 }
988
989
990
991
992 module_init(usbip_common_init);
993 module_exit(usbip_common_exit);
994
995 MODULE_AUTHOR(DRIVER_AUTHOR);
996 MODULE_DESCRIPTION(DRIVER_DESC);
997 MODULE_LICENSE("GPL");