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