hid-roccat-pyra.c 19 KB

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