blob: b07e7f96a3580e0168bce0a89e86b8091c94cd7a [file] [log] [blame]
Stefan Achatz5772f632011-01-30 13:38:23 +01001/*
2 * Roccat common functions for device specific drivers
3 *
4 * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net>
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 */
13
Stefan Achatz1edd5b42011-06-01 15:54:17 +020014#include <linux/hid.h>
Stefan Achatz5772f632011-01-30 13:38:23 +010015#include <linux/slab.h>
Paul Gortmaker8f86a2c2011-07-03 13:39:48 -040016#include <linux/module.h>
Stefan Achatz5772f632011-01-30 13:38:23 +010017#include "hid-roccat-common.h"
18
Stefan Achatz1edd5b42011-06-01 15:54:17 +020019static inline uint16_t roccat_common_feature_report(uint8_t report_id)
20{
21 return 0x300 | report_id;
22}
23
24int roccat_common_receive(struct usb_device *usb_dev, uint report_id,
Stefan Achatz5772f632011-01-30 13:38:23 +010025 void *data, uint size)
26{
27 char *buf;
28 int len;
29
30 buf = kmalloc(size, GFP_KERNEL);
31 if (buf == NULL)
32 return -ENOMEM;
33
34 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
Stefan Achatz1edd5b42011-06-01 15:54:17 +020035 HID_REQ_GET_REPORT,
Stefan Achatz5772f632011-01-30 13:38:23 +010036 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Stefan Achatz1edd5b42011-06-01 15:54:17 +020037 roccat_common_feature_report(report_id),
38 0, buf, size, USB_CTRL_SET_TIMEOUT);
Stefan Achatz5772f632011-01-30 13:38:23 +010039
40 memcpy(data, buf, size);
41 kfree(buf);
42 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
43}
44EXPORT_SYMBOL_GPL(roccat_common_receive);
45
Stefan Achatz1edd5b42011-06-01 15:54:17 +020046int roccat_common_send(struct usb_device *usb_dev, uint report_id,
Stefan Achatz5772f632011-01-30 13:38:23 +010047 void const *data, uint size)
48{
49 char *buf;
50 int len;
51
52 buf = kmalloc(size, GFP_KERNEL);
53 if (buf == NULL)
54 return -ENOMEM;
55
56 memcpy(buf, data, size);
57
58 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
Stefan Achatz1edd5b42011-06-01 15:54:17 +020059 HID_REQ_SET_REPORT,
Stefan Achatz5772f632011-01-30 13:38:23 +010060 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Stefan Achatz1edd5b42011-06-01 15:54:17 +020061 roccat_common_feature_report(report_id),
62 0, buf, size, USB_CTRL_SET_TIMEOUT);
Stefan Achatz5772f632011-01-30 13:38:23 +010063
64 kfree(buf);
65 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
66}
67EXPORT_SYMBOL_GPL(roccat_common_send);
68
69MODULE_AUTHOR("Stefan Achatz");
70MODULE_DESCRIPTION("USB Roccat common driver");
71MODULE_LICENSE("GPL v2");