asynchronous and deferred port detection
Allow plugins to perform asynchronous port detection, and to defer port detection until later. This moves the prober bits into MMPluginBase so that all plugins can take adavantage of it only when needed; the probing is not done at udev time. Furthermore, plugins like Novatel can flip the secondary ports over the AT mode through deferred detection, by deferring the secondary ports until the main port has been detected and AT$NWDMAT has been sent. This commit also finishes the port of the rest of the plugins (except mbm) over to the new port detection methods and plugin API.
This commit is contained in:
@@ -25,291 +25,163 @@
|
||||
#include <time.h>
|
||||
|
||||
#include <gmodule.h>
|
||||
#define G_UDEV_API_IS_SUBJECT_TO_CHANGE
|
||||
#include <gudev/gudev.h>
|
||||
|
||||
#include "mm-plugin-generic.h"
|
||||
#include "mm-generic-gsm.h"
|
||||
#include "mm-generic-cdma.h"
|
||||
#include "mm-errors.h"
|
||||
#include "mm-serial-parsers.h"
|
||||
|
||||
static void plugin_init (MMPlugin *plugin_class);
|
||||
|
||||
G_DEFINE_TYPE_EXTENDED (MMPluginGeneric, mm_plugin_generic, MM_TYPE_PLUGIN_BASE,
|
||||
0, G_IMPLEMENT_INTERFACE (MM_TYPE_PLUGIN, plugin_init))
|
||||
G_DEFINE_TYPE (MMPluginGeneric, mm_plugin_generic, MM_TYPE_PLUGIN_BASE)
|
||||
|
||||
int mm_plugin_major_version = MM_PLUGIN_MAJOR_VERSION;
|
||||
int mm_plugin_minor_version = MM_PLUGIN_MINOR_VERSION;
|
||||
|
||||
#define MM_PLUGIN_GENERIC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_PLUGIN_GENERIC, MMPluginGenericPrivate))
|
||||
|
||||
typedef struct {
|
||||
GUdevClient *client;
|
||||
} MMPluginGenericPrivate;
|
||||
|
||||
|
||||
G_MODULE_EXPORT MMPlugin *
|
||||
mm_plugin_create (void)
|
||||
{
|
||||
return MM_PLUGIN (g_object_new (MM_TYPE_PLUGIN_GENERIC, NULL));
|
||||
return MM_PLUGIN (g_object_new (MM_TYPE_PLUGIN_GENERIC,
|
||||
MM_PLUGIN_BASE_NAME, MM_PLUGIN_GENERIC_NAME,
|
||||
NULL));
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
static char *
|
||||
get_driver_name (GUdevDevice *device)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
if (driver)
|
||||
ret = g_strdup (driver);
|
||||
if (parent)
|
||||
g_object_unref (parent);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static GUdevDevice *
|
||||
find_physical_device (GUdevDevice *child)
|
||||
{
|
||||
GUdevDevice *iter, *old = NULL;
|
||||
const char *bus, *type;
|
||||
|
||||
g_return_val_if_fail (child != NULL, NULL);
|
||||
|
||||
bus = g_udev_device_get_property (child, "ID_BUS");
|
||||
if (!bus)
|
||||
return NULL;
|
||||
|
||||
if (!strcmp (bus, "usb")) {
|
||||
/* Walk the parents to find the '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;
|
||||
|
||||
old = iter;
|
||||
iter = g_udev_device_get_parent (old);
|
||||
g_object_unref (old);
|
||||
}
|
||||
g_object_unref (child);
|
||||
} else if (!strcmp (bus, "pci")) {
|
||||
return g_udev_device_get_parent (child);
|
||||
}
|
||||
|
||||
// FIXME: pci and pcmcia/cardbus? (like Sierra 850/860)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#define PROP_GSM "ID_MM_MODEM_GSM"
|
||||
#define PROP_CDMA "ID_MM_MODEM_IS707_A"
|
||||
#define PROP_EVDO1 "ID_MM_MODEM_IS856"
|
||||
#define PROP_EVDOA "ID_MM_MODEM_IS856_A"
|
||||
|
||||
static GUdevDevice *
|
||||
get_device_type (MMPlugin *plugin,
|
||||
const char *subsys,
|
||||
const char *name,
|
||||
gboolean *gsm,
|
||||
gboolean *cdma)
|
||||
{
|
||||
MMPluginGenericPrivate *priv = MM_PLUGIN_GENERIC_GET_PRIVATE (plugin);
|
||||
GUdevDevice *device;
|
||||
|
||||
g_return_val_if_fail (plugin != NULL, NULL);
|
||||
g_return_val_if_fail (MM_IS_PLUGIN (plugin), NULL);
|
||||
g_return_val_if_fail (subsys != NULL, NULL);
|
||||
g_return_val_if_fail (name != NULL, NULL);
|
||||
|
||||
device = g_udev_client_query_by_subsystem_and_name (priv->client, subsys, name);
|
||||
if (!device)
|
||||
return NULL;
|
||||
|
||||
if (g_udev_device_get_property_as_boolean (device, PROP_GSM))
|
||||
*gsm = TRUE;
|
||||
if ( g_udev_device_get_property_as_boolean (device, PROP_CDMA)
|
||||
|| g_udev_device_get_property_as_boolean (device, PROP_EVDO1)
|
||||
|| g_udev_device_get_property_as_boolean (device, PROP_EVDOA))
|
||||
*cdma = TRUE;
|
||||
|
||||
return device;
|
||||
}
|
||||
#define CAP_CDMA (MM_PLUGIN_BASE_PORT_CAP_IS707_A | \
|
||||
MM_PLUGIN_BASE_PORT_CAP_IS707_P | \
|
||||
MM_PLUGIN_BASE_PORT_CAP_IS856 | \
|
||||
MM_PLUGIN_BASE_PORT_CAP_IS856_A)
|
||||
|
||||
static guint32
|
||||
supports_port (MMPlugin *plugin,
|
||||
const char *subsys,
|
||||
const char *name)
|
||||
get_level_for_capabilities (guint32 capabilities)
|
||||
{
|
||||
GUdevDevice *device, *physdev = NULL;
|
||||
gboolean gsm = FALSE, cdma = FALSE;
|
||||
guint32 level = 0;
|
||||
if (capabilities & MM_PLUGIN_BASE_PORT_CAP_GSM)
|
||||
return 5;
|
||||
if (capabilities & CAP_CDMA)
|
||||
return 5;
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
static void
|
||||
probe_result (MMPluginBase *base,
|
||||
MMPluginBaseSupportsTask *task,
|
||||
guint32 capabilities,
|
||||
gpointer user_data)
|
||||
{
|
||||
mm_plugin_base_supports_task_complete (task, get_level_for_capabilities (capabilities));
|
||||
}
|
||||
|
||||
static MMPluginSupportsResult
|
||||
supports_port (MMPluginBase *base,
|
||||
MMModem *existing,
|
||||
MMPluginBaseSupportsTask *task)
|
||||
{
|
||||
GUdevDevice *port;
|
||||
guint32 cached = 0, level;
|
||||
|
||||
/* Can't do anything with non-serial ports */
|
||||
if (strcmp (subsys, "tty"))
|
||||
return 0;
|
||||
port = mm_plugin_base_supports_task_get_port (task);
|
||||
if (strcmp (g_udev_device_get_subsystem (port), "tty"))
|
||||
return MM_PLUGIN_SUPPORTS_PORT_UNSUPPORTED;
|
||||
|
||||
device = get_device_type (plugin, subsys, name, &gsm, &cdma);
|
||||
if (!device)
|
||||
return 0;
|
||||
if (mm_plugin_base_get_cached_port_capabilities (base, port, &cached)) {
|
||||
level = get_level_for_capabilities (cached);
|
||||
if (level) {
|
||||
mm_plugin_base_supports_task_complete (task, level);
|
||||
return MM_PLUGIN_SUPPORTS_PORT_IN_PROGRESS;
|
||||
}
|
||||
return MM_PLUGIN_SUPPORTS_PORT_UNSUPPORTED;
|
||||
}
|
||||
|
||||
if (!gsm && !cdma)
|
||||
goto out;
|
||||
/* Otherwise kick off a probe */
|
||||
if (mm_plugin_base_probe_port (base, task, NULL))
|
||||
return MM_PLUGIN_SUPPORTS_PORT_IN_PROGRESS;
|
||||
|
||||
physdev = find_physical_device (device);
|
||||
if (!physdev)
|
||||
goto out;
|
||||
g_object_unref (physdev);
|
||||
level = 5;
|
||||
|
||||
out:
|
||||
return level;
|
||||
return MM_PLUGIN_SUPPORTS_PORT_UNSUPPORTED;
|
||||
}
|
||||
|
||||
static MMModem *
|
||||
grab_port (MMPlugin *plugin,
|
||||
const char *subsys,
|
||||
const char *name,
|
||||
grab_port (MMPluginBase *base,
|
||||
MMModem *existing,
|
||||
MMPluginBaseSupportsTask *task,
|
||||
GError **error)
|
||||
{
|
||||
MMPluginGeneric *self = MM_PLUGIN_GENERIC (plugin);
|
||||
GUdevDevice *device = NULL, *physdev = NULL;
|
||||
const char *devfile, *sysfs_path;
|
||||
char *driver = NULL;
|
||||
GUdevDevice *port = NULL, *physdev = NULL;
|
||||
MMModem *modem = NULL;
|
||||
gboolean gsm = FALSE, cdma = FALSE;
|
||||
const char *name, *subsys, *devfile, *sysfs_path;
|
||||
guint32 caps;
|
||||
|
||||
g_return_val_if_fail (subsys != NULL, NULL);
|
||||
g_return_val_if_fail (name != NULL, NULL);
|
||||
port = mm_plugin_base_supports_task_get_port (task);
|
||||
g_assert (port);
|
||||
|
||||
/* Can't do anything with non-serial ports */
|
||||
if (strcmp (subsys, "tty"))
|
||||
return NULL;
|
||||
|
||||
device = get_device_type (plugin, subsys, name, &gsm, &cdma);
|
||||
if (!device) {
|
||||
g_set_error (error, 0, 0, "Could not get port's udev device.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!gsm && !cdma) {
|
||||
g_set_error (error, 0, 0, "Modem unsupported (not GSM or CDMA).");
|
||||
goto out;
|
||||
}
|
||||
|
||||
physdev = find_physical_device (device);
|
||||
if (!physdev) {
|
||||
g_set_error (error, 0, 0, "Could not get ports's physical device.");
|
||||
goto out;
|
||||
}
|
||||
|
||||
devfile = g_udev_device_get_device_file (device);
|
||||
devfile = g_udev_device_get_device_file (port);
|
||||
if (!devfile) {
|
||||
g_set_error (error, 0, 0, "Could not get port's sysfs file.");
|
||||
goto out;
|
||||
}
|
||||
|
||||
driver = get_driver_name (device);
|
||||
if (!driver) {
|
||||
g_set_error (error, 0, 0, "Could not get port's driver name.");
|
||||
goto out;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
physdev = mm_plugin_base_supports_task_get_physdev (task);
|
||||
g_assert (physdev);
|
||||
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 NULL;
|
||||
}
|
||||
|
||||
modem = mm_plugin_base_find_modem (MM_PLUGIN_BASE (self), sysfs_path);
|
||||
if (!modem) {
|
||||
if (gsm) {
|
||||
subsys = g_udev_device_get_subsystem (port);
|
||||
name = g_udev_device_get_name (port);
|
||||
|
||||
caps = mm_plugin_base_supports_task_get_probed_capabilities (task);
|
||||
if (!existing) {
|
||||
if (caps & MM_PLUGIN_BASE_PORT_CAP_GSM) {
|
||||
modem = mm_generic_gsm_new (sysfs_path,
|
||||
driver,
|
||||
mm_plugin_get_name (plugin));
|
||||
} else if (cdma) {
|
||||
mm_plugin_base_supports_task_get_driver (task),
|
||||
mm_plugin_get_name (MM_PLUGIN (base)));
|
||||
} else if (caps & CAP_CDMA) {
|
||||
modem = mm_generic_cdma_new (sysfs_path,
|
||||
driver,
|
||||
mm_plugin_get_name (plugin));
|
||||
mm_plugin_base_supports_task_get_driver (task),
|
||||
mm_plugin_get_name (MM_PLUGIN (base)));
|
||||
}
|
||||
|
||||
if (modem) {
|
||||
if (!mm_modem_grab_port (modem, subsys, name, error)) {
|
||||
if (!mm_modem_grab_port (modem, subsys, name, NULL, error)) {
|
||||
g_object_unref (modem);
|
||||
modem = NULL;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (modem)
|
||||
mm_plugin_base_add_modem (MM_PLUGIN_BASE (self), modem);
|
||||
} else {
|
||||
if (gsm || cdma) {
|
||||
if (!mm_modem_grab_port (modem, subsys, name, error))
|
||||
modem = NULL;
|
||||
if (caps & (MM_PLUGIN_BASE_PORT_CAP_GSM | CAP_CDMA)) {
|
||||
modem = existing;
|
||||
if (!mm_modem_grab_port (modem, subsys, name, NULL, error))
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
g_free (driver);
|
||||
g_object_unref (device);
|
||||
g_object_unref (physdev);
|
||||
return modem;
|
||||
}
|
||||
|
||||
static const char *
|
||||
get_name (MMPlugin *plugin)
|
||||
{
|
||||
return "Generic";
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
static void
|
||||
plugin_init (MMPlugin *plugin_class)
|
||||
{
|
||||
/* interface implementation */
|
||||
plugin_class->get_name = get_name;
|
||||
plugin_class->supports_port = supports_port;
|
||||
plugin_class->grab_port = grab_port;
|
||||
}
|
||||
|
||||
static void
|
||||
mm_plugin_generic_init (MMPluginGeneric *self)
|
||||
{
|
||||
MMPluginGenericPrivate *priv = MM_PLUGIN_GENERIC_GET_PRIVATE (self);
|
||||
const char *subsys[2] = { "tty", NULL };
|
||||
|
||||
priv->client = g_udev_client_new (subsys);
|
||||
g_signal_connect (self, "probe-result", G_CALLBACK (probe_result), NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
dispose (GObject *object)
|
||||
{
|
||||
MMPluginGenericPrivate *priv = MM_PLUGIN_GENERIC_GET_PRIVATE (object);
|
||||
|
||||
g_object_unref (priv->client);
|
||||
}
|
||||
|
||||
static void
|
||||
mm_plugin_generic_class_init (MMPluginGenericClass *klass)
|
||||
{
|
||||
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
||||
|
||||
g_type_class_add_private (object_class, sizeof (MMPluginGenericPrivate));
|
||||
MMPluginBaseClass *pb_class = MM_PLUGIN_BASE_CLASS (klass);
|
||||
|
||||
object_class->dispose = dispose;
|
||||
|
||||
pb_class->supports_port = supports_port;
|
||||
pb_class->grab_port = grab_port;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user