hid-roccat-koneplus.c 21 KB

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