diff options
Diffstat (limited to 'plugins/mm-plugin-hso.c')
-rw-r--r-- | plugins/mm-plugin-hso.c | 365 |
1 files changed, 201 insertions, 164 deletions
diff --git a/plugins/mm-plugin-hso.c b/plugins/mm-plugin-hso.c index ce280759..9481bbf4 100644 --- a/plugins/mm-plugin-hso.c +++ b/plugins/mm-plugin-hso.c @@ -1,18 +1,43 @@ /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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: + * + * Copyright (C) 2008 - 2009 Novell, Inc. + * Copyright (C) 2009 Red Hat, Inc. + */ #include <string.h> #include <gmodule.h> + +#define G_UDEV_API_IS_SUBJECT_TO_CHANGE +#include <gudev/gudev.h> + #include "mm-plugin-hso.h" #include "mm-modem-hso.h" static void plugin_init (MMPlugin *plugin_class); -G_DEFINE_TYPE_EXTENDED (MMPluginHso, mm_plugin_hso, G_TYPE_OBJECT, +G_DEFINE_TYPE_EXTENDED (MMPluginHso, mm_plugin_hso, MM_TYPE_PLUGIN_BASE, 0, G_IMPLEMENT_INTERFACE (MM_TYPE_PLUGIN, plugin_init)) int mm_plugin_major_version = MM_PLUGIN_MAJOR_VERSION; int mm_plugin_minor_version = MM_PLUGIN_MINOR_VERSION; +#define MM_PLUGIN_HSO_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_PLUGIN_HSO, MMPluginHsoPrivate)) + +typedef struct { + GUdevClient *client; +} MMPluginHsoPrivate; + + G_MODULE_EXPORT MMPlugin * mm_plugin_create (void) { @@ -21,202 +46,198 @@ mm_plugin_create (void) /*****************************************************************************/ -static const char * -get_name (MMPlugin *plugin) +static char * +get_driver_name (GUdevDevice *device) { - return "HSO"; -} + GUdevDevice *parent = NULL; + const char *driver; + char *ret; + + driver = g_udev_device_get_driver (device); + if (!driver) { + parent = g_udev_device_get_parent (device); + if (parent) + driver = g_udev_device_get_driver (parent); + } -static char ** -list_supported_udis (MMPlugin *plugin, LibHalContext *hal_ctx) -{ - char **supported = NULL; - char **devices; - int num_devices; - int i; + if (driver) + ret = g_strdup (driver); + if (parent) + g_object_unref (parent); - devices = libhal_find_device_by_capability (hal_ctx, "serial", &num_devices, NULL); - if (devices) { - GPtrArray *array; + return ret; +} - array = g_ptr_array_new (); +static GUdevDevice * +find_physical_device (GUdevDevice *child) +{ + GUdevDevice *iter, *old = NULL; + const char *type; - for (i = 0; i < num_devices; i++) { - char *udi = devices[i]; + g_return_val_if_fail (child != NULL, NULL); - if (mm_plugin_supports_udi (plugin, hal_ctx, udi)) - g_ptr_array_add (array, g_strdup (udi)); - } + /* Walk the parents to find the first 'usb_device' for this device. */ + iter = g_object_ref (child); + while (iter) { + type = g_udev_device_get_devtype (iter); + if (type && !strcmp (type, "usb_device")) + return iter; - if (array->len > 0) { - g_ptr_array_add (array, NULL); - supported = (char **) g_ptr_array_free (array, FALSE); - } else - g_ptr_array_free (array, TRUE); + old = iter; + iter = g_udev_device_get_parent (old); + g_object_unref (old); } + g_object_unref (child); - g_strfreev (devices); - - return supported; + return NULL; } -static char * -get_driver_name (LibHalContext *ctx, const char *udi) +static GUdevDevice * +get_device (GUdevClient *client, + const char *subsys, + const char *name, + GUdevDevice **physdev, + char **driver, + GError **error) { - char *parent_udi; - char *driver = NULL; + GUdevDevice *device = NULL; + + if (strcmp (subsys, "tty") && strcmp (subsys, "net")) { + g_set_error (error, 0, 0, "Unsupported subsystem."); + return NULL; + } + + device = g_udev_client_query_by_subsystem_and_name (client, subsys, name); + if (!device) { + g_set_error (error, 0, 0, "Coud not get port's udev device."); + return NULL; + } - parent_udi = libhal_device_get_property_string (ctx, udi, "info.parent", NULL); - if (parent_udi) { - driver = libhal_device_get_property_string (ctx, parent_udi, "info.linux.driver", NULL); - libhal_free_string (parent_udi); - } + *driver = get_driver_name (device); + if (!*driver || strcmp (*driver, "hso")) { + g_set_error (error, 0, 0, "Unsupported driver (not 'hso')."); + g_object_unref (device); + device = NULL; + goto out; + } - return driver; + *physdev = find_physical_device (device); + if (!*physdev) { + g_set_error (error, 0, 0, "Could not get port's physical udev device."); + g_object_unref (device); + device = NULL; + } + +out: + return device; } -static gboolean -supports_udi (MMPlugin *plugin, LibHalContext *hal_ctx, const char *udi) +static guint32 +supports_port (MMPlugin *plugin, + const char *subsys, + const char *name) { - char *driver_name; - gboolean supported = FALSE; + MMPluginHsoPrivate *priv = MM_PLUGIN_HSO_GET_PRIVATE (plugin); + GUdevDevice *device, *physdev = NULL; + guint32 level = 0; + char *driver = NULL; - driver_name = get_driver_name (hal_ctx, udi); - if (driver_name && !strcmp (driver_name, "hso")) { - char *sysfs_path; + g_return_val_if_fail (plugin != NULL, 0); + g_return_val_if_fail (MM_IS_PLUGIN (plugin), 0); + g_return_val_if_fail (subsys != NULL, 0); + g_return_val_if_fail (name != NULL, 0); - sysfs_path = libhal_device_get_property_string (hal_ctx, udi, "linux.sysfs_path", NULL); - if (sysfs_path) { - char *hso_type_path; - gchar *contents = NULL; - gsize length; + device = get_device (priv->client, subsys, name, &physdev, &driver, NULL); + if (device) + level = 10; - hso_type_path = g_build_filename (sysfs_path, "hsotype", NULL); - libhal_free_string (sysfs_path); + g_free (driver); + if (physdev) + g_object_unref (physdev); + if (device) + g_object_unref (device); + return level; +} - if (g_file_get_contents (hso_type_path, &contents, &length, NULL)) { - if (g_str_has_prefix (contents, "Control")) - supported = TRUE; +static MMModem * +grab_port (MMPlugin *plugin, + const char *subsys, + const char *name, + GError **error) +{ + MMPluginHso *self = MM_PLUGIN_HSO (plugin); + MMPluginHsoPrivate *priv = MM_PLUGIN_HSO_GET_PRIVATE (plugin); + GUdevDevice *device = NULL, *physdev = NULL; + const char *sysfs_path = NULL; + char *driver = NULL, *devfile = NULL; + MMModem *modem = NULL; + + g_return_val_if_fail (subsys != NULL, NULL); + g_return_val_if_fail (name != NULL, NULL); + + device = get_device (priv->client, subsys, name, &physdev, &driver, error); + if (!device) { + g_set_error (error, 0, 0, "Could not get port's udev device."); + return NULL; + } - g_free (contents); + devfile = g_strdup (g_udev_device_get_device_file (device)); + if (!devfile) { + if (!strcmp (subsys, "net")) { + /* Apparently 'hso' doesn't set up the right links for the netdevice, + * and thus libgudev can't get the sysfs file path for it. + */ + devfile = g_strdup_printf ("/sys/class/net/%s", name); + if (!g_file_test (devfile, G_FILE_TEST_EXISTS)) { + g_free (devfile); + devfile = NULL; } + } - g_free (hso_type_path); + if (!devfile) { + g_set_error (error, 0, 0, "Could not get port's sysfs file."); + goto out; } } - libhal_free_string (driver_name); + sysfs_path = g_udev_device_get_sysfs_path (physdev); + if (!sysfs_path) { + g_set_error (error, 0, 0, "Could not get port's physical device sysfs path."); + goto out; + } - return supported; -} + modem = mm_plugin_base_find_modem (MM_PLUGIN_BASE (self), sysfs_path); + if (!modem) { + modem = mm_modem_hso_new (sysfs_path, + driver, + mm_plugin_get_name (plugin)); -static char * -get_netdev (LibHalContext *ctx, const char *udi) -{ - char *serial_parent, *netdev = NULL; - char **netdevs; - int num, i; - - /* Get the serial interface's originating device UDI, used to find the - * originating device's netdev. - */ - serial_parent = libhal_device_get_property_string (ctx, udi, "serial.originating_device", NULL); - if (!serial_parent) - serial_parent = libhal_device_get_property_string (ctx, udi, "info.parent", NULL); - if (!serial_parent) - return NULL; - - /* Look for the originating device's netdev */ - netdevs = libhal_find_device_by_capability (ctx, "net", &num, NULL); - for (i = 0; netdevs && !netdev && (i < num); i++) { - char *netdev_parent, *tmp; - - netdev_parent = libhal_device_get_property_string (ctx, netdevs[i], "net.originating_device", NULL); - if (!netdev_parent) - netdev_parent = libhal_device_get_property_string (ctx, netdevs[i], "net.physical_device", NULL); - if (!netdev_parent) - continue; - - if (!strcmp (netdev_parent, serial_parent)) { - /* We found it */ - tmp = libhal_device_get_property_string (ctx, netdevs[i], "net.interface", NULL); - if (tmp) { - netdev = g_strdup (tmp); - libhal_free_string (tmp); - } - } - - libhal_free_string (netdev_parent); - } - - if (!netdev) { - /* Didn't find from netdev's parents. Try again with "grandparents" */ - char *serial_grandparent; - - serial_grandparent = libhal_device_get_property_string (ctx, serial_parent, "info.parent", NULL); - if (!serial_grandparent) - goto cleanup; - - for (i = 0; netdevs && !netdev && (i < num); i++) { - char *netdev_parent, *tmp; - - tmp = libhal_device_get_property_string (ctx, netdevs[i], "net.originating_device", NULL); - if (!tmp) - tmp = libhal_device_get_property_string (ctx, netdevs[i], "net.physical_device", NULL); - if (!tmp) - tmp = libhal_device_get_property_string (ctx, netdevs[i], "info.parent", NULL); - if (!tmp) - continue; - - netdev_parent = libhal_device_get_property_string (ctx, tmp, "info.parent", NULL); - libhal_free_string (tmp); - - if (netdev_parent) { - if (!strcmp (netdev_parent, serial_grandparent)) { - /* We found it */ - tmp = libhal_device_get_property_string (ctx, netdevs[i], "net.interface", NULL); - if (tmp) { - netdev = g_strdup (tmp); - libhal_free_string (tmp); - } - } - - libhal_free_string (netdev_parent); + if (modem) { + if (!mm_modem_grab_port (modem, subsys, name, error)) { + g_object_unref (modem); + modem = NULL; } } - } - cleanup: - libhal_free_string_array (netdevs); - libhal_free_string (serial_parent); + if (modem) + mm_plugin_base_add_modem (MM_PLUGIN_BASE (self), modem); + } else { + if (!mm_modem_grab_port (modem, subsys, name, error)) + modem = NULL; + } - return netdev; +out: + g_free (devfile); + g_free (driver); + g_object_unref (device); + g_object_unref (physdev); + return modem; } -static MMModem * -create_modem (MMPlugin *plugin, LibHalContext *hal_ctx, const char *udi) +static const char * +get_name (MMPlugin *plugin) { - char *serial_device; - char *net_device; - char *driver; - MMModem *modem; - - serial_device = libhal_device_get_property_string (hal_ctx, udi, "serial.device", NULL); - g_return_val_if_fail (serial_device != NULL, NULL); - - driver = get_driver_name (hal_ctx, udi); - g_return_val_if_fail (driver != NULL, NULL); - - net_device = get_netdev (hal_ctx, udi); - g_return_val_if_fail (net_device != NULL, NULL); - - modem = MM_MODEM (mm_modem_hso_new (serial_device, net_device, driver)); - - g_free (serial_device); - g_free (net_device); - g_free (driver); - - return modem; + return "HSO"; } /*****************************************************************************/ @@ -226,17 +247,33 @@ plugin_init (MMPlugin *plugin_class) { /* interface implementation */ plugin_class->get_name = get_name; - plugin_class->list_supported_udis = list_supported_udis; - plugin_class->supports_udi = supports_udi; - plugin_class->create_modem = create_modem; + plugin_class->supports_port = supports_port; + plugin_class->grab_port = grab_port; } static void mm_plugin_hso_init (MMPluginHso *self) { + MMPluginHsoPrivate *priv = MM_PLUGIN_HSO_GET_PRIVATE (self); + const char *subsys[] = { "tty", "net", NULL }; + + priv->client = g_udev_client_new (subsys); +} + +static void +dispose (GObject *object) +{ + MMPluginHsoPrivate *priv = MM_PLUGIN_HSO_GET_PRIVATE (object); + + g_object_unref (priv->client); } static void mm_plugin_hso_class_init (MMPluginHsoClass *klass) { + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (MMPluginHsoPrivate)); + + object_class->dispose = dispose; } |