1f9f848faSopenharmony_ci/*- 2f9f848faSopenharmony_ci * SPDX-License-Identifier: BSD-2-Clause 3f9f848faSopenharmony_ci * 4f9f848faSopenharmony_ci * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 5f9f848faSopenharmony_ci * 6f9f848faSopenharmony_ci * Redistribution and use in source and binary forms, with or without 7f9f848faSopenharmony_ci * modification, are permitted provided that the following conditions 8f9f848faSopenharmony_ci * are met: 9f9f848faSopenharmony_ci * 1. Redistributions of source code must retain the above copyright 10f9f848faSopenharmony_ci * notice, this list of conditions and the following disclaimer. 11f9f848faSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 12f9f848faSopenharmony_ci * notice, this list of conditions and the following disclaimer in the 13f9f848faSopenharmony_ci * documentation and/or other materials provided with the distribution. 14f9f848faSopenharmony_ci * 15f9f848faSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16f9f848faSopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17f9f848faSopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18f9f848faSopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19f9f848faSopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20f9f848faSopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21f9f848faSopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22f9f848faSopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23f9f848faSopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24f9f848faSopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25f9f848faSopenharmony_ci * SUCH DAMAGE. 26f9f848faSopenharmony_ci */ 27f9f848faSopenharmony_ci 28f9f848faSopenharmony_ci#ifndef _USB_MBUF_H_ 29f9f848faSopenharmony_ci#define _USB_MBUF_H_ 30f9f848faSopenharmony_ci 31f9f848faSopenharmony_ci/* 32f9f848faSopenharmony_ci * The following structure defines a minimum re-implementation of the 33f9f848faSopenharmony_ci * mbuf system in the kernel. 34f9f848faSopenharmony_ci */ 35f9f848faSopenharmony_cistruct usb_mbuf { 36f9f848faSopenharmony_ci uint8_t *cur_data_ptr; 37f9f848faSopenharmony_ci uint8_t *min_data_ptr; 38f9f848faSopenharmony_ci struct usb_mbuf *usb_nextpkt; 39f9f848faSopenharmony_ci struct usb_mbuf *usb_next; 40f9f848faSopenharmony_ci 41f9f848faSopenharmony_ci usb_size_t cur_data_len; 42f9f848faSopenharmony_ci usb_size_t max_data_len; 43f9f848faSopenharmony_ci uint8_t last_packet:1; 44f9f848faSopenharmony_ci uint8_t unused:7; 45f9f848faSopenharmony_ci}; 46f9f848faSopenharmony_ci 47f9f848faSopenharmony_ci#define USB_IF_ENQUEUE(ifq, m) do { \ 48f9f848faSopenharmony_ci (m)->usb_nextpkt = NULL; \ 49f9f848faSopenharmony_ci if ((ifq)->ifq_tail == NULL) \ 50f9f848faSopenharmony_ci (ifq)->ifq_head = (m); \ 51f9f848faSopenharmony_ci else \ 52f9f848faSopenharmony_ci (ifq)->ifq_tail->usb_nextpkt = (m); \ 53f9f848faSopenharmony_ci (ifq)->ifq_tail = (m); \ 54f9f848faSopenharmony_ci (ifq)->ifq_len++; \ 55f9f848faSopenharmony_ci } while (0) 56f9f848faSopenharmony_ci 57f9f848faSopenharmony_ci#define USB_IF_DEQUEUE(ifq, m) do { \ 58f9f848faSopenharmony_ci (m) = (ifq)->ifq_head; \ 59f9f848faSopenharmony_ci if (m) { \ 60f9f848faSopenharmony_ci if (((ifq)->ifq_head = (m)->usb_nextpkt) == NULL) { \ 61f9f848faSopenharmony_ci (ifq)->ifq_tail = NULL; \ 62f9f848faSopenharmony_ci } \ 63f9f848faSopenharmony_ci (m)->usb_nextpkt = NULL; \ 64f9f848faSopenharmony_ci (ifq)->ifq_len--; \ 65f9f848faSopenharmony_ci } \ 66f9f848faSopenharmony_ci } while (0) 67f9f848faSopenharmony_ci 68f9f848faSopenharmony_ci#define USB_IF_PREPEND(ifq, m) do { \ 69f9f848faSopenharmony_ci (m)->usb_nextpkt = (ifq)->ifq_head; \ 70f9f848faSopenharmony_ci if ((ifq)->ifq_tail == NULL) { \ 71f9f848faSopenharmony_ci (ifq)->ifq_tail = (m); \ 72f9f848faSopenharmony_ci } \ 73f9f848faSopenharmony_ci (ifq)->ifq_head = (m); \ 74f9f848faSopenharmony_ci (ifq)->ifq_len++; \ 75f9f848faSopenharmony_ci } while (0) 76f9f848faSopenharmony_ci 77f9f848faSopenharmony_ci#define USB_IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen) 78f9f848faSopenharmony_ci#define USB_IF_QLEN(ifq) ((ifq)->ifq_len) 79f9f848faSopenharmony_ci#define USB_IF_POLL(ifq, m) ((m) = (ifq)->ifq_head) 80f9f848faSopenharmony_ci 81f9f848faSopenharmony_ci#define USB_MBUF_RESET(m) do { \ 82f9f848faSopenharmony_ci (m)->cur_data_ptr = (m)->min_data_ptr; \ 83f9f848faSopenharmony_ci (m)->cur_data_len = (m)->max_data_len; \ 84f9f848faSopenharmony_ci (m)->last_packet = 0; \ 85f9f848faSopenharmony_ci } while (0) 86f9f848faSopenharmony_ci 87f9f848faSopenharmony_ci/* prototypes */ 88f9f848faSopenharmony_civoid *usb_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq, 89f9f848faSopenharmony_ci usb_size_t block_size, uint16_t nblocks); 90f9f848faSopenharmony_ci 91f9f848faSopenharmony_ci#endif /* _USB_MBUF_H_ */ 92