hid-roccat-kone.c 23 KB

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