hid-roccat-koneplus.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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 an updated/improved version of the Kone with more memory
  14. * and functionality and without the non-standard behaviours the Kone had.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/input.h>
  18. #include <linux/hid.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/hid-roccat.h>
  22. #include "hid-ids.h"
  23. #include "hid-roccat-common.h"
  24. #include "hid-roccat-koneplus.h"
  25. static uint profile_numbers[5] = {0, 1, 2, 3, 4};
  26. static struct class *koneplus_class;
  27. static void koneplus_profile_activated(struct koneplus_device *koneplus,
  28. uint new_profile)
  29. {
  30. koneplus->actual_profile = new_profile;
  31. }
  32. static int koneplus_send_control(struct usb_device *usb_dev, uint value,
  33. enum koneplus_control_requests request)
  34. {
  35. struct koneplus_control control;
  36. if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
  37. request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
  38. value > 4)
  39. return -EINVAL;
  40. control.command = KONEPLUS_COMMAND_CONTROL;
  41. control.value = value;
  42. control.request = request;
  43. return roccat_common_send(usb_dev, KONEPLUS_COMMAND_CONTROL,
  44. &control, sizeof(struct koneplus_control));
  45. }
  46. static int koneplus_receive_control_status(struct usb_device *usb_dev)
  47. {
  48. int retval;
  49. struct koneplus_control control;
  50. do {
  51. retval = roccat_common_receive(usb_dev, KONEPLUS_COMMAND_CONTROL,
  52. &control, sizeof(struct koneplus_control));
  53. /* check if we get a completely wrong answer */
  54. if (retval)
  55. return retval;
  56. if (control.value == KONEPLUS_CONTROL_REQUEST_STATUS_OK)
  57. return 0;
  58. /* indicates that hardware needs some more time to complete action */
  59. if (control.value == KONEPLUS_CONTROL_REQUEST_STATUS_WAIT) {
  60. msleep(500); /* windows driver uses 1000 */
  61. continue;
  62. }
  63. /* seems to be critical - replug necessary */
  64. if (control.value == KONEPLUS_CONTROL_REQUEST_STATUS_OVERLOAD)
  65. return -EINVAL;
  66. hid_err(usb_dev, "koneplus_receive_control_status: "
  67. "unknown response value 0x%x\n", control.value);
  68. return -EINVAL;
  69. } while (1);
  70. }
  71. static int koneplus_send(struct usb_device *usb_dev, uint command,
  72. void const *buf, uint size)
  73. {
  74. int retval;
  75. retval = roccat_common_send(usb_dev, command, buf, size);
  76. if (retval)
  77. return retval;
  78. return koneplus_receive_control_status(usb_dev);
  79. }
  80. static int koneplus_select_profile(struct usb_device *usb_dev, uint number,
  81. enum koneplus_control_requests request)
  82. {
  83. int retval;
  84. retval = koneplus_send_control(usb_dev, number, request);
  85. if (retval)
  86. return retval;
  87. /* allow time to settle things - windows driver uses 500 */
  88. msleep(100);
  89. retval = koneplus_receive_control_status(usb_dev);
  90. if (retval)
  91. return retval;
  92. return 0;
  93. }
  94. static int koneplus_get_info(struct usb_device *usb_dev,
  95. struct koneplus_info *buf)
  96. {
  97. return roccat_common_receive(usb_dev, KONEPLUS_COMMAND_INFO,
  98. buf, sizeof(struct koneplus_info));
  99. }
  100. static int koneplus_get_profile_settings(struct usb_device *usb_dev,
  101. struct koneplus_profile_settings *buf, uint number)
  102. {
  103. int retval;
  104. retval = koneplus_select_profile(usb_dev, number,
  105. KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
  106. if (retval)
  107. return retval;
  108. return roccat_common_receive(usb_dev, KONEPLUS_COMMAND_PROFILE_SETTINGS,
  109. buf, sizeof(struct koneplus_profile_settings));
  110. }
  111. static int koneplus_set_profile_settings(struct usb_device *usb_dev,
  112. struct koneplus_profile_settings const *settings)
  113. {
  114. return koneplus_send(usb_dev, KONEPLUS_COMMAND_PROFILE_SETTINGS,
  115. settings, sizeof(struct koneplus_profile_settings));
  116. }
  117. static int koneplus_get_profile_buttons(struct usb_device *usb_dev,
  118. struct koneplus_profile_buttons *buf, int number)
  119. {
  120. int retval;
  121. retval = koneplus_select_profile(usb_dev, number,
  122. KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
  123. if (retval)
  124. return retval;
  125. return roccat_common_receive(usb_dev, KONEPLUS_COMMAND_PROFILE_BUTTONS,
  126. buf, sizeof(struct koneplus_profile_buttons));
  127. }
  128. static int koneplus_set_profile_buttons(struct usb_device *usb_dev,
  129. struct koneplus_profile_buttons const *buttons)
  130. {
  131. return koneplus_send(usb_dev, KONEPLUS_COMMAND_PROFILE_BUTTONS,
  132. buttons, sizeof(struct koneplus_profile_buttons));
  133. }
  134. /* retval is 0-4 on success, < 0 on error */
  135. static int koneplus_get_actual_profile(struct usb_device *usb_dev)
  136. {
  137. struct koneplus_actual_profile buf;
  138. int retval;
  139. retval = roccat_common_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
  140. &buf, sizeof(struct koneplus_actual_profile));
  141. return retval ? retval : buf.actual_profile;
  142. }
  143. static int koneplus_set_actual_profile(struct usb_device *usb_dev,
  144. int new_profile)
  145. {
  146. struct koneplus_actual_profile buf;
  147. buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
  148. buf.size = sizeof(struct koneplus_actual_profile);
  149. buf.actual_profile = new_profile;
  150. return koneplus_send(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
  151. &buf, sizeof(struct koneplus_actual_profile));
  152. }
  153. static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
  154. char *buf, loff_t off, size_t count,
  155. size_t real_size, uint command)
  156. {
  157. struct device *dev =
  158. container_of(kobj, struct device, kobj)->parent->parent;
  159. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  160. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  161. int retval;
  162. if (off >= real_size)
  163. return 0;
  164. if (off != 0 || count != real_size)
  165. return -EINVAL;
  166. mutex_lock(&koneplus->koneplus_lock);
  167. retval = roccat_common_receive(usb_dev, command, buf, real_size);
  168. mutex_unlock(&koneplus->koneplus_lock);
  169. if (retval)
  170. return retval;
  171. return real_size;
  172. }
  173. static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
  174. void const *buf, loff_t off, size_t count,
  175. size_t real_size, uint command)
  176. {
  177. struct device *dev =
  178. container_of(kobj, struct device, kobj)->parent->parent;
  179. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  180. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  181. int retval;
  182. if (off != 0 || count != real_size)
  183. return -EINVAL;
  184. mutex_lock(&koneplus->koneplus_lock);
  185. retval = koneplus_send(usb_dev, command, buf, real_size);
  186. mutex_unlock(&koneplus->koneplus_lock);
  187. if (retval)
  188. return retval;
  189. return real_size;
  190. }
  191. static ssize_t koneplus_sysfs_write_talk(struct file *fp,
  192. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  193. loff_t off, size_t count)
  194. {
  195. return koneplus_sysfs_write(fp, kobj, buf, off, count,
  196. sizeof(struct koneplus_talk), KONEPLUS_COMMAND_TALK);
  197. }
  198. static ssize_t koneplus_sysfs_write_macro(struct file *fp,
  199. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  200. loff_t off, size_t count)
  201. {
  202. return koneplus_sysfs_write(fp, kobj, buf, off, count,
  203. sizeof(struct koneplus_macro), KONEPLUS_COMMAND_MACRO);
  204. }
  205. static ssize_t koneplus_sysfs_read_sensor(struct file *fp,
  206. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  207. loff_t off, size_t count)
  208. {
  209. return koneplus_sysfs_read(fp, kobj, buf, off, count,
  210. sizeof(struct koneplus_sensor), KONEPLUS_COMMAND_SENSOR);
  211. }
  212. static ssize_t koneplus_sysfs_write_sensor(struct file *fp,
  213. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  214. loff_t off, size_t count)
  215. {
  216. return koneplus_sysfs_write(fp, kobj, buf, off, count,
  217. sizeof(struct koneplus_sensor), KONEPLUS_COMMAND_SENSOR);
  218. }
  219. static ssize_t koneplus_sysfs_write_tcu(struct file *fp,
  220. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  221. loff_t off, size_t count)
  222. {
  223. return koneplus_sysfs_write(fp, kobj, buf, off, count,
  224. sizeof(struct koneplus_tcu), KONEPLUS_COMMAND_TCU);
  225. }
  226. static ssize_t koneplus_sysfs_read_tcu_image(struct file *fp,
  227. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  228. loff_t off, size_t count)
  229. {
  230. return koneplus_sysfs_read(fp, kobj, buf, off, count,
  231. sizeof(struct koneplus_tcu_image), KONEPLUS_COMMAND_TCU);
  232. }
  233. static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
  234. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  235. loff_t off, size_t count)
  236. {
  237. struct device *dev =
  238. container_of(kobj, struct device, kobj)->parent->parent;
  239. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  240. if (off >= sizeof(struct koneplus_profile_settings))
  241. return 0;
  242. if (off + count > sizeof(struct koneplus_profile_settings))
  243. count = sizeof(struct koneplus_profile_settings) - off;
  244. mutex_lock(&koneplus->koneplus_lock);
  245. memcpy(buf, ((char const *)&koneplus->profile_settings[*(uint *)(attr->private)]) + off,
  246. count);
  247. mutex_unlock(&koneplus->koneplus_lock);
  248. return count;
  249. }
  250. static ssize_t koneplus_sysfs_write_profile_settings(struct file *fp,
  251. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  252. loff_t off, size_t count)
  253. {
  254. struct device *dev =
  255. container_of(kobj, struct device, kobj)->parent->parent;
  256. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  257. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  258. int retval = 0;
  259. int difference;
  260. int profile_number;
  261. struct koneplus_profile_settings *profile_settings;
  262. if (off != 0 || count != sizeof(struct koneplus_profile_settings))
  263. return -EINVAL;
  264. profile_number = ((struct koneplus_profile_settings const *)buf)->number;
  265. profile_settings = &koneplus->profile_settings[profile_number];
  266. mutex_lock(&koneplus->koneplus_lock);
  267. difference = memcmp(buf, profile_settings,
  268. sizeof(struct koneplus_profile_settings));
  269. if (difference) {
  270. retval = koneplus_set_profile_settings(usb_dev,
  271. (struct koneplus_profile_settings const *)buf);
  272. if (!retval)
  273. memcpy(profile_settings, buf,
  274. sizeof(struct koneplus_profile_settings));
  275. }
  276. mutex_unlock(&koneplus->koneplus_lock);
  277. if (retval)
  278. return retval;
  279. return sizeof(struct koneplus_profile_settings);
  280. }
  281. static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
  282. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  283. loff_t off, size_t count)
  284. {
  285. struct device *dev =
  286. container_of(kobj, struct device, kobj)->parent->parent;
  287. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  288. if (off >= sizeof(struct koneplus_profile_buttons))
  289. return 0;
  290. if (off + count > sizeof(struct koneplus_profile_buttons))
  291. count = sizeof(struct koneplus_profile_buttons) - off;
  292. mutex_lock(&koneplus->koneplus_lock);
  293. memcpy(buf, ((char const *)&koneplus->profile_buttons[*(uint *)(attr->private)]) + off,
  294. count);
  295. mutex_unlock(&koneplus->koneplus_lock);
  296. return count;
  297. }
  298. static ssize_t koneplus_sysfs_write_profile_buttons(struct file *fp,
  299. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  300. loff_t off, size_t count)
  301. {
  302. struct device *dev =
  303. container_of(kobj, struct device, kobj)->parent->parent;
  304. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  305. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  306. int retval = 0;
  307. int difference;
  308. uint profile_number;
  309. struct koneplus_profile_buttons *profile_buttons;
  310. if (off != 0 || count != sizeof(struct koneplus_profile_buttons))
  311. return -EINVAL;
  312. profile_number = ((struct koneplus_profile_buttons const *)buf)->number;
  313. profile_buttons = &koneplus->profile_buttons[profile_number];
  314. mutex_lock(&koneplus->koneplus_lock);
  315. difference = memcmp(buf, profile_buttons,
  316. sizeof(struct koneplus_profile_buttons));
  317. if (difference) {
  318. retval = koneplus_set_profile_buttons(usb_dev,
  319. (struct koneplus_profile_buttons const *)buf);
  320. if (!retval)
  321. memcpy(profile_buttons, buf,
  322. sizeof(struct koneplus_profile_buttons));
  323. }
  324. mutex_unlock(&koneplus->koneplus_lock);
  325. if (retval)
  326. return retval;
  327. return sizeof(struct koneplus_profile_buttons);
  328. }
  329. static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
  330. struct device_attribute *attr, char *buf)
  331. {
  332. struct koneplus_device *koneplus =
  333. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  334. return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
  335. }
  336. static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
  337. struct device_attribute *attr, char const *buf, size_t size)
  338. {
  339. struct koneplus_device *koneplus;
  340. struct usb_device *usb_dev;
  341. unsigned long profile;
  342. int retval;
  343. struct koneplus_roccat_report roccat_report;
  344. dev = dev->parent->parent;
  345. koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  346. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  347. retval = strict_strtoul(buf, 10, &profile);
  348. if (retval)
  349. return retval;
  350. if (profile > 4)
  351. return -EINVAL;
  352. mutex_lock(&koneplus->koneplus_lock);
  353. retval = koneplus_set_actual_profile(usb_dev, profile);
  354. if (retval) {
  355. mutex_unlock(&koneplus->koneplus_lock);
  356. return retval;
  357. }
  358. koneplus_profile_activated(koneplus, profile);
  359. roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
  360. roccat_report.data1 = profile + 1;
  361. roccat_report.data2 = 0;
  362. roccat_report.profile = profile + 1;
  363. roccat_report_event(koneplus->chrdev_minor,
  364. (uint8_t const *)&roccat_report);
  365. mutex_unlock(&koneplus->koneplus_lock);
  366. return size;
  367. }
  368. static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
  369. struct device_attribute *attr, char *buf)
  370. {
  371. struct koneplus_device *koneplus =
  372. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  373. return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->info.firmware_version);
  374. }
  375. static struct device_attribute koneplus_attributes[] = {
  376. __ATTR(actual_profile, 0660,
  377. koneplus_sysfs_show_actual_profile,
  378. koneplus_sysfs_set_actual_profile),
  379. __ATTR(startup_profile, 0660,
  380. koneplus_sysfs_show_actual_profile,
  381. koneplus_sysfs_set_actual_profile),
  382. __ATTR(firmware_version, 0440,
  383. koneplus_sysfs_show_firmware_version, NULL),
  384. __ATTR_NULL
  385. };
  386. static struct bin_attribute koneplus_bin_attributes[] = {
  387. {
  388. .attr = { .name = "sensor", .mode = 0660 },
  389. .size = sizeof(struct koneplus_sensor),
  390. .read = koneplus_sysfs_read_sensor,
  391. .write = koneplus_sysfs_write_sensor
  392. },
  393. {
  394. .attr = { .name = "tcu", .mode = 0220 },
  395. .size = sizeof(struct koneplus_tcu),
  396. .write = koneplus_sysfs_write_tcu
  397. },
  398. {
  399. .attr = { .name = "tcu_image", .mode = 0440 },
  400. .size = sizeof(struct koneplus_tcu_image),
  401. .read = koneplus_sysfs_read_tcu_image
  402. },
  403. {
  404. .attr = { .name = "profile_settings", .mode = 0220 },
  405. .size = sizeof(struct koneplus_profile_settings),
  406. .write = koneplus_sysfs_write_profile_settings
  407. },
  408. {
  409. .attr = { .name = "profile1_settings", .mode = 0440 },
  410. .size = sizeof(struct koneplus_profile_settings),
  411. .read = koneplus_sysfs_read_profilex_settings,
  412. .private = &profile_numbers[0]
  413. },
  414. {
  415. .attr = { .name = "profile2_settings", .mode = 0440 },
  416. .size = sizeof(struct koneplus_profile_settings),
  417. .read = koneplus_sysfs_read_profilex_settings,
  418. .private = &profile_numbers[1]
  419. },
  420. {
  421. .attr = { .name = "profile3_settings", .mode = 0440 },
  422. .size = sizeof(struct koneplus_profile_settings),
  423. .read = koneplus_sysfs_read_profilex_settings,
  424. .private = &profile_numbers[2]
  425. },
  426. {
  427. .attr = { .name = "profile4_settings", .mode = 0440 },
  428. .size = sizeof(struct koneplus_profile_settings),
  429. .read = koneplus_sysfs_read_profilex_settings,
  430. .private = &profile_numbers[3]
  431. },
  432. {
  433. .attr = { .name = "profile5_settings", .mode = 0440 },
  434. .size = sizeof(struct koneplus_profile_settings),
  435. .read = koneplus_sysfs_read_profilex_settings,
  436. .private = &profile_numbers[4]
  437. },
  438. {
  439. .attr = { .name = "profile_buttons", .mode = 0220 },
  440. .size = sizeof(struct koneplus_profile_buttons),
  441. .write = koneplus_sysfs_write_profile_buttons
  442. },
  443. {
  444. .attr = { .name = "profile1_buttons", .mode = 0440 },
  445. .size = sizeof(struct koneplus_profile_buttons),
  446. .read = koneplus_sysfs_read_profilex_buttons,
  447. .private = &profile_numbers[0]
  448. },
  449. {
  450. .attr = { .name = "profile2_buttons", .mode = 0440 },
  451. .size = sizeof(struct koneplus_profile_buttons),
  452. .read = koneplus_sysfs_read_profilex_buttons,
  453. .private = &profile_numbers[1]
  454. },
  455. {
  456. .attr = { .name = "profile3_buttons", .mode = 0440 },
  457. .size = sizeof(struct koneplus_profile_buttons),
  458. .read = koneplus_sysfs_read_profilex_buttons,
  459. .private = &profile_numbers[2]
  460. },
  461. {
  462. .attr = { .name = "profile4_buttons", .mode = 0440 },
  463. .size = sizeof(struct koneplus_profile_buttons),
  464. .read = koneplus_sysfs_read_profilex_buttons,
  465. .private = &profile_numbers[3]
  466. },
  467. {
  468. .attr = { .name = "profile5_buttons", .mode = 0440 },
  469. .size = sizeof(struct koneplus_profile_buttons),
  470. .read = koneplus_sysfs_read_profilex_buttons,
  471. .private = &profile_numbers[4]
  472. },
  473. {
  474. .attr = { .name = "macro", .mode = 0220 },
  475. .size = sizeof(struct koneplus_macro),
  476. .write = koneplus_sysfs_write_macro
  477. },
  478. {
  479. .attr = { .name = "talk", .mode = 0220 },
  480. .size = sizeof(struct koneplus_talk),
  481. .write = koneplus_sysfs_write_talk
  482. },
  483. __ATTR_NULL
  484. };
  485. static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
  486. struct koneplus_device *koneplus)
  487. {
  488. int retval, i;
  489. static uint wait = 200;
  490. mutex_init(&koneplus->koneplus_lock);
  491. retval = koneplus_get_info(usb_dev, &koneplus->info);
  492. if (retval)
  493. return retval;
  494. for (i = 0; i < 5; ++i) {
  495. msleep(wait);
  496. retval = koneplus_get_profile_settings(usb_dev,
  497. &koneplus->profile_settings[i], i);
  498. if (retval)
  499. return retval;
  500. msleep(wait);
  501. retval = koneplus_get_profile_buttons(usb_dev,
  502. &koneplus->profile_buttons[i], i);
  503. if (retval)
  504. return retval;
  505. }
  506. msleep(wait);
  507. retval = koneplus_get_actual_profile(usb_dev);
  508. if (retval < 0)
  509. return retval;
  510. koneplus_profile_activated(koneplus, retval);
  511. return 0;
  512. }
  513. static int koneplus_init_specials(struct hid_device *hdev)
  514. {
  515. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  516. struct usb_device *usb_dev = interface_to_usbdev(intf);
  517. struct koneplus_device *koneplus;
  518. int retval;
  519. if (intf->cur_altsetting->desc.bInterfaceProtocol
  520. == USB_INTERFACE_PROTOCOL_MOUSE) {
  521. koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
  522. if (!koneplus) {
  523. hid_err(hdev, "can't alloc device descriptor\n");
  524. return -ENOMEM;
  525. }
  526. hid_set_drvdata(hdev, koneplus);
  527. retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
  528. if (retval) {
  529. hid_err(hdev, "couldn't init struct koneplus_device\n");
  530. goto exit_free;
  531. }
  532. retval = roccat_connect(koneplus_class, hdev,
  533. sizeof(struct koneplus_roccat_report));
  534. if (retval < 0) {
  535. hid_err(hdev, "couldn't init char dev\n");
  536. } else {
  537. koneplus->chrdev_minor = retval;
  538. koneplus->roccat_claimed = 1;
  539. }
  540. } else {
  541. hid_set_drvdata(hdev, NULL);
  542. }
  543. return 0;
  544. exit_free:
  545. kfree(koneplus);
  546. return retval;
  547. }
  548. static void koneplus_remove_specials(struct hid_device *hdev)
  549. {
  550. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  551. struct koneplus_device *koneplus;
  552. if (intf->cur_altsetting->desc.bInterfaceProtocol
  553. == USB_INTERFACE_PROTOCOL_MOUSE) {
  554. koneplus = hid_get_drvdata(hdev);
  555. if (koneplus->roccat_claimed)
  556. roccat_disconnect(koneplus->chrdev_minor);
  557. kfree(koneplus);
  558. }
  559. }
  560. static int koneplus_probe(struct hid_device *hdev,
  561. const struct hid_device_id *id)
  562. {
  563. int retval;
  564. retval = hid_parse(hdev);
  565. if (retval) {
  566. hid_err(hdev, "parse failed\n");
  567. goto exit;
  568. }
  569. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  570. if (retval) {
  571. hid_err(hdev, "hw start failed\n");
  572. goto exit;
  573. }
  574. retval = koneplus_init_specials(hdev);
  575. if (retval) {
  576. hid_err(hdev, "couldn't install mouse\n");
  577. goto exit_stop;
  578. }
  579. return 0;
  580. exit_stop:
  581. hid_hw_stop(hdev);
  582. exit:
  583. return retval;
  584. }
  585. static void koneplus_remove(struct hid_device *hdev)
  586. {
  587. koneplus_remove_specials(hdev);
  588. hid_hw_stop(hdev);
  589. }
  590. static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
  591. u8 const *data)
  592. {
  593. struct koneplus_mouse_report_button const *button_report;
  594. switch (data[0]) {
  595. case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
  596. button_report = (struct koneplus_mouse_report_button const *)data;
  597. switch (button_report->type) {
  598. case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
  599. koneplus_profile_activated(koneplus, button_report->data1 - 1);
  600. break;
  601. }
  602. break;
  603. }
  604. }
  605. static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
  606. u8 const *data)
  607. {
  608. struct koneplus_roccat_report roccat_report;
  609. struct koneplus_mouse_report_button const *button_report;
  610. if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
  611. return;
  612. button_report = (struct koneplus_mouse_report_button const *)data;
  613. if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
  614. button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
  615. button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
  616. return;
  617. roccat_report.type = button_report->type;
  618. roccat_report.data1 = button_report->data1;
  619. roccat_report.data2 = button_report->data2;
  620. roccat_report.profile = koneplus->actual_profile + 1;
  621. roccat_report_event(koneplus->chrdev_minor,
  622. (uint8_t const *)&roccat_report);
  623. }
  624. static int koneplus_raw_event(struct hid_device *hdev,
  625. struct hid_report *report, u8 *data, int size)
  626. {
  627. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  628. struct koneplus_device *koneplus = hid_get_drvdata(hdev);
  629. if (intf->cur_altsetting->desc.bInterfaceProtocol
  630. != USB_INTERFACE_PROTOCOL_MOUSE)
  631. return 0;
  632. if (koneplus == NULL)
  633. return 0;
  634. koneplus_keep_values_up_to_date(koneplus, data);
  635. if (koneplus->roccat_claimed)
  636. koneplus_report_to_chrdev(koneplus, data);
  637. return 0;
  638. }
  639. static const struct hid_device_id koneplus_devices[] = {
  640. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
  641. { }
  642. };
  643. MODULE_DEVICE_TABLE(hid, koneplus_devices);
  644. static struct hid_driver koneplus_driver = {
  645. .name = "koneplus",
  646. .id_table = koneplus_devices,
  647. .probe = koneplus_probe,
  648. .remove = koneplus_remove,
  649. .raw_event = koneplus_raw_event
  650. };
  651. static int __init koneplus_init(void)
  652. {
  653. int retval;
  654. /* class name has to be same as driver name */
  655. koneplus_class = class_create(THIS_MODULE, "koneplus");
  656. if (IS_ERR(koneplus_class))
  657. return PTR_ERR(koneplus_class);
  658. koneplus_class->dev_attrs = koneplus_attributes;
  659. koneplus_class->dev_bin_attrs = koneplus_bin_attributes;
  660. retval = hid_register_driver(&koneplus_driver);
  661. if (retval)
  662. class_destroy(koneplus_class);
  663. return retval;
  664. }
  665. static void __exit koneplus_exit(void)
  666. {
  667. hid_unregister_driver(&koneplus_driver);
  668. class_destroy(koneplus_class);
  669. }
  670. module_init(koneplus_init);
  671. module_exit(koneplus_exit);
  672. MODULE_AUTHOR("Stefan Achatz");
  673. MODULE_DESCRIPTION("USB Roccat Kone[+] driver");
  674. MODULE_LICENSE("GPL v2");