Merge branch 'topic/lx6464es' into for-linus
[linux-2.6] / sound / usb / 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/usb.h>
21 #include <linux/usb/input.h>
22 #include <sound/pcm.h>
23
24 #include "device.h"
25 #include "input.h"
26
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 };
32
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. */
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
68 #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)
74
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 */
79 static 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
87         weight_b = abs(mid_value - a) - (range / 2 - 100) / 2;
88
89         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;
124
125         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
139 static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
140                                         const unsigned char *buf,
141                                         unsigned int len)
142 {
143         struct input_dev *input_dev = dev->input_dev;
144
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);
151                 break;
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);
157                 break;
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);
164                 break;
165         }
166 }
167
168 static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
169                                      const char *buf, unsigned int len)
170 {
171         struct input_dev *input_dev = dev->input_dev;
172         int i;
173
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);
179                 break;
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);
199                 break;
200         }
201 }
202
203 static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
204                                     char *buf, unsigned int len)
205 {
206         struct input_dev *input_dev = dev->input_dev;
207         unsigned short *keycode = input_dev->keycode;
208         int i;
209
210         if (!keycode)
211                 return;
212
213         if (input_dev->id.product == USB_PID_RIGKONTROL2)
214                 for (i = 0; i < len; i++)
215                         buf[i] = ~buf[i];
216
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)));
220
221         if (dev->chip.usb_id ==
222                 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER) ||
223             dev->chip.usb_id ==
224                 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2))
225                 input_report_abs(dev->input_dev, ABS_MISC, 255 - buf[4]);
226
227         input_sync(input_dev);
228 }
229
230 void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
231                                   char *buf,
232                                   unsigned int len)
233 {
234         if (!dev->input_dev || len < 1)
235                 return;
236
237         switch (buf[0]) {
238         case EP1_CMD_READ_ANALOG:
239                 snd_caiaq_input_read_analog(dev, buf + 1, len - 1);
240                 break;
241         case EP1_CMD_READ_ERP:
242                 snd_caiaq_input_read_erp(dev, buf + 1, len - 1);
243                 break;
244         case EP1_CMD_READ_IO:
245                 snd_caiaq_input_read_io(dev, buf + 1, len - 1);
246                 break;
247         }
248 }
249
250 int 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
260         usb_make_path(usb_dev, dev->phys, sizeof(dev->phys));
261         strlcat(dev->phys, "/input0", sizeof(dev->phys));
262
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;
267
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) |
272                         BIT_MASK(ABS_Z);
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);
280                 break;
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) |
284                         BIT_MASK(ABS_Z);
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);
292                 break;
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);
301                 break;
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) |
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;
329         default:
330                 /* no input methods supported on this device */
331                 input_free_device(input);
332                 return 0;
333         }
334
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);
339
340         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
350 void 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);
356         dev->input_dev = NULL;
357 }
358