~ubuntu-branches/ubuntu/maverick/devicekit-power/maverick

« back to all changes in this revision

Viewing changes to src/dkp-device-csr.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-06 19:12:49 UTC
  • mfrom: (1.1.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20090706191249-hr0a9c2ti5u0b5lc
Tags: 009-1
* New upstream release.   
* debian/control: Add myself to uploaders (discussed with Michael).
* debian/devicekit-power.install: Upstream installs udev rules into
  /lib/udev/rules.d now, update accordingly.
* Add 0001-Add-a-notify-flag-to-set_lid_is_closed.patch: Properly fix the
  silencing of the coldplug lid event, so that the first real lid event
  actually works. Thanks to Loïc Minier! (fd.o #22574)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2005-2008 Richard Hughes <richard@hughsie.com>
 
4
 * Copyright (C) 2004 Sergey V. Udaltsov <svu@gnome.org>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 *
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#  include "config.h"
 
24
#endif
 
25
 
 
26
#include <string.h>
 
27
#include <math.h>
 
28
 
 
29
#include <glib.h>
 
30
#include <glib/gstdio.h>
 
31
#include <glib/gi18n-lib.h>
 
32
#include <glib-object.h>
 
33
#include <devkit-gobject/devkit-gobject.h>
 
34
#include <usb.h>
 
35
 
 
36
#include "sysfs-utils.h"
 
37
#include "egg-debug.h"
 
38
 
 
39
#include "dkp-enum.h"
 
40
#include "dkp-device-csr.h"
 
41
 
 
42
#define DKP_DEVICE_CSR_REFRESH_TIMEOUT          30L
 
43
 
 
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])
 
53
 
 
54
struct DkpDeviceCsrPrivate
 
55
{
 
56
        guint                    poll_timer_id;
 
57
        gboolean                 is_dual;
 
58
        guint                    bus_num;
 
59
        guint                    dev_num;
 
60
        gint                     raw_value;
 
61
        struct usb_device       *device;
 
62
};
 
63
 
 
64
static void     dkp_device_csr_class_init       (DkpDeviceCsrClass      *klass);
 
65
 
 
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))
 
68
 
 
69
static gboolean          dkp_device_csr_refresh         (DkpDevice *device);
 
70
 
 
71
/**
 
72
 * dkp_device_csr_poll_cb:
 
73
 **/
 
74
static gboolean
 
75
dkp_device_csr_poll_cb (DkpDeviceCsr *csr)
 
76
{
 
77
        gboolean ret;
 
78
        DkpDevice *device = DKP_DEVICE (csr);
 
79
 
 
80
        egg_debug ("Polling: %s", dkp_device_get_object_path (device));
 
81
        ret = dkp_device_csr_refresh (device);
 
82
        if (ret)
 
83
                dkp_device_emit_changed (device);
 
84
        return TRUE;
 
85
}
 
86
 
 
87
/**
 
88
 * dkp_device_csr_find_device:
 
89
 **/
 
90
static struct usb_device *
 
91
dkp_device_csr_find_device (DkpDeviceCsr *csr)
 
92
{
 
93
        struct usb_bus *curr_bus;
 
94
        struct usb_device *curr_device;
 
95
        gchar *dir_name;
 
96
        gchar *filename;
 
97
 
 
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);
 
101
 
 
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))
 
105
                        continue;
 
106
 
 
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))
 
111
                                continue;
 
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);
 
116
                        goto out;
 
117
                }
 
118
        }
 
119
        /* nothing found */
 
120
        curr_device = NULL;
 
121
out:
 
122
        g_free (dir_name);
 
123
        g_free (filename);
 
124
        return curr_device;
 
125
}
 
126
 
 
127
/**
 
128
 * dkp_device_csr_coldplug:
 
129
 *
 
130
 * Return %TRUE on success, %FALSE if we failed to get data and should be removed
 
131
 **/
 
132
static gboolean
 
133
dkp_device_csr_coldplug (DkpDevice *device)
 
134
{
 
135
        DkpDeviceCsr *csr = DKP_DEVICE_CSR (device);
 
136
        DevkitDevice *d;
 
137
        gboolean ret = FALSE;
 
138
        const gchar *type;
 
139
        const gchar *native_path;
 
140
        const gchar *vendor;
 
141
        const gchar *product;
 
142
 
 
143
        /* detect what kind of device we are */
 
144
        d = dkp_device_get_d (device);
 
145
        if (d == NULL)
 
146
                egg_error ("could not get device");
 
147
 
 
148
        /* get the type */
 
149
        type = devkit_device_get_property (d, "DKP_BATTERY_TYPE");
 
150
        if (type == NULL)
 
151
                goto out;
 
152
 
 
153
        /* which one? */
 
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);
 
158
        else {
 
159
                egg_debug ("not a recognised csr device");
 
160
                goto out;
 
161
        }
 
162
 
 
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");
 
167
 
 
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");
 
171
                goto out;
 
172
        }
 
173
 
 
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);
 
178
                goto out;
 
179
        }
 
180
 
 
181
        /* get optional quirk parameters */
 
182
        ret = devkit_device_has_property (d, "DKP_CSR_DUAL");
 
183
        if (ret)
 
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);
 
186
 
 
187
        /* prefer DKP names */
 
188
        vendor = devkit_device_get_property (d, "DKP_VENDOR");
 
189
        if (vendor == NULL)
 
190
                vendor = devkit_device_get_property (d, "ID_VENDOR");
 
191
        product = devkit_device_get_property (d, "DKP_PRODUCT");
 
