summaryrefslogtreecommitdiff
path: root/hald/freebsd/probing/probe-hiddev.c
blob: 054b159c660ed1240dd9e2a1528b318d42f0b505 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/***************************************************************************
 * CVSID: $Id$
 *
 * probe-hiddev.c : USB HID prober
 *
 * Copyright (C) 2006 Jean-Yves Lefort <jylefort@FreeBSD.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 **************************************************************************/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <sys/param.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#ifndef HAVE_LIBUSB20
#include <sys/ioctl.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>
#else
#if (__FreeBSD_version >= 800064) || (__FreeBSD_kernel_version >= 800064)
#include <dev/usb/usbhid.h>
#else
#include <dev/usb2/include/usb2_hid.h>
#endif
#endif
#include <usbhid.h>

#include "../libprobe/hfp.h"

#define HID_COLLECTION_APPLICATION	1

int
main (int argc, char **argv)
{
  char *device_file;
  int fd = -1;
  int report_id;
  report_desc_t report_desc;
  struct hid_data *data;
  hid_item_t item;
  boolean is_keyboard = FALSE;
  boolean is_keypad = FALSE;
  boolean is_mouse = FALSE;
  boolean is_joystick = FALSE;

  if (! hfp_init(argc, argv))
    goto end;

  device_file = getenv("HAL_PROP_HIDDEV_DEVICE");
  if (! device_file)
    goto end;

  fd = open(device_file, O_RDONLY);
  if (fd < 0)
    goto end;

  /* give a meaningful process title for ps(1) */
  setproctitle("%s", device_file);

#ifdef HAVE_LIBUSB20
  report_id = hid_get_report_id(fd);
  if (report_id == -1)
#else
  if (ioctl(fd, USB_GET_REPORT_ID, &report_id) < 0)
#endif
    goto end;

  hid_init(NULL);

  report_desc = hid_get_report_desc(fd);
  if (! report_desc)
    goto end;

  for (data = hid_start_parse(report_desc, ~0, report_id); hid_get_item(data, &item);)
    if (item.kind == hid_collection)
      {
	if (item.collection == HID_COLLECTION_APPLICATION)
	  {
	    const char *page;
	    char *full_page;
	    int i;

	    page = hid_usage_page(HID_PAGE(item.usage));

	    full_page = hfp_strdup_printf("%s Page", page);
	    for (i = 0; full_page[i] != 0; i++)
	      if (full_page[i] == '_')
		full_page[i] = ' ';

	    libhal_device_property_strlist_append(hfp_ctx, hfp_udi, "hiddev.application_pages", full_page, &hfp_error);
	    hfp_free(full_page);
	  }

	switch (item.usage)
	  {
	  case HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE):
	    is_mouse = TRUE;
	    break;

	  case HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_JOYSTICK):
	  case HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_GAME_PAD):
	    is_joystick = TRUE;
	    break;

	  case HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD):
	    is_keyboard = TRUE;
	    break;

	  case HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYPAD):
	    is_keypad = TRUE;
	    break;
	  }
      }
  hid_end_parse(data);

  hid_dispose_report_desc(report_desc);

  if (is_keyboard || is_mouse || is_joystick || is_keypad)
    {
      libhal_device_add_capability(hfp_ctx, hfp_udi, "input", &hfp_error);
      libhal_device_set_property_string(hfp_ctx, hfp_udi, "info.category", "input", &hfp_error);
      libhal_device_set_property_string(hfp_ctx, hfp_udi, "input.device", device_file, &hfp_error);
    }
  if (is_keyboard)
      libhal_device_add_capability(hfp_ctx, hfp_udi, "input.keyboard", &hfp_error);
  if (is_keypad)
      libhal_device_add_capability(hfp_ctx, hfp_udi, "input.keypad", &hfp_error);
  if (is_keyboard || is_keypad)
      libhal_device_add_capability(hfp_ctx, hfp_udi, "input.keys", &hfp_error);
  if (is_mouse)
      libhal_device_add_capability(hfp_ctx, hfp_udi, "input.mouse", &hfp_error);
  if (is_joystick)
      libhal_device_add_capability(hfp_ctx, hfp_udi, "input.joystick", &hfp_error);

 end:
  return 0;
}