blob: 70f23026932965fd997528ef48ad62d626f4c9a6 [file] [log] [blame]
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06001/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
matt mooney7aaacb42011-05-11 22:33:43 -070020#include <asm/byteorder.h>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060021#include <linux/file.h>
matt mooney7aaacb42011-05-11 22:33:43 -070022#include <linux/fs.h>
23#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Paul Gortmaker45296232011-08-30 17:50:46 -040025#include <linux/module.h>
matt mooney7aaacb42011-05-11 22:33:43 -070026#include <net/sock.h>
matt mooney4ce0a412011-05-06 03:47:56 -070027
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060028#include "usbip_common.h"
29
matt mooney4ce0a412011-05-06 03:47:56 -070030#define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
matt mooney64e62422011-05-11 22:33:44 -070031#define DRIVER_DESC "USB/IP Core"
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060032
matt mooney64e62422011-05-11 22:33:44 -070033#ifdef CONFIG_USBIP_DEBUG
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060034unsigned long usbip_debug_flag = 0xffffffff;
35#else
36unsigned long usbip_debug_flag;
37#endif
38EXPORT_SYMBOL_GPL(usbip_debug_flag);
39
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060040/* FIXME */
41struct device_attribute dev_attr_usbip_debug;
42EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
43
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060044static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
matt mooneyf9eacc92011-05-06 03:47:46 -070045 char *buf)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060046{
47 return sprintf(buf, "%lx\n", usbip_debug_flag);
48}
49
50static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
matt mooneyf9eacc92011-05-06 03:47:46 -070051 const char *buf, size_t count)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060052{
Himanshu Chauhan1e5065db2010-01-23 01:51:57 +053053 sscanf(buf, "%lx", &usbip_debug_flag);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060054 return count;
55}
56DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
57
58static void usbip_dump_buffer(char *buff, int bufflen)
59{
matt mooney1a4b6f62011-05-19 16:47:32 -070060 print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4,
Himanshu Chauhanaad86572010-01-23 02:52:41 +053061 buff, bufflen, false);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060062}
63
64static void usbip_dump_pipe(unsigned int p)
65{
66 unsigned char type = usb_pipetype(p);
matt mooney87352762011-05-19 21:36:56 -070067 unsigned char ep = usb_pipeendpoint(p);
68 unsigned char dev = usb_pipedevice(p);
69 unsigned char dir = usb_pipein(p);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060070
matt mooney1a4b6f62011-05-19 16:47:32 -070071 pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060072
73 switch (type) {
74 case PIPE_ISOCHRONOUS:
matt mooney1a4b6f62011-05-19 16:47:32 -070075 pr_debug("ISO\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060076 break;
77 case PIPE_INTERRUPT:
matt mooney1a4b6f62011-05-19 16:47:32 -070078 pr_debug("INT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060079 break;
80 case PIPE_CONTROL:
matt mooney1a4b6f62011-05-19 16:47:32 -070081 pr_debug("CTRL\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060082 break;
83 case PIPE_BULK:
matt mooney1a4b6f62011-05-19 16:47:32 -070084 pr_debug("BULK\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060085 break;
86 default:
matt mooney1a4b6f62011-05-19 16:47:32 -070087 pr_debug("ERR\n");
matt mooney49aecef2011-05-06 03:47:54 -070088 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060089 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060090}
91
92static void usbip_dump_usb_device(struct usb_device *udev)
93{
94 struct device *dev = &udev->dev;
95 int i;
96
matt mooney1a4b6f62011-05-19 16:47:32 -070097 dev_dbg(dev, " devnum(%d) devpath(%s) ",
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060098 udev->devnum, udev->devpath);
99
100 switch (udev->speed) {
101 case USB_SPEED_HIGH:
matt mooney1a4b6f62011-05-19 16:47:32 -0700102 pr_debug("SPD_HIGH ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600103 break;
104 case USB_SPEED_FULL:
matt mooney1a4b6f62011-05-19 16:47:32 -0700105 pr_debug("SPD_FULL ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600106 break;
107 case USB_SPEED_LOW:
matt mooney1a4b6f62011-05-19 16:47:32 -0700108 pr_debug("SPD_LOW ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600109 break;
110 case USB_SPEED_UNKNOWN:
matt mooney1a4b6f62011-05-19 16:47:32 -0700111 pr_debug("SPD_UNKNOWN ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600112 break;
113 default:
matt mooney1a4b6f62011-05-19 16:47:32 -0700114 pr_debug("SPD_ERROR ");
matt mooney49aecef2011-05-06 03:47:54 -0700115 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600116 }
117
matt mooney1a4b6f62011-05-19 16:47:32 -0700118 pr_debug("tt %p, ttport %d\n", udev->tt, udev->ttport);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600119
120 dev_dbg(dev, " ");
121 for (i = 0; i < 16; i++)
matt mooney1a4b6f62011-05-19 16:47:32 -0700122 pr_debug(" %2u", i);
123 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600124
125 dev_dbg(dev, " toggle0(IN) :");
126 for (i = 0; i < 16; i++)
matt mooney1a4b6f62011-05-19 16:47:32 -0700127 pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
128 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600129
130 dev_dbg(dev, " toggle1(OUT):");
131 for (i = 0; i < 16; i++)
matt mooney1a4b6f62011-05-19 16:47:32 -0700132 pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
133 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600134
135 dev_dbg(dev, " epmaxp_in :");
136 for (i = 0; i < 16; i++) {
137 if (udev->ep_in[i])
matt mooney1a4b6f62011-05-19 16:47:32 -0700138 pr_debug(" %2u",
139 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600140 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700141 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600142
143 dev_dbg(dev, " epmaxp_out :");
144 for (i = 0; i < 16; i++) {
145 if (udev->ep_out[i])
matt mooney1a4b6f62011-05-19 16:47:32 -0700146 pr_debug(" %2u",
147 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600148 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700149 pr_debug("\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600150
151 dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
152
153 dev_dbg(dev, "descriptor %p, config %p, actconfig %p, "
154 "rawdescriptors %p\n", &udev->descriptor, udev->config,
155 udev->actconfig, udev->rawdescriptors);
156
157 dev_dbg(dev, "have_langid %d, string_langid %d\n",
158 udev->have_langid, udev->string_langid);
159
160 dev_dbg(dev, "maxchild %d, children %p\n",
161 udev->maxchild, udev->children);
162}
163
164static void usbip_dump_request_type(__u8 rt)
165{
166 switch (rt & USB_RECIP_MASK) {
167 case USB_RECIP_DEVICE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700168 pr_debug("DEVICE");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600169 break;
170 case USB_RECIP_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700171 pr_debug("INTERF");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600172 break;
173 case USB_RECIP_ENDPOINT:
matt mooney1a4b6f62011-05-19 16:47:32 -0700174 pr_debug("ENDPOI");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600175 break;
176 case USB_RECIP_OTHER:
matt mooney1a4b6f62011-05-19 16:47:32 -0700177 pr_debug("OTHER ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600178 break;
179 default:
matt mooney1a4b6f62011-05-19 16:47:32 -0700180 pr_debug("------");
matt mooney49aecef2011-05-06 03:47:54 -0700181 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600182 }
183}
184
185static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
186{
187 if (!cmd) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700188 pr_debug(" : null pointer\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600189 return;
190 }
191
matt mooney1a4b6f62011-05-19 16:47:32 -0700192 pr_debug(" ");
193 pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) "
194 "wLength(%04X) ", cmd->bRequestType, cmd->bRequest,
195 cmd->wValue, cmd->wIndex, cmd->wLength);
196 pr_debug("\n ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600197
198 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700199 pr_debug("STANDARD ");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600200 switch (cmd->bRequest) {
201 case USB_REQ_GET_STATUS:
matt mooney1a4b6f62011-05-19 16:47:32 -0700202 pr_debug("GET_STATUS\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600203 break;
204 case USB_REQ_CLEAR_FEATURE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700205 pr_debug("CLEAR_FEAT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600206 break;
207 case USB_REQ_SET_FEATURE:
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400208 pr_debug("SET_FEAT\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600209 break;
210 case USB_REQ_SET_ADDRESS:
matt mooney1a4b6f62011-05-19 16:47:32 -0700211 pr_debug("SET_ADDRRS\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600212 break;
213 case USB_REQ_GET_DESCRIPTOR:
matt mooney1a4b6f62011-05-19 16:47:32 -0700214 pr_debug("GET_DESCRI\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600215 break;
216 case USB_REQ_SET_DESCRIPTOR:
matt mooney1a4b6f62011-05-19 16:47:32 -0700217 pr_debug("SET_DESCRI\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600218 break;
219 case USB_REQ_GET_CONFIGURATION:
matt mooney1a4b6f62011-05-19 16:47:32 -0700220 pr_debug("GET_CONFIG\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600221 break;
222 case USB_REQ_SET_CONFIGURATION:
matt mooney1a4b6f62011-05-19 16:47:32 -0700223 pr_debug("SET_CONFIG\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600224 break;
225 case USB_REQ_GET_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700226 pr_debug("GET_INTERF\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600227 break;
228 case USB_REQ_SET_INTERFACE:
matt mooney1a4b6f62011-05-19 16:47:32 -0700229 pr_debug("SET_INTERF\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600230 break;
231 case USB_REQ_SYNCH_FRAME:
matt mooney1a4b6f62011-05-19 16:47:32 -0700232 pr_debug("SYNC_FRAME\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600233 break;
234 default:
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400235 pr_debug("REQ(%02X)\n", cmd->bRequest);
matt mooney49aecef2011-05-06 03:47:54 -0700236 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600237 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600238 usbip_dump_request_type(cmd->bRequestType);
matt mooney1a4b6f62011-05-19 16:47:32 -0700239 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400240 pr_debug("CLASS\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700241 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
Akshay Joshi5ad7b852011-06-06 09:07:31 -0400242 pr_debug("VENDOR\n");
matt mooney1a4b6f62011-05-19 16:47:32 -0700243 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
244 pr_debug("RESERVED\n");
245 }
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600246}
247
248void usbip_dump_urb(struct urb *urb)
249{
250 struct device *dev;
251
252 if (!urb) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700253 pr_debug("urb: null pointer!!\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600254 return;
255 }
256
257 if (!urb->dev) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700258 pr_debug("urb->dev: null pointer!!\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600259 return;
260 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700261
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600262 dev = &urb->dev->dev;
263
264 dev_dbg(dev, " urb :%p\n", urb);
265 dev_dbg(dev, " dev :%p\n", urb->dev);
266
267 usbip_dump_usb_device(urb->dev);
268
269 dev_dbg(dev, " pipe :%08x ", urb->pipe);
270
271 usbip_dump_pipe(urb->pipe);
272
273 dev_dbg(dev, " status :%d\n", urb->status);
274 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
275 dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600276 dev_dbg(dev, " transfer_buffer_length:%d\n",
277 urb->transfer_buffer_length);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600278 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
279 dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
280
281 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
matt mooneyf9eacc92011-05-06 03:47:46 -0700282 usbip_dump_usb_ctrlrequest(
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600283 (struct usb_ctrlrequest *)urb->setup_packet);
284
285 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
286 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
287 dev_dbg(dev, " interval :%d\n", urb->interval);
288 dev_dbg(dev, " error_count :%d\n", urb->error_count);
289 dev_dbg(dev, " context :%p\n", urb->context);
290 dev_dbg(dev, " complete :%p\n", urb->complete);
291}
292EXPORT_SYMBOL_GPL(usbip_dump_urb);
293
294void usbip_dump_header(struct usbip_header *pdu)
295{
matt mooney1a4b6f62011-05-19 16:47:32 -0700296 pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
297 pdu->base.command,
298 pdu->base.seqnum,
299 pdu->base.devid,
300 pdu->base.direction,
301 pdu->base.ep);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600302
303 switch (pdu->base.command) {
304 case USBIP_CMD_SUBMIT:
matt mooney1a4b6f62011-05-19 16:47:32 -0700305 pr_debug("USBIP_CMD_SUBMIT: "
306 "x_flags %u x_len %u sf %u #p %d iv %d\n",
307 pdu->u.cmd_submit.transfer_flags,
308 pdu->u.cmd_submit.transfer_buffer_length,
309 pdu->u.cmd_submit.start_frame,
310 pdu->u.cmd_submit.number_of_packets,
311 pdu->u.cmd_submit.interval);
matt mooneyf9eacc92011-05-06 03:47:46 -0700312 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600313 case USBIP_CMD_UNLINK:
matt mooney1a4b6f62011-05-19 16:47:32 -0700314 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
315 pdu->u.cmd_unlink.seqnum);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600316 break;
317 case USBIP_RET_SUBMIT:
matt mooney1a4b6f62011-05-19 16:47:32 -0700318 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
319 pdu->u.ret_submit.status,
320 pdu->u.ret_submit.actual_length,
321 pdu->u.ret_submit.start_frame,
322 pdu->u.ret_submit.number_of_packets,
323 pdu->u.ret_submit.error_count);
324 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600325 case USBIP_RET_UNLINK:
matt mooney1a4b6f62011-05-19 16:47:32 -0700326 pr_debug("USBIP_RET_UNLINK: status %d\n",
327 pdu->u.ret_unlink.status);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600328 break;
329 default:
330 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700331 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700332 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600333 }
334}
335EXPORT_SYMBOL_GPL(usbip_dump_header);
336
Bart Westgeest5a08c522011-12-19 17:44:11 -0500337/* Receive data over TCP/IP. */
338int usbip_recv(struct socket *sock, void *buf, int size)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600339{
340 int result;
341 struct msghdr msg;
342 struct kvec iov;
343 int total = 0;
344
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600345 /* for blocks of if (usbip_dbg_flag_xmit) */
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600346 char *bp = buf;
347 int osize = size;
348
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600349 usbip_dbg_xmit("enter\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600350
351 if (!sock || !buf || !size) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700352 pr_err("invalid arg, sock %p buff %p size %d\n", sock, buf,
353 size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600354 return -EINVAL;
355 }
356
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600357 do {
358 sock->sk->sk_allocation = GFP_NOIO;
359 iov.iov_base = buf;
360 iov.iov_len = size;
361 msg.msg_name = NULL;
362 msg.msg_namelen = 0;
363 msg.msg_control = NULL;
364 msg.msg_controllen = 0;
365 msg.msg_namelen = 0;
Bart Westgeest5a08c522011-12-19 17:44:11 -0500366 msg.msg_flags = MSG_NOSIGNAL;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600367
Bart Westgeest5a08c522011-12-19 17:44:11 -0500368 result = kernel_recvmsg(sock, &msg, &iov, 1, size, MSG_WAITALL);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600369 if (result <= 0) {
Bart Westgeest5a08c522011-12-19 17:44:11 -0500370 pr_debug("receive sock %p buf %p size %u ret %d total %d\n",
371 sock, buf, size, result, total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600372 goto err;
373 }
374
375 size -= result;
376 buf += result;
377 total += result;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600378 } while (size > 0);
379
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600380 if (usbip_dbg_flag_xmit) {
Bart Westgeest5a08c522011-12-19 17:44:11 -0500381 if (!in_interrupt())
382 pr_debug("%-10s:", current->comm);
383 else
384 pr_debug("interrupt :");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600385
Bart Westgeest5a08c522011-12-19 17:44:11 -0500386 pr_debug("receiving....\n");
387 usbip_dump_buffer(bp, osize);
388 pr_debug("received, osize %d ret %d size %d total %d\n",
389 osize, result, size, total);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600390 }
391
392 return total;
393
394err:
395 return result;
396}
Bart Westgeest5a08c522011-12-19 17:44:11 -0500397EXPORT_SYMBOL_GPL(usbip_recv);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600398
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600399struct socket *sockfd_to_socket(unsigned int sockfd)
400{
401 struct socket *socket;
402 struct file *file;
403 struct inode *inode;
404
405 file = fget(sockfd);
406 if (!file) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700407 pr_err("invalid sockfd\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600408 return NULL;
409 }
410
411 inode = file->f_dentry->d_inode;
412
413 if (!inode || !S_ISSOCK(inode->i_mode))
414 return NULL;
415
416 socket = SOCKET_I(inode);
417
418 return socket;
419}
420EXPORT_SYMBOL_GPL(sockfd_to_socket);
421
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600422/* there may be more cases to tweak the flags. */
423static unsigned int tweak_transfer_flags(unsigned int flags)
424{
Alan Stern85bcb5e2010-04-30 16:35:37 -0400425 flags &= ~URB_NO_TRANSFER_DMA_MAP;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600426 return flags;
427}
428
429static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
matt mooneyf9eacc92011-05-06 03:47:46 -0700430 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600431{
432 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
433
434 /*
435 * Some members are not still implemented in usbip. I hope this issue
436 * will be discussed when usbip is ported to other operating systems.
437 */
438 if (pack) {
439 /* vhci_tx.c */
440 spdu->transfer_flags =
matt mooneyf9eacc92011-05-06 03:47:46 -0700441 tweak_transfer_flags(urb->transfer_flags);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600442 spdu->transfer_buffer_length = urb->transfer_buffer_length;
443 spdu->start_frame = urb->start_frame;
444 spdu->number_of_packets = urb->number_of_packets;
445 spdu->interval = urb->interval;
446 } else {
447 /* stub_rx.c */
448 urb->transfer_flags = spdu->transfer_flags;
449
450 urb->transfer_buffer_length = spdu->transfer_buffer_length;
451 urb->start_frame = spdu->start_frame;
452 urb->number_of_packets = spdu->number_of_packets;
453 urb->interval = spdu->interval;
454 }
455}
456
457static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
matt mooneyf9eacc92011-05-06 03:47:46 -0700458 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600459{
460 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
461
462 if (pack) {
463 /* stub_tx.c */
464
465 rpdu->status = urb->status;
466 rpdu->actual_length = urb->actual_length;
467 rpdu->start_frame = urb->start_frame;
Arjan Mels1325f852011-04-05 20:26:38 +0200468 rpdu->number_of_packets = urb->number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600469 rpdu->error_count = urb->error_count;
470 } else {
471 /* vhci_rx.c */
472
473 urb->status = rpdu->status;
474 urb->actual_length = rpdu->actual_length;
475 urb->start_frame = rpdu->start_frame;
Arjan Mels1325f852011-04-05 20:26:38 +0200476 urb->number_of_packets = rpdu->number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600477 urb->error_count = rpdu->error_count;
478 }
479}
480
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600481void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
matt mooneyf9eacc92011-05-06 03:47:46 -0700482 int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600483{
484 switch (cmd) {
485 case USBIP_CMD_SUBMIT:
486 usbip_pack_cmd_submit(pdu, urb, pack);
487 break;
488 case USBIP_RET_SUBMIT:
489 usbip_pack_ret_submit(pdu, urb, pack);
490 break;
491 default:
matt mooney49aecef2011-05-06 03:47:54 -0700492 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700493 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700494 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600495 }
496}
497EXPORT_SYMBOL_GPL(usbip_pack_pdu);
498
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600499static void correct_endian_basic(struct usbip_header_basic *base, int send)
500{
501 if (send) {
502 base->command = cpu_to_be32(base->command);
503 base->seqnum = cpu_to_be32(base->seqnum);
504 base->devid = cpu_to_be32(base->devid);
505 base->direction = cpu_to_be32(base->direction);
506 base->ep = cpu_to_be32(base->ep);
507 } else {
508 base->command = be32_to_cpu(base->command);
509 base->seqnum = be32_to_cpu(base->seqnum);
510 base->devid = be32_to_cpu(base->devid);
511 base->direction = be32_to_cpu(base->direction);
512 base->ep = be32_to_cpu(base->ep);
513 }
514}
515
516static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700517 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600518{
519 if (send) {
520 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
521
522 cpu_to_be32s(&pdu->transfer_buffer_length);
523 cpu_to_be32s(&pdu->start_frame);
524 cpu_to_be32s(&pdu->number_of_packets);
525 cpu_to_be32s(&pdu->interval);
526 } else {
527 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
528
529 be32_to_cpus(&pdu->transfer_buffer_length);
530 be32_to_cpus(&pdu->start_frame);
531 be32_to_cpus(&pdu->number_of_packets);
532 be32_to_cpus(&pdu->interval);
533 }
534}
535
536static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700537 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600538{
539 if (send) {
540 cpu_to_be32s(&pdu->status);
541 cpu_to_be32s(&pdu->actual_length);
542 cpu_to_be32s(&pdu->start_frame);
Arjan Mels1325f852011-04-05 20:26:38 +0200543 cpu_to_be32s(&pdu->number_of_packets);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600544 cpu_to_be32s(&pdu->error_count);
545 } else {
546 be32_to_cpus(&pdu->status);
547 be32_to_cpus(&pdu->actual_length);
548 be32_to_cpus(&pdu->start_frame);
David Changcacd18a2011-05-12 18:31:11 +0800549 be32_to_cpus(&pdu->number_of_packets);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600550 be32_to_cpus(&pdu->error_count);
551 }
552}
553
554static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700555 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600556{
557 if (send)
558 pdu->seqnum = cpu_to_be32(pdu->seqnum);
559 else
560 pdu->seqnum = be32_to_cpu(pdu->seqnum);
561}
562
563static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
matt mooneyf9eacc92011-05-06 03:47:46 -0700564 int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600565{
566 if (send)
567 cpu_to_be32s(&pdu->status);
568 else
569 be32_to_cpus(&pdu->status);
570}
571
572void usbip_header_correct_endian(struct usbip_header *pdu, int send)
573{
574 __u32 cmd = 0;
575
576 if (send)
577 cmd = pdu->base.command;
578
579 correct_endian_basic(&pdu->base, send);
580
581 if (!send)
582 cmd = pdu->base.command;
583
584 switch (cmd) {
585 case USBIP_CMD_SUBMIT:
586 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
587 break;
588 case USBIP_RET_SUBMIT:
589 correct_endian_ret_submit(&pdu->u.ret_submit, send);
590 break;
591 case USBIP_CMD_UNLINK:
592 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
593 break;
594 case USBIP_RET_UNLINK:
595 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
596 break;
597 default:
matt mooney49aecef2011-05-06 03:47:54 -0700598 /* NOT REACHED */
matt mooney1a4b6f62011-05-19 16:47:32 -0700599 pr_err("unknown command\n");
matt mooney49aecef2011-05-06 03:47:54 -0700600 break;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600601 }
602}
603EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
604
matt mooney2282e1f2011-05-19 21:37:01 -0700605static void usbip_iso_packet_correct_endian(
matt mooney87352762011-05-19 21:36:56 -0700606 struct usbip_iso_packet_descriptor *iso, int send)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600607{
608 /* does not need all members. but copy all simply. */
609 if (send) {
610 iso->offset = cpu_to_be32(iso->offset);
611 iso->length = cpu_to_be32(iso->length);
612 iso->status = cpu_to_be32(iso->status);
613 iso->actual_length = cpu_to_be32(iso->actual_length);
614 } else {
615 iso->offset = be32_to_cpu(iso->offset);
616 iso->length = be32_to_cpu(iso->length);
617 iso->status = be32_to_cpu(iso->status);
618 iso->actual_length = be32_to_cpu(iso->actual_length);
619 }
620}
621
622static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
matt mooneyf9eacc92011-05-06 03:47:46 -0700623 struct usb_iso_packet_descriptor *uiso, int pack)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600624{
625 if (pack) {
626 iso->offset = uiso->offset;
627 iso->length = uiso->length;
628 iso->status = uiso->status;
629 iso->actual_length = uiso->actual_length;
630 } else {
631 uiso->offset = iso->offset;
632 uiso->length = iso->length;
633 uiso->status = iso->status;
634 uiso->actual_length = iso->actual_length;
635 }
636}
637
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600638/* must free buffer */
639void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
640{
641 void *buff;
642 struct usbip_iso_packet_descriptor *iso;
643 int np = urb->number_of_packets;
644 ssize_t size = np * sizeof(*iso);
645 int i;
646
647 buff = kzalloc(size, GFP_KERNEL);
648 if (!buff)
649 return NULL;
650
651 for (i = 0; i < np; i++) {
652 iso = buff + (i * sizeof(*iso));
653
654 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
matt mooney2282e1f2011-05-19 21:37:01 -0700655 usbip_iso_packet_correct_endian(iso, 1);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600656 }
657
658 *bufflen = size;
659
660 return buff;
661}
662EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
663
664/* some members of urb must be substituted before. */
665int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
666{
667 void *buff;
668 struct usbip_iso_packet_descriptor *iso;
669 int np = urb->number_of_packets;
670 int size = np * sizeof(*iso);
671 int i;
672 int ret;
Arjan Mels28276a22011-04-05 20:26:59 +0200673 int total_length = 0;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600674
675 if (!usb_pipeisoc(urb->pipe))
676 return 0;
677
678 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
679 if (np == 0) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700680 /* pr_info("iso np == 0\n"); */
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600681 /* usbip_dump_urb(urb); */
682 return 0;
683 }
684
685 buff = kzalloc(size, GFP_KERNEL);
686 if (!buff)
687 return -ENOMEM;
688
Bart Westgeest5a08c522011-12-19 17:44:11 -0500689 ret = usbip_recv(ud->tcp_socket, buff, size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600690 if (ret != size) {
691 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
692 ret);
693 kfree(buff);
694
695 if (ud->side == USBIP_STUB)
696 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
697 else
698 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
699
700 return -EPIPE;
701 }
702
703 for (i = 0; i < np; i++) {
704 iso = buff + (i * sizeof(*iso));
705
matt mooney2282e1f2011-05-19 21:37:01 -0700706 usbip_iso_packet_correct_endian(iso, 0);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600707 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
Arjan Mels28276a22011-04-05 20:26:59 +0200708 total_length += urb->iso_frame_desc[i].actual_length;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600709 }
710
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600711 kfree(buff);
712
Arjan Mels28276a22011-04-05 20:26:59 +0200713 if (total_length != urb->actual_length) {
714 dev_err(&urb->dev->dev,
matt mooneyf9eacc92011-05-06 03:47:46 -0700715 "total length of iso packets %d not equal to actual "
716 "length of buffer %d\n",
717 total_length, urb->actual_length);
Arjan Mels28276a22011-04-05 20:26:59 +0200718
719 if (ud->side == USBIP_STUB)
720 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
721 else
722 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
723
724 return -EPIPE;
725 }
726
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600727 return ret;
728}
729EXPORT_SYMBOL_GPL(usbip_recv_iso);
730
Arjan Mels28276a22011-04-05 20:26:59 +0200731/*
732 * This functions restores the padding which was removed for optimizing
733 * the bandwidth during transfer over tcp/ip
734 *
735 * buffer and iso packets need to be stored and be in propeper endian in urb
736 * before calling this function
737 */
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500738void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
Arjan Mels28276a22011-04-05 20:26:59 +0200739{
740 int np = urb->number_of_packets;
741 int i;
Arjan Mels28276a22011-04-05 20:26:59 +0200742 int actualoffset = urb->actual_length;
743
744 if (!usb_pipeisoc(urb->pipe))
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500745 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200746
747 /* if no packets or length of data is 0, then nothing to unpack */
748 if (np == 0 || urb->actual_length == 0)
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500749 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200750
751 /*
752 * if actual_length is transfer_buffer_length then no padding is
753 * present.
754 */
755 if (urb->actual_length == urb->transfer_buffer_length)
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500756 return;
Arjan Mels28276a22011-04-05 20:26:59 +0200757
758 /*
759 * loop over all packets from last to first (to prevent overwritting
760 * memory when padding) and move them into the proper place
761 */
762 for (i = np-1; i > 0; i--) {
763 actualoffset -= urb->iso_frame_desc[i].actual_length;
764 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
matt mooneyf9eacc92011-05-06 03:47:46 -0700765 urb->transfer_buffer + actualoffset,
766 urb->iso_frame_desc[i].actual_length);
Arjan Mels28276a22011-04-05 20:26:59 +0200767 }
Arjan Mels28276a22011-04-05 20:26:59 +0200768}
769EXPORT_SYMBOL_GPL(usbip_pad_iso);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600770
771/* some members of urb must be substituted before. */
772int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
773{
774 int ret;
775 int size;
776
777 if (ud->side == USBIP_STUB) {
778 /* stub_rx.c */
779 /* the direction of urb must be OUT. */
780 if (usb_pipein(urb->pipe))
781 return 0;
782
783 size = urb->transfer_buffer_length;
784 } else {
785 /* vhci_rx.c */
786 /* the direction of urb must be IN. */
787 if (usb_pipeout(urb->pipe))
788 return 0;
789
790 size = urb->actual_length;
791 }
792
793 /* no need to recv xbuff */
794 if (!(size > 0))
795 return 0;
796
Bart Westgeest5a08c522011-12-19 17:44:11 -0500797 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600798 if (ret != size) {
799 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
800 if (ud->side == USBIP_STUB) {
801 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
802 } else {
803 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
804 return -EPIPE;
805 }
806 }
807
808 return ret;
809}
810EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
811
matt mooney3028d0a2011-05-19 21:37:05 -0700812static int __init usbip_core_init(void)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600813{
matt mooney1a4b6f62011-05-19 16:47:32 -0700814 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600815 return 0;
816}
817
matt mooney3028d0a2011-05-19 21:37:05 -0700818static void __exit usbip_core_exit(void)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600819{
820 return;
821}
822
matt mooney3028d0a2011-05-19 21:37:05 -0700823module_init(usbip_core_init);
824module_exit(usbip_core_exit);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600825
826MODULE_AUTHOR(DRIVER_AUTHOR);
827MODULE_DESCRIPTION(DRIVER_DESC);
828MODULE_LICENSE("GPL");
matt mooney6973c6f2011-05-11 01:54:14 -0700829MODULE_VERSION(USBIP_VERSION);