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