]>
Commit | Line | Data |
---|---|---|
04679b34 TH |
1 | /* |
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 | ||
7aaacb43 | 20 | #include <linux/init.h> |
21 | #include <linux/kernel.h> | |
9720b4bc | 22 | #include <linux/kthread.h> |
7aaacb43 | 23 | #include <linux/module.h> |
24 | #include <linux/platform_device.h> | |
25 | #include <linux/slab.h> | |
04679b34 TH |
26 | |
27 | #include "usbip_common.h" | |
28 | #include "vhci.h" | |
29 | ||
04679b34 | 30 | #define DRIVER_AUTHOR "Takahiro Hirofuchi" |
64e62426 | 31 | #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver" |
04679b34 TH |
32 | |
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 | ||
04679b34 TH |
43 | /* See usb gadget dummy hcd */ |
44 | ||
04679b34 TH |
45 | static int vhci_hub_status(struct usb_hcd *hcd, char *buff); |
46 | static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, | |
071d1d47 | 47 | u16 wIndex, char *buff, u16 wLength); |
04679b34 | 48 | static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, |
071d1d47 | 49 | gfp_t mem_flags); |
04679b34 TH |
50 | static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status); |
51 | static int vhci_start(struct usb_hcd *vhci_hcd); | |
52 | static void vhci_stop(struct usb_hcd *hcd); | |
53 | static int vhci_get_frame_number(struct usb_hcd *hcd); | |
54 | ||
55 | static const char driver_name[] = "vhci_hcd"; | |
f5e08ca7 | 56 | static const char driver_desc[] = "USB/IP Virtual Host Controller"; |
04679b34 TH |
57 | |
58 | struct vhci_hcd *the_controller; | |
59 | ||
071d1d47 | 60 | static const char * const bit_desc[] = { |
04679b34 TH |
61 | "CONNECTION", /*0*/ |
62 | "ENABLE", /*1*/ | |
63 | "SUSPEND", /*2*/ | |
64 | "OVER_CURRENT", /*3*/ | |
65 | "RESET", /*4*/ | |
071d1d47 | 66 | "R5", /*5*/ |
67 | "R6", /*6*/ | |
68 | "R7", /*7*/ | |
04679b34 TH |
69 | "POWER", /*8*/ |
70 | "LOWSPEED", /*9*/ | |
71 | "HIGHSPEED", /*10*/ | |
72 | "PORT_TEST", /*11*/ | |
73 | "INDICATOR", /*12*/ | |
071d1d47 | 74 | "R13", /*13*/ |
75 | "R14", /*14*/ | |
76 | "R15", /*15*/ | |
04679b34 TH |
77 | "C_CONNECTION", /*16*/ |
78 | "C_ENABLE", /*17*/ | |
79 | "C_SUSPEND", /*18*/ | |
80 | "C_OVER_CURRENT", /*19*/ | |
81 | "C_RESET", /*20*/ | |
071d1d47 | 82 | "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*/ | |
04679b34 TH |
93 | }; |
94 | ||
04679b34 TH |
95 | static void dump_port_status(u32 status) |
96 | { | |
97 | int i = 0; | |
98 | ||
1a4b6f66 | 99 | pr_debug("status %08x:", status); |
04679b34 TH |
100 | for (i = 0; i < 32; i++) { |
101 | if (status & (1 << i)) | |
1a4b6f66 | 102 | pr_debug(" %s", bit_desc[i]); |
04679b34 | 103 | } |
1a4b6f66 | 104 | pr_debug("\n"); |
04679b34 TH |
105 | } |
106 | ||
04679b34 TH |
107 | void rh_port_connect(int rhport, enum usb_device_speed speed) |
108 | { | |
109 | unsigned long flags; | |
110 | ||
b8868e45 | 111 | usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport); |
04679b34 TH |
112 | |
113 | spin_lock_irqsave(&the_controller->lock, flags); | |
114 | ||
115 | the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION | |
116 | | (1 << USB_PORT_FEAT_C_CONNECTION); | |
117 | ||
118 | switch (speed) { | |
119 | case USB_SPEED_HIGH: | |
120 | the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED; | |
121 | break; | |
122 | case USB_SPEED_LOW: | |
123 | the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED; | |
124 | break; | |
125 | default: | |
126 | break; | |
127 | } | |
128 | ||
129 | /* spin_lock(&the_controller->vdev[rhport].ud.lock); | |
130 | * the_controller->vdev[rhport].ud.status = VDEV_CONNECT; | |
131 | * spin_unlock(&the_controller->vdev[rhport].ud.lock); */ | |
132 | ||
04679b34 TH |
133 | spin_unlock_irqrestore(&the_controller->lock, flags); |
134 | ||
135 | usb_hcd_poll_rh_status(vhci_to_hcd(the_controller)); | |
136 | } | |
137 | ||
138 | void rh_port_disconnect(int rhport) | |
139 | { | |
140 | unsigned long flags; | |
141 | ||
b8868e45 | 142 | usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport); |
04679b34 TH |
143 | |
144 | spin_lock_irqsave(&the_controller->lock, flags); | |
145 | /* stop_activity(dum, driver); */ | |
146 | the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION; | |
147 | the_controller->port_status[rhport] |= | |
148 | (1 << USB_PORT_FEAT_C_CONNECTION); | |
149 | ||
04679b34 TH |
150 | /* not yet complete the disconnection |
151 | * spin_lock(&vdev->ud.lock); | |
152 | * vdev->ud.status = VHC_ST_DISCONNECT; | |
153 | * spin_unlock(&vdev->ud.lock); */ | |
154 | ||
155 | spin_unlock_irqrestore(&the_controller->lock, flags); | |
0c9a32f0 | 156 | usb_hcd_poll_rh_status(vhci_to_hcd(the_controller)); |
04679b34 TH |
157 | } |
158 | ||
071d1d47 | 159 | #define PORT_C_MASK \ |
160 | ((USB_PORT_STAT_C_CONNECTION \ | |
161 | | USB_PORT_STAT_C_ENABLE \ | |
162 | | USB_PORT_STAT_C_SUSPEND \ | |
163 | | USB_PORT_STAT_C_OVERCURRENT \ | |
04679b34 TH |
164 | | USB_PORT_STAT_C_RESET) << 16) |
165 | ||
166 | /* | |
167 | * This function is almostly the same as dummy_hcd.c:dummy_hub_status() without | |
168 | * suspend/resume support. But, it is modified to provide multiple ports. | |
169 | * | |
170 | * @buf: a bitmap to show which port status has been changed. | |
171 | * bit 0: reserved or used for another purpose? | |
172 | * bit 1: the status of port 0 has been changed. | |
173 | * bit 2: the status of port 1 has been changed. | |
174 | * ... | |
175 | * bit 7: the status of port 6 has been changed. | |
176 | * bit 8: the status of port 7 has been changed. | |
177 | * ... | |
178 | * bit 15: the status of port 14 has been changed. | |
179 | * | |
180 | * So, the maximum number of ports is 31 ( port 0 to port 30) ? | |
181 | * | |
25985edc | 182 | * The return value is the actual transferred length in byte. If nothing has |
04679b34 TH |
183 | * been changed, return 0. In the case that the number of ports is less than or |
184 | * equal to 6 (VHCI_NPORTS==7), return 1. | |
185 | * | |
186 | */ | |
187 | static int vhci_hub_status(struct usb_hcd *hcd, char *buf) | |
188 | { | |
189 | struct vhci_hcd *vhci; | |
190 | unsigned long flags; | |
191 | int retval = 0; | |
192 | ||
193 | /* the enough buffer is allocated according to USB_MAXCHILDREN */ | |
194 | unsigned long *event_bits = (unsigned long *) buf; | |
195 | int rhport; | |
196 | int changed = 0; | |
197 | ||
04679b34 TH |
198 | *event_bits = 0; |
199 | ||
200 | vhci = hcd_to_vhci(hcd); | |
201 | ||
202 | spin_lock_irqsave(&vhci->lock, flags); | |
541c7d43 | 203 | if (!HCD_HW_ACCESSIBLE(hcd)) { |
b8868e45 | 204 | usbip_dbg_vhci_rh("hw accessible flag in on?\n"); |
04679b34 TH |
205 | goto done; |
206 | } | |
207 | ||
208 | /* check pseudo status register for each port */ | |
209 | for (rhport = 0; rhport < VHCI_NPORTS; rhport++) { | |
210 | if ((vhci->port_status[rhport] & PORT_C_MASK)) { | |
211 | /* The status of a port has been changed, */ | |
b8868e45 | 212 | usbip_dbg_vhci_rh("port %d is changed\n", rhport); |
04679b34 TH |
213 | |
214 | *event_bits |= 1 << (rhport + 1); | |
215 | changed = 1; | |
216 | } | |
217 | } | |
218 | ||
1a4b6f66 | 219 | pr_info("changed %d\n", changed); |
04679b34 TH |
220 | |
221 | if (hcd->state == HC_STATE_SUSPENDED) | |
222 | usb_hcd_resume_root_hub(hcd); | |
223 | ||
224 | if (changed) | |
225 | retval = 1 + (VHCI_NPORTS / 8); | |
226 | else | |
227 | retval = 0; | |
228 | ||
229 | done: | |
230 | spin_unlock_irqrestore(&vhci->lock, flags); | |
231 | return retval; | |
232 | } | |
233 | ||
234 | /* See hub_configure in hub.c */ | |
235 | static inline void hub_descriptor(struct usb_hub_descriptor *desc) | |
236 | { | |
237 | memset(desc, 0, sizeof(*desc)); | |
238 | desc->bDescriptorType = 0x29; | |
239 | desc->bDescLength = 9; | |
240 | desc->wHubCharacteristics = (__force __u16) | |
241 | (__constant_cpu_to_le16(0x0001)); | |
242 | desc->bNbrPorts = VHCI_NPORTS; | |
dbe79bbe JY |
243 | desc->u.hs.DeviceRemovable[0] = 0xff; |
244 | desc->u.hs.DeviceRemovable[1] = 0xff; | |
04679b34 TH |
245 | } |
246 | ||
247 | static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, | |
248 | u16 wIndex, char *buf, u16 wLength) | |
249 | { | |
250 | struct vhci_hcd *dum; | |
251 | int retval = 0; | |
252 | unsigned long flags; | |
253 | int rhport; | |
254 | ||
255 | u32 prev_port_status[VHCI_NPORTS]; | |
256 | ||
541c7d43 | 257 | if (!HCD_HW_ACCESSIBLE(hcd)) |
04679b34 TH |
258 | return -ETIMEDOUT; |
259 | ||
260 | /* | |
261 | * NOTE: | |
262 | * wIndex shows the port number and begins from 1. | |
263 | */ | |
b8868e45 | 264 | usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue, |
071d1d47 | 265 | wIndex); |
04679b34 | 266 | if (wIndex > VHCI_NPORTS) |
1a4b6f66 | 267 | pr_err("invalid port number %d\n", wIndex); |
04679b34 TH |
268 | rhport = ((__u8)(wIndex & 0x00ff)) - 1; |
269 | ||
270 | dum = hcd_to_vhci(hcd); | |
271 | ||
272 | spin_lock_irqsave(&dum->lock, flags); | |
273 | ||
274 | /* store old status and compare now and old later */ | |
b8868e45 | 275 | if (usbip_dbg_flag_vhci_rh) { |
04679b34 TH |
276 | int i = 0; |
277 | for (i = 0; i < VHCI_NPORTS; i++) | |
278 | prev_port_status[i] = dum->port_status[i]; | |
279 | } | |
280 | ||
281 | switch (typeReq) { | |
282 | case ClearHubFeature: | |
b8868e45 | 283 | usbip_dbg_vhci_rh(" ClearHubFeature\n"); |
04679b34 TH |
284 | break; |
285 | case ClearPortFeature: | |
286 | switch (wValue) { | |
287 | case USB_PORT_FEAT_SUSPEND: | |
288 | if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) { | |
289 | /* 20msec signaling */ | |
290 | dum->resuming = 1; | |
291 | dum->re_timeout = | |
292 | jiffies + msecs_to_jiffies(20); | |
293 | } | |
294 | break; | |
295 | case USB_PORT_FEAT_POWER: | |
b8868e45 | 296 | usbip_dbg_vhci_rh(" ClearPortFeature: " |
071d1d47 | 297 | "USB_PORT_FEAT_POWER\n"); |
04679b34 TH |
298 | dum->port_status[rhport] = 0; |
299 | /* dum->address = 0; */ | |
300 | /* dum->hdev = 0; */ | |
301 | dum->resuming = 0; | |
302 | break; | |
303 | case USB_PORT_FEAT_C_RESET: | |
b8868e45 | 304 | usbip_dbg_vhci_rh(" ClearPortFeature: " |
071d1d47 | 305 | "USB_PORT_FEAT_C_RESET\n"); |
04679b34 TH |
306 | switch (dum->vdev[rhport].speed) { |
307 | case USB_SPEED_HIGH: | |
308 | dum->port_status[rhport] |= | |
071d1d47 | 309 | USB_PORT_STAT_HIGH_SPEED; |
04679b34 TH |
310 | break; |
311 | case USB_SPEED_LOW: | |
312 | dum->port_status[rhport] |= | |
071d1d47 | 313 | USB_PORT_STAT_LOW_SPEED; |
04679b34 TH |
314 | break; |
315 | default: | |
316 | break; | |
317 | } | |
318 | default: | |
b8868e45 | 319 | usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n", |
071d1d47 | 320 | wValue); |
04679b34 | 321 | dum->port_status[rhport] &= ~(1 << wValue); |
49aecefc | 322 | break; |
04679b34 TH |
323 | } |
324 | break; | |
325 | case GetHubDescriptor: | |
b8868e45 | 326 | usbip_dbg_vhci_rh(" GetHubDescriptor\n"); |
04679b34 TH |
327 | hub_descriptor((struct usb_hub_descriptor *) buf); |
328 | break; | |
329 | case GetHubStatus: | |
b8868e45 | 330 | usbip_dbg_vhci_rh(" GetHubStatus\n"); |
04679b34 TH |
331 | *(__le32 *) buf = __constant_cpu_to_le32(0); |
332 | break; | |
333 | case GetPortStatus: | |
b8868e45 | 334 | usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex); |
04679b34 | 335 | if (wIndex > VHCI_NPORTS || wIndex < 1) { |
1a4b6f66 | 336 | pr_err("invalid port number %d\n", wIndex); |
04679b34 TH |
337 | retval = -EPIPE; |
338 | } | |
339 | ||
340 | /* we do no care of resume. */ | |
341 | ||
342 | /* whoever resets or resumes must GetPortStatus to | |
343 | * complete it!! | |
344 | * */ | |
345 | if (dum->resuming && time_after(jiffies, dum->re_timeout)) { | |
04679b34 | 346 | dum->port_status[rhport] |= |
87352760 | 347 | (1 << USB_PORT_FEAT_C_SUSPEND); |
04679b34 | 348 | dum->port_status[rhport] &= |
87352760 | 349 | ~(1 << USB_PORT_FEAT_SUSPEND); |
04679b34 TH |
350 | dum->resuming = 0; |
351 | dum->re_timeout = 0; | |
352 | /* if (dum->driver && dum->driver->resume) { | |
353 | * spin_unlock (&dum->lock); | |
354 | * dum->driver->resume (&dum->gadget); | |
355 | * spin_lock (&dum->lock); | |
356 | * } */ | |
357 | } | |
358 | ||
359 | if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) != | |
071d1d47 | 360 | 0 && time_after(jiffies, dum->re_timeout)) { |
04679b34 | 361 | dum->port_status[rhport] |= |
071d1d47 | 362 | (1 << USB_PORT_FEAT_C_RESET); |
04679b34 | 363 | dum->port_status[rhport] &= |
071d1d47 | 364 | ~(1 << USB_PORT_FEAT_RESET); |
04679b34 TH |
365 | dum->re_timeout = 0; |
366 | ||
367 | if (dum->vdev[rhport].ud.status == | |
071d1d47 | 368 | VDEV_ST_NOTASSIGNED) { |
b8868e45 | 369 | usbip_dbg_vhci_rh(" enable rhport %d " |
071d1d47 | 370 | "(status %u)\n", |
371 | rhport, | |
372 | dum->vdev[rhport].ud.status); | |
04679b34 | 373 | dum->port_status[rhport] |= |
071d1d47 | 374 | USB_PORT_STAT_ENABLE; |
04679b34 TH |
375 | } |
376 | #if 0 | |
377 | if (dum->driver) { | |
04679b34 | 378 | dum->port_status[rhport] |= |
071d1d47 | 379 | USB_PORT_STAT_ENABLE; |
04679b34 TH |
380 | /* give it the best speed we agree on */ |
381 | dum->gadget.speed = dum->driver->speed; | |
382 | dum->gadget.ep0->maxpacket = 64; | |
383 | switch (dum->gadget.speed) { | |
384 | case USB_SPEED_HIGH: | |
385 | dum->port_status[rhport] |= | |
071d1d47 | 386 | USB_PORT_STAT_HIGH_SPEED; |
04679b34 TH |
387 | break; |
388 | case USB_SPEED_LOW: | |
389 | dum->gadget.ep0->maxpacket = 8; | |
390 | dum->port_status[rhport] |= | |
071d1d47 | 391 | USB_PORT_STAT_LOW_SPEED; |
04679b34 TH |
392 | break; |
393 | default: | |
394 | dum->gadget.speed = USB_SPEED_FULL; | |
395 | break; | |
396 | } | |
397 | } | |
398 | #endif | |
04679b34 TH |
399 | } |
400 | ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]); | |
071d1d47 | 401 | ((u16 *) buf)[1] = cpu_to_le16(dum->port_status[rhport] >> 16); |
04679b34 | 402 | |
b8868e45 | 403 | usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0], |
071d1d47 | 404 | ((u16 *)buf)[1]); |
04679b34 TH |
405 | break; |
406 | case SetHubFeature: | |
b8868e45 | 407 | usbip_dbg_vhci_rh(" SetHubFeature\n"); |
04679b34 TH |
408 | retval = -EPIPE; |
409 | break; | |
410 | case SetPortFeature: | |
411 | switch (wValue) { | |
412 | case USB_PORT_FEAT_SUSPEND: | |
b8868e45 | 413 | usbip_dbg_vhci_rh(" SetPortFeature: " |
071d1d47 | 414 | "USB_PORT_FEAT_SUSPEND\n"); |
04679b34 TH |
415 | #if 0 |
416 | dum->port_status[rhport] |= | |
071d1d47 | 417 | (1 << USB_PORT_FEAT_SUSPEND); |
04679b34 TH |
418 | if (dum->driver->suspend) { |
419 | spin_unlock(&dum->lock); | |
420 | dum->driver->suspend(&dum->gadget); | |
421 | spin_lock(&dum->lock); | |
422 | } | |
423 | #endif | |
424 | break; | |
425 | case USB_PORT_FEAT_RESET: | |
b8868e45 | 426 | usbip_dbg_vhci_rh(" SetPortFeature: " |
071d1d47 | 427 | "USB_PORT_FEAT_RESET\n"); |
04679b34 TH |
428 | /* if it's already running, disconnect first */ |
429 | if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) { | |
430 | dum->port_status[rhport] &= | |
071d1d47 | 431 | ~(USB_PORT_STAT_ENABLE | |
432 | USB_PORT_STAT_LOW_SPEED | | |
433 | USB_PORT_STAT_HIGH_SPEED); | |
04679b34 TH |
434 | #if 0 |
435 | if (dum->driver) { | |
436 | dev_dbg(hardware, "disconnect\n"); | |
437 | stop_activity(dum, dum->driver); | |
438 | } | |
439 | #endif | |
440 | ||
441 | /* FIXME test that code path! */ | |
442 | } | |
443 | /* 50msec reset signaling */ | |
444 | dum->re_timeout = jiffies + msecs_to_jiffies(50); | |
445 | ||
446 | /* FALLTHROUGH */ | |
447 | default: | |
b8868e45 | 448 | usbip_dbg_vhci_rh(" SetPortFeature: default %d\n", |
071d1d47 | 449 | wValue); |
04679b34 | 450 | dum->port_status[rhport] |= (1 << wValue); |
49aecefc | 451 | break; |
04679b34 TH |
452 | } |
453 | break; | |
454 | ||
455 | default: | |
1a4b6f66 | 456 | pr_err("default: no such request\n"); |
04679b34 TH |
457 | /* dev_dbg (hardware, |
458 | * "hub control req%04x v%04x i%04x l%d\n", | |
459 | * typeReq, wValue, wIndex, wLength); */ | |
460 | ||
461 | /* "protocol stall" on error */ | |
462 | retval = -EPIPE; | |
463 | } | |
464 | ||
b8868e45 | 465 | if (usbip_dbg_flag_vhci_rh) { |
1a4b6f66 | 466 | pr_debug("port %d\n", rhport); |
bfb4ce20 MN |
467 | /* Only dump valid port status */ |
468 | if (rhport >= 0) { | |
469 | dump_port_status(prev_port_status[rhport]); | |
470 | dump_port_status(dum->port_status[rhport]); | |
471 | } | |
04679b34 | 472 | } |
b8868e45 | 473 | usbip_dbg_vhci_rh(" bye\n"); |
04679b34 TH |
474 | |
475 | spin_unlock_irqrestore(&dum->lock, flags); | |
476 | ||
477 | return retval; | |
478 | } | |
479 | ||
04679b34 TH |
480 | static struct vhci_device *get_vdev(struct usb_device *udev) |
481 | { | |
482 | int i; | |
483 | ||
484 | if (!udev) | |
485 | return NULL; | |
486 | ||
487 | for (i = 0; i < VHCI_NPORTS; i++) | |
488 | if (the_controller->vdev[i].udev == udev) | |
489 | return port_to_vdev(i); | |
490 | ||
491 | return NULL; | |
492 | } | |
493 | ||
494 | static void vhci_tx_urb(struct urb *urb) | |
495 | { | |
496 | struct vhci_device *vdev = get_vdev(urb->dev); | |
497 | struct vhci_priv *priv; | |
498 | unsigned long flag; | |
499 | ||
500 | if (!vdev) { | |
1a4b6f66 | 501 | pr_err("could not get virtual device"); |
04679b34 TH |
502 | /* BUG(); */ |
503 | return; | |
504 | } | |
505 | ||
b8868e45 BM |
506 | priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC); |
507 | ||
04679b34 TH |
508 | spin_lock_irqsave(&vdev->priv_lock, flag); |
509 | ||
04679b34 TH |
510 | if (!priv) { |
511 | dev_err(&urb->dev->dev, "malloc vhci_priv\n"); | |
512 | spin_unlock_irqrestore(&vdev->priv_lock, flag); | |
513 | usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC); | |
514 | return; | |
515 | } | |
516 | ||
517 | priv->seqnum = atomic_inc_return(&the_controller->seqnum); | |
518 | if (priv->seqnum == 0xffff) | |
1a4b6f66 | 519 | dev_info(&urb->dev->dev, "seqnum max\n"); |
04679b34 TH |
520 | |
521 | priv->vdev = vdev; | |
522 | priv->urb = urb; | |
523 | ||
524 | urb->hcpriv = (void *) priv; | |
525 | ||
04679b34 TH |
526 | list_add_tail(&priv->list, &vdev->priv_tx); |
527 | ||
528 | wake_up(&vdev->waitq_tx); | |
529 | spin_unlock_irqrestore(&vdev->priv_lock, flag); | |
530 | } | |
531 | ||
532 | static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, | |
533 | gfp_t mem_flags) | |
534 | { | |
535 | struct device *dev = &urb->dev->dev; | |
536 | int ret = 0; | |
537 | unsigned long flags; | |
6d212153 | 538 | struct vhci_device *vdev; |
04679b34 | 539 | |
b8868e45 | 540 | usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n", |
071d1d47 | 541 | hcd, urb, mem_flags); |
04679b34 TH |
542 | |
543 | /* patch to usb_sg_init() is in 2.5.60 */ | |
544 | BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length); | |
545 | ||
546 | spin_lock_irqsave(&the_controller->lock, flags); | |
547 | ||
04679b34 TH |
548 | if (urb->status != -EINPROGRESS) { |
549 | dev_err(dev, "URB already unlinked!, status %d\n", urb->status); | |
550 | spin_unlock_irqrestore(&the_controller->lock, flags); | |
551 | return urb->status; | |
552 | } | |
553 | ||
01446ef5 | 554 | vdev = port_to_vdev(urb->dev->portnum-1); |
6d212153 MV |
555 | |
556 | /* refuse enqueue for dead connection */ | |
557 | spin_lock(&vdev->ud.lock); | |
071d1d47 | 558 | if (vdev->ud.status == VDEV_ST_NULL || |
559 | vdev->ud.status == VDEV_ST_ERROR) { | |
1a4b6f66 | 560 | dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport); |
6d212153 MV |
561 | spin_unlock(&vdev->ud.lock); |
562 | spin_unlock_irqrestore(&the_controller->lock, flags); | |
563 | return -ENODEV; | |
564 | } | |
565 | spin_unlock(&vdev->ud.lock); | |
566 | ||
04679b34 TH |
567 | ret = usb_hcd_link_urb_to_ep(hcd, urb); |
568 | if (ret) | |
569 | goto no_need_unlink; | |
570 | ||
571 | /* | |
b8868e45 | 572 | * The enumeration process is as follows; |
04679b34 TH |
573 | * |
574 | * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0) | |
575 | * to get max packet length of default pipe | |
576 | * | |
577 | * 2. Set_Address request to DevAddr(0) EndPoint(0) | |
578 | * | |
579 | */ | |
04679b34 TH |
580 | if (usb_pipedevice(urb->pipe) == 0) { |
581 | __u8 type = usb_pipetype(urb->pipe); | |
582 | struct usb_ctrlrequest *ctrlreq = | |
071d1d47 | 583 | (struct usb_ctrlrequest *) urb->setup_packet; |
04679b34 TH |
584 | |
585 | if (type != PIPE_CONTROL || !ctrlreq) { | |
586 | dev_err(dev, "invalid request to devnum 0\n"); | |
a7cd5829 | 587 | ret = -EINVAL; |
04679b34 TH |
588 | goto no_need_xmit; |
589 | } | |
590 | ||
591 | switch (ctrlreq->bRequest) { | |
592 | case USB_REQ_SET_ADDRESS: | |
593 | /* set_address may come when a device is reset */ | |
594 | dev_info(dev, "SetAddress Request (%d) to port %d\n", | |
595 | ctrlreq->wValue, vdev->rhport); | |
596 | ||
7606ee8a MV |
597 | if (vdev->udev) |
598 | usb_put_dev(vdev->udev); | |
599 | vdev->udev = usb_get_dev(urb->dev); | |
04679b34 TH |
600 | |
601 | spin_lock(&vdev->ud.lock); | |
602 | vdev->ud.status = VDEV_ST_USED; | |
603 | spin_unlock(&vdev->ud.lock); | |
604 | ||
605 | if (urb->status == -EINPROGRESS) { | |
606 | /* This request is successfully completed. */ | |
607 | /* If not -EINPROGRESS, possibly unlinked. */ | |
608 | urb->status = 0; | |
609 | } | |
610 | ||
611 | goto no_need_xmit; | |
612 | ||
613 | case USB_REQ_GET_DESCRIPTOR: | |
614 | if (ctrlreq->wValue == (USB_DT_DEVICE << 8)) | |
b8868e45 | 615 | usbip_dbg_vhci_hc("Not yet?: " |
071d1d47 | 616 | "Get_Descriptor to device 0 " |
617 | "(get max pipe size)\n"); | |
04679b34 | 618 | |
7606ee8a MV |
619 | if (vdev->udev) |
620 | usb_put_dev(vdev->udev); | |
621 | vdev->udev = usb_get_dev(urb->dev); | |
04679b34 TH |
622 | goto out; |
623 | ||
624 | default: | |
625 | /* NOT REACHED */ | |
626 | dev_err(dev, "invalid request to devnum 0 bRequest %u, " | |
627 | "wValue %u\n", ctrlreq->bRequest, | |
628 | ctrlreq->wValue); | |
629 | ret = -EINVAL; | |
630 | goto no_need_xmit; | |
631 | } | |
632 | ||
633 | } | |
634 | ||
635 | out: | |
636 | vhci_tx_urb(urb); | |
04679b34 TH |
637 | spin_unlock_irqrestore(&the_controller->lock, flags); |
638 | ||
639 | return 0; | |
640 | ||
641 | no_need_xmit: | |
642 | usb_hcd_unlink_urb_from_ep(hcd, urb); | |
643 | no_need_unlink: | |
644 | spin_unlock_irqrestore(&the_controller->lock, flags); | |
04679b34 | 645 | usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status); |
a7cd5829 | 646 | return ret; |
04679b34 TH |
647 | } |
648 | ||
649 | /* | |
650 | * vhci_rx gives back the urb after receiving the reply of the urb. If an | |
651 | * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives | |
652 | * back its urb. For the driver unlinking the urb, the content of the urb is | |
653 | * not important, but the calling to its completion handler is important; the | |
654 | * completion of unlinking is notified by the completion handler. | |
655 | * | |
656 | * | |
657 | * CLIENT SIDE | |
658 | * | |
659 | * - When vhci_hcd receives RET_SUBMIT, | |
660 | * | |
661 | * - case 1a). the urb of the pdu is not unlinking. | |
662 | * - normal case | |
663 | * => just give back the urb | |
664 | * | |
665 | * - case 1b). the urb of the pdu is unlinking. | |
666 | * - usbip.ko will return a reply of the unlinking request. | |
667 | * => give back the urb now and go to case 2b). | |
668 | * | |
669 | * - When vhci_hcd receives RET_UNLINK, | |
670 | * | |
671 | * - case 2a). a submit request is still pending in vhci_hcd. | |
672 | * - urb was really pending in usbip.ko and urb_unlink_urb() was | |
673 | * completed there. | |
674 | * => free a pending submit request | |
675 | * => notify unlink completeness by giving back the urb | |
676 | * | |
677 | * - case 2b). a submit request is *not* pending in vhci_hcd. | |
678 | * - urb was already given back to the core driver. | |
679 | * => do not give back the urb | |
680 | * | |
681 | * | |
682 | * SERVER SIDE | |
683 | * | |
684 | * - When usbip receives CMD_UNLINK, | |
685 | * | |
686 | * - case 3a). the urb of the unlink request is now in submission. | |
687 | * => do usb_unlink_urb(). | |
688 | * => after the unlink is completed, send RET_UNLINK. | |
689 | * | |
690 | * - case 3b). the urb of the unlink request is not in submission. | |
691 | * - may be already completed or never be received | |
692 | * => send RET_UNLINK | |
693 | * | |
694 | */ | |
695 | static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) | |
696 | { | |
697 | unsigned long flags; | |
698 | struct vhci_priv *priv; | |
699 | struct vhci_device *vdev; | |
700 | ||
1a4b6f66 | 701 | pr_info("dequeue a urb %p\n", urb); |
04679b34 TH |
702 | |
703 | spin_lock_irqsave(&the_controller->lock, flags); | |
704 | ||
705 | priv = urb->hcpriv; | |
706 | if (!priv) { | |
707 | /* URB was never linked! or will be soon given back by | |
708 | * vhci_rx. */ | |
709 | spin_unlock_irqrestore(&the_controller->lock, flags); | |
710 | return 0; | |
711 | } | |
712 | ||
713 | { | |
714 | int ret = 0; | |
715 | ret = usb_hcd_check_unlink_urb(hcd, urb, status); | |
716 | if (ret) { | |
717 | spin_unlock_irqrestore(&the_controller->lock, flags); | |
b8868e45 | 718 | return ret; |
04679b34 TH |
719 | } |
720 | } | |
721 | ||
722 | /* send unlink request here? */ | |
723 | vdev = priv->vdev; | |
724 | ||
725 | if (!vdev->ud.tcp_socket) { | |
726 | /* tcp connection is closed */ | |
727 | unsigned long flags2; | |
728 | ||
729 | spin_lock_irqsave(&vdev->priv_lock, flags2); | |
730 | ||
1a4b6f66 | 731 | pr_info("device %p seems to be disconnected\n", vdev); |
04679b34 TH |
732 | list_del(&priv->list); |
733 | kfree(priv); | |
734 | urb->hcpriv = NULL; | |
735 | ||
736 | spin_unlock_irqrestore(&vdev->priv_lock, flags2); | |
737 | ||
b8868e45 BM |
738 | /* |
739 | * If tcp connection is alive, we have sent CMD_UNLINK. | |
740 | * vhci_rx will receive RET_UNLINK and give back the URB. | |
741 | * Otherwise, we give back it here. | |
742 | */ | |
1a4b6f66 | 743 | pr_info("gives back urb %p\n", urb); |
b8868e45 BM |
744 | |
745 | usb_hcd_unlink_urb_from_ep(hcd, urb); | |
746 | ||
747 | spin_unlock_irqrestore(&the_controller->lock, flags); | |
748 | usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, | |
071d1d47 | 749 | urb->status); |
b8868e45 BM |
750 | spin_lock_irqsave(&the_controller->lock, flags); |
751 | ||
04679b34 TH |
752 | } else { |
753 | /* tcp connection is alive */ | |
754 | unsigned long flags2; | |
755 | struct vhci_unlink *unlink; | |
756 | ||
757 | spin_lock_irqsave(&vdev->priv_lock, flags2); | |
758 | ||
759 | /* setup CMD_UNLINK pdu */ | |
760 | unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC); | |
761 | if (!unlink) { | |
1a4b6f66 | 762 | pr_err("malloc vhci_unlink\n"); |
04679b34 TH |
763 | spin_unlock_irqrestore(&vdev->priv_lock, flags2); |
764 | spin_unlock_irqrestore(&the_controller->lock, flags); | |
765 | usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC); | |
766 | return -ENOMEM; | |
767 | } | |
768 | ||
769 | unlink->seqnum = atomic_inc_return(&the_controller->seqnum); | |
770 | if (unlink->seqnum == 0xffff) | |
1a4b6f66 | 771 | pr_info("seqnum max\n"); |
04679b34 TH |
772 | |
773 | unlink->unlink_seqnum = priv->seqnum; | |
774 | ||
1a4b6f66 | 775 | pr_info("device %p seems to be still connected\n", vdev); |
04679b34 TH |
776 | |
777 | /* send cmd_unlink and try to cancel the pending URB in the | |
778 | * peer */ | |
779 | list_add_tail(&unlink->list, &vdev->unlink_tx); | |
780 | wake_up(&vdev->waitq_tx); | |
781 | ||
782 | spin_unlock_irqrestore(&vdev->priv_lock, flags2); | |
783 | } | |
784 | ||
04679b34 TH |
785 | spin_unlock_irqrestore(&the_controller->lock, flags); |
786 | ||
b8868e45 | 787 | usbip_dbg_vhci_hc("leave\n"); |
04679b34 TH |
788 | return 0; |
789 | } | |
790 | ||
04679b34 TH |
791 | static void vhci_device_unlink_cleanup(struct vhci_device *vdev) |
792 | { | |
793 | struct vhci_unlink *unlink, *tmp; | |
794 | ||
795 | spin_lock(&vdev->priv_lock); | |
796 | ||
797 | list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) { | |
1a4b6f66 | 798 | pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum); |
04679b34 TH |
799 | list_del(&unlink->list); |
800 | kfree(unlink); | |
801 | } | |
802 | ||
803 | list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) { | |
b92a5e23 MV |
804 | struct urb *urb; |
805 | ||
806 | /* give back URB of unanswered unlink request */ | |
1a4b6f66 | 807 | pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum); |
b92a5e23 MV |
808 | |
809 | urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum); | |
810 | if (!urb) { | |
1a4b6f66 | 811 | pr_info("the urb (seqnum %lu) was already given back\n", |
812 | unlink->unlink_seqnum); | |
b92a5e23 MV |
813 | list_del(&unlink->list); |
814 | kfree(unlink); | |
815 | continue; | |
816 | } | |
817 | ||
818 | urb->status = -ENODEV; | |
819 | ||
820 | spin_lock(&the_controller->lock); | |
821 | usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); | |
822 | spin_unlock(&the_controller->lock); | |
823 | ||
071d1d47 | 824 | usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, |
825 | urb->status); | |
b92a5e23 | 826 | |
04679b34 TH |
827 | list_del(&unlink->list); |
828 | kfree(unlink); | |
829 | } | |
830 | ||
831 | spin_unlock(&vdev->priv_lock); | |
832 | } | |
833 | ||
834 | /* | |
835 | * The important thing is that only one context begins cleanup. | |
836 | * This is why error handling and cleanup become simple. | |
837 | * We do not want to consider race condition as possible. | |
838 | */ | |
839 | static void vhci_shutdown_connection(struct usbip_device *ud) | |
840 | { | |
841 | struct vhci_device *vdev = container_of(ud, struct vhci_device, ud); | |
842 | ||
843 | /* need this? see stub_dev.c */ | |
844 | if (ud->tcp_socket) { | |
1a4b6f66 | 845 | pr_debug("shutdown tcp_socket %p\n", ud->tcp_socket); |
04679b34 TH |
846 | kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR); |
847 | } | |
848 | ||
9720b4bc | 849 | /* kill threads related to this sdev, if v.c. exists */ |
d1b2e95a MV |
850 | if (vdev->ud.tcp_rx) |
851 | kthread_stop(vdev->ud.tcp_rx); | |
852 | if (vdev->ud.tcp_tx) | |
853 | kthread_stop(vdev->ud.tcp_tx); | |
9720b4bc | 854 | |
1a4b6f66 | 855 | pr_info("stop threads\n"); |
04679b34 TH |
856 | |
857 | /* active connection is closed */ | |
858 | if (vdev->ud.tcp_socket != NULL) { | |
859 | sock_release(vdev->ud.tcp_socket); | |
860 | vdev->ud.tcp_socket = NULL; | |
861 | } | |
1a4b6f66 | 862 | pr_info("release socket\n"); |
04679b34 TH |
863 | |
864 | vhci_device_unlink_cleanup(vdev); | |
865 | ||
866 | /* | |
867 | * rh_port_disconnect() is a trigger of ... | |
868 | * usb_disable_device(): | |
869 | * disable all the endpoints for a USB device. | |
870 | * usb_disable_endpoint(): | |
871 | * disable endpoints. pending urbs are unlinked(dequeued). | |
872 | * | |
873 | * NOTE: After calling rh_port_disconnect(), the USB device drivers of a | |
874 | * deteched device should release used urbs in a cleanup function(i.e. | |
875 | * xxx_disconnect()). Therefore, vhci_hcd does not need to release | |
876 | * pushed urbs and their private data in this function. | |
877 | * | |
878 | * NOTE: vhci_dequeue() must be considered carefully. When shutdowning | |
879 | * a connection, vhci_shutdown_connection() expects vhci_dequeue() | |
880 | * gives back pushed urbs and frees their private data by request of | |
881 | * the cleanup function of a USB driver. When unlinking a urb with an | |
882 | * active connection, vhci_dequeue() does not give back the urb which | |
883 | * is actually given back by vhci_rx after receiving its return pdu. | |
884 | * | |
885 | */ | |
886 | rh_port_disconnect(vdev->rhport); | |
887 | ||
1a4b6f66 | 888 | pr_info("disconnect device\n"); |
04679b34 TH |
889 | } |
890 | ||
891 | ||
892 | static void vhci_device_reset(struct usbip_device *ud) | |
893 | { | |
894 | struct vhci_device *vdev = container_of(ud, struct vhci_device, ud); | |
895 | ||
896 | spin_lock(&ud->lock); | |
897 | ||
898 | vdev->speed = 0; | |
899 | vdev->devid = 0; | |
900 | ||
7606ee8a MV |
901 | if (vdev->udev) |
902 | usb_put_dev(vdev->udev); | |
903 | vdev->udev = NULL; | |
904 | ||
04679b34 | 905 | ud->tcp_socket = NULL; |
04679b34 TH |
906 | ud->status = VDEV_ST_NULL; |
907 | ||
908 | spin_unlock(&ud->lock); | |
909 | } | |
910 | ||
911 | static void vhci_device_unusable(struct usbip_device *ud) | |
912 | { | |
913 | spin_lock(&ud->lock); | |
04679b34 | 914 | ud->status = VDEV_ST_ERROR; |
04679b34 TH |
915 | spin_unlock(&ud->lock); |
916 | } | |
917 | ||
918 | static void vhci_device_init(struct vhci_device *vdev) | |
919 | { | |
920 | memset(vdev, 0, sizeof(*vdev)); | |
921 | ||
04679b34 TH |
922 | vdev->ud.side = USBIP_VHCI; |
923 | vdev->ud.status = VDEV_ST_NULL; | |
924 | /* vdev->ud.lock = SPIN_LOCK_UNLOCKED; */ | |
925 | spin_lock_init(&vdev->ud.lock); | |
926 | ||
927 | INIT_LIST_HEAD(&vdev->priv_rx); | |
928 | INIT_LIST_HEAD(&vdev->priv_tx); | |
929 | INIT_LIST_HEAD(&vdev->unlink_tx); | |
930 | INIT_LIST_HEAD(&vdev->unlink_rx); | |
931 | /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */ | |
932 | spin_lock_init(&vdev->priv_lock); | |
933 | ||
934 | init_waitqueue_head(&vdev->waitq_tx); | |
935 | ||
936 | vdev->ud.eh_ops.shutdown = vhci_shutdown_connection; | |
937 | vdev->ud.eh_ops.reset = vhci_device_reset; | |
938 | vdev->ud.eh_ops.unusable = vhci_device_unusable; | |
939 | ||
940 | usbip_start_eh(&vdev->ud); | |
941 | } | |
942 | ||
04679b34 TH |
943 | static int vhci_start(struct usb_hcd *hcd) |
944 | { | |
945 | struct vhci_hcd *vhci = hcd_to_vhci(hcd); | |
946 | int rhport; | |
947 | int err = 0; | |
948 | ||
b8868e45 | 949 | usbip_dbg_vhci_hc("enter vhci_start\n"); |
04679b34 | 950 | |
04679b34 TH |
951 | /* initialize private data of usb_hcd */ |
952 | ||
953 | for (rhport = 0; rhport < VHCI_NPORTS; rhport++) { | |
954 | struct vhci_device *vdev = &vhci->vdev[rhport]; | |
955 | vhci_device_init(vdev); | |
956 | vdev->rhport = rhport; | |
957 | } | |
958 | ||
959 | atomic_set(&vhci->seqnum, 0); | |
960 | spin_lock_init(&vhci->lock); | |
961 | ||
04679b34 TH |
962 | hcd->power_budget = 0; /* no limit */ |
963 | hcd->state = HC_STATE_RUNNING; | |
964 | hcd->uses_new_polling = 1; | |
965 | ||
04679b34 TH |
966 | /* vhci_hcd is now ready to be controlled through sysfs */ |
967 | err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group); | |
968 | if (err) { | |
1a4b6f66 | 969 | pr_err("create sysfs files\n"); |
04679b34 TH |
970 | return err; |
971 | } | |
972 | ||
973 | return 0; | |
974 | } | |
975 | ||
976 | static void vhci_stop(struct usb_hcd *hcd) | |
977 | { | |
978 | struct vhci_hcd *vhci = hcd_to_vhci(hcd); | |
979 | int rhport = 0; | |
980 | ||
b8868e45 | 981 | usbip_dbg_vhci_hc("stop VHCI controller\n"); |
04679b34 | 982 | |
04679b34 TH |
983 | /* 1. remove the userland interface of vhci_hcd */ |
984 | sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group); | |
985 | ||
986 | /* 2. shutdown all the ports of vhci_hcd */ | |
987 | for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) { | |
988 | struct vhci_device *vdev = &vhci->vdev[rhport]; | |
989 | ||
990 | usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED); | |
991 | usbip_stop_eh(&vdev->ud); | |
992 | } | |
04679b34 TH |
993 | } |
994 | ||
04679b34 TH |
995 | static int vhci_get_frame_number(struct usb_hcd *hcd) |
996 | { | |
1a4b6f66 | 997 | pr_err("Not yet implemented\n"); |
04679b34 TH |
998 | return 0; |
999 | } | |
1000 | ||
04679b34 TH |
1001 | #ifdef CONFIG_PM |
1002 | ||
1003 | /* FIXME: suspend/resume */ | |
1004 | static int vhci_bus_suspend(struct usb_hcd *hcd) | |
1005 | { | |
1006 | struct vhci_hcd *vhci = hcd_to_vhci(hcd); | |
1007 | ||
1008 | dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__); | |
1009 | ||
1010 | spin_lock_irq(&vhci->lock); | |
1011 | /* vhci->rh_state = DUMMY_RH_SUSPENDED; | |
1012 | * set_link_state(vhci); */ | |
1013 | hcd->state = HC_STATE_SUSPENDED; | |
1014 | spin_unlock_irq(&vhci->lock); | |
1015 | ||
1016 | return 0; | |
1017 | } | |
1018 | ||
1019 | static int vhci_bus_resume(struct usb_hcd *hcd) | |
1020 | { | |
1021 | struct vhci_hcd *vhci = hcd_to_vhci(hcd); | |
1022 | int rc = 0; | |
1023 | ||
1024 | dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__); | |
1025 | ||
1026 | spin_lock_irq(&vhci->lock); | |
541c7d43 | 1027 | if (!HCD_HW_ACCESSIBLE(hcd)) { |
04679b34 TH |
1028 | rc = -ESHUTDOWN; |
1029 | } else { | |
1030 | /* vhci->rh_state = DUMMY_RH_RUNNING; | |
1031 | * set_link_state(vhci); | |
1032 | * if (!list_empty(&vhci->urbp_list)) | |
1033 | * mod_timer(&vhci->timer, jiffies); */ | |
1034 | hcd->state = HC_STATE_RUNNING; | |
1035 | } | |
1036 | spin_unlock_irq(&vhci->lock); | |
04679b34 | 1037 | |
87352760 | 1038 | return rc; |
04679b34 TH |
1039 | } |
1040 | ||
1041 | #else | |
1042 | ||
1043 | #define vhci_bus_suspend NULL | |
1044 | #define vhci_bus_resume NULL | |
1045 | #endif | |
1046 | ||
04679b34 TH |
1047 | static struct hc_driver vhci_hc_driver = { |
1048 | .description = driver_name, | |
1049 | .product_desc = driver_desc, | |
1050 | .hcd_priv_size = sizeof(struct vhci_hcd), | |
1051 | ||
1052 | .flags = HCD_USB2, | |
1053 | ||
1054 | .start = vhci_start, | |
00512687 | 1055 | .stop = vhci_stop, |
04679b34 TH |
1056 | |
1057 | .urb_enqueue = vhci_urb_enqueue, | |
1058 | .urb_dequeue = vhci_urb_dequeue, | |
1059 | ||
1060 | .get_frame_number = vhci_get_frame_number, | |
1061 | ||
1062 | .hub_status_data = vhci_hub_status, | |
1063 | .hub_control = vhci_hub_control, | |
1064 | .bus_suspend = vhci_bus_suspend, | |
1065 | .bus_resume = vhci_bus_resume, | |
1066 | }; | |
1067 | ||
1068 | static int vhci_hcd_probe(struct platform_device *pdev) | |
1069 | { | |
1070 | struct usb_hcd *hcd; | |
1071 | int ret; | |
1072 | ||
b8868e45 | 1073 | usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id); |
04679b34 TH |
1074 | |
1075 | /* will be removed */ | |
1076 | if (pdev->dev.dma_mask) { | |
1077 | dev_info(&pdev->dev, "vhci_hcd DMA not supported\n"); | |
1078 | return -EINVAL; | |
1079 | } | |
1080 | ||
1081 | /* | |
1082 | * Allocate and initialize hcd. | |
1083 | * Our private data is also allocated automatically. | |
1084 | */ | |
e9133972 | 1085 | hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev)); |
04679b34 | 1086 | if (!hcd) { |
1a4b6f66 | 1087 | pr_err("create hcd failed\n"); |
04679b34 TH |
1088 | return -ENOMEM; |
1089 | } | |
cee6a262 | 1090 | hcd->has_tt = 1; |
04679b34 TH |
1091 | |
1092 | /* this is private data for vhci_hcd */ | |
1093 | the_controller = hcd_to_vhci(hcd); | |
1094 | ||
1095 | /* | |
1096 | * Finish generic HCD structure initialization and register. | |
1097 | * Call the driver's reset() and start() routines. | |
1098 | */ | |
1099 | ret = usb_add_hcd(hcd, 0, 0); | |
1100 | if (ret != 0) { | |
1a4b6f66 | 1101 | pr_err("usb_add_hcd failed %d\n", ret); |
04679b34 TH |
1102 | usb_put_hcd(hcd); |
1103 | the_controller = NULL; | |
1104 | return ret; | |
1105 | } | |
1106 | ||
b8868e45 | 1107 | usbip_dbg_vhci_hc("bye\n"); |
04679b34 TH |
1108 | return 0; |
1109 | } | |
1110 | ||
04679b34 TH |
1111 | static int vhci_hcd_remove(struct platform_device *pdev) |
1112 | { | |
1113 | struct usb_hcd *hcd; | |
1114 | ||
1115 | hcd = platform_get_drvdata(pdev); | |
1116 | if (!hcd) | |
1117 | return 0; | |
1118 | ||
1119 | /* | |
1120 | * Disconnects the root hub, | |
1121 | * then reverses the effects of usb_add_hcd(), | |
1122 | * invoking the HCD's stop() methods. | |
1123 | */ | |
1124 | usb_remove_hcd(hcd); | |
1125 | usb_put_hcd(hcd); | |
1126 | the_controller = NULL; | |
1127 | ||
04679b34 TH |
1128 | return 0; |
1129 | } | |
1130 | ||
04679b34 TH |
1131 | #ifdef CONFIG_PM |
1132 | ||
1133 | /* what should happen for USB/IP under suspend/resume? */ | |
1134 | static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state) | |
1135 | { | |
1136 | struct usb_hcd *hcd; | |
1137 | int rhport = 0; | |
1138 | int connected = 0; | |
1139 | int ret = 0; | |
1140 | ||
04679b34 TH |
1141 | hcd = platform_get_drvdata(pdev); |
1142 | ||
1143 | spin_lock(&the_controller->lock); | |
1144 | ||
1145 | for (rhport = 0; rhport < VHCI_NPORTS; rhport++) | |
1146 | if (the_controller->port_status[rhport] & | |
071d1d47 | 1147 | USB_PORT_STAT_CONNECTION) |
04679b34 TH |
1148 | connected += 1; |
1149 | ||
1150 | spin_unlock(&the_controller->lock); | |
1151 | ||
1152 | if (connected > 0) { | |
1a4b6f66 | 1153 | dev_info(&pdev->dev, "We have %d active connection%s. Do not " |
1154 | "suspend.\n", connected, (connected == 1 ? "" : "s")); | |
04679b34 TH |
1155 | ret = -EBUSY; |
1156 | } else { | |
1a4b6f66 | 1157 | dev_info(&pdev->dev, "suspend vhci_hcd"); |
04679b34 TH |
1158 | clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
1159 | } | |
1160 | ||
1161 | return ret; | |
1162 | } | |
1163 | ||
1164 | static int vhci_hcd_resume(struct platform_device *pdev) | |
1165 | { | |
1166 | struct usb_hcd *hcd; | |
1167 | ||
1168 | dev_dbg(&pdev->dev, "%s\n", __func__); | |
1169 | ||
1170 | hcd = platform_get_drvdata(pdev); | |
1171 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); | |
1172 | usb_hcd_poll_rh_status(hcd); | |
1173 | ||
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | #else | |
1178 | ||
1179 | #define vhci_hcd_suspend NULL | |
1180 | #define vhci_hcd_resume NULL | |
1181 | ||
1182 | #endif | |
1183 | ||
04679b34 TH |
1184 | static struct platform_driver vhci_driver = { |
1185 | .probe = vhci_hcd_probe, | |
1186 | .remove = __devexit_p(vhci_hcd_remove), | |
1187 | .suspend = vhci_hcd_suspend, | |
1188 | .resume = vhci_hcd_resume, | |
1189 | .driver = { | |
1190 | .name = (char *) driver_name, | |
1191 | .owner = THIS_MODULE, | |
1192 | }, | |
1193 | }; | |
1194 | ||
04679b34 TH |
1195 | /* |
1196 | * The VHCI 'device' is 'virtual'; not a real plug&play hardware. | |
1197 | * We need to add this virtual device as a platform device arbitrarily: | |
1198 | * 1. platform_device_register() | |
1199 | */ | |
1200 | static void the_pdev_release(struct device *dev) | |
1201 | { | |
1202 | return; | |
1203 | } | |
1204 | ||
1205 | static struct platform_device the_pdev = { | |
1206 | /* should be the same name as driver_name */ | |
1207 | .name = (char *) driver_name, | |
1208 | .id = -1, | |
1209 | .dev = { | |
1210 | /* .driver = &vhci_driver, */ | |
1211 | .release = the_pdev_release, | |
1212 | }, | |
1213 | }; | |
1214 | ||
0392bbb6 | 1215 | static int __init vhci_hcd_init(void) |
04679b34 TH |
1216 | { |
1217 | int ret; | |
1218 | ||
04679b34 TH |
1219 | if (usb_disabled()) |
1220 | return -ENODEV; | |
1221 | ||
04679b34 TH |
1222 | ret = platform_driver_register(&vhci_driver); |
1223 | if (ret < 0) | |
1224 | goto err_driver_register; | |
1225 | ||
1226 | ret = platform_device_register(&the_pdev); | |
1227 | if (ret < 0) | |
1228 | goto err_platform_device_register; | |
1229 | ||
1a4b6f66 | 1230 | pr_info(DRIVER_DESC " v" USBIP_VERSION "\n"); |
04679b34 TH |
1231 | return ret; |
1232 | ||
04679b34 TH |
1233 | err_platform_device_register: |
1234 | platform_driver_unregister(&vhci_driver); | |
04679b34 | 1235 | err_driver_register: |
04679b34 TH |
1236 | return ret; |
1237 | } | |
04679b34 | 1238 | |
0392bbb6 | 1239 | static void __exit vhci_hcd_exit(void) |
04679b34 | 1240 | { |
04679b34 TH |
1241 | platform_device_unregister(&the_pdev); |
1242 | platform_driver_unregister(&vhci_driver); | |
04679b34 | 1243 | } |
071d1d47 | 1244 | |
0392bbb6 | 1245 | module_init(vhci_hcd_init); |
1246 | module_exit(vhci_hcd_exit); | |
071d1d47 | 1247 | |
1248 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
1249 | MODULE_DESCRIPTION(DRIVER_DESC); | |
4ce0a41f | 1250 | MODULE_LICENSE("GPL"); |
6973c6f2 | 1251 | MODULE_VERSION(USBIP_VERSION); |