tizen 2.3.1 release
[framework/connectivity/bluez.git] / profiles / input / device.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
6  *  Copyright (C) 2014       Google Inc.
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <stdbool.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <sys/ioctl.h>
35
36 #include "lib/bluetooth.h"
37 #include "lib/hidp.h"
38 #include "lib/sdp.h"
39 #include "lib/sdp_lib.h"
40 #include "lib/uuid.h"
41
42 #include "gdbus/gdbus.h"
43
44 #include "btio/btio.h"
45 #include "src/log.h"
46 #include "src/adapter.h"
47 #include "src/device.h"
48 #include "src/profile.h"
49 #include "src/service.h"
50 #include "src/storage.h"
51 #include "src/dbus-common.h"
52 #include "src/error.h"
53 #include "src/sdp-client.h"
54 #include "src/shared/uhid.h"
55
56 #include "device.h"
57 #include "hidp_defs.h"
58
59 #define INPUT_INTERFACE "org.bluez.Input1"
60
61 enum reconnect_mode_t {
62         RECONNECT_NONE = 0,
63         RECONNECT_DEVICE,
64         RECONNECT_HOST,
65         RECONNECT_ANY
66 };
67
68 struct input_device {
69         struct btd_service      *service;
70         struct btd_device       *device;
71         char                    *path;
72         bdaddr_t                src;
73         bdaddr_t                dst;
74         uint32_t                handle;
75         GIOChannel              *ctrl_io;
76         GIOChannel              *intr_io;
77         guint                   ctrl_watch;
78         guint                   intr_watch;
79         guint                   sec_watch;
80         struct hidp_connadd_req *req;
81         bool                    disable_sdp;
82         enum reconnect_mode_t   reconnect_mode;
83         guint                   reconnect_timer;
84         uint32_t                reconnect_attempt;
85         struct bt_uhid          *uhid;
86         bool                    uhid_created;
87         uint8_t                 report_req_pending;
88         guint                   report_req_timer;
89         uint32_t                report_rsp_id;
90 };
91
92 static int idle_timeout = 0;
93 static bool uhid_enabled = false;
94
95 void input_set_idle_timeout(int timeout)
96 {
97         idle_timeout = timeout;
98 }
99
100 void input_enable_userspace_hid(bool state)
101 {
102         uhid_enabled = state;
103 }
104
105 static void input_device_enter_reconnect_mode(struct input_device *idev);
106 static int connection_disconnect(struct input_device *idev, uint32_t flags);
107
108 static void input_device_free(struct input_device *idev)
109 {
110         bt_uhid_unref(idev->uhid);
111         btd_service_unref(idev->service);
112         btd_device_unref(idev->device);
113         g_free(idev->path);
114
115         if (idev->ctrl_watch > 0)
116                 g_source_remove(idev->ctrl_watch);
117
118         if (idev->intr_watch > 0)
119                 g_source_remove(idev->intr_watch);
120
121         if (idev->sec_watch > 0)
122                 g_source_remove(idev->sec_watch);
123
124         if (idev->intr_io)
125                 g_io_channel_unref(idev->intr_io);
126
127         if (idev->ctrl_io)
128                 g_io_channel_unref(idev->ctrl_io);
129
130         if (idev->req) {
131                 g_free(idev->req->rd_data);
132                 g_free(idev->req);
133         }
134
135         if (idev->reconnect_timer > 0)
136                 g_source_remove(idev->reconnect_timer);
137
138         if (idev->report_req_timer > 0)
139                 g_source_remove(idev->report_req_timer);
140
141         g_free(idev);
142 }
143
144 static bool hidp_send_message(GIOChannel *chan, uint8_t hdr,
145                                         const uint8_t *data, size_t size)
146 {
147         int fd;
148         ssize_t len;
149         uint8_t msg[size + 1];
150
151         if (!chan) {
152                 error("BT socket not connected");
153                 return false;
154         }
155
156         if (data == NULL)
157                 size = 0;
158
159         msg[0] = hdr;
160         if (size > 0)
161                 memcpy(&msg[1], data, size);
162         ++size;
163
164         fd = g_io_channel_unix_get_fd(chan);
165
166         len = write(fd, msg, size);
167         if (len < 0) {
168                 error("BT socket write error: %s (%d)", strerror(errno), errno);
169                 return false;
170         }
171
172         if ((size_t) len < size) {
173                 error("BT socket write error: partial write (%zd of %zu bytes)",
174                                                                 len, size);
175                 return false;
176         }
177
178         return true;
179 }
180
181 static bool hidp_send_ctrl_message(struct input_device *idev, uint8_t hdr,
182                                         const uint8_t *data, size_t size)
183 {
184         return hidp_send_message(idev->ctrl_io, hdr, data, size);
185 }
186
187 static bool hidp_send_intr_message(struct input_device *idev, uint8_t hdr,
188                                         const uint8_t *data, size_t size)
189 {
190         return hidp_send_message(idev->intr_io, hdr, data, size);
191 }
192
193 static bool uhid_send_feature_answer(struct input_device *idev,
194                                         const uint8_t *data, size_t size,
195                                         uint32_t id, uint16_t err)
196 {
197         struct uhid_event ev;
198         int ret;
199
200         if (data == NULL)
201                 size = 0;
202
203         if (size > sizeof(ev.u.feature_answer.data))
204                 size = sizeof(ev.u.feature_answer.data);
205
206         if (!idev->uhid_created) {
207                 DBG("HID report (%zu bytes) dropped", size);
208                 return false;
209         }
210
211         memset(&ev, 0, sizeof(ev));
212         ev.type = UHID_FEATURE_ANSWER;
213         ev.u.feature_answer.id = id;
214         ev.u.feature_answer.err = err;
215         ev.u.feature_answer.size = size;
216
217         if (size > 0)
218                 memcpy(ev.u.feature_answer.data, data, size);
219
220         ret = bt_uhid_send(idev->uhid, &ev);
221         if (ret < 0) {
222                 error("bt_uhid_send: %s (%d)", strerror(-ret), -ret);
223                 return false;
224         }
225
226         DBG("HID report (%zu bytes)", size);
227
228         return true;
229 }
230
231 static bool uhid_send_input_report(struct input_device *idev,
232                                         const uint8_t *data, size_t size)
233 {
234         struct uhid_event ev;
235         int err;
236
237         if (data == NULL)
238                 size = 0;
239
240         if (size > sizeof(ev.u.input.data))
241                 size = sizeof(ev.u.input.data);
242
243         if (!idev->uhid_created) {
244                 DBG("HID report (%zu bytes) dropped", size);
245                 return false;
246         }
247
248         memset(&ev, 0, sizeof(ev));
249         ev.type = UHID_INPUT;
250         ev.u.input.size = size;
251
252         if (size > 0)
253                 memcpy(ev.u.input.data, data, size);
254
255         err = bt_uhid_send(idev->uhid, &ev);
256         if (err < 0) {
257                 error("bt_uhid_send: %s (%d)", strerror(-err), -err);
258                 return false;
259         }
260
261         DBG("HID report (%zu bytes)", size);
262
263         return true;
264 }
265
266 static bool hidp_recv_intr_data(GIOChannel *chan, struct input_device *idev)
267 {
268         int fd;
269         ssize_t len;
270         uint8_t hdr;
271         uint8_t data[UHID_DATA_MAX + 1];
272
273         fd = g_io_channel_unix_get_fd(chan);
274
275         len = read(fd, data, sizeof(data));
276         if (len < 0) {
277                 error("BT socket read error: %s (%d)", strerror(errno), errno);
278                 return false;
279         }
280
281         if (len == 0) {
282                 DBG("BT socket read returned 0 bytes");
283                 return true;
284         }
285
286         hdr = data[0];
287         if (hdr != (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
288                 DBG("unsupported HIDP protocol header 0x%02x", hdr);
289                 return true;
290         }
291
292         if (len < 2) {
293                 DBG("received empty HID report");
294                 return true;
295         }
296
297         uhid_send_input_report(idev, data + 1, len - 1);
298
299         return true;
300 }
301
302 static gboolean intr_watch_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
303 {
304         struct input_device *idev = data;
305         char address[18];
306
307         if (cond & G_IO_IN) {
308                 if (hidp_recv_intr_data(chan, idev) && (cond == G_IO_IN))
309                         return TRUE;
310         }
311
312         ba2str(&idev->dst, address);
313
314         DBG("Device %s disconnected", address);
315
316         /* Checking for ctrl_watch avoids a double g_io_channel_shutdown since
317          * it's likely that ctrl_watch_cb has been queued for dispatching in
318          * this mainloop iteration */
319         if ((cond & (G_IO_HUP | G_IO_ERR)) && idev->ctrl_watch)
320                 g_io_channel_shutdown(chan, TRUE, NULL);
321
322         idev->intr_watch = 0;
323
324         if (idev->intr_io) {
325                 g_io_channel_unref(idev->intr_io);
326                 idev->intr_io = NULL;
327         }
328
329         /* Close control channel */
330         if (idev->ctrl_io && !(cond & G_IO_NVAL))
331                 g_io_channel_shutdown(idev->ctrl_io, TRUE, NULL);
332
333         btd_service_disconnecting_complete(idev->service, 0);
334
335         /* Enter the auto-reconnect mode if needed */
336         input_device_enter_reconnect_mode(idev);
337
338         return FALSE;
339 }
340
341 static void hidp_recv_ctrl_handshake(struct input_device *idev, uint8_t param)
342 {
343         bool pending_req_complete = false;
344         uint8_t pending_req_type;
345
346         DBG("");
347
348         pending_req_type = idev->report_req_pending & HIDP_HEADER_TRANS_MASK;
349
350         switch (param) {
351         case HIDP_HSHK_SUCCESSFUL:
352                 if (pending_req_type == HIDP_TRANS_SET_REPORT) {
353                         DBG("SET_REPORT successful");
354                         pending_req_complete = true;
355                 } else
356                         DBG("Spurious HIDP_HSHK_SUCCESSFUL");
357                 break;
358
359         case HIDP_HSHK_NOT_READY:
360         case HIDP_HSHK_ERR_INVALID_REPORT_ID:
361         case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST:
362         case HIDP_HSHK_ERR_INVALID_PARAMETER:
363         case HIDP_HSHK_ERR_UNKNOWN:
364         case HIDP_HSHK_ERR_FATAL:
365                 if (pending_req_type == HIDP_TRANS_GET_REPORT) {
366                         DBG("GET_REPORT failed (%u)", param);
367                         uhid_send_feature_answer(idev, NULL, 0,
368                                                 idev->report_rsp_id, EIO);
369                         pending_req_complete = true;
370                 } else if (pending_req_type == HIDP_TRANS_SET_REPORT) {
371                         DBG("SET_REPORT failed (%u)", param);
372                         pending_req_complete = true;
373                 } else
374                         DBG("Spurious HIDP_HSHK_ERR");
375
376                 if (param == HIDP_HSHK_ERR_FATAL)
377                         hidp_send_ctrl_message(idev, HIDP_TRANS_HID_CONTROL |
378                                                 HIDP_CTRL_SOFT_RESET, NULL, 0);
379                 break;
380
381         default:
382                 hidp_send_ctrl_message(idev, HIDP_TRANS_HANDSHAKE |
383                                 HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
384                 break;
385         }
386
387         if (pending_req_complete) {
388                 idev->report_req_pending = 0;
389                 if (idev->report_req_timer > 0) {
390                         g_source_remove(idev->report_req_timer);
391                         idev->report_req_timer = 0;
392                 }
393                 idev->report_rsp_id = 0;
394         }
395 }
396
397 static void hidp_recv_ctrl_hid_control(struct input_device *idev, uint8_t param)
398 {
399         DBG("");
400
401         if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG)
402                 connection_disconnect(idev, 0);
403 }
404
405 static void hidp_recv_ctrl_data(struct input_device *idev, uint8_t param,
406                                         const uint8_t *data, size_t size)
407 {
408         uint8_t pending_req_type;
409         uint8_t pending_req_param;
410
411         DBG("");
412
413         pending_req_type = idev->report_req_pending & HIDP_HEADER_TRANS_MASK;
414         if (pending_req_type != HIDP_TRANS_GET_REPORT) {
415                 DBG("Spurious DATA on control channel");
416                 return;
417         }
418
419         pending_req_param = idev->report_req_pending & HIDP_HEADER_PARAM_MASK;
420         if (pending_req_param != param) {
421                 DBG("Received DATA RTYPE doesn't match pending request RTYPE");
422                 return;
423         }
424
425         switch (param) {
426         case HIDP_DATA_RTYPE_FEATURE:
427         case HIDP_DATA_RTYPE_INPUT:
428         case HIDP_DATA_RTYPE_OUPUT:
429                 uhid_send_feature_answer(idev, data + 1, size - 1,
430                                                         idev->report_rsp_id, 0);
431                 break;
432
433         case HIDP_DATA_RTYPE_OTHER:
434                 DBG("Received DATA_RTYPE_OTHER");
435                 break;
436
437         default:
438                 hidp_send_ctrl_message(idev, HIDP_TRANS_HANDSHAKE |
439                                 HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
440                 break;
441         }
442
443         idev->report_req_pending = 0;
444         if (idev->report_req_timer > 0) {
445                 g_source_remove(idev->report_req_timer);
446                 idev->report_req_timer = 0;
447         }
448         idev->report_rsp_id = 0;
449 }
450
451 static bool hidp_recv_ctrl_message(GIOChannel *chan, struct input_device *idev)
452 {
453         int fd;
454         ssize_t len;
455         uint8_t hdr, type, param;
456         uint8_t data[UHID_DATA_MAX + 1];
457
458         fd = g_io_channel_unix_get_fd(chan);
459
460         len = read(fd, data, sizeof(data));
461         if (len < 0) {
462                 error("BT socket read error: %s (%d)", strerror(errno), errno);
463                 return false;
464         }
465
466         if (len == 0) {
467                 DBG("BT socket read returned 0 bytes");
468                 return true;
469         }
470
471         hdr = data[0];
472         type = hdr & HIDP_HEADER_TRANS_MASK;
473         param = hdr & HIDP_HEADER_PARAM_MASK;
474
475         switch (type) {
476         case HIDP_TRANS_HANDSHAKE:
477                 hidp_recv_ctrl_handshake(idev, param);
478                 break;
479         case HIDP_TRANS_HID_CONTROL:
480                 hidp_recv_ctrl_hid_control(idev, param);
481                 break;
482         case HIDP_TRANS_DATA:
483                 hidp_recv_ctrl_data(idev, param, data, len);
484                 break;
485         default:
486                 error("unsupported HIDP control message");
487                 hidp_send_ctrl_message(idev, HIDP_TRANS_HANDSHAKE |
488                                 HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0);
489                 break;
490         }
491
492         return true;
493 }
494
495 static gboolean ctrl_watch_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
496 {
497         struct input_device *idev = data;
498         char address[18];
499
500         if (cond & G_IO_IN) {
501                 if (hidp_recv_ctrl_message(chan, idev) && (cond == G_IO_IN))
502                         return TRUE;
503         }
504
505         ba2str(&idev->dst, address);
506
507         DBG("Device %s disconnected", address);
508
509         /* Checking for intr_watch avoids a double g_io_channel_shutdown since
510          * it's likely that intr_watch_cb has been queued for dispatching in
511          * this mainloop iteration */
512         if ((cond & (G_IO_HUP | G_IO_ERR)) && idev->intr_watch)
513                 g_io_channel_shutdown(chan, TRUE, NULL);
514
515         idev->ctrl_watch = 0;
516
517         if (idev->ctrl_io) {
518                 g_io_channel_unref(idev->ctrl_io);
519                 idev->ctrl_io = NULL;
520         }
521
522         /* Close interrupt channel */
523         if (idev->intr_io && !(cond & G_IO_NVAL))
524                 g_io_channel_shutdown(idev->intr_io, TRUE, NULL);
525
526         return FALSE;
527 }
528
529 #define REPORT_REQ_TIMEOUT  3
530
531 static gboolean hidp_report_req_timeout(gpointer data)
532 {
533         struct input_device *idev = data;
534         uint8_t pending_req_type;
535         const char *req_type_str;
536         char address[18];
537
538         ba2str(&idev->dst, address);
539         pending_req_type = idev->report_req_pending & HIDP_HEADER_TRANS_MASK;
540
541         switch (pending_req_type) {
542         case HIDP_TRANS_GET_REPORT:
543                 req_type_str = "GET_REPORT";
544                 break;
545         case HIDP_TRANS_SET_REPORT:
546                 req_type_str = "SET_REPORT";
547                 break;
548         default:
549                 /* Should never happen */
550                 req_type_str = "OTHER_TRANS";
551                 break;
552         }
553
554         DBG("Device %s HIDP %s request timed out", address, req_type_str);
555
556         idev->report_req_pending = 0;
557         idev->report_req_timer = 0;
558         idev->report_rsp_id = 0;
559
560         return FALSE;
561 }
562
563 static void hidp_send_set_report(struct uhid_event *ev, void *user_data)
564 {
565         struct input_device *idev = user_data;
566         uint8_t hdr;
567         bool sent;
568
569         DBG("");
570
571         switch (ev->u.output.rtype) {
572         case UHID_FEATURE_REPORT:
573                 /* Send SET_REPORT on control channel */
574                 if (idev->report_req_pending) {
575                         DBG("Old GET_REPORT or SET_REPORT still pending");
576                         return;
577                 }
578
579                 hdr = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE;
580                 sent = hidp_send_ctrl_message(idev, hdr, ev->u.output.data,
581                                                         ev->u.output.size);
582                 if (sent) {
583                         idev->report_req_pending = hdr;
584                         idev->report_req_timer =
585                                 g_timeout_add_seconds(REPORT_REQ_TIMEOUT,
586                                                 hidp_report_req_timeout, idev);
587                 }
588                 break;
589         case UHID_OUTPUT_REPORT:
590                 /* Send DATA on interrupt channel */
591                 hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
592                 hidp_send_intr_message(idev, hdr, ev->u.output.data,
593                                                         ev->u.output.size);
594                 break;
595         default:
596                 DBG("Unsupported HID report type %u", ev->u.output.rtype);
597                 return;
598         }
599 }
600
601 static void hidp_send_get_report(struct uhid_event *ev, void *user_data)
602 {
603         struct input_device *idev = user_data;
604         uint8_t hdr;
605         bool sent;
606
607         DBG("");
608
609         if (idev->report_req_pending) {
610                 DBG("Old GET_REPORT or SET_REPORT still pending");
611                 uhid_send_feature_answer(idev, NULL, 0, ev->u.feature.id,
612                                                                         EBUSY);
613                 return;
614         }
615
616         /* Send GET_REPORT on control channel */
617         switch (ev->u.feature.rtype) {
618         case UHID_FEATURE_REPORT:
619                 hdr = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_FEATURE;
620                 break;
621         case UHID_INPUT_REPORT:
622                 hdr = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_INPUT;
623                 break;
624         case UHID_OUTPUT_REPORT:
625                 hdr = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_OUPUT;
626                 break;
627         default:
628                 DBG("Unsupported HID report type %u", ev->u.feature.rtype);
629                 return;
630         }
631
632         sent = hidp_send_ctrl_message(idev, hdr, &ev->u.feature.rnum,
633                                                 sizeof(ev->u.feature.rnum));
634         if (sent) {
635                 idev->report_req_pending = hdr;
636                 idev->report_req_timer =
637                         g_timeout_add_seconds(REPORT_REQ_TIMEOUT,
638                                                 hidp_report_req_timeout, idev);
639                 idev->report_rsp_id = ev->u.feature.id;
640         }
641 }
642
643 static void epox_endian_quirk(unsigned char *data, int size)
644 {
645         /* USAGE_PAGE (Keyboard)        05 07
646          * USAGE_MINIMUM (0)            19 00
647          * USAGE_MAXIMUM (65280)        2A 00 FF   <= must be FF 00
648          * LOGICAL_MINIMUM (0)          15 00
649          * LOGICAL_MAXIMUM (65280)      26 00 FF   <= must be FF 00
650          */
651         unsigned char pattern[] = { 0x05, 0x07, 0x19, 0x00, 0x2a, 0x00, 0xff,
652                                                 0x15, 0x00, 0x26, 0x00, 0xff };
653         unsigned int i;
654
655         if (!data)
656                 return;
657
658         for (i = 0; i < size - sizeof(pattern); i++) {
659                 if (!memcmp(data + i, pattern, sizeof(pattern))) {
660                         data[i + 5] = 0xff;
661                         data[i + 6] = 0x00;
662                         data[i + 10] = 0xff;
663                         data[i + 11] = 0x00;
664                 }
665         }
666 }
667
668 static int create_hid_dev_name(sdp_record_t *rec, struct hidp_connadd_req *req)
669 {
670         char sdesc[sizeof(req->name)];
671
672         if (sdp_get_service_desc(rec, sdesc, sizeof(sdesc)) == 0) {
673                 char pname[sizeof(req->name)];
674
675                 if (sdp_get_provider_name(rec, pname, sizeof(pname)) == 0 &&
676                                                 strncmp(sdesc, pname, 5) != 0)
677                         snprintf(req->name, sizeof(req->name), "%s %s", pname,
678                                                                         sdesc);
679                 else
680                         snprintf(req->name, sizeof(req->name), "%s", sdesc);
681         } else {
682                 return sdp_get_service_name(rec, req->name, sizeof(req->name));
683         }
684
685         return 0;
686 }
687
688 /* See HID profile specification v1.0, "7.11.6 HIDDescriptorList" for details
689  * on the attribute format. */
690 static int extract_hid_desc_data(sdp_record_t *rec,
691                                                 struct hidp_connadd_req *req)
692 {
693         sdp_data_t *d;
694
695         d = sdp_data_get(rec, SDP_ATTR_HID_DESCRIPTOR_LIST);
696         if (!d)
697                 goto invalid_desc;
698
699         if (!SDP_IS_SEQ(d->dtd))
700                 goto invalid_desc;
701
702         /* First HIDDescriptor */
703         d = d->val.dataseq;
704         if (!SDP_IS_SEQ(d->dtd))
705                 goto invalid_desc;
706
707         /* ClassDescriptorType */
708         d = d->val.dataseq;
709         if (d->dtd != SDP_UINT8)
710                 goto invalid_desc;
711
712         /* ClassDescriptorData */
713         d = d->next;
714         if (!d || !SDP_IS_TEXT_STR(d->dtd))
715                 goto invalid_desc;
716
717         req->rd_data = g_try_malloc0(d->unitSize);
718         if (req->rd_data) {
719                 memcpy(req->rd_data, d->val.str, d->unitSize);
720                 req->rd_size = d->unitSize;
721                 epox_endian_quirk(req->rd_data, req->rd_size);
722         }
723
724         return 0;
725
726 invalid_desc:
727         error("Missing or invalid HIDDescriptorList SDP attribute");
728         return -EINVAL;
729 }
730
731 static int extract_hid_record(sdp_record_t *rec, struct hidp_connadd_req *req)
732 {
733         sdp_data_t *pdlist;
734         uint8_t attr_val;
735         int err;
736
737         err = create_hid_dev_name(rec, req);
738         if (err < 0)
739                 DBG("No valid Service Name or Service Description found");
740
741         pdlist = sdp_data_get(rec, SDP_ATTR_HID_PARSER_VERSION);
742         req->parser = pdlist ? pdlist->val.uint16 : 0x0100;
743
744         pdlist = sdp_data_get(rec, SDP_ATTR_HID_DEVICE_SUBCLASS);
745         req->subclass = pdlist ? pdlist->val.uint8 : 0;
746
747         pdlist = sdp_data_get(rec, SDP_ATTR_HID_COUNTRY_CODE);
748         req->country = pdlist ? pdlist->val.uint8 : 0;
749
750         pdlist = sdp_data_get(rec, SDP_ATTR_HID_VIRTUAL_CABLE);
751         attr_val = pdlist ? pdlist->val.uint8 : 0;
752         if (attr_val)
753                 req->flags |= (1 << HIDP_VIRTUAL_CABLE_UNPLUG);
754
755         pdlist = sdp_data_get(rec, SDP_ATTR_HID_BOOT_DEVICE);
756         attr_val = pdlist ? pdlist->val.uint8 : 0;
757         if (attr_val)
758                 req->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE);
759
760         err = extract_hid_desc_data(rec, req);
761         if (err < 0)
762                 return err;
763
764         return 0;
765 }
766
767 static int ioctl_connadd(struct hidp_connadd_req *req)
768 {
769         int ctl, err = 0;
770
771         ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HIDP);
772         if (ctl < 0)
773                 return -errno;
774
775         if (ioctl(ctl, HIDPCONNADD, req) < 0)
776                 err = -errno;
777
778         close(ctl);
779
780         return err;
781 }
782
783 static bool ioctl_is_connected(struct input_device *idev)
784 {
785         struct hidp_conninfo ci;
786         int ctl;
787
788         /* Standard HID */
789         ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HIDP);
790         if (ctl < 0) {
791                 error("Can't open HIDP control socket");
792                 return false;
793         }
794
795         memset(&ci, 0, sizeof(ci));
796         bacpy(&ci.bdaddr, &idev->dst);
797         if (ioctl(ctl, HIDPGETCONNINFO, &ci) < 0) {
798                 error("Can't get HIDP connection info");
799                 close(ctl);
800                 return false;
801         }
802
803         close(ctl);
804
805         if (ci.state != BT_CONNECTED)
806                 return false;
807
808         return true;
809 }
810
811 static int ioctl_disconnect(struct input_device *idev, uint32_t flags)
812 {
813         struct hidp_conndel_req req;
814         struct hidp_conninfo ci;
815         int ctl, err = 0;
816
817         ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HIDP);
818         if (ctl < 0) {
819                 error("Can't open HIDP control socket");
820                 return -errno;
821         }
822
823         memset(&ci, 0, sizeof(ci));
824         bacpy(&ci.bdaddr, &idev->dst);
825         if ((ioctl(ctl, HIDPGETCONNINFO, &ci) < 0) ||
826                                                 (ci.state != BT_CONNECTED)) {
827                 close(ctl);
828                 return -ENOTCONN;
829         }
830
831         memset(&req, 0, sizeof(req));
832         bacpy(&req.bdaddr, &idev->dst);
833         req.flags = flags;
834         if (ioctl(ctl, HIDPCONNDEL, &req) < 0) {
835                 err = -errno;
836                 error("Can't delete the HID device: %s (%d)",
837                                                         strerror(-err), -err);
838         }
839
840         close(ctl);
841
842         return err;
843 }
844
845 static int uhid_connadd(struct input_device *idev, struct hidp_connadd_req *req)
846 {
847         int err;
848         struct uhid_event ev;
849
850         if (idev->uhid_created)
851                 return 0;
852
853         /* create uHID device */
854         memset(&ev, 0, sizeof(ev));
855         ev.type = UHID_CREATE;
856         strncpy((char *) ev.u.create.name, req->name,
857                                                 sizeof(ev.u.create.name) - 1);
858         ba2str(&idev->src, (char *) ev.u.create.phys);
859         ba2str(&idev->dst, (char *) ev.u.create.uniq);
860         ev.u.create.vendor = req->vendor;
861         ev.u.create.product = req->product;
862         ev.u.create.version = req->version;
863         ev.u.create.country = req->country;
864         ev.u.create.bus = BUS_BLUETOOTH;
865         ev.u.create.rd_data = req->rd_data;
866         ev.u.create.rd_size = req->rd_size;
867
868         err = bt_uhid_send(idev->uhid, &ev);
869         if (err < 0) {
870                 error("bt_uhid_send: %s", strerror(-err));
871                 return err;
872         }
873
874         bt_uhid_register(idev->uhid, UHID_OUTPUT, hidp_send_set_report, idev);
875         bt_uhid_register(idev->uhid, UHID_FEATURE, hidp_send_get_report, idev);
876
877         idev->uhid_created = true;
878
879         return err;
880 }
881
882 static gboolean encrypt_notify(GIOChannel *io, GIOCondition condition,
883                                                                 gpointer data)
884 {
885         struct input_device *idev = data;
886         int err;
887
888         DBG("");
889
890         if (idev->uhid)
891                 err = uhid_connadd(idev, idev->req);
892         else
893                 err = ioctl_connadd(idev->req);
894
895         if (err < 0) {
896                 error("ioctl_connadd(): %s (%d)", strerror(-err), -err);
897
898                 if (idev->ctrl_io) {
899                         g_io_channel_shutdown(idev->ctrl_io, FALSE, NULL);
900                         g_io_channel_unref(idev->ctrl_io);
901                         idev->ctrl_io = NULL;
902                 }
903
904                 if (idev->intr_io) {
905                         g_io_channel_shutdown(idev->intr_io, FALSE, NULL);
906                         g_io_channel_unref(idev->intr_io);
907                         idev->intr_io = NULL;
908                 }
909         }
910
911         idev->sec_watch = 0;
912
913         g_free(idev->req->rd_data);
914         g_free(idev->req);
915         idev->req = NULL;
916
917         return FALSE;
918 }
919
920 static int hidp_add_connection(struct input_device *idev)
921 {
922         struct hidp_connadd_req *req;
923         sdp_record_t *rec;
924         char src_addr[18], dst_addr[18];
925         char filename[PATH_MAX];
926         GKeyFile *key_file;
927         char handle[11], *str;
928         GError *gerr = NULL;
929         int err;
930
931         req = g_new0(struct hidp_connadd_req, 1);
932         req->ctrl_sock = g_io_channel_unix_get_fd(idev->ctrl_io);
933         req->intr_sock = g_io_channel_unix_get_fd(idev->intr_io);
934         req->flags     = 0;
935         req->idle_to   = idle_timeout;
936
937         ba2str(&idev->src, src_addr);
938         ba2str(&idev->dst, dst_addr);
939
940         snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s", src_addr,
941                                                                 dst_addr);
942         sprintf(handle, "0x%8.8X", idev->handle);
943
944         key_file = g_key_file_new();
945         g_key_file_load_from_file(key_file, filename, 0, NULL);
946         str = g_key_file_get_string(key_file, "ServiceRecords", handle, NULL);
947         g_key_file_free(key_file);
948
949         if (!str) {
950                 error("Rejected connection from unknown device %s", dst_addr);
951                 err = -EPERM;
952                 goto cleanup;
953         }
954
955         rec = record_from_string(str);
956         g_free(str);
957
958         err = extract_hid_record(rec, req);
959         sdp_record_free(rec);
960         if (err < 0) {
961                 error("Could not parse HID SDP record: %s (%d)", strerror(-err),
962                                                                         -err);
963                 goto cleanup;
964         }
965
966         req->vendor = btd_device_get_vendor(idev->device);
967         req->product = btd_device_get_product(idev->device);
968         req->version = btd_device_get_version(idev->device);
969
970         if (device_name_known(idev->device))
971                 device_get_name(idev->device, req->name, sizeof(req->name));
972
973         /* Encryption is mandatory for keyboards */
974         if (req->subclass & 0x40) {
975                 if (!bt_io_set(idev->intr_io, &gerr,
976                                         BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
977                                         BT_IO_OPT_INVALID)) {
978                         error("btio: %s", gerr->message);
979                         g_error_free(gerr);
980                         err = -EFAULT;
981                         goto cleanup;
982                 }
983
984                 idev->req = req;
985                 idev->sec_watch = g_io_add_watch(idev->intr_io, G_IO_OUT,
986                                                         encrypt_notify, idev);
987
988                 return 0;
989         }
990
991         if (idev->uhid)
992                 err = uhid_connadd(idev, req);
993         else
994                 err = ioctl_connadd(req);
995
996 cleanup:
997         g_free(req->rd_data);
998         g_free(req);
999
1000         return err;
1001 }
1002
1003 static bool is_connected(struct input_device *idev)
1004 {
1005         if (idev->uhid)
1006                 return (idev->intr_io != NULL && idev->ctrl_io != NULL);
1007         else
1008                 return ioctl_is_connected(idev);
1009 }
1010
1011 static int connection_disconnect(struct input_device *idev, uint32_t flags)
1012 {
1013         if (!is_connected(idev))
1014                 return -ENOTCONN;
1015
1016         /* Standard HID disconnect */
1017         if (idev->intr_io)
1018                 g_io_channel_shutdown(idev->intr_io, TRUE, NULL);
1019         if (idev->ctrl_io)
1020                 g_io_channel_shutdown(idev->ctrl_io, TRUE, NULL);
1021
1022         if (idev->uhid)
1023                 return 0;
1024         else
1025                 return ioctl_disconnect(idev, flags);
1026 }
1027
1028 static int input_device_connected(struct input_device *idev)
1029 {
1030         int err;
1031
1032         if (idev->intr_io == NULL || idev->ctrl_io == NULL)
1033                 return -ENOTCONN;
1034
1035         err = hidp_add_connection(idev);
1036         if (err < 0)
1037                 return err;
1038
1039         btd_service_connecting_complete(idev->service, 0);
1040
1041         return 0;
1042 }
1043
1044 static void interrupt_connect_cb(GIOChannel *chan, GError *conn_err,
1045                                                         gpointer user_data)
1046 {
1047         struct input_device *idev = user_data;
1048         GIOCondition cond = G_IO_HUP | G_IO_ERR | G_IO_NVAL;
1049         int err;
1050
1051         if (conn_err) {
1052                 err = -EIO;
1053                 goto failed;
1054         }
1055
1056         err = input_device_connected(idev);
1057         if (err < 0)
1058                 goto failed;
1059
1060         if (idev->uhid)
1061                 cond |= G_IO_IN;
1062
1063         idev->intr_watch = g_io_add_watch(idev->intr_io, cond, intr_watch_cb,
1064                                                                         idev);
1065
1066         return;
1067
1068 failed:
1069         btd_service_connecting_complete(idev->service, err);
1070
1071         /* So we guarantee the interrupt channel is closed before the
1072          * control channel (if we only do unref GLib will close it only
1073          * after returning control to the mainloop */
1074         if (!conn_err)
1075                 g_io_channel_shutdown(idev->intr_io, FALSE, NULL);
1076
1077         g_io_channel_unref(idev->intr_io);
1078         idev->intr_io = NULL;
1079
1080         if (idev->ctrl_io) {
1081                 g_io_channel_unref(idev->ctrl_io);
1082                 idev->ctrl_io = NULL;
1083         }
1084 }
1085
1086 static void control_connect_cb(GIOChannel *chan, GError *conn_err,
1087                                                         gpointer user_data)
1088 {
1089         struct input_device *idev = user_data;
1090         GIOCondition cond = G_IO_HUP | G_IO_ERR | G_IO_NVAL;
1091         GIOChannel *io;
1092         GError *err = NULL;
1093
1094         if (conn_err) {
1095                 error("%s", conn_err->message);
1096                 goto failed;
1097         }
1098
1099         /* Connect to the HID interrupt channel */
1100         io = bt_io_connect(interrupt_connect_cb, idev,
1101                                 NULL, &err,
1102                                 BT_IO_OPT_SOURCE_BDADDR, &idev->src,
1103                                 BT_IO_OPT_DEST_BDADDR, &idev->dst,
1104                                 BT_IO_OPT_PSM, L2CAP_PSM_HIDP_INTR,
1105                                 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
1106                                 BT_IO_OPT_INVALID);
1107         if (!io) {
1108                 error("%s", err->message);
1109                 g_error_free(err);
1110                 goto failed;
1111         }
1112
1113         idev->intr_io = io;
1114
1115         if (idev->uhid)
1116                 cond |= G_IO_IN;
1117
1118         idev->ctrl_watch = g_io_add_watch(idev->ctrl_io, cond, ctrl_watch_cb,
1119                                                                         idev);
1120
1121         return;
1122
1123 failed:
1124         btd_service_connecting_complete(idev->service, -EIO);
1125         g_io_channel_unref(idev->ctrl_io);
1126         idev->ctrl_io = NULL;
1127 }
1128
1129 static int dev_connect(struct input_device *idev)
1130 {
1131         GError *err = NULL;
1132         GIOChannel *io;
1133
1134         if (idev->disable_sdp)
1135                 bt_clear_cached_session(&idev->src, &idev->dst);
1136
1137         io = bt_io_connect(control_connect_cb, idev,
1138                                 NULL, &err,
1139                                 BT_IO_OPT_SOURCE_BDADDR, &idev->src,
1140                                 BT_IO_OPT_DEST_BDADDR, &idev->dst,
1141                                 BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL,
1142                                 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
1143                                 BT_IO_OPT_INVALID);
1144         idev->ctrl_io = io;
1145
1146         if (err == NULL)
1147                 return 0;
1148
1149         error("%s", err->message);
1150         g_error_free(err);
1151
1152         return -EIO;
1153 }
1154
1155 static gboolean input_device_auto_reconnect(gpointer user_data)
1156 {
1157         struct input_device *idev = user_data;
1158
1159         DBG("path=%s, attempt=%d", idev->path, idev->reconnect_attempt);
1160
1161         /* Stop the recurrent reconnection attempts if the device is
1162          * reconnected or is marked for removal.
1163          */
1164         if (device_is_temporary(idev->device) ||
1165                                         btd_device_is_connected(idev->device))
1166                 return FALSE;
1167
1168         /* Only attempt an auto-reconnect for at most 3 minutes (6 * 30s). */
1169         if (idev->reconnect_attempt >= 6)
1170                 return FALSE;
1171
1172         /* Check if the profile is already connected. */
1173         if (idev->ctrl_io)
1174                 return FALSE;
1175
1176         if (is_connected(idev))
1177                 return FALSE;
1178
1179         idev->reconnect_attempt++;
1180         dev_connect(idev);
1181
1182         return TRUE;
1183 }
1184
1185 static const char * const _reconnect_mode_str[] = {
1186         "none",
1187         "device",
1188         "host",
1189         "any"
1190 };
1191
1192 static const char *reconnect_mode_to_string(const enum reconnect_mode_t mode)
1193 {
1194         return _reconnect_mode_str[mode];
1195 }
1196
1197 static void input_device_enter_reconnect_mode(struct input_device *idev)
1198 {
1199         DBG("path=%s reconnect_mode=%s", idev->path,
1200                                 reconnect_mode_to_string(idev->reconnect_mode));
1201
1202         /* Only attempt an auto-reconnect when the device is required to
1203          * accept reconnections from the host.
1204          */
1205         if (idev->reconnect_mode != RECONNECT_ANY &&
1206                                 idev->reconnect_mode != RECONNECT_HOST)
1207                 return;
1208
1209         /* If the device is temporary we are not required to reconnect
1210          * with the device. This is likely the case of a removing device.
1211          */
1212         if (device_is_temporary(idev->device) ||
1213                                         btd_device_is_connected(idev->device))
1214                 return;
1215
1216         if (idev->reconnect_timer > 0)
1217                 g_source_remove(idev->reconnect_timer);
1218
1219         DBG("registering auto-reconnect");
1220         idev->reconnect_attempt = 0;
1221         idev->reconnect_timer = g_timeout_add_seconds(30,
1222                                         input_device_auto_reconnect, idev);
1223
1224 }
1225
1226 int input_device_connect(struct btd_service *service)
1227 {
1228         struct input_device *idev;
1229
1230         DBG("");
1231
1232         idev = btd_service_get_user_data(service);
1233
1234         if (idev->ctrl_io)
1235                 return -EBUSY;
1236
1237         if (is_connected(idev))
1238                 return -EALREADY;
1239
1240         return dev_connect(idev);
1241 }
1242
1243 int input_device_disconnect(struct btd_service *service)
1244 {
1245         struct input_device *idev;
1246         int err, flags;
1247
1248         DBG("");
1249
1250         idev = btd_service_get_user_data(service);
1251
1252         flags = device_is_temporary(idev->device) ?
1253                                         (1 << HIDP_VIRTUAL_CABLE_UNPLUG) : 0;
1254
1255         err = connection_disconnect(idev, flags);
1256         if (err < 0)
1257                 return err;
1258
1259         return 0;
1260 }
1261
1262 static bool is_device_sdp_disable(const sdp_record_t *rec)
1263 {
1264         sdp_data_t *data;
1265
1266         data = sdp_data_get(rec, SDP_ATTR_HID_SDP_DISABLE);
1267
1268         return data && data->val.uint8;
1269 }
1270
1271 static enum reconnect_mode_t hid_reconnection_mode(bool reconnect_initiate,
1272                                                 bool normally_connectable)
1273 {
1274         if (!reconnect_initiate && !normally_connectable)
1275                 return RECONNECT_NONE;
1276         else if (!reconnect_initiate && normally_connectable)
1277                 return RECONNECT_HOST;
1278         else if (reconnect_initiate && !normally_connectable)
1279                 return RECONNECT_DEVICE;
1280         else /* (reconnect_initiate && normally_connectable) */
1281                 return RECONNECT_ANY;
1282 }
1283
1284 static void extract_hid_props(struct input_device *idev,
1285                                         const sdp_record_t *rec)
1286 {
1287         /* Extract HID connectability */
1288         bool reconnect_initiate, normally_connectable;
1289         sdp_data_t *pdlist;
1290
1291         /* HIDNormallyConnectable is optional and assumed FALSE
1292         * if not present. */
1293         pdlist = sdp_data_get(rec, SDP_ATTR_HID_RECONNECT_INITIATE);
1294         reconnect_initiate = pdlist ? pdlist->val.uint8 : TRUE;
1295
1296         pdlist = sdp_data_get(rec, SDP_ATTR_HID_NORMALLY_CONNECTABLE);
1297         normally_connectable = pdlist ? pdlist->val.uint8 : FALSE;
1298
1299         /* Update local values */
1300         idev->reconnect_mode =
1301                 hid_reconnection_mode(reconnect_initiate, normally_connectable);
1302 }
1303
1304 static struct input_device *input_device_new(struct btd_service *service)
1305 {
1306         struct btd_device *device = btd_service_get_device(service);
1307         struct btd_profile *p = btd_service_get_profile(service);
1308         const char *path = device_get_path(device);
1309         const sdp_record_t *rec = btd_device_get_record(device, p->remote_uuid);
1310         struct btd_adapter *adapter = device_get_adapter(device);
1311         struct input_device *idev;
1312
1313         if (!rec)
1314                 return NULL;
1315
1316         idev = g_new0(struct input_device, 1);
1317         bacpy(&idev->src, btd_adapter_get_address(adapter));
1318         bacpy(&idev->dst, device_get_address(device));
1319         idev->service = btd_service_ref(service);
1320         idev->device = btd_device_ref(device);
1321         idev->path = g_strdup(path);
1322         idev->handle = rec->handle;
1323         idev->disable_sdp = is_device_sdp_disable(rec);
1324
1325         /* Initialize device properties */
1326         extract_hid_props(idev, rec);
1327
1328         return idev;
1329 }
1330
1331 static gboolean property_get_reconnect_mode(
1332                                         const GDBusPropertyTable *property,
1333                                         DBusMessageIter *iter, void *data)
1334 {
1335         struct input_device *idev = data;
1336         const char *str_mode = reconnect_mode_to_string(idev->reconnect_mode);
1337
1338         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &str_mode);
1339
1340         return TRUE;
1341 }
1342
1343 static const GDBusPropertyTable input_properties[] = {
1344         { "ReconnectMode", "s", property_get_reconnect_mode },
1345         { }
1346 };
1347
1348 int input_device_register(struct btd_service *service)
1349 {
1350         struct btd_device *device = btd_service_get_device(service);
1351         const char *path = device_get_path(device);
1352         struct input_device *idev;
1353
1354         DBG("%s", path);
1355
1356         idev = input_device_new(service);
1357         if (!idev)
1358                 return -EINVAL;
1359
1360         if (uhid_enabled) {
1361                 idev->uhid = bt_uhid_new_default();
1362                 if (!idev->uhid) {
1363                         error("bt_uhid_new_default: failed");
1364                         input_device_free(idev);
1365                         return -EIO;
1366                 }
1367         }
1368
1369         if (g_dbus_register_interface(btd_get_dbus_connection(),
1370                                         idev->path, INPUT_INTERFACE,
1371                                         NULL, NULL,
1372                                         input_properties, idev,
1373                                         NULL) == FALSE) {
1374                 error("Unable to register %s interface", INPUT_INTERFACE);
1375                 input_device_free(idev);
1376                 return -EINVAL;
1377         }
1378
1379         btd_service_set_user_data(service, idev);
1380
1381         return 0;
1382 }
1383
1384 static struct input_device *find_device(const bdaddr_t *src,
1385                                         const bdaddr_t *dst)
1386 {
1387         struct btd_device *device;
1388         struct btd_service *service;
1389
1390         device = btd_adapter_find_device(adapter_find(src), dst, BDADDR_BREDR);
1391         if (device == NULL)
1392                 return NULL;
1393
1394         service = btd_device_get_service(device, HID_UUID);
1395         if (service == NULL)
1396                 return NULL;
1397
1398         return btd_service_get_user_data(service);
1399 }
1400
1401 void input_device_unregister(struct btd_service *service)
1402 {
1403         struct btd_device *device = btd_service_get_device(service);
1404         const char *path = device_get_path(device);
1405         struct input_device *idev = btd_service_get_user_data(service);
1406
1407         DBG("%s", path);
1408
1409         g_dbus_unregister_interface(btd_get_dbus_connection(),
1410                                                 idev->path, INPUT_INTERFACE);
1411
1412         input_device_free(idev);
1413 }
1414
1415 static int input_device_connadd(struct input_device *idev)
1416 {
1417         int err;
1418
1419         err = input_device_connected(idev);
1420         if (err == 0)
1421                 return 0;
1422
1423         if (idev->ctrl_io) {
1424                 g_io_channel_shutdown(idev->ctrl_io, FALSE, NULL);
1425                 g_io_channel_unref(idev->ctrl_io);
1426                 idev->ctrl_io = NULL;
1427         }
1428
1429         if (idev->intr_io) {
1430                 g_io_channel_shutdown(idev->intr_io, FALSE, NULL);
1431                 g_io_channel_unref(idev->intr_io);
1432                 idev->intr_io = NULL;
1433         }
1434
1435         return err;
1436 }
1437
1438 bool input_device_exists(const bdaddr_t *src, const bdaddr_t *dst)
1439 {
1440         if (find_device(src, dst))
1441                 return true;
1442
1443         return false;
1444 }
1445
1446 int input_device_set_channel(const bdaddr_t *src, const bdaddr_t *dst, int psm,
1447                                                                 GIOChannel *io)
1448 {
1449         struct input_device *idev = find_device(src, dst);
1450         GIOCondition cond = G_IO_HUP | G_IO_ERR | G_IO_NVAL;
1451
1452         DBG("idev %p psm %d", idev, psm);
1453
1454         if (!idev)
1455                 return -ENOENT;
1456
1457         if (uhid_enabled)
1458                 cond |= G_IO_IN;
1459
1460         switch (psm) {
1461         case L2CAP_PSM_HIDP_CTRL:
1462                 if (idev->ctrl_io)
1463                         return -EALREADY;
1464                 idev->ctrl_io = g_io_channel_ref(io);
1465                 idev->ctrl_watch = g_io_add_watch(idev->ctrl_io, cond,
1466                                                         ctrl_watch_cb, idev);
1467                 break;
1468         case L2CAP_PSM_HIDP_INTR:
1469                 if (idev->intr_io)
1470                         return -EALREADY;
1471                 idev->intr_io = g_io_channel_ref(io);
1472                 idev->intr_watch = g_io_add_watch(idev->intr_io, cond,
1473                                                         intr_watch_cb, idev);
1474                 break;
1475         }
1476
1477         if (idev->intr_io && idev->ctrl_io)
1478                 input_device_connadd(idev);
1479
1480         return 0;
1481 }
1482
1483 int input_device_close_channels(const bdaddr_t *src, const bdaddr_t *dst)
1484 {
1485         struct input_device *idev = find_device(src, dst);
1486
1487         if (!idev)
1488                 return -ENOENT;
1489
1490         if (idev->intr_io)
1491                 g_io_channel_shutdown(idev->intr_io, TRUE, NULL);
1492
1493         if (idev->ctrl_io)
1494                 g_io_channel_shutdown(idev->ctrl_io, TRUE, NULL);
1495
1496         return 0;
1497 }