summaryrefslogtreecommitdiffstats
path: root/sys/netgraph/bluetooth/drivers/ubtbcmfw/ubtbcmfw.c
blob: 26f47cd7032fe8245f8a37edead828b7ab135437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
 * ubtbcmfw.c
 */

/*-
 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: ubtbcmfw.c,v 1.3 2003/10/10 19:15:08 max Exp $
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/filio.h>
#include <sys/fcntl.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/poll.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/uio.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>

#include "usbdevs.h"

/*
 * Download firmware to BCM2033.
 */

#define UBTBCMFW_CONFIG_NO	1	/* Config number */
#define UBTBCMFW_IFACE_IDX	0 	/* Control interface */
#define UBTBCMFW_INTR_IN_EP	0x81	/* Fixed endpoint */
#define UBTBCMFW_BULK_OUT_EP	0x02	/* Fixed endpoint */
#define UBTBCMFW_INTR_IN	UE_GET_ADDR(UBTBCMFW_INTR_IN_EP)
#define UBTBCMFW_BULK_OUT	UE_GET_ADDR(UBTBCMFW_BULK_OUT_EP)

struct ubtbcmfw_softc {
	USBBASEDEVICE		sc_dev;			/* base device */
	usbd_device_handle	sc_udev;		/* USB device handle */
	struct cdev *sc_ctrl_dev;		/* control device */
	struct cdev *sc_intr_in_dev;		/* interrupt device */
	struct cdev *sc_bulk_out_dev;	/* bulk device */
	usbd_pipe_handle	sc_intr_in_pipe;	/* interrupt pipe */
	usbd_pipe_handle	sc_bulk_out_pipe;	/* bulk out pipe */
	int			sc_flags;
#define UBTBCMFW_CTRL_DEV	(1 << 0)
#define UBTBCMFW_INTR_IN_DEV	(1 << 1)
#define UBTBCMFW_BULK_OUT_DEV	(1 << 2)
	int			sc_refcnt;
	int			sc_dying;
};

typedef struct ubtbcmfw_softc	*ubtbcmfw_softc_p;

/*
 * Device methods
 */

#define UBTBCMFW_UNIT(n)	((minor(n) >> 4) & 0xf)
#define UBTBCMFW_ENDPOINT(n)	(minor(n) & 0xf)
#define UBTBCMFW_MINOR(u, e)	(((u) << 4) | (e))
#define UBTBCMFW_BSIZE		1024

Static d_open_t		ubtbcmfw_open;
Static d_close_t	ubtbcmfw_close;
Static d_read_t		ubtbcmfw_read;
Static d_write_t	ubtbcmfw_write;
Static d_ioctl_t	ubtbcmfw_ioctl;
Static d_poll_t		ubtbcmfw_poll;

Static struct cdevsw	ubtbcmfw_cdevsw = {
	.d_version =	D_VERSION,
	.d_flags =	D_NEEDGIANT,
	.d_open =	ubtbcmfw_open,
	.d_close =	ubtbcmfw_close,
	.d_read =	ubtbcmfw_read,
	.d_write =	ubtbcmfw_write,
	.d_ioctl =	ubtbcmfw_ioctl,
	.d_poll =	ubtbcmfw_poll,
	.d_name =	"ubtbcmfw",
};

/*
 * Module
 */

USB_DECLARE_DRIVER(ubtbcmfw);
DRIVER_MODULE(ubtbcmfw, uhub, ubtbcmfw_driver, ubtbcmfw_devclass,
	      usbd_driver_load, 0);

/*
 * Probe for a USB Bluetooth device
 */

USB_MATCH(ubtbcmfw)
{
#define	USB_PRODUCT_BROADCOM_BCM2033NF	0x2033

	USB_MATCH_START(ubtbcmfw, uaa);

	if (uaa->iface != NULL)
		return (UMATCH_NONE);

	/* Match the boot device. */
	if (uaa->vendor == USB_VENDOR_BROADCOM &&
	    uaa->product == USB_PRODUCT_BROADCOM_BCM2033NF)
		return (UMATCH_VENDOR_PRODUCT);

	return (UMATCH_NONE);
}

/*
 * Attach the device
 */

