]>
Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0+ |
05a1f28e TH |
2 | /* |
3 | * Copyright (C) 2003-2008 Takahiro Hirofuchi | |
c7af4c22 IK |
4 | * Copyright (C) 2015-2016 Samsung Electronics |
5 | * Krzysztof Opasiak <k.opasiak@samsung.com> | |
05a1f28e TH |
6 | */ |
7 | ||
7aaacb43 | 8 | #include <asm/byteorder.h> |
05a1f28e | 9 | #include <linux/file.h> |
7aaacb43 | 10 | #include <linux/fs.h> |
11 | #include <linux/kernel.h> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
93efc55b | 13 | #include <linux/stat.h> |
45296236 | 14 | #include <linux/module.h> |
93efc55b | 15 | #include <linux/moduleparam.h> |
7aaacb43 | 16 | #include <net/sock.h> |
4ce0a41f | 17 | |
05a1f28e TH |
18 | #include "usbip_common.h" |
19 | ||
4ce0a41f | 20 | #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>" |
64e62426 | 21 | #define DRIVER_DESC "USB/IP Core" |
05a1f28e | 22 | |
64e62426 | 23 | #ifdef CONFIG_USBIP_DEBUG |
05a1f28e TH |
24 | unsigned long usbip_debug_flag = 0xffffffff; |
25 | #else | |
26 | unsigned long usbip_debug_flag; | |
27 | #endif | |
28 | EXPORT_SYMBOL_GPL(usbip_debug_flag); | |
93efc55b TK |
29 | module_param(usbip_debug_flag, ulong, S_IRUGO|S_IWUSR); |
30 | MODULE_PARM_DESC(usbip_debug_flag, "debug flags (defined in usbip_common.h)"); | |
05a1f28e | 31 | |
05a1f28e TH |
32 | /* FIXME */ |
33 | struct device_attribute dev_attr_usbip_debug; | |
34 | EXPORT_SYMBOL_GPL(dev_attr_usbip_debug); | |
35 | ||
b1f56aca GKH |
36 | static ssize_t usbip_debug_show(struct device *dev, |
37 | struct device_attribute *attr, char *buf) | |
05a1f28e TH |
38 | { |
39 | return sprintf(buf, "%lx\n", usbip_debug_flag); | |
40 | } | |
41 | ||
b1f56aca GKH |
42 | static ssize_t usbip_debug_store(struct device *dev, |
43 | struct device_attribute *attr, const char *buf, | |
44 | size_t count) | |
05a1f28e | 45 | { |
bf988c16 JG |
46 | if (sscanf(buf, "%lx", &usbip_debug_flag) != 1) |
47 | return -EINVAL; | |
05a1f28e TH |
48 | return count; |
49 | } | |
b1f56aca | 50 | DEVICE_ATTR_RW(usbip_debug); |
05a1f28e TH |
51 | |
52 | static void usbip_dump_buffer(char *buff, int bufflen) | |
53 | { | |
1a4b6f66 | 54 | print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4, |
aad86577 | 55 | buff, bufflen, false); |
05a1f28e TH |
56 | } |
57 | ||
58 | static void usbip_dump_pipe(unsigned int p) | |
59 | { | |
60 | unsigned char type = usb_pipetype(p); | |
87352760 | 61 | unsigned char ep = usb_pipeendpoint(p); |
62 | unsigned char dev = usb_pipedevice(p); | |
63 | unsigned char dir = usb_pipein(p); | |
05a1f28e | 64 | |
1a4b6f66 | 65 | pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT"); |
05a1f28e TH |
66 | |
67 | switch (type) { | |
68 | case PIPE_ISOCHRONOUS: | |
1a4b6f66 | 69 | pr_debug("ISO\n"); |
05a1f28e TH |
70 | break; |
71 | case PIPE_INTERRUPT: | |
1a4b6f66 | 72 | pr_debug("INT\n"); |
05a1f28e TH |
73 | break; |
74 | case PIPE_CONTROL: | |
1a4b6f66 | 75 | pr_debug("CTRL\n"); |
05a1f28e TH |
76 | break; |
77 | case PIPE_BULK: | |
1a4b6f66 | 78 | pr_debug("BULK\n"); |
05a1f28e TH |
79 | break; |
80 | default: | |
1a4b6f66 | 81 | pr_debug("ERR\n"); |
49aecefc | 82 | break; |
05a1f28e | 83 | } |
05a1f28e TH |
84 | } |
85 | ||
86 | static void usbip_dump_usb_device(struct usb_device *udev) | |
87 | { | |
88 | struct device *dev = &udev->dev; | |
89 | int i; | |
90 | ||
09c8c8fb SK |
91 | dev_dbg(dev, " devnum(%d) devpath(%s) usb speed(%s)", |
92 | udev->devnum, udev->devpath, usb_speed_string(udev->speed)); | |
05a1f28e | 93 | |
e1346fd8 | 94 | pr_debug("tt hub ttport %d\n", udev->ttport); |
05a1f28e TH |
95 | |
96 | dev_dbg(dev, " "); | |
97 | for (i = 0; i < 16; i++) | |
1a4b6f66 | 98 | pr_debug(" %2u", i); |
99 | pr_debug("\n"); | |
05a1f28e TH |
100 | |
101 | dev_dbg(dev, " toggle0(IN) :"); | |
102 | for (i = 0; i < 16; i++) | |
1a4b6f66 | 103 | pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0); |
104 | pr_debug("\n"); | |
05a1f28e TH |
105 | |
106 | dev_dbg(dev, " toggle1(OUT):"); | |
107 | for (i = 0; i < 16; i++) | |
1a4b6f66 | 108 | pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0); |
109 | pr_debug("\n"); | |
05a1f28e TH |
110 | |
111 | dev_dbg(dev, " epmaxp_in :"); | |
112 | for (i = 0; i < 16; i++) { | |
113 | if (udev->ep_in[i]) | |
1a4b6f66 | 114 | pr_debug(" %2u", |
115 | le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize)); | |
05a1f28e | 116 | } |
1a4b6f66 | 117 | pr_debug("\n"); |
05a1f28e TH |
118 | |
119 | dev_dbg(dev, " epmaxp_out :"); | |
120 | for (i = 0; i < 16; i++) { | |
121 | if (udev->ep_out[i]) | |
1a4b6f66 | 122 | pr_debug(" %2u", |
123 | le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize)); | |
05a1f28e | 124 | } |
1a4b6f66 | 125 | pr_debug("\n"); |
05a1f28e | 126 | |
e1346fd8 SK |
127 | dev_dbg(dev, "parent %s, bus %s\n", dev_name(&udev->parent->dev), |
128 | udev->bus->bus_name); | |
05a1f28e TH |
129 | |
130 | dev_dbg(dev, "have_langid %d, string_langid %d\n", | |
131 | udev->have_langid, udev->string_langid); | |
132 | ||
ff823c79 | 133 | dev_dbg(dev, "maxchild %d\n", udev->maxchild); |
05a1f28e TH |
134 | } |
135 | ||
136 | static void usbip_dump_request_type(__u8 rt) | |
137 | { | |
138 | switch (rt & USB_RECIP_MASK) { | |
139 | case USB_RECIP_DEVICE: | |
1a4b6f66 | 140 | pr_debug("DEVICE"); |
05a1f28e TH |
141 | break; |
142 | case USB_RECIP_INTERFACE: | |
1a4b6f66 | 143 | pr_debug("INTERF"); |
05a1f28e TH |
144 | break; |
145 | case USB_RECIP_ENDPOINT: | |
1a4b6f66 | 146 | pr_debug("ENDPOI"); |
05a1f28e TH |
147 | break; |
148 | case USB_RECIP_OTHER: | |
1a4b6f66 | 149 | pr_debug("OTHER "); |
05a1f28e TH |
150 | break; |
151 | default: | |
1a4b6f66 | 152 | pr_debug("------"); |
49aecefc | 153 | break; |
05a1f28e TH |
154 | } |
155 | } | |
156 | ||
157 | static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd) | |
158 | { | |
159 | if (!cmd) { | |
1a4b6f66 | 160 | pr_debug(" : null pointer\n"); |
05a1f28e TH |
161 | return; |
162 | } | |
163 | ||
1a4b6f66 | 164 | pr_debug(" "); |
6bb3ee69 CC |
165 | pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ", |
166 | cmd->bRequestType, cmd->bRequest, | |
1a4b6f66 | 167 | cmd->wValue, cmd->wIndex, cmd->wLength); |
168 | pr_debug("\n "); | |
05a1f28e TH |
169 | |
170 | if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { | |
1a4b6f66 | 171 | pr_debug("STANDARD "); |
05a1f28e TH |
172 | switch (cmd->bRequest) { |
173 | case USB_REQ_GET_STATUS: | |
1a4b6f66 | 174 | pr_debug("GET_STATUS\n"); |
05a1f28e TH |
175 | break; |
176 | case USB_REQ_CLEAR_FEATURE: | |
1a4b6f66 | 177 | pr_debug("CLEAR_FEAT\n"); |
05a1f28e TH |
178 | break; |
179 | case USB_REQ_SET_FEATURE: | |
5ad7b85b | 180 | pr_debug("SET_FEAT\n"); |
05a1f28e TH |
181 | break; |
182 | case USB_REQ_SET_ADDRESS: | |
1a4b6f66 | 183 | pr_debug("SET_ADDRRS\n"); |
05a1f28e TH |
184 | break; |
185 | case USB_REQ_GET_DESCRIPTOR: | |
1a4b6f66 | 186 | pr_debug("GET_DESCRI\n"); |
05a1f28e TH |
187 | break; |
188 | case USB_REQ_SET_DESCRIPTOR: | |
1a4b6f66 | 189 | pr_debug("SET_DESCRI\n"); |
05a1f28e TH |
190 | break; |
191 | case USB_REQ_GET_CONFIGURATION: | |
1a4b6f66 | 192 | pr_debug("GET_CONFIG\n"); |
05a1f28e TH |
193 | break; |
194 | case USB_REQ_SET_CONFIGURATION: | |
1a4b6f66 | 195 | pr_debug("SET_CONFIG\n"); |
05a1f28e TH |
196 | break; |
197 | case USB_REQ_GET_INTERFACE: | |
1a4b6f66 | 198 | pr_debug("GET_INTERF\n"); |
05a1f28e TH |
199 | break; |
200 | case USB_REQ_SET_INTERFACE: | |
1a4b6f66 | 201 | pr_debug("SET_INTERF\n"); |
05a1f28e TH |
202 | break; |
203 | case USB_REQ_SYNCH_FRAME: | |
1a4b6f66 | 204 | pr_debug("SYNC_FRAME\n"); |
05a1f28e TH |
205 | break; |
206 | default: | |
5ad7b85b | 207 | pr_debug("REQ(%02X)\n", cmd->bRequest); |
49aecefc | 208 | break; |
05a1f28e | 209 | } |
05a1f28e | 210 | usbip_dump_request_type(cmd->bRequestType); |
1a4b6f66 | 211 | } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) { |
5ad7b85b | 212 | pr_debug("CLASS\n"); |
1a4b6f66 | 213 | } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) { |
5ad7b85b | 214 | pr_debug("VENDOR\n"); |
1a4b6f66 | 215 | } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) { |
216 | pr_debug("RESERVED\n"); | |
217 | } | |
05a1f28e TH |
218 | } |
219 | ||
220 | void usbip_dump_urb(struct urb *urb) | |
221 | { | |
222 | struct device *dev; | |
223 | ||
224 | if (!urb) { | |
1a4b6f66 | 225 | pr_debug("urb: null pointer!!\n"); |
05a1f28e TH |
226 | return; |
227 | } | |
228 | ||
229 | if (!urb->dev) { | |
1a4b6f66 | 230 | pr_debug("urb->dev: null pointer!!\n"); |
05a1f28e TH |
231 | return; |
232 | } | |
1a4b6f66 | 233 | |
05a1f28e TH |
234 | dev = &urb->dev->dev; |
235 | ||
05a1f28e TH |
236 | usbip_dump_usb_device(urb->dev); |
237 | ||
238 | dev_dbg(dev, " pipe :%08x ", urb->pipe); | |
239 | ||
240 | usbip_dump_pipe(urb->pipe); | |
241 | ||
242 | dev_dbg(dev, " status :%d\n", urb->status); | |
243 | dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags); | |
b8868e45 BM |
244 | dev_dbg(dev, " transfer_buffer_length:%d\n", |
245 | urb->transfer_buffer_length); | |
05a1f28e | 246 | dev_dbg(dev, " actual_length :%d\n", urb->actual_length); |
05a1f28e TH |
247 | |
248 | if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL) | |
f9eacc98 | 249 | usbip_dump_usb_ctrlrequest( |
05a1f28e TH |
250 | (struct usb_ctrlrequest *)urb->setup_packet); |
251 | ||
252 | dev_dbg(dev, " start_frame :%d\n", urb->start_frame); | |
253 | dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets); | |
254 | dev_dbg(dev, " interval :%d\n", urb->interval); | |
255 | dev_dbg(dev, " error_count :%d\n", urb->error_count); | |
05a1f28e TH |
256 | } |
257 | EXPORT_SYMBOL_GPL(usbip_dump_urb); | |
258 | ||
259 | void usbip_dump_header(struct usbip_header *pdu) | |
260 | { | |
1a4b6f66 | 261 | pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n", |
262 | pdu->base.command, | |
263 | pdu->base.seqnum, | |
264 | pdu->base.devid, | |
265 | pdu->base.direction, | |
266 | pdu->base.ep); | |
05a1f28e TH |
267 | |
268 | switch (pdu->base.command) { | |
269 | case USBIP_CMD_SUBMIT: | |
6bb3ee69 | 270 | pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n", |
1a4b6f66 | 271 | pdu->u.cmd_submit.transfer_flags, |
272 | pdu->u.cmd_submit.transfer_buffer_length, | |
273 | pdu->u.cmd_submit.start_frame, | |
274 | pdu->u.cmd_submit.number_of_packets, | |
275 | pdu->u.cmd_submit.interval); | |
f9eacc98 | 276 | break; |
05a1f28e | 277 | case USBIP_CMD_UNLINK: |
1a4b6f66 | 278 | pr_debug("USBIP_CMD_UNLINK: seq %u\n", |
279 | pdu->u.cmd_unlink.seqnum); | |
05a1f28e TH |
280 | break; |
281 | case USBIP_RET_SUBMIT: | |
1a4b6f66 | 282 | pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n", |
283 | pdu->u.ret_submit.status, | |
284 | pdu->u.ret_submit.actual_length, | |
285 | pdu->u.ret_submit.start_frame, | |
286 | pdu->u.ret_submit.number_of_packets, | |
287 | pdu->u.ret_submit.error_count); | |
288 | break; | |
05a1f28e | 289 | case USBIP_RET_UNLINK: |
1a4b6f66 | 290 | pr_debug("USBIP_RET_UNLINK: status %d\n", |
291 | pdu->u.ret_unlink.status); | |
05a1f28e TH |
292 | break; |
293 | default: | |
294 | /* NOT REACHED */ | |
1a4b6f66 | 295 | pr_err("unknown command\n"); |
49aecefc | 296 | break; |
05a1f28e TH |
297 | } |
298 | } | |
299 | EXPORT_SYMBOL_GPL(usbip_dump_header); | |
300 | ||
5a08c526 BW |
301 | /* Receive data over TCP/IP. */ |
302 | int usbip_recv(struct socket *sock, void *buf, int size) | |
05a1f28e TH |
303 | { |
304 | int result; | |
3995d161 AV |
305 | struct kvec iov = {.iov_base = buf, .iov_len = size}; |
306 | struct msghdr msg = {.msg_flags = MSG_NOSIGNAL}; | |
05a1f28e TH |
307 | int total = 0; |
308 | ||
90120d15 SK |
309 | if (!sock || !buf || !size) |
310 | return -EINVAL; | |
311 | ||
3995d161 | 312 | iov_iter_kvec(&msg.msg_iter, READ|ITER_KVEC, &iov, 1, size); |
05a1f28e | 313 | |
b8868e45 | 314 | usbip_dbg_xmit("enter\n"); |
05a1f28e | 315 | |
05a1f28e | 316 | do { |
90120d15 | 317 | msg_data_left(&msg); |
05a1f28e | 318 | sock->sk->sk_allocation = GFP_NOIO; |
3995d161 AV |
319 | |
320 | result = sock_recvmsg(sock, &msg, MSG_WAITALL); | |
90120d15 | 321 | if (result <= 0) |
05a1f28e | 322 | goto err; |
05a1f28e | 323 | |
05a1f28e | 324 | total += result; |
3995d161 | 325 | } while (msg_data_left(&msg)); |
05a1f28e | 326 | |
b8868e45 | 327 | if (usbip_dbg_flag_xmit) { |
5a08c526 BW |
328 | if (!in_interrupt()) |
329 | pr_debug("%-10s:", current->comm); | |
330 | else | |
331 | pr_debug("interrupt :"); | |
05a1f28e | 332 | |
5a08c526 | 333 | pr_debug("receiving....\n"); |
3995d161 AV |
334 | usbip_dump_buffer(buf, size); |
335 | pr_debug("received, osize %d ret %d size %zd total %d\n", | |
336 | size, result, msg_data_left(&msg), total); | |
05a1f28e TH |
337 | } |
338 | ||
339 | return total; | |
340 | ||
341 | err: | |
342 | return result; | |
343 | } | |
5a08c526 | 344 | EXPORT_SYMBOL_GPL(usbip_recv); |
05a1f28e | 345 | |
05a1f28e TH |
346 | /* there may be more cases to tweak the flags. */ |
347 | static unsigned int tweak_transfer_flags(unsigned int flags) | |
348 | { | |
85bcb5ee | 349 | flags &= ~URB_NO_TRANSFER_DMA_MAP; |
05a1f28e TH |
350 | return flags; |
351 | } | |
352 | ||
353 | static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb, | |
f9eacc98 | 354 | int pack) |
05a1f28e TH |
355 | { |
356 | struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit; | |
357 | ||
358 | /* | |
359 | * Some members are not still implemented in usbip. I hope this issue | |
360 | * will be discussed when usbip is ported to other operating systems. | |
361 | */ | |
362 | if (pack) { | |
05a1f28e | 363 | spdu->transfer_flags = |
f9eacc98 | 364 | tweak_transfer_flags(urb->transfer_flags); |
05a1f28e TH |
365 | spdu->transfer_buffer_length = urb->transfer_buffer_length; |
366 | spdu->start_frame = urb->start_frame; | |
367 | spdu->number_of_packets = urb->number_of_packets; | |
368 | spdu->interval = urb->interval; | |
369 | } else { | |
05a1f28e | 370 | urb->transfer_flags = spdu->transfer_flags; |
05a1f28e TH |
371 | urb->transfer_buffer_length = spdu->transfer_buffer_length; |
372 | urb->start_frame = spdu->start_frame; | |
373 | urb->number_of_packets = spdu->number_of_packets; | |
374 | urb->interval = spdu->interval; | |
375 | } | |
376 | } | |
377 | ||
378 | static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb, | |
f9eacc98 | 379 | int pack) |
05a1f28e TH |
380 | { |
381 | struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit; | |
382 | ||
383 | if (pack) { | |
05a1f28e TH |
384 | rpdu->status = urb->status; |
385 | rpdu->actual_length = urb->actual_length; | |
386 | rpdu->start_frame = urb->start_frame; | |
1325f85f | 387 | rpdu->number_of_packets = urb->number_of_packets; |
05a1f28e TH |
388 | rpdu->error_count = urb->error_count; |
389 | } else { | |
05a1f28e TH |
390 | urb->status = rpdu->status; |
391 | urb->actual_length = rpdu->actual_length; | |
392 | urb->start_frame = rpdu->start_frame; | |
1325f85f | 393 | urb->number_of_packets = rpdu->number_of_packets; |
05a1f28e TH |
394 | urb->error_count = rpdu->error_count; |
395 | } | |
396 | } | |
397 | ||
05a1f28e | 398 | void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd, |
f9eacc98 | 399 | int pack) |
05a1f28e TH |
400 | { |
401 | switch (cmd) { | |
402 | case USBIP_CMD_SUBMIT: | |
403 | usbip_pack_cmd_submit(pdu, urb, pack); | |
404 | break; | |
405 | case USBIP_RET_SUBMIT: | |
406 | usbip_pack_ret_submit(pdu, urb, pack); | |
407 | break; | |
408 | default: | |
49aecefc | 409 | /* NOT REACHED */ |
1a4b6f66 | 410 | pr_err("unknown command\n"); |
49aecefc | 411 | break; |
05a1f28e TH |
412 | } |
413 | } | |
414 | EXPORT_SYMBOL_GPL(usbip_pack_pdu); | |
415 | ||
05a1f28e TH |
416 | static void correct_endian_basic(struct usbip_header_basic *base, int send) |
417 | { | |
418 | if (send) { | |
419 | base->command = cpu_to_be32(base->command); | |
420 | base->seqnum = cpu_to_be32(base->seqnum); | |
421 | base->devid = cpu_to_be32(base->devid); | |
422 | base->direction = cpu_to_be32(base->direction); | |
423 | base->ep = cpu_to_be32(base->ep); | |
424 | } else { | |
425 | base->command = be32_to_cpu(base->command); | |
426 | base->seqnum = be32_to_cpu(base->seqnum); | |
427 | base->devid = be32_to_cpu(base->devid); | |
428 | base->direction = be32_to_cpu(base->direction); | |
429 | base->ep = be32_to_cpu(base->ep); | |
430 | } | |
431 | } | |
432 | ||
433 | static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu, | |
f9eacc98 | 434 | int send) |
05a1f28e TH |
435 | { |
436 | if (send) { | |
437 | pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags); | |
438 | ||
439 | cpu_to_be32s(&pdu->transfer_buffer_length); | |
440 | cpu_to_be32s(&pdu->start_frame); | |
441 | cpu_to_be32s(&pdu->number_of_packets); | |
442 | cpu_to_be32s(&pdu->interval); | |
443 | } else { | |
444 | pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags); | |
445 | ||
446 | be32_to_cpus(&pdu->transfer_buffer_length); | |
447 | be32_to_cpus(&pdu->start_frame); | |
448 | be32_to_cpus(&pdu->number_of_packets); | |
449 | be32_to_cpus(&pdu->interval); | |
450 | } | |
451 | } | |
452 | ||
453 | static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu, | |
f9eacc98 | 454 | int send) |
05a1f28e TH |
455 | { |
456 | if (send) { | |
457 | cpu_to_be32s(&pdu->status); | |
458 | cpu_to_be32s(&pdu->actual_length); | |
459 | cpu_to_be32s(&pdu->start_frame); | |
1325f85f | 460 | cpu_to_be32s(&pdu->number_of_packets); |
05a1f28e TH |
461 | cpu_to_be32s(&pdu->error_count); |
462 | } else { | |
463 | be32_to_cpus(&pdu->status); | |
464 | be32_to_cpus(&pdu->actual_length); | |
465 | be32_to_cpus(&pdu->start_frame); | |
cacd18a8 | 466 | be32_to_cpus(&pdu->number_of_packets); |
05a1f28e TH |
467 | be32_to_cpus(&pdu->error_count); |
468 | } | |
469 | } | |
470 | ||
471 | static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu, | |
f9eacc98 | 472 | int send) |
05a1f28e TH |
473 | { |
474 | if (send) | |
475 | pdu->seqnum = cpu_to_be32(pdu->seqnum); | |
476 | else | |
477 | pdu->seqnum = be32_to_cpu(pdu->seqnum); | |
478 | } | |
479 | ||
480 | static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu, | |
f9eacc98 | 481 | int send) |
05a1f28e TH |
482 | { |
483 | if (send) | |
484 | cpu_to_be32s(&pdu->status); | |
485 | else | |
486 | be32_to_cpus(&pdu->status); | |
487 | } | |
488 | ||
489 | void usbip_header_correct_endian(struct usbip_header *pdu, int send) | |
490 | { | |
491 | __u32 cmd = 0; | |
492 | ||
493 | if (send) | |
494 | cmd = pdu->base.command; | |
495 | ||
496 | correct_endian_basic(&pdu->base, send); | |
497 | ||
498 | if (!send) | |
499 | cmd = pdu->base.command; | |
500 | ||
501 | switch (cmd) { | |
502 | case USBIP_CMD_SUBMIT: | |
503 | correct_endian_cmd_submit(&pdu->u.cmd_submit, send); | |
504 | break; | |
505 | case USBIP_RET_SUBMIT: | |
506 | correct_endian_ret_submit(&pdu->u.ret_submit, send); | |
507 | break; | |
508 | case USBIP_CMD_UNLINK: | |
509 | correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send); | |
510 | break; | |
511 | case USBIP_RET_UNLINK: | |
512 | correct_endian_ret_unlink(&pdu->u.ret_unlink, send); | |
513 | break; | |
514 | default: | |
49aecefc | 515 | /* NOT REACHED */ |
1a4b6f66 | 516 | pr_err("unknown command\n"); |
49aecefc | 517 | break; |
05a1f28e TH |
518 | } |
519 | } | |
520 | EXPORT_SYMBOL_GPL(usbip_header_correct_endian); | |
521 | ||
2282e1fb | 522 | static void usbip_iso_packet_correct_endian( |
87352760 | 523 | struct usbip_iso_packet_descriptor *iso, int send) |
05a1f28e TH |
524 | { |
525 | /* does not need all members. but copy all simply. */ | |
526 | if (send) { | |
527 | iso->offset = cpu_to_be32(iso->offset); | |
528 | iso->length = cpu_to_be32(iso->length); | |
529 | iso->status = cpu_to_be32(iso->status); | |
530 | iso->actual_length = cpu_to_be32(iso->actual_length); | |
531 | } else { | |
532 | iso->offset = be32_to_cpu(iso->offset); | |
533 | iso->length = be32_to_cpu(iso->length); | |
534 | iso->status = be32_to_cpu(iso->status); | |
535 | iso->actual_length = be32_to_cpu(iso->actual_length); | |
536 | } | |
537 | } | |
538 | ||
539 | static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso, | |
f9eacc98 | 540 | struct usb_iso_packet_descriptor *uiso, int pack) |
05a1f28e TH |
541 | { |
542 | if (pack) { | |
543 | iso->offset = uiso->offset; | |
544 | iso->length = uiso->length; | |
545 | iso->status = uiso->status; | |
546 | iso->actual_length = uiso->actual_length; | |
547 | } else { | |
548 | uiso->offset = iso->offset; | |
549 | uiso->length = iso->length; | |
550 | uiso->status = iso->status; | |
551 | uiso->actual_length = iso->actual_length; | |
552 | } | |
553 | } | |
554 | ||
05a1f28e | 555 | /* must free buffer */ |
36ac9b05 BW |
556 | struct usbip_iso_packet_descriptor* |
557 | usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen) | |
05a1f28e | 558 | { |
05a1f28e TH |
559 | struct usbip_iso_packet_descriptor *iso; |
560 | int np = urb->number_of_packets; | |
561 | ssize_t size = np * sizeof(*iso); | |
562 | int i; | |
563 | ||
36ac9b05 BW |
564 | iso = kzalloc(size, GFP_KERNEL); |
565 | if (!iso) | |
05a1f28e TH |
566 | return NULL; |
567 | ||
568 | for (i = 0; i < np; i++) { | |
36ac9b05 BW |
569 | usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1); |
570 | usbip_iso_packet_correct_endian(&iso[i], 1); | |
05a1f28e TH |
571 | } |
572 | ||
573 | *bufflen = size; | |
574 | ||
36ac9b05 | 575 | return iso; |
05a1f28e TH |
576 | } |
577 | EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu); | |
578 | ||
579 | /* some members of urb must be substituted before. */ | |
580 | int usbip_recv_iso(struct usbip_device *ud, struct urb *urb) | |
581 | { | |
582 | void *buff; | |
583 | struct usbip_iso_packet_descriptor *iso; | |
584 | int np = urb->number_of_packets; | |
585 | int size = np * sizeof(*iso); | |
586 | int i; | |
587 | int ret; | |
28276a28 | 588 | int total_length = 0; |
05a1f28e TH |
589 | |
590 | if (!usb_pipeisoc(urb->pipe)) | |
591 | return 0; | |
592 | ||
593 | /* my Bluetooth dongle gets ISO URBs which are np = 0 */ | |
f14287b9 | 594 | if (np == 0) |
05a1f28e | 595 | return 0; |
05a1f28e TH |
596 | |
597 | buff = kzalloc(size, GFP_KERNEL); | |
598 | if (!buff) | |
599 | return -ENOMEM; | |
600 | ||
5a08c526 | 601 | ret = usbip_recv(ud->tcp_socket, buff, size); |
05a1f28e TH |
602 | if (ret != size) { |
603 | dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n", | |
604 | ret); | |
605 | kfree(buff); | |
606 | ||
c7af4c22 | 607 | if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) |
05a1f28e TH |
608 | usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); |
609 | else | |
610 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); | |
611 | ||
612 | return -EPIPE; | |
613 | } | |
614 | ||
36ac9b05 | 615 | iso = (struct usbip_iso_packet_descriptor *) buff; |
05a1f28e | 616 | for (i = 0; i < np; i++) { |
36ac9b05 BW |
617 | usbip_iso_packet_correct_endian(&iso[i], 0); |
618 | usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0); | |
28276a28 | 619 | total_length += urb->iso_frame_desc[i].actual_length; |
05a1f28e TH |
620 | } |
621 | ||
05a1f28e TH |
622 | kfree(buff); |
623 | ||
28276a28 AM |
624 | if (total_length != urb->actual_length) { |
625 | dev_err(&urb->dev->dev, | |
6bb3ee69 | 626 | "total length of iso packets %d not equal to actual length of buffer %d\n", |
f9eacc98 | 627 | total_length, urb->actual_length); |
28276a28 | 628 | |
c7af4c22 | 629 | if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) |
28276a28 AM |
630 | usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); |
631 | else | |
632 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); | |
633 | ||
634 | return -EPIPE; | |
635 | } | |
636 | ||
05a1f28e TH |
637 | return ret; |
638 | } | |
639 | EXPORT_SYMBOL_GPL(usbip_recv_iso); | |
640 | ||
28276a28 AM |
641 | /* |
642 | * This functions restores the padding which was removed for optimizing | |
643 | * the bandwidth during transfer over tcp/ip | |
644 | * | |
645 | * buffer and iso packets need to be stored and be in propeper endian in urb | |
646 | * before calling this function | |
647 | */ | |
ac2b41ac | 648 | void usbip_pad_iso(struct usbip_device *ud, struct urb *urb) |
28276a28 AM |
649 | { |
650 | int np = urb->number_of_packets; | |
651 | int i; | |
28276a28 AM |
652 | int actualoffset = urb->actual_length; |
653 | ||
654 | if (!usb_pipeisoc(urb->pipe)) | |
ac2b41ac | 655 | return; |
28276a28 AM |
656 | |
657 | /* if no packets or length of data is 0, then nothing to unpack */ | |
658 | if (np == 0 || urb->actual_length == 0) | |
ac2b41ac | 659 | return; |
28276a28 AM |
660 | |
661 | /* | |
662 | * if actual_length is transfer_buffer_length then no padding is | |
663 | * present. | |
c7f00899 | 664 | */ |
28276a28 | 665 | if (urb->actual_length == urb->transfer_buffer_length) |
ac2b41ac | 666 | return; |
28276a28 AM |
667 | |
668 | /* | |
9a284e5c | 669 | * loop over all packets from last to first (to prevent overwriting |
28276a28 AM |
670 | * memory when padding) and move them into the proper place |
671 | */ | |
672 | for (i = np-1; i > 0; i--) { | |
673 | actualoffset -= urb->iso_frame_desc[i].actual_length; | |
674 | memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset, | |
f9eacc98 | 675 | urb->transfer_buffer + actualoffset, |
676 | urb->iso_frame_desc[i].actual_length); | |
28276a28 | 677 | } |
28276a28 AM |
678 | } |
679 | EXPORT_SYMBOL_GPL(usbip_pad_iso); | |
05a1f28e TH |
680 | |
681 | /* some members of urb must be substituted before. */ | |
682 | int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb) | |
683 | { | |
684 | int ret; | |
685 | int size; | |
686 | ||
c7af4c22 | 687 | if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) { |
05a1f28e TH |
688 | /* the direction of urb must be OUT. */ |
689 | if (usb_pipein(urb->pipe)) | |
690 | return 0; | |
691 | ||
692 | size = urb->transfer_buffer_length; | |
693 | } else { | |
05a1f28e TH |
694 | /* the direction of urb must be IN. */ |
695 | if (usb_pipeout(urb->pipe)) | |
696 | return 0; | |
697 | ||
698 | size = urb->actual_length; | |
699 | } | |
700 | ||
701 | /* no need to recv xbuff */ | |
702 | if (!(size > 0)) | |
703 | return 0; | |
704 | ||
b348d7dd IK |
705 | if (size > urb->transfer_buffer_length) { |
706 | /* should not happen, probably malicious packet */ | |
707 | if (ud->side == USBIP_STUB) { | |
708 | usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); | |
709 | return 0; | |
710 | } else { | |
711 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); | |
712 | return -EPIPE; | |
713 | } | |
714 | } | |
715 | ||
5a08c526 | 716 | ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size); |
05a1f28e TH |
717 | if (ret != size) { |
718 | dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret); | |
c7af4c22 | 719 | if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) { |
05a1f28e TH |
720 | usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); |
721 | } else { | |
722 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); | |
723 | return -EPIPE; | |
724 | } | |
725 | } | |
726 | ||
727 | return ret; | |
728 | } | |
729 | EXPORT_SYMBOL_GPL(usbip_recv_xbuff); | |
730 | ||
3028d0ae | 731 | static int __init usbip_core_init(void) |
05a1f28e | 732 | { |
bb7871ad NI |
733 | int ret; |
734 | ||
bb7871ad NI |
735 | ret = usbip_init_eh(); |
736 | if (ret) | |
737 | return ret; | |
738 | ||
05a1f28e TH |
739 | return 0; |
740 | } | |
741 | ||
3028d0ae | 742 | static void __exit usbip_core_exit(void) |
05a1f28e | 743 | { |
bb7871ad | 744 | usbip_finish_eh(); |
05a1f28e TH |
745 | return; |
746 | } | |
747 | ||
3028d0ae | 748 | module_init(usbip_core_init); |
749 | module_exit(usbip_core_exit); | |
05a1f28e TH |
750 | |
751 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
752 | MODULE_DESCRIPTION(DRIVER_DESC); | |
753 | MODULE_LICENSE("GPL"); |