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