hid-roccat-koneplus.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * Roccat Kone[+] driver for Linux
  3. *
  4. * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. */
  12. /*
  13. * Roccat Kone[+] is an updated/improved version of the Kone with more memory
  14. * and functionality and without the non-standard behaviours the Kone had.
  15. */
  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_startup_profile(struct usb_device *usb_dev)
  136. {
  137. struct koneplus_startup_profile buf;
  138. int retval;
  139. retval = roccat_common_receive(usb_dev, KONEPLUS_USB_COMMAND_STARTUP_PROFILE,
  140. &buf, sizeof(struct koneplus_startup_profile));
  141. return retval ? retval : buf.startup_profile;
  142. }
  143. static int koneplus_set_startup_profile(struct usb_device *usb_dev,
  144. int startup_profile)
  145. {
  146. struct koneplus_startup_profile buf;
  147. buf.command = KONEPLUS_COMMAND_STARTUP_PROFILE;
  148. buf.size = sizeof(struct koneplus_startup_profile);
  149. buf.startup_profile = startup_profile;
  150. return koneplus_send(usb_dev, KONEPLUS_USB_COMMAND_STARTUP_PROFILE,
  151. &buf, sizeof(struct koneplus_profile_buttons));
  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_startup_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->startup_profile);
  328. }
  329. static ssize_t koneplus_sysfs_set_startup_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. dev = dev->parent->parent;
  337. koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  338. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  339. retval = strict_strtoul(buf, 10, &profile);
  340. if (retval)
  341. return retval;
  342. mutex_lock(&koneplus->koneplus_lock);
  343. retval = koneplus_set_startup_profile(usb_dev, profile);
  344. mutex_unlock(&koneplus->koneplus_lock);
  345. if (retval)
  346. return retval;
  347. return size;
  348. }
  349. static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
  350. struct device_attribute *attr, char *buf)
  351. {
  352. struct koneplus_device *koneplus =
  353. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  354. return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->actual_profile);
  355. }
  356. static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
  357. struct device_attribute *attr, char *buf)
  358. {
  359. struct koneplus_device *koneplus =
  360. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  361. return snprintf(buf, PAGE_SIZE, "%d\n", koneplus->info.firmware_version);
  362. }
  363. static struct device_attribute koneplus_attributes[] = {
  364. __ATTR(startup_profile, 0660,
  365. koneplus_sysfs_show_startup_profile,
  366. koneplus_sysfs_set_startup_profile),
  367. __ATTR(actual_profile, 0440,
  368. koneplus_sysfs_show_actual_profile, NULL),
  369. __ATTR(firmware_version, 0440,
  370. koneplus_sysfs_show_firmware_version, NULL),
  371. __ATTR_NULL
  372. };
  373. static struct bin_attribute koneplus_bin_attributes[] = {
  374. {
  375. .attr = { .name = "sensor", .mode = 0660 },
  376. .size = sizeof(struct koneplus_sensor),
  377. .read = koneplus_sysfs_read_sensor,
  378. .write = koneplus_sysfs_write_sensor
  379. },
  380. {
  381. .attr = { .name = "tcu", .mode = 0220 },
  382. .size = sizeof(struct koneplus_tcu),
  383. .write = koneplus_sysfs_write_tcu
  384. },
  385. {
  386. .attr = { .name = "tcu_image", .mode = 0440 },
  387. .size = sizeof(struct koneplus_tcu_image),
  388. .read = koneplus_sysfs_read_tcu_image
  389. },
  390. {
  391. .attr = { .name = "profile_settings", .mode = 0220 },
  392. .size = sizeof(struct koneplus_profile_settings),
  393. .write = koneplus_sysfs_write_profile_settings
  394. },
  395. {
  396. .attr = { .name = "profile1_settings", .mode = 0440 },
  397. .size = sizeof(struct koneplus_profile_settings),
  398. .read = koneplus_sysfs_read_profilex_settings,
  399. .private = &profile_numbers[0]
  400. },
  401. {
  402. .attr = { .name = "profile2_settings", .mode = 0440 },
  403. .size = sizeof(struct koneplus_profile_settings),
  404. .read = koneplus_sysfs_read_profilex_settings,
  405. .private = &profile_numbers[1]
  406. },
  407. {
  408. .attr = { .name = "profile3_settings", .mode = 0440 },
  409. .size = sizeof(struct koneplus_profile_settings),
  410. .read = koneplus_sysfs_read_profilex_settings,
  411. .private = &profile_numbers[2]
  412. },
  413. {
  414. .attr = { .name = "profile4_settings", .mode = 0440 },
  415. .size = sizeof(struct koneplus_profile_settings),
  416. .read = koneplus_sysfs_read_profilex_settings,
  417. .private = &profile_numbers[3]
  418. },
  419. {
  420. .attr = { .name = "profile5_settings", .mode = 0440 },
  421. .size = sizeof(struct koneplus_profile_settings),
  422. .read = koneplus_sysfs_read_profilex_settings,
  423. .private = &profile_numbers[4]
  424. },
  425. {
  426. .attr = { .name = "profile_buttons", .mode = 0220 },
  427. .size = sizeof(struct koneplus_profile_buttons),
  428. .write = koneplus_sysfs_write_profile_buttons
  429. },
  430. {
  431. .attr = { .name = "profile1_buttons", .mode = 0440 },
  432. .size = sizeof(struct koneplus_profile_buttons),
  433. .read = koneplus_sysfs_read_profilex_buttons,
  434. .private = &profile_numbers[0]
  435. },
  436. {
  437. .attr = { .name = "profile2_buttons", .mode = 0440 },
  438. .size = sizeof(struct koneplus_profile_buttons),
  439. .read = koneplus_sysfs_read_profilex_buttons,
  440. .private = &profile_numbers[1]
  441. },
  442. {
  443. .attr = { .name = "profile3_buttons", .mode = 0440 },
  444. .size = sizeof(struct koneplus_profile_buttons),
  445. .read = koneplus_sysfs_read_profilex_buttons,
  446. .private = &profile_numbers[2]
  447. },
  448. {
  449. .attr = { .name = "profile4_buttons", .mode = 0440 },
  450. .size = sizeof(struct koneplus_profile_buttons),
  451. .read = koneplus_sysfs_read_profilex_buttons,
  452. .private = &profile_numbers[3]
  453. },
  454. {
  455. .attr = { .name = "profile5_buttons", .mode = 0440 },
  456. .size = sizeof(struct koneplus_profile_buttons),
  457. .read = koneplus_sysfs_read_profilex_buttons,
  458. .private = &profile_numbers[4]
  459. },
  460. {
  461. .attr = { .name = "macro", .mode = 0220 },
  462. .size = sizeof(struct koneplus_macro),
  463. .write = koneplus_sysfs_write_macro
  464. },
  465. __ATTR_NULL
  466. };
  467. static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
  468. struct koneplus_device *koneplus)
  469. {
  470. int retval, i;
  471. static uint wait = 100; /* device will freeze with just 60 */
  472. mutex_init(&koneplus->koneplus_lock);
  473. koneplus->startup_profile = koneplus_get_startup_profile(usb_dev);
  474. if (koneplus->startup_profile < 0)
  475. return koneplus->startup_profile;
  476. msleep(wait);
  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. koneplus_profile_activated(koneplus, koneplus->startup_profile);
  493. return 0;
  494. }
  495. static int koneplus_init_specials(struct hid_device *hdev)
  496. {
  497. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  498. struct usb_device *usb_dev = interface_to_usbdev(intf);
  499. struct koneplus_device *koneplus;
  500. int retval;
  501. if (intf->cur_altsetting->desc.bInterfaceProtocol
  502. == USB_INTERFACE_PROTOCOL_MOUSE) {
  503. koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
  504. if (!koneplus) {
  505. hid_err(hdev, "can't alloc device descriptor\n");
  506. return -ENOMEM;
  507. }
  508. hid_set_drvdata(hdev, koneplus);
  509. retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
  510. if (retval) {
  511. hid_err(hdev, "couldn't init struct koneplus_device\n");
  512. goto exit_free;
  513. }
  514. retval = roccat_connect(koneplus_class, hdev,
  515. sizeof(struct koneplus_roccat_report));
  516. if (retval < 0) {
  517. hid_err(hdev, "couldn't init char dev\n");
  518. } else {
  519. koneplus->chrdev_minor = retval;
  520. koneplus->roccat_claimed = 1;
  521. }
  522. } else {
  523. hid_set_drvdata(hdev, NULL);
  524. }
  525. return 0;
  526. exit_free:
  527. kfree(koneplus);
  528. return retval;
  529. }
  530. static void koneplus_remove_specials(struct hid_device *hdev)
  531. {
  532. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  533. struct koneplus_device *koneplus;
  534. if (intf->cur_altsetting->desc.bInterfaceProtocol
  535. == USB_INTERFACE_PROTOCOL_MOUSE) {
  536. koneplus = hid_get_drvdata(hdev);
  537. if (koneplus->roccat_claimed)
  538. roccat_disconnect(koneplus->chrdev_minor);
  539. kfree(koneplus);
  540. }
  541. }
  542. static int koneplus_probe(struct hid_device *hdev,
  543. const struct hid_device_id *id)
  544. {
  545. int retval;
  546. retval = hid_parse(hdev);
  547. if (retval) {
  548. hid_err(hdev, "parse failed\n");
  549. goto exit;
  550. }
  551. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  552. if (retval) {
  553. hid_err(hdev, "hw start failed\n");
  554. goto exit;
  555. }
  556. retval = koneplus_init_specials(hdev);
  557. if (retval) {
  558. hid_err(hdev, "couldn't install mouse\n");
  559. goto exit_stop;
  560. }
  561. return 0;
  562. exit_stop:
  563. hid_hw_stop(hdev);
  564. exit:
  565. return retval;
  566. }
  567. static void koneplus_remove(struct hid_device *hdev)
  568. {
  569. koneplus_remove_specials(hdev);
  570. hid_hw_stop(hdev);
  571. }
  572. static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
  573. u8 const *data)
  574. {
  575. struct koneplus_mouse_report_button const *button_report;
  576. switch (data[0]) {
  577. case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
  578. button_report = (struct koneplus_mouse_report_button const *)data;
  579. switch (button_report->type) {
  580. case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
  581. koneplus_profile_activated(koneplus, button_report->data1 - 1);
  582. break;
  583. }
  584. break;
  585. }
  586. }
  587. static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
  588. u8 const *data)
  589. {
  590. struct koneplus_roccat_report roccat_report;
  591. struct koneplus_mouse_report_button const *button_report;
  592. if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
  593. return;
  594. button_report = (struct koneplus_mouse_report_button const *)data;
  595. if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
  596. button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
  597. button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
  598. return;
  599. roccat_report.type = button_report->type;
  600. roccat_report.data1 = button_report->data1;
  601. roccat_report.data2 = button_report->data2;
  602. roccat_report.profile = koneplus->actual_profile + 1;
  603. roccat_report_event(koneplus->chrdev_minor,
  604. (uint8_t const *)&roccat_report);
  605. }
  606. static int koneplus_raw_event(struct hid_device *hdev,
  607. struct hid_report *report, u8 *data, int size)
  608. {
  609. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  610. struct koneplus_device *koneplus = hid_get_drvdata(hdev);
  611. if (intf->cur_altsetting->desc.bInterfaceProtocol
  612. != USB_INTERFACE_PROTOCOL_MOUSE)
  613. return 0;
  614. koneplus_keep_values_up_to_date(koneplus, data);
  615. if (koneplus->roccat_claimed)
  616. koneplus_report_to_chrdev(koneplus, data);
  617. return 0;
  618. }
  619. static const struct hid_device_id koneplus_devices[] = {
  620. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
  621. { }
  622. };
  623. MODULE_DEVICE_TABLE(hid, koneplus_devices);
  624. static struct hid_driver koneplus_driver = {
  625. .name = "koneplus",
  626. .id_table = koneplus_devices,
  627. .probe = koneplus_probe,
  628. .remove = koneplus_remove,
  629. .raw_event = koneplus_raw_event
  630. };
  631. static int __init koneplus_init(void)
  632. {
  633. int retval;
  634. /* class name has to be same as driver name */
  635. koneplus_class = class_create(THIS_MODULE, "koneplus");
  636. if (IS_ERR(koneplus_class))
  637. return PTR_ERR(koneplus_class);
  638. koneplus_class->dev_attrs = koneplus_attributes;
  639. koneplus_class->dev_bin_attrs = koneplus_bin_attributes;
  640. retval = hid_register_driver(&koneplus_driver);
  641. if (retval)
  642. class_destroy(koneplus_class);
  643. return retval;
  644. }
  645. static void __exit koneplus_exit(void)
  646. {
  647. hid_unregister_driver(&koneplus_driver);
  648. class_destroy(koneplus_class);
  649. }
  650. module_init(koneplus_init);
  651. module_exit(koneplus_exit);
  652. MODULE_AUTHOR("Stefan Achatz");
  653. MODULE_DESCRIPTION("USB Roccat Kone[+] driver");
  654. MODULE_LICENSE("GPL v2");