1/* $FreeBSD$ */
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/param.h>
28#include <sys/queue.h>
29#include <sys/types.h>
30#include <sys/systm.h>
31#include <sys/kernel.h>
32#include <sys/bus.h>
33#include <sys/module.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/condvar.h>
37#include <sys/sysctl.h>
38#include <sys/callout.h>
39#include <sys/malloc.h>
40#include <sys/priv.h>
41
42#include <dev/usb/usb.h>
43#include <dev/usb/usbdi.h>
44
45#include <dev/usb/usb_device.h>
46
47/*------------------------------------------------------------------------*
48 *	 usb_pause_mtx - factored out code
49 *
50 * This function will delay the code by the passed number of system
51 * ticks. The passed mutex "mtx" will be dropped while waiting, if
52 * "mtx" is different from NULL.
53 *------------------------------------------------------------------------*/
54void
55usb_pause_mtx(struct mtx *mtx, int timo)
56{
57	if (mtx != NULL)
58		mtx_unlock(mtx);
59
60	/*
61	 * Add one tick to the timeout so that we don't return too
62	 * early! Note that pause() will assert that the passed
63	 * timeout is positive and non-zero!
64	 */
65	pause("USBWAIT", timo + 1);
66
67	if (mtx != NULL)
68		mtx_lock(mtx);
69}
70
71/*------------------------------------------------------------------------*
72 *	usb_printbcd
73 *
74 * This function will print the version number "bcd" to the string
75 * pointed to by "p" having a maximum length of "p_len" bytes
76 * including the terminating zero.
77 *------------------------------------------------------------------------*/
78void
79usb_printbcd(char *p, uint16_t p_len, uint16_t bcd)
80{
81	if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) {
82		/* ignore any errors */
83	}
84}
85
86/*------------------------------------------------------------------------*
87 *	usb_trim_spaces
88 *
89 * This function removes spaces at the beginning and the end of the string
90 * pointed to by the "p" argument.
91 *------------------------------------------------------------------------*/
92void
93usb_trim_spaces(char *p)
94{
95	char *q;
96	char *e;
97
98	if (p == NULL)
99		return;
100	q = e = p;
101	while (*q == ' ')		/* skip leading spaces */
102		q++;
103	while ((*p = *q++))		/* copy string */
104		if (*p++ != ' ')	/* remember last non-space */
105			e = p;
106	*e = 0;				/* kill trailing spaces */
107}
108