xref: /freebsd/sys/dev/usb/usb_mbuf.c (revision a593f6b8de8b956bd0dd22f74805ede942e3d6a9)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1502ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1602ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1702ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1802ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1902ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2002ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2102ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2202ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2302ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2402ac6454SAndrew Thompson  * SUCH DAMAGE.
2502ac6454SAndrew Thompson  */
2602ac6454SAndrew Thompson 
2702ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
2802ac6454SAndrew Thompson #include <dev/usb/usb_mbuf.h>
2902ac6454SAndrew Thompson 
3002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
31a593f6b8SAndrew Thompson  *      usb_alloc_mbufs - allocate mbufs to an usbd interface queue
3202ac6454SAndrew Thompson  *
3302ac6454SAndrew Thompson  * Returns:
3402ac6454SAndrew Thompson  *   A pointer that should be passed to "free()" when the buffer(s)
3502ac6454SAndrew Thompson  *   should be released.
3602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
3702ac6454SAndrew Thompson void   *
38a593f6b8SAndrew Thompson usb_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq,
39f9cb546cSAndrew Thompson     usb_size_t block_size, uint16_t nblocks)
4002ac6454SAndrew Thompson {
41760bc48eSAndrew Thompson 	struct usb_mbuf *m_ptr;
4202ac6454SAndrew Thompson 	uint8_t *data_ptr;
4302ac6454SAndrew Thompson 	void *free_ptr = NULL;
44f9cb546cSAndrew Thompson 	usb_size_t alloc_size;
4502ac6454SAndrew Thompson 
4602ac6454SAndrew Thompson 	/* align data */
4702ac6454SAndrew Thompson 	block_size += ((-block_size) & (USB_HOST_ALIGN - 1));
4802ac6454SAndrew Thompson 
4902ac6454SAndrew Thompson 	if (nblocks && block_size) {
5002ac6454SAndrew Thompson 
51760bc48eSAndrew Thompson 		alloc_size = (block_size + sizeof(struct usb_mbuf)) * nblocks;
5202ac6454SAndrew Thompson 
5302ac6454SAndrew Thompson 		free_ptr = malloc(alloc_size, type, M_WAITOK | M_ZERO);
5402ac6454SAndrew Thompson 
5502ac6454SAndrew Thompson 		if (free_ptr == NULL) {
5602ac6454SAndrew Thompson 			goto done;
5702ac6454SAndrew Thompson 		}
5802ac6454SAndrew Thompson 		m_ptr = free_ptr;
5902ac6454SAndrew Thompson 		data_ptr = (void *)(m_ptr + nblocks);
6002ac6454SAndrew Thompson 
6102ac6454SAndrew Thompson 		while (nblocks--) {
6202ac6454SAndrew Thompson 
6302ac6454SAndrew Thompson 			m_ptr->cur_data_ptr =
6402ac6454SAndrew Thompson 			    m_ptr->min_data_ptr = data_ptr;
6502ac6454SAndrew Thompson 
6602ac6454SAndrew Thompson 			m_ptr->cur_data_len =
6702ac6454SAndrew Thompson 			    m_ptr->max_data_len = block_size;
6802ac6454SAndrew Thompson 
6902ac6454SAndrew Thompson 			USB_IF_ENQUEUE(ifq, m_ptr);
7002ac6454SAndrew Thompson 
7102ac6454SAndrew Thompson 			m_ptr++;
7202ac6454SAndrew Thompson 			data_ptr += block_size;
7302ac6454SAndrew Thompson 		}
7402ac6454SAndrew Thompson 	}
7502ac6454SAndrew Thompson done:
7602ac6454SAndrew Thompson 	return (free_ptr);
7702ac6454SAndrew Thompson }
78