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 29d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE 30d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE 31d2b99310SHans Petter Selasky #else 32ed6d949aSAndrew Thompson #include <sys/stdint.h> 33ed6d949aSAndrew Thompson #include <sys/stddef.h> 34ed6d949aSAndrew Thompson #include <sys/param.h> 35ed6d949aSAndrew Thompson #include <sys/queue.h> 36ed6d949aSAndrew Thompson #include <sys/types.h> 37ed6d949aSAndrew Thompson #include <sys/systm.h> 38ed6d949aSAndrew Thompson #include <sys/kernel.h> 39ed6d949aSAndrew Thompson #include <sys/bus.h> 40ed6d949aSAndrew Thompson #include <sys/module.h> 41ed6d949aSAndrew Thompson #include <sys/lock.h> 42ed6d949aSAndrew Thompson #include <sys/mutex.h> 43ed6d949aSAndrew Thompson #include <sys/condvar.h> 44ed6d949aSAndrew Thompson #include <sys/sysctl.h> 45ed6d949aSAndrew Thompson #include <sys/sx.h> 46ed6d949aSAndrew Thompson #include <sys/unistd.h> 47ed6d949aSAndrew Thompson #include <sys/callout.h> 48ed6d949aSAndrew Thompson #include <sys/malloc.h> 49ed6d949aSAndrew Thompson #include <sys/priv.h> 50ed6d949aSAndrew Thompson 51ed6d949aSAndrew Thompson #include <dev/usb/usb.h> 52ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 53ed6d949aSAndrew Thompson #include <dev/usb/usb_dev.h> 5402ac6454SAndrew Thompson #include <dev/usb/usb_mbuf.h> 55d2b99310SHans Petter Selasky #endif /* USB_GLOBAL_INCLUDE_FILE */ 5602ac6454SAndrew Thompson 5702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 58a593f6b8SAndrew Thompson * usb_alloc_mbufs - allocate mbufs to an usbd interface queue 5902ac6454SAndrew Thompson * 6002ac6454SAndrew Thompson * Returns: 6102ac6454SAndrew Thompson * A pointer that should be passed to "free()" when the buffer(s) 6202ac6454SAndrew Thompson * should be released. 6302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 6402ac6454SAndrew Thompson void * 65a593f6b8SAndrew Thompson usb_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq, 66f9cb546cSAndrew Thompson usb_size_t block_size, uint16_t nblocks) 6702ac6454SAndrew Thompson { 68760bc48eSAndrew Thompson struct usb_mbuf *m_ptr; 6902ac6454SAndrew Thompson uint8_t *data_ptr; 7002ac6454SAndrew Thompson void *free_ptr = NULL; 71f9cb546cSAndrew Thompson usb_size_t alloc_size; 7202ac6454SAndrew Thompson 7302ac6454SAndrew Thompson /* align data */ 7402ac6454SAndrew Thompson block_size += ((-block_size) & (USB_HOST_ALIGN - 1)); 7502ac6454SAndrew Thompson 7602ac6454SAndrew Thompson if (nblocks && block_size) { 7702ac6454SAndrew Thompson 78760bc48eSAndrew Thompson alloc_size = (block_size + sizeof(struct usb_mbuf)) * nblocks; 7902ac6454SAndrew Thompson 8002ac6454SAndrew Thompson free_ptr = malloc(alloc_size, type, M_WAITOK | M_ZERO); 8102ac6454SAndrew Thompson 8202ac6454SAndrew Thompson if (free_ptr == NULL) { 8302ac6454SAndrew Thompson goto done; 8402ac6454SAndrew Thompson } 8502ac6454SAndrew Thompson m_ptr = free_ptr; 8602ac6454SAndrew Thompson data_ptr = (void *)(m_ptr + nblocks); 8702ac6454SAndrew Thompson 8802ac6454SAndrew Thompson while (nblocks--) { 8902ac6454SAndrew Thompson 9002ac6454SAndrew Thompson m_ptr->cur_data_ptr = 9102ac6454SAndrew Thompson m_ptr->min_data_ptr = data_ptr; 9202ac6454SAndrew Thompson 9302ac6454SAndrew Thompson m_ptr->cur_data_len = 9402ac6454SAndrew Thompson m_ptr->max_data_len = block_size; 9502ac6454SAndrew Thompson 9602ac6454SAndrew Thompson USB_IF_ENQUEUE(ifq, m_ptr); 9702ac6454SAndrew Thompson 9802ac6454SAndrew Thompson m_ptr++; 9902ac6454SAndrew Thompson data_ptr += block_size; 10002ac6454SAndrew Thompson } 10102ac6454SAndrew Thompson } 10202ac6454SAndrew Thompson done: 10302ac6454SAndrew Thompson return (free_ptr); 10402ac6454SAndrew Thompson } 105