hid-roccat-kone.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*
  2. * Roccat Kone driver for Linux
  3. *
  4. * Copyright (c) 2010 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 Kone is a gamer mouse which consists of a mouse part and a keyboard
  14. * part. The keyboard part enables the mouse to execute stored macros with mixed
  15. * key- and button-events.
  16. *
  17. * TODO implement on-the-fly polling-rate change
  18. * The windows driver has the ability to change the polling rate of the
  19. * device on the press of a mousebutton.
  20. * Is it possible to remove and reinstall the urb in raw-event- or any
  21. * other handler, or to defer this action to be executed somewhere else?
  22. *
  23. * TODO is it possible to overwrite group for sysfs attributes via udev?
  24. */
  25. #include <linux/device.h>
  26. #include <linux/input.h>
  27. #include <linux/hid.h>
  28. #include <linux/module.h>
  29. #include <linux/slab.h>
  30. #include <linux/hid-roccat.h>
  31. #include "hid-ids.h"
  32. #include "hid-roccat-common.h"
  33. #include "hid-roccat-kone.h"
  34. static uint profile_numbers[5] = {0, 1, 2, 3, 4};
  35. static void kone_profile_activated(struct kone_device *kone, uint new_profile)
  36. {
  37. kone->actual_profile = new_profile;
  38. kone->actual_dpi = kone->profiles[new_profile - 1].startup_dpi;
  39. }
  40. static void kone_profile_report(struct kone_device *kone, uint new_profile)
  41. {
  42. struct kone_roccat_report roccat_report;
  43. roccat_report.event = kone_mouse_event_switch_profile;
  44. roccat_report.value = new_profile;
  45. roccat_report.key = 0;
  46. roccat_report_event(kone->chrdev_minor, (uint8_t *)&roccat_report);
  47. }
  48. static int kone_receive(struct usb_device *usb_dev, uint usb_command,
  49. void *data, uint size)
  50. {
  51. char *buf;
  52. int len;
  53. buf = kmalloc(size, GFP_KERNEL);
  54. if (buf == NULL)
  55. return -ENOMEM;
  56. len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  57. HID_REQ_GET_REPORT,
  58. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  59. usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
  60. memcpy(data, buf, size);
  61. kfree(buf);
  62. return ((len < 0) ? len : ((len != size) ? -EIO : 0));
  63. }
  64. static int kone_send(struct usb_device *usb_dev, uint usb_command,
  65. void const *data, uint size)
  66. {
  67. char *buf;
  68. int len;
  69. buf = kmemdup(data, size, GFP_KERNEL);
  70. if (buf == NULL)
  71. return -ENOMEM;
  72. len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  73. HID_REQ_SET_REPORT,
  74. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  75. usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT);
  76. kfree(buf);
  77. return ((len < 0) ? len : ((len != size) ? -EIO : 0));
  78. }
  79. /* kone_class is used for creating sysfs attributes via roccat char device */
  80. static struct class *kone_class;
  81. static void kone_set_settings_checksum(struct kone_settings *settings)
  82. {
  83. uint16_t checksum = 0;
  84. unsigned char *address = (unsigned char *)settings;
  85. int i;
  86. for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address)
  87. checksum += *address;
  88. settings->checksum = cpu_to_le16(checksum);
  89. }
  90. /*
  91. * Checks success after writing data to mouse
  92. * On success returns 0
  93. * On failure returns errno
  94. */
  95. static int kone_check_write(struct usb_device *usb_dev)
  96. {
  97. int retval;
  98. uint8_t data;
  99. do {
  100. /*
  101. * Mouse needs 50 msecs until it says ok, but there are
  102. * 30 more msecs needed for next write to work.
  103. */
  104. msleep(80);
  105. retval = kone_receive(usb_dev,
  106. kone_command_confirm_write, &data, 1);
  107. if (retval)
  108. return retval;
  109. /*
  110. * value of 3 seems to mean something like
  111. * "not finished yet, but it looks good"
  112. * So check again after a moment.
  113. */
  114. } while (data == 3);
  115. if (data == 1) /* everything alright */
  116. return 0;
  117. /* unknown answer */
  118. dev_err(&usb_dev->dev, "got retval %d when checking write\n", data);
  119. return -EIO;
  120. }
  121. /*
  122. * Reads settings from mouse and stores it in @buf
  123. * On success returns 0
  124. * On failure returns errno
  125. */
  126. static int kone_get_settings(struct usb_device *usb_dev,
  127. struct kone_settings *buf)
  128. {
  129. return kone_receive(usb_dev, kone_command_settings, buf,
  130. sizeof(struct kone_settings));
  131. }
  132. /*
  133. * Writes settings from @buf to mouse
  134. * On success returns 0
  135. * On failure returns errno
  136. */
  137. static int kone_set_settings(struct usb_device *usb_dev,
  138. struct kone_settings const *settings)
  139. {
  140. int retval;
  141. retval = kone_send(usb_dev, kone_command_settings,
  142. settings, sizeof(struct kone_settings));
  143. if (retval)
  144. return retval;
  145. return kone_check_write(usb_dev);
  146. }
  147. /*
  148. * Reads profile data from mouse and stores it in @buf
  149. * @number: profile number to read
  150. * On success returns 0
  151. * On failure returns errno
  152. */
  153. static int kone_get_profile(struct usb_device *usb_dev,
  154. struct kone_profile *buf, int number)
  155. {
  156. int len;
  157. if (number < 1 || number > 5)
  158. return -EINVAL;
  159. len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  160. USB_REQ_CLEAR_FEATURE,
  161. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  162. kone_command_profile, number, buf,
  163. sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT);
  164. if (len != sizeof(struct kone_profile))
  165. return -EIO;
  166. return 0;
  167. }
  168. /*
  169. * Writes profile data to mouse.
  170. * @number: profile number to write
  171. * On success returns 0
  172. * On failure returns errno
  173. */
  174. static int kone_set_profile(struct usb_device *usb_dev,
  175. struct kone_profile const *profile, int number)
  176. {
  177. int len;
  178. if (number < 1 || number > 5)
  179. return -EINVAL;
  180. len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  181. USB_REQ_SET_CONFIGURATION,
  182. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  183. kone_command_profile, number, (void *)profile,
  184. sizeof(struct kone_profile),
  185. USB_CTRL_SET_TIMEOUT);
  186. if (len != sizeof(struct kone_profile))
  187. return len;
  188. if (kone_check_write(usb_dev))
  189. return -EIO;
  190. return 0;
  191. }
  192. /*
  193. * Reads value of "fast-clip-weight" and stores it in @result
  194. * On success returns 0
  195. * On failure returns errno
  196. */
  197. static int kone_get_weight(struct usb_device *usb_dev, int *result)
  198. {
  199. int retval;
  200. uint8_t data;
  201. retval = kone_receive(usb_dev, kone_command_weight, &data, 1);
  202. if (retval)
  203. return retval;
  204. *result = (int)data;
  205. return 0;
  206. }
  207. /*
  208. * Reads firmware_version of mouse and stores it in @result
  209. * On success returns 0
  210. * On failure returns errno
  211. */
  212. static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
  213. {
  214. int retval;
  215. uint16_t data;
  216. retval = kone_receive(usb_dev, kone_command_firmware_version,
  217. &data, 2);
  218. if (retval)
  219. return retval;
  220. *result = le16_to_cpu(data);
  221. return 0;
  222. }
  223. static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
  224. struct bin_attribute *attr, char *buf,
  225. loff_t off, size_t count) {
  226. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  227. struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
  228. if (off >= sizeof(struct kone_settings))
  229. return 0;
  230. if (off + count > sizeof(struct kone_settings))
  231. count = sizeof(struct kone_settings) - off;
  232. mutex_lock(&kone->kone_lock);
  233. memcpy(buf, ((char const *)&kone->settings) + off, count);
  234. mutex_unlock(&kone->kone_lock);
  235. return count;
  236. }
  237. /*
  238. * Writing settings automatically activates startup_profile.
  239. * This function keeps values in kone_device up to date and assumes that in
  240. * case of error the old data is still valid
  241. */
  242. static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
  243. struct bin_attribute *attr, char *buf,
  244. loff_t off, size_t count) {
  245. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  246. struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
  247. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  248. int retval = 0, difference, old_profile;
  249. struct kone_settings *settings = (struct kone_settings *)buf;
  250. /* I need to get my data in one piece */
  251. if (off != 0 || count != sizeof(struct kone_settings))
  252. return -EINVAL;
  253. mutex_lock(&kone->kone_lock);
  254. difference = memcmp(settings, &kone->settings,
  255. sizeof(struct kone_settings));
  256. if (difference) {
  257. if (settings->startup_profile < 1 ||
  258. settings->startup_profile > 5) {
  259. retval = -EINVAL;
  260. goto unlock;
  261. }
  262. retval = kone_set_settings(usb_dev, settings);
  263. if (retval)
  264. goto unlock;
  265. old_profile = kone->settings.startup_profile;
  266. memcpy(&kone->settings, settings, sizeof(struct kone_settings));
  267. kone_profile_activated(kone, kone->settings.startup_profile);
  268. if (kone->settings.startup_profile != old_profile)
  269. kone_profile_report(kone, kone->settings.startup_profile);
  270. }
  271. unlock:
  272. mutex_unlock(&kone->kone_lock);
  273. if (retval)
  274. return retval;
  275. return sizeof(struct kone_settings);
  276. }
  277. static BIN_ATTR(settings, 0660, kone_sysfs_read_settings,
  278. kone_sysfs_write_settings, sizeof(struct kone_settings));
  279. static ssize_t kone_sysfs_read_profilex(struct file *fp,
  280. struct kobject *kobj, struct bin_attribute *attr,
  281. char *buf, loff_t off, size_t count) {
  282. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  283. struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
  284. if (off >= sizeof(struct kone_profile))
  285. return 0;
  286. if (off + count > sizeof(struct kone_profile))
  287. count = sizeof(struct kone_profile) - off;
  288. mutex_lock(&kone->kone_lock);
  289. memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
  290. mutex_unlock(&kone->kone_lock);
  291. return count;
  292. }
  293. /* Writes data only if different to stored data */
  294. static ssize_t kone_sysfs_write_profilex(struct file *fp,
  295. struct kobject *kobj, struct bin_attribute *attr,
  296. char *buf, loff_t off, size_t count) {
  297. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  298. struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
  299. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  300. struct kone_profile *profile;
  301. int retval = 0, difference;
  302. /* I need to get my data in one piece */
  303. if (off != 0 || count != sizeof(struct kone_profile))
  304. return -EINVAL;
  305. profile = &kone->profiles[*(uint *)(attr->private)];
  306. mutex_lock(&kone->kone_lock);
  307. difference = memcmp(buf, profile, sizeof(struct kone_profile));
  308. if (difference) {
  309. retval = kone_set_profile(usb_dev,
  310. (struct kone_profile const *)buf,
  311. *(uint *)(attr->private) + 1);
  312. if (!retval)
  313. memcpy(profile, buf, sizeof(struct kone_profile));
  314. }
  315. mutex_unlock(&kone->kone_lock);
  316. if (retval)
  317. return retval;
  318. return sizeof(struct kone_profile);
  319. }
  320. #define PROFILE_ATTR(number) \
  321. static struct bin_attribute bin_attr_profile##number = { \
  322. .attr = { .name = "profile" #number, .mode = 0660 }, \
  323. .size = sizeof(struct kone_profile), \
  324. .read = kone_sysfs_read_profilex, \
  325. .write = kone_sysfs_write_profilex, \
  326. .private = &profile_numbers[number-1], \
  327. }
  328. PROFILE_ATTR(1);
  329. PROFILE_ATTR(2);
  330. PROFILE_ATTR(3);
  331. PROFILE_ATTR(4);
  332. PROFILE_ATTR(5);
  333. static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
  334. struct device_attribute *attr, char *buf)
  335. {
  336. struct kone_device *kone =
  337. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  338. return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
  339. }
  340. static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
  341. static ssize_t kone_sysfs_show_actual_dpi(struct device *dev,
  342. struct device_attribute *attr, char *buf)
  343. {
  344. struct kone_device *kone =
  345. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  346. return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi);
  347. }
  348. static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
  349. /* weight is read each time, since we don't get informed when it's changed */
  350. static ssize_t kone_sysfs_show_weight(struct device *dev,
  351. struct device_attribute *attr, char *buf)
  352. {
  353. struct kone_device *kone;
  354. struct usb_device *usb_dev;
  355. int weight = 0;
  356. int retval;
  357. dev = dev->parent->parent;
  358. kone = hid_get_drvdata(dev_get_drvdata(dev));
  359. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  360. mutex_lock(&kone->kone_lock);
  361. retval = kone_get_weight(usb_dev, &weight);
  362. mutex_unlock(&kone->kone_lock);
  363. if (retval)
  364. return retval;
  365. return snprintf(buf, PAGE_SIZE, "%d\n", weight);
  366. }
  367. static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
  368. static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
  369. struct device_attribute *attr, char *buf)
  370. {
  371. struct kone_device *kone =
  372. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  373. return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
  374. }
  375. static DEVICE_ATTR(firmware_version, 0440, kone_sysfs_show_firmware_version,
  376. NULL);
  377. static ssize_t kone_sysfs_show_tcu(struct device *dev,
  378. struct device_attribute *attr, char *buf)
  379. {
  380. struct kone_device *kone =
  381. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  382. return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
  383. }
  384. static int kone_tcu_command(struct usb_device *usb_dev, int number)
  385. {
  386. unsigned char value;
  387. value = number;
  388. return kone_send(usb_dev, kone_command_calibrate, &value, 1);
  389. }
  390. /*
  391. * Calibrating the tcu is the only action that changes settings data inside the
  392. * mouse, so this data needs to be reread
  393. */
  394. static ssize_t kone_sysfs_set_tcu(struct device *dev,
  395. struct device_attribute *attr, char const *buf, size_t size)
  396. {
  397. struct kone_device *kone;
  398. struct usb_device *usb_dev;
  399. int retval;
  400. unsigned long state;
  401. dev = dev->parent->parent;
  402. kone = hid_get_drvdata(dev_get_drvdata(dev));
  403. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  404. retval = kstrtoul(buf, 10, &state);
  405. if (retval)
  406. return retval;
  407. if (state != 0 && state != 1)
  408. return -EINVAL;
  409. mutex_lock(&kone->kone_lock);
  410. if (state == 1) { /* state activate */
  411. retval = kone_tcu_command(usb_dev, 1);
  412. if (retval)
  413. goto exit_unlock;
  414. retval = kone_tcu_command(usb_dev, 2);
  415. if (retval)
  416. goto exit_unlock;
  417. ssleep(5); /* tcu needs this time for calibration */
  418. retval = kone_tcu_command(usb_dev, 3);
  419. if (retval)
  420. goto exit_unlock;
  421. retval = kone_tcu_command(usb_dev, 0);
  422. if (retval)
  423. goto exit_unlock;
  424. retval = kone_tcu_command(usb_dev, 4);
  425. if (retval)
  426. goto exit_unlock;
  427. /*
  428. * Kone needs this time to settle things.
  429. * Reading settings too early will result in invalid data.
  430. * Roccat's driver waits 1 sec, maybe this time could be
  431. * shortened.
  432. */
  433. ssleep(1);
  434. }
  435. /* calibration changes values in settings, so reread */
  436. retval = kone_get_settings(usb_dev, &kone->settings);
  437. if (retval)
  438. goto exit_no_settings;
  439. /* only write settings back if activation state is different */
  440. if (kone->settings.tcu != state) {
  441. kone->settings.tcu = state;
  442. kone_set_settings_checksum(&kone->settings);
  443. retval = kone_set_settings(usb_dev, &kone->settings);
  444. if (retval) {
  445. dev_err(&usb_dev->dev, "couldn't set tcu state\n");
  446. /*
  447. * try to reread valid settings into buffer overwriting
  448. * first error code
  449. */
  450. retval = kone_get_settings(usb_dev, &kone->settings);
  451. if (retval)
  452. goto exit_no_settings;
  453. goto exit_unlock;
  454. }
  455. /* calibration resets profile */
  456. kone_profile_activated(kone, kone->settings.startup_profile);
  457. }
  458. retval = size;
  459. exit_no_settings:
  460. dev_err(&usb_dev->dev, "couldn't read settings\n");
  461. exit_unlock:
  462. mutex_unlock(&kone->kone_lock);
  463. return retval;
  464. }
  465. static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
  466. static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
  467. struct device_attribute *attr, char *buf)
  468. {
  469. struct kone_device *kone =
  470. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  471. return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
  472. }
  473. static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
  474. struct device_attribute *attr, char const *buf, size_t size)
  475. {
  476. struct kone_device *kone;
  477. struct usb_device *usb_dev;
  478. int retval;
  479. unsigned long new_startup_profile;
  480. dev = dev->parent->parent;
  481. kone = hid_get_drvdata(dev_get_drvdata(dev));
  482. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  483. retval = kstrtoul(buf, 10, &new_startup_profile);
  484. if (retval)
  485. return retval;
  486. if (new_startup_profile < 1 || new_startup_profile > 5)
  487. return -EINVAL;
  488. mutex_lock(&kone->kone_lock);
  489. kone->settings.startup_profile = new_startup_profile;
  490. kone_set_settings_checksum(&kone->settings);
  491. retval = kone_set_settings(usb_dev, &kone->settings);
  492. if (retval) {
  493. mutex_unlock(&kone->kone_lock);
  494. return retval;
  495. }
  496. /* changing the startup profile immediately activates this profile */
  497. kone_profile_activated(kone, new_startup_profile);
  498. kone_profile_report(kone, new_startup_profile);
  499. mutex_unlock(&kone->kone_lock);
  500. return size;
  501. }
  502. static DEVICE_ATTR(startup_profile, 0660, kone_sysfs_show_startup_profile,
  503. kone_sysfs_set_startup_profile);
  504. static struct attribute *kone_attrs[] = {
  505. /*
  506. * Read actual dpi settings.
  507. * Returns raw value for further processing. Refer to enum
  508. * kone_polling_rates to get real value.
  509. */
  510. &dev_attr_actual_dpi.attr,
  511. &dev_attr_actual_profile.attr,
  512. /*
  513. * The mouse can be equipped with one of four supplied weights from 5
  514. * to 20 grams which are recognized and its value can be read out.
  515. * This returns the raw value reported by the mouse for easy evaluation
  516. * by software. Refer to enum kone_weights to get corresponding real
  517. * weight.
  518. */
  519. &dev_attr_weight.attr,
  520. /*
  521. * Prints firmware version stored in mouse as integer.
  522. * The raw value reported by the mouse is returned for easy evaluation,
  523. * to get the real version number the decimal point has to be shifted 2
  524. * positions to the left. E.g. a value of 138 means 1.38.
  525. */
  526. &dev_attr_firmware_version.attr,
  527. /*
  528. * Prints state of Tracking Control Unit as number where 0 = off and
  529. * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
  530. * activates the tcu
  531. */
  532. &dev_attr_tcu.attr,
  533. /* Prints and takes the number of the profile the mouse starts with */
  534. &dev_attr_startup_profile.attr,
  535. NULL,
  536. };
  537. static struct bin_attribute *kone_bin_attributes[] = {
  538. &bin_attr_settings,
  539. &bin_attr_profile1,
  540. &bin_attr_profile2,
  541. &bin_attr_profile3,
  542. &bin_attr_profile4,
  543. &bin_attr_profile5,
  544. NULL,
  545. };
  546. static const struct attribute_group kone_group = {
  547. .attrs = kone_attrs,
  548. .bin_attrs = kone_bin_attributes,
  549. };
  550. static const struct attribute_group *kone_groups[] = {
  551. &kone_group,
  552. NULL,
  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. retval = hid_parse(hdev);
  631. if (retval) {
  632. hid_err(hdev, "parse failed\n");
  633. goto exit;
  634. }
  635. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  636. if (retval) {
  637. hid_err(hdev, "hw start failed\n");
  638. goto exit;
  639. }
  640. retval = kone_init_specials(hdev);
  641. if (retval) {
  642. hid_err(hdev, "couldn't install mouse\n");
  643. goto exit_stop;
  644. }
  645. return 0;
  646. exit_stop:
  647. hid_hw_stop(hdev);
  648. exit:
  649. return retval;
  650. }
  651. static void kone_remove(struct hid_device *hdev)
  652. {
  653. kone_remove_specials(hdev);
  654. hid_hw_stop(hdev);
  655. }
  656. /* handle special events and keep actual profile and dpi values up to date */
  657. static void kone_keep_values_up_to_date(struct kone_device *kone,
  658. struct kone_mouse_event const *event)
  659. {
  660. switch (event->event) {
  661. case kone_mouse_event_switch_profile:
  662. kone->actual_dpi = kone->profiles[event->value - 1].
  663. startup_dpi;
  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->tilt, 0, 5);
  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");