hid-roccat-koneplus.c 23 KB

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