hid-roccat.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Roccat driver for Linux
  3. *
  4. * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. */
  12. /*
  13. * Module roccat is a char device used to report special events of roccat
  14. * hardware to userland. These events include requests for on-screen-display of
  15. * profile or dpi settings or requests for execution of macro sequences that are
  16. * not stored in device. The information in these events depends on hid device
  17. * implementation and contains data that is not available in a single hid event
  18. * or else hidraw could have been used.
  19. * It is inspired by hidraw, but uses only one circular buffer for all readers.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/cdev.h>
  23. #include <linux/poll.h>
  24. #include <linux/sched.h>
  25. #include "hid-roccat.h"
  26. #define ROCCAT_FIRST_MINOR 0
  27. #define ROCCAT_MAX_DEVICES 8
  28. /* should be a power of 2 for performance reason */
  29. #define ROCCAT_CBUF_SIZE 16
  30. struct roccat_report {
  31. uint8_t *value;
  32. int len;
  33. };
  34. struct roccat_device {
  35. unsigned int minor;
  36. int open;
  37. int exist;
  38. wait_queue_head_t wait;
  39. struct device *dev;
  40. struct hid_device *hid;
  41. struct list_head readers;
  42. /* protects modifications of readers list */
  43. struct mutex readers_lock;
  44. /*
  45. * circular_buffer has one writer and multiple readers with their own
  46. * read pointers
  47. */
  48. struct roccat_report cbuf[ROCCAT_CBUF_SIZE];
  49. int cbuf_end;
  50. struct mutex cbuf_lock;
  51. };
  52. struct roccat_reader {
  53. struct list_head node;
  54. struct roccat_device *device;
  55. int cbuf_start;
  56. };
  57. static int roccat_major;
  58. static struct class *roccat_class;
  59. static struct cdev roccat_cdev;
  60. static struct roccat_device *devices[ROCCAT_MAX_DEVICES];
  61. /* protects modifications of devices array */
  62. static DEFINE_MUTEX(devices_lock);
  63. static ssize_t roccat_read(struct file *file, char __user *buffer,
  64. size_t count, loff_t *ppos)
  65. {
  66. struct roccat_reader *reader = file->private_data;
  67. struct roccat_device *device = reader->device;
  68. struct roccat_report *report;
  69. ssize_t retval = 0, len;
  70. DECLARE_WAITQUEUE(wait, current);
  71. mutex_lock(&device->cbuf_lock);
  72. /* no data? */
  73. if (reader->cbuf_start == device->cbuf_end) {
  74. add_wait_queue(&device->wait, &wait);
  75. set_current_state(TASK_INTERRUPTIBLE);
  76. /* wait for data */
  77. while (reader->cbuf_start == device->cbuf_end) {
  78. if (file->f_flags & O_NONBLOCK) {
  79. retval = -EAGAIN;
  80. break;
  81. }
  82. if (signal_pending(current)) {
  83. retval = -ERESTARTSYS;
  84. break;
  85. }
  86. if (!device->exist) {
  87. retval = -EIO;
  88. break;
  89. }
  90. mutex_unlock(&device->cbuf_lock);
  91. schedule();
  92. mutex_lock(&device->cbuf_lock);
  93. set_current_state(TASK_INTERRUPTIBLE);
  94. }
  95. set_current_state(TASK_RUNNING);
  96. remove_wait_queue(&device->wait, &wait);
  97. }
  98. /* here we either have data or a reason to return if retval is set */
  99. if (retval)
  100. goto exit_unlock;
  101. report = &device->cbuf[reader->cbuf_start];
  102. /*
  103. * If report is larger than requested amount of data, rest of report
  104. * is lost!
  105. */
  106. len = report->len > count ? count : report->len;
  107. if (copy_to_user(buffer, report->value, len)) {
  108. retval = -EFAULT;
  109. goto exit_unlock;
  110. }
  111. retval += len;
  112. reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
  113. exit_unlock:
  114. mutex_unlock(&device->cbuf_lock);
  115. return retval;
  116. }
  117. static unsigned int roccat_poll(struct file *file, poll_table *wait)
  118. {
  119. struct roccat_reader *reader = file->private_data;
  120. poll_wait(file, &reader->device->wait, wait);
  121. if (reader->cbuf_start != reader->device->cbuf_end)
  122. return POLLIN | POLLRDNORM;
  123. if (!reader->device->exist)
  124. return POLLERR | POLLHUP;
  125. return 0;
  126. }
  127. static int roccat_open(struct inode *inode, struct file *file)
  128. {
  129. unsigned int minor = iminor(inode);
  130. struct roccat_reader *reader;
  131. struct roccat_device *device;
  132. int error = 0;
  133. reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL);
  134. if (!reader)
  135. return -ENOMEM;
  136. mutex_lock(&devices_lock);
  137. device = devices[minor];
  138. mutex_lock(&device->readers_lock);
  139. if (!device) {
  140. pr_emerg("roccat device with minor %d doesn't exist\n", minor);
  141. error = -ENODEV;
  142. goto exit_err;
  143. }
  144. if (!device->open++) {
  145. /* power on device on adding first reader */
  146. error = hid_hw_power(device->hid, PM_HINT_FULLON);
  147. if (error < 0) {
  148. --device->open;
  149. goto exit_err;
  150. }
  151. error = hid_hw_open(device->hid);
  152. if (error < 0) {
  153. hid_hw_power(device->hid, PM_HINT_NORMAL);
  154. --device->open;
  155. goto exit_err;
  156. }
  157. }
  158. reader->device = device;
  159. /* new reader doesn't get old events */
  160. reader->cbuf_start = device->cbuf_end;
  161. list_add_tail(&reader->node, &device->readers);
  162. file->private_data = reader;
  163. exit_unlock:
  164. mutex_unlock(&device->readers_lock);
  165. mutex_unlock(&devices_lock);
  166. return error;
  167. exit_err:
  168. kfree(reader);
  169. goto exit_unlock;
  170. }
  171. static int roccat_release(struct inode *inode, struct file *file)
  172. {
  173. unsigned int minor = iminor(inode);
  174. struct roccat_reader *reader = file->private_data;
  175. struct roccat_device *device;
  176. mutex_lock(&devices_lock);
  177. device = devices[minor];
  178. if (!device) {
  179. mutex_unlock(&devices_lock);
  180. pr_emerg("roccat device with minor %d doesn't exist\n", minor);
  181. return -ENODEV;
  182. }
  183. mutex_lock(&device->readers_lock);
  184. list_del(&reader->node);
  185. mutex_unlock(&device->readers_lock);
  186. kfree(reader);
  187. if (!--device->open) {
  188. /* removing last reader */
  189. if (device->exist) {
  190. hid_hw_power(device->hid, PM_HINT_NORMAL);
  191. hid_hw_close(device->hid);
  192. } else {
  193. kfree(device);
  194. }
  195. }
  196. mutex_unlock(&devices_lock);
  197. return 0;
  198. }
  199. /*
  200. * roccat_report_event() - output data to readers
  201. * @minor: minor device number returned by roccat_connect()
  202. * @data: pointer to data
  203. * @len: size of data
  204. *
  205. * Return value is zero on success, a negative error code on failure.
  206. *
  207. * This is called from interrupt handler.
  208. */
  209. int roccat_report_event(int minor, u8 const *data, int len)
  210. {
  211. struct roccat_device *device;
  212. struct roccat_reader *reader;
  213. struct roccat_report *report;
  214. uint8_t *new_value;
  215. new_value = kmemdup(data, len, GFP_ATOMIC);
  216. if (!new_value)
  217. return -ENOMEM;
  218. device = devices[minor];
  219. report = &device->cbuf[device->cbuf_end];
  220. /* passing NULL is safe */
  221. kfree(report->value);
  222. report->value = new_value;
  223. report->len = len;
  224. device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE;
  225. list_for_each_entry(reader, &device->readers, node) {
  226. /*
  227. * As we already inserted one element, the buffer can't be
  228. * empty. If start and end are equal, buffer is full and we
  229. * increase start, so that slow reader misses one event, but
  230. * gets the newer ones in the right order.
  231. */
  232. if (reader->cbuf_start == device->cbuf_end)
  233. reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
  234. }
  235. wake_up_interruptible(&device->wait);
  236. return 0;
  237. }
  238. EXPORT_SYMBOL_GPL(roccat_report_event);
  239. /*
  240. * roccat_connect() - create a char device for special event output
  241. * @hid: the hid device the char device should be connected to.
  242. *
  243. * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on
  244. * success, a negative error code on failure.
  245. */
  246. int roccat_connect(struct hid_device *hid)
  247. {
  248. unsigned int minor;
  249. struct roccat_device *device;
  250. int temp;
  251. device = kzalloc(sizeof(struct roccat_device), GFP_KERNEL);
  252. if (!device)
  253. return -ENOMEM;
  254. mutex_lock(&devices_lock);
  255. for (minor = 0; minor < ROCCAT_MAX_DEVICES; ++minor) {
  256. if (devices[minor])
  257. continue;
  258. break;
  259. }
  260. if (minor < ROCCAT_MAX_DEVICES) {
  261. devices[minor] = device;
  262. } else {
  263. mutex_unlock(&devices_lock);
  264. kfree(device);
  265. return -EINVAL;
  266. }
  267. device->dev = device_create(roccat_class, &hid->dev,
  268. MKDEV(roccat_major, minor), NULL,
  269. "%s%s%d", "roccat", hid->driver->name, minor);
  270. if (IS_ERR(device->dev)) {
  271. devices[minor] = NULL;
  272. mutex_unlock(&devices_lock);
  273. temp = PTR_ERR(device->dev);
  274. kfree(device);
  275. return temp;
  276. }
  277. mutex_unlock(&devices_lock);
  278. init_waitqueue_head(&device->wait);
  279. INIT_LIST_HEAD(&device->readers);
  280. mutex_init(&device->readers_lock);
  281. mutex_init(&device->cbuf_lock);
  282. device->minor = minor;
  283. device->hid = hid;
  284. device->exist = 1;
  285. device->cbuf_end = 0;
  286. return minor;
  287. }
  288. EXPORT_SYMBOL_GPL(roccat_connect);
  289. /* roccat_disconnect() - remove char device from hid device
  290. * @minor: the minor device number returned by roccat_connect()
  291. */
  292. void roccat_disconnect(int minor)
  293. {
  294. struct roccat_device *device;
  295. mutex_lock(&devices_lock);
  296. device = devices[minor];
  297. devices[minor] = NULL;
  298. mutex_unlock(&devices_lock);
  299. device->exist = 0; /* TODO exist maybe not needed */
  300. device_destroy(roccat_class, MKDEV(roccat_major, minor));
  301. if (device->open) {
  302. hid_hw_close(device->hid);
  303. wake_up_interruptible(&device->wait);
  304. } else {
  305. kfree(device);
  306. }
  307. }
  308. EXPORT_SYMBOL_GPL(roccat_disconnect);
  309. static const struct file_operations roccat_ops = {
  310. .owner = THIS_MODULE,
  311. .read = roccat_read,
  312. .poll = roccat_poll,
  313. .open = roccat_open,
  314. .release = roccat_release,
  315. .llseek = noop_llseek,
  316. };
  317. static int __init roccat_init(void)
  318. {
  319. int retval;
  320. dev_t dev_id;
  321. retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR,
  322. ROCCAT_MAX_DEVICES, "roccat");
  323. roccat_major = MAJOR(dev_id);
  324. if (retval < 0) {
  325. pr_warn("can't get major number\n");
  326. return retval;
  327. }
  328. roccat_class = class_create(THIS_MODULE, "roccat");
  329. if (IS_ERR(roccat_class)) {
  330. retval = PTR_ERR(roccat_class);
  331. unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
  332. return retval;
  333. }
  334. cdev_init(&roccat_cdev, &roccat_ops);
  335. cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
  336. return 0;
  337. }
  338. static void __exit roccat_exit(void)
  339. {
  340. dev_t dev_id = MKDEV(roccat_major, 0);
  341. cdev_del(&roccat_cdev);
  342. class_destroy(roccat_class);
  343. unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
  344. }
  345. module_init(roccat_init);
  346. module_exit(roccat_exit);
  347. MODULE_AUTHOR("Stefan Achatz");
  348. MODULE_DESCRIPTION("USB Roccat char device");
  349. MODULE_LICENSE("GPL v2");