1184610Salfred/* $FreeBSD$ */
2184610Salfred/*-
3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4184610Salfred *
5184610Salfred * Redistribution and use in source and binary forms, with or without
6184610Salfred * modification, are permitted provided that the following conditions
7184610Salfred * are met:
8184610Salfred * 1. Redistributions of source code must retain the above copyright
9184610Salfred *    notice, this list of conditions and the following disclaimer.
10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer in the
12184610Salfred *    documentation and/or other materials provided with the distribution.
13184610Salfred *
14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24184610Salfred * SUCH DAMAGE.
25184610Salfred */
26184610Salfred
27246122Shselasky#ifdef USB_GLOBAL_INCLUDE_FILE
28246122Shselasky#include USB_GLOBAL_INCLUDE_FILE
29246122Shselasky#else
30194677Sthompsa#include <sys/stdint.h>
31194677Sthompsa#include <sys/stddef.h>
32194677Sthompsa#include <sys/param.h>
33194677Sthompsa#include <sys/queue.h>
34194677Sthompsa#include <sys/types.h>
35194677Sthompsa#include <sys/systm.h>
36194677Sthompsa#include <sys/kernel.h>
37194677Sthompsa#include <sys/bus.h>
38194677Sthompsa#include <sys/module.h>
39194677Sthompsa#include <sys/lock.h>
40194677Sthompsa#include <sys/mutex.h>
41194677Sthompsa#include <sys/condvar.h>
42194677Sthompsa#include <sys/sysctl.h>
43194677Sthompsa#include <sys/sx.h>
44194677Sthompsa#include <sys/unistd.h>
45194677Sthompsa#include <sys/callout.h>
46194677Sthompsa#include <sys/malloc.h>
47194677Sthompsa#include <sys/priv.h>
48194677Sthompsa#include <sys/conf.h>
49194677Sthompsa#include <sys/fcntl.h>
50194677Sthompsa
51188942Sthompsa#include <dev/usb/usb.h>
52194677Sthompsa#include <dev/usb/usbdi.h>
53194677Sthompsa#include <dev/usb/usbdi_util.h>
54188942Sthompsa#include <dev/usb/usb_ioctl.h>
55213435Shselasky
56213435Shselasky#if USB_HAVE_UGEN
57213435Shselasky#include <sys/sbuf.h>
58213435Shselasky#endif
59213435Shselasky
60188746Sthompsa#include "usbdevs.h"
61184610Salfred
62194228Sthompsa#define	USB_DEBUG_VAR usb_debug
63184610Salfred
64188942Sthompsa#include <dev/usb/usb_core.h>
65188942Sthompsa#include <dev/usb/usb_debug.h>
66188942Sthompsa#include <dev/usb/usb_process.h>
67188942Sthompsa#include <dev/usb/usb_device.h>
68188942Sthompsa#include <dev/usb/usb_busdma.h>
69188942Sthompsa#include <dev/usb/usb_transfer.h>
70188942Sthompsa#include <dev/usb/usb_request.h>
71188942Sthompsa#include <dev/usb/usb_dynamic.h>
72188942Sthompsa#include <dev/usb/usb_hub.h>
73188942Sthompsa#include <dev/usb/usb_util.h>
74190180Sthompsa#include <dev/usb/usb_msctest.h>
75190180Sthompsa#if USB_HAVE_UGEN
76188942Sthompsa#include <dev/usb/usb_dev.h>
77188942Sthompsa#include <dev/usb/usb_generic.h>
78190180Sthompsa#endif
79184610Salfred
80188942Sthompsa#include <dev/usb/quirk/usb_quirk.h>
81184610Salfred
82188942Sthompsa#include <dev/usb/usb_controller.h>
83188942Sthompsa#include <dev/usb/usb_bus.h>
84246122Shselasky#endif			/* USB_GLOBAL_INCLUDE_FILE */
85184610Salfred
86190754Sthompsa/* function prototypes  */
87184610Salfred
88194228Sthompsastatic void	usb_init_endpoint(struct usb_device *, uint8_t,
89213435Shselasky		    struct usb_endpoint_descriptor *,
90213435Shselasky		    struct usb_endpoint_ss_comp_descriptor *,
91213435Shselasky		    struct usb_endpoint *);
92194228Sthompsastatic void	usb_unconfigure(struct usb_device *, uint8_t);
93194228Sthompsastatic void	usb_detach_device_sub(struct usb_device *, device_t *,
94214429Shselasky		    char **, uint8_t);
95194228Sthompsastatic uint8_t	usb_probe_and_attach_sub(struct usb_device *,
96192984Sthompsa		    struct usb_attach_arg *);
97194228Sthompsastatic void	usb_init_attach_arg(struct usb_device *,
98192984Sthompsa		    struct usb_attach_arg *);
99194228Sthompsastatic void	usb_suspend_resume_sub(struct usb_device *, device_t,
100185948Sthompsa		    uint8_t);
101194228Sthompsastatic void	usbd_clear_stall_proc(struct usb_proc_msg *_pm);
102213435Shselaskystatic usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t);
103194228Sthompsastatic void	usbd_set_device_strings(struct usb_device *);
104214221Shselasky#if USB_HAVE_DEVCTL
105214221Shselaskystatic void	usb_notify_addq(const char *type, struct usb_device *);
106214221Shselasky#endif
107190191Sthompsa#if USB_HAVE_UGEN
108194228Sthompsastatic void	usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t);
109194228Sthompsastatic void	usb_cdev_create(struct usb_device *);
110194228Sthompsastatic void	usb_cdev_free(struct usb_device *);
111189599Sthompsa#endif
112184610Salfred
113184610Salfred/* This variable is global to allow easy access to it: */
114184610Salfred
115250202Shselasky#ifdef	USB_TEMPLATE
116250202Shselaskyint	usb_template = USB_TEMPLATE;
117250202Shselasky#else
118250202Shselaskyint	usb_template;
119250202Shselasky#endif
120184610Salfred
121197559SthompsaTUNABLE_INT("hw.usb.usb_template", &usb_template);
122242126ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RW | CTLFLAG_TUN,
123194228Sthompsa    &usb_template, 0, "Selected USB device side template");
124184610Salfred
125197559Sthompsa/* English is default language */
126197559Sthompsa
127197559Sthompsastatic int usb_lang_id = 0x0009;
128197559Sthompsastatic int usb_lang_mask = 0x00FF;
129197559Sthompsa
130197559SthompsaTUNABLE_INT("hw.usb.usb_lang_id", &usb_lang_id);
131242126ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RW | CTLFLAG_TUN,
132197559Sthompsa    &usb_lang_id, 0, "Preferred USB language ID");
133197559Sthompsa
134197559SthompsaTUNABLE_INT("hw.usb.usb_lang_mask", &usb_lang_mask);
135242126ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RW | CTLFLAG_TUN,
136197559Sthompsa    &usb_lang_mask, 0, "Preferred USB language mask");
137197559Sthompsa
138191498Sthompsastatic const char* statestr[USB_STATE_MAX] = {
139191498Sthompsa	[USB_STATE_DETACHED]	= "DETACHED",
140191498Sthompsa	[USB_STATE_ATTACHED]	= "ATTACHED",
141191498Sthompsa	[USB_STATE_POWERED]	= "POWERED",
142191498Sthompsa	[USB_STATE_ADDRESSED]	= "ADDRESSED",
143191498Sthompsa	[USB_STATE_CONFIGURED]	= "CONFIGURED",
144191498Sthompsa};
145184610Salfred
146191498Sthompsaconst char *
147194228Sthompsausb_statestr(enum usb_dev_state state)
148191498Sthompsa{
149191824Sthompsa	return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
150191498Sthompsa}
151191498Sthompsa
152212136Sthompsaconst char *
153212136Sthompsausb_get_manufacturer(struct usb_device *udev)
154212136Sthompsa{
155212136Sthompsa	return (udev->manufacturer ? udev->manufacturer : "Unknown");
156212136Sthompsa}
157212136Sthompsa
158212136Sthompsaconst char *
159212136Sthompsausb_get_product(struct usb_device *udev)
160212136Sthompsa{
161212136Sthompsa	return (udev->product ? udev->product : "");
162212136Sthompsa}
163212136Sthompsa
164212136Sthompsaconst char *
165212136Sthompsausb_get_serial(struct usb_device *udev)
166212136Sthompsa{
167212136Sthompsa	return (udev->serial ? udev->serial : "");
168212136Sthompsa}
169212136Sthompsa
170184610Salfred/*------------------------------------------------------------------------*
171194228Sthompsa *	usbd_get_ep_by_addr
172184610Salfred *
173193644Sthompsa * This function searches for an USB ep by endpoint address and
174184610Salfred * direction.
175184610Salfred *
176184610Salfred * Returns:
177184610Salfred * NULL: Failure
178184610Salfred * Else: Success
179184610Salfred *------------------------------------------------------------------------*/
180193644Sthompsastruct usb_endpoint *
181194228Sthompsausbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
182184610Salfred{
183193644Sthompsa	struct usb_endpoint *ep = udev->endpoints;
184193644Sthompsa	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
185184610Salfred	enum {
186184610Salfred		EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
187184610Salfred	};
188184610Salfred
189184610Salfred	/*
190184610Salfred	 * According to the USB specification not all bits are used
191184610Salfred	 * for the endpoint address. Keep defined bits only:
192184610Salfred	 */
193184610Salfred	ea_val &= EA_MASK;
194184610Salfred
195184610Salfred	/*
196193644Sthompsa	 * Iterate accross all the USB endpoints searching for a match
197184610Salfred	 * based on the endpoint address:
198184610Salfred	 */
199193644Sthompsa	for (; ep != ep_end; ep++) {
200184610Salfred
201193644Sthompsa		if (ep->edesc == NULL) {
202184610Salfred			continue;
203184610Salfred		}
204184610Salfred		/* do the mask and check the value */
205193644Sthompsa		if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
206184610Salfred			goto found;
207184610Salfred		}
208184610Salfred	}
209184610Salfred
210184610Salfred	/*
211193644Sthompsa	 * The default endpoint is always present and is checked separately:
212184610Salfred	 */
213239177Shselasky	if ((udev->ctrl_ep.edesc != NULL) &&
214207080Sthompsa	    ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
215207080Sthompsa		ep = &udev->ctrl_ep;
216184610Salfred		goto found;
217184610Salfred	}
218184610Salfred	return (NULL);
219184610Salfred
220184610Salfredfound:
221193644Sthompsa	return (ep);
222184610Salfred}
223184610Salfred
224184610Salfred/*------------------------------------------------------------------------*
225194228Sthompsa *	usbd_get_endpoint
226184610Salfred *
227193644Sthompsa * This function searches for an USB endpoint based on the information
228192984Sthompsa * given by the passed "struct usb_config" pointer.
229184610Salfred *
230184610Salfred * Return values:
231184610Salfred * NULL: No match.
232193644Sthompsa * Else: Pointer to "struct usb_endpoint".
233184610Salfred *------------------------------------------------------------------------*/
234193644Sthompsastruct usb_endpoint *
235194228Sthompsausbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
236192984Sthompsa    const struct usb_config *setup)
237184610Salfred{
238193644Sthompsa	struct usb_endpoint *ep = udev->endpoints;
239193644Sthompsa	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
240184610Salfred	uint8_t index = setup->ep_index;
241184610Salfred	uint8_t ea_mask;
242184610Salfred	uint8_t ea_val;
243184610Salfred	uint8_t type_mask;
244184610Salfred	uint8_t type_val;
245184610Salfred
246184610Salfred	DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
247184610Salfred	    "type=0x%x dir=0x%x index=%d\n",
248184610Salfred	    udev, iface_index, setup->endpoint,
249184610Salfred	    setup->type, setup->direction, setup->ep_index);
250184610Salfred
251190734Sthompsa	/* check USB mode */
252190734Sthompsa
253192499Sthompsa	if (setup->usb_mode != USB_MODE_DUAL &&
254192499Sthompsa	    udev->flags.usb_mode != setup->usb_mode) {
255193644Sthompsa		/* wrong mode - no endpoint */
256190734Sthompsa		return (NULL);
257190734Sthompsa	}
258190734Sthompsa
259184610Salfred	/* setup expected endpoint direction mask and value */
260184610Salfred
261190732Sthompsa	if (setup->direction == UE_DIR_RX) {
262190732Sthompsa		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
263192499Sthompsa		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
264190732Sthompsa		    UE_DIR_OUT : UE_DIR_IN;
265190732Sthompsa	} else if (setup->direction == UE_DIR_TX) {
266190732Sthompsa		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
267192499Sthompsa		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
268190732Sthompsa		    UE_DIR_IN : UE_DIR_OUT;
269190732Sthompsa	} else if (setup->direction == UE_DIR_ANY) {
270184610Salfred		/* match any endpoint direction */
271184610Salfred		ea_mask = 0;
272184610Salfred		ea_val = 0;
273184610Salfred	} else {
274184610Salfred		/* match the given endpoint direction */
275184610Salfred		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
276184610Salfred		ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
277184610Salfred	}
278184610Salfred
279184610Salfred	/* setup expected endpoint address */
280184610Salfred
281184610Salfred	if (setup->endpoint == UE_ADDR_ANY) {
282184610Salfred		/* match any endpoint address */
283184610Salfred	} else {
284184610Salfred		/* match the given endpoint address */
285184610Salfred		ea_mask |= UE_ADDR;
286184610Salfred		ea_val |= (setup->endpoint & UE_ADDR);
287184610Salfred	}
288184610Salfred
289184610Salfred	/* setup expected endpoint type */
290184610Salfred
291184610Salfred	if (setup->type == UE_BULK_INTR) {
292184610Salfred		/* this will match BULK and INTERRUPT endpoints */
293184610Salfred		type_mask = 2;
294184610Salfred		type_val = 2;
295184610Salfred	} else if (setup->type == UE_TYPE_ANY) {
296184610Salfred		/* match any endpoint type */
297184610Salfred		type_mask = 0;
298184610Salfred		type_val = 0;
299184610Salfred	} else {
300184610Salfred		/* match the given endpoint type */
301184610Salfred		type_mask = UE_XFERTYPE;
302184610Salfred		type_val = (setup->type & UE_XFERTYPE);
303184610Salfred	}
304184610Salfred
305184610Salfred	/*
306193644Sthompsa	 * Iterate accross all the USB endpoints searching for a match
307184610Salfred	 * based on the endpoint address. Note that we are searching
308193644Sthompsa	 * the endpoints from the beginning of the "udev->endpoints" array.
309184610Salfred	 */
310193644Sthompsa	for (; ep != ep_end; ep++) {
311184610Salfred
312193644Sthompsa		if ((ep->edesc == NULL) ||
313193644Sthompsa		    (ep->iface_index != iface_index)) {
314184610Salfred			continue;
315184610Salfred		}
316184610Salfred		/* do the masks and check the values */
317184610Salfred
318193644Sthompsa		if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
319193644Sthompsa		    ((ep->edesc->bmAttributes & type_mask) == type_val)) {
320184610Salfred			if (!index--) {
321184610Salfred				goto found;
322184610Salfred			}
323184610Salfred		}
324184610Salfred	}
325184610Salfred
326184610Salfred	/*
327193644Sthompsa	 * Match against default endpoint last, so that "any endpoint", "any
328193644Sthompsa	 * address" and "any direction" returns the first endpoint of the
329184610Salfred	 * interface. "iface_index" and "direction" is ignored:
330184610Salfred	 */
331239177Shselasky	if ((udev->ctrl_ep.edesc != NULL) &&
332207080Sthompsa	    ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
333207080Sthompsa	    ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) &&
334184610Salfred	    (!index)) {
335207080Sthompsa		ep = &udev->ctrl_ep;
336184610Salfred		goto found;
337184610Salfred	}
338184610Salfred	return (NULL);
339184610Salfred
340184610Salfredfound:
341193644Sthompsa	return (ep);
342184610Salfred}
343184610Salfred
344184610Salfred/*------------------------------------------------------------------------*
345194677Sthompsa *	usbd_interface_count
346184610Salfred *
347184610Salfred * This function stores the number of USB interfaces excluding
348184610Salfred * alternate settings, which the USB config descriptor reports into
349184610Salfred * the unsigned 8-bit integer pointed to by "count".
350184610Salfred *
351184610Salfred * Returns:
352184610Salfred *    0: Success
353184610Salfred * Else: Failure
354184610Salfred *------------------------------------------------------------------------*/
355193045Sthompsausb_error_t
356194677Sthompsausbd_interface_count(struct usb_device *udev, uint8_t *count)
357184610Salfred{
358184610Salfred	if (udev->cdesc == NULL) {
359184610Salfred		*count = 0;
360184610Salfred		return (USB_ERR_NOT_CONFIGURED);
361184610Salfred	}
362190730Sthompsa	*count = udev->ifaces_max;
363184610Salfred	return (USB_ERR_NORMAL_COMPLETION);
364184610Salfred}
365184610Salfred
366184610Salfred/*------------------------------------------------------------------------*
367194228Sthompsa *	usb_init_endpoint
368184610Salfred *
369193644Sthompsa * This function will initialise the USB endpoint structure pointed to by
370193644Sthompsa * the "endpoint" argument. The structure pointed to by "endpoint" must be
371190730Sthompsa * zeroed before calling this function.
372184610Salfred *------------------------------------------------------------------------*/
373184610Salfredstatic void
374194228Sthompsausb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
375213435Shselasky    struct usb_endpoint_descriptor *edesc,
376213435Shselasky    struct usb_endpoint_ss_comp_descriptor *ecomp,
377213435Shselasky    struct usb_endpoint *ep)
378184610Salfred{
379192984Sthompsa	struct usb_bus_methods *methods;
380239214Shselasky	usb_stream_t x;
381189110Sthompsa
382190730Sthompsa	methods = udev->bus->methods;
383184610Salfred
384193644Sthompsa	(methods->endpoint_init) (udev, edesc, ep);
385184610Salfred
386193644Sthompsa	/* initialise USB endpoint structure */
387193644Sthompsa	ep->edesc = edesc;
388213435Shselasky	ep->ecomp = ecomp;
389193644Sthompsa	ep->iface_index = iface_index;
390184610Salfred
391239214Shselasky	/* setup USB stream queues */
392239214Shselasky	for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
393239214Shselasky		TAILQ_INIT(&ep->endpoint_q[x].head);
394239214Shselasky		ep->endpoint_q[x].command = &usbd_pipe_start;
395239214Shselasky	}
396239214Shselasky
397190735Sthompsa	/* the pipe is not supported by the hardware */
398193644Sthompsa 	if (ep->methods == NULL)
399190735Sthompsa		return;
400190735Sthompsa
401239214Shselasky	/* check for SUPER-speed streams mode endpoint */
402239214Shselasky	if (udev->speed == USB_SPEED_SUPER && ecomp != NULL &&
403239214Shselasky	    (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK &&
404239214Shselasky	    (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) {
405239214Shselasky		usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS);
406239214Shselasky	} else {
407239214Shselasky		usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT);
408239214Shselasky	}
409239214Shselasky
410184610Salfred	/* clear stall, if any */
411190730Sthompsa	if (methods->clear_stall != NULL) {
412184824Sthompsa		USB_BUS_LOCK(udev->bus);
413193644Sthompsa		(methods->clear_stall) (udev, ep);
414184824Sthompsa		USB_BUS_UNLOCK(udev->bus);
415184610Salfred	}
416184610Salfred}
417184610Salfred
418190730Sthompsa/*-----------------------------------------------------------------------*
419194228Sthompsa *	usb_endpoint_foreach
420188985Sthompsa *
421188985Sthompsa * This function will iterate all the USB endpoints except the control
422188985Sthompsa * endpoint. This function is NULL safe.
423188985Sthompsa *
424188985Sthompsa * Return values:
425193644Sthompsa * NULL: End of USB endpoints
426193644Sthompsa * Else: Pointer to next USB endpoint
427188985Sthompsa *------------------------------------------------------------------------*/
428193644Sthompsastruct usb_endpoint *
429194228Sthompsausb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
430188985Sthompsa{
431197558Sthompsa	struct usb_endpoint *ep_end;
432188985Sthompsa
433188985Sthompsa	/* be NULL safe */
434188985Sthompsa	if (udev == NULL)
435188985Sthompsa		return (NULL);
436188985Sthompsa
437197558Sthompsa	ep_end = udev->endpoints + udev->endpoints_max;
438197558Sthompsa
439193644Sthompsa	/* get next endpoint */
440193644Sthompsa	if (ep == NULL)
441193644Sthompsa		ep = udev->endpoints;
442188985Sthompsa	else
443193644Sthompsa		ep++;
444188985Sthompsa
445193644Sthompsa	/* find next allocated ep */
446193644Sthompsa	while (ep != ep_end) {
447193644Sthompsa		if (ep->edesc != NULL)
448193644Sthompsa			return (ep);
449193644Sthompsa		ep++;
450188985Sthompsa	}
451188985Sthompsa	return (NULL);
452188985Sthompsa}
453188985Sthompsa
454188985Sthompsa/*------------------------------------------------------------------------*
455194228Sthompsa *	usb_unconfigure
456184610Salfred *
457193644Sthompsa * This function will free all USB interfaces and USB endpoints belonging
458190730Sthompsa * to an USB device.
459190730Sthompsa *
460190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX".
461184610Salfred *------------------------------------------------------------------------*/
462190730Sthompsastatic void
463194228Sthompsausb_unconfigure(struct usb_device *udev, uint8_t flag)
464184610Salfred{
465190730Sthompsa	uint8_t do_unlock;
466184610Salfred
467246616Shselasky	/* Prevent re-enumeration */
468246616Shselasky	do_unlock = usbd_enum_lock(udev);
469184610Salfred
470190730Sthompsa	/* detach all interface drivers */
471194228Sthompsa	usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
472184610Salfred
473190730Sthompsa#if USB_HAVE_UGEN
474190730Sthompsa	/* free all FIFOs except control endpoint FIFOs */
475194228Sthompsa	usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
476184610Salfred
477184610Salfred	/*
478190730Sthompsa	 * Free all cdev's, if any.
479184610Salfred	 */
480194228Sthompsa	usb_cdev_free(udev);
481190730Sthompsa#endif
482184610Salfred
483190180Sthompsa#if USB_HAVE_COMPAT_LINUX
484184610Salfred	/* free Linux compat device, if any */
485192984Sthompsa	if (udev->linux_endpoint_start) {
486192984Sthompsa		usb_linux_free_device(udev);
487192984Sthompsa		udev->linux_endpoint_start = NULL;
488184610Salfred	}
489190180Sthompsa#endif
490184610Salfred
491194228Sthompsa	usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
492184610Salfred
493193644Sthompsa	/* free "cdesc" after "ifaces" and "endpoints", if any */
494190727Sthompsa	if (udev->cdesc != NULL) {
495192499Sthompsa		if (udev->flags.usb_mode != USB_MODE_DEVICE)
496250207Shselasky			usbd_free_config_desc(udev, udev->cdesc);
497184610Salfred		udev->cdesc = NULL;
498184610Salfred	}
499184610Salfred	/* set unconfigured state */
500184610Salfred	udev->curr_config_no = USB_UNCONFIG_NO;
501184610Salfred	udev->curr_config_index = USB_UNCONFIG_INDEX;
502190730Sthompsa
503196498Salfred	if (do_unlock)
504196498Salfred		usbd_enum_unlock(udev);
505184610Salfred}
506184610Salfred
507184610Salfred/*------------------------------------------------------------------------*
508194228Sthompsa *	usbd_set_config_index
509184610Salfred *
510184610Salfred * This function selects configuration by index, independent of the
511184610Salfred * actual configuration number. This function should not be used by
512184610Salfred * USB drivers.
513184610Salfred *
514184610Salfred * Returns:
515184610Salfred *    0: Success
516184610Salfred * Else: Failure
517184610Salfred *------------------------------------------------------------------------*/
518193045Sthompsausb_error_t
519194228Sthompsausbd_set_config_index(struct usb_device *udev, uint8_t index)
520184610Salfred{
521192984Sthompsa	struct usb_status ds;
522192984Sthompsa	struct usb_config_descriptor *cdp;
523184610Salfred	uint16_t power;
524184610Salfred	uint16_t max_power;
525184610Salfred	uint8_t selfpowered;
526184610Salfred	uint8_t do_unlock;
527193045Sthompsa	usb_error_t err;
528184610Salfred
529184610Salfred	DPRINTFN(6, "udev=%p index=%d\n", udev, index);
530184610Salfred
531246616Shselasky	/* Prevent re-enumeration */
532246616Shselasky	do_unlock = usbd_enum_lock(udev);
533184610Salfred
534197553Sthompsa	usb_unconfigure(udev, 0);
535184610Salfred
536184610Salfred	if (index == USB_UNCONFIG_INDEX) {
537184610Salfred		/*
538184610Salfred		 * Leave unallocated when unconfiguring the
539194228Sthompsa		 * device. "usb_unconfigure()" will also reset
540184610Salfred		 * the current config number and index.
541184610Salfred		 */
542194228Sthompsa		err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
543191494Sthompsa		if (udev->state == USB_STATE_CONFIGURED)
544194228Sthompsa			usb_set_device_state(udev, USB_STATE_ADDRESSED);
545184610Salfred		goto done;
546184610Salfred	}
547184610Salfred	/* get the full config descriptor */
548192499Sthompsa	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
549190727Sthompsa		/* save some memory */
550194228Sthompsa		err = usbd_req_get_descriptor_ptr(udev, &cdp,
551191402Sthompsa		    (UDESC_CONFIG << 8) | index);
552190727Sthompsa	} else {
553190727Sthompsa		/* normal request */
554194228Sthompsa		err = usbd_req_get_config_desc_full(udev,
555250207Shselasky		    NULL, &cdp, index);
556190727Sthompsa	}
557184610Salfred	if (err) {
558184610Salfred		goto done;
559184610Salfred	}
560184610Salfred	/* set the new config descriptor */
561184610Salfred
562184610Salfred	udev->cdesc = cdp;
563184610Salfred
564184610Salfred	/* Figure out if the device is self or bus powered. */
565184610Salfred	selfpowered = 0;
566184610Salfred	if ((!udev->flags.uq_bus_powered) &&
567184610Salfred	    (cdp->bmAttributes & UC_SELF_POWERED) &&
568192499Sthompsa	    (udev->flags.usb_mode == USB_MODE_HOST)) {
569184610Salfred		/* May be self powered. */
570184610Salfred		if (cdp->bmAttributes & UC_BUS_POWERED) {
571184610Salfred			/* Must ask device. */
572194228Sthompsa			err = usbd_req_get_device_status(udev, NULL, &ds);
573190743Sthompsa			if (err) {
574190743Sthompsa				DPRINTFN(0, "could not read "
575190743Sthompsa				    "device status: %s\n",
576194228Sthompsa				    usbd_errstr(err));
577190743Sthompsa			} else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
578190743Sthompsa				selfpowered = 1;
579184610Salfred			}
580190743Sthompsa			DPRINTF("status=0x%04x \n",
581190743Sthompsa				UGETW(ds.wStatus));
582184610Salfred		} else
583184610Salfred			selfpowered = 1;
584184610Salfred	}
585184610Salfred	DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
586184610Salfred	    "selfpowered=%d, power=%d\n",
587184610Salfred	    udev, cdp,
588190328Sthompsa	    udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
589184610Salfred	    selfpowered, cdp->bMaxPower * 2);
590184610Salfred
591184610Salfred	/* Check if we have enough power. */
592184610Salfred	power = cdp->bMaxPower * 2;
593184610Salfred
594184610Salfred	if (udev->parent_hub) {
595184610Salfred		max_power = udev->parent_hub->hub->portpower;
596184610Salfred	} else {
597184610Salfred		max_power = USB_MAX_POWER;
598184610Salfred	}
599184610Salfred
600184610Salfred	if (power > max_power) {
601184610Salfred		DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
602184610Salfred		err = USB_ERR_NO_POWER;
603184610Salfred		goto done;
604184610Salfred	}
605184610Salfred	/* Only update "self_powered" in USB Host Mode */
606192499Sthompsa	if (udev->flags.usb_mode == USB_MODE_HOST) {
607184610Salfred		udev->flags.self_powered = selfpowered;
608184610Salfred	}
609184610Salfred	udev->power = power;
610184610Salfred	udev->curr_config_no = cdp->bConfigurationValue;
611184610Salfred	udev->curr_config_index = index;
612194228Sthompsa	usb_set_device_state(udev, USB_STATE_CONFIGURED);
613184610Salfred
614184610Salfred	/* Set the actual configuration value. */
615194228Sthompsa	err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
616184610Salfred	if (err) {
617184610Salfred		goto done;
618184610Salfred	}
619190730Sthompsa
620194228Sthompsa	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
621190730Sthompsa	if (err) {
622190730Sthompsa		goto done;
623184610Salfred	}
624190730Sthompsa
625194228Sthompsa	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
626190730Sthompsa	if (err) {
627190730Sthompsa		goto done;
628190730Sthompsa	}
629190730Sthompsa
630189599Sthompsa#if USB_HAVE_UGEN
631189110Sthompsa	/* create device nodes for each endpoint */
632194228Sthompsa	usb_cdev_create(udev);
633189599Sthompsa#endif
634184610Salfred
635184610Salfreddone:
636194228Sthompsa	DPRINTF("error=%s\n", usbd_errstr(err));
637184610Salfred	if (err) {
638197553Sthompsa		usb_unconfigure(udev, 0);
639184610Salfred	}
640196498Salfred	if (do_unlock)
641196498Salfred		usbd_enum_unlock(udev);
642184610Salfred	return (err);
643184610Salfred}
644184610Salfred
645184610Salfred/*------------------------------------------------------------------------*
646194228Sthompsa *	usb_config_parse
647190730Sthompsa *
648193644Sthompsa * This function will allocate and free USB interfaces and USB endpoints,
649193644Sthompsa * parse the USB configuration structure and initialise the USB endpoints
650190730Sthompsa * and interfaces. If "iface_index" is not equal to
651190730Sthompsa * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
652190730Sthompsa * alternate_setting to be selected for the given interface. Else the
653190730Sthompsa * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
654190730Sthompsa * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
655190730Sthompsa * is typically called when setting the configuration or when setting
656190730Sthompsa * an alternate interface.
657190730Sthompsa *
658190730Sthompsa * Returns:
659190730Sthompsa *    0: Success
660190730Sthompsa * Else: Failure
661190730Sthompsa *------------------------------------------------------------------------*/
662213435Shselaskystatic usb_error_t
663194228Sthompsausb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
664190730Sthompsa{
665192984Sthompsa	struct usb_idesc_parse_state ips;
666192984Sthompsa	struct usb_interface_descriptor *id;
667192984Sthompsa	struct usb_endpoint_descriptor *ed;
668192984Sthompsa	struct usb_interface *iface;
669193644Sthompsa	struct usb_endpoint *ep;
670193045Sthompsa	usb_error_t err;
671190730Sthompsa	uint8_t ep_curr;
672190730Sthompsa	uint8_t ep_max;
673190730Sthompsa	uint8_t temp;
674190730Sthompsa	uint8_t do_init;
675190730Sthompsa	uint8_t alt_index;
676190730Sthompsa
677190730Sthompsa	if (iface_index != USB_IFACE_INDEX_ANY) {
678190730Sthompsa		/* parameter overload */
679190730Sthompsa		alt_index = cmd;
680190730Sthompsa		cmd = USB_CFG_INIT;
681190730Sthompsa	} else {
682190730Sthompsa		/* not used */
683190730Sthompsa		alt_index = 0;
684190730Sthompsa	}
685190730Sthompsa
686190730Sthompsa	err = 0;
687190730Sthompsa
688190730Sthompsa	DPRINTFN(5, "iface_index=%d cmd=%d\n",
689190730Sthompsa	    iface_index, cmd);
690190730Sthompsa
691190730Sthompsa	if (cmd == USB_CFG_FREE)
692190730Sthompsa		goto cleanup;
693190730Sthompsa
694190730Sthompsa	if (cmd == USB_CFG_INIT) {
695207079Sthompsa		sx_assert(&udev->enum_sx, SA_LOCKED);
696190730Sthompsa
697193644Sthompsa		/* check for in-use endpoints */
698190730Sthompsa
699193644Sthompsa		ep = udev->endpoints;
700193644Sthompsa		ep_max = udev->endpoints_max;
701190730Sthompsa		while (ep_max--) {
702193644Sthompsa			/* look for matching endpoints */
703190730Sthompsa			if ((iface_index == USB_IFACE_INDEX_ANY) ||
704193644Sthompsa			    (iface_index == ep->iface_index)) {
705199672Sthompsa				if (ep->refcount_alloc != 0) {
706190730Sthompsa					/*
707190730Sthompsa					 * This typically indicates a
708190730Sthompsa					 * more serious error.
709190730Sthompsa					 */
710190730Sthompsa					err = USB_ERR_IN_USE;
711190730Sthompsa				} else {
712193644Sthompsa					/* reset endpoint */
713193644Sthompsa					memset(ep, 0, sizeof(*ep));
714193644Sthompsa					/* make sure we don't zero the endpoint again */
715193644Sthompsa					ep->iface_index = USB_IFACE_INDEX_ANY;
716190730Sthompsa				}
717190730Sthompsa			}
718193644Sthompsa			ep++;
719190730Sthompsa		}
720190730Sthompsa
721190730Sthompsa		if (err)
722190730Sthompsa			return (err);
723190730Sthompsa	}
724190730Sthompsa
725190730Sthompsa	memset(&ips, 0, sizeof(ips));
726190730Sthompsa
727190730Sthompsa	ep_curr = 0;
728190730Sthompsa	ep_max = 0;
729190730Sthompsa
730194228Sthompsa	while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
731190730Sthompsa
732190730Sthompsa		iface = udev->ifaces + ips.iface_index;
733190730Sthompsa
734190730Sthompsa		/* check for specific interface match */
735190730Sthompsa
736190730Sthompsa		if (cmd == USB_CFG_INIT) {
737190730Sthompsa			if ((iface_index != USB_IFACE_INDEX_ANY) &&
738190730Sthompsa			    (iface_index != ips.iface_index)) {
739190730Sthompsa				/* wrong interface */
740190730Sthompsa				do_init = 0;
741190730Sthompsa			} else if (alt_index != ips.iface_index_alt) {
742190730Sthompsa				/* wrong alternate setting */
743190730Sthompsa				do_init = 0;
744190730Sthompsa			} else {
745190730Sthompsa				/* initialise interface */
746190730Sthompsa				do_init = 1;
747190730Sthompsa			}
748190730Sthompsa		} else
749190730Sthompsa			do_init = 0;
750190730Sthompsa
751190730Sthompsa		/* check for new interface */
752190730Sthompsa		if (ips.iface_index_alt == 0) {
753190730Sthompsa			/* update current number of endpoints */
754190730Sthompsa			ep_curr = ep_max;
755190730Sthompsa		}
756190730Sthompsa		/* check for init */
757190730Sthompsa		if (do_init) {
758190730Sthompsa			/* setup the USB interface structure */
759190730Sthompsa			iface->idesc = id;
760190730Sthompsa			/* set alternate index */
761190730Sthompsa			iface->alt_index = alt_index;
762236407Shselasky			/* set default interface parent */
763236407Shselasky			if (iface_index == USB_IFACE_INDEX_ANY) {
764236407Shselasky				iface->parent_iface_index =
765236407Shselasky				    USB_IFACE_INDEX_ANY;
766236407Shselasky			}
767190730Sthompsa		}
768190730Sthompsa
769190730Sthompsa		DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
770190730Sthompsa
771192984Sthompsa		ed = (struct usb_endpoint_descriptor *)id;
772190730Sthompsa
773190730Sthompsa		temp = ep_curr;
774190730Sthompsa
775190730Sthompsa		/* iterate all the endpoint descriptors */
776194228Sthompsa		while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
777190730Sthompsa
778250204Shselasky			/* check if endpoint limit has been reached */
779250204Shselasky			if (temp >= USB_MAX_EP_UNITS) {
780250204Shselasky				DPRINTF("Endpoint limit reached\n");
781250204Shselasky				break;
782250204Shselasky			}
783190730Sthompsa
784193644Sthompsa			ep = udev->endpoints + temp;
785190730Sthompsa
786190730Sthompsa			if (do_init) {
787213435Shselasky				void *ecomp;
788213435Shselasky
789213435Shselasky				ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
790213435Shselasky				if (ecomp != NULL)
791213435Shselasky					DPRINTFN(5, "Found endpoint companion descriptor\n");
792213435Shselasky
793194228Sthompsa				usb_init_endpoint(udev,
794213435Shselasky				    ips.iface_index, ed, ecomp, ep);
795190730Sthompsa			}
796190730Sthompsa
797190730Sthompsa			temp ++;
798190730Sthompsa
799190730Sthompsa			/* find maximum number of endpoints */
800190730Sthompsa			if (ep_max < temp)
801190730Sthompsa				ep_max = temp;
802190730Sthompsa		}
803190730Sthompsa	}
804190730Sthompsa
805190730Sthompsa	/* NOTE: It is valid to have no interfaces and no endpoints! */
806190730Sthompsa
807190730Sthompsa	if (cmd == USB_CFG_ALLOC) {
808190730Sthompsa		udev->ifaces_max = ips.iface_index;
809250204Shselasky#if (USB_HAVE_FIXED_IFACE == 0)
810190730Sthompsa		udev->ifaces = NULL;
811190730Sthompsa		if (udev->ifaces_max != 0) {
812190730Sthompsa			udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
813190730Sthompsa			        M_USB, M_WAITOK | M_ZERO);
814190730Sthompsa			if (udev->ifaces == NULL) {
815190730Sthompsa				err = USB_ERR_NOMEM;
816190730Sthompsa				goto done;
817190730Sthompsa			}
818190730Sthompsa		}
819250204Shselasky#endif
820250204Shselasky#if (USB_HAVE_FIXED_ENDPOINT == 0)
821191398Sthompsa		if (ep_max != 0) {
822193644Sthompsa			udev->endpoints = malloc(sizeof(*ep) * ep_max,
823190730Sthompsa			        M_USB, M_WAITOK | M_ZERO);
824193644Sthompsa			if (udev->endpoints == NULL) {
825190730Sthompsa				err = USB_ERR_NOMEM;
826190730Sthompsa				goto done;
827190730Sthompsa			}
828191398Sthompsa		} else {
829193644Sthompsa			udev->endpoints = NULL;
830190730Sthompsa		}
831250204Shselasky#endif
832191398Sthompsa		USB_BUS_LOCK(udev->bus);
833193644Sthompsa		udev->endpoints_max = ep_max;
834191398Sthompsa		/* reset any ongoing clear-stall */
835193644Sthompsa		udev->ep_curr = NULL;
836191398Sthompsa		USB_BUS_UNLOCK(udev->bus);
837190730Sthompsa	}
838250212Shselasky#if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0)
839190730Sthompsadone:
840250212Shselasky#endif
841190730Sthompsa	if (err) {
842190730Sthompsa		if (cmd == USB_CFG_ALLOC) {
843190730Sthompsacleanup:
844191398Sthompsa			USB_BUS_LOCK(udev->bus);
845193644Sthompsa			udev->endpoints_max = 0;
846191398Sthompsa			/* reset any ongoing clear-stall */
847193644Sthompsa			udev->ep_curr = NULL;
848191398Sthompsa			USB_BUS_UNLOCK(udev->bus);
849191398Sthompsa
850250204Shselasky#if (USB_HAVE_FIXED_IFACE == 0)
851250204Shselasky			free(udev->ifaces, M_USB);
852190730Sthompsa			udev->ifaces = NULL;
853250204Shselasky#endif
854250204Shselasky#if (USB_HAVE_FIXED_ENDPOINT == 0)
855250204Shselasky			free(udev->endpoints, M_USB);
856193644Sthompsa			udev->endpoints = NULL;
857250204Shselasky#endif
858190730Sthompsa			udev->ifaces_max = 0;
859190730Sthompsa		}
860190730Sthompsa	}
861190730Sthompsa	return (err);
862190730Sthompsa}
863190730Sthompsa
864190730Sthompsa/*------------------------------------------------------------------------*
865194228Sthompsa *	usbd_set_alt_interface_index
866184610Salfred *
867184610Salfred * This function will select an alternate interface index for the
868184610Salfred * given interface index. The interface should not be in use when this
869188986Sthompsa * function is called. That means there should not be any open USB
870188986Sthompsa * transfers. Else an error is returned. If the alternate setting is
871188986Sthompsa * already set this function will simply return success. This function
872188986Sthompsa * is called in Host mode and Device mode!
873184610Salfred *
874184610Salfred * Returns:
875184610Salfred *    0: Success
876184610Salfred * Else: Failure
877184610Salfred *------------------------------------------------------------------------*/
878193045Sthompsausb_error_t
879194228Sthompsausbd_set_alt_interface_index(struct usb_device *udev,
880184610Salfred    uint8_t iface_index, uint8_t alt_index)
881184610Salfred{
882194228Sthompsa	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
883193045Sthompsa	usb_error_t err;
884184610Salfred	uint8_t do_unlock;
885184610Salfred
886246616Shselasky	/* Prevent re-enumeration */
887246616Shselasky	do_unlock = usbd_enum_lock(udev);
888246616Shselasky
889184610Salfred	if (iface == NULL) {
890184610Salfred		err = USB_ERR_INVAL;
891184610Salfred		goto done;
892184610Salfred	}
893195963Salfred	if (iface->alt_index == alt_index) {
894195963Salfred		/*
895195963Salfred		 * Optimise away duplicate setting of
896195963Salfred		 * alternate setting in USB Host Mode!
897195963Salfred		 */
898195963Salfred		err = 0;
899195963Salfred		goto done;
900184610Salfred	}
901189599Sthompsa#if USB_HAVE_UGEN
902185087Salfred	/*
903185087Salfred	 * Free all generic FIFOs for this interface, except control
904185087Salfred	 * endpoint FIFOs:
905185087Salfred	 */
906194228Sthompsa	usb_fifo_free_wrap(udev, iface_index, 0);
907189599Sthompsa#endif
908190730Sthompsa
909194228Sthompsa	err = usb_config_parse(udev, iface_index, alt_index);
910184610Salfred	if (err) {
911184610Salfred		goto done;
912184610Salfred	}
913195963Salfred	if (iface->alt_index != alt_index) {
914195963Salfred		/* the alternate setting does not exist */
915195963Salfred		err = USB_ERR_INVAL;
916195963Salfred		goto done;
917195963Salfred	}
918195963Salfred
919194228Sthompsa	err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
920184610Salfred	    iface->idesc->bAlternateSetting);
921184610Salfred
922184610Salfreddone:
923196498Salfred	if (do_unlock)
924196498Salfred		usbd_enum_unlock(udev);
925184610Salfred	return (err);
926184610Salfred}
927184610Salfred
928184610Salfred/*------------------------------------------------------------------------*
929194228Sthompsa *	usbd_set_endpoint_stall
930184610Salfred *
931213435Shselasky * This function is used to make a BULK or INTERRUPT endpoint send
932213435Shselasky * STALL tokens in USB device mode.
933184610Salfred *
934184610Salfred * Returns:
935184610Salfred *    0: Success
936184610Salfred * Else: Failure
937184610Salfred *------------------------------------------------------------------------*/
938193045Sthompsausb_error_t
939194228Sthompsausbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
940184610Salfred    uint8_t do_stall)
941184610Salfred{
942192984Sthompsa	struct usb_xfer *xfer;
943239214Shselasky	usb_stream_t x;
944184610Salfred	uint8_t et;
945184610Salfred	uint8_t was_stalled;
946184610Salfred
947193644Sthompsa	if (ep == NULL) {
948184610Salfred		/* nothing to do */
949184610Salfred		DPRINTF("Cannot find endpoint\n");
950184610Salfred		/*
951184610Salfred		 * Pretend that the clear or set stall request is
952184610Salfred		 * successful else some USB host stacks can do
953184610Salfred		 * strange things, especially when a control endpoint
954184610Salfred		 * stalls.
955184610Salfred		 */
956184610Salfred		return (0);
957184610Salfred	}
958193644Sthompsa	et = (ep->edesc->bmAttributes & UE_XFERTYPE);
959184610Salfred
960184610Salfred	if ((et != UE_BULK) &&
961184610Salfred	    (et != UE_INTERRUPT)) {
962184610Salfred		/*
963184610Salfred	         * Should not stall control
964184610Salfred	         * nor isochronous endpoints.
965184610Salfred	         */
966184610Salfred		DPRINTF("Invalid endpoint\n");
967184610Salfred		return (0);
968184610Salfred	}
969184824Sthompsa	USB_BUS_LOCK(udev->bus);
970184610Salfred
971184610Salfred	/* store current stall state */
972193644Sthompsa	was_stalled = ep->is_stalled;
973184610Salfred
974184610Salfred	/* check for no change */
975184610Salfred	if (was_stalled && do_stall) {
976193644Sthompsa		/* if the endpoint is already stalled do nothing */
977184824Sthompsa		USB_BUS_UNLOCK(udev->bus);
978184610Salfred		DPRINTF("No change\n");
979184610Salfred		return (0);
980184610Salfred	}
981184610Salfred	/* set stalled state */
982193644Sthompsa	ep->is_stalled = 1;
983184610Salfred
984184610Salfred	if (do_stall || (!was_stalled)) {
985184610Salfred		if (!was_stalled) {
986239214Shselasky			for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
987239214Shselasky				/* lookup the current USB transfer, if any */
988239214Shselasky				xfer = ep->endpoint_q[x].curr;
989239214Shselasky				if (xfer != NULL) {
990239214Shselasky					/*
991239214Shselasky					 * The "xfer_stall" method
992239214Shselasky					 * will complete the USB
993239214Shselasky					 * transfer like in case of a
994239214Shselasky					 * timeout setting the error
995239214Shselasky					 * code "USB_ERR_STALLED".
996239214Shselasky					 */
997239214Shselasky					(udev->bus->methods->xfer_stall) (xfer);
998239214Shselasky				}
999239214Shselasky			}
1000184610Salfred		}
1001239214Shselasky		(udev->bus->methods->set_stall) (udev, ep, &do_stall);
1002184610Salfred	}
1003184610Salfred	if (!do_stall) {
1004193644Sthompsa		ep->toggle_next = 0;	/* reset data toggle */
1005193644Sthompsa		ep->is_stalled = 0;	/* clear stalled state */
1006184610Salfred
1007193644Sthompsa		(udev->bus->methods->clear_stall) (udev, ep);
1008184610Salfred
1009239214Shselasky		/* start the current or next transfer, if any */
1010239214Shselasky		for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
1011239214Shselasky			usb_command_wrapper(&ep->endpoint_q[x],
1012239214Shselasky			    ep->endpoint_q[x].curr);
1013239214Shselasky		}
1014184610Salfred	}
1015184824Sthompsa	USB_BUS_UNLOCK(udev->bus);
1016184610Salfred	return (0);
1017184610Salfred}
1018184610Salfred
1019184610Salfred/*------------------------------------------------------------------------*
1020194228Sthompsa *	usb_reset_iface_endpoints - used in USB device side mode
1021184610Salfred *------------------------------------------------------------------------*/
1022193045Sthompsausb_error_t
1023194228Sthompsausb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
1024184610Salfred{
1025193644Sthompsa	struct usb_endpoint *ep;
1026193644Sthompsa	struct usb_endpoint *ep_end;
1027184610Salfred
1028193644Sthompsa	ep = udev->endpoints;
1029193644Sthompsa	ep_end = udev->endpoints + udev->endpoints_max;
1030184610Salfred
1031193644Sthompsa	for (; ep != ep_end; ep++) {
1032184610Salfred
1033193644Sthompsa		if ((ep->edesc == NULL) ||
1034193644Sthompsa		    (ep->iface_index != iface_index)) {
1035184610Salfred			continue;
1036184610Salfred		}
1037184610Salfred		/* simulate a clear stall from the peer */
1038195963Salfred		usbd_set_endpoint_stall(udev, ep, 0);
1039184610Salfred	}
1040184610Salfred	return (0);
1041184610Salfred}
1042184610Salfred
1043184610Salfred/*------------------------------------------------------------------------*
1044194228Sthompsa *	usb_detach_device_sub
1045184610Salfred *
1046184610Salfred * This function will try to detach an USB device. If it fails a panic
1047184610Salfred * will result.
1048190730Sthompsa *
1049190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX".
1050184610Salfred *------------------------------------------------------------------------*/
1051184610Salfredstatic void
1052194228Sthompsausb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
1053214429Shselasky    char **ppnpinfo, uint8_t flag)
1054184610Salfred{
1055184610Salfred	device_t dev;
1056214429Shselasky	char *pnpinfo;
1057184610Salfred	int err;
1058184610Salfred
1059197553Sthompsa	dev = *ppdev;
1060197553Sthompsa	if (dev) {
1061184610Salfred		/*
1062184610Salfred		 * NOTE: It is important to clear "*ppdev" before deleting
1063184610Salfred		 * the child due to some device methods being called late
1064184610Salfred		 * during the delete process !
1065184610Salfred		 */
1066184610Salfred		*ppdev = NULL;
1067184610Salfred
1068184610Salfred		device_printf(dev, "at %s, port %d, addr %d "
1069184610Salfred		    "(disconnected)\n",
1070184610Salfred		    device_get_nameunit(udev->parent_dev),
1071184610Salfred		    udev->port_no, udev->address);
1072184610Salfred
1073184610Salfred		if (device_is_attached(dev)) {
1074191824Sthompsa			if (udev->flags.peer_suspended) {
1075184610Salfred				err = DEVICE_RESUME(dev);
1076184610Salfred				if (err) {
1077199816Sthompsa					device_printf(dev, "Resume failed\n");
1078184610Salfred				}
1079184610Salfred			}
1080184610Salfred			if (device_detach(dev)) {
1081184610Salfred				goto error;
1082184610Salfred			}
1083184610Salfred		}
1084184610Salfred		if (device_delete_child(udev->parent_dev, dev)) {
1085184610Salfred			goto error;
1086184610Salfred		}
1087184610Salfred	}
1088214429Shselasky
1089214429Shselasky	pnpinfo = *ppnpinfo;
1090214429Shselasky	if (pnpinfo != NULL) {
1091214429Shselasky		*ppnpinfo = NULL;
1092214429Shselasky		free(pnpinfo, M_USBDEV);
1093214429Shselasky	}
1094184610Salfred	return;
1095184610Salfred
1096184610Salfrederror:
1097184610Salfred	/* Detach is not allowed to fail in the USB world */
1098214429Shselasky	panic("usb_detach_device_sub: A USB driver would not detach\n");
1099184610Salfred}
1100184610Salfred
1101184610Salfred/*------------------------------------------------------------------------*
1102194228Sthompsa *	usb_detach_device
1103184610Salfred *
1104184610Salfred * The following function will detach the matching interfaces.
1105184610Salfred * This function is NULL safe.
1106190730Sthompsa *
1107190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX".
1108184610Salfred *------------------------------------------------------------------------*/
1109184610Salfredvoid
1110194228Sthompsausb_detach_device(struct usb_device *udev, uint8_t iface_index,
1111190730Sthompsa    uint8_t flag)
1112184610Salfred{
1113192984Sthompsa	struct usb_interface *iface;
1114184610Salfred	uint8_t i;
1115184610Salfred
1116184610Salfred	if (udev == NULL) {
1117184610Salfred		/* nothing to do */
1118184610Salfred		return;
1119184610Salfred	}
1120184610Salfred	DPRINTFN(4, "udev=%p\n", udev);
1121184610Salfred
1122207079Sthompsa	sx_assert(&udev->enum_sx, SA_LOCKED);
1123184610Salfred
1124184610Salfred	/*
1125184610Salfred	 * First detach the child to give the child's detach routine a
1126184610Salfred	 * chance to detach the sub-devices in the correct order.
1127184610Salfred	 * Then delete the child using "device_delete_child()" which
1128184610Salfred	 * will detach all sub-devices from the bottom and upwards!
1129184610Salfred	 */
1130184610Salfred	if (iface_index != USB_IFACE_INDEX_ANY) {
1131184610Salfred		i = iface_index;
1132184610Salfred		iface_index = i + 1;
1133184610Salfred	} else {
1134184610Salfred		i = 0;
1135184610Salfred		iface_index = USB_IFACE_MAX;
1136184610Salfred	}
1137184610Salfred
1138184610Salfred	/* do the detach */
1139184610Salfred
1140184610Salfred	for (; i != iface_index; i++) {
1141184610Salfred
1142194228Sthompsa		iface = usbd_get_iface(udev, i);
1143184610Salfred		if (iface == NULL) {
1144184610Salfred			/* looks like the end of the USB interfaces */
1145184610Salfred			break;
1146184610Salfred		}
1147214429Shselasky		usb_detach_device_sub(udev, &iface->subdev,
1148214429Shselasky		    &iface->pnpinfo, flag);
1149184610Salfred	}
1150184610Salfred}
1151184610Salfred
1152184610Salfred/*------------------------------------------------------------------------*
1153194228Sthompsa *	usb_probe_and_attach_sub
1154184610Salfred *
1155184610Salfred * Returns:
1156184610Salfred *    0: Success
1157184610Salfred * Else: Failure
1158184610Salfred *------------------------------------------------------------------------*/
1159184610Salfredstatic uint8_t
1160194228Sthompsausb_probe_and_attach_sub(struct usb_device *udev,
1161192984Sthompsa    struct usb_attach_arg *uaa)
1162184610Salfred{
1163192984Sthompsa	struct usb_interface *iface;
1164184610Salfred	device_t dev;
1165184610Salfred	int err;
1166184610Salfred
1167184610Salfred	iface = uaa->iface;
1168184610Salfred	if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
1169184610Salfred		/* leave interface alone */
1170184610Salfred		return (0);
1171184610Salfred	}
1172184610Salfred	dev = iface->subdev;
1173184610Salfred	if (dev) {
1174184610Salfred
1175184610Salfred		/* clean up after module unload */
1176184610Salfred
1177184610Salfred		if (device_is_attached(dev)) {
1178184610Salfred			/* already a device there */
1179184610Salfred			return (0);
1180184610Salfred		}
1181184610Salfred		/* clear "iface->subdev" as early as possible */
1182184610Salfred
1183184610Salfred		iface->subdev = NULL;
1184184610Salfred
1185184610Salfred		if (device_delete_child(udev->parent_dev, dev)) {
1186184610Salfred
1187184610Salfred			/*
1188184610Salfred			 * Panic here, else one can get a double call
1189184610Salfred			 * to device_detach().  USB devices should
1190184610Salfred			 * never fail on detach!
1191184610Salfred			 */
1192199816Sthompsa			panic("device_delete_child() failed\n");
1193184610Salfred		}
1194184610Salfred	}
1195184610Salfred	if (uaa->temp_dev == NULL) {
1196184610Salfred
1197184610Salfred		/* create a new child */
1198184610Salfred		uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1199184610Salfred		if (uaa->temp_dev == NULL) {
1200184610Salfred			device_printf(udev->parent_dev,
1201199816Sthompsa			    "Device creation failed\n");
1202184610Salfred			return (1);	/* failure */
1203184610Salfred		}
1204184610Salfred		device_set_ivars(uaa->temp_dev, uaa);
1205184610Salfred		device_quiet(uaa->temp_dev);
1206184610Salfred	}
1207184610Salfred	/*
1208184610Salfred	 * Set "subdev" before probe and attach so that "devd" gets
1209184610Salfred	 * the information it needs.
1210184610Salfred	 */
1211184610Salfred	iface->subdev = uaa->temp_dev;
1212184610Salfred
1213184610Salfred	if (device_probe_and_attach(iface->subdev) == 0) {
1214184610Salfred		/*
1215184610Salfred		 * The USB attach arguments are only available during probe
1216184610Salfred		 * and attach !
1217184610Salfred		 */
1218184610Salfred		uaa->temp_dev = NULL;
1219184610Salfred		device_set_ivars(iface->subdev, NULL);
1220184610Salfred
1221191824Sthompsa		if (udev->flags.peer_suspended) {
1222184610Salfred			err = DEVICE_SUSPEND(iface->subdev);
1223191397Sthompsa			if (err)
1224191397Sthompsa				device_printf(iface->subdev, "Suspend failed\n");
1225184610Salfred		}
1226184610Salfred		return (0);		/* success */
1227184610Salfred	} else {
1228184610Salfred		/* No USB driver found */
1229184610Salfred		iface->subdev = NULL;
1230184610Salfred	}
1231184610Salfred	return (1);			/* failure */
1232184610Salfred}
1233184610Salfred
1234184610Salfred/*------------------------------------------------------------------------*
1235194228Sthompsa *	usbd_set_parent_iface
1236184610Salfred *
1237184610Salfred * Using this function will lock the alternate interface setting on an
1238184610Salfred * interface. It is typically used for multi interface drivers. In USB
1239184610Salfred * device side mode it is assumed that the alternate interfaces all
1240184610Salfred * have the same endpoint descriptors. The default parent index value
1241184610Salfred * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1242184610Salfred * locked.
1243184610Salfred *------------------------------------------------------------------------*/
1244184610Salfredvoid
1245194228Sthompsausbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
1246184610Salfred    uint8_t parent_index)
1247184610Salfred{
1248192984Sthompsa	struct usb_interface *iface;
1249184610Salfred
1250236407Shselasky	if (udev == NULL) {
1251236407Shselasky		/* nothing to do */
1252236407Shselasky		return;
1253236407Shselasky	}
1254194228Sthompsa	iface = usbd_get_iface(udev, iface_index);
1255236407Shselasky	if (iface != NULL)
1256184610Salfred		iface->parent_iface_index = parent_index;
1257184610Salfred}
1258184610Salfred
1259184610Salfredstatic void
1260194228Sthompsausb_init_attach_arg(struct usb_device *udev,
1261192984Sthompsa    struct usb_attach_arg *uaa)
1262184610Salfred{
1263225350Shselasky	memset(uaa, 0, sizeof(*uaa));
1264225350Shselasky
1265184610Salfred	uaa->device = udev;
1266192499Sthompsa	uaa->usb_mode = udev->flags.usb_mode;
1267184610Salfred	uaa->port = udev->port_no;
1268200653Sthompsa	uaa->dev_state = UAA_DEV_READY;
1269184610Salfred
1270184610Salfred	uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1271184610Salfred	uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1272184610Salfred	uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1273184610Salfred	uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1274184610Salfred	uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1275184610Salfred	uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1276184610Salfred	uaa->info.bConfigIndex = udev->curr_config_index;
1277184610Salfred	uaa->info.bConfigNum = udev->curr_config_no;
1278184610Salfred}
1279184610Salfred
1280184610Salfred/*------------------------------------------------------------------------*
1281194228Sthompsa *	usb_probe_and_attach
1282184610Salfred *
1283184610Salfred * This function is called from "uhub_explore_sub()",
1284194228Sthompsa * "usb_handle_set_config()" and "usb_handle_request()".
1285184610Salfred *
1286184610Salfred * Returns:
1287184610Salfred *    0: Success
1288184610Salfred * Else: A control transfer failed
1289184610Salfred *------------------------------------------------------------------------*/
1290193045Sthompsausb_error_t
1291194228Sthompsausb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
1292184610Salfred{
1293192984Sthompsa	struct usb_attach_arg uaa;
1294192984Sthompsa	struct usb_interface *iface;
1295184610Salfred	uint8_t i;
1296184610Salfred	uint8_t j;
1297184610Salfred	uint8_t do_unlock;
1298184610Salfred
1299184610Salfred	if (udev == NULL) {
1300184610Salfred		DPRINTF("udev == NULL\n");
1301184610Salfred		return (USB_ERR_INVAL);
1302184610Salfred	}
1303246616Shselasky	/* Prevent re-enumeration */
1304246616Shselasky	do_unlock = usbd_enum_lock(udev);
1305184610Salfred
1306184610Salfred	if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1307184610Salfred		/* do nothing - no configuration has been set */
1308184610Salfred		goto done;
1309184610Salfred	}
1310184610Salfred	/* setup USB attach arguments */
1311184610Salfred
1312194228Sthompsa	usb_init_attach_arg(udev, &uaa);
1313184610Salfred
1314223512Shselasky	/*
1315223512Shselasky	 * If the whole USB device is targeted, invoke the USB event
1316223512Shselasky	 * handler(s):
1317223512Shselasky	 */
1318223512Shselasky	if (iface_index == USB_IFACE_INDEX_ANY) {
1319223512Shselasky
1320223512Shselasky		EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1321223512Shselasky
1322223512Shselasky		if (uaa.dev_state != UAA_DEV_READY) {
1323223512Shselasky			/* leave device unconfigured */
1324223512Shselasky			usb_unconfigure(udev, 0);
1325223512Shselasky			goto done;
1326223512Shselasky		}
1327223512Shselasky	}
1328223512Shselasky
1329184610Salfred	/* Check if only one interface should be probed: */
1330184610Salfred	if (iface_index != USB_IFACE_INDEX_ANY) {
1331184610Salfred		i = iface_index;
1332184610Salfred		j = i + 1;
1333184610Salfred	} else {
1334184610Salfred		i = 0;
1335184610Salfred		j = USB_IFACE_MAX;
1336184610Salfred	}
1337184610Salfred
1338184610Salfred	/* Do the probe and attach */
1339184610Salfred	for (; i != j; i++) {
1340184610Salfred
1341194228Sthompsa		iface = usbd_get_iface(udev, i);
1342184610Salfred		if (iface == NULL) {
1343184610Salfred			/*
1344184610Salfred			 * Looks like the end of the USB
1345184610Salfred			 * interfaces !
1346184610Salfred			 */
1347184610Salfred			DPRINTFN(2, "end of interfaces "
1348184610Salfred			    "at %u\n", i);
1349184610Salfred			break;
1350184610Salfred		}
1351184610Salfred		if (iface->idesc == NULL) {
1352184610Salfred			/* no interface descriptor */
1353184610Salfred			continue;
1354184610Salfred		}
1355184610Salfred		uaa.iface = iface;
1356184610Salfred
1357184610Salfred		uaa.info.bInterfaceClass =
1358184610Salfred		    iface->idesc->bInterfaceClass;
1359184610Salfred		uaa.info.bInterfaceSubClass =
1360184610Salfred		    iface->idesc->bInterfaceSubClass;
1361184610Salfred		uaa.info.bInterfaceProtocol =
1362184610Salfred		    iface->idesc->bInterfaceProtocol;
1363184610Salfred		uaa.info.bIfaceIndex = i;
1364184610Salfred		uaa.info.bIfaceNum =
1365184610Salfred		    iface->idesc->bInterfaceNumber;
1366195963Salfred		uaa.driver_info = 0;	/* reset driver_info */
1367184610Salfred
1368184610Salfred		DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1369184610Salfred		    uaa.info.bInterfaceClass,
1370184610Salfred		    uaa.info.bInterfaceSubClass,
1371184610Salfred		    uaa.info.bInterfaceProtocol,
1372184610Salfred		    uaa.info.bIfaceIndex,
1373184610Salfred		    uaa.info.bIfaceNum);
1374184610Salfred
1375223513Shselasky		usb_probe_and_attach_sub(udev, &uaa);
1376184610Salfred
1377223513Shselasky		/*
1378223513Shselasky		 * Remove the leftover child, if any, to enforce that
1379223513Shselasky		 * a new nomatch devd event is generated for the next
1380223513Shselasky		 * interface if no driver is found:
1381223513Shselasky		 */
1382223513Shselasky		if (uaa.temp_dev == NULL)
1383223513Shselasky			continue;
1384223513Shselasky		if (device_delete_child(udev->parent_dev, uaa.temp_dev))
1385199816Sthompsa			DPRINTFN(0, "device delete child failed\n");
1386223513Shselasky		uaa.temp_dev = NULL;
1387184610Salfred	}
1388184610Salfreddone:
1389196498Salfred	if (do_unlock)
1390196498Salfred		usbd_enum_unlock(udev);
1391184610Salfred	return (0);
1392184610Salfred}
1393184610Salfred
1394184610Salfred/*------------------------------------------------------------------------*
1395194228Sthompsa *	usb_suspend_resume_sub
1396184610Salfred *
1397184610Salfred * This function is called when the suspend or resume methods should
1398184610Salfred * be executed on an USB device.
1399184610Salfred *------------------------------------------------------------------------*/
1400184610Salfredstatic void
1401194228Sthompsausb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
1402184610Salfred{
1403184610Salfred	int err;
1404184610Salfred
1405184610Salfred	if (dev == NULL) {
1406184610Salfred		return;
1407184610Salfred	}
1408184610Salfred	if (!device_is_attached(dev)) {
1409184610Salfred		return;
1410184610Salfred	}
1411184610Salfred	if (do_suspend) {
1412184610Salfred		err = DEVICE_SUSPEND(dev);
1413184610Salfred	} else {
1414184610Salfred		err = DEVICE_RESUME(dev);
1415184610Salfred	}
1416184610Salfred	if (err) {
1417199816Sthompsa		device_printf(dev, "%s failed\n",
1418184610Salfred		    do_suspend ? "Suspend" : "Resume");
1419184610Salfred	}
1420184610Salfred}
1421184610Salfred
1422184610Salfred/*------------------------------------------------------------------------*
1423194228Sthompsa *	usb_suspend_resume
1424184610Salfred *
1425184610Salfred * The following function will suspend or resume the USB device.
1426184610Salfred *
1427184610Salfred * Returns:
1428184610Salfred *    0: Success
1429184610Salfred * Else: Failure
1430184610Salfred *------------------------------------------------------------------------*/
1431193045Sthompsausb_error_t
1432194228Sthompsausb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
1433184610Salfred{
1434192984Sthompsa	struct usb_interface *iface;
1435184610Salfred	uint8_t i;
1436184610Salfred
1437184610Salfred	if (udev == NULL) {
1438184610Salfred		/* nothing to do */
1439184610Salfred		return (0);
1440184610Salfred	}
1441184610Salfred	DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1442184610Salfred
1443208008Sthompsa	sx_assert(&udev->sr_sx, SA_LOCKED);
1444184610Salfred
1445184824Sthompsa	USB_BUS_LOCK(udev->bus);
1446184610Salfred	/* filter the suspend events */
1447191824Sthompsa	if (udev->flags.peer_suspended == do_suspend) {
1448184824Sthompsa		USB_BUS_UNLOCK(udev->bus);
1449184610Salfred		/* nothing to do */
1450184610Salfred		return (0);
1451184610Salfred	}
1452191824Sthompsa	udev->flags.peer_suspended = do_suspend;
1453184824Sthompsa	USB_BUS_UNLOCK(udev->bus);
1454184610Salfred
1455184610Salfred	/* do the suspend or resume */
1456184610Salfred
1457184610Salfred	for (i = 0; i != USB_IFACE_MAX; i++) {
1458184610Salfred
1459194228Sthompsa		iface = usbd_get_iface(udev, i);
1460184610Salfred		if (iface == NULL) {
1461184610Salfred			/* looks like the end of the USB interfaces */
1462184610Salfred			break;
1463184610Salfred		}
1464194228Sthompsa		usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
1465184610Salfred	}
1466184610Salfred	return (0);
1467184610Salfred}
1468184610Salfred
1469184610Salfred/*------------------------------------------------------------------------*
1470194228Sthompsa *      usbd_clear_stall_proc
1471184610Salfred *
1472184610Salfred * This function performs generic USB clear stall operations.
1473184610Salfred *------------------------------------------------------------------------*/
1474184610Salfredstatic void
1475194228Sthompsausbd_clear_stall_proc(struct usb_proc_msg *_pm)
1476184610Salfred{
1477192984Sthompsa	struct usb_clear_stall_msg *pm = (void *)_pm;
1478192984Sthompsa	struct usb_device *udev = pm->udev;
1479184610Salfred
1480184610Salfred	/* Change lock */
1481184824Sthompsa	USB_BUS_UNLOCK(udev->bus);
1482207079Sthompsa	mtx_lock(&udev->device_mtx);
1483184610Salfred
1484184610Salfred	/* Start clear stall callback */
1485207080Sthompsa	usbd_transfer_start(udev->ctrl_xfer[1]);
1486184610Salfred
1487184610Salfred	/* Change lock */
1488207079Sthompsa	mtx_unlock(&udev->device_mtx);
1489184824Sthompsa	USB_BUS_LOCK(udev->bus);
1490184610Salfred}
1491184610Salfred
1492184610Salfred/*------------------------------------------------------------------------*
1493194228Sthompsa *	usb_alloc_device
1494184610Salfred *
1495184610Salfred * This function allocates a new USB device. This function is called
1496184610Salfred * when a new device has been put in the powered state, but not yet in
1497184610Salfred * the addressed state. Get initial descriptor, set the address, get
1498184610Salfred * full descriptor and get strings.
1499184610Salfred *
1500184610Salfred * Return values:
1501184610Salfred *    0: Failure
1502184610Salfred * Else: Success
1503184610Salfred *------------------------------------------------------------------------*/
1504192984Sthompsastruct usb_device *
1505194228Sthompsausb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1506192984Sthompsa    struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
1507192500Sthompsa    uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
1508184610Salfred{
1509192984Sthompsa	struct usb_attach_arg uaa;
1510192984Sthompsa	struct usb_device *udev;
1511192984Sthompsa	struct usb_device *adev;
1512192984Sthompsa	struct usb_device *hub;
1513184610Salfred	uint8_t *scratch_ptr;
1514193045Sthompsa	usb_error_t err;
1515184610Salfred	uint8_t device_index;
1516200653Sthompsa	uint8_t config_index;
1517200653Sthompsa	uint8_t config_quirk;
1518200653Sthompsa	uint8_t set_config_failed;
1519246616Shselasky	uint8_t do_unlock;
1520184610Salfred
1521184610Salfred	DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1522192499Sthompsa	    "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
1523184610Salfred	    parent_dev, bus, parent_hub, depth, port_index, port_no,
1524192499Sthompsa	    speed, mode);
1525184610Salfred
1526184610Salfred	/*
1527184610Salfred	 * Find an unused device index. In USB Host mode this is the
1528184610Salfred	 * same as the device address.
1529184610Salfred	 *
1530187170Sthompsa	 * Device index zero is not used and device index 1 should
1531187170Sthompsa	 * always be the root hub.
1532184610Salfred	 */
1533187171Sthompsa	for (device_index = USB_ROOT_HUB_ADDR;
1534187171Sthompsa	    (device_index != bus->devices_max) &&
1535187171Sthompsa	    (bus->devices[device_index] != NULL);
1536187171Sthompsa	    device_index++) /* nop */;
1537187171Sthompsa
1538187171Sthompsa	if (device_index == bus->devices_max) {
1539187171Sthompsa		device_printf(bus->bdev,
1540199816Sthompsa		    "No free USB device index for new device\n");
1541187171Sthompsa		return (NULL);
1542184610Salfred	}
1543184610Salfred
1544184610Salfred	if (depth > 0x10) {
1545184610Salfred		device_printf(bus->bdev,
1546199816Sthompsa		    "Invalid device depth\n");
1547184610Salfred		return (NULL);
1548184610Salfred	}
1549184610Salfred	udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1550184610Salfred	if (udev == NULL) {
1551184610Salfred		return (NULL);
1552184610Salfred	}
1553184610Salfred	/* initialise our SX-lock */
1554207079Sthompsa	sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1555223512Shselasky	sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
1556184610Salfred
1557207079Sthompsa	cv_init(&udev->ctrlreq_cv, "WCTRL");
1558207079Sthompsa	cv_init(&udev->ref_cv, "UGONE");
1559184610Salfred
1560184610Salfred	/* initialise our mutex */
1561207079Sthompsa	mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
1562184610Salfred
1563184610Salfred	/* initialise generic clear stall */
1564194228Sthompsa	udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
1565184610Salfred	udev->cs_msg[0].udev = udev;
1566194228Sthompsa	udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
1567184610Salfred	udev->cs_msg[1].udev = udev;
1568184610Salfred
1569184610Salfred	/* initialise some USB device fields */
1570184610Salfred	udev->parent_hub = parent_hub;
1571184610Salfred	udev->parent_dev = parent_dev;
1572184610Salfred	udev->port_index = port_index;
1573184610Salfred	udev->port_no = port_no;
1574184610Salfred	udev->depth = depth;
1575184610Salfred	udev->bus = bus;
1576184610Salfred	udev->address = USB_START_ADDR;	/* default value */
1577193045Sthompsa	udev->plugtime = (usb_ticks_t)ticks;
1578186730Salfred	/*
1579186730Salfred	 * We need to force the power mode to "on" because there are plenty
1580186730Salfred	 * of USB devices out there that do not work very well with
1581186730Salfred	 * automatic suspend and resume!
1582186730Salfred	 */
1583212135Sthompsa	udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
1584186730Salfred	udev->pwr_save.last_xfer_time = ticks;
1585184610Salfred	/* we are not ready yet */
1586184610Salfred	udev->refcount = 1;
1587184610Salfred
1588184610Salfred	/* set up default endpoint descriptor */
1589207080Sthompsa	udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
1590207080Sthompsa	udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1591207080Sthompsa	udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1592207080Sthompsa	udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
1593207080Sthompsa	udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1594207080Sthompsa	udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
1595207080Sthompsa	udev->ctrl_ep_desc.bInterval = 0;
1596213435Shselasky
1597213435Shselasky	/* set up default endpoint companion descriptor */
1598213435Shselasky	udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1599213435Shselasky	udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1600213435Shselasky
1601184610Salfred	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1602184610Salfred
1603184610Salfred	udev->speed = speed;
1604192499Sthompsa	udev->flags.usb_mode = mode;
1605184610Salfred
1606184610Salfred	/* search for our High Speed USB HUB, if any */
1607184610Salfred
1608184610Salfred	adev = udev;
1609184610Salfred	hub = udev->parent_hub;
1610184610Salfred
1611184610Salfred	while (hub) {
1612184610Salfred		if (hub->speed == USB_SPEED_HIGH) {
1613184610Salfred			udev->hs_hub_addr = hub->address;
1614191395Sthompsa			udev->parent_hs_hub = hub;
1615184610Salfred			udev->hs_port_no = adev->port_no;
1616184610Salfred			break;
1617184610Salfred		}
1618184610Salfred		adev = hub;
1619184610Salfred		hub = hub->parent_hub;
1620184610Salfred	}
1621184610Salfred
1622193644Sthompsa	/* init the default endpoint */
1623194228Sthompsa	usb_init_endpoint(udev, 0,
1624207080Sthompsa	    &udev->ctrl_ep_desc,
1625213435Shselasky	    &udev->ctrl_ep_comp_desc,
1626207080Sthompsa	    &udev->ctrl_ep);
1627184610Salfred
1628184610Salfred	/* set device index */
1629184610Salfred	udev->device_index = device_index;
1630184610Salfred
1631190191Sthompsa#if USB_HAVE_UGEN
1632189599Sthompsa	/* Create ugen name */
1633189599Sthompsa	snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1634189599Sthompsa	    USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1635189599Sthompsa	    device_index);
1636190191Sthompsa	LIST_INIT(&udev->pd_list);
1637190191Sthompsa
1638189110Sthompsa	/* Create the control endpoint device */
1639224777Shselasky	udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0,
1640224777Shselasky	    FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600);
1641189599Sthompsa
1642189110Sthompsa	/* Create a link from /dev/ugenX.X to the default endpoint */
1643224777Shselasky	if (udev->ctrl_dev != NULL)
1644224777Shselasky		make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name);
1645189599Sthompsa#endif
1646213435Shselasky	/* Initialise device */
1647213435Shselasky	if (bus->methods->device_init != NULL) {
1648213435Shselasky		err = (bus->methods->device_init) (udev);
1649213435Shselasky		if (err != 0) {
1650213435Shselasky			DPRINTFN(0, "device init %d failed "
1651213435Shselasky			    "(%s, ignored)\n", device_index,
1652213435Shselasky			    usbd_errstr(err));
1653213435Shselasky			goto done;
1654213435Shselasky		}
1655213435Shselasky	}
1656213435Shselasky	/* set powered device state after device init is complete */
1657213435Shselasky	usb_set_device_state(udev, USB_STATE_POWERED);
1658213435Shselasky
1659192499Sthompsa	if (udev->flags.usb_mode == USB_MODE_HOST) {
1660184610Salfred
1661194228Sthompsa		err = usbd_req_set_address(udev, NULL, device_index);
1662184610Salfred
1663213435Shselasky		/*
1664213435Shselasky		 * This is the new USB device address from now on, if
1665213435Shselasky		 * the set address request didn't set it already.
1666213435Shselasky		 */
1667213435Shselasky		if (udev->address == USB_START_ADDR)
1668213435Shselasky			udev->address = device_index;
1669184610Salfred
1670184610Salfred		/*
1671184610Salfred		 * We ignore any set-address errors, hence there are
1672184610Salfred		 * buggy USB devices out there that actually receive
1673184610Salfred		 * the SETUP PID, but manage to set the address before
1674184610Salfred		 * the STATUS stage is ACK'ed. If the device responds
1675184610Salfred		 * to the subsequent get-descriptor at the new
1676184610Salfred		 * address, then we know that the set-address command
1677184610Salfred		 * was successful.
1678184610Salfred		 */
1679184610Salfred		if (err) {
1680184610Salfred			DPRINTFN(0, "set address %d failed "
1681190739Sthompsa			    "(%s, ignored)\n", udev->address,
1682194228Sthompsa			    usbd_errstr(err));
1683184610Salfred		}
1684184610Salfred	} else {
1685184610Salfred		/* We are not self powered */
1686184610Salfred		udev->flags.self_powered = 0;
1687184610Salfred
1688184610Salfred		/* Set unconfigured state */
1689184610Salfred		udev->curr_config_no = USB_UNCONFIG_NO;
1690184610Salfred		udev->curr_config_index = USB_UNCONFIG_INDEX;
1691184610Salfred
1692184610Salfred		/* Setup USB descriptors */
1693194228Sthompsa		err = (usb_temp_setup_by_index_p) (udev, usb_template);
1694184610Salfred		if (err) {
1695184610Salfred			DPRINTFN(0, "setting up USB template failed maybe the USB "
1696184610Salfred			    "template module has not been loaded\n");
1697184610Salfred			goto done;
1698184610Salfred		}
1699184610Salfred	}
1700194228Sthompsa	usb_set_device_state(udev, USB_STATE_ADDRESSED);
1701184610Salfred
1702213435Shselasky	/* setup the device descriptor and the initial "wMaxPacketSize" */
1703213435Shselasky	err = usbd_setup_device_desc(udev, NULL);
1704213435Shselasky
1705213435Shselasky	if (err != 0) {
1706248246Shselasky		/* try to enumerate two more times */
1707194228Sthompsa		err = usbd_req_re_enumerate(udev, NULL);
1708248246Shselasky		if (err != 0) {
1709248246Shselasky			err = usbd_req_re_enumerate(udev, NULL);
1710248246Shselasky			if (err != 0) {
1711248246Shselasky				goto done;
1712248246Shselasky			}
1713248246Shselasky		}
1714184610Salfred	}
1715184610Salfred
1716184610Salfred	/*
1717184610Salfred	 * Setup temporary USB attach args so that we can figure out some
1718184610Salfred	 * basic quirks for this device.
1719184610Salfred	 */
1720194228Sthompsa	usb_init_attach_arg(udev, &uaa);
1721184610Salfred
1722194228Sthompsa	if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
1723184610Salfred		udev->flags.uq_bus_powered = 1;
1724184610Salfred	}
1725194228Sthompsa	if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
1726184610Salfred		udev->flags.no_strings = 1;
1727184610Salfred	}
1728184610Salfred	/*
1729184610Salfred	 * Workaround for buggy USB devices.
1730184610Salfred	 *
1731184610Salfred	 * It appears that some string-less USB chips will crash and
1732184610Salfred	 * disappear if any attempts are made to read any string
1733184610Salfred	 * descriptors.
1734184610Salfred	 *
1735184610Salfred	 * Try to detect such chips by checking the strings in the USB
1736184610Salfred	 * device descriptor. If no strings are present there we
1737184610Salfred	 * simply disable all USB strings.
1738184610Salfred	 */
1739184610Salfred
1740246616Shselasky	/* Protect scratch area */
1741246616Shselasky	do_unlock = usbd_enum_lock(udev);
1742246616Shselasky
1743246616Shselasky	scratch_ptr = udev->scratch.data;
1744246616Shselasky
1745184610Salfred	if (udev->ddesc.iManufacturer ||
1746184610Salfred	    udev->ddesc.iProduct ||
1747184610Salfred	    udev->ddesc.iSerialNumber) {
1748184610Salfred		/* read out the language ID string */
1749194228Sthompsa		err = usbd_req_get_string_desc(udev, NULL,
1750201680Sthompsa		    (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
1751184610Salfred	} else {
1752184610Salfred		err = USB_ERR_INVAL;
1753184610Salfred	}
1754184610Salfred
1755184610Salfred	if (err || (scratch_ptr[0] < 4)) {
1756184610Salfred		udev->flags.no_strings = 1;
1757184610Salfred	} else {
1758197559Sthompsa		uint16_t langid;
1759197559Sthompsa		uint16_t pref;
1760197559Sthompsa		uint16_t mask;
1761197559Sthompsa		uint8_t x;
1762197559Sthompsa
1763197559Sthompsa		/* load preferred value and mask */
1764197559Sthompsa		pref = usb_lang_id;
1765197559Sthompsa		mask = usb_lang_mask;
1766197559Sthompsa
1767197559Sthompsa		/* align length correctly */
1768246616Shselasky		scratch_ptr[0] &= ~1U;
1769197559Sthompsa
1770197559Sthompsa		/* fix compiler warning */
1771197559Sthompsa		langid = 0;
1772197559Sthompsa
1773197559Sthompsa		/* search for preferred language */
1774197559Sthompsa		for (x = 2; (x < scratch_ptr[0]); x += 2) {
1775197559Sthompsa			langid = UGETW(scratch_ptr + x);
1776197559Sthompsa			if ((langid & mask) == pref)
1777197559Sthompsa				break;
1778197559Sthompsa		}
1779197559Sthompsa		if (x >= scratch_ptr[0]) {
1780197559Sthompsa			/* pick the first language as the default */
1781197559Sthompsa			DPRINTFN(1, "Using first language\n");
1782197559Sthompsa			langid = UGETW(scratch_ptr + 2);
1783197559Sthompsa		}
1784197559Sthompsa
1785197559Sthompsa		DPRINTFN(1, "Language selected: 0x%04x\n", langid);
1786197559Sthompsa		udev->langid = langid;
1787184610Salfred	}
1788184610Salfred
1789246616Shselasky	if (do_unlock)
1790246616Shselasky		usbd_enum_unlock(udev);
1791246616Shselasky
1792184610Salfred	/* assume 100mA bus powered for now. Changed when configured. */
1793184610Salfred	udev->power = USB_MIN_POWER;
1794192938Sthompsa	/* fetch the vendor and product strings from the device */
1795194228Sthompsa	usbd_set_device_strings(udev);
1796184610Salfred
1797200653Sthompsa	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1798200653Sthompsa		/* USB device mode setup is complete */
1799200653Sthompsa		err = 0;
1800200653Sthompsa		goto config_done;
1801200653Sthompsa	}
1802184610Salfred
1803200653Sthompsa	/*
1804200653Sthompsa	 * Most USB devices should attach to config index 0 by
1805200653Sthompsa	 * default
1806200653Sthompsa	 */
1807200653Sthompsa	if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1808200653Sthompsa		config_index = 0;
1809200653Sthompsa		config_quirk = 1;
1810200653Sthompsa	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1811200653Sthompsa		config_index = 1;
1812200653Sthompsa		config_quirk = 1;
1813200653Sthompsa	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1814200653Sthompsa		config_index = 2;
1815200653Sthompsa		config_quirk = 1;
1816200653Sthompsa	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1817200653Sthompsa		config_index = 3;
1818200653Sthompsa		config_quirk = 1;
1819200653Sthompsa	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
1820200653Sthompsa		config_index = 4;
1821200653Sthompsa		config_quirk = 1;
1822200653Sthompsa	} else {
1823200653Sthompsa		config_index = 0;
1824200653Sthompsa		config_quirk = 0;
1825200653Sthompsa	}
1826184610Salfred
1827200653Sthompsa	set_config_failed = 0;
1828184610Salfredrepeat_set_config:
1829184610Salfred
1830200653Sthompsa	DPRINTF("setting config %u\n", config_index);
1831184610Salfred
1832200653Sthompsa	/* get the USB device configured */
1833200653Sthompsa	err = usbd_set_config_index(udev, config_index);
1834200653Sthompsa	if (err) {
1835200653Sthompsa		if (udev->ddesc.bNumConfigurations != 0) {
1836200653Sthompsa			if (!set_config_failed) {
1837200653Sthompsa				set_config_failed = 1;
1838200653Sthompsa				/* XXX try to re-enumerate the device */
1839200653Sthompsa				err = usbd_req_re_enumerate(udev, NULL);
1840200653Sthompsa				if (err == 0)
1841200653Sthompsa					goto repeat_set_config;
1842186730Salfred			}
1843200653Sthompsa			DPRINTFN(0, "Failure selecting configuration index %u:"
1844200653Sthompsa			    "%s, port %u, addr %u (ignored)\n",
1845200653Sthompsa			    config_index, usbd_errstr(err), udev->port_no,
1846200653Sthompsa			    udev->address);
1847200653Sthompsa		}
1848200653Sthompsa		/*
1849200653Sthompsa		 * Some USB devices do not have any configurations. Ignore any
1850200653Sthompsa		 * set config failures!
1851200653Sthompsa		 */
1852200653Sthompsa		err = 0;
1853200653Sthompsa		goto config_done;
1854200653Sthompsa	}
1855200653Sthompsa	if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
1856200653Sthompsa		if ((udev->cdesc->bNumInterface < 2) &&
1857200653Sthompsa		    usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
1858200653Sthompsa			DPRINTFN(0, "Found no endpoints, trying next config\n");
1859200653Sthompsa			config_index++;
1860200653Sthompsa			goto repeat_set_config;
1861200653Sthompsa		}
1862249043Shselasky#if USB_HAVE_MSCTEST
1863200653Sthompsa		if (config_index == 0) {
1864186730Salfred			/*
1865200653Sthompsa			 * Try to figure out if we have an
1866200653Sthompsa			 * auto-install disk there:
1867186730Salfred			 */
1868201681Sthompsa			if (usb_iface_is_cdrom(udev, 0)) {
1869200653Sthompsa				DPRINTFN(0, "Found possible auto-install "
1870200653Sthompsa				    "disk (trying next config)\n");
1871184610Salfred				config_index++;
1872184610Salfred				goto repeat_set_config;
1873184610Salfred			}
1874184610Salfred		}
1875249043Shselasky#endif
1876184610Salfred	}
1877249043Shselasky#if USB_HAVE_MSCTEST
1878225350Shselasky	if (set_config_failed == 0 && config_index == 0 &&
1879226903Shselasky	    usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 &&
1880226903Shselasky	    usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) {
1881184610Salfred
1882225350Shselasky		/*
1883225350Shselasky		 * Try to figure out if there are any MSC quirks we
1884225350Shselasky		 * should apply automatically:
1885225350Shselasky		 */
1886225350Shselasky		err = usb_msc_auto_quirk(udev, 0);
1887225350Shselasky
1888225350Shselasky		if (err != 0) {
1889225350Shselasky			set_config_failed = 1;
1890225350Shselasky			goto repeat_set_config;
1891225350Shselasky		}
1892225350Shselasky	}
1893249043Shselasky#endif
1894225350Shselasky
1895200653Sthompsaconfig_done:
1896184610Salfred	DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
1897184610Salfred	    udev->address, udev, udev->parent_hub);
1898184610Salfred
1899184610Salfred	/* register our device - we are ready */
1900194228Sthompsa	usb_bus_port_set_device(bus, parent_hub ?
1901184610Salfred	    parent_hub->hub->ports + port_index : NULL, udev, device_index);
1902184610Salfred
1903189599Sthompsa#if USB_HAVE_UGEN
1904190191Sthompsa	/* Symlink the ugen device name */
1905194228Sthompsa	udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
1906190191Sthompsa
1907190191Sthompsa	/* Announce device */
1908212136Sthompsa	printf("%s: <%s> at %s\n", udev->ugen_name,
1909212136Sthompsa	    usb_get_manufacturer(udev),
1910184610Salfred	    device_get_nameunit(udev->bus->bdev));
1911214221Shselasky#endif
1912190727Sthompsa
1913214221Shselasky#if USB_HAVE_DEVCTL
1914207020Sthompsa	usb_notify_addq("ATTACH", udev);
1915190191Sthompsa#endif
1916184610Salfreddone:
1917184610Salfred	if (err) {
1918197553Sthompsa		/*
1919197553Sthompsa		 * Free USB device and all subdevices, if any.
1920197553Sthompsa		 */
1921197553Sthompsa		usb_free_device(udev, 0);
1922184610Salfred		udev = NULL;
1923184610Salfred	}
1924184610Salfred	return (udev);
1925184610Salfred}
1926184610Salfred
1927189599Sthompsa#if USB_HAVE_UGEN
1928224777Shselaskystruct usb_fs_privdata *
1929224777Shselaskyusb_make_dev(struct usb_device *udev, const char *devname, int ep,
1930224777Shselasky    int fi, int rwmode, uid_t uid, gid_t gid, int mode)
1931189110Sthompsa{
1932192984Sthompsa	struct usb_fs_privdata* pd;
1933224777Shselasky	char buffer[32];
1934189110Sthompsa
1935189110Sthompsa	/* Store information to locate ourselves again later */
1936192984Sthompsa	pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
1937189110Sthompsa	    M_WAITOK | M_ZERO);
1938189110Sthompsa	pd->bus_index = device_get_unit(udev->bus->bdev);
1939189110Sthompsa	pd->dev_index = udev->device_index;
1940189110Sthompsa	pd->ep_addr = ep;
1941224777Shselasky	pd->fifo_index = fi;
1942224777Shselasky	pd->mode = rwmode;
1943189110Sthompsa
1944189110Sthompsa	/* Now, create the device itself */
1945224777Shselasky	if (devname == NULL) {
1946224777Shselasky		devname = buffer;
1947224777Shselasky		snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u",
1948224777Shselasky		    pd->bus_index, pd->dev_index, pd->ep_addr);
1949224777Shselasky	}
1950224777Shselasky
1951224777Shselasky	pd->cdev = make_dev(&usb_devsw, 0, uid, gid, mode, "%s", devname);
1952224777Shselasky
1953224777Shselasky	if (pd->cdev == NULL) {
1954224777Shselasky		DPRINTFN(0, "Failed to create device %s\n", devname);
1955224777Shselasky		free(pd, M_USBDEV);
1956224777Shselasky		return (NULL);
1957224777Shselasky	}
1958224777Shselasky
1959224777Shselasky	/* XXX setting si_drv1 and creating the device is not atomic! */
1960189110Sthompsa	pd->cdev->si_drv1 = pd;
1961189110Sthompsa
1962224777Shselasky	return (pd);
1963189110Sthompsa}
1964189110Sthompsa
1965224777Shselaskyvoid
1966224777Shselaskyusb_destroy_dev(struct usb_fs_privdata *pd)
1967224777Shselasky{
1968224777Shselasky	if (pd == NULL)
1969224777Shselasky		return;
1970224777Shselasky
1971224777Shselasky	destroy_dev(pd->cdev);
1972224777Shselasky
1973224777Shselasky	free(pd, M_USBDEV);
1974224777Shselasky}
1975224777Shselasky
1976189110Sthompsastatic void
1977194228Sthompsausb_cdev_create(struct usb_device *udev)
1978189110Sthompsa{
1979192984Sthompsa	struct usb_config_descriptor *cd;
1980192984Sthompsa	struct usb_endpoint_descriptor *ed;
1981192984Sthompsa	struct usb_descriptor *desc;
1982192984Sthompsa	struct usb_fs_privdata* pd;
1983189172Sthompsa	int inmode, outmode, inmask, outmask, mode;
1984189172Sthompsa	uint8_t ep;
1985189110Sthompsa
1986189110Sthompsa	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
1987189110Sthompsa
1988189110Sthompsa	DPRINTFN(2, "Creating device nodes\n");
1989189110Sthompsa
1990194228Sthompsa	if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
1991189110Sthompsa		inmode = FWRITE;
1992189110Sthompsa		outmode = FREAD;
1993189110Sthompsa	} else {		 /* USB_MODE_HOST */
1994189110Sthompsa		inmode = FREAD;
1995189110Sthompsa		outmode = FWRITE;
1996189110Sthompsa	}
1997189110Sthompsa
1998189172Sthompsa	inmask = 0;
1999189172Sthompsa	outmask = 0;
2000189172Sthompsa	desc = NULL;
2001189110Sthompsa
2002189172Sthompsa	/*
2003189172Sthompsa	 * Collect all used endpoint numbers instead of just
2004189172Sthompsa	 * generating 16 static endpoints.
2005189172Sthompsa	 */
2006194228Sthompsa	cd = usbd_get_config_descriptor(udev);
2007194228Sthompsa	while ((desc = usb_desc_foreach(cd, desc))) {
2008189172Sthompsa		/* filter out all endpoint descriptors */
2009189172Sthompsa		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
2010189172Sthompsa		    (desc->bLength >= sizeof(*ed))) {
2011192984Sthompsa			ed = (struct usb_endpoint_descriptor *)desc;
2012189110Sthompsa
2013189172Sthompsa			/* update masks */
2014189172Sthompsa			ep = ed->bEndpointAddress;
2015189172Sthompsa			if (UE_GET_DIR(ep)  == UE_DIR_OUT)
2016189172Sthompsa				outmask |= 1 << UE_GET_ADDR(ep);
2017189172Sthompsa			else
2018189172Sthompsa				inmask |= 1 << UE_GET_ADDR(ep);
2019189110Sthompsa		}
2020189110Sthompsa	}
2021189172Sthompsa
2022189172Sthompsa	/* Create all available endpoints except EP0 */
2023189172Sthompsa	for (ep = 1; ep < 16; ep++) {
2024224777Shselasky		mode = (inmask & (1 << ep)) ? inmode : 0;
2025224777Shselasky		mode |= (outmask & (1 << ep)) ? outmode : 0;
2026189172Sthompsa		if (mode == 0)
2027189172Sthompsa			continue;	/* no IN or OUT endpoint */
2028189172Sthompsa
2029224777Shselasky		pd = usb_make_dev(udev, NULL, ep, 0,
2030224777Shselasky		    mode, UID_ROOT, GID_OPERATOR, 0600);
2031224777Shselasky
2032224777Shselasky		if (pd != NULL)
2033224777Shselasky			LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
2034189172Sthompsa	}
2035189110Sthompsa}
2036189110Sthompsa
2037189110Sthompsastatic void
2038194228Sthompsausb_cdev_free(struct usb_device *udev)
2039189110Sthompsa{
2040192984Sthompsa	struct usb_fs_privdata* pd;
2041189110Sthompsa
2042189110Sthompsa	DPRINTFN(2, "Freeing device nodes\n");
2043189110Sthompsa
2044189110Sthompsa	while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
2045189110Sthompsa		KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
2046189110Sthompsa
2047189110Sthompsa		LIST_REMOVE(pd, pd_next);
2048224777Shselasky
2049224777Shselasky		usb_destroy_dev(pd);
2050189110Sthompsa	}
2051189110Sthompsa}
2052189599Sthompsa#endif
2053189110Sthompsa
2054184610Salfred/*------------------------------------------------------------------------*
2055194228Sthompsa *	usb_free_device
2056184610Salfred *
2057197553Sthompsa * This function is NULL safe and will free an USB device and its
2058197553Sthompsa * children devices, if any.
2059190730Sthompsa *
2060197553Sthompsa * Flag values: Reserved, set to zero.
2061184610Salfred *------------------------------------------------------------------------*/
2062184610Salfredvoid
2063194228Sthompsausb_free_device(struct usb_device *udev, uint8_t flag)
2064184610Salfred{
2065192984Sthompsa	struct usb_bus *bus;
2066184610Salfred
2067190730Sthompsa	if (udev == NULL)
2068190730Sthompsa		return;		/* already freed */
2069190730Sthompsa
2070184610Salfred	DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
2071184610Salfred
2072191494Sthompsa	bus = udev->bus;
2073194228Sthompsa	usb_set_device_state(udev, USB_STATE_DETACHED);
2074190730Sthompsa
2075214221Shselasky#if USB_HAVE_DEVCTL
2076207020Sthompsa	usb_notify_addq("DETACH", udev);
2077214221Shselasky#endif
2078184610Salfred
2079214221Shselasky#if USB_HAVE_UGEN
2080189110Sthompsa	printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
2081212136Sthompsa	    usb_get_manufacturer(udev), device_get_nameunit(bus->bdev));
2082184610Salfred
2083189110Sthompsa	/* Destroy UGEN symlink, if any */
2084184610Salfred	if (udev->ugen_symlink) {
2085194228Sthompsa		usb_free_symlink(udev->ugen_symlink);
2086184610Salfred		udev->ugen_symlink = NULL;
2087184610Salfred	}
2088190191Sthompsa#endif
2089184610Salfred	/*
2090184610Salfred	 * Unregister our device first which will prevent any further
2091184610Salfred	 * references:
2092184610Salfred	 */
2093194228Sthompsa	usb_bus_port_set_device(bus, udev->parent_hub ?
2094184610Salfred	    udev->parent_hub->hub->ports + udev->port_index : NULL,
2095184610Salfred	    NULL, USB_ROOT_HUB_ADDR);
2096184610Salfred
2097189599Sthompsa#if USB_HAVE_UGEN
2098184610Salfred	/* wait for all pending references to go away: */
2099194228Sthompsa	mtx_lock(&usb_ref_lock);
2100184610Salfred	udev->refcount--;
2101184610Salfred	while (udev->refcount != 0) {
2102207079Sthompsa		cv_wait(&udev->ref_cv, &usb_ref_lock);
2103184610Salfred	}
2104194228Sthompsa	mtx_unlock(&usb_ref_lock);
2105190730Sthompsa
2106224777Shselasky	usb_destroy_dev(udev->ctrl_dev);
2107189599Sthompsa#endif
2108184610Salfred
2109192499Sthompsa	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2110184610Salfred		/* stop receiving any control transfers (Device Side Mode) */
2111207080Sthompsa		usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2112184610Salfred	}
2113184610Salfred
2114190730Sthompsa	/* the following will get the device unconfigured in software */
2115197553Sthompsa	usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
2116184610Salfred
2117184610Salfred	/* unsetup any leftover default USB transfers */
2118207080Sthompsa	usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2119184610Salfred
2120187174Sthompsa	/* template unsetup, if any */
2121194228Sthompsa	(usb_temp_unsetup_p) (udev);
2122184610Salfred
2123187174Sthompsa	/*
2124187174Sthompsa	 * Make sure that our clear-stall messages are not queued
2125187174Sthompsa	 * anywhere:
2126187174Sthompsa	 */
2127187174Sthompsa	USB_BUS_LOCK(udev->bus);
2128246363Shselasky	usb_proc_mwait(USB_BUS_NON_GIANT_PROC(udev->bus),
2129187174Sthompsa	    &udev->cs_msg[0], &udev->cs_msg[1]);
2130187174Sthompsa	USB_BUS_UNLOCK(udev->bus);
2131187174Sthompsa
2132207079Sthompsa	sx_destroy(&udev->enum_sx);
2133208008Sthompsa	sx_destroy(&udev->sr_sx);
2134184610Salfred
2135207079Sthompsa	cv_destroy(&udev->ctrlreq_cv);
2136207079Sthompsa	cv_destroy(&udev->ref_cv);
2137184610Salfred
2138207079Sthompsa	mtx_destroy(&udev->device_mtx);
2139190191Sthompsa#if USB_HAVE_UGEN
2140189110Sthompsa	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2141190191Sthompsa#endif
2142184610Salfred
2143213435Shselasky	/* Uninitialise device */
2144213435Shselasky	if (bus->methods->device_uninit != NULL)
2145213435Shselasky		(bus->methods->device_uninit) (udev);
2146213435Shselasky
2147184610Salfred	/* free device */
2148193042Sthompsa	free(udev->serial, M_USB);
2149193042Sthompsa	free(udev->manufacturer, M_USB);
2150193042Sthompsa	free(udev->product, M_USB);
2151184610Salfred	free(udev, M_USB);
2152184610Salfred}
2153184610Salfred
2154184610Salfred/*------------------------------------------------------------------------*
2155194228Sthompsa *	usbd_get_iface
2156184610Salfred *
2157184610Salfred * This function is the safe way to get the USB interface structure
2158184610Salfred * pointer by interface index.
2159184610Salfred *
2160184610Salfred * Return values:
2161184610Salfred *   NULL: Interface not present.
2162184610Salfred *   Else: Pointer to USB interface structure.
2163184610Salfred *------------------------------------------------------------------------*/
2164192984Sthompsastruct usb_interface *
2165194228Sthompsausbd_get_iface(struct usb_device *udev, uint8_t iface_index)
2166184610Salfred{
2167192984Sthompsa	struct usb_interface *iface = udev->ifaces + iface_index;
2168184610Salfred
2169190730Sthompsa	if (iface_index >= udev->ifaces_max)
2170184610Salfred		return (NULL);
2171184610Salfred	return (iface);
2172184610Salfred}
2173184610Salfred
2174184610Salfred/*------------------------------------------------------------------------*
2175194271Sthompsa *	usbd_find_descriptor
2176184610Salfred *
2177184610Salfred * This function will lookup the first descriptor that matches the
2178184610Salfred * criteria given by the arguments "type" and "subtype". Descriptors
2179184610Salfred * will only be searched within the interface having the index
2180184610Salfred * "iface_index".  If the "id" argument points to an USB descriptor,
2181184610Salfred * it will be skipped before the search is started. This allows
2182184610Salfred * searching for multiple descriptors using the same criteria. Else
2183184610Salfred * the search is started after the interface descriptor.
2184184610Salfred *
2185184610Salfred * Return values:
2186184610Salfred *   NULL: End of descriptors
2187184610Salfred *   Else: A descriptor matching the criteria
2188184610Salfred *------------------------------------------------------------------------*/
2189184610Salfredvoid   *
2190194271Sthompsausbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
2191184610Salfred    uint8_t type, uint8_t type_mask,
2192184610Salfred    uint8_t subtype, uint8_t subtype_mask)
2193184610Salfred{
2194192984Sthompsa	struct usb_descriptor *desc;
2195192984Sthompsa	struct usb_config_descriptor *cd;
2196192984Sthompsa	struct usb_interface *iface;
2197184610Salfred
2198194228Sthompsa	cd = usbd_get_config_descriptor(udev);
2199184610Salfred	if (cd == NULL) {
2200184610Salfred		return (NULL);
2201184610Salfred	}
2202184610Salfred	if (id == NULL) {
2203194228Sthompsa		iface = usbd_get_iface(udev, iface_index);
2204184610Salfred		if (iface == NULL) {
2205184610Salfred			return (NULL);
2206184610Salfred		}
2207194228Sthompsa		id = usbd_get_interface_descriptor(iface);
2208184610Salfred		if (id == NULL) {
2209184610Salfred			return (NULL);
2210184610Salfred		}
2211184610Salfred	}
2212184610Salfred	desc = (void *)id;
2213184610Salfred
2214194228Sthompsa	while ((desc = usb_desc_foreach(cd, desc))) {
2215184610Salfred
2216184610Salfred		if (desc->bDescriptorType == UDESC_INTERFACE) {
2217184610Salfred			break;
2218184610Salfred		}
2219184610Salfred		if (((desc->bDescriptorType & type_mask) == type) &&
2220184610Salfred		    ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2221184610Salfred			return (desc);
2222184610Salfred		}
2223184610Salfred	}
2224184610Salfred	return (NULL);
2225184610Salfred}
2226184610Salfred
2227184610Salfred/*------------------------------------------------------------------------*
2228194228Sthompsa *	usb_devinfo
2229184610Salfred *
2230184610Salfred * This function will dump information from the device descriptor
2231184610Salfred * belonging to the USB device pointed to by "udev", to the string
2232184610Salfred * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2233184610Salfred * including the terminating zero.
2234184610Salfred *------------------------------------------------------------------------*/
2235184610Salfredvoid
2236194228Sthompsausb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
2237184610Salfred{
2238192984Sthompsa	struct usb_device_descriptor *udd = &udev->ddesc;
2239184610Salfred	uint16_t bcdDevice;
2240184610Salfred	uint16_t bcdUSB;
2241184610Salfred
2242184610Salfred	bcdUSB = UGETW(udd->bcdUSB);
2243184610Salfred	bcdDevice = UGETW(udd->bcdDevice);
2244184610Salfred
2245184610Salfred	if (udd->bDeviceClass != 0xFF) {
2246184610Salfred		snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
2247190191Sthompsa		    "%x.%02x, addr %d",
2248212136Sthompsa		    usb_get_manufacturer(udev),
2249212136Sthompsa		    usb_get_product(udev),
2250184610Salfred		    udd->bDeviceClass, udd->bDeviceSubClass,
2251184610Salfred		    (bcdUSB >> 8), bcdUSB & 0xFF,
2252184610Salfred		    (bcdDevice >> 8), bcdDevice & 0xFF,
2253184610Salfred		    udev->address);
2254184610Salfred	} else {
2255184610Salfred		snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
2256190191Sthompsa		    "%x.%02x, addr %d",
2257212136Sthompsa		    usb_get_manufacturer(udev),
2258212136Sthompsa		    usb_get_product(udev),
2259184610Salfred		    (bcdUSB >> 8), bcdUSB & 0xFF,
2260184610Salfred		    (bcdDevice >> 8), bcdDevice & 0xFF,
2261184610Salfred		    udev->address);
2262184610Salfred	}
2263184610Salfred}
2264184610Salfred
2265194677Sthompsa#ifdef USB_VERBOSE
2266184610Salfred/*
2267184610Salfred * Descriptions of of known vendors and devices ("products").
2268184610Salfred */
2269184610Salfredstruct usb_knowndev {
2270184610Salfred	uint16_t vendor;
2271184610Salfred	uint16_t product;
2272184610Salfred	uint32_t flags;
2273184610Salfred	const char *vendorname;
2274184610Salfred	const char *productname;
2275184610Salfred};
2276184610Salfred
2277184610Salfred#define	USB_KNOWNDEV_NOPROD	0x01	/* match on vendor only */
2278184610Salfred
2279188746Sthompsa#include "usbdevs.h"
2280188746Sthompsa#include "usbdevs_data.h"
2281184610Salfred#endif					/* USB_VERBOSE */
2282184610Salfred
2283184610Salfredstatic void
2284194228Sthompsausbd_set_device_strings(struct usb_device *udev)
2285184610Salfred{
2286192984Sthompsa	struct usb_device_descriptor *udd = &udev->ddesc;
2287194677Sthompsa#ifdef USB_VERBOSE
2288184610Salfred	const struct usb_knowndev *kdp;
2289184610Salfred#endif
2290208008Sthompsa	char *temp_ptr;
2291197559Sthompsa	size_t temp_size;
2292184610Salfred	uint16_t vendor_id;
2293184610Salfred	uint16_t product_id;
2294246616Shselasky	uint8_t do_unlock;
2295184610Salfred
2296246616Shselasky	/* Protect scratch area */
2297246616Shselasky	do_unlock = usbd_enum_lock(udev);
2298197559Sthompsa
2299246616Shselasky	temp_ptr = (char *)udev->scratch.data;
2300246616Shselasky	temp_size = sizeof(udev->scratch.data);
2301246616Shselasky
2302192938Sthompsa	vendor_id = UGETW(udd->idVendor);
2303192938Sthompsa	product_id = UGETW(udd->idProduct);
2304184610Salfred
2305192938Sthompsa	/* get serial number string */
2306197559Sthompsa	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2307192938Sthompsa	    udev->ddesc.iSerialNumber);
2308197559Sthompsa	udev->serial = strdup(temp_ptr, M_USB);
2309184610Salfred
2310192938Sthompsa	/* get manufacturer string */
2311197559Sthompsa	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2312192938Sthompsa	    udev->ddesc.iManufacturer);
2313197559Sthompsa	usb_trim_spaces(temp_ptr);
2314197559Sthompsa	if (temp_ptr[0] != '\0')
2315197559Sthompsa		udev->manufacturer = strdup(temp_ptr, M_USB);
2316184610Salfred
2317192938Sthompsa	/* get product string */
2318197559Sthompsa	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2319192938Sthompsa	    udev->ddesc.iProduct);
2320197559Sthompsa	usb_trim_spaces(temp_ptr);
2321197559Sthompsa	if (temp_ptr[0] != '\0')
2322197559Sthompsa		udev->product = strdup(temp_ptr, M_USB);
2323184610Salfred
2324194677Sthompsa#ifdef USB_VERBOSE
2325192938Sthompsa	if (udev->manufacturer == NULL || udev->product == NULL) {
2326192938Sthompsa		for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
2327184610Salfred			if (kdp->vendor == vendor_id &&
2328184610Salfred			    (kdp->product == product_id ||
2329184610Salfred			    (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
2330184610Salfred				break;
2331184610Salfred		}
2332184610Salfred		if (kdp->vendorname != NULL) {
2333192938Sthompsa			/* XXX should use pointer to knowndevs string */
2334192938Sthompsa			if (udev->manufacturer == NULL) {
2335192938Sthompsa				udev->manufacturer = strdup(kdp->vendorname,
2336192938Sthompsa				    M_USB);
2337192938Sthompsa			}
2338192938Sthompsa			if (udev->product == NULL &&
2339192938Sthompsa			    (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2340192938Sthompsa				udev->product = strdup(kdp->productname,
2341192938Sthompsa				    M_USB);
2342192938Sthompsa			}
2343184610Salfred		}
2344184610Salfred	}
2345184610Salfred#endif
2346192938Sthompsa	/* Provide default strings if none were found */
2347192938Sthompsa	if (udev->manufacturer == NULL) {
2348197559Sthompsa		snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
2349197559Sthompsa		udev->manufacturer = strdup(temp_ptr, M_USB);
2350184610Salfred	}
2351192938Sthompsa	if (udev->product == NULL) {
2352197559Sthompsa		snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
2353197559Sthompsa		udev->product = strdup(temp_ptr, M_USB);
2354184610Salfred	}
2355246616Shselasky
2356246616Shselasky	if (do_unlock)
2357246616Shselasky		usbd_enum_unlock(udev);
2358184610Salfred}
2359184610Salfred
2360188411Sthompsa/*
2361188411Sthompsa * Returns:
2362188411Sthompsa * See: USB_MODE_XXX
2363188411Sthompsa */
2364192499Sthompsaenum usb_hc_mode
2365194228Sthompsausbd_get_mode(struct usb_device *udev)
2366188411Sthompsa{
2367192499Sthompsa	return (udev->flags.usb_mode);
2368188411Sthompsa}
2369188411Sthompsa
2370188411Sthompsa/*
2371188411Sthompsa * Returns:
2372188411Sthompsa * See: USB_SPEED_XXX
2373188411Sthompsa */
2374192500Sthompsaenum usb_dev_speed
2375194228Sthompsausbd_get_speed(struct usb_device *udev)
2376184610Salfred{
2377184610Salfred	return (udev->speed);
2378184610Salfred}
2379184610Salfred
2380186730Salfreduint32_t
2381194228Sthompsausbd_get_isoc_fps(struct usb_device *udev)
2382186730Salfred{
2383186730Salfred	;				/* indent fix */
2384186730Salfred	switch (udev->speed) {
2385186730Salfred	case USB_SPEED_LOW:
2386186730Salfred	case USB_SPEED_FULL:
2387186730Salfred		return (1000);
2388186730Salfred	default:
2389186730Salfred		return (8000);
2390186730Salfred	}
2391186730Salfred}
2392186730Salfred
2393192984Sthompsastruct usb_device_descriptor *
2394194228Sthompsausbd_get_device_descriptor(struct usb_device *udev)
2395184610Salfred{
2396184610Salfred	if (udev == NULL)
2397184610Salfred		return (NULL);		/* be NULL safe */
2398184610Salfred	return (&udev->ddesc);
2399184610Salfred}
2400184610Salfred
2401192984Sthompsastruct usb_config_descriptor *
2402194228Sthompsausbd_get_config_descriptor(struct usb_device *udev)
2403184610Salfred{
2404184610Salfred	if (udev == NULL)
2405184610Salfred		return (NULL);		/* be NULL safe */
2406184610Salfred	return (udev->cdesc);
2407184610Salfred}
2408184610Salfred
2409184610Salfred/*------------------------------------------------------------------------*
2410194228Sthompsa *	usb_test_quirk - test a device for a given quirk
2411184610Salfred *
2412184610Salfred * Return values:
2413184610Salfred * 0: The USB device does not have the given quirk.
2414184610Salfred * Else: The USB device has the given quirk.
2415184610Salfred *------------------------------------------------------------------------*/
2416184610Salfreduint8_t
2417194228Sthompsausb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
2418184610Salfred{
2419184610Salfred	uint8_t found;
2420225469Shselasky	uint8_t x;
2421184610Salfred
2422225469Shselasky	if (quirk == UQ_NONE)
2423225469Shselasky		return (0);
2424225469Shselasky
2425225469Shselasky	/* search the automatic per device quirks first */
2426225469Shselasky
2427225469Shselasky	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
2428225469Shselasky		if (uaa->device->autoQuirk[x] == quirk)
2429225469Shselasky			return (1);
2430225469Shselasky	}
2431225469Shselasky
2432225469Shselasky	/* search global quirk table, if any */
2433225469Shselasky
2434194228Sthompsa	found = (usb_test_quirk_p) (&uaa->info, quirk);
2435225469Shselasky
2436184610Salfred	return (found);
2437184610Salfred}
2438184610Salfred
2439192984Sthompsastruct usb_interface_descriptor *
2440194228Sthompsausbd_get_interface_descriptor(struct usb_interface *iface)
2441184610Salfred{
2442184610Salfred	if (iface == NULL)
2443184610Salfred		return (NULL);		/* be NULL safe */
2444184610Salfred	return (iface->idesc);
2445184610Salfred}
2446184610Salfred
2447184610Salfreduint8_t
2448194228Sthompsausbd_get_interface_altindex(struct usb_interface *iface)
2449184610Salfred{
2450184610Salfred	return (iface->alt_index);
2451184610Salfred}
2452184610Salfred
2453184610Salfreduint8_t
2454194228Sthompsausbd_get_bus_index(struct usb_device *udev)
2455184610Salfred{
2456184610Salfred	return ((uint8_t)device_get_unit(udev->bus->bdev));
2457184610Salfred}
2458184610Salfred
2459184610Salfreduint8_t
2460194228Sthompsausbd_get_device_index(struct usb_device *udev)
2461184610Salfred{
2462184610Salfred	return (udev->device_index);
2463184610Salfred}
2464184610Salfred
2465214221Shselasky#if USB_HAVE_DEVCTL
2466184610Salfredstatic void
2467207020Sthompsausb_notify_addq(const char *type, struct usb_device *udev)
2468207020Sthompsa{
2469207020Sthompsa	struct usb_interface *iface;
2470207020Sthompsa	struct sbuf *sb;
2471207020Sthompsa	int i;
2472207020Sthompsa
2473207020Sthompsa	/* announce the device */
2474207020Sthompsa	sb = sbuf_new_auto();
2475207020Sthompsa	sbuf_printf(sb,
2476214221Shselasky#if USB_HAVE_UGEN
2477214221Shselasky	    "ugen=%s "
2478216072Shselasky	    "cdev=%s "
2479214221Shselasky#endif
2480207020Sthompsa	    "vendor=0x%04x "
2481207020Sthompsa	    "product=0x%04x "
2482207020Sthompsa	    "devclass=0x%02x "
2483207020Sthompsa	    "devsubclass=0x%02x "
2484207020Sthompsa	    "sernum=\"%s\" "
2485207020Sthompsa	    "release=0x%04x "
2486207027Sthompsa	    "mode=%s "
2487207020Sthompsa	    "port=%u "
2488214221Shselasky#if USB_HAVE_UGEN
2489214809Sn_hibma	    "parent=%s"
2490214221Shselasky#endif
2491214221Shselasky	    "",
2492214221Shselasky#if USB_HAVE_UGEN
2493207020Sthompsa	    udev->ugen_name,
2494216072Shselasky	    udev->ugen_name,
2495214221Shselasky#endif
2496207020Sthompsa	    UGETW(udev->ddesc.idVendor),
2497207020Sthompsa	    UGETW(udev->ddesc.idProduct),
2498207020Sthompsa	    udev->ddesc.bDeviceClass,
2499207020Sthompsa	    udev->ddesc.bDeviceSubClass,
2500212136Sthompsa	    usb_get_serial(udev),
2501207020Sthompsa	    UGETW(udev->ddesc.bcdDevice),
2502207027Sthompsa	    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2503214221Shselasky	    udev->port_no
2504214221Shselasky#if USB_HAVE_UGEN
2505214221Shselasky	    , udev->parent_hub != NULL ?
2506214221Shselasky		udev->parent_hub->ugen_name :
2507214221Shselasky		device_get_nameunit(device_get_parent(udev->bus->bdev))
2508214221Shselasky#endif
2509214221Shselasky	    );
2510207020Sthompsa	sbuf_finish(sb);
2511207020Sthompsa	devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
2512207020Sthompsa	sbuf_delete(sb);
2513207020Sthompsa
2514207020Sthompsa	/* announce each interface */
2515207020Sthompsa	for (i = 0; i < USB_IFACE_MAX; i++) {
2516207020Sthompsa		iface = usbd_get_iface(udev, i);
2517207020Sthompsa		if (iface == NULL)
2518207020Sthompsa			break;		/* end of interfaces */
2519207020Sthompsa		if (iface->idesc == NULL)
2520207020Sthompsa			continue;	/* no interface descriptor */
2521207020Sthompsa
2522207020Sthompsa		sb = sbuf_new_auto();
2523207020Sthompsa		sbuf_printf(sb,
2524214221Shselasky#if USB_HAVE_UGEN
2525214221Shselasky		    "ugen=%s "
2526216072Shselasky		    "cdev=%s "
2527214221Shselasky#endif
2528207020Sthompsa		    "vendor=0x%04x "
2529207020Sthompsa		    "product=0x%04x "
2530207020Sthompsa		    "devclass=0x%02x "
2531207020Sthompsa		    "devsubclass=0x%02x "
2532207020Sthompsa		    "sernum=\"%s\" "
2533207020Sthompsa		    "release=0x%04x "
2534207027Sthompsa		    "mode=%s "
2535207020Sthompsa		    "interface=%d "
2536207020Sthompsa		    "endpoints=%d "
2537207020Sthompsa		    "intclass=0x%02x "
2538207020Sthompsa		    "intsubclass=0x%02x "
2539214809Sn_hibma		    "intprotocol=0x%02x",
2540214221Shselasky#if USB_HAVE_UGEN
2541207020Sthompsa		    udev->ugen_name,
2542216072Shselasky		    udev->ugen_name,
2543214221Shselasky#endif
2544207020Sthompsa		    UGETW(udev->ddesc.idVendor),
2545207020Sthompsa		    UGETW(udev->ddesc.idProduct),
2546207020Sthompsa		    udev->ddesc.bDeviceClass,
2547207020Sthompsa		    udev->ddesc.bDeviceSubClass,
2548212136Sthompsa		    usb_get_serial(udev),
2549207020Sthompsa		    UGETW(udev->ddesc.bcdDevice),
2550207027Sthompsa		    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2551207020Sthompsa		    iface->idesc->bInterfaceNumber,
2552207020Sthompsa		    iface->idesc->bNumEndpoints,
2553207020Sthompsa		    iface->idesc->bInterfaceClass,
2554207020Sthompsa		    iface->idesc->bInterfaceSubClass,
2555207020Sthompsa		    iface->idesc->bInterfaceProtocol);
2556207020Sthompsa		sbuf_finish(sb);
2557207020Sthompsa		devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
2558207020Sthompsa		sbuf_delete(sb);
2559207020Sthompsa	}
2560207020Sthompsa}
2561214221Shselasky#endif
2562207020Sthompsa
2563214221Shselasky#if USB_HAVE_UGEN
2564184610Salfred/*------------------------------------------------------------------------*
2565194228Sthompsa *	usb_fifo_free_wrap
2566184610Salfred *
2567185087Salfred * This function will free the FIFOs.
2568185087Salfred *
2569190730Sthompsa * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2570190730Sthompsa * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2571190730Sthompsa * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2572190730Sthompsa * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2573190730Sthompsa * control endpoint FIFOs. If "iface_index" is not set to
2574190730Sthompsa * "USB_IFACE_INDEX_ANY" the flag has no effect.
2575184610Salfred *------------------------------------------------------------------------*/
2576184610Salfredstatic void
2577194228Sthompsausb_fifo_free_wrap(struct usb_device *udev,
2578185087Salfred    uint8_t iface_index, uint8_t flag)
2579184610Salfred{
2580192984Sthompsa	struct usb_fifo *f;
2581184610Salfred	uint16_t i;
2582184610Salfred
2583184610Salfred	/*
2584184610Salfred	 * Free any USB FIFOs on the given interface:
2585184610Salfred	 */
2586184610Salfred	for (i = 0; i != USB_FIFO_MAX; i++) {
2587184610Salfred		f = udev->fifo[i];
2588184610Salfred		if (f == NULL) {
2589184610Salfred			continue;
2590184610Salfred		}
2591185087Salfred		/* Check if the interface index matches */
2592185087Salfred		if (iface_index == f->iface_index) {
2593194228Sthompsa			if (f->methods != &usb_ugen_methods) {
2594185087Salfred				/*
2595185087Salfred				 * Don't free any non-generic FIFOs in
2596185087Salfred				 * this case.
2597185087Salfred				 */
2598185087Salfred				continue;
2599185087Salfred			}
2600185290Salfred			if ((f->dev_ep_index == 0) &&
2601185290Salfred			    (f->fs_xfer == NULL)) {
2602185290Salfred				/* no need to free this FIFO */
2603185087Salfred				continue;
2604185087Salfred			}
2605185087Salfred		} else if (iface_index == USB_IFACE_INDEX_ANY) {
2606194228Sthompsa			if ((f->methods == &usb_ugen_methods) &&
2607190730Sthompsa			    (f->dev_ep_index == 0) &&
2608190730Sthompsa			    (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
2609185290Salfred			    (f->fs_xfer == NULL)) {
2610185290Salfred				/* no need to free this FIFO */
2611185087Salfred				continue;
2612185087Salfred			}
2613185087Salfred		} else {
2614185290Salfred			/* no need to free this FIFO */
2615184610Salfred			continue;
2616184610Salfred		}
2617185290Salfred		/* free this FIFO */
2618194228Sthompsa		usb_fifo_free(f);
2619184610Salfred	}
2620184610Salfred}
2621189599Sthompsa#endif
2622186730Salfred
2623186730Salfred/*------------------------------------------------------------------------*
2624194228Sthompsa *	usb_peer_can_wakeup
2625186730Salfred *
2626186730Salfred * Return values:
2627186730Salfred * 0: Peer cannot do resume signalling.
2628186730Salfred * Else: Peer can do resume signalling.
2629186730Salfred *------------------------------------------------------------------------*/
2630186730Salfreduint8_t
2631194228Sthompsausb_peer_can_wakeup(struct usb_device *udev)
2632186730Salfred{
2633192984Sthompsa	const struct usb_config_descriptor *cdp;
2634186730Salfred
2635186730Salfred	cdp = udev->cdesc;
2636192499Sthompsa	if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
2637186730Salfred		return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2638186730Salfred	}
2639186730Salfred	return (0);			/* not supported */
2640186730Salfred}
2641191494Sthompsa
2642191494Sthompsavoid
2643194228Sthompsausb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2644191494Sthompsa{
2645191494Sthompsa
2646191494Sthompsa	KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2647191494Sthompsa
2648191494Sthompsa	DPRINTF("udev %p state %s -> %s\n", udev,
2649194228Sthompsa	    usb_statestr(udev->state), usb_statestr(state));
2650191494Sthompsa	udev->state = state;
2651213435Shselasky
2652213435Shselasky	if (udev->bus->methods->device_state_change != NULL)
2653213435Shselasky		(udev->bus->methods->device_state_change) (udev);
2654191494Sthompsa}
2655191494Sthompsa
2656213435Shselaskyenum usb_dev_state
2657213435Shselaskyusb_get_device_state(struct usb_device *udev)
2658213435Shselasky{
2659213435Shselasky	if (udev == NULL)
2660213435Shselasky		return (USB_STATE_DETACHED);
2661213435Shselasky	return (udev->state);
2662213435Shselasky}
2663213435Shselasky
2664191824Sthompsauint8_t
2665194228Sthompsausbd_device_attached(struct usb_device *udev)
2666191494Sthompsa{
2667191494Sthompsa	return (udev->state > USB_STATE_DETACHED);
2668191494Sthompsa}
2669196498Salfred
2670246616Shselasky/*
2671246616Shselasky * The following function locks enumerating the given USB device. If
2672246616Shselasky * the lock is already grabbed this function returns zero. Else a
2673246616Shselasky * non-zero value is returned.
2674246616Shselasky */
2675246616Shselaskyuint8_t
2676196498Salfredusbd_enum_lock(struct usb_device *udev)
2677196498Salfred{
2678246616Shselasky	if (sx_xlocked(&udev->enum_sx))
2679246616Shselasky		return (0);
2680246616Shselasky
2681207079Sthompsa	sx_xlock(&udev->enum_sx);
2682208008Sthompsa	sx_xlock(&udev->sr_sx);
2683196498Salfred	/*
2684196498Salfred	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2685196498Salfred	 * are locked before locking Giant. Else the lock can be
2686196498Salfred	 * locked multiple times.
2687196498Salfred	 */
2688196498Salfred	mtx_lock(&Giant);
2689246616Shselasky	return (1);
2690196498Salfred}
2691196498Salfred
2692196498Salfred/* The following function unlocks enumerating the given USB device. */
2693196498Salfred
2694196498Salfredvoid
2695196498Salfredusbd_enum_unlock(struct usb_device *udev)
2696196498Salfred{
2697196498Salfred	mtx_unlock(&Giant);
2698207079Sthompsa	sx_xunlock(&udev->enum_sx);
2699208008Sthompsa	sx_xunlock(&udev->sr_sx);
2700196498Salfred}
2701196498Salfred
2702208008Sthompsa/* The following function locks suspend and resume. */
2703208008Sthompsa
2704208008Sthompsavoid
2705208008Sthompsausbd_sr_lock(struct usb_device *udev)
2706208008Sthompsa{
2707208008Sthompsa	sx_xlock(&udev->sr_sx);
2708208008Sthompsa	/*
2709208008Sthompsa	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2710208008Sthompsa	 * are locked before locking Giant. Else the lock can be
2711208008Sthompsa	 * locked multiple times.
2712208008Sthompsa	 */
2713208008Sthompsa	mtx_lock(&Giant);
2714208008Sthompsa}
2715208008Sthompsa
2716208008Sthompsa/* The following function unlocks suspend and resume. */
2717208008Sthompsa
2718208008Sthompsavoid
2719208008Sthompsausbd_sr_unlock(struct usb_device *udev)
2720208008Sthompsa{
2721208008Sthompsa	mtx_unlock(&Giant);
2722208008Sthompsa	sx_xunlock(&udev->sr_sx);
2723208008Sthompsa}
2724208008Sthompsa
2725196498Salfred/*
2726196498Salfred * The following function checks the enumerating lock for the given
2727196498Salfred * USB device.
2728196498Salfred */
2729196498Salfred
2730196498Salfreduint8_t
2731196498Salfredusbd_enum_is_locked(struct usb_device *udev)
2732196498Salfred{
2733207079Sthompsa	return (sx_xlocked(&udev->enum_sx));
2734196498Salfred}
2735214429Shselasky
2736214429Shselasky/*
2737214429Shselasky * The following function is used to set the per-interface specific
2738214429Shselasky * plug and play information. The string referred to by the pnpinfo
2739214429Shselasky * argument can safely be freed after calling this function. The
2740214429Shselasky * pnpinfo of an interface will be reset at device detach or when
2741214429Shselasky * passing a NULL argument to this function. This function
2742214429Shselasky * returns zero on success, else a USB_ERR_XXX failure code.
2743214429Shselasky */
2744214429Shselasky
2745214429Shselaskyusb_error_t
2746214429Shselaskyusbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
2747214429Shselasky{
2748214429Shselasky	struct usb_interface *iface;
2749214429Shselasky
2750214429Shselasky	iface = usbd_get_iface(udev, iface_index);
2751214429Shselasky	if (iface == NULL)
2752214429Shselasky		return (USB_ERR_INVAL);
2753214429Shselasky
2754214429Shselasky	if (iface->pnpinfo != NULL) {
2755214429Shselasky		free(iface->pnpinfo, M_USBDEV);
2756214429Shselasky		iface->pnpinfo = NULL;
2757214429Shselasky	}
2758214429Shselasky
2759214429Shselasky	if (pnpinfo == NULL || pnpinfo[0] == 0)
2760214429Shselasky		return (0);		/* success */
2761214429Shselasky
2762214429Shselasky	iface->pnpinfo = strdup(pnpinfo, M_USBDEV);
2763214429Shselasky	if (iface->pnpinfo == NULL)
2764214429Shselasky		return (USB_ERR_NOMEM);
2765214429Shselasky
2766214429Shselasky	return (0);			/* success */
2767214429Shselasky}
2768214429Shselasky
2769225350Shselaskyusb_error_t
2770225350Shselaskyusbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk)
2771225350Shselasky{
2772225350Shselasky	uint8_t x;
2773225350Shselasky
2774225350Shselasky	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
2775225469Shselasky		if (udev->autoQuirk[x] == 0 ||
2776225469Shselasky		    udev->autoQuirk[x] == quirk) {
2777225350Shselasky			udev->autoQuirk[x] = quirk;
2778225350Shselasky			return (0);	/* success */
2779225350Shselasky		}
2780225350Shselasky	}
2781225350Shselasky	return (USB_ERR_NOMEM);
2782225350Shselasky}
2783239214Shselasky
2784239214Shselasky/*
2785239214Shselasky * The following function is used to select the endpoint mode. It
2786239214Shselasky * should not be called outside enumeration context.
2787239214Shselasky */
2788239214Shselasky
2789239214Shselaskyusb_error_t
2790239214Shselaskyusbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
2791239214Shselasky    uint8_t ep_mode)
2792239214Shselasky{
2793239214Shselasky	usb_error_t error;
2794239240Shselasky	uint8_t do_unlock;
2795239214Shselasky
2796246616Shselasky	/* Prevent re-enumeration */
2797246616Shselasky	do_unlock = usbd_enum_lock(udev);
2798239214Shselasky
2799239214Shselasky	if (udev->bus->methods->set_endpoint_mode != NULL) {
2800239214Shselasky		error = (udev->bus->methods->set_endpoint_mode) (
2801239214Shselasky		    udev, ep, ep_mode);
2802239214Shselasky	} else if (ep_mode != USB_EP_MODE_DEFAULT) {
2803239214Shselasky		error = USB_ERR_INVAL;
2804239214Shselasky	} else {
2805239214Shselasky		error = 0;
2806239214Shselasky	}
2807239214Shselasky
2808239214Shselasky	/* only set new mode regardless of error */
2809239214Shselasky	ep->ep_mode = ep_mode;
2810239214Shselasky
2811239240Shselasky	if (do_unlock)
2812239240Shselasky		usbd_enum_unlock(udev);
2813239214Shselasky	return (error);
2814239214Shselasky}
2815239214Shselasky
2816239214Shselaskyuint8_t
2817239214Shselaskyusbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep)
2818239214Shselasky{
2819239214Shselasky	return (ep->ep_mode);
2820239214Shselasky}
2821