Home | History | Annotate | Line # | Download | only in usb
usb_mem.h revision 1.23.32.2
      1  1.23.32.1     itohy /*	$NetBSD: usb_mem.h,v 1.23.32.2 2007/05/31 23:15:18 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.1  augustss         caddr_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.1     itohy 	((void *)((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 	caddr_t		ud_kaddr;
    110  1.23.32.1     itohy #elif defined(__FreeBSD__)
    111  1.23.32.1     itohy 	void		*ud_kaddr;
    112  1.23.32.1     itohy 	bus_dma_tag_t	ud_tag;
    113  1.23.32.1     itohy 	bus_dmamap_t	ud_map;
    114  1.23.32.1     itohy #endif
    115       1.21      fvdl 
    116  1.23.32.1     itohy 	SIMPLEQ_ENTRY(usb_dmamem)	ud_next;
    117       1.21      fvdl 
    118  1.23.32.1     itohy #ifdef USBMEM_DEBUG
    119  1.23.32.1     itohy 	TAILQ_ENTRY(usb_dmamem)		ud_debug_next;
    120  1.23.32.1     itohy 	const char			*ud_fn;
    121  1.23.32.1     itohy 	int				ud_ln;
    122       1.21      fvdl #endif
    123  1.23.32.1     itohy };
    124       1.21      fvdl 
    125  1.23.32.1     itohy /*
    126  1.23.32.1     itohy  * DMA buffer
    127  1.23.32.1     itohy  */
    128  1.23.32.1     itohy struct usb_buffer_dma {
    129  1.23.32.1     itohy 	enum { UB_NONE, UB_BUF, UB_ALLOC_MAP, UB_MAP } ub_type;
    130  1.23.32.1     itohy 	int		ub_flags;
    131  1.23.32.1     itohy #define USB_BUFFL_ASYNCMAP	0x01	/* created with BUS_DMA_ALLOCNOW */
    132  1.23.32.1     itohy #define USB_BUFFL_MAP		0x02	/* created without BUS_DMA_ALLOCNOW */
    133       1.21      fvdl 
    134  1.23.32.1     itohy 	usb_dma_t	ub_allocbuf;
    135  1.23.32.1     itohy 	struct usb_dmamem *ub_dmamem;
    136       1.21      fvdl 
    137  1.23.32.1     itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
    138  1.23.32.1     itohy 	bus_dmamap_t	ub_map;
    139  1.23.32.1     itohy 	bus_dma_segment_t ub_bufseg;
    140  1.23.32.1     itohy #define _USB_BUFFER_NSEGS(ub)	(ub)->ub_map->dm_nsegs
    141  1.23.32.1     itohy #define USB_BUFFER_NSEGS(ub)	\
    142  1.23.32.1     itohy 	((ub)->ub_type == UB_BUF ? 1 : _USB_BUFFER_NSEGS(ub))
    143  1.23.32.1     itohy #define USB_BUFFER_SEGS(ub)	\
    144  1.23.32.1     itohy 	((ub)->ub_type == UB_BUF ? \
    145  1.23.32.1     itohy 	 &(ub)->ub_bufseg : (ub)->ub_map->dm_segs)
    146       1.21      fvdl 
    147        1.4  augustss #elif defined(__FreeBSD__)
    148  1.23.32.1     itohy 	bus_dma_tag_t	ub_curtag;
    149  1.23.32.1     itohy 	bus_dma_tag_t	ub_asynctag;
    150  1.23.32.1     itohy 	bus_dmamap_t	ub_map;
    151  1.23.32.1     itohy 	bus_dma_segment_t ub_segs[USB_DMA_NSEG];
    152  1.23.32.1     itohy 	int		ub_nsegs;
    153  1.23.32.1     itohy 	int		ub_error;
    154  1.23.32.1     itohy #define _USB_BUFFER_NSEGS(ub)	(ub)->ub_nsegs
    155  1.23.32.1     itohy #define USB_BUFFER_NSEGS(ub)	(_USB_BUFFER_NSEGS(ub))
    156  1.23.32.1     itohy #define USB_BUFFER_SEGS(ub)	(ub)->ub_segs
    157  1.23.32.1     itohy #endif
    158  1.23.32.1     itohy };
    159        1.3  augustss 
    160        1.3  augustss 
    161  1.23.32.1     itohy #ifdef USBMEM_DEBUG
    162  1.23.32.1     itohy struct usb_frag_debug {
    163  1.23.32.1     itohy 	const char			*f_fn;
    164  1.23.32.1     itohy 	int				f_ln;
    165  1.23.32.1     itohy 	struct usb_frag_dma		*f_frag;
    166  1.23.32.1     itohy 	TAILQ_ENTRY(usb_frag_debug)	f_debug_next;
    167  1.23.32.1     itohy };
    168  1.23.32.1     itohy #endif
    169       1.18  augustss 
    170  1.23.32.1     itohy /*
    171  1.23.32.1     itohy  * per host controller
    172  1.23.32.1     itohy  */
    173  1.23.32.1     itohy typedef struct usb_dma_tag {
    174  1.23.32.1     itohy 	/*
    175  1.23.32.1     itohy 	 * bus_dma(9) tag, filled by host controller driver
    176  1.23.32.1     itohy 	 */
    177  1.23.32.1     itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
    178  1.23.32.1     itohy 	bus_dma_tag_t	tag;
    179  1.23.32.1     itohy #endif
    180  1.23.32.1     itohy #ifdef __FreeBSD__
    181  1.23.32.1     itohy 	bus_dma_tag_t	tag_parent;
    182  1.23.32.1     itohy 	bus_dma_tag_t	tag_buffer;
    183  1.23.32.1     itohy #endif
    184        1.3  augustss 
    185  1.23.32.1     itohy 	/*
    186  1.23.32.1     itohy 	 * Lists of free DMA memory
    187  1.23.32.1     itohy 	 */
    188  1.23.32.1     itohy 	LIST_HEAD(, usb_dma_block) blk_freelist;
    189  1.23.32.1     itohy 	int	blk_nfree;
    190  1.23.32.1     itohy 	LIST_HEAD(, usb_frag_dma) frag_freelist;
    191  1.23.32.1     itohy 	/* for statistical and diagnostic purposes */
    192  1.23.32.1     itohy 	int	nblks;		/* number of allocated buffers */
    193  1.23.32.1     itohy 	int	nfrags;		/* number of allocated fragments */
    194  1.23.32.1     itohy 
    195  1.23.32.1     itohy 	/* for delayed free */
    196  1.23.32.1     itohy 	SIMPLEQ_HEAD(, usb_dmamem) uds_idle, uds_tobefreed;
    197  1.23.32.1     itohy 	/* for statistical and diagnostic purposes */
    198  1.23.32.1     itohy 	int	nbufs;		/* number of allocated buffer structures */
    199  1.23.32.1     itohy 
    200  1.23.32.1     itohy 	struct usb_task		unmap_task;
    201  1.23.32.1     itohy 
    202  1.23.32.1     itohy #ifdef USBMEM_DEBUG
    203  1.23.32.1     itohy 	const char			*devname;
    204  1.23.32.1     itohy 	TAILQ_HEAD(usb_debug_dmamem_head, usb_dmamem)	uds_lease;
    205  1.23.32.1     itohy 	TAILQ_HEAD(usb_debug_blk_head, usb_dma_block)	blks_lease;
    206  1.23.32.1     itohy 	TAILQ_HEAD(usb_debug_frag_head, usb_frag_debug)
    207  1.23.32.1     itohy 					frags_lease, free_frag_debug;
    208  1.23.32.1     itohy #define USBMEM_DEBUG_NFRAG	100
    209  1.23.32.1     itohy 	struct usb_frag_debug		frag_debug_chunk[USBMEM_DEBUG_NFRAG];
    210  1.23.32.1     itohy 	int	frag_debug_used, frag_debug_used_max;
    211  1.23.32.1     itohy #endif
    212  1.23.32.1     itohy } usb_dma_tag_t;
    213        1.3  augustss 
    214  1.23.32.1     itohy #ifdef USBMEM_DEBUG
    215  1.23.32.1     itohy #define usb_allocmem		usb_allocmem_debug
    216  1.23.32.1     itohy #define usb_freemem		usb_freemem_debug
    217  1.23.32.1     itohy #define usb_alloc_buffer_dma	usb_alloc_buffer_dma_debug
    218  1.23.32.1     itohy #define usb_free_buffer_dma	usb_free_buffer_dma_debug
    219  1.23.32.1     itohy #define usb_clean_buffer_dma	usb_clean_buffer_dma_debug
    220  1.23.32.1     itohy #define usb_dma_tag_init	usb_dma_tag_init_debug
    221  1.23.32.1     itohy #define usb_dma_tag_finish	usb_dma_tag_finish_debug
    222  1.23.32.1     itohy #define USBMEM_DEBUGPARAMS	, const char *, int
    223        1.9  augustss #else
    224  1.23.32.1     itohy #define USBMEM_DEBUGPARAMS
    225        1.9  augustss #endif
    226        1.3  augustss 
    227  1.23.32.1     itohy usbd_status	usb_allocmem(usb_dma_tag_t *, size_t, size_t, usb_dma_t *
    228  1.23.32.1     itohy 		    USBMEM_DEBUGPARAMS);
    229  1.23.32.1     itohy void		usb_freemem(usb_dma_tag_t *, usb_dma_t *
    230  1.23.32.1     itohy 		    USBMEM_DEBUGPARAMS);
    231  1.23.32.1     itohy usbd_status	usb_alloc_buffer_dma(usb_dma_tag_t *, struct usb_buffer_dma *,
    232  1.23.32.1     itohy 		    void *, size_t, void **
    233  1.23.32.1     itohy 		    USBMEM_DEBUGPARAMS);
    234  1.23.32.1     itohy void		usb_free_buffer_dma(usb_dma_tag_t *, struct usb_buffer_dma *,
    235  1.23.32.1     itohy 		    enum usbd_waitflg
    236  1.23.32.1     itohy 		    USBMEM_DEBUGPARAMS);
    237  1.23.32.1     itohy void		usb_map_dma(usb_dma_tag_t *, struct usb_buffer_dma *,
    238  1.23.32.1     itohy 		    void *, size_t);
    239  1.23.32.2     itohy usbd_status	usb_map_mbuf_dma(usb_dma_tag_t *, struct usb_buffer_dma *,
    240  1.23.32.1     itohy 		    struct mbuf *);
    241  1.23.32.1     itohy void		usb_unmap_dma(usb_dma_tag_t *u, struct usb_buffer_dma *);
    242  1.23.32.1     itohy usbd_status	usb_alloc_dma_resources(usb_dma_tag_t *,
    243  1.23.32.1     itohy 		    struct usb_buffer_dma *);
    244  1.23.32.1     itohy void		usb_free_dma_resources(usb_dma_tag_t *,
    245  1.23.32.1     itohy 		    struct usb_buffer_dma *);
    246  1.23.32.1     itohy void		usb_sync_buffer_dma(usb_dma_tag_t *, struct usb_buffer_dma *,
    247  1.23.32.1     itohy 		    int /*ops*/);
    248  1.23.32.1     itohy void		usb_clean_buffer_dma(usb_dma_tag_t *, struct usb_buffer_dma *
    249  1.23.32.1     itohy 		    USBMEM_DEBUGPARAMS);
    250  1.23.32.1     itohy usbd_status	usb_insert_transfer_dma(usbd_xfer_handle, usb_dma_tag_t *,
    251  1.23.32.1     itohy 		    struct usb_buffer_dma *);
    252  1.23.32.1     itohy void		usb_transfer_complete_dma(usbd_xfer_handle, usb_dma_tag_t *,
    253  1.23.32.1     itohy 		    struct usb_buffer_dma *);
    254  1.23.32.1     itohy void		usb_dma_tag_init(usb_dma_tag_t *
    255  1.23.32.1     itohy #ifdef USBMEM_DEBUG
    256  1.23.32.1     itohy 		    , device_ptr_t
    257  1.23.32.1     itohy #endif
    258  1.23.32.1     itohy 		    USBMEM_DEBUGPARAMS);
    259  1.23.32.1     itohy void		usb_dma_tag_finish(usb_dma_tag_t *
    260  1.23.32.1     itohy 		    USBMEM_DEBUGPARAMS);
    261  1.23.32.1     itohy 
    262  1.23.32.1     itohy #ifdef USBMEM_DEBUG
    263  1.23.32.1     itohy #undef USBMEM_DEBUGPARAMS
    264  1.23.32.1     itohy #ifndef USBMEM_PRIVATE
    265  1.23.32.1     itohy #undef usb_allocmem
    266  1.23.32.1     itohy #define usb_allocmem(utag, sz, al, dma) \
    267  1.23.32.1     itohy 	usb_allocmem_debug(utag, sz, al, dma, __FILE__, __LINE__)
    268  1.23.32.1     itohy #undef usb_freemem
    269  1.23.32.1     itohy #define usb_freemem(utag, dma) \
    270  1.23.32.1     itohy 	usb_freemem_debug(utag, dma, __FILE__, __LINE__)
    271  1.23.32.1     itohy #undef usb_alloc_buffer_dma
    272  1.23.32.1     itohy #define usb_alloc_buffer_dma(utag, ub, ptr, sz, padr) \
    273  1.23.32.1     itohy 	usb_alloc_buffer_dma_debug(utag, ub, ptr, sz, padr, __FILE__, __LINE__)
    274  1.23.32.1     itohy #undef usb_free_buffer_dma
    275  1.23.32.1     itohy #define usb_free_buffer_dma(utag, ub, w) \
    276  1.23.32.1     itohy 	usb_free_buffer_dma_debug(utag, ub, w, __FILE__, __LINE__)
    277  1.23.32.1     itohy #undef usb_clean_buffer_dma
    278  1.23.32.1     itohy #define usb_clean_buffer_dma(utag, ub) \
    279  1.23.32.1     itohy 	usb_clean_buffer_dma_debug(utag, ub, __FILE__, __LINE__)
    280  1.23.32.1     itohy #undef usb_dma_tag_init
    281  1.23.32.1     itohy #define usb_dma_tag_init(utag) \
    282  1.23.32.1     itohy 	usb_dma_tag_init_debug(utag, (device_ptr_t)sc, __FILE__, __LINE__)
    283  1.23.32.1     itohy #undef usb_dma_tag_finish
    284  1.23.32.1     itohy #define usb_dma_tag_finish(utag) \
    285  1.23.32.1     itohy 	usb_dma_tag_finish_debug(utag, __FILE__, __LINE__)
    286  1.23.32.1     itohy #endif	/* !USBMEM_PRIVATE */
    287  1.23.32.1     itohy #endif	/* USBMEM_DEBUG */
    288