hid-roccat-kone.c 28 KB

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