Index: head/usr.sbin/bluetooth/bthidd/bthidd.h =================================================================== --- head/usr.sbin/bluetooth/bthidd/bthidd.h (revision 322439) +++ head/usr.sbin/bluetooth/bthidd/bthidd.h (revision 322440) @@ -1,93 +1,95 @@ /* * bthidd.h */ /*- * Copyright (c) 2006 Maksim Yevmenkin * 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: bthidd.h,v 1.7 2006/09/07 21:06:53 max Exp $ * $FreeBSD$ */ #ifndef _BTHIDD_H_ #define _BTHIDD_H_ 1 #define BTHIDD_IDENT "bthidd" #define BTHIDD_PIDFILE "/var/run/" BTHIDD_IDENT ".pid" struct bthid_session; struct bthid_server { bdaddr_t bdaddr; /* local bdaddr */ int32_t cons; /* /dev/consolectl */ int32_t ctrl; /* control channel (listen) */ int32_t intr; /* intr. channel (listen) */ int32_t maxfd; /* max fd in sets */ fd_set rfdset; /* read descriptor set */ fd_set wfdset; /* write descriptor set */ LIST_HEAD(, bthid_session) sessions; }; typedef struct bthid_server bthid_server_t; typedef struct bthid_server * bthid_server_p; struct bthid_session { bthid_server_p srv; /* pointer back to server */ int32_t ctrl; /* control channel */ int32_t intr; /* interrupt channel */ int32_t vkbd; /* virual keyboard */ + void *ctx; /* product specific dev state */ bdaddr_t bdaddr;/* remote bdaddr */ uint16_t state; /* session state */ #define CLOSED 0 #define W4CTRL 1 #define W4INTR 2 #define OPEN 3 bitstr_t *keys1; /* keys map (new) */ bitstr_t *keys2; /* keys map (old) */ LIST_ENTRY(bthid_session) next; /* link to next */ }; typedef struct bthid_session bthid_session_t; typedef struct bthid_session * bthid_session_p; int32_t server_init (bthid_server_p srv); void server_shutdown (bthid_server_p srv); int32_t server_do (bthid_server_p srv); int32_t client_rescan (bthid_server_p srv); int32_t client_connect (bthid_server_p srv, int fd); bthid_session_p session_open (bthid_server_p srv, hid_device_p const d); bthid_session_p session_by_bdaddr(bthid_server_p srv, bdaddr_p bdaddr); bthid_session_p session_by_fd (bthid_server_p srv, int32_t fd); void session_close (bthid_session_p s); +void hid_initialise (bthid_session_p s); int32_t hid_control (bthid_session_p s, uint8_t *data, int32_t len); int32_t hid_interrupt (bthid_session_p s, uint8_t *data, int32_t len); #endif /* ndef _BTHIDD_H_ */ Index: head/usr.sbin/bluetooth/bthidd/hid.c =================================================================== --- head/usr.sbin/bluetooth/bthidd/hid.c (revision 322439) +++ head/usr.sbin/bluetooth/bthidd/hid.c (revision 322440) @@ -1,415 +1,545 @@ /* * hid.c */ /*- * Copyright (c) 2006 Maksim Yevmenkin * 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: hid.c,v 1.5 2006/09/07 21:06:53 max Exp $ * $FreeBSD$ */ #include #include #include #include #define L2CAP_SOCKET_CHECKED #include #include #include #include #include +#include #include #include #include #include #include "bthid_config.h" #include "bthidd.h" #include "kbd.h" /* + * Inoffical and unannounced report ids for Apple Mice and trackpad + */ +#define TRACKPAD_REPORT_ID 0x28 +#define AMM_REPORT_ID 0x29 +#define BATT_STAT_REPORT_ID 0x30 +#define BATT_STRENGTH_REPORT_ID 0x47 +#define SURFACE_REPORT_ID 0x61 + +/* + * Apple magic mouse (AMM) specific device state + */ +#define AMM_MAX_BUTTONS 16 +struct apple_state { + int y [AMM_MAX_BUTTONS]; + int button_state; +}; + +#define MAGIC_MOUSE(D) (((D)->vendor_id == 0x5ac) && ((D)->product_id == 0x30d)) +#define AMM_BASIC_BLOCK 5 +#define AMM_FINGER_BLOCK 8 +#define AMM_VALID_REPORT(L) (((L) >= AMM_BASIC_BLOCK) && \ + ((L) <= 16*AMM_FINGER_BLOCK + AMM_BASIC_BLOCK) && \ + ((L) % AMM_FINGER_BLOCK) == AMM_BASIC_BLOCK) +#define AMM_WHEEL_SPEED 100 + +/* + * Probe for per-device initialisation + */ +void +hid_initialise(bthid_session_p s) +{ + hid_device_p hid_device = get_hid_device(&s->bdaddr); + + if (hid_device && MAGIC_MOUSE(hid_device)) { + /* Magic report to enable trackpad on Apple's Magic Mouse */ + static uint8_t rep[] = {0x53, 0xd7, 0x01}; + + if ((s->ctx = calloc(1, sizeof(struct apple_state))) == NULL) + return; + write(s->ctrl, rep, 3); + } +} + +/* * Process data from control channel */ int32_t hid_control(bthid_session_p s, uint8_t *data, int32_t len) { assert(s != NULL); assert(data != NULL); assert(len > 0); switch (data[0] >> 4) { case 0: /* Handshake (response to command) */ if (data[0] & 0xf) syslog(LOG_ERR, "Got handshake message with error " \ "response 0x%x from %s", data[0], bt_ntoa(&s->bdaddr, NULL)); break; case 1: /* HID Control */ switch (data[0] & 0xf) { case 0: /* NOP */ break; case 1: /* Hard reset */ case 2: /* Soft reset */ syslog(LOG_WARNING, "Device %s requested %s reset", bt_ntoa(&s->bdaddr, NULL), ((data[0] & 0xf) == 1)? "hard" : "soft"); break; case 3: /* Suspend */ syslog(LOG_NOTICE, "Device %s requested Suspend", bt_ntoa(&s->bdaddr, NULL)); break; case 4: /* Exit suspend */ syslog(LOG_NOTICE, "Device %s requested Exit Suspend", bt_ntoa(&s->bdaddr, NULL)); break; case 5: /* Virtual cable unplug */ syslog(LOG_NOTICE, "Device %s unplugged virtual cable", bt_ntoa(&s->bdaddr, NULL)); session_close(s); break; default: syslog(LOG_WARNING, "Device %s sent unknown " \ "HID_Control message 0x%x", bt_ntoa(&s->bdaddr, NULL), data[0]); break; } break; default: syslog(LOG_WARNING, "Got unexpected message 0x%x on Control " \ "channel from %s", data[0], bt_ntoa(&s->bdaddr, NULL)); break; } return (0); } /* * Process data from the interrupt channel */ int32_t hid_interrupt(bthid_session_p s, uint8_t *data, int32_t len) { hid_device_p hid_device; hid_data_t d; hid_item_t h; int32_t report_id, usage, page, val, mouse_x, mouse_y, mouse_z, mouse_butt, mevents, kevents, i; assert(s != NULL); assert(s->srv != NULL); assert(data != NULL); if (len < 3) { syslog(LOG_ERR, "Got short message (%d bytes) on Interrupt " \ "channel from %s", len, bt_ntoa(&s->bdaddr, NULL)); return (-1); } if (data[0] != 0xa1) { syslog(LOG_ERR, "Got unexpected message 0x%x on " \ "Interrupt channel from %s", data[0], bt_ntoa(&s->bdaddr, NULL)); return (-1); } report_id = data[1]; data ++; len --; hid_device = get_hid_device(&s->bdaddr); assert(hid_device != NULL); mouse_x = mouse_y = mouse_z = mouse_butt = mevents = kevents = 0; for (d = hid_start_parse(hid_device->desc, 1 << hid_input, -1); hid_get_item(d, &h) > 0; ) { if ((h.flags & HIO_CONST) || (h.report_ID != report_id) || (h.kind != hid_input)) continue; page = HID_PAGE(h.usage); val = hid_get_data(data, &h); /* * When the input field is an array and the usage is specified * with a range instead of an ID, we have to derive the actual * usage by using the item value as an index in the usage range * list. */ if ((h.flags & HIO_VARIABLE)) { usage = HID_USAGE(h.usage); } else { const uint32_t usage_offset = val - h.logical_minimum; usage = HID_USAGE(h.usage_minimum + usage_offset); } switch (page) { case HUP_GENERIC_DESKTOP: switch (usage) { case HUG_X: mouse_x = val; mevents ++; break; case HUG_Y: mouse_y = val; mevents ++; break; case HUG_WHEEL: mouse_z = -val; mevents ++; break; case HUG_SYSTEM_SLEEP: if (val) syslog(LOG_NOTICE, "Sleep button pressed"); break; } break; case HUP_KEYBOARD: kevents ++; if (h.flags & HIO_VARIABLE) { if (val && usage < kbd_maxkey()) bit_set(s->keys1, usage); } else { if (val && val < kbd_maxkey()) bit_set(s->keys1, val); for (i = 1; i < h.report_count; i++) { h.pos += h.report_size; val = hid_get_data(data, &h); if (val && val < kbd_maxkey()) bit_set(s->keys1, val); } } break; case HUP_BUTTON: if (usage != 0) { if (usage == 2) usage = 3; else if (usage == 3) usage = 2; mouse_butt |= (val << (usage - 1)); mevents ++; } break; case HUP_CONSUMER: if (!val) break; switch (usage) { case HUC_AC_PAN: /* Horizontal scroll */ if (val < 0) mouse_butt |= (1 << 5); else mouse_butt |= (1 << 6); mevents ++; val = 0; break; case 0xb5: /* Scan Next Track */ val = 0x19; break; case 0xb6: /* Scan Previous Track */ val = 0x10; break; case 0xb7: /* Stop */ val = 0x24; break; case 0xcd: /* Play/Pause */ val = 0x22; break; case 0xe2: /* Mute */ val = 0x20; break; case 0xe9: /* Volume Up */ val = 0x30; break; case 0xea: /* Volume Down */ val = 0x2E; break; case 0x183: /* Media Select */ val = 0x6D; break; case 0x018a: /* Mail */ val = 0x6C; break; case 0x192: /* Calculator */ val = 0x21; break; case 0x194: /* My Computer */ val = 0x6B; break; case 0x221: /* WWW Search */ val = 0x65; break; case 0x223: /* WWW Home */ val = 0x32; break; case 0x224: /* WWW Back */ val = 0x6A; break; case 0x225: /* WWW Forward */ val = 0x69; break; case 0x226: /* WWW Stop */ val = 0x68; break; case 0x227: /* WWW Refresh */ val = 0x67; break; case 0x22a: /* WWW Favorites */ val = 0x66; break; default: val = 0; break; } /* XXX FIXME - UGLY HACK */ if (val != 0) { if (hid_device->keyboard) { int32_t buf[4] = { 0xe0, val, 0xe0, val|0x80 }; assert(s->vkbd != -1); write(s->vkbd, buf, sizeof(buf)); } else syslog(LOG_ERR, "Keyboard events " \ "received from non-keyboard " \ "device %s. Please report", bt_ntoa(&s->bdaddr, NULL)); } break; case HUP_MICROSOFT: switch (usage) { case 0xfe01: if (!hid_device->battery_power) break; switch (val) { case 1: syslog(LOG_INFO, "Battery is OK on %s", bt_ntoa(&s->bdaddr, NULL)); break; case 2: syslog(LOG_NOTICE, "Low battery on %s", bt_ntoa(&s->bdaddr, NULL)); break; case 3: syslog(LOG_WARNING, "Very low battery "\ "on %s", bt_ntoa(&s->bdaddr, NULL)); break; } break; } break; } } hid_end_parse(d); + + /* + * Apple adheres to no standards and sends reports it does + * not introduce in its hid descriptor for its magic mouse. + * Handle those reports here. + */ + if (MAGIC_MOUSE(hid_device) && s->ctx) { + struct apple_state *c = (struct apple_state *)s->ctx; + int firm = 0, middle = 0; + int16_t v; + + data++, len--; /* Chomp report_id */ + + if (report_id != AMM_REPORT_ID || !AMM_VALID_REPORT(len)) + goto check_middle_button; + + /* + * The basics. When touches are detected, no normal mouse + * reports are sent. Collect clicks and dx/dy + */ + if (data[2] & 1) + mouse_butt |= 0x1; + if (data[2] & 2) + mouse_butt |= 0x4; + + if ((v = data[0] + ((data[2] & 0x0C) << 6))) + mouse_x += ((int16_t)(v << 6)) >> 6, mevents++; + if ((v = data[1] + ((data[2] & 0x30) << 4))) + mouse_y += ((int16_t)(v << 6)) >> 6, mevents++; + + /* + * The hard part: accumulate touch events and emulate middle + */ + for (data += AMM_BASIC_BLOCK, len -= AMM_BASIC_BLOCK; + len >= AMM_FINGER_BLOCK; + data += AMM_FINGER_BLOCK, len -= AMM_FINGER_BLOCK) { + int x, y, z, force, id; + + v = data[0] | ((data[1] & 0xf) << 8); + x = ((int16_t)(v << 4)) >> 4; + + v = (data[1] >> 4) | (data[2] << 4); + y = -(((int16_t)(v << 4)) >> 4); + + force = data[5] & 0x3f; + id = 0xf & ((data[5] >> 6) | (data[6] << 2)); + z = (y - c->y[id]) / AMM_WHEEL_SPEED; + + switch ((data[7] >> 4) & 0x7) { /* Phase */ + case 3: /* First touch */ + c->y[id] = y; + break; + case 4: /* Touch dragged */ + if (z) { + mouse_z += z; + c->y[id] += z * AMM_WHEEL_SPEED; + mevents++; + } + break; + default: + break; + } + /* Count firm touches vs. firm+middle touches */ + if (force >= 8 && ++firm && x > -350 && x < 350) + ++middle; + } + + /* + * If a new click is registered by mouse and there are firm + * touches which are all in center, make it a middle click + */ + if (mouse_butt && !c->button_state && firm && middle == firm) + mouse_butt = 0x2; + + /* + * If we're still clicking and have converted the click + * to a middle click, keep it middle clicking + */ +check_middle_button: + if (mouse_butt && c->button_state == 0x2) + mouse_butt = 0x2; + + if (mouse_butt != c->button_state) + c->button_state = mouse_butt, mevents++; + } /* * XXX FIXME Feed keyboard events into kernel. * The code below works, bit host also needs to track * and handle repeat. * * Key repeat currently works in X, but not in console. */ if (kevents > 0) { if (hid_device->keyboard) { assert(s->vkbd != -1); kbd_process_keys(s); } else syslog(LOG_ERR, "Keyboard events received from " \ "non-keyboard device %s. Please report", bt_ntoa(&s->bdaddr, NULL)); } /* * XXX FIXME Feed mouse events into kernel. * The code block below works, but it is not good enough. * Need to track double-clicks etc. * * Double click currently works in X, but not in console. */ if (mevents > 0) { struct mouse_info mi; mi.operation = MOUSE_ACTION; mi.u.data.x = mouse_x; mi.u.data.y = mouse_y; mi.u.data.z = mouse_z; mi.u.data.buttons = mouse_butt; if (ioctl(s->srv->cons, CONS_MOUSECTL, &mi) < 0) syslog(LOG_ERR, "Could not process mouse events from " \ "%s. %s (%d)", bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno); } return (0); } Index: head/usr.sbin/bluetooth/bthidd/server.c =================================================================== --- head/usr.sbin/bluetooth/bthidd/server.c (revision 322439) +++ head/usr.sbin/bluetooth/bthidd/server.c (revision 322440) @@ -1,352 +1,356 @@ /* * server.c */ /*- * Copyright (c) 2006 Maksim Yevmenkin * 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: server.c,v 1.9 2006/09/07 21:06:53 max Exp $ * $FreeBSD$ */ #include #include #define L2CAP_SOCKET_CHECKED #include #include #include #include #include #include #include #include #include #include #include "bthid_config.h" #include "bthidd.h" #include "kbd.h" #undef max #define max(x, y) (((x) > (y))? (x) : (y)) static int32_t server_accept (bthid_server_p srv, int32_t fd); static int32_t server_process(bthid_server_p srv, int32_t fd); /* * Initialize server */ int32_t server_init(bthid_server_p srv) { struct sockaddr_l2cap l2addr; assert(srv != NULL); srv->ctrl = srv->intr = -1; FD_ZERO(&srv->rfdset); FD_ZERO(&srv->wfdset); LIST_INIT(&srv->sessions); /* Open /dev/consolectl */ srv->cons = open("/dev/consolectl", O_RDWR); if (srv->cons < 0) { syslog(LOG_ERR, "Could not open /dev/consolectl. %s (%d)", strerror(errno), errno); return (-1); } /* Create control socket */ srv->ctrl = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); if (srv->ctrl < 0) { syslog(LOG_ERR, "Could not create control L2CAP socket. " \ "%s (%d)", strerror(errno), errno); close(srv->cons); return (-1); } l2addr.l2cap_len = sizeof(l2addr); l2addr.l2cap_family = AF_BLUETOOTH; memcpy(&l2addr.l2cap_bdaddr, &srv->bdaddr, sizeof(l2addr.l2cap_bdaddr)); l2addr.l2cap_psm = htole16(0x11); l2addr.l2cap_bdaddr_type = BDADDR_BREDR; l2addr.l2cap_cid = 0; if (bind(srv->ctrl, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) { syslog(LOG_ERR, "Could not bind control L2CAP socket. " \ "%s (%d)", strerror(errno), errno); close(srv->ctrl); close(srv->cons); return (-1); } if (listen(srv->ctrl, 10) < 0) { syslog(LOG_ERR, "Could not listen on control L2CAP socket. " \ "%s (%d)", strerror(errno), errno); close(srv->ctrl); close(srv->cons); return (-1); } /* Create intrrupt socket */ srv->intr = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); if (srv->intr < 0) { syslog(LOG_ERR, "Could not create interrupt L2CAP socket. " \ "%s (%d)", strerror(errno), errno); close(srv->ctrl); close(srv->cons); return (-1); } l2addr.l2cap_psm = htole16(0x13); if (bind(srv->intr, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) { syslog(LOG_ERR, "Could not bind interrupt L2CAP socket. " \ "%s (%d)", strerror(errno), errno); close(srv->intr); close(srv->ctrl); close(srv->cons); return (-1); } if (listen(srv->intr, 10) < 0) { syslog(LOG_ERR, "Could not listen on interrupt L2CAP socket. "\ "%s (%d)", strerror(errno), errno); close(srv->intr); close(srv->ctrl); close(srv->cons); return (-1); } FD_SET(srv->ctrl, &srv->rfdset); FD_SET(srv->intr, &srv->rfdset); srv->maxfd = max(srv->ctrl, srv->intr); return (0); } /* * Shutdown server */ void server_shutdown(bthid_server_p srv) { assert(srv != NULL); close(srv->cons); close(srv->ctrl); close(srv->intr); while (!LIST_EMPTY(&srv->sessions)) session_close(LIST_FIRST(&srv->sessions)); memset(srv, 0, sizeof(*srv)); } /* * Do one server iteration */ int32_t server_do(bthid_server_p srv) { struct timeval tv; fd_set rfdset, wfdset; int32_t n, fd; assert(srv != NULL); tv.tv_sec = 1; tv.tv_usec = 0; /* Copy cached version of the fd sets and call select */ memcpy(&rfdset, &srv->rfdset, sizeof(rfdset)); memcpy(&wfdset, &srv->wfdset, sizeof(wfdset)); n = select(srv->maxfd + 1, &rfdset, &wfdset, NULL, &tv); if (n < 0) { if (errno == EINTR) return (0); syslog(LOG_ERR, "Could not select(%d, %p, %p). %s (%d)", srv->maxfd + 1, &rfdset, &wfdset, strerror(errno), errno); return (-1); } /* Process descriptors (if any) */ for (fd = 0; fd < srv->maxfd + 1 && n > 0; fd ++) { if (FD_ISSET(fd, &rfdset)) { n --; if (fd == srv->ctrl || fd == srv->intr) server_accept(srv, fd); else server_process(srv, fd); } else if (FD_ISSET(fd, &wfdset)) { n --; client_connect(srv, fd); } } return (0); } /* * Accept new connection */ static int32_t server_accept(bthid_server_p srv, int32_t fd) { bthid_session_p s; hid_device_p d; struct sockaddr_l2cap l2addr; int32_t new_fd; socklen_t len; len = sizeof(l2addr); if ((new_fd = accept(fd, (struct sockaddr *) &l2addr, &len)) < 0) { syslog(LOG_ERR, "Could not accept %s connection. %s (%d)", (fd == srv->ctrl)? "control" : "interrupt", strerror(errno), errno); return (-1); } /* Is device configured? */ if ((d = get_hid_device(&l2addr.l2cap_bdaddr)) == NULL) { syslog(LOG_ERR, "Rejecting %s connection from %s. " \ "Device not configured", (fd == srv->ctrl)? "control" : "interrupt", bt_ntoa(&l2addr.l2cap_bdaddr, NULL)); close(new_fd); return (-1); } /* Check if we have session for the device */ if ((s = session_by_bdaddr(srv, &l2addr.l2cap_bdaddr)) == NULL) { d->new_device = 0; /* reset new device flag */ write_hids_file(); /* Create new inbound session */ if ((s = session_open(srv, d)) == NULL) { syslog(LOG_CRIT, "Could not open inbound session " "for %s", bt_ntoa(&l2addr.l2cap_bdaddr, NULL)); close(new_fd); return (-1); } } /* Update descriptors */ if (fd == srv->ctrl) { assert(s->ctrl == -1); s->ctrl = new_fd; s->state = (s->intr == -1)? W4INTR : OPEN; } else { assert(s->intr == -1); s->intr = new_fd; s->state = (s->ctrl == -1)? W4CTRL : OPEN; } FD_SET(new_fd, &srv->rfdset); if (new_fd > srv->maxfd) srv->maxfd = new_fd; syslog(LOG_NOTICE, "Accepted %s connection from %s", (fd == srv->ctrl)? "control" : "interrupt", bt_ntoa(&l2addr.l2cap_bdaddr, NULL)); /* Register session's vkbd descriptor (if needed) for read */ if (s->state == OPEN && d->keyboard) { assert(s->vkbd != -1); FD_SET(s->vkbd, &srv->rfdset); if (s->vkbd > srv->maxfd) srv->maxfd = s->vkbd; } + /* Pass device for probing after both channels are established */ + if (s->state == OPEN) + hid_initialise(s); + return (0); } /* * Process data on the connection */ static int32_t server_process(bthid_server_p srv, int32_t fd) { bthid_session_p s = session_by_fd(srv, fd); int32_t len, to_read; int32_t (*cb)(bthid_session_p, uint8_t *, int32_t); union { uint8_t b[1024]; vkbd_status_t s; } data; if (s == NULL) return (0); /* can happen on device disconnect */ if (fd == s->ctrl) { cb = hid_control; to_read = sizeof(data.b); } else if (fd == s->intr) { cb = hid_interrupt; to_read = sizeof(data.b); } else { assert(fd == s->vkbd); cb = kbd_status_changed; to_read = sizeof(data.s); } do { len = read(fd, &data, to_read); } while (len < 0 && errno == EINTR); if (len < 0) { syslog(LOG_ERR, "Could not read data from %s (%s). %s (%d)", bt_ntoa(&s->bdaddr, NULL), (fd == s->ctrl)? "control" : "interrupt", strerror(errno), errno); session_close(s); return (0); } if (len == 0) { syslog(LOG_NOTICE, "Remote device %s has closed %s connection", bt_ntoa(&s->bdaddr, NULL), (fd == s->ctrl)? "control" : "interrupt"); session_close(s); return (0); } (*cb)(s, (uint8_t *) &data, len); return (0); } Index: head/usr.sbin/bluetooth/bthidd/session.c =================================================================== --- head/usr.sbin/bluetooth/bthidd/session.c (revision 322439) +++ head/usr.sbin/bluetooth/bthidd/session.c (revision 322440) @@ -1,185 +1,187 @@ /* * session.c */ /*- * Copyright (c) 2006 Maksim Yevmenkin * 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: session.c,v 1.3 2006/09/07 21:06:53 max Exp $ * $FreeBSD$ */ #include #include #define L2CAP_SOCKET_CHECKED #include #include #include #include #include #include #include #include #include #include "bthid_config.h" #include "bthidd.h" #include "kbd.h" /* * Create new session */ bthid_session_p session_open(bthid_server_p srv, hid_device_p const d) { bthid_session_p s; assert(srv != NULL); assert(d != NULL); if ((s = (bthid_session_p) malloc(sizeof(*s))) == NULL) return (NULL); s->srv = srv; memcpy(&s->bdaddr, &d->bdaddr, sizeof(s->bdaddr)); s->ctrl = -1; s->intr = -1; + s->ctx = NULL; if (d->keyboard) { /* Open /dev/vkbdctl */ s->vkbd = open("/dev/vkbdctl", O_RDWR); if (s->vkbd < 0) { syslog(LOG_ERR, "Could not open /dev/vkbdctl " \ "for %s. %s (%d)", bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno); free(s); return (NULL); } } else s->vkbd = -1; s->state = CLOSED; s->keys1 = bit_alloc(kbd_maxkey()); if (s->keys1 == NULL) { free(s); return (NULL); } s->keys2 = bit_alloc(kbd_maxkey()); if (s->keys2 == NULL) { free(s->keys1); free(s); return (NULL); } LIST_INSERT_HEAD(&srv->sessions, s, next); return (s); } /* * Lookup session by bdaddr */ bthid_session_p session_by_bdaddr(bthid_server_p srv, bdaddr_p bdaddr) { bthid_session_p s; assert(srv != NULL); assert(bdaddr != NULL); LIST_FOREACH(s, &srv->sessions, next) if (memcmp(&s->bdaddr, bdaddr, sizeof(s->bdaddr)) == 0) break; return (s); } /* * Lookup session by fd */ bthid_session_p session_by_fd(bthid_server_p srv, int32_t fd) { bthid_session_p s; assert(srv != NULL); assert(fd >= 0); LIST_FOREACH(s, &srv->sessions, next) if (s->ctrl == fd || s->intr == fd || s->vkbd == fd) break; return (s); } /* * Close session */ void session_close(bthid_session_p s) { assert(s != NULL); assert(s->srv != NULL); LIST_REMOVE(s, next); if (s->intr != -1) { FD_CLR(s->intr, &s->srv->rfdset); FD_CLR(s->intr, &s->srv->wfdset); close(s->intr); if (s->srv->maxfd == s->intr) s->srv->maxfd --; } if (s->ctrl != -1) { FD_CLR(s->ctrl, &s->srv->rfdset); FD_CLR(s->ctrl, &s->srv->wfdset); close(s->ctrl); if (s->srv->maxfd == s->ctrl) s->srv->maxfd --; } if (s->vkbd != -1) { FD_CLR(s->vkbd, &s->srv->rfdset); close(s->vkbd); if (s->srv->maxfd == s->vkbd) s->srv->maxfd --; } + free(s->ctx); free(s->keys1); free(s->keys2); memset(s, 0, sizeof(*s)); free(s); }