hid-roccat-kone.c 28 KB

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