1184610Salfred/* $FreeBSD: releng/10.2/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
27246122Shselasky#ifdef USB_GLOBAL_INCLUDE_FILE
28246122Shselasky#include USB_GLOBAL_INCLUDE_FILE
29246122Shselasky#else
30194677Sthompsa#include <sys/stdint.h>
31194677Sthompsa#include <sys/stddef.h>
32194677Sthompsa#include <sys/param.h>
33194677Sthompsa#include <sys/queue.h>
34194677Sthompsa#include <sys/types.h>
35194677Sthompsa#include <sys/systm.h>
36194677Sthompsa#include <sys/kernel.h>
37194677Sthompsa#include <sys/bus.h>
38194677Sthompsa#include <sys/module.h>
39194677Sthompsa#include <sys/lock.h>
40194677Sthompsa#include <sys/mutex.h>
41194677Sthompsa#include <sys/condvar.h>
42194677Sthompsa#include <sys/sysctl.h>
43194677Sthompsa#include <sys/sx.h>
44194677Sthompsa#include <sys/unistd.h>
45194677Sthompsa#include <sys/callout.h>
46194677Sthompsa#include <sys/malloc.h>
47194677Sthompsa#include <sys/priv.h>
48194677Sthompsa
49194677Sthompsa#include <dev/usb/usb.h>
50194677Sthompsa#include <dev/usb/usbdi.h>
51194677Sthompsa#include <dev/usb/usb_dev.h>
52188942Sthompsa#include <dev/usb/usb_mbuf.h>
53246122Shselasky#endif			/* USB_GLOBAL_INCLUDE_FILE */
54184610Salfred
55184610Salfred/*------------------------------------------------------------------------*
56194228Sthompsa *      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   *
63194228Sthompsausb_alloc_mbufs(struct malloc_type *type, struct usb_ifqueue *ifq,
64193074Sthompsa    usb_size_t block_size, uint16_t nblocks)
65184610Salfred{
66192984Sthompsa	struct usb_mbuf *m_ptr;
67184610Salfred	uint8_t *data_ptr;
68184610Salfred	void *free_ptr = NULL;
69193074Sthompsa	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
76192984Sthompsa		alloc_size = (block_size + sizeof(struct usb_mbuf)) * nblocks;
77184610Salfred
78184610Salfred		free_ptr = malloc(alloc_size, type, M_WAITOK | M_ZERO);
79184610Salfred
80184610Salfred		if (free_ptr == NULL) {
81184610Salfred			goto done;
82184610Salfred		}
83184610Salfred		m_ptr = free_ptr;
84184610Salfred		data_ptr = (void *)(m_ptr + nblocks);
85184610Salfred
86184610Salfred		while (nblocks--) {
87184610Salfred
88184610Salfred			m_ptr->cur_data_ptr =
89184610Salfred			    m_ptr->min_data_ptr = data_ptr;
90184610Salfred
91184610Salfred			m_ptr->cur_data_len =
92184610Salfred			    m_ptr->max_data_len = block_size;
93184610Salfred
94184610Salfred			USB_IF_ENQUEUE(ifq, m_ptr);
95184610Salfred
96184610Salfred			m_ptr++;
97184610Salfred			data_ptr += block_size;
98184610Salfred		}
99184610Salfred	}
100184610Salfreddone:
101184610Salfred	return (free_ptr);
102184610Salfred}
103