hid-roccat.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 <linux/hid-roccat.h>
  26. #include <linux/module.h>
  27. #define ROCCAT_FIRST_MINOR 0
  28. #define ROCCAT_MAX_DEVICES 8
  29. /* should be a power of 2 for performance reason */
  30. #define ROCCAT_CBUF_SIZE 16
  31. struct roccat_report {
  32. uint8_t *value;
  33. };
  34. struct roccat_device {
  35. unsigned int minor;
  36. int report_size;
  37. int open;
  38. int exist;
  39. wait_queue_head_t wait;
  40. struct device *dev;
  41. struct hid_device *hid;
  42. struct list_head readers;
  43. /* protects modifications of readers list */
  44. struct mutex readers_lock;
  45. /*
  46. * circular_buffer has one writer and multiple readers with their own
  47. * read pointers
  48. */
  49. struct roccat_report cbuf[ROCCAT_CBUF_SIZE];
  50. int cbuf_end;
  51. struct mutex cbuf_lock;
  52. };
  53. struct roccat_reader {
  54. struct list_head node;
  55. struct roccat_device *device;
  56. int cbuf_start;
  57. };
  58. static int roccat_major;
  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 = device->report_size > count ? count : device->report_size;
  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. if (!device) {
  139. pr_emerg("roccat device with minor %d doesn't exist\n", minor);
  140. error = -ENODEV;
  141. goto exit_err_devices;
  142. }
  143. mutex_lock(&device->readers_lock);
  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_readers;
  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_readers;
  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_err_readers:
  164. mutex_unlock(&device->readers_lock);
  165. exit_err_devices:
  166. mutex_unlock(&devices_lock);
  167. if (error)
  168. kfree(reader);
  169. return error;
  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. *
  204. * Return value is zero on success, a negative error code on failure.
  205. *
  206. * This is called from interrupt handler.
  207. */
  208. int roccat_report_event(int minor, u8 const *data)
  209. {
  210. struct roccat_device *device;
  211. struct roccat_reader *reader;
  212. struct roccat_report *report;
  213. uint8_t *new_value;
  214. device = devices[minor];
  215. new_value = kmemdup(data, device->report_size, GFP_ATOMIC);
  216. if (!new_value)
  217. return -ENOMEM;
  218. report = &device->cbuf[device->cbuf_end];
  219. /* passing NULL is safe */
  220. kfree(report->value);
  221. report->value = new_value;
  222. device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE;
  223. list_for_each_entry(reader, &device->readers, node) {
  224. /*
  225. * As we already inserted one element, the buffer can't be
  226. * empty. If start and end are equal, buffer is full and we
  227. * increase start, so that slow reader misses one event, but
  228. * gets the newer ones in the right order.
  229. */
  230. if (reader->cbuf_start == device->cbuf_end)
  231. reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
  232. }
  233. wake_up_interruptible(&device->wait);
  234. return 0;
  235. }
  236. EXPORT_SYMBOL_GPL(roccat_report_event);
  237. /*
  238. * roccat_connect() - create a char device for special event output
  239. * @class: the class thats used to create the device. Meant to hold device
  240. * specific sysfs attributes.
  241. * @hid: the hid device the char device should be connected to.
  242. * @report_size: size of reports
  243. *
  244. * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on
  245. * success, a negative error code on failure.
  246. */
  247. int roccat_connect(struct class *klass, struct hid_device *hid, int report_size)
  248. {
  249. unsigned int minor;
  250. struct roccat_device *device;
  251. int temp;
  252. device = kzalloc(sizeof(struct roccat_device), GFP_KERNEL);
  253. if (!device)
  254. return -ENOMEM;
  255. mutex_lock(&devices_lock);
  256. for (minor = 0; minor < ROCCAT_MAX_DEVICES; ++minor) {
  257. if (devices[minor])
  258. continue;
  259. break;
  260. }
  261. if (minor < ROCCAT_MAX_DEVICES) {
  262. devices[minor] = device;
  263. } else {
  264. mutex_unlock(&devices_lock);
  265. kfree(device);
  266. return -EINVAL;
  267. }
  268. device->dev = device_create(klass, &hid->dev,
  269. MKDEV(roccat_major, minor), NULL,
  270. "%s%s%d", "roccat", hid->driver->name, minor);
  271. if (IS_ERR(device->dev)) {
  272. devices[minor] = NULL;
  273. mutex_unlock(&devices_lock);
  274. temp = PTR_ERR(device->dev);
  275. kfree(device);
  276. return temp;
  277. }
  278. mutex_unlock(&devices_lock);
  279. init_waitqueue_head(&device->wait);
  280. INIT_LIST_HEAD(&device->readers);
  281. mutex_init(&device->readers_lock);
  282. mutex_init(&device->cbuf_lock);
  283. device->minor = minor;
  284. device->hid = hid;
  285. device->exist = 1;
  286. device->cbuf_end = 0;
  287. device->report_size = report_size;
  288. return minor;
  289. }
  290. EXPORT_SYMBOL_GPL(roccat_connect);
  291. /* roccat_disconnect() - remove char device from hid device
  292. * @minor: the minor device number returned by roccat_connect()
  293. */
  294. void roccat_disconnect(int minor)
  295. {
  296. struct roccat_device *device;
  297. mutex_lock(&devices_lock);
  298. device = devices[minor];
  299. mutex_unlock(&devices_lock);
  300. device->exist = 0; /* TODO exist maybe not needed */
  301. device_destroy(device->dev->class, MKDEV(roccat_major, minor));
  302. mutex_lock(&devices_lock);
  303. devices[minor] = NULL;
  304. mutex_unlock(&devices_lock);
  305. if (device->open) {
  306. hid_hw_close(device->hid);
  307. wake_up_interruptible(&device->wait);
  308. } else {
  309. kfree(device);
  310. }
  311. }
  312. EXPORT_SYMBOL_GPL(roccat_disconnect);
  313. static long roccat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  314. {
  315. struct inode *inode = file_inode(file);
  316. struct roccat_device *device;
  317. unsigned int minor = iminor(inode);
  318. long retval = 0;
  319. mutex_lock(&devices_lock);
  320. device = devices[minor];
  321. if (!device) {
  322. retval = -ENODEV;
  323. goto out;
  324. }
  325. switch (cmd) {
  326. case ROCCATIOCGREPSIZE:
  327. if (put_user(device->report_size, (int __user *)arg))
  328. retval = -EFAULT;
  329. break;
  330. default:
  331. retval = -ENOTTY;
  332. }
  333. out:
  334. mutex_unlock(&devices_lock);
  335. return retval;
  336. }
  337. static const struct file_operations roccat_ops = {
  338. .owner = THIS_MODULE,
  339. .read = roccat_read,
  340. .poll = roccat_poll,
  341. .open = roccat_open,
  342. .release = roccat_release,
  343. .llseek = noop_llseek,
  344. .unlocked_ioctl = roccat_ioctl,
  345. };
  346. static int __init roccat_init(void)
  347. {
  348. int retval;
  349. dev_t dev_id;
  350. retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR,
  351. ROCCAT_MAX_DEVICES, "roccat");
  352. roccat_major = MAJOR(dev_id);
  353. if (retval < 0) {
  354. pr_warn("can't get major number\n");
  355. goto error;
  356. }
  357. cdev_init(&roccat_cdev, &roccat_ops);
  358. retval = cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
  359. if (retval < 0) {
  360. pr_warn("cannot add cdev\n");
  361. goto cleanup_alloc_chrdev_region;
  362. }
  363. return 0;
  364. cleanup_alloc_chrdev_region:
  365. unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
  366. error:
  367. return retval;
  368. }
  369. static void __exit roccat_exit(void)
  370. {
  371. dev_t dev_id = MKDEV(roccat_major, 0);
  372. cdev_del(&roccat_cdev);
  373. unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
  374. }
  375. module_init(roccat_init);
  376. module_exit(roccat_exit);
  377. MODULE_AUTHOR("Stefan Achatz");
  378. MODULE_DESCRIPTION("USB Roccat char device");
  379. MODULE_LICENSE("GPL v2");