hid-roccat-kone.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Roccat Kone driver for Linux
  4. *
  5. * Copyright (c) 2010 Stefan Achatz <[email protected]>
  6. */
  7. /*
  8. */
  9. /*
  10. * Roccat Kone is a gamer mouse which consists of a mouse part and a keyboard
  11. * part. The keyboard part enables the mouse to execute stored macros with mixed
  12. * key- and button-events.
  13. *
  14. * TODO implement on-the-fly polling-rate change
  15. * The windows driver has the ability to change the polling rate of the
  16. * device on the press of a mousebutton.
  17. * Is it possible to remove and reinstall the urb in raw-event- or any
  18. * other handler, or to defer this action to be executed somewhere else?
  19. *
  20. * TODO is it possible to overwrite group for sysfs attributes via udev?
  21. */
  22. #include <linux/device.h>
  23. #include <linux/input.h>
  24. #include <linux/hid.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/hid-roccat.h>
  28. #include "hid-ids.h"
  29. #include "hid-roccat-common.h"
  30. #include "hid-roccat-kone.h"
  31. static uint profile_numbers[5] = {0, 1, 2, 3, 4};
  32. static void kone_profile_activated(struct kone_device *kone, uint new_profile)
  33. {
  34. kone->actual_profile = new_profile;
  35. kone->actual_dpi = kone->profiles[new_profile - 1].startup_dpi;
  36. }
  37. static void kone_profile_report(struct kone_device *kone, uint new_profile)
  38. {
  39. struct kone_roccat_report roccat_report;
  40. roccat_report.event = kone_mouse_event_switch_profile;
  41. roccat_report.value = new_profile;
  42. roccat_report.key = 0;
  43. roccat_report_event(kone->chrdev_minor, (uint8_t *)&roccat_report);
  44. }
  45. static int kone_receive(struct usb_device *usb_dev, uint usb_command,
  46. void *data, uint size)
  47. {
  48. char *buf;
  49. int len;
  50. buf = kmalloc(size, GFP_KERNEL);
  51. if (buf == NULL)
  52. return -ENOMEM;
  53. len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  54. HID_REQ_GET_REPORT,
  55. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  56. usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
  57. memcpy(data, buf, size);
  58. kfree(buf);
  59. return ((len < 0) ? len : ((len != size) ? -EIO : 0));
  60. }
  61. static int kone_send(struct usb_device *usb_dev, uint usb_command,
  62. void const *data, uint size)
  63. {
  64. char *buf;
  65. int len;
  66. buf = kmemdup(data, size, GFP_KERNEL);
  67. if (buf == NULL)
  68. return -ENOMEM;
  69. len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  70. HID_REQ_SET_REPORT,
  71. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  72. usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
  73. kfree(buf);
  74. return ((len < 0) ? len : ((len != size) ? -EIO : 0));
  75. }
  76. /* kone_class is used for creating sysfs attributes via roccat char device */
  77. static struct class *kone_class;
  78. static void kone_set_settings_checksum(struct kone_settings *settings)
  79. {
  80. uint16_t checksum = 0;
  81. unsigned char *address = (unsigned char *)settings;
  82. int i;
  83. for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
  84. checksum += *address;
  85. settings->checksum = cpu_to_le16(checksum);
  86. }
  87. /*
  88. * Checks success after writing data to mouse
  89. * On success returns 0
  90. * On failure returns errno
  91. */
  92. static int kone_check_write(struct usb_device *usb_dev)
  93. {
  94. int retval;
  95. uint8_t data;
  96. do {
  97. /*
  98. * Mouse needs 50 msecs until it says ok, but there are
  99. * 30 more msecs needed for next write to work.
  100. */
  101. msleep(80);
  102. retval = kone_receive(usb_dev,
  103. kone_command_confirm_write, &data, 1);
  104. if (retval)
  105. return retval;
  106. /*
  107. * value of 3 seems to mean something like
  108. * "not finished yet, but it looks good"
  109. * So check again after a moment.
  110. */
  111. } while (data == 3);
  112. if (data == 1) /* everything alright */
  113. return 0;
  114. /* unknown answer */
  115. dev_err(&usb_dev->dev, "got retval %d when checking write\n", data);
  116. return -EIO;
  117. }
  118. /*
  119. * Reads settings from mouse and stores it in @buf
  120. * On success returns 0
  121. * On failure returns errno
  122. */
  123. static int kone_get_settings(struct usb_device *usb_dev,
  124. struct kone_settings *buf)
  125. {
  126. return kone_receive(usb_dev, kone_command_settings, buf,
  127. sizeof(struct kone_settings));
  128. }
  129. /*
  130. * Writes settings from @buf to mouse
  131. * On success returns 0
  132. * On failure returns errno
  133. */
  134. static int kone_set_settings(struct usb_device *usb_dev,
  135. struct kone_settings const *settings)
  136. {
  137. int retval;
  138. retval = kone_send(usb_dev, kone_command_settings,
  139. settings, sizeof(struct kone_settings));
  140. if (retval)
  141. return retval;
  142. return kone_check_write(usb_dev);
  143. }
  144. /*
  145. * Reads profile data from mouse and stores it in @buf
  146. * @number: profile number to read
  147. * On success returns 0
  148. * On failure returns errno
  149. */
  150. static int kone_get_profile(struct usb_device *usb_dev,
  151. struct kone_profile *buf, int number)
  152. {
  153. int len;
  154. if (number < 1 || number > 5)
  155. return -EINVAL;
  156. len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  157. USB_REQ_CLEAR_FEATURE,
  158. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  159. kone_command_profile, number, buf,
  160. sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
  161. if (len != sizeof(struct kone_profile))
  162. return -EIO;
  163. return 0;
  164. }
  165. /*
  166. * Writes profile data to mouse.
  167. * @number: profile number to write
  168. * On success returns 0
  169. * On failure returns errno
  170. */
  171. static int kone_set_profile(struct usb_device *usb_dev,
  172. struct kone_profile const *profile, int number)
  173. {
  174. int len;
  175. if (number < 1 || number > 5)
  176. return -EINVAL;
  177. len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  178. USB_REQ_SET_CONFIGURATION,
  179. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  180. kone_command_profile, number, (void *)profile,
  181. sizeof(struct kone_profile),
  182. USB_CTRL_SET_TIMEOUT);
  183. if (len != sizeof(struct kone_profile))
  184. return len;
  185. if (kone_check_write(usb_dev))
  186. return -EIO;
  187. return 0;
  188. }
  189. /*
  190. * Reads value of "fast-clip-weight" and stores it in @result
  191. * On success returns 0
  192. * On failure returns errno
  193. */
  194. static int kone_get_weight(struct usb_device *usb_dev, int *result)
  195. {
  196. int retval;
  197. uint8_t data;
  198. retval = kone_receive(usb_dev, kone_command_weight, &data, 1);
  199. if (retval)
  200. return retval;
  201. *result = (int)data;
  202. return 0;
  203. }
  204. /*
  205. * Reads firmware_version of mouse and stores it in @result
  206. * On success returns 0
  207. * On failure returns errno
  208. */
  209. static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
  210. {
  211. int retval;
  212. uint16_t data;
  213. retval = kone_receive(usb_dev, kone_command_firmware_version,
  214. &data, 2);
  215. if (retval)
  216. return retval;
  217. *result = le16_to_cpu(data);
  218. return 0;
  219. }
  220. static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
  221. struct bin_attribute *attr, char *buf,
  222. loff_t off, size_t count) {
  223. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  224. struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
  225. if (off >= sizeof(struct kone_settings))
  226. return 0;
  227. if (off + count > sizeof(struct kone_settings))
  228. count = sizeof(struct kone_settings) - off;
  229. mutex_lock(&kone->kone_lock);
  230. memcpy(buf, ((char const *)&kone->settings) + off, count);
  231. mutex_unlock(&kone->kone_lock);
  232. return count;
  233. }
  234. /*
  235. * Writing settings automatically activates startup_profile.
  236. * This function keeps values in kone_device up to date and assumes that in
  237. * case of error the old data is still valid
  238. */
  239. static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
  240. struct bin_attribute *attr, char *buf,
  241. loff_t off, size_t count) {
  242. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  243. struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
  244. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  245. int retval = 0, difference, old_profile;
  246. struct kone_settings *settings = (struct kone_settings *)buf;
  247. /* I need to get my data in one piece */
  248. if (off != 0 || count != sizeof(struct kone_settings))
  249. return -EINVAL;
  250. mutex_lock(&kone->kone_lock);
  251. difference = memcmp(settings, &kone->settings,
  252. sizeof(struct kone_settings));
  253. if (difference) {
  254. if (settings->startup_profile < 1 ||
  255. settings->startup_profile > 5) {
  256. retval = -EINVAL;
  257. goto unlock;
  258. }
  259. retval = kone_set_settings(usb_dev, settings);
  260. if (retval)
  261. goto unlock;
  262. old_profile = kone->settings.startup_profile;
  263. memcpy(&kone->settings, settings, sizeof(struct kone_settings));
  264. kone_profile_activated(kone, kone->settings.startup_profile);
  265. if (kone->settings.startup_profile != old_profile)
  266. kone_profile_report(kone, kone->settings.startup_profile);
  267. }
  268. unlock:
  269. mutex_unlock(&kone->kone_lock);
  270. if (retval)
  271. return retval;
  272. return sizeof(struct kone_settings);
  273. }
  274. static BIN_ATTR(settings, 0660, kone_sysfs_read_settings,
  275. kone_sysfs_write_settings, sizeof(struct kone_settings));
  276. static ssize_t kone_sysfs_read_profilex(struct file *fp,
  277. struct kobject *kobj, struct bin_attribute *attr,
  278. char *buf, loff_t off, size_t count) {
  279. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  280. struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
  281. if (off >= sizeof(struct kone_profile))
  282. return 0;
  283. if (off + count > sizeof(struct kone_profile))
  284. count = sizeof(struct kone_profile) - off;
  285. mutex_lock(&kone->kone_lock);
  286. memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
  287. mutex_unlock(&kone->kone_lock);
  288. return count;
  289. }
  290. /* Writes data only if different to stored data */
  291. static ssize_t kone_sysfs_write_profilex(struct file *fp,
  292. struct kobject *kobj, struct bin_attribute *attr,
  293. char *buf, loff_t off, size_t count) {
  294. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  295. struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
  296. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  297. struct kone_profile *profile;
  298. int retval = 0, difference;
  299. /* I need to get my data in one piece */
  300. if (off != 0 || count != sizeof(struct kone_profile))
  301. return -EINVAL;
  302. profile = &kone->profiles[*(uint *)(attr->private)];
  303. mutex_lock(&kone->kone_lock);
  304. difference = memcmp(buf, profile, sizeof(struct kone_profile));
  305. if (difference) {
  306. retval = kone_set_profile(usb_dev,
  307. (struct kone_profile const *)buf,
  308. *(uint *)(attr->private) + 1);
  309. if (!retval)
  310. memcpy(profile, buf, sizeof(struct kone_profile));
  311. }
  312. mutex_unlock(&kone->kone_lock);
  313. if (retval)
  314. return retval;
  315. return sizeof(struct kone_profile);
  316. }
  317. #define PROFILE_ATTR(number) \
  318. static struct bin_attribute bin_attr_profile##number = { \
  319. .attr = { .name = "profile" #number, .mode = 0660 }, \
  320. .size = sizeof(struct kone_profile), \
  321. .read = kone_sysfs_read_profilex, \
  322. .write = kone_sysfs_write_profilex, \
  323. .private = &profile_numbers[number-1], \
  324. }
  325. PROFILE_ATTR(1);
  326. PROFILE_ATTR(2);
  327. PROFILE_ATTR(3);
  328. PROFILE_ATTR(4);
  329. PROFILE_ATTR(5);
  330. static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
  331. struct device_attribute *attr, char *buf)
  332. {
  333. struct kone_device *kone =
  334. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  335. return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
  336. }
  337. static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
  338. static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
  339. struct device_attribute *attr, char *buf)
  340. {
  341. struct kone_device *kone =
  342. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  343. return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
  344. }
  345. static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
  346. /* weight is read each time, since we don't get informed when it's changed */
  347. static ssize_t kone_sysfs_show_weight(struct device *dev,
  348. struct device_attribute *attr, char *buf)
  349. {
  350. struct kone_device *kone;
  351. struct usb_device *usb_dev;
  352. int weight = 0;
  353. int retval;
  354. dev = dev->parent->parent;
  355. kone = hid_get_drvdata(dev_get_drvdata(dev));
  356. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  357. mutex_lock(&kone->kone_lock);
  358. retval = kone_get_weight(usb_dev, &weight);
  359. mutex_unlock(&kone->kone_lock);
  360. if (retval)
  361. return retval;
  362. return snprintf(buf, PAGE_SIZE, "%d\n", weight);
  363. }
  364. static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
  365. static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
  366. struct device_attribute *attr, char *buf)
  367. {
  368. struct kone_device *kone =
  369. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  370. return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
  371. }
  372. static DEVICE_ATTR(firmware_version, 0440, kone_sysfs_show_firmware_version,
  373. NULL);
  374. static ssize_t kone_sysfs_show_tcu(struct device *dev,
  375. struct device_attribute *attr, char *buf)
  376. {
  377. struct kone_device *kone =
  378. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  379. return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
  380. }
  381. static int kone_tcu_command(struct usb_device *usb_dev, int number)
  382. {
  383. unsigned char value;
  384. value = number;
  385. return kone_send(usb_dev, kone_command_calibrate, &value, 1);
  386. }
  387. /*
  388. * Calibrating the tcu is the only action that changes settings data inside the
  389. * mouse, so this data needs to be reread
  390. */
  391. static ssize_t kone_sysfs_set_tcu(struct device *dev,
  392. struct device_attribute *attr, char const *buf, size_t size)
  393. {
  394. struct kone_device *kone;
  395. struct usb_device *usb_dev;
  396. int retval;
  397. unsigned long state;
  398. dev = dev->parent->parent;
  399. kone = hid_get_drvdata(dev_get_drvdata(dev));
  400. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  401. retval = kstrtoul(buf, 10, &state);
  402. if (retval)
  403. return retval;
  404. if (state != 0 && state != 1)
  405. return -EINVAL;
  406. mutex_lock(&kone->kone_lock);
  407. if (state == 1) { /* state activate */
  408. retval = kone_tcu_command(usb_dev, 1);
  409. if (retval)
  410. goto exit_unlock;
  411. retval = kone_tcu_command(usb_dev, 2);
  412. if (retval)
  413. goto exit_unlock;
  414. ssleep(5); /* tcu needs this time for calibration */
  415. retval = kone_tcu_command(usb_dev, 3);
  416. if (retval)
  417. goto exit_unlock;
  418. retval = kone_tcu_command(usb_dev, 0);
  419. if (retval)
  420. goto exit_unlock;
  421. retval = kone_tcu_command(usb_dev, 4);
  422. if (retval)
  423. goto exit_unlock;
  424. /*
  425. * Kone needs this time to settle things.
  426. * Reading settings too early will result in invalid data.
  427. * Roccat's driver waits 1 sec, maybe this time could be
  428. * shortened.
  429. */
  430. ssleep(1);
  431. }
  432. /* calibration changes values in settings, so reread */
  433. retval = kone_get_settings(usb_dev, &kone->settings);
  434. if (retval)
  435. goto exit_no_settings;
  436. /* only write settings back if activation state is different */
  437. if (kone->settings.tcu != state) {
  438. kone->settings.tcu = state;
  439. kone_set_settings_checksum(&kone->settings);
  440. retval = kone_set_settings(usb_dev, &kone->settings);
  441. if (retval) {
  442. dev_err(&usb_dev->dev, "couldn't set tcu state\n");
  443. /*
  444. * try to reread valid settings into buffer overwriting
  445. * first error code
  446. */
  447. retval = kone_get_settings(usb_dev, &kone->settings);
  448. if (retval)
  449. goto exit_no_settings;
  450. goto exit_unlock;
  451. }
  452. /* calibration resets profile */
  453. kone_profile_activated(kone, kone->settings.startup_profile);
  454. }
  455. retval = size;
  456. exit_no_settings:
  457. dev_err(&usb_dev->dev, "couldn't read settings\n");
  458. exit_unlock:
  459. mutex_unlock(&kone->kone_lock);
  460. return retval;
  461. }
  462. static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
  463. static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
  464. struct device_attribute *attr, char *buf)
  465. {
  466. struct kone_device *kone =
  467. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  468. return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
  469. }
  470. static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
  471. struct device_attribute *attr, char const *buf, size_t size)
  472. {
  473. struct kone_device *kone;
  474. struct usb_device *usb_dev;
  475. int retval;
  476. unsigned long new_startup_profile;
  477. dev = dev->parent->parent;
  478. kone = hid_get_drvdata(dev_get_drvdata(dev));
  479. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  480. retval = kstrtoul(buf, 10, &new_startup_profile);
  481. if (retval)
  482. return retval;
  483. if (new_startup_profile < 1 || new_startup_profile > 5)
  484. return -EINVAL;
  485. mutex_lock(&kone->kone_lock);
  486. kone->settings.startup_profile = new_startup_profile;
  487. kone_set_settings_checksum(&kone->settings);
  488. retval = kone_set_settings(usb_dev, &kone->settings);
  489. if (retval) {
  490. mutex_unlock(&kone->kone_lock);
  491. return retval;
  492. }
  493. /* changing the startup profile immediately activates this profile */
  494. kone_profile_activated(kone, new_startup_profile);
  495. kone_profile_report(kone, new_startup_profile);
  496. mutex_unlock(&kone->kone_lock);
  497. return size;
  498. }
  499. static DEVICE_ATTR(startup_profile, 0660, kone_sysfs_show_startup_profile,
  500. kone_sysfs_set_startup_profile);
  501. static struct attribute *kone_attrs[] = {
  502. /*
  503. * Read actual dpi settings.
  504. * Returns raw value for further processing. Refer to enum
  505. * kone_polling_rates to get real value.
  506. */
  507. &dev_attr_actual_dpi.attr,
  508. &dev_attr_actual_profile.attr,
  509. /*
  510. * The mouse can be equipped with one of four supplied weights from 5
  511. * to 20 grams which are recognized and its value can be read out.
  512. * This returns the raw value reported by the mouse for easy evaluation
  513. * by software. Refer to enum kone_weights to get corresponding real
  514. * weight.
  515. */
  516. &dev_attr_weight.attr,
  517. /*
  518. * Prints firmware version stored in mouse as integer.
  519. * The raw value reported by the mouse is returned for easy evaluation,
  520. * to get the real version number the decimal point has to be shifted 2
  521. * positions to the left. E.g. a value of 138 means 1.38.
  522. */
  523. &dev_attr_firmware_version.attr,
  524. /*
  525. * Prints state of Tracking Control Unit as number where 0 = off and
  526. * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
  527. * activates the tcu
  528. */
  529. &dev_attr_tcu.attr,
  530. /* Prints and takes the number of the profile the mouse starts with */
  531. &dev_attr_startup_profile.attr,
  532. NULL,
  533. };
  534. static struct bin_attribute *kone_bin_attributes[] = {
  535. &bin_attr_settings,
  536. &bin_attr_profile1,
  537. &bin_attr_profile2,
  538. &bin_attr_profile3,
  539. &bin_attr_profile4,
  540. &bin_attr_profile5,
  541. NULL,
  542. };
  543. static const struct attribute_group kone_group = {
  544. .attrs = kone_attrs,
  545. .bin_attrs = kone_bin_attributes,
  546. };
  547. static const struct attribute_group *kone_groups[] = {
  548. &kone_group,
  549. NULL,
  550. };
  551. static int kone_init_kone_device_struct(struct usb_device *usb_dev,
  552. struct kone_device *kone)
  553. {
  554. uint i;
  555. int retval;
  556. mutex_init(&kone->kone_lock);
  557. for (i = 0; i < 5; ++i) {
  558. retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
  559. if (retval)
  560. return retval;
  561. }
  562. retval = kone_get_settings(usb_dev, &kone->settings);
  563. if (retval)
  564. return retval;
  565. retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
  566. if (retval)
  567. return retval;
  568. kone_profile_activated(kone, kone->settings.startup_profile);
  569. return 0;
  570. }
  571. /*
  572. * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
  573. * mousepart if usb_hid is compiled into the kernel and kone is compiled as
  574. * module.
  575. * Secial behaviour is bound only to mousepart since only mouseevents contain
  576. * additional notifications.
  577. */
  578. static int kone_init_specials(struct hid_device *hdev)
  579. {
  580. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  581. struct usb_device *usb_dev = interface_to_usbdev(intf);
  582. struct kone_device *kone;
  583. int retval;
  584. if (intf->cur_altsetting->desc.bInterfaceProtocol
  585. == USB_INTERFACE_PROTOCOL_MOUSE) {
  586. kone = kzalloc(sizeof(*kone), GFP_KERNEL);
  587. if (!kone)
  588. return -ENOMEM;
  589. hid_set_drvdata(hdev, kone);
  590. retval = kone_init_kone_device_struct(usb_dev, kone);
  591. if (retval) {
  592. hid_err(hdev, "couldn't init struct kone_device\n");
  593. goto exit_free;
  594. }
  595. retval = roccat_connect(kone_class, hdev,
  596. sizeof(struct kone_roccat_report));
  597. if (retval < 0) {
  598. hid_err(hdev, "couldn't init char dev\n");
  599. /* be tolerant about not getting chrdev */
  600. } else {
  601. kone->roccat_claimed = 1;
  602. kone->chrdev_minor = retval;
  603. }
  604. } else {
  605. hid_set_drvdata(hdev, NULL);
  606. }
  607. return 0;
  608. exit_free:
  609. kfree(kone);
  610. return retval;
  611. }
  612. static void kone_remove_specials(struct hid_device *hdev)
  613. {
  614. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  615. struct kone_device *kone;
  616. if (intf->cur_altsetting->desc.bInterfaceProtocol
  617. == USB_INTERFACE_PROTOCOL_MOUSE) {
  618. kone = hid_get_drvdata(hdev);
  619. if (kone->roccat_claimed)
  620. roccat_disconnect(kone->chrdev_minor);
  621. kfree(hid_get_drvdata(hdev));
  622. }
  623. }
  624. static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
  625. {
  626. int retval;
  627. if (!hid_is_usb(hdev))
  628. return -EINVAL;
  629. retval = hid_parse(hdev);
  630. if (retval) {
  631. hid_err(hdev, "parse failed\n");
  632. goto exit;
  633. }
  634. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  635. if (retval) {
  636. hid_err(hdev, "hw start failed\n");
  637. goto exit;
  638. }
  639. retval = kone_init_specials(hdev);
  640. if (retval) {
  641. hid_err(hdev, "couldn't install mouse\n");
  642. goto exit_stop;
  643. }
  644. return 0;
  645. exit_stop:
  646. hid_hw_stop(hdev);
  647. exit:
  648. return retval;
  649. }
  650. static void kone_remove(struct hid_device *hdev)
  651. {
  652. kone_remove_specials(hdev);
  653. hid_hw_stop(hdev);
  654. }
  655. /* handle special events and keep actual profile and dpi values up to date */
  656. static void kone_keep_values_up_to_date(struct kone_device *kone,
  657. struct kone_mouse_event const *event)
  658. {
  659. switch (event->event) {
  660. case kone_mouse_event_switch_profile:
  661. kone->actual_dpi = kone->profiles[event->value - 1].
  662. startup_dpi;
  663. fallthrough;
  664. case kone_mouse_event_osd_profile:
  665. kone->actual_profile = event->value;
  666. break;
  667. case kone_mouse_event_switch_dpi:
  668. case kone_mouse_event_osd_dpi:
  669. kone->actual_dpi = event->value;
  670. break;
  671. }
  672. }
  673. static void kone_report_to_chrdev(struct kone_device const *kone,
  674. struct kone_mouse_event const *event)
  675. {
  676. struct kone_roccat_report roccat_report;
  677. switch (event->event) {
  678. case kone_mouse_event_switch_profile:
  679. case kone_mouse_event_switch_dpi:
  680. case kone_mouse_event_osd_profile:
  681. case kone_mouse_event_osd_dpi:
  682. roccat_report.event = event->event;
  683. roccat_report.value = event->value;
  684. roccat_report.key = 0;
  685. roccat_report_event(kone->chrdev_minor,
  686. (uint8_t *)&roccat_report);
  687. break;
  688. case kone_mouse_event_call_overlong_macro:
  689. case kone_mouse_event_multimedia:
  690. if (event->value == kone_keystroke_action_press) {
  691. roccat_report.event = event->event;
  692. roccat_report.value = kone->actual_profile;
  693. roccat_report.key = event->macro_key;
  694. roccat_report_event(kone->chrdev_minor,
  695. (uint8_t *)&roccat_report);
  696. }
  697. break;
  698. }
  699. }
  700. /*
  701. * Is called for keyboard- and mousepart.
  702. * Only mousepart gets informations about special events in its extended event
  703. * structure.
  704. */
  705. static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
  706. u8 *data, int size)
  707. {
  708. struct kone_device *kone = hid_get_drvdata(hdev);
  709. struct kone_mouse_event *event = (struct kone_mouse_event *)data;
  710. /* keyboard events are always processed by default handler */
  711. if (size != sizeof(struct kone_mouse_event))
  712. return 0;
  713. if (kone == NULL)
  714. return 0;
  715. /*
  716. * Firmware 1.38 introduced new behaviour for tilt and special buttons.
  717. * Pressed button is reported in each movement event.
  718. * Workaround sends only one event per press.
  719. */
  720. if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
  721. memcpy(&kone->last_mouse_event, event,
  722. sizeof(struct kone_mouse_event));
  723. else
  724. memset(&event->wipe, 0, sizeof(event->wipe));
  725. kone_keep_values_up_to_date(kone, event);
  726. if (kone->roccat_claimed)
  727. kone_report_to_chrdev(kone, event);
  728. return 0; /* always do further processing */
  729. }
  730. static const struct hid_device_id kone_devices[] = {
  731. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
  732. { }
  733. };
  734. MODULE_DEVICE_TABLE(hid, kone_devices);
  735. static struct hid_driver kone_driver = {
  736. .name = "kone",
  737. .id_table = kone_devices,
  738. .probe = kone_probe,
  739. .remove = kone_remove,
  740. .raw_event = kone_raw_event
  741. };
  742. static int __init kone_init(void)
  743. {
  744. int retval;
  745. /* class name has to be same as driver name */
  746. kone_class = class_create(THIS_MODULE, "kone");
  747. if (IS_ERR(kone_class))
  748. return PTR_ERR(kone_class);
  749. kone_class->dev_groups = kone_groups;
  750. retval = hid_register_driver(&kone_driver);
  751. if (retval)
  752. class_destroy(kone_class);
  753. return retval;
  754. }
  755. static void __exit kone_exit(void)
  756. {
  757. hid_unregister_driver(&kone_driver);
  758. class_destroy(kone_class);
  759. }
  760. module_init(kone_init);
  761. module_exit(kone_exit);
  762. MODULE_AUTHOR("Stefan Achatz");
  763. MODULE_DESCRIPTION("USB Roccat Kone driver");
  764. MODULE_LICENSE("GPL v2");