xref: /freebsd/sys/dev/usb/usb_mbuf.h (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
3*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4*718cf2ccSPedro F. Giffuni  *
502ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
802ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
902ac6454SAndrew Thompson  * are met:
1002ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1202ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1402ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1502ac6454SAndrew Thompson  *
1602ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1702ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1802ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1902ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2002ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2102ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2202ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2302ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2402ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2502ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2602ac6454SAndrew Thompson  * SUCH DAMAGE.
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
2975973647SAndrew Thompson #ifndef _USB_MBUF_H_
3075973647SAndrew Thompson #define	_USB_MBUF_H_
3102ac6454SAndrew Thompson 
3202ac6454SAndrew Thompson /*
3302ac6454SAndrew Thompson  * The following structure defines a minimum re-implementation of the
3402ac6454SAndrew Thompson  * mbuf system in the kernel.
3502ac6454SAndrew Thompson  */
36760bc48eSAndrew Thompson struct usb_mbuf {
3702ac6454SAndrew Thompson 	uint8_t *cur_data_ptr;
3802ac6454SAndrew Thompson 	uint8_t *min_data_ptr;
39a593f6b8SAndrew Thompson 	struct usb_mbuf *usb_nextpkt;
40a593f6b8SAndrew Thompson 	struct usb_mbuf *usb_next;
4102ac6454SAndrew Thompson 
42f9cb546cSAndrew Thompson 	usb_size_t cur_data_len;
43f9cb546cSAndrew Thompson 	usb_size_t max_data_len;
4402ac6454SAndrew Thompson 	uint8_t last_packet:1;
4502ac6454SAndrew Thompson 	uint8_t unused:7;
4602ac6454SAndrew Thompson };
4702ac6454SAndrew Thompson 
4802ac6454SAndrew Thompson #define	USB_IF_ENQUEUE(ifq, m) do {		\
49a593f6b8SAndrew Thompson     (m)->usb_nextpkt = NULL;			\
5002ac6454SAndrew Thompson     if ((ifq)->ifq_tail == NULL)		\
5102ac6454SAndrew Thompson         (ifq)->ifq_head = (m);			\
5202ac6454SAndrew Thompson     else					\
53a593f6b8SAndrew Thompson         (ifq)->ifq_tail->usb_nextpkt = (m);	\
5402ac6454SAndrew Thompson     (ifq)->ifq_tail = (m);			\
5502ac6454SAndrew Thompson     (ifq)->ifq_len++;				\
5602ac6454SAndrew Thompson   } while (0)
5702ac6454SAndrew Thompson 
5802ac6454SAndrew Thompson #define	USB_IF_DEQUEUE(ifq, m) do {				\
5902ac6454SAndrew Thompson     (m) = (ifq)->ifq_head;					\
6002ac6454SAndrew Thompson     if (m) {							\
61a593f6b8SAndrew Thompson         if (((ifq)->ifq_head = (m)->usb_nextpkt) == NULL) {	\
6202ac6454SAndrew Thompson 	     (ifq)->ifq_tail = NULL;				\
6302ac6454SAndrew Thompson 	}							\
64a593f6b8SAndrew Thompson 	(m)->usb_nextpkt = NULL;				\
6502ac6454SAndrew Thompson 	(ifq)->ifq_len--;					\
6602ac6454SAndrew Thompson     }								\
6702ac6454SAndrew Thompson   } while (0)
6802ac6454SAndrew Thompson 
6902ac6454SAndrew Thompson #define	USB_IF_PREPEND(ifq, m) do {		\
70a593f6b8SAndrew Thompson       (m)->usb_nextpkt = (ifq)->ifq_head;	\
7102ac6454SAndrew Thompson       if ((ifq)->ifq_tail == NULL) {		\
7202ac6454SAndrew Thompson 	  (ifq)->ifq_tail = (m);		\
7302ac6454SAndrew Thompson       }						\
7402ac6454SAndrew Thompson       (ifq)->ifq_head = (m);			\
7502ac6454SAndrew Thompson       (ifq)->ifq_len++;				\
7602ac6454SAndrew Thompson   } while (0)
7702ac6454SAndrew Thompson 
7802ac6454SAndrew Thompson #define	USB_IF_QFULL(ifq)   ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
7902ac6454SAndrew Thompson #define	USB_IF_QLEN(ifq)    ((ifq)->ifq_len)
8002ac6454SAndrew Thompson #define	USB_IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
8102ac6454SAndrew Thompson 
8202ac6454SAndrew Thompson #define	USB_MBUF_RESET(m) do {			\
8302ac6454SAndrew Thompson     (m)->cur_data_ptr = (m)->min_data_ptr;	\
8402ac6454SAndrew Thompson     (m)->cur_data_len = (m)->max_data_len;	\
8502ac6454SAndrew Thompson     (m)->last_packet = 0;			\
8602ac6454SAndrew Thompson   } while (0)
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson /* prototypes */
89a593f6b8SAndrew Thompson void   *usb_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq,
90f9cb546cSAndrew Thompson 	    usb_size_t block_size, uint16_t nblocks);
9102ac6454SAndrew Thompson 
9275973647SAndrew Thompson #endif					/* _USB_MBUF_H_ */
93