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