usb_mbuf.c revision 246122
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_mbuf.c 246122 2013-01-30 15:26:04Z hselasky $ */ 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 27188942Sthompsa#ifdef USB_GLOBAL_INCLUDE_FILE 28188942Sthompsa#include USB_GLOBAL_INCLUDE_FILE 29184610Salfred#else 30184610Salfred#include <sys/stdint.h> 31194228Sthompsa#include <sys/stddef.h> 32184610Salfred#include <sys/param.h> 33184610Salfred#include <sys/queue.h> 34184610Salfred#include <sys/types.h> 35184610Salfred#include <sys/systm.h> 36184610Salfred#include <sys/kernel.h> 37184610Salfred#include <sys/bus.h> 38194228Sthompsa#include <sys/module.h> 39193074Sthompsa#include <sys/lock.h> 40184610Salfred#include <sys/mutex.h> 41192984Sthompsa#include <sys/condvar.h> 42184610Salfred#include <sys/sysctl.h> 43184610Salfred#include <sys/sx.h> 44193074Sthompsa#include <sys/unistd.h> 45184610Salfred#include <sys/callout.h> 46184610Salfred#include <sys/malloc.h> 47184610Salfred#include <sys/priv.h> 48184610Salfred 49184610Salfred#include <dev/usb/usb.h> 50184610Salfred#include <dev/usb/usbdi.h> 51192984Sthompsa#include <dev/usb/usb_dev.h> 52184610Salfred#include <dev/usb/usb_mbuf.h> 53184610Salfred#endif /* USB_GLOBAL_INCLUDE_FILE */ 54184610Salfred 55184610Salfred/*------------------------------------------------------------------------* 56184610Salfred * usb_alloc_mbufs - allocate mbufs to an usbd interface queue 57184610Salfred * 58184610Salfred * Returns: 59184610Salfred * A pointer that should be passed to "free()" when the buffer(s) 60184610Salfred * should be released. 61184610Salfred *------------------------------------------------------------------------*/ 62184610Salfredvoid * 63184610Salfredusb_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq, 64184610Salfred usb_size_t block_size, uint16_t nblocks) 65184610Salfred{ 66184610Salfred struct usb_mbuf *m_ptr; 67184610Salfred uint8_t *data_ptr; 68184610Salfred void *free_ptr = NULL; 69184610Salfred usb_size_t alloc_size; 70184610Salfred 71184610Salfred /* align data */ 72184610Salfred block_size += ((-block_size) & (USB_HOST_ALIGN - 1)); 73184610Salfred 74184610Salfred if (nblocks && block_size) { 75184610Salfred 76184610Salfred alloc_size = (block_size + sizeof(struct usb_mbuf)) * nblocks; 77184610Salfred 78 free_ptr = malloc(alloc_size, type, M_WAITOK | M_ZERO); 79 80 if (free_ptr == NULL) { 81 goto done; 82 } 83 m_ptr = free_ptr; 84 data_ptr = (void *)(m_ptr + nblocks); 85 86 while (nblocks--) { 87 88 m_ptr->cur_data_ptr = 89 m_ptr->min_data_ptr = data_ptr; 90 91 m_ptr->cur_data_len = 92 m_ptr->max_data_len = block_size; 93 94 USB_IF_ENQUEUE(ifq, m_ptr); 95 96 m_ptr++; 97 data_ptr += block_size; 98 } 99 } 100done: 101 return (free_ptr); 102} 103