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