xref: /openbmc/linux/drivers/usb/host/xen-hcd.c (revision 9dd7c463)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * xen-hcd.c
4  *
5  * Xen USB Virtual Host Controller driver
6  *
7  * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
8  * Author: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/usb.h>
13 #include <linux/list.h>
14 #include <linux/usb/hcd.h>
15 #include <linux/io.h>
16 
17 #include <xen/xen.h>
18 #include <xen/xenbus.h>
19 #include <xen/grant_table.h>
20 #include <xen/events.h>
21 #include <xen/page.h>
22 
23 #include <xen/interface/io/usbif.h>
24 
25 /* Private per-URB data */
26 struct urb_priv {
27 	struct list_head list;
28 	struct urb *urb;
29 	int req_id;		/* RING_REQUEST id for submitting */
30 	int unlink_req_id;	/* RING_REQUEST id for unlinking */
31 	int status;
32 	bool unlinked;		/* dequeued marker */
33 };
34 
35 /* virtual roothub port status */
36 struct rhport_status {
37 	__u32 status;
38 	bool resuming;		/* in resuming */
39 	bool c_connection;	/* connection changed */
40 	unsigned long timeout;
41 };
42 
43 /* status of attached device */
44 struct vdevice_status {
45 	int devnum;
46 	enum usb_device_state status;
47 	enum usb_device_speed speed;
48 };
49 
50 /* RING request shadow */
51 struct usb_shadow {
52 	struct xenusb_urb_request req;
53 	struct urb *urb;
54 };
55 
56 struct xenhcd_info {
57 	/* Virtual Host Controller has 4 urb queues */
58 	struct list_head pending_submit_list;
59 	struct list_head pending_unlink_list;
60 	struct list_head in_progress_list;
61 	struct list_head giveback_waiting_list;
62 
63 	spinlock_t lock;
64 
65 	/* timer that kick pending and giveback waiting urbs */
66 	struct timer_list watchdog;
67 	unsigned long actions;
68 
69 	/* virtual root hub */
70 	int rh_numports;
71 	struct rhport_status ports[XENUSB_MAX_PORTNR];
72 	struct vdevice_status devices[XENUSB_MAX_PORTNR];
73 
74 	/* Xen related staff */
75 	struct xenbus_device *xbdev;
76 	int urb_ring_ref;
77 	int conn_ring_ref;
78 	struct xenusb_urb_front_ring urb_ring;
79 	struct xenusb_conn_front_ring conn_ring;
80 
81 	unsigned int evtchn;
82 	unsigned int irq;
83 	struct usb_shadow shadow[XENUSB_URB_RING_SIZE];
84 	unsigned int shadow_free;
85 
86 	bool error;
87 };
88 
89 #define GRANT_INVALID_REF 0
90 
91 #define XENHCD_RING_JIFFIES (HZ/200)
92 #define XENHCD_SCAN_JIFFIES 1
93 
94 enum xenhcd_timer_action {
95 	TIMER_RING_WATCHDOG,
96 	TIMER_SCAN_PENDING_URBS,
97 };
98 
99 static struct kmem_cache *xenhcd_urbp_cachep;
100 
101 static inline struct xenhcd_info *xenhcd_hcd_to_info(struct usb_hcd *hcd)
102 {
103 	return (struct xenhcd_info *)hcd->hcd_priv;
104 }
105 
106 static inline struct usb_hcd *xenhcd_info_to_hcd(struct xenhcd_info *info)
107 {
108 	return container_of((void *)info, struct usb_hcd, hcd_priv);
109 }
110 
111 static void xenhcd_set_error(struct xenhcd_info *info, const char *msg)
112 {
113 	info->error = true;
114 
115 	pr_alert("xen-hcd: protocol error: %s!\n", msg);
116 }
117 
118 static inline void xenhcd_timer_action_done(struct xenhcd_info *info,
119 					    enum xenhcd_timer_action action)
120 {
121 	clear_bit(action, &info->actions);
122 }
123 
124 static void xenhcd_timer_action(struct xenhcd_info *info,
125 				enum xenhcd_timer_action action)
126 {
127 	if (timer_pending(&info->watchdog) &&
128 	    test_bit(TIMER_SCAN_PENDING_URBS, &info->actions))
129 		return;
130 
131 	if (!test_and_set_bit(action, &info->actions)) {
132 		unsigned long t;
133 
134 		switch (action) {
135 		case TIMER_RING_WATCHDOG:
136 			t = XENHCD_RING_JIFFIES;
137 			break;
138 		default:
139 			t = XENHCD_SCAN_JIFFIES;
140 			break;
141 		}
142 		mod_timer(&info->watchdog, t + jiffies);
143 	}
144 }
145 
146 /*
147  * set virtual port connection status
148  */
149 static void xenhcd_set_connect_state(struct xenhcd_info *info, int portnum)
150 {
151 	int port;
152 
153 	port = portnum - 1;
154 	if (info->ports[port].status & USB_PORT_STAT_POWER) {
155 		switch (info->devices[port].speed) {
156 		case XENUSB_SPEED_NONE:
157 			info->ports[port].status &=
158 				~(USB_PORT_STAT_CONNECTION |
159 				  USB_PORT_STAT_ENABLE |
160 				  USB_PORT_STAT_LOW_SPEED |
161 				  USB_PORT_STAT_HIGH_SPEED |
162 				  USB_PORT_STAT_SUSPEND);
163 			break;
164 		case XENUSB_SPEED_LOW:
165 			info->ports[port].status |= USB_PORT_STAT_CONNECTION;
166 			info->ports[port].status |= USB_PORT_STAT_LOW_SPEED;
167 			break;
168 		case XENUSB_SPEED_FULL:
169 			info->ports[port].status |= USB_PORT_STAT_CONNECTION;
170 			break;
171 		case XENUSB_SPEED_HIGH:
172 			info->ports[port].status |= USB_PORT_STAT_CONNECTION;
173 			info->ports[port].status |= USB_PORT_STAT_HIGH_SPEED;
174 			break;
175 		default: /* error */
176 			return;
177 		}
178 		info->ports[port].status |= (USB_PORT_STAT_C_CONNECTION << 16);
179 	}
180 }
181 
182 /*
183  * set virtual device connection status
184  */
185 static int xenhcd_rhport_connect(struct xenhcd_info *info, __u8 portnum,
186 				 __u8 speed)
187 {
188 	int port;
189 
190 	if (portnum < 1 || portnum > info->rh_numports)
191 		return -EINVAL; /* invalid port number */
192 
193 	port = portnum - 1;
194 	if (info->devices[port].speed != speed) {
195 		switch (speed) {
196 		case XENUSB_SPEED_NONE: /* disconnect */
197 			info->devices[port].status = USB_STATE_NOTATTACHED;
198 			break;
199 		case XENUSB_SPEED_LOW:
200 		case XENUSB_SPEED_FULL:
201 		case XENUSB_SPEED_HIGH:
202 			info->devices[port].status = USB_STATE_ATTACHED;
203 			break;
204 		default: /* error */
205 			return -EINVAL;
206 		}
207 		info->devices[port].speed = speed;
208 		info->ports[port].c_connection = true;
209 
210 		xenhcd_set_connect_state(info, portnum);
211 	}
212 
213 	return 0;
214 }
215 
216 /*
217  * SetPortFeature(PORT_SUSPENDED)
218  */
219 static void xenhcd_rhport_suspend(struct xenhcd_info *info, int portnum)
220 {
221 	int port;
222 
223 	port = portnum - 1;
224 	info->ports[port].status |= USB_PORT_STAT_SUSPEND;
225 	info->devices[port].status = USB_STATE_SUSPENDED;
226 }
227 
228 /*
229  * ClearPortFeature(PORT_SUSPENDED)
230  */
231 static void xenhcd_rhport_resume(struct xenhcd_info *info, int portnum)
232 {
233 	int port;
234 
235 	port = portnum - 1;
236 	if (info->ports[port].status & USB_PORT_STAT_SUSPEND) {
237 		info->ports[port].resuming = true;
238 		info->ports[port].timeout = jiffies + msecs_to_jiffies(20);
239 	}
240 }
241 
242 /*
243  * SetPortFeature(PORT_POWER)
244  */
245 static void xenhcd_rhport_power_on(struct xenhcd_info *info, int portnum)
246 {
247 	int port;
248 
249 	port = portnum - 1;
250 	if ((info->ports[port].status & USB_PORT_STAT_POWER) == 0) {
251 		info->ports[port].status |= USB_PORT_STAT_POWER;
252 		if (info->devices[port].status != USB_STATE_NOTATTACHED)
253 			info->devices[port].status = USB_STATE_POWERED;
254 		if (info->ports[port].c_connection)
255 			xenhcd_set_connect_state(info, portnum);
256 	}
257 }
258 
259 /*
260  * ClearPortFeature(PORT_POWER)
261  * SetConfiguration(non-zero)
262  * Power_Source_Off
263  * Over-current
264  */
265 static void xenhcd_rhport_power_off(struct xenhcd_info *info, int portnum)
266 {
267 	int port;
268 
269 	port = portnum - 1;
270 	if (info->ports[port].status & USB_PORT_STAT_POWER) {
271 		info->ports[port].status = 0;
272 		if (info->devices[port].status != USB_STATE_NOTATTACHED)
273 			info->devices[port].status = USB_STATE_ATTACHED;
274 	}
275 }
276 
277 /*
278  * ClearPortFeature(PORT_ENABLE)
279  */
280 static void xenhcd_rhport_disable(struct xenhcd_info *info, int portnum)
281 {
282 	int port;
283 
284 	port = portnum - 1;
285 	info->ports[port].status &= ~USB_PORT_STAT_ENABLE;
286 	info->ports[port].status &= ~USB_PORT_STAT_SUSPEND;
287 	info->ports[port].resuming = false;
288 	if (info->devices[port].status != USB_STATE_NOTATTACHED)
289 		info->devices[port].status = USB_STATE_POWERED;
290 }
291 
292 /*
293  * SetPortFeature(PORT_RESET)
294  */
295 static void xenhcd_rhport_reset(struct xenhcd_info *info, int portnum)
296 {
297 	int port;
298 
299 	port = portnum - 1;
300 	info->ports[port].status &= ~(USB_PORT_STAT_ENABLE |
301 				      USB_PORT_STAT_LOW_SPEED |
302 				      USB_PORT_STAT_HIGH_SPEED);
303 	info->ports[port].status |= USB_PORT_STAT_RESET;
304 
305 	if (info->devices[port].status != USB_STATE_NOTATTACHED)
306 		info->devices[port].status = USB_STATE_ATTACHED;
307 
308 	/* 10msec reset signaling */
309 	info->ports[port].timeout = jiffies + msecs_to_jiffies(10);
310 }
311 
312 #ifdef CONFIG_PM
313 static int xenhcd_bus_suspend(struct usb_hcd *hcd)
314 {
315 	struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
316 	int ret = 0;
317 	int i, ports;
318 
319 	ports = info->rh_numports;
320 
321 	spin_lock_irq(&info->lock);
322 	if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
323 		ret = -ESHUTDOWN;
324 	} else {
325 		/* suspend any active ports*/
326 		for (i = 1; i <= ports; i++)
327 			xenhcd_rhport_suspend(info, i);
328 	}
329 	spin_unlock_irq(&info->lock);
330 
331 	del_timer_sync(&info->watchdog);
332 
333 	return ret;
334 }
335 
336 static int xenhcd_bus_resume(struct usb_hcd *hcd)
337 {
338 	struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
339 	int ret = 0;
340 	int i, ports;
341 
342 	ports = info->rh_numports;
343 
344 	spin_lock_irq(&info->lock);
345 	if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
346 		ret = -ESHUTDOWN;
347 	} else {
348 		/* resume any suspended ports*/
349 		for (i = 1; i <= ports; i++)
350 			xenhcd_rhport_resume(info, i);
351 	}
352 	spin_unlock_irq(&info->lock);
353 
354 	return ret;
355 }
356 #endif
357 
358 static void xenhcd_hub_descriptor(struct xenhcd_info *info,
359 				  struct usb_hub_descriptor *desc)
360 {
361 	__u16 temp;
362 	int ports = info->rh_numports;
363 
364 	desc->bDescriptorType = 0x29;
365 	desc->bPwrOn2PwrGood = 10; /* EHCI says 20ms max */
366 	desc->bHubContrCurrent = 0;
367 	desc->bNbrPorts = ports;
368 
369 	/* size of DeviceRemovable and PortPwrCtrlMask fields */
370 	temp = 1 + (ports / 8);
371 	desc->bDescLength = 7 + 2 * temp;
372 
373 	/* bitmaps for DeviceRemovable and PortPwrCtrlMask */
374 	memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
375 	memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
376 
377 	/* per-port over current reporting and no power switching */
378 	temp = 0x000a;
379 	desc->wHubCharacteristics = cpu_to_le16(temp);
380 }
381 
382 /* port status change mask for hub_status_data */
383 #define PORT_C_MASK	((USB_PORT_STAT_C_CONNECTION |		\
384 			  USB_PORT_STAT_C_ENABLE |		\
385 			  USB_PORT_STAT_C_SUSPEND |		\
386 			  USB_PORT_STAT_C_OVERCURRENT |		\
387 			  USB_PORT_STAT_C_RESET) << 16)
388 
389 /*
390  * See USB 2.0 Spec, 11.12.4 Hub and Port Status Change Bitmap.
391  * If port status changed, writes the bitmap to buf and return
392  * that length(number of bytes).
393  * If Nothing changed, return 0.
394  */
395 static int xenhcd_hub_status_data(struct usb_hcd *hcd, char *buf)
396 {
397 	struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
398 	int ports;
399 	int i;
400 	unsigned long flags;
401 	int ret;
402 	int changed = 0;
403 
404 	/* initialize the status to no-changes */
405 	ports = info->rh_numports;
406 	ret = 1 + (ports / 8);
407 	memset(buf, 0, ret);
408 
409 	spin_lock_irqsave(&info->lock, flags);
410 
411 	for (i = 0; i < ports; i++) {
412 		/* check status for each port */
413 		if (info->ports[i].status & PORT_C_MASK) {
414 			buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
415 			changed = 1;
416 		}
417 	}
418 
419 	if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
420 		usb_hcd_resume_root_hub(hcd);
421 
422 	spin_unlock_irqrestore(&info->lock, flags);
423 
424 	return changed ? ret : 0;
425 }
426 
427 static int xenhcd_hub_control(struct usb_hcd *hcd, __u16 typeReq, __u16 wValue,
428 			      __u16 wIndex, char *buf, __u16 wLength)
429 {
430 	struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
431 	int ports = info->rh_numports;
432 	unsigned long flags;
433 	int ret = 0;
434 	int i;
435 	int changed = 0;
436 
437 	spin_lock_irqsave(&info->lock, flags);
438 	switch (typeReq) {
439 	case ClearHubFeature:
440 		/* ignore this request */
441 		break;
442 	case ClearPortFeature:
443 		if (!wIndex || wIndex > ports)
444 			goto error;
445 
446 		switch (wValue) {
447 		case USB_PORT_FEAT_SUSPEND:
448 			xenhcd_rhport_resume(info, wIndex);
449 			break;
450 		case USB_PORT_FEAT_POWER:
451 			xenhcd_rhport_power_off(info, wIndex);
452 			break;
453 		case USB_PORT_FEAT_ENABLE:
454 			xenhcd_rhport_disable(info, wIndex);
455 			break;
456 		case USB_PORT_FEAT_C_CONNECTION:
457 			info->ports[wIndex - 1].c_connection = false;
458 			fallthrough;
459 		default:
460 			info->ports[wIndex - 1].status &= ~(1 << wValue);
461 			break;
462 		}
463 		break;
464 	case GetHubDescriptor:
465 		xenhcd_hub_descriptor(info, (struct usb_hub_descriptor *)buf);
466 		break;
467 	case GetHubStatus:
468 		/* always local power supply good and no over-current exists. */
469 		*(__le32 *)buf = cpu_to_le32(0);
470 		break;
471 	case GetPortStatus:
472 		if (!wIndex || wIndex > ports)
473 			goto error;
474 
475 		wIndex--;
476 
477 		/* resume completion */
478 		if (info->ports[wIndex].resuming &&
479 		    time_after_eq(jiffies, info->ports[wIndex].timeout)) {
480 			info->ports[wIndex].status |=
481 				USB_PORT_STAT_C_SUSPEND << 16;
482 			info->ports[wIndex].status &= ~USB_PORT_STAT_SUSPEND;
483 		}
484 
485 		/* reset completion */
486 		if ((info->ports[wIndex].status & USB_PORT_STAT_RESET) != 0 &&
487 		    time_after_eq(jiffies, info->ports[wIndex].timeout)) {
488 			info->ports[wIndex].status |=
489 				USB_PORT_STAT_C_RESET << 16;
490 			info->ports[wIndex].status &= ~USB_PORT_STAT_RESET;
491 
492 			if (info->devices[wIndex].status !=
493 			    USB_STATE_NOTATTACHED) {
494 				info->ports[wIndex].status |=
495 					USB_PORT_STAT_ENABLE;
496 				info->devices[wIndex].status =
497 					USB_STATE_DEFAULT;
498 			}
499 
500 			switch (info->devices[wIndex].speed) {
501 			case XENUSB_SPEED_LOW:
502 				info->ports[wIndex].status |=
503 					USB_PORT_STAT_LOW_SPEED;
504 				break;
505 			case XENUSB_SPEED_HIGH:
506 				info->ports[wIndex].status |=
507 					USB_PORT_STAT_HIGH_SPEED;
508 				break;
509 			default:
510 				break;
511 			}
512 		}
513 
514 		*(__le32 *)buf = cpu_to_le32(info->ports[wIndex].status);
515 		break;
516 	case SetPortFeature:
517 		if (!wIndex || wIndex > ports)
518 			goto error;
519 
520 		switch (wValue) {
521 		case USB_PORT_FEAT_POWER:
522 			xenhcd_rhport_power_on(info, wIndex);
523 			break;
524 		case USB_PORT_FEAT_RESET:
525 			xenhcd_rhport_reset(info, wIndex);
526 			break;
527 		case USB_PORT_FEAT_SUSPEND:
528 			xenhcd_rhport_suspend(info, wIndex);
529 			break;
530 		default:
531 			if (info->ports[wIndex-1].status & USB_PORT_STAT_POWER)
532 				info->ports[wIndex-1].status |= (1 << wValue);
533 		}
534 		break;
535 
536 	case SetHubFeature:
537 		/* not supported */
538 	default:
539 error:
540 		ret = -EPIPE;
541 	}
542 	spin_unlock_irqrestore(&info->lock, flags);
543 
544 	/* check status for each port */
545 	for (i = 0; i < ports; i++) {
546 		if (info->ports[i].status & PORT_C_MASK)
547 			changed = 1;
548 	}
549 	if (changed)
550 		usb_hcd_poll_rh_status(hcd);
551 
552 	return ret;
553 }
554 
555 static void xenhcd_free_urb_priv(struct urb_priv *urbp)
556 {
557 	urbp->urb->hcpriv = NULL;
558 	kmem_cache_free(xenhcd_urbp_cachep, urbp);
559 }
560 
561 static inline unsigned int xenhcd_get_id_from_freelist(struct xenhcd_info *info)
562 {
563 	unsigned int free;
564 
565 	free = info->shadow_free;
566 	info->shadow_free = info->shadow[free].req.id;
567 	info->shadow[free].req.id = 0x0fff; /* debug */
568 	return free;
569 }
570 
571 static inline void xenhcd_add_id_to_freelist(struct xenhcd_info *info,
572 					     unsigned int id)
573 {
574 	info->shadow[id].req.id	= info->shadow_free;
575 	info->shadow[id].urb = NULL;
576 	info->shadow_free = id;
577 }
578 
579 static inline int xenhcd_count_pages(void *addr, int length)
580 {
581 	unsigned long vaddr = (unsigned long)addr;
582 
583 	return PFN_UP(vaddr + length) - PFN_DOWN(vaddr);
584 }
585 
586 static void xenhcd_gnttab_map(struct xenhcd_info *info, void *addr, int length,
587 			      grant_ref_t *gref_head,
588 			      struct xenusb_request_segment *seg,
589 			      int nr_pages, int flags)
590 {
591 	grant_ref_t ref;
592 	unsigned long buffer_mfn;
593 	unsigned int offset;
594 	unsigned int len = length;
595 	unsigned int bytes;
596 	int i;
597 
598 	for (i = 0; i < nr_pages; i++) {
599 		buffer_mfn = PFN_DOWN(arbitrary_virt_to_machine(addr).maddr);
600 		offset = offset_in_page(addr);
601 
602 		bytes = PAGE_SIZE - offset;
603 		if (bytes > len)
604 			bytes = len;
605 
606 		ref = gnttab_claim_grant_reference(gref_head);
607 		gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
608 						buffer_mfn, flags);
609 		seg[i].gref = ref;
610 		seg[i].offset = (__u16)offset;
611 		seg[i].length = (__u16)bytes;
612 
613 		addr += bytes;
614 		len -= bytes;
615 	}
616 }
617 
618 static __u32 xenhcd_pipe_urb_to_xenusb(__u32 urb_pipe, __u8 port)
619 {
620 	static __u32 pipe;
621 
622 	pipe = usb_pipedevice(urb_pipe) << XENUSB_PIPE_DEV_SHIFT;
623 	pipe |= usb_pipeendpoint(urb_pipe) << XENUSB_PIPE_EP_SHIFT;
624 	if (usb_pipein(urb_pipe))
625 		pipe |= XENUSB_PIPE_DIR;
626 	switch (usb_pipetype(urb_pipe)) {
627 	case PIPE_ISOCHRONOUS:
628 		pipe |= XENUSB_PIPE_TYPE_ISOC << XENUSB_PIPE_TYPE_SHIFT;
629 		break;
630 	case PIPE_INTERRUPT:
631 		pipe |= XENUSB_PIPE_TYPE_INT << XENUSB_PIPE_TYPE_SHIFT;
632 		break;
633 	case PIPE_CONTROL:
634 		pipe |= XENUSB_PIPE_TYPE_CTRL << XENUSB_PIPE_TYPE_SHIFT;
635 		break;
636 	case PIPE_BULK:
637 		pipe |= XENUSB_PIPE_TYPE_BULK << XENUSB_PIPE_TYPE_SHIFT;
638 		break;
639 	}
640 	pipe = xenusb_setportnum_pipe(pipe, port);
641 
642 	return pipe;
643 }
644 
645 static int xenhcd_map_urb_for_request(struct xenhcd_info *info, struct urb *urb,
646 				      struct xenusb_urb_request *req)
647 {
648 	grant_ref_t gref_head;
649 	int nr_buff_pages = 0;
650 	int nr_isodesc_pages = 0;
651 	int nr_grants = 0;
652 
653 	if (urb->transfer_buffer_length) {
654 		nr_buff_pages = xenhcd_count_pages(urb->transfer_buffer,
655 						urb->transfer_buffer_length);
656 
657 		if (usb_pipeisoc(urb->pipe))
658 			nr_isodesc_pages = xenhcd_count_pages(
659 				&urb->iso_frame_desc[0],
660 				sizeof(struct usb_iso_packet_descriptor) *
661 				urb->number_of_packets);
662 
663 		nr_grants = nr_buff_pages + nr_isodesc_pages;
664 		if (nr_grants > XENUSB_MAX_SEGMENTS_PER_REQUEST) {
665 			pr_err("xenhcd: error: %d grants\n", nr_grants);
666 			return -E2BIG;
667 		}
668 
669 		if (gnttab_alloc_grant_references(nr_grants, &gref_head)) {
670 			pr_err("xenhcd: gnttab_alloc_grant_references() error\n");
671 			return -ENOMEM;
672 		}
673 
674 		xenhcd_gnttab_map(info, urb->transfer_buffer,
675 				  urb->transfer_buffer_length, &gref_head,
676 				  &req->seg[0], nr_buff_pages,
677 				  usb_pipein(urb->pipe) ? 0 : GTF_readonly);
678 	}
679 
680 	req->pipe = xenhcd_pipe_urb_to_xenusb(urb->pipe, urb->dev->portnum);
681 	req->transfer_flags = 0;
682 	if (urb->transfer_flags & URB_SHORT_NOT_OK)
683 		req->transfer_flags |= XENUSB_SHORT_NOT_OK;
684 	req->buffer_length = urb->transfer_buffer_length;
685 	req->nr_buffer_segs = nr_buff_pages;
686 
687 	switch (usb_pipetype(urb->pipe)) {
688 	case PIPE_ISOCHRONOUS:
689 		req->u.isoc.interval = urb->interval;
690 		req->u.isoc.start_frame = urb->start_frame;
691 		req->u.isoc.number_of_packets = urb->number_of_packets;
692 		req->u.isoc.nr_frame_desc_segs = nr_isodesc_pages;
693 
694 		xenhcd_gnttab_map(info, &urb->iso_frame_desc[0],
695 				  sizeof(struct usb_iso_packet_descriptor) *
696 				  urb->number_of_packets,
697 				  &gref_head, &req->seg[nr_buff_pages],
698 				  nr_isodesc_pages, 0);
699 		break;
700 	case PIPE_INTERRUPT:
701 		req->u.intr.interval = urb->interval;
702 		break;
703 	case PIPE_CONTROL:
704 		if (urb->setup_packet)
705 			memcpy(req->u.ctrl, urb->setup_packet, 8);
706 		break;
707 	case PIPE_BULK:
708 		break;
709 	default:
710 		break;
711 	}
712 
713 	if (nr_grants)
714 		gnttab_free_grant_references(gref_head);
715 
716 	return 0;
717 }
718 
719 static void xenhcd_gnttab_done(struct usb_shadow *shadow)
720 {
721 	int nr_segs = 0;
722 	int i;
723 
724 	nr_segs = shadow->req.nr_buffer_segs;
725 
726 	if (xenusb_pipeisoc(shadow->req.pipe))
727 		nr_segs += shadow->req.u.isoc.nr_frame_desc_segs;
728 
729 	for (i = 0; i < nr_segs; i++)
730 		gnttab_end_foreign_access(shadow->req.seg[i].gref, 0, 0UL);
731 
732 	shadow->req.nr_buffer_segs = 0;
733 	shadow->req.u.isoc.nr_frame_desc_segs = 0;
734 }
735 
736 static int xenhcd_translate_status(int status)
737 {
738 	switch (status) {
739 	case XENUSB_STATUS_OK:
740 		return 0;
741 	case XENUSB_STATUS_NODEV:
742 		return -ENODEV;
743 	case XENUSB_STATUS_INVAL:
744 		return -EINVAL;
745 	case XENUSB_STATUS_STALL:
746 		return -EPIPE;
747 	case XENUSB_STATUS_IOERROR:
748 		return -EPROTO;
749 	case XENUSB_STATUS_BABBLE:
750 		return -EOVERFLOW;
751 	default:
752 		return -ESHUTDOWN;
753 	}
754 }
755 
756 static void xenhcd_giveback_urb(struct xenhcd_info *info, struct urb *urb,
757 				int status)
758 {
759 	struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
760 	int priv_status = urbp->status;
761 
762 	list_del_init(&urbp->list);
763 	xenhcd_free_urb_priv(urbp);
764 
765 	if (urb->status == -EINPROGRESS)
766 		urb->status = xenhcd_translate_status(status);
767 
768 	spin_unlock(&info->lock);
769 	usb_hcd_giveback_urb(xenhcd_info_to_hcd(info), urb,
770 			     priv_status <= 0 ? priv_status : urb->status);
771 	spin_lock(&info->lock);
772 }
773 
774 static int xenhcd_do_request(struct xenhcd_info *info, struct urb_priv *urbp)
775 {
776 	struct xenusb_urb_request *req;
777 	struct urb *urb = urbp->urb;
778 	unsigned int id;
779 	int notify;
780 	int ret;
781 
782 	id = xenhcd_get_id_from_freelist(info);
783 	req = &info->shadow[id].req;
784 	req->id = id;
785 
786 	if (unlikely(urbp->unlinked)) {
787 		req->u.unlink.unlink_id = urbp->req_id;
788 		req->pipe = xenusb_setunlink_pipe(xenhcd_pipe_urb_to_xenusb(
789 						 urb->pipe, urb->dev->portnum));
790 		urbp->unlink_req_id = id;
791 	} else {
792 		ret = xenhcd_map_urb_for_request(info, urb, req);
793 		if (ret) {
794 			xenhcd_add_id_to_freelist(info, id);
795 			return ret;
796 		}
797 		urbp->req_id = id;
798 	}
799 
800 	req = RING_GET_REQUEST(&info->urb_ring, info->urb_ring.req_prod_pvt);
801 	*req = info->shadow[id].req;
802 
803 	info->urb_ring.req_prod_pvt++;
804 	info->shadow[id].urb = urb;
805 
806 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->urb_ring, notify);
807 	if (notify)
808 		notify_remote_via_irq(info->irq);
809 
810 	return 0;
811 }
812 
813 static void xenhcd_kick_pending_urbs(struct xenhcd_info *info)
814 {
815 	struct urb_priv *urbp;
816 
817 	while (!list_empty(&info->pending_submit_list)) {
818 		if (RING_FULL(&info->urb_ring)) {
819 			xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
820 			return;
821 		}
822 
823 		urbp = list_entry(info->pending_submit_list.next,
824 				  struct urb_priv, list);
825 		if (!xenhcd_do_request(info, urbp))
826 			list_move_tail(&urbp->list, &info->in_progress_list);
827 		else
828 			xenhcd_giveback_urb(info, urbp->urb, -ESHUTDOWN);
829 	}
830 	xenhcd_timer_action_done(info, TIMER_SCAN_PENDING_URBS);
831 }
832 
833 /*
834  * caller must lock info->lock
835  */
836 static void xenhcd_cancel_all_enqueued_urbs(struct xenhcd_info *info)
837 {
838 	struct urb_priv *urbp, *tmp;
839 	int req_id;
840 
841 	list_for_each_entry_safe(urbp, tmp, &info->in_progress_list, list) {
842 		req_id = urbp->req_id;
843 		if (!urbp->unlinked) {
844 			xenhcd_gnttab_done(&info->shadow[req_id]);
845 			if (urbp->urb->status == -EINPROGRESS)
846 				/* not dequeued */
847 				xenhcd_giveback_urb(info, urbp->urb,
848 						    -ESHUTDOWN);
849 			else	/* dequeued */
850 				xenhcd_giveback_urb(info, urbp->urb,
851 						    urbp->urb->status);
852 		}
853 		info->shadow[req_id].urb = NULL;
854 	}
855 
856 	list_for_each_entry_safe(urbp, tmp, &info->pending_submit_list, list)
857 		xenhcd_giveback_urb(info, urbp->urb, -ESHUTDOWN);
858 }
859 
860 /*
861  * caller must lock info->lock
862  */
863 static void xenhcd_giveback_unlinked_urbs(struct xenhcd_info *info)
864 {
865 	struct urb_priv *urbp, *tmp;
866 
867 	list_for_each_entry_safe(urbp, tmp, &info->giveback_waiting_list, list)
868 		xenhcd_giveback_urb(info, urbp->urb, urbp->urb->status);
869 }
870 
871 static int xenhcd_submit_urb(struct xenhcd_info *info, struct urb_priv *urbp)
872 {
873 	int ret;
874 
875 	if (RING_FULL(&info->urb_ring)) {
876 		list_add_tail(&urbp->list, &info->pending_submit_list);
877 		xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
878 		return 0;
879 	}
880 
881 	if (!list_empty(&info->pending_submit_list)) {
882 		list_add_tail(&urbp->list, &info->pending_submit_list);
883 		xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
884 		return 0;
885 	}
886 
887 	ret = xenhcd_do_request(info, urbp);
888 	if (ret == 0)
889 		list_add_tail(&urbp->list, &info->in_progress_list);
890 
891 	return ret;
892 }
893 
894 static int xenhcd_unlink_urb(struct xenhcd_info *info, struct urb_priv *urbp)
895 {
896 	int ret;
897 
898 	/* already unlinked? */
899 	if (urbp->unlinked)
900 		return -EBUSY;
901 
902 	urbp->unlinked = true;
903 
904 	/* the urb is still in pending_submit queue */
905 	if (urbp->req_id == ~0) {
906 		list_move_tail(&urbp->list, &info->giveback_waiting_list);
907 		xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
908 		return 0;
909 	}
910 
911 	/* send unlink request to backend */
912 	if (RING_FULL(&info->urb_ring)) {
913 		list_move_tail(&urbp->list, &info->pending_unlink_list);
914 		xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
915 		return 0;
916 	}
917 
918 	if (!list_empty(&info->pending_unlink_list)) {
919 		list_move_tail(&urbp->list, &info->pending_unlink_list);
920 		xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
921 		return 0;
922 	}
923 
924 	ret = xenhcd_do_request(info, urbp);
925 	if (ret == 0)
926 		list_move_tail(&urbp->list, &info->in_progress_list);
927 
928 	return ret;
929 }
930 
931 static int xenhcd_urb_request_done(struct xenhcd_info *info)
932 {
933 	struct xenusb_urb_response res;
934 	struct urb *urb;
935 	RING_IDX i, rp;
936 	__u16 id;
937 	int more_to_do = 0;
938 	unsigned long flags;
939 
940 	spin_lock_irqsave(&info->lock, flags);
941 
942 	rp = info->urb_ring.sring->rsp_prod;
943 	if (RING_RESPONSE_PROD_OVERFLOW(&info->urb_ring, rp)) {
944 		xenhcd_set_error(info, "Illegal index on urb-ring");
945 		spin_unlock_irqrestore(&info->lock, flags);
946 		return 0;
947 	}
948 	rmb(); /* ensure we see queued responses up to "rp" */
949 
950 	for (i = info->urb_ring.rsp_cons; i != rp; i++) {
951 		RING_COPY_RESPONSE(&info->urb_ring, i, &res);
952 		id = res.id;
953 		if (id >= XENUSB_URB_RING_SIZE) {
954 			xenhcd_set_error(info, "Illegal data on urb-ring");
955 			continue;
956 		}
957 
958 		if (likely(xenusb_pipesubmit(info->shadow[id].req.pipe))) {
959 			xenhcd_gnttab_done(&info->shadow[id]);
960 			urb = info->shadow[id].urb;
961 			if (likely(urb)) {
962 				urb->actual_length = res.actual_length;
963 				urb->error_count = res.error_count;
964 				urb->start_frame = res.start_frame;
965 				xenhcd_giveback_urb(info, urb, res.status);
966 			}
967 		}
968 
969 		xenhcd_add_id_to_freelist(info, id);
970 	}
971 	info->urb_ring.rsp_cons = i;
972 
973 	if (i != info->urb_ring.req_prod_pvt)
974 		RING_FINAL_CHECK_FOR_RESPONSES(&info->urb_ring, more_to_do);
975 	else
976 		info->urb_ring.sring->rsp_event = i + 1;
977 
978 	spin_unlock_irqrestore(&info->lock, flags);
979 
980 	return more_to_do;
981 }
982 
983 static int xenhcd_conn_notify(struct xenhcd_info *info)
984 {
985 	struct xenusb_conn_response res;
986 	struct xenusb_conn_request *req;
987 	RING_IDX rc, rp;
988 	__u16 id;
989 	__u8 portnum, speed;
990 	int more_to_do = 0;
991 	int notify;
992 	int port_changed = 0;
993 	unsigned long flags;
994 
995 	spin_lock_irqsave(&info->lock, flags);
996 
997 	rc = info->conn_ring.rsp_cons;
998 	rp = info->conn_ring.sring->rsp_prod;
999 	if (RING_RESPONSE_PROD_OVERFLOW(&info->conn_ring, rp)) {
1000 		xenhcd_set_error(info, "Illegal index on conn-ring");
1001 		spin_unlock_irqrestore(&info->lock, flags);
1002 		return 0;
1003 	}
1004 	rmb(); /* ensure we see queued responses up to "rp" */
1005 
1006 	while (rc != rp) {
1007 		RING_COPY_RESPONSE(&info->conn_ring, rc, &res);
1008 		id = res.id;
1009 		portnum = res.portnum;
1010 		speed = res.speed;
1011 		info->conn_ring.rsp_cons = ++rc;
1012 
1013 		if (xenhcd_rhport_connect(info, portnum, speed)) {
1014 			xenhcd_set_error(info, "Illegal data on conn-ring");
1015 			spin_unlock_irqrestore(&info->lock, flags);
1016 			return 0;
1017 		}
1018 
1019 		if (info->ports[portnum - 1].c_connection)
1020 			port_changed = 1;
1021 
1022 		barrier();
1023 
1024 		req = RING_GET_REQUEST(&info->conn_ring,
1025 				       info->conn_ring.req_prod_pvt);
1026 		req->id = id;
1027 		info->conn_ring.req_prod_pvt++;
1028 	}
1029 
1030 	if (rc != info->conn_ring.req_prod_pvt)
1031 		RING_FINAL_CHECK_FOR_RESPONSES(&info->conn_ring, more_to_do);
1032 	else
1033 		info->conn_ring.sring->rsp_event = rc + 1;
1034 
1035 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->conn_ring, notify);
1036 	if (notify)
1037 		notify_remote_via_irq(info->irq);
1038 
1039 	spin_unlock_irqrestore(&info->lock, flags);
1040 
1041 	if (port_changed)
1042 		usb_hcd_poll_rh_status(xenhcd_info_to_hcd(info));
1043 
1044 	return more_to_do;
1045 }
1046 
1047 static irqreturn_t xenhcd_int(int irq, void *dev_id)
1048 {
1049 	struct xenhcd_info *info = (struct xenhcd_info *)dev_id;
1050 
1051 	if (unlikely(info->error))
1052 		return IRQ_HANDLED;
1053 
1054 	while (xenhcd_urb_request_done(info) | xenhcd_conn_notify(info))
1055 		/* Yield point for this unbounded loop. */
1056 		cond_resched();
1057 
1058 	return IRQ_HANDLED;
1059 }
1060 
1061 static void xenhcd_destroy_rings(struct xenhcd_info *info)
1062 {
1063 	if (info->irq)
1064 		unbind_from_irqhandler(info->irq, info);
1065 	info->irq = 0;
1066 
1067 	if (info->urb_ring_ref != GRANT_INVALID_REF) {
1068 		gnttab_end_foreign_access(info->urb_ring_ref, 0,
1069 					  (unsigned long)info->urb_ring.sring);
1070 		info->urb_ring_ref = GRANT_INVALID_REF;
1071 	}
1072 	info->urb_ring.sring = NULL;
1073 
1074 	if (info->conn_ring_ref != GRANT_INVALID_REF) {
1075 		gnttab_end_foreign_access(info->conn_ring_ref, 0,
1076 					  (unsigned long)info->conn_ring.sring);
1077 		info->conn_ring_ref = GRANT_INVALID_REF;
1078 	}
1079 	info->conn_ring.sring = NULL;
1080 }
1081 
1082 static int xenhcd_setup_rings(struct xenbus_device *dev,
1083 			      struct xenhcd_info *info)
1084 {
1085 	struct xenusb_urb_sring *urb_sring;
1086 	struct xenusb_conn_sring *conn_sring;
1087 	grant_ref_t gref;
1088 	int err;
1089 
1090 	info->urb_ring_ref = GRANT_INVALID_REF;
1091 	info->conn_ring_ref = GRANT_INVALID_REF;
1092 
1093 	urb_sring = (struct xenusb_urb_sring *)get_zeroed_page(
1094 							GFP_NOIO | __GFP_HIGH);
1095 	if (!urb_sring) {
1096 		xenbus_dev_fatal(dev, -ENOMEM, "allocating urb ring");
1097 		return -ENOMEM;
1098 	}
1099 	SHARED_RING_INIT(urb_sring);
1100 	FRONT_RING_INIT(&info->urb_ring, urb_sring, PAGE_SIZE);
1101 
1102 	err = xenbus_grant_ring(dev, urb_sring, 1, &gref);
1103 	if (err < 0) {
1104 		free_page((unsigned long)urb_sring);
1105 		info->urb_ring.sring = NULL;
1106 		goto fail;
1107 	}
1108 	info->urb_ring_ref = gref;
1109 
1110 	conn_sring = (struct xenusb_conn_sring *)get_zeroed_page(
1111 							GFP_NOIO | __GFP_HIGH);
1112 	if (!conn_sring) {
1113 		xenbus_dev_fatal(dev, -ENOMEM, "allocating conn ring");
1114 		err = -ENOMEM;
1115 		goto fail;
1116 	}
1117 	SHARED_RING_INIT(conn_sring);
1118 	FRONT_RING_INIT(&info->conn_ring, conn_sring, PAGE_SIZE);
1119 
1120 	err = xenbus_grant_ring(dev, conn_sring, 1, &gref);
1121 	if (err < 0) {
1122 		free_page((unsigned long)conn_sring);
1123 		info->conn_ring.sring = NULL;
1124 		goto fail;
1125 	}
1126 	info->conn_ring_ref = gref;
1127 
1128 	err = xenbus_alloc_evtchn(dev, &info->evtchn);
1129 	if (err) {
1130 		xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
1131 		goto fail;
1132 	}
1133 
1134 	err = bind_evtchn_to_irq(info->evtchn);
1135 	if (err <= 0) {
1136 		xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq");
1137 		goto fail;
1138 	}
1139 
1140 	info->irq = err;
1141 
1142 	err = request_threaded_irq(info->irq, NULL, xenhcd_int,
1143 				   IRQF_ONESHOT, "xenhcd", info);
1144 	if (err) {
1145 		xenbus_dev_fatal(dev, err, "request_threaded_irq");
1146 		goto free_irq;
1147 	}
1148 
1149 	return 0;
1150 
1151 free_irq:
1152 	unbind_from_irqhandler(info->irq, info);
1153 fail:
1154 	xenhcd_destroy_rings(info);
1155 	return err;
1156 }
1157 
1158 static int xenhcd_talk_to_backend(struct xenbus_device *dev,
1159 				  struct xenhcd_info *info)
1160 {
1161 	const char *message;
1162 	struct xenbus_transaction xbt;
1163 	int err;
1164 
1165 	err = xenhcd_setup_rings(dev, info);
1166 	if (err)
1167 		return err;
1168 
1169 again:
1170 	err = xenbus_transaction_start(&xbt);
1171 	if (err) {
1172 		xenbus_dev_fatal(dev, err, "starting transaction");
1173 		goto destroy_ring;
1174 	}
1175 
1176 	err = xenbus_printf(xbt, dev->nodename, "urb-ring-ref", "%u",
1177 			    info->urb_ring_ref);
1178 	if (err) {
1179 		message = "writing urb-ring-ref";
1180 		goto abort_transaction;
1181 	}
1182 
1183 	err = xenbus_printf(xbt, dev->nodename, "conn-ring-ref", "%u",
1184 			    info->conn_ring_ref);
1185 	if (err) {
1186 		message = "writing conn-ring-ref";
1187 		goto abort_transaction;
1188 	}
1189 
1190 	err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
1191 			    info->evtchn);
1192 	if (err) {
1193 		message = "writing event-channel";
1194 		goto abort_transaction;
1195 	}
1196 
1197 	err = xenbus_transaction_end(xbt, 0);
1198 	if (err) {
1199 		if (err == -EAGAIN)
1200 			goto again;
1201 		xenbus_dev_fatal(dev, err, "completing transaction");
1202 		goto destroy_ring;
1203 	}
1204 
1205 	return 0;
1206 
1207 abort_transaction:
1208 	xenbus_transaction_end(xbt, 1);
1209 	xenbus_dev_fatal(dev, err, "%s", message);
1210 
1211 destroy_ring:
1212 	xenhcd_destroy_rings(info);
1213 
1214 	return err;
1215 }
1216 
1217 static int xenhcd_connect(struct xenbus_device *dev)
1218 {
1219 	struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1220 	struct xenusb_conn_request *req;
1221 	int idx, err;
1222 	int notify;
1223 	char name[TASK_COMM_LEN];
1224 	struct usb_hcd *hcd;
1225 
1226 	hcd = xenhcd_info_to_hcd(info);
1227 	snprintf(name, TASK_COMM_LEN, "xenhcd.%d", hcd->self.busnum);
1228 
1229 	err = xenhcd_talk_to_backend(dev, info);
1230 	if (err)
1231 		return err;
1232 
1233 	/* prepare ring for hotplug notification */
1234 	for (idx = 0; idx < XENUSB_CONN_RING_SIZE; idx++) {
1235 		req = RING_GET_REQUEST(&info->conn_ring, idx);
1236 		req->id = idx;
1237 	}
1238 	info->conn_ring.req_prod_pvt = idx;
1239 
1240 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->conn_ring, notify);
1241 	if (notify)
1242 		notify_remote_via_irq(info->irq);
1243 
1244 	return 0;
1245 }
1246 
1247 static void xenhcd_disconnect(struct xenbus_device *dev)
1248 {
1249 	struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1250 	struct usb_hcd *hcd = xenhcd_info_to_hcd(info);
1251 
1252 	usb_remove_hcd(hcd);
1253 	xenbus_frontend_closed(dev);
1254 }
1255 
1256 static void xenhcd_watchdog(struct timer_list *timer)
1257 {
1258 	struct xenhcd_info *info = from_timer(info, timer, watchdog);
1259 	unsigned long flags;
1260 
1261 	spin_lock_irqsave(&info->lock, flags);
1262 	if (likely(HC_IS_RUNNING(xenhcd_info_to_hcd(info)->state))) {
1263 		xenhcd_timer_action_done(info, TIMER_RING_WATCHDOG);
1264 		xenhcd_giveback_unlinked_urbs(info);
1265 		xenhcd_kick_pending_urbs(info);
1266 	}
1267 	spin_unlock_irqrestore(&info->lock, flags);
1268 }
1269 
1270 /*
1271  * one-time HC init
1272  */
1273 static int xenhcd_setup(struct usb_hcd *hcd)
1274 {
1275 	struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1276 
1277 	spin_lock_init(&info->lock);
1278 	INIT_LIST_HEAD(&info->pending_submit_list);
1279 	INIT_LIST_HEAD(&info->pending_unlink_list);
1280 	INIT_LIST_HEAD(&info->in_progress_list);
1281 	INIT_LIST_HEAD(&info->giveback_waiting_list);
1282 	timer_setup(&info->watchdog, xenhcd_watchdog, 0);
1283 
1284 	hcd->has_tt = (hcd->driver->flags & HCD_MASK) != HCD_USB11;
1285 
1286 	return 0;
1287 }
1288 
1289 /*
1290  * start HC running
1291  */
1292 static int xenhcd_run(struct usb_hcd *hcd)
1293 {
1294 	hcd->uses_new_polling = 1;
1295 	clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1296 	hcd->state = HC_STATE_RUNNING;
1297 	return 0;
1298 }
1299 
1300 /*
1301  * stop running HC
1302  */
1303 static void xenhcd_stop(struct usb_hcd *hcd)
1304 {
1305 	struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1306 
1307 	del_timer_sync(&info->watchdog);
1308 	spin_lock_irq(&info->lock);
1309 	/* cancel all urbs */
1310 	hcd->state = HC_STATE_HALT;
1311 	xenhcd_cancel_all_enqueued_urbs(info);
1312 	xenhcd_giveback_unlinked_urbs(info);
1313 	spin_unlock_irq(&info->lock);
1314 }
1315 
1316 /*
1317  * called as .urb_enqueue()
1318  * non-error returns are promise to giveback the urb later
1319  */
1320 static int xenhcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1321 			      gfp_t mem_flags)
1322 {
1323 	struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1324 	struct urb_priv *urbp;
1325 	unsigned long flags;
1326 	int ret;
1327 
1328 	if (unlikely(info->error))
1329 		return -ESHUTDOWN;
1330 
1331 	urbp = kmem_cache_zalloc(xenhcd_urbp_cachep, mem_flags);
1332 	if (!urbp)
1333 		return -ENOMEM;
1334 
1335 	spin_lock_irqsave(&info->lock, flags);
1336 
1337 	urbp->urb = urb;
1338 	urb->hcpriv = urbp;
1339 	urbp->req_id = ~0;
1340 	urbp->unlink_req_id = ~0;
1341 	INIT_LIST_HEAD(&urbp->list);
1342 	urbp->status = 1;
1343 	urb->unlinked = false;
1344 
1345 	ret = xenhcd_submit_urb(info, urbp);
1346 
1347 	if (ret)
1348 		xenhcd_free_urb_priv(urbp);
1349 
1350 	spin_unlock_irqrestore(&info->lock, flags);
1351 
1352 	return ret;
1353 }
1354 
1355 /*
1356  * called as .urb_dequeue()
1357  */
1358 static int xenhcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1359 {
1360 	struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1361 	struct urb_priv *urbp;
1362 	unsigned long flags;
1363 	int ret = 0;
1364 
1365 	spin_lock_irqsave(&info->lock, flags);
1366 
1367 	urbp = urb->hcpriv;
1368 	if (urbp) {
1369 		urbp->status = status;
1370 		ret = xenhcd_unlink_urb(info, urbp);
1371 	}
1372 
1373 	spin_unlock_irqrestore(&info->lock, flags);
1374 
1375 	return ret;
1376 }
1377 
1378 /*
1379  * called from usb_get_current_frame_number(),
1380  * but, almost all drivers not use such function.
1381  */
1382 static int xenhcd_get_frame(struct usb_hcd *hcd)
1383 {
1384 	/* it means error, but probably no problem :-) */
1385 	return 0;
1386 }
1387 
1388 static struct hc_driver xenhcd_usb20_hc_driver = {
1389 	.description = "xen-hcd",
1390 	.product_desc = "Xen USB2.0 Virtual Host Controller",
1391 	.hcd_priv_size = sizeof(struct xenhcd_info),
1392 	.flags = HCD_USB2,
1393 
1394 	/* basic HC lifecycle operations */
1395 	.reset = xenhcd_setup,
1396 	.start = xenhcd_run,
1397 	.stop = xenhcd_stop,
1398 
1399 	/* managing urb I/O */
1400 	.urb_enqueue = xenhcd_urb_enqueue,
1401 	.urb_dequeue = xenhcd_urb_dequeue,
1402 	.get_frame_number = xenhcd_get_frame,
1403 
1404 	/* root hub operations */
1405 	.hub_status_data = xenhcd_hub_status_data,
1406 	.hub_control = xenhcd_hub_control,
1407 #ifdef CONFIG_PM
1408 	.bus_suspend = xenhcd_bus_suspend,
1409 	.bus_resume = xenhcd_bus_resume,
1410 #endif
1411 };
1412 
1413 static struct hc_driver xenhcd_usb11_hc_driver = {
1414 	.description = "xen-hcd",
1415 	.product_desc = "Xen USB1.1 Virtual Host Controller",
1416 	.hcd_priv_size = sizeof(struct xenhcd_info),
1417 	.flags = HCD_USB11,
1418 
1419 	/* basic HC lifecycle operations */
1420 	.reset = xenhcd_setup,
1421 	.start = xenhcd_run,
1422 	.stop = xenhcd_stop,
1423 
1424 	/* managing urb I/O */
1425 	.urb_enqueue = xenhcd_urb_enqueue,
1426 	.urb_dequeue = xenhcd_urb_dequeue,
1427 	.get_frame_number = xenhcd_get_frame,
1428 
1429 	/* root hub operations */
1430 	.hub_status_data = xenhcd_hub_status_data,
1431 	.hub_control = xenhcd_hub_control,
1432 #ifdef CONFIG_PM
1433 	.bus_suspend = xenhcd_bus_suspend,
1434 	.bus_resume = xenhcd_bus_resume,
1435 #endif
1436 };
1437 
1438 static struct usb_hcd *xenhcd_create_hcd(struct xenbus_device *dev)
1439 {
1440 	int i;
1441 	int err = 0;
1442 	int num_ports;
1443 	int usb_ver;
1444 	struct usb_hcd *hcd = NULL;
1445 	struct xenhcd_info *info;
1446 
1447 	err = xenbus_scanf(XBT_NIL, dev->otherend, "num-ports", "%d",
1448 			   &num_ports);
1449 	if (err != 1) {
1450 		xenbus_dev_fatal(dev, err, "reading num-ports");
1451 		return ERR_PTR(-EINVAL);
1452 	}
1453 	if (num_ports < 1 || num_ports > XENUSB_MAX_PORTNR) {
1454 		xenbus_dev_fatal(dev, err, "invalid num-ports");
1455 		return ERR_PTR(-EINVAL);
1456 	}
1457 
1458 	err = xenbus_scanf(XBT_NIL, dev->otherend, "usb-ver", "%d", &usb_ver);
1459 	if (err != 1) {
1460 		xenbus_dev_fatal(dev, err, "reading usb-ver");
1461 		return ERR_PTR(-EINVAL);
1462 	}
1463 	switch (usb_ver) {
1464 	case XENUSB_VER_USB11:
1465 		hcd = usb_create_hcd(&xenhcd_usb11_hc_driver, &dev->dev,
1466 				     dev_name(&dev->dev));
1467 		break;
1468 	case XENUSB_VER_USB20:
1469 		hcd = usb_create_hcd(&xenhcd_usb20_hc_driver, &dev->dev,
1470 				     dev_name(&dev->dev));
1471 		break;
1472 	default:
1473 		xenbus_dev_fatal(dev, err, "invalid usb-ver");
1474 		return ERR_PTR(-EINVAL);
1475 	}
1476 	if (!hcd) {
1477 		xenbus_dev_fatal(dev, err,
1478 				 "fail to allocate USB host controller");
1479 		return ERR_PTR(-ENOMEM);
1480 	}
1481 
1482 	info = xenhcd_hcd_to_info(hcd);
1483 	info->xbdev = dev;
1484 	info->rh_numports = num_ports;
1485 
1486 	for (i = 0; i < XENUSB_URB_RING_SIZE; i++) {
1487 		info->shadow[i].req.id = i + 1;
1488 		info->shadow[i].urb = NULL;
1489 	}
1490 	info->shadow[XENUSB_URB_RING_SIZE - 1].req.id = 0x0fff;
1491 
1492 	return hcd;
1493 }
1494 
1495 static void xenhcd_backend_changed(struct xenbus_device *dev,
1496 				   enum xenbus_state backend_state)
1497 {
1498 	switch (backend_state) {
1499 	case XenbusStateInitialising:
1500 	case XenbusStateReconfiguring:
1501 	case XenbusStateReconfigured:
1502 	case XenbusStateUnknown:
1503 		break;
1504 
1505 	case XenbusStateInitWait:
1506 	case XenbusStateInitialised:
1507 	case XenbusStateConnected:
1508 		if (dev->state != XenbusStateInitialising)
1509 			break;
1510 		if (!xenhcd_connect(dev))
1511 			xenbus_switch_state(dev, XenbusStateConnected);
1512 		break;
1513 
1514 	case XenbusStateClosed:
1515 		if (dev->state == XenbusStateClosed)
1516 			break;
1517 		fallthrough;	/* Missed the backend's Closing state. */
1518 	case XenbusStateClosing:
1519 		xenhcd_disconnect(dev);
1520 		break;
1521 
1522 	default:
1523 		xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1524 				 backend_state);
1525 		break;
1526 	}
1527 }
1528 
1529 static int xenhcd_remove(struct xenbus_device *dev)
1530 {
1531 	struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1532 	struct usb_hcd *hcd = xenhcd_info_to_hcd(info);
1533 
1534 	xenhcd_destroy_rings(info);
1535 	usb_put_hcd(hcd);
1536 
1537 	return 0;
1538 }
1539 
1540 static int xenhcd_probe(struct xenbus_device *dev,
1541 			const struct xenbus_device_id *id)
1542 {
1543 	int err;
1544 	struct usb_hcd *hcd;
1545 	struct xenhcd_info *info;
1546 
1547 	if (usb_disabled())
1548 		return -ENODEV;
1549 
1550 	hcd = xenhcd_create_hcd(dev);
1551 	if (IS_ERR(hcd)) {
1552 		err = PTR_ERR(hcd);
1553 		xenbus_dev_fatal(dev, err,
1554 				 "fail to create usb host controller");
1555 		return err;
1556 	}
1557 
1558 	info = xenhcd_hcd_to_info(hcd);
1559 	dev_set_drvdata(&dev->dev, info);
1560 
1561 	err = usb_add_hcd(hcd, 0, 0);
1562 	if (err) {
1563 		xenbus_dev_fatal(dev, err, "fail to add USB host controller");
1564 		usb_put_hcd(hcd);
1565 		dev_set_drvdata(&dev->dev, NULL);
1566 	}
1567 
1568 	return err;
1569 }
1570 
1571 static const struct xenbus_device_id xenhcd_ids[] = {
1572 	{ "vusb" },
1573 	{ "" },
1574 };
1575 
1576 static struct xenbus_driver xenhcd_driver = {
1577 	.ids			= xenhcd_ids,
1578 	.probe			= xenhcd_probe,
1579 	.otherend_changed	= xenhcd_backend_changed,
1580 	.remove			= xenhcd_remove,
1581 };
1582 
1583 static int __init xenhcd_init(void)
1584 {
1585 	if (!xen_domain())
1586 		return -ENODEV;
1587 
1588 	xenhcd_urbp_cachep = kmem_cache_create("xenhcd_urb_priv",
1589 					sizeof(struct urb_priv), 0, 0, NULL);
1590 	if (!xenhcd_urbp_cachep) {
1591 		pr_err("xenhcd failed to create kmem cache\n");
1592 		return -ENOMEM;
1593 	}
1594 
1595 	return xenbus_register_frontend(&xenhcd_driver);
1596 }
1597 module_init(xenhcd_init);
1598 
1599 static void __exit xenhcd_exit(void)
1600 {
1601 	kmem_cache_destroy(xenhcd_urbp_cachep);
1602 	xenbus_unregister_driver(&xenhcd_driver);
1603 }
1604 module_exit(xenhcd_exit);
1605 
1606 MODULE_ALIAS("xen:vusb");
1607 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");
1608 MODULE_DESCRIPTION("Xen USB Virtual Host Controller driver (xen-hcd)");
1609 MODULE_LICENSE("Dual BSD/GPL");
1610