1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2005-2008 Richard Hughes <richard@hughsie.com>
4
* Copyright (C) 2004 Sergey V. Udaltsov <svu@gnome.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30
#include <glib/gstdio.h>
31
#include <glib/gi18n-lib.h>
32
#include <glib-object.h>
33
#include <devkit-gobject/devkit-gobject.h>
36
#include "sysfs-utils.h"
37
#include "egg-debug.h"
40
#include "dkp-device-csr.h"
42
#define DKP_DEVICE_CSR_REFRESH_TIMEOUT 30L
44
/* Internal CSR registers */
45
#define CSR_P6 (buf[0])
46
#define CSR_P0 (buf[1])
47
#define CSR_P4 (buf[2])
48
#define CSR_P5 (buf[3])
49
#define CSR_P8 (buf[4])
50
#define CSR_P9 (buf[5])
51
#define CSR_PB0 (buf[6])
52
#define CSR_PB1 (buf[7])
54
struct DkpDeviceCsrPrivate
61
struct usb_device *device;
64
static void dkp_device_csr_class_init (DkpDeviceCsrClass *klass);
66
G_DEFINE_TYPE (DkpDeviceCsr, dkp_device_csr, DKP_TYPE_DEVICE)
67
#define DKP_DEVICE_CSR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DKP_TYPE_CSR, DkpDeviceCsrPrivate))
69
static gboolean dkp_device_csr_refresh (DkpDevice *device);
72
* dkp_device_csr_poll_cb:
75
dkp_device_csr_poll_cb (DkpDeviceCsr *csr)
78
DkpDevice *device = DKP_DEVICE (csr);
80
egg_debug ("Polling: %s", dkp_device_get_object_path (device));
81
ret = dkp_device_csr_refresh (device);
83
dkp_device_emit_changed (device);
88
* dkp_device_csr_find_device:
90
static struct usb_device *
91
dkp_device_csr_find_device (DkpDeviceCsr *csr)
93
struct usb_bus *curr_bus;
94
struct usb_device *curr_device;
98
dir_name = g_strdup_printf ("%03d", csr->priv->bus_num);
99
filename = g_strdup_printf ("%03d",csr->priv->dev_num);
100
egg_debug ("Looking for: [%s][%s]", dir_name, filename);
102
for (curr_bus = usb_busses; curr_bus != NULL; curr_bus = curr_bus->next) {
103
/* egg_debug ("Checking bus: [%s]", curr_bus->dirname); */
104
if (g_strcasecmp (dir_name, curr_bus->dirname))
107
for (curr_device = curr_bus->devices; curr_device != NULL;
108
curr_device = curr_device->next) {
109
/* egg_debug ("Checking port: [%s]", curr_device->filename); */
110
if (g_strcasecmp (filename, curr_device->filename))
112
egg_debug ("Matched device: [%s][%s][%04X:%04X]", curr_bus->dirname,
113
curr_device->filename,
114
curr_device->descriptor.idVendor,
115
curr_device->descriptor.idProduct);
128
* dkp_device_csr_coldplug:
130
* Return %TRUE on success, %FALSE if we failed to get data and should be removed
133
dkp_device_csr_coldplug (DkpDevice *device)
135
DkpDeviceCsr *csr = DKP_DEVICE_CSR (device);
137
gboolean ret = FALSE;
139
const gchar *native_path;
141
const gchar *product;
143
/* detect what kind of device we are */
144
d = dkp_device_get_d (device);
146
egg_error ("could not get device");
149
type = devkit_device_get_property (d, "DKP_BATTERY_TYPE");
154
if (g_strcmp0 (type, "mouse") == 0)
155
g_object_set (device, "type", DKP_DEVICE_TYPE_MOUSE, NULL);
156
else if (g_strcmp0 (type, "keyboard") == 0)
157
g_object_set (device, "type", DKP_DEVICE_TYPE_KEYBOARD, NULL);
159
egg_debug ("not a recognised csr device");
163
/* get what USB device we are */
164
native_path = devkit_device_get_native_path (d);
165
csr->priv->bus_num = sysfs_get_int (native_path, "busnum");
166
csr->priv->dev_num = sysfs_get_int (native_path, "devnum");
168
/* get correct bus numbers? */
169
if (csr->priv->bus_num == 0 || csr->priv->dev_num == 0) {
170
egg_warning ("unable to get bus or device numbers");
174
/* try to get the usb device */
175
csr->priv->device = dkp_device_csr_find_device (csr);
176
if (csr->priv->device == NULL) {
177
egg_debug ("failed to get device %p", csr);
181
/* get optional quirk parameters */
182
ret = devkit_device_has_property (d, "DKP_CSR_DUAL");
184
csr->priv->is_dual = devkit_device_get_property_as_boolean (d, "DKP_CSR_DUAL");
185
egg_debug ("is_dual=%i", csr->priv->is_dual);
187
/* prefer DKP names */
188
vendor = devkit_device_get_property (d, "DKP_VENDOR");
190
vendor = devkit_device_get_property (d, "ID_VENDOR");
191
product = devkit_device_get_property (d, "DKP_PRODUCT");
193
product = devkit_device_get_property (d, "ID_PRODUCT");
195
/* hardcode some values */
196
g_object_set (device,
199
"power-supply", FALSE,
201
"is-rechargeable", TRUE,
202
"state", DKP_DEVICE_STATE_DISCHARGING,
207
ret = dkp_device_csr_refresh (device);
212
csr->priv->poll_timer_id = g_timeout_add_seconds (DKP_DEVICE_CSR_REFRESH_TIMEOUT,
213
(GSourceFunc) dkp_device_csr_poll_cb, csr);
220
* dkp_device_csr_refresh:
222
* Return %TRUE on success, %FALSE if we failed to refresh or no data
225
dkp_device_csr_refresh (DkpDevice *device)
227
gboolean ret = FALSE;
229
DkpDeviceCsr *csr = DKP_DEVICE_CSR (device);
230
usb_dev_handle *handle = NULL;
236
g_get_current_time (&time);
237
g_object_set (device, "update-time", (guint64) time.tv_sec, NULL);
239
/* For dual receivers C502, C504 and C505, the mouse is the
240
* second device and uses an addr of 1 in the value and index
241
* fields' high byte */
242
addr = csr->priv->is_dual ? 1<<8 : 0;
244
if (csr->priv->device == NULL) {
245
egg_warning ("no device!");
249
/* open USB device */
250
handle = usb_open (csr->priv->device);
251
if (handle == NULL) {
252
egg_warning ("could not open device");
257
written = usb_control_msg (handle, 0xc0, 0x09, 0x03|addr, 0x00|addr, buf, 8, DKP_DEVICE_CSR_REFRESH_TIMEOUT);
258
ret = (written == 8);
260
egg_warning ("failed to write to device, wrote %i bytes", written);
264
/* is a C504 receiver busy? */
265
if (CSR_P0 == 0x3b && CSR_P4 == 0) {
266
egg_debug ("receiver busy");
270
/* get battery status */
271
csr->priv->raw_value = CSR_P5 & 0x07;
272
egg_debug ("charge level: %d", csr->priv->raw_value);
273
if (csr->priv->raw_value != 0) {
274
percentage = (100.0 / 7.0) * csr->priv->raw_value;
275
g_object_set (device, "percentage", percentage, NULL);
276
egg_debug ("percentage=%f", percentage);
286
* dkp_device_csr_init:
289
dkp_device_csr_init (DkpDeviceCsr *csr)
291
csr->priv = DKP_DEVICE_CSR_GET_PRIVATE (csr);
297
csr->priv->is_dual = FALSE;
298
csr->priv->raw_value = -1;
299
csr->priv->poll_timer_id = 0;
303
* dkp_device_csr_finalize:
306
dkp_device_csr_finalize (GObject *object)
310
g_return_if_fail (object != NULL);
311
g_return_if_fail (DKP_IS_CSR (object));
313
csr = DKP_DEVICE_CSR (object);
314
g_return_if_fail (csr->priv != NULL);
316
if (csr->priv->poll_timer_id > 0)
317
g_source_remove (csr->priv->poll_timer_id);
319
G_OBJECT_CLASS (dkp_device_csr_parent_class)->finalize (object);
323
* dkp_device_csr_class_init:
326
dkp_device_csr_class_init (DkpDeviceCsrClass *klass)
328
GObjectClass *object_class = G_OBJECT_CLASS (klass);
329
DkpDeviceClass *device_class = DKP_DEVICE_CLASS (klass);
331
object_class->finalize = dkp_device_csr_finalize;
332
device_class->coldplug = dkp_device_csr_coldplug;
333
device_class->refresh = dkp_device_csr_refresh;
335
g_type_class_add_private (klass, sizeof (DkpDeviceCsrPrivate));
339
* dkp_device_csr_new:
342
dkp_device_csr_new (void)
344
return g_object_new (DKP_TYPE_CSR, NULL);