hid-roccat-kovaplus.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * Roccat Kova[+] driver for Linux
  3. *
  4. * Copyright (c) 2011 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. * Roccat Kova[+] is a bigger version of the Pyra with two more side buttons.
  14. */
  15. #include <linux/device.h>
  16. #include <linux/input.h>
  17. #include <linux/hid.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/hid-roccat.h>
  21. #include "hid-ids.h"
  22. #include "hid-roccat-common.h"
  23. #include "hid-roccat-kovaplus.h"
  24. static uint profile_numbers[5] = {0, 1, 2, 3, 4};
  25. static struct class *kovaplus_class;
  26. static uint kovaplus_convert_event_cpi(uint value)
  27. {
  28. return (value == 7 ? 4 : (value == 4 ? 3 : value));
  29. }
  30. static void kovaplus_profile_activated(struct kovaplus_device *kovaplus,
  31. uint new_profile_index)
  32. {
  33. kovaplus->actual_profile = new_profile_index;
  34. kovaplus->actual_cpi = kovaplus->profile_settings[new_profile_index].cpi_startup_level;
  35. kovaplus->actual_x_sensitivity = kovaplus->profile_settings[new_profile_index].sensitivity_x;
  36. kovaplus->actual_y_sensitivity = kovaplus->profile_settings[new_profile_index].sensitivity_y;
  37. }
  38. static int kovaplus_send_control(struct usb_device *usb_dev, uint value,
  39. enum kovaplus_control_requests request)
  40. {
  41. int retval;
  42. struct kovaplus_control control;
  43. if ((request == KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
  44. request == KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
  45. value > 4)
  46. return -EINVAL;
  47. control.command = KOVAPLUS_COMMAND_CONTROL;
  48. control.value = value;
  49. control.request = request;
  50. retval = roccat_common_send(usb_dev, KOVAPLUS_USB_COMMAND_CONTROL,
  51. &control, sizeof(struct kovaplus_control));
  52. return retval;
  53. }
  54. static int kovaplus_receive_control_status(struct usb_device *usb_dev)
  55. {
  56. int retval;
  57. struct kovaplus_control control;
  58. do {
  59. retval = roccat_common_receive(usb_dev, KOVAPLUS_USB_COMMAND_CONTROL,
  60. &control, sizeof(struct kovaplus_control));
  61. /* check if we get a completely wrong answer */
  62. if (retval)
  63. return retval;
  64. if (control.value == KOVAPLUS_CONTROL_REQUEST_STATUS_OK)
  65. return 0;
  66. /* indicates that hardware needs some more time to complete action */
  67. if (control.value == KOVAPLUS_CONTROL_REQUEST_STATUS_WAIT) {
  68. msleep(500); /* windows driver uses 1000 */
  69. continue;
  70. }
  71. /* seems to be critical - replug necessary */
  72. if (control.value == KOVAPLUS_CONTROL_REQUEST_STATUS_OVERLOAD)
  73. return -EINVAL;
  74. hid_err(usb_dev, "kovaplus_receive_control_status: "
  75. "unknown response value 0x%x\n", control.value);
  76. return -EINVAL;
  77. } while (1);
  78. }
  79. static int kovaplus_send(struct usb_device *usb_dev, uint command,
  80. void const *buf, uint size)
  81. {
  82. int retval;
  83. retval = roccat_common_send(usb_dev, command, buf, size);
  84. if (retval)
  85. return retval;
  86. msleep(100);
  87. return kovaplus_receive_control_status(usb_dev);
  88. }
  89. static int kovaplus_select_profile(struct usb_device *usb_dev, uint number,
  90. enum kovaplus_control_requests request)
  91. {
  92. return kovaplus_send_control(usb_dev, number, request);
  93. }
  94. static int kovaplus_get_info(struct usb_device *usb_dev,
  95. struct kovaplus_info *buf)
  96. {
  97. return roccat_common_receive(usb_dev, KOVAPLUS_USB_COMMAND_INFO,
  98. buf, sizeof(struct kovaplus_info));
  99. }
  100. static int kovaplus_get_profile_settings(struct usb_device *usb_dev,
  101. struct kovaplus_profile_settings *buf, uint number)
  102. {
  103. int retval;
  104. retval = kovaplus_select_profile(usb_dev, number,
  105. KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
  106. if (retval)
  107. return retval;
  108. return roccat_common_receive(usb_dev, KOVAPLUS_USB_COMMAND_PROFILE_SETTINGS,
  109. buf, sizeof(struct kovaplus_profile_settings));
  110. }
  111. static int kovaplus_set_profile_settings(struct usb_device *usb_dev,
  112. struct kovaplus_profile_settings const *settings)
  113. {
  114. return kovaplus_send(usb_dev, KOVAPLUS_USB_COMMAND_PROFILE_SETTINGS,
  115. settings, sizeof(struct kovaplus_profile_settings));
  116. }
  117. static int kovaplus_get_profile_buttons(struct usb_device *usb_dev,
  118. struct kovaplus_profile_buttons *buf, int number)
  119. {
  120. int retval;
  121. retval = kovaplus_select_profile(usb_dev, number,
  122. KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
  123. if (retval)
  124. return retval;
  125. return roccat_common_receive(usb_dev, KOVAPLUS_USB_COMMAND_PROFILE_BUTTONS,
  126. buf, sizeof(struct kovaplus_profile_buttons));
  127. }
  128. static int kovaplus_set_profile_buttons(struct usb_device *usb_dev,
  129. struct kovaplus_profile_buttons const *buttons)
  130. {
  131. return kovaplus_send(usb_dev, KOVAPLUS_USB_COMMAND_PROFILE_BUTTONS,
  132. buttons, sizeof(struct kovaplus_profile_buttons));
  133. }
  134. /* retval is 0-4 on success, < 0 on error */
  135. static int kovaplus_get_actual_profile(struct usb_device *usb_dev)
  136. {
  137. struct kovaplus_actual_profile buf;
  138. int retval;
  139. retval = roccat_common_receive(usb_dev, KOVAPLUS_USB_COMMAND_ACTUAL_PROFILE,
  140. &buf, sizeof(struct kovaplus_actual_profile));
  141. return retval ? retval : buf.actual_profile;
  142. }
  143. static int kovaplus_set_actual_profile(struct usb_device *usb_dev,
  144. int new_profile)
  145. {
  146. struct kovaplus_actual_profile buf;
  147. buf.command = KOVAPLUS_COMMAND_ACTUAL_PROFILE;
  148. buf.size = sizeof(struct kovaplus_actual_profile);
  149. buf.actual_profile = new_profile;
  150. return kovaplus_send(usb_dev, KOVAPLUS_USB_COMMAND_ACTUAL_PROFILE,
  151. &buf, sizeof(struct kovaplus_actual_profile));
  152. }
  153. static ssize_t kovaplus_sysfs_read_profilex_settings(struct file *fp,
  154. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  155. loff_t off, size_t count)
  156. {
  157. struct device *dev =
  158. container_of(kobj, struct device, kobj)->parent->parent;
  159. struct kovaplus_device *kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
  160. if (off >= sizeof(struct kovaplus_profile_settings))
  161. return 0;
  162. if (off + count > sizeof(struct kovaplus_profile_settings))
  163. count = sizeof(struct kovaplus_profile_settings) - off;
  164. mutex_lock(&kovaplus->kovaplus_lock);
  165. memcpy(buf, ((char const *)&kovaplus->profile_settings[*(uint *)(attr->private)]) + off,
  166. count);
  167. mutex_unlock(&kovaplus->kovaplus_lock);
  168. return count;
  169. }
  170. static ssize_t kovaplus_sysfs_write_profile_settings(struct file *fp,
  171. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  172. loff_t off, size_t count)
  173. {
  174. struct device *dev =
  175. container_of(kobj, struct device, kobj)->parent->parent;
  176. struct kovaplus_device *kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
  177. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  178. int retval = 0;
  179. int difference;
  180. int profile_index;
  181. struct kovaplus_profile_settings *profile_settings;
  182. if (off != 0 || count != sizeof(struct kovaplus_profile_settings))
  183. return -EINVAL;
  184. profile_index = ((struct kovaplus_profile_settings const *)buf)->profile_index;
  185. profile_settings = &kovaplus->profile_settings[profile_index];
  186. mutex_lock(&kovaplus->kovaplus_lock);
  187. difference = memcmp(buf, profile_settings,
  188. sizeof(struct kovaplus_profile_settings));
  189. if (difference) {
  190. retval = kovaplus_set_profile_settings(usb_dev,
  191. (struct kovaplus_profile_settings const *)buf);
  192. if (!retval)
  193. memcpy(profile_settings, buf,
  194. sizeof(struct kovaplus_profile_settings));
  195. }
  196. mutex_unlock(&kovaplus->kovaplus_lock);
  197. if (retval)
  198. return retval;
  199. return sizeof(struct kovaplus_profile_settings);
  200. }
  201. static ssize_t kovaplus_sysfs_read_profilex_buttons(struct file *fp,
  202. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  203. loff_t off, size_t count)
  204. {
  205. struct device *dev =
  206. container_of(kobj, struct device, kobj)->parent->parent;
  207. struct kovaplus_device *kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
  208. if (off >= sizeof(struct kovaplus_profile_buttons))
  209. return 0;
  210. if (off + count > sizeof(struct kovaplus_profile_buttons))
  211. count = sizeof(struct kovaplus_profile_buttons) - off;
  212. mutex_lock(&kovaplus->kovaplus_lock);
  213. memcpy(buf, ((char const *)&kovaplus->profile_buttons[*(uint *)(attr->private)]) + off,
  214. count);
  215. mutex_unlock(&kovaplus->kovaplus_lock);
  216. return count;
  217. }
  218. static ssize_t kovaplus_sysfs_write_profile_buttons(struct file *fp,
  219. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  220. loff_t off, size_t count)
  221. {
  222. struct device *dev =
  223. container_of(kobj, struct device, kobj)->parent->parent;
  224. struct kovaplus_device *kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
  225. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  226. int retval = 0;
  227. int difference;
  228. uint profile_index;
  229. struct kovaplus_profile_buttons *profile_buttons;
  230. if (off != 0 || count != sizeof(struct kovaplus_profile_buttons))
  231. return -EINVAL;
  232. profile_index = ((struct kovaplus_profile_buttons const *)buf)->profile_index;
  233. profile_buttons = &kovaplus->profile_buttons[profile_index];
  234. mutex_lock(&kovaplus->kovaplus_lock);
  235. difference = memcmp(buf, profile_buttons,
  236. sizeof(struct kovaplus_profile_buttons));
  237. if (difference) {
  238. retval = kovaplus_set_profile_buttons(usb_dev,
  239. (struct kovaplus_profile_buttons const *)buf);
  240. if (!retval)
  241. memcpy(profile_buttons, buf,
  242. sizeof(struct kovaplus_profile_buttons));
  243. }
  244. mutex_unlock(&kovaplus->kovaplus_lock);
  245. if (retval)
  246. return retval;
  247. return sizeof(struct kovaplus_profile_buttons);
  248. }
  249. static ssize_t kovaplus_sysfs_show_actual_profile(struct device *dev,
  250. struct device_attribute *attr, char *buf)
  251. {
  252. struct kovaplus_device *kovaplus =
  253. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  254. return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_profile);
  255. }
  256. static ssize_t kovaplus_sysfs_set_actual_profile(struct device *dev,
  257. struct device_attribute *attr, char const *buf, size_t size)
  258. {
  259. struct kovaplus_device *kovaplus;
  260. struct usb_device *usb_dev;
  261. unsigned long profile;
  262. int retval;
  263. dev = dev->parent->parent;
  264. kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
  265. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  266. retval = strict_strtoul(buf, 10, &profile);
  267. if (retval)
  268. return retval;
  269. if (profile >= 5)
  270. return -EINVAL;
  271. mutex_lock(&kovaplus->kovaplus_lock);
  272. retval = kovaplus_set_actual_profile(usb_dev, profile);
  273. kovaplus->actual_profile = profile;
  274. mutex_unlock(&kovaplus->kovaplus_lock);
  275. if (retval)
  276. return retval;
  277. return size;
  278. }
  279. static ssize_t kovaplus_sysfs_show_actual_cpi(struct device *dev,
  280. struct device_attribute *attr, char *buf)
  281. {
  282. struct kovaplus_device *kovaplus =
  283. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  284. return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_cpi);
  285. }
  286. static ssize_t kovaplus_sysfs_show_actual_sensitivity_x(struct device *dev,
  287. struct device_attribute *attr, char *buf)
  288. {
  289. struct kovaplus_device *kovaplus =
  290. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  291. return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_x_sensitivity);
  292. }
  293. static ssize_t kovaplus_sysfs_show_actual_sensitivity_y(struct device *dev,
  294. struct device_attribute *attr, char *buf)
  295. {
  296. struct kovaplus_device *kovaplus =
  297. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  298. return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_y_sensitivity);
  299. }
  300. static ssize_t kovaplus_sysfs_show_firmware_version(struct device *dev,
  301. struct device_attribute *attr, char *buf)
  302. {
  303. struct kovaplus_device *kovaplus =
  304. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  305. return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->info.firmware_version);
  306. }
  307. static struct device_attribute kovaplus_attributes[] = {
  308. __ATTR(actual_cpi, 0440,
  309. kovaplus_sysfs_show_actual_cpi, NULL),
  310. __ATTR(firmware_version, 0440,
  311. kovaplus_sysfs_show_firmware_version, NULL),
  312. __ATTR(actual_profile, 0660,
  313. kovaplus_sysfs_show_actual_profile,
  314. kovaplus_sysfs_set_actual_profile),
  315. __ATTR(actual_sensitivity_x, 0440,
  316. kovaplus_sysfs_show_actual_sensitivity_x, NULL),
  317. __ATTR(actual_sensitivity_y, 0440,
  318. kovaplus_sysfs_show_actual_sensitivity_y, NULL),
  319. __ATTR_NULL
  320. };
  321. static struct bin_attribute kovaplus_bin_attributes[] = {
  322. {
  323. .attr = { .name = "profile_settings", .mode = 0220 },
  324. .size = sizeof(struct kovaplus_profile_settings),
  325. .write = kovaplus_sysfs_write_profile_settings
  326. },
  327. {
  328. .attr = { .name = "profile1_settings", .mode = 0440 },
  329. .size = sizeof(struct kovaplus_profile_settings),
  330. .read = kovaplus_sysfs_read_profilex_settings,
  331. .private = &profile_numbers[0]
  332. },
  333. {
  334. .attr = { .name = "profile2_settings", .mode = 0440 },
  335. .size = sizeof(struct kovaplus_profile_settings),
  336. .read = kovaplus_sysfs_read_profilex_settings,
  337. .private = &profile_numbers[1]
  338. },
  339. {
  340. .attr = { .name = "profile3_settings", .mode = 0440 },
  341. .size = sizeof(struct kovaplus_profile_settings),
  342. .read = kovaplus_sysfs_read_profilex_settings,
  343. .private = &profile_numbers[2]
  344. },
  345. {
  346. .attr = { .name = "profile4_settings", .mode = 0440 },
  347. .size = sizeof(struct kovaplus_profile_settings),
  348. .read = kovaplus_sysfs_read_profilex_settings,
  349. .private = &profile_numbers[3]
  350. },
  351. {
  352. .attr = { .name = "profile5_settings", .mode = 0440 },
  353. .size = sizeof(struct kovaplus_profile_settings),
  354. .read = kovaplus_sysfs_read_profilex_settings,
  355. .private = &profile_numbers[4]
  356. },
  357. {
  358. .attr = { .name = "profile_buttons", .mode = 0220 },
  359. .size = sizeof(struct kovaplus_profile_buttons),
  360. .write = kovaplus_sysfs_write_profile_buttons
  361. },
  362. {
  363. .attr = { .name = "profile1_buttons", .mode = 0440 },
  364. .size = sizeof(struct kovaplus_profile_buttons),
  365. .read = kovaplus_sysfs_read_profilex_buttons,
  366. .private = &profile_numbers[0]
  367. },
  368. {
  369. .attr = { .name = "profile2_buttons", .mode = 0440 },
  370. .size = sizeof(struct kovaplus_profile_buttons),
  371. .read = kovaplus_sysfs_read_profilex_buttons,
  372. .private = &profile_numbers[1]
  373. },
  374. {
  375. .attr = { .name = "profile3_buttons", .mode = 0440 },
  376. .size = sizeof(struct kovaplus_profile_buttons),
  377. .read = kovaplus_sysfs_read_profilex_buttons,
  378. .private = &profile_numbers[2]
  379. },
  380. {
  381. .attr = { .name = "profile4_buttons", .mode = 0440 },
  382. .size = sizeof(struct kovaplus_profile_buttons),
  383. .read = kovaplus_sysfs_read_profilex_buttons,
  384. .private = &profile_numbers[3]
  385. },
  386. {
  387. .attr = { .name = "profile5_buttons", .mode = 0440 },
  388. .size = sizeof(struct kovaplus_profile_buttons),
  389. .read = kovaplus_sysfs_read_profilex_buttons,
  390. .private = &profile_numbers[4]
  391. },
  392. __ATTR_NULL
  393. };
  394. static int kovaplus_init_kovaplus_device_struct(struct usb_device *usb_dev,
  395. struct kovaplus_device *kovaplus)
  396. {
  397. int retval, i;
  398. static uint wait = 70; /* device will freeze with just 60 */
  399. mutex_init(&kovaplus->kovaplus_lock);
  400. retval = kovaplus_get_info(usb_dev, &kovaplus->info);
  401. if (retval)
  402. return retval;
  403. for (i = 0; i < 5; ++i) {
  404. msleep(wait);
  405. retval = kovaplus_get_profile_settings(usb_dev,
  406. &kovaplus->profile_settings[i], i);
  407. if (retval)
  408. return retval;
  409. msleep(wait);
  410. retval = kovaplus_get_profile_buttons(usb_dev,
  411. &kovaplus->profile_buttons[i], i);
  412. if (retval)
  413. return retval;
  414. }
  415. msleep(wait);
  416. retval = kovaplus_get_actual_profile(usb_dev);
  417. if (retval < 0)
  418. return retval;
  419. kovaplus_profile_activated(kovaplus, retval);
  420. return 0;
  421. }
  422. static int kovaplus_init_specials(struct hid_device *hdev)
  423. {
  424. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  425. struct usb_device *usb_dev = interface_to_usbdev(intf);
  426. struct kovaplus_device *kovaplus;
  427. int retval;
  428. if (intf->cur_altsetting->desc.bInterfaceProtocol
  429. == USB_INTERFACE_PROTOCOL_MOUSE) {
  430. kovaplus = kzalloc(sizeof(*kovaplus), GFP_KERNEL);
  431. if (!kovaplus) {
  432. hid_err(hdev, "can't alloc device descriptor\n");
  433. return -ENOMEM;
  434. }
  435. hid_set_drvdata(hdev, kovaplus);
  436. retval = kovaplus_init_kovaplus_device_struct(usb_dev, kovaplus);
  437. if (retval) {
  438. hid_err(hdev, "couldn't init struct kovaplus_device\n");
  439. goto exit_free;
  440. }
  441. retval = roccat_connect(kovaplus_class, hdev,
  442. sizeof(struct kovaplus_roccat_report));
  443. if (retval < 0) {
  444. hid_err(hdev, "couldn't init char dev\n");
  445. } else {
  446. kovaplus->chrdev_minor = retval;
  447. kovaplus->roccat_claimed = 1;
  448. }
  449. } else {
  450. hid_set_drvdata(hdev, NULL);
  451. }
  452. return 0;
  453. exit_free:
  454. kfree(kovaplus);
  455. return retval;
  456. }
  457. static void kovaplus_remove_specials(struct hid_device *hdev)
  458. {
  459. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  460. struct kovaplus_device *kovaplus;
  461. if (intf->cur_altsetting->desc.bInterfaceProtocol
  462. == USB_INTERFACE_PROTOCOL_MOUSE) {
  463. kovaplus = hid_get_drvdata(hdev);
  464. if (kovaplus->roccat_claimed)
  465. roccat_disconnect(kovaplus->chrdev_minor);
  466. kfree(kovaplus);
  467. }
  468. }
  469. static int kovaplus_probe(struct hid_device *hdev,
  470. const struct hid_device_id *id)
  471. {
  472. int retval;
  473. retval = hid_parse(hdev);
  474. if (retval) {
  475. hid_err(hdev, "parse failed\n");
  476. goto exit;
  477. }
  478. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  479. if (retval) {
  480. hid_err(hdev, "hw start failed\n");
  481. goto exit;
  482. }
  483. retval = kovaplus_init_specials(hdev);
  484. if (retval) {
  485. hid_err(hdev, "couldn't install mouse\n");
  486. goto exit_stop;
  487. }
  488. return 0;
  489. exit_stop:
  490. hid_hw_stop(hdev);
  491. exit:
  492. return retval;
  493. }
  494. static void kovaplus_remove(struct hid_device *hdev)
  495. {
  496. kovaplus_remove_specials(hdev);
  497. hid_hw_stop(hdev);
  498. }
  499. static void kovaplus_keep_values_up_to_date(struct kovaplus_device *kovaplus,
  500. u8 const *data)
  501. {
  502. struct kovaplus_mouse_report_button const *button_report;
  503. if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON)
  504. return;
  505. button_report = (struct kovaplus_mouse_report_button const *)data;
  506. switch (button_report->type) {
  507. case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1:
  508. kovaplus_profile_activated(kovaplus, button_report->data1 - 1);
  509. break;
  510. case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI:
  511. kovaplus->actual_cpi = kovaplus_convert_event_cpi(button_report->data1);
  512. case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SENSITIVITY:
  513. kovaplus->actual_x_sensitivity = button_report->data1;
  514. kovaplus->actual_y_sensitivity = button_report->data2;
  515. }
  516. }
  517. static void kovaplus_report_to_chrdev(struct kovaplus_device const *kovaplus,
  518. u8 const *data)
  519. {
  520. struct kovaplus_roccat_report roccat_report;
  521. struct kovaplus_mouse_report_button const *button_report;
  522. if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON)
  523. return;
  524. button_report = (struct kovaplus_mouse_report_button const *)data;
  525. if (button_report->type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_2)
  526. return;
  527. roccat_report.type = button_report->type;
  528. roccat_report.profile = kovaplus->actual_profile + 1;
  529. if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_MACRO ||
  530. roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SHORTCUT ||
  531. roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
  532. roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER)
  533. roccat_report.button = button_report->data1;
  534. else
  535. roccat_report.button = 0;
  536. if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI)
  537. roccat_report.data1 = kovaplus_convert_event_cpi(button_report->data1);
  538. else
  539. roccat_report.data1 = button_report->data1;
  540. roccat_report.data2 = button_report->data2;
  541. roccat_report_event(kovaplus->chrdev_minor,
  542. (uint8_t const *)&roccat_report);
  543. }
  544. static int kovaplus_raw_event(struct hid_device *hdev,
  545. struct hid_report *report, u8 *data, int size)
  546. {
  547. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  548. struct kovaplus_device *kovaplus = hid_get_drvdata(hdev);
  549. if (intf->cur_altsetting->desc.bInterfaceProtocol
  550. != USB_INTERFACE_PROTOCOL_MOUSE)
  551. return 0;
  552. kovaplus_keep_values_up_to_date(kovaplus, data);
  553. if (kovaplus->roccat_claimed)
  554. kovaplus_report_to_chrdev(kovaplus, data);
  555. return 0;
  556. }
  557. static const struct hid_device_id kovaplus_devices[] = {
  558. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KOVAPLUS) },
  559. { }
  560. };
  561. MODULE_DEVICE_TABLE(hid, kovaplus_devices);
  562. static struct hid_driver kovaplus_driver = {
  563. .name = "kovaplus",
  564. .id_table = kovaplus_devices,
  565. .probe = kovaplus_probe,
  566. .remove = kovaplus_remove,
  567. .raw_event = kovaplus_raw_event
  568. };
  569. static int __init kovaplus_init(void)
  570. {
  571. int retval;
  572. kovaplus_class = class_create(THIS_MODULE, "kovaplus");
  573. if (IS_ERR(kovaplus_class))
  574. return PTR_ERR(kovaplus_class);
  575. kovaplus_class->dev_attrs = kovaplus_attributes;
  576. kovaplus_class->dev_bin_attrs = kovaplus_bin_attributes;
  577. retval = hid_register_driver(&kovaplus_driver);
  578. if (retval)
  579. class_destroy(kovaplus_class);
  580. return retval;
  581. }
  582. static void __exit kovaplus_exit(void)
  583. {
  584. hid_unregister_driver(&kovaplus_driver);
  585. class_destroy(kovaplus_class);
  586. }
  587. module_init(kovaplus_init);
  588. module_exit(kovaplus_exit);
  589. MODULE_AUTHOR("Stefan Achatz");
  590. MODULE_DESCRIPTION("USB Roccat Kova[+] driver");
  591. MODULE_LICENSE("GPL v2");