hid-roccat.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Roccat driver for Linux
  4. *
  5. * Copyright (c) 2010 Stefan Achatz <[email protected]>
  6. */
  7. /*
  8. */
  9. /*
  10. * Module roccat is a char device used to report special events of roccat
  11. * hardware to userland. These events include requests for on-screen-display of
  12. * profile or dpi settings or requests for execution of macro sequences that are
  13. * not stored in device. The information in these events depends on hid device
  14. * implementation and contains data that is not available in a single hid event
  15. * or else hidraw could have been used.
  16. * It is inspired by hidraw, but uses only one circular buffer for all readers.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/cdev.h>
  20. #include <linux/poll.h>
  21. #include <linux/sched/signal.h>
  22. #include <linux/hid-roccat.h>
  23. #include <linux/module.h>
  24. #define ROCCAT_FIRST_MINOR 0
  25. #define ROCCAT_MAX_DEVICES 8
  26. /* should be a power of 2 for performance reason */
  27. #define ROCCAT_CBUF_SIZE 16
  28. struct roccat_report {
  29. uint8_t *value;
  30. };
  31. struct roccat_device {
  32. unsigned int minor;
  33. int report_size;
  34. int open;
  35. int exist;
  36. wait_queue_head_t wait;
  37. struct device *dev;
  38. struct hid_device *hid;
  39. struct list_head readers;
  40. /* protects modifications of readers list */
  41. struct mutex readers_lock;
  42. /*
  43. * circular_buffer has one writer and multiple readers with their own
  44. * read pointers
  45. */
  46. struct roccat_report cbuf[ROCCAT_CBUF_SIZE];
  47. int cbuf_end;
  48. struct mutex cbuf_lock;
  49. };
  50. struct roccat_reader {
  51. struct list_head node;
  52. struct roccat_device *device;
  53. int cbuf_start;
  54. };
  55. static int roccat_major;
  56. static struct cdev roccat_cdev;
  57. static struct roccat_device *devices[ROCCAT_MAX_DEVICES];
  58. /* protects modifications of devices array */
  59. static DEFINE_MUTEX(devices_lock);
  60. static ssize_t roccat_read(struct file *file, char __user *buffer,
  61. size_t count, loff_t *ppos)
  62. {
  63. struct roccat_reader *reader = file->private_data;
  64. struct roccat_device *device = reader->device;
  65. struct roccat_report *report;
  66. ssize_t retval = 0, len;
  67. DECLARE_WAITQUEUE(wait, current);
  68. mutex_lock(&device->cbuf_lock);
  69. /* no data? */
  70. if (reader->cbuf_start == device->cbuf_end) {
  71. add_wait_queue(&device->wait, &wait);
  72. set_current_state(TASK_INTERRUPTIBLE);
  73. /* wait for data */
  74. while (reader->cbuf_start == device->cbuf_end) {
  75. if (file->f_flags & O_NONBLOCK) {
  76. retval = -EAGAIN;
  77. break;
  78. }
  79. if (signal_pending(current)) {
  80. retval = -ERESTARTSYS;
  81. break;
  82. }
  83. if (!device->exist) {
  84. retval = -EIO;
  85. break;
  86. }
  87. mutex_unlock(&device->cbuf_lock);
  88. schedule();
  89. mutex_lock(&device->cbuf_lock);
  90. set_current_state(TASK_INTERRUPTIBLE);
  91. }
  92. set_current_state(TASK_RUNNING);
  93. remove_wait_queue(&device->wait, &wait);
  94. }
  95. /* here we either have data or a reason to return if retval is set */
  96. if (retval)
  97. goto exit_unlock;
  98. report = &device->cbuf[reader->cbuf_start];
  99. /*
  100. * If report is larger than requested amount of data, rest of report
  101. * is lost!
  102. */
  103. len = device->report_size > count ? count : device->report_size;
  104. if (copy_to_user(buffer, report->value, len)) {
  105. retval = -EFAULT;
  106. goto exit_unlock;
  107. }
  108. retval += len;
  109. reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
  110. exit_unlock:
  111. mutex_unlock(&device->cbuf_lock);
  112. return retval;
  113. }
  114. static __poll_t roccat_poll(struct file *file, poll_table *wait)
  115. {
  116. struct roccat_reader *reader = file->private_data;
  117. poll_wait(file, &reader->device->wait, wait);
  118. if (reader->cbuf_start != reader->device->cbuf_end)
  119. return EPOLLIN | EPOLLRDNORM;
  120. if (!reader->device->exist)
  121. return EPOLLERR | EPOLLHUP;
  122. return 0;
  123. }
  124. static int roccat_open(struct inode *inode, struct file *file)
  125. {
  126. unsigned int minor = iminor(inode);
  127. struct roccat_reader *reader;
  128. struct roccat_device *device;
  129. int error = 0;
  130. reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL);
  131. if (!reader)
  132. return -ENOMEM;
  133. mutex_lock(&devices_lock);
  134. device = devices[minor];
  135. if (!device) {
  136. pr_emerg("roccat device with minor %d doesn't exist\n", minor);
  137. error = -ENODEV;
  138. goto exit_err_devices;
  139. }
  140. mutex_lock(&device->readers_lock);
  141. if (!device->open++) {
  142. /* power on device on adding first reader */
  143. error = hid_hw_power(device->hid, PM_HINT_FULLON);
  144. if (error < 0) {
  145. --device->open;
  146. goto exit_err_readers;
  147. }
  148. error = hid_hw_open(device->hid);
  149. if (error < 0) {
  150. hid_hw_power(device->hid, PM_HINT_NORMAL);
  151. --device->open;
  152. goto exit_err_readers;
  153. }
  154. }
  155. reader->device = device;
  156. /* new reader doesn't get old events */
  157. reader->cbuf_start = device->cbuf_end;
  158. list_add_tail(&reader->node, &device->readers);
  159. file->private_data = reader;
  160. exit_err_readers:
  161. mutex_unlock(&device->readers_lock);
  162. exit_err_devices:
  163. mutex_unlock(&devices_lock);
  164. if (error)
  165. kfree(reader);
  166. return error;
  167. }
  168. static int roccat_release(struct inode *inode, struct file *file)
  169. {
  170. unsigned int minor = iminor(inode);
  171. struct roccat_reader *reader = file->private_data;
  172. struct roccat_device *device;
  173. mutex_lock(&devices_lock);
  174. device = devices[minor];
  175. if (!device) {
  176. mutex_unlock(&devices_lock);
  177. pr_emerg("roccat device with minor %d doesn't exist\n", minor);
  178. return -ENODEV;
  179. }
  180. mutex_lock(&device->readers_lock);
  181. list_del(&reader->node);
  182. mutex_unlock(&device->readers_lock);
  183. kfree(reader);
  184. if (!--device->open) {
  185. /* removing last reader */
  186. if (device->exist) {
  187. hid_hw_power(device->hid, PM_HINT_NORMAL);
  188. hid_hw_close(device->hid);
  189. } else {
  190. kfree(device);
  191. }
  192. }
  193. mutex_unlock(&devices_lock);
  194. return 0;
  195. }
  196. /*
  197. * roccat_report_event() - output data to readers
  198. * @minor: minor device number returned by roccat_connect()
  199. * @data: pointer to data
  200. *
  201. * Return value is zero on success, a negative error code on failure.
  202. *
  203. * This is called from interrupt handler.
  204. */
  205. int roccat_report_event(int minor, u8 const *data)
  206. {
  207. struct roccat_device *device;
  208. struct roccat_reader *reader;
  209. struct roccat_report *report;
  210. uint8_t *new_value;
  211. device = devices[minor];
  212. new_value = kmemdup(data, device->report_size, GFP_ATOMIC);
  213. if (!new_value)
  214. return -ENOMEM;
  215. mutex_lock(&device->cbuf_lock);
  216. report = &device->cbuf[device->cbuf_end];
  217. /* passing NULL is safe */
  218. kfree(report->value);
  219. report->value = new_value;
  220. device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE;
  221. list_for_each_entry(reader, &device->readers, node) {
  222. /*
  223. * As we already inserted one element, the buffer can't be
  224. * empty. If start and end are equal, buffer is full and we
  225. * increase start, so that slow reader misses one event, but
  226. * gets the newer ones in the right order.
  227. */
  228. if (reader->cbuf_start == device->cbuf_end)
  229. reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
  230. }
  231. mutex_unlock(&device->cbuf_lock);
  232. wake_up_interruptible(&device->wait);
  233. return 0;
  234. }
  235. EXPORT_SYMBOL_GPL(roccat_report_event);
  236. /*
  237. * roccat_connect() - create a char device for special event output
  238. * @class: the class thats used to create the device. Meant to hold device
  239. * specific sysfs attributes.
  240. * @hid: the hid device the char device should be connected to.
  241. * @report_size: size of reports
  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_inode(file);
  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. if (retval < 0) {
  352. pr_warn("can't get major number\n");
  353. goto error;
  354. }
  355. roccat_major = MAJOR(dev_id);
  356. cdev_init(&roccat_cdev, &roccat_ops);
  357. retval = cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
  358. if (retval < 0) {
  359. pr_warn("cannot add cdev\n");
  360. goto cleanup_alloc_chrdev_region;
  361. }
  362. return 0;
  363. cleanup_alloc_chrdev_region:
  364. unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
  365. error:
  366. return retval;
  367. }
  368. static void __exit roccat_exit(void)
  369. {
  370. dev_t dev_id = MKDEV(roccat_major, 0);
  371. cdev_del(&roccat_cdev);
  372. unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
  373. }
  374. module_init(roccat_init);
  375. module_exit(roccat_exit);
  376. MODULE_AUTHOR("Stefan Achatz");
  377. MODULE_DESCRIPTION("USB Roccat char device");
  378. MODULE_LICENSE("GPL v2");