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