ALSA: snd-usb-caiaq: clean up header includes
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / usb / caiaq / caiaq-input.c
CommitLineData
523f1dce
DM
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>
523f1dce 20#include <linux/usb.h>
b18b493f 21#include <linux/usb/input.h>
523f1dce
DM
22#include <sound/pcm.h>
23#include "caiaq-device.h"
24#include "caiaq-input.h"
25
b18b493f
DT
26static unsigned short keycode_ak1[] = { KEY_C, KEY_B, KEY_A };
27static unsigned short keycode_rk2[] = { KEY_1, KEY_2, KEY_3, KEY_4,
28 KEY_5, KEY_6, KEY_7 };
29static unsigned short keycode_rk3[] = { KEY_1, KEY_2, KEY_3, KEY_4,
30 KEY_5, KEY_6, KEY_7, KEY_5, KEY_6 };
523f1dce 31
8e3cd08e
DM
32static unsigned short keycode_kore[] = {
33 KEY_FN_F1, /* "menu" */
34 KEY_FN_F7, /* "lcd backlight */
35 KEY_FN_F2, /* "control" */
36 KEY_FN_F3, /* "enter" */
37 KEY_FN_F4, /* "view" */
38 KEY_FN_F5, /* "esc" */
39 KEY_FN_F6, /* "sound" */
40 KEY_FN_F8, /* array spacer, never triggered. */
41 KEY_RIGHT,
42 KEY_DOWN,
43 KEY_UP,
44 KEY_LEFT,
45 KEY_SOUND, /* "listen" */
46 KEY_RECORD,
47 KEY_PLAYPAUSE,
48 KEY_STOP,
49 BTN_4, /* 8 softkeys */
50 BTN_3,
51 BTN_2,
52 BTN_1,
53 BTN_8,
54 BTN_7,
55 BTN_6,
56 BTN_5,
57 KEY_BRL_DOT4, /* touch sensitive knobs */
58 KEY_BRL_DOT3,
59 KEY_BRL_DOT2,
60 KEY_BRL_DOT1,
61 KEY_BRL_DOT8,
62 KEY_BRL_DOT7,
63 KEY_BRL_DOT6,
64 KEY_BRL_DOT5
65};
66
b18b493f
DT
67#define DEG90 (range / 2)
68#define DEG180 (range)
69#define DEG270 (DEG90 + DEG180)
70#define DEG360 (DEG180 * 2)
71#define HIGH_PEAK (268)
72#define LOW_PEAK (-7)
523f1dce
DM
73
74/* some of these devices have endless rotation potentiometers
75 * built in which use two tapers, 90 degrees phase shifted.
76 * this algorithm decodes them to one single value, ranging
77 * from 0 to 999 */
78static unsigned int decode_erp(unsigned char a, unsigned char b)
79{
80 int weight_a, weight_b;
81 int pos_a, pos_b;
82 int ret;
83 int range = HIGH_PEAK - LOW_PEAK;
84 int mid_value = (HIGH_PEAK + LOW_PEAK) / 2;
85
b18b493f
DT
86 weight_b = abs(mid_value - a) - (range / 2 - 100) / 2;
87
523f1dce
DM
88 if (weight_b < 0)
89 weight_b = 0;
90
91 if (weight_b > 100)
92 weight_b = 100;
93
94 weight_a = 100 - weight_b;
95
96 if (a < mid_value) {
97 /* 0..90 and 270..360 degrees */
98 pos_b = b - LOW_PEAK + DEG270;
99 if (pos_b >= DEG360)
100 pos_b -= DEG360;
101 } else
102 /* 90..270 degrees */
103 pos_b = HIGH_PEAK - b + DEG90;
104
105
106 if (b > mid_value)
107 /* 0..180 degrees */
108 pos_a = a - LOW_PEAK;
109 else
110 /* 180..360 degrees */
111 pos_a = HIGH_PEAK - a + DEG180;
112
113 /* interpolate both slider values, depending on weight factors */
114 /* 0..99 x DEG360 */
115 ret = pos_a * weight_a + pos_b * weight_b;
116
117 /* normalize to 0..999 */
118 ret *= 10;
119 ret /= DEG360;
120
121 if (ret < 0)
122 ret += 1000;
b18b493f 123
523f1dce
DM
124 if (ret >= 1000)
125 ret -= 1000;
126
127 return ret;
128}
129
130#undef DEG90
131#undef DEG180
132#undef DEG270
133#undef DEG360
134#undef HIGH_PEAK
135#undef LOW_PEAK
136
137
b18b493f 138static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
ad1e34b5
DM
139 const unsigned char *buf,
140 unsigned int len)
523f1dce 141{
b18b493f
DT
142 struct input_dev *input_dev = dev->input_dev;
143
8e3cd08e
DM
144 switch (dev->chip.usb_id) {
145 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
b18b493f
DT
146 input_report_abs(input_dev, ABS_X, (buf[4] << 8) | buf[5]);
147 input_report_abs(input_dev, ABS_Y, (buf[0] << 8) | buf[1]);
148 input_report_abs(input_dev, ABS_Z, (buf[2] << 8) | buf[3]);
149 input_sync(input_dev);
523f1dce 150 break;
8e3cd08e
DM
151 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
152 input_report_abs(input_dev, ABS_X, (buf[0] << 8) | buf[1]);
153 input_report_abs(input_dev, ABS_Y, (buf[2] << 8) | buf[3]);
154 input_report_abs(input_dev, ABS_Z, (buf[4] << 8) | buf[5]);
155 input_sync(input_dev);
156 break;
157 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
7829d0ec 158 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
b18b493f
DT
159 input_report_abs(input_dev, ABS_X, (buf[0] << 8) | buf[1]);
160 input_report_abs(input_dev, ABS_Y, (buf[2] << 8) | buf[3]);
161 input_report_abs(input_dev, ABS_Z, (buf[4] << 8) | buf[5]);
162 input_sync(input_dev);
ad1e34b5 163 break;
523f1dce
DM
164 }
165}
166
b18b493f 167static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
523f1dce
DM
168 const char *buf, unsigned int len)
169{
b18b493f 170 struct input_dev *input_dev = dev->input_dev;
523f1dce
DM
171 int i;
172
8e3cd08e
DM
173 switch (dev->chip.usb_id) {
174 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
523f1dce 175 i = decode_erp(buf[0], buf[1]);
b18b493f
DT
176 input_report_abs(input_dev, ABS_X, i);
177 input_sync(input_dev);
523f1dce 178 break;
8e3cd08e 179 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
7829d0ec 180 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
8e3cd08e
DM
181 i = decode_erp(buf[7], buf[5]);
182 input_report_abs(input_dev, ABS_HAT0X, i);
183 i = decode_erp(buf[12], buf[14]);
184 input_report_abs(input_dev, ABS_HAT0Y, i);
185 i = decode_erp(buf[15], buf[13]);
186 input_report_abs(input_dev, ABS_HAT1X, i);
187 i = decode_erp(buf[0], buf[2]);
188 input_report_abs(input_dev, ABS_HAT1Y, i);
189 i = decode_erp(buf[3], buf[1]);
190 input_report_abs(input_dev, ABS_HAT2X, i);
191 i = decode_erp(buf[8], buf[10]);
192 input_report_abs(input_dev, ABS_HAT2Y, i);
193 i = decode_erp(buf[11], buf[9]);
194 input_report_abs(input_dev, ABS_HAT3X, i);
195 i = decode_erp(buf[4], buf[6]);
196 input_report_abs(input_dev, ABS_HAT3Y, i);
197 input_sync(input_dev);
198 break;
523f1dce
DM
199 }
200}
201
b18b493f 202static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
523f1dce
DM
203 char *buf, unsigned int len)
204{
b18b493f
DT
205 struct input_dev *input_dev = dev->input_dev;
206 unsigned short *keycode = input_dev->keycode;
523f1dce 207 int i;
523f1dce
DM
208
209 if (!keycode)
210 return;
211
b18b493f
DT
212 if (input_dev->id.product == USB_PID_RIGKONTROL2)
213 for (i = 0; i < len; i++)
523f1dce
DM
214 buf[i] = ~buf[i];
215
8e3cd08e 216 for (i = 0; i < input_dev->keycodemax && i < len * 8; i++)
b18b493f
DT
217 input_report_key(input_dev, keycode[i],
218 buf[i / 8] & (1 << (i % 8)));
523f1dce 219
8e3cd08e 220 if (dev->chip.usb_id ==
7829d0ec
DM
221 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER) ||
222 dev->chip.usb_id ==
223 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2))
8e3cd08e
DM
224 input_report_abs(dev->input_dev, ABS_MISC, 255 - buf[4]);
225
b18b493f 226 input_sync(input_dev);
523f1dce
DM
227}
228
b18b493f
DT
229void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
230 char *buf,
523f1dce
DM
231 unsigned int len)
232{
b18b493f 233 if (!dev->input_dev || len < 1)
523f1dce
DM
234 return;
235
236 switch (buf[0]) {
237 case EP1_CMD_READ_ANALOG:
b18b493f 238 snd_caiaq_input_read_analog(dev, buf + 1, len - 1);
523f1dce
DM
239 break;
240 case EP1_CMD_READ_ERP:
b18b493f 241 snd_caiaq_input_read_erp(dev, buf + 1, len - 1);
523f1dce
DM
242 break;
243 case EP1_CMD_READ_IO:
b18b493f 244 snd_caiaq_input_read_io(dev, buf + 1, len - 1);
523f1dce
DM
245 break;
246 }
247}
248
249int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
250{
251 struct usb_device *usb_dev = dev->chip.dev;
252 struct input_dev *input;
253 int i, ret;
254
255 input = input_allocate_device();
256 if (!input)
257 return -ENOMEM;
258
b18b493f
DT
259 usb_make_path(usb_dev, dev->phys, sizeof(dev->phys));
260 strlcat(dev->phys, "/input0", sizeof(dev->phys));
261
523f1dce 262 input->name = dev->product_name;
b18b493f
DT
263 input->phys = dev->phys;
264 usb_to_input_id(usb_dev, &input->id);
265 input->dev.parent = &usb_dev->dev;
523f1dce
DM
266
267 switch (dev->chip.usb_id) {
268 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
7b19ada2
JS
269 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
270 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
271 BIT_MASK(ABS_Z);
b18b493f
DT
272 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk2));
273 memcpy(dev->keycode, keycode_rk2, sizeof(keycode_rk2));
523f1dce 274 input->keycodemax = ARRAY_SIZE(keycode_rk2);
523f1dce
DM
275 input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
276 input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
277 input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
278 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
279 break;
ad1e34b5 280 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
8e3cd08e
DM
281 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
282 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
283 BIT_MASK(ABS_Z);
b18b493f
DT
284 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk3));
285 memcpy(dev->keycode, keycode_rk3, sizeof(keycode_rk3));
ad1e34b5 286 input->keycodemax = ARRAY_SIZE(keycode_rk3);
ad1e34b5
DM
287 input_set_abs_params(input, ABS_X, 0, 1024, 0, 10);
288 input_set_abs_params(input, ABS_Y, 0, 1024, 0, 10);
289 input_set_abs_params(input, ABS_Z, 0, 1024, 0, 10);
290 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
291 break;
523f1dce 292 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
7b19ada2
JS
293 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
294 input->absbit[0] = BIT_MASK(ABS_X);
b18b493f
DT
295 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_ak1));
296 memcpy(dev->keycode, keycode_ak1, sizeof(keycode_ak1));
523f1dce 297 input->keycodemax = ARRAY_SIZE(keycode_ak1);
523f1dce
DM
298 input_set_abs_params(input, ABS_X, 0, 999, 0, 10);
299 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 5);
300 break;
8e3cd08e 301 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
7829d0ec 302 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
8e3cd08e
DM
303 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
304 input->absbit[0] = BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
305 BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y) |
306 BIT_MASK(ABS_HAT2X) | BIT_MASK(ABS_HAT2Y) |
307 BIT_MASK(ABS_HAT3X) | BIT_MASK(ABS_HAT3Y) |
308 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
309 BIT_MASK(ABS_Z);
310 input->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
311 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_kore));
312 memcpy(dev->keycode, keycode_kore, sizeof(keycode_kore));
313 input->keycodemax = ARRAY_SIZE(keycode_kore);
314 input_set_abs_params(input, ABS_HAT0X, 0, 999, 0, 10);
315 input_set_abs_params(input, ABS_HAT0Y, 0, 999, 0, 10);
316 input_set_abs_params(input, ABS_HAT1X, 0, 999, 0, 10);
317 input_set_abs_params(input, ABS_HAT1Y, 0, 999, 0, 10);
318 input_set_abs_params(input, ABS_HAT2X, 0, 999, 0, 10);
319 input_set_abs_params(input, ABS_HAT2Y, 0, 999, 0, 10);
320 input_set_abs_params(input, ABS_HAT3X, 0, 999, 0, 10);
321 input_set_abs_params(input, ABS_HAT3Y, 0, 999, 0, 10);
322 input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
323 input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
324 input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
325 input_set_abs_params(input, ABS_MISC, 0, 255, 0, 1);
326 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 5);
327 break;
523f1dce
DM
328 default:
329 /* no input methods supported on this device */
330 input_free_device(input);
331 return 0;
332 }
333
b18b493f
DT
334 input->keycode = dev->keycode;
335 input->keycodesize = sizeof(unsigned short);
336 for (i = 0; i < input->keycodemax; i++)
337 __set_bit(dev->keycode[i], input->keybit);
338
523f1dce
DM
339 ret = input_register_device(input);
340 if (ret < 0) {
341 input_free_device(input);
342 return ret;
343 }
344
345 dev->input_dev = input;
346 return 0;
347}
348
349void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev)
350{
351 if (!dev || !dev->input_dev)
352 return;
353
354 input_unregister_device(dev->input_dev);
523f1dce
DM
355 dev->input_dev = NULL;
356}
357