blob: f743847a5e5a0a9e8aa7d1e8a67ccb204586821a [file] [log] [blame]
Daniel Mack523f1dc2007-03-26 19:11:24 +02001/*
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
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>
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010024#include <linux/usb/input.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020025#include <linux/spinlock.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020026#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
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010032static unsigned short keycode_ak1[] = { KEY_C, KEY_B, KEY_A };
33static unsigned short keycode_rk2[] = { KEY_1, KEY_2, KEY_3, KEY_4,
34 KEY_5, KEY_6, KEY_7 };
35static unsigned short keycode_rk3[] = { KEY_1, KEY_2, KEY_3, KEY_4,
36 KEY_5, KEY_6, KEY_7, KEY_5, KEY_6 };
Daniel Mack523f1dc2007-03-26 19:11:24 +020037
Daniel Mack8e3cd082007-11-22 11:40:04 +010038static 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. */
47 KEY_RIGHT,
48 KEY_DOWN,
49 KEY_UP,
50 KEY_LEFT,
51 KEY_SOUND, /* "listen" */
52 KEY_RECORD,
53 KEY_PLAYPAUSE,
54 KEY_STOP,
55 BTN_4, /* 8 softkeys */
56 BTN_3,
57 BTN_2,
58 BTN_1,
59 BTN_8,
60 BTN_7,
61 BTN_6,
62 BTN_5,
63 KEY_BRL_DOT4, /* touch sensitive knobs */
64 KEY_BRL_DOT3,
65 KEY_BRL_DOT2,
66 KEY_BRL_DOT1,
67 KEY_BRL_DOT8,
68 KEY_BRL_DOT7,
69 KEY_BRL_DOT6,
70 KEY_BRL_DOT5
71};
72
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010073#define DEG90 (range / 2)
74#define DEG180 (range)
75#define DEG270 (DEG90 + DEG180)
76#define DEG360 (DEG180 * 2)
77#define HIGH_PEAK (268)
78#define LOW_PEAK (-7)
Daniel Mack523f1dc2007-03-26 19:11:24 +020079
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
83 * from 0 to 999 */
84static unsigned int decode_erp(unsigned char a, unsigned char b)
85{
86 int weight_a, weight_b;
87 int pos_a, pos_b;
88 int ret;
89 int range = HIGH_PEAK - LOW_PEAK;
90 int mid_value = (HIGH_PEAK + LOW_PEAK) / 2;
91
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010092 weight_b = abs(mid_value - a) - (range / 2 - 100) / 2;
93
Daniel Mack523f1dc2007-03-26 19:11:24 +020094 if (weight_b < 0)
95 weight_b = 0;
96
97 if (weight_b > 100)
98 weight_b = 100;
99
100 weight_a = 100 - weight_b;
101
102 if (a < mid_value) {
103 /* 0..90 and 270..360 degrees */
104 pos_b = b - LOW_PEAK + DEG270;
105 if (pos_b >= DEG360)
106 pos_b -= DEG360;
107 } else
108 /* 90..270 degrees */
109 pos_b = HIGH_PEAK - b + DEG90;
110
111
112 if (b > mid_value)
113 /* 0..180 degrees */
114 pos_a = a - LOW_PEAK;
115 else
116 /* 180..360 degrees */
117 pos_a = HIGH_PEAK - a + DEG180;
118
119 /* interpolate both slider values, depending on weight factors */
120 /* 0..99 x DEG360 */
121 ret = pos_a * weight_a + pos_b * weight_b;
122
123 /* normalize to 0..999 */
124 ret *= 10;
125 ret /= DEG360;
126
127 if (ret < 0)
128 ret += 1000;
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100129
Daniel Mack523f1dc2007-03-26 19:11:24 +0200130 if (ret >= 1000)
131 ret -= 1000;
132
133 return ret;
134}
135
136#undef DEG90
137#undef DEG180
138#undef DEG270
139#undef DEG360
140#undef HIGH_PEAK
141#undef LOW_PEAK
142
143
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100144static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
Daniel Mackad1e34b2007-09-17 14:45:14 +0200145 const unsigned char *buf,
146 unsigned int len)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200147{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100148 struct input_dev *input_dev = dev->input_dev;
149
Daniel Mack8e3cd082007-11-22 11:40:04 +0100150 switch (dev->chip.usb_id) {
151 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100152 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);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200156 break;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100157 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);
162 break;
163 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
Daniel Mack7829d0e2007-11-26 09:00:56 +0100164 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100165 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);
Daniel Mackad1e34b2007-09-17 14:45:14 +0200169 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200170 }
171}
172
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100173static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200174 const char *buf, unsigned int len)
175{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100176 struct input_dev *input_dev = dev->input_dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200177 int i;
178
Daniel Mack8e3cd082007-11-22 11:40:04 +0100179 switch (dev->chip.usb_id) {
180 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Daniel Mack523f1dc2007-03-26 19:11:24 +0200181 i = decode_erp(buf[0], buf[1]);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100182 input_report_abs(input_dev, ABS_X, i);
183 input_sync(input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200184 break;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100185 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
Daniel Mack7829d0e2007-11-26 09:00:56 +0100186 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
Daniel Mack8e3cd082007-11-22 11:40:04 +0100187 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);
204 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200205 }
206}
207
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100208static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200209 char *buf, unsigned int len)
210{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100211 struct input_dev *input_dev = dev->input_dev;
212 unsigned short *keycode = input_dev->keycode;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200213 int i;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200214
215 if (!keycode)
216 return;
217
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100218 if (input_dev->id.product == USB_PID_RIGKONTROL2)
219 for (i = 0; i < len; i++)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200220 buf[i] = ~buf[i];
221
Daniel Mack8e3cd082007-11-22 11:40:04 +0100222 for (i = 0; i < input_dev->keycodemax && i < len * 8; i++)
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100223 input_report_key(input_dev, keycode[i],
224 buf[i / 8] & (1 << (i % 8)));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200225
Daniel Mack8e3cd082007-11-22 11:40:04 +0100226 if (dev->chip.usb_id ==
Daniel Mack7829d0e2007-11-26 09:00:56 +0100227 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER) ||
228 dev->chip.usb_id ==
229 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2))
Daniel Mack8e3cd082007-11-22 11:40:04 +0100230 input_report_abs(dev->input_dev, ABS_MISC, 255 - buf[4]);
231
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100232 input_sync(input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200233}
234
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100235void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
236 char *buf,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200237 unsigned int len)
238{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100239 if (!dev->input_dev || len < 1)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200240 return;
241
242 switch (buf[0]) {
243 case EP1_CMD_READ_ANALOG:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100244 snd_caiaq_input_read_analog(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200245 break;
246 case EP1_CMD_READ_ERP:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100247 snd_caiaq_input_read_erp(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200248 break;
249 case EP1_CMD_READ_IO:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100250 snd_caiaq_input_read_io(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200251 break;
252 }
253}
254
255int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
256{
257 struct usb_device *usb_dev = dev->chip.dev;
258 struct input_dev *input;
259 int i, ret;
260
261 input = input_allocate_device();
262 if (!input)
263 return -ENOMEM;
264
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100265 usb_make_path(usb_dev, dev->phys, sizeof(dev->phys));
266 strlcat(dev->phys, "/input0", sizeof(dev->phys));
267
Daniel Mack523f1dc2007-03-26 19:11:24 +0200268 input->name = dev->product_name;
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100269 input->phys = dev->phys;
270 usb_to_input_id(usb_dev, &input->id);
271 input->dev.parent = &usb_dev->dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200272
273 switch (dev->chip.usb_id) {
274 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700275 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
276 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
277 BIT_MASK(ABS_Z);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100278 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk2));
279 memcpy(dev->keycode, keycode_rk2, sizeof(keycode_rk2));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200280 input->keycodemax = ARRAY_SIZE(keycode_rk2);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200281 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);
285 break;
Daniel Mackad1e34b2007-09-17 14:45:14 +0200286 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
Daniel Mack8e3cd082007-11-22 11:40:04 +0100287 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
288 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
289 BIT_MASK(ABS_Z);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100290 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk3));
291 memcpy(dev->keycode, keycode_rk3, sizeof(keycode_rk3));
Daniel Mackad1e34b2007-09-17 14:45:14 +0200292 input->keycodemax = ARRAY_SIZE(keycode_rk3);
Daniel Mackad1e34b2007-09-17 14:45:14 +0200293 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);
297 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200298 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700299 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
300 input->absbit[0] = BIT_MASK(ABS_X);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100301 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_ak1));
302 memcpy(dev->keycode, keycode_ak1, sizeof(keycode_ak1));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200303 input->keycodemax = ARRAY_SIZE(keycode_ak1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200304 input_set_abs_params(input, ABS_X, 0, 999, 0, 10);
305 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 5);
306 break;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100307 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
Daniel Mack7829d0e2007-11-26 09:00:56 +0100308 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
Daniel Mack8e3cd082007-11-22 11:40:04 +0100309 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) |
315 BIT_MASK(ABS_Z);
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);
333 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200334 default:
335 /* no input methods supported on this device */
336 input_free_device(input);
337 return 0;
338 }
339
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100340 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);
344
Daniel Mack523f1dc2007-03-26 19:11:24 +0200345 ret = input_register_device(input);
346 if (ret < 0) {
347 input_free_device(input);
348 return ret;
349 }
350
351 dev->input_dev = input;
352 return 0;
353}
354
355void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev)
356{
357 if (!dev || !dev->input_dev)
358 return;
359
360 input_unregister_device(dev->input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200361 dev->input_dev = NULL;
362}
363