blob: a48d309bd94cb15757c79408d42bbcdf298adbed [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>
Daniel Mack523f1dc2007-03-26 19:11:24 +020020#include <linux/usb.h>
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010021#include <linux/usb/input.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020022#include <sound/pcm.h>
Daniel Mack936e7d02009-04-01 19:05:39 +020023
24#include "device.h"
25#include "input.h"
Daniel Mack523f1dc2007-03-26 19:11:24 +020026
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010027static unsigned short keycode_ak1[] = { KEY_C, KEY_B, KEY_A };
28static unsigned short keycode_rk2[] = { KEY_1, KEY_2, KEY_3, KEY_4,
29 KEY_5, KEY_6, KEY_7 };
30static unsigned short keycode_rk3[] = { KEY_1, KEY_2, KEY_3, KEY_4,
31 KEY_5, KEY_6, KEY_7, KEY_5, KEY_6 };
Daniel Mack523f1dc2007-03-26 19:11:24 +020032
Daniel Mack8e3cd082007-11-22 11:40:04 +010033static 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. */
42 KEY_RIGHT,
43 KEY_DOWN,
44 KEY_UP,
45 KEY_LEFT,
46 KEY_SOUND, /* "listen" */
47 KEY_RECORD,
48 KEY_PLAYPAUSE,
49 KEY_STOP,
50 BTN_4, /* 8 softkeys */
51 BTN_3,
52 BTN_2,
53 BTN_1,
54 BTN_8,
55 BTN_7,
56 BTN_6,
57 BTN_5,
58 KEY_BRL_DOT4, /* touch sensitive knobs */
59 KEY_BRL_DOT3,
60 KEY_BRL_DOT2,
61 KEY_BRL_DOT1,
62 KEY_BRL_DOT8,
63 KEY_BRL_DOT7,
64 KEY_BRL_DOT6,
65 KEY_BRL_DOT5
66};
67
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010068#define DEG90 (range / 2)
69#define DEG180 (range)
70#define DEG270 (DEG90 + DEG180)
71#define DEG360 (DEG180 * 2)
72#define HIGH_PEAK (268)
73#define LOW_PEAK (-7)
Daniel Mack523f1dc2007-03-26 19:11:24 +020074
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
78 * from 0 to 999 */
79static unsigned int decode_erp(unsigned char a, unsigned char b)
80{
81 int weight_a, weight_b;
82 int pos_a, pos_b;
83 int ret;
84 int range = HIGH_PEAK - LOW_PEAK;
85 int mid_value = (HIGH_PEAK + LOW_PEAK) / 2;
86
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010087 weight_b = abs(mid_value - a) - (range / 2 - 100) / 2;
88
Daniel Mack523f1dc2007-03-26 19:11:24 +020089 if (weight_b < 0)
90 weight_b = 0;
91
92 if (weight_b > 100)
93 weight_b = 100;
94
95 weight_a = 100 - weight_b;
96
97 if (a < mid_value) {
98 /* 0..90 and 270..360 degrees */
99 pos_b = b - LOW_PEAK + DEG270;
100 if (pos_b >= DEG360)
101 pos_b -= DEG360;
102 } else
103 /* 90..270 degrees */
104 pos_b = HIGH_PEAK - b + DEG90;
105
106
107 if (b > mid_value)
108 /* 0..180 degrees */
109 pos_a = a - LOW_PEAK;
110 else
111 /* 180..360 degrees */
112 pos_a = HIGH_PEAK - a + DEG180;
113
114 /* interpolate both slider values, depending on weight factors */
115 /* 0..99 x DEG360 */
116 ret = pos_a * weight_a + pos_b * weight_b;
117
118 /* normalize to 0..999 */
119 ret *= 10;
120 ret /= DEG360;
121
122 if (ret < 0)
123 ret += 1000;
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100124
Daniel Mack523f1dc2007-03-26 19:11:24 +0200125 if (ret >= 1000)
126 ret -= 1000;
127
128 return ret;
129}
130
131#undef DEG90
132#undef DEG180
133#undef DEG270
134#undef DEG360
135#undef HIGH_PEAK
136#undef LOW_PEAK
137
138
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100139static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
Daniel Mackad1e34b2007-09-17 14:45:14 +0200140 const unsigned char *buf,
141 unsigned int len)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200142{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100143 struct input_dev *input_dev = dev->input_dev;
144
Daniel Mack8e3cd082007-11-22 11:40:04 +0100145 switch (dev->chip.usb_id) {
146 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100147 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);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200151 break;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100152 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);
157 break;
158 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
Daniel Mack7829d0e2007-11-26 09:00:56 +0100159 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100160 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);
Daniel Mackad1e34b2007-09-17 14:45:14 +0200164 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200165 }
166}
167
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100168static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200169 const char *buf, unsigned int len)
170{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100171 struct input_dev *input_dev = dev->input_dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200172 int i;
173
Daniel Mack8e3cd082007-11-22 11:40:04 +0100174 switch (dev->chip.usb_id) {
175 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Daniel Mack523f1dc2007-03-26 19:11:24 +0200176 i = decode_erp(buf[0], buf[1]);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100177 input_report_abs(input_dev, ABS_X, i);
178 input_sync(input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200179 break;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100180 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
Daniel Mack7829d0e2007-11-26 09:00:56 +0100181 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
Daniel Mack8e3cd082007-11-22 11:40:04 +0100182 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);
199 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200200 }
201}
202
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100203static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200204 char *buf, unsigned int len)
205{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100206 struct input_dev *input_dev = dev->input_dev;
207 unsigned short *keycode = input_dev->keycode;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200208 int i;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200209
210 if (!keycode)
211 return;
212
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100213 if (input_dev->id.product == USB_PID_RIGKONTROL2)
214 for (i = 0; i < len; i++)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200215 buf[i] = ~buf[i];
216
Daniel Mack8e3cd082007-11-22 11:40:04 +0100217 for (i = 0; i < input_dev->keycodemax && i < len * 8; i++)
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100218 input_report_key(input_dev, keycode[i],
219 buf[i / 8] & (1 << (i % 8)));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200220
Daniel Mack8e3cd082007-11-22 11:40:04 +0100221 if (dev->chip.usb_id ==
Daniel Mack7829d0e2007-11-26 09:00:56 +0100222 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER) ||
223 dev->chip.usb_id ==
224 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2))
Daniel Mack8e3cd082007-11-22 11:40:04 +0100225 input_report_abs(dev->input_dev, ABS_MISC, 255 - buf[4]);
226
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100227 input_sync(input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200228}
229
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100230void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
231 char *buf,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200232 unsigned int len)
233{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100234 if (!dev->input_dev || len < 1)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200235 return;
236
237 switch (buf[0]) {
238 case EP1_CMD_READ_ANALOG:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100239 snd_caiaq_input_read_analog(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200240 break;
241 case EP1_CMD_READ_ERP:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100242 snd_caiaq_input_read_erp(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200243 break;
244 case EP1_CMD_READ_IO:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100245 snd_caiaq_input_read_io(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200246 break;
247 }
248}
249
250int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
251{
252 struct usb_device *usb_dev = dev->chip.dev;
253 struct input_dev *input;
254 int i, ret;
255
256 input = input_allocate_device();
257 if (!input)
258 return -ENOMEM;
259
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100260 usb_make_path(usb_dev, dev->phys, sizeof(dev->phys));
261 strlcat(dev->phys, "/input0", sizeof(dev->phys));
262
Daniel Mack523f1dc2007-03-26 19:11:24 +0200263 input->name = dev->product_name;
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100264 input->phys = dev->phys;
265 usb_to_input_id(usb_dev, &input->id);
266 input->dev.parent = &usb_dev->dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200267
268 switch (dev->chip.usb_id) {
269 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700270 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
271 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
272 BIT_MASK(ABS_Z);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100273 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk2));
274 memcpy(dev->keycode, keycode_rk2, sizeof(keycode_rk2));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200275 input->keycodemax = ARRAY_SIZE(keycode_rk2);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200276 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);
280 break;
Daniel Mackad1e34b2007-09-17 14:45:14 +0200281 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
Daniel Mack8e3cd082007-11-22 11:40:04 +0100282 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
283 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
284 BIT_MASK(ABS_Z);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100285 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk3));
286 memcpy(dev->keycode, keycode_rk3, sizeof(keycode_rk3));
Daniel Mackad1e34b2007-09-17 14:45:14 +0200287 input->keycodemax = ARRAY_SIZE(keycode_rk3);
Daniel Mackad1e34b2007-09-17 14:45:14 +0200288 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);
292 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200293 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700294 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
295 input->absbit[0] = BIT_MASK(ABS_X);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100296 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_ak1));
297 memcpy(dev->keycode, keycode_ak1, sizeof(keycode_ak1));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200298 input->keycodemax = ARRAY_SIZE(keycode_ak1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200299 input_set_abs_params(input, ABS_X, 0, 999, 0, 10);
300 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 5);
301 break;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100302 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
Daniel Mack7829d0e2007-11-26 09:00:56 +0100303 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
Daniel Mack8e3cd082007-11-22 11:40:04 +0100304 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) |
310 BIT_MASK(ABS_Z);
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);
328 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200329 default:
330 /* no input methods supported on this device */
331 input_free_device(input);
332 return 0;
333 }
334
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100335 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);
339
Daniel Mack523f1dc2007-03-26 19:11:24 +0200340 ret = input_register_device(input);
341 if (ret < 0) {
342 input_free_device(input);
343 return ret;
344 }
345
346 dev->input_dev = input;
347 return 0;
348}
349
350void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev)
351{
352 if (!dev || !dev->input_dev)
353 return;
354
355 input_unregister_device(dev->input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200356 dev->input_dev = NULL;
357}
358