hid-roccat-kone.c 24 KB

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