hid-roccat-kone.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. * Read actual dpi settings.
  517. * Returns raw value for further processing. Refer to enum kone_polling_rates to
  518. * get real value.
  519. */
  520. static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL);
  521. static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL);
  522. /*
  523. * The mouse can be equipped with one of four supplied weights from 5 to 20
  524. * grams which are recognized and its value can be read out.
  525. * This returns the raw value reported by the mouse for easy evaluation by
  526. * software. Refer to enum kone_weights to get corresponding real weight.
  527. */
  528. static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL);
  529. /*
  530. * Prints firmware version stored in mouse as integer.
  531. * The raw value reported by the mouse is returned for easy evaluation, to get
  532. * the real version number the decimal point has to be shifted 2 positions to
  533. * the left. E.g. a value of 138 means 1.38.
  534. */
  535. static DEVICE_ATTR(firmware_version, 0440,
  536. kone_sysfs_show_firmware_version, NULL);
  537. /*
  538. * Prints state of Tracking Control Unit as number where 0 = off and 1 = on
  539. * Writing 0 deactivates tcu and writing 1 calibrates and activates the tcu
  540. */
  541. static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu);
  542. /* Prints and takes the number of the profile the mouse starts with */
  543. static DEVICE_ATTR(startup_profile, 0660,
  544. kone_sysfs_show_startup_profile,
  545. kone_sysfs_set_startup_profile);
  546. static struct attribute *kone_attributes[] = {
  547. &dev_attr_actual_dpi.attr,
  548. &dev_attr_actual_profile.attr,
  549. &dev_attr_weight.attr,
  550. &dev_attr_firmware_version.attr,
  551. &dev_attr_tcu.attr,
  552. &dev_attr_startup_profile.attr,
  553. NULL
  554. };
  555. static struct attribute_group kone_attribute_group = {
  556. .attrs = kone_attributes
  557. };
  558. static struct bin_attribute kone_settings_attr = {
  559. .attr = { .name = "settings", .mode = 0660 },
  560. .size = sizeof(struct kone_settings),
  561. .read = kone_sysfs_read_settings,
  562. .write = kone_sysfs_write_settings
  563. };
  564. static struct bin_attribute kone_profile1_attr = {
  565. .attr = { .name = "profile1", .mode = 0660 },
  566. .size = sizeof(struct kone_profile),
  567. .read = kone_sysfs_read_profile1,
  568. .write = kone_sysfs_write_profile1
  569. };
  570. static struct bin_attribute kone_profile2_attr = {
  571. .attr = { .name = "profile2", .mode = 0660 },
  572. .size = sizeof(struct kone_profile),
  573. .read = kone_sysfs_read_profile2,
  574. .write = kone_sysfs_write_profile2
  575. };
  576. static struct bin_attribute kone_profile3_attr = {
  577. .attr = { .name = "profile3", .mode = 0660 },
  578. .size = sizeof(struct kone_profile),
  579. .read = kone_sysfs_read_profile3,
  580. .write = kone_sysfs_write_profile3
  581. };
  582. static struct bin_attribute kone_profile4_attr = {
  583. .attr = { .name = "profile4", .mode = 0660 },
  584. .size = sizeof(struct kone_profile),
  585. .read = kone_sysfs_read_profile4,
  586. .write = kone_sysfs_write_profile4
  587. };
  588. static struct bin_attribute kone_profile5_attr = {
  589. .attr = { .name = "profile5", .mode = 0660 },
  590. .size = sizeof(struct kone_profile),
  591. .read = kone_sysfs_read_profile5,
  592. .write = kone_sysfs_write_profile5
  593. };
  594. static int kone_create_sysfs_attributes(struct usb_interface *intf)
  595. {
  596. int retval;
  597. retval = sysfs_create_group(&intf->dev.kobj, &kone_attribute_group);
  598. if (retval)
  599. goto exit_1;
  600. retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_settings_attr);
  601. if (retval)
  602. goto exit_2;
  603. retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile1_attr);
  604. if (retval)
  605. goto exit_3;
  606. retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile2_attr);
  607. if (retval)
  608. goto exit_4;
  609. retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile3_attr);
  610. if (retval)
  611. goto exit_5;
  612. retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile4_attr);
  613. if (retval)
  614. goto exit_6;
  615. retval = sysfs_create_bin_file(&intf->dev.kobj, &kone_profile5_attr);
  616. if (retval)
  617. goto exit_7;
  618. return 0;
  619. exit_7:
  620. sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
  621. exit_6:
  622. sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
  623. exit_5:
  624. sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
  625. exit_4:
  626. sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
  627. exit_3:
  628. sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
  629. exit_2:
  630. sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
  631. exit_1:
  632. return retval;
  633. }
  634. static void kone_remove_sysfs_attributes(struct usb_interface *intf)
  635. {
  636. sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile5_attr);
  637. sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile4_attr);
  638. sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile3_attr);
  639. sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile2_attr);
  640. sysfs_remove_bin_file(&intf->dev.kobj, &kone_profile1_attr);
  641. sysfs_remove_bin_file(&intf->dev.kobj, &kone_settings_attr);
  642. sysfs_remove_group(&intf->dev.kobj, &kone_attribute_group);
  643. }
  644. static int kone_init_kone_device_struct(struct usb_device *usb_dev,
  645. struct kone_device *kone)
  646. {
  647. uint i;
  648. int retval;
  649. mutex_init(&kone->kone_lock);
  650. for (i = 0; i < 5; ++i) {
  651. retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1);
  652. if (retval)
  653. return retval;
  654. }
  655. retval = kone_get_settings(usb_dev, &kone->settings);
  656. if (retval)
  657. return retval;
  658. retval = kone_get_firmware_version(usb_dev, &kone->firmware_version);
  659. if (retval)
  660. return retval;
  661. kone->actual_profile = kone->settings.startup_profile;
  662. kone->actual_dpi = kone->profiles[kone->actual_profile].startup_dpi;
  663. return 0;
  664. }
  665. /*
  666. * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to
  667. * mousepart if usb_hid is compiled into the kernel and kone is compiled as
  668. * module.
  669. * Secial behaviour is bound only to mousepart since only mouseevents contain
  670. * additional notifications.
  671. */
  672. static int kone_init_specials(struct hid_device *hdev)
  673. {
  674. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  675. struct usb_device *usb_dev = interface_to_usbdev(intf);
  676. struct kone_device *kone;
  677. int retval;
  678. if (intf->cur_altsetting->desc.bInterfaceProtocol
  679. == USB_INTERFACE_PROTOCOL_MOUSE) {
  680. kone = kzalloc(sizeof(*kone), GFP_KERNEL);
  681. if (!kone) {
  682. dev_err(&hdev->dev, "can't alloc device descriptor\n");
  683. return -ENOMEM;
  684. }
  685. hid_set_drvdata(hdev, kone);
  686. retval = kone_init_kone_device_struct(usb_dev, kone);
  687. if (retval) {
  688. dev_err(&hdev->dev,
  689. "couldn't init struct kone_device\n");
  690. goto exit_free;
  691. }
  692. retval = roccat_connect(hdev);
  693. if (retval < 0) {
  694. dev_err(&hdev->dev, "couldn't init char dev\n");
  695. /* be tolerant about not getting chrdev */
  696. } else {
  697. kone->roccat_claimed = 1;
  698. kone->chrdev_minor = retval;
  699. }
  700. retval = kone_create_sysfs_attributes(intf);
  701. if (retval) {
  702. dev_err(&hdev->dev, "cannot create sysfs files\n");
  703. goto exit_free;
  704. }
  705. } else {
  706. hid_set_drvdata(hdev, NULL);
  707. }
  708. return 0;
  709. exit_free:
  710. kfree(kone);
  711. return retval;
  712. }
  713. static void kone_remove_specials(struct hid_device *hdev)
  714. {
  715. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  716. struct kone_device *kone;
  717. if (intf->cur_altsetting->desc.bInterfaceProtocol
  718. == USB_INTERFACE_PROTOCOL_MOUSE) {
  719. kone_remove_sysfs_attributes(intf);
  720. kone = hid_get_drvdata(hdev);
  721. if (kone->roccat_claimed)
  722. roccat_disconnect(kone->chrdev_minor);
  723. kfree(hid_get_drvdata(hdev));
  724. }
  725. }
  726. static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id)
  727. {
  728. int retval;
  729. retval = hid_parse(hdev);
  730. if (retval) {
  731. dev_err(&hdev->dev, "parse failed\n");
  732. goto exit;
  733. }
  734. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  735. if (retval) {
  736. dev_err(&hdev->dev, "hw start failed\n");
  737. goto exit;
  738. }
  739. retval = kone_init_specials(hdev);
  740. if (retval) {
  741. dev_err(&hdev->dev, "couldn't install mouse\n");
  742. goto exit_stop;
  743. }
  744. return 0;
  745. exit_stop:
  746. hid_hw_stop(hdev);
  747. exit:
  748. return retval;
  749. }
  750. static void kone_remove(struct hid_device *hdev)
  751. {
  752. kone_remove_specials(hdev);
  753. hid_hw_stop(hdev);
  754. }
  755. /* handle special events and keep actual profile and dpi values up to date */
  756. static void kone_keep_values_up_to_date(struct kone_device *kone,
  757. struct kone_mouse_event const *event)
  758. {
  759. switch (event->event) {
  760. case kone_mouse_event_switch_profile:
  761. case kone_mouse_event_osd_profile:
  762. kone->actual_profile = event->value;
  763. kone->actual_dpi = kone->profiles[kone->actual_profile - 1].
  764. startup_dpi;
  765. break;
  766. case kone_mouse_event_switch_dpi:
  767. case kone_mouse_event_osd_dpi:
  768. kone->actual_dpi = event->value;
  769. break;
  770. }
  771. }
  772. static void kone_report_to_chrdev(struct kone_device const *kone,
  773. struct kone_mouse_event const *event)
  774. {
  775. struct kone_roccat_report roccat_report;
  776. switch (event->event) {
  777. case kone_mouse_event_switch_profile:
  778. case kone_mouse_event_switch_dpi:
  779. case kone_mouse_event_osd_profile:
  780. case kone_mouse_event_osd_dpi:
  781. roccat_report.event = event->event;
  782. roccat_report.value = event->value;
  783. roccat_report.key = 0;
  784. roccat_report_event(kone->chrdev_minor,
  785. (uint8_t *)&roccat_report,
  786. sizeof(struct kone_roccat_report));
  787. break;
  788. case kone_mouse_event_call_overlong_macro:
  789. if (event->value == kone_keystroke_action_press) {
  790. roccat_report.event = kone_mouse_event_call_overlong_macro;
  791. roccat_report.value = kone->actual_profile;
  792. roccat_report.key = event->macro_key;
  793. roccat_report_event(kone->chrdev_minor,
  794. (uint8_t *)&roccat_report,
  795. sizeof(struct kone_roccat_report));
  796. }
  797. break;
  798. }
  799. }
  800. /*
  801. * Is called for keyboard- and mousepart.
  802. * Only mousepart gets informations about special events in its extended event
  803. * structure.
  804. */
  805. static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
  806. u8 *data, int size)
  807. {
  808. struct kone_device *kone = hid_get_drvdata(hdev);
  809. struct kone_mouse_event *event = (struct kone_mouse_event *)data;
  810. /* keyboard events are always processed by default handler */
  811. if (size != sizeof(struct kone_mouse_event))
  812. return 0;
  813. /*
  814. * Firmware 1.38 introduced new behaviour for tilt and special buttons.
  815. * Pressed button is reported in each movement event.
  816. * Workaround sends only one event per press.
  817. */
  818. if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5))
  819. memcpy(&kone->last_mouse_event, event,
  820. sizeof(struct kone_mouse_event));
  821. else
  822. memset(&event->tilt, 0, 5);
  823. kone_keep_values_up_to_date(kone, event);
  824. if (kone->roccat_claimed)
  825. kone_report_to_chrdev(kone, event);
  826. return 0; /* always do further processing */
  827. }
  828. static const struct hid_device_id kone_devices[] = {
  829. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) },
  830. { }
  831. };
  832. MODULE_DEVICE_TABLE(hid, kone_devices);
  833. static struct hid_driver kone_driver = {
  834. .name = "kone",
  835. .id_table = kone_devices,
  836. .probe = kone_probe,
  837. .remove = kone_remove,
  838. .raw_event = kone_raw_event
  839. };
  840. static int __init kone_init(void)
  841. {
  842. return hid_register_driver(&kone_driver);
  843. }
  844. static void __exit kone_exit(void)
  845. {
  846. hid_unregister_driver(&kone_driver);
  847. }
  848. module_init(kone_init);
  849. module_exit(kone_exit);
  850. MODULE_AUTHOR("Stefan Achatz");
  851. MODULE_DESCRIPTION("USB Roccat Kone driver");
  852. MODULE_LICENSE("GPL v2");