blob: 8b232290be6b97a04be362582e4a4d87db8d9185 [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
matt mooney1a4b6f62011-05-19 16:47:32 -0700108 pr_debug("tt %p, ttport %d\n", udev->tt, 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
141 dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
142
Himangi Saraogib482da22013-11-02 17:38:10 +0530143 dev_dbg(dev,
144 "descriptor %p, config %p, actconfig %p, rawdescriptors %p\n",
145 &udev->descriptor, udev->config,
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600146 udev->actconfig, udev->rawdescriptors);
147
148 dev_dbg(dev, "have_langid %d, string_langid %d\n",
149 udev->have_langid, udev->string_langid);
150
Lan Tianyuff823c72012-09-05 13:44:32 +0800151 dev_dbg(dev, "maxchild %d\n", udev->maxchild);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600152}
153
154static void usbip_dump_request_type(__u8 rt)
155{
156 switch (rt & USB_RECIP_MASK) {
157 case USB_RECIP_DEVICE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700158 pr_debug("DEVICE");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600159 break;
160 case USB_RECIP_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700161 pr_debug("INTERF");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600162 break;
163 case USB_RECIP_ENDPOINT:
matt mooney1a4b6f62011-05-19 16:47:32 -0700164 pr_debug("ENDPOI");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600165 break;
166 case USB_RECIP_OTHER:
matt mooney1a4b6f62011-05-19 16:47:32 -0700167 pr_debug("OTHER ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600168 break;
169 default:
matt mooney1a4b6f62011-05-19 16:47:32 -0700170 pr_debug("------");
matt mooney49aecef2011-05-06 03:47:54 -0700171 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600172 }
173}
174
175static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
176{
177 if (!cmd) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700178 pr_debug(" : null pointer\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600179 return;
180 }
181
matt mooney1a4b6f62011-05-19 16:47:32 -0700182 pr_debug(" ");
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100183 pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
184 cmd->bRequestType, cmd->bRequest,
matt mooney1a4b6f62011-05-19 16:47:32 -0700185 cmd->wValue, cmd->wIndex, cmd->wLength);
186 pr_debug("\n ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600187
188 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700189 pr_debug("STANDARD ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600190 switch (cmd->bRequest) {
191 case USB_REQ_GET_STATUS:
matt mooney1a4b6f62011-05-19 16:47:32 -0700192 pr_debug("GET_STATUS\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600193 break;
194 case USB_REQ_CLEAR_FEATURE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700195 pr_debug("CLEAR_FEAT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600196 break;
197 case USB_REQ_SET_FEATURE:
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400198 pr_debug("SET_FEAT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600199 break;
200 case USB_REQ_SET_ADDRESS:
matt mooney1a4b6f62011-05-19 16:47:32 -0700201 pr_debug("SET_ADDRRS\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600202 break;
203 case USB_REQ_GET_DESCRIPTOR:
matt mooney1a4b6f62011-05-19 16:47:32 -0700204 pr_debug("GET_DESCRI\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600205 break;
206 case USB_REQ_SET_DESCRIPTOR:
matt mooney1a4b6f62011-05-19 16:47:32 -0700207 pr_debug("SET_DESCRI\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600208 break;
209 case USB_REQ_GET_CONFIGURATION:
matt mooney1a4b6f62011-05-19 16:47:32 -0700210 pr_debug("GET_CONFIG\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600211 break;
212 case USB_REQ_SET_CONFIGURATION:
matt mooney1a4b6f62011-05-19 16:47:32 -0700213 pr_debug("SET_CONFIG\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600214 break;
215 case USB_REQ_GET_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700216 pr_debug("GET_INTERF\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600217 break;
218 case USB_REQ_SET_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700219 pr_debug("SET_INTERF\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600220 break;
221 case USB_REQ_SYNCH_FRAME:
matt mooney1a4b6f62011-05-19 16:47:32 -0700222 pr_debug("SYNC_FRAME\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600223 break;
224 default:
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400225 pr_debug("REQ(%02X)\n", cmd->bRequest);
matt mooney49aecef2011-05-06 03:47:54 -0700226 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600227 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600228 usbip_dump_request_type(cmd->bRequestType);
matt mooney1a4b6f62011-05-19 16:47:32 -0700229 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400230 pr_debug("CLASS\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700231 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400232 pr_debug("VENDOR\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700233 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
234 pr_debug("RESERVED\n");
235 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600236}
237
238void usbip_dump_urb(struct urb *urb)
239{
240 struct device *dev;
241
242 if (!urb) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700243 pr_debug("urb: null pointer!!\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600244 return;
245 }
246
247 if (!urb->dev) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700248 pr_debug("urb->dev: null pointer!!\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600249 return;
250 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700251
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600252 dev = &urb->dev->dev;
253
254 dev_dbg(dev, " urb :%p\n", urb);
255 dev_dbg(dev, " dev :%p\n", urb->dev);
256
257 usbip_dump_usb_device(urb->dev);
258
259 dev_dbg(dev, " pipe :%08x ", urb->pipe);
260
261 usbip_dump_pipe(urb->pipe);
262
263 dev_dbg(dev, " status :%d\n", urb->status);
264 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
265 dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600266 dev_dbg(dev, " transfer_buffer_length:%d\n",
267 urb->transfer_buffer_length);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600268 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
269 dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
270
271 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
matt mooneyf9eacc92011-05-06 03:47:46 -0700272 usbip_dump_usb_ctrlrequest(
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600273 (struct usb_ctrlrequest *)urb->setup_packet);
274
275 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
276 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
277 dev_dbg(dev, " interval :%d\n", urb->interval);
278 dev_dbg(dev, " error_count :%d\n", urb->error_count);
279 dev_dbg(dev, " context :%p\n", urb->context);
280 dev_dbg(dev, " complete :%p\n", urb->complete);
281}
282EXPORT_SYMBOL_GPL(usbip_dump_urb);
283
284void usbip_dump_header(struct usbip_header *pdu)
285{
matt mooney1a4b6f62011-05-19 16:47:32 -0700286 pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
287 pdu->base.command,
288 pdu->base.seqnum,
289 pdu->base.devid,
290 pdu->base.direction,
291 pdu->base.ep);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600292
293 switch (pdu->base.command) {
294 case USBIP_CMD_SUBMIT:
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100295 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 -0700296 pdu->u.cmd_submit.transfer_flags,
297 pdu->u.cmd_submit.transfer_buffer_length,
298 pdu->u.cmd_submit.start_frame,
299 pdu->u.cmd_submit.number_of_packets,
300 pdu->u.cmd_submit.interval);
matt mooneyf9eacc92011-05-06 03:47:46 -0700301 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600302 case USBIP_CMD_UNLINK:
matt mooney1a4b6f62011-05-19 16:47:32 -0700303 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
304 pdu->u.cmd_unlink.seqnum);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600305 break;
306 case USBIP_RET_SUBMIT:
matt mooney1a4b6f62011-05-19 16:47:32 -0700307 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
308 pdu->u.ret_submit.status,
309 pdu->u.ret_submit.actual_length,
310 pdu->u.ret_submit.start_frame,
311 pdu->u.ret_submit.number_of_packets,
312 pdu->u.ret_submit.error_count);
313 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600314 case USBIP_RET_UNLINK:
matt mooney1a4b6f62011-05-19 16:47:32 -0700315 pr_debug("USBIP_RET_UNLINK: status %d\n",
316 pdu->u.ret_unlink.status);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600317 break;
318 default:
319 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700320 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700321 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600322 }
323}
324EXPORT_SYMBOL_GPL(usbip_dump_header);
325
Bart Westgeest5a08c522011-12-19 17:44:11 -0500326/* Receive data over TCP/IP. */
327int usbip_recv(struct socket *sock, void *buf, int size)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600328{
329 int result;
330 struct msghdr msg;
331 struct kvec iov;
332 int total = 0;
333
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600334 /* for blocks of if (usbip_dbg_flag_xmit) */
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600335 char *bp = buf;
336 int osize = size;
337
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600338 usbip_dbg_xmit("enter\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600339
340 if (!sock || !buf || !size) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700341 pr_err("invalid arg, sock %p buff %p size %d\n", sock, buf,
342 size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600343 return -EINVAL;
344 }
345
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600346 do {
347 sock->sk->sk_allocation = GFP_NOIO;
348 iov.iov_base = buf;
349 iov.iov_len = size;
350 msg.msg_name = NULL;
351 msg.msg_namelen = 0;
352 msg.msg_control = NULL;
353 msg.msg_controllen = 0;
Bart Westgeest5a08c522011-12-19 17:44:11 -0500354 msg.msg_flags = MSG_NOSIGNAL;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600355
Bart Westgeest5a08c522011-12-19 17:44:11 -0500356 result = kernel_recvmsg(sock, &msg, &iov, 1, size, MSG_WAITALL);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600357 if (result <= 0) {
Bart Westgeest5a08c522011-12-19 17:44:11 -0500358 pr_debug("receive sock %p buf %p size %u ret %d total %d\n",
359 sock, buf, size, result, total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600360 goto err;
361 }
362
363 size -= result;
364 buf += result;
365 total += result;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600366 } while (size > 0);
367
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600368 if (usbip_dbg_flag_xmit) {
Bart Westgeest5a08c522011-12-19 17:44:11 -0500369 if (!in_interrupt())
370 pr_debug("%-10s:", current->comm);
371 else
372 pr_debug("interrupt :");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600373
Bart Westgeest5a08c522011-12-19 17:44:11 -0500374 pr_debug("receiving....\n");
375 usbip_dump_buffer(bp, osize);
376 pr_debug("received, osize %d ret %d size %d total %d\n",
Stefan Reifca9fb172013-04-04 16:03:07 +0200377 osize, result, size, total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600378 }
379
380 return total;
381
382err:
383 return result;
384}
Bart Westgeest5a08c522011-12-19 17:44:11 -0500385EXPORT_SYMBOL_GPL(usbip_recv);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600386
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600387/* there may be more cases to tweak the flags. */
388static unsigned int tweak_transfer_flags(unsigned int flags)
389{
Alan Stern85bcb5e2010-04-30 16:35:37 -0400390 flags &= ~URB_NO_TRANSFER_DMA_MAP;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600391 return flags;
392}
393
394static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
matt mooneyf9eacc92011-05-06 03:47:46 -0700395 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600396{
397 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
398
399 /*
400 * Some members are not still implemented in usbip. I hope this issue
401 * will be discussed when usbip is ported to other operating systems.
402 */
403 if (pack) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600404 spdu->transfer_flags =
matt mooneyf9eacc92011-05-06 03:47:46 -0700405 tweak_transfer_flags(urb->transfer_flags);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600406 spdu->transfer_buffer_length = urb->transfer_buffer_length;
407 spdu->start_frame = urb->start_frame;
408 spdu->number_of_packets = urb->number_of_packets;
409 spdu->interval = urb->interval;
410 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600411 urb->transfer_flags = spdu->transfer_flags;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600412 urb->transfer_buffer_length = spdu->transfer_buffer_length;
413 urb->start_frame = spdu->start_frame;
414 urb->number_of_packets = spdu->number_of_packets;
415 urb->interval = spdu->interval;
416 }
417}
418
419static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
matt mooneyf9eacc92011-05-06 03:47:46 -0700420 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600421{
422 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
423
424 if (pack) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600425 rpdu->status = urb->status;
426 rpdu->actual_length = urb->actual_length;
427 rpdu->start_frame = urb->start_frame;
Arjan Mels1325f852011-04-05 20:26:38 +0200428 rpdu->number_of_packets = urb->number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600429 rpdu->error_count = urb->error_count;
430 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600431 urb->status = rpdu->status;
432 urb->actual_length = rpdu->actual_length;
433 urb->start_frame = rpdu->start_frame;
Arjan Mels1325f852011-04-05 20:26:38 +0200434 urb->number_of_packets = rpdu->number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600435 urb->error_count = rpdu->error_count;
436 }
437}
438
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600439void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
matt mooneyf9eacc92011-05-06 03:47:46 -0700440 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600441{
442 switch (cmd) {
443 case USBIP_CMD_SUBMIT:
444 usbip_pack_cmd_submit(pdu, urb, pack);
445 break;
446 case USBIP_RET_SUBMIT:
447 usbip_pack_ret_submit(pdu, urb, pack);
448 break;
449 default:
matt mooney49aecef2011-05-06 03:47:54 -0700450 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700451 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700452 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600453 }
454}
455EXPORT_SYMBOL_GPL(usbip_pack_pdu);
456
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600457static void correct_endian_basic(struct usbip_header_basic *base, int send)
458{
459 if (send) {
460 base->command = cpu_to_be32(base->command);
461 base->seqnum = cpu_to_be32(base->seqnum);
462 base->devid = cpu_to_be32(base->devid);
463 base->direction = cpu_to_be32(base->direction);
464 base->ep = cpu_to_be32(base->ep);
465 } else {
466 base->command = be32_to_cpu(base->command);
467 base->seqnum = be32_to_cpu(base->seqnum);
468 base->devid = be32_to_cpu(base->devid);
469 base->direction = be32_to_cpu(base->direction);
470 base->ep = be32_to_cpu(base->ep);
471 }
472}
473
474static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700475 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600476{
477 if (send) {
478 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
479
480 cpu_to_be32s(&pdu->transfer_buffer_length);
481 cpu_to_be32s(&pdu->start_frame);
482 cpu_to_be32s(&pdu->number_of_packets);
483 cpu_to_be32s(&pdu->interval);
484 } else {
485 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
486
487 be32_to_cpus(&pdu->transfer_buffer_length);
488 be32_to_cpus(&pdu->start_frame);
489 be32_to_cpus(&pdu->number_of_packets);
490 be32_to_cpus(&pdu->interval);
491 }
492}
493
494static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700495 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600496{
497 if (send) {
498 cpu_to_be32s(&pdu->status);
499 cpu_to_be32s(&pdu->actual_length);
500 cpu_to_be32s(&pdu->start_frame);
Arjan Mels1325f852011-04-05 20:26:38 +0200501 cpu_to_be32s(&pdu->number_of_packets);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600502 cpu_to_be32s(&pdu->error_count);
503 } else {
504 be32_to_cpus(&pdu->status);
505 be32_to_cpus(&pdu->actual_length);
506 be32_to_cpus(&pdu->start_frame);
David Changcacd18a2011-05-12 18:31:11 +0800507 be32_to_cpus(&pdu->number_of_packets);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600508 be32_to_cpus(&pdu->error_count);
509 }
510}
511
512static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700513 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600514{
515 if (send)
516 pdu->seqnum = cpu_to_be32(pdu->seqnum);
517 else
518 pdu->seqnum = be32_to_cpu(pdu->seqnum);
519}
520
521static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700522 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600523{
524 if (send)
525 cpu_to_be32s(&pdu->status);
526 else
527 be32_to_cpus(&pdu->status);
528}
529
530void usbip_header_correct_endian(struct usbip_header *pdu, int send)
531{
532 __u32 cmd = 0;
533
534 if (send)
535 cmd = pdu->base.command;
536
537 correct_endian_basic(&pdu->base, send);
538
539 if (!send)
540 cmd = pdu->base.command;
541
542 switch (cmd) {
543 case USBIP_CMD_SUBMIT:
544 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
545 break;
546 case USBIP_RET_SUBMIT:
547 correct_endian_ret_submit(&pdu->u.ret_submit, send);
548 break;
549 case USBIP_CMD_UNLINK:
550 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
551 break;
552 case USBIP_RET_UNLINK:
553 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
554 break;
555 default:
matt mooney49aecef2011-05-06 03:47:54 -0700556 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700557 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700558 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600559 }
560}
561EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
562
matt mooney2282e1f2011-05-19 21:37:01 -0700563static void usbip_iso_packet_correct_endian(
matt mooney87352762011-05-19 21:36:56 -0700564 struct usbip_iso_packet_descriptor *iso, int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600565{
566 /* does not need all members. but copy all simply. */
567 if (send) {
568 iso->offset = cpu_to_be32(iso->offset);
569 iso->length = cpu_to_be32(iso->length);
570 iso->status = cpu_to_be32(iso->status);
571 iso->actual_length = cpu_to_be32(iso->actual_length);
572 } else {
573 iso->offset = be32_to_cpu(iso->offset);
574 iso->length = be32_to_cpu(iso->length);
575 iso->status = be32_to_cpu(iso->status);
576 iso->actual_length = be32_to_cpu(iso->actual_length);
577 }
578}
579
580static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
matt mooneyf9eacc92011-05-06 03:47:46 -0700581 struct usb_iso_packet_descriptor *uiso, int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600582{
583 if (pack) {
584 iso->offset = uiso->offset;
585 iso->length = uiso->length;
586 iso->status = uiso->status;
587 iso->actual_length = uiso->actual_length;
588 } else {
589 uiso->offset = iso->offset;
590 uiso->length = iso->length;
591 uiso->status = iso->status;
592 uiso->actual_length = iso->actual_length;
593 }
594}
595
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600596/* must free buffer */
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400597struct usbip_iso_packet_descriptor*
598usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600599{
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600600 struct usbip_iso_packet_descriptor *iso;
601 int np = urb->number_of_packets;
602 ssize_t size = np * sizeof(*iso);
603 int i;
604
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400605 iso = kzalloc(size, GFP_KERNEL);
606 if (!iso)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600607 return NULL;
608
609 for (i = 0; i < np; i++) {
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400610 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
611 usbip_iso_packet_correct_endian(&iso[i], 1);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600612 }
613
614 *bufflen = size;
615
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400616 return iso;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600617}
618EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
619
620/* some members of urb must be substituted before. */
621int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
622{
623 void *buff;
624 struct usbip_iso_packet_descriptor *iso;
625 int np = urb->number_of_packets;
626 int size = np * sizeof(*iso);
627 int i;
628 int ret;
Arjan Mels28276a22011-04-05 20:26:59 +0200629 int total_length = 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600630
631 if (!usb_pipeisoc(urb->pipe))
632 return 0;
633
634 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
Jake Champlinf14287b2013-01-16 22:16:05 -0500635 if (np == 0)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600636 return 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600637
638 buff = kzalloc(size, GFP_KERNEL);
639 if (!buff)
640 return -ENOMEM;
641
Bart Westgeest5a08c522011-12-19 17:44:11 -0500642 ret = usbip_recv(ud->tcp_socket, buff, size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600643 if (ret != size) {
644 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
645 ret);
646 kfree(buff);
647
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100648 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600649 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
650 else
651 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
652
653 return -EPIPE;
654 }
655
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400656 iso = (struct usbip_iso_packet_descriptor *) buff;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600657 for (i = 0; i < np; i++) {
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400658 usbip_iso_packet_correct_endian(&iso[i], 0);
659 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
Arjan Mels28276a22011-04-05 20:26:59 +0200660 total_length += urb->iso_frame_desc[i].actual_length;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600661 }
662
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600663 kfree(buff);
664
Arjan Mels28276a22011-04-05 20:26:59 +0200665 if (total_length != urb->actual_length) {
666 dev_err(&urb->dev->dev,
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100667 "total length of iso packets %d not equal to actual length of buffer %d\n",
matt mooneyf9eacc92011-05-06 03:47:46 -0700668 total_length, urb->actual_length);
Arjan Mels28276a22011-04-05 20:26:59 +0200669
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100670 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
Arjan Mels28276a22011-04-05 20:26:59 +0200671 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
672 else
673 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
674
675 return -EPIPE;
676 }
677
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600678 return ret;
679}
680EXPORT_SYMBOL_GPL(usbip_recv_iso);
681
Arjan Mels28276a22011-04-05 20:26:59 +0200682/*
683 * This functions restores the padding which was removed for optimizing
684 * the bandwidth during transfer over tcp/ip
685 *
686 * buffer and iso packets need to be stored and be in propeper endian in urb
687 * before calling this function
688 */
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500689void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
Arjan Mels28276a22011-04-05 20:26:59 +0200690{
691 int np = urb->number_of_packets;
692 int i;
Arjan Mels28276a22011-04-05 20:26:59 +0200693 int actualoffset = urb->actual_length;
694
695 if (!usb_pipeisoc(urb->pipe))
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500696 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200697
698 /* if no packets or length of data is 0, then nothing to unpack */
699 if (np == 0 || urb->actual_length == 0)
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500700 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200701
702 /*
703 * if actual_length is transfer_buffer_length then no padding is
704 * present.
Bart Westgeestc7f00892012-10-10 13:34:27 -0400705 */
Arjan Mels28276a22011-04-05 20:26:59 +0200706 if (urb->actual_length == urb->transfer_buffer_length)
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500707 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200708
709 /*
710 * loop over all packets from last to first (to prevent overwritting
711 * memory when padding) and move them into the proper place
712 */
713 for (i = np-1; i > 0; i--) {
714 actualoffset -= urb->iso_frame_desc[i].actual_length;
715 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
matt mooneyf9eacc92011-05-06 03:47:46 -0700716 urb->transfer_buffer + actualoffset,
717 urb->iso_frame_desc[i].actual_length);
Arjan Mels28276a22011-04-05 20:26:59 +0200718 }
Arjan Mels28276a22011-04-05 20:26:59 +0200719}
720EXPORT_SYMBOL_GPL(usbip_pad_iso);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600721
722/* some members of urb must be substituted before. */
723int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
724{
725 int ret;
726 int size;
727
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100728 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600729 /* the direction of urb must be OUT. */
730 if (usb_pipein(urb->pipe))
731 return 0;
732
733 size = urb->transfer_buffer_length;
734 } else {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600735 /* the direction of urb must be IN. */
736 if (usb_pipeout(urb->pipe))
737 return 0;
738
739 size = urb->actual_length;
740 }
741
742 /* no need to recv xbuff */
743 if (!(size > 0))
744 return 0;
745
Ignat Korchaginb348d7d2016-03-17 18:00:29 +0000746 if (size > urb->transfer_buffer_length) {
747 /* should not happen, probably malicious packet */
748 if (ud->side == USBIP_STUB) {
749 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
750 return 0;
751 } else {
752 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
753 return -EPIPE;
754 }
755 }
756
Bart Westgeest5a08c522011-12-19 17:44:11 -0500757 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600758 if (ret != size) {
759 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100760 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600761 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
762 } else {
763 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
764 return -EPIPE;
765 }
766 }
767
768 return ret;
769}
770EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
771
matt mooney3028d0a2011-05-19 21:37:05 -0700772static int __init usbip_core_init(void)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600773{
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900774 int ret;
775
matt mooney1a4b6f62011-05-19 16:47:32 -0700776 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900777 ret = usbip_init_eh();
778 if (ret)
779 return ret;
780
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600781 return 0;
782}
783
matt mooney3028d0a2011-05-19 21:37:05 -0700784static void __exit usbip_core_exit(void)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600785{
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900786 usbip_finish_eh();
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600787 return;
788}
789
matt mooney3028d0a2011-05-19 21:37:05 -0700790module_init(usbip_core_init);
791module_exit(usbip_core_exit);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600792
793MODULE_AUTHOR(DRIVER_AUTHOR);
794MODULE_DESCRIPTION(DRIVER_DESC);
795MODULE_LICENSE("GPL");
matt mooney6973c6f2011-05-11 01:54:14 -0700796MODULE_VERSION(USBIP_VERSION);