hid-roccat-koneplus.c 23 KB

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