usb_mbuf.h revision 193074
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_mbuf.h 193074 2009-05-30 00:22:57Z thompsa $ */
2184610Salfred/*-
3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4184610Salfred *
5184610Salfred * Redistribution and use in source and binary forms, with or without
6184610Salfred * modification, are permitted provided that the following conditions
7184610Salfred * are met:
8184610Salfred * 1. Redistributions of source code must retain the above copyright
9184610Salfred *    notice, this list of conditions and the following disclaimer.
10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer in the
12184610Salfred *    documentation and/or other materials provided with the distribution.
13184610Salfred *
14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24184610Salfred * SUCH DAMAGE.
25184610Salfred */
26184610Salfred
27184610Salfred#ifndef _USB2_MBUF_H_
28184610Salfred#define	_USB2_MBUF_H_
29184610Salfred
30184610Salfred/*
31184610Salfred * The following structure defines a minimum re-implementation of the
32184610Salfred * mbuf system in the kernel.
33184610Salfred */
34192984Sthompsastruct usb_mbuf {
35184610Salfred	uint8_t *cur_data_ptr;
36184610Salfred	uint8_t *min_data_ptr;
37192984Sthompsa	struct usb_mbuf *usb2_nextpkt;
38192984Sthompsa	struct usb_mbuf *usb2_next;
39184610Salfred
40193074Sthompsa	usb_size_t cur_data_len;
41193074Sthompsa	usb_size_t max_data_len;
42187183Sthompsa	uint8_t last_packet:1;
43187183Sthompsa	uint8_t unused:7;
44184610Salfred};
45184610Salfred
46184610Salfred/*
47184610Salfred * The following structure defines a minimum re-implementation of the
48184610Salfred * ifqueue structure in the kernel.
49184610Salfred */
50192984Sthompsastruct usb_ifqueue {
51192984Sthompsa	struct usb_mbuf *ifq_head;
52192984Sthompsa	struct usb_mbuf *ifq_tail;
53184610Salfred
54193074Sthompsa	usb_size_t ifq_len;
55193074Sthompsa	usb_size_t ifq_maxlen;
56184610Salfred};
57184610Salfred
58184610Salfred#define	USB_IF_ENQUEUE(ifq, m) do {		\
59184610Salfred    (m)->usb2_nextpkt = NULL;			\
60184610Salfred    if ((ifq)->ifq_tail == NULL)		\
61184610Salfred        (ifq)->ifq_head = (m);			\
62184610Salfred    else					\
63184610Salfred        (ifq)->ifq_tail->usb2_nextpkt = (m);	\
64184610Salfred    (ifq)->ifq_tail = (m);			\
65184610Salfred    (ifq)->ifq_len++;				\
66184610Salfred  } while (0)
67184610Salfred
68184610Salfred#define	USB_IF_DEQUEUE(ifq, m) do {				\
69184610Salfred    (m) = (ifq)->ifq_head;					\
70184610Salfred    if (m) {							\
71184610Salfred        if (((ifq)->ifq_head = (m)->usb2_nextpkt) == NULL) {	\
72184610Salfred	     (ifq)->ifq_tail = NULL;				\
73184610Salfred	}							\
74184610Salfred	(m)->usb2_nextpkt = NULL;				\
75184610Salfred	(ifq)->ifq_len--;					\
76184610Salfred    }								\
77184610Salfred  } while (0)
78184610Salfred
79184610Salfred#define	USB_IF_PREPEND(ifq, m) do {		\
80184610Salfred      (m)->usb2_nextpkt = (ifq)->ifq_head;	\
81184610Salfred      if ((ifq)->ifq_tail == NULL) {		\
82184610Salfred	  (ifq)->ifq_tail = (m);		\
83184610Salfred      }						\
84184610Salfred      (ifq)->ifq_head = (m);			\
85184610Salfred      (ifq)->ifq_len++;				\
86184610Salfred  } while (0)
87184610Salfred
88184610Salfred#define	USB_IF_QFULL(ifq)   ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
89184610Salfred#define	USB_IF_QLEN(ifq)    ((ifq)->ifq_len)
90184610Salfred#define	USB_IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
91184610Salfred
92184610Salfred#define	USB_MBUF_RESET(m) do {			\
93184610Salfred    (m)->cur_data_ptr = (m)->min_data_ptr;	\
94184610Salfred    (m)->cur_data_len = (m)->max_data_len;	\
95184610Salfred    (m)->last_packet = 0;			\
96184610Salfred  } while (0)
97184610Salfred
98184610Salfred/* prototypes */
99192984Sthompsavoid   *usb2_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq,
100193074Sthompsa	    usb_size_t block_size, uint16_t nblocks);
101184610Salfred
102184610Salfred#endif					/* _USB2_MBUF_H_ */
103