hid-roccat-kone.c 27 KB

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