blob: e4b96674c40526c75726c9b5c53a81d427f83d14 [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
Shuah Khane1346fd2017-12-22 17:00:06 -070094 pr_debug("tt hub ttport %d\n", 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
Shuah Khane1346fd2017-12-22 17:00:06 -0700127 dev_dbg(dev, "parent %s, bus %s\n", dev_name(&udev->parent->dev),
128 udev->bus->bus_name);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600129
130 dev_dbg(dev, "have_langid %d, string_langid %d\n",
131 udev->have_langid, udev->string_langid);
132
Lan Tianyuff823c792012-09-05 13:44:32 +0800133 dev_dbg(dev, "maxchild %d\n", udev->maxchild);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600134}
135
136static void usbip_dump_request_type(__u8 rt)
137{
138 switch (rt & USB_RECIP_MASK) {
139 case USB_RECIP_DEVICE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700140 pr_debug("DEVICE");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600141 break;
142 case USB_RECIP_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700143 pr_debug("INTERF");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600144 break;
145 case USB_RECIP_ENDPOINT:
matt mooney1a4b6f62011-05-19 16:47:32 -0700146 pr_debug("ENDPOI");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600147 break;
148 case USB_RECIP_OTHER:
matt mooney1a4b6f62011-05-19 16:47:32 -0700149 pr_debug("OTHER ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600150 break;
151 default:
matt mooney1a4b6f62011-05-19 16:47:32 -0700152 pr_debug("------");
matt mooney49aecef2011-05-06 03:47:54 -0700153 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600154 }
155}
156
157static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
158{
159 if (!cmd) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700160 pr_debug(" : null pointer\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600161 return;
162 }
163
matt mooney1a4b6f62011-05-19 16:47:32 -0700164 pr_debug(" ");
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100165 pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
166 cmd->bRequestType, cmd->bRequest,
matt mooney1a4b6f62011-05-19 16:47:32 -0700167 cmd->wValue, cmd->wIndex, cmd->wLength);
168 pr_debug("\n ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600169
170 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700171 pr_debug("STANDARD ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600172 switch (cmd->bRequest) {
173 case USB_REQ_GET_STATUS:
matt mooney1a4b6f62011-05-19 16:47:32 -0700174 pr_debug("GET_STATUS\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600175 break;
176 case USB_REQ_CLEAR_FEATURE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700177 pr_debug("CLEAR_FEAT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600178 break;
179 case USB_REQ_SET_FEATURE:
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400180 pr_debug("SET_FEAT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600181 break;
182 case USB_REQ_SET_ADDRESS:
matt mooney1a4b6f62011-05-19 16:47:32 -0700183 pr_debug("SET_ADDRRS\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600184 break;
185 case USB_REQ_GET_DESCRIPTOR:
matt mooney1a4b6f62011-05-19 16:47:32 -0700186 pr_debug("GET_DESCRI\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600187 break;
188 case USB_REQ_SET_DESCRIPTOR:
matt mooney1a4b6f62011-05-19 16:47:32 -0700189 pr_debug("SET_DESCRI\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600190 break;
191 case USB_REQ_GET_CONFIGURATION:
matt mooney1a4b6f62011-05-19 16:47:32 -0700192 pr_debug("GET_CONFIG\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600193 break;
194 case USB_REQ_SET_CONFIGURATION:
matt mooney1a4b6f62011-05-19 16:47:32 -0700195 pr_debug("SET_CONFIG\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600196 break;
197 case USB_REQ_GET_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700198 pr_debug("GET_INTERF\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600199 break;
200 case USB_REQ_SET_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700201 pr_debug("SET_INTERF\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600202 break;
203 case USB_REQ_SYNCH_FRAME:
matt mooney1a4b6f62011-05-19 16:47:32 -0700204 pr_debug("SYNC_FRAME\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600205 break;
206 default:
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400207 pr_debug("REQ(%02X)\n", cmd->bRequest);
matt mooney49aecef2011-05-06 03:47:54 -0700208 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600209 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600210 usbip_dump_request_type(cmd->bRequestType);
matt mooney1a4b6f62011-05-19 16:47:32 -0700211 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400212 pr_debug("CLASS\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700213 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400214 pr_debug("VENDOR\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700215 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
216 pr_debug("RESERVED\n");
217 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600218}
219
220void usbip_dump_urb(struct urb *urb)
221{
222 struct device *dev;
223
224 if (!urb) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700225 pr_debug("urb: null pointer!!\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600226 return;
227 }
228
229 if (!urb->dev) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700230 pr_debug("urb->dev: null pointer!!\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600231 return;
232 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700233
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600234 dev = &urb->dev->dev;
235
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600236 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);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600244 dev_dbg(dev, " transfer_buffer_length:%d\n",
245 urb->transfer_buffer_length);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600246 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600247
248 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
matt mooneyf9eacc92011-05-06 03:47:46 -0700249 usbip_dump_usb_ctrlrequest(
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600250 (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);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600256}
257EXPORT_SYMBOL_GPL(usbip_dump_urb);
258
259void usbip_dump_header(struct usbip_header *pdu)
260{
matt mooney1a4b6f62011-05-19 16:47:32 -0700261 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);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600267
268 switch (pdu->base.command) {
269 case USBIP_CMD_SUBMIT:
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100270 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 -0700271 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);
matt mooneyf9eacc92011-05-06 03:47:46 -0700276 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600277 case USBIP_CMD_UNLINK:
matt mooney1a4b6f62011-05-19 16:47:32 -0700278 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
279 pdu->u.cmd_unlink.seqnum);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600280 break;
281 case USBIP_RET_SUBMIT:
matt mooney1a4b6f62011-05-19 16:47:32 -0700282 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;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600289 case USBIP_RET_UNLINK:
matt mooney1a4b6f62011-05-19 16:47:32 -0700290 pr_debug("USBIP_RET_UNLINK: status %d\n",
291 pdu->u.ret_unlink.status);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600292 break;
293 default:
294 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700295 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700296 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600297 }
298}
299EXPORT_SYMBOL_GPL(usbip_dump_header);
300
Bart Westgeest5a08c522011-12-19 17:44:11 -0500301/* Receive data over TCP/IP. */
302int usbip_recv(struct socket *sock, void *buf, int size)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600303{
304 int result;
Al Viro3995d1612014-12-21 03:53:10 -0500305 struct kvec iov = {.iov_base = buf, .iov_len = size};
306 struct msghdr msg = {.msg_flags = MSG_NOSIGNAL};
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600307 int total = 0;
308
Shuah Khan90120d12017-12-15 10:50:09 -0700309 if (!sock || !buf || !size)
310 return -EINVAL;
311
David Howellsaa563d72018-10-20 00:57:56 +0100312 iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600313
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600314 usbip_dbg_xmit("enter\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600315
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600316 do {
317 sock->sk->sk_allocation = GFP_NOIO;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600318
Al Viro3995d1612014-12-21 03:53:10 -0500319 result = sock_recvmsg(sock, &msg, MSG_WAITALL);
Shuah Khan90120d12017-12-15 10:50:09 -0700320 if (result <= 0)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600321 goto err;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600322
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600323 total += result;
Al Viro3995d1612014-12-21 03:53:10 -0500324 } while (msg_data_left(&msg));
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600325
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600326 if (usbip_dbg_flag_xmit) {
Bart Westgeest5a08c522011-12-19 17:44:11 -0500327 if (!in_interrupt())
328 pr_debug("%-10s:", current->comm);
329 else
330 pr_debug("interrupt :");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600331
Bart Westgeest5a08c522011-12-19 17:44:11 -0500332 pr_debug("receiving....\n");
Al Viro3995d1612014-12-21 03:53:10 -0500333 usbip_dump_buffer(buf, size);
334 pr_debug("received, osize %d ret %d size %zd total %d\n",
335 size, result, msg_data_left(&msg), total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600336 }
337
338 return total;
339
340err:
341 return result;
342}
Bart Westgeest5a08c522011-12-19 17:44:11 -0500343EXPORT_SYMBOL_GPL(usbip_recv);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600344
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600345/* there may be more cases to tweak the flags. */
346static unsigned int tweak_transfer_flags(unsigned int flags)
347{
Alan Stern85bcb5e2010-04-30 16:35:37 -0400348 flags &= ~URB_NO_TRANSFER_DMA_MAP;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600349 return flags;
350}
351
352static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
matt mooneyf9eacc92011-05-06 03:47:46 -0700353 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600354{
355 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
356
357 /*
358 * Some members are not still implemented in usbip. I hope this issue
359 * will be discussed when usbip is ported to other operating systems.
360 */
361 if (pack) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600362 spdu->transfer_flags =
matt mooneyf9eacc92011-05-06 03:47:46 -0700363 tweak_transfer_flags(urb->transfer_flags);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600364 spdu->transfer_buffer_length = urb->transfer_buffer_length;
365 spdu->start_frame = urb->start_frame;
366 spdu->number_of_packets = urb->number_of_packets;
367 spdu->interval = urb->interval;
368 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600369 urb->transfer_flags = spdu->transfer_flags;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600370 urb->transfer_buffer_length = spdu->transfer_buffer_length;
371 urb->start_frame = spdu->start_frame;
372 urb->number_of_packets = spdu->number_of_packets;
373 urb->interval = spdu->interval;
374 }
375}
376
377static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
matt mooneyf9eacc92011-05-06 03:47:46 -0700378 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600379{
380 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
381
382 if (pack) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600383 rpdu->status = urb->status;
384 rpdu->actual_length = urb->actual_length;
385 rpdu->start_frame = urb->start_frame;
Arjan Mels1325f852011-04-05 20:26:38 +0200386 rpdu->number_of_packets = urb->number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600387 rpdu->error_count = urb->error_count;
388 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600389 urb->status = rpdu->status;
390 urb->actual_length = rpdu->actual_length;
391 urb->start_frame = rpdu->start_frame;
Arjan Mels1325f852011-04-05 20:26:38 +0200392 urb->number_of_packets = rpdu->number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600393 urb->error_count = rpdu->error_count;
394 }
395}
396
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600397void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
matt mooneyf9eacc92011-05-06 03:47:46 -0700398 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600399{
400 switch (cmd) {
401 case USBIP_CMD_SUBMIT:
402 usbip_pack_cmd_submit(pdu, urb, pack);
403 break;
404 case USBIP_RET_SUBMIT:
405 usbip_pack_ret_submit(pdu, urb, pack);
406 break;
407 default:
matt mooney49aecef2011-05-06 03:47:54 -0700408 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700409 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700410 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600411 }
412}
413EXPORT_SYMBOL_GPL(usbip_pack_pdu);
414
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600415static void correct_endian_basic(struct usbip_header_basic *base, int send)
416{
417 if (send) {
418 base->command = cpu_to_be32(base->command);
419 base->seqnum = cpu_to_be32(base->seqnum);
420 base->devid = cpu_to_be32(base->devid);
421 base->direction = cpu_to_be32(base->direction);
422 base->ep = cpu_to_be32(base->ep);
423 } else {
424 base->command = be32_to_cpu(base->command);
425 base->seqnum = be32_to_cpu(base->seqnum);
426 base->devid = be32_to_cpu(base->devid);
427 base->direction = be32_to_cpu(base->direction);
428 base->ep = be32_to_cpu(base->ep);
429 }
430}
431
432static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700433 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600434{
435 if (send) {
436 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
437
438 cpu_to_be32s(&pdu->transfer_buffer_length);
439 cpu_to_be32s(&pdu->start_frame);
440 cpu_to_be32s(&pdu->number_of_packets);
441 cpu_to_be32s(&pdu->interval);
442 } else {
443 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
444
445 be32_to_cpus(&pdu->transfer_buffer_length);
446 be32_to_cpus(&pdu->start_frame);
447 be32_to_cpus(&pdu->number_of_packets);
448 be32_to_cpus(&pdu->interval);
449 }
450}
451
452static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700453 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600454{
455 if (send) {
456 cpu_to_be32s(&pdu->status);
457 cpu_to_be32s(&pdu->actual_length);
458 cpu_to_be32s(&pdu->start_frame);
Arjan Mels1325f852011-04-05 20:26:38 +0200459 cpu_to_be32s(&pdu->number_of_packets);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600460 cpu_to_be32s(&pdu->error_count);
461 } else {
462 be32_to_cpus(&pdu->status);
463 be32_to_cpus(&pdu->actual_length);
464 be32_to_cpus(&pdu->start_frame);
David Changcacd18a2011-05-12 18:31:11 +0800465 be32_to_cpus(&pdu->number_of_packets);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600466 be32_to_cpus(&pdu->error_count);
467 }
468}
469
470static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700471 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600472{
473 if (send)
474 pdu->seqnum = cpu_to_be32(pdu->seqnum);
475 else
476 pdu->seqnum = be32_to_cpu(pdu->seqnum);
477}
478
479static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700480 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600481{
482 if (send)
483 cpu_to_be32s(&pdu->status);
484 else
485 be32_to_cpus(&pdu->status);
486}
487
488void usbip_header_correct_endian(struct usbip_header *pdu, int send)
489{
490 __u32 cmd = 0;
491
492 if (send)
493 cmd = pdu->base.command;
494
495 correct_endian_basic(&pdu->base, send);
496
497 if (!send)
498 cmd = pdu->base.command;
499
500 switch (cmd) {
501 case USBIP_CMD_SUBMIT:
502 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
503 break;
504 case USBIP_RET_SUBMIT:
505 correct_endian_ret_submit(&pdu->u.ret_submit, send);
506 break;
507 case USBIP_CMD_UNLINK:
508 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
509 break;
510 case USBIP_RET_UNLINK:
511 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
512 break;
513 default:
matt mooney49aecef2011-05-06 03:47:54 -0700514 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700515 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700516 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600517 }
518}
519EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
520
matt mooney2282e1f2011-05-19 21:37:01 -0700521static void usbip_iso_packet_correct_endian(
matt mooney87352762011-05-19 21:36:56 -0700522 struct usbip_iso_packet_descriptor *iso, int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600523{
524 /* does not need all members. but copy all simply. */
525 if (send) {
526 iso->offset = cpu_to_be32(iso->offset);
527 iso->length = cpu_to_be32(iso->length);
528 iso->status = cpu_to_be32(iso->status);
529 iso->actual_length = cpu_to_be32(iso->actual_length);
530 } else {
531 iso->offset = be32_to_cpu(iso->offset);
532 iso->length = be32_to_cpu(iso->length);
533 iso->status = be32_to_cpu(iso->status);
534 iso->actual_length = be32_to_cpu(iso->actual_length);
535 }
536}
537
538static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
matt mooneyf9eacc92011-05-06 03:47:46 -0700539 struct usb_iso_packet_descriptor *uiso, int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600540{
541 if (pack) {
542 iso->offset = uiso->offset;
543 iso->length = uiso->length;
544 iso->status = uiso->status;
545 iso->actual_length = uiso->actual_length;
546 } else {
547 uiso->offset = iso->offset;
548 uiso->length = iso->length;
549 uiso->status = iso->status;
550 uiso->actual_length = iso->actual_length;
551 }
552}
553
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600554/* must free buffer */
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400555struct usbip_iso_packet_descriptor*
556usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600557{
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600558 struct usbip_iso_packet_descriptor *iso;
559 int np = urb->number_of_packets;
560 ssize_t size = np * sizeof(*iso);
561 int i;
562
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400563 iso = kzalloc(size, GFP_KERNEL);
564 if (!iso)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600565 return NULL;
566
567 for (i = 0; i < np; i++) {
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400568 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
569 usbip_iso_packet_correct_endian(&iso[i], 1);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600570 }
571
572 *bufflen = size;
573
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400574 return iso;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600575}
576EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
577
578/* some members of urb must be substituted before. */
579int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
580{
581 void *buff;
582 struct usbip_iso_packet_descriptor *iso;
583 int np = urb->number_of_packets;
584 int size = np * sizeof(*iso);
585 int i;
586 int ret;
Arjan Mels28276a22011-04-05 20:26:59 +0200587 int total_length = 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600588
589 if (!usb_pipeisoc(urb->pipe))
590 return 0;
591
592 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
Jake Champlinf14287b2013-01-16 22:16:05 -0500593 if (np == 0)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600594 return 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600595
596 buff = kzalloc(size, GFP_KERNEL);
597 if (!buff)
598 return -ENOMEM;
599
Bart Westgeest5a08c522011-12-19 17:44:11 -0500600 ret = usbip_recv(ud->tcp_socket, buff, size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600601 if (ret != size) {
602 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
603 ret);
604 kfree(buff);
605
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100606 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600607 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
608 else
609 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
610
611 return -EPIPE;
612 }
613
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400614 iso = (struct usbip_iso_packet_descriptor *) buff;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600615 for (i = 0; i < np; i++) {
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400616 usbip_iso_packet_correct_endian(&iso[i], 0);
617 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
Arjan Mels28276a22011-04-05 20:26:59 +0200618 total_length += urb->iso_frame_desc[i].actual_length;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600619 }
620
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600621 kfree(buff);
622
Arjan Mels28276a22011-04-05 20:26:59 +0200623 if (total_length != urb->actual_length) {
624 dev_err(&urb->dev->dev,
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100625 "total length of iso packets %d not equal to actual length of buffer %d\n",
matt mooneyf9eacc92011-05-06 03:47:46 -0700626 total_length, urb->actual_length);
Arjan Mels28276a22011-04-05 20:26:59 +0200627
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100628 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
Arjan Mels28276a22011-04-05 20:26:59 +0200629 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
630 else
631 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
632
633 return -EPIPE;
634 }
635
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600636 return ret;
637}
638EXPORT_SYMBOL_GPL(usbip_recv_iso);
639
Arjan Mels28276a22011-04-05 20:26:59 +0200640/*
641 * This functions restores the padding which was removed for optimizing
642 * the bandwidth during transfer over tcp/ip
643 *
644 * buffer and iso packets need to be stored and be in propeper endian in urb
645 * before calling this function
646 */
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500647void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
Arjan Mels28276a22011-04-05 20:26:59 +0200648{
649 int np = urb->number_of_packets;
650 int i;
Arjan Mels28276a22011-04-05 20:26:59 +0200651 int actualoffset = urb->actual_length;
652
653 if (!usb_pipeisoc(urb->pipe))
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500654 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200655
656 /* if no packets or length of data is 0, then nothing to unpack */
657 if (np == 0 || urb->actual_length == 0)
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500658 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200659
660 /*
661 * if actual_length is transfer_buffer_length then no padding is
662 * present.
Bart Westgeestc7f00892012-10-10 13:34:27 -0400663 */
Arjan Mels28276a22011-04-05 20:26:59 +0200664 if (urb->actual_length == urb->transfer_buffer_length)
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500665 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200666
667 /*
Masahiro Yamada9a284e52017-02-27 14:29:48 -0800668 * loop over all packets from last to first (to prevent overwriting
Arjan Mels28276a22011-04-05 20:26:59 +0200669 * memory when padding) and move them into the proper place
670 */
671 for (i = np-1; i > 0; i--) {
672 actualoffset -= urb->iso_frame_desc[i].actual_length;
673 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
matt mooneyf9eacc92011-05-06 03:47:46 -0700674 urb->transfer_buffer + actualoffset,
675 urb->iso_frame_desc[i].actual_length);
Arjan Mels28276a22011-04-05 20:26:59 +0200676 }
Arjan Mels28276a22011-04-05 20:26:59 +0200677}
678EXPORT_SYMBOL_GPL(usbip_pad_iso);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600679
680/* some members of urb must be substituted before. */
681int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
682{
Suwan Kimea44d192019-08-28 12:27:41 +0900683 struct scatterlist *sg;
684 int ret = 0;
685 int recv;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600686 int size;
Suwan Kimea44d192019-08-28 12:27:41 +0900687 int copy;
688 int i;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600689
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100690 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600691 /* the direction of urb must be OUT. */
692 if (usb_pipein(urb->pipe))
693 return 0;
694
695 size = urb->transfer_buffer_length;
696 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600697 /* the direction of urb must be IN. */
698 if (usb_pipeout(urb->pipe))
699 return 0;
700
701 size = urb->actual_length;
702 }
703
704 /* no need to recv xbuff */
705 if (!(size > 0))
706 return 0;
707
Suwan Kimea44d192019-08-28 12:27:41 +0900708 if (size > urb->transfer_buffer_length)
Ignat Korchaginb348d7d2016-03-17 18:00:29 +0000709 /* should not happen, probably malicious packet */
Suwan Kimea44d192019-08-28 12:27:41 +0900710 goto error;
Ignat Korchaginb348d7d2016-03-17 18:00:29 +0000711
Suwan Kimea44d192019-08-28 12:27:41 +0900712 if (urb->num_sgs) {
713 copy = size;
714 for_each_sg(urb->sg, sg, urb->num_sgs, i) {
715 int recv_size;
716
717 if (copy < sg->length)
718 recv_size = copy;
719 else
720 recv_size = sg->length;
721
722 recv = usbip_recv(ud->tcp_socket, sg_virt(sg),
723 recv_size);
724
725 if (recv != recv_size)
726 goto error;
727
728 copy -= recv;
729 ret += recv;
Suwan Kimd9862942019-12-13 11:30:54 +0900730
731 if (!copy)
732 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600733 }
Suwan Kimea44d192019-08-28 12:27:41 +0900734
735 if (ret != size)
736 goto error;
737 } else {
738 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
739 if (ret != size)
740 goto error;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600741 }
742
743 return ret;
Suwan Kimea44d192019-08-28 12:27:41 +0900744
745error:
746 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
747 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
748 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
749 else
750 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
751
752 return -EPIPE;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600753}
754EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
755
matt mooney3028d0a2011-05-19 21:37:05 -0700756static int __init usbip_core_init(void)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600757{
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900758 int ret;
759
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900760 ret = usbip_init_eh();
761 if (ret)
762 return ret;
763
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600764 return 0;
765}
766
matt mooney3028d0a2011-05-19 21:37:05 -0700767static void __exit usbip_core_exit(void)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600768{
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900769 usbip_finish_eh();
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600770 return;
771}
772
matt mooney3028d0a2011-05-19 21:37:05 -0700773module_init(usbip_core_init);
774module_exit(usbip_core_exit);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600775
776MODULE_AUTHOR(DRIVER_AUTHOR);
777MODULE_DESCRIPTION(DRIVER_DESC);
778MODULE_LICENSE("GPL");