| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: bthidev.c,v 1.23 2012/12/20 11:13:53 plunky Exp $ */ | | 1 | /* $NetBSD: bthidev.c,v 1.24 2012/12/20 11:17:47 plunky Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2006 Itronix Inc. | | 4 | * Copyright (c) 2006 Itronix Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Written by Iain Hibbert for Itronix Inc. | | 7 | * Written by Iain Hibbert for Itronix Inc. |
8 | * | | 8 | * |
9 | * Redistribution and use in source and binary forms, with or without | | 9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | | 10 | * modification, are permitted provided that the following conditions |
11 | * are met: | | 11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright | | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright | | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| @@ -22,27 +22,27 @@ | | | @@ -22,27 +22,27 @@ |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
23 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 23 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY | | 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY |
25 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | | 25 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
26 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | | 26 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | | 27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
28 | * ON ANY THEORY OF LIABILITY, WHETHER IN | | 28 | * ON ANY THEORY OF LIABILITY, WHETHER IN |
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
31 | * POSSIBILITY OF SUCH DAMAGE. | | 31 | * POSSIBILITY OF SUCH DAMAGE. |
32 | */ | | 32 | */ |
33 | | | 33 | |
34 | #include <sys/cdefs.h> | | 34 | #include <sys/cdefs.h> |
35 | __KERNEL_RCSID(0, "$NetBSD: bthidev.c,v 1.23 2012/12/20 11:13:53 plunky Exp $"); | | 35 | __KERNEL_RCSID(0, "$NetBSD: bthidev.c,v 1.24 2012/12/20 11:17:47 plunky Exp $"); |
36 | | | 36 | |
37 | #include <sys/param.h> | | 37 | #include <sys/param.h> |
38 | #include <sys/condvar.h> | | 38 | #include <sys/condvar.h> |
39 | #include <sys/conf.h> | | 39 | #include <sys/conf.h> |
40 | #include <sys/device.h> | | 40 | #include <sys/device.h> |
41 | #include <sys/fcntl.h> | | 41 | #include <sys/fcntl.h> |
42 | #include <sys/kernel.h> | | 42 | #include <sys/kernel.h> |
43 | #include <sys/kthread.h> | | 43 | #include <sys/kthread.h> |
44 | #include <sys/queue.h> | | 44 | #include <sys/queue.h> |
45 | #include <sys/malloc.h> | | 45 | #include <sys/malloc.h> |
46 | #include <sys/mbuf.h> | | 46 | #include <sys/mbuf.h> |
47 | #include <sys/mutex.h> | | 47 | #include <sys/mutex.h> |
48 | #include <sys/proc.h> | | 48 | #include <sys/proc.h> |
| @@ -183,26 +183,27 @@ bthidev_attach(device_t parent, device_t | | | @@ -183,26 +183,27 @@ bthidev_attach(device_t parent, device_t |
183 | { | | 183 | { |
184 | struct bthidev_softc *sc = device_private(self); | | 184 | struct bthidev_softc *sc = device_private(self); |
185 | prop_dictionary_t dict = aux; | | 185 | prop_dictionary_t dict = aux; |
186 | prop_object_t obj; | | 186 | prop_object_t obj; |
187 | device_t dev; | | 187 | device_t dev; |
188 | struct bthidev_attach_args bha; | | 188 | struct bthidev_attach_args bha; |
189 | struct bthidev *hidev; | | 189 | struct bthidev *hidev; |
190 | struct hid_data *d; | | 190 | struct hid_data *d; |
191 | struct hid_item h; | | 191 | struct hid_item h; |
192 | const void *desc; | | 192 | const void *desc; |
193 | int locs[BTHIDBUSCF_NLOCS]; | | 193 | int locs[BTHIDBUSCF_NLOCS]; |
194 | int maxid, rep, dlen; | | 194 | int maxid, rep, dlen; |
195 | int vendor, product; | | 195 | int vendor, product; |
| | | 196 | int err; |
196 | | | 197 | |
197 | /* | | 198 | /* |
198 | * Init softc | | 199 | * Init softc |
199 | */ | | 200 | */ |
200 | sc->sc_dev = self; | | 201 | sc->sc_dev = self; |
201 | LIST_INIT(&sc->sc_list); | | 202 | LIST_INIT(&sc->sc_list); |
202 | MBUFQ_INIT(&sc->sc_inq); | | 203 | MBUFQ_INIT(&sc->sc_inq); |
203 | callout_init(&sc->sc_reconnect, 0); | | 204 | callout_init(&sc->sc_reconnect, 0); |
204 | callout_setfunc(&sc->sc_reconnect, bthidev_timeout, sc); | | 205 | callout_setfunc(&sc->sc_reconnect, bthidev_timeout, sc); |
205 | sc->sc_state = BTHID_CLOSED; | | 206 | sc->sc_state = BTHID_CLOSED; |
206 | sc->sc_flags = BTHID_CONNECTING; | | 207 | sc->sc_flags = BTHID_CONNECTING; |
207 | sc->sc_ctlpsm = L2CAP_PSM_HID_CNTL; | | 208 | sc->sc_ctlpsm = L2CAP_PSM_HID_CNTL; |
208 | sc->sc_intpsm = L2CAP_PSM_HID_INTR; | | 209 | sc->sc_intpsm = L2CAP_PSM_HID_INTR; |
| @@ -327,28 +328,29 @@ bthidev_attach(device_t parent, device_t | | | @@ -327,28 +328,29 @@ bthidev_attach(device_t parent, device_t |
327 | hidev->sc_id = rep; | | 328 | hidev->sc_id = rep; |
328 | hidev->sc_input = bha.ba_input; | | 329 | hidev->sc_input = bha.ba_input; |
329 | hidev->sc_feature = bha.ba_feature; | | 330 | hidev->sc_feature = bha.ba_feature; |
330 | LIST_INSERT_HEAD(&sc->sc_list, hidev, sc_next); | | 331 | LIST_INSERT_HEAD(&sc->sc_list, hidev, sc_next); |
331 | } | | 332 | } |
332 | } | | 333 | } |
333 | | | 334 | |
334 | pmf_device_register(self, NULL, NULL); | | 335 | pmf_device_register(self, NULL, NULL); |
335 | | | 336 | |
336 | /* | | 337 | /* |
337 | * start bluetooth connections | | 338 | * start bluetooth connections |
338 | */ | | 339 | */ |
339 | mutex_enter(bt_lock); | | 340 | mutex_enter(bt_lock); |
340 | if ((sc->sc_flags & BTHID_RECONNECT) == 0) | | 341 | if ((sc->sc_flags & BTHID_RECONNECT) == 0 |
341 | bthidev_listen(sc); | | 342 | && (err = bthidev_listen(sc)) != 0) |
| | | 343 | aprint_error_dev(self, "failed to listen (%d)\n", err); |
342 | | | 344 | |
343 | if (sc->sc_flags & BTHID_CONNECTING) | | 345 | if (sc->sc_flags & BTHID_CONNECTING) |
344 | bthidev_connect(sc); | | 346 | bthidev_connect(sc); |
345 | mutex_exit(bt_lock); | | 347 | mutex_exit(bt_lock); |
346 | } | | 348 | } |
347 | | | 349 | |
348 | static int | | 350 | static int |
349 | bthidev_detach(device_t self, int flags) | | 351 | bthidev_detach(device_t self, int flags) |
350 | { | | 352 | { |
351 | struct bthidev_softc *sc = device_private(self); | | 353 | struct bthidev_softc *sc = device_private(self); |
352 | struct bthidev *hidev; | | 354 | struct bthidev *hidev; |
353 | | | 355 | |
354 | mutex_enter(bt_lock); | | 356 | mutex_enter(bt_lock); |
| @@ -551,28 +553,30 @@ bthidev_connect(struct bthidev_softc *sc | | | @@ -551,28 +553,30 @@ bthidev_connect(struct bthidev_softc *sc |
551 | aprint_verbose_dev(sc->sc_dev, "connect (#%d)\n", sc->sc_attempts); | | 553 | aprint_verbose_dev(sc->sc_dev, "connect (#%d)\n", sc->sc_attempts); |
552 | | | 554 | |
553 | memset(&sa, 0, sizeof(sa)); | | 555 | memset(&sa, 0, sizeof(sa)); |
554 | sa.bt_len = sizeof(sa); | | 556 | sa.bt_len = sizeof(sa); |
555 | sa.bt_family = AF_BLUETOOTH; | | 557 | sa.bt_family = AF_BLUETOOTH; |
556 | | | 558 | |
557 | err = l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc); | | 559 | err = l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc); |
558 | if (err) { | | 560 | if (err) { |
559 | aprint_error_dev(sc->sc_dev, "l2cap_attach failed (%d)\n", err); | | 561 | aprint_error_dev(sc->sc_dev, "l2cap_attach failed (%d)\n", err); |
560 | return err; | | 562 | return err; |
561 | } | | 563 | } |
562 | | | 564 | |
563 | err = l2cap_setopt(sc->sc_ctl, &sc->sc_mode); | | 565 | err = l2cap_setopt(sc->sc_ctl, &sc->sc_mode); |
564 | if (err) | | 566 | if (err) { |
| | | 567 | aprint_error_dev(sc->sc_dev, "l2cap_setopt failed (%d)\n", err); |
565 | return err; | | 568 | return err; |
| | | 569 | } |
566 | | | 570 | |
567 | bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr); | | 571 | bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr); |
568 | err = l2cap_bind(sc->sc_ctl, &sa); | | 572 | err = l2cap_bind(sc->sc_ctl, &sa); |
569 | if (err) { | | 573 | if (err) { |
570 | aprint_error_dev(sc->sc_dev, "l2cap_bind failed (%d)\n", err); | | 574 | aprint_error_dev(sc->sc_dev, "l2cap_bind failed (%d)\n", err); |
571 | return err; | | 575 | return err; |
572 | } | | 576 | } |
573 | | | 577 | |
574 | sa.bt_psm = sc->sc_ctlpsm; | | 578 | sa.bt_psm = sc->sc_ctlpsm; |
575 | bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr); | | 579 | bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr); |
576 | err = l2cap_connect(sc->sc_ctl, &sa); | | 580 | err = l2cap_connect(sc->sc_ctl, &sa); |
577 | if (err) { | | 581 | if (err) { |
578 | aprint_error_dev(sc->sc_dev, "l2cap_connect failed (%d)\n", err); | | 582 | aprint_error_dev(sc->sc_dev, "l2cap_connect failed (%d)\n", err); |
| @@ -792,27 +796,27 @@ bthidev_int_connected(void *arg) | | | @@ -792,27 +796,27 @@ bthidev_int_connected(void *arg) |
792 | static void | | 796 | static void |
793 | bthidev_ctl_disconnected(void *arg, int err) | | 797 | bthidev_ctl_disconnected(void *arg, int err) |
794 | { | | 798 | { |
795 | struct bthidev_softc *sc = arg; | | 799 | struct bthidev_softc *sc = arg; |
796 | | | 800 | |
797 | if (sc->sc_ctl != NULL) { | | 801 | if (sc->sc_ctl != NULL) { |
798 | l2cap_detach(&sc->sc_ctl); | | 802 | l2cap_detach(&sc->sc_ctl); |
799 | sc->sc_ctl = NULL; | | 803 | sc->sc_ctl = NULL; |
800 | } | | 804 | } |
801 | | | 805 | |
802 | sc->sc_state = BTHID_CLOSED; | | 806 | sc->sc_state = BTHID_CLOSED; |
803 | | | 807 | |
804 | if (sc->sc_int == NULL) { | | 808 | if (sc->sc_int == NULL) { |
805 | aprint_normal_dev(sc->sc_dev, "disconnected\n"); | | 809 | aprint_normal_dev(sc->sc_dev, "disconnected (%d)\n", err); |
806 | sc->sc_flags &= ~BTHID_CONNECTING; | | 810 | sc->sc_flags &= ~BTHID_CONNECTING; |
807 | | | 811 | |
808 | if (sc->sc_flags & BTHID_RECONNECT) | | 812 | if (sc->sc_flags & BTHID_RECONNECT) |
809 | callout_schedule(&sc->sc_reconnect, | | 813 | callout_schedule(&sc->sc_reconnect, |
810 | BTHID_RETRY_INTERVAL * hz); | | 814 | BTHID_RETRY_INTERVAL * hz); |
811 | else | | 815 | else |
812 | sc->sc_state = BTHID_WAIT_CTL; | | 816 | sc->sc_state = BTHID_WAIT_CTL; |
813 | } else { | | 817 | } else { |
814 | /* | | 818 | /* |
815 | * The interrupt channel should have been closed first, | | 819 | * The interrupt channel should have been closed first, |
816 | * but its potentially unsafe to detach that from here. | | 820 | * but its potentially unsafe to detach that from here. |
817 | * Give them a second to do the right thing or let the | | 821 | * Give them a second to do the right thing or let the |
818 | * callout handle it. | | 822 | * callout handle it. |
| @@ -824,27 +828,27 @@ bthidev_ctl_disconnected(void *arg, int | | | @@ -824,27 +828,27 @@ bthidev_ctl_disconnected(void *arg, int |
824 | static void | | 828 | static void |
825 | bthidev_int_disconnected(void *arg, int err) | | 829 | bthidev_int_disconnected(void *arg, int err) |
826 | { | | 830 | { |
827 | struct bthidev_softc *sc = arg; | | 831 | struct bthidev_softc *sc = arg; |
828 | | | 832 | |
829 | if (sc->sc_int != NULL) { | | 833 | if (sc->sc_int != NULL) { |
830 | l2cap_detach(&sc->sc_int); | | 834 | l2cap_detach(&sc->sc_int); |
831 | sc->sc_int = NULL; | | 835 | sc->sc_int = NULL; |
832 | } | | 836 | } |
833 | | | 837 | |
834 | sc->sc_state = BTHID_CLOSED; | | 838 | sc->sc_state = BTHID_CLOSED; |
835 | | | 839 | |
836 | if (sc->sc_ctl == NULL) { | | 840 | if (sc->sc_ctl == NULL) { |
837 | aprint_normal_dev(sc->sc_dev, "disconnected\n"); | | 841 | aprint_normal_dev(sc->sc_dev, "disconnected (%d)\n", err); |
838 | sc->sc_flags &= ~BTHID_CONNECTING; | | 842 | sc->sc_flags &= ~BTHID_CONNECTING; |
839 | | | 843 | |
840 | if (sc->sc_flags & BTHID_RECONNECT) | | 844 | if (sc->sc_flags & BTHID_RECONNECT) |
841 | callout_schedule(&sc->sc_reconnect, | | 845 | callout_schedule(&sc->sc_reconnect, |
842 | BTHID_RETRY_INTERVAL * hz); | | 846 | BTHID_RETRY_INTERVAL * hz); |
843 | else | | 847 | else |
844 | sc->sc_state = BTHID_WAIT_CTL; | | 848 | sc->sc_state = BTHID_WAIT_CTL; |
845 | } else { | | 849 | } else { |
846 | /* | | 850 | /* |
847 | * The control channel should be closing also, allow | | 851 | * The control channel should be closing also, allow |
848 | * them a chance to do that before we force it. | | 852 | * them a chance to do that before we force it. |
849 | */ | | 853 | */ |
850 | callout_schedule(&sc->sc_reconnect, hz); | | 854 | callout_schedule(&sc->sc_reconnect, hz); |