1 /*
2 * ubtbcmfw.c
3 */
4
5 /*-
6 * Copyright (c) 2003-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $Id: ubtbcmfw.c,v 1.3 2003/10/10 19:15:08 max Exp $
31 * $FreeBSD: head/sys/netgraph/bluetooth/drivers/ubtbcmfw/ubtbcmfw.c 223486 2011-06-24 02:30:02Z hselasky $
32 */
33
34 #include <sys/stdint.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/bus.h>
41 #include <sys/module.h>
42 #include <sys/lock.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/caps.h>
49 #include <sys/conf.h>
50 #include <sys/fcntl.h>
51
52 #include "usbdevs.h"
53 #include <bus/u4b/usb.h>
54 #include <bus/u4b/usbdi.h>
55 #include <bus/u4b/usb_ioctl.h>
56
57 #define USB_DEBUG_VAR usb_debug
58 #include <bus/u4b/usb_debug.h>
59 #include <bus/u4b/usb_dev.h>
60
61 /*
62 * Download firmware to BCM2033.
63 */
64
65 #define UBTBCMFW_CONFIG_NO 1 /* Config number */
66 #define UBTBCMFW_IFACE_IDX 0 /* Control interface */
67
68 #define UBTBCMFW_BSIZE 1024
69 #define UBTBCMFW_IFQ_MAXLEN 2
70
71 enum {
72 UBTBCMFW_BULK_DT_WR = 0,
73 UBTBCMFW_INTR_DT_RD,
74 UBTBCMFW_N_TRANSFER,
75 };
76
77 struct ubtbcmfw_softc {
78 struct usb_device *sc_udev;
79 struct lock sc_lock;
80 struct usb_xfer *sc_xfer[UBTBCMFW_N_TRANSFER];
81 struct usb_fifo_sc sc_fifo;
82 };
83
84 /*
85 * Prototypes
86 */
87
88 static device_probe_t ubtbcmfw_probe;
89 static device_attach_t ubtbcmfw_attach;
90 static device_detach_t ubtbcmfw_detach;
91
92 static usb_callback_t ubtbcmfw_write_callback;
93 static usb_callback_t ubtbcmfw_read_callback;
94
95 static usb_fifo_close_t ubtbcmfw_close;
96 static usb_fifo_cmd_t ubtbcmfw_start_read;
97 static usb_fifo_cmd_t ubtbcmfw_start_write;
98 static usb_fifo_cmd_t ubtbcmfw_stop_read;
99 static usb_fifo_cmd_t ubtbcmfw_stop_write;
100 static usb_fifo_ioctl_t ubtbcmfw_ioctl;
101 static usb_fifo_open_t ubtbcmfw_open;
102
103 static struct usb_fifo_methods ubtbcmfw_fifo_methods =
104 {
105 .f_close = &ubtbcmfw_close,
106 .f_ioctl = &ubtbcmfw_ioctl,
107 .f_open = &ubtbcmfw_open,
108 .f_start_read = &ubtbcmfw_start_read,
109 .f_start_write = &ubtbcmfw_start_write,
110 .f_stop_read = &ubtbcmfw_stop_read,
111 .f_stop_write = &ubtbcmfw_stop_write,
112 .basename[0] = "ubtbcmfw",
113 .basename[1] = "ubtbcmfw",
114 .basename[2] = "ubtbcmfw",
115 .postfix[0] = "",
116 .postfix[1] = ".1",
117 .postfix[2] = ".2",
118 };
119
120 /*
121 * Device's config structure
122 */
123
124 static const struct usb_config ubtbcmfw_config[UBTBCMFW_N_TRANSFER] =
125 {
126 [UBTBCMFW_BULK_DT_WR] = {
127 .type = UE_BULK,
128 .endpoint = 0x02, /* fixed */
129 .direction = UE_DIR_OUT,
130 .if_index = UBTBCMFW_IFACE_IDX,
131 .bufsize = UBTBCMFW_BSIZE,
132 .flags = { .pipe_bof = 1, .force_short_xfer = 1,
133 .proxy_buffer = 1, },
134 .callback = &ubtbcmfw_write_callback,
135 },
136
137 [UBTBCMFW_INTR_DT_RD] = {
138 .type = UE_INTERRUPT,
139 .endpoint = 0x01, /* fixed */
140 .direction = UE_DIR_IN,
141 .if_index = UBTBCMFW_IFACE_IDX,
142 .bufsize = UBTBCMFW_BSIZE,
143 .flags = { .pipe_bof = 1, .short_xfer_ok = 1,
144 .proxy_buffer = 1, },
145 .callback = &ubtbcmfw_read_callback,
146 },
147 };
148
149 /*
150 * Module
151 */
152
153 static devclass_t ubtbcmfw_devclass;
154
155 static device_method_t ubtbcmfw_methods[] =
156 {
157 DEVMETHOD(device_probe, ubtbcmfw_probe),
158 DEVMETHOD(device_attach, ubtbcmfw_attach),
159 DEVMETHOD(device_detach, ubtbcmfw_detach),
160
161 DEVMETHOD_END
162 };
163
164 static driver_t ubtbcmfw_driver =
165 {
166 .name = "ubtbcmfw",
167 .methods = ubtbcmfw_methods,
168 .size = sizeof(struct ubtbcmfw_softc),
169 };
170
171 DRIVER_MODULE(ubtbcmfw, uhub, ubtbcmfw_driver, ubtbcmfw_devclass, NULL, NULL);
172 MODULE_DEPEND(ubtbcmfw, usb, 1, 1, 1);
173
174 /*
175 * Probe for a USB Bluetooth device
176 */
177
178 static int
ubtbcmfw_probe(device_t dev)179 ubtbcmfw_probe(device_t dev)
180 {
181 static const STRUCT_USB_HOST_ID devs[] = {
182 /* Broadcom BCM2033 devices only */
183 { USB_VPI(USB_VENDOR_BROADCOM, USB_PRODUCT_BROADCOM_BCM2033, 0) },
184 };
185
186 struct usb_attach_arg *uaa = device_get_ivars(dev);
187
188 if (uaa->usb_mode != USB_MODE_HOST)
189 return (ENXIO);
190
191 if (uaa->info.bIfaceIndex != 0)
192 return (ENXIO);
193
194 return (usbd_lookup_id_by_uaa(devs, sizeof(devs), uaa));
195 } /* ubtbcmfw_probe */
196
197 /*
198 * Attach the device
199 */
200
201 static int
ubtbcmfw_attach(device_t dev)202 ubtbcmfw_attach(device_t dev)
203 {
204 struct usb_attach_arg *uaa = device_get_ivars(dev);
205 struct ubtbcmfw_softc *sc = device_get_softc(dev);
206 uint8_t iface_index;
207 int error;
208
209 sc->sc_udev = uaa->device;
210
211 device_set_usb_desc(dev);
212
213 lockinit(&sc->sc_lock, "ubtbcmfw lock", 0, LK_CANRECURSE);
214
215 iface_index = UBTBCMFW_IFACE_IDX;
216 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
217 ubtbcmfw_config, UBTBCMFW_N_TRANSFER,
218 sc, &sc->sc_lock);
219 if (error != 0) {
220 device_printf(dev, "allocating USB transfers failed. %s\n",
221 usbd_errstr(error));
222 goto detach;
223 }
224
225 error = usb_fifo_attach(uaa->device, sc, &sc->sc_lock,
226 &ubtbcmfw_fifo_methods, &sc->sc_fifo,
227 device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex,
228 UID_ROOT, GID_OPERATOR, 0644);
229 if (error != 0) {
230 device_printf(dev, "could not attach fifo. %s\n",
231 usbd_errstr(error));
232 goto detach;
233 }
234
235 return (0); /* success */
236
237 detach:
238 ubtbcmfw_detach(dev);
239
240 return (ENXIO); /* failure */
241 } /* ubtbcmfw_attach */
242
243 /*
244 * Detach the device
245 */
246
247 static int
ubtbcmfw_detach(device_t dev)248 ubtbcmfw_detach(device_t dev)
249 {
250 struct ubtbcmfw_softc *sc = device_get_softc(dev);
251
252 usb_fifo_detach(&sc->sc_fifo);
253
254 usbd_transfer_unsetup(sc->sc_xfer, UBTBCMFW_N_TRANSFER);
255
256 lockuninit(&sc->sc_lock);
257
258 return (0);
259 } /* ubtbcmfw_detach */
260
261 /*
262 * USB write callback
263 */
264
265 static void
ubtbcmfw_write_callback(struct usb_xfer * xfer,usb_error_t error)266 ubtbcmfw_write_callback(struct usb_xfer *xfer, usb_error_t error)
267 {
268 struct ubtbcmfw_softc *sc = usbd_xfer_softc(xfer);
269 struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX];
270 struct usb_page_cache *pc;
271 uint32_t actlen;
272
273 switch (USB_GET_STATE(xfer)) {
274 case USB_ST_SETUP:
275 case USB_ST_TRANSFERRED:
276 setup_next:
277 pc = usbd_xfer_get_frame(xfer, 0);
278 if (usb_fifo_get_data(f, pc, 0, usbd_xfer_max_len(xfer),
279 &actlen, 0)) {
280 usbd_xfer_set_frame_len(xfer, 0, actlen);
281 usbd_transfer_submit(xfer);
282 }
283 break;
284
285 default: /* Error */
286 if (error != USB_ERR_CANCELLED) {
287 /* try to clear stall first */
288 usbd_xfer_set_stall(xfer);
289 goto setup_next;
290 }
291 break;
292 }
293 } /* ubtbcmfw_write_callback */
294
295 /*
296 * USB read callback
297 */
298
299 static void
ubtbcmfw_read_callback(struct usb_xfer * xfer,usb_error_t error)300 ubtbcmfw_read_callback(struct usb_xfer *xfer, usb_error_t error)
301 {
302 struct ubtbcmfw_softc *sc = usbd_xfer_softc(xfer);
303 struct usb_fifo *fifo = sc->sc_fifo.fp[USB_FIFO_RX];
304 struct usb_page_cache *pc;
305 int actlen;
306
307 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
308
309 switch (USB_GET_STATE(xfer)) {
310 case USB_ST_TRANSFERRED:
311 pc = usbd_xfer_get_frame(xfer, 0);
312 usb_fifo_put_data(fifo, pc, 0, actlen, 1);
313 /* FALLTHROUGH */
314
315 case USB_ST_SETUP:
316 setup_next:
317 if (usb_fifo_put_bytes_max(fifo) > 0) {
318 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
319 usbd_transfer_submit(xfer);
320 }
321 break;
322
323 default: /* Error */
324 if (error != USB_ERR_CANCELLED) {
325 /* try to clear stall first */
326 usbd_xfer_set_stall(xfer);
327 goto setup_next;
328 }
329 break;
330 }
331 } /* ubtbcmfw_read_callback */
332
333 /*
334 * Called when we about to start read()ing from the device
335 */
336
337 static void
ubtbcmfw_start_read(struct usb_fifo * fifo)338 ubtbcmfw_start_read(struct usb_fifo *fifo)
339 {
340 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
341
342 usbd_transfer_start(sc->sc_xfer[UBTBCMFW_INTR_DT_RD]);
343 } /* ubtbcmfw_start_read */
344
345 /*
346 * Called when we about to stop reading (i.e. closing fifo)
347 */
348
349 static void
ubtbcmfw_stop_read(struct usb_fifo * fifo)350 ubtbcmfw_stop_read(struct usb_fifo *fifo)
351 {
352 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
353
354 usbd_transfer_stop(sc->sc_xfer[UBTBCMFW_INTR_DT_RD]);
355 } /* ubtbcmfw_stop_read */
356
357 /*
358 * Called when we about to start write()ing to the device, poll()ing
359 * for write or flushing fifo
360 */
361
362 static void
ubtbcmfw_start_write(struct usb_fifo * fifo)363 ubtbcmfw_start_write(struct usb_fifo *fifo)
364 {
365 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
366
367 usbd_transfer_start(sc->sc_xfer[UBTBCMFW_BULK_DT_WR]);
368 } /* ubtbcmfw_start_write */
369
370 /*
371 * Called when we about to stop writing (i.e. closing fifo)
372 */
373
374 static void
ubtbcmfw_stop_write(struct usb_fifo * fifo)375 ubtbcmfw_stop_write(struct usb_fifo *fifo)
376 {
377 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
378
379 usbd_transfer_stop(sc->sc_xfer[UBTBCMFW_BULK_DT_WR]);
380 } /* ubtbcmfw_stop_write */
381
382 /*
383 * Called when fifo is open
384 */
385
386 static int
ubtbcmfw_open(struct usb_fifo * fifo,int fflags)387 ubtbcmfw_open(struct usb_fifo *fifo, int fflags)
388 {
389 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
390 struct usb_xfer *xfer;
391
392 /*
393 * f_open fifo method can only be called with either FREAD
394 * or FWRITE flag set at one time.
395 */
396
397 if (fflags & FREAD)
398 xfer = sc->sc_xfer[UBTBCMFW_INTR_DT_RD];
399 else if (fflags & FWRITE)
400 xfer = sc->sc_xfer[UBTBCMFW_BULK_DT_WR];
401 else
402 return (EINVAL); /* should not happen */
403
404 if (usb_fifo_alloc_buffer(fifo, usbd_xfer_max_len(xfer),
405 UBTBCMFW_IFQ_MAXLEN) != 0)
406 return (ENOMEM);
407
408 return (0);
409 } /* ubtbcmfw_open */
410
411 /*
412 * Called when fifo is closed
413 */
414
415 static void
ubtbcmfw_close(struct usb_fifo * fifo,int fflags)416 ubtbcmfw_close(struct usb_fifo *fifo, int fflags)
417 {
418 if (fflags & (FREAD | FWRITE))
419 usb_fifo_free_buffer(fifo);
420 } /* ubtbcmfw_close */
421
422 /*
423 * Process ioctl() on USB device
424 */
425
426 static int
ubtbcmfw_ioctl(struct usb_fifo * fifo,u_long cmd,void * data,int fflags)427 ubtbcmfw_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
428 int fflags)
429 {
430 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
431 int error = 0;
432
433 switch (cmd) {
434 case USB_GET_DEVICE_DESC:
435 memcpy(data, usbd_get_device_descriptor(sc->sc_udev),
436 sizeof(struct usb_device_descriptor));
437 break;
438
439 default:
440 error = EINVAL;
441 break;
442 }
443
444 return (error);
445 } /* ubtbcmfw_ioctl */
446