hid-roccat-koneplus.c 21 KB

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