hid-roccat-kone.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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 =
  227. container_of(kobj, struct device, kobj)->parent->parent;
  228. struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
  229. if (off >= sizeof(struct kone_settings))
  230. return 0;
  231. if (off + count > sizeof(struct kone_settings))
  232. count = sizeof(struct kone_settings) - off;
  233. mutex_lock(&kone->kone_lock);
  234. memcpy(buf, ((char const *)&kone->settings) + off, count);
  235. mutex_unlock(&kone->kone_lock);
  236. return count;
  237. }
  238. /*
  239. * Writing settings automatically activates startup_profile.
  240. * This function keeps values in kone_device up to date and assumes that in
  241. * case of error the old data is still valid
  242. */
  243. static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
  244. struct bin_attribute *attr, char *buf,
  245. loff_t off, size_t count) {
  246. struct device *dev =
  247. container_of(kobj, struct device, kobj)->parent->parent;
  248. struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
  249. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  250. int retval = 0, difference, old_profile;
  251. /* I need to get my data in one piece */
  252. if (off != 0 || count != sizeof(struct kone_settings))
  253. return -EINVAL;
  254. mutex_lock(&kone->kone_lock);
  255. difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings));
  256. if (difference) {
  257. retval = kone_set_settings(usb_dev,
  258. (struct kone_settings const *)buf);
  259. if (retval) {
  260. mutex_unlock(&kone->kone_lock);
  261. return retval;
  262. }
  263. old_profile = kone->settings.startup_profile;
  264. memcpy(&kone->settings, buf, sizeof(struct kone_settings));
  265. kone_profile_activated(kone, kone->settings.startup_profile);
  266. if (kone->settings.startup_profile != old_profile)
  267. kone_profile_report(kone, kone->settings.startup_profile);
  268. }
  269. mutex_unlock(&kone->kone_lock);
  270. return sizeof(struct kone_settings);
  271. }
  272. static ssize_t kone_sysfs_read_profilex(struct file *fp,
  273. struct kobject *kobj, struct bin_attribute *attr,
  274. char *buf, loff_t off, size_t count) {
  275. struct device *dev =
  276. container_of(kobj, struct device, kobj)->parent->parent;
  277. struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev));
  278. if (off >= sizeof(struct kone_profile))
  279. return 0;
  280. if (off + count > sizeof(struct kone_profile))
  281. count = sizeof(struct kone_profile) - off;
  282. mutex_lock(&kone->kone_lock);
  283. memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count);
  284. mutex_unlock(&kone->kone_lock);
  285. return count;
  286. }
  287. /* Writes data only if different to stored data */
  288. static ssize_t kone_sysfs_write_profilex(struct file *fp,
  289. struct kobject *kobj, struct bin_attribute *attr,
  290. char *buf, loff_t off, size_t count) {
  291. struct device *dev =
  292. container_of(kobj, struct device, 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. static ssize_t kone_sysfs_show_actual_profile(struct device *dev,
  316. struct device_attribute *attr, char *buf)
  317. {
  318. struct kone_device *kone =
  319. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  320. return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile);
  321. }
  322. static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
  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. static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
  331. /* weight is read each time, since we don't get informed when it's changed */
  332. static ssize_t kone_sysfs_show_weight(struct device *dev,
  333. struct device_attribute *attr, char *buf)
  334. {
  335. struct kone_device *kone;
  336. struct usb_device *usb_dev;
  337. int weight = 0;
  338. int retval;
  339. dev = dev->parent->parent;
  340. kone = hid_get_drvdata(dev_get_drvdata(dev));
  341. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  342. mutex_lock(&kone->kone_lock);
  343. retval = kone_get_weight(usb_dev, &weight);
  344. mutex_unlock(&kone->kone_lock);
  345. if (retval)
  346. return retval;
  347. return snprintf(buf, PAGE_SIZE, "%d\n", weight);
  348. }
  349. static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
  350. static ssize_t kone_sysfs_show_firmware_version(struct device *dev,
  351. struct device_attribute *attr, char *buf)
  352. {
  353. struct kone_device *kone =
  354. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  355. return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version);
  356. }
  357. static DEVICE_ATTR(firmware_version, 0440, kone_sysfs_show_firmware_version,
  358. NULL);
  359. static ssize_t kone_sysfs_show_tcu(struct device *dev,
  360. struct device_attribute *attr, char *buf)
  361. {
  362. struct kone_device *kone =
  363. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  364. return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu);
  365. }
  366. static int kone_tcu_command(struct usb_device *usb_dev, int number)
  367. {
  368. unsigned char value;
  369. value = number;
  370. return kone_send(usb_dev, kone_command_calibrate, &value, 1);
  371. }
  372. /*
  373. * Calibrating the tcu is the only action that changes settings data inside the
  374. * mouse, so this data needs to be reread
  375. */
  376. static ssize_t kone_sysfs_set_tcu(struct device *dev,
  377. struct device_attribute *attr, char const *buf, size_t size)
  378. {
  379. struct kone_device *kone;
  380. struct usb_device *usb_dev;
  381. int retval;
  382. unsigned long state;
  383. dev = dev->parent->parent;
  384. kone = hid_get_drvdata(dev_get_drvdata(dev));
  385. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  386. retval = strict_strtoul(buf, 10, &state);
  387. if (retval)
  388. return retval;
  389. if (state != 0 && state != 1)
  390. return -EINVAL;
  391. mutex_lock(&kone->kone_lock);
  392. if (state == 1) { /* state activate */
  393. retval = kone_tcu_command(usb_dev, 1);
  394. if (retval)
  395. goto exit_unlock;
  396. retval = kone_tcu_command(usb_dev, 2);
  397. if (retval)
  398. goto exit_unlock;
  399. ssleep(5); /* tcu needs this time for calibration */
  400. retval = kone_tcu_command(usb_dev, 3);
  401. if (retval)
  402. goto exit_unlock;
  403. retval = kone_tcu_command(usb_dev, 0);
  404. if (retval)
  405. goto exit_unlock;
  406. retval = kone_tcu_command(usb_dev, 4);
  407. if (retval)
  408. goto exit_unlock;
  409. /*
  410. * Kone needs this time to settle things.
  411. * Reading settings too early will result in invalid data.
  412. * Roccat's driver waits 1 sec, maybe this time could be
  413. * shortened.
  414. */
  415. ssleep(1);
  416. }
  417. /* calibration changes values in settings, so reread */
  418. retval = kone_get_settings(usb_dev, &kone->settings);
  419. if (retval)
  420. goto exit_no_settings;
  421. /* only write settings back if activation state is different */
  422. if (kone->settings.tcu != state) {
  423. kone->settings.tcu = state;
  424. kone_set_settings_checksum(&kone->settings);
  425. retval = kone_set_settings(usb_dev, &kone->settings);
  426. if (retval) {
  427. dev_err(&usb_dev->dev, "couldn't set tcu state\n");
  428. /*
  429. * try to reread valid settings into buffer overwriting
  430. * first error code
  431. */
  432. retval = kone_get_settings(usb_dev, &kone->settings);
  433. if (retval)
  434. goto exit_no_settings;
  435. goto exit_unlock;
  436. }
  437. /* calibration resets profile */
  438. kone_profile_activated(kone, kone->settings.startup_profile);
  439. }
  440. retval = size;
  441. exit_no_settings:
  442. dev_err(&usb_dev->dev, "couldn't read settings\n");
  443. exit_unlock:
  444. mutex_unlock(&kone->kone_lock);
  445. return retval;
  446. }
  447. static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
  448. static ssize_t kone_sysfs_show_startup_profile(struct device *dev,
  449. struct device_attribute *attr, char *buf)
  450. {
  451. struct kone_device *kone =
  452. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  453. return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile);
  454. }
  455. static ssize_t kone_sysfs_set_startup_profile(struct device *dev,
  456. struct device_attribute *attr, char const *buf, size_t size)
  457. {
  458. struct kone_device *kone;
  459. struct usb_device *usb_dev;
  460. int retval;
  461. unsigned long new_startup_profile;
  462. dev = dev->parent->parent;
  463. kone = hid_get_drvdata(dev_get_drvdata(dev));
  464. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  465. retval = strict_strtoul(buf, 10, &new_startup_profile);
  466. if (retval)
  467. return retval;
  468. if (new_startup_profile < 1 || new_startup_profile > 5)
  469. return -EINVAL;
  470. mutex_lock(&kone->kone_lock);
  471. kone->settings.startup_profile = new_startup_profile;
  472. kone_set_settings_checksum(&kone->settings);
  473. retval = kone_set_settings(usb_dev, &kone->settings);
  474. if (retval) {
  475. mutex_unlock(&kone->kone_lock);
  476. return retval;
  477. }
  478. /* changing the startup profile immediately activates this profile */
  479. kone_profile_activated(kone, new_startup_profile);
  480. kone_profile_report(kone, new_startup_profile);
  481. mutex_unlock(&kone->kone_lock);
  482. return size;
  483. }
  484. static DEVICE_ATTR(startup_profile, 0660, kone_sysfs_show_startup_profile,
  485. kone_sysfs_set_startup_profile);
  486. static struct attribute *kone_attrs[] = {
  487. /*
  488. * Read actual dpi settings.
  489. * Returns raw value for further processing. Refer to enum
  490. * kone_polling_rates to get real value.
  491. */
  492. &dev_attr_actual_dpi.attr,
  493. &dev_attr_actual_profile.attr,
  494. /*
  495. * The mouse can be equipped with one of four supplied weights from 5
  496. * to 20 grams which are recognized and its value can be read out.
  497. * This returns the raw value reported by the mouse for easy evaluation
  498. * by software. Refer to enum kone_weights to get corresponding real
  499. * weight.
  500. */
  501. &dev_attr_weight.attr,
  502. /*
  503. * Prints firmware version stored in mouse as integer.
  504. * The raw value reported by the mouse is returned for easy evaluation,
  505. * to get the real version number the decimal point has to be shifted 2
  506. * positions to the left. E.g. a value of 138 means 1.38.
  507. */
  508. &dev_attr_firmware_version.attr,
  509. /*
  510. * Prints state of Tracking Control Unit as number where 0 = off and
  511. * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and
  512. * activates the tcu
  513. */
  514. &dev_attr_tcu.attr,
  515. /* Prints and takes the number of the profile the mouse starts with */
  516. &dev_attr_startup_profile.attr,
  517. NULL,
  518. };
  519. ATTRIBUTE_GROUPS(kone);
  520. static struct bin_attribute kone_bin_attributes[] = {
  521. {
  522. .attr = { .name = "settings", .mode = 0660 },
  523. .size = sizeof(struct kone_settings),
  524. .read = kone_sysfs_read_settings,
  525. .write = kone_sysfs_write_settings
  526. },
  527. {
  528. .attr = { .name = "profile1", .mode = 0660 },
  529. .size = sizeof(struct kone_profile),
  530. .read = kone_sysfs_read_profilex,
  531. .write = kone_sysfs_write_profilex,
  532. .private = &profile_numbers[0]
  533. },
  534. {
  535. .attr = { .name = "profile2", .mode = 0660 },
  536. .size = sizeof(struct kone_profile),
  537. .read = kone_sysfs_read_profilex,
  538. .write = kone_sysfs_write_profilex,
  539. .private = &profile_numbers[1]
  540. },
  541. {
  542. .attr = { .name = "profile3", .mode = 0660 },
  543. .size = sizeof(struct kone_profile),
  544. .read = kone_sysfs_read_profilex,
  545. .write = kone_sysfs_write_profilex,
  546. .private = &profile_numbers[2]
  547. },
  548. {
  549. .attr = { .name = "profile4", .mode = 0660 },
  550. .size = sizeof(struct kone_profile),
  551. .read = kone_sysfs_read_profilex,
  552. .write = kone_sysfs_write_profilex,
  553. .private = &profile_numbers[3]
  554. },
  555. {
  556. .attr = { .name = "profile5", .mode = 0660 },
  557. .size = sizeof(struct kone_profile),
  558. .read = kone_sysfs_read_profilex,
  559. .write = kone_sysfs_write_profilex,
  560. .private = &profile_numbers[4]
  561. },
  562. __ATTR_NULL
  563. };
  564. static int kone_init_kone_device_struct(struct usb_device *usb_dev,
  565. struct kone_device *kone)
  566. {
  567. uint i;
  568. int retval;
  569. mutex_init(&kone->kone_lock);
  570. for (i = 0; i < 5; ++i) {
  571. retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
  572. if (retval)
  573. return retval;
  574. }
  575. retval = kone_get_settings(usb_dev, &kone->settings);
  576. if (retval)
  577. return retval;
  578. retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
  579. if (retval)
  580. return retval;
  581. kone_profile_activated(kone, kone->settings.startup_profile);
  582. return 0;
  583. }
  584. /*
  585. * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
  586. * mousepart if usb_hid is compiled into the kernel and kone is compiled as
  587. * module.
  588. * Secial behaviour is bound only to mousepart since only mouseevents contain
  589. * additional notifications.
  590. */
  591. static int kone_init_specials(struct hid_device *hdev)
  592. {
  593. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  594. struct usb_device *usb_dev = interface_to_usbdev(intf);
  595. struct kone_device *kone;
  596. int retval;
  597. if (intf->cur_altsetting->desc.bInterfaceProtocol
  598. == USB_INTERFACE_PROTOCOL_MOUSE) {
  599. kone = kzalloc(sizeof(*kone), GFP_KERNEL);
  600. if (!kone) {
  601. hid_err(hdev, "can't alloc device descriptor\n");
  602. return -ENOMEM;
  603. }
  604. hid_set_drvdata(hdev, kone);
  605. retval = kone_init_kone_device_struct(usb_dev, kone);
  606. if (retval) {
  607. hid_err(hdev, "couldn't init struct kone_device\n");
  608. goto exit_free;
  609. }
  610. retval = roccat_connect(kone_class, hdev,
  611. sizeof(struct kone_roccat_report));
  612. if (retval < 0) {
  613. hid_err(hdev, "couldn't init char dev\n");
  614. /* be tolerant about not getting chrdev */
  615. } else {
  616. kone->roccat_claimed = 1;
  617. kone->chrdev_minor = retval;
  618. }
  619. } else {
  620. hid_set_drvdata(hdev, NULL);
  621. }
  622. return 0;
  623. exit_free:
  624. kfree(kone);
  625. return retval;
  626. }
  627. static void kone_remove_specials(struct hid_device *hdev)
  628. {
  629. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  630. struct kone_device *kone;
  631. if (intf->cur_altsetting->desc.bInterfaceProtocol
  632. == USB_INTERFACE_PROTOCOL_MOUSE) {
  633. kone = hid_get_drvdata(hdev);
  634. if (kone->roccat_claimed)
  635. roccat_disconnect(kone->chrdev_minor);
  636. kfree(hid_get_drvdata(hdev));
  637. }
  638. }
  639. static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
  640. {
  641. int retval;
  642. retval = hid_parse(hdev);
  643. if (retval) {
  644. hid_err(hdev, "parse failed\n");
  645. goto exit;
  646. }
  647. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  648. if (retval) {
  649. hid_err(hdev, "hw start failed\n");
  650. goto exit;
  651. }
  652. retval = kone_init_specials(hdev);
  653. if (retval) {
  654. hid_err(hdev, "couldn't install mouse\n");
  655. goto exit_stop;
  656. }
  657. return 0;
  658. exit_stop:
  659. hid_hw_stop(hdev);
  660. exit:
  661. return retval;
  662. }
  663. static void kone_remove(struct hid_device *hdev)
  664. {
  665. kone_remove_specials(hdev);
  666. hid_hw_stop(hdev);
  667. }
  668. /* handle special events and keep actual profile and dpi values up to date */
  669. static void kone_keep_values_up_to_date(struct kone_device *kone,
  670. struct kone_mouse_event const *event)
  671. {
  672. switch (event->event) {
  673. case kone_mouse_event_switch_profile:
  674. kone->actual_dpi = kone->profiles[event->value - 1].
  675. startup_dpi;
  676. case kone_mouse_event_osd_profile:
  677. kone->actual_profile = event->value;
  678. break;
  679. case kone_mouse_event_switch_dpi:
  680. case kone_mouse_event_osd_dpi:
  681. kone->actual_dpi = event->value;
  682. break;
  683. }
  684. }
  685. static void kone_report_to_chrdev(struct kone_device const *kone,
  686. struct kone_mouse_event const *event)
  687. {
  688. struct kone_roccat_report roccat_report;
  689. switch (event->event) {
  690. case kone_mouse_event_switch_profile:
  691. case kone_mouse_event_switch_dpi:
  692. case kone_mouse_event_osd_profile:
  693. case kone_mouse_event_osd_dpi:
  694. roccat_report.event = event->event;
  695. roccat_report.value = event->value;
  696. roccat_report.key = 0;
  697. roccat_report_event(kone->chrdev_minor,
  698. (uint8_t *)&roccat_report);
  699. break;
  700. case kone_mouse_event_call_overlong_macro:
  701. case kone_mouse_event_multimedia:
  702. if (event->value == kone_keystroke_action_press) {
  703. roccat_report.event = event->event;
  704. roccat_report.value = kone->actual_profile;
  705. roccat_report.key = event->macro_key;
  706. roccat_report_event(kone->chrdev_minor,
  707. (uint8_t *)&roccat_report);
  708. }
  709. break;
  710. }
  711. }
  712. /*
  713. * Is called for keyboard- and mousepart.
  714. * Only mousepart gets informations about special events in its extended event
  715. * structure.
  716. */
  717. static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
  718. u8 *data, int size)
  719. {
  720. struct kone_device *kone = hid_get_drvdata(hdev);
  721. struct kone_mouse_event *event = (struct kone_mouse_event *)data;
  722. /* keyboard events are always processed by default handler */
  723. if (size != sizeof(struct kone_mouse_event))
  724. return 0;
  725. if (kone == NULL)
  726. return 0;
  727. /*
  728. * Firmware 1.38 introduced new behaviour for tilt and special buttons.
  729. * Pressed button is reported in each movement event.
  730. * Workaround sends only one event per press.
  731. */
  732. if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
  733. memcpy(&kone->last_mouse_event, event,
  734. sizeof(struct kone_mouse_event));
  735. else
  736. memset(&event->tilt, 0, 5);
  737. kone_keep_values_up_to_date(kone, event);
  738. if (kone->roccat_claimed)
  739. kone_report_to_chrdev(kone, event);
  740. return 0; /* always do further processing */
  741. }
  742. static const struct hid_device_id kone_devices[] = {
  743. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
  744. { }
  745. };
  746. MODULE_DEVICE_TABLE(hid, kone_devices);
  747. static struct hid_driver kone_driver = {
  748. .name = "kone",
  749. .id_table = kone_devices,
  750. .probe = kone_probe,
  751. .remove = kone_remove,
  752. .raw_event = kone_raw_event
  753. };
  754. static int __init kone_init(void)
  755. {
  756. int retval;
  757. /* class name has to be same as driver name */
  758. kone_class = class_create(THIS_MODULE, "kone");
  759. if (IS_ERR(kone_class))
  760. return PTR_ERR(kone_class);
  761. kone_class->dev_groups = kone_groups;
  762. kone_class->dev_bin_attrs = kone_bin_attributes;
  763. retval = hid_register_driver(&kone_driver);
  764. if (retval)
  765. class_destroy(kone_class);
  766. return retval;
  767. }
  768. static void __exit kone_exit(void)
  769. {
  770. hid_unregister_driver(&kone_driver);
  771. class_destroy(kone_class);
  772. }
  773. module_init(kone_init);
  774. module_exit(kone_exit);
  775. MODULE_AUTHOR("Stefan Achatz");
  776. MODULE_DESCRIPTION("USB Roccat Kone driver");
  777. MODULE_LICENSE("GPL v2");