hid-roccat-pyra.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Roccat Pyra 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 Pyra is a mobile gamer mouse which comes in wired and wireless
  14. * variant. Wireless variant is not tested.
  15. * Userland tools can be found at http://sourceforge.net/projects/roccat
  16. */
  17. #include <linux/device.h>
  18. #include <linux/input.h>
  19. #include <linux/hid.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/hid-roccat.h>
  23. #include "hid-ids.h"
  24. #include "hid-roccat-common.h"
  25. #include "hid-roccat-pyra.h"
  26. static uint profile_numbers[5] = {0, 1, 2, 3, 4};
  27. /* pyra_class is used for creating sysfs attributes via roccat char device */
  28. static struct class *pyra_class;
  29. static void profile_activated(struct pyra_device *pyra,
  30. unsigned int new_profile)
  31. {
  32. if (new_profile >= ARRAY_SIZE(pyra->profile_settings))
  33. return;
  34. pyra->actual_profile = new_profile;
  35. pyra->actual_cpi = pyra->profile_settings[pyra->actual_profile].y_cpi;
  36. }
  37. static int pyra_send_control(struct usb_device *usb_dev, int value,
  38. enum pyra_control_requests request)
  39. {
  40. struct pyra_control control;
  41. if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS ||
  42. request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) &&
  43. (value < 0 || value > 4))
  44. return -EINVAL;
  45. control.command = PYRA_COMMAND_CONTROL;
  46. control.value = value;
  47. control.request = request;
  48. return roccat_common_send(usb_dev, PYRA_COMMAND_CONTROL,
  49. &control, sizeof(struct pyra_control));
  50. }
  51. static int pyra_receive_control_status(struct usb_device *usb_dev)
  52. {
  53. int retval;
  54. struct pyra_control control;
  55. do {
  56. msleep(10);
  57. retval = roccat_common_receive(usb_dev, PYRA_COMMAND_CONTROL,
  58. &control, sizeof(struct pyra_control));
  59. /* requested too early, try again */
  60. } while (retval == -EPROTO);
  61. if (!retval && control.command == PYRA_COMMAND_CONTROL &&
  62. control.request == PYRA_CONTROL_REQUEST_STATUS &&
  63. control.value == 1)
  64. return 0;
  65. else {
  66. hid_err(usb_dev, "receive control status: unknown response 0x%x 0x%x\n",
  67. control.request, control.value);
  68. return retval ? retval : -EINVAL;
  69. }
  70. }
  71. static int pyra_get_profile_settings(struct usb_device *usb_dev,
  72. struct pyra_profile_settings *buf, int number)
  73. {
  74. int retval;
  75. retval = pyra_send_control(usb_dev, number,
  76. PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
  77. if (retval)
  78. return retval;
  79. return roccat_common_receive(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS,
  80. buf, sizeof(struct pyra_profile_settings));
  81. }
  82. static int pyra_get_profile_buttons(struct usb_device *usb_dev,
  83. struct pyra_profile_buttons *buf, int number)
  84. {
  85. int retval;
  86. retval = pyra_send_control(usb_dev, number,
  87. PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
  88. if (retval)
  89. return retval;
  90. return roccat_common_receive(usb_dev, PYRA_COMMAND_PROFILE_BUTTONS,
  91. buf, sizeof(struct pyra_profile_buttons));
  92. }
  93. static int pyra_get_settings(struct usb_device *usb_dev,
  94. struct pyra_settings *buf)
  95. {
  96. return roccat_common_receive(usb_dev, PYRA_COMMAND_SETTINGS,
  97. buf, sizeof(struct pyra_settings));
  98. }
  99. static int pyra_get_info(struct usb_device *usb_dev, struct pyra_info *buf)
  100. {
  101. return roccat_common_receive(usb_dev, PYRA_COMMAND_INFO,
  102. buf, sizeof(struct pyra_info));
  103. }
  104. static int pyra_send(struct usb_device *usb_dev, uint command,
  105. void const *buf, uint size)
  106. {
  107. int retval;
  108. retval = roccat_common_send(usb_dev, command, buf, size);
  109. if (retval)
  110. return retval;
  111. return pyra_receive_control_status(usb_dev);
  112. }
  113. static int pyra_set_profile_settings(struct usb_device *usb_dev,
  114. struct pyra_profile_settings const *settings)
  115. {
  116. return pyra_send(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS, settings,
  117. sizeof(struct pyra_profile_settings));
  118. }
  119. static int pyra_set_profile_buttons(struct usb_device *usb_dev,
  120. struct pyra_profile_buttons const *buttons)
  121. {
  122. return pyra_send(usb_dev, PYRA_COMMAND_PROFILE_BUTTONS, buttons,
  123. sizeof(struct pyra_profile_buttons));
  124. }
  125. static int pyra_set_settings(struct usb_device *usb_dev,
  126. struct pyra_settings const *settings)
  127. {
  128. return pyra_send(usb_dev, PYRA_COMMAND_SETTINGS, settings,
  129. sizeof(struct pyra_settings));
  130. }
  131. static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
  132. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  133. loff_t off, size_t count)
  134. {
  135. struct device *dev =
  136. container_of(kobj, struct device, kobj)->parent->parent;
  137. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  138. if (off >= sizeof(struct pyra_profile_settings))
  139. return 0;
  140. if (off + count > sizeof(struct pyra_profile_settings))
  141. count = sizeof(struct pyra_profile_settings) - off;
  142. mutex_lock(&pyra->pyra_lock);
  143. memcpy(buf, ((char const *)&pyra->profile_settings[*(uint *)(attr->private)]) + off,
  144. count);
  145. mutex_unlock(&pyra->pyra_lock);
  146. return count;
  147. }
  148. static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
  149. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  150. loff_t off, size_t count)
  151. {
  152. struct device *dev =
  153. container_of(kobj, struct device, kobj)->parent->parent;
  154. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  155. if (off >= sizeof(struct pyra_profile_buttons))
  156. return 0;
  157. if (off + count > sizeof(struct pyra_profile_buttons))
  158. count = sizeof(struct pyra_profile_buttons) - off;
  159. mutex_lock(&pyra->pyra_lock);
  160. memcpy(buf, ((char const *)&pyra->profile_buttons[*(uint *)(attr->private)]) + off,
  161. count);
  162. mutex_unlock(&pyra->pyra_lock);
  163. return count;
  164. }
  165. static ssize_t pyra_sysfs_write_profile_settings(struct file *fp,
  166. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  167. loff_t off, size_t count)
  168. {
  169. struct device *dev =
  170. container_of(kobj, struct device, kobj)->parent->parent;
  171. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  172. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  173. int retval = 0;
  174. int difference;
  175. int profile_number;
  176. struct pyra_profile_settings *profile_settings;
  177. if (off != 0 || count != sizeof(struct pyra_profile_settings))
  178. return -EINVAL;
  179. profile_number = ((struct pyra_profile_settings const *)buf)->number;
  180. profile_settings = &pyra->profile_settings[profile_number];
  181. mutex_lock(&pyra->pyra_lock);
  182. difference = memcmp(buf, profile_settings,
  183. sizeof(struct pyra_profile_settings));
  184. if (difference) {
  185. retval = pyra_set_profile_settings(usb_dev,
  186. (struct pyra_profile_settings const *)buf);
  187. if (!retval)
  188. memcpy(profile_settings, buf,
  189. sizeof(struct pyra_profile_settings));
  190. }
  191. mutex_unlock(&pyra->pyra_lock);
  192. if (retval)
  193. return retval;
  194. return sizeof(struct pyra_profile_settings);
  195. }
  196. static ssize_t pyra_sysfs_write_profile_buttons(struct file *fp,
  197. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  198. loff_t off, size_t count)
  199. {
  200. struct device *dev =
  201. container_of(kobj, struct device, kobj)->parent->parent;
  202. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  203. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  204. int retval = 0;
  205. int difference;
  206. int profile_number;
  207. struct pyra_profile_buttons *profile_buttons;
  208. if (off != 0 || count != sizeof(struct pyra_profile_buttons))
  209. return -EINVAL;
  210. profile_number = ((struct pyra_profile_buttons const *)buf)->number;
  211. profile_buttons = &pyra->profile_buttons[profile_number];
  212. mutex_lock(&pyra->pyra_lock);
  213. difference = memcmp(buf, profile_buttons,
  214. sizeof(struct pyra_profile_buttons));
  215. if (difference) {
  216. retval = pyra_set_profile_buttons(usb_dev,
  217. (struct pyra_profile_buttons const *)buf);
  218. if (!retval)
  219. memcpy(profile_buttons, buf,
  220. sizeof(struct pyra_profile_buttons));
  221. }
  222. mutex_unlock(&pyra->pyra_lock);
  223. if (retval)
  224. return retval;
  225. return sizeof(struct pyra_profile_buttons);
  226. }
  227. static ssize_t pyra_sysfs_read_settings(struct file *fp,
  228. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  229. loff_t off, size_t count)
  230. {
  231. struct device *dev =
  232. container_of(kobj, struct device, kobj)->parent->parent;
  233. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  234. if (off >= sizeof(struct pyra_settings))
  235. return 0;
  236. if (off + count > sizeof(struct pyra_settings))
  237. count = sizeof(struct pyra_settings) - off;
  238. mutex_lock(&pyra->pyra_lock);
  239. memcpy(buf, ((char const *)&pyra->settings) + off, count);
  240. mutex_unlock(&pyra->pyra_lock);
  241. return count;
  242. }
  243. static ssize_t pyra_sysfs_write_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 pyra_device *pyra = 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. struct pyra_roccat_report roccat_report;
  254. struct pyra_settings const *settings;
  255. if (off != 0 || count != sizeof(struct pyra_settings))
  256. return -EINVAL;
  257. settings = (struct pyra_settings const *)buf;
  258. if (settings->startup_profile >= ARRAY_SIZE(pyra->profile_settings))
  259. return -EINVAL;
  260. mutex_lock(&pyra->pyra_lock);
  261. difference = memcmp(buf, &pyra->settings, sizeof(struct pyra_settings));
  262. if (difference) {
  263. retval = pyra_set_settings(usb_dev,
  264. (struct pyra_settings const *)buf);
  265. if (retval) {
  266. mutex_unlock(&pyra->pyra_lock);
  267. return retval;
  268. }
  269. memcpy(&pyra->settings, buf,
  270. sizeof(struct pyra_settings));
  271. profile_activated(pyra, pyra->settings.startup_profile);
  272. roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
  273. roccat_report.value = pyra->settings.startup_profile + 1;
  274. roccat_report.key = 0;
  275. roccat_report_event(pyra->chrdev_minor,
  276. (uint8_t const *)&roccat_report);
  277. }
  278. mutex_unlock(&pyra->pyra_lock);
  279. return sizeof(struct pyra_settings);
  280. }
  281. static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
  282. struct device_attribute *attr, char *buf)
  283. {
  284. struct pyra_device *pyra =
  285. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  286. return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
  287. }
  288. static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
  289. struct device_attribute *attr, char *buf)
  290. {
  291. struct pyra_device *pyra =
  292. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  293. return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_profile);
  294. }
  295. static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
  296. struct device_attribute *attr, char *buf)
  297. {
  298. struct pyra_device *pyra =
  299. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  300. return snprintf(buf, PAGE_SIZE, "%d\n", pyra->firmware_version);
  301. }
  302. static ssize_t pyra_sysfs_show_startup_profile(struct device *dev,
  303. struct device_attribute *attr, char *buf)
  304. {
  305. struct pyra_device *pyra =
  306. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  307. return snprintf(buf, PAGE_SIZE, "%d\n", pyra->settings.startup_profile);
  308. }
  309. static struct device_attribute pyra_attributes[] = {
  310. __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL),
  311. __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL),
  312. __ATTR(firmware_version, 0440,
  313. pyra_sysfs_show_firmware_version, NULL),
  314. __ATTR(startup_profile, 0440,
  315. pyra_sysfs_show_startup_profile, NULL),
  316. __ATTR_NULL
  317. };
  318. static struct bin_attribute pyra_bin_attributes[] = {
  319. {
  320. .attr = { .name = "profile_settings", .mode = 0220 },
  321. .size = sizeof(struct pyra_profile_settings),
  322. .write = pyra_sysfs_write_profile_settings
  323. },
  324. {
  325. .attr = { .name = "profile1_settings", .mode = 0440 },
  326. .size = sizeof(struct pyra_profile_settings),
  327. .read = pyra_sysfs_read_profilex_settings,
  328. .private = &profile_numbers[0]
  329. },
  330. {
  331. .attr = { .name = "profile2_settings", .mode = 0440 },
  332. .size = sizeof(struct pyra_profile_settings),
  333. .read = pyra_sysfs_read_profilex_settings,
  334. .private = &profile_numbers[1]
  335. },
  336. {
  337. .attr = { .name = "profile3_settings", .mode = 0440 },
  338. .size = sizeof(struct pyra_profile_settings),
  339. .read = pyra_sysfs_read_profilex_settings,
  340. .private = &profile_numbers[2]
  341. },
  342. {
  343. .attr = { .name = "profile4_settings", .mode = 0440 },
  344. .size = sizeof(struct pyra_profile_settings),
  345. .read = pyra_sysfs_read_profilex_settings,
  346. .private = &profile_numbers[3]
  347. },
  348. {
  349. .attr = { .name = "profile5_settings", .mode = 0440 },
  350. .size = sizeof(struct pyra_profile_settings),
  351. .read = pyra_sysfs_read_profilex_settings,
  352. .private = &profile_numbers[4]
  353. },
  354. {
  355. .attr = { .name = "profile_buttons", .mode = 0220 },
  356. .size = sizeof(struct pyra_profile_buttons),
  357. .write = pyra_sysfs_write_profile_buttons
  358. },
  359. {
  360. .attr = { .name = "profile1_buttons", .mode = 0440 },
  361. .size = sizeof(struct pyra_profile_buttons),
  362. .read = pyra_sysfs_read_profilex_buttons,
  363. .private = &profile_numbers[0]
  364. },
  365. {
  366. .attr = { .name = "profile2_buttons", .mode = 0440 },
  367. .size = sizeof(struct pyra_profile_buttons),
  368. .read = pyra_sysfs_read_profilex_buttons,
  369. .private = &profile_numbers[1]
  370. },
  371. {
  372. .attr = { .name = "profile3_buttons", .mode = 0440 },
  373. .size = sizeof(struct pyra_profile_buttons),
  374. .read = pyra_sysfs_read_profilex_buttons,
  375. .private = &profile_numbers[2]
  376. },
  377. {
  378. .attr = { .name = "profile4_buttons", .mode = 0440 },
  379. .size = sizeof(struct pyra_profile_buttons),
  380. .read = pyra_sysfs_read_profilex_buttons,
  381. .private = &profile_numbers[3]
  382. },
  383. {
  384. .attr = { .name = "profile5_buttons", .mode = 0440 },
  385. .size = sizeof(struct pyra_profile_buttons),
  386. .read = pyra_sysfs_read_profilex_buttons,
  387. .private = &profile_numbers[4]
  388. },
  389. {
  390. .attr = { .name = "settings", .mode = 0660 },
  391. .size = sizeof(struct pyra_settings),
  392. .read = pyra_sysfs_read_settings,
  393. .write = pyra_sysfs_write_settings
  394. },
  395. __ATTR_NULL
  396. };
  397. static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
  398. struct pyra_device *pyra)
  399. {
  400. struct pyra_info info;
  401. int retval, i;
  402. mutex_init(&pyra->pyra_lock);
  403. retval = pyra_get_info(usb_dev, &info);
  404. if (retval)
  405. return retval;
  406. pyra->firmware_version = info.firmware_version;
  407. retval = pyra_get_settings(usb_dev, &pyra->settings);
  408. if (retval)
  409. return retval;
  410. for (i = 0; i < 5; ++i) {
  411. retval = pyra_get_profile_settings(usb_dev,
  412. &pyra->profile_settings[i], i);
  413. if (retval)
  414. return retval;
  415. retval = pyra_get_profile_buttons(usb_dev,
  416. &pyra->profile_buttons[i], i);
  417. if (retval)
  418. return retval;
  419. }
  420. profile_activated(pyra, pyra->settings.startup_profile);
  421. return 0;
  422. }
  423. static int pyra_init_specials(struct hid_device *hdev)
  424. {
  425. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  426. struct usb_device *usb_dev = interface_to_usbdev(intf);
  427. struct pyra_device *pyra;
  428. int retval;
  429. if (intf->cur_altsetting->desc.bInterfaceProtocol
  430. == USB_INTERFACE_PROTOCOL_MOUSE) {
  431. pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
  432. if (!pyra) {
  433. hid_err(hdev, "can't alloc device descriptor\n");
  434. return -ENOMEM;
  435. }
  436. hid_set_drvdata(hdev, pyra);
  437. retval = pyra_init_pyra_device_struct(usb_dev, pyra);
  438. if (retval) {
  439. hid_err(hdev, "couldn't init struct pyra_device\n");
  440. goto exit_free;
  441. }
  442. retval = roccat_connect(pyra_class, hdev,
  443. sizeof(struct pyra_roccat_report));
  444. if (retval < 0) {
  445. hid_err(hdev, "couldn't init char dev\n");
  446. } else {
  447. pyra->chrdev_minor = retval;
  448. pyra->roccat_claimed = 1;
  449. }
  450. } else {
  451. hid_set_drvdata(hdev, NULL);
  452. }
  453. return 0;
  454. exit_free:
  455. kfree(pyra);
  456. return retval;
  457. }
  458. static void pyra_remove_specials(struct hid_device *hdev)
  459. {
  460. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  461. struct pyra_device *pyra;
  462. if (intf->cur_altsetting->desc.bInterfaceProtocol
  463. == USB_INTERFACE_PROTOCOL_MOUSE) {
  464. pyra = hid_get_drvdata(hdev);
  465. if (pyra->roccat_claimed)
  466. roccat_disconnect(pyra->chrdev_minor);
  467. kfree(hid_get_drvdata(hdev));
  468. }
  469. }
  470. static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
  471. {
  472. int retval;
  473. retval = hid_parse(hdev);
  474. if (retval) {
  475. hid_err(hdev, "parse failed\n");
  476. goto exit;
  477. }
  478. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  479. if (retval) {
  480. hid_err(hdev, "hw start failed\n");
  481. goto exit;
  482. }
  483. retval = pyra_init_specials(hdev);
  484. if (retval) {
  485. hid_err(hdev, "couldn't install mouse\n");
  486. goto exit_stop;
  487. }
  488. return 0;
  489. exit_stop:
  490. hid_hw_stop(hdev);
  491. exit:
  492. return retval;
  493. }
  494. static void pyra_remove(struct hid_device *hdev)
  495. {
  496. pyra_remove_specials(hdev);
  497. hid_hw_stop(hdev);
  498. }
  499. static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
  500. u8 const *data)
  501. {
  502. struct pyra_mouse_event_button const *button_event;
  503. switch (data[0]) {
  504. case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
  505. button_event = (struct pyra_mouse_event_button const *)data;
  506. switch (button_event->type) {
  507. case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
  508. profile_activated(pyra, button_event->data1 - 1);
  509. break;
  510. case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
  511. pyra->actual_cpi = button_event->data1;
  512. break;
  513. }
  514. break;
  515. }
  516. }
  517. static void pyra_report_to_chrdev(struct pyra_device const *pyra,
  518. u8 const *data)
  519. {
  520. struct pyra_roccat_report roccat_report;
  521. struct pyra_mouse_event_button const *button_event;
  522. if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
  523. return;
  524. button_event = (struct pyra_mouse_event_button const *)data;
  525. switch (button_event->type) {
  526. case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
  527. case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
  528. roccat_report.type = button_event->type;
  529. roccat_report.value = button_event->data1;
  530. roccat_report.key = 0;
  531. roccat_report_event(pyra->chrdev_minor,
  532. (uint8_t const *)&roccat_report);
  533. break;
  534. case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
  535. case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
  536. case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
  537. if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
  538. roccat_report.type = button_event->type;
  539. roccat_report.key = button_event->data1;
  540. /*
  541. * pyra reports profile numbers with range 1-5.
  542. * Keeping this behaviour.
  543. */
  544. roccat_report.value = pyra->actual_profile + 1;
  545. roccat_report_event(pyra->chrdev_minor,
  546. (uint8_t const *)&roccat_report);
  547. }
  548. break;
  549. }
  550. }
  551. static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
  552. u8 *data, int size)
  553. {
  554. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  555. struct pyra_device *pyra = hid_get_drvdata(hdev);
  556. if (intf->cur_altsetting->desc.bInterfaceProtocol
  557. != USB_INTERFACE_PROTOCOL_MOUSE)
  558. return 0;
  559. if (pyra == NULL)
  560. return 0;
  561. pyra_keep_values_up_to_date(pyra, data);
  562. if (pyra->roccat_claimed)
  563. pyra_report_to_chrdev(pyra, data);
  564. return 0;
  565. }
  566. static const struct hid_device_id pyra_devices[] = {
  567. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
  568. USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
  569. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
  570. USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
  571. { }
  572. };
  573. MODULE_DEVICE_TABLE(hid, pyra_devices);
  574. static struct hid_driver pyra_driver = {
  575. .name = "pyra",
  576. .id_table = pyra_devices,
  577. .probe = pyra_probe,
  578. .remove = pyra_remove,
  579. .raw_event = pyra_raw_event
  580. };
  581. static int __init pyra_init(void)
  582. {
  583. int retval;
  584. /* class name has to be same as driver name */
  585. pyra_class = class_create(THIS_MODULE, "pyra");
  586. if (IS_ERR(pyra_class))
  587. return PTR_ERR(pyra_class);
  588. pyra_class->dev_attrs = pyra_attributes;
  589. pyra_class->dev_bin_attrs = pyra_bin_attributes;
  590. retval = hid_register_driver(&pyra_driver);
  591. if (retval)
  592. class_destroy(pyra_class);
  593. return retval;
  594. }
  595. static void __exit pyra_exit(void)
  596. {
  597. hid_unregister_driver(&pyra_driver);
  598. class_destroy(pyra_class);
  599. }
  600. module_init(pyra_init);
  601. module_exit(pyra_exit);
  602. MODULE_AUTHOR("Stefan Achatz");
  603. MODULE_DESCRIPTION("USB Roccat Pyra driver");
  604. MODULE_LICENSE("GPL v2");