hid-roccat-koneplus.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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(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), KONEPLUS_COMMAND_TCU);
  189. }
  190. static ssize_t koneplus_sysfs_read_tcu_image(struct file *fp,
  191. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  192. loff_t off, size_t count)
  193. {
  194. return koneplus_sysfs_read(fp, kobj, buf, off, count,
  195. sizeof(struct koneplus_tcu_image), KONEPLUS_COMMAND_TCU);
  196. }
  197. static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
  198. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  199. loff_t off, size_t count)
  200. {
  201. struct device *dev =
  202. container_of(kobj, struct device, kobj)->parent->parent;
  203. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  204. if (off >= sizeof(struct koneplus_profile_settings))
  205. return 0;
  206. if (off + count > sizeof(struct koneplus_profile_settings))
  207. count = sizeof(struct koneplus_profile_settings) - off;
  208. mutex_lock(&koneplus->koneplus_lock);
  209. memcpy(buf, ((char const *)&koneplus->profile_settings[*(uint *)(attr->private)]) + off,
  210. count);
  211. mutex_unlock(&koneplus->koneplus_lock);
  212. return count;
  213. }
  214. static ssize_t koneplus_sysfs_write_profile_settings(struct file *fp,
  215. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  216. loff_t off, size_t count)
  217. {
  218. struct device *dev =
  219. container_of(kobj, struct device, kobj)->parent->parent;
  220. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  221. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  222. int retval = 0;
  223. int difference;
  224. int profile_number;
  225. struct koneplus_profile_settings *profile_settings;
  226. if (off != 0 || count != sizeof(struct koneplus_profile_settings))
  227. return -EINVAL;
  228. profile_number = ((struct koneplus_profile_settings const *)buf)->number;
  229. profile_settings = &koneplus->profile_settings[profile_number];
  230. mutex_lock(&koneplus->koneplus_lock);
  231. difference = memcmp(buf, profile_settings,
  232. sizeof(struct koneplus_profile_settings));
  233. if (difference) {
  234. retval = koneplus_set_profile_settings(usb_dev,
  235. (struct koneplus_profile_settings const *)buf);
  236. if (!retval)
  237. memcpy(profile_settings, buf,
  238. sizeof(struct koneplus_profile_settings));
  239. }
  240. mutex_unlock(&koneplus->koneplus_lock);
  241. if (retval)
  242. return retval;
  243. return sizeof(struct koneplus_profile_settings);
  244. }
  245. static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
  246. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  247. loff_t off, size_t count)
  248. {
  249. struct device *dev =
  250. container_of(kobj, struct device, kobj)->parent->parent;
  251. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  252. if (off >= sizeof(struct koneplus_profile_buttons))
  253. return 0;
  254. if (off + count > sizeof(struct koneplus_profile_buttons))
  255. count = sizeof(struct koneplus_profile_buttons) - off;
  256. mutex_lock(&koneplus->koneplus_lock);
  257. memcpy(buf, ((char const *)&koneplus->profile_buttons[*(uint *)(attr->private)]) + off,
  258. count);
  259. mutex_unlock(&koneplus->koneplus_lock);
  260. return count;
  261. }
  262. static ssize_t koneplus_sysfs_write_profile_buttons(struct file *fp,
  263. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  264. loff_t off, size_t count)
  265. {
  266. struct device *dev =
  267. container_of(kobj, struct device, kobj)->parent->parent;
  268. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  269. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  270. int retval = 0;
  271. int difference;
  272. uint profile_number;
  273. struct koneplus_profile_buttons *profile_buttons;
  274. if (off != 0 || count != sizeof(struct koneplus_profile_buttons))
  275. return -EINVAL;
  276. profile_number = ((struct koneplus_profile_buttons const *)buf)->number;
  277. profile_buttons = &koneplus->profile_buttons[profile_number];
  278. mutex_lock(&koneplus->koneplus_lock);
  279. difference = memcmp(buf, profile_buttons,
  280. sizeof(struct koneplus_profile_buttons));
  281. if (difference) {
  282. retval = koneplus_set_profile_buttons(usb_dev,
  283. (struct koneplus_profile_buttons const *)buf);
  284. if (!retval)
  285. memcpy(profile_buttons, buf,
  286. sizeof(struct koneplus_profile_buttons));
  287. }
  288. mutex_unlock(&koneplus->koneplus_lock);
  289. if (retval)
  290. return retval;
  291. return sizeof(struct koneplus_profile_buttons);
  292. }
  293. static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
  294. struct device_attribute *attr, char *buf)
  295. {
  296. struct koneplus_device *koneplus =
  297. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  298. return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
  299. }
  300. static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
  301. struct device_attribute *attr, char const *buf, size_t size)
  302. {
  303. struct koneplus_device *koneplus;
  304. struct usb_device *usb_dev;
  305. unsigned long profile;
  306. int retval;
  307. struct koneplus_roccat_report roccat_report;
  308. dev = dev->parent->parent;
  309. koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  310. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  311. retval = strict_strtoul(buf, 10, &profile);
  312. if (retval)
  313. return retval;
  314. if (profile > 4)
  315. return -EINVAL;
  316. mutex_lock(&koneplus->koneplus_lock);
  317. retval = koneplus_set_actual_profile(usb_dev, profile);
  318. if (retval) {
  319. mutex_unlock(&koneplus->koneplus_lock);
  320. return retval;
  321. }
  322. koneplus_profile_activated(koneplus, profile);
  323. roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
  324. roccat_report.data1 = profile + 1;
  325. roccat_report.data2 = 0;
  326. roccat_report.profile = profile + 1;
  327. roccat_report_event(koneplus->chrdev_minor,
  328. (uint8_t const *)&roccat_report);
  329. mutex_unlock(&koneplus->koneplus_lock);
  330. return size;
  331. }
  332. static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
  333. struct device_attribute *attr, char *buf)
  334. {
  335. struct koneplus_device *koneplus =
  336. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  337. return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->info.firmware_version);
  338. }
  339. static struct device_attribute koneplus_attributes[] = {
  340. __ATTR(actual_profile, 0660,
  341. koneplus_sysfs_show_actual_profile,
  342. koneplus_sysfs_set_actual_profile),
  343. __ATTR(startup_profile, 0660,
  344. koneplus_sysfs_show_actual_profile,
  345. koneplus_sysfs_set_actual_profile),
  346. __ATTR(firmware_version, 0440,
  347. koneplus_sysfs_show_firmware_version, NULL),
  348. __ATTR_NULL
  349. };
  350. static struct bin_attribute koneplus_bin_attributes[] = {
  351. {
  352. .attr = { .name = "sensor", .mode = 0660 },
  353. .size = sizeof(struct koneplus_sensor),
  354. .read = koneplus_sysfs_read_sensor,
  355. .write = koneplus_sysfs_write_sensor
  356. },
  357. {
  358. .attr = { .name = "tcu", .mode = 0660 },
  359. .size = sizeof(struct koneplus_tcu),
  360. .read = koneplus_sysfs_read_tcu,
  361. .write = koneplus_sysfs_write_tcu
  362. },
  363. {
  364. .attr = { .name = "tcu_image", .mode = 0440 },
  365. .size = sizeof(struct koneplus_tcu_image),
  366. .read = koneplus_sysfs_read_tcu_image
  367. },
  368. {
  369. .attr = { .name = "profile_settings", .mode = 0220 },
  370. .size = sizeof(struct koneplus_profile_settings),
  371. .write = koneplus_sysfs_write_profile_settings
  372. },
  373. {
  374. .attr = { .name = "profile1_settings", .mode = 0440 },
  375. .size = sizeof(struct koneplus_profile_settings),
  376. .read = koneplus_sysfs_read_profilex_settings,
  377. .private = &profile_numbers[0]
  378. },
  379. {
  380. .attr = { .name = "profile2_settings", .mode = 0440 },
  381. .size = sizeof(struct koneplus_profile_settings),
  382. .read = koneplus_sysfs_read_profilex_settings,
  383. .private = &profile_numbers[1]
  384. },
  385. {
  386. .attr = { .name = "profile3_settings", .mode = 0440 },
  387. .size = sizeof(struct koneplus_profile_settings),
  388. .read = koneplus_sysfs_read_profilex_settings,
  389. .private = &profile_numbers[2]
  390. },
  391. {
  392. .attr = { .name = "profile4_settings", .mode = 0440 },
  393. .size = sizeof(struct koneplus_profile_settings),
  394. .read = koneplus_sysfs_read_profilex_settings,
  395. .private = &profile_numbers[3]
  396. },
  397. {
  398. .attr = { .name = "profile5_settings", .mode = 0440 },
  399. .size = sizeof(struct koneplus_profile_settings),
  400. .read = koneplus_sysfs_read_profilex_settings,
  401. .private = &profile_numbers[4]
  402. },
  403. {
  404. .attr = { .name = "profile_buttons", .mode = 0220 },
  405. .size = sizeof(struct koneplus_profile_buttons),
  406. .write = koneplus_sysfs_write_profile_buttons
  407. },
  408. {
  409. .attr = { .name = "profile1_buttons", .mode = 0440 },
  410. .size = sizeof(struct koneplus_profile_buttons),
  411. .read = koneplus_sysfs_read_profilex_buttons,
  412. .private = &profile_numbers[0]
  413. },
  414. {
  415. .attr = { .name = "profile2_buttons", .mode = 0440 },
  416. .size = sizeof(struct koneplus_profile_buttons),
  417. .read = koneplus_sysfs_read_profilex_buttons,
  418. .private = &profile_numbers[1]
  419. },
  420. {
  421. .attr = { .name = "profile3_buttons", .mode = 0440 },
  422. .size = sizeof(struct koneplus_profile_buttons),
  423. .read = koneplus_sysfs_read_profilex_buttons,
  424. .private = &profile_numbers[2]
  425. },
  426. {
  427. .attr = { .name = "profile4_buttons", .mode = 0440 },
  428. .size = sizeof(struct koneplus_profile_buttons),
  429. .read = koneplus_sysfs_read_profilex_buttons,
  430. .private = &profile_numbers[3]
  431. },
  432. {
  433. .attr = { .name = "profile5_buttons", .mode = 0440 },
  434. .size = sizeof(struct koneplus_profile_buttons),
  435. .read = koneplus_sysfs_read_profilex_buttons,
  436. .private = &profile_numbers[4]
  437. },
  438. {
  439. .attr = { .name = "macro", .mode = 0220 },
  440. .size = sizeof(struct koneplus_macro),
  441. .write = koneplus_sysfs_write_macro
  442. },
  443. {
  444. .attr = { .name = "talk", .mode = 0220 },
  445. .size = sizeof(struct koneplus_talk),
  446. .write = koneplus_sysfs_write_talk
  447. },
  448. __ATTR_NULL
  449. };
  450. static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
  451. struct koneplus_device *koneplus)
  452. {
  453. int retval, i;
  454. static uint wait = 200;
  455. mutex_init(&koneplus->koneplus_lock);
  456. retval = koneplus_get_info(usb_dev, &koneplus->info);
  457. if (retval)
  458. return retval;
  459. for (i = 0; i < 5; ++i) {
  460. msleep(wait);
  461. retval = koneplus_get_profile_settings(usb_dev,
  462. &koneplus->profile_settings[i], i);
  463. if (retval)
  464. return retval;
  465. msleep(wait);
  466. retval = koneplus_get_profile_buttons(usb_dev,
  467. &koneplus->profile_buttons[i], i);
  468. if (retval)
  469. return retval;
  470. }
  471. msleep(wait);
  472. retval = koneplus_get_actual_profile(usb_dev);
  473. if (retval < 0)
  474. return retval;
  475. koneplus_profile_activated(koneplus, retval);
  476. return 0;
  477. }
  478. static int koneplus_init_specials(struct hid_device *hdev)
  479. {
  480. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  481. struct usb_device *usb_dev = interface_to_usbdev(intf);
  482. struct koneplus_device *koneplus;
  483. int retval;
  484. if (intf->cur_altsetting->desc.bInterfaceProtocol
  485. == USB_INTERFACE_PROTOCOL_MOUSE) {
  486. koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
  487. if (!koneplus) {
  488. hid_err(hdev, "can't alloc device descriptor\n");
  489. return -ENOMEM;
  490. }
  491. hid_set_drvdata(hdev, koneplus);
  492. retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
  493. if (retval) {
  494. hid_err(hdev, "couldn't init struct koneplus_device\n");
  495. goto exit_free;
  496. }
  497. retval = roccat_connect(koneplus_class, hdev,
  498. sizeof(struct koneplus_roccat_report));
  499. if (retval < 0) {
  500. hid_err(hdev, "couldn't init char dev\n");
  501. } else {
  502. koneplus->chrdev_minor = retval;
  503. koneplus->roccat_claimed = 1;
  504. }
  505. } else {
  506. hid_set_drvdata(hdev, NULL);
  507. }
  508. return 0;
  509. exit_free:
  510. kfree(koneplus);
  511. return retval;
  512. }
  513. static void koneplus_remove_specials(struct hid_device *hdev)
  514. {
  515. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  516. struct koneplus_device *koneplus;
  517. if (intf->cur_altsetting->desc.bInterfaceProtocol
  518. == USB_INTERFACE_PROTOCOL_MOUSE) {
  519. koneplus = hid_get_drvdata(hdev);
  520. if (koneplus->roccat_claimed)
  521. roccat_disconnect(koneplus->chrdev_minor);
  522. kfree(koneplus);
  523. }
  524. }
  525. static int koneplus_probe(struct hid_device *hdev,
  526. const struct hid_device_id *id)
  527. {
  528. int retval;
  529. retval = hid_parse(hdev);
  530. if (retval) {
  531. hid_err(hdev, "parse failed\n");
  532. goto exit;
  533. }
  534. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  535. if (retval) {
  536. hid_err(hdev, "hw start failed\n");
  537. goto exit;
  538. }
  539. retval = koneplus_init_specials(hdev);
  540. if (retval) {
  541. hid_err(hdev, "couldn't install mouse\n");
  542. goto exit_stop;
  543. }
  544. return 0;
  545. exit_stop:
  546. hid_hw_stop(hdev);
  547. exit:
  548. return retval;
  549. }
  550. static void koneplus_remove(struct hid_device *hdev)
  551. {
  552. koneplus_remove_specials(hdev);
  553. hid_hw_stop(hdev);
  554. }
  555. static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
  556. u8 const *data)
  557. {
  558. struct koneplus_mouse_report_button const *button_report;
  559. switch (data[0]) {
  560. case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
  561. button_report = (struct koneplus_mouse_report_button const *)data;
  562. switch (button_report->type) {
  563. case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
  564. koneplus_profile_activated(koneplus, button_report->data1 - 1);
  565. break;
  566. }
  567. break;
  568. }
  569. }
  570. static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
  571. u8 const *data)
  572. {
  573. struct koneplus_roccat_report roccat_report;
  574. struct koneplus_mouse_report_button const *button_report;
  575. if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
  576. return;
  577. button_report = (struct koneplus_mouse_report_button const *)data;
  578. if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
  579. button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
  580. button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
  581. return;
  582. roccat_report.type = button_report->type;
  583. roccat_report.data1 = button_report->data1;
  584. roccat_report.data2 = button_report->data2;
  585. roccat_report.profile = koneplus->actual_profile + 1;
  586. roccat_report_event(koneplus->chrdev_minor,
  587. (uint8_t const *)&roccat_report);
  588. }
  589. static int koneplus_raw_event(struct hid_device *hdev,
  590. struct hid_report *report, u8 *data, int size)
  591. {
  592. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  593. struct koneplus_device *koneplus = hid_get_drvdata(hdev);
  594. if (intf->cur_altsetting->desc.bInterfaceProtocol
  595. != USB_INTERFACE_PROTOCOL_MOUSE)
  596. return 0;
  597. if (koneplus == NULL)
  598. return 0;
  599. koneplus_keep_values_up_to_date(koneplus, data);
  600. if (koneplus->roccat_claimed)
  601. koneplus_report_to_chrdev(koneplus, data);
  602. return 0;
  603. }
  604. static const struct hid_device_id koneplus_devices[] = {
  605. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
  606. { }
  607. };
  608. MODULE_DEVICE_TABLE(hid, koneplus_devices);
  609. static struct hid_driver koneplus_driver = {
  610. .name = "koneplus",
  611. .id_table = koneplus_devices,
  612. .probe = koneplus_probe,
  613. .remove = koneplus_remove,
  614. .raw_event = koneplus_raw_event
  615. };
  616. static int __init koneplus_init(void)
  617. {
  618. int retval;
  619. /* class name has to be same as driver name */
  620. koneplus_class = class_create(THIS_MODULE, "koneplus");
  621. if (IS_ERR(koneplus_class))
  622. return PTR_ERR(koneplus_class);
  623. koneplus_class->dev_attrs = koneplus_attributes;
  624. koneplus_class->dev_bin_attrs = koneplus_bin_attributes;
  625. retval = hid_register_driver(&koneplus_driver);
  626. if (retval)
  627. class_destroy(koneplus_class);
  628. return retval;
  629. }
  630. static void __exit koneplus_exit(void)
  631. {
  632. hid_unregister_driver(&koneplus_driver);
  633. class_destroy(koneplus_class);
  634. }
  635. module_init(koneplus_init);
  636. module_exit(koneplus_exit);
  637. MODULE_AUTHOR("Stefan Achatz");
  638. MODULE_DESCRIPTION("USB Roccat Kone[+] driver");
  639. MODULE_LICENSE("GPL v2");