xref: /freebsd/sys/netgraph/bluetooth/drivers/ubtbcmfw/ubtbcmfw.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
1 /*
2  * ubtbcmfw.c
3  */
4 
5 /*-
6  * Copyright (c) 2003 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/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/filio.h>
39 #include <sys/fcntl.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/poll.h>
43 #include <sys/proc.h>
44 #include <sys/sysctl.h>
45 #include <sys/uio.h>
46 
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdi_util.h>
50 
51 #include "usbdevs.h"
52 
53 /* FreeBSD 7.0 defines */
54 
55 #define	USBBASEDEVICE	device_t
56 #define	USBDEVNAME	device_get_nameunit
57 #define USBDEVUNIT(bdev) device_get_unit(bdev)
58 
59 /*
60  * Download firmware to BCM2033.
61  */
62 
63 #define UBTBCMFW_CONFIG_NO	1	/* Config number */
64 #define UBTBCMFW_IFACE_IDX	0 	/* Control interface */
65 #define UBTBCMFW_INTR_IN_EP	0x81	/* Fixed endpoint */
66 #define UBTBCMFW_BULK_OUT_EP	0x02	/* Fixed endpoint */
67 #define UBTBCMFW_INTR_IN	UE_GET_ADDR(UBTBCMFW_INTR_IN_EP)
68 #define UBTBCMFW_BULK_OUT	UE_GET_ADDR(UBTBCMFW_BULK_OUT_EP)
69 
70 struct ubtbcmfw_softc {
71 	USBBASEDEVICE		sc_dev;			/* base device */
72 	usbd_device_handle	sc_udev;		/* USB device handle */
73 	struct cdev *sc_ctrl_dev;		/* control device */
74 	struct cdev *sc_intr_in_dev;		/* interrupt device */
75 	struct cdev *sc_bulk_out_dev;	/* bulk device */
76 	usbd_pipe_handle	sc_intr_in_pipe;	/* interrupt pipe */
77 	usbd_pipe_handle	sc_bulk_out_pipe;	/* bulk out pipe */
78 	int			sc_flags;
79 #define UBTBCMFW_CTRL_DEV	(1 << 0)
80 #define UBTBCMFW_INTR_IN_DEV	(1 << 1)
81 #define UBTBCMFW_BULK_OUT_DEV	(1 << 2)
82 	int			sc_refcnt;
83 	int			sc_dying;
84 };
85 
86 typedef struct ubtbcmfw_softc	*ubtbcmfw_softc_p;
87 
88 /*
89  * Device methods
90  */
91 
92 #define UBTBCMFW_UNIT(n)	((minor(n) >> 4) & 0xf)
93 #define UBTBCMFW_ENDPOINT(n)	(minor(n) & 0xf)
94 #define UBTBCMFW_MINOR(u, e)	(((u) << 4) | (e))
95 #define UBTBCMFW_BSIZE		1024
96 
97 static d_open_t		ubtbcmfw_open;
98 static d_close_t	ubtbcmfw_close;
99 static d_read_t		ubtbcmfw_read;
100 static d_write_t	ubtbcmfw_write;
101 static d_ioctl_t	ubtbcmfw_ioctl;
102 static d_poll_t		ubtbcmfw_poll;
103 
104 static struct cdevsw	ubtbcmfw_cdevsw = {
105 	.d_version =	D_VERSION,
106 	.d_flags =	D_NEEDGIANT,
107 	.d_open =	ubtbcmfw_open,
108 	.d_close =	ubtbcmfw_close,
109 	.d_read =	ubtbcmfw_read,
110 	.d_write =	ubtbcmfw_write,
111 	.d_ioctl =	ubtbcmfw_ioctl,
112 	.d_poll =	ubtbcmfw_poll,
113 	.d_name =	"ubtbcmfw",
114 };
115 
116 /*
117  * Module
118  */
119 
120 USB_DECLARE_DRIVER(ubtbcmfw);
121 DRIVER_MODULE(ubtbcmfw, uhub, ubtbcmfw_driver, ubtbcmfw_devclass,
122 	      usbd_driver_load, 0);
123 
124 /*
125  * Probe for a USB Bluetooth device
126  */
127 
128 USB_MATCH(ubtbcmfw)
129 {
130 #define	USB_PRODUCT_BROADCOM_BCM2033NF	0x2033
131 
132 	USB_MATCH_START(ubtbcmfw, uaa);
133 
134 	if (uaa->iface != NULL)
135 		return (UMATCH_NONE);
136 
137 	/* Match the boot device. */
138 	if (uaa->vendor == USB_VENDOR_BROADCOM &&
139 	    uaa->product == USB_PRODUCT_BROADCOM_BCM2033NF)
140 		return (UMATCH_VENDOR_PRODUCT);
141 
142 	return (UMATCH_NONE);
143 }
144 
145 /*
146  * Attach the device
147  */
148 
149 USB_ATTACH(ubtbcmfw)
150 {
151 	USB_ATTACH_START(ubtbcmfw, sc, uaa);
152 	usbd_interface_handle	iface;
153 	usbd_status		err;
154 	char			devinfo[1024];
155 
156 	sc->sc_udev = uaa->device;
157 	usbd_devinfo(sc->sc_udev, 0, devinfo);
158 	USB_ATTACH_SETUP;
159 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
160 
161 	sc->sc_ctrl_dev = sc->sc_intr_in_dev = sc->sc_bulk_out_dev = NULL;
162 	sc->sc_intr_in_pipe = sc->sc_bulk_out_pipe = NULL;
163 	sc->sc_flags = sc->sc_refcnt = sc->sc_dying = 0;
164 
165 	err = usbd_set_config_no(sc->sc_udev, UBTBCMFW_CONFIG_NO, 1);
166 	if (err) {
167 		printf("%s: setting config no failed. %s\n",
168 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
169 		goto bad;
170 	}
171 
172 	err = usbd_device2interface_handle(sc->sc_udev, UBTBCMFW_IFACE_IDX,
173 			&iface);
174 	if (err) {
175 		printf("%s: getting interface handle failed. %s\n",
176 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
177 		goto bad;
178 	}
179 
180 	/* Will be used as a bulk pipe */
181 	err = usbd_open_pipe(iface, UBTBCMFW_INTR_IN_EP, 0,
182 			&sc->sc_intr_in_pipe);
183 	if (err) {
184 		printf("%s: open intr in failed. %s\n",
185 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
186 		goto bad;
187 	}
188 
189 	err = usbd_open_pipe(iface, UBTBCMFW_BULK_OUT_EP, 0,
190 			&sc->sc_bulk_out_pipe);
191 	if (err) {
192 		printf("%s: open bulk out failed. %s\n",
193 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
194 		goto bad;
195 	}
196 
197 	/* Create device nodes */
198 	sc->sc_ctrl_dev = make_dev(&ubtbcmfw_cdevsw,
199 		UBTBCMFW_MINOR(USBDEVUNIT(sc->sc_dev), 0),
200 		UID_ROOT, GID_OPERATOR, 0644,
201 		"%s", USBDEVNAME(sc->sc_dev));
202 
203 	sc->sc_intr_in_dev = make_dev(&ubtbcmfw_cdevsw,
204 		UBTBCMFW_MINOR(USBDEVUNIT(sc->sc_dev), UBTBCMFW_INTR_IN),
205 		UID_ROOT, GID_OPERATOR, 0644,
206 		"%s.%d", USBDEVNAME(sc->sc_dev), UBTBCMFW_INTR_IN);
207 
208 	sc->sc_bulk_out_dev = make_dev(&ubtbcmfw_cdevsw,
209 		UBTBCMFW_MINOR(USBDEVUNIT(sc->sc_dev), UBTBCMFW_BULK_OUT),
210 		UID_ROOT, GID_OPERATOR, 0644,
211 		"%s.%d", USBDEVNAME(sc->sc_dev), UBTBCMFW_BULK_OUT);
212 
213 	USB_ATTACH_SUCCESS_RETURN;
214 bad:
215 	ubtbcmfw_detach(self);
216 
217         USB_ATTACH_ERROR_RETURN;
218 }
219 
220 /*
221  * Detach the device
222  */
223 
224 USB_DETACH(ubtbcmfw)
225 {
226 	USB_DETACH_START(ubtbcmfw, sc);
227 
228 	sc->sc_dying = 1;
229 
230 	if (-- sc->sc_refcnt >= 0) {
231 		if (sc->sc_intr_in_pipe != NULL)
232 			usbd_abort_pipe(sc->sc_intr_in_pipe);
233 
234 		if (sc->sc_bulk_out_pipe != NULL)
235 			usbd_abort_pipe(sc->sc_bulk_out_pipe);
236 
237 		usb_detach_wait(USBDEV(sc->sc_dev));
238 	}
239 
240 	/* Destroy device nodes */
241 	if (sc->sc_bulk_out_dev != NULL) {
242 		destroy_dev(sc->sc_bulk_out_dev);
243 		sc->sc_bulk_out_dev = NULL;
244 	}
245 
246 	if (sc->sc_intr_in_dev != NULL) {
247 		destroy_dev(sc->sc_intr_in_dev);
248 		sc->sc_intr_in_dev = NULL;
249 	}
250 
251 	if (sc->sc_ctrl_dev != NULL) {
252 		destroy_dev(sc->sc_ctrl_dev);
253 		sc->sc_ctrl_dev = NULL;
254 	}
255 
256 	/* Close pipes */
257 	if (sc->sc_intr_in_pipe != NULL) {
258 		usbd_close_pipe(sc->sc_intr_in_pipe);
259 		sc->sc_intr_in_pipe = NULL;
260 	}
261 
262 	if (sc->sc_bulk_out_pipe != NULL) {
263 		usbd_close_pipe(sc->sc_bulk_out_pipe);
264 		sc->sc_intr_in_pipe = NULL;
265 	}
266 
267 	return (0);
268 }
269 
270 /*
271  * Open endpoint device
272  * XXX FIXME softc locking
273  */
274 
275 static int
276 ubtbcmfw_open(struct cdev *dev, int flag, int mode, usb_proc_ptr p)
277 {
278 	ubtbcmfw_softc_p	sc = NULL;
279 	int			error = 0;
280 
281 	/* checks for sc != NULL */
282 	USB_GET_SC_OPEN(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
283 	if (sc->sc_dying)
284 		return (ENXIO);
285 
286 	switch (UBTBCMFW_ENDPOINT(dev)) {
287 	case USB_CONTROL_ENDPOINT:
288 		if (!(sc->sc_flags & UBTBCMFW_CTRL_DEV))
289 			sc->sc_flags |= UBTBCMFW_CTRL_DEV;
290 		else
291 			error = EBUSY;
292 		break;
293 
294 	case UBTBCMFW_INTR_IN:
295 		if (!(sc->sc_flags & UBTBCMFW_INTR_IN_DEV)) {
296 			if (sc->sc_intr_in_pipe != NULL)
297 				sc->sc_flags |= UBTBCMFW_INTR_IN_DEV;
298 			else
299 				error = ENXIO;
300 		} else
301 			error = EBUSY;
302 		break;
303 
304 	case UBTBCMFW_BULK_OUT:
305 		if (!(sc->sc_flags & UBTBCMFW_BULK_OUT_DEV)) {
306 			if (sc->sc_bulk_out_pipe != NULL)
307 				sc->sc_flags |= UBTBCMFW_BULK_OUT_DEV;
308 			else
309 				error = ENXIO;
310 		} else
311 			error = EBUSY;
312 		break;
313 
314 	default:
315 		error = ENXIO;
316 		break;
317 	}
318 
319 	return (error);
320 }
321 
322 /*
323  * Close endpoint device
324  * XXX FIXME softc locking
325  */
326 
327 static int
328 ubtbcmfw_close(struct cdev *dev, int flag, int mode, usb_proc_ptr p)
329 {
330 	ubtbcmfw_softc_p	sc = NULL;
331 
332 	USB_GET_SC(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
333 	if (sc == NULL)
334 		return (ENXIO);
335 
336 	switch (UBTBCMFW_ENDPOINT(dev)) {
337 	case USB_CONTROL_ENDPOINT:
338 		sc->sc_flags &= ~UBTBCMFW_CTRL_DEV;
339 		break;
340 
341 	case UBTBCMFW_INTR_IN:
342 		if (sc->sc_intr_in_pipe != NULL)
343 			usbd_abort_pipe(sc->sc_intr_in_pipe);
344 
345 		sc->sc_flags &= ~UBTBCMFW_INTR_IN_DEV;
346 		break;
347 
348 	case UBTBCMFW_BULK_OUT:
349 		if (sc->sc_bulk_out_pipe != NULL)
350 			usbd_abort_pipe(sc->sc_bulk_out_pipe);
351 
352 		sc->sc_flags &= ~UBTBCMFW_BULK_OUT_DEV;
353 		break;
354 	}
355 
356 	return (0);
357 }
358 
359 /*
360  * Read from the endpoint device
361  * XXX FIXME softc locking
362  */
363 
364 static int
365 ubtbcmfw_read(struct cdev *dev, struct uio *uio, int flag)
366 {
367 	ubtbcmfw_softc_p	sc = NULL;
368 	u_int8_t		buf[UBTBCMFW_BSIZE];
369 	usbd_xfer_handle	xfer;
370 	usbd_status		err;
371 	int			n, tn, error = 0;
372 
373 	USB_GET_SC(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
374 	if (sc == NULL || sc->sc_dying)
375 		return (ENXIO);
376 
377 	if (UBTBCMFW_ENDPOINT(dev) != UBTBCMFW_INTR_IN)
378 		return (EOPNOTSUPP);
379 	if (sc->sc_intr_in_pipe == NULL)
380 		return (ENXIO);
381 
382 	xfer = usbd_alloc_xfer(sc->sc_udev);
383 	if (xfer == NULL)
384 		return (ENOMEM);
385 
386 	sc->sc_refcnt ++;
387 
388 	while ((n = min(sizeof(buf), uio->uio_resid)) != 0) {
389 		tn = n;
390 		err = usbd_bulk_transfer(xfer, sc->sc_intr_in_pipe,
391 				USBD_SHORT_XFER_OK, USBD_DEFAULT_TIMEOUT,
392 				buf, &tn, "bcmrd");
393 		switch (err) {
394 		case USBD_NORMAL_COMPLETION:
395 			error = uiomove(buf, tn, uio);
396 			break;
397 
398 		case USBD_INTERRUPTED:
399 			error = EINTR;
400 			break;
401 
402 		case USBD_TIMEOUT:
403 			error = ETIMEDOUT;
404 			break;
405 
406 		default:
407 			error = EIO;
408 			break;
409 		}
410 
411 		if (error != 0 || tn < n)
412 			break;
413 	}
414 
415 	usbd_free_xfer(xfer);
416 
417 	if (-- sc->sc_refcnt < 0)
418 		usb_detach_wakeup(USBDEV(sc->sc_dev));
419 
420 	return (error);
421 }
422 
423 /*
424  * Write into the endpoint device
425  * XXX FIXME softc locking
426  */
427 
428 static int
429 ubtbcmfw_write(struct cdev *dev, struct uio *uio, int flag)
430 {
431 	ubtbcmfw_softc_p	sc = NULL;
432 	u_int8_t		buf[UBTBCMFW_BSIZE];
433 	usbd_xfer_handle	xfer;
434 	usbd_status		err;
435 	int			n, error = 0;
436 
437 	USB_GET_SC(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
438 	if (sc == NULL || sc->sc_dying)
439 		return (ENXIO);
440 
441 	if (UBTBCMFW_ENDPOINT(dev) != UBTBCMFW_BULK_OUT)
442 		return (EOPNOTSUPP);
443 	if (sc->sc_bulk_out_pipe == NULL)
444 		return (ENXIO);
445 
446 	xfer = usbd_alloc_xfer(sc->sc_udev);
447 	if (xfer == NULL)
448 		return (ENOMEM);
449 
450 	sc->sc_refcnt ++;
451 
452 	while ((n = min(sizeof(buf), uio->uio_resid)) != 0) {
453 		error = uiomove(buf, n, uio);
454 		if (error != 0)
455 			break;
456 
457 		err = usbd_bulk_transfer(xfer, sc->sc_bulk_out_pipe,
458 				0, USBD_DEFAULT_TIMEOUT, buf, &n, "bcmwr");
459 		switch (err) {
460 		case USBD_NORMAL_COMPLETION:
461 			break;
462 
463 		case USBD_INTERRUPTED:
464 			error = EINTR;
465 			break;
466 
467 		case USBD_TIMEOUT:
468 			error = ETIMEDOUT;
469 			break;
470 
471 		default:
472 			error = EIO;
473 			break;
474 		}
475 
476 		if (error != 0)
477 			break;
478 	}
479 
480 	usbd_free_xfer(xfer);
481 
482 	if (-- sc->sc_refcnt < 0)
483 		usb_detach_wakeup(USBDEV(sc->sc_dev));
484 
485 	return (error);
486 }
487 
488 /*
489  * Process ioctl on the endpoint device
490  * XXX FIXME softc locking
491  */
492 
493 static int
494 ubtbcmfw_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
495 {
496 	ubtbcmfw_softc_p	sc = NULL;
497 	int			error = 0;
498 
499 	USB_GET_SC(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
500 	if (sc == NULL || sc->sc_dying)
501 		return (ENXIO);
502 
503 	if (UBTBCMFW_ENDPOINT(dev) != USB_CONTROL_ENDPOINT)
504 		return (EOPNOTSUPP);
505 
506 	sc->sc_refcnt ++;
507 
508 	switch (cmd) {
509 	case USB_GET_DEVICE_DESC:
510 		*(usb_device_descriptor_t *) data =
511 				*usbd_get_device_descriptor(sc->sc_udev);
512 		break;
513 
514 	default:
515 		error = EINVAL;
516 		break;
517 	}
518 
519 	if (-- sc->sc_refcnt < 0)
520 		usb_detach_wakeup(USBDEV(sc->sc_dev));
521 
522 	return (error);
523 }
524 
525 /*
526  * Poll the endpoint device
527  * XXX FIXME softc locking
528  */
529 
530 static int
531 ubtbcmfw_poll(struct cdev *dev, int events, usb_proc_ptr p)
532 {
533 	ubtbcmfw_softc_p	sc = NULL;
534 	int			revents = 0;
535 
536 	USB_GET_SC(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
537 	if (sc == NULL)
538 		return (ENXIO);
539 
540 	switch (UBTBCMFW_ENDPOINT(dev)) {
541 	case UBTBCMFW_INTR_IN:
542 		if (sc->sc_intr_in_pipe != NULL)
543 			revents |= events & (POLLIN | POLLRDNORM);
544 		else
545 			revents = ENXIO;
546 		break;
547 
548 	case UBTBCMFW_BULK_OUT:
549 		if (sc->sc_bulk_out_pipe != NULL)
550 			revents |= events & (POLLOUT | POLLWRNORM);
551 		else
552 			revents = ENXIO;
553 		break;
554 
555 	default:
556 		revents = EOPNOTSUPP;
557 		break;
558 	}
559 
560 	return (revents);
561 }
562 
563