usb_mem.h revision 1.23.32.4 1 1.23.32.4 itohy /* $NetBSD: usb_mem.h,v 1.23.32.4 2008/05/21 05:03:48 itohy Exp $ */
2 1.23.32.1 itohy /* $FreeBSD: src/sys/dev/usb/usb_mem.h,v 1.21 2005/01/06 01:43:29 imp Exp $ */
3 1.1 augustss
4 1.23.32.1 itohy /*-
5 1.23.32.1 itohy * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
6 1.1 augustss * All rights reserved.
7 1.1 augustss *
8 1.2 augustss * This code is derived from software contributed to The NetBSD Foundation
9 1.13 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
10 1.2 augustss * Carlstedt Research & Technology.
11 1.1 augustss *
12 1.23.32.1 itohy * This code is derived from software contributed to The NetBSD Foundation
13 1.23.32.1 itohy * by ITOH Yasufumi (itohy (at) NetBSD.org).
14 1.23.32.1 itohy *
15 1.1 augustss * Redistribution and use in source and binary forms, with or without
16 1.1 augustss * modification, are permitted provided that the following conditions
17 1.1 augustss * are met:
18 1.1 augustss * 1. Redistributions of source code must retain the above copyright
19 1.1 augustss * notice, this list of conditions and the following disclaimer.
20 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
21 1.1 augustss * notice, this list of conditions and the following disclaimer in the
22 1.1 augustss * documentation and/or other materials provided with the distribution.
23 1.1 augustss * 3. All advertising materials mentioning features or use of this software
24 1.1 augustss * must display the following acknowledgement:
25 1.1 augustss * This product includes software developed by the NetBSD
26 1.1 augustss * Foundation, Inc. and its contributors.
27 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
28 1.1 augustss * contributors may be used to endorse or promote products derived
29 1.1 augustss * from this software without specific prior written permission.
30 1.1 augustss *
31 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
42 1.1 augustss */
43 1.1 augustss
44 1.23.32.1 itohy /* track memory allocation/free */
45 1.23.32.1 itohy #define USBMEM_DEBUG
46 1.23.32.1 itohy
47 1.23.32.1 itohy #define USB_DMA_NSEG (btoc(MAXPHYS) + 1)
48 1.23.32.1 itohy
49 1.6 augustss typedef struct usb_dma_block {
50 1.23.32.1 itohy #ifdef __FreeBSD__
51 1.1 augustss bus_dma_tag_t tag;
52 1.23.32.1 itohy #endif
53 1.1 augustss bus_dmamap_t map;
54 1.23.32.3 itohy usb_busdma_kaddr_t kaddr;
55 1.1 augustss bus_dma_segment_t segs[1];
56 1.1 augustss int nsegs;
57 1.1 augustss size_t size;
58 1.1 augustss size_t align;
59 1.21 fvdl int flags;
60 1.21 fvdl #define USB_DMA_FULLBLOCK 0x0001
61 1.23.32.1 itohy #define USB_DMA_MARKED_TO_FREE 0x0002
62 1.6 augustss LIST_ENTRY(usb_dma_block) next;
63 1.23.32.1 itohy #ifdef USBMEM_DEBUG
64 1.23.32.1 itohy TAILQ_ENTRY(usb_dma_block) b_debug_next;
65 1.23.32.1 itohy const char *b_fn;
66 1.23.32.1 itohy int b_ln;
67 1.23.32.1 itohy #endif
68 1.1 augustss } usb_dma_block_t;
69 1.1 augustss
70 1.23.32.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
71 1.23.32.1 itohy #define DMAADDR(dma, o) \
72 1.23.32.1 itohy ((dma)->block->map->dm_segs[0].ds_addr + (dma)->offs + (o))
73 1.23.32.1 itohy #define USB_MEM_SYNC(utag, dma, ops) \
74 1.23.32.1 itohy bus_dmamap_sync((utag)->tag, (dma)->block->map, \
75 1.23.32.1 itohy (dma)->offs, (dma)->len, (ops))
76 1.23.32.1 itohy #define USB_MEM_SYNC2(utag, dma, off, len, ops) \
77 1.23.32.1 itohy bus_dmamap_sync((utag)->tag, (dma)->block->map, \
78 1.23.32.1 itohy (dma)->offs + (off), (len), (ops))
79 1.23.32.1 itohy #elif defined(__FreeBSD__)
80 1.23.32.1 itohy #define DMAADDR(dma, o) \
81 1.23.32.1 itohy ((dma)->block->segs[0].ds_addr + (dma)->offs + (o))
82 1.23.32.1 itohy /*
83 1.23.32.1 itohy * XXX overkill
84 1.23.32.1 itohy * FreeBSD can't sync map partially
85 1.23.32.1 itohy */
86 1.23.32.1 itohy #define USB_MEM_SYNC(utag, dma, ops) \
87 1.23.32.1 itohy bus_dmamap_sync((dma)->block->tag, (dma)->block->map, (ops))
88 1.23.32.1 itohy #define USB_MEM_SYNC2(utag, dma, off, len, ops) \
89 1.23.32.1 itohy bus_dmamap_sync((dma)->block->tag, (dma)->block->map, (ops))
90 1.23.32.1 itohy #endif
91 1.23.32.1 itohy
92 1.16 augustss #define KERNADDR(dma, o) \
93 1.23.32.3 itohy ((void *)((char *)(dma)->block->kaddr + (dma)->offs + (o)))
94 1.1 augustss
95 1.23.32.1 itohy typedef struct {
96 1.23.32.1 itohy struct usb_dma_block *block;
97 1.23.32.1 itohy u_int offs;
98 1.23.32.1 itohy u_int len;
99 1.23.32.1 itohy } usb_dma_t;
100 1.3 augustss
101 1.23.32.1 itohy /*
102 1.23.32.1 itohy * DMA memory
103 1.23.32.1 itohy */
104 1.23.32.1 itohy struct usb_dmamem {
105 1.23.32.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
106 1.23.32.1 itohy bus_dma_segment_t ud_segs[USB_DMA_NSEG];
107 1.23.32.1 itohy int ud_nsegs;
108 1.23.32.1 itohy size_t ud_len;
109 1.23.32.1 itohy #elif defined(__FreeBSD__)
110 1.23.32.1 itohy bus_dma_tag_t ud_tag;
111 1.23.32.1 itohy bus_dmamap_t ud_map;
112 1.23.32.1 itohy #endif
113 1.23.32.3 itohy usb_busdma_kaddr_t ud_kaddr;
114 1.21 fvdl
115 1.23.32.1 itohy SIMPLEQ_ENTRY(usb_dmamem) ud_next;
116 1.21 fvdl
117 1.23.32.1 itohy #ifdef USBMEM_DEBUG
118 1.23.32.1 itohy TAILQ_ENTRY(usb_dmamem) ud_debug_next;
119 1.23.32.1 itohy const char *ud_fn;
120 1.23.32.1 itohy int ud_ln;
121 1.21 fvdl #endif
122 1.23.32.1 itohy };
123 1.21 fvdl
124 1.23.32.1 itohy /*
125 1.23.32.1 itohy * DMA buffer
126 1.23.32.1 itohy */
127 1.23.32.1 itohy struct usb_buffer_dma {
128 1.23.32.1 itohy enum { UB_NONE, UB_BUF, UB_ALLOC_MAP, UB_MAP } ub_type;
129 1.23.32.1 itohy int ub_flags;
130 1.23.32.1 itohy #define USB_BUFFL_ASYNCMAP 0x01 /* created with BUS_DMA_ALLOCNOW */
131 1.23.32.1 itohy #define USB_BUFFL_MAP 0x02 /* created without BUS_DMA_ALLOCNOW */
132 1.21 fvdl
133 1.23.32.1 itohy usb_dma_t ub_allocbuf;
134 1.23.32.1 itohy struct usb_dmamem *ub_dmamem;
135 1.21 fvdl
136 1.23.32.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
137 1.23.32.1 itohy bus_dmamap_t ub_map;
138 1.23.32.1 itohy bus_dma_segment_t ub_bufseg;
139 1.23.32.1 itohy #define _USB_BUFFER_NSEGS(ub) (ub)->ub_map->dm_nsegs
140 1.23.32.1 itohy #define USB_BUFFER_NSEGS(ub) \
141 1.23.32.1 itohy ((ub)->ub_type == UB_BUF ? 1 : _USB_BUFFER_NSEGS(ub))
142 1.23.32.1 itohy #define USB_BUFFER_SEGS(ub) \
143 1.23.32.1 itohy ((ub)->ub_type == UB_BUF ? \
144 1.23.32.1 itohy &(ub)->ub_bufseg : (ub)->ub_map->dm_segs)
145 1.21 fvdl
146 1.4 augustss #elif defined(__FreeBSD__)
147 1.23.32.1 itohy bus_dma_tag_t ub_curtag;
148 1.23.32.1 itohy bus_dma_tag_t ub_asynctag;
149 1.23.32.1 itohy bus_dmamap_t ub_map;
150 1.23.32.1 itohy bus_dma_segment_t ub_segs[USB_DMA_NSEG];
151 1.23.32.1 itohy int ub_nsegs;
152 1.23.32.1 itohy int ub_error;
153 1.23.32.1 itohy #define _USB_BUFFER_NSEGS(ub) (ub)->ub_nsegs
154 1.23.32.1 itohy #define USB_BUFFER_NSEGS(ub) (_USB_BUFFER_NSEGS(ub))
155 1.23.32.1 itohy #define USB_BUFFER_SEGS(ub) (ub)->ub_segs
156 1.23.32.1 itohy #endif
157 1.23.32.1 itohy };
158 1.3 augustss
159 1.3 augustss
160 1.23.32.1 itohy #ifdef USBMEM_DEBUG
161 1.23.32.1 itohy struct usb_frag_debug {
162 1.23.32.1 itohy const char *f_fn;
163 1.23.32.1 itohy int f_ln;
164 1.23.32.1 itohy struct usb_frag_dma *f_frag;
165 1.23.32.1 itohy TAILQ_ENTRY(usb_frag_debug) f_debug_next;
166 1.23.32.1 itohy };
167 1.23.32.1 itohy #endif
168 1.18 augustss
169 1.23.32.1 itohy /*
170 1.23.32.1 itohy * per host controller
171 1.23.32.1 itohy */
172 1.23.32.1 itohy typedef struct usb_dma_tag {
173 1.23.32.1 itohy /*
174 1.23.32.1 itohy * bus_dma(9) tag, filled by host controller driver
175 1.23.32.1 itohy */
176 1.23.32.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
177 1.23.32.1 itohy bus_dma_tag_t tag;
178 1.23.32.1 itohy #endif
179 1.23.32.1 itohy #ifdef __FreeBSD__
180 1.23.32.1 itohy bus_dma_tag_t tag_parent;
181 1.23.32.1 itohy bus_dma_tag_t tag_buffer;
182 1.23.32.1 itohy #endif
183 1.3 augustss
184 1.23.32.1 itohy /*
185 1.23.32.1 itohy * Lists of free DMA memory
186 1.23.32.1 itohy */
187 1.23.32.1 itohy LIST_HEAD(, usb_dma_block) blk_freelist;
188 1.23.32.1 itohy int blk_nfree;
189 1.23.32.1 itohy LIST_HEAD(, usb_frag_dma) frag_freelist;
190 1.23.32.1 itohy /* for statistical and diagnostic purposes */
191 1.23.32.1 itohy int nblks; /* number of allocated buffers */
192 1.23.32.1 itohy int nfrags; /* number of allocated fragments */
193 1.23.32.1 itohy
194 1.23.32.1 itohy /* for delayed free */
195 1.23.32.1 itohy SIMPLEQ_HEAD(, usb_dmamem) uds_idle, uds_tobefreed;
196 1.23.32.1 itohy /* for statistical and diagnostic purposes */
197 1.23.32.1 itohy int nbufs; /* number of allocated buffer structures */
198 1.23.32.1 itohy
199 1.23.32.1 itohy struct usb_task unmap_task;
200 1.23.32.1 itohy
201 1.23.32.1 itohy #ifdef USBMEM_DEBUG
202 1.23.32.1 itohy const char *devname;
203 1.23.32.1 itohy TAILQ_HEAD(usb_debug_dmamem_head, usb_dmamem) uds_lease;
204 1.23.32.1 itohy TAILQ_HEAD(usb_debug_blk_head, usb_dma_block) blks_lease;
205 1.23.32.1 itohy TAILQ_HEAD(usb_debug_frag_head, usb_frag_debug)
206 1.23.32.1 itohy frags_lease, free_frag_debug;
207 1.23.32.1 itohy #define USBMEM_DEBUG_NFRAG 100
208 1.23.32.1 itohy struct usb_frag_debug frag_debug_chunk[USBMEM_DEBUG_NFRAG];
209 1.23.32.1 itohy int frag_debug_used, frag_debug_used_max;
210 1.23.32.1 itohy #endif
211 1.23.32.1 itohy } usb_dma_tag_t;
212 1.3 augustss
213 1.23.32.4 itohy /*
214 1.23.32.4 itohy * bufer pointer
215 1.23.32.4 itohy */
216 1.23.32.4 itohy union usb_bufptr {
217 1.23.32.4 itohy /* plain buffer */
218 1.23.32.4 itohy struct {
219 1.23.32.4 itohy unsigned char *p_buf;
220 1.23.32.4 itohy } ptr_p;
221 1.23.32.4 itohy /* mbuf */
222 1.23.32.4 itohy struct {
223 1.23.32.4 itohy struct mbuf *m_mbuf;
224 1.23.32.4 itohy int m_off;
225 1.23.32.4 itohy } ptr_m;
226 1.23.32.4 itohy };
227 1.23.32.4 itohy
228 1.23.32.4 itohy /*
229 1.23.32.4 itohy * auxillary storage (bounce buffer)
230 1.23.32.4 itohy */
231 1.23.32.4 itohy /* auxillary buffer description */
232 1.23.32.4 itohy struct usb_aux_desc {
233 1.23.32.4 itohy int aux_len; /* Auxillary storage size. */
234 1.23.32.4 itohy void *aux_kern; /* Kernel address of aux storage */
235 1.23.32.4 itohy union usb_bufptr aux_ptr; /* Original aux data pointer. */
236 1.23.32.4 itohy };
237 1.23.32.4 itohy
238 1.23.32.1 itohy #ifdef USBMEM_DEBUG
239 1.23.32.1 itohy #define usb_allocmem usb_allocmem_debug
240 1.23.32.1 itohy #define usb_freemem usb_freemem_debug
241 1.23.32.1 itohy #define usb_alloc_buffer_dma usb_alloc_buffer_dma_debug
242 1.23.32.1 itohy #define usb_free_buffer_dma usb_free_buffer_dma_debug
243 1.23.32.1 itohy #define usb_clean_buffer_dma usb_clean_buffer_dma_debug
244 1.23.32.1 itohy #define usb_dma_tag_init usb_dma_tag_init_debug
245 1.23.32.1 itohy #define usb_dma_tag_finish usb_dma_tag_finish_debug
246 1.23.32.1 itohy #define USBMEM_DEBUGPARAMS , const char *, int
247 1.9 augustss #else
248 1.23.32.1 itohy #define USBMEM_DEBUGPARAMS
249 1.9 augustss #endif
250 1.3 augustss
251 1.23.32.1 itohy usbd_status usb_allocmem(usb_dma_tag_t *, size_t, size_t, usb_dma_t *
252 1.23.32.1 itohy USBMEM_DEBUGPARAMS);
253 1.23.32.1 itohy void usb_freemem(usb_dma_tag_t *, usb_dma_t *
254 1.23.32.1 itohy USBMEM_DEBUGPARAMS);
255 1.23.32.1 itohy usbd_status usb_alloc_buffer_dma(usb_dma_tag_t *, struct usb_buffer_dma *,
256 1.23.32.1 itohy void *, size_t, void **
257 1.23.32.1 itohy USBMEM_DEBUGPARAMS);
258 1.23.32.1 itohy void usb_free_buffer_dma(usb_dma_tag_t *, struct usb_buffer_dma *,
259 1.23.32.1 itohy enum usbd_waitflg
260 1.23.32.1 itohy USBMEM_DEBUGPARAMS);
261 1.23.32.1 itohy void usb_map_dma(usb_dma_tag_t *, struct usb_buffer_dma *,
262 1.23.32.1 itohy void *, size_t);
263 1.23.32.2 itohy usbd_status usb_map_mbuf_dma(usb_dma_tag_t *, struct usb_buffer_dma *,
264 1.23.32.1 itohy struct mbuf *);
265 1.23.32.1 itohy void usb_unmap_dma(usb_dma_tag_t *u, struct usb_buffer_dma *);
266 1.23.32.1 itohy usbd_status usb_alloc_dma_resources(usb_dma_tag_t *,
267 1.23.32.1 itohy struct usb_buffer_dma *);
268 1.23.32.1 itohy void usb_free_dma_resources(usb_dma_tag_t *,
269 1.23.32.1 itohy struct usb_buffer_dma *);
270 1.23.32.1 itohy void usb_sync_buffer_dma(usb_dma_tag_t *, struct usb_buffer_dma *,
271 1.23.32.1 itohy int /*ops*/);
272 1.23.32.1 itohy void usb_clean_buffer_dma(usb_dma_tag_t *, struct usb_buffer_dma *
273 1.23.32.1 itohy USBMEM_DEBUGPARAMS);
274 1.23.32.1 itohy usbd_status usb_insert_transfer_dma(usbd_xfer_handle, usb_dma_tag_t *,
275 1.23.32.1 itohy struct usb_buffer_dma *);
276 1.23.32.1 itohy void usb_transfer_complete_dma(usbd_xfer_handle, usb_dma_tag_t *,
277 1.23.32.1 itohy struct usb_buffer_dma *);
278 1.23.32.1 itohy void usb_dma_tag_init(usb_dma_tag_t *
279 1.23.32.1 itohy #ifdef USBMEM_DEBUG
280 1.23.32.1 itohy , device_ptr_t
281 1.23.32.1 itohy #endif
282 1.23.32.1 itohy USBMEM_DEBUGPARAMS);
283 1.23.32.1 itohy void usb_dma_tag_finish(usb_dma_tag_t *
284 1.23.32.1 itohy USBMEM_DEBUGPARAMS);
285 1.23.32.1 itohy
286 1.23.32.4 itohy void usb_bufptr_init(union usb_bufptr *, usbd_xfer_handle);
287 1.23.32.4 itohy void usb_bufptr_advance(union usb_bufptr *, int /*len*/,
288 1.23.32.4 itohy int /*is_mbuf*/);
289 1.23.32.4 itohy void usb_bufptr_rd(const union usb_bufptr *, const void *,
290 1.23.32.4 itohy int /*len*/, int /*is_mbuf*/);
291 1.23.32.4 itohy void usb_bufptr_wr(const union usb_bufptr *, void *,
292 1.23.32.4 itohy int /*len*/, int /*is_mbuf*/);
293 1.23.32.4 itohy
294 1.23.32.1 itohy #ifdef USBMEM_DEBUG
295 1.23.32.1 itohy #undef USBMEM_DEBUGPARAMS
296 1.23.32.1 itohy #ifndef USBMEM_PRIVATE
297 1.23.32.1 itohy #undef usb_allocmem
298 1.23.32.1 itohy #define usb_allocmem(utag, sz, al, dma) \
299 1.23.32.1 itohy usb_allocmem_debug(utag, sz, al, dma, __FILE__, __LINE__)
300 1.23.32.1 itohy #undef usb_freemem
301 1.23.32.1 itohy #define usb_freemem(utag, dma) \
302 1.23.32.1 itohy usb_freemem_debug(utag, dma, __FILE__, __LINE__)
303 1.23.32.1 itohy #undef usb_alloc_buffer_dma
304 1.23.32.1 itohy #define usb_alloc_buffer_dma(utag, ub, ptr, sz, padr) \
305 1.23.32.1 itohy usb_alloc_buffer_dma_debug(utag, ub, ptr, sz, padr, __FILE__, __LINE__)
306 1.23.32.1 itohy #undef usb_free_buffer_dma
307 1.23.32.1 itohy #define usb_free_buffer_dma(utag, ub, w) \
308 1.23.32.1 itohy usb_free_buffer_dma_debug(utag, ub, w, __FILE__, __LINE__)
309 1.23.32.1 itohy #undef usb_clean_buffer_dma
310 1.23.32.1 itohy #define usb_clean_buffer_dma(utag, ub) \
311 1.23.32.1 itohy usb_clean_buffer_dma_debug(utag, ub, __FILE__, __LINE__)
312 1.23.32.1 itohy #undef usb_dma_tag_init
313 1.23.32.1 itohy #define usb_dma_tag_init(utag) \
314 1.23.32.1 itohy usb_dma_tag_init_debug(utag, (device_ptr_t)sc, __FILE__, __LINE__)
315 1.23.32.1 itohy #undef usb_dma_tag_finish
316 1.23.32.1 itohy #define usb_dma_tag_finish(utag) \
317 1.23.32.1 itohy usb_dma_tag_finish_debug(utag, __FILE__, __LINE__)
318 1.23.32.1 itohy #endif /* !USBMEM_PRIVATE */
319 1.23.32.1 itohy #endif /* USBMEM_DEBUG */
320