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