xref: /linux/sound/usb/caiaq/input.c (revision 2290c0d06d82faee87b1ab2d9d4f7bf81ef64379)
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/gfp.h>
20 #include <linux/init.h>
21 #include <linux/usb.h>
22 #include <linux/usb/input.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 
26 #include "device.h"
27 #include "input.h"
28 
29 static unsigned short keycode_ak1[] =  { KEY_C, KEY_B, KEY_A };
30 static unsigned short keycode_rk2[] =  { KEY_1, KEY_2, KEY_3, KEY_4,
31 					 KEY_5, KEY_6, KEY_7 };
32 static unsigned short keycode_rk3[] =  { KEY_1, KEY_2, KEY_3, KEY_4,
33 					 KEY_5, KEY_6, KEY_7, KEY_8, KEY_9 };
34 
35 static unsigned short keycode_kore[] = {
36 	KEY_FN_F1,      /* "menu"               */
37 	KEY_FN_F7,      /* "lcd backlight       */
38 	KEY_FN_F2,      /* "control"            */
39 	KEY_FN_F3,      /* "enter"              */
40 	KEY_FN_F4,      /* "view"               */
41 	KEY_FN_F5,      /* "esc"                */
42 	KEY_FN_F6,      /* "sound"              */
43 	KEY_FN_F8,      /* array spacer, never triggered. */
44 	KEY_RIGHT,
45 	KEY_DOWN,
46 	KEY_UP,
47 	KEY_LEFT,
48 	KEY_SOUND,      /* "listen"             */
49 	KEY_RECORD,
50 	KEY_PLAYPAUSE,
51 	KEY_STOP,
52 	BTN_4,          /* 8 softkeys */
53 	BTN_3,
54 	BTN_2,
55 	BTN_1,
56 	BTN_8,
57 	BTN_7,
58 	BTN_6,
59 	BTN_5,
60 	KEY_BRL_DOT4,   /* touch sensitive knobs */
61 	KEY_BRL_DOT3,
62 	KEY_BRL_DOT2,
63 	KEY_BRL_DOT1,
64 	KEY_BRL_DOT8,
65 	KEY_BRL_DOT7,
66 	KEY_BRL_DOT6,
67 	KEY_BRL_DOT5
68 };
69 
70 #define MASCHINE_BUTTONS   (42)
71 #define MASCHINE_BUTTON(X) ((X) + BTN_MISC)
72 #define MASCHINE_PADS      (16)
73 #define MASCHINE_PAD(X)    ((X) + ABS_PRESSURE)
74 
75 static unsigned short keycode_maschine[] = {
76 	MASCHINE_BUTTON(40), /* mute       */
77 	MASCHINE_BUTTON(39), /* solo       */
78 	MASCHINE_BUTTON(38), /* select     */
79 	MASCHINE_BUTTON(37), /* duplicate  */
80 	MASCHINE_BUTTON(36), /* navigate   */
81 	MASCHINE_BUTTON(35), /* pad mode   */
82 	MASCHINE_BUTTON(34), /* pattern    */
83 	MASCHINE_BUTTON(33), /* scene      */
84 	KEY_RESERVED, /* spacer */
85 
86 	MASCHINE_BUTTON(30), /* rec        */
87 	MASCHINE_BUTTON(31), /* erase      */
88 	MASCHINE_BUTTON(32), /* shift      */
89 	MASCHINE_BUTTON(28), /* grid       */
90 	MASCHINE_BUTTON(27), /* >          */
91 	MASCHINE_BUTTON(26), /* <          */
92 	MASCHINE_BUTTON(25), /* restart    */
93 
94 	MASCHINE_BUTTON(21), /* E          */
95 	MASCHINE_BUTTON(22), /* F          */
96 	MASCHINE_BUTTON(23), /* G          */
97 	MASCHINE_BUTTON(24), /* H          */
98 	MASCHINE_BUTTON(20), /* D          */
99 	MASCHINE_BUTTON(19), /* C          */
100 	MASCHINE_BUTTON(18), /* B          */
101 	MASCHINE_BUTTON(17), /* A          */
102 
103 	MASCHINE_BUTTON(0),  /* control    */
104 	MASCHINE_BUTTON(2),  /* browse     */
105 	MASCHINE_BUTTON(4),  /* <          */
106 	MASCHINE_BUTTON(6),  /* snap       */
107 	MASCHINE_BUTTON(7),  /* autowrite  */
108 	MASCHINE_BUTTON(5),  /* >          */
109 	MASCHINE_BUTTON(3),  /* sampling   */
110 	MASCHINE_BUTTON(1),  /* step       */
111 
112 	MASCHINE_BUTTON(15), /* 8 softkeys */
113 	MASCHINE_BUTTON(14),
114 	MASCHINE_BUTTON(13),
115 	MASCHINE_BUTTON(12),
116 	MASCHINE_BUTTON(11),
117 	MASCHINE_BUTTON(10),
118 	MASCHINE_BUTTON(9),
119 	MASCHINE_BUTTON(8),
120 
121 	MASCHINE_BUTTON(16), /* note repeat */
122 	MASCHINE_BUTTON(29)  /* play        */
123 };
124 
125 #define KONTROLX1_INPUTS	(40)
126 #define KONTROLS4_BUTTONS	(12 * 8)
127 #define KONTROLS4_AXIS		(46)
128 
129 #define KONTROLS4_BUTTON(X)	((X) + BTN_MISC)
130 #define KONTROLS4_ABS(X)	((X) + ABS_HAT0X)
131 
132 #define DEG90		(range / 2)
133 #define DEG180		(range)
134 #define DEG270		(DEG90 + DEG180)
135 #define DEG360		(DEG180 * 2)
136 #define HIGH_PEAK	(268)
137 #define LOW_PEAK	(-7)
138 
139 /* some of these devices have endless rotation potentiometers
140  * built in which use two tapers, 90 degrees phase shifted.
141  * this algorithm decodes them to one single value, ranging
142  * from 0 to 999 */
143 static unsigned int decode_erp(unsigned char a, unsigned char b)
144 {
145 	int weight_a, weight_b;
146 	int pos_a, pos_b;
147 	int ret;
148 	int range = HIGH_PEAK - LOW_PEAK;
149 	int mid_value = (HIGH_PEAK + LOW_PEAK) / 2;
150 
151 	weight_b = abs(mid_value - a) - (range / 2 - 100) / 2;
152 
153 	if (weight_b < 0)
154 		weight_b = 0;
155 
156 	if (weight_b > 100)
157 		weight_b = 100;
158 
159 	weight_a = 100 - weight_b;
160 
161 	if (a < mid_value) {
162 		/* 0..90 and 270..360 degrees */
163 		pos_b = b - LOW_PEAK + DEG270;
164 		if (pos_b >= DEG360)
165 			pos_b -= DEG360;
166 	} else
167 		/* 90..270 degrees */
168 		pos_b = HIGH_PEAK - b + DEG90;
169 
170 
171 	if (b > mid_value)
172 		/* 0..180 degrees */
173 		pos_a = a - LOW_PEAK;
174 	else
175 		/* 180..360 degrees */
176 		pos_a = HIGH_PEAK - a + DEG180;
177 
178 	/* interpolate both slider values, depending on weight factors */
179 	/* 0..99 x DEG360 */
180 	ret = pos_a * weight_a + pos_b * weight_b;
181 
182 	/* normalize to 0..999 */
183 	ret *= 10;
184 	ret /= DEG360;
185 
186 	if (ret < 0)
187 		ret += 1000;
188 
189 	if (ret >= 1000)
190 		ret -= 1000;
191 
192 	return ret;
193 }
194 
195 #undef DEG90
196 #undef DEG180
197 #undef DEG270
198 #undef DEG360
199 #undef HIGH_PEAK
200 #undef LOW_PEAK
201 
202 static inline void snd_caiaq_input_report_abs(struct snd_usb_caiaqdev *dev,
203 					      int axis, const unsigned char *buf,
204 					      int offset)
205 {
206 	input_report_abs(dev->input_dev, axis,
207 			 (buf[offset * 2] << 8) | buf[offset * 2 + 1]);
208 }
209 
210 static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
211 					const unsigned char *buf,
212 					unsigned int len)
213 {
214 	struct input_dev *input_dev = dev->input_dev;
215 
216 	switch (dev->chip.usb_id) {
217 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
218 		snd_caiaq_input_report_abs(dev, ABS_X, buf, 2);
219 		snd_caiaq_input_report_abs(dev, ABS_Y, buf, 0);
220 		snd_caiaq_input_report_abs(dev, ABS_Z, buf, 1);
221 		break;
222 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
223 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
224 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
225 		snd_caiaq_input_report_abs(dev, ABS_X, buf, 0);
226 		snd_caiaq_input_report_abs(dev, ABS_Y, buf, 1);
227 		snd_caiaq_input_report_abs(dev, ABS_Z, buf, 2);
228 		break;
229 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
230 		snd_caiaq_input_report_abs(dev, ABS_HAT0X, buf, 4);
231 		snd_caiaq_input_report_abs(dev, ABS_HAT0Y, buf, 2);
232 		snd_caiaq_input_report_abs(dev, ABS_HAT1X, buf, 6);
233 		snd_caiaq_input_report_abs(dev, ABS_HAT1Y, buf, 1);
234 		snd_caiaq_input_report_abs(dev, ABS_HAT2X, buf, 7);
235 		snd_caiaq_input_report_abs(dev, ABS_HAT2Y, buf, 0);
236 		snd_caiaq_input_report_abs(dev, ABS_HAT3X, buf, 5);
237 		snd_caiaq_input_report_abs(dev, ABS_HAT3Y, buf, 3);
238 		break;
239 	}
240 
241 	input_sync(input_dev);
242 }
243 
244 static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
245 				     const char *buf, unsigned int len)
246 {
247 	struct input_dev *input_dev = dev->input_dev;
248 	int i;
249 
250 	switch (dev->chip.usb_id) {
251 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
252 		i = decode_erp(buf[0], buf[1]);
253 		input_report_abs(input_dev, ABS_X, i);
254 		input_sync(input_dev);
255 		break;
256 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
257 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
258 		i = decode_erp(buf[7], buf[5]);
259 		input_report_abs(input_dev, ABS_HAT0X, i);
260 		i = decode_erp(buf[12], buf[14]);
261 		input_report_abs(input_dev, ABS_HAT0Y, i);
262 		i = decode_erp(buf[15], buf[13]);
263 		input_report_abs(input_dev, ABS_HAT1X, i);
264 		i = decode_erp(buf[0], buf[2]);
265 		input_report_abs(input_dev, ABS_HAT1Y, i);
266 		i = decode_erp(buf[3], buf[1]);
267 		input_report_abs(input_dev, ABS_HAT2X, i);
268 		i = decode_erp(buf[8], buf[10]);
269 		input_report_abs(input_dev, ABS_HAT2Y, i);
270 		i = decode_erp(buf[11], buf[9]);
271 		input_report_abs(input_dev, ABS_HAT3X, i);
272 		i = decode_erp(buf[4], buf[6]);
273 		input_report_abs(input_dev, ABS_HAT3Y, i);
274 		input_sync(input_dev);
275 		break;
276 
277 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_MASCHINECONTROLLER):
278 		/* 4 under the left screen */
279 		input_report_abs(input_dev, ABS_HAT0X, decode_erp(buf[21], buf[20]));
280 		input_report_abs(input_dev, ABS_HAT0Y, decode_erp(buf[15], buf[14]));
281 		input_report_abs(input_dev, ABS_HAT1X, decode_erp(buf[9],  buf[8]));
282 		input_report_abs(input_dev, ABS_HAT1Y, decode_erp(buf[3],  buf[2]));
283 
284 		/* 4 under the right screen */
285 		input_report_abs(input_dev, ABS_HAT2X, decode_erp(buf[19], buf[18]));
286 		input_report_abs(input_dev, ABS_HAT2Y, decode_erp(buf[13], buf[12]));
287 		input_report_abs(input_dev, ABS_HAT3X, decode_erp(buf[7],  buf[6]));
288 		input_report_abs(input_dev, ABS_HAT3Y, decode_erp(buf[1],  buf[0]));
289 
290 		/* volume */
291 		input_report_abs(input_dev, ABS_RX, decode_erp(buf[17], buf[16]));
292 		/* tempo */
293 		input_report_abs(input_dev, ABS_RY, decode_erp(buf[11], buf[10]));
294 		/* swing */
295 		input_report_abs(input_dev, ABS_RZ, decode_erp(buf[5],  buf[4]));
296 
297 		input_sync(input_dev);
298 		break;
299 	}
300 }
301 
302 static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
303 				    unsigned char *buf, unsigned int len)
304 {
305 	struct input_dev *input_dev = dev->input_dev;
306 	unsigned short *keycode = input_dev->keycode;
307 	int i;
308 
309 	if (!keycode)
310 		return;
311 
312 	if (input_dev->id.product == USB_PID_RIGKONTROL2)
313 		for (i = 0; i < len; i++)
314 			buf[i] = ~buf[i];
315 
316 	for (i = 0; i < input_dev->keycodemax && i < len * 8; i++)
317 		input_report_key(input_dev, keycode[i],
318 				 buf[i / 8] & (1 << (i % 8)));
319 
320 	switch (dev->chip.usb_id) {
321 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
322 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
323 		input_report_abs(dev->input_dev, ABS_MISC, 255 - buf[4]);
324 		break;
325 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
326 		/* rotary encoders */
327 		input_report_abs(dev->input_dev, ABS_X, buf[5] & 0xf);
328 		input_report_abs(dev->input_dev, ABS_Y, buf[5] >> 4);
329 		input_report_abs(dev->input_dev, ABS_Z, buf[6] & 0xf);
330 		input_report_abs(dev->input_dev, ABS_MISC, buf[6] >> 4);
331 		break;
332 	}
333 
334 	input_sync(input_dev);
335 }
336 
337 #define TKS4_MSGBLOCK_SIZE	16
338 
339 static void snd_usb_caiaq_tks4_dispatch(struct snd_usb_caiaqdev *dev,
340 					const unsigned char *buf,
341 					unsigned int len)
342 {
343 	while (len) {
344 		unsigned int i, block_id = (buf[0] << 8) | buf[1];
345 
346 		switch (block_id) {
347 		case 0:
348 			/* buttons */
349 			for (i = 0; i < KONTROLS4_BUTTONS; i++)
350 				input_report_key(dev->input_dev, KONTROLS4_BUTTON(i),
351 						 (buf[4 + (i / 8)] >> (i % 8)) & 1);
352 			break;
353 
354 		case 1:
355 			/* left wheel */
356 			input_report_abs(dev->input_dev, KONTROLS4_ABS(36), buf[9] | ((buf[8] & 0x3) << 8));
357 			/* right wheel */
358 			input_report_abs(dev->input_dev, KONTROLS4_ABS(37), buf[13] | ((buf[12] & 0x3) << 8));
359 
360 			/* rotary encoders */
361 			input_report_abs(dev->input_dev, KONTROLS4_ABS(38), buf[3] & 0xf);
362 			input_report_abs(dev->input_dev, KONTROLS4_ABS(39), buf[4] >> 4);
363 			input_report_abs(dev->input_dev, KONTROLS4_ABS(40), buf[4] & 0xf);
364 			input_report_abs(dev->input_dev, KONTROLS4_ABS(41), buf[5] >> 4);
365 			input_report_abs(dev->input_dev, KONTROLS4_ABS(42), buf[5] & 0xf);
366 			input_report_abs(dev->input_dev, KONTROLS4_ABS(43), buf[6] >> 4);
367 			input_report_abs(dev->input_dev, KONTROLS4_ABS(44), buf[6] & 0xf);
368 			input_report_abs(dev->input_dev, KONTROLS4_ABS(45), buf[7] >> 4);
369 			input_report_abs(dev->input_dev, KONTROLS4_ABS(46), buf[7] & 0xf);
370 
371 			break;
372 		case 2:
373 			/* Volume Fader Channel D */
374 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(0), buf, 1);
375 			/* Volume Fader Channel B */
376 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(1), buf, 2);
377 			/* Volume Fader Channel A */
378 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(2), buf, 3);
379 			/* Volume Fader Channel C */
380 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(3), buf, 4);
381 			/* Loop Volume */
382 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(4), buf, 6);
383 			/* Crossfader */
384 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(7), buf, 7);
385 
386 			break;
387 
388 		case 3:
389 			/* Tempo Fader R */
390 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(6), buf, 3);
391 			/* Tempo Fader L */
392 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(5), buf, 4);
393 			/* Mic Volume */
394 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(8), buf, 6);
395 			/* Cue Mix */
396 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(9), buf, 7);
397 
398 			break;
399 
400 		case 4:
401 			/* Wheel distance sensor L */
402 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(10), buf, 1);
403 			/* Wheel distance sensor R */
404 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(11), buf, 2);
405 			/* Channel D EQ - Filter */
406 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(12), buf, 3);
407 			/* Channel D EQ - Low */
408 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(13), buf, 4);
409 			/* Channel D EQ - Mid */
410 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(14), buf, 5);
411 			/* Channel D EQ - Hi */
412 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(15), buf, 6);
413 			/* FX2 - dry/wet */
414 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(16), buf, 7);
415 
416 			break;
417 
418 		case 5:
419 			/* FX2 - 1 */
420 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(17), buf, 1);
421 			/* FX2 - 2 */
422 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(18), buf, 2);
423 			/* FX2 - 3 */
424 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(19), buf, 3);
425 			/* Channel B EQ - Filter */
426 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(20), buf, 4);
427 			/* Channel B EQ - Low */
428 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(21), buf, 5);
429 			/* Channel B EQ - Mid */
430 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(22), buf, 6);
431 			/* Channel B EQ - Hi */
432 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(23), buf, 7);
433 
434 			break;
435 
436 		case 6:
437 			/* Channel A EQ - Filter */
438 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(24), buf, 1);
439 			/* Channel A EQ - Low */
440 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(25), buf, 2);
441 			/* Channel A EQ - Mid */
442 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(26), buf, 3);
443 			/* Channel A EQ - Hi */
444 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(27), buf, 4);
445 			/* Channel C EQ - Filter */
446 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(28), buf, 5);
447 			/* Channel C EQ - Low */
448 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(29), buf, 6);
449 			/* Channel C EQ - Mid */
450 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(30), buf, 7);
451 
452 			break;
453 
454 		case 7:
455 			/* Channel C EQ - Hi */
456 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(31), buf, 1);
457 			/* FX1 - wet/dry */
458 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(32), buf, 2);
459 			/* FX1 - 1 */
460 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(33), buf, 3);
461 			/* FX1 - 2 */
462 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(34), buf, 4);
463 			/* FX1 - 3 */
464 			snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(35), buf, 5);
465 
466 			break;
467 
468 		default:
469 			debug("%s(): bogus block (id %d)\n",
470 				__func__, block_id);
471 			return;
472 		}
473 
474 		len -= TKS4_MSGBLOCK_SIZE;
475 		buf += TKS4_MSGBLOCK_SIZE;
476 	}
477 
478 	input_sync(dev->input_dev);
479 }
480 
481 #define MASCHINE_MSGBLOCK_SIZE 2
482 
483 static void snd_usb_caiaq_maschine_dispatch(struct snd_usb_caiaqdev *dev,
484 					const unsigned char *buf,
485 					unsigned int len)
486 {
487 	unsigned int i, pad_id;
488 	uint16_t pressure;
489 
490 	for (i = 0; i < MASCHINE_PADS; i++) {
491 		pressure = be16_to_cpu(buf[i * 2] << 8 | buf[(i * 2) + 1]);
492 		pad_id = pressure >> 12;
493 
494 		input_report_abs(dev->input_dev, MASCHINE_PAD(pad_id), pressure & 0xfff);
495 	}
496 
497 	input_sync(dev->input_dev);
498 }
499 
500 static void snd_usb_caiaq_ep4_reply_dispatch(struct urb *urb)
501 {
502 	struct snd_usb_caiaqdev *dev = urb->context;
503 	unsigned char *buf = urb->transfer_buffer;
504 	int ret;
505 
506 	if (urb->status || !dev || urb != dev->ep4_in_urb)
507 		return;
508 
509 	switch (dev->chip.usb_id) {
510 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
511 		if (urb->actual_length < 24)
512 			goto requeue;
513 
514 		if (buf[0] & 0x3)
515 			snd_caiaq_input_read_io(dev, buf + 1, 7);
516 
517 		if (buf[0] & 0x4)
518 			snd_caiaq_input_read_analog(dev, buf + 8, 16);
519 
520 		break;
521 
522 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4):
523 		snd_usb_caiaq_tks4_dispatch(dev, buf, urb->actual_length);
524 		break;
525 
526 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_MASCHINECONTROLLER):
527 		if (urb->actual_length < (MASCHINE_PADS * MASCHINE_MSGBLOCK_SIZE))
528 			goto requeue;
529 
530 		snd_usb_caiaq_maschine_dispatch(dev, buf, urb->actual_length);
531 		break;
532 	}
533 
534 requeue:
535 	dev->ep4_in_urb->actual_length = 0;
536 	ret = usb_submit_urb(dev->ep4_in_urb, GFP_ATOMIC);
537 	if (ret < 0)
538 		log("unable to submit urb. OOM!?\n");
539 }
540 
541 static int snd_usb_caiaq_input_open(struct input_dev *idev)
542 {
543 	struct snd_usb_caiaqdev *dev = input_get_drvdata(idev);
544 
545 	if (!dev)
546 		return -EINVAL;
547 
548 	switch (dev->chip.usb_id) {
549 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
550 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4):
551 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_MASCHINECONTROLLER):
552 		if (usb_submit_urb(dev->ep4_in_urb, GFP_KERNEL) != 0)
553 			return -EIO;
554 		break;
555 	}
556 
557 	return 0;
558 }
559 
560 static void snd_usb_caiaq_input_close(struct input_dev *idev)
561 {
562 	struct snd_usb_caiaqdev *dev = input_get_drvdata(idev);
563 
564 	if (!dev)
565 		return;
566 
567 	switch (dev->chip.usb_id) {
568 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
569 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4):
570 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_MASCHINECONTROLLER):
571 		usb_kill_urb(dev->ep4_in_urb);
572 		break;
573 	}
574 }
575 
576 void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
577 				  char *buf,
578 				  unsigned int len)
579 {
580 	if (!dev->input_dev || len < 1)
581 		return;
582 
583 	switch (buf[0]) {
584 	case EP1_CMD_READ_ANALOG:
585 		snd_caiaq_input_read_analog(dev, buf + 1, len - 1);
586 		break;
587 	case EP1_CMD_READ_ERP:
588 		snd_caiaq_input_read_erp(dev, buf + 1, len - 1);
589 		break;
590 	case EP1_CMD_READ_IO:
591 		snd_caiaq_input_read_io(dev, buf + 1, len - 1);
592 		break;
593 	}
594 }
595 
596 int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
597 {
598 	struct usb_device *usb_dev = dev->chip.dev;
599 	struct input_dev *input;
600 	int i, ret = 0;
601 
602 	input = input_allocate_device();
603 	if (!input)
604 		return -ENOMEM;
605 
606 	usb_make_path(usb_dev, dev->phys, sizeof(dev->phys));
607 	strlcat(dev->phys, "/input0", sizeof(dev->phys));
608 
609 	input->name = dev->product_name;
610 	input->phys = dev->phys;
611 	usb_to_input_id(usb_dev, &input->id);
612 	input->dev.parent = &usb_dev->dev;
613 
614 	input_set_drvdata(input, dev);
615 
616 	switch (dev->chip.usb_id) {
617 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
618 		input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
619 		input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
620 			BIT_MASK(ABS_Z);
621 		BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk2));
622 		memcpy(dev->keycode, keycode_rk2, sizeof(keycode_rk2));
623 		input->keycodemax = ARRAY_SIZE(keycode_rk2);
624 		input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
625 		input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
626 		input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
627 		snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
628 		break;
629 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
630 		input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
631 		input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
632 			BIT_MASK(ABS_Z);
633 		BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk3));
634 		memcpy(dev->keycode, keycode_rk3, sizeof(keycode_rk3));
635 		input->keycodemax = ARRAY_SIZE(keycode_rk3);
636 		input_set_abs_params(input, ABS_X, 0, 1024, 0, 10);
637 		input_set_abs_params(input, ABS_Y, 0, 1024, 0, 10);
638 		input_set_abs_params(input, ABS_Z, 0, 1024, 0, 10);
639 		snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
640 		break;
641 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
642 		input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
643 		input->absbit[0] = BIT_MASK(ABS_X);
644 		BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_ak1));
645 		memcpy(dev->keycode, keycode_ak1, sizeof(keycode_ak1));
646 		input->keycodemax = ARRAY_SIZE(keycode_ak1);
647 		input_set_abs_params(input, ABS_X, 0, 999, 0, 10);
648 		snd_usb_caiaq_set_auto_msg(dev, 1, 0, 5);
649 		break;
650 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
651 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
652 		input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
653 		input->absbit[0] = BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
654 				   BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y) |
655 				   BIT_MASK(ABS_HAT2X) | BIT_MASK(ABS_HAT2Y) |
656 				   BIT_MASK(ABS_HAT3X) | BIT_MASK(ABS_HAT3Y) |
657 				   BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
658 				   BIT_MASK(ABS_Z);
659 		input->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
660 		BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_kore));
661 		memcpy(dev->keycode, keycode_kore, sizeof(keycode_kore));
662 		input->keycodemax = ARRAY_SIZE(keycode_kore);
663 		input_set_abs_params(input, ABS_HAT0X, 0, 999, 0, 10);
664 		input_set_abs_params(input, ABS_HAT0Y, 0, 999, 0, 10);
665 		input_set_abs_params(input, ABS_HAT1X, 0, 999, 0, 10);
666 		input_set_abs_params(input, ABS_HAT1Y, 0, 999, 0, 10);
667 		input_set_abs_params(input, ABS_HAT2X, 0, 999, 0, 10);
668 		input_set_abs_params(input, ABS_HAT2Y, 0, 999, 0, 10);
669 		input_set_abs_params(input, ABS_HAT3X, 0, 999, 0, 10);
670 		input_set_abs_params(input, ABS_HAT3Y, 0, 999, 0, 10);
671 		input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
672 		input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
673 		input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
674 		input_set_abs_params(input, ABS_MISC, 0, 255, 0, 1);
675 		snd_usb_caiaq_set_auto_msg(dev, 1, 10, 5);
676 		break;
677 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
678 		input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
679 		input->absbit[0] = BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
680 				   BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y) |
681 				   BIT_MASK(ABS_HAT2X) | BIT_MASK(ABS_HAT2Y) |
682 				   BIT_MASK(ABS_HAT3X) | BIT_MASK(ABS_HAT3Y) |
683 				   BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
684 				   BIT_MASK(ABS_Z);
685 		input->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
686 		BUILD_BUG_ON(sizeof(dev->keycode) < KONTROLX1_INPUTS);
687 		for (i = 0; i < KONTROLX1_INPUTS; i++)
688 			dev->keycode[i] = BTN_MISC + i;
689 		input->keycodemax = KONTROLX1_INPUTS;
690 
691 		/* analog potentiometers */
692 		input_set_abs_params(input, ABS_HAT0X, 0, 4096, 0, 10);
693 		input_set_abs_params(input, ABS_HAT0Y, 0, 4096, 0, 10);
694 		input_set_abs_params(input, ABS_HAT1X, 0, 4096, 0, 10);
695 		input_set_abs_params(input, ABS_HAT1Y, 0, 4096, 0, 10);
696 		input_set_abs_params(input, ABS_HAT2X, 0, 4096, 0, 10);
697 		input_set_abs_params(input, ABS_HAT2Y, 0, 4096, 0, 10);
698 		input_set_abs_params(input, ABS_HAT3X, 0, 4096, 0, 10);
699 		input_set_abs_params(input, ABS_HAT3Y, 0, 4096, 0, 10);
700 
701 		/* rotary encoders */
702 		input_set_abs_params(input, ABS_X, 0, 0xf, 0, 1);
703 		input_set_abs_params(input, ABS_Y, 0, 0xf, 0, 1);
704 		input_set_abs_params(input, ABS_Z, 0, 0xf, 0, 1);
705 		input_set_abs_params(input, ABS_MISC, 0, 0xf, 0, 1);
706 
707 		dev->ep4_in_urb = usb_alloc_urb(0, GFP_KERNEL);
708 		if (!dev->ep4_in_urb) {
709 			ret = -ENOMEM;
710 			goto exit_free_idev;
711 		}
712 
713 		usb_fill_bulk_urb(dev->ep4_in_urb, usb_dev,
714 				  usb_rcvbulkpipe(usb_dev, 0x4),
715 				  dev->ep4_in_buf, EP4_BUFSIZE,
716 				  snd_usb_caiaq_ep4_reply_dispatch, dev);
717 
718 		snd_usb_caiaq_set_auto_msg(dev, 1, 10, 5);
719 
720 		break;
721 
722 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4):
723 		input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
724 		BUILD_BUG_ON(sizeof(dev->keycode) < KONTROLS4_BUTTONS);
725 		for (i = 0; i < KONTROLS4_BUTTONS; i++)
726 			dev->keycode[i] = KONTROLS4_BUTTON(i);
727 		input->keycodemax = KONTROLS4_BUTTONS;
728 
729 		for (i = 0; i < KONTROLS4_AXIS; i++) {
730 			int axis = KONTROLS4_ABS(i);
731 			input->absbit[BIT_WORD(axis)] |= BIT_MASK(axis);
732 		}
733 
734 		/* 36 analog potentiometers and faders */
735 		for (i = 0; i < 36; i++)
736 			input_set_abs_params(input, KONTROLS4_ABS(i), 0, 0xfff, 0, 10);
737 
738 		/* 2 encoder wheels */
739 		input_set_abs_params(input, KONTROLS4_ABS(36), 0, 0x3ff, 0, 1);
740 		input_set_abs_params(input, KONTROLS4_ABS(37), 0, 0x3ff, 0, 1);
741 
742 		/* 9 rotary encoders */
743 		for (i = 0; i < 9; i++)
744 			input_set_abs_params(input, KONTROLS4_ABS(38+i), 0, 0xf, 0, 1);
745 
746 		dev->ep4_in_urb = usb_alloc_urb(0, GFP_KERNEL);
747 		if (!dev->ep4_in_urb) {
748 			ret = -ENOMEM;
749 			goto exit_free_idev;
750 		}
751 
752 		usb_fill_bulk_urb(dev->ep4_in_urb, usb_dev,
753 				  usb_rcvbulkpipe(usb_dev, 0x4),
754 				  dev->ep4_in_buf, EP4_BUFSIZE,
755 				  snd_usb_caiaq_ep4_reply_dispatch, dev);
756 
757 		snd_usb_caiaq_set_auto_msg(dev, 1, 10, 5);
758 
759 		break;
760 
761 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_MASCHINECONTROLLER):
762 		input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
763 		input->absbit[0] = BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
764 			BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y) |
765 			BIT_MASK(ABS_HAT2X) | BIT_MASK(ABS_HAT2Y) |
766 			BIT_MASK(ABS_HAT3X) | BIT_MASK(ABS_HAT3Y) |
767 			BIT_MASK(ABS_RX) | BIT_MASK(ABS_RY) |
768 			BIT_MASK(ABS_RZ);
769 
770 		BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_maschine));
771 		memcpy(dev->keycode, keycode_maschine, sizeof(keycode_maschine));
772 		input->keycodemax = ARRAY_SIZE(keycode_maschine);
773 
774 		for (i = 0; i < MASCHINE_PADS; i++) {
775 			input->absbit[0] |= MASCHINE_PAD(i);
776 			input_set_abs_params(input, MASCHINE_PAD(i), 0, 0xfff, 5, 10);
777 		}
778 
779 		input_set_abs_params(input, ABS_HAT0X, 0, 999, 0, 10);
780 		input_set_abs_params(input, ABS_HAT0Y, 0, 999, 0, 10);
781 		input_set_abs_params(input, ABS_HAT1X, 0, 999, 0, 10);
782 		input_set_abs_params(input, ABS_HAT1Y, 0, 999, 0, 10);
783 		input_set_abs_params(input, ABS_HAT2X, 0, 999, 0, 10);
784 		input_set_abs_params(input, ABS_HAT2Y, 0, 999, 0, 10);
785 		input_set_abs_params(input, ABS_HAT3X, 0, 999, 0, 10);
786 		input_set_abs_params(input, ABS_HAT3Y, 0, 999, 0, 10);
787 		input_set_abs_params(input, ABS_RX, 0, 999, 0, 10);
788 		input_set_abs_params(input, ABS_RY, 0, 999, 0, 10);
789 		input_set_abs_params(input, ABS_RZ, 0, 999, 0, 10);
790 
791 		dev->ep4_in_urb = usb_alloc_urb(0, GFP_KERNEL);
792 		if (!dev->ep4_in_urb) {
793 			ret = -ENOMEM;
794 			goto exit_free_idev;
795 		}
796 
797 		usb_fill_bulk_urb(dev->ep4_in_urb, usb_dev,
798 				  usb_rcvbulkpipe(usb_dev, 0x4),
799 				  dev->ep4_in_buf, EP4_BUFSIZE,
800 				  snd_usb_caiaq_ep4_reply_dispatch, dev);
801 
802 		snd_usb_caiaq_set_auto_msg(dev, 1, 10, 5);
803 		break;
804 
805 	default:
806 		/* no input methods supported on this device */
807 		goto exit_free_idev;
808 	}
809 
810 	input->open = snd_usb_caiaq_input_open;
811 	input->close = snd_usb_caiaq_input_close;
812 	input->keycode = dev->keycode;
813 	input->keycodesize = sizeof(unsigned short);
814 	for (i = 0; i < input->keycodemax; i++)
815 		__set_bit(dev->keycode[i], input->keybit);
816 
817 	dev->input_dev = input;
818 
819 	ret = input_register_device(input);
820 	if (ret < 0)
821 		goto exit_free_idev;
822 
823 	return 0;
824 
825 exit_free_idev:
826 	input_free_device(input);
827 	dev->input_dev = NULL;
828 	return ret;
829 }
830 
831 void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev)
832 {
833 	if (!dev || !dev->input_dev)
834 		return;
835 
836 	usb_kill_urb(dev->ep4_in_urb);
837 	usb_free_urb(dev->ep4_in_urb);
838 	dev->ep4_in_urb = NULL;
839 
840 	input_unregister_device(dev->input_dev);
841 	dev->input_dev = NULL;
842 }
843