blob: 7a45da8f9565e3731538095531402ad5833d87d6 [file] [log] [blame]
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06001/*
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>
Jean Delvare1d03d2b2009-07-13 12:39:05 +020021#include <linux/smp_lock.h>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060022#include <linux/file.h>
23#include <linux/tcp.h>
24#include <linux/in.h>
Brian G. Merrellb8868e42009-07-21 00:46:13 -060025#include <linux/kthread.h>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060026#include "usbip_common.h"
27
28/* version information */
29#define DRIVER_VERSION "1.0"
30#define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi _at_ users.sourceforge.net>"
31#define DRIVER_DESC "usbip common driver"
32
33/*-------------------------------------------------------------------------*/
34/* debug routines */
35
Himanshu411a8612010-01-23 17:52:02 +053036#ifdef CONFIG_USB_IP_DEBUG_ENABLE
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060037unsigned long usbip_debug_flag = 0xffffffff;
38#else
39unsigned long usbip_debug_flag;
40#endif
41EXPORT_SYMBOL_GPL(usbip_debug_flag);
42
43
44/* FIXME */
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{
Himanshu Chauhan1e5065db2010-01-23 01:51:57 +053058 sscanf(buf, "%lx", &usbip_debug_flag);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060059
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{
Himanshu Chauhanaad86572010-01-23 02:52:41 +053066 print_hex_dump(KERN_DEBUG, "usb-ip", DUMP_PREFIX_OFFSET, 16, 4,
67 buff, bufflen, false);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060068}
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
Brian G. Merrellb8868e42009-07-21 00:46:13 -060077 printk(KERN_DEBUG "dev(%d) ", dev);
78 printk(KERN_DEBUG "ep(%d) ", ep);
79 printk(KERN_DEBUG "%s ", dir ? "IN" : "OUT");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060080
81 switch (type) {
82 case PIPE_ISOCHRONOUS:
Brian G. Merrellb8868e42009-07-21 00:46:13 -060083 printk(KERN_DEBUG "%s ", "ISO");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060084 break;
85 case PIPE_INTERRUPT:
Brian G. Merrellb8868e42009-07-21 00:46:13 -060086 printk(KERN_DEBUG "%s ", "INT");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060087 break;
88 case PIPE_CONTROL:
Brian G. Merrellb8868e42009-07-21 00:46:13 -060089 printk(KERN_DEBUG "%s ", "CTL");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060090 break;
91 case PIPE_BULK:
Brian G. Merrellb8868e42009-07-21 00:46:13 -060092 printk(KERN_DEBUG "%s ", "BLK");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060093 break;
94 default:
Brian G. Merrellb8868e42009-07-21 00:46:13 -060095 printk(KERN_DEBUG "ERR");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060096 }
97
Brian G. Merrellb8868e42009-07-21 00:46:13 -060098 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060099
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:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600112 printk(KERN_DEBUG " SPD_HIGH");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600113 break;
114 case USB_SPEED_FULL:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600115 printk(KERN_DEBUG " SPD_FULL");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600116 break;
117 case USB_SPEED_LOW:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600118 printk(KERN_DEBUG " SPD_LOW");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600119 break;
120 case USB_SPEED_UNKNOWN:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600121 printk(KERN_DEBUG " SPD_UNKNOWN");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600122 break;
123 default:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600124 printk(KERN_DEBUG " SPD_ERROR");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600125 }
126
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600127 printk(KERN_DEBUG " tt %p, ttport %d", udev->tt, udev->ttport);
128 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600129
130 dev_dbg(dev, " ");
131 for (i = 0; i < 16; i++)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600132 printk(KERN_DEBUG " %2u", i);
133 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600134
135 dev_dbg(dev, " toggle0(IN) :");
136 for (i = 0; i < 16; i++)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600137 printk(KERN_DEBUG " %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
138 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600139
140 dev_dbg(dev, " toggle1(OUT):");
141 for (i = 0; i < 16; i++)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600142 printk(KERN_DEBUG " %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
143 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600144
145
146 dev_dbg(dev, " epmaxp_in :");
147 for (i = 0; i < 16; i++) {
148 if (udev->ep_in[i])
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600149 printk(KERN_DEBUG " %2u",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600150 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
151 }
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600152 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600153
154 dev_dbg(dev, " epmaxp_out :");
155 for (i = 0; i < 16; i++) {
156 if (udev->ep_out[i])
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600157 printk(KERN_DEBUG " %2u",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600158 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
159 }
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600160 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600161
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:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600179 printk(KERN_DEBUG "DEVICE");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600180 break;
181 case USB_RECIP_INTERFACE:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600182 printk(KERN_DEBUG "INTERF");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600183 break;
184 case USB_RECIP_ENDPOINT:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600185 printk(KERN_DEBUG "ENDPOI");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600186 break;
187 case USB_RECIP_OTHER:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600188 printk(KERN_DEBUG "OTHER ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600189 break;
190 default:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600191 printk(KERN_DEBUG "------");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600192 }
193}
194
195static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
196{
197 if (!cmd) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600198 printk(KERN_DEBUG " %s : null pointer\n", __func__);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600199 return;
200 }
201
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600202 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);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600208
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600209 printk(KERN_DEBUG "\n ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600210
211 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600212 printk(KERN_DEBUG "STANDARD ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600213 switch (cmd->bRequest) {
214 case USB_REQ_GET_STATUS:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600215 printk(KERN_DEBUG "GET_STATUS");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600216 break;
217 case USB_REQ_CLEAR_FEATURE:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600218 printk(KERN_DEBUG "CLEAR_FEAT");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600219 break;
220 case USB_REQ_SET_FEATURE:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600221 printk(KERN_DEBUG "SET_FEAT ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600222 break;
223 case USB_REQ_SET_ADDRESS:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600224 printk(KERN_DEBUG "SET_ADDRRS");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600225 break;
226 case USB_REQ_GET_DESCRIPTOR:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600227 printk(KERN_DEBUG "GET_DESCRI");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600228 break;
229 case USB_REQ_SET_DESCRIPTOR:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600230 printk(KERN_DEBUG "SET_DESCRI");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600231 break;
232 case USB_REQ_GET_CONFIGURATION:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600233 printk(KERN_DEBUG "GET_CONFIG");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600234 break;
235 case USB_REQ_SET_CONFIGURATION:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600236 printk(KERN_DEBUG "SET_CONFIG");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600237 break;
238 case USB_REQ_GET_INTERFACE:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600239 printk(KERN_DEBUG "GET_INTERF");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600240 break;
241 case USB_REQ_SET_INTERFACE:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600242 printk(KERN_DEBUG "SET_INTERF");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600243 break;
244 case USB_REQ_SYNCH_FRAME:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600245 printk(KERN_DEBUG "SYNC_FRAME");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600246 break;
247 default:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600248 printk(KERN_DEBUG "REQ(%02X) ", cmd->bRequest);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600249 }
250
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600251 printk(KERN_DEBUG " ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600252 usbip_dump_request_type(cmd->bRequestType);
253
254 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600255 printk(KERN_DEBUG "CLASS ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600256
257 else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600258 printk(KERN_DEBUG "VENDOR ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600259
260 else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600261 printk(KERN_DEBUG "RESERVED");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600262
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600263 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600264}
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);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600295 dev_dbg(dev, " transfer_buffer_length:%d\n",
296 urb->transfer_buffer_length);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600297 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{
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600315 usbip_udbg("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600316 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:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600324 usbip_udbg("CMD_SUBMIT: "
325 "x_flags %u x_len %u sf %u #p %u iv %u\n",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600326 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:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600333 usbip_udbg("CMD_UNLINK: seq %u\n", pdu->u.cmd_unlink.seqnum);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600334 break;
335 case USBIP_RET_SUBMIT:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600336 usbip_udbg("RET_SUBMIT: st %d al %u sf %d ec %d\n",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600337 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:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600342 usbip_udbg("RET_UNLINK: status %d\n", pdu->u.ret_unlink.status);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600343 break;
344 default:
345 /* NOT REACHED */
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600346 usbip_udbg("UNKNOWN\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600347 }
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
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600380int usbip_start_threads(struct usbip_device *ud)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600381{
382 /*
383 * threads are invoked per one device (per one connection).
384 */
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600385 struct task_struct *th;
Roel Kluin05d6d672008-12-19 23:37:30 +0100386
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600387 th = kthread_run(usbip_thread, (void *)&ud->tcp_rx, "usbip");
388 if (IS_ERR(th)) {
389 printk(KERN_WARNING
390 "Unable to start control thread\n");
391 return PTR_ERR(th);
Roel Kluin05d6d672008-12-19 23:37:30 +0100392 }
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600393 th = kthread_run(usbip_thread, (void *)&ud->tcp_tx, "usbip");
394 if (IS_ERR(th)) {
395 printk(KERN_WARNING
396 "Unable to start control thread\n");
397 return PTR_ERR(th);
Roel Kluin05d6d672008-12-19 23:37:30 +0100398 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600399
400 /* confirm threads are starting */
401 wait_for_completion(&ud->tcp_rx.thread_done);
402 wait_for_completion(&ud->tcp_tx.thread_done);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600403 return 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600404}
405EXPORT_SYMBOL_GPL(usbip_start_threads);
406
407void usbip_stop_threads(struct usbip_device *ud)
408{
409 /* kill threads related to this sdev, if v.c. exists */
410 if (ud->tcp_rx.thread != NULL) {
411 send_sig(SIGKILL, ud->tcp_rx.thread, 1);
412 wait_for_completion(&ud->tcp_rx.thread_done);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600413 usbip_udbg("rx_thread for ud %p has finished\n", ud);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600414 }
415
416 if (ud->tcp_tx.thread != NULL) {
417 send_sig(SIGKILL, ud->tcp_tx.thread, 1);
418 wait_for_completion(&ud->tcp_tx.thread_done);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600419 usbip_udbg("tx_thread for ud %p has finished\n", ud);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600420 }
421}
422EXPORT_SYMBOL_GPL(usbip_stop_threads);
423
424void usbip_task_init(struct usbip_task *ut, char *name,
425 void (*loop_ops)(struct usbip_task *))
426{
427 ut->thread = NULL;
428 init_completion(&ut->thread_done);
429 ut->name = name;
430 ut->loop_ops = loop_ops;
431}
432EXPORT_SYMBOL_GPL(usbip_task_init);
433
434
435/*-------------------------------------------------------------------------*/
436/* socket routines */
437
438 /* Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
439int usbip_xmit(int send, struct socket *sock, char *buf,
440 int size, int msg_flags)
441{
442 int result;
443 struct msghdr msg;
444 struct kvec iov;
445 int total = 0;
446
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600447 /* for blocks of if (usbip_dbg_flag_xmit) */
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600448 char *bp = buf;
449 int osize = size;
450
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600451 usbip_dbg_xmit("enter\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600452
453 if (!sock || !buf || !size) {
454 printk(KERN_ERR "%s: invalid arg, sock %p buff %p size %d\n",
455 __func__, sock, buf, size);
456 return -EINVAL;
457 }
458
459
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600460 if (usbip_dbg_flag_xmit) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600461 if (send) {
462 if (!in_interrupt())
463 printk(KERN_DEBUG "%-10s:", current->comm);
464 else
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600465 printk(KERN_DEBUG "interrupt :");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600466
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600467 printk(KERN_DEBUG "%s: sending... , sock %p, buf %p, "
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600468 "size %d, msg_flags %d\n", __func__,
469 sock, buf, size, msg_flags);
470 usbip_dump_buffer(buf, size);
471 }
472 }
473
474
475 do {
476 sock->sk->sk_allocation = GFP_NOIO;
477 iov.iov_base = buf;
478 iov.iov_len = size;
479 msg.msg_name = NULL;
480 msg.msg_namelen = 0;
481 msg.msg_control = NULL;
482 msg.msg_controllen = 0;
483 msg.msg_namelen = 0;
484 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
485
486 if (send)
487 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
488 else
489 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
490 MSG_WAITALL);
491
492 if (result <= 0) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600493 usbip_udbg("usbip_xmit: %s sock %p buf %p size %u ret "
494 "%d total %d\n",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600495 send ? "send" : "receive", sock, buf,
496 size, result, total);
497 goto err;
498 }
499
500 size -= result;
501 buf += result;
502 total += result;
503
504 } while (size > 0);
505
506
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600507 if (usbip_dbg_flag_xmit) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600508 if (!send) {
509 if (!in_interrupt())
510 printk(KERN_DEBUG "%-10s:", current->comm);
511 else
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600512 printk(KERN_DEBUG "interrupt :");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600513
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600514 printk(KERN_DEBUG "usbip_xmit: receiving....\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600515 usbip_dump_buffer(bp, osize);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600516 printk(KERN_DEBUG "usbip_xmit: received, osize %d ret "
517 "%d size %d total %d\n", osize, result,
518 size, total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600519 }
520
521 if (send)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600522 printk(KERN_DEBUG "usbip_xmit: send, total %d\n",
523 total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600524 }
525
526 return total;
527
528err:
529 return result;
530}
531EXPORT_SYMBOL_GPL(usbip_xmit);
532
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600533struct socket *sockfd_to_socket(unsigned int sockfd)
534{
535 struct socket *socket;
536 struct file *file;
537 struct inode *inode;
538
539 file = fget(sockfd);
540 if (!file) {
541 printk(KERN_ERR "%s: invalid sockfd\n", __func__);
542 return NULL;
543 }
544
545 inode = file->f_dentry->d_inode;
546
547 if (!inode || !S_ISSOCK(inode->i_mode))
548 return NULL;
549
550 socket = SOCKET_I(inode);
551
552 return socket;
553}
554EXPORT_SYMBOL_GPL(sockfd_to_socket);
555
556
557
558/*-------------------------------------------------------------------------*/
559/* pdu routines */
560
561/* there may be more cases to tweak the flags. */
562static unsigned int tweak_transfer_flags(unsigned int flags)
563{
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600564 flags &= ~(URB_NO_TRANSFER_DMA_MAP|URB_NO_SETUP_DMA_MAP);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600565 return flags;
566}
567
568static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
569 int pack)
570{
571 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
572
573 /*
574 * Some members are not still implemented in usbip. I hope this issue
575 * will be discussed when usbip is ported to other operating systems.
576 */
577 if (pack) {
578 /* vhci_tx.c */
579 spdu->transfer_flags =
580 tweak_transfer_flags(urb->transfer_flags);
581 spdu->transfer_buffer_length = urb->transfer_buffer_length;
582 spdu->start_frame = urb->start_frame;
583 spdu->number_of_packets = urb->number_of_packets;
584 spdu->interval = urb->interval;
585 } else {
586 /* stub_rx.c */
587 urb->transfer_flags = spdu->transfer_flags;
588
589 urb->transfer_buffer_length = spdu->transfer_buffer_length;
590 urb->start_frame = spdu->start_frame;
591 urb->number_of_packets = spdu->number_of_packets;
592 urb->interval = spdu->interval;
593 }
594}
595
596static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
597 int pack)
598{
599 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
600
601 if (pack) {
602 /* stub_tx.c */
603
604 rpdu->status = urb->status;
605 rpdu->actual_length = urb->actual_length;
606 rpdu->start_frame = urb->start_frame;
607 rpdu->error_count = urb->error_count;
608 } else {
609 /* vhci_rx.c */
610
611 urb->status = rpdu->status;
612 urb->actual_length = rpdu->actual_length;
613 urb->start_frame = rpdu->start_frame;
614 urb->error_count = rpdu->error_count;
615 }
616}
617
618
619void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
620 int pack)
621{
622 switch (cmd) {
623 case USBIP_CMD_SUBMIT:
624 usbip_pack_cmd_submit(pdu, urb, pack);
625 break;
626 case USBIP_RET_SUBMIT:
627 usbip_pack_ret_submit(pdu, urb, pack);
628 break;
629 default:
630 err("unknown command");
631 /* NOTREACHED */
632 /* BUG(); */
633 }
634}
635EXPORT_SYMBOL_GPL(usbip_pack_pdu);
636
637
638static void correct_endian_basic(struct usbip_header_basic *base, int send)
639{
640 if (send) {
641 base->command = cpu_to_be32(base->command);
642 base->seqnum = cpu_to_be32(base->seqnum);
643 base->devid = cpu_to_be32(base->devid);
644 base->direction = cpu_to_be32(base->direction);
645 base->ep = cpu_to_be32(base->ep);
646 } else {
647 base->command = be32_to_cpu(base->command);
648 base->seqnum = be32_to_cpu(base->seqnum);
649 base->devid = be32_to_cpu(base->devid);
650 base->direction = be32_to_cpu(base->direction);
651 base->ep = be32_to_cpu(base->ep);
652 }
653}
654
655static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
656 int send)
657{
658 if (send) {
659 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
660
661 cpu_to_be32s(&pdu->transfer_buffer_length);
662 cpu_to_be32s(&pdu->start_frame);
663 cpu_to_be32s(&pdu->number_of_packets);
664 cpu_to_be32s(&pdu->interval);
665 } else {
666 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
667
668 be32_to_cpus(&pdu->transfer_buffer_length);
669 be32_to_cpus(&pdu->start_frame);
670 be32_to_cpus(&pdu->number_of_packets);
671 be32_to_cpus(&pdu->interval);
672 }
673}
674
675static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
676 int send)
677{
678 if (send) {
679 cpu_to_be32s(&pdu->status);
680 cpu_to_be32s(&pdu->actual_length);
681 cpu_to_be32s(&pdu->start_frame);
682 cpu_to_be32s(&pdu->error_count);
683 } else {
684 be32_to_cpus(&pdu->status);
685 be32_to_cpus(&pdu->actual_length);
686 be32_to_cpus(&pdu->start_frame);
687 be32_to_cpus(&pdu->error_count);
688 }
689}
690
691static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
692 int send)
693{
694 if (send)
695 pdu->seqnum = cpu_to_be32(pdu->seqnum);
696 else
697 pdu->seqnum = be32_to_cpu(pdu->seqnum);
698}
699
700static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
701 int send)
702{
703 if (send)
704 cpu_to_be32s(&pdu->status);
705 else
706 be32_to_cpus(&pdu->status);
707}
708
709void usbip_header_correct_endian(struct usbip_header *pdu, int send)
710{
711 __u32 cmd = 0;
712
713 if (send)
714 cmd = pdu->base.command;
715
716 correct_endian_basic(&pdu->base, send);
717
718 if (!send)
719 cmd = pdu->base.command;
720
721 switch (cmd) {
722 case USBIP_CMD_SUBMIT:
723 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
724 break;
725 case USBIP_RET_SUBMIT:
726 correct_endian_ret_submit(&pdu->u.ret_submit, send);
727 break;
728 case USBIP_CMD_UNLINK:
729 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
730 break;
731 case USBIP_RET_UNLINK:
732 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
733 break;
734 default:
735 /* NOTREACHED */
736 err("unknown command in pdu header: %d", cmd);
737 /* BUG(); */
738 }
739}
740EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
741
742static void usbip_iso_pakcet_correct_endian(
743 struct usbip_iso_packet_descriptor *iso,
744 int send)
745{
746 /* does not need all members. but copy all simply. */
747 if (send) {
748 iso->offset = cpu_to_be32(iso->offset);
749 iso->length = cpu_to_be32(iso->length);
750 iso->status = cpu_to_be32(iso->status);
751 iso->actual_length = cpu_to_be32(iso->actual_length);
752 } else {
753 iso->offset = be32_to_cpu(iso->offset);
754 iso->length = be32_to_cpu(iso->length);
755 iso->status = be32_to_cpu(iso->status);
756 iso->actual_length = be32_to_cpu(iso->actual_length);
757 }
758}
759
760static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
761 struct usb_iso_packet_descriptor *uiso, int pack)
762{
763 if (pack) {
764 iso->offset = uiso->offset;
765 iso->length = uiso->length;
766 iso->status = uiso->status;
767 iso->actual_length = uiso->actual_length;
768 } else {
769 uiso->offset = iso->offset;
770 uiso->length = iso->length;
771 uiso->status = iso->status;
772 uiso->actual_length = iso->actual_length;
773 }
774}
775
776
777/* must free buffer */
778void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
779{
780 void *buff;
781 struct usbip_iso_packet_descriptor *iso;
782 int np = urb->number_of_packets;
783 ssize_t size = np * sizeof(*iso);
784 int i;
785
786 buff = kzalloc(size, GFP_KERNEL);
787 if (!buff)
788 return NULL;
789
790 for (i = 0; i < np; i++) {
791 iso = buff + (i * sizeof(*iso));
792
793 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
794 usbip_iso_pakcet_correct_endian(iso, 1);
795 }
796
797 *bufflen = size;
798
799 return buff;
800}
801EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
802
803/* some members of urb must be substituted before. */
804int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
805{
806 void *buff;
807 struct usbip_iso_packet_descriptor *iso;
808 int np = urb->number_of_packets;
809 int size = np * sizeof(*iso);
810 int i;
811 int ret;
812
813 if (!usb_pipeisoc(urb->pipe))
814 return 0;
815
816 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
817 if (np == 0) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600818 /* usbip_uinfo("iso np == 0\n"); */
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600819 /* usbip_dump_urb(urb); */
820 return 0;
821 }
822
823 buff = kzalloc(size, GFP_KERNEL);
824 if (!buff)
825 return -ENOMEM;
826
827 ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0);
828 if (ret != size) {
829 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
830 ret);
831 kfree(buff);
832
833 if (ud->side == USBIP_STUB)
834 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
835 else
836 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
837
838 return -EPIPE;
839 }
840
841 for (i = 0; i < np; i++) {
842 iso = buff + (i * sizeof(*iso));
843
844 usbip_iso_pakcet_correct_endian(iso, 0);
845 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
846 }
847
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600848 kfree(buff);
849
850 return ret;
851}
852EXPORT_SYMBOL_GPL(usbip_recv_iso);
853
854
855/* some members of urb must be substituted before. */
856int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
857{
858 int ret;
859 int size;
860
861 if (ud->side == USBIP_STUB) {
862 /* stub_rx.c */
863 /* the direction of urb must be OUT. */
864 if (usb_pipein(urb->pipe))
865 return 0;
866
867 size = urb->transfer_buffer_length;
868 } else {
869 /* vhci_rx.c */
870 /* the direction of urb must be IN. */
871 if (usb_pipeout(urb->pipe))
872 return 0;
873
874 size = urb->actual_length;
875 }
876
877 /* no need to recv xbuff */
878 if (!(size > 0))
879 return 0;
880
881 ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer,
882 size, 0);
883 if (ret != size) {
884 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
885 if (ud->side == USBIP_STUB) {
886 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
887 } else {
888 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
889 return -EPIPE;
890 }
891 }
892
893 return ret;
894}
895EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
896
897
898/*-------------------------------------------------------------------------*/
899
900static int __init usbip_common_init(void)
901{
902 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "" DRIVER_VERSION);
903
904 return 0;
905}
906
907static void __exit usbip_common_exit(void)
908{
909 return;
910}
911
912
913
914
915module_init(usbip_common_init);
916module_exit(usbip_common_exit);
917
918MODULE_AUTHOR(DRIVER_AUTHOR);
919MODULE_DESCRIPTION(DRIVER_DESC);
920MODULE_LICENSE("GPL");