1 /*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 * Copyright (C) 2015-2016 Nobuo Iwata
4 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20
21 #include <linux/init.h>
22 #include <linux/file.h>
23 #include <linux/kernel.h>
24 #include <linux/kthread.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28
29 #include "usbip_common.h"
30 #include "vhci.h"
31
32 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
33 #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
34
35 /*
36 * TODO
37 * - update root hub emulation
38 * - move the emulation code to userland ?
39 * porting to other operating systems
40 * minimize kernel code
41 * - add suspend/resume code
42 * - clean up everything
43 */
44
45 /* See usb gadget dummy hcd */
46
47 static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
48 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
49 u16 wIndex, char *buff, u16 wLength);
50 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
51 gfp_t mem_flags);
52 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
53 static int vhci_start(struct usb_hcd *vhci_hcd);
54 static void vhci_stop(struct usb_hcd *hcd);
55 static int vhci_get_frame_number(struct usb_hcd *hcd);
56
57 static const char driver_name[] = "vhci_hcd";
58 static const char driver_desc[] = "USB/IP Virtual Host Controller";
59
60 int vhci_num_controllers = VHCI_NR_HCS;
61 struct vhci *vhcis;
62
63 static const char * const bit_desc[] = {
64 "CONNECTION", /*0*/
65 "ENABLE", /*1*/
66 "SUSPEND", /*2*/
67 "OVER_CURRENT", /*3*/
68 "RESET", /*4*/
69 "L1", /*5*/
70 "R6", /*6*/
71 "R7", /*7*/
72 "POWER", /*8*/
73 "LOWSPEED", /*9*/
74 "HIGHSPEED", /*10*/
75 "PORT_TEST", /*11*/
76 "INDICATOR", /*12*/
77 "R13", /*13*/
78 "R14", /*14*/
79 "R15", /*15*/
80 "C_CONNECTION", /*16*/
81 "C_ENABLE", /*17*/
82 "C_SUSPEND", /*18*/
83 "C_OVER_CURRENT", /*19*/
84 "C_RESET", /*20*/
85 "C_L1", /*21*/
86 "R22", /*22*/
87 "R23", /*23*/
88 "R24", /*24*/
89 "R25", /*25*/
90 "R26", /*26*/
91 "R27", /*27*/
92 "R28", /*28*/
93 "R29", /*29*/
94 "R30", /*30*/
95 "R31", /*31*/
96 };
97
98 static const char * const bit_desc_ss[] = {
99 "CONNECTION", /*0*/
100 "ENABLE", /*1*/
101 "SUSPEND", /*2*/
102 "OVER_CURRENT", /*3*/
103 "RESET", /*4*/
104 "L1", /*5*/
105 "R6", /*6*/
106 "R7", /*7*/
107 "R8", /*8*/
108 "POWER", /*9*/
109 "HIGHSPEED", /*10*/
110 "PORT_TEST", /*11*/
111 "INDICATOR", /*12*/
112 "R13", /*13*/
113 "R14", /*14*/
114 "R15", /*15*/
115 "C_CONNECTION", /*16*/
116 "C_ENABLE", /*17*/
117 "C_SUSPEND", /*18*/
118 "C_OVER_CURRENT", /*19*/
119 "C_RESET", /*20*/
120 "C_BH_RESET", /*21*/
121 "C_LINK_STATE", /*22*/
122 "C_CONFIG_ERROR", /*23*/
123 "R24", /*24*/
124 "R25", /*25*/
125 "R26", /*26*/
126 "R27", /*27*/
127 "R28", /*28*/
128 "R29", /*29*/
129 "R30", /*30*/
130 "R31", /*31*/
131 };
132
dump_port_status_diff(u32 prev_status,u32 new_status,bool usb3)133 static void dump_port_status_diff(u32 prev_status, u32 new_status, bool usb3)
134 {
135 int i = 0;
136 u32 bit = 1;
137 const char * const *desc = bit_desc;
138
139 if (usb3)
140 desc = bit_desc_ss;
141
142 pr_debug("status prev -> new: %08x -> %08x\n", prev_status, new_status);
143 while (bit) {
144 u32 prev = prev_status & bit;
145 u32 new = new_status & bit;
146 char change;
147
148 if (!prev && new)
149 change = '+';
150 else if (prev && !new)
151 change = '-';
152 else
153 change = ' ';
154
155 if (prev || new) {
156 pr_debug(" %c%s\n", change, desc[i]);
157
158 if (bit == 1) /* USB_PORT_STAT_CONNECTION */
159 pr_debug(" %c%s\n", change, "USB_PORT_STAT_SPEED_5GBPS");
160 }
161 bit <<= 1;
162 i++;
163 }
164 pr_debug("\n");
165 }
166
rh_port_connect(struct vhci_device * vdev,enum usb_device_speed speed)167 void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed)
168 {
169 struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
170 struct vhci *vhci = vhci_hcd->vhci;
171 int rhport = vdev->rhport;
172 u32 status;
173 unsigned long flags;
174
175 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
176
177 spin_lock_irqsave(&vhci->lock, flags);
178
179 status = vhci_hcd->port_status[rhport];
180
181 status |= USB_PORT_STAT_CONNECTION | (1 << USB_PORT_FEAT_C_CONNECTION);
182
183 switch (speed) {
184 case USB_SPEED_HIGH:
185 status |= USB_PORT_STAT_HIGH_SPEED;
186 break;
187 case USB_SPEED_LOW:
188 status |= USB_PORT_STAT_LOW_SPEED;
189 break;
190 default:
191 break;
192 }
193
194 vhci_hcd->port_status[rhport] = status;
195
196 spin_unlock_irqrestore(&vhci->lock, flags);
197
198 usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd));
199 }
200
rh_port_disconnect(struct vhci_device * vdev)201 static void rh_port_disconnect(struct vhci_device *vdev)
202 {
203 struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
204 struct vhci *vhci = vhci_hcd->vhci;
205 int rhport = vdev->rhport;
206 u32 status;
207 unsigned long flags;
208
209 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
210
211 spin_lock_irqsave(&vhci->lock, flags);
212
213 status = vhci_hcd->port_status[rhport];
214
215 status &= ~USB_PORT_STAT_CONNECTION;
216 status |= (1 << USB_PORT_FEAT_C_CONNECTION);
217
218 vhci_hcd->port_status[rhport] = status;
219
220 spin_unlock_irqrestore(&vhci->lock, flags);
221 usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd));
222 }
223
224 #define PORT_C_MASK \
225 ((USB_PORT_STAT_C_CONNECTION \
226 | USB_PORT_STAT_C_ENABLE \
227 | USB_PORT_STAT_C_SUSPEND \
228 | USB_PORT_STAT_C_OVERCURRENT \
229 | USB_PORT_STAT_C_RESET) << 16)
230
231 /*
232 * Returns 0 if the status hasn't changed, or the number of bytes in buf.
233 * Ports are 0-indexed from the HCD point of view,
234 * and 1-indexed from the USB core pointer of view.
235 *
236 * @buf: a bitmap to show which port status has been changed.
237 * bit 0: reserved
238 * bit 1: the status of port 0 has been changed.
239 * bit 2: the status of port 1 has been changed.
240 * ...
241 */
vhci_hub_status(struct usb_hcd * hcd,char * buf)242 static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
243 {
244 struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
245 struct vhci *vhci = vhci_hcd->vhci;
246 int retval = DIV_ROUND_UP(VHCI_HC_PORTS + 1, 8);
247 int rhport;
248 int changed = 0;
249 unsigned long flags;
250
251 memset(buf, 0, retval);
252
253 spin_lock_irqsave(&vhci->lock, flags);
254 if (!HCD_HW_ACCESSIBLE(hcd)) {
255 usbip_dbg_vhci_rh("hw accessible flag not on?\n");
256 goto done;
257 }
258
259 /* check pseudo status register for each port */
260 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
261 if ((vhci_hcd->port_status[rhport] & PORT_C_MASK)) {
262 /* The status of a port has been changed, */
263 usbip_dbg_vhci_rh("port %d status changed\n", rhport);
264
265 buf[(rhport + 1) / 8] |= 1 << (rhport + 1) % 8;
266 changed = 1;
267 }
268 }
269
270 if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
271 usb_hcd_resume_root_hub(hcd);
272
273 done:
274 spin_unlock_irqrestore(&vhci->lock, flags);
275 return changed ? retval : 0;
276 }
277
278 /* usb 3.0 root hub device descriptor */
279 static struct {
280 struct usb_bos_descriptor bos;
281 struct usb_ss_cap_descriptor ss_cap;
282 } __packed usb3_bos_desc = {
283
284 .bos = {
285 .bLength = USB_DT_BOS_SIZE,
286 .bDescriptorType = USB_DT_BOS,
287 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
288 .bNumDeviceCaps = 1,
289 },
290 .ss_cap = {
291 .bLength = USB_DT_USB_SS_CAP_SIZE,
292 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
293 .bDevCapabilityType = USB_SS_CAP_TYPE,
294 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
295 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
296 },
297 };
298
299 static inline void
ss_hub_descriptor(struct usb_hub_descriptor * desc)300 ss_hub_descriptor(struct usb_hub_descriptor *desc)
301 {
302 memset(desc, 0, sizeof *desc);
303 desc->bDescriptorType = USB_DT_SS_HUB;
304 desc->bDescLength = 12;
305 desc->wHubCharacteristics = cpu_to_le16(
306 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
307 desc->bNbrPorts = VHCI_HC_PORTS;
308 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
309 desc->u.ss.DeviceRemovable = 0xffff;
310 }
311
hub_descriptor(struct usb_hub_descriptor * desc)312 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
313 {
314 int width;
315
316 memset(desc, 0, sizeof(*desc));
317 desc->bDescriptorType = USB_DT_HUB;
318 desc->wHubCharacteristics = cpu_to_le16(
319 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
320
321 desc->bNbrPorts = VHCI_HC_PORTS;
322 BUILD_BUG_ON(VHCI_HC_PORTS > USB_MAXCHILDREN);
323 width = desc->bNbrPorts / 8 + 1;
324 desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * width;
325 memset(&desc->u.hs.DeviceRemovable[0], 0, width);
326 memset(&desc->u.hs.DeviceRemovable[width], 0xff, width);
327 }
328
vhci_hub_control(struct usb_hcd * hcd,u16 typeReq,u16 wValue,u16 wIndex,char * buf,u16 wLength)329 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
330 u16 wIndex, char *buf, u16 wLength)
331 {
332 struct vhci_hcd *vhci_hcd;
333 struct vhci *vhci;
334 int retval = 0;
335 int rhport = -1;
336 unsigned long flags;
337 bool invalid_rhport = false;
338
339 u32 prev_port_status[VHCI_HC_PORTS];
340
341 if (!HCD_HW_ACCESSIBLE(hcd))
342 return -ETIMEDOUT;
343
344 /*
345 * NOTE:
346 * wIndex (bits 0-7) shows the port number and begins from 1?
347 */
348 wIndex = ((__u8)(wIndex & 0x00ff));
349 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
350 wIndex);
351
352 /*
353 * wIndex can be 0 for some request types (typeReq). rhport is
354 * in valid range when wIndex >= 1 and < VHCI_HC_PORTS.
355 *
356 * Reference port_status[] only with valid rhport when
357 * invalid_rhport is false.
358 */
359 if (wIndex < 1 || wIndex > VHCI_HC_PORTS) {
360 invalid_rhport = true;
361 if (wIndex > VHCI_HC_PORTS)
362 pr_err("invalid port number %d\n", wIndex);
363 } else
364 rhport = wIndex - 1;
365
366 vhci_hcd = hcd_to_vhci_hcd(hcd);
367 vhci = vhci_hcd->vhci;
368
369 spin_lock_irqsave(&vhci->lock, flags);
370
371 /* store old status and compare now and old later */
372 if (usbip_dbg_flag_vhci_rh) {
373 if (!invalid_rhport)
374 memcpy(prev_port_status, vhci_hcd->port_status,
375 sizeof(prev_port_status));
376 }
377
378 switch (typeReq) {
379 case ClearHubFeature:
380 usbip_dbg_vhci_rh(" ClearHubFeature\n");
381 break;
382 case ClearPortFeature:
383 if (invalid_rhport) {
384 pr_err("invalid port number %d\n", wIndex);
385 goto error;
386 }
387 switch (wValue) {
388 case USB_PORT_FEAT_SUSPEND:
389 if (hcd->speed == HCD_USB3) {
390 pr_err(" ClearPortFeature: USB_PORT_FEAT_SUSPEND req not "
391 "supported for USB 3.0 roothub\n");
392 goto error;
393 }
394 usbip_dbg_vhci_rh(
395 " ClearPortFeature: USB_PORT_FEAT_SUSPEND\n");
396 if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
397 /* 20msec signaling */
398 vhci_hcd->resuming = 1;
399 vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
400 }
401 break;
402 case USB_PORT_FEAT_POWER:
403 usbip_dbg_vhci_rh(
404 " ClearPortFeature: USB_PORT_FEAT_POWER\n");
405 if (hcd->speed == HCD_USB3)
406 vhci_hcd->port_status[rhport] &= ~USB_SS_PORT_STAT_POWER;
407 else
408 vhci_hcd->port_status[rhport] &= ~USB_PORT_STAT_POWER;
409 break;
410 default:
411 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
412 wValue);
413 vhci_hcd->port_status[rhport] &= ~(1 << wValue);
414 break;
415 }
416 break;
417 case GetHubDescriptor:
418 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
419 if (hcd->speed == HCD_USB3 &&
420 (wLength < USB_DT_SS_HUB_SIZE ||
421 wValue != (USB_DT_SS_HUB << 8))) {
422 pr_err("Wrong hub descriptor type for USB 3.0 roothub.\n");
423 goto error;
424 }
425 if (hcd->speed == HCD_USB3)
426 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
427 else
428 hub_descriptor((struct usb_hub_descriptor *) buf);
429 break;
430 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
431 if (hcd->speed != HCD_USB3)
432 goto error;
433
434 if ((wValue >> 8) != USB_DT_BOS)
435 goto error;
436
437 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
438 retval = sizeof(usb3_bos_desc);
439 break;
440 case GetHubStatus:
441 usbip_dbg_vhci_rh(" GetHubStatus\n");
442 *(__le32 *) buf = cpu_to_le32(0);
443 break;
444 case GetPortStatus:
445 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
446 if (invalid_rhport) {
447 pr_err("invalid port number %d\n", wIndex);
448 retval = -EPIPE;
449 goto error;
450 }
451
452 /* we do not care about resume. */
453
454 /* whoever resets or resumes must GetPortStatus to
455 * complete it!!
456 */
457 if (vhci_hcd->resuming && time_after(jiffies, vhci_hcd->re_timeout)) {
458 vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_SUSPEND);
459 vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_SUSPEND);
460 vhci_hcd->resuming = 0;
461 vhci_hcd->re_timeout = 0;
462 }
463
464 if ((vhci_hcd->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
465 0 && time_after(jiffies, vhci_hcd->re_timeout)) {
466 vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_RESET);
467 vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_RESET);
468 vhci_hcd->re_timeout = 0;
469
470 if (vhci_hcd->vdev[rhport].ud.status ==
471 VDEV_ST_NOTASSIGNED) {
472 usbip_dbg_vhci_rh(
473 " enable rhport %d (status %u)\n",
474 rhport,
475 vhci_hcd->vdev[rhport].ud.status);
476 vhci_hcd->port_status[rhport] |=
477 USB_PORT_STAT_ENABLE;
478 }
479
480 if (hcd->speed < HCD_USB3) {
481 switch (vhci_hcd->vdev[rhport].speed) {
482 case USB_SPEED_HIGH:
483 vhci_hcd->port_status[rhport] |=
484 USB_PORT_STAT_HIGH_SPEED;
485 break;
486 case USB_SPEED_LOW:
487 vhci_hcd->port_status[rhport] |=
488 USB_PORT_STAT_LOW_SPEED;
489 break;
490 default:
491 pr_err("vhci_device speed not set\n");
492 break;
493 }
494 }
495 }
496 ((__le16 *) buf)[0] = cpu_to_le16(vhci_hcd->port_status[rhport]);
497 ((__le16 *) buf)[1] =
498 cpu_to_le16(vhci_hcd->port_status[rhport] >> 16);
499
500 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
501 ((u16 *)buf)[1]);
502 break;
503 case SetHubFeature:
504 usbip_dbg_vhci_rh(" SetHubFeature\n");
505 retval = -EPIPE;
506 break;
507 case SetPortFeature:
508 switch (wValue) {
509 case USB_PORT_FEAT_LINK_STATE:
510 usbip_dbg_vhci_rh(
511 " SetPortFeature: USB_PORT_FEAT_LINK_STATE\n");
512 if (hcd->speed != HCD_USB3) {
513 pr_err("USB_PORT_FEAT_LINK_STATE req not "
514 "supported for USB 2.0 roothub\n");
515 goto error;
516 }
517 /*
518 * Since this is dummy we don't have an actual link so
519 * there is nothing to do for the SET_LINK_STATE cmd
520 */
521 break;
522 case USB_PORT_FEAT_U1_TIMEOUT:
523 usbip_dbg_vhci_rh(
524 " SetPortFeature: USB_PORT_FEAT_U1_TIMEOUT\n");
525 case USB_PORT_FEAT_U2_TIMEOUT:
526 usbip_dbg_vhci_rh(
527 " SetPortFeature: USB_PORT_FEAT_U2_TIMEOUT\n");
528 /* TODO: add suspend/resume support! */
529 if (hcd->speed != HCD_USB3) {
530 pr_err("USB_PORT_FEAT_U1/2_TIMEOUT req not "
531 "supported for USB 2.0 roothub\n");
532 goto error;
533 }
534 break;
535 case USB_PORT_FEAT_SUSPEND:
536 usbip_dbg_vhci_rh(
537 " SetPortFeature: USB_PORT_FEAT_SUSPEND\n");
538 /* Applicable only for USB2.0 hub */
539 if (hcd->speed == HCD_USB3) {
540 pr_err("USB_PORT_FEAT_SUSPEND req not "
541 "supported for USB 3.0 roothub\n");
542 goto error;
543 }
544
545 if (invalid_rhport) {
546 pr_err("invalid port number %d\n", wIndex);
547 goto error;
548 }
549
550 vhci_hcd->port_status[rhport] |= USB_PORT_STAT_SUSPEND;
551 break;
552 case USB_PORT_FEAT_POWER:
553 usbip_dbg_vhci_rh(
554 " SetPortFeature: USB_PORT_FEAT_POWER\n");
555 if (invalid_rhport) {
556 pr_err("invalid port number %d\n", wIndex);
557 goto error;
558 }
559 if (hcd->speed == HCD_USB3)
560 vhci_hcd->port_status[rhport] |= USB_SS_PORT_STAT_POWER;
561 else
562 vhci_hcd->port_status[rhport] |= USB_PORT_STAT_POWER;
563 break;
564 case USB_PORT_FEAT_BH_PORT_RESET:
565 usbip_dbg_vhci_rh(
566 " SetPortFeature: USB_PORT_FEAT_BH_PORT_RESET\n");
567 if (invalid_rhport) {
568 pr_err("invalid port number %d\n", wIndex);
569 goto error;
570 }
571 /* Applicable only for USB3.0 hub */
572 if (hcd->speed != HCD_USB3) {
573 pr_err("USB_PORT_FEAT_BH_PORT_RESET req not "
574 "supported for USB 2.0 roothub\n");
575 goto error;
576 }
577 /* FALLS THROUGH */
578 case USB_PORT_FEAT_RESET:
579 usbip_dbg_vhci_rh(
580 " SetPortFeature: USB_PORT_FEAT_RESET\n");
581 if (invalid_rhport) {
582 pr_err("invalid port number %d\n", wIndex);
583 goto error;
584 }
585 /* if it's already enabled, disable */
586 if (hcd->speed == HCD_USB3) {
587 vhci_hcd->port_status[rhport] = 0;
588 vhci_hcd->port_status[rhport] =
589 (USB_SS_PORT_STAT_POWER |
590 USB_PORT_STAT_CONNECTION |
591 USB_PORT_STAT_RESET);
592 } else if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_ENABLE) {
593 vhci_hcd->port_status[rhport] &= ~(USB_PORT_STAT_ENABLE
594 | USB_PORT_STAT_LOW_SPEED
595 | USB_PORT_STAT_HIGH_SPEED);
596 }
597
598 /* 50msec reset signaling */
599 vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
600
601 /* FALLS THROUGH */
602 default:
603 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
604 wValue);
605 if (invalid_rhport) {
606 pr_err("invalid port number %d\n", wIndex);
607 goto error;
608 }
609 if (hcd->speed == HCD_USB3) {
610 if ((vhci_hcd->port_status[rhport] &
611 USB_SS_PORT_STAT_POWER) != 0) {
612 vhci_hcd->port_status[rhport] |= (1 << wValue);
613 }
614 } else
615 if ((vhci_hcd->port_status[rhport] &
616 USB_PORT_STAT_POWER) != 0) {
617 vhci_hcd->port_status[rhport] |= (1 << wValue);
618 }
619 }
620 break;
621 case GetPortErrorCount:
622 usbip_dbg_vhci_rh(" GetPortErrorCount\n");
623 if (hcd->speed != HCD_USB3) {
624 pr_err("GetPortErrorCount req not "
625 "supported for USB 2.0 roothub\n");
626 goto error;
627 }
628 /* We'll always return 0 since this is a dummy hub */
629 *(__le32 *) buf = cpu_to_le32(0);
630 break;
631 case SetHubDepth:
632 usbip_dbg_vhci_rh(" SetHubDepth\n");
633 if (hcd->speed != HCD_USB3) {
634 pr_err("SetHubDepth req not supported for "
635 "USB 2.0 roothub\n");
636 goto error;
637 }
638 break;
639 default:
640 pr_err("default hub control req: %04x v%04x i%04x l%d\n",
641 typeReq, wValue, wIndex, wLength);
642 error:
643 /* "protocol stall" on error */
644 retval = -EPIPE;
645 }
646
647 if (usbip_dbg_flag_vhci_rh) {
648 pr_debug("port %d\n", rhport);
649 /* Only dump valid port status */
650 if (!invalid_rhport) {
651 dump_port_status_diff(prev_port_status[rhport],
652 vhci_hcd->port_status[rhport],
653 hcd->speed == HCD_USB3);
654 }
655 }
656 usbip_dbg_vhci_rh(" bye\n");
657
658 spin_unlock_irqrestore(&vhci->lock, flags);
659
660 if (!invalid_rhport &&
661 (vhci_hcd->port_status[rhport] & PORT_C_MASK) != 0) {
662 usb_hcd_poll_rh_status(hcd);
663 }
664
665 return retval;
666 }
667
vhci_tx_urb(struct urb * urb,struct vhci_device * vdev)668 static void vhci_tx_urb(struct urb *urb, struct vhci_device *vdev)
669 {
670 struct vhci_priv *priv;
671 struct vhci_hcd *vhci_hcd;
672 unsigned long flags;
673
674 if (!vdev) {
675 pr_err("could not get virtual device");
676 return;
677 }
678 vhci_hcd = vdev_to_vhci_hcd(vdev);
679
680 priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
681 if (!priv) {
682 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
683 return;
684 }
685
686 spin_lock_irqsave(&vdev->priv_lock, flags);
687
688 priv->seqnum = atomic_inc_return(&vhci_hcd->seqnum);
689 if (priv->seqnum == 0xffff)
690 dev_info(&urb->dev->dev, "seqnum max\n");
691
692 priv->vdev = vdev;
693 priv->urb = urb;
694
695 urb->hcpriv = (void *) priv;
696
697 list_add_tail(&priv->list, &vdev->priv_tx);
698
699 wake_up(&vdev->waitq_tx);
700 spin_unlock_irqrestore(&vdev->priv_lock, flags);
701 }
702
vhci_urb_enqueue(struct usb_hcd * hcd,struct urb * urb,gfp_t mem_flags)703 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
704 {
705 struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
706 struct vhci *vhci = vhci_hcd->vhci;
707 struct device *dev = &urb->dev->dev;
708 u8 portnum = urb->dev->portnum;
709 int ret = 0;
710 struct vhci_device *vdev;
711 unsigned long flags;
712
713 if (portnum > VHCI_HC_PORTS) {
714 pr_err("invalid port number %d\n", portnum);
715 return -ENODEV;
716 }
717 vdev = &vhci_hcd->vdev[portnum-1];
718
719 if (!urb->transfer_buffer && !urb->num_sgs &&
720 urb->transfer_buffer_length) {
721 dev_dbg(dev, "Null URB transfer buffer\n");
722 return -EINVAL;
723 }
724
725 spin_lock_irqsave(&vhci->lock, flags);
726
727 if (urb->status != -EINPROGRESS) {
728 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
729 spin_unlock_irqrestore(&vhci->lock, flags);
730 return urb->status;
731 }
732
733 /* refuse enqueue for dead connection */
734 spin_lock(&vdev->ud.lock);
735 if (vdev->ud.status == VDEV_ST_NULL ||
736 vdev->ud.status == VDEV_ST_ERROR) {
737 dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
738 spin_unlock(&vdev->ud.lock);
739 spin_unlock_irqrestore(&vhci->lock, flags);
740 return -ENODEV;
741 }
742 spin_unlock(&vdev->ud.lock);
743
744 ret = usb_hcd_link_urb_to_ep(hcd, urb);
745 if (ret)
746 goto no_need_unlink;
747
748 /*
749 * The enumeration process is as follows;
750 *
751 * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
752 * to get max packet length of default pipe
753 *
754 * 2. Set_Address request to DevAddr(0) EndPoint(0)
755 *
756 */
757 if (usb_pipedevice(urb->pipe) == 0) {
758 __u8 type = usb_pipetype(urb->pipe);
759 struct usb_ctrlrequest *ctrlreq =
760 (struct usb_ctrlrequest *) urb->setup_packet;
761
762 if (type != PIPE_CONTROL || !ctrlreq) {
763 dev_err(dev, "invalid request to devnum 0\n");
764 ret = -EINVAL;
765 goto no_need_xmit;
766 }
767
768 switch (ctrlreq->bRequest) {
769 case USB_REQ_SET_ADDRESS:
770 /* set_address may come when a device is reset */
771 dev_info(dev, "SetAddress Request (%d) to port %d\n",
772 ctrlreq->wValue, vdev->rhport);
773
774 usb_put_dev(vdev->udev);
775 vdev->udev = usb_get_dev(urb->dev);
776
777 spin_lock(&vdev->ud.lock);
778 vdev->ud.status = VDEV_ST_USED;
779 spin_unlock(&vdev->ud.lock);
780
781 if (urb->status == -EINPROGRESS) {
782 /* This request is successfully completed. */
783 /* If not -EINPROGRESS, possibly unlinked. */
784 urb->status = 0;
785 }
786
787 goto no_need_xmit;
788
789 case USB_REQ_GET_DESCRIPTOR:
790 if (ctrlreq->wValue == cpu_to_le16(USB_DT_DEVICE << 8))
791 usbip_dbg_vhci_hc(
792 "Not yet?:Get_Descriptor to device 0 (get max pipe size)\n");
793
794 usb_put_dev(vdev->udev);
795 vdev->udev = usb_get_dev(urb->dev);
796 goto out;
797
798 default:
799 /* NOT REACHED */
800 dev_err(dev,
801 "invalid request to devnum 0 bRequest %u, wValue %u\n",
802 ctrlreq->bRequest,
803 ctrlreq->wValue);
804 ret = -EINVAL;
805 goto no_need_xmit;
806 }
807
808 }
809
810 out:
811 vhci_tx_urb(urb, vdev);
812 spin_unlock_irqrestore(&vhci->lock, flags);
813
814 return 0;
815
816 no_need_xmit:
817 usb_hcd_unlink_urb_from_ep(hcd, urb);
818 no_need_unlink:
819 spin_unlock_irqrestore(&vhci->lock, flags);
820 if (!ret)
821 usb_hcd_giveback_urb(hcd, urb, urb->status);
822 return ret;
823 }
824
825 /*
826 * vhci_rx gives back the urb after receiving the reply of the urb. If an
827 * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
828 * back its urb. For the driver unlinking the urb, the content of the urb is
829 * not important, but the calling to its completion handler is important; the
830 * completion of unlinking is notified by the completion handler.
831 *
832 *
833 * CLIENT SIDE
834 *
835 * - When vhci_hcd receives RET_SUBMIT,
836 *
837 * - case 1a). the urb of the pdu is not unlinking.
838 * - normal case
839 * => just give back the urb
840 *
841 * - case 1b). the urb of the pdu is unlinking.
842 * - usbip.ko will return a reply of the unlinking request.
843 * => give back the urb now and go to case 2b).
844 *
845 * - When vhci_hcd receives RET_UNLINK,
846 *
847 * - case 2a). a submit request is still pending in vhci_hcd.
848 * - urb was really pending in usbip.ko and urb_unlink_urb() was
849 * completed there.
850 * => free a pending submit request
851 * => notify unlink completeness by giving back the urb
852 *
853 * - case 2b). a submit request is *not* pending in vhci_hcd.
854 * - urb was already given back to the core driver.
855 * => do not give back the urb
856 *
857 *
858 * SERVER SIDE
859 *
860 * - When usbip receives CMD_UNLINK,
861 *
862 * - case 3a). the urb of the unlink request is now in submission.
863 * => do usb_unlink_urb().
864 * => after the unlink is completed, send RET_UNLINK.
865 *
866 * - case 3b). the urb of the unlink request is not in submission.
867 * - may be already completed or never be received
868 * => send RET_UNLINK
869 *
870 */
vhci_urb_dequeue(struct usb_hcd * hcd,struct urb * urb,int status)871 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
872 {
873 struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
874 struct vhci *vhci = vhci_hcd->vhci;
875 struct vhci_priv *priv;
876 struct vhci_device *vdev;
877 unsigned long flags;
878
879 spin_lock_irqsave(&vhci->lock, flags);
880
881 priv = urb->hcpriv;
882 if (!priv) {
883 /* URB was never linked! or will be soon given back by
884 * vhci_rx. */
885 spin_unlock_irqrestore(&vhci->lock, flags);
886 return -EIDRM;
887 }
888
889 {
890 int ret = 0;
891
892 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
893 if (ret) {
894 spin_unlock_irqrestore(&vhci->lock, flags);
895 return ret;
896 }
897 }
898
899 /* send unlink request here? */
900 vdev = priv->vdev;
901
902 if (!vdev->ud.tcp_socket) {
903 /* tcp connection is closed */
904 spin_lock(&vdev->priv_lock);
905
906 list_del(&priv->list);
907 kfree(priv);
908 urb->hcpriv = NULL;
909
910 spin_unlock(&vdev->priv_lock);
911
912 /*
913 * If tcp connection is alive, we have sent CMD_UNLINK.
914 * vhci_rx will receive RET_UNLINK and give back the URB.
915 * Otherwise, we give back it here.
916 */
917 usb_hcd_unlink_urb_from_ep(hcd, urb);
918
919 spin_unlock_irqrestore(&vhci->lock, flags);
920 usb_hcd_giveback_urb(hcd, urb, urb->status);
921 spin_lock_irqsave(&vhci->lock, flags);
922
923 } else {
924 /* tcp connection is alive */
925 struct vhci_unlink *unlink;
926
927 spin_lock(&vdev->priv_lock);
928
929 /* setup CMD_UNLINK pdu */
930 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
931 if (!unlink) {
932 spin_unlock(&vdev->priv_lock);
933 spin_unlock_irqrestore(&vhci->lock, flags);
934 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
935 return -ENOMEM;
936 }
937
938 unlink->seqnum = atomic_inc_return(&vhci_hcd->seqnum);
939 if (unlink->seqnum == 0xffff)
940 pr_info("seqnum max\n");
941
942 unlink->unlink_seqnum = priv->seqnum;
943
944 /* send cmd_unlink and try to cancel the pending URB in the
945 * peer */
946 list_add_tail(&unlink->list, &vdev->unlink_tx);
947 wake_up(&vdev->waitq_tx);
948
949 spin_unlock(&vdev->priv_lock);
950 }
951
952 spin_unlock_irqrestore(&vhci->lock, flags);
953
954 usbip_dbg_vhci_hc("leave\n");
955 return 0;
956 }
957
vhci_device_unlink_cleanup(struct vhci_device * vdev)958 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
959 {
960 struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
961 struct usb_hcd *hcd = vhci_hcd_to_hcd(vhci_hcd);
962 struct vhci *vhci = vhci_hcd->vhci;
963 struct vhci_unlink *unlink, *tmp;
964 unsigned long flags;
965
966 spin_lock_irqsave(&vhci->lock, flags);
967 spin_lock(&vdev->priv_lock);
968
969 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
970 pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
971 list_del(&unlink->list);
972 kfree(unlink);
973 }
974
975 while (!list_empty(&vdev->unlink_rx)) {
976 struct urb *urb;
977
978 unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink,
979 list);
980
981 /* give back URB of unanswered unlink request */
982 pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
983
984 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
985 if (!urb) {
986 pr_info("the urb (seqnum %lu) was already given back\n",
987 unlink->unlink_seqnum);
988 list_del(&unlink->list);
989 kfree(unlink);
990 continue;
991 }
992
993 urb->status = -ENODEV;
994
995 usb_hcd_unlink_urb_from_ep(hcd, urb);
996
997 list_del(&unlink->list);
998
999 spin_unlock(&vdev->priv_lock);
1000 spin_unlock_irqrestore(&vhci->lock, flags);
1001
1002 usb_hcd_giveback_urb(hcd, urb, urb->status);
1003
1004 spin_lock_irqsave(&vhci->lock, flags);
1005 spin_lock(&vdev->priv_lock);
1006
1007 kfree(unlink);
1008 }
1009
1010 spin_unlock(&vdev->priv_lock);
1011 spin_unlock_irqrestore(&vhci->lock, flags);
1012 }
1013
1014 /*
1015 * The important thing is that only one context begins cleanup.
1016 * This is why error handling and cleanup become simple.
1017 * We do not want to consider race condition as possible.
1018 */
vhci_shutdown_connection(struct usbip_device * ud)1019 static void vhci_shutdown_connection(struct usbip_device *ud)
1020 {
1021 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1022
1023 /* need this? see stub_dev.c */
1024 if (ud->tcp_socket) {
1025 pr_debug("shutdown tcp_socket %d\n", ud->sockfd);
1026 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
1027 }
1028
1029 /* kill threads related to this sdev */
1030 if (vdev->ud.tcp_rx) {
1031 kthread_stop_put(vdev->ud.tcp_rx);
1032 vdev->ud.tcp_rx = NULL;
1033 }
1034 if (vdev->ud.tcp_tx) {
1035 kthread_stop_put(vdev->ud.tcp_tx);
1036 vdev->ud.tcp_tx = NULL;
1037 }
1038 pr_info("stop threads\n");
1039
1040 /* active connection is closed */
1041 if (vdev->ud.tcp_socket) {
1042 sockfd_put(vdev->ud.tcp_socket);
1043 vdev->ud.tcp_socket = NULL;
1044 vdev->ud.sockfd = -1;
1045 }
1046 pr_info("release socket\n");
1047
1048 vhci_device_unlink_cleanup(vdev);
1049
1050 /*
1051 * rh_port_disconnect() is a trigger of ...
1052 * usb_disable_device():
1053 * disable all the endpoints for a USB device.
1054 * usb_disable_endpoint():
1055 * disable endpoints. pending urbs are unlinked(dequeued).
1056 *
1057 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
1058 * detached device should release used urbs in a cleanup function (i.e.
1059 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
1060 * pushed urbs and their private data in this function.
1061 *
1062 * NOTE: vhci_dequeue() must be considered carefully. When shutting down
1063 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
1064 * gives back pushed urbs and frees their private data by request of
1065 * the cleanup function of a USB driver. When unlinking a urb with an
1066 * active connection, vhci_dequeue() does not give back the urb which
1067 * is actually given back by vhci_rx after receiving its return pdu.
1068 *
1069 */
1070 rh_port_disconnect(vdev);
1071
1072 pr_info("disconnect device\n");
1073 }
1074
vhci_device_reset(struct usbip_device * ud)1075 static void vhci_device_reset(struct usbip_device *ud)
1076 {
1077 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1078 unsigned long flags;
1079
1080 spin_lock_irqsave(&ud->lock, flags);
1081
1082 vdev->speed = 0;
1083 vdev->devid = 0;
1084
1085 usb_put_dev(vdev->udev);
1086 vdev->udev = NULL;
1087
1088 if (ud->tcp_socket) {
1089 sockfd_put(ud->tcp_socket);
1090 ud->tcp_socket = NULL;
1091 ud->sockfd = -1;
1092 }
1093 ud->status = VDEV_ST_NULL;
1094
1095 spin_unlock_irqrestore(&ud->lock, flags);
1096 }
1097
vhci_device_unusable(struct usbip_device * ud)1098 static void vhci_device_unusable(struct usbip_device *ud)
1099 {
1100 unsigned long flags;
1101
1102 spin_lock_irqsave(&ud->lock, flags);
1103 ud->status = VDEV_ST_ERROR;
1104 spin_unlock_irqrestore(&ud->lock, flags);
1105 }
1106
vhci_device_init(struct vhci_device * vdev)1107 static void vhci_device_init(struct vhci_device *vdev)
1108 {
1109 memset(vdev, 0, sizeof(struct vhci_device));
1110
1111 vdev->ud.side = USBIP_VHCI;
1112 vdev->ud.status = VDEV_ST_NULL;
1113 spin_lock_init(&vdev->ud.lock);
1114
1115 INIT_LIST_HEAD(&vdev->priv_rx);
1116 INIT_LIST_HEAD(&vdev->priv_tx);
1117 INIT_LIST_HEAD(&vdev->unlink_tx);
1118 INIT_LIST_HEAD(&vdev->unlink_rx);
1119 spin_lock_init(&vdev->priv_lock);
1120
1121 init_waitqueue_head(&vdev->waitq_tx);
1122
1123 vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
1124 vdev->ud.eh_ops.reset = vhci_device_reset;
1125 vdev->ud.eh_ops.unusable = vhci_device_unusable;
1126
1127 usbip_start_eh(&vdev->ud);
1128 }
1129
hcd_name_to_id(const char * name)1130 static int hcd_name_to_id(const char *name)
1131 {
1132 char *c;
1133 long val;
1134 int ret;
1135
1136 c = strchr(name, '.');
1137 if (c == NULL)
1138 return 0;
1139
1140 ret = kstrtol(c+1, 10, &val);
1141 if (ret < 0)
1142 return ret;
1143
1144 return val;
1145 }
1146
vhci_setup(struct usb_hcd * hcd)1147 static int vhci_setup(struct usb_hcd *hcd)
1148 {
1149 struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1150 if (usb_hcd_is_primary_hcd(hcd)) {
1151 vhci->vhci_hcd_hs = hcd_to_vhci_hcd(hcd);
1152 vhci->vhci_hcd_hs->vhci = vhci;
1153 /*
1154 * Mark the first roothub as being USB 2.0.
1155 * The USB 3.0 roothub will be registered later by
1156 * vhci_hcd_probe()
1157 */
1158 hcd->speed = HCD_USB2;
1159 hcd->self.root_hub->speed = USB_SPEED_HIGH;
1160 } else {
1161 vhci->vhci_hcd_ss = hcd_to_vhci_hcd(hcd);
1162 vhci->vhci_hcd_ss->vhci = vhci;
1163 hcd->speed = HCD_USB3;
1164 hcd->self.root_hub->speed = USB_SPEED_SUPER;
1165 }
1166
1167 /*
1168 * Support SG.
1169 * sg_tablesize is an arbitrary value to alleviate memory pressure
1170 * on the host.
1171 */
1172 hcd->self.sg_tablesize = 32;
1173 hcd->self.no_sg_constraint = 1;
1174
1175 return 0;
1176 }
1177
vhci_start(struct usb_hcd * hcd)1178 static int vhci_start(struct usb_hcd *hcd)
1179 {
1180 struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
1181 int id, rhport;
1182 int err;
1183
1184 usbip_dbg_vhci_hc("enter vhci_start\n");
1185
1186 if (usb_hcd_is_primary_hcd(hcd))
1187 spin_lock_init(&vhci_hcd->vhci->lock);
1188
1189 /* initialize private data of usb_hcd */
1190
1191 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1192 struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
1193
1194 vhci_device_init(vdev);
1195 vdev->rhport = rhport;
1196 }
1197
1198 atomic_set(&vhci_hcd->seqnum, 0);
1199
1200 hcd->power_budget = 0; /* no limit */
1201 hcd->uses_new_polling = 1;
1202
1203 #ifdef CONFIG_USB_OTG
1204 hcd->self.otg_port = 1;
1205 #endif
1206
1207 id = hcd_name_to_id(hcd_name(hcd));
1208 if (id < 0) {
1209 pr_err("invalid vhci name %s\n", hcd_name(hcd));
1210 return -EINVAL;
1211 }
1212
1213 /* vhci_hcd is now ready to be controlled through sysfs */
1214 if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
1215 err = vhci_init_attr_group();
1216 if (err) {
1217 pr_err("init attr group\n");
1218 return err;
1219 }
1220 err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
1221 if (err) {
1222 pr_err("create sysfs files\n");
1223 vhci_finish_attr_group();
1224 return err;
1225 }
1226 pr_info("created sysfs %s\n", hcd_name(hcd));
1227 }
1228
1229 return 0;
1230 }
1231
vhci_stop(struct usb_hcd * hcd)1232 static void vhci_stop(struct usb_hcd *hcd)
1233 {
1234 struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
1235 int id, rhport;
1236
1237 usbip_dbg_vhci_hc("stop VHCI controller\n");
1238
1239 /* 1. remove the userland interface of vhci_hcd */
1240 id = hcd_name_to_id(hcd_name(hcd));
1241 if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
1242 sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
1243 vhci_finish_attr_group();
1244 }
1245
1246 /* 2. shutdown all the ports of vhci_hcd */
1247 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1248 struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
1249
1250 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
1251 usbip_stop_eh(&vdev->ud);
1252 }
1253 }
1254
vhci_get_frame_number(struct usb_hcd * hcd)1255 static int vhci_get_frame_number(struct usb_hcd *hcd)
1256 {
1257 dev_err_ratelimited(&hcd->self.root_hub->dev, "Not yet implemented\n");
1258 return 0;
1259 }
1260
1261 #ifdef CONFIG_PM
1262
1263 /* FIXME: suspend/resume */
vhci_bus_suspend(struct usb_hcd * hcd)1264 static int vhci_bus_suspend(struct usb_hcd *hcd)
1265 {
1266 struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1267 unsigned long flags;
1268
1269 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1270
1271 spin_lock_irqsave(&vhci->lock, flags);
1272 hcd->state = HC_STATE_SUSPENDED;
1273 spin_unlock_irqrestore(&vhci->lock, flags);
1274
1275 return 0;
1276 }
1277
vhci_bus_resume(struct usb_hcd * hcd)1278 static int vhci_bus_resume(struct usb_hcd *hcd)
1279 {
1280 struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1281 int rc = 0;
1282 unsigned long flags;
1283
1284 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1285
1286 spin_lock_irqsave(&vhci->lock, flags);
1287 if (!HCD_HW_ACCESSIBLE(hcd))
1288 rc = -ESHUTDOWN;
1289 else
1290 hcd->state = HC_STATE_RUNNING;
1291 spin_unlock_irqrestore(&vhci->lock, flags);
1292
1293 return rc;
1294 }
1295
1296 #else
1297
1298 #define vhci_bus_suspend NULL
1299 #define vhci_bus_resume NULL
1300 #endif
1301
1302 /* Change a group of bulk endpoints to support multiple stream IDs */
vhci_alloc_streams(struct usb_hcd * hcd,struct usb_device * udev,struct usb_host_endpoint ** eps,unsigned int num_eps,unsigned int num_streams,gfp_t mem_flags)1303 static int vhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1304 struct usb_host_endpoint **eps, unsigned int num_eps,
1305 unsigned int num_streams, gfp_t mem_flags)
1306 {
1307 dev_dbg(&hcd->self.root_hub->dev, "vhci_alloc_streams not implemented\n");
1308 return 0;
1309 }
1310
1311 /* Reverts a group of bulk endpoints back to not using stream IDs. */
vhci_free_streams(struct usb_hcd * hcd,struct usb_device * udev,struct usb_host_endpoint ** eps,unsigned int num_eps,gfp_t mem_flags)1312 static int vhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
1313 struct usb_host_endpoint **eps, unsigned int num_eps,
1314 gfp_t mem_flags)
1315 {
1316 dev_dbg(&hcd->self.root_hub->dev, "vhci_free_streams not implemented\n");
1317 return 0;
1318 }
1319
1320 static const struct hc_driver vhci_hc_driver = {
1321 .description = driver_name,
1322 .product_desc = driver_desc,
1323 .hcd_priv_size = sizeof(struct vhci_hcd),
1324
1325 .flags = HCD_USB3 | HCD_SHARED,
1326
1327 .reset = vhci_setup,
1328 .start = vhci_start,
1329 .stop = vhci_stop,
1330
1331 .urb_enqueue = vhci_urb_enqueue,
1332 .urb_dequeue = vhci_urb_dequeue,
1333
1334 .get_frame_number = vhci_get_frame_number,
1335
1336 .hub_status_data = vhci_hub_status,
1337 .hub_control = vhci_hub_control,
1338 .bus_suspend = vhci_bus_suspend,
1339 .bus_resume = vhci_bus_resume,
1340
1341 .alloc_streams = vhci_alloc_streams,
1342 .free_streams = vhci_free_streams,
1343 };
1344
vhci_hcd_probe(struct platform_device * pdev)1345 static int vhci_hcd_probe(struct platform_device *pdev)
1346 {
1347 struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev));
1348 struct usb_hcd *hcd_hs;
1349 struct usb_hcd *hcd_ss;
1350 int ret;
1351
1352 usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1353
1354 /*
1355 * Allocate and initialize hcd.
1356 * Our private data is also allocated automatically.
1357 */
1358 hcd_hs = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1359 if (!hcd_hs) {
1360 pr_err("create primary hcd failed\n");
1361 return -ENOMEM;
1362 }
1363 hcd_hs->has_tt = 1;
1364
1365 /*
1366 * Finish generic HCD structure initialization and register.
1367 * Call the driver's reset() and start() routines.
1368 */
1369 ret = usb_add_hcd(hcd_hs, 0, 0);
1370 if (ret != 0) {
1371 pr_err("usb_add_hcd hs failed %d\n", ret);
1372 goto put_usb2_hcd;
1373 }
1374
1375 hcd_ss = usb_create_shared_hcd(&vhci_hc_driver, &pdev->dev,
1376 dev_name(&pdev->dev), hcd_hs);
1377 if (!hcd_ss) {
1378 ret = -ENOMEM;
1379 pr_err("create shared hcd failed\n");
1380 goto remove_usb2_hcd;
1381 }
1382
1383 ret = usb_add_hcd(hcd_ss, 0, 0);
1384 if (ret) {
1385 pr_err("usb_add_hcd ss failed %d\n", ret);
1386 goto put_usb3_hcd;
1387 }
1388
1389 usbip_dbg_vhci_hc("bye\n");
1390 return 0;
1391
1392 put_usb3_hcd:
1393 usb_put_hcd(hcd_ss);
1394 remove_usb2_hcd:
1395 usb_remove_hcd(hcd_hs);
1396 put_usb2_hcd:
1397 usb_put_hcd(hcd_hs);
1398 vhci->vhci_hcd_hs = NULL;
1399 vhci->vhci_hcd_ss = NULL;
1400 return ret;
1401 }
1402
vhci_hcd_remove(struct platform_device * pdev)1403 static int vhci_hcd_remove(struct platform_device *pdev)
1404 {
1405 struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev));
1406
1407 /*
1408 * Disconnects the root hub,
1409 * then reverses the effects of usb_add_hcd(),
1410 * invoking the HCD's stop() methods.
1411 */
1412 usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
1413 usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
1414
1415 usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
1416 usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
1417
1418 vhci->vhci_hcd_hs = NULL;
1419 vhci->vhci_hcd_ss = NULL;
1420
1421 return 0;
1422 }
1423
1424 #ifdef CONFIG_PM
1425
1426 /* what should happen for USB/IP under suspend/resume? */
vhci_hcd_suspend(struct platform_device * pdev,pm_message_t state)1427 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1428 {
1429 struct usb_hcd *hcd;
1430 struct vhci *vhci;
1431 int rhport;
1432 int connected = 0;
1433 int ret = 0;
1434 unsigned long flags;
1435
1436 dev_dbg(&pdev->dev, "%s\n", __func__);
1437
1438 hcd = platform_get_drvdata(pdev);
1439 if (!hcd)
1440 return 0;
1441
1442 vhci = *((void **)dev_get_platdata(hcd->self.controller));
1443
1444 spin_lock_irqsave(&vhci->lock, flags);
1445
1446 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1447 if (vhci->vhci_hcd_hs->port_status[rhport] &
1448 USB_PORT_STAT_CONNECTION)
1449 connected += 1;
1450
1451 if (vhci->vhci_hcd_ss->port_status[rhport] &
1452 USB_PORT_STAT_CONNECTION)
1453 connected += 1;
1454 }
1455
1456 spin_unlock_irqrestore(&vhci->lock, flags);
1457
1458 if (connected > 0) {
1459 dev_info(&pdev->dev,
1460 "We have %d active connection%s. Do not suspend.\n",
1461 connected, (connected == 1 ? "" : "s"));
1462 ret = -EBUSY;
1463 } else {
1464 dev_info(&pdev->dev, "suspend vhci_hcd");
1465 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1466 }
1467
1468 return ret;
1469 }
1470
vhci_hcd_resume(struct platform_device * pdev)1471 static int vhci_hcd_resume(struct platform_device *pdev)
1472 {
1473 struct usb_hcd *hcd;
1474
1475 dev_dbg(&pdev->dev, "%s\n", __func__);
1476
1477 hcd = platform_get_drvdata(pdev);
1478 if (!hcd)
1479 return 0;
1480 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1481 usb_hcd_poll_rh_status(hcd);
1482
1483 return 0;
1484 }
1485
1486 #else
1487
1488 #define vhci_hcd_suspend NULL
1489 #define vhci_hcd_resume NULL
1490
1491 #endif
1492
1493 static struct platform_driver vhci_driver = {
1494 .probe = vhci_hcd_probe,
1495 .remove = vhci_hcd_remove,
1496 .suspend = vhci_hcd_suspend,
1497 .resume = vhci_hcd_resume,
1498 .driver = {
1499 .name = driver_name,
1500 },
1501 };
1502
del_platform_devices(void)1503 static void del_platform_devices(void)
1504 {
1505 struct platform_device *pdev;
1506 int i;
1507
1508 for (i = 0; i < vhci_num_controllers; i++) {
1509 pdev = vhcis[i].pdev;
1510 if (pdev != NULL)
1511 platform_device_unregister(pdev);
1512 vhcis[i].pdev = NULL;
1513 }
1514 sysfs_remove_link(&platform_bus.kobj, driver_name);
1515 }
1516
vhci_hcd_init(void)1517 static int __init vhci_hcd_init(void)
1518 {
1519 int i, ret;
1520
1521 if (usb_disabled())
1522 return -ENODEV;
1523
1524 if (vhci_num_controllers < 1)
1525 vhci_num_controllers = 1;
1526
1527 vhcis = kcalloc(vhci_num_controllers, sizeof(struct vhci), GFP_KERNEL);
1528 if (vhcis == NULL)
1529 return -ENOMEM;
1530
1531 for (i = 0; i < vhci_num_controllers; i++) {
1532 vhcis[i].pdev = platform_device_alloc(driver_name, i);
1533 if (!vhcis[i].pdev) {
1534 i--;
1535 while (i >= 0)
1536 platform_device_put(vhcis[i--].pdev);
1537 ret = -ENOMEM;
1538 goto err_device_alloc;
1539 }
1540 }
1541 for (i = 0; i < vhci_num_controllers; i++) {
1542 void *vhci = &vhcis[i];
1543 ret = platform_device_add_data(vhcis[i].pdev, &vhci, sizeof(void *));
1544 if (ret)
1545 goto err_driver_register;
1546 }
1547
1548 ret = platform_driver_register(&vhci_driver);
1549 if (ret)
1550 goto err_driver_register;
1551
1552 for (i = 0; i < vhci_num_controllers; i++) {
1553 ret = platform_device_add(vhcis[i].pdev);
1554 if (ret < 0) {
1555 i--;
1556 while (i >= 0)
1557 platform_device_del(vhcis[i--].pdev);
1558 goto err_add_hcd;
1559 }
1560 }
1561
1562 return ret;
1563
1564 err_add_hcd:
1565 platform_driver_unregister(&vhci_driver);
1566 err_driver_register:
1567 for (i = 0; i < vhci_num_controllers; i++)
1568 platform_device_put(vhcis[i].pdev);
1569 err_device_alloc:
1570 kfree(vhcis);
1571 return ret;
1572 }
1573
vhci_hcd_exit(void)1574 static void __exit vhci_hcd_exit(void)
1575 {
1576 del_platform_devices();
1577 platform_driver_unregister(&vhci_driver);
1578 kfree(vhcis);
1579 }
1580
1581 module_init(vhci_hcd_init);
1582 module_exit(vhci_hcd_exit);
1583
1584 MODULE_AUTHOR(DRIVER_AUTHOR);
1585 MODULE_DESCRIPTION(DRIVER_DESC);
1586 MODULE_LICENSE("GPL");
1587