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