hid-roccat-koneplus.c 23 KB

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