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