pid.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * PID Force feedback support for hid devices.
  3. *
  4. * Copyright (c) 2002 Rodrigo Damazio.
  5. * Portions by Johann Deneux and Bjorn Augustson
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * Should you need to contact me, the author, you can do so by
  23. * e-mail - mail your message to <rdamazio@lsi.usp.br>
  24. */
  25. #include <linux/config.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/kernel.h>
  29. #include <linux/init.h>
  30. #include <linux/mm.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/input.h>
  34. #include <linux/usb.h>
  35. #include "hid.h"
  36. #include "pid.h"
  37. #define DEBUG
  38. #define CHECK_OWNERSHIP(i, hid_pid) \
  39. ((i) < FF_EFFECTS_MAX && i >= 0 && \
  40. test_bit(FF_PID_FLAGS_USED, &hid_pid->effects[(i)].flags) && \
  41. (current->pid == 0 || \
  42. (hid_pid)->effects[(i)].owner == current->pid))
  43. /* Called when a transfer is completed */
  44. static void hid_pid_ctrl_out(struct urb *u, struct pt_regs *regs)
  45. {
  46. dev_dbg(&u->dev->dev, "hid_pid_ctrl_out - Transfer Completed\n");
  47. }
  48. static void hid_pid_exit(struct hid_device *hid)
  49. {
  50. struct hid_ff_pid *private = hid->ff_private;
  51. if (private->urbffout) {
  52. usb_kill_urb(private->urbffout);
  53. usb_free_urb(private->urbffout);
  54. }
  55. }
  56. static int pid_upload_periodic(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
  57. {
  58. dev_info(&pid->hid->dev->dev, "requested periodic force upload\n");
  59. return 0;
  60. }
  61. static int pid_upload_constant(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
  62. {
  63. dev_info(&pid->hid->dev->dev, "requested constant force upload\n");
  64. return 0;
  65. }
  66. static int pid_upload_condition(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
  67. {
  68. dev_info(&pid->hid->dev->dev, "requested Condition force upload\n");
  69. return 0;
  70. }
  71. static int pid_upload_ramp(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
  72. {
  73. dev_info(&pid->hid->dev->dev, "request ramp force upload\n");
  74. return 0;
  75. }
  76. static int hid_pid_event(struct hid_device *hid, struct input_dev *input,
  77. unsigned int type, unsigned int code, int value)
  78. {
  79. dev_dbg(&hid->dev->dev, "PID event received: type=%d,code=%d,value=%d.\n", type, code, value);
  80. if (type != EV_FF)
  81. return -1;
  82. return 0;
  83. }
  84. /* Lock must be held by caller */
  85. static void hid_pid_ctrl_playback(struct hid_device *hid, struct hid_pid_effect *effect, int play)
  86. {
  87. if (play)
  88. set_bit(FF_PID_FLAGS_PLAYING, &effect->flags);
  89. else
  90. clear_bit(FF_PID_FLAGS_PLAYING, &effect->flags);
  91. }
  92. static int hid_pid_erase(struct input_dev *dev, int id)
  93. {
  94. struct hid_device *hid = dev->private;
  95. struct hid_ff_pid *pid = hid->ff_private;
  96. struct hid_field *field;
  97. unsigned long flags;
  98. int ret;
  99. if (!CHECK_OWNERSHIP(id, pid))
  100. return -EACCES;
  101. /* Find report */
  102. field = hid_find_field_by_usage(hid, HID_UP_PID | FF_PID_USAGE_BLOCK_FREE,
  103. HID_OUTPUT_REPORT);
  104. if (!field) {
  105. dev_err(&hid->dev->dev, "couldn't find report\n");
  106. return -EIO;
  107. }
  108. ret = hid_set_field(field, 0, pid->effects[id].device_id);
  109. if (ret) {
  110. dev_err(&hid->dev->dev, "couldn't set field\n");
  111. return ret;
  112. }
  113. hid_submit_report(hid, field->report, USB_DIR_OUT);
  114. spin_lock_irqsave(&pid->lock, flags);
  115. hid_pid_ctrl_playback(hid, pid->effects + id, 0);
  116. pid->effects[id].flags = 0;
  117. spin_unlock_irqrestore(&pid->lock, flags);
  118. return 0;
  119. }
  120. /* Erase all effects this process owns */
  121. static int hid_pid_flush(struct input_dev *dev, struct file *file)
  122. {
  123. struct hid_device *hid = dev->private;
  124. struct hid_ff_pid *pid = hid->ff_private;
  125. int i;
  126. /*NOTE: no need to lock here. The only times EFFECT_USED is
  127. modified is when effects are uploaded or when an effect is
  128. erased. But a process cannot close its dev/input/eventX fd
  129. and perform ioctls on the same fd all at the same time */
  130. /*FIXME: multiple threads, anyone? */
  131. for (i = 0; i < dev->ff_effects_max; ++i)
  132. if (current->pid == pid->effects[i].owner
  133. && test_bit(FF_PID_FLAGS_USED, &pid->effects[i].flags))
  134. if (hid_pid_erase(dev, i))
  135. dev_warn(&hid->dev->dev, "erase effect %d failed", i);
  136. return 0;
  137. }
  138. static int hid_pid_upload_effect(struct input_dev *dev,
  139. struct ff_effect *effect)
  140. {
  141. struct hid_ff_pid *pid_private = (struct hid_ff_pid *)(dev->private);
  142. int ret;
  143. int is_update;
  144. unsigned long flags;
  145. dev_dbg(&pid_private->hid->dev->dev, "upload effect called: effect_type=%x\n", effect->type);
  146. /* Check this effect type is supported by this device */
  147. if (!test_bit(effect->type, dev->ffbit)) {
  148. dev_dbg(&pid_private->hid->dev->dev,
  149. "invalid kind of effect requested.\n");
  150. return -EINVAL;
  151. }
  152. /*
  153. * If we want to create a new effect, get a free id
  154. */
  155. if (effect->id == -1) {
  156. int id = 0;
  157. // Spinlock so we don`t get a race condition when choosing IDs
  158. spin_lock_irqsave(&pid_private->lock, flags);
  159. while (id < FF_EFFECTS_MAX)
  160. if (!test_and_set_bit(FF_PID_FLAGS_USED, &pid_private->effects[id++].flags))
  161. break;
  162. if (id == FF_EFFECTS_MAX) {
  163. spin_unlock_irqrestore(&pid_private->lock, flags);
  164. // TEMP - We need to get ff_effects_max correctly first: || id >= dev->ff_effects_max) {
  165. dev_dbg(&pid_private->hid->dev->dev, "Not enough device memory\n");
  166. return -ENOMEM;
  167. }
  168. effect->id = id;
  169. dev_dbg(&pid_private->hid->dev->dev, "effect ID is %d\n.", id);
  170. pid_private->effects[id].owner = current->pid;
  171. pid_private->effects[id].flags = (1 << FF_PID_FLAGS_USED);
  172. spin_unlock_irqrestore(&pid_private->lock, flags);
  173. is_update = FF_PID_FALSE;
  174. } else {
  175. /* We want to update an effect */
  176. if (!CHECK_OWNERSHIP(effect->id, pid_private))
  177. return -EACCES;
  178. /* Parameter type cannot be updated */
  179. if (effect->type != pid_private->effects[effect->id].effect.type)
  180. return -EINVAL;
  181. /* Check the effect is not already being updated */
  182. if (test_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags))
  183. return -EAGAIN;
  184. is_update = FF_PID_TRUE;
  185. }
  186. /*
  187. * Upload the effect
  188. */
  189. switch (effect->type) {
  190. case FF_PERIODIC:
  191. ret = pid_upload_periodic(pid_private, effect, is_update);
  192. break;
  193. case FF_CONSTANT:
  194. ret = pid_upload_constant(pid_private, effect, is_update);
  195. break;
  196. case FF_SPRING:
  197. case FF_FRICTION:
  198. case FF_DAMPER:
  199. case FF_INERTIA:
  200. ret = pid_upload_condition(pid_private, effect, is_update);
  201. break;
  202. case FF_RAMP:
  203. ret = pid_upload_ramp(pid_private, effect, is_update);
  204. break;
  205. default:
  206. dev_dbg(&pid_private->hid->dev->dev,
  207. "invalid type of effect requested - %x.\n",
  208. effect->type);
  209. return -EINVAL;
  210. }
  211. /* If a packet was sent, forbid new updates until we are notified
  212. * that the packet was updated
  213. */
  214. if (ret == 0)
  215. set_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags);
  216. pid_private->effects[effect->id].effect = *effect;
  217. return ret;
  218. }
  219. int hid_pid_init(struct hid_device *hid)
  220. {
  221. struct hid_ff_pid *private;
  222. struct hid_input *hidinput = list_entry(&hid->inputs, struct hid_input, list);
  223. private = hid->ff_private = kzalloc(sizeof(struct hid_ff_pid), GFP_KERNEL);
  224. if (!private)
  225. return -ENOMEM;
  226. private->hid = hid;
  227. hid->ff_exit = hid_pid_exit;
  228. hid->ff_event = hid_pid_event;
  229. /* Open output URB */
  230. if (!(private->urbffout = usb_alloc_urb(0, GFP_KERNEL))) {
  231. kfree(private);
  232. return -1;
  233. }
  234. usb_fill_control_urb(private->urbffout, hid->dev, 0,
  235. (void *)&private->ffcr, private->ctrl_buffer, 8,
  236. hid_pid_ctrl_out, hid);
  237. hidinput->input.upload_effect = hid_pid_upload_effect;
  238. hidinput->input.flush = hid_pid_flush;
  239. hidinput->input.ff_effects_max = 8; // A random default
  240. set_bit(EV_FF, hidinput->input.evbit);
  241. set_bit(EV_FF_STATUS, hidinput->input.evbit);
  242. spin_lock_init(&private->lock);
  243. printk(KERN_INFO "Force feedback driver for PID devices by Rodrigo Damazio <rdamazio@lsi.usp.br>.\n");
  244. return 0;
  245. }