xref: /freebsd/sys/dev/usb/usb_mbuf.h (revision a593f6b8de8b956bd0dd22f74805ede942e3d6a9)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1502ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1602ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1702ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1802ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1902ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2002ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2102ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2202ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2302ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2402ac6454SAndrew Thompson  * SUCH DAMAGE.
2502ac6454SAndrew Thompson  */
2602ac6454SAndrew Thompson 
2702ac6454SAndrew Thompson #ifndef _USB2_MBUF_H_
2802ac6454SAndrew Thompson #define	_USB2_MBUF_H_
2902ac6454SAndrew Thompson 
3002ac6454SAndrew Thompson /*
3102ac6454SAndrew Thompson  * The following structure defines a minimum re-implementation of the
3202ac6454SAndrew Thompson  * mbuf system in the kernel.
3302ac6454SAndrew Thompson  */
34760bc48eSAndrew Thompson struct usb_mbuf {
3502ac6454SAndrew Thompson 	uint8_t *cur_data_ptr;
3602ac6454SAndrew Thompson 	uint8_t *min_data_ptr;
37a593f6b8SAndrew Thompson 	struct usb_mbuf *usb_nextpkt;
38a593f6b8SAndrew Thompson 	struct usb_mbuf *usb_next;
3902ac6454SAndrew Thompson 
40f9cb546cSAndrew Thompson 	usb_size_t cur_data_len;
41f9cb546cSAndrew Thompson 	usb_size_t max_data_len;
4202ac6454SAndrew Thompson 	uint8_t last_packet:1;
4302ac6454SAndrew Thompson 	uint8_t unused:7;
4402ac6454SAndrew Thompson };
4502ac6454SAndrew Thompson 
4602ac6454SAndrew Thompson /*
4702ac6454SAndrew Thompson  * The following structure defines a minimum re-implementation of the
4802ac6454SAndrew Thompson  * ifqueue structure in the kernel.
4902ac6454SAndrew Thompson  */
50760bc48eSAndrew Thompson struct usb_ifqueue {
51760bc48eSAndrew Thompson 	struct usb_mbuf *ifq_head;
52760bc48eSAndrew Thompson 	struct usb_mbuf *ifq_tail;
5302ac6454SAndrew Thompson 
54f9cb546cSAndrew Thompson 	usb_size_t ifq_len;
55f9cb546cSAndrew Thompson 	usb_size_t ifq_maxlen;
5602ac6454SAndrew Thompson };
5702ac6454SAndrew Thompson 
5802ac6454SAndrew Thompson #define	USB_IF_ENQUEUE(ifq, m) do {		\
59a593f6b8SAndrew Thompson     (m)->usb_nextpkt = NULL;			\
6002ac6454SAndrew Thompson     if ((ifq)->ifq_tail == NULL)		\
6102ac6454SAndrew Thompson         (ifq)->ifq_head = (m);			\
6202ac6454SAndrew Thompson     else					\
63a593f6b8SAndrew Thompson         (ifq)->ifq_tail->usb_nextpkt = (m);	\
6402ac6454SAndrew Thompson     (ifq)->ifq_tail = (m);			\
6502ac6454SAndrew Thompson     (ifq)->ifq_len++;				\
6602ac6454SAndrew Thompson   } while (0)
6702ac6454SAndrew Thompson 
6802ac6454SAndrew Thompson #define	USB_IF_DEQUEUE(ifq, m) do {				\
6902ac6454SAndrew Thompson     (m) = (ifq)->ifq_head;					\
7002ac6454SAndrew Thompson     if (m) {							\
71a593f6b8SAndrew Thompson         if (((ifq)->ifq_head = (m)->usb_nextpkt) == NULL) {	\
7202ac6454SAndrew Thompson 	     (ifq)->ifq_tail = NULL;				\
7302ac6454SAndrew Thompson 	}							\
74a593f6b8SAndrew Thompson 	(m)->usb_nextpkt = NULL;				\
7502ac6454SAndrew Thompson 	(ifq)->ifq_len--;					\
7602ac6454SAndrew Thompson     }								\
7702ac6454SAndrew Thompson   } while (0)
7802ac6454SAndrew Thompson 
7902ac6454SAndrew Thompson #define	USB_IF_PREPEND(ifq, m) do {		\
80a593f6b8SAndrew Thompson       (m)->usb_nextpkt = (ifq)->ifq_head;	\
8102ac6454SAndrew Thompson       if ((ifq)->ifq_tail == NULL) {		\
8202ac6454SAndrew Thompson 	  (ifq)->ifq_tail = (m);		\
8302ac6454SAndrew Thompson       }						\
8402ac6454SAndrew Thompson       (ifq)->ifq_head = (m);			\
8502ac6454SAndrew Thompson       (ifq)->ifq_len++;				\
8602ac6454SAndrew Thompson   } while (0)
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson #define	USB_IF_QFULL(ifq)   ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
8902ac6454SAndrew Thompson #define	USB_IF_QLEN(ifq)    ((ifq)->ifq_len)
9002ac6454SAndrew Thompson #define	USB_IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
9102ac6454SAndrew Thompson 
9202ac6454SAndrew Thompson #define	USB_MBUF_RESET(m) do {			\
9302ac6454SAndrew Thompson     (m)->cur_data_ptr = (m)->min_data_ptr;	\
9402ac6454SAndrew Thompson     (m)->cur_data_len = (m)->max_data_len;	\
9502ac6454SAndrew Thompson     (m)->last_packet = 0;			\
9602ac6454SAndrew Thompson   } while (0)
9702ac6454SAndrew Thompson 
9802ac6454SAndrew Thompson /* prototypes */
99a593f6b8SAndrew Thompson void   *usb_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq,
100f9cb546cSAndrew Thompson 	    usb_size_t block_size, uint16_t nblocks);
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson #endif					/* _USB2_MBUF_H_ */
103