blob: dca9bf11f0c2e49a5e299867b2e771fa754dc200 [file] [log] [blame]
Takahiro Hirofuchi04679b32008-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 <linux/init.h>
21#include <linux/kernel.h>
Arnd Bergmann9720b4b2011-03-02 00:13:05 +010022#include <linux/kthread.h>
matt mooney7aaacb42011-05-11 22:33:43 -070023#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060026
27#include "usbip_common.h"
28#include "vhci.h"
29
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060030#define DRIVER_AUTHOR "Takahiro Hirofuchi"
matt mooney64e62422011-05-11 22:33:44 -070031#define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060032
33/*
34 * TODO
35 * - update root hub emulation
36 * - move the emulation code to userland ?
37 * porting to other operating systems
38 * minimize kernel code
39 * - add suspend/resume code
40 * - clean up everything
41 */
42
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060043/* See usb gadget dummy hcd */
44
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060045static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
46static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
matt mooney071d1d42011-05-06 03:47:50 -070047 u16 wIndex, char *buff, u16 wLength);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060048static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
matt mooney071d1d42011-05-06 03:47:50 -070049 gfp_t mem_flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060050static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
51static int vhci_start(struct usb_hcd *vhci_hcd);
52static void vhci_stop(struct usb_hcd *hcd);
53static int vhci_get_frame_number(struct usb_hcd *hcd);
54
55static const char driver_name[] = "vhci_hcd";
Robert P. J. Dayf5e08ca72009-11-13 15:13:43 -050056static const char driver_desc[] = "USB/IP Virtual Host Controller";
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060057
58struct vhci_hcd *the_controller;
59
matt mooney071d1d42011-05-06 03:47:50 -070060static const char * const bit_desc[] = {
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060061 "CONNECTION", /*0*/
62 "ENABLE", /*1*/
63 "SUSPEND", /*2*/
64 "OVER_CURRENT", /*3*/
65 "RESET", /*4*/
matt mooney071d1d42011-05-06 03:47:50 -070066 "R5", /*5*/
67 "R6", /*6*/
68 "R7", /*7*/
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060069 "POWER", /*8*/
70 "LOWSPEED", /*9*/
71 "HIGHSPEED", /*10*/
72 "PORT_TEST", /*11*/
73 "INDICATOR", /*12*/
matt mooney071d1d42011-05-06 03:47:50 -070074 "R13", /*13*/
75 "R14", /*14*/
76 "R15", /*15*/
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060077 "C_CONNECTION", /*16*/
78 "C_ENABLE", /*17*/
79 "C_SUSPEND", /*18*/
80 "C_OVER_CURRENT", /*19*/
81 "C_RESET", /*20*/
matt mooney071d1d42011-05-06 03:47:50 -070082 "R21", /*21*/
83 "R22", /*22*/
84 "R23", /*23*/
85 "R24", /*24*/
86 "R25", /*25*/
87 "R26", /*26*/
88 "R27", /*27*/
89 "R28", /*28*/
90 "R29", /*29*/
91 "R30", /*30*/
92 "R31", /*31*/
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060093};
94
Márton Németh4a3ca2b2011-06-14 08:28:26 +020095static void dump_port_status_diff(u32 prev_status, u32 new_status)
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060096{
97 int i = 0;
Márton Németh4a3ca2b2011-06-14 08:28:26 +020098 u32 bit = 1;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060099
Márton Németh4a3ca2b2011-06-14 08:28:26 +0200100 pr_debug("status prev -> new: %08x -> %08x\n", prev_status, new_status);
101 while (bit) {
102 u32 prev = prev_status & bit;
103 u32 new = new_status & bit;
104 char change;
105
106 if (!prev && new)
107 change = '+';
108 else if (prev && !new)
109 change = '-';
110 else
111 change = ' ';
112
113 if (prev || new)
114 pr_debug(" %c%s\n", change, bit_desc[i]);
115 bit <<= 1;
116 i++;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600117 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700118 pr_debug("\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600119}
120
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600121void rh_port_connect(int rhport, enum usb_device_speed speed)
122{
123 unsigned long flags;
124
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600125 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600126
127 spin_lock_irqsave(&the_controller->lock, flags);
128
129 the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
130 | (1 << USB_PORT_FEAT_C_CONNECTION);
131
132 switch (speed) {
133 case USB_SPEED_HIGH:
134 the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED;
135 break;
136 case USB_SPEED_LOW:
137 the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED;
138 break;
139 default:
140 break;
141 }
142
143 /* spin_lock(&the_controller->vdev[rhport].ud.lock);
144 * the_controller->vdev[rhport].ud.status = VDEV_CONNECT;
145 * spin_unlock(&the_controller->vdev[rhport].ud.lock); */
146
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600147 spin_unlock_irqrestore(&the_controller->lock, flags);
148
149 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
150}
151
152void rh_port_disconnect(int rhport)
153{
154 unsigned long flags;
155
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600156 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600157
158 spin_lock_irqsave(&the_controller->lock, flags);
159 /* stop_activity(dum, driver); */
160 the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION;
161 the_controller->port_status[rhport] |=
162 (1 << USB_PORT_FEAT_C_CONNECTION);
163
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600164 /* not yet complete the disconnection
165 * spin_lock(&vdev->ud.lock);
166 * vdev->ud.status = VHC_ST_DISCONNECT;
167 * spin_unlock(&vdev->ud.lock); */
168
169 spin_unlock_irqrestore(&the_controller->lock, flags);
Max Vozeler0c9a32f2010-09-21 17:31:40 +0200170 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600171}
172
matt mooney071d1d42011-05-06 03:47:50 -0700173#define PORT_C_MASK \
174 ((USB_PORT_STAT_C_CONNECTION \
175 | USB_PORT_STAT_C_ENABLE \
176 | USB_PORT_STAT_C_SUSPEND \
177 | USB_PORT_STAT_C_OVERCURRENT \
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600178 | USB_PORT_STAT_C_RESET) << 16)
179
180/*
181 * This function is almostly the same as dummy_hcd.c:dummy_hub_status() without
182 * suspend/resume support. But, it is modified to provide multiple ports.
183 *
184 * @buf: a bitmap to show which port status has been changed.
185 * bit 0: reserved or used for another purpose?
186 * bit 1: the status of port 0 has been changed.
187 * bit 2: the status of port 1 has been changed.
188 * ...
189 * bit 7: the status of port 6 has been changed.
190 * bit 8: the status of port 7 has been changed.
191 * ...
192 * bit 15: the status of port 14 has been changed.
193 *
194 * So, the maximum number of ports is 31 ( port 0 to port 30) ?
195 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300196 * The return value is the actual transferred length in byte. If nothing has
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600197 * been changed, return 0. In the case that the number of ports is less than or
198 * equal to 6 (VHCI_NPORTS==7), return 1.
199 *
200 */
201static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
202{
203 struct vhci_hcd *vhci;
204 unsigned long flags;
205 int retval = 0;
206
207 /* the enough buffer is allocated according to USB_MAXCHILDREN */
208 unsigned long *event_bits = (unsigned long *) buf;
209 int rhport;
210 int changed = 0;
211
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600212 *event_bits = 0;
213
214 vhci = hcd_to_vhci(hcd);
215
216 spin_lock_irqsave(&vhci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -0400217 if (!HCD_HW_ACCESSIBLE(hcd)) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600218 usbip_dbg_vhci_rh("hw accessible flag in on?\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600219 goto done;
220 }
221
222 /* check pseudo status register for each port */
223 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
224 if ((vhci->port_status[rhport] & PORT_C_MASK)) {
225 /* The status of a port has been changed, */
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600226 usbip_dbg_vhci_rh("port %d is changed\n", rhport);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600227
228 *event_bits |= 1 << (rhport + 1);
229 changed = 1;
230 }
231 }
232
matt mooney1a4b6f62011-05-19 16:47:32 -0700233 pr_info("changed %d\n", changed);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600234
235 if (hcd->state == HC_STATE_SUSPENDED)
236 usb_hcd_resume_root_hub(hcd);
237
238 if (changed)
239 retval = 1 + (VHCI_NPORTS / 8);
240 else
241 retval = 0;
242
243done:
244 spin_unlock_irqrestore(&vhci->lock, flags);
245 return retval;
246}
247
248/* See hub_configure in hub.c */
249static inline void hub_descriptor(struct usb_hub_descriptor *desc)
250{
251 memset(desc, 0, sizeof(*desc));
252 desc->bDescriptorType = 0x29;
253 desc->bDescLength = 9;
254 desc->wHubCharacteristics = (__force __u16)
255 (__constant_cpu_to_le16(0x0001));
256 desc->bNbrPorts = VHCI_NPORTS;
John Youndbe79bb2001-09-17 00:00:00 -0700257 desc->u.hs.DeviceRemovable[0] = 0xff;
258 desc->u.hs.DeviceRemovable[1] = 0xff;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600259}
260
261static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
262 u16 wIndex, char *buf, u16 wLength)
263{
264 struct vhci_hcd *dum;
265 int retval = 0;
266 unsigned long flags;
267 int rhport;
268
269 u32 prev_port_status[VHCI_NPORTS];
270
Alan Stern541c7d42010-06-22 16:39:10 -0400271 if (!HCD_HW_ACCESSIBLE(hcd))
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600272 return -ETIMEDOUT;
273
274 /*
275 * NOTE:
276 * wIndex shows the port number and begins from 1.
277 */
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600278 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
matt mooney071d1d42011-05-06 03:47:50 -0700279 wIndex);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600280 if (wIndex > VHCI_NPORTS)
matt mooney1a4b6f62011-05-19 16:47:32 -0700281 pr_err("invalid port number %d\n", wIndex);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600282 rhport = ((__u8)(wIndex & 0x00ff)) - 1;
283
284 dum = hcd_to_vhci(hcd);
285
286 spin_lock_irqsave(&dum->lock, flags);
287
288 /* store old status and compare now and old later */
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600289 if (usbip_dbg_flag_vhci_rh) {
Márton Németh6f480bf2011-06-13 23:30:09 +0200290 memcpy(prev_port_status, dum->port_status,
291 sizeof(prev_port_status));
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600292 }
293
294 switch (typeReq) {
295 case ClearHubFeature:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600296 usbip_dbg_vhci_rh(" ClearHubFeature\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600297 break;
298 case ClearPortFeature:
299 switch (wValue) {
300 case USB_PORT_FEAT_SUSPEND:
301 if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
302 /* 20msec signaling */
303 dum->resuming = 1;
304 dum->re_timeout =
305 jiffies + msecs_to_jiffies(20);
306 }
307 break;
308 case USB_PORT_FEAT_POWER:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600309 usbip_dbg_vhci_rh(" ClearPortFeature: "
matt mooney071d1d42011-05-06 03:47:50 -0700310 "USB_PORT_FEAT_POWER\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600311 dum->port_status[rhport] = 0;
312 /* dum->address = 0; */
313 /* dum->hdev = 0; */
314 dum->resuming = 0;
315 break;
316 case USB_PORT_FEAT_C_RESET:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600317 usbip_dbg_vhci_rh(" ClearPortFeature: "
matt mooney071d1d42011-05-06 03:47:50 -0700318 "USB_PORT_FEAT_C_RESET\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600319 switch (dum->vdev[rhport].speed) {
320 case USB_SPEED_HIGH:
321 dum->port_status[rhport] |=
matt mooney071d1d42011-05-06 03:47:50 -0700322 USB_PORT_STAT_HIGH_SPEED;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600323 break;
324 case USB_SPEED_LOW:
325 dum->port_status[rhport] |=
matt mooney071d1d42011-05-06 03:47:50 -0700326 USB_PORT_STAT_LOW_SPEED;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600327 break;
328 default:
329 break;
330 }
331 default:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600332 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
matt mooney071d1d42011-05-06 03:47:50 -0700333 wValue);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600334 dum->port_status[rhport] &= ~(1 << wValue);
matt mooney49aecef2011-05-06 03:47:54 -0700335 break;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600336 }
337 break;
338 case GetHubDescriptor:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600339 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600340 hub_descriptor((struct usb_hub_descriptor *) buf);
341 break;
342 case GetHubStatus:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600343 usbip_dbg_vhci_rh(" GetHubStatus\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600344 *(__le32 *) buf = __constant_cpu_to_le32(0);
345 break;
346 case GetPortStatus:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600347 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600348 if (wIndex > VHCI_NPORTS || wIndex < 1) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700349 pr_err("invalid port number %d\n", wIndex);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600350 retval = -EPIPE;
351 }
352
353 /* we do no care of resume. */
354
355 /* whoever resets or resumes must GetPortStatus to
356 * complete it!!
357 * */
358 if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600359 dum->port_status[rhport] |=
matt mooney87352762011-05-19 21:36:56 -0700360 (1 << USB_PORT_FEAT_C_SUSPEND);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600361 dum->port_status[rhport] &=
matt mooney87352762011-05-19 21:36:56 -0700362 ~(1 << USB_PORT_FEAT_SUSPEND);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600363 dum->resuming = 0;
364 dum->re_timeout = 0;
365 /* if (dum->driver && dum->driver->resume) {
366 * spin_unlock (&dum->lock);
367 * dum->driver->resume (&dum->gadget);
368 * spin_lock (&dum->lock);
369 * } */
370 }
371
372 if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
matt mooney071d1d42011-05-06 03:47:50 -0700373 0 && time_after(jiffies, dum->re_timeout)) {
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600374 dum->port_status[rhport] |=
matt mooney071d1d42011-05-06 03:47:50 -0700375 (1 << USB_PORT_FEAT_C_RESET);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600376 dum->port_status[rhport] &=
matt mooney071d1d42011-05-06 03:47:50 -0700377 ~(1 << USB_PORT_FEAT_RESET);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600378 dum->re_timeout = 0;
379
380 if (dum->vdev[rhport].ud.status ==
matt mooney071d1d42011-05-06 03:47:50 -0700381 VDEV_ST_NOTASSIGNED) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600382 usbip_dbg_vhci_rh(" enable rhport %d "
matt mooney071d1d42011-05-06 03:47:50 -0700383 "(status %u)\n",
384 rhport,
385 dum->vdev[rhport].ud.status);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600386 dum->port_status[rhport] |=
matt mooney071d1d42011-05-06 03:47:50 -0700387 USB_PORT_STAT_ENABLE;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600388 }
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600389 }
390 ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
matt mooney071d1d42011-05-06 03:47:50 -0700391 ((u16 *) buf)[1] = cpu_to_le16(dum->port_status[rhport] >> 16);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600392
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600393 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
matt mooney071d1d42011-05-06 03:47:50 -0700394 ((u16 *)buf)[1]);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600395 break;
396 case SetHubFeature:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600397 usbip_dbg_vhci_rh(" SetHubFeature\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600398 retval = -EPIPE;
399 break;
400 case SetPortFeature:
401 switch (wValue) {
402 case USB_PORT_FEAT_SUSPEND:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600403 usbip_dbg_vhci_rh(" SetPortFeature: "
matt mooney071d1d42011-05-06 03:47:50 -0700404 "USB_PORT_FEAT_SUSPEND\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600405 break;
406 case USB_PORT_FEAT_RESET:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600407 usbip_dbg_vhci_rh(" SetPortFeature: "
matt mooney071d1d42011-05-06 03:47:50 -0700408 "USB_PORT_FEAT_RESET\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600409 /* if it's already running, disconnect first */
410 if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
411 dum->port_status[rhport] &=
matt mooney071d1d42011-05-06 03:47:50 -0700412 ~(USB_PORT_STAT_ENABLE |
413 USB_PORT_STAT_LOW_SPEED |
414 USB_PORT_STAT_HIGH_SPEED);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600415 /* FIXME test that code path! */
416 }
417 /* 50msec reset signaling */
418 dum->re_timeout = jiffies + msecs_to_jiffies(50);
419
420 /* FALLTHROUGH */
421 default:
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600422 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
matt mooney071d1d42011-05-06 03:47:50 -0700423 wValue);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600424 dum->port_status[rhport] |= (1 << wValue);
matt mooney49aecef2011-05-06 03:47:54 -0700425 break;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600426 }
427 break;
428
429 default:
matt mooney1a4b6f62011-05-19 16:47:32 -0700430 pr_err("default: no such request\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600431 /* dev_dbg (hardware,
432 * "hub control req%04x v%04x i%04x l%d\n",
433 * typeReq, wValue, wIndex, wLength); */
434
435 /* "protocol stall" on error */
436 retval = -EPIPE;
437 }
438
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600439 if (usbip_dbg_flag_vhci_rh) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700440 pr_debug("port %d\n", rhport);
Márton Némethbfb4ce22011-06-13 23:47:39 +0200441 /* Only dump valid port status */
442 if (rhport >= 0) {
Márton Németh4a3ca2b2011-06-14 08:28:26 +0200443 dump_port_status_diff(prev_port_status[rhport],
444 dum->port_status[rhport]);
Márton Némethbfb4ce22011-06-13 23:47:39 +0200445 }
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600446 }
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600447 usbip_dbg_vhci_rh(" bye\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600448
449 spin_unlock_irqrestore(&dum->lock, flags);
450
451 return retval;
452}
453
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600454static struct vhci_device *get_vdev(struct usb_device *udev)
455{
456 int i;
457
458 if (!udev)
459 return NULL;
460
461 for (i = 0; i < VHCI_NPORTS; i++)
462 if (the_controller->vdev[i].udev == udev)
463 return port_to_vdev(i);
464
465 return NULL;
466}
467
468static void vhci_tx_urb(struct urb *urb)
469{
470 struct vhci_device *vdev = get_vdev(urb->dev);
471 struct vhci_priv *priv;
472 unsigned long flag;
473
474 if (!vdev) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700475 pr_err("could not get virtual device");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600476 /* BUG(); */
477 return;
478 }
479
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600480 priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
481
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600482 spin_lock_irqsave(&vdev->priv_lock, flag);
483
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600484 if (!priv) {
485 dev_err(&urb->dev->dev, "malloc vhci_priv\n");
486 spin_unlock_irqrestore(&vdev->priv_lock, flag);
487 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
488 return;
489 }
490
491 priv->seqnum = atomic_inc_return(&the_controller->seqnum);
492 if (priv->seqnum == 0xffff)
matt mooney1a4b6f62011-05-19 16:47:32 -0700493 dev_info(&urb->dev->dev, "seqnum max\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600494
495 priv->vdev = vdev;
496 priv->urb = urb;
497
498 urb->hcpriv = (void *) priv;
499
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600500 list_add_tail(&priv->list, &vdev->priv_tx);
501
502 wake_up(&vdev->waitq_tx);
503 spin_unlock_irqrestore(&vdev->priv_lock, flag);
504}
505
506static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
507 gfp_t mem_flags)
508{
509 struct device *dev = &urb->dev->dev;
510 int ret = 0;
511 unsigned long flags;
Max Vozeler6d212152011-01-12 15:02:02 +0200512 struct vhci_device *vdev;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600513
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600514 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
matt mooney071d1d42011-05-06 03:47:50 -0700515 hcd, urb, mem_flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600516
517 /* patch to usb_sg_init() is in 2.5.60 */
518 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
519
520 spin_lock_irqsave(&the_controller->lock, flags);
521
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600522 if (urb->status != -EINPROGRESS) {
523 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
524 spin_unlock_irqrestore(&the_controller->lock, flags);
525 return urb->status;
526 }
527
Max Vozeler01446ef2011-01-12 15:02:05 +0200528 vdev = port_to_vdev(urb->dev->portnum-1);
Max Vozeler6d212152011-01-12 15:02:02 +0200529
530 /* refuse enqueue for dead connection */
531 spin_lock(&vdev->ud.lock);
matt mooney071d1d42011-05-06 03:47:50 -0700532 if (vdev->ud.status == VDEV_ST_NULL ||
533 vdev->ud.status == VDEV_ST_ERROR) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700534 dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
Max Vozeler6d212152011-01-12 15:02:02 +0200535 spin_unlock(&vdev->ud.lock);
536 spin_unlock_irqrestore(&the_controller->lock, flags);
537 return -ENODEV;
538 }
539 spin_unlock(&vdev->ud.lock);
540
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600541 ret = usb_hcd_link_urb_to_ep(hcd, urb);
542 if (ret)
543 goto no_need_unlink;
544
545 /*
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600546 * The enumeration process is as follows;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600547 *
548 * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
549 * to get max packet length of default pipe
550 *
551 * 2. Set_Address request to DevAddr(0) EndPoint(0)
552 *
553 */
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600554 if (usb_pipedevice(urb->pipe) == 0) {
555 __u8 type = usb_pipetype(urb->pipe);
556 struct usb_ctrlrequest *ctrlreq =
matt mooney071d1d42011-05-06 03:47:50 -0700557 (struct usb_ctrlrequest *) urb->setup_packet;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600558
559 if (type != PIPE_CONTROL || !ctrlreq) {
560 dev_err(dev, "invalid request to devnum 0\n");
Shan Weia7cd5822009-07-24 16:57:35 +0800561 ret = -EINVAL;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600562 goto no_need_xmit;
563 }
564
565 switch (ctrlreq->bRequest) {
566 case USB_REQ_SET_ADDRESS:
567 /* set_address may come when a device is reset */
568 dev_info(dev, "SetAddress Request (%d) to port %d\n",
569 ctrlreq->wValue, vdev->rhport);
570
Max Vozeler7606ee82011-01-12 15:02:00 +0200571 if (vdev->udev)
572 usb_put_dev(vdev->udev);
573 vdev->udev = usb_get_dev(urb->dev);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600574
575 spin_lock(&vdev->ud.lock);
576 vdev->ud.status = VDEV_ST_USED;
577 spin_unlock(&vdev->ud.lock);
578
579 if (urb->status == -EINPROGRESS) {
580 /* This request is successfully completed. */
581 /* If not -EINPROGRESS, possibly unlinked. */
582 urb->status = 0;
583 }
584
585 goto no_need_xmit;
586
587 case USB_REQ_GET_DESCRIPTOR:
588 if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600589 usbip_dbg_vhci_hc("Not yet?: "
matt mooney071d1d42011-05-06 03:47:50 -0700590 "Get_Descriptor to device 0 "
591 "(get max pipe size)\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600592
Max Vozeler7606ee82011-01-12 15:02:00 +0200593 if (vdev->udev)
594 usb_put_dev(vdev->udev);
595 vdev->udev = usb_get_dev(urb->dev);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600596 goto out;
597
598 default:
599 /* NOT REACHED */
600 dev_err(dev, "invalid request to devnum 0 bRequest %u, "
601 "wValue %u\n", ctrlreq->bRequest,
602 ctrlreq->wValue);
603 ret = -EINVAL;
604 goto no_need_xmit;
605 }
606
607 }
608
609out:
610 vhci_tx_urb(urb);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600611 spin_unlock_irqrestore(&the_controller->lock, flags);
612
613 return 0;
614
615no_need_xmit:
616 usb_hcd_unlink_urb_from_ep(hcd, urb);
617no_need_unlink:
618 spin_unlock_irqrestore(&the_controller->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600619 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
Shan Weia7cd5822009-07-24 16:57:35 +0800620 return ret;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600621}
622
623/*
624 * vhci_rx gives back the urb after receiving the reply of the urb. If an
625 * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
626 * back its urb. For the driver unlinking the urb, the content of the urb is
627 * not important, but the calling to its completion handler is important; the
628 * completion of unlinking is notified by the completion handler.
629 *
630 *
631 * CLIENT SIDE
632 *
633 * - When vhci_hcd receives RET_SUBMIT,
634 *
635 * - case 1a). the urb of the pdu is not unlinking.
636 * - normal case
637 * => just give back the urb
638 *
639 * - case 1b). the urb of the pdu is unlinking.
640 * - usbip.ko will return a reply of the unlinking request.
641 * => give back the urb now and go to case 2b).
642 *
643 * - When vhci_hcd receives RET_UNLINK,
644 *
645 * - case 2a). a submit request is still pending in vhci_hcd.
646 * - urb was really pending in usbip.ko and urb_unlink_urb() was
647 * completed there.
648 * => free a pending submit request
649 * => notify unlink completeness by giving back the urb
650 *
651 * - case 2b). a submit request is *not* pending in vhci_hcd.
652 * - urb was already given back to the core driver.
653 * => do not give back the urb
654 *
655 *
656 * SERVER SIDE
657 *
658 * - When usbip receives CMD_UNLINK,
659 *
660 * - case 3a). the urb of the unlink request is now in submission.
661 * => do usb_unlink_urb().
662 * => after the unlink is completed, send RET_UNLINK.
663 *
664 * - case 3b). the urb of the unlink request is not in submission.
665 * - may be already completed or never be received
666 * => send RET_UNLINK
667 *
668 */
669static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
670{
671 unsigned long flags;
672 struct vhci_priv *priv;
673 struct vhci_device *vdev;
674
matt mooney1a4b6f62011-05-19 16:47:32 -0700675 pr_info("dequeue a urb %p\n", urb);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600676
677 spin_lock_irqsave(&the_controller->lock, flags);
678
679 priv = urb->hcpriv;
680 if (!priv) {
681 /* URB was never linked! or will be soon given back by
682 * vhci_rx. */
683 spin_unlock_irqrestore(&the_controller->lock, flags);
684 return 0;
685 }
686
687 {
688 int ret = 0;
689 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
690 if (ret) {
691 spin_unlock_irqrestore(&the_controller->lock, flags);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600692 return ret;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600693 }
694 }
695
696 /* send unlink request here? */
697 vdev = priv->vdev;
698
699 if (!vdev->ud.tcp_socket) {
700 /* tcp connection is closed */
701 unsigned long flags2;
702
703 spin_lock_irqsave(&vdev->priv_lock, flags2);
704
matt mooney1a4b6f62011-05-19 16:47:32 -0700705 pr_info("device %p seems to be disconnected\n", vdev);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600706 list_del(&priv->list);
707 kfree(priv);
708 urb->hcpriv = NULL;
709
710 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
711
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600712 /*
713 * If tcp connection is alive, we have sent CMD_UNLINK.
714 * vhci_rx will receive RET_UNLINK and give back the URB.
715 * Otherwise, we give back it here.
716 */
matt mooney1a4b6f62011-05-19 16:47:32 -0700717 pr_info("gives back urb %p\n", urb);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600718
719 usb_hcd_unlink_urb_from_ep(hcd, urb);
720
721 spin_unlock_irqrestore(&the_controller->lock, flags);
722 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
matt mooney071d1d42011-05-06 03:47:50 -0700723 urb->status);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600724 spin_lock_irqsave(&the_controller->lock, flags);
725
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600726 } else {
727 /* tcp connection is alive */
728 unsigned long flags2;
729 struct vhci_unlink *unlink;
730
731 spin_lock_irqsave(&vdev->priv_lock, flags2);
732
733 /* setup CMD_UNLINK pdu */
734 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
735 if (!unlink) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700736 pr_err("malloc vhci_unlink\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600737 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
738 spin_unlock_irqrestore(&the_controller->lock, flags);
739 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
740 return -ENOMEM;
741 }
742
743 unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
744 if (unlink->seqnum == 0xffff)
matt mooney1a4b6f62011-05-19 16:47:32 -0700745 pr_info("seqnum max\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600746
747 unlink->unlink_seqnum = priv->seqnum;
748
matt mooney1a4b6f62011-05-19 16:47:32 -0700749 pr_info("device %p seems to be still connected\n", vdev);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600750
751 /* send cmd_unlink and try to cancel the pending URB in the
752 * peer */
753 list_add_tail(&unlink->list, &vdev->unlink_tx);
754 wake_up(&vdev->waitq_tx);
755
756 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
757 }
758
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600759 spin_unlock_irqrestore(&the_controller->lock, flags);
760
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600761 usbip_dbg_vhci_hc("leave\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600762 return 0;
763}
764
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600765static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
766{
767 struct vhci_unlink *unlink, *tmp;
768
769 spin_lock(&vdev->priv_lock);
770
771 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700772 pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600773 list_del(&unlink->list);
774 kfree(unlink);
775 }
776
777 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
Max Vozelerb92a5e22011-01-12 15:02:01 +0200778 struct urb *urb;
779
780 /* give back URB of unanswered unlink request */
matt mooney1a4b6f62011-05-19 16:47:32 -0700781 pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
Max Vozelerb92a5e22011-01-12 15:02:01 +0200782
783 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
784 if (!urb) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700785 pr_info("the urb (seqnum %lu) was already given back\n",
786 unlink->unlink_seqnum);
Max Vozelerb92a5e22011-01-12 15:02:01 +0200787 list_del(&unlink->list);
788 kfree(unlink);
789 continue;
790 }
791
792 urb->status = -ENODEV;
793
794 spin_lock(&the_controller->lock);
795 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
796 spin_unlock(&the_controller->lock);
797
matt mooney071d1d42011-05-06 03:47:50 -0700798 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
799 urb->status);
Max Vozelerb92a5e22011-01-12 15:02:01 +0200800
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600801 list_del(&unlink->list);
802 kfree(unlink);
803 }
804
805 spin_unlock(&vdev->priv_lock);
806}
807
808/*
809 * The important thing is that only one context begins cleanup.
810 * This is why error handling and cleanup become simple.
811 * We do not want to consider race condition as possible.
812 */
813static void vhci_shutdown_connection(struct usbip_device *ud)
814{
815 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
816
817 /* need this? see stub_dev.c */
818 if (ud->tcp_socket) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700819 pr_debug("shutdown tcp_socket %p\n", ud->tcp_socket);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600820 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
821 }
822
Arnd Bergmann9720b4b2011-03-02 00:13:05 +0100823 /* kill threads related to this sdev, if v.c. exists */
Tobias Klauser8547d4c2011-06-24 15:48:47 +0200824 if (vdev->ud.tcp_rx && !task_is_dead(vdev->ud.tcp_rx))
Max Vozelerd1b2e952011-04-18 21:44:10 +0200825 kthread_stop(vdev->ud.tcp_rx);
Tobias Klauser8547d4c2011-06-24 15:48:47 +0200826 if (vdev->ud.tcp_tx && !task_is_dead(vdev->ud.tcp_tx))
Max Vozelerd1b2e952011-04-18 21:44:10 +0200827 kthread_stop(vdev->ud.tcp_tx);
Arnd Bergmann9720b4b2011-03-02 00:13:05 +0100828
matt mooney1a4b6f62011-05-19 16:47:32 -0700829 pr_info("stop threads\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600830
831 /* active connection is closed */
832 if (vdev->ud.tcp_socket != NULL) {
833 sock_release(vdev->ud.tcp_socket);
834 vdev->ud.tcp_socket = NULL;
835 }
matt mooney1a4b6f62011-05-19 16:47:32 -0700836 pr_info("release socket\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600837
838 vhci_device_unlink_cleanup(vdev);
839
840 /*
841 * rh_port_disconnect() is a trigger of ...
842 * usb_disable_device():
843 * disable all the endpoints for a USB device.
844 * usb_disable_endpoint():
845 * disable endpoints. pending urbs are unlinked(dequeued).
846 *
847 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
848 * deteched device should release used urbs in a cleanup function(i.e.
849 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
850 * pushed urbs and their private data in this function.
851 *
852 * NOTE: vhci_dequeue() must be considered carefully. When shutdowning
853 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
854 * gives back pushed urbs and frees their private data by request of
855 * the cleanup function of a USB driver. When unlinking a urb with an
856 * active connection, vhci_dequeue() does not give back the urb which
857 * is actually given back by vhci_rx after receiving its return pdu.
858 *
859 */
860 rh_port_disconnect(vdev->rhport);
861
matt mooney1a4b6f62011-05-19 16:47:32 -0700862 pr_info("disconnect device\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600863}
864
865
866static void vhci_device_reset(struct usbip_device *ud)
867{
868 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
869
870 spin_lock(&ud->lock);
871
872 vdev->speed = 0;
873 vdev->devid = 0;
874
Max Vozeler7606ee82011-01-12 15:02:00 +0200875 if (vdev->udev)
876 usb_put_dev(vdev->udev);
877 vdev->udev = NULL;
878
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600879 ud->tcp_socket = NULL;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600880 ud->status = VDEV_ST_NULL;
881
882 spin_unlock(&ud->lock);
883}
884
885static void vhci_device_unusable(struct usbip_device *ud)
886{
887 spin_lock(&ud->lock);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600888 ud->status = VDEV_ST_ERROR;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600889 spin_unlock(&ud->lock);
890}
891
892static void vhci_device_init(struct vhci_device *vdev)
893{
894 memset(vdev, 0, sizeof(*vdev));
895
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600896 vdev->ud.side = USBIP_VHCI;
897 vdev->ud.status = VDEV_ST_NULL;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600898 spin_lock_init(&vdev->ud.lock);
899
900 INIT_LIST_HEAD(&vdev->priv_rx);
901 INIT_LIST_HEAD(&vdev->priv_tx);
902 INIT_LIST_HEAD(&vdev->unlink_tx);
903 INIT_LIST_HEAD(&vdev->unlink_rx);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600904 spin_lock_init(&vdev->priv_lock);
905
906 init_waitqueue_head(&vdev->waitq_tx);
907
908 vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
909 vdev->ud.eh_ops.reset = vhci_device_reset;
910 vdev->ud.eh_ops.unusable = vhci_device_unusable;
911
912 usbip_start_eh(&vdev->ud);
913}
914
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600915static int vhci_start(struct usb_hcd *hcd)
916{
917 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
918 int rhport;
919 int err = 0;
920
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600921 usbip_dbg_vhci_hc("enter vhci_start\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600922
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600923 /* initialize private data of usb_hcd */
924
925 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
926 struct vhci_device *vdev = &vhci->vdev[rhport];
927 vhci_device_init(vdev);
928 vdev->rhport = rhport;
929 }
930
931 atomic_set(&vhci->seqnum, 0);
932 spin_lock_init(&vhci->lock);
933
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600934 hcd->power_budget = 0; /* no limit */
935 hcd->state = HC_STATE_RUNNING;
936 hcd->uses_new_polling = 1;
937
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600938 /* vhci_hcd is now ready to be controlled through sysfs */
939 err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
940 if (err) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700941 pr_err("create sysfs files\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600942 return err;
943 }
944
945 return 0;
946}
947
948static void vhci_stop(struct usb_hcd *hcd)
949{
950 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
951 int rhport = 0;
952
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600953 usbip_dbg_vhci_hc("stop VHCI controller\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600954
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600955 /* 1. remove the userland interface of vhci_hcd */
956 sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
957
958 /* 2. shutdown all the ports of vhci_hcd */
959 for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
960 struct vhci_device *vdev = &vhci->vdev[rhport];
961
962 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
963 usbip_stop_eh(&vdev->ud);
964 }
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600965}
966
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600967static int vhci_get_frame_number(struct usb_hcd *hcd)
968{
matt mooney1a4b6f62011-05-19 16:47:32 -0700969 pr_err("Not yet implemented\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600970 return 0;
971}
972
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600973#ifdef CONFIG_PM
974
975/* FIXME: suspend/resume */
976static int vhci_bus_suspend(struct usb_hcd *hcd)
977{
978 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
979
980 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
981
982 spin_lock_irq(&vhci->lock);
983 /* vhci->rh_state = DUMMY_RH_SUSPENDED;
984 * set_link_state(vhci); */
985 hcd->state = HC_STATE_SUSPENDED;
986 spin_unlock_irq(&vhci->lock);
987
988 return 0;
989}
990
991static int vhci_bus_resume(struct usb_hcd *hcd)
992{
993 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
994 int rc = 0;
995
996 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
997
998 spin_lock_irq(&vhci->lock);
Alan Stern541c7d42010-06-22 16:39:10 -0400999 if (!HCD_HW_ACCESSIBLE(hcd)) {
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001000 rc = -ESHUTDOWN;
1001 } else {
1002 /* vhci->rh_state = DUMMY_RH_RUNNING;
1003 * set_link_state(vhci);
1004 * if (!list_empty(&vhci->urbp_list))
1005 * mod_timer(&vhci->timer, jiffies); */
1006 hcd->state = HC_STATE_RUNNING;
1007 }
1008 spin_unlock_irq(&vhci->lock);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001009
matt mooney87352762011-05-19 21:36:56 -07001010 return rc;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001011}
1012
1013#else
1014
1015#define vhci_bus_suspend NULL
1016#define vhci_bus_resume NULL
1017#endif
1018
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001019static struct hc_driver vhci_hc_driver = {
1020 .description = driver_name,
1021 .product_desc = driver_desc,
1022 .hcd_priv_size = sizeof(struct vhci_hcd),
1023
1024 .flags = HCD_USB2,
1025
1026 .start = vhci_start,
Ruslan Pisarev00512682010-03-15 17:06:44 +02001027 .stop = vhci_stop,
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001028
1029 .urb_enqueue = vhci_urb_enqueue,
1030 .urb_dequeue = vhci_urb_dequeue,
1031
1032 .get_frame_number = vhci_get_frame_number,
1033
1034 .hub_status_data = vhci_hub_status,
1035 .hub_control = vhci_hub_control,
1036 .bus_suspend = vhci_bus_suspend,
1037 .bus_resume = vhci_bus_resume,
1038};
1039
1040static int vhci_hcd_probe(struct platform_device *pdev)
1041{
1042 struct usb_hcd *hcd;
1043 int ret;
1044
Brian G. Merrellb8868e42009-07-21 00:46:13 -06001045 usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001046
1047 /* will be removed */
1048 if (pdev->dev.dma_mask) {
1049 dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
1050 return -EINVAL;
1051 }
1052
1053 /*
1054 * Allocate and initialize hcd.
1055 * Our private data is also allocated automatically.
1056 */
Kay Sieverse9133972008-10-30 01:59:32 +01001057 hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001058 if (!hcd) {
matt mooney1a4b6f62011-05-19 16:47:32 -07001059 pr_err("create hcd failed\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001060 return -ENOMEM;
1061 }
Alan Sterncee6a262011-05-02 14:21:44 -04001062 hcd->has_tt = 1;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001063
1064 /* this is private data for vhci_hcd */
1065 the_controller = hcd_to_vhci(hcd);
1066
1067 /*
1068 * Finish generic HCD structure initialization and register.
1069 * Call the driver's reset() and start() routines.
1070 */
1071 ret = usb_add_hcd(hcd, 0, 0);
1072 if (ret != 0) {
matt mooney1a4b6f62011-05-19 16:47:32 -07001073 pr_err("usb_add_hcd failed %d\n", ret);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001074 usb_put_hcd(hcd);
1075 the_controller = NULL;
1076 return ret;
1077 }
1078
Brian G. Merrellb8868e42009-07-21 00:46:13 -06001079 usbip_dbg_vhci_hc("bye\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001080 return 0;
1081}
1082
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001083static int vhci_hcd_remove(struct platform_device *pdev)
1084{
1085 struct usb_hcd *hcd;
1086
1087 hcd = platform_get_drvdata(pdev);
1088 if (!hcd)
1089 return 0;
1090
1091 /*
1092 * Disconnects the root hub,
1093 * then reverses the effects of usb_add_hcd(),
1094 * invoking the HCD's stop() methods.
1095 */
1096 usb_remove_hcd(hcd);
1097 usb_put_hcd(hcd);
1098 the_controller = NULL;
1099
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001100 return 0;
1101}
1102
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001103#ifdef CONFIG_PM
1104
1105/* what should happen for USB/IP under suspend/resume? */
1106static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1107{
1108 struct usb_hcd *hcd;
1109 int rhport = 0;
1110 int connected = 0;
1111 int ret = 0;
1112
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001113 hcd = platform_get_drvdata(pdev);
1114
1115 spin_lock(&the_controller->lock);
1116
1117 for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
1118 if (the_controller->port_status[rhport] &
matt mooney071d1d42011-05-06 03:47:50 -07001119 USB_PORT_STAT_CONNECTION)
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001120 connected += 1;
1121
1122 spin_unlock(&the_controller->lock);
1123
1124 if (connected > 0) {
matt mooney1a4b6f62011-05-19 16:47:32 -07001125 dev_info(&pdev->dev, "We have %d active connection%s. Do not "
1126 "suspend.\n", connected, (connected == 1 ? "" : "s"));
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001127 ret = -EBUSY;
1128 } else {
matt mooney1a4b6f62011-05-19 16:47:32 -07001129 dev_info(&pdev->dev, "suspend vhci_hcd");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001130 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1131 }
1132
1133 return ret;
1134}
1135
1136static int vhci_hcd_resume(struct platform_device *pdev)
1137{
1138 struct usb_hcd *hcd;
1139
1140 dev_dbg(&pdev->dev, "%s\n", __func__);
1141
1142 hcd = platform_get_drvdata(pdev);
1143 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1144 usb_hcd_poll_rh_status(hcd);
1145
1146 return 0;
1147}
1148
1149#else
1150
1151#define vhci_hcd_suspend NULL
1152#define vhci_hcd_resume NULL
1153
1154#endif
1155
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001156static struct platform_driver vhci_driver = {
1157 .probe = vhci_hcd_probe,
1158 .remove = __devexit_p(vhci_hcd_remove),
1159 .suspend = vhci_hcd_suspend,
1160 .resume = vhci_hcd_resume,
1161 .driver = {
1162 .name = (char *) driver_name,
1163 .owner = THIS_MODULE,
1164 },
1165};
1166
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001167/*
1168 * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1169 * We need to add this virtual device as a platform device arbitrarily:
1170 * 1. platform_device_register()
1171 */
1172static void the_pdev_release(struct device *dev)
1173{
1174 return;
1175}
1176
1177static struct platform_device the_pdev = {
1178 /* should be the same name as driver_name */
1179 .name = (char *) driver_name,
1180 .id = -1,
1181 .dev = {
1182 /* .driver = &vhci_driver, */
1183 .release = the_pdev_release,
1184 },
1185};
1186
matt mooney0392bbb2011-05-19 21:37:06 -07001187static int __init vhci_hcd_init(void)
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001188{
1189 int ret;
1190
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001191 if (usb_disabled())
1192 return -ENODEV;
1193
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001194 ret = platform_driver_register(&vhci_driver);
1195 if (ret < 0)
1196 goto err_driver_register;
1197
1198 ret = platform_device_register(&the_pdev);
1199 if (ret < 0)
1200 goto err_platform_device_register;
1201
matt mooney1a4b6f62011-05-19 16:47:32 -07001202 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001203 return ret;
1204
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001205err_platform_device_register:
1206 platform_driver_unregister(&vhci_driver);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001207err_driver_register:
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001208 return ret;
1209}
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001210
matt mooney0392bbb2011-05-19 21:37:06 -07001211static void __exit vhci_hcd_exit(void)
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001212{
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001213 platform_device_unregister(&the_pdev);
1214 platform_driver_unregister(&vhci_driver);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001215}
matt mooney071d1d42011-05-06 03:47:50 -07001216
matt mooney0392bbb2011-05-19 21:37:06 -07001217module_init(vhci_hcd_init);
1218module_exit(vhci_hcd_exit);
matt mooney071d1d42011-05-06 03:47:50 -07001219
1220MODULE_AUTHOR(DRIVER_AUTHOR);
1221MODULE_DESCRIPTION(DRIVER_DESC);
matt mooney4ce0a412011-05-06 03:47:56 -07001222MODULE_LICENSE("GPL");
matt mooney6973c6f2011-05-11 01:54:14 -07001223MODULE_VERSION(USBIP_VERSION);