summaryrefslogtreecommitdiff
path: root/hald/linux/usbif_bus_device.c
blob: 9578fe2573ef07487080ac8bc31221a1fc318781 (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
155
156
157
158
159
160
161
162
163
164
165
166
/***************************************************************************
 * CVSID: $Id$
 *
 * linux_usb.c : USB handling on Linux 2.6
 *
 * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
 *
 * Licensed under the Academic Free License version 2.0
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **************************************************************************/

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

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <assert.h>
#include <unistd.h>
#include <stdarg.h>

#include "../logger.h"
#include "../device_store.h"
#include "bus_device.h"
#include "common.h"

/**
 * @defgroup HalDaemonLinuxUsbIF USB interfaces
 * @ingroup HalDaemonLinux
 * @brief USB interfaces
 * @{
 */

/** Specialised accept function since both USB devices and USB interfaces
 *  share the same bus name
 *
 *  @param  self                Pointer to class members
 *  @param  path                Sysfs-path for device
 *  @param  device              libsysfs object for device
 *  @param  is_probing          Set to TRUE only on initial detection
 */
static dbus_bool_t
usbif_device_accept (BusDeviceHandler *self, const char *path, 
		     struct sysfs_device *device, dbus_bool_t is_probing)
{
	unsigned int i;

	if (strcmp (device->bus, "usb") != 0)
		return FALSE;

	/* only USB interfaces got a : in the bus_id */
	for (i = 0; device->bus_id[i] != 0; i++) {
		if (device->bus_id[i] == ':') {
			return TRUE;
		}
	}

	return FALSE;
}

static char *
usbif_device_compute_udi (HalDevice *d, int append_num)
{
	int i, len;
	const char *format;
	const char *pd;
	const char *name;
	static char buf[256];

	if (append_num == -1)
		format = "/org/freedesktop/Hal/devices/usbif_%s_%d";
	else
		format = "/org/freedesktop/Hal/devices/usbif_%s_%d-%d";

	hal_device_print (d);

	pd = hal_device_property_get_string (d, "info.parent");
	len = strlen (pd);
	for (i = len - 1; pd[i] != '/' && i >= 0; i--);
	name = pd + i + 1;

	snprintf (buf, 256, format,
		  name,
		  hal_device_property_get_int (d, "usbif.number"), append_num);

	return buf;
}


static void 
usbif_device_post_process (BusDeviceHandler *self,
			   HalDevice *d,
			   const char *sysfs_path,
			   struct sysfs_device *device)
{
	int i;
	int len;
	struct sysfs_attribute *cur;
	char attr_name[SYSFS_NAME_LEN];

	dlist_for_each_data (sysfs_get_device_attributes (device), cur,
			     struct sysfs_attribute) {

		if (sysfs_get_name_from_path (cur->path,
					      attr_name,
					      SYSFS_NAME_LEN) != 0)
			continue;

		/* strip whitespace */
		len = strlen (cur->value);
		for (i = len - 1; i > 0 && isspace (cur->value[i]); --i)
			cur->value[i] = '\0';

		/*printf("attr_name=%s -> '%s'\n", attr_name, cur->value); */

		if (strcmp (attr_name, "bInterfaceClass") == 0)
			hal_device_property_set_int (d, "usbif.interface_class",
					     parse_dec (cur->value));
		else if (strcmp (attr_name, "bInterfaceSubClass") == 0)
			hal_device_property_set_int (d, "usbif.interface_subclass",
					     parse_dec (cur->value));
		else if (strcmp (attr_name, "bInterfaceProtocol") == 0)
			hal_device_property_set_int (d, "usbif.interface_protocol",
					     parse_dec (cur->value));
		else if (strcmp (attr_name, "bInterfaceNumber") == 0)
			hal_device_property_set_int (d, "usbif.number",
					     parse_dec (cur->value));
	}

	hal_device_property_set_bool (d, "info.virtual", TRUE);
}

/** Method specialisations for bustype usbif */
BusDeviceHandler usbif_bus_handler = {
	bus_device_init,           /**< init function */
	bus_device_detection_done, /**< detection is done */
	bus_device_shutdown,       /**< shutdown function */
	bus_device_tick,           /**< timer function */
	usbif_device_accept,       /**< accept function */
 	bus_device_visit,          /**< visitor function */
	bus_device_removed,        /**< device is removed */
	usbif_device_compute_udi,  /**< UDI computing function */
	usbif_device_post_process, /**< add more properties */
	bus_device_got_udi,        /**< got UDI */
	"usb",                     /**< sysfs bus name */
	"usbif"                    /**< namespace */
};

/** @} */