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