Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 1 | /* |
| 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 Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 14 | #include <linux/hid.h> |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 15 | #include <linux/slab.h> |
Paul Gortmaker | 8f86a2c | 2011-07-03 13:39:48 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 17 | #include "hid-roccat-common.h" |
| 18 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 19 | static inline uint16_t roccat_common2_feature_report(uint8_t report_id) |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 20 | { |
| 21 | return 0x300 | report_id; |
| 22 | } |
| 23 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 24 | int roccat_common2_receive(struct usb_device *usb_dev, uint report_id, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 25 | 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 Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 35 | HID_REQ_GET_REPORT, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 36 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 37 | roccat_common2_feature_report(report_id), |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 38 | 0, buf, size, USB_CTRL_SET_TIMEOUT); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 39 | |
| 40 | memcpy(data, buf, size); |
| 41 | kfree(buf); |
| 42 | return ((len < 0) ? len : ((len != size) ? -EIO : 0)); |
| 43 | } |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 44 | EXPORT_SYMBOL_GPL(roccat_common2_receive); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 45 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 46 | int roccat_common2_send(struct usb_device *usb_dev, uint report_id, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 47 | void const *data, uint size) |
| 48 | { |
| 49 | char *buf; |
| 50 | int len; |
| 51 | |
Thomas Meyer | 4c33a88 | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 52 | buf = kmemdup(data, size, GFP_KERNEL); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 53 | if (buf == NULL) |
| 54 | return -ENOMEM; |
| 55 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 56 | len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 57 | HID_REQ_SET_REPORT, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 58 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 59 | roccat_common2_feature_report(report_id), |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 60 | 0, buf, size, USB_CTRL_SET_TIMEOUT); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 61 | |
| 62 | kfree(buf); |
| 63 | return ((len < 0) ? len : ((len != size) ? -EIO : 0)); |
| 64 | } |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 65 | EXPORT_SYMBOL_GPL(roccat_common2_send); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 66 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 67 | enum roccat_common2_control_states { |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 68 | ROCCAT_COMMON_CONTROL_STATUS_OVERLOAD = 0, |
| 69 | ROCCAT_COMMON_CONTROL_STATUS_OK = 1, |
| 70 | ROCCAT_COMMON_CONTROL_STATUS_INVALID = 2, |
| 71 | ROCCAT_COMMON_CONTROL_STATUS_WAIT = 3, |
| 72 | }; |
| 73 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 74 | static int roccat_common2_receive_control_status(struct usb_device *usb_dev) |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 75 | { |
| 76 | int retval; |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 77 | struct roccat_common2_control control; |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 78 | |
| 79 | do { |
| 80 | msleep(50); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 81 | retval = roccat_common2_receive(usb_dev, |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 82 | ROCCAT_COMMON_COMMAND_CONTROL, |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 83 | &control, sizeof(struct roccat_common2_control)); |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 84 | |
| 85 | if (retval) |
| 86 | return retval; |
| 87 | |
| 88 | switch (control.value) { |
| 89 | case ROCCAT_COMMON_CONTROL_STATUS_OK: |
| 90 | return 0; |
| 91 | case ROCCAT_COMMON_CONTROL_STATUS_WAIT: |
| 92 | msleep(500); |
| 93 | continue; |
| 94 | case ROCCAT_COMMON_CONTROL_STATUS_INVALID: |
| 95 | |
| 96 | case ROCCAT_COMMON_CONTROL_STATUS_OVERLOAD: |
| 97 | /* seems to be critical - replug necessary */ |
| 98 | return -EINVAL; |
| 99 | default: |
| 100 | dev_err(&usb_dev->dev, |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 101 | "roccat_common2_receive_control_status: " |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 102 | "unknown response value 0x%x\n", |
| 103 | control.value); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | |
| 107 | } while (1); |
| 108 | } |
| 109 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 110 | int roccat_common2_send_with_status(struct usb_device *usb_dev, |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 111 | uint command, void const *buf, uint size) |
| 112 | { |
| 113 | int retval; |
| 114 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 115 | retval = roccat_common2_send(usb_dev, command, buf, size); |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 116 | if (retval) |
| 117 | return retval; |
| 118 | |
| 119 | msleep(100); |
| 120 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 121 | return roccat_common2_receive_control_status(usb_dev); |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 122 | } |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 123 | EXPORT_SYMBOL_GPL(roccat_common2_send_with_status); |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 124 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 125 | MODULE_AUTHOR("Stefan Achatz"); |
| 126 | MODULE_DESCRIPTION("USB Roccat common driver"); |
| 127 | MODULE_LICENSE("GPL v2"); |