hid-roccat-koneplus.c 22 KB

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