hid-roccat-kovaplus.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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_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_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, "roccat_common_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_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_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_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_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_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_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_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. struct kovaplus_roccat_report roccat_report;
  264. dev = dev->parent->parent;
  265. kovaplus = hid_get_drvdata(dev_get_drvdata(dev));
  266. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  267. retval = strict_strtoul(buf, 10, &profile);
  268. if (retval)
  269. return retval;
  270. if (profile >= 5)
  271. return -EINVAL;
  272. mutex_lock(&kovaplus->kovaplus_lock);
  273. retval = kovaplus_set_actual_profile(usb_dev, profile);
  274. if (retval) {
  275. mutex_unlock(&kovaplus->kovaplus_lock);
  276. return retval;
  277. }
  278. kovaplus_profile_activated(kovaplus, profile);
  279. roccat_report.type = KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1;
  280. roccat_report.profile = profile + 1;
  281. roccat_report.button = 0;
  282. roccat_report.data1 = profile + 1;
  283. roccat_report.data2 = 0;
  284. roccat_report_event(kovaplus->chrdev_minor,
  285. (uint8_t const *)&roccat_report);
  286. mutex_unlock(&kovaplus->kovaplus_lock);
  287. return size;
  288. }
  289. static ssize_t kovaplus_sysfs_show_actual_cpi(struct device *dev,
  290. struct device_attribute *attr, char *buf)
  291. {
  292. struct kovaplus_device *kovaplus =
  293. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  294. return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_cpi);
  295. }
  296. static ssize_t kovaplus_sysfs_show_actual_sensitivity_x(struct device *dev,
  297. struct device_attribute *attr, char *buf)
  298. {
  299. struct kovaplus_device *kovaplus =
  300. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  301. return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_x_sensitivity);
  302. }
  303. static ssize_t kovaplus_sysfs_show_actual_sensitivity_y(struct device *dev,
  304. struct device_attribute *attr, char *buf)
  305. {
  306. struct kovaplus_device *kovaplus =
  307. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  308. return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_y_sensitivity);
  309. }
  310. static ssize_t kovaplus_sysfs_show_firmware_version(struct device *dev,
  311. struct device_attribute *attr, char *buf)
  312. {
  313. struct kovaplus_device *kovaplus =
  314. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  315. return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->info.firmware_version);
  316. }
  317. static struct device_attribute kovaplus_attributes[] = {
  318. __ATTR(actual_cpi, 0440,
  319. kovaplus_sysfs_show_actual_cpi, NULL),
  320. __ATTR(firmware_version, 0440,
  321. kovaplus_sysfs_show_firmware_version, NULL),
  322. __ATTR(actual_profile, 0660,
  323. kovaplus_sysfs_show_actual_profile,
  324. kovaplus_sysfs_set_actual_profile),
  325. __ATTR(actual_sensitivity_x, 0440,
  326. kovaplus_sysfs_show_actual_sensitivity_x, NULL),
  327. __ATTR(actual_sensitivity_y, 0440,
  328. kovaplus_sysfs_show_actual_sensitivity_y, NULL),
  329. __ATTR_NULL
  330. };
  331. static struct bin_attribute kovaplus_bin_attributes[] = {
  332. {
  333. .attr = { .name = "profile_settings", .mode = 0220 },
  334. .size = sizeof(struct kovaplus_profile_settings),
  335. .write = kovaplus_sysfs_write_profile_settings
  336. },
  337. {
  338. .attr = { .name = "profile1_settings", .mode = 0440 },
  339. .size = sizeof(struct kovaplus_profile_settings),
  340. .read = kovaplus_sysfs_read_profilex_settings,
  341. .private = &profile_numbers[0]
  342. },
  343. {
  344. .attr = { .name = "profile2_settings", .mode = 0440 },
  345. .size = sizeof(struct kovaplus_profile_settings),
  346. .read = kovaplus_sysfs_read_profilex_settings,
  347. .private = &profile_numbers[1]
  348. },
  349. {
  350. .attr = { .name = "profile3_settings", .mode = 0440 },
  351. .size = sizeof(struct kovaplus_profile_settings),
  352. .read = kovaplus_sysfs_read_profilex_settings,
  353. .private = &profile_numbers[2]
  354. },
  355. {
  356. .attr = { .name = "profile4_settings", .mode = 0440 },
  357. .size = sizeof(struct kovaplus_profile_settings),
  358. .read = kovaplus_sysfs_read_profilex_settings,
  359. .private = &profile_numbers[3]
  360. },
  361. {
  362. .attr = { .name = "profile5_settings", .mode = 0440 },
  363. .size = sizeof(struct kovaplus_profile_settings),
  364. .read = kovaplus_sysfs_read_profilex_settings,
  365. .private = &profile_numbers[4]
  366. },
  367. {
  368. .attr = { .name = "profile_buttons", .mode = 0220 },
  369. .size = sizeof(struct kovaplus_profile_buttons),
  370. .write = kovaplus_sysfs_write_profile_buttons
  371. },
  372. {
  373. .attr = { .name = "profile1_buttons", .mode = 0440 },
  374. .size = sizeof(struct kovaplus_profile_buttons),
  375. .read = kovaplus_sysfs_read_profilex_buttons,
  376. .private = &profile_numbers[0]
  377. },
  378. {
  379. .attr = { .name = "profile2_buttons", .mode = 0440 },
  380. .size = sizeof(struct kovaplus_profile_buttons),
  381. .read = kovaplus_sysfs_read_profilex_buttons,
  382. .private = &profile_numbers[1]
  383. },
  384. {
  385. .attr = { .name = "profile3_buttons", .mode = 0440 },
  386. .size = sizeof(struct kovaplus_profile_buttons),
  387. .read = kovaplus_sysfs_read_profilex_buttons,
  388. .private = &profile_numbers[2]
  389. },
  390. {
  391. .attr = { .name = "profile4_buttons", .mode = 0440 },
  392. .size = sizeof(struct kovaplus_profile_buttons),
  393. .read = kovaplus_sysfs_read_profilex_buttons,
  394. .private = &profile_numbers[3]
  395. },
  396. {
  397. .attr = { .name = "profile5_buttons", .mode = 0440 },
  398. .size = sizeof(struct kovaplus_profile_buttons),
  399. .read = kovaplus_sysfs_read_profilex_buttons,
  400. .private = &profile_numbers[4]
  401. },
  402. __ATTR_NULL
  403. };
  404. static int kovaplus_init_kovaplus_device_struct(struct usb_device *usb_dev,
  405. struct kovaplus_device *kovaplus)
  406. {
  407. int retval, i;
  408. static uint wait = 70; /* device will freeze with just 60 */
  409. mutex_init(&kovaplus->kovaplus_lock);
  410. retval = kovaplus_get_info(usb_dev, &kovaplus->info);
  411. if (retval)
  412. return retval;
  413. for (i = 0; i < 5; ++i) {
  414. msleep(wait);
  415. retval = kovaplus_get_profile_settings(usb_dev,
  416. &kovaplus->profile_settings[i], i);
  417. if (retval)
  418. return retval;
  419. msleep(wait);
  420. retval = kovaplus_get_profile_buttons(usb_dev,
  421. &kovaplus->profile_buttons[i], i);
  422. if (retval)
  423. return retval;
  424. }
  425. msleep(wait);
  426. retval = kovaplus_get_actual_profile(usb_dev);
  427. if (retval < 0)
  428. return retval;
  429. kovaplus_profile_activated(kovaplus, retval);
  430. return 0;
  431. }
  432. static int kovaplus_init_specials(struct hid_device *hdev)
  433. {
  434. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  435. struct usb_device *usb_dev = interface_to_usbdev(intf);
  436. struct kovaplus_device *kovaplus;
  437. int retval;
  438. if (intf->cur_altsetting->desc.bInterfaceProtocol
  439. == USB_INTERFACE_PROTOCOL_MOUSE) {
  440. kovaplus = kzalloc(sizeof(*kovaplus), GFP_KERNEL);
  441. if (!kovaplus) {
  442. hid_err(hdev, "can't alloc device descriptor\n");
  443. return -ENOMEM;
  444. }
  445. hid_set_drvdata(hdev, kovaplus);
  446. retval = kovaplus_init_kovaplus_device_struct(usb_dev, kovaplus);
  447. if (retval) {
  448. hid_err(hdev, "couldn't init struct kovaplus_device\n");
  449. goto exit_free;
  450. }
  451. retval = roccat_connect(kovaplus_class, hdev,
  452. sizeof(struct kovaplus_roccat_report));
  453. if (retval < 0) {
  454. hid_err(hdev, "couldn't init char dev\n");
  455. } else {
  456. kovaplus->chrdev_minor = retval;
  457. kovaplus->roccat_claimed = 1;
  458. }
  459. } else {
  460. hid_set_drvdata(hdev, NULL);
  461. }
  462. return 0;
  463. exit_free:
  464. kfree(kovaplus);
  465. return retval;
  466. }
  467. static void kovaplus_remove_specials(struct hid_device *hdev)
  468. {
  469. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  470. struct kovaplus_device *kovaplus;
  471. if (intf->cur_altsetting->desc.bInterfaceProtocol
  472. == USB_INTERFACE_PROTOCOL_MOUSE) {
  473. kovaplus = hid_get_drvdata(hdev);
  474. if (kovaplus->roccat_claimed)
  475. roccat_disconnect(kovaplus->chrdev_minor);
  476. kfree(kovaplus);
  477. }
  478. }
  479. static int kovaplus_probe(struct hid_device *hdev,
  480. const struct hid_device_id *id)
  481. {
  482. int retval;
  483. retval = hid_parse(hdev);
  484. if (retval) {
  485. hid_err(hdev, "parse failed\n");
  486. goto exit;
  487. }
  488. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  489. if (retval) {
  490. hid_err(hdev, "hw start failed\n");
  491. goto exit;
  492. }
  493. retval = kovaplus_init_specials(hdev);
  494. if (retval) {
  495. hid_err(hdev, "couldn't install mouse\n");
  496. goto exit_stop;
  497. }
  498. return 0;
  499. exit_stop:
  500. hid_hw_stop(hdev);
  501. exit:
  502. return retval;
  503. }
  504. static void kovaplus_remove(struct hid_device *hdev)
  505. {
  506. kovaplus_remove_specials(hdev);
  507. hid_hw_stop(hdev);
  508. }
  509. static void kovaplus_keep_values_up_to_date(struct kovaplus_device *kovaplus,
  510. u8 const *data)
  511. {
  512. struct kovaplus_mouse_report_button const *button_report;
  513. if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON)
  514. return;
  515. button_report = (struct kovaplus_mouse_report_button const *)data;
  516. switch (button_report->type) {
  517. case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1:
  518. kovaplus_profile_activated(kovaplus, button_report->data1 - 1);
  519. break;
  520. case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI:
  521. kovaplus->actual_cpi = kovaplus_convert_event_cpi(button_report->data1);
  522. break;
  523. case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SENSITIVITY:
  524. kovaplus->actual_x_sensitivity = button_report->data1;
  525. kovaplus->actual_y_sensitivity = button_report->data2;
  526. break;
  527. default:
  528. break;
  529. }
  530. }
  531. static void kovaplus_report_to_chrdev(struct kovaplus_device const *kovaplus,
  532. u8 const *data)
  533. {
  534. struct kovaplus_roccat_report roccat_report;
  535. struct kovaplus_mouse_report_button const *button_report;
  536. if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON)
  537. return;
  538. button_report = (struct kovaplus_mouse_report_button const *)data;
  539. if (button_report->type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_2)
  540. return;
  541. roccat_report.type = button_report->type;
  542. roccat_report.profile = kovaplus->actual_profile + 1;
  543. if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_MACRO ||
  544. roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SHORTCUT ||
  545. roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
  546. roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER)
  547. roccat_report.button = button_report->data1;
  548. else
  549. roccat_report.button = 0;
  550. if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI)
  551. roccat_report.data1 = kovaplus_convert_event_cpi(button_report->data1);
  552. else
  553. roccat_report.data1 = button_report->data1;
  554. roccat_report.data2 = button_report->data2;
  555. roccat_report_event(kovaplus->chrdev_minor,
  556. (uint8_t const *)&roccat_report);
  557. }
  558. static int kovaplus_raw_event(struct hid_device *hdev,
  559. struct hid_report *report, u8 *data, int size)
  560. {
  561. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  562. struct kovaplus_device *kovaplus = hid_get_drvdata(hdev);
  563. if (intf->cur_altsetting->desc.bInterfaceProtocol
  564. != USB_INTERFACE_PROTOCOL_MOUSE)
  565. return 0;
  566. if (kovaplus == NULL)
  567. return 0;
  568. kovaplus_keep_values_up_to_date(kovaplus, data);
  569. if (kovaplus->roccat_claimed)
  570. kovaplus_report_to_chrdev(kovaplus, data);
  571. return 0;
  572. }
  573. static const struct hid_device_id kovaplus_devices[] = {
  574. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KOVAPLUS) },
  575. { }
  576. };
  577. MODULE_DEVICE_TABLE(hid, kovaplus_devices);
  578. static struct hid_driver kovaplus_driver = {
  579. .name = "kovaplus",
  580. .id_table = kovaplus_devices,
  581. .probe = kovaplus_probe,
  582. .remove = kovaplus_remove,
  583. .raw_event = kovaplus_raw_event
  584. };
  585. static int __init kovaplus_init(void)
  586. {
  587. int retval;
  588. kovaplus_class = class_create(THIS_MODULE, "kovaplus");
  589. if (IS_ERR(kovaplus_class))
  590. return PTR_ERR(kovaplus_class);
  591. kovaplus_class->dev_attrs = kovaplus_attributes;
  592. kovaplus_class->dev_bin_attrs = kovaplus_bin_attributes;
  593. retval = hid_register_driver(&kovaplus_driver);
  594. if (retval)
  595. class_destroy(kovaplus_class);
  596. return retval;
  597. }
  598. static void __exit kovaplus_exit(void)
  599. {
  600. hid_unregister_driver(&kovaplus_driver);
  601. class_destroy(kovaplus_class);
  602. }
  603. module_init(kovaplus_init);
  604. module_exit(kovaplus_exit);
  605. MODULE_AUTHOR("Stefan Achatz");
  606. MODULE_DESCRIPTION("USB Roccat Kova[+] driver");
  607. MODULE_LICENSE("GPL v2");