caiaq-input.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/usb/input.h>
  24. #include <linux/spinlock.h>
  25. #include <sound/driver.h>
  26. #include <sound/core.h>
  27. #include <sound/rawmidi.h>
  28. #include <sound/pcm.h>
  29. #include "caiaq-device.h"
  30. #include "caiaq-input.h"
  31. #ifdef CONFIG_SND_USB_CAIAQ_INPUT
  32. static unsigned short keycode_ak1[] = { KEY_C, KEY_B, KEY_A };
  33. static unsigned short keycode_rk2[] = { KEY_1, KEY_2, KEY_3, KEY_4,
  34. KEY_5, KEY_6, KEY_7 };
  35. static unsigned short keycode_rk3[] = { KEY_1, KEY_2, KEY_3, KEY_4,
  36. KEY_5, KEY_6, KEY_7, KEY_5, KEY_6 };
  37. #define DEG90 (range / 2)
  38. #define DEG180 (range)
  39. #define DEG270 (DEG90 + DEG180)
  40. #define DEG360 (DEG180 * 2)
  41. #define HIGH_PEAK (268)
  42. #define LOW_PEAK (-7)
  43. /* some of these devices have endless rotation potentiometers
  44. * built in which use two tapers, 90 degrees phase shifted.
  45. * this algorithm decodes them to one single value, ranging
  46. * from 0 to 999 */
  47. static unsigned int decode_erp(unsigned char a, unsigned char b)
  48. {
  49. int weight_a, weight_b;
  50. int pos_a, pos_b;
  51. int ret;
  52. int range = HIGH_PEAK - LOW_PEAK;
  53. int mid_value = (HIGH_PEAK + LOW_PEAK) / 2;
  54. weight_b = abs(mid_value - a) - (range / 2 - 100) / 2;
  55. if (weight_b < 0)
  56. weight_b = 0;
  57. if (weight_b > 100)
  58. weight_b = 100;
  59. weight_a = 100 - weight_b;
  60. if (a < mid_value) {
  61. /* 0..90 and 270..360 degrees */
  62. pos_b = b - LOW_PEAK + DEG270;
  63. if (pos_b >= DEG360)
  64. pos_b -= DEG360;
  65. } else
  66. /* 90..270 degrees */
  67. pos_b = HIGH_PEAK - b + DEG90;
  68. if (b > mid_value)
  69. /* 0..180 degrees */
  70. pos_a = a - LOW_PEAK;
  71. else
  72. /* 180..360 degrees */
  73. pos_a = HIGH_PEAK - a + DEG180;
  74. /* interpolate both slider values, depending on weight factors */
  75. /* 0..99 x DEG360 */
  76. ret = pos_a * weight_a + pos_b * weight_b;
  77. /* normalize to 0..999 */
  78. ret *= 10;
  79. ret /= DEG360;
  80. if (ret < 0)
  81. ret += 1000;
  82. if (ret >= 1000)
  83. ret -= 1000;
  84. return ret;
  85. }
  86. #undef DEG90
  87. #undef DEG180
  88. #undef DEG270
  89. #undef DEG360
  90. #undef HIGH_PEAK
  91. #undef LOW_PEAK
  92. static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
  93. const unsigned char *buf,
  94. unsigned int len)
  95. {
  96. struct input_dev *input_dev = dev->input_dev;
  97. switch (input_dev->id.product) {
  98. case USB_PID_RIGKONTROL2:
  99. input_report_abs(input_dev, ABS_X, (buf[4] << 8) | buf[5]);
  100. input_report_abs(input_dev, ABS_Y, (buf[0] << 8) | buf[1]);
  101. input_report_abs(input_dev, ABS_Z, (buf[2] << 8) | buf[3]);
  102. input_sync(input_dev);
  103. break;
  104. case USB_PID_RIGKONTROL3:
  105. input_report_abs(input_dev, ABS_X, (buf[0] << 8) | buf[1]);
  106. input_report_abs(input_dev, ABS_Y, (buf[2] << 8) | buf[3]);
  107. input_report_abs(input_dev, ABS_Z, (buf[4] << 8) | buf[5]);
  108. input_sync(input_dev);
  109. break;
  110. }
  111. }
  112. static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
  113. const char *buf, unsigned int len)
  114. {
  115. struct input_dev *input_dev = dev->input_dev;
  116. int i;
  117. switch (input_dev->id.product) {
  118. case USB_PID_AK1:
  119. i = decode_erp(buf[0], buf[1]);
  120. input_report_abs(input_dev, ABS_X, i);
  121. input_sync(input_dev);
  122. break;
  123. }
  124. }
  125. static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
  126. char *buf, unsigned int len)
  127. {
  128. struct input_dev *input_dev = dev->input_dev;
  129. unsigned short *keycode = input_dev->keycode;
  130. int i;
  131. if (!keycode)
  132. return;
  133. if (input_dev->id.product == USB_PID_RIGKONTROL2)
  134. for (i = 0; i < len; i++)
  135. buf[i] = ~buf[i];
  136. for (i = 0; i < input_dev->keycodemax && i < len; i++)
  137. input_report_key(input_dev, keycode[i],
  138. buf[i / 8] & (1 << (i % 8)));
  139. input_sync(input_dev);
  140. }
  141. void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
  142. char *buf,
  143. unsigned int len)
  144. {
  145. if (!dev->input_dev || len < 1)
  146. return;
  147. switch (buf[0]) {
  148. case EP1_CMD_READ_ANALOG:
  149. snd_caiaq_input_read_analog(dev, buf + 1, len - 1);
  150. break;
  151. case EP1_CMD_READ_ERP:
  152. snd_caiaq_input_read_erp(dev, buf + 1, len - 1);
  153. break;
  154. case EP1_CMD_READ_IO:
  155. snd_caiaq_input_read_io(dev, buf + 1, len - 1);
  156. break;
  157. }
  158. }
  159. int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
  160. {
  161. struct usb_device *usb_dev = dev->chip.dev;
  162. struct input_dev *input;
  163. int i, ret;
  164. input = input_allocate_device();
  165. if (!input)
  166. return -ENOMEM;
  167. usb_make_path(usb_dev, dev->phys, sizeof(dev->phys));
  168. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  169. input->name = dev->product_name;
  170. input->phys = dev->phys;
  171. usb_to_input_id(usb_dev, &input->id);
  172. input->dev.parent = &usb_dev->dev;
  173. switch (dev->chip.usb_id) {
  174. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
  175. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  176. input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
  177. BIT_MASK(ABS_Z);
  178. BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk2));
  179. memcpy(dev->keycode, keycode_rk2, sizeof(keycode_rk2));
  180. input->keycodemax = ARRAY_SIZE(keycode_rk2);
  181. input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
  182. input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
  183. input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
  184. snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
  185. break;
  186. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
  187. input->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  188. input->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_Z);
  189. BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk3));
  190. memcpy(dev->keycode, keycode_rk3, sizeof(keycode_rk3));
  191. input->keycodemax = ARRAY_SIZE(keycode_rk3);
  192. input_set_abs_params(input, ABS_X, 0, 1024, 0, 10);
  193. input_set_abs_params(input, ABS_Y, 0, 1024, 0, 10);
  194. input_set_abs_params(input, ABS_Z, 0, 1024, 0, 10);
  195. snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
  196. break;
  197. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
  198. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  199. input->absbit[0] = BIT_MASK(ABS_X);
  200. BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_ak1));
  201. memcpy(dev->keycode, keycode_ak1, sizeof(keycode_ak1));
  202. input->keycodemax = ARRAY_SIZE(keycode_ak1);
  203. input_set_abs_params(input, ABS_X, 0, 999, 0, 10);
  204. snd_usb_caiaq_set_auto_msg(dev, 1, 0, 5);
  205. break;
  206. default:
  207. /* no input methods supported on this device */
  208. input_free_device(input);
  209. return 0;
  210. }
  211. input->keycode = dev->keycode;
  212. input->keycodesize = sizeof(unsigned short);
  213. for (i = 0; i < input->keycodemax; i++)
  214. __set_bit(dev->keycode[i], input->keybit);
  215. ret = input_register_device(input);
  216. if (ret < 0) {
  217. input_free_device(input);
  218. return ret;
  219. }
  220. dev->input_dev = input;
  221. return 0;
  222. }
  223. void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev)
  224. {
  225. if (!dev || !dev->input_dev)
  226. return;
  227. input_unregister_device(dev->input_dev);
  228. dev->input_dev = NULL;
  229. }
  230. #endif /* CONFIG_SND_USB_CAIAQ_INPUT */