xref: /freebsd/sys/dev/usb/usb_mbuf.h (revision d5fc25e5d6c52b306312784663ccad85923a9c76)
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 #ifndef _USB2_MBUF_H_
28 #define	_USB2_MBUF_H_
29 
30 /*
31  * The following structure defines a minimum re-implementation of the
32  * mbuf system in the kernel.
33  */
34 struct usb_mbuf {
35 	uint8_t *cur_data_ptr;
36 	uint8_t *min_data_ptr;
37 	struct usb_mbuf *usb2_nextpkt;
38 	struct usb_mbuf *usb2_next;
39 
40 	usb_size_t cur_data_len;
41 	usb_size_t max_data_len;
42 	uint8_t last_packet:1;
43 	uint8_t unused:7;
44 };
45 
46 /*
47  * The following structure defines a minimum re-implementation of the
48  * ifqueue structure in the kernel.
49  */
50 struct usb_ifqueue {
51 	struct usb_mbuf *ifq_head;
52 	struct usb_mbuf *ifq_tail;
53 
54 	usb_size_t ifq_len;
55 	usb_size_t ifq_maxlen;
56 };
57 
58 #define	USB_IF_ENQUEUE(ifq, m) do {		\
59     (m)->usb2_nextpkt = NULL;			\
60     if ((ifq)->ifq_tail == NULL)		\
61         (ifq)->ifq_head = (m);			\
62     else					\
63         (ifq)->ifq_tail->usb2_nextpkt = (m);	\
64     (ifq)->ifq_tail = (m);			\
65     (ifq)->ifq_len++;				\
66   } while (0)
67 
68 #define	USB_IF_DEQUEUE(ifq, m) do {				\
69     (m) = (ifq)->ifq_head;					\
70     if (m) {							\
71         if (((ifq)->ifq_head = (m)->usb2_nextpkt) == NULL) {	\
72 	     (ifq)->ifq_tail = NULL;				\
73 	}							\
74 	(m)->usb2_nextpkt = NULL;				\
75 	(ifq)->ifq_len--;					\
76     }								\
77   } while (0)
78 
79 #define	USB_IF_PREPEND(ifq, m) do {		\
80       (m)->usb2_nextpkt = (ifq)->ifq_head;	\
81       if ((ifq)->ifq_tail == NULL) {		\
82 	  (ifq)->ifq_tail = (m);		\
83       }						\
84       (ifq)->ifq_head = (m);			\
85       (ifq)->ifq_len++;				\
86   } while (0)
87 
88 #define	USB_IF_QFULL(ifq)   ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
89 #define	USB_IF_QLEN(ifq)    ((ifq)->ifq_len)
90 #define	USB_IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
91 
92 #define	USB_MBUF_RESET(m) do {			\
93     (m)->cur_data_ptr = (m)->min_data_ptr;	\
94     (m)->cur_data_len = (m)->max_data_len;	\
95     (m)->last_packet = 0;			\
96   } while (0)
97 
98 /* prototypes */
99 void   *usb2_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq,
100 	    usb_size_t block_size, uint16_t nblocks);
101 
102 #endif					/* _USB2_MBUF_H_ */
103