hid-roccat-pyra.c 21 KB

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