hid-roccat-kone.c 22 KB

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