hid-roccat-koneplus.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * Roccat Kone[+] driver for Linux
  3. *
  4. * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. */
  12. /*
  13. * Roccat Kone[+] is an updated/improved version of the Kone with more memory
  14. * and functionality and without the non-standard behaviours the Kone had.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/input.h>
  18. #include <linux/hid.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/hid-roccat.h>
  22. #include "hid-ids.h"
  23. #include "hid-roccat-common.h"
  24. #include "hid-roccat-koneplus.h"
  25. static uint profile_numbers[5] = {0, 1, 2, 3, 4};
  26. static struct class *koneplus_class;
  27. static void koneplus_profile_activated(struct koneplus_device *koneplus,
  28. uint new_profile)
  29. {
  30. koneplus->actual_profile = new_profile;
  31. }
  32. static int koneplus_send_control(struct usb_device *usb_dev, uint value,
  33. enum koneplus_control_requests request)
  34. {
  35. struct koneplus_control control;
  36. if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
  37. request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
  38. value > 4)
  39. return -EINVAL;
  40. control.command = KONEPLUS_COMMAND_CONTROL;
  41. control.value = value;
  42. control.request = request;
  43. return roccat_common_send(usb_dev, KONEPLUS_USB_COMMAND_CONTROL,
  44. &control, sizeof(struct koneplus_control));
  45. }
  46. static int koneplus_receive_control_status(struct usb_device *usb_dev)
  47. {
  48. int retval;
  49. struct koneplus_control control;
  50. do {
  51. retval = roccat_common_receive(usb_dev, KONEPLUS_USB_COMMAND_CONTROL,
  52. &control, sizeof(struct koneplus_control));
  53. /* check if we get a completely wrong answer */
  54. if (retval)
  55. return retval;
  56. if (control.value == KONEPLUS_CONTROL_REQUEST_STATUS_OK)
  57. return 0;
  58. /* indicates that hardware needs some more time to complete action */
  59. if (control.value == KONEPLUS_CONTROL_REQUEST_STATUS_WAIT) {
  60. msleep(500); /* windows driver uses 1000 */
  61. continue;
  62. }
  63. /* seems to be critical - replug necessary */
  64. if (control.value == KONEPLUS_CONTROL_REQUEST_STATUS_OVERLOAD)
  65. return -EINVAL;
  66. hid_err(usb_dev, "koneplus_receive_control_status: "
  67. "unknown response value 0x%x\n", control.value);
  68. return -EINVAL;
  69. } while (1);
  70. }
  71. static int koneplus_send(struct usb_device *usb_dev, uint command,
  72. void const *buf, uint size)
  73. {
  74. int retval;
  75. retval = roccat_common_send(usb_dev, command, buf, size);
  76. if (retval)
  77. return retval;
  78. return koneplus_receive_control_status(usb_dev);
  79. }
  80. static int koneplus_select_profile(struct usb_device *usb_dev, uint number,
  81. enum koneplus_control_requests request)
  82. {
  83. int retval;
  84. retval = koneplus_send_control(usb_dev, number, request);
  85. if (retval)
  86. return retval;
  87. /* allow time to settle things - windows driver uses 500 */
  88. msleep(100);
  89. retval = koneplus_receive_control_status(usb_dev);
  90. if (retval)
  91. return retval;
  92. return 0;
  93. }
  94. static int koneplus_get_info(struct usb_device *usb_dev,
  95. struct koneplus_info *buf)
  96. {
  97. return roccat_common_receive(usb_dev, KONEPLUS_USB_COMMAND_INFO,
  98. buf, sizeof(struct koneplus_info));
  99. }
  100. static int koneplus_get_profile_settings(struct usb_device *usb_dev,
  101. struct koneplus_profile_settings *buf, uint number)
  102. {
  103. int retval;
  104. retval = koneplus_select_profile(usb_dev, number,
  105. KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
  106. if (retval)
  107. return retval;
  108. return roccat_common_receive(usb_dev, KONEPLUS_USB_COMMAND_PROFILE_SETTINGS,
  109. buf, sizeof(struct koneplus_profile_settings));
  110. }
  111. static int koneplus_set_profile_settings(struct usb_device *usb_dev,
  112. struct koneplus_profile_settings const *settings)
  113. {
  114. return koneplus_send(usb_dev, KONEPLUS_USB_COMMAND_PROFILE_SETTINGS,
  115. settings, sizeof(struct koneplus_profile_settings));
  116. }
  117. static int koneplus_get_profile_buttons(struct usb_device *usb_dev,
  118. struct koneplus_profile_buttons *buf, int number)
  119. {
  120. int retval;
  121. retval = koneplus_select_profile(usb_dev, number,
  122. KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
  123. if (retval)
  124. return retval;
  125. return roccat_common_receive(usb_dev, KONEPLUS_USB_COMMAND_PROFILE_BUTTONS,
  126. buf, sizeof(struct koneplus_profile_buttons));
  127. }
  128. static int koneplus_set_profile_buttons(struct usb_device *usb_dev,
  129. struct koneplus_profile_buttons const *buttons)
  130. {
  131. return koneplus_send(usb_dev, KONEPLUS_USB_COMMAND_PROFILE_BUTTONS,
  132. buttons, sizeof(struct koneplus_profile_buttons));
  133. }
  134. /* retval is 0-4 on success, < 0 on error */
  135. static int koneplus_get_actual_profile(struct usb_device *usb_dev)
  136. {
  137. struct koneplus_actual_profile buf;
  138. int retval;
  139. retval = roccat_common_receive(usb_dev, KONEPLUS_USB_COMMAND_ACTUAL_PROFILE,
  140. &buf, sizeof(struct koneplus_actual_profile));
  141. return retval ? retval : buf.actual_profile;
  142. }
  143. static int koneplus_set_actual_profile(struct usb_device *usb_dev,
  144. int new_profile)
  145. {
  146. struct koneplus_actual_profile buf;
  147. buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
  148. buf.size = sizeof(struct koneplus_actual_profile);
  149. buf.actual_profile = new_profile;
  150. return koneplus_send(usb_dev, KONEPLUS_USB_COMMAND_ACTUAL_PROFILE,
  151. &buf, sizeof(struct koneplus_actual_profile));
  152. }
  153. static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
  154. char *buf, loff_t off, size_t count,
  155. size_t real_size, uint command)
  156. {
  157. struct device *dev =
  158. container_of(kobj, struct device, kobj)->parent->parent;
  159. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  160. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  161. int retval;
  162. if (off >= real_size)
  163. return 0;
  164. if (off != 0 || count != real_size)
  165. return -EINVAL;
  166. mutex_lock(&koneplus->koneplus_lock);
  167. retval = roccat_common_receive(usb_dev, command, buf, real_size);
  168. mutex_unlock(&koneplus->koneplus_lock);
  169. if (retval)
  170. return retval;
  171. return real_size;
  172. }
  173. static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
  174. void const *buf, loff_t off, size_t count,
  175. size_t real_size, uint command)
  176. {
  177. struct device *dev =
  178. container_of(kobj, struct device, kobj)->parent->parent;
  179. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  180. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  181. int retval;
  182. if (off != 0 || count != real_size)
  183. return -EINVAL;
  184. mutex_lock(&koneplus->koneplus_lock);
  185. retval = koneplus_send(usb_dev, command, buf, real_size);
  186. mutex_unlock(&koneplus->koneplus_lock);
  187. if (retval)
  188. return retval;
  189. return real_size;
  190. }
  191. static ssize_t koneplus_sysfs_write_macro(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_macro), KONEPLUS_USB_COMMAND_MACRO);
  197. }
  198. static ssize_t koneplus_sysfs_read_sensor(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_sensor), KONEPLUS_USB_COMMAND_SENSOR);
  204. }
  205. static ssize_t koneplus_sysfs_write_sensor(struct file *fp,
  206. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  207. loff_t off, size_t count)
  208. {
  209. return koneplus_sysfs_write(fp, kobj, buf, off, count,
  210. sizeof(struct koneplus_sensor), KONEPLUS_USB_COMMAND_SENSOR);
  211. }
  212. static ssize_t koneplus_sysfs_write_tcu(struct file *fp,
  213. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  214. loff_t off, size_t count)
  215. {
  216. return koneplus_sysfs_write(fp, kobj, buf, off, count,
  217. sizeof(struct koneplus_tcu), KONEPLUS_USB_COMMAND_TCU);
  218. }
  219. static ssize_t koneplus_sysfs_read_tcu_image(struct file *fp,
  220. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  221. loff_t off, size_t count)
  222. {
  223. return koneplus_sysfs_read(fp, kobj, buf, off, count,
  224. sizeof(struct koneplus_tcu_image), KONEPLUS_USB_COMMAND_TCU);
  225. }
  226. static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
  227. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  228. loff_t off, size_t count)
  229. {
  230. struct device *dev =
  231. container_of(kobj, struct device, kobj)->parent->parent;
  232. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  233. if (off >= sizeof(struct koneplus_profile_settings))
  234. return 0;
  235. if (off + count > sizeof(struct koneplus_profile_settings))
  236. count = sizeof(struct koneplus_profile_settings) - off;
  237. mutex_lock(&koneplus->koneplus_lock);
  238. memcpy(buf, ((char const *)&koneplus->profile_settings[*(uint *)(attr->private)]) + off,
  239. count);
  240. mutex_unlock(&koneplus->koneplus_lock);
  241. return count;
  242. }
  243. static ssize_t koneplus_sysfs_write_profile_settings(struct file *fp,
  244. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  245. loff_t off, size_t count)
  246. {
  247. struct device *dev =
  248. container_of(kobj, struct device, kobj)->parent->parent;
  249. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  250. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  251. int retval = 0;
  252. int difference;
  253. int profile_number;
  254. struct koneplus_profile_settings *profile_settings;
  255. if (off != 0 || count != sizeof(struct koneplus_profile_settings))
  256. return -EINVAL;
  257. profile_number = ((struct koneplus_profile_settings const *)buf)->number;
  258. profile_settings = &koneplus->profile_settings[profile_number];
  259. mutex_lock(&koneplus->koneplus_lock);
  260. difference = memcmp(buf, profile_settings,
  261. sizeof(struct koneplus_profile_settings));
  262. if (difference) {
  263. retval = koneplus_set_profile_settings(usb_dev,
  264. (struct koneplus_profile_settings const *)buf);
  265. if (!retval)
  266. memcpy(profile_settings, buf,
  267. sizeof(struct koneplus_profile_settings));
  268. }
  269. mutex_unlock(&koneplus->koneplus_lock);
  270. if (retval)
  271. return retval;
  272. return sizeof(struct koneplus_profile_settings);
  273. }
  274. static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
  275. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  276. loff_t off, size_t count)
  277. {
  278. struct device *dev =
  279. container_of(kobj, struct device, kobj)->parent->parent;
  280. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  281. if (off >= sizeof(struct koneplus_profile_buttons))
  282. return 0;
  283. if (off + count > sizeof(struct koneplus_profile_buttons))
  284. count = sizeof(struct koneplus_profile_buttons) - off;
  285. mutex_lock(&koneplus->koneplus_lock);
  286. memcpy(buf, ((char const *)&koneplus->profile_buttons[*(uint *)(attr->private)]) + off,
  287. count);
  288. mutex_unlock(&koneplus->koneplus_lock);
  289. return count;
  290. }
  291. static ssize_t koneplus_sysfs_write_profile_buttons(struct file *fp,
  292. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  293. loff_t off, size_t count)
  294. {
  295. struct device *dev =
  296. container_of(kobj, struct device, kobj)->parent->parent;
  297. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  298. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  299. int retval = 0;
  300. int difference;
  301. uint profile_number;
  302. struct koneplus_profile_buttons *profile_buttons;
  303. if (off != 0 || count != sizeof(struct koneplus_profile_buttons))
  304. return -EINVAL;
  305. profile_number = ((struct koneplus_profile_buttons const *)buf)->number;
  306. profile_buttons = &koneplus->profile_buttons[profile_number];
  307. mutex_lock(&koneplus->koneplus_lock);
  308. difference = memcmp(buf, profile_buttons,
  309. sizeof(struct koneplus_profile_buttons));
  310. if (difference) {
  311. retval = koneplus_set_profile_buttons(usb_dev,
  312. (struct koneplus_profile_buttons const *)buf);
  313. if (!retval)
  314. memcpy(profile_buttons, buf,
  315. sizeof(struct koneplus_profile_buttons));
  316. }
  317. mutex_unlock(&koneplus->koneplus_lock);
  318. if (retval)
  319. return retval;
  320. return sizeof(struct koneplus_profile_buttons);
  321. }
  322. static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
  323. struct device_attribute *attr, char *buf)
  324. {
  325. struct koneplus_device *koneplus =
  326. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  327. return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
  328. }
  329. static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
  330. struct device_attribute *attr, char const *buf, size_t size)
  331. {
  332. struct koneplus_device *koneplus;
  333. struct usb_device *usb_dev;
  334. unsigned long profile;
  335. int retval;
  336. struct koneplus_roccat_report roccat_report;
  337. dev = dev->parent->parent;
  338. koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  339. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  340. retval = strict_strtoul(buf, 10, &profile);
  341. if (retval)
  342. return retval;
  343. mutex_lock(&koneplus->koneplus_lock);
  344. retval = koneplus_set_actual_profile(usb_dev, profile);
  345. if (retval) {
  346. mutex_unlock(&koneplus->koneplus_lock);
  347. return retval;
  348. }
  349. koneplus->actual_profile = profile;
  350. roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
  351. roccat_report.data1 = profile + 1;
  352. roccat_report.data2 = 0;
  353. roccat_report.profile = profile + 1;
  354. roccat_report_event(koneplus->chrdev_minor,
  355. (uint8_t const *)&roccat_report);
  356. mutex_unlock(&koneplus->koneplus_lock);
  357. return size;
  358. }
  359. static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
  360. struct device_attribute *attr, char *buf)
  361. {
  362. struct koneplus_device *koneplus =
  363. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  364. return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->info.firmware_version);
  365. }
  366. static struct device_attribute koneplus_attributes[] = {
  367. __ATTR(actual_profile, 0660,
  368. koneplus_sysfs_show_actual_profile,
  369. koneplus_sysfs_set_actual_profile),
  370. __ATTR(startup_profile, 0660,
  371. koneplus_sysfs_show_actual_profile,
  372. koneplus_sysfs_set_actual_profile),
  373. __ATTR(firmware_version, 0440,
  374. koneplus_sysfs_show_firmware_version, NULL),
  375. __ATTR_NULL
  376. };
  377. static struct bin_attribute koneplus_bin_attributes[] = {
  378. {
  379. .attr = { .name = "sensor", .mode = 0660 },
  380. .size = sizeof(struct koneplus_sensor),
  381. .read = koneplus_sysfs_read_sensor,
  382. .write = koneplus_sysfs_write_sensor
  383. },
  384. {
  385. .attr = { .name = "tcu", .mode = 0220 },
  386. .size = sizeof(struct koneplus_tcu),
  387. .write = koneplus_sysfs_write_tcu
  388. },
  389. {
  390. .attr = { .name = "tcu_image", .mode = 0440 },
  391. .size = sizeof(struct koneplus_tcu_image),
  392. .read = koneplus_sysfs_read_tcu_image
  393. },
  394. {
  395. .attr = { .name = "profile_settings", .mode = 0220 },
  396. .size = sizeof(struct koneplus_profile_settings),
  397. .write = koneplus_sysfs_write_profile_settings
  398. },
  399. {
  400. .attr = { .name = "profile1_settings", .mode = 0440 },
  401. .size = sizeof(struct koneplus_profile_settings),
  402. .read = koneplus_sysfs_read_profilex_settings,
  403. .private = &profile_numbers[0]
  404. },
  405. {
  406. .attr = { .name = "profile2_settings", .mode = 0440 },
  407. .size = sizeof(struct koneplus_profile_settings),
  408. .read = koneplus_sysfs_read_profilex_settings,
  409. .private = &profile_numbers[1]
  410. },
  411. {
  412. .attr = { .name = "profile3_settings", .mode = 0440 },
  413. .size = sizeof(struct koneplus_profile_settings),
  414. .read = koneplus_sysfs_read_profilex_settings,
  415. .private = &profile_numbers[2]
  416. },
  417. {
  418. .attr = { .name = "profile4_settings", .mode = 0440 },
  419. .size = sizeof(struct koneplus_profile_settings),
  420. .read = koneplus_sysfs_read_profilex_settings,
  421. .private = &profile_numbers[3]
  422. },
  423. {
  424. .attr = { .name = "profile5_settings", .mode = 0440 },
  425. .size = sizeof(struct koneplus_profile_settings),
  426. .read = koneplus_sysfs_read_profilex_settings,
  427. .private = &profile_numbers[4]
  428. },
  429. {
  430. .attr = { .name = "profile_buttons", .mode = 0220 },
  431. .size = sizeof(struct koneplus_profile_buttons),
  432. .write = koneplus_sysfs_write_profile_buttons
  433. },
  434. {
  435. .attr = { .name = "profile1_buttons", .mode = 0440 },
  436. .size = sizeof(struct koneplus_profile_buttons),
  437. .read = koneplus_sysfs_read_profilex_buttons,
  438. .private = &profile_numbers[0]
  439. },
  440. {
  441. .attr = { .name = "profile2_buttons", .mode = 0440 },
  442. .size = sizeof(struct koneplus_profile_buttons),
  443. .read = koneplus_sysfs_read_profilex_buttons,
  444. .private = &profile_numbers[1]
  445. },
  446. {
  447. .attr = { .name = "profile3_buttons", .mode = 0440 },
  448. .size = sizeof(struct koneplus_profile_buttons),
  449. .read = koneplus_sysfs_read_profilex_buttons,
  450. .private = &profile_numbers[2]
  451. },
  452. {
  453. .attr = { .name = "profile4_buttons", .mode = 0440 },
  454. .size = sizeof(struct koneplus_profile_buttons),
  455. .read = koneplus_sysfs_read_profilex_buttons,
  456. .private = &profile_numbers[3]
  457. },
  458. {
  459. .attr = { .name = "profile5_buttons", .mode = 0440 },
  460. .size = sizeof(struct koneplus_profile_buttons),
  461. .read = koneplus_sysfs_read_profilex_buttons,
  462. .private = &profile_numbers[4]
  463. },
  464. {
  465. .attr = { .name = "macro", .mode = 0220 },
  466. .size = sizeof(struct koneplus_macro),
  467. .write = koneplus_sysfs_write_macro
  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. koneplus_keep_values_up_to_date(koneplus, data);
  619. if (koneplus->roccat_claimed)
  620. koneplus_report_to_chrdev(koneplus, data);
  621. return 0;
  622. }
  623. static const struct hid_device_id koneplus_devices[] = {
  624. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
  625. { }
  626. };
  627. MODULE_DEVICE_TABLE(hid, koneplus_devices);
  628. static struct hid_driver koneplus_driver = {
  629. .name = "koneplus",
  630. .id_table = koneplus_devices,
  631. .probe = koneplus_probe,
  632. .remove = koneplus_remove,
  633. .raw_event = koneplus_raw_event
  634. };
  635. static int __init koneplus_init(void)
  636. {
  637. int retval;
  638. /* class name has to be same as driver name */
  639. koneplus_class = class_create(THIS_MODULE, "koneplus");
  640. if (IS_ERR(koneplus_class))
  641. return PTR_ERR(koneplus_class);
  642. koneplus_class->dev_attrs = koneplus_attributes;
  643. koneplus_class->dev_bin_attrs = koneplus_bin_attributes;
  644. retval = hid_register_driver(&koneplus_driver);
  645. if (retval)
  646. class_destroy(koneplus_class);
  647. return retval;
  648. }
  649. static void __exit koneplus_exit(void)
  650. {
  651. hid_unregister_driver(&koneplus_driver);
  652. class_destroy(koneplus_class);
  653. }
  654. module_init(koneplus_init);
  655. module_exit(koneplus_exit);
  656. MODULE_AUTHOR("Stefan Achatz");
  657. MODULE_DESCRIPTION("USB Roccat Kone[+] driver");
  658. MODULE_LICENSE("GPL v2");