Drop capability events
[platform/upstream/libinput.git] / src / evdev.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2013 Jonas Ådahl
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #include "config.h"
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <linux/input.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <mtdev.h>
33 #include <assert.h>
34
35 #include "libinput.h"
36 #include "evdev.h"
37 #include "libinput-private.h"
38
39 #define DEFAULT_AXIS_STEP_DISTANCE li_fixed_from_int(10)
40
41 void
42 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
43 {
44         static const struct {
45                 enum libinput_led weston;
46                 int evdev;
47         } map[] = {
48                 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
49                 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
50                 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
51         };
52         struct input_event ev[ARRAY_LENGTH(map) + 1];
53         unsigned int i;
54
55         if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
56                 return;
57
58         memset(ev, 0, sizeof(ev));
59         for (i = 0; i < ARRAY_LENGTH(map); i++) {
60                 ev[i].type = EV_LED;
61                 ev[i].code = map[i].evdev;
62                 ev[i].value = !!(leds & map[i].weston);
63         }
64         ev[i].type = EV_SYN;
65         ev[i].code = SYN_REPORT;
66
67         i = write(device->fd, ev, sizeof ev);
68         (void)i; /* no, we really don't care about the return value */
69 }
70
71 static void
72 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
73 {
74         if (!device->abs.apply_calibration) {
75                 *x = device->abs.x;
76                 *y = device->abs.y;
77                 return;
78         } else {
79                 *x = device->abs.x * device->abs.calibration[0] +
80                         device->abs.y * device->abs.calibration[1] +
81                         device->abs.calibration[2];
82
83                 *y = device->abs.x * device->abs.calibration[3] +
84                         device->abs.y * device->abs.calibration[4] +
85                         device->abs.calibration[5];
86         }
87 }
88
89 static void
90 evdev_flush_pending_event(struct evdev_device *device, uint32_t time)
91 {
92         int32_t cx, cy;
93         int slot;
94         struct libinput_device *base = &device->base;
95
96         slot = device->mt.slot;
97
98         switch (device->pending_event) {
99         case EVDEV_NONE:
100                 return;
101         case EVDEV_RELATIVE_MOTION:
102                 pointer_notify_motion(base,
103                                       time,
104                                       device->rel.dx,
105                                       device->rel.dy);
106                 device->rel.dx = 0;
107                 device->rel.dy = 0;
108                 goto handled;
109         case EVDEV_ABSOLUTE_MT_DOWN:
110                 touch_notify_touch(base,
111                                    time,
112                                    slot,
113                                    li_fixed_from_int(device->mt.slots[slot].x),
114                                    li_fixed_from_int(device->mt.slots[slot].y),
115                                    LIBINPUT_TOUCH_TYPE_DOWN);
116                 goto handled;
117         case EVDEV_ABSOLUTE_MT_MOTION:
118                 touch_notify_touch(base,
119                                    time,
120                                    slot,
121                                    li_fixed_from_int(device->mt.slots[slot].x),
122                                    li_fixed_from_int(device->mt.slots[slot].y),
123                                    LIBINPUT_TOUCH_TYPE_MOTION);
124                 goto handled;
125         case EVDEV_ABSOLUTE_MT_UP:
126                 touch_notify_touch(base,
127                                    time,
128                                    slot,
129                                    0, 0,
130                                    LIBINPUT_TOUCH_TYPE_UP);
131                 goto handled;
132         case EVDEV_ABSOLUTE_TOUCH_DOWN:
133                 transform_absolute(device, &cx, &cy);
134                 touch_notify_touch(base,
135                                    time,
136                                    slot,
137                                    li_fixed_from_int(cx),
138                                    li_fixed_from_int(cy),
139                                    LIBINPUT_TOUCH_TYPE_DOWN);
140                 goto handled;
141         case EVDEV_ABSOLUTE_MOTION:
142                 transform_absolute(device, &cx, &cy);
143                 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
144                         touch_notify_touch(base,
145                                            time,
146                                            slot,
147                                            li_fixed_from_int(cx),
148                                            li_fixed_from_int(cy),
149                                            LIBINPUT_TOUCH_TYPE_DOWN);
150                 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
151                         pointer_notify_motion_absolute(base,
152                                                        time,
153                                                        li_fixed_from_int(cx),
154                                                        li_fixed_from_int(cy));
155                 }
156                 goto handled;
157         case EVDEV_ABSOLUTE_TOUCH_UP:
158                 touch_notify_touch(base,
159                                    time,
160                                    0, 0, 0, LIBINPUT_TOUCH_TYPE_UP);
161                 goto handled;
162         }
163
164         assert(0 && "Unknown pending event type");
165
166 handled:
167         device->pending_event = EVDEV_NONE;
168 }
169
170 static void
171 evdev_process_touch_button(struct evdev_device *device, int time, int value)
172 {
173         if (device->pending_event != EVDEV_NONE &&
174             device->pending_event != EVDEV_ABSOLUTE_MOTION)
175                 evdev_flush_pending_event(device, time);
176
177         device->pending_event = (value ?
178                                  EVDEV_ABSOLUTE_TOUCH_DOWN :
179                                  EVDEV_ABSOLUTE_TOUCH_UP);
180 }
181
182 static inline void
183 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
184 {
185         /* ignore kernel key repeat */
186         if (e->value == 2)
187                 return;
188
189         if (e->code == BTN_TOUCH) {
190                 if (!device->is_mt)
191                         evdev_process_touch_button(device, time, e->value);
192                 return;
193         }
194
195         evdev_flush_pending_event(device, time);
196
197         switch (e->code) {
198         case BTN_LEFT:
199         case BTN_RIGHT:
200         case BTN_MIDDLE:
201         case BTN_SIDE:
202         case BTN_EXTRA:
203         case BTN_FORWARD:
204         case BTN_BACK:
205         case BTN_TASK:
206                 pointer_notify_button(
207                         &device->base,
208                         time,
209                         e->code,
210                         e->value ? LIBINPUT_POINTER_BUTTON_STATE_PRESSED :
211                                    LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
212                 break;
213
214         default:
215                 keyboard_notify_key(
216                         &device->base,
217                         time,
218                         e->code,
219                         e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
220                                    LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
221                 break;
222         }
223 }
224
225 static void
226 evdev_process_touch(struct evdev_device *device,
227                     struct input_event *e,
228                     uint32_t time)
229 {
230         struct libinput *libinput = device->base.seat->libinput;
231         int screen_width;
232         int screen_height;
233
234         libinput->interface->get_current_screen_dimensions(
235                 &device->base,
236                 &screen_width,
237                 &screen_height,
238                 libinput->user_data);
239
240         switch (e->code) {
241         case ABS_MT_SLOT:
242                 evdev_flush_pending_event(device, time);
243                 device->mt.slot = e->value;
244                 break;
245         case ABS_MT_TRACKING_ID:
246                 if (device->pending_event != EVDEV_NONE &&
247                     device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
248                         evdev_flush_pending_event(device, time);
249                 if (e->value >= 0)
250                         device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
251                 else
252                         device->pending_event = EVDEV_ABSOLUTE_MT_UP;
253                 break;
254         case ABS_MT_POSITION_X:
255                 device->mt.slots[device->mt.slot].x =
256                         (e->value - device->abs.min_x) * screen_width /
257                         (device->abs.max_x - device->abs.min_x);
258                 if (device->pending_event == EVDEV_NONE)
259                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
260                 break;
261         case ABS_MT_POSITION_Y:
262                 device->mt.slots[device->mt.slot].y =
263                         (e->value - device->abs.min_y) * screen_height /
264                         (device->abs.max_y - device->abs.min_y);
265                 if (device->pending_event == EVDEV_NONE)
266                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
267                 break;
268         }
269 }
270
271 static inline void
272 evdev_process_absolute_motion(struct evdev_device *device,
273                               struct input_event *e)
274 {
275         struct libinput *libinput = device->base.seat->libinput;
276         int screen_width;
277         int screen_height;
278
279         libinput->interface->get_current_screen_dimensions(
280                 &device->base,
281                 &screen_width,
282                 &screen_height,
283                 libinput->user_data);
284
285         switch (e->code) {
286         case ABS_X:
287                 device->abs.x =
288                         (e->value - device->abs.min_x) * screen_width /
289                         (device->abs.max_x - device->abs.min_x);
290                 if (device->pending_event == EVDEV_NONE)
291                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
292                 break;
293         case ABS_Y:
294                 device->abs.y =
295                         (e->value - device->abs.min_y) * screen_height /
296                         (device->abs.max_y - device->abs.min_y);
297                 if (device->pending_event == EVDEV_NONE)
298                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
299                 break;
300         }
301 }
302
303 static inline void
304 evdev_process_relative(struct evdev_device *device,
305                        struct input_event *e, uint32_t time)
306 {
307         struct libinput_device *base = &device->base;
308
309         switch (e->code) {
310         case REL_X:
311                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
312                         evdev_flush_pending_event(device, time);
313                 device->rel.dx += li_fixed_from_int(e->value);
314                 device->pending_event = EVDEV_RELATIVE_MOTION;
315                 break;
316         case REL_Y:
317                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
318                         evdev_flush_pending_event(device, time);
319                 device->rel.dy += li_fixed_from_int(e->value);
320                 device->pending_event = EVDEV_RELATIVE_MOTION;
321                 break;
322         case REL_WHEEL:
323                 evdev_flush_pending_event(device, time);
324                 switch (e->value) {
325                 case -1:
326                         /* Scroll down */
327                 case 1:
328                         /* Scroll up */
329                         pointer_notify_axis(
330                                 base,
331                                 time,
332                                 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
333                                 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
334                         break;
335                 default:
336                         break;
337                 }
338                 break;
339         case REL_HWHEEL:
340                 evdev_flush_pending_event(device, time);
341                 switch (e->value) {
342                 case -1:
343                         /* Scroll left */
344                 case 1:
345                         /* Scroll right */
346                         pointer_notify_axis(
347                                 base,
348                                 time,
349                                 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
350                                 e->value * DEFAULT_AXIS_STEP_DISTANCE);
351                         break;
352                 default:
353                         break;
354
355                 }
356         }
357 }
358
359 static inline void
360 evdev_process_absolute(struct evdev_device *device,
361                        struct input_event *e,
362                        uint32_t time)
363 {
364         if (device->is_mt) {
365                 evdev_process_touch(device, e, time);
366         } else {
367                 evdev_process_absolute_motion(device, e);
368         }
369 }
370
371 static void
372 fallback_process(struct evdev_dispatch *dispatch,
373                  struct evdev_device *device,
374                  struct input_event *event,
375                  uint32_t time)
376 {
377         switch (event->type) {
378         case EV_REL:
379                 evdev_process_relative(device, event, time);
380                 break;
381         case EV_ABS:
382                 evdev_process_absolute(device, event, time);
383                 break;
384         case EV_KEY:
385                 evdev_process_key(device, event, time);
386                 break;
387         case EV_SYN:
388                 evdev_flush_pending_event(device, time);
389                 break;
390         }
391 }
392
393 static void
394 fallback_destroy(struct evdev_dispatch *dispatch)
395 {
396         free(dispatch);
397 }
398
399 struct evdev_dispatch_interface fallback_interface = {
400         fallback_process,
401         fallback_destroy
402 };
403
404 static struct evdev_dispatch *
405 fallback_dispatch_create(void)
406 {
407         struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
408         if (dispatch == NULL)
409                 return NULL;
410
411         dispatch->interface = &fallback_interface;
412
413         return dispatch;
414 }
415
416 static void
417 evdev_process_events(struct evdev_device *device,
418                      struct input_event *ev, int count)
419 {
420         struct evdev_dispatch *dispatch = device->dispatch;
421         struct input_event *e, *end;
422         uint32_t time = 0;
423
424         e = ev;
425         end = e + count;
426         for (e = ev; e < end; e++) {
427                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
428
429                 dispatch->interface->process(dispatch, device, e, time);
430         }
431 }
432
433 static void
434 evdev_device_dispatch(void *data)
435 {
436         struct evdev_device *device = data;
437         struct libinput *libinput = device->base.seat->libinput;
438         int fd = device->fd;
439         struct input_event ev[32];
440         int len;
441
442         /* If the compositor is repainting, this function is called only once
443          * per frame and we have to process all the events available on the
444          * fd, otherwise there will be input lag. */
445         do {
446                 if (device->mtdev)
447                         len = mtdev_get(device->mtdev, fd, ev,
448                                         ARRAY_LENGTH(ev)) *
449                                 sizeof (struct input_event);
450                 else
451                         len = read(fd, &ev, sizeof ev);
452
453                 if (len < 0 || len % sizeof ev[0] != 0) {
454                         if (len < 0 && errno != EAGAIN && errno != EINTR) {
455                                 libinput_remove_source(libinput,
456                                                        device->source);
457                                 device->source = NULL;
458                         }
459
460                         return;
461                 }
462
463                 evdev_process_events(device, ev, len / sizeof ev[0]);
464
465         } while (len > 0);
466 }
467
468 static int
469 evdev_configure_device(struct evdev_device *device)
470 {
471         struct input_absinfo absinfo;
472         unsigned long ev_bits[NBITS(EV_MAX)];
473         unsigned long abs_bits[NBITS(ABS_MAX)];
474         unsigned long rel_bits[NBITS(REL_MAX)];
475         unsigned long key_bits[NBITS(KEY_MAX)];
476         int has_abs, has_rel, has_mt;
477         int has_button, has_keyboard, has_touch;
478         unsigned int i;
479
480         has_rel = 0;
481         has_abs = 0;
482         has_mt = 0;
483         has_button = 0;
484         has_keyboard = 0;
485         has_touch = 0;
486
487         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
488         if (TEST_BIT(ev_bits, EV_ABS)) {
489                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
490                       abs_bits);
491
492                 if (TEST_BIT(abs_bits, ABS_X)) {
493                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
494                         device->abs.min_x = absinfo.minimum;
495                         device->abs.max_x = absinfo.maximum;
496                         has_abs = 1;
497                 }
498                 if (TEST_BIT(abs_bits, ABS_Y)) {
499                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
500                         device->abs.min_y = absinfo.minimum;
501                         device->abs.max_y = absinfo.maximum;
502                         has_abs = 1;
503                 }
504                 /* We only handle the slotted Protocol B in weston.
505                    Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
506                    require mtdev for conversion. */
507                 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
508                     TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
509                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
510                               &absinfo);
511                         device->abs.min_x = absinfo.minimum;
512                         device->abs.max_x = absinfo.maximum;
513                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
514                               &absinfo);
515                         device->abs.min_y = absinfo.minimum;
516                         device->abs.max_y = absinfo.maximum;
517                         device->is_mt = 1;
518                         has_touch = 1;
519                         has_mt = 1;
520
521                         if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) {
522                                 device->mtdev = mtdev_new_open(device->fd);
523                                 if (!device->mtdev)
524                                         return 0;
525                                 device->mt.slot = device->mtdev->caps.slot.value;
526                         } else {
527                                 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT),
528                                       &absinfo);
529                                 device->mt.slot = absinfo.value;
530                         }
531                 }
532         }
533         if (TEST_BIT(ev_bits, EV_REL)) {
534                 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
535                       rel_bits);
536                 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
537                         has_rel = 1;
538         }
539         if (TEST_BIT(ev_bits, EV_KEY)) {
540                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
541                       key_bits);
542                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
543                     !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
544                     (has_abs || has_mt)) {
545                         device->dispatch = evdev_touchpad_create(device);
546                 }
547                 for (i = KEY_ESC; i < KEY_MAX; i++) {
548                         if (i >= BTN_MISC && i < KEY_OK)
549                                 continue;
550                         if (TEST_BIT(key_bits, i)) {
551                                 has_keyboard = 1;
552                                 break;
553                         }
554                 }
555                 if (TEST_BIT(key_bits, BTN_TOUCH))
556                         has_touch = 1;
557                 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
558                         if (TEST_BIT(key_bits, i)) {
559                                 has_button = 1;
560                                 break;
561                         }
562                 }
563         }
564         if (TEST_BIT(ev_bits, EV_LED))
565                 has_keyboard = 1;
566
567         if ((has_abs || has_rel) && has_button)
568                 device->seat_caps |= EVDEV_DEVICE_POINTER;
569         if (has_keyboard)
570                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
571         if (has_touch)
572                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
573
574         return 0;
575 }
576
577 struct evdev_device *
578 evdev_device_create(struct libinput_seat *seat,
579                     const char *devnode,
580                     const char *sysname,
581                     int fd)
582 {
583         struct libinput *libinput = seat->libinput;
584         struct evdev_device *device;
585         char devname[256] = "unknown";
586
587         device = zalloc(sizeof *device);
588         if (device == NULL)
589                 return NULL;
590
591         libinput_device_init(&device->base, seat);
592
593         device->seat_caps = 0;
594         device->is_mt = 0;
595         device->mtdev = NULL;
596         device->devnode = strdup(devnode);
597         device->sysname = strdup(sysname);
598         device->mt.slot = -1;
599         device->rel.dx = 0;
600         device->rel.dy = 0;
601         device->dispatch = NULL;
602         device->fd = fd;
603         device->pending_event = EVDEV_NONE;
604
605         ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
606         devname[sizeof(devname) - 1] = '\0';
607         device->devname = strdup(devname);
608
609         if (evdev_configure_device(device) == -1)
610                 goto err;
611
612         if (device->seat_caps == 0) {
613                 evdev_device_destroy(device);
614                 return NULL;
615         }
616
617         /* If the dispatch was not set up use the fallback. */
618         if (device->dispatch == NULL)
619                 device->dispatch = fallback_dispatch_create();
620         if (device->dispatch == NULL)
621                 goto err;
622
623         device->source =
624                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
625         if (!device->source)
626                 goto err;
627
628         list_insert(seat->devices_list.prev, &device->base.link);
629         notify_added_device(&device->base);
630
631         return device;
632
633 err:
634         evdev_device_destroy(device);
635         return NULL;
636 }
637
638 int
639 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
640 {
641         memset(keys, 0, size);
642         return ioctl(device->fd, EVIOCGKEY(size), keys);
643 }
644
645 const char *
646 evdev_device_get_output(struct evdev_device *device)
647 {
648         return device->output_name;
649 }
650
651 const char *
652 evdev_device_get_sysname(struct evdev_device *device)
653 {
654         return device->sysname;
655 }
656
657 void
658 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
659 {
660         device->abs.apply_calibration = 1;
661         memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
662 }
663
664 int
665 evdev_device_has_capability(struct evdev_device *device,
666                             enum libinput_device_capability capability)
667 {
668         switch (capability) {
669         case LIBINPUT_DEVICE_CAP_POINTER:
670                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
671         case LIBINPUT_DEVICE_CAP_KEYBOARD:
672                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
673         case LIBINPUT_DEVICE_CAP_TOUCH:
674                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
675         default:
676                 return 0;
677         }
678 }
679
680 void
681 evdev_device_remove(struct evdev_device *device)
682 {
683         if (device->source)
684                 libinput_remove_source(device->base.seat->libinput,
685                                        device->source);
686
687         if (device->mtdev)
688                 mtdev_close_delete(device->mtdev);
689         close(device->fd);
690         list_remove(&device->base.link);
691
692         notify_removed_device(&device->base);
693         libinput_device_unref(&device->base);
694 }
695
696 void
697 evdev_device_destroy(struct evdev_device *device)
698 {
699         struct evdev_dispatch *dispatch;
700
701         dispatch = device->dispatch;
702         if (dispatch)
703                 dispatch->interface->destroy(dispatch);
704
705         free(device->devname);
706         free(device->devnode);
707         free(device->sysname);
708         free(device);
709 }