hid-roccat-kone.c 24 KB

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