blob: 2a5d3180777d9aa714162097aad36c1f3d4708d0 [file] [log] [blame]
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06001/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +01003 * Copyright (C) 2015-2016 Samsung Electronics
4 * Krzysztof Opasiak <k.opasiak@samsung.com>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06005 *
6 * This is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
20 */
21
matt mooney7aaacb42011-05-11 22:33:43 -070022#include <asm/byteorder.h>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060023#include <linux/file.h>
matt mooney7aaacb42011-05-11 22:33:43 -070024#include <linux/fs.h>
25#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
W. Trevor King93efc552012-08-16 22:22:36 -040027#include <linux/stat.h>
Paul Gortmaker45296232011-08-30 17:50:46 -040028#include <linux/module.h>
W. Trevor King93efc552012-08-16 22:22:36 -040029#include <linux/moduleparam.h>
matt mooney7aaacb42011-05-11 22:33:43 -070030#include <net/sock.h>
matt mooney4ce0a412011-05-06 03:47:56 -070031
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060032#include "usbip_common.h"
33
matt mooney4ce0a412011-05-06 03:47:56 -070034#define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
matt mooney64e62422011-05-11 22:33:44 -070035#define DRIVER_DESC "USB/IP Core"
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060036
matt mooney64e62422011-05-11 22:33:44 -070037#ifdef CONFIG_USBIP_DEBUG
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060038unsigned long usbip_debug_flag = 0xffffffff;
39#else
40unsigned long usbip_debug_flag;
41#endif
42EXPORT_SYMBOL_GPL(usbip_debug_flag);
W. Trevor King93efc552012-08-16 22:22:36 -040043module_param(usbip_debug_flag, ulong, S_IRUGO|S_IWUSR);
44MODULE_PARM_DESC(usbip_debug_flag, "debug flags (defined in usbip_common.h)");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060045
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060046/* FIXME */
47struct device_attribute dev_attr_usbip_debug;
48EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
49
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070050static ssize_t usbip_debug_show(struct device *dev,
51 struct device_attribute *attr, char *buf)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060052{
53 return sprintf(buf, "%lx\n", usbip_debug_flag);
54}
55
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070056static ssize_t usbip_debug_store(struct device *dev,
57 struct device_attribute *attr, const char *buf,
58 size_t count)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060059{
John de la Garzabf988c12014-03-06 15:51:59 -080060 if (sscanf(buf, "%lx", &usbip_debug_flag) != 1)
61 return -EINVAL;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060062 return count;
63}
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070064DEVICE_ATTR_RW(usbip_debug);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060065
66static void usbip_dump_buffer(char *buff, int bufflen)
67{
matt mooney1a4b6f62011-05-19 16:47:32 -070068 print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4,
Himanshu Chauhanaad86572010-01-23 02:52:41 +053069 buff, bufflen, false);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060070}
71
72static void usbip_dump_pipe(unsigned int p)
73{
74 unsigned char type = usb_pipetype(p);
matt mooney87352762011-05-19 21:36:56 -070075 unsigned char ep = usb_pipeendpoint(p);
76 unsigned char dev = usb_pipedevice(p);
77 unsigned char dir = usb_pipein(p);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060078
matt mooney1a4b6f62011-05-19 16:47:32 -070079 pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060080
81 switch (type) {
82 case PIPE_ISOCHRONOUS:
matt mooney1a4b6f62011-05-19 16:47:32 -070083 pr_debug("ISO\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060084 break;
85 case PIPE_INTERRUPT:
matt mooney1a4b6f62011-05-19 16:47:32 -070086 pr_debug("INT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060087 break;
88 case PIPE_CONTROL:
matt mooney1a4b6f62011-05-19 16:47:32 -070089 pr_debug("CTRL\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060090 break;
91 case PIPE_BULK:
matt mooney1a4b6f62011-05-19 16:47:32 -070092 pr_debug("BULK\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060093 break;
94 default:
matt mooney1a4b6f62011-05-19 16:47:32 -070095 pr_debug("ERR\n");
matt mooney49aecef2011-05-06 03:47:54 -070096 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060097 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060098}
99
100static void usbip_dump_usb_device(struct usb_device *udev)
101{
102 struct device *dev = &udev->dev;
103 int i;
104
Shuah Khan09c8c8f2014-01-24 09:25:05 -0700105 dev_dbg(dev, " devnum(%d) devpath(%s) usb speed(%s)",
106 udev->devnum, udev->devpath, usb_speed_string(udev->speed));
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600107
Shuah Khan6851ec72017-12-22 17:00:06 -0700108 pr_debug("tt hub ttport %d\n", udev->ttport);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600109
110 dev_dbg(dev, " ");
111 for (i = 0; i < 16; i++)
matt mooney1a4b6f62011-05-19 16:47:32 -0700112 pr_debug(" %2u", i);
113 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600114
115 dev_dbg(dev, " toggle0(IN) :");
116 for (i = 0; i < 16; i++)
matt mooney1a4b6f62011-05-19 16:47:32 -0700117 pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
118 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600119
120 dev_dbg(dev, " toggle1(OUT):");
121 for (i = 0; i < 16; i++)
matt mooney1a4b6f62011-05-19 16:47:32 -0700122 pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
123 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600124
125 dev_dbg(dev, " epmaxp_in :");
126 for (i = 0; i < 16; i++) {
127 if (udev->ep_in[i])
matt mooney1a4b6f62011-05-19 16:47:32 -0700128 pr_debug(" %2u",
129 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600130 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700131 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600132
133 dev_dbg(dev, " epmaxp_out :");
134 for (i = 0; i < 16; i++) {
135 if (udev->ep_out[i])
matt mooney1a4b6f62011-05-19 16:47:32 -0700136 pr_debug(" %2u",
137 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600138 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700139 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600140
Shuah Khan6851ec72017-12-22 17:00:06 -0700141 dev_dbg(dev, "parent %s, bus %s\n", dev_name(&udev->parent->dev),
142 udev->bus->bus_name);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600143
144 dev_dbg(dev, "have_langid %d, string_langid %d\n",
145 udev->have_langid, udev->string_langid);
146
Lan Tianyuff823c72012-09-05 13:44:32 +0800147 dev_dbg(dev, "maxchild %d\n", udev->maxchild);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600148}
149
150static void usbip_dump_request_type(__u8 rt)
151{
152 switch (rt & USB_RECIP_MASK) {
153 case USB_RECIP_DEVICE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700154 pr_debug("DEVICE");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600155 break;
156 case USB_RECIP_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700157 pr_debug("INTERF");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600158 break;
159 case USB_RECIP_ENDPOINT:
matt mooney1a4b6f62011-05-19 16:47:32 -0700160 pr_debug("ENDPOI");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600161 break;
162 case USB_RECIP_OTHER:
matt mooney1a4b6f62011-05-19 16:47:32 -0700163 pr_debug("OTHER ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600164 break;
165 default:
matt mooney1a4b6f62011-05-19 16:47:32 -0700166 pr_debug("------");
matt mooney49aecef2011-05-06 03:47:54 -0700167 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600168 }
169}
170
171static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
172{
173 if (!cmd) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700174 pr_debug(" : null pointer\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600175 return;
176 }
177
matt mooney1a4b6f62011-05-19 16:47:32 -0700178 pr_debug(" ");
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100179 pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
180 cmd->bRequestType, cmd->bRequest,
matt mooney1a4b6f62011-05-19 16:47:32 -0700181 cmd->wValue, cmd->wIndex, cmd->wLength);
182 pr_debug("\n ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600183
184 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700185 pr_debug("STANDARD ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600186 switch (cmd->bRequest) {
187 case USB_REQ_GET_STATUS:
matt mooney1a4b6f62011-05-19 16:47:32 -0700188 pr_debug("GET_STATUS\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600189 break;
190 case USB_REQ_CLEAR_FEATURE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700191 pr_debug("CLEAR_FEAT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600192 break;
193 case USB_REQ_SET_FEATURE:
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400194 pr_debug("SET_FEAT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600195 break;
196 case USB_REQ_SET_ADDRESS:
matt mooney1a4b6f62011-05-19 16:47:32 -0700197 pr_debug("SET_ADDRRS\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600198 break;
199 case USB_REQ_GET_DESCRIPTOR:
matt mooney1a4b6f62011-05-19 16:47:32 -0700200 pr_debug("GET_DESCRI\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600201 break;
202 case USB_REQ_SET_DESCRIPTOR:
matt mooney1a4b6f62011-05-19 16:47:32 -0700203 pr_debug("SET_DESCRI\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600204 break;
205 case USB_REQ_GET_CONFIGURATION:
matt mooney1a4b6f62011-05-19 16:47:32 -0700206 pr_debug("GET_CONFIG\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600207 break;
208 case USB_REQ_SET_CONFIGURATION:
matt mooney1a4b6f62011-05-19 16:47:32 -0700209 pr_debug("SET_CONFIG\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600210 break;
211 case USB_REQ_GET_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700212 pr_debug("GET_INTERF\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600213 break;
214 case USB_REQ_SET_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700215 pr_debug("SET_INTERF\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600216 break;
217 case USB_REQ_SYNCH_FRAME:
matt mooney1a4b6f62011-05-19 16:47:32 -0700218 pr_debug("SYNC_FRAME\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600219 break;
220 default:
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400221 pr_debug("REQ(%02X)\n", cmd->bRequest);
matt mooney49aecef2011-05-06 03:47:54 -0700222 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600223 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600224 usbip_dump_request_type(cmd->bRequestType);
matt mooney1a4b6f62011-05-19 16:47:32 -0700225 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400226 pr_debug("CLASS\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700227 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400228 pr_debug("VENDOR\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700229 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
230 pr_debug("RESERVED\n");
231 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600232}
233
234void usbip_dump_urb(struct urb *urb)
235{
236 struct device *dev;
237
238 if (!urb) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700239 pr_debug("urb: null pointer!!\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600240 return;
241 }
242
243 if (!urb->dev) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700244 pr_debug("urb->dev: null pointer!!\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600245 return;
246 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700247
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600248 dev = &urb->dev->dev;
249
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600250 usbip_dump_usb_device(urb->dev);
251
252 dev_dbg(dev, " pipe :%08x ", urb->pipe);
253
254 usbip_dump_pipe(urb->pipe);
255
256 dev_dbg(dev, " status :%d\n", urb->status);
257 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600258 dev_dbg(dev, " transfer_buffer_length:%d\n",
259 urb->transfer_buffer_length);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600260 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600261
262 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
matt mooneyf9eacc92011-05-06 03:47:46 -0700263 usbip_dump_usb_ctrlrequest(
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600264 (struct usb_ctrlrequest *)urb->setup_packet);
265
266 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
267 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
268 dev_dbg(dev, " interval :%d\n", urb->interval);
269 dev_dbg(dev, " error_count :%d\n", urb->error_count);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600270}
271EXPORT_SYMBOL_GPL(usbip_dump_urb);
272
273void usbip_dump_header(struct usbip_header *pdu)
274{
matt mooney1a4b6f62011-05-19 16:47:32 -0700275 pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
276 pdu->base.command,
277 pdu->base.seqnum,
278 pdu->base.devid,
279 pdu->base.direction,
280 pdu->base.ep);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600281
282 switch (pdu->base.command) {
283 case USBIP_CMD_SUBMIT:
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100284 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 -0700285 pdu->u.cmd_submit.transfer_flags,
286 pdu->u.cmd_submit.transfer_buffer_length,
287 pdu->u.cmd_submit.start_frame,
288 pdu->u.cmd_submit.number_of_packets,
289 pdu->u.cmd_submit.interval);
matt mooneyf9eacc92011-05-06 03:47:46 -0700290 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600291 case USBIP_CMD_UNLINK:
matt mooney1a4b6f62011-05-19 16:47:32 -0700292 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
293 pdu->u.cmd_unlink.seqnum);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600294 break;
295 case USBIP_RET_SUBMIT:
matt mooney1a4b6f62011-05-19 16:47:32 -0700296 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
297 pdu->u.ret_submit.status,
298 pdu->u.ret_submit.actual_length,
299 pdu->u.ret_submit.start_frame,
300 pdu->u.ret_submit.number_of_packets,
301 pdu->u.ret_submit.error_count);
302 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600303 case USBIP_RET_UNLINK:
matt mooney1a4b6f62011-05-19 16:47:32 -0700304 pr_debug("USBIP_RET_UNLINK: status %d\n",
305 pdu->u.ret_unlink.status);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600306 break;
307 default:
308 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700309 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700310 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600311 }
312}
313EXPORT_SYMBOL_GPL(usbip_dump_header);
314
Bart Westgeest5a08c522011-12-19 17:44:11 -0500315/* Receive data over TCP/IP. */
316int usbip_recv(struct socket *sock, void *buf, int size)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600317{
318 int result;
319 struct msghdr msg;
320 struct kvec iov;
321 int total = 0;
322
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600323 /* for blocks of if (usbip_dbg_flag_xmit) */
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600324 char *bp = buf;
325 int osize = size;
326
Shuah Khan1ef5c432017-12-15 10:50:09 -0700327 if (!sock || !buf || !size)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600328 return -EINVAL;
Shuah Khan1ef5c432017-12-15 10:50:09 -0700329
330 usbip_dbg_xmit("enter\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600331
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600332 do {
333 sock->sk->sk_allocation = GFP_NOIO;
334 iov.iov_base = buf;
335 iov.iov_len = size;
336 msg.msg_name = NULL;
337 msg.msg_namelen = 0;
338 msg.msg_control = NULL;
339 msg.msg_controllen = 0;
Bart Westgeest5a08c522011-12-19 17:44:11 -0500340 msg.msg_flags = MSG_NOSIGNAL;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600341
Bart Westgeest5a08c522011-12-19 17:44:11 -0500342 result = kernel_recvmsg(sock, &msg, &iov, 1, size, MSG_WAITALL);
Shuah Khan1ef5c432017-12-15 10:50:09 -0700343 if (result <= 0)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600344 goto err;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600345
346 size -= result;
347 buf += result;
348 total += result;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600349 } while (size > 0);
350
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600351 if (usbip_dbg_flag_xmit) {
Bart Westgeest5a08c522011-12-19 17:44:11 -0500352 if (!in_interrupt())
353 pr_debug("%-10s:", current->comm);
354 else
355 pr_debug("interrupt :");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600356
Bart Westgeest5a08c522011-12-19 17:44:11 -0500357 pr_debug("receiving....\n");
358 usbip_dump_buffer(bp, osize);
359 pr_debug("received, osize %d ret %d size %d total %d\n",
Stefan Reifca9fb172013-04-04 16:03:07 +0200360 osize, result, size, total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600361 }
362
363 return total;
364
365err:
366 return result;
367}
Bart Westgeest5a08c522011-12-19 17:44:11 -0500368EXPORT_SYMBOL_GPL(usbip_recv);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600369
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600370/* there may be more cases to tweak the flags. */
371static unsigned int tweak_transfer_flags(unsigned int flags)
372{
Alan Stern85bcb5e2010-04-30 16:35:37 -0400373 flags &= ~URB_NO_TRANSFER_DMA_MAP;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600374 return flags;
375}
376
377static void usbip_pack_cmd_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_cmd_submit *spdu = &pdu->u.cmd_submit;
381
382 /*
383 * Some members are not still implemented in usbip. I hope this issue
384 * will be discussed when usbip is ported to other operating systems.
385 */
386 if (pack) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600387 spdu->transfer_flags =
matt mooneyf9eacc92011-05-06 03:47:46 -0700388 tweak_transfer_flags(urb->transfer_flags);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600389 spdu->transfer_buffer_length = urb->transfer_buffer_length;
390 spdu->start_frame = urb->start_frame;
391 spdu->number_of_packets = urb->number_of_packets;
392 spdu->interval = urb->interval;
393 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600394 urb->transfer_flags = spdu->transfer_flags;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600395 urb->transfer_buffer_length = spdu->transfer_buffer_length;
396 urb->start_frame = spdu->start_frame;
397 urb->number_of_packets = spdu->number_of_packets;
398 urb->interval = spdu->interval;
399 }
400}
401
402static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
matt mooneyf9eacc92011-05-06 03:47:46 -0700403 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600404{
405 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
406
407 if (pack) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600408 rpdu->status = urb->status;
409 rpdu->actual_length = urb->actual_length;
410 rpdu->start_frame = urb->start_frame;
Arjan Mels1325f852011-04-05 20:26:38 +0200411 rpdu->number_of_packets = urb->number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600412 rpdu->error_count = urb->error_count;
413 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600414 urb->status = rpdu->status;
415 urb->actual_length = rpdu->actual_length;
416 urb->start_frame = rpdu->start_frame;
Arjan Mels1325f852011-04-05 20:26:38 +0200417 urb->number_of_packets = rpdu->number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600418 urb->error_count = rpdu->error_count;
419 }
420}
421
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600422void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
matt mooneyf9eacc92011-05-06 03:47:46 -0700423 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600424{
425 switch (cmd) {
426 case USBIP_CMD_SUBMIT:
427 usbip_pack_cmd_submit(pdu, urb, pack);
428 break;
429 case USBIP_RET_SUBMIT:
430 usbip_pack_ret_submit(pdu, urb, pack);
431 break;
432 default:
matt mooney49aecef2011-05-06 03:47:54 -0700433 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700434 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700435 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600436 }
437}
438EXPORT_SYMBOL_GPL(usbip_pack_pdu);
439
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600440static void correct_endian_basic(struct usbip_header_basic *base, int send)
441{
442 if (send) {
443 base->command = cpu_to_be32(base->command);
444 base->seqnum = cpu_to_be32(base->seqnum);
445 base->devid = cpu_to_be32(base->devid);
446 base->direction = cpu_to_be32(base->direction);
447 base->ep = cpu_to_be32(base->ep);
448 } else {
449 base->command = be32_to_cpu(base->command);
450 base->seqnum = be32_to_cpu(base->seqnum);
451 base->devid = be32_to_cpu(base->devid);
452 base->direction = be32_to_cpu(base->direction);
453 base->ep = be32_to_cpu(base->ep);
454 }
455}
456
457static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700458 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600459{
460 if (send) {
461 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
462
463 cpu_to_be32s(&pdu->transfer_buffer_length);
464 cpu_to_be32s(&pdu->start_frame);
465 cpu_to_be32s(&pdu->number_of_packets);
466 cpu_to_be32s(&pdu->interval);
467 } else {
468 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
469
470 be32_to_cpus(&pdu->transfer_buffer_length);
471 be32_to_cpus(&pdu->start_frame);
472 be32_to_cpus(&pdu->number_of_packets);
473 be32_to_cpus(&pdu->interval);
474 }
475}
476
477static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700478 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600479{
480 if (send) {
481 cpu_to_be32s(&pdu->status);
482 cpu_to_be32s(&pdu->actual_length);
483 cpu_to_be32s(&pdu->start_frame);
Arjan Mels1325f852011-04-05 20:26:38 +0200484 cpu_to_be32s(&pdu->number_of_packets);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600485 cpu_to_be32s(&pdu->error_count);
486 } else {
487 be32_to_cpus(&pdu->status);
488 be32_to_cpus(&pdu->actual_length);
489 be32_to_cpus(&pdu->start_frame);
David Changcacd18a2011-05-12 18:31:11 +0800490 be32_to_cpus(&pdu->number_of_packets);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600491 be32_to_cpus(&pdu->error_count);
492 }
493}
494
495static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700496 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600497{
498 if (send)
499 pdu->seqnum = cpu_to_be32(pdu->seqnum);
500 else
501 pdu->seqnum = be32_to_cpu(pdu->seqnum);
502}
503
504static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700505 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600506{
507 if (send)
508 cpu_to_be32s(&pdu->status);
509 else
510 be32_to_cpus(&pdu->status);
511}
512
513void usbip_header_correct_endian(struct usbip_header *pdu, int send)
514{
515 __u32 cmd = 0;
516
517 if (send)
518 cmd = pdu->base.command;
519
520 correct_endian_basic(&pdu->base, send);
521
522 if (!send)
523 cmd = pdu->base.command;
524
525 switch (cmd) {
526 case USBIP_CMD_SUBMIT:
527 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
528 break;
529 case USBIP_RET_SUBMIT:
530 correct_endian_ret_submit(&pdu->u.ret_submit, send);
531 break;
532 case USBIP_CMD_UNLINK:
533 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
534 break;
535 case USBIP_RET_UNLINK:
536 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
537 break;
538 default:
matt mooney49aecef2011-05-06 03:47:54 -0700539 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700540 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700541 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600542 }
543}
544EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
545
matt mooney2282e1f2011-05-19 21:37:01 -0700546static void usbip_iso_packet_correct_endian(
matt mooney87352762011-05-19 21:36:56 -0700547 struct usbip_iso_packet_descriptor *iso, int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600548{
549 /* does not need all members. but copy all simply. */
550 if (send) {
551 iso->offset = cpu_to_be32(iso->offset);
552 iso->length = cpu_to_be32(iso->length);
553 iso->status = cpu_to_be32(iso->status);
554 iso->actual_length = cpu_to_be32(iso->actual_length);
555 } else {
556 iso->offset = be32_to_cpu(iso->offset);
557 iso->length = be32_to_cpu(iso->length);
558 iso->status = be32_to_cpu(iso->status);
559 iso->actual_length = be32_to_cpu(iso->actual_length);
560 }
561}
562
563static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
matt mooneyf9eacc92011-05-06 03:47:46 -0700564 struct usb_iso_packet_descriptor *uiso, int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600565{
566 if (pack) {
567 iso->offset = uiso->offset;
568 iso->length = uiso->length;
569 iso->status = uiso->status;
570 iso->actual_length = uiso->actual_length;
571 } else {
572 uiso->offset = iso->offset;
573 uiso->length = iso->length;
574 uiso->status = iso->status;
575 uiso->actual_length = iso->actual_length;
576 }
577}
578
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600579/* must free buffer */
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400580struct usbip_iso_packet_descriptor*
581usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600582{
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600583 struct usbip_iso_packet_descriptor *iso;
584 int np = urb->number_of_packets;
585 ssize_t size = np * sizeof(*iso);
586 int i;
587
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400588 iso = kzalloc(size, GFP_KERNEL);
589 if (!iso)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600590 return NULL;
591
592 for (i = 0; i < np; i++) {
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400593 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
594 usbip_iso_packet_correct_endian(&iso[i], 1);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600595 }
596
597 *bufflen = size;
598
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400599 return iso;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600600}
601EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
602
603/* some members of urb must be substituted before. */
604int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
605{
606 void *buff;
607 struct usbip_iso_packet_descriptor *iso;
608 int np = urb->number_of_packets;
609 int size = np * sizeof(*iso);
610 int i;
611 int ret;
Arjan Mels28276a22011-04-05 20:26:59 +0200612 int total_length = 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600613
614 if (!usb_pipeisoc(urb->pipe))
615 return 0;
616
617 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
Jake Champlinf14287b2013-01-16 22:16:05 -0500618 if (np == 0)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600619 return 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600620
621 buff = kzalloc(size, GFP_KERNEL);
622 if (!buff)
623 return -ENOMEM;
624
Bart Westgeest5a08c522011-12-19 17:44:11 -0500625 ret = usbip_recv(ud->tcp_socket, buff, size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600626 if (ret != size) {
627 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
628 ret);
629 kfree(buff);
630
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100631 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600632 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
633 else
634 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
635
636 return -EPIPE;
637 }
638
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400639 iso = (struct usbip_iso_packet_descriptor *) buff;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600640 for (i = 0; i < np; i++) {
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400641 usbip_iso_packet_correct_endian(&iso[i], 0);
642 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
Arjan Mels28276a22011-04-05 20:26:59 +0200643 total_length += urb->iso_frame_desc[i].actual_length;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600644 }
645
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600646 kfree(buff);
647
Arjan Mels28276a22011-04-05 20:26:59 +0200648 if (total_length != urb->actual_length) {
649 dev_err(&urb->dev->dev,
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100650 "total length of iso packets %d not equal to actual length of buffer %d\n",
matt mooneyf9eacc92011-05-06 03:47:46 -0700651 total_length, urb->actual_length);
Arjan Mels28276a22011-04-05 20:26:59 +0200652
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100653 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
Arjan Mels28276a22011-04-05 20:26:59 +0200654 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
655 else
656 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
657
658 return -EPIPE;
659 }
660
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600661 return ret;
662}
663EXPORT_SYMBOL_GPL(usbip_recv_iso);
664
Arjan Mels28276a22011-04-05 20:26:59 +0200665/*
666 * This functions restores the padding which was removed for optimizing
667 * the bandwidth during transfer over tcp/ip
668 *
669 * buffer and iso packets need to be stored and be in propeper endian in urb
670 * before calling this function
671 */
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500672void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
Arjan Mels28276a22011-04-05 20:26:59 +0200673{
674 int np = urb->number_of_packets;
675 int i;
Arjan Mels28276a22011-04-05 20:26:59 +0200676 int actualoffset = urb->actual_length;
677
678 if (!usb_pipeisoc(urb->pipe))
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500679 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200680
681 /* if no packets or length of data is 0, then nothing to unpack */
682 if (np == 0 || urb->actual_length == 0)
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500683 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200684
685 /*
686 * if actual_length is transfer_buffer_length then no padding is
687 * present.
Bart Westgeestc7f00892012-10-10 13:34:27 -0400688 */
Arjan Mels28276a22011-04-05 20:26:59 +0200689 if (urb->actual_length == urb->transfer_buffer_length)
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500690 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200691
692 /*
693 * loop over all packets from last to first (to prevent overwritting
694 * memory when padding) and move them into the proper place
695 */
696 for (i = np-1; i > 0; i--) {
697 actualoffset -= urb->iso_frame_desc[i].actual_length;
698 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
matt mooneyf9eacc92011-05-06 03:47:46 -0700699 urb->transfer_buffer + actualoffset,
700 urb->iso_frame_desc[i].actual_length);
Arjan Mels28276a22011-04-05 20:26:59 +0200701 }
Arjan Mels28276a22011-04-05 20:26:59 +0200702}
703EXPORT_SYMBOL_GPL(usbip_pad_iso);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600704
705/* some members of urb must be substituted before. */
706int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
707{
708 int ret;
709 int size;
710
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100711 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600712 /* the direction of urb must be OUT. */
713 if (usb_pipein(urb->pipe))
714 return 0;
715
716 size = urb->transfer_buffer_length;
717 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600718 /* the direction of urb must be IN. */
719 if (usb_pipeout(urb->pipe))
720 return 0;
721
722 size = urb->actual_length;
723 }
724
725 /* no need to recv xbuff */
726 if (!(size > 0))
727 return 0;
728
Ignat Korchaginb348d7d2016-03-17 18:00:29 +0000729 if (size > urb->transfer_buffer_length) {
730 /* should not happen, probably malicious packet */
731 if (ud->side == USBIP_STUB) {
732 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
733 return 0;
734 } else {
735 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
736 return -EPIPE;
737 }
738 }
739
Bart Westgeest5a08c522011-12-19 17:44:11 -0500740 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600741 if (ret != size) {
742 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100743 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600744 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
745 } else {
746 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
747 return -EPIPE;
748 }
749 }
750
751 return ret;
752}
753EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
754
matt mooney3028d0a2011-05-19 21:37:05 -0700755static int __init usbip_core_init(void)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600756{
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900757 int ret;
758
matt mooney1a4b6f62011-05-19 16:47:32 -0700759 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
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");
matt mooney6973c6f2011-05-11 01:54:14 -0700779MODULE_VERSION(USBIP_VERSION);