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$
32 */
33
34 #include <sys/stdint.h>
35 #include <sys/stddef.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/module.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/sx.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
51 #include <sys/priv.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54
55 #include "usbdevs.h"
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usb_ioctl.h>
59
60 #define USB_DEBUG_VAR usb_debug
61 #include <dev/usb/usb_debug.h>
62 #include <dev/usb/usb_dev.h>
63
64 /*
65 * Download firmware to BCM2033.
66 */
67
68 #define UBTBCMFW_CONFIG_NO 1 /* Config number */
69 #define UBTBCMFW_IFACE_IDX 0 /* Control interface */
70
71 #define UBTBCMFW_BSIZE 1024
72 #define UBTBCMFW_IFQ_MAXLEN 2
73
74 enum {
75 UBTBCMFW_BULK_DT_WR = 0,
76 UBTBCMFW_INTR_DT_RD,
77 UBTBCMFW_N_TRANSFER,
78 };
79
80 struct ubtbcmfw_softc {
81 struct usb_device *sc_udev;
82 struct mtx sc_mtx;
83 struct usb_xfer *sc_xfer[UBTBCMFW_N_TRANSFER];
84 struct usb_fifo_sc sc_fifo;
85 };
86
87 /*
88 * Prototypes
89 */
90
91 static device_probe_t ubtbcmfw_probe;
92 static device_attach_t ubtbcmfw_attach;
93 static device_detach_t ubtbcmfw_detach;
94
95 static usb_callback_t ubtbcmfw_write_callback;
96 static usb_callback_t ubtbcmfw_read_callback;
97
98 static usb_fifo_close_t ubtbcmfw_close;
99 static usb_fifo_cmd_t ubtbcmfw_start_read;
100 static usb_fifo_cmd_t ubtbcmfw_start_write;
101 static usb_fifo_cmd_t ubtbcmfw_stop_read;
102 static usb_fifo_cmd_t ubtbcmfw_stop_write;
103 static usb_fifo_ioctl_t ubtbcmfw_ioctl;
104 static usb_fifo_open_t ubtbcmfw_open;
105
106 static struct usb_fifo_methods ubtbcmfw_fifo_methods =
107 {
108 .f_close = &ubtbcmfw_close,
109 .f_ioctl = &ubtbcmfw_ioctl,
110 .f_open = &ubtbcmfw_open,
111 .f_start_read = &ubtbcmfw_start_read,
112 .f_start_write = &ubtbcmfw_start_write,
113 .f_stop_read = &ubtbcmfw_stop_read,
114 .f_stop_write = &ubtbcmfw_stop_write,
115 .basename[0] = "ubtbcmfw",
116 .basename[1] = "ubtbcmfw",
117 .basename[2] = "ubtbcmfw",
118 .postfix[0] = "",
119 .postfix[1] = ".1",
120 .postfix[2] = ".2",
121 };
122
123 /*
124 * Device's config structure
125 */
126
127 static const struct usb_config ubtbcmfw_config[UBTBCMFW_N_TRANSFER] =
128 {
129 [UBTBCMFW_BULK_DT_WR] = {
130 .type = UE_BULK,
131 .endpoint = 0x02, /* fixed */
132 .direction = UE_DIR_OUT,
133 .if_index = UBTBCMFW_IFACE_IDX,
134 .bufsize = UBTBCMFW_BSIZE,
135 .flags = { .pipe_bof = 1, .force_short_xfer = 1,
136 .proxy_buffer = 1, },
137 .callback = &ubtbcmfw_write_callback,
138 },
139
140 [UBTBCMFW_INTR_DT_RD] = {
141 .type = UE_INTERRUPT,
142 .endpoint = 0x01, /* fixed */
143 .direction = UE_DIR_IN,
144 .if_index = UBTBCMFW_IFACE_IDX,
145 .bufsize = UBTBCMFW_BSIZE,
146 .flags = { .pipe_bof = 1, .short_xfer_ok = 1,
147 .proxy_buffer = 1, },
148 .callback = &ubtbcmfw_read_callback,
149 },
150 };
151
152 /*
153 * Module
154 */
155
156 static devclass_t ubtbcmfw_devclass;
157
158 static device_method_t ubtbcmfw_methods[] =
159 {
160 DEVMETHOD(device_probe, ubtbcmfw_probe),
161 DEVMETHOD(device_attach, ubtbcmfw_attach),
162 DEVMETHOD(device_detach, ubtbcmfw_detach),
163 {0, 0}
164 };
165
166 static driver_t ubtbcmfw_driver =
167 {
168 .name = "ubtbcmfw",
169 .methods = ubtbcmfw_methods,
170 .size = sizeof(struct ubtbcmfw_softc),
171 };
172
173 static const STRUCT_USB_HOST_ID ubtbcmfw_devs[] = {
174 /* Broadcom BCM2033 devices only */
175 { USB_VPI(USB_VENDOR_BROADCOM, USB_PRODUCT_BROADCOM_BCM2033, 0) },
176 };
177
178
179 DRIVER_MODULE(ubtbcmfw, uhub, ubtbcmfw_driver, ubtbcmfw_devclass, NULL, 0);
180 MODULE_DEPEND(ubtbcmfw, usb, 1, 1, 1);
181 USB_PNP_HOST_INFO(ubtbcmfw_devs);
182
183 /*
184 * Probe for a USB Bluetooth device
185 */
186
187 static int
ubtbcmfw_probe(device_t dev)188 ubtbcmfw_probe(device_t dev)
189 {
190 struct usb_attach_arg *uaa = device_get_ivars(dev);
191
192 if (uaa->usb_mode != USB_MODE_HOST)
193 return (ENXIO);
194
195 if (uaa->info.bIfaceIndex != 0)
196 return (ENXIO);
197
198 return (usbd_lookup_id_by_uaa(ubtbcmfw_devs, sizeof(ubtbcmfw_devs), uaa));
199 } /* ubtbcmfw_probe */
200
201 /*
202 * Attach the device
203 */
204
205 static int
ubtbcmfw_attach(device_t dev)206 ubtbcmfw_attach(device_t dev)
207 {
208 struct usb_attach_arg *uaa = device_get_ivars(dev);
209 struct ubtbcmfw_softc *sc = device_get_softc(dev);
210 uint8_t iface_index;
211 int error;
212
213 sc->sc_udev = uaa->device;
214
215 device_set_usb_desc(dev);
216
217 mtx_init(&sc->sc_mtx, "ubtbcmfw lock", NULL, MTX_DEF | MTX_RECURSE);
218
219 iface_index = UBTBCMFW_IFACE_IDX;
220 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
221 ubtbcmfw_config, UBTBCMFW_N_TRANSFER,
222 sc, &sc->sc_mtx);
223 if (error != 0) {
224 device_printf(dev, "allocating USB transfers failed. %s\n",
225 usbd_errstr(error));
226 goto detach;
227 }
228
229 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
230 &ubtbcmfw_fifo_methods, &sc->sc_fifo,
231 device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex,
232 UID_ROOT, GID_OPERATOR, 0644);
233 if (error != 0) {
234 device_printf(dev, "could not attach fifo. %s\n",
235 usbd_errstr(error));
236 goto detach;
237 }
238
239 return (0); /* success */
240
241 detach:
242 ubtbcmfw_detach(dev);
243
244 return (ENXIO); /* failure */
245 } /* ubtbcmfw_attach */
246
247 /*
248 * Detach the device
249 */
250
251 static int
ubtbcmfw_detach(device_t dev)252 ubtbcmfw_detach(device_t dev)
253 {
254 struct ubtbcmfw_softc *sc = device_get_softc(dev);
255
256 usb_fifo_detach(&sc->sc_fifo);
257
258 usbd_transfer_unsetup(sc->sc_xfer, UBTBCMFW_N_TRANSFER);
259
260 mtx_destroy(&sc->sc_mtx);
261
262 return (0);
263 } /* ubtbcmfw_detach */
264
265 /*
266 * USB write callback
267 */
268
269 static void
ubtbcmfw_write_callback(struct usb_xfer * xfer,usb_error_t error)270 ubtbcmfw_write_callback(struct usb_xfer *xfer, usb_error_t error)
271 {
272 struct ubtbcmfw_softc *sc = usbd_xfer_softc(xfer);
273 struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX];
274 struct usb_page_cache *pc;
275 uint32_t actlen;
276
277 switch (USB_GET_STATE(xfer)) {
278 case USB_ST_SETUP:
279 case USB_ST_TRANSFERRED:
280 setup_next:
281 pc = usbd_xfer_get_frame(xfer, 0);
282 if (usb_fifo_get_data(f, pc, 0, usbd_xfer_max_len(xfer),
283 &actlen, 0)) {
284 usbd_xfer_set_frame_len(xfer, 0, actlen);
285 usbd_transfer_submit(xfer);
286 }
287 break;
288
289 default: /* Error */
290 if (error != USB_ERR_CANCELLED) {
291 /* try to clear stall first */
292 usbd_xfer_set_stall(xfer);
293 goto setup_next;
294 }
295 break;
296 }
297 } /* ubtbcmfw_write_callback */
298
299 /*
300 * USB read callback
301 */
302
303 static void
ubtbcmfw_read_callback(struct usb_xfer * xfer,usb_error_t error)304 ubtbcmfw_read_callback(struct usb_xfer *xfer, usb_error_t error)
305 {
306 struct ubtbcmfw_softc *sc = usbd_xfer_softc(xfer);
307 struct usb_fifo *fifo = sc->sc_fifo.fp[USB_FIFO_RX];
308 struct usb_page_cache *pc;
309 int actlen;
310
311 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
312
313 switch (USB_GET_STATE(xfer)) {
314 case USB_ST_TRANSFERRED:
315 pc = usbd_xfer_get_frame(xfer, 0);
316 usb_fifo_put_data(fifo, pc, 0, actlen, 1);
317 /* FALLTHROUGH */
318
319 case USB_ST_SETUP:
320 setup_next:
321 if (usb_fifo_put_bytes_max(fifo) > 0) {
322 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
323 usbd_transfer_submit(xfer);
324 }
325 break;
326
327 default: /* Error */
328 if (error != USB_ERR_CANCELLED) {
329 /* try to clear stall first */
330 usbd_xfer_set_stall(xfer);
331 goto setup_next;
332 }
333 break;
334 }
335 } /* ubtbcmfw_read_callback */
336
337 /*
338 * Called when we about to start read()ing from the device
339 */
340
341 static void
ubtbcmfw_start_read(struct usb_fifo * fifo)342 ubtbcmfw_start_read(struct usb_fifo *fifo)
343 {
344 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
345
346 usbd_transfer_start(sc->sc_xfer[UBTBCMFW_INTR_DT_RD]);
347 } /* ubtbcmfw_start_read */
348
349 /*
350 * Called when we about to stop reading (i.e. closing fifo)
351 */
352
353 static void
ubtbcmfw_stop_read(struct usb_fifo * fifo)354 ubtbcmfw_stop_read(struct usb_fifo *fifo)
355 {
356 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
357
358 usbd_transfer_stop(sc->sc_xfer[UBTBCMFW_INTR_DT_RD]);
359 } /* ubtbcmfw_stop_read */
360
361 /*
362 * Called when we about to start write()ing to the device, poll()ing
363 * for write or flushing fifo
364 */
365
366 static void
ubtbcmfw_start_write(struct usb_fifo * fifo)367 ubtbcmfw_start_write(struct usb_fifo *fifo)
368 {
369 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
370
371 usbd_transfer_start(sc->sc_xfer[UBTBCMFW_BULK_DT_WR]);
372 } /* ubtbcmfw_start_write */
373
374 /*
375 * Called when we about to stop writing (i.e. closing fifo)
376 */
377
378 static void
ubtbcmfw_stop_write(struct usb_fifo * fifo)379 ubtbcmfw_stop_write(struct usb_fifo *fifo)
380 {
381 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
382
383 usbd_transfer_stop(sc->sc_xfer[UBTBCMFW_BULK_DT_WR]);
384 } /* ubtbcmfw_stop_write */
385
386 /*
387 * Called when fifo is open
388 */
389
390 static int
ubtbcmfw_open(struct usb_fifo * fifo,int fflags)391 ubtbcmfw_open(struct usb_fifo *fifo, int fflags)
392 {
393 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
394 struct usb_xfer *xfer;
395
396 /*
397 * f_open fifo method can only be called with either FREAD
398 * or FWRITE flag set at one time.
399 */
400
401 if (fflags & FREAD)
402 xfer = sc->sc_xfer[UBTBCMFW_INTR_DT_RD];
403 else if (fflags & FWRITE)
404 xfer = sc->sc_xfer[UBTBCMFW_BULK_DT_WR];
405 else
406 return (EINVAL); /* should not happen */
407
408 if (usb_fifo_alloc_buffer(fifo, usbd_xfer_max_len(xfer),
409 UBTBCMFW_IFQ_MAXLEN) != 0)
410 return (ENOMEM);
411
412 return (0);
413 } /* ubtbcmfw_open */
414
415 /*
416 * Called when fifo is closed
417 */
418
419 static void
ubtbcmfw_close(struct usb_fifo * fifo,int fflags)420 ubtbcmfw_close(struct usb_fifo *fifo, int fflags)
421 {
422 if (fflags & (FREAD | FWRITE))
423 usb_fifo_free_buffer(fifo);
424 } /* ubtbcmfw_close */
425
426 /*
427 * Process ioctl() on USB device
428 */
429
430 static int
ubtbcmfw_ioctl(struct usb_fifo * fifo,u_long cmd,void * data,int fflags)431 ubtbcmfw_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
432 int fflags)
433 {
434 struct ubtbcmfw_softc *sc = usb_fifo_softc(fifo);
435 int error = 0;
436
437 switch (cmd) {
438 case USB_GET_DEVICE_DESC:
439 memcpy(data, usbd_get_device_descriptor(sc->sc_udev),
440 sizeof(struct usb_device_descriptor));
441 break;
442
443 default:
444 error = EINVAL;
445 break;
446 }
447
448 return (error);
449 } /* ubtbcmfw_ioctl */
450