Merge branch 'vendor/LIBEDIT'
[dragonfly.git] / sys / bus / u4b / usb_util.c
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/stdint.h>
28 #include <sys/param.h>
29 #include <sys/queue.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/module.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/condvar.h>
38 #include <sys/sysctl.h>
39 #include <sys/unistd.h>
40 #include <sys/callout.h>
41 #include <sys/malloc.h>
42 #include <sys/priv.h>
43
44 #include <bus/u4b/usb.h>
45 #include <bus/u4b/usbdi.h>
46 #include <bus/u4b/usbdi_util.h>
47
48 #include <bus/u4b/usb_core.h>
49 #include <bus/u4b/usb_util.h>
50 #include <bus/u4b/usb_process.h>
51 #include <bus/u4b/usb_device.h>
52 #include <bus/u4b/usb_request.h>
53 #include <bus/u4b/usb_busdma.h>
54
55 #include <bus/u4b/usb_controller.h>
56 #include <bus/u4b/usb_bus.h>
57
58 /*------------------------------------------------------------------------*
59  *      device_set_usb_desc
60  *
61  * This function can be called at probe or attach to set the USB
62  * device supplied textual description for the given device.
63  *------------------------------------------------------------------------*/
64 void
65 device_set_usb_desc(device_t dev)
66 {
67         struct usb_attach_arg *uaa;
68         struct usb_device *udev;
69         struct usb_interface *iface;
70         char *temp_p;
71         usb_error_t err;
72         uint8_t do_unlock;
73
74         if (dev == NULL) {
75                 /* should not happen */
76                 return;
77         }
78         uaa = device_get_ivars(dev);
79         if (uaa == NULL) {
80                 /* can happen if called at the wrong time */
81                 return;
82         }
83         udev = uaa->device;
84         iface = uaa->iface;
85
86         if ((iface == NULL) ||
87             (iface->idesc == NULL) ||
88             (iface->idesc->iInterface == 0)) {
89                 err = USB_ERR_INVAL;
90         } else {
91                 err = 0;
92         }
93
94         /* Protect scratch area */
95         do_unlock = usbd_enum_lock(udev);
96
97         temp_p = (char *)udev->scratch.data;
98
99         if (err == 0) {
100                 /* try to get the interface string ! */
101                 err = usbd_req_get_string_any(udev, NULL, temp_p,
102                     sizeof(udev->scratch.data),
103                     iface->idesc->iInterface);
104         }
105         if (err != 0) {
106                 /* use default description */
107                 usb_devinfo(udev, temp_p,
108                     sizeof(udev->scratch.data));
109         }
110
111         if (do_unlock)
112                 usbd_enum_unlock(udev);
113
114         device_set_desc_copy(dev, temp_p);
115         device_printf(dev, "<%s> on %s\n", temp_p,
116             device_get_nameunit(udev->bus->bdev));
117 }
118
119 /*------------------------------------------------------------------------*
120  *       usb_pause_mtx - factored out code
121  *
122  * This function will delay the code by the passed number of system
123  * ticks. The passed mutex "mtx" will be dropped while waiting, if
124  * "mtx" is different from NULL.
125  *------------------------------------------------------------------------*/
126 void
127 usb_pause_mtx(struct lock *lock, int timo)
128 {
129         /*
130          * Add one tick to the timeout so that we don't return too
131          * early! Note that pause() will assert that the passed
132          * timeout is positive and non-zero!
133          */
134         if (lock != NULL) {
135                 lksleep(&usb_pause_mtx, lock, 0, "USBSLP", timo + 1);
136         } else {
137                 KKASSERT(timo + 1 > 0);
138                 tsleep(&usb_pause_mtx, 0, "USBSLP", timo + 1);
139         }
140 }
141
142
143 /*------------------------------------------------------------------------*
144  *       usb_pause_ls - factored out code
145  *
146  * This function will delay the code by the passed number of system
147  * ticks. The passed lock "lock", will be dropped while waiting, 
148  * if * "lock" is different from NULL, "slz" will be left.
149  *
150  * This function is here because sometimes we have two subsystems with
151  * different locking primitives (lockmgr locks, lwkt serializers) involved.
152  *------------------------------------------------------------------------*/
153 void
154 usb_pause_ls(struct lock *lock, struct lwkt_serialize *slz, int _ticks)
155 {
156         int locked = 0;
157
158         locked = (lock != NULL) && lockowned(lock);
159
160         tsleep_interlock(&usb_pause_ls, 0);
161
162         lwkt_serialize_exit(slz);
163
164         if(locked)
165                 lockmgr(lock, LK_RELEASE);
166
167         tsleep(&usb_pause_ls, PINTERLOCKED, "USBSL2", _ticks);
168
169         if(locked)
170                 lockmgr(lock, LK_EXCLUSIVE);
171
172         lwkt_serialize_enter(slz);
173 }
174
175 /*------------------------------------------------------------------------*
176  *      usb_printbcd
177  *
178  * This function will print the version number "bcd" to the string
179  * pointed to by "p" having a maximum length of "p_len" bytes
180  * including the terminating zero.
181  *------------------------------------------------------------------------*/
182 void
183 usb_printbcd(char *p, uint16_t p_len, uint16_t bcd)
184 {
185         if (ksnprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) {
186                 /* ignore any errors */
187         }
188 }
189
190 /*------------------------------------------------------------------------*
191  *      usb_trim_spaces
192  *
193  * This function removes spaces at the beginning and the end of the string
194  * pointed to by the "p" argument.
195  *------------------------------------------------------------------------*/
196 void
197 usb_trim_spaces(char *p)
198 {
199         char *q;
200         char *e;
201
202         if (p == NULL)
203                 return;
204         q = e = p;
205         while (*q == ' ')               /* skip leading spaces */
206                 q++;
207         while ((*p = *q++))             /* copy string */
208                 if (*p++ != ' ')        /* remember last non-space */
209                         e = p;
210         *e = 0;                         /* kill trailing spaces */
211 }
212
213 /*------------------------------------------------------------------------*
214  *      usb_make_str_desc - convert an ASCII string into a UNICODE string
215  *------------------------------------------------------------------------*/
216 uint8_t
217 usb_make_str_desc(void *ptr, uint16_t max_len, const char *s)
218 {
219         struct usb_string_descriptor *p = ptr;
220         uint8_t totlen;
221         int j;
222
223         if (max_len < 2) {
224                 /* invalid length */
225                 return (0);
226         }
227         max_len = ((max_len / 2) - 1);
228
229         j = strlen(s);
230
231         if (j < 0) {
232                 j = 0;
233         }
234         if (j > 126) {
235                 j = 126;
236         }
237         if (max_len > j) {
238                 max_len = j;
239         }
240         totlen = (max_len + 1) * 2;
241
242         p->bLength = totlen;
243         p->bDescriptorType = UDESC_STRING;
244
245         while (max_len--) {
246                 USETW2(p->bString[max_len], 0, s[max_len]);
247         }
248         return (totlen);
249 }
250
251 void 
252 usb_callout_timeout_wrapper(void *arg)
253 {
254         struct usb_callout *uco = (struct usb_callout *)arg;
255
256         KKASSERT(uco != NULL);
257
258         /*
259          * Simulate FreeBSD's callout behaviour which allows
260          * a lock to be acquired before the function is called
261          */
262
263         lockmgr(uco->uco_lock, LK_EXCLUSIVE);
264         uco->uco_func(uco->uco_arg);
265         lockmgr(uco->uco_lock, LK_RELEASE);
266         /* XXX Have to introduce flags and release lock? */
267 }
268
269 void 
270 usb_callout_init_mtx_dfly(struct usb_callout *uco, struct lock *lock,
271     int flags)
272 {
273         callout_init(&uco->co);
274         uco->uco_lock = lock;
275         uco->uco_flags = flags;    
276 }
277
278 void
279 usb_callout_reset_dfly(struct usb_callout *uco, int ticks, timeout_t *func,
280     void *arg)
281 {
282         KKASSERT(uco != NULL);
283         uco->uco_func = func;
284         uco->uco_arg = arg;
285
286         callout_reset(&uco->co, ticks, &usb_callout_timeout_wrapper, uco);
287 }