Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

bus_device.c

00001 /***************************************************************************
00002  * CVSID: $Id: bus_device.c,v 1.5 2004/04/12 20:11:51 joe Exp $
00003  *
00004  * Generic methods for bus devices
00005  *
00006  * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
00007  *
00008  * Licensed under the Academic Free License version 2.0
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  **************************************************************************/
00025 
00026 #ifdef HAVE_CONFIG_H
00027 #  include <config.h>
00028 #endif
00029 
00030 #include <ctype.h>
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <getopt.h>
00035 #include <assert.h>
00036 #include <unistd.h>
00037 #include <stdarg.h>
00038 #include <limits.h>
00039 #include <glib.h>
00040 
00041 #include "../logger.h"
00042 #include "../device_store.h"
00043 #include "../hald.h"
00044 #include "common.h"
00045 #include "bus_device.h"
00046 
00054 typedef struct {
00055     HalDevice *device;
00056     BusDeviceHandler *handler;
00057 } AsyncInfo;
00058 
00059 /* fwd decl */
00060 static void bus_device_got_parent (HalDeviceStore *store, HalDevice *parent,
00061                    gpointer user_data);
00062 
00071 dbus_bool_t
00072 bus_device_accept (BusDeviceHandler *self, const char *path, 
00073            struct sysfs_device *device, dbus_bool_t is_probing)
00074 {
00075     /* only care about given bus name  */
00076     return strcmp (device->bus, self->sysfs_bus_name) == 0;
00077 }
00078 
00089 void
00090 bus_device_visit (BusDeviceHandler *self, const char *path, 
00091           struct sysfs_device *device, dbus_bool_t is_probing)
00092 {
00093     AsyncInfo *ai;
00094     HalDevice *d;
00095     char *parent_sysfs_path;
00096     char buf[256];
00097 
00098     /* Construct a new device and add to temporary device list */
00099     d = hal_device_new ();
00100     hal_device_store_add (hald_get_tdl (), d);
00101     hal_device_property_set_string (d, "info.bus", self->hal_bus_name);
00102     hal_device_property_set_string (d, "linux.sysfs_path", path);
00103     hal_device_property_set_string (d, "linux.sysfs_path_device", path);
00104 
00109     snprintf (buf, sizeof(buf), "%s.linux.sysfs_path", self->hal_bus_name);
00110     hal_device_property_set_string (d, buf, path);
00111 
00112     parent_sysfs_path = get_parent_sysfs_path (path);
00113 
00114     /* Find parent; this happens asynchronously as our parent might
00115      * be added later. If we are probing this can't happen so the
00116      * timeout is set to zero in that event
00117      */
00118 
00119     ai = g_new0 (AsyncInfo, 1);
00120     ai->device = d;
00121     ai->handler = self;
00122         
00123     hal_device_store_match_key_value_string_async (
00124         hald_get_gdl (),
00125         "linux.sysfs_path_device",
00126         parent_sysfs_path,
00127         bus_device_got_parent, ai,
00128         is_probing ? 0 : HAL_LINUX_HOTPLUG_TIMEOUT);
00129 
00130     free (parent_sysfs_path);
00131 }
00132 
00140 static void
00141 bus_device_got_parent (HalDeviceStore *store, HalDevice *parent,
00142                gpointer user_data)
00143 {
00144     const char *sysfs_path = NULL;
00145     char *new_udi = NULL;
00146     HalDevice *new_d = NULL;
00147     AsyncInfo *ai = (AsyncInfo*) user_data;
00148     HalDevice *d = (HalDevice *) ai->device;
00149     BusDeviceHandler *self = (BusDeviceHandler *) ai->handler;
00150     struct sysfs_device *device;
00151 
00152     g_free (ai);
00153 
00154     /* set parent, if any */
00155     if (parent != NULL) {
00156         hal_device_property_set_string (d, "info.parent", parent->udi);
00157     }
00158 
00159     /* get more information about the device from the specialised 
00160      * function */
00161     sysfs_path = hal_device_property_get_string (d, "linux.sysfs_path");
00162     assert (sysfs_path != NULL);
00163     device = sysfs_open_device (sysfs_path);
00164     if (device == NULL)
00165         DIE (("Coulnd't get sysfs device object for path %s", 
00166               sysfs_path));
00167     self->pre_process (self, d, sysfs_path, device);
00168     sysfs_close_device (device);
00169 
00170     /* Compute a proper UDI (unique device id) and try to locate a 
00171      * persistent unplugged device or simply add this new device...
00172      */
00173     new_udi = rename_and_merge (d, self->compute_udi, self->hal_bus_name);
00174     if (new_udi != NULL) {
00175         new_d = hal_device_store_find (hald_get_gdl (), new_udi);
00176 
00177         self->got_udi (self, new_d != NULL ? new_d : d, new_udi);
00178 
00179         hal_device_store_add (hald_get_gdl (),
00180                       new_d != NULL ? new_d : d);
00181     }
00182     hal_device_store_remove (hald_get_tdl (), d);
00183     g_object_unref (d);
00184 }
00185 
00191 void
00192 bus_device_detection_done (BusDeviceHandler *self)
00193 {
00194 }
00195 
00200 void
00201 bus_device_init (BusDeviceHandler *self)
00202 {
00203 }
00204 
00209 void
00210 bus_device_shutdown (BusDeviceHandler *self)
00211 {
00212 }
00213 
00214 
00220 void
00221 bus_device_tick (BusDeviceHandler *self)
00222 {
00223 }
00224 
00233 void
00234 bus_device_removed (BusDeviceHandler *self, const char *sysfs_path, 
00235             HalDevice *d)
00236 {
00237 }
00238 
00251 void 
00252 bus_device_pre_process (BusDeviceHandler *self,
00253             HalDevice *d,
00254             const char *sysfs_path,
00255             struct sysfs_device *device)
00256 {
00257 }
00258 
00259 void 
00260 bus_device_got_udi (BusDeviceHandler *self,
00261             HalDevice *d,
00262             const char *udi)
00263 {
00264 }
00265 
00266 

Generated on Sat Apr 24 19:57:44 2004 for HAL by doxygen 1.3.6-20040222