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