]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/patches/suse-2.6.27.39/patches.drivers/0012-Staging-USB-IP-add-client-driver.patch
Add a patch to fix Intel E100 wake-on-lan problems.
[people/pmueller/ipfire-2.x.git] / src / patches / suse-2.6.27.39 / patches.drivers / 0012-Staging-USB-IP-add-client-driver.patch
1 From 04679b3489e048cd5dae79e050a3afed8e4e42b6 Mon Sep 17 00:00:00 2001
2 From: Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>
3 Date: Wed, 9 Jul 2008 14:56:51 -0600
4 Subject: [PATCH 12/23] Staging: USB/IP: add client driver
5 Patch-mainline: 2.6.28
6
7 This adds the USB IP client driver
8
9 Brian Merrell cleaned up a lot of this code and submitted it for
10 inclusion. Greg also did a lot of cleanup.
11
12 Signed-off-by: Brian G. Merrell <bgmerrell@novell.com>
13 Cc: Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>
14 Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
15 ---
16 drivers/staging/usbip/Kconfig | 11 +
17 drivers/staging/usbip/Makefile | 3 +
18 drivers/staging/usbip/vhci.h | 142 ++++
19 drivers/staging/usbip/vhci_hcd.c | 1275 ++++++++++++++++++++++++++++++++++++
20 drivers/staging/usbip/vhci_rx.c | 251 +++++++
21 drivers/staging/usbip/vhci_sysfs.c | 250 +++++++
22 drivers/staging/usbip/vhci_tx.c | 239 +++++++
23 7 files changed, 2171 insertions(+), 0 deletions(-)
24 create mode 100644 drivers/staging/usbip/vhci.h
25 create mode 100644 drivers/staging/usbip/vhci_hcd.c
26 create mode 100644 drivers/staging/usbip/vhci_rx.c
27 create mode 100644 drivers/staging/usbip/vhci_sysfs.c
28 create mode 100644 drivers/staging/usbip/vhci_tx.c
29
30 diff --git a/drivers/staging/usbip/Kconfig b/drivers/staging/usbip/Kconfig
31 index 37efb5e..c4d68e1 100644
32 --- a/drivers/staging/usbip/Kconfig
33 +++ b/drivers/staging/usbip/Kconfig
34 @@ -12,3 +12,14 @@ config USB_IP_COMMON
35 module will be called usbip_common_mod.
36
37 If unsure, say N.
38 +
39 +config USB_IP_VHCI_HCD
40 + tristate "USB IP client driver"
41 + depends on USB_IP_COMMON
42 + default N
43 + ---help---
44 + This enables the USB IP host controller driver which will
45 + run on the client machine.
46 +
47 + To compile this driver as a module, choose M here: the
48 + module will be called vhci_hcd.
49 diff --git a/drivers/staging/usbip/Makefile b/drivers/staging/usbip/Makefile
50 index ce925ca..6ef4c39 100644
51 --- a/drivers/staging/usbip/Makefile
52 +++ b/drivers/staging/usbip/Makefile
53 @@ -1,6 +1,9 @@
54 obj-$(CONFIG_USB_IP_COMMON) += usbip_common_mod.o
55 usbip_common_mod-objs := usbip_common.o usbip_event.o
56
57 +obj-$(CONFIG_USB_IP_VHCI_HCD) += vhci-hcd.o
58 +vhci-hcd-objs := vhci_sysfs.o vhci_tx.o vhci_rx.o vhci_hcd.o
59 +
60 ifeq ($(CONFIG_USB_DEBUG),y)
61 EXTRA_CFLAGS += -DDEBUG
62 endif
63 diff --git a/drivers/staging/usbip/vhci.h b/drivers/staging/usbip/vhci.h
64 new file mode 100644
65 index 0000000..5e37517
66 --- /dev/null
67 +++ b/drivers/staging/usbip/vhci.h
68 @@ -0,0 +1,142 @@
69 +/*
70 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
71 + *
72 + * This is free software; you can redistribute it and/or modify
73 + * it under the terms of the GNU General Public License as published by
74 + * the Free Software Foundation; either version 2 of the License, or
75 + * (at your option) any later version.
76 + *
77 + * This is distributed in the hope that it will be useful,
78 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
79 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80 + * GNU General Public License for more details.
81 + *
82 + * You should have received a copy of the GNU General Public License
83 + * along with this program; if not, write to the Free Software
84 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
85 + * USA.
86 + */
87 +
88 +#include <linux/platform_device.h>
89 +#include "../../usb/core/hcd.h"
90 +
91 +
92 +struct vhci_device {
93 + struct usb_device *udev;
94 +
95 + /*
96 + * devid specifies a remote usb device uniquely instead
97 + * of combination of busnum and devnum.
98 + */
99 + __u32 devid;
100 +
101 + /* speed of a remote device */
102 + enum usb_device_speed speed;
103 +
104 + /* vhci root-hub port to which this device is attached */
105 + __u32 rhport;
106 +
107 + struct usbip_device ud;
108 +
109 +
110 + /* lock for the below link lists */
111 + spinlock_t priv_lock;
112 +
113 + /* vhci_priv is linked to one of them. */
114 + struct list_head priv_tx;
115 + struct list_head priv_rx;
116 +
117 + /* vhci_unlink is linked to one of them */
118 + struct list_head unlink_tx;
119 + struct list_head unlink_rx;
120 +
121 + /* vhci_tx thread sleeps for this queue */
122 + wait_queue_head_t waitq_tx;
123 +};
124 +
125 +
126 +/* urb->hcpriv, use container_of() */
127 +struct vhci_priv {
128 + unsigned long seqnum;
129 + struct list_head list;
130 +
131 + struct vhci_device *vdev;
132 + struct urb *urb;
133 +};
134 +
135 +
136 +struct vhci_unlink {
137 + /* seqnum of this request */
138 + unsigned long seqnum;
139 +
140 + struct list_head list;
141 +
142 + /* seqnum of the unlink target */
143 + unsigned long unlink_seqnum;
144 +};
145 +
146 +/*
147 + * The number of ports is less than 16 ?
148 + * USB_MAXCHILDREN is statically defined to 16 in usb.h. Its maximum value
149 + * would be 31 because the event_bits[1] of struct usb_hub is defined as
150 + * unsigned long in hub.h
151 + */
152 +#define VHCI_NPORTS 8
153 +
154 +/* for usb_bus.hcpriv */
155 +struct vhci_hcd {
156 + spinlock_t lock;
157 +
158 + u32 port_status[VHCI_NPORTS];
159 +
160 + unsigned resuming:1;
161 + unsigned long re_timeout;
162 +
163 + atomic_t seqnum;
164 +
165 + /*
166 + * NOTE:
167 + * wIndex shows the port number and begins from 1.
168 + * But, the index of this array begins from 0.
169 + */
170 + struct vhci_device vdev[VHCI_NPORTS];
171 +
172 + /* vhci_device which has not been assiged its address yet */
173 + int pending_port;
174 +};
175 +
176 +
177 +extern struct vhci_hcd *the_controller;
178 +extern struct attribute_group dev_attr_group;
179 +
180 +
181 +/*-------------------------------------------------------------------------*/
182 +/* prototype declaration */
183 +
184 +/* vhci_hcd.c */
185 +void rh_port_connect(int rhport, enum usb_device_speed speed);
186 +void rh_port_disconnect(int rhport);
187 +void vhci_rx_loop(struct usbip_task *ut);
188 +void vhci_tx_loop(struct usbip_task *ut);
189 +
190 +#define hardware (&the_controller->pdev.dev)
191 +
192 +static inline struct vhci_device *port_to_vdev(__u32 port)
193 +{
194 + return &the_controller->vdev[port];
195 +}
196 +
197 +static inline struct vhci_hcd *hcd_to_vhci(struct usb_hcd *hcd)
198 +{
199 + return (struct vhci_hcd *) (hcd->hcd_priv);
200 +}
201 +
202 +static inline struct usb_hcd *vhci_to_hcd(struct vhci_hcd *vhci)
203 +{
204 + return container_of((void *) vhci, struct usb_hcd, hcd_priv);
205 +}
206 +
207 +static inline struct device *vhci_dev(struct vhci_hcd *vhci)
208 +{
209 + return vhci_to_hcd(vhci)->self.controller;
210 +}
211 diff --git a/drivers/staging/usbip/vhci_hcd.c b/drivers/staging/usbip/vhci_hcd.c
212 new file mode 100644
213 index 0000000..5b5a2e3
214 --- /dev/null
215 +++ b/drivers/staging/usbip/vhci_hcd.c
216 @@ -0,0 +1,1275 @@
217 +/*
218 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
219 + *
220 + * This is free software; you can redistribute it and/or modify
221 + * it under the terms of the GNU General Public License as published by
222 + * the Free Software Foundation; either version 2 of the License, or
223 + * (at your option) any later version.
224 + *
225 + * This is distributed in the hope that it will be useful,
226 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
227 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
228 + * GNU General Public License for more details.
229 + *
230 + * You should have received a copy of the GNU General Public License
231 + * along with this program; if not, write to the Free Software
232 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
233 + * USA.
234 + */
235 +
236 +
237 +#include "usbip_common.h"
238 +#include "vhci.h"
239 +
240 +#define DRIVER_VERSION "1.0"
241 +#define DRIVER_AUTHOR "Takahiro Hirofuchi"
242 +#define DRIVER_DESC "Virtual Host Controller Interface Driver for USB/IP"
243 +#define DRIVER_LICENCE "GPL"
244 +MODULE_AUTHOR(DRIVER_AUTHOR);
245 +MODULE_DESCRIPTION(DRIVER_DESC);
246 +MODULE_LICENSE(DRIVER_LICENCE);
247 +
248 +
249 +
250 +/*
251 + * TODO
252 + * - update root hub emulation
253 + * - move the emulation code to userland ?
254 + * porting to other operating systems
255 + * minimize kernel code
256 + * - add suspend/resume code
257 + * - clean up everything
258 + */
259 +
260 +
261 +/* See usb gadget dummy hcd */
262 +
263 +
264 +static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
265 +static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
266 + u16 wIndex, char *buff, u16 wLength);
267 +static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
268 + gfp_t mem_flags);
269 +static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
270 +static int vhci_start(struct usb_hcd *vhci_hcd);
271 +static void vhci_stop(struct usb_hcd *hcd);
272 +static int vhci_get_frame_number(struct usb_hcd *hcd);
273 +
274 +static const char driver_name[] = "vhci_hcd";
275 +static const char driver_desc[] = "USB/IP Virtual Host Contoroller";
276 +
277 +struct vhci_hcd *the_controller;
278 +
279 +static const char *bit_desc[] = {
280 + "CONNECTION", /*0*/
281 + "ENABLE", /*1*/
282 + "SUSPEND", /*2*/
283 + "OVER_CURRENT", /*3*/
284 + "RESET", /*4*/
285 + "R5", /*5*/
286 + "R6", /*6*/
287 + "R7", /*7*/
288 + "POWER", /*8*/
289 + "LOWSPEED", /*9*/
290 + "HIGHSPEED", /*10*/
291 + "PORT_TEST", /*11*/
292 + "INDICATOR", /*12*/
293 + "R13", /*13*/
294 + "R14", /*14*/
295 + "R15", /*15*/
296 + "C_CONNECTION", /*16*/
297 + "C_ENABLE", /*17*/
298 + "C_SUSPEND", /*18*/
299 + "C_OVER_CURRENT", /*19*/
300 + "C_RESET", /*20*/
301 + "R21", /*21*/
302 + "R22", /*22*/
303 + "R23", /*23*/
304 + "R24", /*24*/
305 + "R25", /*25*/
306 + "R26", /*26*/
307 + "R27", /*27*/
308 + "R28", /*28*/
309 + "R29", /*29*/
310 + "R30", /*30*/
311 + "R31", /*31*/
312 +};
313 +
314 +
315 +static void dump_port_status(u32 status)
316 +{
317 + int i = 0;
318 +
319 + printk(KERN_DEBUG "status %08x:", status);
320 + for (i = 0; i < 32; i++) {
321 + if (status & (1 << i))
322 + printk(" %s", bit_desc[i]);
323 + }
324 +
325 + printk("\n");
326 +}
327 +
328 +
329 +
330 +void rh_port_connect(int rhport, enum usb_device_speed speed)
331 +{
332 + unsigned long flags;
333 +
334 + dbg_vhci_rh("rh_port_connect %d\n", rhport);
335 +
336 + spin_lock_irqsave(&the_controller->lock, flags);
337 +
338 + the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
339 + | (1 << USB_PORT_FEAT_C_CONNECTION);
340 +
341 + switch (speed) {
342 + case USB_SPEED_HIGH:
343 + the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED;
344 + break;
345 + case USB_SPEED_LOW:
346 + the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED;
347 + break;
348 + default:
349 + break;
350 + }
351 +
352 + /* spin_lock(&the_controller->vdev[rhport].ud.lock);
353 + * the_controller->vdev[rhport].ud.status = VDEV_CONNECT;
354 + * spin_unlock(&the_controller->vdev[rhport].ud.lock); */
355 +
356 + the_controller->pending_port = rhport;
357 +
358 + spin_unlock_irqrestore(&the_controller->lock, flags);
359 +
360 + usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
361 +}
362 +
363 +void rh_port_disconnect(int rhport)
364 +{
365 + unsigned long flags;
366 +
367 + dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
368 +
369 + spin_lock_irqsave(&the_controller->lock, flags);
370 + /* stop_activity(dum, driver); */
371 + the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION;
372 + the_controller->port_status[rhport] |=
373 + (1 << USB_PORT_FEAT_C_CONNECTION);
374 +
375 +
376 + /* not yet complete the disconnection
377 + * spin_lock(&vdev->ud.lock);
378 + * vdev->ud.status = VHC_ST_DISCONNECT;
379 + * spin_unlock(&vdev->ud.lock); */
380 +
381 + spin_unlock_irqrestore(&the_controller->lock, flags);
382 +}
383 +
384 +
385 +
386 +/*----------------------------------------------------------------------*/
387 +
388 +#define PORT_C_MASK \
389 + ((USB_PORT_STAT_C_CONNECTION \
390 + | USB_PORT_STAT_C_ENABLE \
391 + | USB_PORT_STAT_C_SUSPEND \
392 + | USB_PORT_STAT_C_OVERCURRENT \
393 + | USB_PORT_STAT_C_RESET) << 16)
394 +
395 +/*
396 + * This function is almostly the same as dummy_hcd.c:dummy_hub_status() without
397 + * suspend/resume support. But, it is modified to provide multiple ports.
398 + *
399 + * @buf: a bitmap to show which port status has been changed.
400 + * bit 0: reserved or used for another purpose?
401 + * bit 1: the status of port 0 has been changed.
402 + * bit 2: the status of port 1 has been changed.
403 + * ...
404 + * bit 7: the status of port 6 has been changed.
405 + * bit 8: the status of port 7 has been changed.
406 + * ...
407 + * bit 15: the status of port 14 has been changed.
408 + *
409 + * So, the maximum number of ports is 31 ( port 0 to port 30) ?
410 + *
411 + * The return value is the actual transfered length in byte. If nothing has
412 + * been changed, return 0. In the case that the number of ports is less than or
413 + * equal to 6 (VHCI_NPORTS==7), return 1.
414 + *
415 + */
416 +static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
417 +{
418 + struct vhci_hcd *vhci;
419 + unsigned long flags;
420 + int retval = 0;
421 +
422 + /* the enough buffer is allocated according to USB_MAXCHILDREN */
423 + unsigned long *event_bits = (unsigned long *) buf;
424 + int rhport;
425 + int changed = 0;
426 +
427 +
428 + *event_bits = 0;
429 +
430 + vhci = hcd_to_vhci(hcd);
431 +
432 + spin_lock_irqsave(&vhci->lock, flags);
433 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
434 + dbg_vhci_rh("hw accessible flag in on?\n");
435 + goto done;
436 + }
437 +
438 + /* check pseudo status register for each port */
439 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
440 + if ((vhci->port_status[rhport] & PORT_C_MASK)) {
441 + /* The status of a port has been changed, */
442 + dbg_vhci_rh("port %d is changed\n", rhport);
443 +
444 + *event_bits |= 1 << (rhport + 1);
445 + changed = 1;
446 + }
447 + }
448 +
449 + uinfo("changed %d\n", changed);
450 +
451 + if (hcd->state == HC_STATE_SUSPENDED)
452 + usb_hcd_resume_root_hub(hcd);
453 +
454 + if (changed)
455 + retval = 1 + (VHCI_NPORTS / 8);
456 + else
457 + retval = 0;
458 +
459 +done:
460 + spin_unlock_irqrestore(&vhci->lock, flags);
461 + return retval;
462 +}
463 +
464 +/* See hub_configure in hub.c */
465 +static inline void hub_descriptor(struct usb_hub_descriptor *desc)
466 +{
467 + memset(desc, 0, sizeof(*desc));
468 + desc->bDescriptorType = 0x29;
469 + desc->bDescLength = 9;
470 + desc->wHubCharacteristics = (__force __u16)
471 + (__constant_cpu_to_le16(0x0001));
472 + desc->bNbrPorts = VHCI_NPORTS;
473 + desc->bitmap[0] = 0xff;
474 + desc->bitmap[1] = 0xff;
475 +}
476 +
477 +static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
478 + u16 wIndex, char *buf, u16 wLength)
479 +{
480 + struct vhci_hcd *dum;
481 + int retval = 0;
482 + unsigned long flags;
483 + int rhport;
484 +
485 + u32 prev_port_status[VHCI_NPORTS];
486 +
487 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
488 + return -ETIMEDOUT;
489 +
490 + /*
491 + * NOTE:
492 + * wIndex shows the port number and begins from 1.
493 + */
494 + dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
495 + wIndex);
496 + if (wIndex > VHCI_NPORTS)
497 + printk(KERN_ERR "%s: invalid port number %d\n", __func__, wIndex);
498 + rhport = ((__u8)(wIndex & 0x00ff)) - 1;
499 +
500 + dum = hcd_to_vhci(hcd);
501 +
502 + spin_lock_irqsave(&dum->lock, flags);
503 +
504 + /* store old status and compare now and old later */
505 + if (dbg_flag_vhci_rh) {
506 + int i = 0;
507 + for (i = 0; i < VHCI_NPORTS; i++)
508 + prev_port_status[i] = dum->port_status[i];
509 + }
510 +
511 + switch (typeReq) {
512 + case ClearHubFeature:
513 + dbg_vhci_rh(" ClearHubFeature\n");
514 + break;
515 + case ClearPortFeature:
516 + switch (wValue) {
517 + case USB_PORT_FEAT_SUSPEND:
518 + if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
519 + /* 20msec signaling */
520 + dum->resuming = 1;
521 + dum->re_timeout =
522 + jiffies + msecs_to_jiffies(20);
523 + }
524 + break;
525 + case USB_PORT_FEAT_POWER:
526 + dbg_vhci_rh(" ClearPortFeature: USB_PORT_FEAT_POWER\n");
527 + dum->port_status[rhport] = 0;
528 + /* dum->address = 0; */
529 + /* dum->hdev = 0; */
530 + dum->resuming = 0;
531 + break;
532 + case USB_PORT_FEAT_C_RESET:
533 + dbg_vhci_rh(" ClearPortFeature: "
534 + "USB_PORT_FEAT_C_RESET\n");
535 + switch (dum->vdev[rhport].speed) {
536 + case USB_SPEED_HIGH:
537 + dum->port_status[rhport] |=
538 + USB_PORT_STAT_HIGH_SPEED;
539 + break;
540 + case USB_SPEED_LOW:
541 + dum->port_status[rhport] |=
542 + USB_PORT_STAT_LOW_SPEED;
543 + break;
544 + default:
545 + break;
546 + }
547 + default:
548 + dbg_vhci_rh(" ClearPortFeature: default %x\n", wValue);
549 + dum->port_status[rhport] &= ~(1 << wValue);
550 + }
551 + break;
552 + case GetHubDescriptor:
553 + dbg_vhci_rh(" GetHubDescriptor\n");
554 + hub_descriptor((struct usb_hub_descriptor *) buf);
555 + break;
556 + case GetHubStatus:
557 + dbg_vhci_rh(" GetHubStatus\n");
558 + *(__le32 *) buf = __constant_cpu_to_le32(0);
559 + break;
560 + case GetPortStatus:
561 + dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
562 + if (wIndex > VHCI_NPORTS || wIndex < 1) {
563 + printk(KERN_ERR "%s: invalid port number %d\n",
564 + __func__, wIndex);
565 + retval = -EPIPE;
566 + }
567 +
568 + /* we do no care of resume. */
569 +
570 + /* whoever resets or resumes must GetPortStatus to
571 + * complete it!!
572 + * */
573 + if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
574 + printk(KERN_ERR "%s: not yet\n", __func__);
575 + dum->port_status[rhport] |=
576 + (1 << USB_PORT_FEAT_C_SUSPEND);
577 + dum->port_status[rhport] &=
578 + ~(1 << USB_PORT_FEAT_SUSPEND);
579 + dum->resuming = 0;
580 + dum->re_timeout = 0;
581 + /* if (dum->driver && dum->driver->resume) {
582 + * spin_unlock (&dum->lock);
583 + * dum->driver->resume (&dum->gadget);
584 + * spin_lock (&dum->lock);
585 + * } */
586 + }
587 +
588 + if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
589 + 0 && time_after(jiffies, dum->re_timeout)) {
590 + dum->port_status[rhport] |=
591 + (1 << USB_PORT_FEAT_C_RESET);
592 + dum->port_status[rhport] &=
593 + ~(1 << USB_PORT_FEAT_RESET);
594 + dum->re_timeout = 0;
595 +
596 + if (dum->vdev[rhport].ud.status ==
597 + VDEV_ST_NOTASSIGNED) {
598 + dbg_vhci_rh(" enable rhport %d (status %u)\n",
599 + rhport,
600 + dum->vdev[rhport].ud.status);
601 + dum->port_status[rhport] |=
602 + USB_PORT_STAT_ENABLE;
603 + }
604 +#if 0
605 + if (dum->driver) {
606 +
607 + dum->port_status[rhport] |=
608 + USB_PORT_STAT_ENABLE;
609 + /* give it the best speed we agree on */
610 + dum->gadget.speed = dum->driver->speed;
611 + dum->gadget.ep0->maxpacket = 64;
612 + switch (dum->gadget.speed) {
613 + case USB_SPEED_HIGH:
614 + dum->port_status[rhport] |=
615 + USB_PORT_STAT_HIGH_SPEED;
616 + break;
617 + case USB_SPEED_LOW:
618 + dum->gadget.ep0->maxpacket = 8;
619 + dum->port_status[rhport] |=
620 + USB_PORT_STAT_LOW_SPEED;
621 + break;
622 + default:
623 + dum->gadget.speed = USB_SPEED_FULL;
624 + break;
625 + }
626 + }
627 +#endif
628 +
629 + }
630 + ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
631 + ((u16 *) buf)[1] =
632 + cpu_to_le16(dum->port_status[rhport] >> 16);
633 +
634 + dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
635 + ((u16 *)buf)[1]);
636 + break;
637 + case SetHubFeature:
638 + dbg_vhci_rh(" SetHubFeature\n");
639 + retval = -EPIPE;
640 + break;
641 + case SetPortFeature:
642 + switch (wValue) {
643 + case USB_PORT_FEAT_SUSPEND:
644 + dbg_vhci_rh(" SetPortFeature: "
645 + "USB_PORT_FEAT_SUSPEND\n");
646 + printk(KERN_ERR "%s: not yet\n", __func__);
647 +#if 0
648 + dum->port_status[rhport] |=
649 + (1 << USB_PORT_FEAT_SUSPEND);
650 + if (dum->driver->suspend) {
651 + spin_unlock(&dum->lock);
652 + dum->driver->suspend(&dum->gadget);
653 + spin_lock(&dum->lock);
654 + }
655 +#endif
656 + break;
657 + case USB_PORT_FEAT_RESET:
658 + dbg_vhci_rh(" SetPortFeature: USB_PORT_FEAT_RESET\n");
659 + /* if it's already running, disconnect first */
660 + if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
661 + dum->port_status[rhport] &=
662 + ~(USB_PORT_STAT_ENABLE |
663 + USB_PORT_STAT_LOW_SPEED |
664 + USB_PORT_STAT_HIGH_SPEED);
665 +#if 0
666 + if (dum->driver) {
667 + dev_dbg(hardware, "disconnect\n");
668 + stop_activity(dum, dum->driver);
669 + }
670 +#endif
671 +
672 + /* FIXME test that code path! */
673 + }
674 + /* 50msec reset signaling */
675 + dum->re_timeout = jiffies + msecs_to_jiffies(50);
676 +
677 + /* FALLTHROUGH */
678 + default:
679 + dbg_vhci_rh(" SetPortFeature: default %d\n", wValue);
680 + dum->port_status[rhport] |= (1 << wValue);
681 + }
682 + break;
683 +
684 + default:
685 + printk(KERN_ERR "%s: default: no such request\n", __func__);
686 + /* dev_dbg (hardware,
687 + * "hub control req%04x v%04x i%04x l%d\n",
688 + * typeReq, wValue, wIndex, wLength); */
689 +
690 + /* "protocol stall" on error */
691 + retval = -EPIPE;
692 + }
693 +
694 + if (dbg_flag_vhci_rh) {
695 + printk(KERN_DEBUG "port %d\n", rhport);
696 + dump_port_status(prev_port_status[rhport]);
697 + dump_port_status(dum->port_status[rhport]);
698 + }
699 + dbg_vhci_rh(" bye\n");
700 +
701 + spin_unlock_irqrestore(&dum->lock, flags);
702 +
703 + return retval;
704 +}
705 +
706 +
707 +
708 +/*----------------------------------------------------------------------*/
709 +
710 +static struct vhci_device *get_vdev(struct usb_device *udev)
711 +{
712 + int i;
713 +
714 + if (!udev)
715 + return NULL;
716 +
717 + for (i = 0; i < VHCI_NPORTS; i++)
718 + if (the_controller->vdev[i].udev == udev)
719 + return port_to_vdev(i);
720 +
721 + return NULL;
722 +}
723 +
724 +static void vhci_tx_urb(struct urb *urb)
725 +{
726 + struct vhci_device *vdev = get_vdev(urb->dev);
727 + struct vhci_priv *priv;
728 + unsigned long flag;
729 +
730 + if (!vdev) {
731 + err("could not get virtual device");
732 + /* BUG(); */
733 + return;
734 + }
735 +
736 + spin_lock_irqsave(&vdev->priv_lock, flag);
737 +
738 + priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
739 + if (!priv) {
740 + dev_err(&urb->dev->dev, "malloc vhci_priv\n");
741 + spin_unlock_irqrestore(&vdev->priv_lock, flag);
742 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
743 + return;
744 + }
745 +
746 + priv->seqnum = atomic_inc_return(&the_controller->seqnum);
747 + if (priv->seqnum == 0xffff)
748 + uinfo("seqnum max\n");
749 +
750 + priv->vdev = vdev;
751 + priv->urb = urb;
752 +
753 + urb->hcpriv = (void *) priv;
754 +
755 +
756 + list_add_tail(&priv->list, &vdev->priv_tx);
757 +
758 + wake_up(&vdev->waitq_tx);
759 + spin_unlock_irqrestore(&vdev->priv_lock, flag);
760 +}
761 +
762 +static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
763 + gfp_t mem_flags)
764 +{
765 + struct device *dev = &urb->dev->dev;
766 + int ret = 0;
767 + unsigned long flags;
768 +
769 + dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
770 + hcd, urb, mem_flags);
771 +
772 + /* patch to usb_sg_init() is in 2.5.60 */
773 + BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
774 +
775 + spin_lock_irqsave(&the_controller->lock, flags);
776 +
777 + /* check HC is active or not */
778 + if (!HC_IS_RUNNING(hcd->state)) {
779 + dev_err(dev, "HC is not running\n");
780 + spin_unlock_irqrestore(&the_controller->lock, flags);
781 + return -ENODEV;
782 + }
783 +
784 + if (urb->status != -EINPROGRESS) {
785 + dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
786 + spin_unlock_irqrestore(&the_controller->lock, flags);
787 + return urb->status;
788 + }
789 +
790 + ret = usb_hcd_link_urb_to_ep(hcd, urb);
791 + if (ret)
792 + goto no_need_unlink;
793 +
794 + /*
795 + * The enumelation process is as follows;
796 + *
797 + * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
798 + * to get max packet length of default pipe
799 + *
800 + * 2. Set_Address request to DevAddr(0) EndPoint(0)
801 + *
802 + */
803 +
804 + if (usb_pipedevice(urb->pipe) == 0) {
805 + __u8 type = usb_pipetype(urb->pipe);
806 + struct usb_ctrlrequest *ctrlreq =
807 + (struct usb_ctrlrequest *) urb->setup_packet;
808 + struct vhci_device *vdev =
809 + port_to_vdev(the_controller->pending_port);
810 +
811 + if (type != PIPE_CONTROL || !ctrlreq) {
812 + dev_err(dev, "invalid request to devnum 0\n");
813 + ret = EINVAL;
814 + goto no_need_xmit;
815 + }
816 +
817 + switch (ctrlreq->bRequest) {
818 + case USB_REQ_SET_ADDRESS:
819 + /* set_address may come when a device is reset */
820 + dev_info(dev, "SetAddress Request (%d) to port %d\n",
821 + ctrlreq->wValue, vdev->rhport);
822 +
823 + vdev->udev = urb->dev;
824 +
825 + spin_lock(&vdev->ud.lock);
826 + vdev->ud.status = VDEV_ST_USED;
827 + spin_unlock(&vdev->ud.lock);
828 +
829 + if (urb->status == -EINPROGRESS) {
830 + /* This request is successfully completed. */
831 + /* If not -EINPROGRESS, possibly unlinked. */
832 + urb->status = 0;
833 + }
834 +
835 + goto no_need_xmit;
836 +
837 + case USB_REQ_GET_DESCRIPTOR:
838 + if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
839 + dbg_vhci_hc("Not yet?: "
840 + "Get_Descriptor to device 0 "
841 + "(get max pipe size)\n");
842 +
843 + /* FIXME: reference count? (usb_get_dev()) */
844 + vdev->udev = urb->dev;
845 + goto out;
846 +
847 + default:
848 + /* NOT REACHED */
849 + dev_err(dev, "invalid request to devnum 0 bRequest %u, "
850 + "wValue %u\n", ctrlreq->bRequest,
851 + ctrlreq->wValue);
852 + ret = -EINVAL;
853 + goto no_need_xmit;
854 + }
855 +
856 + }
857 +
858 +out:
859 + vhci_tx_urb(urb);
860 +
861 + spin_unlock_irqrestore(&the_controller->lock, flags);
862 +
863 + return 0;
864 +
865 +no_need_xmit:
866 + usb_hcd_unlink_urb_from_ep(hcd, urb);
867 +no_need_unlink:
868 + spin_unlock_irqrestore(&the_controller->lock, flags);
869 +
870 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
871 +
872 + return 0;
873 +}
874 +
875 +/*
876 + * vhci_rx gives back the urb after receiving the reply of the urb. If an
877 + * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
878 + * back its urb. For the driver unlinking the urb, the content of the urb is
879 + * not important, but the calling to its completion handler is important; the
880 + * completion of unlinking is notified by the completion handler.
881 + *
882 + *
883 + * CLIENT SIDE
884 + *
885 + * - When vhci_hcd receives RET_SUBMIT,
886 + *
887 + * - case 1a). the urb of the pdu is not unlinking.
888 + * - normal case
889 + * => just give back the urb
890 + *
891 + * - case 1b). the urb of the pdu is unlinking.
892 + * - usbip.ko will return a reply of the unlinking request.
893 + * => give back the urb now and go to case 2b).
894 + *
895 + * - When vhci_hcd receives RET_UNLINK,
896 + *
897 + * - case 2a). a submit request is still pending in vhci_hcd.
898 + * - urb was really pending in usbip.ko and urb_unlink_urb() was
899 + * completed there.
900 + * => free a pending submit request
901 + * => notify unlink completeness by giving back the urb
902 + *
903 + * - case 2b). a submit request is *not* pending in vhci_hcd.
904 + * - urb was already given back to the core driver.
905 + * => do not give back the urb
906 + *
907 + *
908 + * SERVER SIDE
909 + *
910 + * - When usbip receives CMD_UNLINK,
911 + *
912 + * - case 3a). the urb of the unlink request is now in submission.
913 + * => do usb_unlink_urb().
914 + * => after the unlink is completed, send RET_UNLINK.
915 + *
916 + * - case 3b). the urb of the unlink request is not in submission.
917 + * - may be already completed or never be received
918 + * => send RET_UNLINK
919 + *
920 + */
921 +static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
922 +{
923 + unsigned long flags;
924 + struct vhci_priv *priv;
925 + struct vhci_device *vdev;
926 +
927 + uinfo("vhci_hcd: dequeue a urb %p\n", urb);
928 +
929 +
930 + spin_lock_irqsave(&the_controller->lock, flags);
931 +
932 + priv = urb->hcpriv;
933 + if (!priv) {
934 + /* URB was never linked! or will be soon given back by
935 + * vhci_rx. */
936 + spin_unlock_irqrestore(&the_controller->lock, flags);
937 + return 0;
938 + }
939 +
940 + {
941 + int ret = 0;
942 + ret = usb_hcd_check_unlink_urb(hcd, urb, status);
943 + if (ret) {
944 + spin_unlock_irqrestore(&the_controller->lock, flags);
945 + return 0;
946 + }
947 + }
948 +
949 + /* send unlink request here? */
950 + vdev = priv->vdev;
951 +
952 + if (!vdev->ud.tcp_socket) {
953 + /* tcp connection is closed */
954 + unsigned long flags2;
955 +
956 + spin_lock_irqsave(&vdev->priv_lock, flags2);
957 +
958 + uinfo("vhci_hcd: device %p seems to be disconnected\n", vdev);
959 + list_del(&priv->list);
960 + kfree(priv);
961 + urb->hcpriv = NULL;
962 +
963 + spin_unlock_irqrestore(&vdev->priv_lock, flags2);
964 +
965 + } else {
966 + /* tcp connection is alive */
967 + unsigned long flags2;
968 + struct vhci_unlink *unlink;
969 +
970 + spin_lock_irqsave(&vdev->priv_lock, flags2);
971 +
972 + /* setup CMD_UNLINK pdu */
973 + unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
974 + if (!unlink) {
975 + uerr("malloc vhci_unlink\n");
976 + spin_unlock_irqrestore(&vdev->priv_lock, flags2);
977 + spin_unlock_irqrestore(&the_controller->lock, flags);
978 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
979 + return -ENOMEM;
980 + }
981 +
982 + unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
983 + if (unlink->seqnum == 0xffff)
984 + uinfo("seqnum max\n");
985 +
986 + unlink->unlink_seqnum = priv->seqnum;
987 +
988 + uinfo("vhci_hcd: device %p seems to be still connected\n",
989 + vdev);
990 +
991 + /* send cmd_unlink and try to cancel the pending URB in the
992 + * peer */
993 + list_add_tail(&unlink->list, &vdev->unlink_tx);
994 + wake_up(&vdev->waitq_tx);
995 +
996 + spin_unlock_irqrestore(&vdev->priv_lock, flags2);
997 + }
998 +
999 +
1000 + /*
1001 + * If tcp connection is alive, we have sent CMD_UNLINK.
1002 + * vhci_rx will receive RET_UNLINK and give back the URB.
1003 + * Otherwise, we give back it here.
1004 + */
1005 + if (!vdev->ud.tcp_socket) {
1006 + /* tcp connection is closed */
1007 + uinfo("vhci_hcd: vhci_urb_dequeue() gives back urb %p\n", urb);
1008 +
1009 + usb_hcd_unlink_urb_from_ep(hcd, urb);
1010 +
1011 + spin_unlock_irqrestore(&the_controller->lock, flags);
1012 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
1013 + urb->status);
1014 + spin_lock_irqsave(&the_controller->lock, flags);
1015 + }
1016 +
1017 + spin_unlock_irqrestore(&the_controller->lock, flags);
1018 +
1019 + dbg_vhci_hc("leave\n");
1020 + return 0;
1021 +}
1022 +
1023 +
1024 +static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
1025 +{
1026 + struct vhci_unlink *unlink, *tmp;
1027 +
1028 + spin_lock(&vdev->priv_lock);
1029 +
1030 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
1031 + list_del(&unlink->list);
1032 + kfree(unlink);
1033 + }
1034 +
1035 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
1036 + list_del(&unlink->list);
1037 + kfree(unlink);
1038 + }
1039 +
1040 + spin_unlock(&vdev->priv_lock);
1041 +}
1042 +
1043 +/*
1044 + * The important thing is that only one context begins cleanup.
1045 + * This is why error handling and cleanup become simple.
1046 + * We do not want to consider race condition as possible.
1047 + */
1048 +static void vhci_shutdown_connection(struct usbip_device *ud)
1049 +{
1050 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1051 +
1052 + /* need this? see stub_dev.c */
1053 + if (ud->tcp_socket) {
1054 + udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
1055 + kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
1056 + }
1057 +
1058 + usbip_stop_threads(&vdev->ud);
1059 + uinfo("stop threads\n");
1060 +
1061 + /* active connection is closed */
1062 + if (vdev->ud.tcp_socket != NULL) {
1063 + sock_release(vdev->ud.tcp_socket);
1064 + vdev->ud.tcp_socket = NULL;
1065 + }
1066 + uinfo("release socket\n");
1067 +
1068 + vhci_device_unlink_cleanup(vdev);
1069 +
1070 + /*
1071 + * rh_port_disconnect() is a trigger of ...
1072 + * usb_disable_device():
1073 + * disable all the endpoints for a USB device.
1074 + * usb_disable_endpoint():
1075 + * disable endpoints. pending urbs are unlinked(dequeued).
1076 + *
1077 + * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
1078 + * deteched device should release used urbs in a cleanup function(i.e.
1079 + * xxx_disconnect()). Therefore, vhci_hcd does not need to release
1080 + * pushed urbs and their private data in this function.
1081 + *
1082 + * NOTE: vhci_dequeue() must be considered carefully. When shutdowning
1083 + * a connection, vhci_shutdown_connection() expects vhci_dequeue()
1084 + * gives back pushed urbs and frees their private data by request of
1085 + * the cleanup function of a USB driver. When unlinking a urb with an
1086 + * active connection, vhci_dequeue() does not give back the urb which
1087 + * is actually given back by vhci_rx after receiving its return pdu.
1088 + *
1089 + */
1090 + rh_port_disconnect(vdev->rhport);
1091 +
1092 + uinfo("disconnect device\n");
1093 +}
1094 +
1095 +
1096 +static void vhci_device_reset(struct usbip_device *ud)
1097 +{
1098 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1099 +
1100 + spin_lock(&ud->lock);
1101 +
1102 + vdev->speed = 0;
1103 + vdev->devid = 0;
1104 +
1105 + ud->tcp_socket = NULL;
1106 +
1107 + ud->status = VDEV_ST_NULL;
1108 +
1109 + spin_unlock(&ud->lock);
1110 +}
1111 +
1112 +static void vhci_device_unusable(struct usbip_device *ud)
1113 +{
1114 + spin_lock(&ud->lock);
1115 +
1116 + ud->status = VDEV_ST_ERROR;
1117 +
1118 + spin_unlock(&ud->lock);
1119 +}
1120 +
1121 +static void vhci_device_init(struct vhci_device *vdev)
1122 +{
1123 + memset(vdev, 0, sizeof(*vdev));
1124 +
1125 + usbip_task_init(&vdev->ud.tcp_rx, "vhci_rx", vhci_rx_loop);
1126 + usbip_task_init(&vdev->ud.tcp_tx, "vhci_tx", vhci_tx_loop);
1127 +
1128 + vdev->ud.side = USBIP_VHCI;
1129 + vdev->ud.status = VDEV_ST_NULL;
1130 + /* vdev->ud.lock = SPIN_LOCK_UNLOCKED; */
1131 + spin_lock_init(&vdev->ud.lock);
1132 +
1133 + INIT_LIST_HEAD(&vdev->priv_rx);
1134 + INIT_LIST_HEAD(&vdev->priv_tx);
1135 + INIT_LIST_HEAD(&vdev->unlink_tx);
1136 + INIT_LIST_HEAD(&vdev->unlink_rx);
1137 + /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */
1138 + spin_lock_init(&vdev->priv_lock);
1139 +
1140 + init_waitqueue_head(&vdev->waitq_tx);
1141 +
1142 + vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
1143 + vdev->ud.eh_ops.reset = vhci_device_reset;
1144 + vdev->ud.eh_ops.unusable = vhci_device_unusable;
1145 +
1146 + usbip_start_eh(&vdev->ud);
1147 +}
1148 +
1149 +
1150 +/*----------------------------------------------------------------------*/
1151 +
1152 +static int vhci_start(struct usb_hcd *hcd)
1153 +{
1154 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1155 + int rhport;
1156 + int err = 0;
1157 +
1158 + dbg_vhci_hc("enter vhci_start\n");
1159 +
1160 +
1161 + /* initialize private data of usb_hcd */
1162 +
1163 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
1164 + struct vhci_device *vdev = &vhci->vdev[rhport];
1165 + vhci_device_init(vdev);
1166 + vdev->rhport = rhport;
1167 + }
1168 +
1169 + atomic_set(&vhci->seqnum, 0);
1170 + spin_lock_init(&vhci->lock);
1171 +
1172 +
1173 +
1174 + hcd->power_budget = 0; /* no limit */
1175 + hcd->state = HC_STATE_RUNNING;
1176 + hcd->uses_new_polling = 1;
1177 +
1178 +
1179 + /* vhci_hcd is now ready to be controlled through sysfs */
1180 + err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
1181 + if (err) {
1182 + uerr("create sysfs files\n");
1183 + return err;
1184 + }
1185 +
1186 + return 0;
1187 +}
1188 +
1189 +static void vhci_stop(struct usb_hcd *hcd)
1190 +{
1191 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1192 + int rhport = 0;
1193 +
1194 + dbg_vhci_hc("stop VHCI controller\n");
1195 +
1196 +
1197 + /* 1. remove the userland interface of vhci_hcd */
1198 + sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
1199 +
1200 + /* 2. shutdown all the ports of vhci_hcd */
1201 + for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
1202 + struct vhci_device *vdev = &vhci->vdev[rhport];
1203 +
1204 + usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
1205 + usbip_stop_eh(&vdev->ud);
1206 + }
1207 +
1208 +
1209 + uinfo("vhci_stop done\n");
1210 +}
1211 +
1212 +/*----------------------------------------------------------------------*/
1213 +
1214 +static int vhci_get_frame_number(struct usb_hcd *hcd)
1215 +{
1216 + uerr("Not yet implemented\n");
1217 + return 0;
1218 +}
1219 +
1220 +
1221 +#ifdef CONFIG_PM
1222 +
1223 +/* FIXME: suspend/resume */
1224 +static int vhci_bus_suspend(struct usb_hcd *hcd)
1225 +{
1226 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1227 +
1228 + dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1229 +
1230 + spin_lock_irq(&vhci->lock);
1231 + /* vhci->rh_state = DUMMY_RH_SUSPENDED;
1232 + * set_link_state(vhci); */
1233 + hcd->state = HC_STATE_SUSPENDED;
1234 + spin_unlock_irq(&vhci->lock);
1235 +
1236 + return 0;
1237 +}
1238 +
1239 +static int vhci_bus_resume(struct usb_hcd *hcd)
1240 +{
1241 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1242 + int rc = 0;
1243 +
1244 + dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1245 +
1246 + spin_lock_irq(&vhci->lock);
1247 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1248 + rc = -ESHUTDOWN;
1249 + } else {
1250 + /* vhci->rh_state = DUMMY_RH_RUNNING;
1251 + * set_link_state(vhci);
1252 + * if (!list_empty(&vhci->urbp_list))
1253 + * mod_timer(&vhci->timer, jiffies); */
1254 + hcd->state = HC_STATE_RUNNING;
1255 + }
1256 + spin_unlock_irq(&vhci->lock);
1257 + return rc;
1258 +
1259 + return 0;
1260 +}
1261 +
1262 +#else
1263 +
1264 +#define vhci_bus_suspend NULL
1265 +#define vhci_bus_resume NULL
1266 +#endif
1267 +
1268 +
1269 +
1270 +static struct hc_driver vhci_hc_driver = {
1271 + .description = driver_name,
1272 + .product_desc = driver_desc,
1273 + .hcd_priv_size = sizeof(struct vhci_hcd),
1274 +
1275 + .flags = HCD_USB2,
1276 +
1277 + .start = vhci_start,
1278 + .stop = vhci_stop,
1279 +
1280 + .urb_enqueue = vhci_urb_enqueue,
1281 + .urb_dequeue = vhci_urb_dequeue,
1282 +
1283 + .get_frame_number = vhci_get_frame_number,
1284 +
1285 + .hub_status_data = vhci_hub_status,
1286 + .hub_control = vhci_hub_control,
1287 + .bus_suspend = vhci_bus_suspend,
1288 + .bus_resume = vhci_bus_resume,
1289 +};
1290 +
1291 +static int vhci_hcd_probe(struct platform_device *pdev)
1292 +{
1293 + struct usb_hcd *hcd;
1294 + int ret;
1295 +
1296 + uinfo("proving...\n");
1297 +
1298 + dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1299 +
1300 + /* will be removed */
1301 + if (pdev->dev.dma_mask) {
1302 + dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
1303 + return -EINVAL;
1304 + }
1305 +
1306 + /*
1307 + * Allocate and initialize hcd.
1308 + * Our private data is also allocated automatically.
1309 + */
1310 + hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, pdev->dev.bus_id);
1311 + if (!hcd) {
1312 + uerr("create hcd failed\n");
1313 + return -ENOMEM;
1314 + }
1315 +
1316 +
1317 + /* this is private data for vhci_hcd */
1318 + the_controller = hcd_to_vhci(hcd);
1319 +
1320 + /*
1321 + * Finish generic HCD structure initialization and register.
1322 + * Call the driver's reset() and start() routines.
1323 + */
1324 + ret = usb_add_hcd(hcd, 0, 0);
1325 + if (ret != 0) {
1326 + uerr("usb_add_hcd failed %d\n", ret);
1327 + usb_put_hcd(hcd);
1328 + the_controller = NULL;
1329 + return ret;
1330 + }
1331 +
1332 +
1333 + dbg_vhci_hc("bye\n");
1334 + return 0;
1335 +}
1336 +
1337 +
1338 +static int vhci_hcd_remove(struct platform_device *pdev)
1339 +{
1340 + struct usb_hcd *hcd;
1341 +
1342 + hcd = platform_get_drvdata(pdev);
1343 + if (!hcd)
1344 + return 0;
1345 +
1346 + /*
1347 + * Disconnects the root hub,
1348 + * then reverses the effects of usb_add_hcd(),
1349 + * invoking the HCD's stop() methods.
1350 + */
1351 + usb_remove_hcd(hcd);
1352 + usb_put_hcd(hcd);
1353 + the_controller = NULL;
1354 +
1355 +
1356 + return 0;
1357 +}
1358 +
1359 +
1360 +
1361 +#ifdef CONFIG_PM
1362 +
1363 +/* what should happen for USB/IP under suspend/resume? */
1364 +static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1365 +{
1366 + struct usb_hcd *hcd;
1367 + int rhport = 0;
1368 + int connected = 0;
1369 + int ret = 0;
1370 +
1371 + dev_dbg(&pdev->dev, "%s\n", __func__);
1372 +
1373 + hcd = platform_get_drvdata(pdev);
1374 +
1375 + spin_lock(&the_controller->lock);
1376 +
1377 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
1378 + if (the_controller->port_status[rhport] &
1379 + USB_PORT_STAT_CONNECTION)
1380 + connected += 1;
1381 +
1382 + spin_unlock(&the_controller->lock);
1383 +
1384 + if (connected > 0) {
1385 + uinfo("We have %d active connection%s. Do not suspend.\n",
1386 + connected, (connected == 1 ? "" : "s"));
1387 + ret = -EBUSY;
1388 + } else {
1389 + uinfo("suspend vhci_hcd");
1390 + clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1391 + }
1392 +
1393 + return ret;
1394 +}
1395 +
1396 +static int vhci_hcd_resume(struct platform_device *pdev)
1397 +{
1398 + struct usb_hcd *hcd;
1399 +
1400 + dev_dbg(&pdev->dev, "%s\n", __func__);
1401 +
1402 + hcd = platform_get_drvdata(pdev);
1403 + set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1404 + usb_hcd_poll_rh_status(hcd);
1405 +
1406 + return 0;
1407 +}
1408 +
1409 +#else
1410 +
1411 +#define vhci_hcd_suspend NULL
1412 +#define vhci_hcd_resume NULL
1413 +
1414 +#endif
1415 +
1416 +
1417 +static struct platform_driver vhci_driver = {
1418 + .probe = vhci_hcd_probe,
1419 + .remove = __devexit_p(vhci_hcd_remove),
1420 + .suspend = vhci_hcd_suspend,
1421 + .resume = vhci_hcd_resume,
1422 + .driver = {
1423 + .name = (char *) driver_name,
1424 + .owner = THIS_MODULE,
1425 + },
1426 +};
1427 +
1428 +/*----------------------------------------------------------------------*/
1429 +
1430 +/*
1431 + * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1432 + * We need to add this virtual device as a platform device arbitrarily:
1433 + * 1. platform_device_register()
1434 + */
1435 +static void the_pdev_release(struct device *dev)
1436 +{
1437 + return;
1438 +}
1439 +
1440 +static struct platform_device the_pdev = {
1441 + /* should be the same name as driver_name */
1442 + .name = (char *) driver_name,
1443 + .id = -1,
1444 + .dev = {
1445 + /* .driver = &vhci_driver, */
1446 + .release = the_pdev_release,
1447 + },
1448 +};
1449 +
1450 +static int __init vhci_init(void)
1451 +{
1452 + int ret;
1453 +
1454 + dbg_vhci_hc("enter\n");
1455 + if (usb_disabled())
1456 + return -ENODEV;
1457 +
1458 + printk(KERN_INFO KBUILD_MODNAME ": %s, %s\n", driver_name,
1459 + DRIVER_VERSION);
1460 +
1461 + ret = platform_driver_register(&vhci_driver);
1462 + if (ret < 0)
1463 + goto err_driver_register;
1464 +
1465 + ret = platform_device_register(&the_pdev);
1466 + if (ret < 0)
1467 + goto err_platform_device_register;
1468 +
1469 + dbg_vhci_hc("bye\n");
1470 + return ret;
1471 +
1472 + /* error occurred */
1473 +err_platform_device_register:
1474 + platform_driver_unregister(&vhci_driver);
1475 +
1476 +err_driver_register:
1477 + dbg_vhci_hc("bye\n");
1478 + return ret;
1479 +}
1480 +module_init(vhci_init);
1481 +
1482 +static void __exit vhci_cleanup(void)
1483 +{
1484 + dbg_vhci_hc("enter\n");
1485 +
1486 + platform_device_unregister(&the_pdev);
1487 + platform_driver_unregister(&vhci_driver);
1488 +
1489 + dbg_vhci_hc("bye\n");
1490 +}
1491 +module_exit(vhci_cleanup);
1492 diff --git a/drivers/staging/usbip/vhci_rx.c b/drivers/staging/usbip/vhci_rx.c
1493 new file mode 100644
1494 index 0000000..933ccaf
1495 --- /dev/null
1496 +++ b/drivers/staging/usbip/vhci_rx.c
1497 @@ -0,0 +1,251 @@
1498 +/*
1499 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
1500 + *
1501 + * This is free software; you can redistribute it and/or modify
1502 + * it under the terms of the GNU General Public License as published by
1503 + * the Free Software Foundation; either version 2 of the License, or
1504 + * (at your option) any later version.
1505 + *
1506 + * This is distributed in the hope that it will be useful,
1507 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1508 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1509 + * GNU General Public License for more details.
1510 + *
1511 + * You should have received a copy of the GNU General Public License
1512 + * along with this program; if not, write to the Free Software
1513 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1514 + * USA.
1515 + */
1516 +
1517 +#include "usbip_common.h"
1518 +#include "vhci.h"
1519 +
1520 +
1521 +/* get URB from transmitted urb queue */
1522 +static struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev,
1523 + __u32 seqnum)
1524 +{
1525 + struct vhci_priv *priv, *tmp;
1526 + struct urb *urb = NULL;
1527 + int status;
1528 +
1529 + spin_lock(&vdev->priv_lock);
1530 +
1531 + list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
1532 + if (priv->seqnum == seqnum) {
1533 + urb = priv->urb;
1534 + status = urb->status;
1535 +
1536 + dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
1537 + urb, priv, seqnum);
1538 +
1539 + /* TODO: fix logic here to improve indent situtation */
1540 + if (status != -EINPROGRESS) {
1541 + if (status == -ENOENT ||
1542 + status == -ECONNRESET)
1543 + dev_info(&urb->dev->dev,
1544 + "urb %p was unlinked "
1545 + "%ssynchronuously.\n", urb,
1546 + status == -ENOENT ? "" : "a");
1547 + else
1548 + dev_info(&urb->dev->dev,
1549 + "urb %p may be in a error, "
1550 + "status %d\n", urb, status);
1551 + }
1552 +
1553 + list_del(&priv->list);
1554 + kfree(priv);
1555 + urb->hcpriv = NULL;
1556 +
1557 + break;
1558 + }
1559 + }
1560 +
1561 + spin_unlock(&vdev->priv_lock);
1562 +
1563 + return urb;
1564 +}
1565 +
1566 +static void vhci_recv_ret_submit(struct vhci_device *vdev,
1567 + struct usbip_header *pdu)
1568 +{
1569 + struct usbip_device *ud = &vdev->ud;
1570 + struct urb *urb;
1571 +
1572 +
1573 + urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
1574 +
1575 +
1576 + if (!urb) {
1577 + uerr("cannot find a urb of seqnum %u\n", pdu->base.seqnum);
1578 + uinfo("max seqnum %d\n", atomic_read(&the_controller->seqnum));
1579 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
1580 + return;
1581 + }
1582 +
1583 +
1584 + /* unpack the pdu to a urb */
1585 + usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
1586 +
1587 +
1588 + /* recv transfer buffer */
1589 + if (usbip_recv_xbuff(ud, urb) < 0)
1590 + return;
1591 +
1592 +
1593 + /* recv iso_packet_descriptor */
1594 + if (usbip_recv_iso(ud, urb) < 0)
1595 + return;
1596 +
1597 +
1598 + if (dbg_flag_vhci_rx)
1599 + usbip_dump_urb(urb);
1600 +
1601 +
1602 + dbg_vhci_rx("now giveback urb %p\n", urb);
1603 +
1604 + spin_lock(&the_controller->lock);
1605 + usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
1606 + spin_unlock(&the_controller->lock);
1607 +
1608 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
1609 +
1610 +
1611 + dbg_vhci_rx("Leave\n");
1612 +
1613 + return;
1614 +}
1615 +
1616 +
1617 +static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
1618 + struct usbip_header *pdu)
1619 +{
1620 + struct vhci_unlink *unlink, *tmp;
1621 +
1622 + spin_lock(&vdev->priv_lock);
1623 +
1624 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
1625 + uinfo("unlink->seqnum %lu\n", unlink->seqnum);
1626 + if (unlink->seqnum == pdu->base.seqnum) {
1627 + dbg_vhci_rx("found pending unlink, %lu\n",
1628 + unlink->seqnum);
1629 + list_del(&unlink->list);
1630 +
1631 + spin_unlock(&vdev->priv_lock);
1632 + return unlink;
1633 + }
1634 + }
1635 +
1636 + spin_unlock(&vdev->priv_lock);
1637 +
1638 + return NULL;
1639 +}
1640 +
1641 +
1642 +static void vhci_recv_ret_unlink(struct vhci_device *vdev,
1643 + struct usbip_header *pdu)
1644 +{
1645 + struct vhci_unlink *unlink;
1646 + struct urb *urb;
1647 +
1648 + usbip_dump_header(pdu);
1649 +
1650 + unlink = dequeue_pending_unlink(vdev, pdu);
1651 + if (!unlink) {
1652 + uinfo("cannot find the pending unlink %u\n", pdu->base.seqnum);
1653 + return;
1654 + }
1655 +
1656 + urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
1657 + if (!urb) {
1658 + /*
1659 + * I get the result of a unlink request. But, it seems that I
1660 + * already received the result of its submit result and gave
1661 + * back the URB.
1662 + */
1663 + uinfo("the urb (seqnum %d) was already given backed\n",
1664 + pdu->base.seqnum);
1665 + } else {
1666 + dbg_vhci_rx("now giveback urb %p\n", urb);
1667 +
1668 + /* If unlink is succeed, status is -ECONNRESET */
1669 + urb->status = pdu->u.ret_unlink.status;
1670 + uinfo("%d\n", urb->status);
1671 +
1672 + spin_lock(&the_controller->lock);
1673 + usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
1674 + spin_unlock(&the_controller->lock);
1675 +
1676 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
1677 + urb->status);
1678 + }
1679 +
1680 + kfree(unlink);
1681 +
1682 + return;
1683 +}
1684 +
1685 +/* recv a pdu */
1686 +static void vhci_rx_pdu(struct usbip_device *ud)
1687 +{
1688 + int ret;
1689 + struct usbip_header pdu;
1690 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1691 +
1692 +
1693 + dbg_vhci_rx("Enter\n");
1694 +
1695 + memset(&pdu, 0, sizeof(pdu));
1696 +
1697 +
1698 + /* 1. receive a pdu header */
1699 + ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
1700 + if (ret != sizeof(pdu)) {
1701 + uerr("receiving pdu failed! size is %d, should be %d\n",
1702 + ret, sizeof(pdu));
1703 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
1704 + return;
1705 + }
1706 +
1707 + usbip_header_correct_endian(&pdu, 0);
1708 +
1709 + if (dbg_flag_vhci_rx)
1710 + usbip_dump_header(&pdu);
1711 +
1712 + switch (pdu.base.command) {
1713 + case USBIP_RET_SUBMIT:
1714 + vhci_recv_ret_submit(vdev, &pdu);
1715 + break;
1716 + case USBIP_RET_UNLINK:
1717 + vhci_recv_ret_unlink(vdev, &pdu);
1718 + break;
1719 + default:
1720 + /* NOTREACHED */
1721 + uerr("unknown pdu %u\n", pdu.base.command);
1722 + usbip_dump_header(&pdu);
1723 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
1724 + }
1725 +}
1726 +
1727 +
1728 +/*-------------------------------------------------------------------------*/
1729 +
1730 +void vhci_rx_loop(struct usbip_task *ut)
1731 +{
1732 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
1733 +
1734 +
1735 + while (1) {
1736 + if (signal_pending(current)) {
1737 + dbg_vhci_rx("signal catched!\n");
1738 + break;
1739 + }
1740 +
1741 +
1742 + if (usbip_event_happend(ud))
1743 + break;
1744 +
1745 + vhci_rx_pdu(ud);
1746 + }
1747 +}
1748 +
1749 diff --git a/drivers/staging/usbip/vhci_sysfs.c b/drivers/staging/usbip/vhci_sysfs.c
1750 new file mode 100644
1751 index 0000000..24c2851
1752 --- /dev/null
1753 +++ b/drivers/staging/usbip/vhci_sysfs.c
1754 @@ -0,0 +1,250 @@
1755 +/*
1756 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
1757 + *
1758 + * This is free software; you can redistribute it and/or modify
1759 + * it under the terms of the GNU General Public License as published by
1760 + * the Free Software Foundation; either version 2 of the License, or
1761 + * (at your option) any later version.
1762 + *
1763 + * This is distributed in the hope that it will be useful,
1764 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1765 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1766 + * GNU General Public License for more details.
1767 + *
1768 + * You should have received a copy of the GNU General Public License
1769 + * along with this program; if not, write to the Free Software
1770 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1771 + * USA.
1772 + */
1773 +
1774 +#include "usbip_common.h"
1775 +#include "vhci.h"
1776 +
1777 +#include <linux/in.h>
1778 +
1779 +/* TODO: refine locking ?*/
1780 +
1781 +/* Sysfs entry to show port status */
1782 +static ssize_t show_status(struct device *dev, struct device_attribute *attr,
1783 + char *out)
1784 +{
1785 + char *s = out;
1786 + int i = 0;
1787 +
1788 + if (!the_controller || !out)
1789 + BUG();
1790 +
1791 + spin_lock(&the_controller->lock);
1792 +
1793 + /*
1794 + * output example:
1795 + * prt sta spd dev socket local_busid
1796 + * 000 004 000 000 c5a7bb80 1-2.3
1797 + * 001 004 000 000 d8cee980 2-3.4
1798 + *
1799 + * IP address can be retrieved from a socket pointer address by looking
1800 + * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
1801 + * port number and its peer IP address.
1802 + */
1803 + out += sprintf(out, "prt sta spd bus dev socket "
1804 + "local_busid\n");
1805 +
1806 + for (i = 0; i < VHCI_NPORTS; i++) {
1807 + struct vhci_device *vdev = port_to_vdev(i);
1808 +
1809 + spin_lock(&vdev->ud.lock);
1810 +
1811 + out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
1812 +
1813 + if (vdev->ud.status == VDEV_ST_USED) {
1814 + out += sprintf(out, "%03u %08x ",
1815 + vdev->speed, vdev->devid);
1816 + out += sprintf(out, "%16p ", vdev->ud.tcp_socket);
1817 + out += sprintf(out, "%s", vdev->udev->dev.bus_id);
1818 +
1819 + } else
1820 + out += sprintf(out, "000 000 000 0000000000000000 0-0");
1821 +
1822 + out += sprintf(out, "\n");
1823 +
1824 + spin_unlock(&vdev->ud.lock);
1825 + }
1826 +
1827 + spin_unlock(&the_controller->lock);
1828 +
1829 + return out - s;
1830 +}
1831 +static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
1832 +
1833 +/* Sysfs entry to shutdown a virtual connection */
1834 +static int vhci_port_disconnect(__u32 rhport)
1835 +{
1836 + struct vhci_device *vdev;
1837 +
1838 + dbg_vhci_sysfs("enter\n");
1839 +
1840 + /* lock */
1841 + spin_lock(&the_controller->lock);
1842 +
1843 + vdev = port_to_vdev(rhport);
1844 +
1845 + spin_lock(&vdev->ud.lock);
1846 + if (vdev->ud.status == VDEV_ST_NULL) {
1847 + uerr("not connected %d\n", vdev->ud.status);
1848 +
1849 + /* unlock */
1850 + spin_unlock(&vdev->ud.lock);
1851 + spin_unlock(&the_controller->lock);
1852 +
1853 + return -EINVAL;
1854 + }
1855 +
1856 + /* unlock */
1857 + spin_unlock(&vdev->ud.lock);
1858 + spin_unlock(&the_controller->lock);
1859 +
1860 + usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
1861 +
1862 + return 0;
1863 +}
1864 +
1865 +static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
1866 + const char *buf, size_t count)
1867 +{
1868 + int err;
1869 + __u32 rhport = 0;
1870 +
1871 + sscanf(buf, "%u", &rhport);
1872 +
1873 + /* check rhport */
1874 + if (rhport >= VHCI_NPORTS) {
1875 + uerr("invalid port %u\n", rhport);
1876 + return -EINVAL;
1877 + }
1878 +
1879 + err = vhci_port_disconnect(rhport);
1880 + if (err < 0)
1881 + return -EINVAL;
1882 +
1883 + dbg_vhci_sysfs("Leave\n");
1884 + return count;
1885 +}
1886 +static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
1887 +
1888 +/* Sysfs entry to establish a virtual connection */
1889 +static int valid_args(__u32 rhport, enum usb_device_speed speed)
1890 +{
1891 + /* check rhport */
1892 + if ((rhport < 0) || (rhport >= VHCI_NPORTS)) {
1893 + uerr("port %u\n", rhport);
1894 + return -EINVAL;
1895 + }
1896 +
1897 + /* check speed */
1898 + switch (speed) {
1899 + case USB_SPEED_LOW:
1900 + case USB_SPEED_FULL:
1901 + case USB_SPEED_HIGH:
1902 + case USB_SPEED_VARIABLE:
1903 + break;
1904 + default:
1905 + uerr("speed %d\n", speed);
1906 + return -EINVAL;
1907 + }
1908 +
1909 + return 0;
1910 +}
1911 +
1912 +/*
1913 + * To start a new USB/IP attachment, a userland program needs to setup a TCP
1914 + * connection and then write its socket descriptor with remote device
1915 + * information into this sysfs file.
1916 + *
1917 + * A remote device is virtually attached to the root-hub port of @rhport with
1918 + * @speed. @devid is embedded into a request to specify the remote device in a
1919 + * server host.
1920 + *
1921 + * write() returns 0 on success, else negative errno.
1922 + */
1923 +static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
1924 + const char *buf, size_t count)
1925 +{
1926 + struct vhci_device *vdev;
1927 + struct socket *socket;
1928 + int sockfd = 0;
1929 + __u32 rhport = 0, devid = 0, speed = 0;
1930 +
1931 + /*
1932 + * @rhport: port number of vhci_hcd
1933 + * @sockfd: socket descriptor of an established TCP connection
1934 + * @devid: unique device identifier in a remote host
1935 + * @speed: usb device speed in a remote host
1936 + */
1937 + sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed);
1938 +
1939 + dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
1940 + rhport, sockfd, devid, speed);
1941 +
1942 +
1943 + /* check received parameters */
1944 + if (valid_args(rhport, speed) < 0)
1945 + return -EINVAL;
1946 +
1947 + /* check sockfd */
1948 + socket = sockfd_to_socket(sockfd);
1949 + if (!socket)
1950 + return -EINVAL;
1951 +
1952 + /* now need lock until setting vdev status as used */
1953 +
1954 + /* begin a lock */
1955 + spin_lock(&the_controller->lock);
1956 +
1957 + vdev = port_to_vdev(rhport);
1958 +
1959 + spin_lock(&vdev->ud.lock);
1960 +
1961 + if (vdev->ud.status != VDEV_ST_NULL) {
1962 + /* end of the lock */
1963 + spin_unlock(&vdev->ud.lock);
1964 + spin_unlock(&the_controller->lock);
1965 +
1966 + uerr("port %d already used\n", rhport);
1967 + return -EINVAL;
1968 + }
1969 +
1970 + uinfo("rhport(%u) sockfd(%d) devid(%u) speed(%u)\n",
1971 + rhport, sockfd, devid, speed);
1972 +
1973 + vdev->devid = devid;
1974 + vdev->speed = speed;
1975 + vdev->ud.tcp_socket = socket;
1976 + vdev->ud.status = VDEV_ST_NOTASSIGNED;
1977 +
1978 + spin_unlock(&vdev->ud.lock);
1979 + spin_unlock(&the_controller->lock);
1980 + /* end the lock */
1981 +
1982 + /*
1983 + * this function will sleep, so should be out of the lock. but, it's ok
1984 + * because we already marked vdev as being used. really?
1985 + */
1986 + usbip_start_threads(&vdev->ud);
1987 +
1988 + rh_port_connect(rhport, speed);
1989 +
1990 + return count;
1991 +}
1992 +static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
1993 +
1994 +static struct attribute *dev_attrs[] = {
1995 + &dev_attr_status.attr,
1996 + &dev_attr_detach.attr,
1997 + &dev_attr_attach.attr,
1998 + &dev_attr_usbip_debug.attr,
1999 + NULL,
2000 +};
2001 +
2002 +struct attribute_group dev_attr_group = {
2003 + .attrs = dev_attrs,
2004 +};
2005 diff --git a/drivers/staging/usbip/vhci_tx.c b/drivers/staging/usbip/vhci_tx.c
2006 new file mode 100644
2007 index 0000000..1f552a9
2008 --- /dev/null
2009 +++ b/drivers/staging/usbip/vhci_tx.c
2010 @@ -0,0 +1,239 @@
2011 +/*
2012 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
2013 + *
2014 + * This is free software; you can redistribute it and/or modify
2015 + * it under the terms of the GNU General Public License as published by
2016 + * the Free Software Foundation; either version 2 of the License, or
2017 + * (at your option) any later version.
2018 + *
2019 + * This is distributed in the hope that it will be useful,
2020 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2021 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2022 + * GNU General Public License for more details.
2023 + *
2024 + * You should have received a copy of the GNU General Public License
2025 + * along with this program; if not, write to the Free Software
2026 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
2027 + * USA.
2028 + */
2029 +
2030 +#include "usbip_common.h"
2031 +#include "vhci.h"
2032 +
2033 +
2034 +static void setup_cmd_submit_pdu(struct usbip_header *pdup, struct urb *urb)
2035 +{
2036 + struct vhci_priv *priv = ((struct vhci_priv *)urb->hcpriv);
2037 + struct vhci_device *vdev = priv->vdev;
2038 +
2039 + dbg_vhci_tx("URB, local devnum %u, remote devid %u\n",
2040 + usb_pipedevice(urb->pipe), vdev->devid);
2041 +
2042 + pdup->base.command = USBIP_CMD_SUBMIT;
2043 + pdup->base.seqnum = priv->seqnum;
2044 + pdup->base.devid = vdev->devid;
2045 + if (usb_pipein(urb->pipe))
2046 + pdup->base.direction = USBIP_DIR_IN;
2047 + else
2048 + pdup->base.direction = USBIP_DIR_OUT;
2049 + pdup->base.ep = usb_pipeendpoint(urb->pipe);
2050 +
2051 + usbip_pack_pdu(pdup, urb, USBIP_CMD_SUBMIT, 1);
2052 +
2053 + if (urb->setup_packet)
2054 + memcpy(pdup->u.cmd_submit.setup, urb->setup_packet, 8);
2055 +}
2056 +
2057 +static struct vhci_priv *dequeue_from_priv_tx(struct vhci_device *vdev)
2058 +{
2059 + unsigned long flags;
2060 + struct vhci_priv *priv, *tmp;
2061 +
2062 + spin_lock_irqsave(&vdev->priv_lock, flags);
2063 +
2064 + list_for_each_entry_safe(priv, tmp, &vdev->priv_tx, list) {
2065 + list_move_tail(&priv->list, &vdev->priv_rx);
2066 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
2067 + return priv;
2068 + }
2069 +
2070 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
2071 +
2072 + return NULL;
2073 +}
2074 +
2075 +
2076 +
2077 +static int vhci_send_cmd_submit(struct vhci_device *vdev)
2078 +{
2079 + struct vhci_priv *priv = NULL;
2080 +
2081 + struct msghdr msg;
2082 + struct kvec iov[3];
2083 + size_t txsize;
2084 +
2085 + size_t total_size = 0;
2086 +
2087 + while ((priv = dequeue_from_priv_tx(vdev)) != NULL) {
2088 + int ret;
2089 + struct urb *urb = priv->urb;
2090 + struct usbip_header pdu_header;
2091 + void *iso_buffer = NULL;
2092 +
2093 + txsize = 0;
2094 + memset(&pdu_header, 0, sizeof(pdu_header));
2095 + memset(&msg, 0, sizeof(msg));
2096 + memset(&iov, 0, sizeof(iov));
2097 +
2098 + dbg_vhci_tx("setup txdata urb %p\n", urb);
2099 +
2100 +
2101 + /* 1. setup usbip_header */
2102 + setup_cmd_submit_pdu(&pdu_header, urb);
2103 + usbip_header_correct_endian(&pdu_header, 1);
2104 +
2105 + iov[0].iov_base = &pdu_header;
2106 + iov[0].iov_len = sizeof(pdu_header);
2107 + txsize += sizeof(pdu_header);
2108 +
2109 + /* 2. setup transfer buffer */
2110 + if (!usb_pipein(urb->pipe) && urb->transfer_buffer_length > 0) {
2111 + iov[1].iov_base = urb->transfer_buffer;
2112 + iov[1].iov_len = urb->transfer_buffer_length;
2113 + txsize += urb->transfer_buffer_length;
2114 + }
2115 +
2116 + /* 3. setup iso_packet_descriptor */
2117 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2118 + ssize_t len = 0;
2119 +
2120 + iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
2121 + if (!iso_buffer) {
2122 + usbip_event_add(&vdev->ud,
2123 + SDEV_EVENT_ERROR_MALLOC);
2124 + return -1;
2125 + }
2126 +
2127 + iov[2].iov_base = iso_buffer;
2128 + iov[2].iov_len = len;
2129 + txsize += len;
2130 + }
2131 +
2132 + ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 3, txsize);
2133 + if (ret != txsize) {
2134 + uerr("sendmsg failed!, retval %d for %zd\n", ret,
2135 + txsize);
2136 + kfree(iso_buffer);
2137 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
2138 + return -1;
2139 + }
2140 +
2141 + kfree(iso_buffer);
2142 + dbg_vhci_tx("send txdata\n");
2143 +
2144 + total_size += txsize;
2145 + }
2146 +
2147 + return total_size;
2148 +}
2149 +
2150 +
2151 +/*-------------------------------------------------------------------------*/
2152 +
2153 +static struct vhci_unlink *dequeue_from_unlink_tx(struct vhci_device *vdev)
2154 +{
2155 + unsigned long flags;
2156 + struct vhci_unlink *unlink, *tmp;
2157 +
2158 + spin_lock_irqsave(&vdev->priv_lock, flags);
2159 +
2160 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
2161 + list_move_tail(&unlink->list, &vdev->unlink_rx);
2162 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
2163 + return unlink;
2164 + }
2165 +
2166 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
2167 +
2168 + return NULL;
2169 +}
2170 +
2171 +static int vhci_send_cmd_unlink(struct vhci_device *vdev)
2172 +{
2173 + struct vhci_unlink *unlink = NULL;
2174 +
2175 + struct msghdr msg;
2176 + struct kvec iov[3];
2177 + size_t txsize;
2178 +
2179 + size_t total_size = 0;
2180 +
2181 + while ((unlink = dequeue_from_unlink_tx(vdev)) != NULL) {
2182 + int ret;
2183 + struct usbip_header pdu_header;
2184 +
2185 + txsize = 0;
2186 + memset(&pdu_header, 0, sizeof(pdu_header));
2187 + memset(&msg, 0, sizeof(msg));
2188 + memset(&iov, 0, sizeof(iov));
2189 +
2190 + dbg_vhci_tx("setup cmd unlink, %lu \n", unlink->seqnum);
2191 +
2192 +
2193 + /* 1. setup usbip_header */
2194 + pdu_header.base.command = USBIP_CMD_UNLINK;
2195 + pdu_header.base.seqnum = unlink->seqnum;
2196 + pdu_header.base.devid = vdev->devid;
2197 + pdu_header.base.ep = 0;
2198 + pdu_header.u.cmd_unlink.seqnum = unlink->unlink_seqnum;
2199 +
2200 + usbip_header_correct_endian(&pdu_header, 1);
2201 +
2202 + iov[0].iov_base = &pdu_header;
2203 + iov[0].iov_len = sizeof(pdu_header);
2204 + txsize += sizeof(pdu_header);
2205 +
2206 + ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 1, txsize);
2207 + if (ret != txsize) {
2208 + uerr("sendmsg failed!, retval %d for %zd\n", ret,
2209 + txsize);
2210 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
2211 + return -1;
2212 + }
2213 +
2214 +
2215 + dbg_vhci_tx("send txdata\n");
2216 +
2217 + total_size += txsize;
2218 + }
2219 +
2220 + return total_size;
2221 +}
2222 +
2223 +
2224 +/*-------------------------------------------------------------------------*/
2225 +
2226 +void vhci_tx_loop(struct usbip_task *ut)
2227 +{
2228 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
2229 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
2230 +
2231 + while (1) {
2232 + if (signal_pending(current)) {
2233 + uinfo("vhci_tx signal catched\n");
2234 + break;
2235 + }
2236 +
2237 + if (vhci_send_cmd_submit(vdev) < 0)
2238 + break;
2239 +
2240 + if (vhci_send_cmd_unlink(vdev) < 0)
2241 + break;
2242 +
2243 + wait_event_interruptible(vdev->waitq_tx,
2244 + (!list_empty(&vdev->priv_tx) ||
2245 + !list_empty(&vdev->unlink_tx)));
2246 +
2247 + dbg_vhci_tx("pending urbs ?, now wake up\n");
2248 + }
2249 +}
2250 --
2251 1.6.0.2
2252