1
0
forked from drath/libratbag
Files
libratbag-plus/src/driver-hidpp10.c
Peter Hutterer 2fbf156403 hidpp10: use 0x00 to talk to all devices, skip receiver handling
The kernel overwrites the device ID with the correct one for our device, so we
can just use 0x00 for all devices and skip the search for the unifying
receiver hidraw device.

Since we're not talking to the receiver anymore, we can/have to drop the
pairing information queries and the related info. Since we weren't using this
anyway it's not a big loss.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2015-08-27 11:42:48 +10:00

265 lines
6.8 KiB
C

/*
* Copyright 2013-2015 Benjamin Tissoires <benjamin.tissoires@gmail.com>
* Copyright 2013-2015 Red Hat, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*
* Based on the HID++ 1.0 documentation provided by Nestor Lopez Casado at:
* https://drive.google.com/folderview?id=0BxbRzx7vEV7eWmgwazJ3NUFfQ28&usp=sharing
*/
/*
* for this driver to work, you need a kernel >= v3.19 or one which contains
* 925f0f3ed24f98b40c28627e74ff3e7f9d1e28bc ("HID: logitech-dj: allow transfer
* of HID++ reports from/to the correct dj device")
*/
#include "config.h"
#define USB_VENDOR_ID_LOGITECH 0x046d
#include <linux/types.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "hidpp10.h"
#include "libratbag-private.h"
#include "libratbag-hidraw.h"
struct hidpp10drv_data {
struct hidpp10_device *dev;
};
static void
hidpp10drv_read_button(struct ratbag_button *button)
{
enum ratbag_button_type type = RATBAG_BUTTON_TYPE_UNKNOWN;
/* FIXME: this is valid for the G500s only */
switch (button->index) {
case 0: type = RATBAG_BUTTON_TYPE_LEFT; break;
case 1: type = RATBAG_BUTTON_TYPE_MIDDLE; break;
case 2: type = RATBAG_BUTTON_TYPE_RIGHT; break;
case 3: type = RATBAG_BUTTON_TYPE_THUMB; break;
case 4: type = RATBAG_BUTTON_TYPE_THUMB2; break;
case 5: type = RATBAG_BUTTON_TYPE_THUMB3; break;
case 6: type = RATBAG_BUTTON_TYPE_WHEEL_LEFT; break;
case 7: type = RATBAG_BUTTON_TYPE_WHEEL_RIGHT; break;
case 8: type = RATBAG_BUTTON_TYPE_RESOLUTION_UP; break;
case 9: type = RATBAG_BUTTON_TYPE_RESOLUTION_DOWN; break;
case 10:
case 11:
case 12: /* these don't actually exist on the device */
type = RATBAG_BUTTON_TYPE_UNKNOWN; break;
default:
break;
}
button->type = type;
}
static int
hidpp10drv_write_button(struct ratbag_button *button,
const struct ratbag_button_action *action)
{
return -1;
}
static int
hidpp10drv_has_capability(const struct ratbag_device *device, enum ratbag_capability cap)
{
switch (cap) {
case RATBAG_CAP_NONE:
abort();
break;
case RATBAG_CAP_SWITCHABLE_PROFILE:
case RATBAG_CAP_SWITCHABLE_RESOLUTION:
case RATBAG_CAP_BUTTON_KEY:
case RATBAG_CAP_BUTTON_MACROS:
return (device->num_profiles > 1);
}
return 0;
}
static int
hidpp10drv_current_profile(struct ratbag_device *device)
{
return -1;
}
static int
hidpp10drv_set_current_profile(struct ratbag_device *device, unsigned int index)
{
return -1;
}
static void
hidpp10drv_read_profile(struct ratbag_profile *profile, unsigned int index)
{
struct ratbag_device *device = profile->device;
struct hidpp10drv_data *drv_data;
struct hidpp10_device *hidpp10;
uint16_t xres, yres, refresh;
int rc;
drv_data = ratbag_get_drv_data(device);
hidpp10 = drv_data->dev;
rc = hidpp10_get_current_resolution(hidpp10, &xres, &yres);
if (rc == 0)
profile->current_dpi = xres;
rc = hidpp10_get_usb_refresh_rate(hidpp10, &refresh);
if (rc == 0)
profile->report_rate = refresh;
}
static int
hidpp10drv_write_profile(struct ratbag_profile *profile)
{
return -1;
}
static int
hidpp10drv_fill_from_profile(struct ratbag_device *device, struct hidpp10_device *dev)
{
int rc;
struct hidpp10_profile profile;
/* We don't know the HID++1.0 requests to query for buttons, etc.
* and the get_current_profile request is garbage. So get profile 1
* and fill the device information in from that.
*/
rc = hidpp10_get_profile(dev, 0, &profile);
if (rc)
return rc;
ratbag_device_init_profiles(device, HIDPP10_NUM_PROFILES, profile.num_buttons);
return 0;
}
static int
hidpp10drv_probe(struct ratbag_device *device, const struct ratbag_id id)
{
int rc;
struct hidpp10drv_data *drv_data;
struct hidpp10_device *dev;
rc = ratbag_open_hidraw(device);
if (rc) {
log_error(device->ratbag,
"Can't open corresponding hidraw node: '%s' (%d)\n",
strerror(-rc),
rc);
rc = -ENODEV;
goto err;
}
drv_data = zalloc(sizeof(*drv_data));
if (!drv_data) {
rc = -ENODEV;
goto err;
}
/* We can treat all devices as wired devices here. If we talk to the
* correct hidraw device the kernel adjusts the device index for us,
* so even for unifying receiver devices we can just 0x00 as device
* index.
*/
dev = hidpp10_device_new_from_idx(device, WIRED_DEVICE_IDX);
if (!dev) {
log_error(device->ratbag,
"Failed to get HID++1.0 device for %s\n",
device->name);
goto err;
}
drv_data->dev = dev;
ratbag_set_drv_data(device, drv_data);
if (hidpp10drv_fill_from_profile(device, dev)) {
/* Fall back to something that every mouse has */
ratbag_device_init_profiles(device, 1, 3);
}
return 0;
err:
free(drv_data);
ratbag_set_drv_data(device, NULL);
hidpp10_device_destroy(dev);
return rc;
}
static void
hidpp10drv_remove(struct ratbag_device *device)
{
struct hidpp10drv_data *drv_data;
struct hidpp10_device *dev;
drv_data = ratbag_get_drv_data(device);
dev = drv_data->dev;
hidpp10_device_destroy(dev);
free(drv_data);
}
#define LOGITECH_DEVICE(_bus, _pid) \
{ .bustype = (_bus), \
.vendor = USB_VENDOR_ID_LOGITECH, \
.product = (_pid), \
.version = VERSION_ANY },
static const struct ratbag_id hidpp10drv_table[] = {
/* M705 */
{ .id = LOGITECH_DEVICE(BUS_USB, 0x101b) },
/* M570 */
{ .id = LOGITECH_DEVICE(BUS_USB, 0x1028) },
/* G500s */
{ .id = LOGITECH_DEVICE(BUS_USB, 0xc24e) },
{ },
};
struct ratbag_driver hidpp10_driver = {
.name = "Logitech HID++1.0",
.table_ids = hidpp10drv_table,
.probe = hidpp10drv_probe,
.remove = hidpp10drv_remove,
.read_profile = hidpp10drv_read_profile,
.write_profile = hidpp10drv_write_profile,
.get_active_profile = hidpp10drv_current_profile,
.set_active_profile = hidpp10drv_set_current_profile,
.has_capability = hidpp10drv_has_capability,
.read_button = hidpp10drv_read_button,
.write_button = hidpp10drv_write_button,
};