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/usb.h>
21 #include <linux/usb/input.h>
22 #include <sound/pcm.h>
27 static unsigned short keycode_ak1[] = { KEY_C, KEY_B, KEY_A };
28 static unsigned short keycode_rk2[] = { KEY_1, KEY_2, KEY_3, KEY_4,
29 KEY_5, KEY_6, KEY_7 };
30 static unsigned short keycode_rk3[] = { KEY_1, KEY_2, KEY_3, KEY_4,
31 KEY_5, KEY_6, KEY_7, KEY_5, KEY_6 };
33 static unsigned short keycode_kore[] = {
34 KEY_FN_F1, /* "menu" */
35 KEY_FN_F7, /* "lcd backlight */
36 KEY_FN_F2, /* "control" */
37 KEY_FN_F3, /* "enter" */
38 KEY_FN_F4, /* "view" */
39 KEY_FN_F5, /* "esc" */
40 KEY_FN_F6, /* "sound" */
41 KEY_FN_F8, /* array spacer, never triggered. */
46 KEY_SOUND, /* "listen" */
50 BTN_4, /* 8 softkeys */
58 KEY_BRL_DOT4, /* touch sensitive knobs */
68 #define DEG90 (range / 2)
69 #define DEG180 (range)
70 #define DEG270 (DEG90 + DEG180)
71 #define DEG360 (DEG180 * 2)
72 #define HIGH_PEAK (268)
75 /* some of these devices have endless rotation potentiometers
76 * built in which use two tapers, 90 degrees phase shifted.
77 * this algorithm decodes them to one single value, ranging
79 static unsigned int decode_erp(unsigned char a, unsigned char b)
81 int weight_a, weight_b;
84 int range = HIGH_PEAK - LOW_PEAK;
85 int mid_value = (HIGH_PEAK + LOW_PEAK) / 2;
87 weight_b = abs(mid_value - a) - (range / 2 - 100) / 2;
95 weight_a = 100 - weight_b;
98 /* 0..90 and 270..360 degrees */
99 pos_b = b - LOW_PEAK + DEG270;
103 /* 90..270 degrees */
104 pos_b = HIGH_PEAK - b + DEG90;
109 pos_a = a - LOW_PEAK;
111 /* 180..360 degrees */
112 pos_a = HIGH_PEAK - a + DEG180;
114 /* interpolate both slider values, depending on weight factors */
116 ret = pos_a * weight_a + pos_b * weight_b;
118 /* normalize to 0..999 */
139 static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
140 const unsigned char *buf,
143 struct input_dev *input_dev = dev->input_dev;
145 switch (dev->chip.usb_id) {
146 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
147 input_report_abs(input_dev, ABS_X, (buf[4] << 8) | buf[5]);
148 input_report_abs(input_dev, ABS_Y, (buf[0] << 8) | buf[1]);
149 input_report_abs(input_dev, ABS_Z, (buf[2] << 8) | buf[3]);
150 input_sync(input_dev);
152 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
153 input_report_abs(input_dev, ABS_X, (buf[0] << 8) | buf[1]);
154 input_report_abs(input_dev, ABS_Y, (buf[2] << 8) | buf[3]);
155 input_report_abs(input_dev, ABS_Z, (buf[4] << 8) | buf[5]);
156 input_sync(input_dev);
158 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
159 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
160 input_report_abs(input_dev, ABS_X, (buf[0] << 8) | buf[1]);
161 input_report_abs(input_dev, ABS_Y, (buf[2] << 8) | buf[3]);
162 input_report_abs(input_dev, ABS_Z, (buf[4] << 8) | buf[5]);
163 input_sync(input_dev);
168 static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
169 const char *buf, unsigned int len)
171 struct input_dev *input_dev = dev->input_dev;
174 switch (dev->chip.usb_id) {
175 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
176 i = decode_erp(buf[0], buf[1]);
177 input_report_abs(input_dev, ABS_X, i);
178 input_sync(input_dev);
180 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
181 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
182 i = decode_erp(buf[7], buf[5]);
183 input_report_abs(input_dev, ABS_HAT0X, i);
184 i = decode_erp(buf[12], buf[14]);
185 input_report_abs(input_dev, ABS_HAT0Y, i);
186 i = decode_erp(buf[15], buf[13]);
187 input_report_abs(input_dev, ABS_HAT1X, i);
188 i = decode_erp(buf[0], buf[2]);
189 input_report_abs(input_dev, ABS_HAT1Y, i);
190 i = decode_erp(buf[3], buf[1]);
191 input_report_abs(input_dev, ABS_HAT2X, i);
192 i = decode_erp(buf[8], buf[10]);
193 input_report_abs(input_dev, ABS_HAT2Y, i);
194 i = decode_erp(buf[11], buf[9]);
195 input_report_abs(input_dev, ABS_HAT3X, i);
196 i = decode_erp(buf[4], buf[6]);
197 input_report_abs(input_dev, ABS_HAT3Y, i);
198 input_sync(input_dev);
203 static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
204 char *buf, unsigned int len)
206 struct input_dev *input_dev = dev->input_dev;
207 unsigned short *keycode = input_dev->keycode;
213 if (input_dev->id.product == USB_PID_RIGKONTROL2)
214 for (i = 0; i < len; i++)
217 for (i = 0; i < input_dev->keycodemax && i < len * 8; i++)
218 input_report_key(input_dev, keycode[i],
219 buf[i / 8] & (1 << (i % 8)));
221 if (dev->chip.usb_id ==
222 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER) ||
224 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2))
225 input_report_abs(dev->input_dev, ABS_MISC, 255 - buf[4]);
227 input_sync(input_dev);
230 void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
234 if (!dev->input_dev || len < 1)
238 case EP1_CMD_READ_ANALOG:
239 snd_caiaq_input_read_analog(dev, buf + 1, len - 1);
241 case EP1_CMD_READ_ERP:
242 snd_caiaq_input_read_erp(dev, buf + 1, len - 1);
244 case EP1_CMD_READ_IO:
245 snd_caiaq_input_read_io(dev, buf + 1, len - 1);
250 int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
252 struct usb_device *usb_dev = dev->chip.dev;
253 struct input_dev *input;
256 input = input_allocate_device();
260 usb_make_path(usb_dev, dev->phys, sizeof(dev->phys));
261 strlcat(dev->phys, "/input0", sizeof(dev->phys));
263 input->name = dev->product_name;
264 input->phys = dev->phys;
265 usb_to_input_id(usb_dev, &input->id);
266 input->dev.parent = &usb_dev->dev;
268 switch (dev->chip.usb_id) {
269 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
270 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
271 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
273 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk2));
274 memcpy(dev->keycode, keycode_rk2, sizeof(keycode_rk2));
275 input->keycodemax = ARRAY_SIZE(keycode_rk2);
276 input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
277 input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
278 input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
279 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
281 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
282 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
283 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
285 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk3));
286 memcpy(dev->keycode, keycode_rk3, sizeof(keycode_rk3));
287 input->keycodemax = ARRAY_SIZE(keycode_rk3);
288 input_set_abs_params(input, ABS_X, 0, 1024, 0, 10);
289 input_set_abs_params(input, ABS_Y, 0, 1024, 0, 10);
290 input_set_abs_params(input, ABS_Z, 0, 1024, 0, 10);
291 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
293 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
294 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
295 input->absbit[0] = BIT_MASK(ABS_X);
296 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_ak1));
297 memcpy(dev->keycode, keycode_ak1, sizeof(keycode_ak1));
298 input->keycodemax = ARRAY_SIZE(keycode_ak1);
299 input_set_abs_params(input, ABS_X, 0, 999, 0, 10);
300 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 5);
302 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
303 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
304 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
305 input->absbit[0] = BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
306 BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y) |
307 BIT_MASK(ABS_HAT2X) | BIT_MASK(ABS_HAT2Y) |
308 BIT_MASK(ABS_HAT3X) | BIT_MASK(ABS_HAT3Y) |
309 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
311 input->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
312 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_kore));
313 memcpy(dev->keycode, keycode_kore, sizeof(keycode_kore));
314 input->keycodemax = ARRAY_SIZE(keycode_kore);
315 input_set_abs_params(input, ABS_HAT0X, 0, 999, 0, 10);
316 input_set_abs_params(input, ABS_HAT0Y, 0, 999, 0, 10);
317 input_set_abs_params(input, ABS_HAT1X, 0, 999, 0, 10);
318 input_set_abs_params(input, ABS_HAT1Y, 0, 999, 0, 10);
319 input_set_abs_params(input, ABS_HAT2X, 0, 999, 0, 10);
320 input_set_abs_params(input, ABS_HAT2Y, 0, 999, 0, 10);
321 input_set_abs_params(input, ABS_HAT3X, 0, 999, 0, 10);
322 input_set_abs_params(input, ABS_HAT3Y, 0, 999, 0, 10);
323 input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
324 input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
325 input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
326 input_set_abs_params(input, ABS_MISC, 0, 255, 0, 1);
327 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 5);
330 /* no input methods supported on this device */
331 input_free_device(input);
335 input->keycode = dev->keycode;
336 input->keycodesize = sizeof(unsigned short);
337 for (i = 0; i < input->keycodemax; i++)
338 __set_bit(dev->keycode[i], input->keybit);
340 ret = input_register_device(input);
342 input_free_device(input);
346 dev->input_dev = input;
350 void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev)
352 if (!dev || !dev->input_dev)
355 input_unregister_device(dev->input_dev);
356 dev->input_dev = NULL;