hid-roccat-kone.c 24 KB

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