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 CHECK_OWNERSHIP(i, hid_pid) \
  38. ((i) < FF_EFFECTS_MAX && i >= 0 && \
  39. test_bit(FF_PID_FLAGS_USED, &hid_pid->effects[(i)].flags) && \
  40. (current->pid == 0 || \
  41. (hid_pid)->effects[(i)].owner == current->pid))
  42. /* Called when a transfer is completed */
  43. static void hid_pid_ctrl_out(struct urb *u, struct pt_regs *regs)
  44. {
  45. dev_dbg(&u->dev->dev, "hid_pid_ctrl_out - Transfer Completed\n");
  46. }
  47. static void hid_pid_exit(struct hid_device *hid)
  48. {
  49. struct hid_ff_pid *private = hid->ff_private;
  50. if (private->urbffout) {
  51. usb_kill_urb(private->urbffout);
  52. usb_free_urb(private->urbffout);
  53. }
  54. }
  55. static int pid_upload_periodic(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
  56. {
  57. dev_info(&pid->hid->dev->dev, "requested periodic force upload\n");
  58. return 0;
  59. }
  60. static int pid_upload_constant(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
  61. {
  62. dev_info(&pid->hid->dev->dev, "requested constant force upload\n");
  63. return 0;
  64. }
  65. static int pid_upload_condition(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
  66. {
  67. dev_info(&pid->hid->dev->dev, "requested Condition force upload\n");
  68. return 0;
  69. }
  70. static int pid_upload_ramp(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
  71. {
  72. dev_info(&pid->hid->dev->dev, "request ramp force upload\n");
  73. return 0;
  74. }
  75. static int hid_pid_event(struct hid_device *hid, struct input_dev *input,
  76. unsigned int type, unsigned int code, int value)
  77. {
  78. dev_dbg(&hid->dev->dev, "PID event received: type=%d,code=%d,value=%d.\n", type, code, value);
  79. if (type != EV_FF)
  80. return -1;
  81. return 0;
  82. }
  83. /* Lock must be held by caller */
  84. static void hid_pid_ctrl_playback(struct hid_device *hid, struct hid_pid_effect *effect, int play)
  85. {
  86. if (play)
  87. set_bit(FF_PID_FLAGS_PLAYING, &effect->flags);
  88. else
  89. clear_bit(FF_PID_FLAGS_PLAYING, &effect->flags);
  90. }
  91. static int hid_pid_erase(struct input_dev *dev, int id)
  92. {
  93. struct hid_device *hid = dev->private;
  94. struct hid_ff_pid *pid = hid->ff_private;
  95. struct hid_field *field;
  96. unsigned long flags;
  97. int ret;
  98. if (!CHECK_OWNERSHIP(id, pid))
  99. return -EACCES;
  100. /* Find report */
  101. field = hid_find_field_by_usage(hid, HID_UP_PID | FF_PID_USAGE_BLOCK_FREE,
  102. HID_OUTPUT_REPORT);
  103. if (!field) {
  104. dev_err(&hid->dev->dev, "couldn't find report\n");
  105. return -EIO;
  106. }
  107. ret = hid_set_field(field, 0, pid->effects[id].device_id);
  108. if (ret) {
  109. dev_err(&hid->dev->dev, "couldn't set field\n");
  110. return ret;
  111. }
  112. hid_submit_report(hid, field->report, USB_DIR_OUT);
  113. spin_lock_irqsave(&pid->lock, flags);
  114. hid_pid_ctrl_playback(hid, pid->effects + id, 0);
  115. pid->effects[id].flags = 0;
  116. spin_unlock_irqrestore(&pid->lock, flags);
  117. return 0;
  118. }
  119. /* Erase all effects this process owns */
  120. static int hid_pid_flush(struct input_dev *dev, struct file *file)
  121. {
  122. struct hid_device *hid = dev->private;
  123. struct hid_ff_pid *pid = hid->ff_private;
  124. int i;
  125. /*NOTE: no need to lock here. The only times EFFECT_USED is
  126. modified is when effects are uploaded or when an effect is
  127. erased. But a process cannot close its dev/input/eventX fd
  128. and perform ioctls on the same fd all at the same time */
  129. /*FIXME: multiple threads, anyone? */
  130. for (i = 0; i < dev->ff_effects_max; ++i)
  131. if (current->pid == pid->effects[i].owner
  132. && test_bit(FF_PID_FLAGS_USED, &pid->effects[i].flags))
  133. if (hid_pid_erase(dev, i))
  134. dev_warn(&hid->dev->dev, "erase effect %d failed", i);
  135. return 0;
  136. }
  137. static int hid_pid_upload_effect(struct input_dev *dev,
  138. struct ff_effect *effect)
  139. {
  140. struct hid_ff_pid *pid_private = (struct hid_ff_pid *)(dev->private);
  141. int ret;
  142. int is_update;
  143. unsigned long flags;
  144. dev_dbg(&pid_private->hid->dev->dev, "upload effect called: effect_type=%x\n", effect->type);
  145. /* Check this effect type is supported by this device */
  146. if (!test_bit(effect->type, dev->ffbit)) {
  147. dev_dbg(&pid_private->hid->dev->dev,
  148. "invalid kind of effect requested.\n");
  149. return -EINVAL;
  150. }
  151. /*
  152. * If we want to create a new effect, get a free id
  153. */
  154. if (effect->id == -1) {
  155. int id = 0;
  156. // Spinlock so we don`t get a race condition when choosing IDs
  157. spin_lock_irqsave(&pid_private->lock, flags);
  158. while (id < FF_EFFECTS_MAX)
  159. if (!test_and_set_bit(FF_PID_FLAGS_USED, &pid_private->effects[id++].flags))
  160. break;
  161. if (id == FF_EFFECTS_MAX) {
  162. spin_unlock_irqrestore(&pid_private->lock, flags);
  163. // TEMP - We need to get ff_effects_max correctly first: || id >= dev->ff_effects_max) {
  164. dev_dbg(&pid_private->hid->dev->dev, "Not enough device memory\n");
  165. return -ENOMEM;
  166. }
  167. effect->id = id;
  168. dev_dbg(&pid_private->hid->dev->dev, "effect ID is %d.\n", id);
  169. pid_private->effects[id].owner = current->pid;
  170. pid_private->effects[id].flags = (1 << FF_PID_FLAGS_USED);
  171. spin_unlock_irqrestore(&pid_private->lock, flags);
  172. is_update = FF_PID_FALSE;
  173. } else {
  174. /* We want to update an effect */
  175. if (!CHECK_OWNERSHIP(effect->id, pid_private))
  176. return -EACCES;
  177. /* Parameter type cannot be updated */
  178. if (effect->type != pid_private->effects[effect->id].effect.type)
  179. return -EINVAL;
  180. /* Check the effect is not already being updated */
  181. if (test_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags))
  182. return -EAGAIN;
  183. is_update = FF_PID_TRUE;
  184. }
  185. /*
  186. * Upload the effect
  187. */
  188. switch (effect->type) {
  189. case FF_PERIODIC:
  190. ret = pid_upload_periodic(pid_private, effect, is_update);
  191. break;
  192. case FF_CONSTANT:
  193. ret = pid_upload_constant(pid_private, effect, is_update);
  194. break;
  195. case FF_SPRING:
  196. case FF_FRICTION:
  197. case FF_DAMPER:
  198. case FF_INERTIA:
  199. ret = pid_upload_condition(pid_private, effect, is_update);
  200. break;
  201. case FF_RAMP:
  202. ret = pid_upload_ramp(pid_private, effect, is_update);
  203. break;
  204. default:
  205. dev_dbg(&pid_private->hid->dev->dev,
  206. "invalid type of effect requested - %x.\n",
  207. effect->type);
  208. return -EINVAL;
  209. }
  210. /* If a packet was sent, forbid new updates until we are notified
  211. * that the packet was updated
  212. */
  213. if (ret == 0)
  214. set_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags);
  215. pid_private->effects[effect->id].effect = *effect;
  216. return ret;
  217. }
  218. int hid_pid_init(struct hid_device *hid)
  219. {
  220. struct hid_ff_pid *private;
  221. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  222. struct input_dev *input_dev = hidinput->input;
  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. input_dev->upload_effect = hid_pid_upload_effect;
  238. input_dev->flush = hid_pid_flush;
  239. input_dev->ff_effects_max = 8; // A random default
  240. set_bit(EV_FF, input_dev->evbit);
  241. set_bit(EV_FF_STATUS, input_dev->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. }