20 #include <asm/byteorder.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/stat.h>
26 #include <linux/module.h>
33 #define DRIVER_DESC "USB/IP Core"
35 #ifdef CONFIG_USBIP_DEBUG
62 static void usbip_dump_buffer(
char *buff,
int bufflen)
65 buff, bufflen,
false);
68 static void usbip_dump_pipe(
unsigned int p)
70 unsigned char type = usb_pipetype(p);
71 unsigned char ep = usb_pipeendpoint(p);
72 unsigned char dev = usb_pipedevice(p);
73 unsigned char dir = usb_pipein(p);
75 pr_debug(
"dev(%d) ep(%d) [%s] ", dev, ep, dir ?
"IN" :
"OUT");
78 case PIPE_ISOCHRONOUS:
96 static void usbip_dump_usb_device(
struct usb_device *
udev)
98 struct device *dev = &udev->dev;
101 dev_dbg(dev,
" devnum(%d) devpath(%s) ",
102 udev->devnum, udev->devpath);
104 switch (udev->speed) {
122 pr_debug(
"tt %p, ttport %d\n", udev->tt, udev->ttport);
125 for (i = 0; i < 16; i++)
129 dev_dbg(dev,
" toggle0(IN) :");
130 for (i = 0; i < 16; i++)
131 pr_debug(
" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
134 dev_dbg(dev,
" toggle1(OUT):");
135 for (i = 0; i < 16; i++)
136 pr_debug(
" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
140 for (i = 0; i < 16; i++) {
148 for (i = 0; i < 16; i++) {
151 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
155 dev_dbg(dev,
"parent %p, bus %p\n", udev->
parent, udev->bus);
157 dev_dbg(dev,
"descriptor %p, config %p, actconfig %p, "
158 "rawdescriptors %p\n", &udev->descriptor, udev->config,
159 udev->actconfig, udev->rawdescriptors);
161 dev_dbg(dev,
"have_langid %d, string_langid %d\n",
162 udev->have_langid, udev->string_langid);
164 dev_dbg(dev,
"maxchild %d\n", udev->maxchild);
167 static void usbip_dump_request_type(
__u8 rt)
196 pr_debug(
"bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) "
261 pr_debug(
"urb->dev: null pointer!!\n");
265 dev = &urb->dev->dev;
267 dev_dbg(dev,
" urb :%p\n", urb);
268 dev_dbg(dev,
" dev :%p\n", urb->dev);
270 usbip_dump_usb_device(urb->dev);
272 dev_dbg(dev,
" pipe :%08x ", urb->pipe);
274 usbip_dump_pipe(urb->pipe);
276 dev_dbg(dev,
" status :%d\n", urb->status);
277 dev_dbg(dev,
" transfer_flags :%08X\n", urb->transfer_flags);
278 dev_dbg(dev,
" transfer_buffer :%p\n", urb->transfer_buffer);
279 dev_dbg(dev,
" transfer_buffer_length:%d\n",
280 urb->transfer_buffer_length);
281 dev_dbg(dev,
" actual_length :%d\n", urb->actual_length);
282 dev_dbg(dev,
" setup_packet :%p\n", urb->setup_packet);
284 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
285 usbip_dump_usb_ctrlrequest(
288 dev_dbg(dev,
" start_frame :%d\n", urb->start_frame);
289 dev_dbg(dev,
" number_of_packets :%d\n", urb->number_of_packets);
290 dev_dbg(dev,
" interval :%d\n", urb->interval);
291 dev_dbg(dev,
" error_count :%d\n", urb->error_count);
292 dev_dbg(dev,
" context :%p\n", urb->context);
293 dev_dbg(dev,
" complete :%p\n", urb->complete);
299 pr_debug(
"BASE: cmd %u seq %u devid %u dir %u ep %u\n",
306 switch (pdu->
base.command) {
309 "x_flags %u x_len %u sf %u #p %d iv %d\n",
317 pr_debug(
"USBIP_CMD_UNLINK: seq %u\n",
321 pr_debug(
"USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
329 pr_debug(
"USBIP_RET_UNLINK: status %d\n",
334 pr_err(
"unknown command\n");
354 if (!sock || !buf || !size) {
355 pr_err(
"invalid arg, sock %p buff %p size %d\n", sock, buf,
373 pr_debug(
"receive sock %p buf %p size %u ret %d total %d\n",
374 sock, buf, size, result, total);
390 usbip_dump_buffer(bp, osize);
391 pr_debug(
"received, osize %d ret %d size %d total %d\n",
392 osize, result, size, total);
410 pr_err(
"invalid sockfd\n");
414 inode = file->f_dentry->d_inode;
419 socket = SOCKET_I(inode);
426 static unsigned int tweak_transfer_flags(
unsigned int flags)
428 flags &= ~URB_NO_TRANSFER_DMA_MAP;
444 tweak_transfer_flags(urb->transfer_flags);
460 static void usbip_pack_ret_submit(
struct usbip_header *pdu,
struct urb *urb,
468 rpdu->
status = urb->status;
476 urb->status = rpdu->
status;
489 usbip_pack_cmd_submit(pdu, urb, pack);
492 usbip_pack_ret_submit(pdu, urb, pack);
496 pr_err(
"unknown command\n");
580 cmd = pdu->
base.command;
582 correct_endian_basic(&pdu->
base, send);
585 cmd = pdu->
base.command;
589 correct_endian_cmd_submit(&pdu->
u.
cmd_submit, send);
592 correct_endian_ret_submit(&pdu->
u.
ret_submit, send);
595 correct_endian_cmd_unlink(&pdu->
u.
cmd_unlink, send);
598 correct_endian_ret_unlink(&pdu->
u.
ret_unlink, send);
602 pr_err(
"unknown command\n");
608 static void usbip_iso_packet_correct_endian(
626 struct usb_iso_packet_descriptor *uiso,
int pack)
629 iso->
offset = uiso->offset;
630 iso->
length = uiso->length;
631 iso->
status = uiso->status;
634 uiso->offset = iso->
offset;
635 uiso->length = iso->
length;
636 uiso->status = iso->
status;
646 int np = urb->number_of_packets;
654 for (i = 0; i < np; i++) {
655 iso = buff + (i *
sizeof(*iso));
657 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
658 usbip_iso_packet_correct_endian(iso, 1);
672 int np = urb->number_of_packets;
673 int size = np *
sizeof(*iso);
676 int total_length = 0;
678 if (!usb_pipeisoc(urb->pipe))
694 dev_err(&urb->dev->dev,
"recv iso_frame_descriptor, %d\n",
706 for (i = 0; i < np; i++) {
707 iso = buff + (i *
sizeof(*iso));
709 usbip_iso_packet_correct_endian(iso, 0);
710 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
711 total_length += urb->iso_frame_desc[
i].actual_length;
716 if (total_length != urb->actual_length) {
718 "total length of iso packets %d not equal to actual "
719 "length of buffer %d\n",
720 total_length, urb->actual_length);
743 int np = urb->number_of_packets;
745 int actualoffset = urb->actual_length;
747 if (!usb_pipeisoc(urb->pipe))
751 if (np == 0 || urb->actual_length == 0)
758 if (urb->actual_length == urb->transfer_buffer_length)
765 for (i = np-1; i > 0; i--) {
766 actualoffset -= urb->iso_frame_desc[
i].actual_length;
767 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
768 urb->transfer_buffer + actualoffset,
769 urb->iso_frame_desc[i].actual_length);
783 if (usb_pipein(urb->pipe))
786 size = urb->transfer_buffer_length;
790 if (usb_pipeout(urb->pipe))
793 size = urb->actual_length;
802 dev_err(&urb->dev->dev,
"recv xbuf, %d\n", ret);
815 static int __init usbip_core_init(
void)
821 static void __exit usbip_core_exit(
void)