Synchronize the USB, CAM, and TASKQUEUE subsystems with FreeBSD RELENG_4.
[dragonfly.git] / sys / bus / usb / usb_port.h
1 /*      $NetBSD: usb_port.h,v 1.15 1999/11/16 12:04:28 augustss Exp $   */
2 /*      $FreeBSD: src/sys/dev/usb/usb_port.h,v 1.25.2.7 2002/08/12 14:19:49 joe Exp $   */
3 /*      $DragonFly: src/sys/bus/usb/usb_port.h,v 1.4 2003/06/27 01:53:24 dillon Exp $   */
4
5 /*
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41
42
43 /* 
44  * Macro's to cope with the differences between operating systems.
45  */
46
47 #if defined(__NetBSD__)
48 /*
49  * NetBSD
50  */
51
52 #include "opt_usbverbose.h"
53
54 #define Static static
55
56 typedef struct proc *usb_proc_ptr;
57
58 typedef struct device *device_ptr_t;
59 #define USBBASEDEVICE struct device
60 #define USBDEV(bdev) (&(bdev))
61 #define USBDEVNAME(bdev) ((bdev).dv_xname)
62 #define USBDEVPTRNAME(bdevptr) ((bdevptr)->dv_xname)
63 #define USBDEVUNIT(bdev) ((bdev).dv_unit)
64
65 #define DECLARE_USB_DMA_T \
66         struct usb_dma_block; \
67         typedef struct { \
68                 struct usb_dma_block *block; \
69                 u_int offs; \
70         } usb_dma_t
71
72 #define usb_timeout(f, d, t, h) timeout((f), (d), (t))
73 #define usb_untimeout(f, d, h) untimeout((f), (d))
74
75 #define logprintf printf
76
77 #define USB_DECLARE_DRIVER(dname)  \
78 int __CONCAT(dname,_match)(struct device *, struct cfdata *, void *); \
79 void __CONCAT(dname,_attach)(struct device *, struct device *, void *); \
80 int __CONCAT(dname,_detach)(struct device *, int); \
81 int __CONCAT(dname,_activate)(struct device *, enum devact); \
82 \
83 extern struct cfdriver __CONCAT(dname,_cd); \
84 \
85 struct cfattach __CONCAT(dname,_ca) = { \
86         sizeof(struct __CONCAT(dname,_softc)), \
87         __CONCAT(dname,_match), \
88         __CONCAT(dname,_attach), \
89         __CONCAT(dname,_detach), \
90         __CONCAT(dname,_activate), \
91 }
92
93 #define USB_MATCH(dname) \
94 int \
95 __CONCAT(dname,_match)(parent, match, aux) \
96         struct device *parent; \
97         struct cfdata *match; \
98         void *aux;
99
100 #define USB_MATCH_START(dname, uaa) \
101         struct usb_attach_arg *uaa = aux
102
103 #define USB_MATCH_SETUP         /* nop */
104
105 #define USB_ATTACH(dname) \
106 void \
107 __CONCAT(dname,_attach)(parent, self, aux) \
108         struct device *parent; \
109         struct device *self; \
110         void *aux;
111
112 #define USB_ATTACH_START(dname, sc, uaa) \
113         struct __CONCAT(dname,_softc) *sc = \
114                 (struct __CONCAT(dname,_softc) *)self; \
115         struct usb_attach_arg *uaa = aux
116
117 /* Returns from attach */
118 #define USB_ATTACH_ERROR_RETURN return
119 #define USB_ATTACH_SUCCESS_RETURN       return
120
121 #define USB_ATTACH_SETUP printf("\n")
122
123 #define USB_DETACH(dname) \
124 int \
125 __CONCAT(dname,_detach)(self, flags) \
126         struct device *self; \
127         int flags;
128
129 #define USB_DETACH_START(dname, sc) \
130         struct __CONCAT(dname,_softc) *sc = \
131                 (struct __CONCAT(dname,_softc) *)self
132
133 #define USB_GET_SC_OPEN(dname, unit, sc) \
134         if (unit >= __CONCAT(dname,_cd).cd_ndevs) \
135                 return (ENXIO); \
136         sc = __CONCAT(dname,_cd).cd_devs[unit]; \
137         if (!sc) \
138                 return (ENXIO)
139
140 #define USB_GET_SC(dname, unit, sc) \
141         sc = __CONCAT(dname,_cd).cd_devs[unit]
142
143 #define USB_DO_ATTACH(dev, bdev, parent, args, print, sub) \
144         (config_found_sm(parent, args, print, sub))
145
146 #elif defined(__OpenBSD__)
147 /*
148  * OpenBSD
149  */
150 #define Static static
151
152 typedef struct proc *usb_proc_ptr;
153
154 #define memcpy(d, s, l)         bcopy((s),(d),(l))
155 #define memset(d, v, l)         bzero((d),(l))
156 #define bswap32(x)              swap32(x)
157 #define kthread_create1         kthread_create
158 #define kthread_create          kthread_create_deferred
159
160 #define usbpoll                 usbselect
161 #define uhidpoll                uhidselect
162 #define ugenpoll                ugenselect
163
164 #define PWR_RESUME 0
165 #define PWR_SUSPEND 1
166
167 typedef struct device device_ptr_t;
168 #define USBBASEDEVICE struct device
169 #define USBDEV(bdev) (&(bdev))
170 #define USBDEVNAME(bdev) ((bdev).dv_xname)
171 #define USBDEVPTRNAME(bdevptr) ((bdevptr)->dv_xname)
172 #define USBDEVUNIT(bdev) ((bdev).dv_unit)
173
174 #define DECLARE_USB_DMA_T \
175         struct usb_dma_block; \
176         typedef struct { \
177                 struct usb_dma_block *block; \
178                 u_int offs; \
179         } usb_dma_t
180
181 #define usb_timeout(f, d, t, h) timeout((f), (d), (t))
182 #define usb_untimeout(f, d, h) untimeout((f), (d))
183
184 #define USB_DECLARE_DRIVER(dname)  \
185 int __CONCAT(dname,_match)(struct device *, void *, void *); \
186 void __CONCAT(dname,_attach)(struct device *, struct device *, void *); \
187 int __CONCAT(dname,_detach)(struct device *, int); \
188 int __CONCAT(dname,_activate)(struct device *, enum devact); \
189 \
190 struct cfdriver __CONCAT(dname,_cd) = { \
191         NULL, #dname, DV_DULL \
192 }; \
193 \
194 struct cfattach __CONCAT(dname,_ca) = { \
195         sizeof(struct __CONCAT(dname,_softc)), \
196         __CONCAT(dname,_match), \
197         __CONCAT(dname,_attach), \
198         __CONCAT(dname,_detach), \
199         __CONCAT(dname,_activate), \
200 }
201
202 #define USB_MATCH(dname) \
203 int \
204 __CONCAT(dname,_match)(parent, match, aux) \
205         struct device *parent; \
206         void *match; \
207         void *aux;
208
209 #define USB_MATCH_START(dname, uaa) \
210         struct usb_attach_arg *uaa = aux
211
212 #define USB_MATCH_SETUP         /* nop */
213
214 #define USB_ATTACH(dname) \
215 void \
216 __CONCAT(dname,_attach)(parent, self, aux) \
217         struct device *parent; \
218         struct device *self; \
219         void *aux;
220
221 #define USB_ATTACH_START(dname, sc, uaa) \
222         struct __CONCAT(dname,_softc) *sc = \
223                 (struct __CONCAT(dname,_softc) *)self; \
224         struct usb_attach_arg *uaa = aux
225
226 /* Returns from attach */
227 #define USB_ATTACH_ERROR_RETURN return
228 #define USB_ATTACH_SUCCESS_RETURN       return
229
230 #define USB_ATTACH_SETUP printf("\n")
231
232 #define USB_DETACH(dname) \
233 int \
234 __CONCAT(dname,_detach)(self, flags) \
235         struct device *self; \
236         int flags;
237
238 #define USB_DETACH_START(dname, sc) \
239         struct __CONCAT(dname,_softc) *sc = \
240                 (struct __CONCAT(dname,_softc) *)self
241
242 #define USB_GET_SC_OPEN(dname, unit, sc) \
243         if (unit >= __CONCAT(dname,_cd).cd_ndevs) \
244                 return (ENXIO); \
245         sc = __CONCAT(dname,_cd).cd_devs[unit]; \
246         if (!sc) \
247                 return (ENXIO)
248
249 #define USB_GET_SC(dname, unit, sc) \
250         sc = __CONCAT(dname,_cd).cd_devs[unit]
251
252 #define USB_DO_ATTACH(dev, bdev, parent, args, print, sub) \
253         (config_found_sm(parent, args, print, sub))
254
255 #elif defined(__FreeBSD__)
256 /*
257  * FreeBSD
258  */
259
260 #include "opt_usb.h"
261
262 #define USBVERBOSE
263
264 #define Static static
265
266 #define device_ptr_t device_t
267 #define USBBASEDEVICE device_t
268 #define USBDEV(bdev) (bdev)
269 #define USBDEVNAME(bdev) device_get_nameunit(bdev)
270 #define USBDEVPTRNAME(bdev) device_get_nameunit(bdev)
271 #define USBDEVUNIT(bdev) device_get_unit(bdev)
272
273 #define DECLARE_USB_DMA_T typedef char * usb_dma_t
274
275 typedef struct thread *usb_proc_ptr;
276
277 /* XXX Change this when FreeBSD has memset */
278 #define memcpy(d, s, l)         bcopy((s),(d),(l))
279 #define memset(d, v, l)         bzero((d),(l))
280 #define bswap32(x)              swap32(x)
281 #define kthread_create1(function, sc, priv, string, name)
282 #define kthread_create(create_function, sc)
283 #define kthread_exit()
284
285 #define usb_timeout(f, d, t, h) ((h) = timeout((f), (d), (t)))
286 #define usb_untimeout(f, d, h) untimeout((f), (d), (h))
287
288 #define clalloc(p, s, x) (clist_alloc_cblocks((p), (s), (s)), 0)
289 #define clfree(p) clist_free_cblocks((p))
290
291 #define PWR_RESUME 0
292 #define PWR_SUSPEND 1
293
294 #define USB_DECLARE_DRIVER_INIT(dname, init...) \
295 Static device_probe_t __CONCAT(dname,_match); \
296 Static device_attach_t __CONCAT(dname,_attach); \
297 Static device_detach_t __CONCAT(dname,_detach); \
298 \
299 Static devclass_t __CONCAT(dname,_devclass); \
300 \
301 Static device_method_t __CONCAT(dname,_methods)[] = { \
302         DEVMETHOD(device_probe, __CONCAT(dname,_match)), \
303         DEVMETHOD(device_attach, __CONCAT(dname,_attach)), \
304         DEVMETHOD(device_detach, __CONCAT(dname,_detach)), \
305         init, \
306         {0,0} \
307 }; \
308 \
309 Static driver_t __CONCAT(dname,_driver) = { \
310         #dname, \
311         __CONCAT(dname,_methods), \
312         sizeof(struct __CONCAT(dname,_softc)) \
313 }
314 #define METHODS_NONE                    {0,0}
315 #define USB_DECLARE_DRIVER(dname)       USB_DECLARE_DRIVER_INIT(dname, METHODS_NONE)
316
317 #define USB_MATCH(dname) \
318 Static int \
319 __CONCAT(dname,_match)(device_t self)
320
321 #define USB_MATCH_START(dname, uaa) \
322         struct usb_attach_arg *uaa = device_get_ivars(self)
323
324 #define USB_MATCH_SETUP \
325         sc->sc_dev = self
326
327 #define USB_ATTACH(dname) \
328 Static int \
329 __CONCAT(dname,_attach)(device_t self)
330
331 #define USB_ATTACH_START(dname, sc, uaa) \
332         struct __CONCAT(dname,_softc) *sc = device_get_softc(self); \
333         struct usb_attach_arg *uaa = device_get_ivars(self)
334
335 /* Returns from attach */
336 #define USB_ATTACH_ERROR_RETURN return ENXIO
337 #define USB_ATTACH_SUCCESS_RETURN       return 0
338
339 #define USB_ATTACH_SETUP \
340         sc->sc_dev = self; \
341         device_set_desc_copy(self, devinfo)
342
343 #define USB_DETACH(dname) \
344 Static int \
345 __CONCAT(dname,_detach)(device_t self)
346
347 #define USB_DETACH_START(dname, sc) \
348         struct __CONCAT(dname,_softc) *sc = device_get_softc(self)
349
350 #define USB_GET_SC_OPEN(dname, unit, sc) \
351         sc = devclass_get_softc(__CONCAT(dname,_devclass), unit); \
352         if (!sc) \
353                 return (ENXIO)
354
355 #define USB_GET_SC(dname, unit, sc) \
356         sc = devclass_get_softc(__CONCAT(dname,_devclass), unit)
357
358 #define USB_DO_ATTACH(dev, bdev, parent, args, print, sub) \
359         (device_probe_and_attach((bdev)) == 0 ? (bdev) : 0)
360
361 /* conversion from one type of queue to the other */
362 /* XXX In FreeBSD SIMPLEQ_REMOVE_HEAD only removes the head element.
363  */
364 #define SIMPLEQ_REMOVE_HEAD(h, e, f)    do {                            \
365                 if ( (e) != SIMPLEQ_FIRST((h)) )                        \
366                         panic("Removing other than first element");     \
367                 STAILQ_REMOVE_HEAD(h, f);                               \
368 } while (0)
369 #define SIMPLEQ_INSERT_HEAD     STAILQ_INSERT_HEAD
370 #define SIMPLEQ_INSERT_TAIL     STAILQ_INSERT_TAIL
371 #define SIMPLEQ_NEXT            STAILQ_NEXT
372 #define SIMPLEQ_FIRST           STAILQ_FIRST
373 #define SIMPLEQ_HEAD            STAILQ_HEAD
374 #define SIMPLEQ_INIT            STAILQ_INIT
375 #define SIMPLEQ_HEAD_INITIALIZER        STAILQ_HEAD_INITIALIZER
376 #define SIMPLEQ_ENTRY           STAILQ_ENTRY
377
378 #include <sys/syslog.h>
379 /*
380 #define logprintf(args...)      log(LOG_DEBUG, args)
381 */
382 #define logprintf               printf
383
384 #ifdef SYSCTL_DECL
385 SYSCTL_DECL(_hw_usb);
386 #endif
387
388 #endif /* __FreeBSD__ */