summaryrefslogtreecommitdiff
path: root/sysutils/hal/files/patch-hald_freebsd_hf-usb2.c
blob: 9f0f5726186c181f3bf4c8aadbf0cf11a43e3fb9 (plain) (blame)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
--- hald/freebsd/hf-usb2.c.orig	2009-11-21 19:57:40.000000000 -0500
+++ hald/freebsd/hf-usb2.c	2009-11-21 19:58:07.000000000 -0500
@@ -0,0 +1,312 @@
+/***************************************************************************
+ * CVSID: $Id$
+ *
+ * hf-usb.c : USB support
+ *
+ * Copyright (C) 2009 Joe Marcus Clarke <marcus@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 <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <libusb20.h>
+
+#include "../logger.h"
+#include "../osspec.h"
+
+#include "hf-usb.h"
+#include "hf-usb2.h"
+#include "hf-devtree.h"
+#include "hf-util.h"
+
+static struct libusb20_backend *hf_usb2_be = NULL;
+
+static void
+hf_usb2_copy_parent (HalDevice *parent,
+		     const char *key,
+		     gpointer user_data)
+{
+  HalDevice *device;
+
+  g_return_if_fail(HAL_IS_DEVICE(parent));
+  g_return_if_fail(HAL_IS_DEVICE(user_data));
+
+  device = HAL_DEVICE(user_data);
+
+  if (! strncmp(key, "usb_device.", strlen("usb_device.")))
+    hal_device_copy_property(parent, key, device, key);
+}
+
+static void
+hf_usb2_probe_interfaces(HalDevice *parent)
+{
+  int num_interfaces;
+  int i;
+
+  g_return_if_fail(HAL_IS_DEVICE(parent));
+
+  if (hal_device_property_get_bool(parent, "info.ignore"))
+    return;
+
+  num_interfaces = hal_device_property_get_int(parent,
+    "usb_device.num_interfaces");
+
+  for (i = 0; i < num_interfaces; i++)
+    {
+      HalDevice *device;
+
+      device = hf_device_new(parent);
+
+      hal_device_property_set_string(device, "info.subsystem", "usb");
+      hal_device_property_set_int(device, "usb.interface.number", i);
+      hal_device_property_foreach(parent, hf_usb2_copy_parent, device);
+      hal_device_copy_property(parent, "info.product", device, "info.product");
+      hal_device_copy_property(parent, "info.vendor", device, "info.vendor");
+
+      if (hf_device_preprobe(device))
+        {
+          const char *driver, *devname;
+
+          hf_runner_run_sync(device, 0, "hald-probe-usb2-interface", NULL);
+
+	  devname = hal_device_property_get_string(device,
+            "usb.freebsd.devname");
+	  if (devname)
+            hf_devtree_device_set_name(device, devname);
+
+	  driver = hal_device_property_get_string(device, "freebsd.driver");
+	  if (driver)
+            {
+	      if (! strcmp(driver, "ukbd"))
+                hf_device_set_input(device, "keyboard", NULL);
+	      else if (! strcmp(driver, "ums") || ! strcmp(driver, "atp"))
+                {
+                  hf_device_set_input(device, "mouse", devname);
+	          hf_runner_run_sync(device, 0, "hald-probe-mouse", NULL);
+	        }
+	      else if (! strcmp(driver, "uhid"))
+                {
+                  hal_device_property_set_string(device, "info.category",
+                    "hiddev");
+	          hal_device_add_capability(device, "hiddev");
+	          hf_device_property_set_string_printf(device, "hiddev.device",
+                    "/dev/%s", devname);
+	          hal_device_copy_property(device, "info.product", device,
+                    "hiddev.product");
+	          hf_runner_run_sync(device, 0, "hald-probe-hiddev", NULL);
+	        }
+	      else if (! strcmp(driver, "ldev"))
+                {
+                  /* Linux driver (webcam) */
+
+	          /*
+                   * XXX This is a hack.  Currently, all ldev devices are
+		   * webcams.  That may not always be the case.  Hopefully,
+		   * when other Linux driver support is added, there will be
+		   * a sysctl or some other way to determine device class.
+	           */
+                  hf_usb_add_webcam_properties(device);
+	        }
+	      else if (! strcmp(driver, "pwc"))
+                {
+                  /* Phillips Web Cam */
+                  hf_usb_add_webcam_properties(device);
+	        }
+	    }
+
+	  hf_usb_device_compute_udi(device);
+	  hf_device_add(device);
+	}
+    }
+}
+
+static void
+hf_usb2_probe_device (HalDevice *parent, int bus, int addr)
+{
+  HalDevice *device;
+
+  g_return_if_fail(HAL_IS_DEVICE(parent));
+
+  device = hf_device_new(parent);
+
+  hal_device_property_set_string(device, "info.subsystem", "usb_device");
+  hal_device_property_set_int(device, "usb_device.bus_number", bus);
+  hal_device_property_set_int(device, "usb_device.level_number", addr - 1);
+  hal_device_property_set_int(device, "usb_device.port_number", addr);
+
+  if (hf_device_preprobe(device))
+    {
+      hf_runner_run_sync(device, 0, "hald-probe-usb2-device", NULL);
+      hf_usb_device_compute_udi(device);
+
+      hf_device_add(device);
+    }
+  else
+    return;
+
+  hf_usb2_probe_interfaces(device);
+}
+
+static void
+hf_usb2_privileged_init (void)
+{
+  hf_usb2_be = libusb20_be_alloc_default();
+  if (hf_usb2_be == NULL)
+    HAL_INFO(("unable to open USB backend: %s", g_strerror(errno)));
+}
+
+static void
+hf_usb2_probe (void)
+{
+  struct libusb20_device *pdev = NULL;
+
+  if (hf_usb2_be == NULL)
+    return;
+
+  while ((pdev = libusb20_be_device_foreach(hf_usb2_be, pdev)))
+    {
+      HalDevice *parent;
+      int bus, addr;
+
+      bus = libusb20_dev_get_bus_number(pdev);
+      addr = libusb20_dev_get_address(pdev);
+
+      if (addr == 1)
+        parent = hf_devtree_find_from_info(hald_get_gdl(), "usbus", bus);
+      else
+        parent = hf_device_store_match(hald_get_gdl(), "usb_device.bus_number",
+          HAL_PROPERTY_TYPE_INT32, bus, "usb_device.port_number",
+	  HAL_PROPERTY_TYPE_INT32, addr - 1, "info.bus",
+	  HAL_PROPERTY_TYPE_STRING, "usb_device", NULL);
+      if (! parent || hal_device_property_get_bool(parent, "info.ignore"))
+        continue;
+
+      hf_usb2_probe_device(parent, bus, addr);
+    }
+
+  libusb20_be_free(hf_usb2_be);
+  hf_usb2_be = NULL;
+}
+
+static gboolean
+hf_usb2_devd_add (const char *name,
+		  GHashTable *params,
+		  GHashTable *at,
+		  const char *parent)
+{
+  HalDevice *parent_device;
+  int bus, addr, pbus, paddr;
+
+  if (! parent)
+    return FALSE;
+
+  if (strncmp(name, "ugen", strlen("ugen")) &&
+      ! strncmp(parent, "uhub", strlen("uhub")))
+    return TRUE;
+  else if (strncmp(name, "ugen", strlen("ugen")))
+    return FALSE;
+  else if (strncmp(parent, "ugen", strlen("ugen")))
+    return TRUE;
+
+  if (sscanf(name, "ugen%i.%i", &bus, &addr) != 2)
+    return FALSE;
+
+  if (sscanf(parent, "ugen%i.%i", &pbus, &paddr) != 2)
+    return FALSE;
+
+  HAL_INFO(("received devd add event for device '%s' with parent '%s'",
+           name, parent));
+
+  parent_device = hf_device_store_match(hald_get_gdl(),
+    "usb_device.bus_number", HAL_PROPERTY_TYPE_INT32, pbus,
+    "usb_device.port_number", HAL_PROPERTY_TYPE_INT32, paddr, "info.bus",
+    HAL_PROPERTY_TYPE_STRING, "usb_device", NULL);
+
+  if (parent_device && ! hal_device_property_get_bool(parent_device,
+      "info.ignore"))
+    {
+      hf_usb2_probe_device(parent_device, bus, addr);
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+static gboolean
+hf_usb2_devd_remove (const char *name,
+		     GHashTable *params,
+		     GHashTable *at,
+		     const char *parent)
+{
+  HalDevice *device;
+  int bus, addr;
+
+  if (strncmp(name, "ugen", strlen("ugen")))
+    return FALSE;
+
+  if (sscanf(name, "ugen%i.%i", &bus, &addr) != 2)
+    return FALSE;
+
+  HAL_INFO(("received devd remove event, device %s", name));
+
+  device = hf_device_store_match(hald_get_gdl(), "usb_device.bus_number",
+    HAL_PROPERTY_TYPE_INT32, bus, "usb_device.port_number",
+    HAL_PROPERTY_TYPE_INT32, addr, "info.bus",
+    HAL_PROPERTY_TYPE_STRING, "usb_device", NULL);
+
+  if (device)
+    {
+      hf_device_remove_tree(device);
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+static gboolean
+hf_usb2_devd_notify (const char *system,
+		     const char *subsystem,
+		     const char *type,
+		     const char *data)
+{
+  if (! data || strcmp(system, "DEVFS") || strcmp(subsystem, "CDEV") ||
+      (strcmp(type, "CREATE") && strcmp(type, "DESTROY")))
+    return FALSE;
+
+  if (! strncmp(data, "cdev=ugen", strlen("cdev=ugen")) ||
+      ! strncmp(data, "cdev=usb", strlen("cdev=usb")))
+    return TRUE;
+
+  return FALSE;
+}
+
+HFHandler hf_usb2_handler = {
+  .privileged_init	= hf_usb2_privileged_init,
+  .probe		= hf_usb2_probe
+};
+
+HFDevdHandler hf_usb2_devd_handler = {
+  .add =	hf_usb2_devd_add,
+  .remove =	hf_usb2_devd_remove,
+  .notify =     hf_usb2_devd_notify
+};