hid-roccat-pyra.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 void profile_activated(struct pyra_device *pyra,
  27. unsigned int new_profile)
  28. {
  29. pyra->actual_profile = new_profile;
  30. pyra->actual_cpi = pyra->profile_settings[pyra->actual_profile].y_cpi;
  31. }
  32. static int pyra_send_control(struct usb_device *usb_dev, int value,
  33. enum pyra_control_requests request)
  34. {
  35. int len;
  36. struct pyra_control control;
  37. if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS ||
  38. request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) &&
  39. (value < 0 || value > 4))
  40. return -EINVAL;
  41. control.command = PYRA_COMMAND_CONTROL;
  42. control.value = value;
  43. control.request = request;
  44. len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  45. USB_REQ_SET_CONFIGURATION,
  46. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  47. PYRA_USB_COMMAND_CONTROL, 0, (char *)&control,
  48. sizeof(struct pyra_control),
  49. USB_CTRL_SET_TIMEOUT);
  50. if (len != sizeof(struct pyra_control))
  51. return len;
  52. return 0;
  53. }
  54. static int pyra_receive_control_status(struct usb_device *usb_dev)
  55. {
  56. int len;
  57. struct pyra_control control;
  58. do {
  59. msleep(10);
  60. len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  61. USB_REQ_CLEAR_FEATURE,
  62. USB_TYPE_CLASS | USB_RECIP_INTERFACE |
  63. USB_DIR_IN,
  64. PYRA_USB_COMMAND_CONTROL, 0, (char *)&control,
  65. sizeof(struct pyra_control),
  66. USB_CTRL_SET_TIMEOUT);
  67. /* requested too early, try again */
  68. } while (len == -EPROTO);
  69. if (len == sizeof(struct pyra_control) &&
  70. control.command == PYRA_COMMAND_CONTROL &&
  71. control.request == PYRA_CONTROL_REQUEST_STATUS &&
  72. control.value == 1)
  73. return 0;
  74. else {
  75. hid_err(usb_dev, "receive control status: unknown response 0x%x 0x%x\n",
  76. control.request, control.value);
  77. return -EINVAL;
  78. }
  79. }
  80. static int pyra_get_profile_settings(struct usb_device *usb_dev,
  81. struct pyra_profile_settings *buf, int number)
  82. {
  83. int retval;
  84. retval = pyra_send_control(usb_dev, number,
  85. PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
  86. if (retval)
  87. return retval;
  88. retval = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  89. USB_REQ_CLEAR_FEATURE,
  90. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  91. PYRA_USB_COMMAND_PROFILE_SETTINGS, 0, (char *)buf,
  92. sizeof(struct pyra_profile_settings),
  93. USB_CTRL_SET_TIMEOUT);
  94. if (retval != sizeof(struct pyra_profile_settings))
  95. return retval;
  96. return 0;
  97. }
  98. static int pyra_get_profile_buttons(struct usb_device *usb_dev,
  99. struct pyra_profile_buttons *buf, int number)
  100. {
  101. int retval;
  102. retval = pyra_send_control(usb_dev, number,
  103. PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
  104. if (retval)
  105. return retval;
  106. retval = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  107. USB_REQ_CLEAR_FEATURE,
  108. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  109. PYRA_USB_COMMAND_PROFILE_BUTTONS, 0, (char *)buf,
  110. sizeof(struct pyra_profile_buttons),
  111. USB_CTRL_SET_TIMEOUT);
  112. if (retval != sizeof(struct pyra_profile_buttons))
  113. return retval;
  114. return 0;
  115. }
  116. static int pyra_get_settings(struct usb_device *usb_dev,
  117. struct pyra_settings *buf)
  118. {
  119. int len;
  120. len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  121. USB_REQ_CLEAR_FEATURE,
  122. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  123. PYRA_USB_COMMAND_SETTINGS, 0, buf,
  124. sizeof(struct pyra_settings), USB_CTRL_SET_TIMEOUT);
  125. if (len != sizeof(struct pyra_settings))
  126. return -EIO;
  127. return 0;
  128. }
  129. static int pyra_get_info(struct usb_device *usb_dev, struct pyra_info *buf)
  130. {
  131. int len;
  132. len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  133. USB_REQ_CLEAR_FEATURE,
  134. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  135. PYRA_USB_COMMAND_INFO, 0, buf,
  136. sizeof(struct pyra_info), USB_CTRL_SET_TIMEOUT);
  137. if (len != sizeof(struct pyra_info))
  138. return -EIO;
  139. return 0;
  140. }
  141. static int pyra_set_profile_settings(struct usb_device *usb_dev,
  142. struct pyra_profile_settings const *settings)
  143. {
  144. int len;
  145. len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  146. USB_REQ_SET_CONFIGURATION,
  147. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  148. PYRA_USB_COMMAND_PROFILE_SETTINGS, 0, (char *)settings,
  149. sizeof(struct pyra_profile_settings),
  150. USB_CTRL_SET_TIMEOUT);
  151. if (len != sizeof(struct pyra_profile_settings))
  152. return -EIO;
  153. if (pyra_receive_control_status(usb_dev))
  154. return -EIO;
  155. return 0;
  156. }
  157. static int pyra_set_profile_buttons(struct usb_device *usb_dev,
  158. struct pyra_profile_buttons const *buttons)
  159. {
  160. int len;
  161. len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  162. USB_REQ_SET_CONFIGURATION,
  163. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  164. PYRA_USB_COMMAND_PROFILE_BUTTONS, 0, (char *)buttons,
  165. sizeof(struct pyra_profile_buttons),
  166. USB_CTRL_SET_TIMEOUT);
  167. if (len != sizeof(struct pyra_profile_buttons))
  168. return -EIO;
  169. if (pyra_receive_control_status(usb_dev))
  170. return -EIO;
  171. return 0;
  172. }
  173. static int pyra_set_settings(struct usb_device *usb_dev,
  174. struct pyra_settings const *settings)
  175. {
  176. int len;
  177. len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  178. USB_REQ_SET_CONFIGURATION,
  179. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  180. PYRA_USB_COMMAND_SETTINGS, 0, (char *)settings,
  181. sizeof(struct pyra_settings), USB_CTRL_SET_TIMEOUT);
  182. if (len != sizeof(struct pyra_settings))
  183. return -EIO;
  184. if (pyra_receive_control_status(usb_dev))
  185. return -EIO;
  186. return 0;
  187. }
  188. static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
  189. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  190. loff_t off, size_t count, int number)
  191. {
  192. struct device *dev = container_of(kobj, struct device, kobj);
  193. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  194. if (off >= sizeof(struct pyra_profile_settings))
  195. return 0;
  196. if (off + count > sizeof(struct pyra_profile_settings))
  197. count = sizeof(struct pyra_profile_settings) - off;
  198. mutex_lock(&pyra->pyra_lock);
  199. memcpy(buf, ((char const *)&pyra->profile_settings[number]) + off,
  200. count);
  201. mutex_unlock(&pyra->pyra_lock);
  202. return count;
  203. }
  204. static ssize_t pyra_sysfs_read_profile1_settings(struct file *fp,
  205. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  206. loff_t off, size_t count)
  207. {
  208. return pyra_sysfs_read_profilex_settings(fp, kobj,
  209. attr, buf, off, count, 0);
  210. }
  211. static ssize_t pyra_sysfs_read_profile2_settings(struct file *fp,
  212. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  213. loff_t off, size_t count)
  214. {
  215. return pyra_sysfs_read_profilex_settings(fp, kobj,
  216. attr, buf, off, count, 1);
  217. }
  218. static ssize_t pyra_sysfs_read_profile3_settings(struct file *fp,
  219. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  220. loff_t off, size_t count)
  221. {
  222. return pyra_sysfs_read_profilex_settings(fp, kobj,
  223. attr, buf, off, count, 2);
  224. }
  225. static ssize_t pyra_sysfs_read_profile4_settings(struct file *fp,
  226. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  227. loff_t off, size_t count)
  228. {
  229. return pyra_sysfs_read_profilex_settings(fp, kobj,
  230. attr, buf, off, count, 3);
  231. }
  232. static ssize_t pyra_sysfs_read_profile5_settings(struct file *fp,
  233. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  234. loff_t off, size_t count)
  235. {
  236. return pyra_sysfs_read_profilex_settings(fp, kobj,
  237. attr, buf, off, count, 4);
  238. }
  239. static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
  240. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  241. loff_t off, size_t count, int number)
  242. {
  243. struct device *dev = container_of(kobj, struct device, kobj);
  244. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  245. if (off >= sizeof(struct pyra_profile_buttons))
  246. return 0;
  247. if (off + count > sizeof(struct pyra_profile_buttons))
  248. count = sizeof(struct pyra_profile_buttons) - off;
  249. mutex_lock(&pyra->pyra_lock);
  250. memcpy(buf, ((char const *)&pyra->profile_buttons[number]) + off,
  251. count);
  252. mutex_unlock(&pyra->pyra_lock);
  253. return count;
  254. }
  255. static ssize_t pyra_sysfs_read_profile1_buttons(struct file *fp,
  256. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  257. loff_t off, size_t count)
  258. {
  259. return pyra_sysfs_read_profilex_buttons(fp, kobj,
  260. attr, buf, off, count, 0);
  261. }
  262. static ssize_t pyra_sysfs_read_profile2_buttons(struct file *fp,
  263. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  264. loff_t off, size_t count)
  265. {
  266. return pyra_sysfs_read_profilex_buttons(fp, kobj,
  267. attr, buf, off, count, 1);
  268. }
  269. static ssize_t pyra_sysfs_read_profile3_buttons(struct file *fp,
  270. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  271. loff_t off, size_t count)
  272. {
  273. return pyra_sysfs_read_profilex_buttons(fp, kobj,
  274. attr, buf, off, count, 2);
  275. }
  276. static ssize_t pyra_sysfs_read_profile4_buttons(struct file *fp,
  277. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  278. loff_t off, size_t count)
  279. {
  280. return pyra_sysfs_read_profilex_buttons(fp, kobj,
  281. attr, buf, off, count, 3);
  282. }
  283. static ssize_t pyra_sysfs_read_profile5_buttons(struct file *fp,
  284. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  285. loff_t off, size_t count)
  286. {
  287. return pyra_sysfs_read_profilex_buttons(fp, kobj,
  288. attr, buf, off, count, 4);
  289. }
  290. static ssize_t pyra_sysfs_write_profile_settings(struct file *fp,
  291. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  292. loff_t off, size_t count)
  293. {
  294. struct device *dev = container_of(kobj, struct device, kobj);
  295. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  296. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  297. int retval = 0;
  298. int difference;
  299. int profile_number;
  300. struct pyra_profile_settings *profile_settings;
  301. if (off != 0 || count != sizeof(struct pyra_profile_settings))
  302. return -EINVAL;
  303. profile_number = ((struct pyra_profile_settings const *)buf)->number;
  304. profile_settings = &pyra->profile_settings[profile_number];
  305. mutex_lock(&pyra->pyra_lock);
  306. difference = memcmp(buf, profile_settings,
  307. sizeof(struct pyra_profile_settings));
  308. if (difference) {
  309. retval = pyra_set_profile_settings(usb_dev,
  310. (struct pyra_profile_settings const *)buf);
  311. if (!retval)
  312. memcpy(profile_settings, buf,
  313. sizeof(struct pyra_profile_settings));
  314. }
  315. mutex_unlock(&pyra->pyra_lock);
  316. if (retval)
  317. return retval;
  318. return sizeof(struct pyra_profile_settings);
  319. }
  320. static ssize_t pyra_sysfs_write_profile_buttons(struct file *fp,
  321. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  322. loff_t off, size_t count)
  323. {
  324. struct device *dev = container_of(kobj, struct device, kobj);
  325. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  326. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  327. int retval = 0;
  328. int difference;
  329. int profile_number;
  330. struct pyra_profile_buttons *profile_buttons;
  331. if (off != 0 || count != sizeof(struct pyra_profile_buttons))
  332. return -EINVAL;
  333. profile_number = ((struct pyra_profile_buttons const *)buf)->number;
  334. profile_buttons = &pyra->profile_buttons[profile_number];
  335. mutex_lock(&pyra->pyra_lock);
  336. difference = memcmp(buf, profile_buttons,
  337. sizeof(struct pyra_profile_buttons));
  338. if (difference) {
  339. retval = pyra_set_profile_buttons(usb_dev,
  340. (struct pyra_profile_buttons const *)buf);
  341. if (!retval)
  342. memcpy(profile_buttons, buf,
  343. sizeof(struct pyra_profile_buttons));
  344. }
  345. mutex_unlock(&pyra->pyra_lock);
  346. if (retval)
  347. return retval;
  348. return sizeof(struct pyra_profile_buttons);
  349. }
  350. static ssize_t pyra_sysfs_read_settings(struct file *fp,
  351. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  352. loff_t off, size_t count)
  353. {
  354. struct device *dev = container_of(kobj, struct device, kobj);
  355. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  356. if (off >= sizeof(struct pyra_settings))
  357. return 0;
  358. if (off + count > sizeof(struct pyra_settings))
  359. count = sizeof(struct pyra_settings) - off;
  360. mutex_lock(&pyra->pyra_lock);
  361. memcpy(buf, ((char const *)&pyra->settings) + off, count);
  362. mutex_unlock(&pyra->pyra_lock);
  363. return count;
  364. }
  365. static ssize_t pyra_sysfs_write_settings(struct file *fp,
  366. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  367. loff_t off, size_t count)
  368. {
  369. struct device *dev = container_of(kobj, struct device, kobj);
  370. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  371. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  372. int retval = 0;
  373. int difference;
  374. if (off != 0 || count != sizeof(struct pyra_settings))
  375. return -EINVAL;
  376. mutex_lock(&pyra->pyra_lock);
  377. difference = memcmp(buf, &pyra->settings, sizeof(struct pyra_settings));
  378. if (difference) {
  379. retval = pyra_set_settings(usb_dev,
  380. (struct pyra_settings const *)buf);
  381. if (!retval)
  382. memcpy(&pyra->settings, buf,
  383. sizeof(struct pyra_settings));
  384. }
  385. mutex_unlock(&pyra->pyra_lock);
  386. if (retval)
  387. return retval;
  388. profile_activated(pyra, pyra->settings.startup_profile);
  389. return sizeof(struct pyra_settings);
  390. }
  391. static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
  392. struct device_attribute *attr, char *buf)
  393. {
  394. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  395. return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
  396. }
  397. static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
  398. struct device_attribute *attr, char *buf)
  399. {
  400. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  401. return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_profile);
  402. }
  403. static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
  404. struct device_attribute *attr, char *buf)
  405. {
  406. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  407. return snprintf(buf, PAGE_SIZE, "%d\n", pyra->firmware_version);
  408. }
  409. static ssize_t pyra_sysfs_show_startup_profile(struct device *dev,
  410. struct device_attribute *attr, char *buf)
  411. {
  412. struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
  413. return snprintf(buf, PAGE_SIZE, "%d\n", pyra->settings.startup_profile);
  414. }
  415. static DEVICE_ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL);
  416. static DEVICE_ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL);
  417. static DEVICE_ATTR(firmware_version, 0440,
  418. pyra_sysfs_show_firmware_version, NULL);
  419. static DEVICE_ATTR(startup_profile, 0440,
  420. pyra_sysfs_show_startup_profile, NULL);
  421. static struct attribute *pyra_attributes[] = {
  422. &dev_attr_actual_cpi.attr,
  423. &dev_attr_actual_profile.attr,
  424. &dev_attr_firmware_version.attr,
  425. &dev_attr_startup_profile.attr,
  426. NULL
  427. };
  428. static struct attribute_group pyra_attribute_group = {
  429. .attrs = pyra_attributes
  430. };
  431. static struct bin_attribute pyra_profile_settings_attr = {
  432. .attr = { .name = "profile_settings", .mode = 0220 },
  433. .size = sizeof(struct pyra_profile_settings),
  434. .write = pyra_sysfs_write_profile_settings
  435. };
  436. static struct bin_attribute pyra_profile1_settings_attr = {
  437. .attr = { .name = "profile1_settings", .mode = 0440 },
  438. .size = sizeof(struct pyra_profile_settings),
  439. .read = pyra_sysfs_read_profile1_settings
  440. };
  441. static struct bin_attribute pyra_profile2_settings_attr = {
  442. .attr = { .name = "profile2_settings", .mode = 0440 },
  443. .size = sizeof(struct pyra_profile_settings),
  444. .read = pyra_sysfs_read_profile2_settings
  445. };
  446. static struct bin_attribute pyra_profile3_settings_attr = {
  447. .attr = { .name = "profile3_settings", .mode = 0440 },
  448. .size = sizeof(struct pyra_profile_settings),
  449. .read = pyra_sysfs_read_profile3_settings
  450. };
  451. static struct bin_attribute pyra_profile4_settings_attr = {
  452. .attr = { .name = "profile4_settings", .mode = 0440 },
  453. .size = sizeof(struct pyra_profile_settings),
  454. .read = pyra_sysfs_read_profile4_settings
  455. };
  456. static struct bin_attribute pyra_profile5_settings_attr = {
  457. .attr = { .name = "profile5_settings", .mode = 0440 },
  458. .size = sizeof(struct pyra_profile_settings),
  459. .read = pyra_sysfs_read_profile5_settings
  460. };
  461. static struct bin_attribute pyra_profile_buttons_attr = {
  462. .attr = { .name = "profile_buttons", .mode = 0220 },
  463. .size = sizeof(struct pyra_profile_buttons),
  464. .write = pyra_sysfs_write_profile_buttons
  465. };
  466. static struct bin_attribute pyra_profile1_buttons_attr = {
  467. .attr = { .name = "profile1_buttons", .mode = 0440 },
  468. .size = sizeof(struct pyra_profile_buttons),
  469. .read = pyra_sysfs_read_profile1_buttons
  470. };
  471. static struct bin_attribute pyra_profile2_buttons_attr = {
  472. .attr = { .name = "profile2_buttons", .mode = 0440 },
  473. .size = sizeof(struct pyra_profile_buttons),
  474. .read = pyra_sysfs_read_profile2_buttons
  475. };
  476. static struct bin_attribute pyra_profile3_buttons_attr = {
  477. .attr = { .name = "profile3_buttons", .mode = 0440 },
  478. .size = sizeof(struct pyra_profile_buttons),
  479. .read = pyra_sysfs_read_profile3_buttons
  480. };
  481. static struct bin_attribute pyra_profile4_buttons_attr = {
  482. .attr = { .name = "profile4_buttons", .mode = 0440 },
  483. .size = sizeof(struct pyra_profile_buttons),
  484. .read = pyra_sysfs_read_profile4_buttons
  485. };
  486. static struct bin_attribute pyra_profile5_buttons_attr = {
  487. .attr = { .name = "profile5_buttons", .mode = 0440 },
  488. .size = sizeof(struct pyra_profile_buttons),
  489. .read = pyra_sysfs_read_profile5_buttons
  490. };
  491. static struct bin_attribute pyra_settings_attr = {
  492. .attr = { .name = "settings", .mode = 0660 },
  493. .size = sizeof(struct pyra_settings),
  494. .read = pyra_sysfs_read_settings,
  495. .write = pyra_sysfs_write_settings
  496. };
  497. static int pyra_create_sysfs_attributes(struct usb_interface *intf)
  498. {
  499. int retval;
  500. retval = sysfs_create_group(&intf->dev.kobj, &pyra_attribute_group);
  501. if (retval)
  502. goto exit_1;
  503. retval = sysfs_create_bin_file(&intf->dev.kobj,
  504. &pyra_profile_settings_attr);
  505. if (retval)
  506. goto exit_2;
  507. retval = sysfs_create_bin_file(&intf->dev.kobj,
  508. &pyra_profile1_settings_attr);
  509. if (retval)
  510. goto exit_3;
  511. retval = sysfs_create_bin_file(&intf->dev.kobj,
  512. &pyra_profile2_settings_attr);
  513. if (retval)
  514. goto exit_4;
  515. retval = sysfs_create_bin_file(&intf->dev.kobj,
  516. &pyra_profile3_settings_attr);
  517. if (retval)
  518. goto exit_5;
  519. retval = sysfs_create_bin_file(&intf->dev.kobj,
  520. &pyra_profile4_settings_attr);
  521. if (retval)
  522. goto exit_6;
  523. retval = sysfs_create_bin_file(&intf->dev.kobj,
  524. &pyra_profile5_settings_attr);
  525. if (retval)
  526. goto exit_7;
  527. retval = sysfs_create_bin_file(&intf->dev.kobj,
  528. &pyra_profile_buttons_attr);
  529. if (retval)
  530. goto exit_8;
  531. retval = sysfs_create_bin_file(&intf->dev.kobj,
  532. &pyra_profile1_buttons_attr);
  533. if (retval)
  534. goto exit_9;
  535. retval = sysfs_create_bin_file(&intf->dev.kobj,
  536. &pyra_profile2_buttons_attr);
  537. if (retval)
  538. goto exit_10;
  539. retval = sysfs_create_bin_file(&intf->dev.kobj,
  540. &pyra_profile3_buttons_attr);
  541. if (retval)
  542. goto exit_11;
  543. retval = sysfs_create_bin_file(&intf->dev.kobj,
  544. &pyra_profile4_buttons_attr);
  545. if (retval)
  546. goto exit_12;
  547. retval = sysfs_create_bin_file(&intf->dev.kobj,
  548. &pyra_profile5_buttons_attr);
  549. if (retval)
  550. goto exit_13;
  551. retval = sysfs_create_bin_file(&intf->dev.kobj,
  552. &pyra_settings_attr);
  553. if (retval)
  554. goto exit_14;
  555. return 0;
  556. exit_14:
  557. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile5_buttons_attr);
  558. exit_13:
  559. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile4_buttons_attr);
  560. exit_12:
  561. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile3_buttons_attr);
  562. exit_11:
  563. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile2_buttons_attr);
  564. exit_10:
  565. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile1_buttons_attr);
  566. exit_9:
  567. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile_buttons_attr);
  568. exit_8:
  569. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile5_settings_attr);
  570. exit_7:
  571. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile4_settings_attr);
  572. exit_6:
  573. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile3_settings_attr);
  574. exit_5:
  575. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile2_settings_attr);
  576. exit_4:
  577. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile1_settings_attr);
  578. exit_3:
  579. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile_settings_attr);
  580. exit_2:
  581. sysfs_remove_group(&intf->dev.kobj, &pyra_attribute_group);
  582. exit_1:
  583. return retval;
  584. }
  585. static void pyra_remove_sysfs_attributes(struct usb_interface *intf)
  586. {
  587. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_settings_attr);
  588. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile5_buttons_attr);
  589. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile4_buttons_attr);
  590. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile3_buttons_attr);
  591. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile2_buttons_attr);
  592. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile1_buttons_attr);
  593. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile_buttons_attr);
  594. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile5_settings_attr);
  595. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile4_settings_attr);
  596. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile3_settings_attr);
  597. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile2_settings_attr);
  598. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile1_settings_attr);
  599. sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile_settings_attr);
  600. sysfs_remove_group(&intf->dev.kobj, &pyra_attribute_group);
  601. }
  602. static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
  603. struct pyra_device *pyra)
  604. {
  605. struct pyra_info *info;
  606. int retval, i;
  607. mutex_init(&pyra->pyra_lock);
  608. info = kmalloc(sizeof(struct pyra_info), GFP_KERNEL);
  609. if (!info)
  610. return -ENOMEM;
  611. retval = pyra_get_info(usb_dev, info);
  612. if (retval) {
  613. kfree(info);
  614. return retval;
  615. }
  616. pyra->firmware_version = info->firmware_version;
  617. kfree(info);
  618. retval = pyra_get_settings(usb_dev, &pyra->settings);
  619. if (retval)
  620. return retval;
  621. for (i = 0; i < 5; ++i) {
  622. retval = pyra_get_profile_settings(usb_dev,
  623. &pyra->profile_settings[i], i);
  624. if (retval)
  625. return retval;
  626. retval = pyra_get_profile_buttons(usb_dev,
  627. &pyra->profile_buttons[i], i);
  628. if (retval)
  629. return retval;
  630. }
  631. profile_activated(pyra, pyra->settings.startup_profile);
  632. return 0;
  633. }
  634. static int pyra_init_specials(struct hid_device *hdev)
  635. {
  636. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  637. struct usb_device *usb_dev = interface_to_usbdev(intf);
  638. struct pyra_device *pyra;
  639. int retval;
  640. if (intf->cur_altsetting->desc.bInterfaceProtocol
  641. == USB_INTERFACE_PROTOCOL_MOUSE) {
  642. pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
  643. if (!pyra) {
  644. hid_err(hdev, "can't alloc device descriptor\n");
  645. return -ENOMEM;
  646. }
  647. hid_set_drvdata(hdev, pyra);
  648. retval = pyra_init_pyra_device_struct(usb_dev, pyra);
  649. if (retval) {
  650. hid_err(hdev, "couldn't init struct pyra_device\n");
  651. goto exit_free;
  652. }
  653. retval = roccat_connect(hdev);
  654. if (retval < 0) {
  655. hid_err(hdev, "couldn't init char dev\n");
  656. } else {
  657. pyra->chrdev_minor = retval;
  658. pyra->roccat_claimed = 1;
  659. }
  660. retval = pyra_create_sysfs_attributes(intf);
  661. if (retval) {
  662. hid_err(hdev, "cannot create sysfs files\n");
  663. goto exit_free;
  664. }
  665. } else {
  666. hid_set_drvdata(hdev, NULL);
  667. }
  668. return 0;
  669. exit_free:
  670. kfree(pyra);
  671. return retval;
  672. }
  673. static void pyra_remove_specials(struct hid_device *hdev)
  674. {
  675. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  676. struct pyra_device *pyra;
  677. if (intf->cur_altsetting->desc.bInterfaceProtocol
  678. == USB_INTERFACE_PROTOCOL_MOUSE) {
  679. pyra_remove_sysfs_attributes(intf);
  680. pyra = hid_get_drvdata(hdev);
  681. if (pyra->roccat_claimed)
  682. roccat_disconnect(pyra->chrdev_minor);
  683. kfree(hid_get_drvdata(hdev));
  684. }
  685. }
  686. static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
  687. {
  688. int retval;
  689. retval = hid_parse(hdev);
  690. if (retval) {
  691. hid_err(hdev, "parse failed\n");
  692. goto exit;
  693. }
  694. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  695. if (retval) {
  696. hid_err(hdev, "hw start failed\n");
  697. goto exit;
  698. }
  699. retval = pyra_init_specials(hdev);
  700. if (retval) {
  701. hid_err(hdev, "couldn't install mouse\n");
  702. goto exit_stop;
  703. }
  704. return 0;
  705. exit_stop:
  706. hid_hw_stop(hdev);
  707. exit:
  708. return retval;
  709. }
  710. static void pyra_remove(struct hid_device *hdev)
  711. {
  712. pyra_remove_specials(hdev);
  713. hid_hw_stop(hdev);
  714. }
  715. static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
  716. u8 const *data)
  717. {
  718. struct pyra_mouse_event_button const *button_event;
  719. switch (data[0]) {
  720. case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
  721. button_event = (struct pyra_mouse_event_button const *)data;
  722. switch (button_event->type) {
  723. case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
  724. profile_activated(pyra, button_event->data1 - 1);
  725. break;
  726. case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
  727. pyra->actual_cpi = button_event->data1;
  728. break;
  729. }
  730. break;
  731. }
  732. }
  733. static void pyra_report_to_chrdev(struct pyra_device const *pyra,
  734. u8 const *data)
  735. {
  736. struct pyra_roccat_report roccat_report;
  737. struct pyra_mouse_event_button const *button_event;
  738. if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
  739. return;
  740. button_event = (struct pyra_mouse_event_button const *)data;
  741. switch (button_event->type) {
  742. case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
  743. case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
  744. roccat_report.type = button_event->type;
  745. roccat_report.value = button_event->data1;
  746. roccat_report.key = 0;
  747. roccat_report_event(pyra->chrdev_minor,
  748. (uint8_t const *)&roccat_report,
  749. sizeof(struct pyra_roccat_report));
  750. break;
  751. case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
  752. case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
  753. case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
  754. if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
  755. roccat_report.type = button_event->type;
  756. roccat_report.key = button_event->data1;
  757. /*
  758. * pyra reports profile numbers with range 1-5.
  759. * Keeping this behaviour.
  760. */
  761. roccat_report.value = pyra->actual_profile + 1;
  762. roccat_report_event(pyra->chrdev_minor,
  763. (uint8_t const *)&roccat_report,
  764. sizeof(struct pyra_roccat_report));
  765. }
  766. break;
  767. }
  768. }
  769. static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
  770. u8 *data, int size)
  771. {
  772. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  773. struct pyra_device *pyra = hid_get_drvdata(hdev);
  774. if (intf->cur_altsetting->desc.bInterfaceProtocol
  775. != USB_INTERFACE_PROTOCOL_MOUSE)
  776. return 0;
  777. pyra_keep_values_up_to_date(pyra, data);
  778. if (pyra->roccat_claimed)
  779. pyra_report_to_chrdev(pyra, data);
  780. return 0;
  781. }
  782. static const struct hid_device_id pyra_devices[] = {
  783. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
  784. USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
  785. /* TODO add USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS after testing */
  786. { }
  787. };
  788. MODULE_DEVICE_TABLE(hid, pyra_devices);
  789. static struct hid_driver pyra_driver = {
  790. .name = "pyra",
  791. .id_table = pyra_devices,
  792. .probe = pyra_probe,
  793. .remove = pyra_remove,
  794. .raw_event = pyra_raw_event
  795. };
  796. static int __init pyra_init(void)
  797. {
  798. return hid_register_driver(&pyra_driver);
  799. }
  800. static void __exit pyra_exit(void)
  801. {
  802. hid_unregister_driver(&pyra_driver);
  803. }
  804. module_init(pyra_init);
  805. module_exit(pyra_exit);
  806. MODULE_AUTHOR("Stefan Achatz");
  807. MODULE_DESCRIPTION("USB Roccat Pyra driver");
  808. MODULE_LICENSE("GPL v2");