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