caiaq-input.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) 2006,2007 Daniel Mack, Tim Ruetz
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/input.h>
  22. #include <linux/usb.h>
  23. #include <linux/spinlock.h>
  24. #include <sound/driver.h>
  25. #include <sound/core.h>
  26. #include <sound/rawmidi.h>
  27. #include <sound/pcm.h>
  28. #include "caiaq-device.h"
  29. #include "caiaq-input.h"
  30. #ifdef CONFIG_SND_USB_CAIAQ_INPUT
  31. static unsigned char keycode_ak1[] = { KEY_C, KEY_B, KEY_A };
  32. static unsigned char keycode_rk2[] = { KEY_1, KEY_2, KEY_3, KEY_4,
  33. KEY_5, KEY_6, KEY_7 };
  34. static unsigned char keycode_rk3[] = { KEY_1, KEY_2, KEY_3, KEY_4,
  35. KEY_5, KEY_6, KEY_7, KEY_5, KEY_6 };
  36. #define DEG90 (range/2)
  37. #define DEG180 (range)
  38. #define DEG270 (DEG90 + DEG180)
  39. #define DEG360 (DEG180 * 2)
  40. #define HIGH_PEAK (268)
  41. #define LOW_PEAK (-7)
  42. /* some of these devices have endless rotation potentiometers
  43. * built in which use two tapers, 90 degrees phase shifted.
  44. * this algorithm decodes them to one single value, ranging
  45. * from 0 to 999 */
  46. static unsigned int decode_erp(unsigned char a, unsigned char b)
  47. {
  48. int weight_a, weight_b;
  49. int pos_a, pos_b;
  50. int ret;
  51. int range = HIGH_PEAK - LOW_PEAK;
  52. int mid_value = (HIGH_PEAK + LOW_PEAK) / 2;
  53. weight_b = abs(mid_value-a) - (range/2 - 100)/2;
  54. if (weight_b < 0)
  55. weight_b = 0;
  56. if (weight_b > 100)
  57. weight_b = 100;
  58. weight_a = 100 - weight_b;
  59. if (a < mid_value) {
  60. /* 0..90 and 270..360 degrees */
  61. pos_b = b - LOW_PEAK + DEG270;
  62. if (pos_b >= DEG360)
  63. pos_b -= DEG360;
  64. } else
  65. /* 90..270 degrees */
  66. pos_b = HIGH_PEAK - b + DEG90;
  67. if (b > mid_value)
  68. /* 0..180 degrees */
  69. pos_a = a - LOW_PEAK;
  70. else
  71. /* 180..360 degrees */
  72. pos_a = HIGH_PEAK - a + DEG180;
  73. /* interpolate both slider values, depending on weight factors */
  74. /* 0..99 x DEG360 */
  75. ret = pos_a * weight_a + pos_b * weight_b;
  76. /* normalize to 0..999 */
  77. ret *= 10;
  78. ret /= DEG360;
  79. if (ret < 0)
  80. ret += 1000;
  81. if (ret >= 1000)
  82. ret -= 1000;
  83. return ret;
  84. }
  85. #undef DEG90
  86. #undef DEG180
  87. #undef DEG270
  88. #undef DEG360
  89. #undef HIGH_PEAK
  90. #undef LOW_PEAK
  91. static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
  92. const unsigned char *buf,
  93. unsigned int len)
  94. {
  95. switch(dev->input_dev->id.product) {
  96. case USB_PID_RIGKONTROL2:
  97. input_report_abs(dev->input_dev, ABS_X, (buf[4] << 8) |buf[5]);
  98. input_report_abs(dev->input_dev, ABS_Y, (buf[0] << 8) |buf[1]);
  99. input_report_abs(dev->input_dev, ABS_Z, (buf[2] << 8) |buf[3]);
  100. input_sync(dev->input_dev);
  101. break;
  102. case USB_PID_RIGKONTROL3:
  103. input_report_abs(dev->input_dev, ABS_X, (buf[0] << 8) |buf[1]);
  104. input_report_abs(dev->input_dev, ABS_Y, (buf[2] << 8) |buf[3]);
  105. input_report_abs(dev->input_dev, ABS_Z, (buf[4] << 8) |buf[5]);
  106. input_sync(dev->input_dev);
  107. break;
  108. }
  109. }
  110. static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
  111. const char *buf, unsigned int len)
  112. {
  113. int i;
  114. switch(dev->input_dev->id.product) {
  115. case USB_PID_AK1:
  116. i = decode_erp(buf[0], buf[1]);
  117. input_report_abs(dev->input_dev, ABS_X, i);
  118. input_sync(dev->input_dev);
  119. break;
  120. }
  121. }
  122. static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
  123. char *buf, unsigned int len)
  124. {
  125. int i;
  126. unsigned char *keycode = dev->input_dev->keycode;
  127. if (!keycode)
  128. return;
  129. if (dev->input_dev->id.product == USB_PID_RIGKONTROL2)
  130. for (i=0; i<len; i++)
  131. buf[i] = ~buf[i];
  132. for (i=0; (i<dev->input_dev->keycodemax) && (i < len); i++)
  133. input_report_key(dev->input_dev, keycode[i],
  134. buf[i/8] & (1 << (i%8)));
  135. input_sync(dev->input_dev);
  136. }
  137. void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
  138. char *buf,
  139. unsigned int len)
  140. {
  141. if (!dev->input_dev || (len < 1))
  142. return;
  143. switch (buf[0]) {
  144. case EP1_CMD_READ_ANALOG:
  145. snd_caiaq_input_read_analog(dev, buf+1, len-1);
  146. break;
  147. case EP1_CMD_READ_ERP:
  148. snd_caiaq_input_read_erp(dev, buf+1, len-1);
  149. break;
  150. case EP1_CMD_READ_IO:
  151. snd_caiaq_input_read_io(dev, buf+1, len-1);
  152. break;
  153. }
  154. }
  155. int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
  156. {
  157. struct usb_device *usb_dev = dev->chip.dev;
  158. struct input_dev *input;
  159. int i, ret;
  160. input = input_allocate_device();
  161. if (!input)
  162. return -ENOMEM;
  163. input->name = dev->product_name;
  164. input->id.bustype = BUS_USB;
  165. input->id.vendor = usb_dev->descriptor.idVendor;
  166. input->id.product = usb_dev->descriptor.idProduct;
  167. input->id.version = usb_dev->descriptor.bcdDevice;
  168. switch (dev->chip.usb_id) {
  169. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
  170. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  171. input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
  172. BIT_MASK(ABS_Z);
  173. input->keycode = keycode_rk2;
  174. input->keycodesize = sizeof(char);
  175. input->keycodemax = ARRAY_SIZE(keycode_rk2);
  176. for (i=0; i<ARRAY_SIZE(keycode_rk2); i++)
  177. set_bit(keycode_rk2[i], input->keybit);
  178. input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
  179. input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
  180. input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
  181. snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
  182. break;
  183. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
  184. input->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  185. input->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_Z);
  186. input->keycode = keycode_rk3;
  187. input->keycodesize = sizeof(char);
  188. input->keycodemax = ARRAY_SIZE(keycode_rk3);
  189. for (i=0; i<ARRAY_SIZE(keycode_rk3); i++)
  190. set_bit(keycode_rk3[i], input->keybit);
  191. input_set_abs_params(input, ABS_X, 0, 1024, 0, 10);
  192. input_set_abs_params(input, ABS_Y, 0, 1024, 0, 10);
  193. input_set_abs_params(input, ABS_Z, 0, 1024, 0, 10);
  194. snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
  195. break;
  196. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
  197. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  198. input->absbit[0] = BIT_MASK(ABS_X);
  199. input->keycode = keycode_ak1;
  200. input->keycodesize = sizeof(char);
  201. input->keycodemax = ARRAY_SIZE(keycode_ak1);
  202. for (i=0; i<ARRAY_SIZE(keycode_ak1); i++)
  203. set_bit(keycode_ak1[i], input->keybit);
  204. input_set_abs_params(input, ABS_X, 0, 999, 0, 10);
  205. snd_usb_caiaq_set_auto_msg(dev, 1, 0, 5);
  206. break;
  207. default:
  208. /* no input methods supported on this device */
  209. input_free_device(input);
  210. return 0;
  211. }
  212. ret = input_register_device(input);
  213. if (ret < 0) {
  214. input_free_device(input);
  215. return ret;
  216. }
  217. dev->input_dev = input;
  218. return 0;
  219. }
  220. void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev)
  221. {
  222. if (!dev || !dev->input_dev)
  223. return;
  224. input_unregister_device(dev->input_dev);
  225. dev->input_dev = NULL;
  226. }
  227. #endif /* CONFIG_SND_USB_CAIAQ_INPUT */