192
        if (product == NULL)
 
193
                product = devkit_device_get_property (d, "ID_PRODUCT");
 
194
 
 
195
        /* hardcode some values */
 
196
        g_object_set (device,
 
197
                      "vendor", vendor,
 
198
                      "model", product,
 
199
                      "power-supply", FALSE,
 
200
                      "is-present", TRUE,
 
201
                      "is-rechargeable", TRUE,
 
202
                      "state", DKP_DEVICE_STATE_DISCHARGING,
 
203
                      "has-history", TRUE,
 
204
                      NULL);
 
205
 
 
206
        /* coldplug */
 
207
        ret = dkp_device_csr_refresh (device);
 
208
        if (!ret)
 
209
                goto out;
 
210
 
 
211
        /* set up a poll */
 
212
        csr->priv->poll_timer_id = g_timeout_add_seconds (DKP_DEVICE_CSR_REFRESH_TIMEOUT,
 
213
                                                          (GSourceFunc) dkp_device_csr_poll_cb, csr);
 
214
 
 
215
out:
 
216
        return ret;
 
217
}
 
218
 
 
219
/**
 
220
 * dkp_device_csr_refresh:
 
221
 *
 
222
 * Return %TRUE on success, %FALSE if we failed to refresh or no data
 
223
 **/
 
224
static gboolean
 
225
dkp_device_csr_refresh (DkpDevice *device)
 
226
{
 
227
        gboolean ret = FALSE;
 
228
        GTimeVal time;
 
229
        DkpDeviceCsr *csr = DKP_DEVICE_CSR (device);
 
230
        usb_dev_handle *handle = NULL;
 
231
        char buf[80];
 
232
        unsigned int addr;
 
233
        gdouble percentage;
 
234
        guint written;
 
235
 
 
236
        g_get_current_time (&time);
 
237
        g_object_set (device, "update-time", (guint64) time.tv_sec, NULL);
 
238
 
 
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;
 
243
 
 
244
        if (csr->priv->device == NULL) {
 
245
                egg_warning ("no device!");
 
246
                goto out;
 
247
        }
 
248
 
 
249
        /* open USB device */
 
250
        handle = usb_open (csr->priv->device);
 
251
        if (handle == NULL) {
 
252
                egg_warning ("could not open device");
 
253
                goto out;
 
254
        }
 
255
 
 
256
        /* get the charge */
 
257
        written = usb_control_msg (handle, 0xc0, 0x09, 0x03|addr, 0x00|addr, buf, 8, DKP_DEVICE_CSR_REFRESH_TIMEOUT);
 
258
        ret = (written == 8);
 
259
        if (!ret) {
 
260
                egg_warning ("failed to write to device, wrote %i bytes", written);
 
261
                goto out;
 
262
        }
 
263
 
 
264
        /* is a C504 receiver busy? */
 
265
        if (CSR_P0 == 0x3b && CSR_P4 == 0) {
 
266
                egg_debug ("receiver busy");
 
267
                goto out;
 
268
        }
 
269
 
 
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);
 
277
        }
 
278
 
 
279
out:
 
280
        if (handle != NULL)
 
281
                usb_close (handle);
 
282
        return ret;
 
283
}
 
284
 
 
285
/**
 
286
 * dkp_device_csr_init:
 
287
 **/
 
288
static void
 
289
dkp_device_csr_init (DkpDeviceCsr *csr)
 
290
{
 
291
        csr->priv = DKP_DEVICE_CSR_GET_PRIVATE (csr);
 
292
 
 
293
        usb_init ();
 
294
        usb_find_busses ();
 
295
        usb_find_devices ();
 
296
 
 
297
        csr->priv->is_dual = FALSE;
 
298
        csr->priv->raw_value = -1;
 
299
        csr->priv->poll_timer_id = 0;
 
300
}
 
301
 
 
302
/**
 
303
 * dkp_device_csr_finalize:
 
304
 **/
 
305
static void
 
306
dkp_device_csr_finalize (GObject *object)
 
307
{
 
308
        DkpDeviceCsr *csr;
 
309
 
 
310
        g_return_if_fail (object != NULL);
 
311
        g_return_if_fail (DKP_IS_CSR (object));
 
312
 
 
313
        csr = DKP_DEVICE_CSR (object);
 
314
        g_return_if_fail (csr->priv != NULL);
 
315
 
 
316
        if (csr->priv->poll_timer_id > 0)
 
317
                g_source_remove (csr->priv->poll_timer_id);
 
318
 
 
319
        G_OBJECT_CLASS (dkp_device_csr_parent_class)->finalize (object);
 
320
}
 
321
 
 
322
/**
 
323
 * dkp_device_csr_class_init:
 
324
 **/
 
325
static void
 
326
dkp_device_csr_class_init (DkpDeviceCsrClass *klass)
 
327
{
 
328
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
329
        DkpDeviceClass *device_class = DKP_DEVICE_CLASS (klass);
 
330
 
 
331
        object_class->finalize = dkp_device_csr_finalize;
 
332
        device_class->coldplug = dkp_device_csr_coldplug;
 
333
        device_class->refresh = dkp_device_csr_refresh;
 
334
 
 
335
        g_type_class_add_private (klass, sizeof (DkpDeviceCsrPrivate));
 
336
}
 
337
 
 
338
/**
 
339
 * dkp_device_csr_new:
 
340
 **/
 
341
DkpDeviceCsr *
 
342
dkp_device_csr_new (void)
 
343
{
 
344
        return g_object_new (DKP_TYPE_CSR, NULL);
 
345
}
 
346