2 * Copyright (c) 2006,2007 Daniel Mack, Tim Ruetz
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.
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.
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
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/input.h>
23 #include <linux/usb.h>
24 #include <linux/usb/input.h>
25 #include <linux/spinlock.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"
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 };
38 static unsigned short keycode_kore[] = {
39 KEY_FN_F1, /* "menu" */
40 KEY_FN_F7, /* "lcd backlight */
41 KEY_FN_F2, /* "control" */
42 KEY_FN_F3, /* "enter" */
43 KEY_FN_F4, /* "view" */
44 KEY_FN_F5, /* "esc" */
45 KEY_FN_F6, /* "sound" */
46 KEY_FN_F8, /* array spacer, never triggered. */
51 KEY_SOUND, /* "listen" */
55 BTN_4, /* 8 softkeys */
63 KEY_BRL_DOT4, /* touch sensitive knobs */
73 #define DEG90 (range / 2)
74 #define DEG180 (range)
75 #define DEG270 (DEG90 + DEG180)
76 #define DEG360 (DEG180 * 2)
77 #define HIGH_PEAK (268)
80 /* some of these devices have endless rotation potentiometers
81 * built in which use two tapers, 90 degrees phase shifted.
82 * this algorithm decodes them to one single value, ranging
84 static unsigned int decode_erp(unsigned char a, unsigned char b)
86 int weight_a, weight_b;
89 int range = HIGH_PEAK - LOW_PEAK;
90 int mid_value = (HIGH_PEAK + LOW_PEAK) / 2;
92 weight_b = abs(mid_value - a) - (range / 2 - 100) / 2;
100 weight_a = 100 - weight_b;
103 /* 0..90 and 270..360 degrees */
104 pos_b = b - LOW_PEAK + DEG270;
108 /* 90..270 degrees */
109 pos_b = HIGH_PEAK - b + DEG90;
114 pos_a = a - LOW_PEAK;
116 /* 180..360 degrees */
117 pos_a = HIGH_PEAK - a + DEG180;
119 /* interpolate both slider values, depending on weight factors */
121 ret = pos_a * weight_a + pos_b * weight_b;
123 /* normalize to 0..999 */
144 static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
145 const unsigned char *buf,
148 struct input_dev *input_dev = dev->input_dev;
150 switch (dev->chip.usb_id) {
151 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
152 input_report_abs(input_dev, ABS_X, (buf[4] << 8) | buf[5]);
153 input_report_abs(input_dev, ABS_Y, (buf[0] << 8) | buf[1]);
154 input_report_abs(input_dev, ABS_Z, (buf[2] << 8) | buf[3]);
155 input_sync(input_dev);
157 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
158 input_report_abs(input_dev, ABS_X, (buf[0] << 8) | buf[1]);
159 input_report_abs(input_dev, ABS_Y, (buf[2] << 8) | buf[3]);
160 input_report_abs(input_dev, ABS_Z, (buf[4] << 8) | buf[5]);
161 input_sync(input_dev);
163 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
164 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
165 input_report_abs(input_dev, ABS_X, (buf[0] << 8) | buf[1]);
166 input_report_abs(input_dev, ABS_Y, (buf[2] << 8) | buf[3]);
167 input_report_abs(input_dev, ABS_Z, (buf[4] << 8) | buf[5]);
168 input_sync(input_dev);
173 static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
174 const char *buf, unsigned int len)
176 struct input_dev *input_dev = dev->input_dev;
179 switch (dev->chip.usb_id) {
180 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
181 i = decode_erp(buf[0], buf[1]);
182 input_report_abs(input_dev, ABS_X, i);
183 input_sync(input_dev);
185 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
186 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
187 i = decode_erp(buf[7], buf[5]);
188 input_report_abs(input_dev, ABS_HAT0X, i);
189 i = decode_erp(buf[12], buf[14]);
190 input_report_abs(input_dev, ABS_HAT0Y, i);
191 i = decode_erp(buf[15], buf[13]);
192 input_report_abs(input_dev, ABS_HAT1X, i);
193 i = decode_erp(buf[0], buf[2]);
194 input_report_abs(input_dev, ABS_HAT1Y, i);
195 i = decode_erp(buf[3], buf[1]);
196 input_report_abs(input_dev, ABS_HAT2X, i);
197 i = decode_erp(buf[8], buf[10]);
198 input_report_abs(input_dev, ABS_HAT2Y, i);
199 i = decode_erp(buf[11], buf[9]);
200 input_report_abs(input_dev, ABS_HAT3X, i);
201 i = decode_erp(buf[4], buf[6]);
202 input_report_abs(input_dev, ABS_HAT3Y, i);
203 input_sync(input_dev);
208 static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
209 char *buf, unsigned int len)
211 struct input_dev *input_dev = dev->input_dev;
212 unsigned short *keycode = input_dev->keycode;
218 if (input_dev->id.product == USB_PID_RIGKONTROL2)
219 for (i = 0; i < len; i++)
222 for (i = 0; i < input_dev->keycodemax && i < len * 8; i++)
223 input_report_key(input_dev, keycode[i],
224 buf[i / 8] & (1 << (i % 8)));
226 if (dev->chip.usb_id ==
227 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER) ||
229 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2))
230 input_report_abs(dev->input_dev, ABS_MISC, 255 - buf[4]);
232 input_sync(input_dev);
235 void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
239 if (!dev->input_dev || len < 1)
243 case EP1_CMD_READ_ANALOG:
244 snd_caiaq_input_read_analog(dev, buf + 1, len - 1);
246 case EP1_CMD_READ_ERP:
247 snd_caiaq_input_read_erp(dev, buf + 1, len - 1);
249 case EP1_CMD_READ_IO:
250 snd_caiaq_input_read_io(dev, buf + 1, len - 1);
255 int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
257 struct usb_device *usb_dev = dev->chip.dev;
258 struct input_dev *input;
261 input = input_allocate_device();
265 usb_make_path(usb_dev, dev->phys, sizeof(dev->phys));
266 strlcat(dev->phys, "/input0", sizeof(dev->phys));
268 input->name = dev->product_name;
269 input->phys = dev->phys;
270 usb_to_input_id(usb_dev, &input->id);
271 input->dev.parent = &usb_dev->dev;
273 switch (dev->chip.usb_id) {
274 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
275 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
276 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
278 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk2));
279 memcpy(dev->keycode, keycode_rk2, sizeof(keycode_rk2));
280 input->keycodemax = ARRAY_SIZE(keycode_rk2);
281 input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
282 input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
283 input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
284 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
286 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
287 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
288 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
290 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk3));
291 memcpy(dev->keycode, keycode_rk3, sizeof(keycode_rk3));
292 input->keycodemax = ARRAY_SIZE(keycode_rk3);
293 input_set_abs_params(input, ABS_X, 0, 1024, 0, 10);
294 input_set_abs_params(input, ABS_Y, 0, 1024, 0, 10);
295 input_set_abs_params(input, ABS_Z, 0, 1024, 0, 10);
296 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
298 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
299 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
300 input->absbit[0] = BIT_MASK(ABS_X);
301 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_ak1));
302 memcpy(dev->keycode, keycode_ak1, sizeof(keycode_ak1));
303 input->keycodemax = ARRAY_SIZE(keycode_ak1);
304 input_set_abs_params(input, ABS_X, 0, 999, 0, 10);
305 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 5);
307 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
308 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
309 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
310 input->absbit[0] = BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
311 BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y) |
312 BIT_MASK(ABS_HAT2X) | BIT_MASK(ABS_HAT2Y) |
313 BIT_MASK(ABS_HAT3X) | BIT_MASK(ABS_HAT3Y) |
314 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
316 input->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
317 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_kore));
318 memcpy(dev->keycode, keycode_kore, sizeof(keycode_kore));
319 input->keycodemax = ARRAY_SIZE(keycode_kore);
320 input_set_abs_params(input, ABS_HAT0X, 0, 999, 0, 10);
321 input_set_abs_params(input, ABS_HAT0Y, 0, 999, 0, 10);
322 input_set_abs_params(input, ABS_HAT1X, 0, 999, 0, 10);
323 input_set_abs_params(input, ABS_HAT1Y, 0, 999, 0, 10);
324 input_set_abs_params(input, ABS_HAT2X, 0, 999, 0, 10);
325 input_set_abs_params(input, ABS_HAT2Y, 0, 999, 0, 10);
326 input_set_abs_params(input, ABS_HAT3X, 0, 999, 0, 10);
327 input_set_abs_params(input, ABS_HAT3Y, 0, 999, 0, 10);
328 input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
329 input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
330 input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
331 input_set_abs_params(input, ABS_MISC, 0, 255, 0, 1);
332 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 5);
335 /* no input methods supported on this device */
336 input_free_device(input);
340 input->keycode = dev->keycode;
341 input->keycodesize = sizeof(unsigned short);
342 for (i = 0; i < input->keycodemax; i++)
343 __set_bit(dev->keycode[i], input->keybit);
345 ret = input_register_device(input);
347 input_free_device(input);
351 dev->input_dev = input;
355 void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev)
357 if (!dev || !dev->input_dev)
360 input_unregister_device(dev->input_dev);
361 dev->input_dev = NULL;