hid-roccat-kone.c 26 KB

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