blob: e3fa4216c1cdaf0552f1e8f45c9e826bbf35f622 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060027#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
Himanshu411a8612010-01-23 17:52:02 +053037#ifdef CONFIG_USB_IP_DEBUG_ENABLE
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060038unsigned long usbip_debug_flag = 0xffffffff;
39#else
40unsigned long usbip_debug_flag;
41#endif
42EXPORT_SYMBOL_GPL(usbip_debug_flag);
43
44
45/* FIXME */
46struct device_attribute dev_attr_usbip_debug;
47EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
48
49
50static 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
56static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
57 const char *buf, size_t count)
58{
Himanshu Chauhan1e5065db2010-01-23 01:51:57 +053059 sscanf(buf, "%lx", &usbip_debug_flag);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060060
61 return count;
62}
63DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
64
65static void usbip_dump_buffer(char *buff, int bufflen)
66{
Himanshu Chauhanaad86572010-01-23 02:52:41 +053067 print_hex_dump(KERN_DEBUG, "usb-ip", DUMP_PREFIX_OFFSET, 16, 4,
68 buff, bufflen, false);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060069}
70
71static 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
Brian G. Merrellb8868e42009-07-21 00:46:13 -060078 printk(KERN_DEBUG "dev(%d) ", dev);
79 printk(KERN_DEBUG "ep(%d) ", ep);
80 printk(KERN_DEBUG "%s ", dir ? "IN" : "OUT");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060081
82 switch (type) {
83 case PIPE_ISOCHRONOUS:
Brian G. Merrellb8868e42009-07-21 00:46:13 -060084 printk(KERN_DEBUG "%s ", "ISO");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060085 break;
86 case PIPE_INTERRUPT:
Brian G. Merrellb8868e42009-07-21 00:46:13 -060087 printk(KERN_DEBUG "%s ", "INT");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060088 break;
89 case PIPE_CONTROL:
Brian G. Merrellb8868e42009-07-21 00:46:13 -060090 printk(KERN_DEBUG "%s ", "CTL");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060091 break;
92 case PIPE_BULK:
Brian G. Merrellb8868e42009-07-21 00:46:13 -060093 printk(KERN_DEBUG "%s ", "BLK");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060094 break;
95 default:
Brian G. Merrellb8868e42009-07-21 00:46:13 -060096 printk(KERN_DEBUG "ERR");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060097 }
98
Brian G. Merrellb8868e42009-07-21 00:46:13 -060099 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600100
101}
102
103static 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:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600113 printk(KERN_DEBUG " SPD_HIGH");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600114 break;
115 case USB_SPEED_FULL:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600116 printk(KERN_DEBUG " SPD_FULL");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600117 break;
118 case USB_SPEED_LOW:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600119 printk(KERN_DEBUG " SPD_LOW");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600120 break;
121 case USB_SPEED_UNKNOWN:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600122 printk(KERN_DEBUG " SPD_UNKNOWN");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600123 break;
124 default:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600125 printk(KERN_DEBUG " SPD_ERROR");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600126 }
127
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600128 printk(KERN_DEBUG " tt %p, ttport %d", udev->tt, udev->ttport);
129 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600130
131 dev_dbg(dev, " ");
132 for (i = 0; i < 16; i++)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600133 printk(KERN_DEBUG " %2u", i);
134 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600135
136 dev_dbg(dev, " toggle0(IN) :");
137 for (i = 0; i < 16; i++)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600138 printk(KERN_DEBUG " %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
139 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600140
141 dev_dbg(dev, " toggle1(OUT):");
142 for (i = 0; i < 16; i++)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600143 printk(KERN_DEBUG " %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
144 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600145
146
147 dev_dbg(dev, " epmaxp_in :");
148 for (i = 0; i < 16; i++) {
149 if (udev->ep_in[i])
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600150 printk(KERN_DEBUG " %2u",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600151 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
152 }
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600153 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600154
155 dev_dbg(dev, " epmaxp_out :");
156 for (i = 0; i < 16; i++) {
157 if (udev->ep_out[i])
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600158 printk(KERN_DEBUG " %2u",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600159 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
160 }
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600161 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600162
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
176static void usbip_dump_request_type(__u8 rt)
177{
178 switch (rt & USB_RECIP_MASK) {
179 case USB_RECIP_DEVICE:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600180 printk(KERN_DEBUG "DEVICE");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600181 break;
182 case USB_RECIP_INTERFACE:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600183 printk(KERN_DEBUG "INTERF");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600184 break;
185 case USB_RECIP_ENDPOINT:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600186 printk(KERN_DEBUG "ENDPOI");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600187 break;
188 case USB_RECIP_OTHER:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600189 printk(KERN_DEBUG "OTHER ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600190 break;
191 default:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600192 printk(KERN_DEBUG "------");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600193 }
194}
195
196static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
197{
198 if (!cmd) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600199 printk(KERN_DEBUG " %s : null pointer\n", __func__);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600200 return;
201 }
202
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600203 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);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600209
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600210 printk(KERN_DEBUG "\n ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600211
212 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600213 printk(KERN_DEBUG "STANDARD ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600214 switch (cmd->bRequest) {
215 case USB_REQ_GET_STATUS:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600216 printk(KERN_DEBUG "GET_STATUS");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600217 break;
218 case USB_REQ_CLEAR_FEATURE:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600219 printk(KERN_DEBUG "CLEAR_FEAT");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600220 break;
221 case USB_REQ_SET_FEATURE:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600222 printk(KERN_DEBUG "SET_FEAT ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600223 break;
224 case USB_REQ_SET_ADDRESS:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600225 printk(KERN_DEBUG "SET_ADDRRS");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600226 break;
227 case USB_REQ_GET_DESCRIPTOR:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600228 printk(KERN_DEBUG "GET_DESCRI");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600229 break;
230 case USB_REQ_SET_DESCRIPTOR:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600231 printk(KERN_DEBUG "SET_DESCRI");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600232 break;
233 case USB_REQ_GET_CONFIGURATION:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600234 printk(KERN_DEBUG "GET_CONFIG");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600235 break;
236 case USB_REQ_SET_CONFIGURATION:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600237 printk(KERN_DEBUG "SET_CONFIG");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600238 break;
239 case USB_REQ_GET_INTERFACE:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600240 printk(KERN_DEBUG "GET_INTERF");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600241 break;
242 case USB_REQ_SET_INTERFACE:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600243 printk(KERN_DEBUG "SET_INTERF");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600244 break;
245 case USB_REQ_SYNCH_FRAME:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600246 printk(KERN_DEBUG "SYNC_FRAME");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600247 break;
248 default:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600249 printk(KERN_DEBUG "REQ(%02X) ", cmd->bRequest);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600250 }
251
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600252 printk(KERN_DEBUG " ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600253 usbip_dump_request_type(cmd->bRequestType);
254
255 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600256 printk(KERN_DEBUG "CLASS ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600257
258 else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600259 printk(KERN_DEBUG "VENDOR ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600260
261 else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600262 printk(KERN_DEBUG "RESERVED");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600263
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600264 printk(KERN_DEBUG "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600265}
266
267void 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);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600296 dev_dbg(dev, " transfer_buffer_length:%d\n",
297 urb->transfer_buffer_length);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600298 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}
312EXPORT_SYMBOL_GPL(usbip_dump_urb);
313
314void usbip_dump_header(struct usbip_header *pdu)
315{
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600316 usbip_udbg("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600317 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:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600325 usbip_udbg("CMD_SUBMIT: "
326 "x_flags %u x_len %u sf %u #p %u iv %u\n",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600327 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:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600334 usbip_udbg("CMD_UNLINK: seq %u\n", pdu->u.cmd_unlink.seqnum);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600335 break;
336 case USBIP_RET_SUBMIT:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600337 usbip_udbg("RET_SUBMIT: st %d al %u sf %d ec %d\n",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600338 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:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600343 usbip_udbg("RET_UNLINK: status %d\n", pdu->u.ret_unlink.status);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600344 break;
345 default:
346 /* NOT REACHED */
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600347 usbip_udbg("UNKNOWN\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600348 }
349}
350EXPORT_SYMBOL_GPL(usbip_dump_header);
351
352
353/*-------------------------------------------------------------------------*/
354/* thread routines */
355
356int 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
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600381int usbip_start_threads(struct usbip_device *ud)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600382{
383 /*
384 * threads are invoked per one device (per one connection).
385 */
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600386 struct task_struct *th;
Roel Kluin05d6d672008-12-19 23:37:30 +0100387
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600388 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);
Roel Kluin05d6d672008-12-19 23:37:30 +0100393 }
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600394 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);
Roel Kluin05d6d672008-12-19 23:37:30 +0100399 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600400
401 /* confirm threads are starting */
402 wait_for_completion(&ud->tcp_rx.thread_done);
403 wait_for_completion(&ud->tcp_tx.thread_done);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600404 return 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600405}
406EXPORT_SYMBOL_GPL(usbip_start_threads);
407
408void 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);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600414 usbip_udbg("rx_thread for ud %p has finished\n", ud);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600415 }
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);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600420 usbip_udbg("tx_thread for ud %p has finished\n", ud);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600421 }
422}
423EXPORT_SYMBOL_GPL(usbip_stop_threads);
424
425void 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}
433EXPORT_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 */
440int 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
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600448 /* for blocks of if (usbip_dbg_flag_xmit) */
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600449 char *bp = buf;
450 int osize = size;
451
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600452 usbip_dbg_xmit("enter\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600453
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
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600461 if (usbip_dbg_flag_xmit) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600462 if (send) {
463 if (!in_interrupt())
464 printk(KERN_DEBUG "%-10s:", current->comm);
465 else
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600466 printk(KERN_DEBUG "interrupt :");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600467
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600468 printk(KERN_DEBUG "%s: sending... , sock %p, buf %p, "
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600469 "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) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600494 usbip_udbg("usbip_xmit: %s sock %p buf %p size %u ret "
495 "%d total %d\n",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600496 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
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600508 if (usbip_dbg_flag_xmit) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600509 if (!send) {
510 if (!in_interrupt())
511 printk(KERN_DEBUG "%-10s:", current->comm);
512 else
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600513 printk(KERN_DEBUG "interrupt :");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600514
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600515 printk(KERN_DEBUG "usbip_xmit: receiving....\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600516 usbip_dump_buffer(bp, osize);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600517 printk(KERN_DEBUG "usbip_xmit: received, osize %d ret "
518 "%d size %d total %d\n", osize, result,
519 size, total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600520 }
521
522 if (send)
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600523 printk(KERN_DEBUG "usbip_xmit: send, total %d\n",
524 total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600525 }
526
527 return total;
528
529err:
530 return result;
531}
532EXPORT_SYMBOL_GPL(usbip_xmit);
533
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600534struct 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}
555EXPORT_SYMBOL_GPL(sockfd_to_socket);
556
557
558
559/*-------------------------------------------------------------------------*/
560/* pdu routines */
561
562/* there may be more cases to tweak the flags. */
563static unsigned int tweak_transfer_flags(unsigned int flags)
564{
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600565 flags &= ~(URB_NO_TRANSFER_DMA_MAP|URB_NO_SETUP_DMA_MAP);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600566 return flags;
567}
568
569static 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
597static 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
620void 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}
636EXPORT_SYMBOL_GPL(usbip_pack_pdu);
637
638
639static 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
656static 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
676static 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
692static 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
701static 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
710void 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}
741EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
742
743static 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
761static 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 */
779void *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}
802EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
803
804/* some members of urb must be substituted before. */
805int 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) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600819 /* usbip_uinfo("iso np == 0\n"); */
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600820 /* 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
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600849 kfree(buff);
850
851 return ret;
852}
853EXPORT_SYMBOL_GPL(usbip_recv_iso);
854
855
856/* some members of urb must be substituted before. */
857int 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}
896EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
897
898
899/*-------------------------------------------------------------------------*/
900
901static int __init usbip_common_init(void)
902{
903 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "" DRIVER_VERSION);
904
905 return 0;
906}
907
908static void __exit usbip_common_exit(void)
909{
910 return;
911}
912
913
914
915
916module_init(usbip_common_init);
917module_exit(usbip_common_exit);
918
919MODULE_AUTHOR(DRIVER_AUTHOR);
920MODULE_DESCRIPTION(DRIVER_DESC);
921MODULE_LICENSE("GPL");