usb_mbuf.c revision 193074
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_mbuf.c 193074 2009-05-30 00:22:57Z thompsa $ */
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#include <dev/usb/usb_core.h>
28188942Sthompsa#include <dev/usb/usb_mbuf.h>
29184610Salfred
30184610Salfred/*------------------------------------------------------------------------*
31184610Salfred *      usb2_alloc_mbufs - allocate mbufs to an usbd interface queue
32184610Salfred *
33184610Salfred * Returns:
34184610Salfred *   A pointer that should be passed to "free()" when the buffer(s)
35184610Salfred *   should be released.
36184610Salfred *------------------------------------------------------------------------*/
37184610Salfredvoid   *
38192984Sthompsausb2_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq,
39193074Sthompsa    usb_size_t block_size, uint16_t nblocks)
40184610Salfred{
41192984Sthompsa	struct usb_mbuf *m_ptr;
42184610Salfred	uint8_t *data_ptr;
43184610Salfred	void *free_ptr = NULL;
44193074Sthompsa	usb_size_t alloc_size;
45184610Salfred
46184610Salfred	/* align data */
47184610Salfred	block_size += ((-block_size) & (USB_HOST_ALIGN - 1));
48184610Salfred
49184610Salfred	if (nblocks && block_size) {
50184610Salfred
51192984Sthompsa		alloc_size = (block_size + sizeof(struct usb_mbuf)) * nblocks;
52184610Salfred
53184610Salfred		free_ptr = malloc(alloc_size, type, M_WAITOK | M_ZERO);
54184610Salfred
55184610Salfred		if (free_ptr == NULL) {
56184610Salfred			goto done;
57184610Salfred		}
58184610Salfred		m_ptr = free_ptr;
59184610Salfred		data_ptr = (void *)(m_ptr + nblocks);
60184610Salfred
61184610Salfred		while (nblocks--) {
62184610Salfred
63184610Salfred			m_ptr->cur_data_ptr =
64184610Salfred			    m_ptr->min_data_ptr = data_ptr;
65184610Salfred
66184610Salfred			m_ptr->cur_data_len =
67184610Salfred			    m_ptr->max_data_len = block_size;
68184610Salfred
69184610Salfred			USB_IF_ENQUEUE(ifq, m_ptr);
70184610Salfred
71184610Salfred			m_ptr++;
72184610Salfred			data_ptr += block_size;
73184610Salfred		}
74184610Salfred	}
75184610Salfreddone:
76184610Salfred	return (free_ptr);
77184610Salfred}
78