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