hid-roccat.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. #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. };
  33. struct roccat_device {
  34. unsigned int minor;
  35. int report_size;
  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 cdev roccat_cdev;
  59. static struct roccat_device *devices[ROCCAT_MAX_DEVICES];
  60. /* protects modifications of devices array */
  61. static DEFINE_MUTEX(devices_lock);
  62. static ssize_t roccat_read(struct file *file, char __user *buffer,
  63. size_t count, loff_t *ppos)
  64. {
  65. struct roccat_reader *reader = file->private_data;
  66. struct roccat_device *device = reader->device;
  67. struct roccat_report *report;
  68. ssize_t retval = 0, len;
  69. DECLARE_WAITQUEUE(wait, current);
  70. mutex_lock(&device->cbuf_lock);
  71. /* no data? */
  72. if (reader->cbuf_start == device->cbuf_end) {
  73. add_wait_queue(&device->wait, &wait);
  74. set_current_state(TASK_INTERRUPTIBLE);
  75. /* wait for data */
  76. while (reader->cbuf_start == device->cbuf_end) {
  77. if (file->f_flags & O_NONBLOCK) {
  78. retval = -EAGAIN;
  79. break;
  80. }
  81. if (signal_pending(current)) {
  82. retval = -ERESTARTSYS;
  83. break;
  84. }
  85. if (!device->exist) {
  86. retval = -EIO;
  87. break;
  88. }
  89. mutex_unlock(&device->cbuf_lock);
  90. schedule();
  91. mutex_lock(&device->cbuf_lock);
  92. set_current_state(TASK_INTERRUPTIBLE);
  93. }
  94. set_current_state(TASK_RUNNING);
  95. remove_wait_queue(&device->wait, &wait);
  96. }
  97. /* here we either have data or a reason to return if retval is set */
  98. if (retval)
  99. goto exit_unlock;
  100. report = &device->cbuf[reader->cbuf_start];
  101. /*
  102. * If report is larger than requested amount of data, rest of report
  103. * is lost!
  104. */
  105. len = device->report_size > count ? count : device->report_size;
  106. if (copy_to_user(buffer, report->value, len)) {
  107. retval = -EFAULT;
  108. goto exit_unlock;
  109. }
  110. retval += len;
  111. reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
  112. exit_unlock:
  113. mutex_unlock(&device->cbuf_lock);
  114. return retval;
  115. }
  116. static unsigned int roccat_poll(struct file *file, poll_table *wait)
  117. {
  118. struct roccat_reader *reader = file->private_data;
  119. poll_wait(file, &reader->device->wait, wait);
  120. if (reader->cbuf_start != reader->device->cbuf_end)
  121. return POLLIN | POLLRDNORM;
  122. if (!reader->device->exist)
  123. return POLLERR | POLLHUP;
  124. return 0;
  125. }
  126. static int roccat_open(struct inode *inode, struct file *file)
  127. {
  128. unsigned int minor = iminor(inode);
  129. struct roccat_reader *reader;
  130. struct roccat_device *device;
  131. int error = 0;
  132. reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL);
  133. if (!reader)
  134. return -ENOMEM;
  135. mutex_lock(&devices_lock);
  136. device = devices[minor];
  137. mutex_lock(&device->readers_lock);
  138. if (!device) {
  139. pr_emerg("roccat device with minor %d doesn't exist\n", minor);
  140. error = -ENODEV;
  141. goto exit_err;
  142. }
  143. if (!device->open++) {
  144. /* power on device on adding first reader */
  145. error = hid_hw_power(device->hid, PM_HINT_FULLON);
  146. if (error < 0) {
  147. --device->open;
  148. goto exit_err;
  149. }
  150. error = hid_hw_open(device->hid);
  151. if (error < 0) {
  152. hid_hw_power(device->hid, PM_HINT_NORMAL);
  153. --device->open;
  154. goto exit_err;
  155. }
  156. }
  157. reader->device = device;
  158. /* new reader doesn't get old events */
  159. reader->cbuf_start = device->cbuf_end;
  160. list_add_tail(&reader->node, &device->readers);
  161. file->private_data = reader;
  162. exit_unlock:
  163. mutex_unlock(&device->readers_lock);
  164. mutex_unlock(&devices_lock);
  165. return error;
  166. exit_err:
  167. kfree(reader);
  168. goto exit_unlock;
  169. }
  170. static int roccat_release(struct inode *inode, struct file *file)
  171. {
  172. unsigned int minor = iminor(inode);
  173. struct roccat_reader *reader = file->private_data;
  174. struct roccat_device *device;
  175. mutex_lock(&devices_lock);
  176. device = devices[minor];
  177. if (!device) {
  178. mutex_unlock(&devices_lock);
  179. pr_emerg("roccat device with minor %d doesn't exist\n", minor);
  180. return -ENODEV;
  181. }
  182. mutex_lock(&device->readers_lock);
  183. list_del(&reader->node);
  184. mutex_unlock(&device->readers_lock);
  185. kfree(reader);
  186. if (!--device->open) {
  187. /* removing last reader */
  188. if (device->exist) {
  189. hid_hw_power(device->hid, PM_HINT_NORMAL);
  190. hid_hw_close(device->hid);
  191. } else {
  192. kfree(device);
  193. }
  194. }
  195. mutex_unlock(&devices_lock);
  196. return 0;
  197. }
  198. /*
  199. * roccat_report_event() - output data to readers
  200. * @minor: minor device number returned by roccat_connect()
  201. * @data: pointer to data
  202. * @len: size of 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. *
  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 class *klass, struct hid_device *hid, int report_size)
  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(klass, &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. device->report_size = report_size;
  287. return minor;
  288. }
  289. EXPORT_SYMBOL_GPL(roccat_connect);
  290. /* roccat_disconnect() - remove char device from hid device
  291. * @minor: the minor device number returned by roccat_connect()
  292. */
  293. void roccat_disconnect(int minor)
  294. {
  295. struct roccat_device *device;
  296. mutex_lock(&devices_lock);
  297. device = devices[minor];
  298. mutex_unlock(&devices_lock);
  299. device->exist = 0; /* TODO exist maybe not needed */
  300. device_destroy(device->dev->class, MKDEV(roccat_major, minor));
  301. mutex_lock(&devices_lock);
  302. devices[minor] = NULL;
  303. mutex_unlock(&devices_lock);
  304. if (device->open) {
  305. hid_hw_close(device->hid);
  306. wake_up_interruptible(&device->wait);
  307. } else {
  308. kfree(device);
  309. }
  310. }
  311. EXPORT_SYMBOL_GPL(roccat_disconnect);
  312. static long roccat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  313. {
  314. struct inode *inode = file->f_path.dentry->d_inode;
  315. struct roccat_device *device;
  316. unsigned int minor = iminor(inode);
  317. long retval = 0;
  318. mutex_lock(&devices_lock);
  319. device = devices[minor];
  320. if (!device) {
  321. retval = -ENODEV;
  322. goto out;
  323. }
  324. switch (cmd) {
  325. case ROCCATIOCGREPSIZE:
  326. if (put_user(device->report_size, (int __user *)arg))
  327. retval = -EFAULT;
  328. break;
  329. default:
  330. retval = -ENOTTY;
  331. }
  332. out:
  333. mutex_unlock(&devices_lock);
  334. return retval;
  335. }
  336. static const struct file_operations roccat_ops = {
  337. .owner = THIS_MODULE,
  338. .read = roccat_read,
  339. .poll = roccat_poll,
  340. .open = roccat_open,
  341. .release = roccat_release,
  342. .llseek = noop_llseek,
  343. .unlocked_ioctl = roccat_ioctl,
  344. };
  345. static int __init roccat_init(void)
  346. {
  347. int retval;
  348. dev_t dev_id;
  349. retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR,
  350. ROCCAT_MAX_DEVICES, "roccat");
  351. roccat_major = MAJOR(dev_id);
  352. if (retval < 0) {
  353. pr_warn("can't get major number\n");
  354. return retval;
  355. }
  356. cdev_init(&roccat_cdev, &roccat_ops);
  357. cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
  358. return 0;
  359. }
  360. static void __exit roccat_exit(void)
  361. {
  362. dev_t dev_id = MKDEV(roccat_major, 0);
  363. cdev_del(&roccat_cdev);
  364. unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
  365. }
  366. module_init(roccat_init);
  367. module_exit(roccat_exit);
  368. MODULE_AUTHOR("Stefan Achatz");
  369. MODULE_DESCRIPTION("USB Roccat char device");
  370. MODULE_LICENSE("GPL v2");