hid-roccat-pyra.c 27 KB

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