USB_ATTACH(ubtbcmfw)
{
	USB_ATTACH_START(ubtbcmfw, sc, uaa);
	usbd_interface_handle	iface;
	usbd_status		err;
	char			devinfo[1024];

	sc->sc_udev = uaa->device;
	usbd_devinfo(sc->sc_udev, 0, devinfo);
	USB_ATTACH_SETUP;
	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);

	sc->sc_ctrl_dev = sc->sc_intr_in_dev = sc->sc_bulk_out_dev = NULL;
	sc->sc_intr_in_pipe = sc->sc_bulk_out_pipe = NULL;
	sc->sc_flags = sc->sc_refcnt = sc->sc_dying = 0;

	err = usbd_set_config_no(sc->sc_udev, UBTBCMFW_CONFIG_NO, 1);
	if (err) {
		printf("%s: setting config no failed. %s\n",
			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
		goto bad;
	}

	err = usbd_device2interface_handle(sc->sc_udev, UBTBCMFW_IFACE_IDX,
			&iface);
	if (err) {
		printf("%s: getting interface handle failed. %s\n",
			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
		goto bad;
	}

	/* Will be used as a bulk pipe */
	err = usbd_open_pipe(iface, UBTBCMFW_INTR_IN_EP, 0,
			&sc->sc_intr_in_pipe);
	if (err) {
		printf("%s: open intr in failed. %s\n",
			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
		goto bad;
	}

	err = usbd_open_pipe(iface, UBTBCMFW_BULK_OUT_EP, 0,
			&sc->sc_bulk_out_pipe);
	if (err) {
		printf("%s: open bulk out failed. %s\n",
			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
		goto bad;
	}

	/* Create device nodes */
	sc->sc_ctrl_dev = make_dev(&ubtbcmfw_cdevsw,
		UBTBCMFW_MINOR(USBDEVUNIT(sc->sc_dev), 0),
		UID_ROOT, GID_OPERATOR, 0644,
		"%s", USBDEVNAME(sc->sc_dev));

	sc->sc_intr_in_dev = make_dev(&ubtbcmfw_cdevsw,
		UBTBCMFW_MINOR(USBDEVUNIT(sc->sc_dev), UBTBCMFW_INTR_IN),
		UID_ROOT, GID_OPERATOR, 0644,
		"%s.%d", USBDEVNAME(sc->sc_dev), UBTBCMFW_INTR_IN);

	sc->sc_bulk_out_dev = make_dev(&ubtbcmfw_cdevsw,
		UBTBCMFW_MINOR(USBDEVUNIT(sc->sc_dev), UBTBCMFW_BULK_OUT),
		UID_ROOT, GID_OPERATOR, 0644,
		"%s.%d", USBDEVNAME(sc->sc_dev), UBTBCMFW_BULK_OUT);

	USB_ATTACH_SUCCESS_RETURN;
bad:
	ubtbcmfw_detach(self);  
        
        USB_ATTACH_ERROR_RETURN;
}

/*
 * Detach the device
 */

USB_DETACH(ubtbcmfw)
{
	USB_DETACH_START(ubtbcmfw, sc);

	sc->sc_dying = 1;

	if (-- sc->sc_refcnt >= 0) {
		if (sc->sc_intr_in_pipe != NULL) 
			usbd_abort_pipe(sc->sc_intr_in_pipe);

		if (sc->sc_bulk_out_pipe != NULL) 
			usbd_abort_pipe(sc->sc_bulk_out_pipe);

		usb_detach_wait(USBDEV(sc->sc_dev));
	}

	/* Destroy device nodes */
	if (sc->sc_bulk_out_dev != NULL) {
		destroy_dev(sc->sc_bulk_out_dev);
		sc->sc_bulk_out_dev = NULL;
	}

	if (sc->sc_intr_in_dev != NULL) {
		destroy_dev(sc->sc_intr_in_dev);
		sc->sc_intr_in_dev = NULL;
	}

	if (sc->sc_ctrl_dev != NULL) {
		destroy_dev(sc->sc_ctrl_dev);
		sc->sc_ctrl_dev = NULL;
	}

	/* Close pipes */
	if (sc->sc_intr_in_pipe != NULL) {
		usbd_close_pipe(sc->sc_intr_in_pipe);
		sc->sc_intr_in_pipe = NULL;
	}

	if (sc->sc_bulk_out_pipe != NULL) {
		usbd_close_pipe(sc->sc_bulk_out_pipe);
		sc->sc_intr_in_pipe = NULL;
	}

	return (0);
}

/*
 * Open endpoint device
 * XXX FIXME softc locking
 */

Static int
ubtbcmfw_open(struct cdev *dev, int flag, int mode, usb_proc_ptr p)
{
	ubtbcmfw_softc_p	sc = NULL;
	int			error = 0;

	/* checks for sc != NULL */
	USB_GET_SC_OPEN(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
	if (sc->sc_dying)
		return (ENXIO);

	switch (UBTBCMFW_ENDPOINT(dev)) {
	case USB_CONTROL_ENDPOINT:
		if (!(sc->sc_flags & UBTBCMFW_CTRL_DEV))
			sc->sc_flags |= UBTBCMFW_CTRL_DEV;
		else
			error = EBUSY;
		break;

	case UBTBCMFW_INTR_IN:
		if (!(sc->sc_flags & UBTBCMFW_INTR_IN_DEV)) {
			if (sc->sc_intr_in_pipe != NULL)
				sc->sc_flags |= UBTBCMFW_INTR_IN_DEV;
			else
				error = ENXIO;
		} else
			error = EBUSY;
		break;

	case UBTBCMFW_BULK_OUT:
		if (!(sc->sc_flags & UBTBCMFW_BULK_OUT_DEV)) {
			if (sc->sc_bulk_out_pipe != NULL)
				sc->sc_flags |= UBTBCMFW_BULK_OUT_DEV;
			else
				error = ENXIO;
		} else
			error = EBUSY;
		break;

	default:
		error = ENXIO;
		break;
	}

	return (error);
}

/*
 * Close endpoint device
 * XXX FIXME softc locking
 */

Static int
ubtbcmfw_close(struct cdev *dev, int flag, int mode, usb_proc_ptr p)
{
	ubtbcmfw_softc_p	sc = NULL;

	USB_GET_SC(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
	if (sc == NULL)
		return (ENXIO);

	switch (UBTBCMFW_ENDPOINT(dev)) {
	case USB_CONTROL_ENDPOINT:
		sc->sc_flags &= ~UBTBCMFW_CTRL_DEV;
		break;

	case UBTBCMFW_INTR_IN:
		if (sc->sc_intr_in_pipe != NULL)
			usbd_abort_pipe(sc->sc_intr_in_pipe);

		sc->sc_flags &= ~UBTBCMFW_INTR_IN_DEV;
		break;

	case UBTBCMFW_BULK_OUT:
		if (sc->sc_bulk_out_pipe != NULL)
			usbd_abort_pipe(sc->sc_bulk_out_pipe);

		sc->sc_flags &= ~UBTBCMFW_BULK_OUT_DEV;
		break;
	}

	return (0);
}

/*
 * Read from the endpoint device
 * XXX FIXME softc locking
 */

Static int
ubtbcmfw_read(struct cdev *dev, struct uio *uio, int flag)
{
	ubtbcmfw_softc_p	sc = NULL;
	u_int8_t		buf[UBTBCMFW_BSIZE];
	usbd_xfer_handle	xfer;
	usbd_status		err;
	int			n, tn, error = 0;

	USB_GET_SC(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
	if (sc == NULL || sc->sc_dying)
		return (ENXIO);

	if (UBTBCMFW_ENDPOINT(dev) != UBTBCMFW_INTR_IN)
		return (EOPNOTSUPP);
	if (sc->sc_intr_in_pipe == NULL)
		return (ENXIO);

	xfer = usbd_alloc_xfer(sc->sc_udev);
	if (xfer == NULL)
		return (ENOMEM);

	sc->sc_refcnt ++;

	while ((n = min(sizeof(buf), uio->uio_resid)) != 0) {
		tn = n;
		err = usbd_bulk_transfer(xfer, sc->sc_intr_in_pipe,
				USBD_SHORT_XFER_OK, USBD_DEFAULT_TIMEOUT,
				buf, &tn, "bcmrd");
		switch (err) {
		case USBD_NORMAL_COMPLETION:
			error = uiomove(buf, tn, uio);
			break;

		case USBD_INTERRUPTED:
			error = EINTR;
			break;

		case USBD_TIMEOUT:
			error = ETIMEDOUT;
			break;

		default:
			error = EIO;
			break;
		}

		if (error != 0 || tn < n)
			break;
	}

	usbd_free_xfer(xfer);

	if (-- sc->sc_refcnt < 0)
		usb_detach_wakeup(USBDEV(sc->sc_dev));

	return (error);
}

/*
 * Write into the endpoint device
 * XXX FIXME softc locking
 */

Static int
ubtbcmfw_write(struct cdev *dev, struct uio *uio, int flag)
{
	ubtbcmfw_softc_p	sc = NULL;
	u_int8_t		buf[UBTBCMFW_BSIZE];
	usbd_xfer_handle	xfer;
	usbd_status		err;
	int			n, error = 0;

	USB_GET_SC(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
	if (sc == NULL || sc->sc_dying)
		return (ENXIO);

	if (UBTBCMFW_ENDPOINT(dev) != UBTBCMFW_BULK_OUT)
		return (EOPNOTSUPP);
	if (sc->sc_bulk_out_pipe == NULL)
		return (ENXIO);

	xfer = usbd_alloc_xfer(sc->sc_udev);
	if (xfer == NULL)
		return (ENOMEM);

	sc->sc_refcnt ++;

	while ((n = min(sizeof(buf), uio->uio_resid)) != 0) {
		error = uiomove(buf, n, uio);
		if (error != 0)
			break;

		err = usbd_bulk_transfer(xfer, sc->sc_bulk_out_pipe,
				0, USBD_DEFAULT_TIMEOUT, buf, &n, "bcmwr");
		switch (err) {
		case USBD_NORMAL_COMPLETION:
			break;

		case USBD_INTERRUPTED:
			error = EINTR;
			break;

		case USBD_TIMEOUT:
			error = ETIMEDOUT;
			break;

		default:
			error = EIO;
			break;
		}

		if (error != 0)
			break;
	}

	usbd_free_xfer(xfer);

	if (-- sc->sc_refcnt < 0)
		usb_detach_wakeup(USBDEV(sc->sc_dev));

	return (error);
}

/*
 * Process ioctl on the endpoint device
 * XXX FIXME softc locking
 */

Static int
ubtbcmfw_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
{
	ubtbcmfw_softc_p	sc = NULL;
	int			error = 0;

	USB_GET_SC(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
	if (sc == NULL || sc->sc_dying)
		return (ENXIO);

	if (UBTBCMFW_ENDPOINT(dev) != USB_CONTROL_ENDPOINT)
		return (EOPNOTSUPP);

	sc->sc_refcnt ++;

	switch (cmd) {
	case USB_GET_DEVICE_DESC:
		*(usb_device_descriptor_t *) data =
				*usbd_get_device_descriptor(sc->sc_udev);
		break;

	default:
		error = EINVAL;
		break;
	}

	if (-- sc->sc_refcnt < 0)
		usb_detach_wakeup(USBDEV(sc->sc_dev));

	return (error);
}

/*
 * Poll the endpoint device
 * XXX FIXME softc locking
 */

Static int
ubtbcmfw_poll(struct cdev *dev, int events, usb_proc_ptr p)
{
	ubtbcmfw_softc_p	sc = NULL;
	int			revents = 0;

	USB_GET_SC(ubtbcmfw, UBTBCMFW_UNIT(dev), sc);
	if (sc == NULL)
		return (ENXIO);

	switch (UBTBCMFW_ENDPOINT(dev)) {
	case UBTBCMFW_INTR_IN:
		if (sc->sc_intr_in_pipe != NULL)
			revents |= events & (POLLIN | POLLRDNORM);
		else
			revents = ENXIO;
		break;

	case UBTBCMFW_BULK_OUT:
		if (sc->sc_bulk_out_pipe != NULL)
			revents |= events & (POLLOUT | POLLWRNORM);
		else
			revents = ENXIO;
		break;

	default:
		revents = EOPNOTSUPP;
		break;
	}

	return (revents);
}

OpenPOWER on IntegriCloud