int bthidev_detach(struct device *self, int flags) { struct bthidev_softc *sc = (struct bthidev_softc *)self; struct bthidev *hidev; mutex_enter(&bt_lock); sc->sc_flags = 0; /* disable reconnecting */ /* release interrupt listen */ if (sc->sc_int_l != NULL) { l2cap_detach(&sc->sc_int_l); sc->sc_int_l = NULL; } /* release control listen */ if (sc->sc_ctl_l != NULL) { l2cap_detach(&sc->sc_ctl_l); sc->sc_ctl_l = NULL; } /* close interrupt channel */ if (sc->sc_int != NULL) { l2cap_disconnect(sc->sc_int, 0); l2cap_detach(&sc->sc_int); sc->sc_int = NULL; } /* close control channel */ if (sc->sc_ctl != NULL) { l2cap_disconnect(sc->sc_ctl, 0); l2cap_detach(&sc->sc_ctl); sc->sc_ctl = NULL; } /* remove timeout */ timeout_del(&sc->sc_reconnect); mutex_exit(&bt_lock); /* detach children */ while ((hidev = LIST_FIRST(&sc->sc_list)) != NULL) { LIST_REMOVE(hidev, sc_next); config_detach(&hidev->sc_dev, flags); } /* release descriptor */ if (sc->sc_desc != NULL) { free(sc->sc_desc, M_BTHIDEV); sc->sc_desc = NULL; } return 0; }
static int bthidev_detach(device_t self, int flags) { struct bthidev_softc *sc = device_private(self); struct bthidev *hidev; mutex_enter(bt_lock); sc->sc_flags = 0; /* disable reconnecting */ /* release interrupt listen */ if (sc->sc_int_l != NULL) { l2cap_detach(&sc->sc_int_l); sc->sc_int_l = NULL; } /* release control listen */ if (sc->sc_ctl_l != NULL) { l2cap_detach(&sc->sc_ctl_l); sc->sc_ctl_l = NULL; } /* close interrupt channel */ if (sc->sc_int != NULL) { l2cap_disconnect(sc->sc_int, 0); l2cap_detach(&sc->sc_int); sc->sc_int = NULL; } /* close control channel */ if (sc->sc_ctl != NULL) { l2cap_disconnect(sc->sc_ctl, 0); l2cap_detach(&sc->sc_ctl); sc->sc_ctl = NULL; } callout_halt(&sc->sc_reconnect, bt_lock); callout_destroy(&sc->sc_reconnect); mutex_exit(bt_lock); /* detach children */ while ((hidev = LIST_FIRST(&sc->sc_list)) != NULL) { LIST_REMOVE(hidev, sc_next); config_detach(hidev->sc_dev, flags); } sockopt_destroy(&sc->sc_mode); return 0; }
static void bthidev_int_disconnected(void *arg, int err) { struct bthidev_softc *sc = arg; if (sc->sc_int != NULL) { l2cap_detach(&sc->sc_int); sc->sc_int = NULL; } sc->sc_state = BTHID_CLOSED; if (sc->sc_ctl == NULL) { aprint_normal_dev(sc->sc_dev, "disconnected\n"); sc->sc_flags &= ~BTHID_CONNECTING; if (sc->sc_flags & BTHID_RECONNECT) callout_schedule(&sc->sc_reconnect, BTHID_RETRY_INTERVAL * hz); else sc->sc_state = BTHID_WAIT_CTL; } else { /* * The control channel should be closing also, allow * them a chance to do that before we force it. */ callout_schedule(&sc->sc_reconnect, hz); } }
/* * Disconnected * * Depending on our state, this could mean several things, but essentially * we are lost. If both channels are closed, and we are marked to reconnect, * schedule another try otherwise just give up. They will contact us. */ static void bthidev_ctl_disconnected(void *arg, int err) { struct bthidev_softc *sc = arg; if (sc->sc_ctl != NULL) { l2cap_detach(&sc->sc_ctl); sc->sc_ctl = NULL; } sc->sc_state = BTHID_CLOSED; if (sc->sc_int == NULL) { aprint_normal_dev(sc->sc_dev, "disconnected\n"); sc->sc_flags &= ~BTHID_CONNECTING; if (sc->sc_flags & BTHID_RECONNECT) callout_schedule(&sc->sc_reconnect, BTHID_RETRY_INTERVAL * hz); else sc->sc_state = BTHID_WAIT_CTL; } else { /* * The interrupt channel should have been closed first, * but its potentially unsafe to detach that from here. * Give them a second to do the right thing or let the * callout handle it. */ callout_schedule(&sc->sc_reconnect, hz); } }
void bthidev_int_disconnected(void *arg, int err) { struct bthidev_softc *sc = arg; if (sc->sc_int != NULL) { l2cap_detach(&sc->sc_int); sc->sc_int = NULL; } sc->sc_state = BTHID_CLOSED; if (sc->sc_ctl == NULL) { printf("%s: disconnected\n", sc->sc_btdev.sc_dev.dv_xname); sc->sc_flags &= ~BTHID_CONNECTING; if (sc->sc_flags & BTHID_RECONNECT) timeout_add_sec(&sc->sc_reconnect, BTHID_RETRY_INTERVAL); else sc->sc_state = BTHID_WAIT_CTL; } else { /* * The control channel should be closing also, allow * them a chance to do that before we force it. */ timeout_add_sec(&sc->sc_reconnect, 1); } }
/* * Disconnected * * Depending on our state, this could mean several things, but essentially * we are lost. If both channels are closed, and we are marked to reconnect, * schedule another try otherwise just give up. They will contact us. */ void bthidev_ctl_disconnected(void *arg, int err) { struct bthidev_softc *sc = arg; if (sc->sc_ctl != NULL) { l2cap_detach(&sc->sc_ctl); sc->sc_ctl = NULL; } sc->sc_state = BTHID_CLOSED; if (sc->sc_int == NULL) { printf("%s: disconnected\n", sc->sc_btdev.sc_dev.dv_xname); sc->sc_flags &= ~BTHID_CONNECTING; if (sc->sc_flags & BTHID_RECONNECT) timeout_add_sec(&sc->sc_reconnect, BTHID_RETRY_INTERVAL); else sc->sc_state = BTHID_WAIT_CTL; } else { /* * The interrupt channel should have been closed first, * but its potentially unsafe to detach that from here. * Give them a second to do the right thing or let the * callout handle it. */ timeout_add_sec(&sc->sc_reconnect, 1); } }
static void bthidev_ctl_connected(void *arg) { struct sockaddr_bt sa; struct bthidev_softc *sc = arg; int err; if (sc->sc_state != BTHID_WAIT_CTL) return; KASSERT(sc->sc_ctl != NULL); KASSERT(sc->sc_int == NULL); if (sc->sc_flags & BTHID_CONNECTING) { /* initiate connect on interrupt PSM */ err = l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc); if (err) goto fail; err = l2cap_setopt(sc->sc_int, &sc->sc_mode); if (err) goto fail; memset(&sa, 0, sizeof(sa)); sa.bt_len = sizeof(sa); sa.bt_family = AF_BLUETOOTH; bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr); err = l2cap_bind(sc->sc_int, &sa); if (err) goto fail; sa.bt_psm = sc->sc_intpsm; bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr); err = l2cap_connect(sc->sc_int, &sa); if (err) goto fail; } sc->sc_state = BTHID_WAIT_INT; return; fail: l2cap_detach(&sc->sc_ctl); sc->sc_ctl = NULL; aprint_error_dev(sc->sc_dev, "connect failed (%d)\n", err); }
/* * User Request. * up is socket * m is either * optional mbuf chain containing message * ioctl command (PRU_CONTROL) * nam is either * optional mbuf chain containing an address * ioctl data (PRU_CONTROL) * optionally protocol number (PRU_ATTACH) * message flags (PRU_RCVD) * ctl is either * optional mbuf chain containing socket options * optional interface pointer (PRU_CONTROL, PRU_PURGEIF) * l is pointer to process requesting action (if any) * * we are responsible for disposing of m and ctl if * they are mbuf chains */ int l2cap_usrreq(struct socket *up, int req, struct mbuf *m, struct mbuf *nam, struct mbuf *ctl, struct proc *p) { struct l2cap_channel *pcb = up->so_pcb; struct sockaddr_bt *sa; struct mbuf *m0; int err = 0; #ifdef notyet /* XXX */ DPRINTFN(2, "%s\n", prurequests[req]); #endif switch (req) { case PRU_CONTROL: return EPASSTHROUGH; #ifdef notyet /* XXX */ case PRU_PURGEIF: return EOPNOTSUPP; #endif case PRU_ATTACH: /* XXX solock() and bt_lock fiddling in NetBSD */ if (pcb != NULL) return EINVAL; /* * For L2CAP socket PCB we just use an l2cap_channel structure * since we have nothing to add.. */ err = soreserve(up, l2cap_sendspace, l2cap_recvspace); if (err) return err; return l2cap_attach((struct l2cap_channel **)&up->so_pcb, &l2cap_proto, up); } if (pcb == NULL) { err = EINVAL; goto release; } switch(req) { case PRU_DISCONNECT: soisdisconnecting(up); return l2cap_disconnect(pcb, up->so_linger); case PRU_ABORT: l2cap_disconnect(pcb, 0); soisdisconnected(up); /* fall through to */ case PRU_DETACH: return l2cap_detach((struct l2cap_channel **)&up->so_pcb); case PRU_BIND: KASSERT(nam != NULL); sa = mtod(nam, struct sockaddr_bt *); if (sa->bt_len != sizeof(struct sockaddr_bt)) return EINVAL; if (sa->bt_family != AF_BLUETOOTH) return EAFNOSUPPORT; return l2cap_bind(pcb, sa); case PRU_CONNECT: KASSERT(nam != NULL); sa = mtod(nam, struct sockaddr_bt *); if (sa->bt_len != sizeof(struct sockaddr_bt)) return EINVAL; if (sa->bt_family != AF_BLUETOOTH) return EAFNOSUPPORT; soisconnecting(up); return l2cap_connect(pcb, sa); case PRU_PEERADDR: KASSERT(nam != NULL); sa = mtod(nam, struct sockaddr_bt *); nam->m_len = sizeof(struct sockaddr_bt); return l2cap_peeraddr(pcb, sa); case PRU_SOCKADDR: KASSERT(nam != NULL); sa = mtod(nam, struct sockaddr_bt *); nam->m_len = sizeof(struct sockaddr_bt); return l2cap_sockaddr(pcb, sa); case PRU_SHUTDOWN: socantsendmore(up); break; case PRU_SEND: KASSERT(m != NULL); if (m->m_pkthdr.len == 0) break; if (m->m_pkthdr.len > pcb->lc_omtu) { err = EMSGSIZE; break; } m0 = m_copym(m, 0, M_COPYALL, M_DONTWAIT); if (m0 == NULL) { err = ENOMEM; break; } if (ctl) /* no use for that */ m_freem(ctl); sbappendrecord(&up->so_snd, m); return l2cap_send(pcb, m0); case PRU_SENSE: return 0; /* (no release) */ case PRU_RCVD: case PRU_RCVOOB: return EOPNOTSUPP; /* (no release) */ case PRU_LISTEN: return l2cap_listen(pcb); case PRU_ACCEPT: KASSERT(nam != NULL); sa = mtod(nam, struct sockaddr_bt *); nam->m_len = sizeof(struct sockaddr_bt); return l2cap_peeraddr(pcb, sa); case PRU_CONNECT2: case PRU_SENDOOB: case PRU_FASTTIMO: case PRU_SLOWTIMO: case PRU_PROTORCV: case PRU_PROTOSEND: err = EOPNOTSUPP; break; default: UNKNOWN(req); err = EOPNOTSUPP; break; } release: if (m) m_freem(m); if (ctl) m_freem(ctl); return err; }