Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/perex/alsa
[linux-2.6] / sound / usb / caiaq / caiaq-input.c
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
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"
31
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
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. */
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
73 #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)
79
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 */
84 static 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
92         weight_b = abs(mid_value - a) - (range / 2 - 100) / 2;
93
94         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;
129
130         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
144 static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
145                                         const unsigned char *buf,
146                                         unsigned int len)
147 {
148         struct input_dev *input_dev = dev->input_dev;
149
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);
156                 break;
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);
162                 break;
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);
169                 break;
170         }
171 }
172
173 static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
174                                      const char *buf, unsigned int len)
175 {
176         struct input_dev *input_dev = dev->input_dev;
177         int i;
178
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);
184                 break;
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);
204                 break;
205         }
206 }
207
208 static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
209                                     char *buf, unsigned int len)
210 {
211         struct input_dev *input_dev = dev->input_dev;
212         unsigned short *keycode = input_dev->keycode;
213         int i;
214
215         if (!keycode)
216                 return;
217
218         if (input_dev->id.product == USB_PID_RIGKONTROL2)
219                 for (i = 0; i < len; i++)
220                         buf[i] = ~buf[i];
221
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)));
225
226         if (dev->chip.usb_id ==
227                 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER) ||
228             dev->chip.usb_id ==
229                 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2))
230                 input_report_abs(dev->input_dev, ABS_MISC, 255 - buf[4]);
231
232         input_sync(input_dev);
233 }
234
235 void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
236                                   char *buf,
237                                   unsigned int len)
238 {
239         if (!dev->input_dev || len < 1)
240                 return;
241
242         switch (buf[0]) {
243         case EP1_CMD_READ_ANALOG:
244                 snd_caiaq_input_read_analog(dev, buf + 1, len - 1);
245                 break;
246         case EP1_CMD_READ_ERP:
247                 snd_caiaq_input_read_erp(dev, buf + 1, len - 1);
248                 break;
249         case EP1_CMD_READ_IO:
250                 snd_caiaq_input_read_io(dev, buf + 1, len - 1);
251                 break;
252         }
253 }
254
255 int 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
265         usb_make_path(usb_dev, dev->phys, sizeof(dev->phys));
266         strlcat(dev->phys, "/input0", sizeof(dev->phys));
267
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;
272
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) |
277                         BIT_MASK(ABS_Z);
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);
285                 break;
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) |
289                         BIT_MASK(ABS_Z);
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);
297                 break;
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);
306                 break;
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) |
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;
334         default:
335                 /* no input methods supported on this device */
336                 input_free_device(input);
337                 return 0;
338         }
339
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);
344
345         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
355 void 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);
361         dev->input_dev = NULL;
362 }
363