From a0be0fea1782ed7b0eb0753f67866811eb166f48 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Sun, 10 Aug 2008 17:45:38 +0100 Subject: [PATCH] add CSR support so we know about mice and keyboards --- src/Makefile.am | 2 + src/dkp-csr.c | 318 +++++++++++++++++++++++++++++++++++++++++++++++ src/dkp-csr.h | 56 +++++++++ src/dkp-daemon.c | 4 +- src/dkp-device.c | 63 ++++++---- 5 files changed, 414 insertions(+), 29 deletions(-) create mode 100644 src/dkp-csr.c create mode 100644 src/dkp-csr.h diff --git a/src/Makefile.am b/src/Makefile.am index 28a9cba..94bd40e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -46,6 +46,7 @@ devkit_power_daemon_SOURCES = \ dkp-device.h dkp-device.c \ dkp-device-list.h dkp-device-list.c \ dkp-supply.h dkp-supply.c \ + dkp-csr.h dkp-csr.c \ dkp-history.h dkp-history.c \ sysfs-utils.h sysfs-utils.c \ main.c \ @@ -58,6 +59,7 @@ devkit_power_daemon_CPPFLAGS = \ $(AM_CPPFLAGS) devkit_power_daemon_LDADD = \ + -lusb \ $(GIO_LIBS) \ $(DBUS_GLIB_LIBS) \ $(POLKIT_DBUS_LIBS) \ diff --git a/src/dkp-csr.c b/src/dkp-csr.c new file mode 100644 index 0000000..2cbbcaf --- /dev/null +++ b/src/dkp-csr.c @@ -0,0 +1,318 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2005-2008 Richard Hughes + * Copyright (C) 2004 Sergey V. Udaltsov + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "sysfs-utils.h" +#include "dkp-debug.h" +#include "dkp-enum.h" +#include "dkp-object.h" +#include "dkp-csr.h" +#include "dkp-history.h" + +#define DKP_CSR_REFRESH_TIMEOUT 30L + +/* Internal CSR registers */ +#define CSR_P6 (buf[0]) +#define CSR_P0 (buf[1]) +#define CSR_P4 (buf[2]) +#define CSR_P5 (buf[3]) +#define CSR_P8 (buf[4]) +#define CSR_P9 (buf[5]) +#define CSR_PB0 (buf[6]) +#define CSR_PB1 (buf[7]) + +struct DkpCsrPrivate +{ + guint poll_timer_id; + gboolean is_dual; + gboolean has_sms; + gboolean has_res; + guint bus_num; + guint dev_num; + gint raw_value; + struct usb_device *device; +}; + +static void dkp_csr_class_init (DkpCsrClass *klass); + +G_DEFINE_TYPE (DkpCsr, dkp_csr, DKP_TYPE_DEVICE) +#define DKP_CSR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DKP_TYPE_CSR, DkpCsrPrivate)) + +static gboolean dkp_csr_refresh (DkpDevice *device); + +/** + * dkp_csr_poll: + **/ +static gboolean +dkp_csr_poll (DkpCsr *csr) +{ + gboolean ret; + DkpDevice *device = DKP_DEVICE (csr); + DkpObject *obj = dkp_device_get_obj (device); + + dkp_debug ("Polling: %s", obj->native_path); + ret = dkp_csr_refresh (device); + if (ret) + dkp_device_emit_changed (device); + return TRUE; +} + +/** + * dkp_csr_find_device: + **/ +static struct usb_device * +dkp_csr_find_device (DkpCsr *csr) +{ + struct usb_bus *curr_bus; + struct usb_device *curr_device; + gchar *dir_name; + gchar *filename; + + dir_name = g_strdup_printf ("%03d", csr->priv->bus_num); + filename = g_strdup_printf ("%03d",csr->priv->dev_num); + dkp_debug ("Looking for: [%s][%s]", dir_name, filename); + + for (curr_bus = usb_busses; curr_bus != NULL; curr_bus = curr_bus->next) { + /* dkp_debug ("Checking bus: [%s]", curr_bus->dirname); */ + if (g_strcasecmp (dir_name, curr_bus->dirname)) + continue; + + for (curr_device = curr_bus->devices; curr_device != NULL; + curr_device = curr_device->next) { + /* dkp_debug ("Checking port: [%s]", curr_device->filename); */ + if (g_strcasecmp (filename, curr_device->filename)) + continue; + dkp_debug ("Matched device: [%s][%s][%04X:%04X]", curr_bus->dirname, + curr_device->filename, + curr_device->descriptor.idVendor, + curr_device->descriptor.idProduct); + goto out; + } + } + /* nothing found */ + curr_device = NULL; +out: + g_free (dir_name); + g_free (filename); + return curr_device; +} + +/** + * dkp_csr_coldplug: + **/ +static gboolean +dkp_csr_coldplug (DkpDevice *device) +{ + DkpCsr *csr = DKP_CSR (device); + DevkitDevice *d; + gboolean ret = TRUE; + const gchar *type; + DkpObject *obj = dkp_device_get_obj (device); + + /* detect what kind of device we are */ + d = dkp_device_get_d (device); + if (d == NULL) + dkp_error ("could not get device"); + + /* get the type */ + type = devkit_device_get_property (d, "ID_BATTERY_TYPE"); + if (strcmp (type, "mouse") == 0) + obj->type = DKP_SOURCE_TYPE_MOUSE; + if (strcmp (type, "keyboard") == 0) + obj->type = DKP_SOURCE_TYPE_KEYBOARD; + + /* nothing known */ + if (obj->type == DKP_SOURCE_TYPE_UNKNOWN) { + dkp_warning ("not a mouse or keyboard"); + ret = FALSE; + goto out; + } + + /* get what USB device we are */ + csr->priv->bus_num = sysfs_get_int (obj->native_path, "busnum"); + csr->priv->dev_num = sysfs_get_int (obj->native_path, "devnum"); + + /* get optional quirk parameters */ + ret = devkit_device_has_property (d, "ID_CSR_HAS_SMS"); + if (ret) + csr->priv->has_sms = devkit_device_get_property_as_boolean (d, "ID_CSR_HAS_SMS"); + ret = devkit_device_has_property (d, "ID_CSR_HAS_RES"); + if (ret) + csr->priv->has_res = devkit_device_get_property_as_boolean (d, "ID_CSR_HAS_RES"); + ret = devkit_device_has_property (d, "ID_CSR_IS_DUAL"); + if (ret) + csr->priv->is_dual = devkit_device_get_property_as_boolean (d, "ID_CSR_IS_DUAL"); + dkp_debug ("has_sms=%i, has_res=%i, is_dual=%i", csr->priv->has_sms, csr->priv->has_res, csr->priv->is_dual); + + obj->vendor = g_strdup (devkit_device_get_property (d, "ID_VENDOR")); + obj->model = g_strdup (devkit_device_get_property (d, "ID_PRODUCT")); + obj->power_supply = FALSE; + obj->battery_is_present = TRUE; + obj->battery_is_rechargeable = TRUE; + obj->battery_state = DKP_SOURCE_STATE_DISCHARGING; + + /* try to get the usb device */ + csr->priv->device = dkp_csr_find_device (csr); + if (csr->priv->device == NULL) { + dkp_warning ("failed to get %p", csr); + ret = FALSE; + goto out; + } + + /* coldplug */ + ret = dkp_csr_refresh (device); +out: + return ret; +} + +/** + * dkp_csr_refresh: + **/ +static gboolean +dkp_csr_refresh (DkpDevice *device) +{ + gboolean ret = TRUE; + GTimeVal time; + DkpCsr *csr = DKP_CSR (device); + DkpObject *obj = dkp_device_get_obj (device); + usb_dev_handle *handle; + char buf[80]; + unsigned int addr; + guint written; + + g_get_current_time (&time); + obj->update_time = time.tv_sec; + + /* Which of subdevices to address */ + addr = csr->priv->is_dual ? 1<<8 : 0; + + /* open USB device */ + handle = usb_open (csr->priv->device); + if (handle == NULL) { + dkp_warning ("could not open device"); + return FALSE; + } + + /* get the charge */ + written = usb_control_msg (handle, 0xc0, 0x09, 0x03|addr, 0x00|addr, buf, 8, DKP_CSR_REFRESH_TIMEOUT); + ret = (written == 8); + if (!ret) { + dkp_warning ("failed to write to device, wrote %i bytes", written); + goto out; + } + + /* are we busy? */ + if (CSR_P0 == 0x3b && CSR_P4 == 0) { + dkp_debug ("receiver busy"); + goto out; + } + + /* process the result */ + csr->priv->raw_value = CSR_P5 & 0x07; + dkp_debug ("charge level: %d", csr->priv->raw_value); + if (csr->priv->raw_value != 0) { + obj->battery_percentage = (100.0 / 7.0) * csr->priv->raw_value; + dkp_debug ("percentage=%f", obj->battery_percentage); + } + +out: + usb_close (handle); + return ret; +} + +/** + * dkp_csr_init: + **/ +static void +dkp_csr_init (DkpCsr *csr) +{ + csr->priv = DKP_CSR_GET_PRIVATE (csr); + + usb_init (); + usb_find_busses (); + usb_find_devices (); + + csr->priv->is_dual = FALSE; + csr->priv->has_sms = FALSE; + csr->priv->has_res = FALSE; + csr->priv->raw_value = -1; + csr->priv->poll_timer_id = g_timeout_add_seconds (DKP_CSR_REFRESH_TIMEOUT, + (GSourceFunc) dkp_csr_poll, csr); +} + +/** + * dkp_csr_finalize: + **/ +static void +dkp_csr_finalize (GObject *object) +{ + DkpCsr *csr; + + g_return_if_fail (object != NULL); + g_return_if_fail (DKP_IS_CSR (object)); + + csr = DKP_CSR (object); + g_return_if_fail (csr->priv != NULL); + + if (csr->priv->poll_timer_id > 0) + g_source_remove (csr->priv->poll_timer_id); + + G_OBJECT_CLASS (dkp_csr_parent_class)->finalize (object); +} + +/** + * dkp_csr_class_init: + **/ +static void +dkp_csr_class_init (DkpCsrClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + DkpDeviceClass *device_class = DKP_DEVICE_CLASS (klass); + + object_class->finalize = dkp_csr_finalize; + device_class->coldplug = dkp_csr_coldplug; + device_class->refresh = dkp_csr_refresh; + + g_type_class_add_private (klass, sizeof (DkpCsrPrivate)); +} + +/** + * dkp_csr_new: + **/ +DkpCsr * +dkp_csr_new (void) +{ + return g_object_new (DKP_TYPE_CSR, NULL); +} + diff --git a/src/dkp-csr.h b/src/dkp-csr.h new file mode 100644 index 0000000..a67101e --- /dev/null +++ b/src/dkp-csr.h @@ -0,0 +1,56 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 David Zeuthen + * Copyright (C) 2008 Richard Hughes + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __DKP_CSR_H__ +#define __DKP_CSR_H__ + +#include +#include "dkp-device.h" + +G_BEGIN_DECLS + +#define DKP_TYPE_CSR (dkp_csr_get_type ()) +#define DKP_CSR(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), DKP_TYPE_CSR, DkpCsr)) +#define DKP_CSR_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), DKP_TYPE_CSR, DkpCsrClass)) +#define DKP_IS_CSR(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), DKP_TYPE_CSR)) +#define DKP_IS_CSR_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), DKP_TYPE_CSR)) +#define DKP_CSR_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), DKP_TYPE_CSR, DkpCsrClass)) + +typedef struct DkpCsrPrivate DkpCsrPrivate; + +typedef struct +{ + DkpDevice parent; + DkpCsrPrivate *priv; +} DkpCsr; + +typedef struct +{ + DkpDeviceClass parent_class; +} DkpCsrClass; + +GType dkp_csr_get_type (void); +DkpCsr *dkp_csr_new (void); + +G_END_DECLS + +#endif /* __DKP_CSR_H__ */ + diff --git a/src/dkp-daemon.c b/src/dkp-daemon.c index 114b3ac..c23ef92 100644 --- a/src/dkp-daemon.c +++ b/src/dkp-daemon.c @@ -35,6 +35,7 @@ #include "dkp-daemon.h" #include "dkp-device.h" #include "dkp-supply.h" +#include "dkp-csr.h" #include "dkp-device-list.h" #include "dkp-daemon-glue.h" @@ -433,8 +434,7 @@ gpk_daemon_device_get (DevkitDevice *d) /* see if this is a CSR mouse or keyboard */ type = devkit_device_get_property (d, "ID_BATTERY_TYPE"); if (type != NULL) - //device = DKP_DEVICE (dkp_csr_new ()); - dkp_warning ("should add %s !!", devkit_device_get_native_path (d)); + device = DKP_DEVICE (dkp_csr_new ()); } else { native_path = devkit_device_get_native_path (d); dkp_warning ("native path %s (%s) ignoring", native_path, subsys); diff --git a/src/dkp-device.c b/src/dkp-device.c index 809aa48..d0fbca7 100644 --- a/src/dkp-device.c +++ b/src/dkp-device.c @@ -229,21 +229,35 @@ dkp_device_removed (DkpDevice *device) /** * dkp_device_get_on_battery: + * + * Note: Only implement for system devices, i.e. ones supplying the system **/ gboolean dkp_device_get_on_battery (DkpDevice *device, gboolean *on_battery) { DkpDeviceClass *klass = DKP_DEVICE_GET_CLASS (device); + + /* no support */ + if (klass->get_stats == NULL) + return FALSE; + return klass->get_on_battery (device, on_battery); } /** * dkp_device_get_low_battery: + * + * Note: Only implement for system devices, i.e. ones supplying the system **/ gboolean dkp_device_get_low_battery (DkpDevice *device, gboolean *low_battery) { DkpDeviceClass *klass = DKP_DEVICE_GET_CLASS (device); + + /* no support */ + if (klass->get_low_battery == NULL) + return FALSE; + return klass->get_low_battery (device, low_battery); } @@ -430,42 +444,36 @@ dkp_device_emit_changed (DkpDevice *device) g_signal_emit (device, signals[CHANGED_SIGNAL], 0); } -/** - * dkp_device_compute_object_path_from_basename: - **/ -static char * -dkp_device_compute_object_path_from_basename (const char *native_path_basename) -{ - gchar *basename; - gchar *object_path; - unsigned int n; - - /* TODO: need to be more thorough with making proper object - * names that won't make D-Bus crash. This is just to cope - * with dm-0... - */ - basename = g_path_get_basename (native_path_basename); - for (n = 0; basename[n] != '\0'; n++) - if (basename[n] == '-') - basename[n] = '_'; - object_path = g_build_filename ("/devices/", basename, NULL); - g_free (basename); - - return object_path; -} - /** * dkp_device_compute_object_path: **/ static gchar * -dkp_device_compute_object_path (const char *native_path) +dkp_device_compute_object_path (DkpDevice *device) { gchar *basename; + gchar *id; gchar *object_path; + const gchar *native_path; + const gchar *type; + guint i; + type = dkp_source_type_to_text (device->priv->obj->type); + native_path = device->priv->obj->native_path; basename = g_path_get_basename (native_path); - object_path = dkp_device_compute_object_path_from_basename (basename); + id = g_strjoin ("_", type, basename, NULL); + + /* make DBUS valid path */ + for (i=0; id[i] != '\0'; i++) { + if (id[i] == '-') + id[i] = '_'; + if (id[i] == '.') + id[i] = 'x'; + } + object_path = g_build_filename ("/devices", id, NULL); + g_free (basename); + g_free (id); + return object_path; } @@ -477,7 +485,8 @@ dkp_device_register_device (DkpDevice *device) { gboolean ret = TRUE; - device->priv->object_path = dkp_device_compute_object_path (device->priv->obj->native_path); + device->priv->object_path = dkp_device_compute_object_path (device); + dkp_warning ("object path = %s", device->priv->object_path); dbus_g_connection_register_g_object (device->priv->system_bus_connection, device->priv->object_path, G_OBJECT (device)); device->priv->system_bus_proxy = dbus_g_proxy_new_for_name (device->priv->system_bus_connection,