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

linux_i2c.c

00001 /***************************************************************************
00002  * CVSID: $Id: linux_i2c.c,v 1.2 2004/03/03 17:56:56 david Exp $
00003  *
00004  * linux_i2c.c : I2C handling on Linux 2.6; based on linux_pci.c
00005  *
00006  * Copyright (C) 2004 Matthew Mastracci <matt@aclaro.com>
00007  * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
00008  *
00009  * Licensed under the Academic Free License version 2.0
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  *
00025  **************************************************************************/
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 #include <ctype.h>
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <getopt.h>
00036 #include <assert.h>
00037 #include <unistd.h>
00038 #include <stdarg.h>
00039 
00040 #include "../logger.h"
00041 #include "../device_store.h"
00042 #include "linux_i2c.h"
00043 
00069 static char *
00070 i2c_compute_udi (HalDevice * d, int append_num)
00071 {
00072     static char buf[256];
00073 
00074     if (append_num == -1)
00075         sprintf (buf, "/org/freedesktop/Hal/devices/i2c_%s",
00076              ds_property_get_string (d, "i2c.product"));
00077     else
00078         sprintf (buf, "/org/freedesktop/Hal/devices/i2c_%s/%d",
00079              ds_property_get_string (d, "i2c.product"),
00080              append_num);
00081 
00082     return buf;
00083 }
00084 
00085 
00086 /* fwd decl */
00087 static void visit_device_i2c_got_parent (HalDevice * parent,
00088                      void *data1, void *data2);
00089 
00098 void
00099 visit_device_i2c (const char *path, struct sysfs_device *device)
00100 {
00101     int i;
00102     int len;
00103     HalDevice *d;
00104     char attr_name[SYSFS_NAME_LEN];
00105     struct sysfs_attribute *cur;
00106     char *product_name = NULL;
00107     const char *driver;
00108     char *parent_sysfs_path;
00109 
00110     /* Must be a new I2C device */
00111     d = ds_device_new ();
00112     ds_property_set_string (d, "info.bus", "i2c");
00113     ds_property_set_string (d, "info.category", "i2c");
00114     ds_property_set_string (d, "info.capabilities", "i2c");
00115     ds_property_set_string (d, "linux.sysfs_path", path);
00116     ds_property_set_string (d, "linux.sysfs_path_device", path);
00121     ds_property_set_string (d, "i2c.linux.sysfs_path", path);
00122     /*printf("*** created udi=%s for path=%s\n", d, path); */
00123 
00124     /* set driver */
00125     driver = drivers_lookup (path);
00126     if (driver != NULL)
00127         ds_property_set_string (d, "linux.driver", driver);
00128 
00129     dlist_for_each_data (sysfs_get_device_attributes (device), cur,
00130                  struct sysfs_attribute) {
00131 
00132         if (sysfs_get_name_from_path (cur->path,
00133                           attr_name,
00134                           SYSFS_NAME_LEN) != 0)
00135             continue;
00136 
00137         /* strip whitespace */
00138         len = strlen (cur->value);
00139         for (i = len - 1; isspace (cur->value[i]); --i)
00140             cur->value[i] = '\0';
00141 
00142         /*printf("attr_name=%s -> '%s'\n", attr_name, cur->value); */
00143 
00144         if (strcmp (attr_name, "name") == 0)
00145             product_name = cur->value;
00146     }
00147 
00148     if (product_name == NULL)
00149         product_name = "Unknown";
00150 
00151     ds_property_set_string (d, "i2c.product", product_name);
00152 
00153     /* Provide best-guess of name, goes in Product property; 
00154      * .fdi files can override this */
00155     ds_property_set_string (d, "info.product", product_name);
00156 
00157     parent_sysfs_path = get_parent_sysfs_path (path);
00158 
00159     /* Find parent; this happens asynchronously as our parent might
00160      * be added later. If we are probing this can't happen so the
00161      * timeout is set to zero in that event..
00162      */
00163     HAL_INFO (("For device = %s, parent = %s",
00164            ds_property_get_string (d, "linux.sysfs_path"),
00165            parent_sysfs_path));
00166     ds_device_async_find_by_key_value_string
00167         ("linux.sysfs_path_device", parent_sysfs_path, TRUE,
00168          visit_device_i2c_got_parent, (void *) d, NULL,
00169          is_probing ? 0 : HAL_LINUX_HOTPLUG_TIMEOUT);
00170 
00171     free (parent_sysfs_path);
00172 }
00173 
00181 static void
00182 visit_device_i2c_got_parent (HalDevice * parent, void *data1, void *data2)
00183 {
00184     char *new_udi = NULL;
00185     HalDevice *new_d = NULL;
00186     HalDevice *d = (HalDevice *) data1;
00187 
00188     if (parent != NULL) {
00189         ds_property_set_string (d, "info.parent", parent->udi);
00190     } else {
00191         /* An I2C device should always have a parent! */
00192         HAL_WARNING (("No parent for I2C device!"));
00193     }
00194 
00195     /* Compute a proper UDI (unique device id) and try to locate a 
00196      * persistent unplugged device or simple add this new device...
00197      */
00198     new_udi = rename_and_merge (d, i2c_compute_udi, "i2c");
00199     if (new_udi != NULL) {
00200         new_d = ds_device_find (new_udi);
00201         if (new_d != NULL) {
00202             ds_gdl_add (new_d);
00203         }
00204     }
00205 }
00206 
00210 void
00211 linux_i2c_init ()
00212 {
00213     /* get all drivers under /sys/bus/i2c/drivers */
00214     drivers_collect ("i2c");
00215 }
00216 
00221 void
00222 linux_i2c_detection_done ()
00223 {
00224 }
00225 
00229 void
00230 linux_i2c_shutdown ()
00231 {
00232 }
00233 

Generated on Thu Mar 11 21:32:22 2004 for HAL by doxygen 1.3.6-20040222