2 * Roccat common functions for device specific drivers
4 * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net>
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)
14 #include <linux/hid.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include "hid-roccat-common.h"
19 static inline uint16_t roccat_common2_feature_report(uint8_t report_id)
21 return 0x300 | report_id;
24 int roccat_common2_receive(struct usb_device *usb_dev, uint report_id,
25 void *data, uint size)
30 buf = kmalloc(size, GFP_KERNEL);
34 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
36 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
37 roccat_common2_feature_report(report_id),
38 0, buf, size, USB_CTRL_SET_TIMEOUT);
40 memcpy(data, buf, size);
42 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
44 EXPORT_SYMBOL_GPL(roccat_common2_receive);
46 int roccat_common2_send(struct usb_device *usb_dev, uint report_id,
47 void const *data, uint size)
52 buf = kmemdup(data, size, GFP_KERNEL);
56 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
58 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
59 roccat_common2_feature_report(report_id),
60 0, buf, size, USB_CTRL_SET_TIMEOUT);
63 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
65 EXPORT_SYMBOL_GPL(roccat_common2_send);
67 enum roccat_common2_control_states {
68 ROCCAT_COMMON_CONTROL_STATUS_CRITICAL = 0,
69 ROCCAT_COMMON_CONTROL_STATUS_OK = 1,
70 ROCCAT_COMMON_CONTROL_STATUS_INVALID = 2,
71 ROCCAT_COMMON_CONTROL_STATUS_BUSY = 3,
72 ROCCAT_COMMON_CONTROL_STATUS_CRITICAL_NEW = 4,
75 static int roccat_common2_receive_control_status(struct usb_device *usb_dev)
78 struct roccat_common2_control control;
82 retval = roccat_common2_receive(usb_dev,
83 ROCCAT_COMMON_COMMAND_CONTROL,
84 &control, sizeof(struct roccat_common2_control));
89 switch (control.value) {
90 case ROCCAT_COMMON_CONTROL_STATUS_OK:
92 case ROCCAT_COMMON_CONTROL_STATUS_BUSY:
95 case ROCCAT_COMMON_CONTROL_STATUS_INVALID:
96 case ROCCAT_COMMON_CONTROL_STATUS_CRITICAL:
97 case ROCCAT_COMMON_CONTROL_STATUS_CRITICAL_NEW:
100 dev_err(&usb_dev->dev,
101 "roccat_common2_receive_control_status: "
102 "unknown response value 0x%x\n",
110 int roccat_common2_send_with_status(struct usb_device *usb_dev,
111 uint command, void const *buf, uint size)
115 retval = roccat_common2_send(usb_dev, command, buf, size);
121 return roccat_common2_receive_control_status(usb_dev);
123 EXPORT_SYMBOL_GPL(roccat_common2_send_with_status);
125 int roccat_common2_device_init_struct(struct usb_device *usb_dev,
126 struct roccat_common2_device *dev)
128 mutex_init(&dev->lock);
131 EXPORT_SYMBOL_GPL(roccat_common2_device_init_struct);
133 ssize_t roccat_common2_sysfs_read(struct file *fp, struct kobject *kobj,
134 char *buf, loff_t off, size_t count,
135 size_t real_size, uint command)
137 struct device *dev = kobj_to_dev(kobj)->parent->parent;
138 struct roccat_common2_device *roccat_dev = hid_get_drvdata(dev_get_drvdata(dev));
139 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
142 if (off >= real_size)
145 if (off != 0 || count != real_size)
148 mutex_lock(&roccat_dev->lock);
149 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
150 mutex_unlock(&roccat_dev->lock);
152 return retval ? retval : real_size;
154 EXPORT_SYMBOL_GPL(roccat_common2_sysfs_read);
156 ssize_t roccat_common2_sysfs_write(struct file *fp, struct kobject *kobj,
157 void const *buf, loff_t off, size_t count,
158 size_t real_size, uint command)
160 struct device *dev = kobj_to_dev(kobj)->parent->parent;
161 struct roccat_common2_device *roccat_dev = hid_get_drvdata(dev_get_drvdata(dev));
162 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
165 if (off != 0 || count != real_size)
168 mutex_lock(&roccat_dev->lock);
169 retval = roccat_common2_send_with_status(usb_dev, command, buf, real_size);
170 mutex_unlock(&roccat_dev->lock);
172 return retval ? retval : real_size;
174 EXPORT_SYMBOL_GPL(roccat_common2_sysfs_write);
176 MODULE_AUTHOR("Stefan Achatz");
177 MODULE_DESCRIPTION("USB Roccat common driver");
178 MODULE_LICENSE("GPL v2");