blob: f7978933b40290787ded82fd7b244783610d206e [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06002/*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +01004 * Copyright (C) 2015-2016 Samsung Electronics
5 * Krzysztof Opasiak <k.opasiak@samsung.com>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06006 */
7
matt mooney7aaacb42011-05-11 22:33:43 -07008#include <asm/byteorder.h>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06009#include <linux/file.h>
matt mooney7aaacb42011-05-11 22:33:43 -070010#include <linux/fs.h>
11#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
W. Trevor King93efc552012-08-16 22:22:36 -040013#include <linux/stat.h>
Paul Gortmaker45296232011-08-30 17:50:46 -040014#include <linux/module.h>
W. Trevor King93efc552012-08-16 22:22:36 -040015#include <linux/moduleparam.h>
matt mooney7aaacb42011-05-11 22:33:43 -070016#include <net/sock.h>
matt mooney4ce0a412011-05-06 03:47:56 -070017
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060018#include "usbip_common.h"
19
matt mooney4ce0a412011-05-06 03:47:56 -070020#define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
matt mooney64e62422011-05-11 22:33:44 -070021#define DRIVER_DESC "USB/IP Core"
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060022
matt mooney64e62422011-05-11 22:33:44 -070023#ifdef CONFIG_USBIP_DEBUG
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060024unsigned long usbip_debug_flag = 0xffffffff;
25#else
26unsigned long usbip_debug_flag;
27#endif
28EXPORT_SYMBOL_GPL(usbip_debug_flag);
W. Trevor King93efc552012-08-16 22:22:36 -040029module_param(usbip_debug_flag, ulong, S_IRUGO|S_IWUSR);
30MODULE_PARM_DESC(usbip_debug_flag, "debug flags (defined in usbip_common.h)");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060031
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060032/* FIXME */
33struct device_attribute dev_attr_usbip_debug;
34EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
35
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070036static ssize_t usbip_debug_show(struct device *dev,
37 struct device_attribute *attr, char *buf)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060038{
39 return sprintf(buf, "%lx\n", usbip_debug_flag);
40}
41
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070042static ssize_t usbip_debug_store(struct device *dev,
43 struct device_attribute *attr, const char *buf,
44 size_t count)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060045{
John de la Garzabf988c12014-03-06 15:51:59 -080046 if (sscanf(buf, "%lx", &usbip_debug_flag) != 1)
47 return -EINVAL;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060048 return count;
49}
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070050DEVICE_ATTR_RW(usbip_debug);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060051
52static void usbip_dump_buffer(char *buff, int bufflen)
53{
matt mooney1a4b6f62011-05-19 16:47:32 -070054 print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4,
Himanshu Chauhanaad865772010-01-23 02:52:41 +053055 buff, bufflen, false);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060056}
57
58static void usbip_dump_pipe(unsigned int p)
59{
60 unsigned char type = usb_pipetype(p);
matt mooney87352762011-05-19 21:36:56 -070061 unsigned char ep = usb_pipeendpoint(p);
62 unsigned char dev = usb_pipedevice(p);
63 unsigned char dir = usb_pipein(p);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060064
matt mooney1a4b6f62011-05-19 16:47:32 -070065 pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060066
67 switch (type) {
68 case PIPE_ISOCHRONOUS:
matt mooney1a4b6f62011-05-19 16:47:32 -070069 pr_debug("ISO\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060070 break;
71 case PIPE_INTERRUPT:
matt mooney1a4b6f62011-05-19 16:47:32 -070072 pr_debug("INT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060073 break;
74 case PIPE_CONTROL:
matt mooney1a4b6f62011-05-19 16:47:32 -070075 pr_debug("CTRL\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060076 break;
77 case PIPE_BULK:
matt mooney1a4b6f62011-05-19 16:47:32 -070078 pr_debug("BULK\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060079 break;
80 default:
matt mooney1a4b6f62011-05-19 16:47:32 -070081 pr_debug("ERR\n");
matt mooney49aecef2011-05-06 03:47:54 -070082 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060083 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060084}
85
86static void usbip_dump_usb_device(struct usb_device *udev)
87{
88 struct device *dev = &udev->dev;
89 int i;
90
Shuah Khan09c8c8f2014-01-24 09:25:05 -070091 dev_dbg(dev, " devnum(%d) devpath(%s) usb speed(%s)",
92 udev->devnum, udev->devpath, usb_speed_string(udev->speed));
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060093
matt mooney1a4b6f62011-05-19 16:47:32 -070094 pr_debug("tt %p, ttport %d\n", udev->tt, udev->ttport);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060095
96 dev_dbg(dev, " ");
97 for (i = 0; i < 16; i++)
matt mooney1a4b6f62011-05-19 16:47:32 -070098 pr_debug(" %2u", i);
99 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600100
101 dev_dbg(dev, " toggle0(IN) :");
102 for (i = 0; i < 16; i++)
matt mooney1a4b6f62011-05-19 16:47:32 -0700103 pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
104 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600105
106 dev_dbg(dev, " toggle1(OUT):");
107 for (i = 0; i < 16; i++)
matt mooney1a4b6f62011-05-19 16:47:32 -0700108 pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
109 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600110
111 dev_dbg(dev, " epmaxp_in :");
112 for (i = 0; i < 16; i++) {
113 if (udev->ep_in[i])
matt mooney1a4b6f62011-05-19 16:47:32 -0700114 pr_debug(" %2u",
115 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600116 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700117 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600118
119 dev_dbg(dev, " epmaxp_out :");
120 for (i = 0; i < 16; i++) {
121 if (udev->ep_out[i])
matt mooney1a4b6f62011-05-19 16:47:32 -0700122 pr_debug(" %2u",
123 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600124 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700125 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600126
127 dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
128
Himangi Saraogib482da22013-11-02 17:38:10 +0530129 dev_dbg(dev,
130 "descriptor %p, config %p, actconfig %p, rawdescriptors %p\n",
131 &udev->descriptor, udev->config,
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600132 udev->actconfig, udev->rawdescriptors);
133
134 dev_dbg(dev, "have_langid %d, string_langid %d\n",
135 udev->have_langid, udev->string_langid);
136
Lan Tianyuff823c792012-09-05 13:44:32 +0800137 dev_dbg(dev, "maxchild %d\n", udev->maxchild);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600138}
139
140static void usbip_dump_request_type(__u8 rt)
141{
142 switch (rt & USB_RECIP_MASK) {
143 case USB_RECIP_DEVICE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700144 pr_debug("DEVICE");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600145 break;
146 case USB_RECIP_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700147 pr_debug("INTERF");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600148 break;
149 case USB_RECIP_ENDPOINT:
matt mooney1a4b6f62011-05-19 16:47:32 -0700150 pr_debug("ENDPOI");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600151 break;
152 case USB_RECIP_OTHER:
matt mooney1a4b6f62011-05-19 16:47:32 -0700153 pr_debug("OTHER ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600154 break;
155 default:
matt mooney1a4b6f62011-05-19 16:47:32 -0700156 pr_debug("------");
matt mooney49aecef2011-05-06 03:47:54 -0700157 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600158 }
159}
160
161static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
162{
163 if (!cmd) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700164 pr_debug(" : null pointer\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600165 return;
166 }
167
matt mooney1a4b6f62011-05-19 16:47:32 -0700168 pr_debug(" ");
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100169 pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
170 cmd->bRequestType, cmd->bRequest,
matt mooney1a4b6f62011-05-19 16:47:32 -0700171 cmd->wValue, cmd->wIndex, cmd->wLength);
172 pr_debug("\n ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600173
174 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700175 pr_debug("STANDARD ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600176 switch (cmd->bRequest) {
177 case USB_REQ_GET_STATUS:
matt mooney1a4b6f62011-05-19 16:47:32 -0700178 pr_debug("GET_STATUS\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600179 break;
180 case USB_REQ_CLEAR_FEATURE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700181 pr_debug("CLEAR_FEAT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600182 break;
183 case USB_REQ_SET_FEATURE:
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400184 pr_debug("SET_FEAT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600185 break;
186 case USB_REQ_SET_ADDRESS:
matt mooney1a4b6f62011-05-19 16:47:32 -0700187 pr_debug("SET_ADDRRS\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600188 break;
189 case USB_REQ_GET_DESCRIPTOR:
matt mooney1a4b6f62011-05-19 16:47:32 -0700190 pr_debug("GET_DESCRI\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600191 break;
192 case USB_REQ_SET_DESCRIPTOR:
matt mooney1a4b6f62011-05-19 16:47:32 -0700193 pr_debug("SET_DESCRI\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600194 break;
195 case USB_REQ_GET_CONFIGURATION:
matt mooney1a4b6f62011-05-19 16:47:32 -0700196 pr_debug("GET_CONFIG\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600197 break;
198 case USB_REQ_SET_CONFIGURATION:
matt mooney1a4b6f62011-05-19 16:47:32 -0700199 pr_debug("SET_CONFIG\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600200 break;
201 case USB_REQ_GET_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700202 pr_debug("GET_INTERF\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600203 break;
204 case USB_REQ_SET_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700205 pr_debug("SET_INTERF\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600206 break;
207 case USB_REQ_SYNCH_FRAME:
matt mooney1a4b6f62011-05-19 16:47:32 -0700208 pr_debug("SYNC_FRAME\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600209 break;
210 default:
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400211 pr_debug("REQ(%02X)\n", cmd->bRequest);
matt mooney49aecef2011-05-06 03:47:54 -0700212 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600213 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600214 usbip_dump_request_type(cmd->bRequestType);
matt mooney1a4b6f62011-05-19 16:47:32 -0700215 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400216 pr_debug("CLASS\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700217 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400218 pr_debug("VENDOR\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700219 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
220 pr_debug("RESERVED\n");
221 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600222}
223
224void usbip_dump_urb(struct urb *urb)
225{
226 struct device *dev;
227
228 if (!urb) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700229 pr_debug("urb: null pointer!!\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600230 return;
231 }
232
233 if (!urb->dev) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700234 pr_debug("urb->dev: null pointer!!\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600235 return;
236 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700237
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600238 dev = &urb->dev->dev;
239
240 dev_dbg(dev, " urb :%p\n", urb);
241 dev_dbg(dev, " dev :%p\n", urb->dev);
242
243 usbip_dump_usb_device(urb->dev);
244
245 dev_dbg(dev, " pipe :%08x ", urb->pipe);
246
247 usbip_dump_pipe(urb->pipe);
248
249 dev_dbg(dev, " status :%d\n", urb->status);
250 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
251 dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600252 dev_dbg(dev, " transfer_buffer_length:%d\n",
253 urb->transfer_buffer_length);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600254 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
255 dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
256
257 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
matt mooneyf9eacc92011-05-06 03:47:46 -0700258 usbip_dump_usb_ctrlrequest(
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600259 (struct usb_ctrlrequest *)urb->setup_packet);
260
261 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
262 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
263 dev_dbg(dev, " interval :%d\n", urb->interval);
264 dev_dbg(dev, " error_count :%d\n", urb->error_count);
265 dev_dbg(dev, " context :%p\n", urb->context);
266 dev_dbg(dev, " complete :%p\n", urb->complete);
267}
268EXPORT_SYMBOL_GPL(usbip_dump_urb);
269
270void usbip_dump_header(struct usbip_header *pdu)
271{
matt mooney1a4b6f62011-05-19 16:47:32 -0700272 pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
273 pdu->base.command,
274 pdu->base.seqnum,
275 pdu->base.devid,
276 pdu->base.direction,
277 pdu->base.ep);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600278
279 switch (pdu->base.command) {
280 case USBIP_CMD_SUBMIT:
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100281 pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n",
matt mooney1a4b6f62011-05-19 16:47:32 -0700282 pdu->u.cmd_submit.transfer_flags,
283 pdu->u.cmd_submit.transfer_buffer_length,
284 pdu->u.cmd_submit.start_frame,
285 pdu->u.cmd_submit.number_of_packets,
286 pdu->u.cmd_submit.interval);
matt mooneyf9eacc92011-05-06 03:47:46 -0700287 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600288 case USBIP_CMD_UNLINK:
matt mooney1a4b6f62011-05-19 16:47:32 -0700289 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
290 pdu->u.cmd_unlink.seqnum);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600291 break;
292 case USBIP_RET_SUBMIT:
matt mooney1a4b6f62011-05-19 16:47:32 -0700293 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
294 pdu->u.ret_submit.status,
295 pdu->u.ret_submit.actual_length,
296 pdu->u.ret_submit.start_frame,
297 pdu->u.ret_submit.number_of_packets,
298 pdu->u.ret_submit.error_count);
299 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600300 case USBIP_RET_UNLINK:
matt mooney1a4b6f62011-05-19 16:47:32 -0700301 pr_debug("USBIP_RET_UNLINK: status %d\n",
302 pdu->u.ret_unlink.status);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600303 break;
304 default:
305 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700306 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700307 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600308 }
309}
310EXPORT_SYMBOL_GPL(usbip_dump_header);
311
Bart Westgeest5a08c522011-12-19 17:44:11 -0500312/* Receive data over TCP/IP. */
313int usbip_recv(struct socket *sock, void *buf, int size)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600314{
315 int result;
Al Viro3995d1612014-12-21 03:53:10 -0500316 struct kvec iov = {.iov_base = buf, .iov_len = size};
317 struct msghdr msg = {.msg_flags = MSG_NOSIGNAL};
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600318 int total = 0;
319
Al Viro3995d1612014-12-21 03:53:10 -0500320 iov_iter_kvec(&msg.msg_iter, READ|ITER_KVEC, &iov, 1, size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600321
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600322 usbip_dbg_xmit("enter\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600323
324 if (!sock || !buf || !size) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700325 pr_err("invalid arg, sock %p buff %p size %d\n", sock, buf,
326 size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600327 return -EINVAL;
328 }
329
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600330 do {
Al Viro3995d1612014-12-21 03:53:10 -0500331 int sz = msg_data_left(&msg);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600332 sock->sk->sk_allocation = GFP_NOIO;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600333
Al Viro3995d1612014-12-21 03:53:10 -0500334 result = sock_recvmsg(sock, &msg, MSG_WAITALL);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600335 if (result <= 0) {
Bart Westgeest5a08c522011-12-19 17:44:11 -0500336 pr_debug("receive sock %p buf %p size %u ret %d total %d\n",
Al Viro3995d1612014-12-21 03:53:10 -0500337 sock, buf + total, sz, result, total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600338 goto err;
339 }
340
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600341 total += result;
Al Viro3995d1612014-12-21 03:53:10 -0500342 } while (msg_data_left(&msg));
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600343
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600344 if (usbip_dbg_flag_xmit) {
Bart Westgeest5a08c522011-12-19 17:44:11 -0500345 if (!in_interrupt())
346 pr_debug("%-10s:", current->comm);
347 else
348 pr_debug("interrupt :");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600349
Bart Westgeest5a08c522011-12-19 17:44:11 -0500350 pr_debug("receiving....\n");
Al Viro3995d1612014-12-21 03:53:10 -0500351 usbip_dump_buffer(buf, size);
352 pr_debug("received, osize %d ret %d size %zd total %d\n",
353 size, result, msg_data_left(&msg), total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600354 }
355
356 return total;
357
358err:
359 return result;
360}
Bart Westgeest5a08c522011-12-19 17:44:11 -0500361EXPORT_SYMBOL_GPL(usbip_recv);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600362
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600363/* there may be more cases to tweak the flags. */
364static unsigned int tweak_transfer_flags(unsigned int flags)
365{
Alan Stern85bcb5e2010-04-30 16:35:37 -0400366 flags &= ~URB_NO_TRANSFER_DMA_MAP;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600367 return flags;
368}
369
370static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
matt mooneyf9eacc92011-05-06 03:47:46 -0700371 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600372{
373 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
374
375 /*
376 * Some members are not still implemented in usbip. I hope this issue
377 * will be discussed when usbip is ported to other operating systems.
378 */
379 if (pack) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600380 spdu->transfer_flags =
matt mooneyf9eacc92011-05-06 03:47:46 -0700381 tweak_transfer_flags(urb->transfer_flags);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600382 spdu->transfer_buffer_length = urb->transfer_buffer_length;
383 spdu->start_frame = urb->start_frame;
384 spdu->number_of_packets = urb->number_of_packets;
385 spdu->interval = urb->interval;
386 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600387 urb->transfer_flags = spdu->transfer_flags;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600388 urb->transfer_buffer_length = spdu->transfer_buffer_length;
389 urb->start_frame = spdu->start_frame;
390 urb->number_of_packets = spdu->number_of_packets;
391 urb->interval = spdu->interval;
392 }
393}
394
395static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
matt mooneyf9eacc92011-05-06 03:47:46 -0700396 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600397{
398 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
399
400 if (pack) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600401 rpdu->status = urb->status;
402 rpdu->actual_length = urb->actual_length;
403 rpdu->start_frame = urb->start_frame;
Arjan Mels1325f852011-04-05 20:26:38 +0200404 rpdu->number_of_packets = urb->number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600405 rpdu->error_count = urb->error_count;
406 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600407 urb->status = rpdu->status;
408 urb->actual_length = rpdu->actual_length;
409 urb->start_frame = rpdu->start_frame;
Arjan Mels1325f852011-04-05 20:26:38 +0200410 urb->number_of_packets = rpdu->number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600411 urb->error_count = rpdu->error_count;
412 }
413}
414
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600415void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
matt mooneyf9eacc92011-05-06 03:47:46 -0700416 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600417{
418 switch (cmd) {
419 case USBIP_CMD_SUBMIT:
420 usbip_pack_cmd_submit(pdu, urb, pack);
421 break;
422 case USBIP_RET_SUBMIT:
423 usbip_pack_ret_submit(pdu, urb, pack);
424 break;
425 default:
matt mooney49aecef2011-05-06 03:47:54 -0700426 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700427 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700428 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600429 }
430}
431EXPORT_SYMBOL_GPL(usbip_pack_pdu);
432
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600433static void correct_endian_basic(struct usbip_header_basic *base, int send)
434{
435 if (send) {
436 base->command = cpu_to_be32(base->command);
437 base->seqnum = cpu_to_be32(base->seqnum);
438 base->devid = cpu_to_be32(base->devid);
439 base->direction = cpu_to_be32(base->direction);
440 base->ep = cpu_to_be32(base->ep);
441 } else {
442 base->command = be32_to_cpu(base->command);
443 base->seqnum = be32_to_cpu(base->seqnum);
444 base->devid = be32_to_cpu(base->devid);
445 base->direction = be32_to_cpu(base->direction);
446 base->ep = be32_to_cpu(base->ep);
447 }
448}
449
450static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700451 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600452{
453 if (send) {
454 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
455
456 cpu_to_be32s(&pdu->transfer_buffer_length);
457 cpu_to_be32s(&pdu->start_frame);
458 cpu_to_be32s(&pdu->number_of_packets);
459 cpu_to_be32s(&pdu->interval);
460 } else {
461 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
462
463 be32_to_cpus(&pdu->transfer_buffer_length);
464 be32_to_cpus(&pdu->start_frame);
465 be32_to_cpus(&pdu->number_of_packets);
466 be32_to_cpus(&pdu->interval);
467 }
468}
469
470static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700471 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600472{
473 if (send) {
474 cpu_to_be32s(&pdu->status);
475 cpu_to_be32s(&pdu->actual_length);
476 cpu_to_be32s(&pdu->start_frame);
Arjan Mels1325f852011-04-05 20:26:38 +0200477 cpu_to_be32s(&pdu->number_of_packets);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600478 cpu_to_be32s(&pdu->error_count);
479 } else {
480 be32_to_cpus(&pdu->status);
481 be32_to_cpus(&pdu->actual_length);
482 be32_to_cpus(&pdu->start_frame);
David Changcacd18a2011-05-12 18:31:11 +0800483 be32_to_cpus(&pdu->number_of_packets);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600484 be32_to_cpus(&pdu->error_count);
485 }
486}
487
488static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700489 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600490{
491 if (send)
492 pdu->seqnum = cpu_to_be32(pdu->seqnum);
493 else
494 pdu->seqnum = be32_to_cpu(pdu->seqnum);
495}
496
497static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700498 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600499{
500 if (send)
501 cpu_to_be32s(&pdu->status);
502 else
503 be32_to_cpus(&pdu->status);
504}
505
506void usbip_header_correct_endian(struct usbip_header *pdu, int send)
507{
508 __u32 cmd = 0;
509
510 if (send)
511 cmd = pdu->base.command;
512
513 correct_endian_basic(&pdu->base, send);
514
515 if (!send)
516 cmd = pdu->base.command;
517
518 switch (cmd) {
519 case USBIP_CMD_SUBMIT:
520 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
521 break;
522 case USBIP_RET_SUBMIT:
523 correct_endian_ret_submit(&pdu->u.ret_submit, send);
524 break;
525 case USBIP_CMD_UNLINK:
526 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
527 break;
528 case USBIP_RET_UNLINK:
529 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
530 break;
531 default:
matt mooney49aecef2011-05-06 03:47:54 -0700532 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700533 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700534 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600535 }
536}
537EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
538
matt mooney2282e1f2011-05-19 21:37:01 -0700539static void usbip_iso_packet_correct_endian(
matt mooney87352762011-05-19 21:36:56 -0700540 struct usbip_iso_packet_descriptor *iso, int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600541{
542 /* does not need all members. but copy all simply. */
543 if (send) {
544 iso->offset = cpu_to_be32(iso->offset);
545 iso->length = cpu_to_be32(iso->length);
546 iso->status = cpu_to_be32(iso->status);
547 iso->actual_length = cpu_to_be32(iso->actual_length);
548 } else {
549 iso->offset = be32_to_cpu(iso->offset);
550 iso->length = be32_to_cpu(iso->length);
551 iso->status = be32_to_cpu(iso->status);
552 iso->actual_length = be32_to_cpu(iso->actual_length);
553 }
554}
555
556static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
matt mooneyf9eacc92011-05-06 03:47:46 -0700557 struct usb_iso_packet_descriptor *uiso, int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600558{
559 if (pack) {
560 iso->offset = uiso->offset;
561 iso->length = uiso->length;
562 iso->status = uiso->status;
563 iso->actual_length = uiso->actual_length;
564 } else {
565 uiso->offset = iso->offset;
566 uiso->length = iso->length;
567 uiso->status = iso->status;
568 uiso->actual_length = iso->actual_length;
569 }
570}
571
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600572/* must free buffer */
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400573struct usbip_iso_packet_descriptor*
574usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600575{
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600576 struct usbip_iso_packet_descriptor *iso;
577 int np = urb->number_of_packets;
578 ssize_t size = np * sizeof(*iso);
579 int i;
580
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400581 iso = kzalloc(size, GFP_KERNEL);
582 if (!iso)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600583 return NULL;
584
585 for (i = 0; i < np; i++) {
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400586 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
587 usbip_iso_packet_correct_endian(&iso[i], 1);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600588 }
589
590 *bufflen = size;
591
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400592 return iso;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600593}
594EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
595
596/* some members of urb must be substituted before. */
597int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
598{
599 void *buff;
600 struct usbip_iso_packet_descriptor *iso;
601 int np = urb->number_of_packets;
602 int size = np * sizeof(*iso);
603 int i;
604 int ret;
Arjan Mels28276a22011-04-05 20:26:59 +0200605 int total_length = 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600606
607 if (!usb_pipeisoc(urb->pipe))
608 return 0;
609
610 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
Jake Champlinf14287b2013-01-16 22:16:05 -0500611 if (np == 0)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600612 return 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600613
614 buff = kzalloc(size, GFP_KERNEL);
615 if (!buff)
616 return -ENOMEM;
617
Bart Westgeest5a08c522011-12-19 17:44:11 -0500618 ret = usbip_recv(ud->tcp_socket, buff, size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600619 if (ret != size) {
620 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
621 ret);
622 kfree(buff);
623
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100624 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600625 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
626 else
627 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
628
629 return -EPIPE;
630 }
631
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400632 iso = (struct usbip_iso_packet_descriptor *) buff;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600633 for (i = 0; i < np; i++) {
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400634 usbip_iso_packet_correct_endian(&iso[i], 0);
635 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
Arjan Mels28276a22011-04-05 20:26:59 +0200636 total_length += urb->iso_frame_desc[i].actual_length;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600637 }
638
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600639 kfree(buff);
640
Arjan Mels28276a22011-04-05 20:26:59 +0200641 if (total_length != urb->actual_length) {
642 dev_err(&urb->dev->dev,
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100643 "total length of iso packets %d not equal to actual length of buffer %d\n",
matt mooneyf9eacc92011-05-06 03:47:46 -0700644 total_length, urb->actual_length);
Arjan Mels28276a22011-04-05 20:26:59 +0200645
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100646 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
Arjan Mels28276a22011-04-05 20:26:59 +0200647 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
648 else
649 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
650
651 return -EPIPE;
652 }
653
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600654 return ret;
655}
656EXPORT_SYMBOL_GPL(usbip_recv_iso);
657
Arjan Mels28276a22011-04-05 20:26:59 +0200658/*
659 * This functions restores the padding which was removed for optimizing
660 * the bandwidth during transfer over tcp/ip
661 *
662 * buffer and iso packets need to be stored and be in propeper endian in urb
663 * before calling this function
664 */
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500665void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
Arjan Mels28276a22011-04-05 20:26:59 +0200666{
667 int np = urb->number_of_packets;
668 int i;
Arjan Mels28276a22011-04-05 20:26:59 +0200669 int actualoffset = urb->actual_length;
670
671 if (!usb_pipeisoc(urb->pipe))
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500672 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200673
674 /* if no packets or length of data is 0, then nothing to unpack */
675 if (np == 0 || urb->actual_length == 0)
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500676 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200677
678 /*
679 * if actual_length is transfer_buffer_length then no padding is
680 * present.
Bart Westgeestc7f00892012-10-10 13:34:27 -0400681 */
Arjan Mels28276a22011-04-05 20:26:59 +0200682 if (urb->actual_length == urb->transfer_buffer_length)
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500683 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200684
685 /*
Masahiro Yamada9a284e52017-02-27 14:29:48 -0800686 * loop over all packets from last to first (to prevent overwriting
Arjan Mels28276a22011-04-05 20:26:59 +0200687 * memory when padding) and move them into the proper place
688 */
689 for (i = np-1; i > 0; i--) {
690 actualoffset -= urb->iso_frame_desc[i].actual_length;
691 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
matt mooneyf9eacc92011-05-06 03:47:46 -0700692 urb->transfer_buffer + actualoffset,
693 urb->iso_frame_desc[i].actual_length);
Arjan Mels28276a22011-04-05 20:26:59 +0200694 }
Arjan Mels28276a22011-04-05 20:26:59 +0200695}
696EXPORT_SYMBOL_GPL(usbip_pad_iso);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600697
698/* some members of urb must be substituted before. */
699int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
700{
701 int ret;
702 int size;
703
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100704 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600705 /* the direction of urb must be OUT. */
706 if (usb_pipein(urb->pipe))
707 return 0;
708
709 size = urb->transfer_buffer_length;
710 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600711 /* the direction of urb must be IN. */
712 if (usb_pipeout(urb->pipe))
713 return 0;
714
715 size = urb->actual_length;
716 }
717
718 /* no need to recv xbuff */
719 if (!(size > 0))
720 return 0;
721
Ignat Korchaginb348d7d2016-03-17 18:00:29 +0000722 if (size > urb->transfer_buffer_length) {
723 /* should not happen, probably malicious packet */
724 if (ud->side == USBIP_STUB) {
725 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
726 return 0;
727 } else {
728 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
729 return -EPIPE;
730 }
731 }
732
Bart Westgeest5a08c522011-12-19 17:44:11 -0500733 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600734 if (ret != size) {
735 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100736 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600737 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
738 } else {
739 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
740 return -EPIPE;
741 }
742 }
743
744 return ret;
745}
746EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
747
matt mooney3028d0a2011-05-19 21:37:05 -0700748static int __init usbip_core_init(void)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600749{
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900750 int ret;
751
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900752 ret = usbip_init_eh();
753 if (ret)
754 return ret;
755
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600756 return 0;
757}
758
matt mooney3028d0a2011-05-19 21:37:05 -0700759static void __exit usbip_core_exit(void)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600760{
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900761 usbip_finish_eh();
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600762 return;
763}
764
matt mooney3028d0a2011-05-19 21:37:05 -0700765module_init(usbip_core_init);
766module_exit(usbip_core_exit);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600767
768MODULE_AUTHOR(DRIVER_AUTHOR);
769MODULE_DESCRIPTION(DRIVER_DESC);
770MODULE_LICENSE("GPL");