1184610Salfred/*
2184610Salfred * ubtbcmfw.c
3184610Salfred */
4184610Salfred
5184610Salfred/*-
6187866Semax * Copyright (c) 2003-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7184610Salfred * All rights reserved.
8184610Salfred *
9184610Salfred * Redistribution and use in source and binary forms, with or without
10184610Salfred * modification, are permitted provided that the following conditions
11184610Salfred * are met:
12184610Salfred * 1. Redistributions of source code must retain the above copyright
13184610Salfred *    notice, this list of conditions and the following disclaimer.
14184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
15184610Salfred *    notice, this list of conditions and the following disclaimer in the
16184610Salfred *    documentation and/or other materials provided with the distribution.
17184610Salfred *
18184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28184610Salfred * SUCH DAMAGE.
29184610Salfred *
30184610Salfred * $Id: ubtbcmfw.c,v 1.3 2003/10/10 19:15:08 max Exp $
31184610Salfred * $FreeBSD$
32184610Salfred */
33184610Salfred
34194677Sthompsa#include <sys/stdint.h>
35194677Sthompsa#include <sys/stddef.h>
36194677Sthompsa#include <sys/param.h>
37194677Sthompsa#include <sys/queue.h>
38194677Sthompsa#include <sys/types.h>
39194677Sthompsa#include <sys/systm.h>
40194677Sthompsa#include <sys/kernel.h>
41194677Sthompsa#include <sys/bus.h>
42194677Sthompsa#include <sys/module.h>
43194677Sthompsa#include <sys/lock.h>
44194677Sthompsa#include <sys/mutex.h>
45194677Sthompsa#include <sys/condvar.h>
46194677Sthompsa#include <sys/sysctl.h>
47194677Sthompsa#include <sys/sx.h>
48194677Sthompsa#include <sys/unistd.h>
49194677Sthompsa#include <sys/callout.h>
50194677Sthompsa#include <sys/malloc.h>
51194677Sthompsa#include <sys/priv.h>
52194677Sthompsa#include <sys/conf.h>
53194677Sthompsa#include <sys/fcntl.h>
54194677Sthompsa
55188746Sthompsa#include "usbdevs.h"
56188942Sthompsa#include <dev/usb/usb.h>
57194677Sthompsa#include <dev/usb/usbdi.h>
58188942Sthompsa#include <dev/usb/usb_ioctl.h>
59184610Salfred
60194228Sthompsa#define	USB_DEBUG_VAR usb_debug
61188942Sthompsa#include <dev/usb/usb_debug.h>
62188942Sthompsa#include <dev/usb/usb_dev.h>
63184610Salfred
64184610Salfred/*
65184610Salfred * Download firmware to BCM2033.
66184610Salfred */
67184610Salfred
68184610Salfred#define	UBTBCMFW_CONFIG_NO	1	/* Config number */
69184610Salfred#define	UBTBCMFW_IFACE_IDX	0	/* Control interface */
70184610Salfred
71187866Semax#define	UBTBCMFW_BSIZE		1024
72187866Semax#define	UBTBCMFW_IFQ_MAXLEN	2
73187866Semax
74187259Sthompsaenum {
75187866Semax	UBTBCMFW_BULK_DT_WR = 0,
76187866Semax	UBTBCMFW_INTR_DT_RD,
77187866Semax	UBTBCMFW_N_TRANSFER,
78187259Sthompsa};
79187259Sthompsa
80184610Salfredstruct ubtbcmfw_softc {
81192984Sthompsa	struct usb_device	*sc_udev;
82187866Semax	struct mtx		sc_mtx;
83192984Sthompsa	struct usb_xfer	*sc_xfer[UBTBCMFW_N_TRANSFER];
84192984Sthompsa	struct usb_fifo_sc	sc_fifo;
85184610Salfred};
86184610Salfred
87187866Semax/*
88187866Semax * Prototypes
89187866Semax */
90184610Salfred
91187866Semaxstatic device_probe_t		ubtbcmfw_probe;
92187866Semaxstatic device_attach_t		ubtbcmfw_attach;
93187866Semaxstatic device_detach_t		ubtbcmfw_detach;
94184610Salfred
95193045Sthompsastatic usb_callback_t		ubtbcmfw_write_callback;
96193045Sthompsastatic usb_callback_t		ubtbcmfw_read_callback;
97184610Salfred
98193045Sthompsastatic usb_fifo_close_t	ubtbcmfw_close;
99193045Sthompsastatic usb_fifo_cmd_t		ubtbcmfw_start_read;
100193045Sthompsastatic usb_fifo_cmd_t		ubtbcmfw_start_write;
101193045Sthompsastatic usb_fifo_cmd_t		ubtbcmfw_stop_read;
102193045Sthompsastatic usb_fifo_cmd_t		ubtbcmfw_stop_write;
103193045Sthompsastatic usb_fifo_ioctl_t	ubtbcmfw_ioctl;
104193045Sthompsastatic usb_fifo_open_t		ubtbcmfw_open;
105184610Salfred
106192984Sthompsastatic struct usb_fifo_methods	ubtbcmfw_fifo_methods =
107187866Semax{
108187866Semax	.f_close =		&ubtbcmfw_close,
109187866Semax	.f_ioctl =		&ubtbcmfw_ioctl,
110187866Semax	.f_open =		&ubtbcmfw_open,
111187866Semax	.f_start_read =		&ubtbcmfw_start_read,
112187866Semax	.f_start_write =	&ubtbcmfw_start_write,
113187866Semax	.f_stop_read =		&ubtbcmfw_stop_read,
114187866Semax	.f_stop_write =		&ubtbcmfw_stop_write,
115187866Semax	.basename[0] =		"ubtbcmfw",
116187866Semax	.basename[1] =		"ubtbcmfw",
117187866Semax	.basename[2] =		"ubtbcmfw",
118187866Semax	.postfix[0] =		"",
119187866Semax	.postfix[1] =		".1",
120187866Semax	.postfix[2] =		".2",
121184610Salfred};
122184610Salfred
123187866Semax/*
124187866Semax * Device's config structure
125187866Semax */
126184610Salfred
127192984Sthompsastatic const struct usb_config	ubtbcmfw_config[UBTBCMFW_N_TRANSFER] =
128187866Semax{
129187259Sthompsa	[UBTBCMFW_BULK_DT_WR] = {
130187866Semax		.type =		UE_BULK,
131187866Semax		.endpoint =	0x02,	/* fixed */
132187866Semax		.direction =	UE_DIR_OUT,
133187866Semax		.if_index =	UBTBCMFW_IFACE_IDX,
134190734Sthompsa		.bufsize =	UBTBCMFW_BSIZE,
135190734Sthompsa		.flags =	{ .pipe_bof = 1, .force_short_xfer = 1,
136187866Semax				  .proxy_buffer = 1, },
137190734Sthompsa		.callback =	&ubtbcmfw_write_callback,
138184610Salfred	},
139184610Salfred
140187866Semax	[UBTBCMFW_INTR_DT_RD] = {
141187866Semax		.type =		UE_INTERRUPT,
142187866Semax		.endpoint =	0x01,	/* fixed */
143187866Semax		.direction =	UE_DIR_IN,
144187866Semax		.if_index =	UBTBCMFW_IFACE_IDX,
145190734Sthompsa		.bufsize =	UBTBCMFW_BSIZE,
146190734Sthompsa		.flags =	{ .pipe_bof = 1, .short_xfer_ok = 1,
147187866Semax				  .proxy_buffer = 1, },
148190734Sthompsa		.callback =	&ubtbcmfw_read_callback,
149184610Salfred	},
150184610Salfred};
151184610Salfred
152184610Salfred/*
153184610Salfred * Module
154184610Salfred */
155184610Salfred
156187866Semaxstatic devclass_t	ubtbcmfw_devclass;
157184610Salfred
158187866Semaxstatic device_method_t	ubtbcmfw_methods[] =
159187866Semax{
160184610Salfred	DEVMETHOD(device_probe, ubtbcmfw_probe),
161184610Salfred	DEVMETHOD(device_attach, ubtbcmfw_attach),
162184610Salfred	DEVMETHOD(device_detach, ubtbcmfw_detach),
163184610Salfred	{0, 0}
164184610Salfred};
165184610Salfred
166187866Semaxstatic driver_t		ubtbcmfw_driver =
167187866Semax{
168187866Semax	.name =		"ubtbcmfw",
169187866Semax	.methods =	ubtbcmfw_methods,
170187866Semax	.size =		sizeof(struct ubtbcmfw_softc),
171184610Salfred};
172184610Salfred
173292080Simpstatic const STRUCT_USB_HOST_ID ubtbcmfw_devs[] = {
174292080Simp/* Broadcom BCM2033 devices only */
175292080Simp	{ USB_VPI(USB_VENDOR_BROADCOM, USB_PRODUCT_BROADCOM_BCM2033, 0) },
176292080Simp};
177292080Simp
178292080Simp
179189275SthompsaDRIVER_MODULE(ubtbcmfw, uhub, ubtbcmfw_driver, ubtbcmfw_devclass, NULL, 0);
180188942SthompsaMODULE_DEPEND(ubtbcmfw, usb, 1, 1, 1);
181292080SimpUSB_PNP_HOST_INFO(ubtbcmfw_devs);
182184610Salfred
183184610Salfred/*
184184610Salfred * Probe for a USB Bluetooth device
185184610Salfred */
186184610Salfred
187184610Salfredstatic int
188184610Salfredubtbcmfw_probe(device_t dev)
189184610Salfred{
190192984Sthompsa	struct usb_attach_arg	*uaa = device_get_ivars(dev);
191187866Semax
192192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
193184610Salfred		return (ENXIO);
194187866Semax
195184610Salfred	if (uaa->info.bIfaceIndex != 0)
196184610Salfred		return (ENXIO);
197184610Salfred
198292080Simp	return (usbd_lookup_id_by_uaa(ubtbcmfw_devs, sizeof(ubtbcmfw_devs), uaa));
199187866Semax} /* ubtbcmfw_probe */
200184610Salfred
201184610Salfred/*
202184610Salfred * Attach the device
203184610Salfred */
204184610Salfred
205184610Salfredstatic int
206184610Salfredubtbcmfw_attach(device_t dev)
207184610Salfred{
208192984Sthompsa	struct usb_attach_arg	*uaa = device_get_ivars(dev);
209187866Semax	struct ubtbcmfw_softc	*sc = device_get_softc(dev);
210187866Semax	uint8_t			iface_index;
211187866Semax	int			error;
212184610Salfred
213184610Salfred	sc->sc_udev = uaa->device;
214184610Salfred
215194228Sthompsa	device_set_usb_desc(dev);
216184610Salfred
217184610Salfred	mtx_init(&sc->sc_mtx, "ubtbcmfw lock", NULL, MTX_DEF | MTX_RECURSE);
218184610Salfred
219184610Salfred	iface_index = UBTBCMFW_IFACE_IDX;
220194228Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
221187866Semax				ubtbcmfw_config, UBTBCMFW_N_TRANSFER,
222187866Semax				sc, &sc->sc_mtx);
223187866Semax	if (error != 0) {
224187866Semax		device_printf(dev, "allocating USB transfers failed. %s\n",
225194228Sthompsa			usbd_errstr(error));
226184610Salfred		goto detach;
227184610Salfred	}
228187866Semax
229194228Sthompsa	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
230187866Semax			&ubtbcmfw_fifo_methods, &sc->sc_fifo,
231189110Sthompsa			device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex,
232189110Sthompsa			UID_ROOT, GID_OPERATOR, 0644);
233187866Semax	if (error != 0) {
234187866Semax		device_printf(dev, "could not attach fifo. %s\n",
235194228Sthompsa			usbd_errstr(error));
236184610Salfred		goto detach;
237184610Salfred	}
238184610Salfred
239187866Semax	return (0);	/* success */
240187866Semax
241184610Salfreddetach:
242184610Salfred	ubtbcmfw_detach(dev);
243184610Salfred
244187866Semax	return (ENXIO);	/* failure */
245187866Semax} /* ubtbcmfw_attach */
246187866Semax
247184610Salfred/*
248184610Salfred * Detach the device
249184610Salfred */
250184610Salfred
251184610Salfredstatic int
252184610Salfredubtbcmfw_detach(device_t dev)
253184610Salfred{
254187866Semax	struct ubtbcmfw_softc	*sc = device_get_softc(dev);
255184610Salfred
256194228Sthompsa	usb_fifo_detach(&sc->sc_fifo);
257184610Salfred
258194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, UBTBCMFW_N_TRANSFER);
259184610Salfred
260184610Salfred	mtx_destroy(&sc->sc_mtx);
261184610Salfred
262184610Salfred	return (0);
263187866Semax} /* ubtbcmfw_detach */
264184610Salfred
265187866Semax/*
266187866Semax * USB write callback
267187866Semax */
268187866Semax
269184610Salfredstatic void
270194677Sthompsaubtbcmfw_write_callback(struct usb_xfer *xfer, usb_error_t error)
271184610Salfred{
272194677Sthompsa	struct ubtbcmfw_softc	*sc = usbd_xfer_softc(xfer);
273192984Sthompsa	struct usb_fifo	*f = sc->sc_fifo.fp[USB_FIFO_TX];
274194677Sthompsa	struct usb_page_cache	*pc;
275187866Semax	uint32_t		actlen;
276184610Salfred
277184610Salfred	switch (USB_GET_STATE(xfer)) {
278187866Semax	case USB_ST_SETUP:
279184610Salfred	case USB_ST_TRANSFERRED:
280187866Semaxsetup_next:
281194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
282194677Sthompsa		if (usb_fifo_get_data(f, pc, 0, usbd_xfer_max_len(xfer),
283194677Sthompsa			    &actlen, 0)) {
284194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, actlen);
285194228Sthompsa			usbd_transfer_submit(xfer);
286184610Salfred		}
287187866Semax		break;
288184610Salfred
289187866Semax	default: /* Error */
290194677Sthompsa		if (error != USB_ERR_CANCELLED) {
291184610Salfred			/* try to clear stall first */
292194677Sthompsa			usbd_xfer_set_stall(xfer);
293187866Semax			goto setup_next;
294184610Salfred		}
295187866Semax		break;
296184610Salfred	}
297187866Semax} /* ubtbcmfw_write_callback */
298184610Salfred
299187866Semax/*
300187866Semax * USB read callback
301187866Semax */
302184610Salfred
303184610Salfredstatic void
304194677Sthompsaubtbcmfw_read_callback(struct usb_xfer *xfer, usb_error_t error)
305184610Salfred{
306194677Sthompsa	struct ubtbcmfw_softc	*sc = usbd_xfer_softc(xfer);
307192984Sthompsa	struct usb_fifo	*fifo = sc->sc_fifo.fp[USB_FIFO_RX];
308194677Sthompsa	struct usb_page_cache	*pc;
309194677Sthompsa	int			actlen;
310184610Salfred
311194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
312194677Sthompsa
313184610Salfred	switch (USB_GET_STATE(xfer)) {
314184610Salfred	case USB_ST_TRANSFERRED:
315194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
316194677Sthompsa		usb_fifo_put_data(fifo, pc, 0, actlen, 1);
317187866Semax		/* FALLTHROUGH */
318184610Salfred
319184610Salfred	case USB_ST_SETUP:
320187866Semaxsetup_next:
321194228Sthompsa		if (usb_fifo_put_bytes_max(fifo) > 0) {
322194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
323194228Sthompsa			usbd_transfer_submit(xfer);
324184610Salfred		}
325187866Semax		break;
326184610Salfred
327187866Semax	default: /* Error */
328194677Sthompsa		if (error != USB_ERR_CANCELLED) {
329184610Salfred			/* try to clear stall first */
330194677Sthompsa			usbd_xfer_set_stall(xfer);
331187866Semax			goto setup_next;
332184610Salfred		}
333187866Semax		break;
334184610Salfred	}
335187866Semax} /* ubtbcmfw_read_callback */
336184610Salfred
337187866Semax/*
338187866Semax * Called when we about to start read()ing from the device
339187866Semax */
340184610Salfred
341184610Salfredstatic void
342192984Sthompsaubtbcmfw_start_read(struct usb_fifo *fifo)
343184610Salfred{
344194677Sthompsa	struct ubtbcmfw_softc	*sc = usb_fifo_softc(fifo);
345184610Salfred
346194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UBTBCMFW_INTR_DT_RD]);
347187866Semax} /* ubtbcmfw_start_read */
348184610Salfred
349187866Semax/*
350187866Semax * Called when we about to stop reading (i.e. closing fifo)
351187866Semax */
352187866Semax
353184610Salfredstatic void
354192984Sthompsaubtbcmfw_stop_read(struct usb_fifo *fifo)
355184610Salfred{
356194677Sthompsa	struct ubtbcmfw_softc	*sc = usb_fifo_softc(fifo);
357184610Salfred
358194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[UBTBCMFW_INTR_DT_RD]);
359187866Semax} /* ubtbcmfw_stop_read */
360184610Salfred
361187866Semax/*
362187866Semax * Called when we about to start write()ing to the device, poll()ing
363187866Semax * for write or flushing fifo
364187866Semax */
365187866Semax
366184610Salfredstatic void
367192984Sthompsaubtbcmfw_start_write(struct usb_fifo *fifo)
368184610Salfred{
369194677Sthompsa	struct ubtbcmfw_softc	*sc = usb_fifo_softc(fifo);
370184610Salfred
371194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UBTBCMFW_BULK_DT_WR]);
372187866Semax} /* ubtbcmfw_start_write */
373184610Salfred
374187866Semax/*
375187866Semax * Called when we about to stop writing (i.e. closing fifo)
376187866Semax */
377187866Semax
378184610Salfredstatic void
379192984Sthompsaubtbcmfw_stop_write(struct usb_fifo *fifo)
380184610Salfred{
381194677Sthompsa	struct ubtbcmfw_softc	*sc = usb_fifo_softc(fifo);
382184610Salfred
383194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[UBTBCMFW_BULK_DT_WR]);
384187866Semax} /* ubtbcmfw_stop_write */
385184610Salfred
386187866Semax/*
387187866Semax * Called when fifo is open
388187866Semax */
389187866Semax
390184610Salfredstatic int
391192984Sthompsaubtbcmfw_open(struct usb_fifo *fifo, int fflags)
392184610Salfred{
393194677Sthompsa	struct ubtbcmfw_softc	*sc = usb_fifo_softc(fifo);
394192984Sthompsa	struct usb_xfer	*xfer;
395184610Salfred
396187866Semax	/*
397187866Semax	 * f_open fifo method can only be called with either FREAD
398187866Semax	 * or FWRITE flag set at one time.
399187866Semax	 */
400187866Semax
401187866Semax	if (fflags & FREAD)
402187866Semax		xfer = sc->sc_xfer[UBTBCMFW_INTR_DT_RD];
403187866Semax	else if (fflags & FWRITE)
404187866Semax		xfer = sc->sc_xfer[UBTBCMFW_BULK_DT_WR];
405187866Semax	else
406187994Salfred		return (EINVAL);	/* should not happen */
407187994Salfred
408194677Sthompsa	if (usb_fifo_alloc_buffer(fifo, usbd_xfer_max_len(xfer),
409187866Semax			UBTBCMFW_IFQ_MAXLEN) != 0)
410187866Semax		return (ENOMEM);
411187866Semax
412184610Salfred	return (0);
413187866Semax} /* ubtbcmfw_open */
414184610Salfred
415187866Semax/*
416187866Semax * Called when fifo is closed
417187866Semax */
418187866Semax
419184610Salfredstatic void
420192984Sthompsaubtbcmfw_close(struct usb_fifo *fifo, int fflags)
421184610Salfred{
422187866Semax	if (fflags & (FREAD | FWRITE))
423194228Sthompsa		usb_fifo_free_buffer(fifo);
424187866Semax} /* ubtbcmfw_close */
425184610Salfred
426187866Semax/*
427187866Semax * Process ioctl() on USB device
428187866Semax */
429187866Semax
430184610Salfredstatic int
431192984Sthompsaubtbcmfw_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
432189110Sthompsa    int fflags)
433184610Salfred{
434194677Sthompsa	struct ubtbcmfw_softc	*sc = usb_fifo_softc(fifo);
435187866Semax	int			error = 0;
436184610Salfred
437184610Salfred	switch (cmd) {
438184610Salfred	case USB_GET_DEVICE_DESC:
439194228Sthompsa		memcpy(data, usbd_get_device_descriptor(sc->sc_udev),
440192984Sthompsa			sizeof(struct usb_device_descriptor));
441184610Salfred		break;
442184610Salfred
443184610Salfred	default:
444184610Salfred		error = EINVAL;
445184610Salfred		break;
446184610Salfred	}
447187866Semax
448184610Salfred	return (error);
449187866Semax} /* ubtbcmfw_ioctl */
450