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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/***************************************************************************
* CVSID: $Id$
*
* probe.c : Handling hardware of on Linux, using kernel 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 <dbus/dbus.h>
#include "../osspec.h"
#include "../logger.h"
#include "linux_common.h"
#include "linux_pci.h"
#include "linux_usb.h"
#include "linux_ide.h"
#include "linux_class_block.h"
#include "linux_class_scsi.h"
#include "linux_class_net.h"
#include "linux_class_input.h"
#include "libsysfs/libsysfs.h"
/**
* @defgroup HalDaemonLinux Linux 2.6 support
* @ingroup HalDaemon
* @brief Device detection and monitoring code for version 2.6 of Linux
* @{
*/
/** Mount path for sysfs */
char sysfs_mount_path[SYSFS_PATH_MAX];
/** Visitor function for any class device.
*
* This function determines the class of the device and call the
* appropriate visit_class_device_<classtype> function if matched.
*
* @param path Sysfs-path for class device, e.g.
* /sys/class/scsi_host/host7
* @param visit_children If children of this device should be visited
* set this to #TRUE. For device-probing, this
* should set be set to true so as to visit
* all devices. For hotplug events, it should
* be set to #FALSE as each sysfs object will
* generate a separate event.
*/
static void visit_class_device(const char* path, dbus_bool_t visit_children)
{
struct sysfs_class_device* class_device;
struct sysfs_directory* subdir;
class_device = sysfs_open_class_device(path);
if( class_device==NULL )
DIE(("Coulnd't get sysfs class device object for path %s", path));
/*
printf(" visit_class_device classname=%s name=%s path=%s\n",
class_device->classname,
class_device->name,
class_device->path);
*/
if( strcmp(class_device->classname, "scsi_host")==0 )
visit_class_device_scsi_host(path, class_device);
else if( strcmp(class_device->classname, "scsi_device")==0 )
visit_class_device_scsi_device(path, class_device);
else if( strcmp(class_device->classname, "block")==0 )
visit_class_device_block(path, class_device);
else if( strcmp(class_device->classname, "net")==0 )
visit_class_device_net(path, class_device);
/* Visit children */
if( visit_children && class_device->directory!=NULL &&
class_device->directory->subdirs!=NULL )
{
dlist_for_each_data(class_device->directory->subdirs, subdir,
struct sysfs_directory)
{
char newpath[SYSFS_PATH_MAX];
snprintf(newpath, SYSFS_PATH_MAX, "%s/%s", path, subdir->name);
visit_class_device(newpath, TRUE);
}
}
sysfs_close_class_device(class_device);
}
/** Visit all devices of a given class
*
* @param class_name Name of class, e.g. scsi_host or block
* @param visit_children If children of this device should be visited
* set this to #TRUE. For device-probing, this
* should set be set to true so as to visit
* all devices. For hotplug events, it should
* be set to #FALSE as each sysfs object will
* generate a separate event.
*/
static void visit_class(const char* class_name, dbus_bool_t visit_children)
{
struct sysfs_class* cls = NULL;
struct sysfs_class_device* cur = NULL;
cls = sysfs_open_class(class_name);
if( cls==NULL )
{
HAL_ERROR(("Error opening class %s\n", class_name));
return;
}
if( cls->devices!=NULL )
{
dlist_for_each_data(cls->devices, cur, struct sysfs_class_device)
{
visit_class_device(cur->path, visit_children);
}
}
sysfs_close_class(cls);
}
/** Visitor function for any device.
*
* This function determines the bus-type of the device and call the
* appropriate visit_device_<bustype> function if matched.
*
* @param path Sysfs-path for device
* @param visit_children If children of this device should be visited
* set this to #TRUE. For device-probing, this
* should set be set to true so as to visit
* all devices. For hotplug events, it should
* be set to #FALSE as each sysfs object will
* generate a separate event.
*/
static void visit_device(const char* path, dbus_bool_t visit_children)
{
struct sysfs_device* device;
struct sysfs_directory* subdir;
device = sysfs_open_device(path);
if( device==NULL )
DIE(("Coulnd't get sysfs device object for path %s", path));
/*printf(" %s busid=%s\n", device->bus, device->bus_id);*/
if( device->bus!=NULL )
{
if( strcmp(device->bus, "pci")==0 )
visit_device_pci(path, device);
else if( strcmp(device->bus, "usb")==0 )
visit_device_usb(path, device);
else if( strcmp(device->bus, "ide")==0 )
visit_device_ide(path, device);
/** @todo This is a hack; is there such a thing as an ide_host? */
else if( strncmp(device->bus_id, "ide", 3)==0 )
visit_device_ide_host(path, device);
else
{
/*printf("bus=%s path=%s\n", device->bus, path);*/
}
/*
*/
/*
*/
}
/* Visit children */
if( visit_children && device->directory->subdirs!=NULL )
{
dlist_for_each_data(device->directory->subdirs, subdir,
struct sysfs_directory)
{
char newpath[SYSFS_PATH_MAX];
snprintf(newpath, SYSFS_PATH_MAX, "%s/%s", path, subdir->name);
visit_device(newpath, TRUE);
}
}
sysfs_close_device(device);
}
/* This function is documented in ../osspec.h */
void osspec_init(DBusConnection* dbus_connection)
{
int rc;
/* get mount path for sysfs */
rc = sysfs_get_mnt_path(sysfs_mount_path, SYSFS_PATH_MAX);
if( rc!=0 )
{
DIE(("Couldn't get mount path for sysfs"));
}
HAL_INFO(("Mountpoint for sysfs is %s", sysfs_mount_path));
linux_pci_init();
linux_usb_init();
linux_ide_init();
linux_class_scsi_init();
linux_class_block_init();
linux_class_net_init();
}
/** This is set to #TRUE if we are probing and #FALSE otherwise */
dbus_bool_t is_probing;
/* This function is documented in ../osspec.h */
void osspec_probe()
{
char path[SYSFS_PATH_MAX];
struct sysfs_directory* current;
struct sysfs_directory* dir;
is_probing = TRUE;
/* traverse /sys/devices */
strncpy(path, sysfs_mount_path, SYSFS_PATH_MAX);
strncat(path, SYSFS_DEVICES_DIR, SYSFS_PATH_MAX);
dir = sysfs_open_directory(path);
if( dir==NULL )
{
DIE(("Error opening sysfs directory at %s\n", path));
}
if( sysfs_read_directory(dir)!=0 )
{
DIE(("Error reading sysfs directory at %s\n", path));
}
if( dir->subdirs!=NULL )
{
dlist_for_each_data(dir->subdirs, current, struct sysfs_directory)
{
visit_device(current->path, TRUE);
}
}
sysfs_close_directory(dir);
/* visit class devices in /sys/class/scsi_host */
visit_class("scsi_host", FALSE);
/* visit class devices in /sys/class/scsi_host */
visit_class("scsi_device", FALSE);
/* visit all block devices */
visit_class("block", TRUE);
/* visit all net devices */
visit_class("net", TRUE);
/* input devices are not yet in sysfs */
linux_class_input_probe();
is_probing = FALSE;
/* Notify various device and class types that detection is done, so
* they can do some (optional) batch processing
*/
linux_pci_detection_done();
linux_usb_detection_done();
linux_ide_detection_done();
linux_class_block_detection_done();
linux_class_input_detection_done();
linux_class_net_detection_done();
linux_class_scsi_detection_done();
}
/** Maximum length of string parameter in hotplug input events */
#define HOTPLUG_INPUT_MAX 128
/** Handle a org.freedesktop.Hal.HotplugEvent message. This message
* origins from the hal.hotplug program, tools/linux/hal_hotplug.c,
* and is basically just a D-BUS-ification of the hotplug event.
*
* @param connection D-BUS connection
* @param message Message
* @return What to do with the message
*/
static DBusHandlerResult osspec_hotplug(DBusConnection* connection,
DBusMessage* message)
{
DBusMessageIter iter;
DBusMessageIter dict_iter;
dbus_bool_t is_add;
char* subsystem;
char input_name[HOTPLUG_INPUT_MAX];
char input_phys[HOTPLUG_INPUT_MAX];
char input_key[HOTPLUG_INPUT_MAX];
int input_ev=0;
int input_rel=0;
int input_abs=0;
int input_led=0;
char sysfs_devpath[SYSFS_PATH_MAX];
sysfs_devpath[0] = '\0';
input_name[0] = input_phys[0] = input_key[0] = '\0';
dbus_message_iter_init(message, &iter);
if( dbus_message_iter_get_arg_type(&iter)!=DBUS_TYPE_STRING )
{
/** @todo Report error */
dbus_message_unref(message);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
subsystem = dbus_message_iter_get_string(&iter);
dbus_message_iter_next(&iter);
dbus_message_iter_init_dict_iterator(&iter, &dict_iter);
is_add = FALSE;
for( ;dbus_message_iter_has_next(&dict_iter);
dbus_message_iter_next(&dict_iter))
{
char* key;
char* value;
key = dbus_message_iter_get_dict_key(&dict_iter);
value = dbus_message_iter_get_string(&dict_iter);
/*printf(" key/value = %s |-> %s\n", key, value);*/
if( strcmp(key, "ACTION")==0 )
{
if( strcmp(value, "add")==0 )
{
is_add = TRUE;
}
}
else if( strcmp(key, "DEVPATH")==0 )
{
strncpy(sysfs_devpath, sysfs_mount_path, SYSFS_PATH_MAX);
strncat(sysfs_devpath, value, SYSFS_PATH_MAX);
}
else if( strcmp(key, "NAME")==0 )
strncpy(input_name, value, HOTPLUG_INPUT_MAX);
else if( strcmp(key, "PHYS")==0 )
strncpy(input_phys, value, HOTPLUG_INPUT_MAX);
else if( strcmp(key, "KEY")==0 )
strncpy(input_key, value, HOTPLUG_INPUT_MAX);
else if( strcmp(key, "EV")==0 )
input_ev = parse_dec(value);
else if( strcmp(key, "REL")==0 )
input_rel = parse_hex(value);
else if( strcmp(key, "ABS")==0 )
input_abs = parse_hex(value);
else if( strcmp(key, "LED")==0 )
input_led = parse_hex(value);
}
HAL_INFO(("HotplugEvent %s, subsystem=%s",
(is_add ? "add" : "remove"), subsystem));
if( sysfs_devpath[0]!='\0' &&
(strcmp(subsystem, "usb")==0 ||
strcmp(subsystem, "pci")==0) )
{
if( is_add )
{
HAL_INFO(("Adding device @ sysfspath %s", sysfs_devpath));
visit_device(sysfs_devpath, FALSE);
}
else
{
HalDevice* d;
d = ds_device_find_by_key_value_string("Linux.sysfs_path",
sysfs_devpath);
if( d==NULL )
{
HAL_WARNING(("Couldn't remove device @ %s on hotplug remove",
sysfs_devpath));
}
else
{
HAL_INFO(("Removing device @ sysfspath %s, udi %s",
sysfs_devpath, d->udi));
ds_device_destroy(d);
}
}
}
else if( sysfs_devpath[0]!='\0' &&
(strcmp(subsystem, "net")==0 ||
strcmp(subsystem, "block")==0 ||
strcmp(subsystem, "scsi_host")==0 ||
strcmp(subsystem, "scsi_device")==0) )
{
if( is_add )
{
HAL_INFO(("Adding classdevice @ sysfspath %s", sysfs_devpath));
visit_class_device(sysfs_devpath, FALSE);
}
else
{
HalDevice* d;
d = ds_device_find_by_key_value_string("Linux.sysfs_path",
sysfs_devpath);
if( d==NULL )
{
HAL_WARNING(("Couldn't remove device @ %s on hotplug remove",
sysfs_devpath));
}
else
{
HAL_INFO(("Removing classdevice @ sysfspath %s, udi %s",
sysfs_devpath, d->udi));
ds_device_destroy(d);
}
}
}
else if( strcmp(subsystem, "input")==0 )
{
if( is_add )
{
/** @todo FIXME when kernel 2.6 got input devices in sysfs
* this terrible hack can be removed
*/
linux_class_input_handle_hotplug_add(input_name, input_phys,
input_key,
input_ev, input_rel,
input_abs, input_led);
}
else
{
/* This block is left intentionally blank, since input class
* devices simply attach to other (physical) devices and when
* they disappear, the input part also goes away
*/
}
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
/** Message handler for method invocations. All invocations on any object
* or interface is routed through this function.
*
* @param connection D-BUS connection
* @param message Message
* @param user_data User data
* @return What to do with the message
*/
DBusHandlerResult osspec_filter_function(DBusConnection* connection,
DBusMessage* message,
void* user_data)
{
/*
HAL_INFO(("obj_path=%s interface=%s method=%s",
dbus_message_get_path(message),
dbus_message_get_interface(message),
dbus_message_get_member(message)));
*/
if( dbus_message_is_method_call(
message,
"org.freedesktop.Hal.Linux.Hotplug",
"HotplugEvent") &&
strcmp(dbus_message_get_path(message),
"/org/freedesktop/Hal/Linux/Hotplug")==0 )
{
return osspec_hotplug(connection, message);
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
/** @} */
|