3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifdef USB_GLOBAL_INCLUDE_FILE
30 #include USB_GLOBAL_INCLUDE_FILE
32 #include <sys/stdint.h>
33 #include <sys/stddef.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdi_util.h>
55 #include <dev/usb/usb_core.h>
56 #include <dev/usb/usb_util.h>
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/usb_device.h>
59 #include <dev/usb/usb_request.h>
60 #include <dev/usb/usb_busdma.h>
62 #include <dev/usb/usb_controller.h>
63 #include <dev/usb/usb_bus.h>
64 #endif /* USB_GLOBAL_INCLUDE_FILE */
66 /*------------------------------------------------------------------------*
69 * This function can be called at probe or attach to set the USB
70 * device supplied textual description for the given device.
71 *------------------------------------------------------------------------*/
73 device_set_usb_desc(device_t dev)
75 struct usb_attach_arg *uaa;
76 struct usb_device *udev;
77 struct usb_interface *iface;
83 /* should not happen */
86 uaa = device_get_ivars(dev);
88 /* can happen if called at the wrong time */
94 if ((iface == NULL) ||
95 (iface->idesc == NULL) ||
96 (iface->idesc->iInterface == 0)) {
102 /* Protect scratch area */
103 do_unlock = usbd_ctrl_lock(udev);
105 temp_p = (char *)udev->scratch.data;
108 /* try to get the interface string ! */
109 err = usbd_req_get_string_any(udev, NULL, temp_p,
110 sizeof(udev->scratch.data),
111 iface->idesc->iInterface);
114 /* use default description */
115 usb_devinfo(udev, temp_p,
116 sizeof(udev->scratch.data));
120 usbd_ctrl_unlock(udev);
122 device_set_desc_copy(dev, temp_p);
123 device_printf(dev, "<%s> on %s\n", temp_p,
124 device_get_nameunit(udev->bus->bdev));
127 /*------------------------------------------------------------------------*
128 * usb_pause_mtx - factored out code
130 * This function will delay the code by the passed number of system
131 * ticks. The passed mutex "mtx" will be dropped while waiting, if
132 * "mtx" is different from NULL.
133 *------------------------------------------------------------------------*/
135 usb_pause_mtx(struct mtx *mtx, int timo)
141 * Add one tick to the timeout so that we don't return too
142 * early! Note that pause() will assert that the passed
143 * timeout is positive and non-zero!
145 pause("USBWAIT", timo + 1);
151 /*------------------------------------------------------------------------*
154 * This function will print the version number "bcd" to the string
155 * pointed to by "p" having a maximum length of "p_len" bytes
156 * including the terminating zero.
157 *------------------------------------------------------------------------*/
159 usb_printbcd(char *p, uint16_t p_len, uint16_t bcd)
161 if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) {
162 /* ignore any errors */
166 /*------------------------------------------------------------------------*
169 * This function removes spaces at the beginning and the end of the string
170 * pointed to by the "p" argument.
171 *------------------------------------------------------------------------*/
173 usb_trim_spaces(char *p)
181 while (*q == ' ') /* skip leading spaces */
183 while ((*p = *q++)) /* copy string */
184 if (*p++ != ' ') /* remember last non-space */
186 *e = 0; /* kill trailing spaces */
189 /*------------------------------------------------------------------------*
190 * usb_make_str_desc - convert an ASCII string into a UNICODE string
191 *------------------------------------------------------------------------*/
193 usb_make_str_desc(void *ptr, uint16_t max_len, const char *s)
195 struct usb_string_descriptor *p = ptr;
203 max_len = ((max_len / 2) - 1);
216 totlen = (max_len + 1) * 2;
219 p->bDescriptorType = UDESC_STRING;
222 USETW2(p->bString[max_len], 0, s[max_len]);