1300829Sgrehan/*-
2300829Sgrehan * Copyright (c) 2014 Nahanni Systems Inc.
3300829Sgrehan * All rights reserved.
4300829Sgrehan *
5300829Sgrehan * Redistribution and use in source and binary forms, with or without
6300829Sgrehan * modification, are permitted provided that the following conditions
7300829Sgrehan * are met:
8300829Sgrehan * 1. Redistributions of source code must retain the above copyright
9300829Sgrehan *    notice, this list of conditions and the following disclaimer.
10300829Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11300829Sgrehan *    notice, this list of conditions and the following disclaimer in the
12300829Sgrehan *    documentation and/or other materials provided with the distribution.
13300829Sgrehan *
14300829Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15300829Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16300829Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17300829Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18300829Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19300829Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20300829Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21300829Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22300829Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23300829Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24300829Sgrehan * SUCH DAMAGE.
25300829Sgrehan */
26300829Sgrehan
27300829Sgrehan#include <sys/cdefs.h>
28300829Sgrehan__FBSDID("$FreeBSD: releng/11.0/usr.sbin/bhyve/usb_emul.c 302408 2016-07-08 00:04:57Z gjb $");
29300829Sgrehan
30300829Sgrehan#include <sys/types.h>
31300829Sgrehan#include <sys/queue.h>
32300829Sgrehan
33300829Sgrehan#include <assert.h>
34300829Sgrehan#include <stdio.h>
35300829Sgrehan#include <stdlib.h>
36300829Sgrehan#include <string.h>
37300829Sgrehan#include <pthread.h>
38300829Sgrehan
39300829Sgrehan#include "usb_emul.h"
40300829Sgrehan
41300829SgrehanSET_DECLARE(usb_emu_set, struct usb_devemu);
42300829Sgrehan
43300829Sgrehanstruct usb_devemu *
44300829Sgrehanusb_emu_finddev(char *name)
45300829Sgrehan{
46300829Sgrehan	struct usb_devemu **udpp, *udp;
47300829Sgrehan
48300829Sgrehan	SET_FOREACH(udpp, usb_emu_set) {
49300829Sgrehan		udp = *udpp;
50300829Sgrehan		if (!strcmp(udp->ue_emu, name))
51300829Sgrehan			return (udp);
52300829Sgrehan	}
53300829Sgrehan
54300829Sgrehan	return (NULL);
55300829Sgrehan}
56300829Sgrehan
57300829Sgrehanstruct usb_data_xfer_block *
58300829Sgrehanusb_data_xfer_append(struct usb_data_xfer *xfer, void *buf, int blen,
59300829Sgrehan                     void *hci_data, int ccs)
60300829Sgrehan{
61300829Sgrehan	struct usb_data_xfer_block *xb;
62300829Sgrehan
63300829Sgrehan	if (xfer->ndata >= USB_MAX_XFER_BLOCKS)
64300829Sgrehan		return (NULL);
65300829Sgrehan
66300829Sgrehan	xb = &xfer->data[xfer->tail];
67300829Sgrehan	xb->buf = buf;
68300829Sgrehan	xb->blen = blen;
69300829Sgrehan	xb->hci_data = hci_data;
70300829Sgrehan	xb->ccs = ccs;
71300829Sgrehan	xb->processed = 0;
72300829Sgrehan	xb->bdone = 0;
73300829Sgrehan	xfer->ndata++;
74300829Sgrehan	xfer->tail = (xfer->tail + 1) % USB_MAX_XFER_BLOCKS;
75300829Sgrehan	return (xb);
76300829Sgrehan}
77