blob: a213813487bd77b02211597a8448a9dde44d32ef [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
Tejun Heo7b7b9042010-03-30 02:52:29 +090019#include <linux/gfp.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020020#include <linux/init.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020021#include <linux/usb.h>
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010022#include <linux/usb/input.h>
Daniel Mack6da7a2a2010-03-22 13:13:37 +010023#include <sound/core.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020024#include <sound/pcm.h>
Daniel Mack936e7d02009-04-01 19:05:39 +020025
26#include "device.h"
27#include "input.h"
Daniel Mack523f1dc2007-03-26 19:11:24 +020028
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010029static unsigned short keycode_ak1[] = { KEY_C, KEY_B, KEY_A };
30static unsigned short keycode_rk2[] = { KEY_1, KEY_2, KEY_3, KEY_4,
31 KEY_5, KEY_6, KEY_7 };
32static unsigned short keycode_rk3[] = { KEY_1, KEY_2, KEY_3, KEY_4,
Daniel Mackf4389482011-08-06 09:13:08 +020033 KEY_5, KEY_6, KEY_7, KEY_8, KEY_9 };
Daniel Mack523f1dc2007-03-26 19:11:24 +020034
Daniel Mack8e3cd082007-11-22 11:40:04 +010035static 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
Daniel Mack15c5ab62010-09-10 17:04:57 +080070#define KONTROLX1_INPUTS (40)
71#define KONTROLS4_BUTTONS (12 * 8)
72#define KONTROLS4_AXIS (46)
73
74#define KONTROLS4_BUTTON(X) ((X) + BTN_MISC)
75#define KONTROLS4_ABS(X) ((X) + ABS_HAT0X)
Daniel Mack6da7a2a2010-03-22 13:13:37 +010076
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010077#define DEG90 (range / 2)
78#define DEG180 (range)
79#define DEG270 (DEG90 + DEG180)
80#define DEG360 (DEG180 * 2)
81#define HIGH_PEAK (268)
82#define LOW_PEAK (-7)
Daniel Mack523f1dc2007-03-26 19:11:24 +020083
84/* some of these devices have endless rotation potentiometers
85 * built in which use two tapers, 90 degrees phase shifted.
86 * this algorithm decodes them to one single value, ranging
87 * from 0 to 999 */
88static unsigned int decode_erp(unsigned char a, unsigned char b)
89{
90 int weight_a, weight_b;
91 int pos_a, pos_b;
92 int ret;
93 int range = HIGH_PEAK - LOW_PEAK;
94 int mid_value = (HIGH_PEAK + LOW_PEAK) / 2;
95
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010096 weight_b = abs(mid_value - a) - (range / 2 - 100) / 2;
97
Daniel Mack523f1dc2007-03-26 19:11:24 +020098 if (weight_b < 0)
99 weight_b = 0;
100
101 if (weight_b > 100)
102 weight_b = 100;
103
104 weight_a = 100 - weight_b;
105
106 if (a < mid_value) {
107 /* 0..90 and 270..360 degrees */
108 pos_b = b - LOW_PEAK + DEG270;
109 if (pos_b >= DEG360)
110 pos_b -= DEG360;
111 } else
112 /* 90..270 degrees */
113 pos_b = HIGH_PEAK - b + DEG90;
114
115
116 if (b > mid_value)
117 /* 0..180 degrees */
118 pos_a = a - LOW_PEAK;
119 else
120 /* 180..360 degrees */
121 pos_a = HIGH_PEAK - a + DEG180;
122
123 /* interpolate both slider values, depending on weight factors */
124 /* 0..99 x DEG360 */
125 ret = pos_a * weight_a + pos_b * weight_b;
126
127 /* normalize to 0..999 */
128 ret *= 10;
129 ret /= DEG360;
130
131 if (ret < 0)
132 ret += 1000;
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100133
Daniel Mack523f1dc2007-03-26 19:11:24 +0200134 if (ret >= 1000)
135 ret -= 1000;
136
137 return ret;
138}
139
140#undef DEG90
141#undef DEG180
142#undef DEG270
143#undef DEG360
144#undef HIGH_PEAK
145#undef LOW_PEAK
146
Daniel Mack15c5ab62010-09-10 17:04:57 +0800147static inline void snd_caiaq_input_report_abs(struct snd_usb_caiaqdev *dev,
148 int axis, const unsigned char *buf,
149 int offset)
150{
151 input_report_abs(dev->input_dev, axis,
152 (buf[offset * 2] << 8) | buf[offset * 2 + 1]);
153}
Daniel Mack523f1dc2007-03-26 19:11:24 +0200154
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100155static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
Daniel Mackad1e34b2007-09-17 14:45:14 +0200156 const unsigned char *buf,
157 unsigned int len)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200158{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100159 struct input_dev *input_dev = dev->input_dev;
160
Daniel Mack8e3cd082007-11-22 11:40:04 +0100161 switch (dev->chip.usb_id) {
162 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
Daniel Mack15c5ab62010-09-10 17:04:57 +0800163 snd_caiaq_input_report_abs(dev, ABS_X, buf, 2);
164 snd_caiaq_input_report_abs(dev, ABS_Y, buf, 0);
165 snd_caiaq_input_report_abs(dev, ABS_Z, buf, 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200166 break;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100167 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
Daniel Mack8e3cd082007-11-22 11:40:04 +0100168 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
Daniel Mack7829d0e2007-11-26 09:00:56 +0100169 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
Daniel Mack15c5ab62010-09-10 17:04:57 +0800170 snd_caiaq_input_report_abs(dev, ABS_X, buf, 0);
171 snd_caiaq_input_report_abs(dev, ABS_Y, buf, 1);
172 snd_caiaq_input_report_abs(dev, ABS_Z, buf, 2);
Daniel Mackad1e34b2007-09-17 14:45:14 +0200173 break;
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100174 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
Daniel Mack15c5ab62010-09-10 17:04:57 +0800175 snd_caiaq_input_report_abs(dev, ABS_HAT0X, buf, 4);
176 snd_caiaq_input_report_abs(dev, ABS_HAT0Y, buf, 2);
177 snd_caiaq_input_report_abs(dev, ABS_HAT1X, buf, 6);
178 snd_caiaq_input_report_abs(dev, ABS_HAT1Y, buf, 1);
179 snd_caiaq_input_report_abs(dev, ABS_HAT2X, buf, 7);
180 snd_caiaq_input_report_abs(dev, ABS_HAT2Y, buf, 0);
181 snd_caiaq_input_report_abs(dev, ABS_HAT3X, buf, 5);
182 snd_caiaq_input_report_abs(dev, ABS_HAT3Y, buf, 3);
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100183 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200184 }
Daniel Mack15c5ab62010-09-10 17:04:57 +0800185
186 input_sync(input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200187}
188
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100189static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200190 const char *buf, unsigned int len)
191{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100192 struct input_dev *input_dev = dev->input_dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200193 int i;
194
Daniel Mack8e3cd082007-11-22 11:40:04 +0100195 switch (dev->chip.usb_id) {
196 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Daniel Mack523f1dc2007-03-26 19:11:24 +0200197 i = decode_erp(buf[0], buf[1]);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100198 input_report_abs(input_dev, ABS_X, i);
199 input_sync(input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200200 break;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100201 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
Daniel Mack7829d0e2007-11-26 09:00:56 +0100202 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
Daniel Mack8e3cd082007-11-22 11:40:04 +0100203 i = decode_erp(buf[7], buf[5]);
204 input_report_abs(input_dev, ABS_HAT0X, i);
205 i = decode_erp(buf[12], buf[14]);
206 input_report_abs(input_dev, ABS_HAT0Y, i);
207 i = decode_erp(buf[15], buf[13]);
208 input_report_abs(input_dev, ABS_HAT1X, i);
209 i = decode_erp(buf[0], buf[2]);
210 input_report_abs(input_dev, ABS_HAT1Y, i);
211 i = decode_erp(buf[3], buf[1]);
212 input_report_abs(input_dev, ABS_HAT2X, i);
213 i = decode_erp(buf[8], buf[10]);
214 input_report_abs(input_dev, ABS_HAT2Y, i);
215 i = decode_erp(buf[11], buf[9]);
216 input_report_abs(input_dev, ABS_HAT3X, i);
217 i = decode_erp(buf[4], buf[6]);
218 input_report_abs(input_dev, ABS_HAT3Y, i);
219 input_sync(input_dev);
220 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200221 }
222}
223
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100224static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100225 unsigned char *buf, unsigned int len)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200226{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100227 struct input_dev *input_dev = dev->input_dev;
228 unsigned short *keycode = input_dev->keycode;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200229 int i;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200230
231 if (!keycode)
232 return;
233
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100234 if (input_dev->id.product == USB_PID_RIGKONTROL2)
235 for (i = 0; i < len; i++)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200236 buf[i] = ~buf[i];
237
Daniel Mack8e3cd082007-11-22 11:40:04 +0100238 for (i = 0; i < input_dev->keycodemax && i < len * 8; i++)
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100239 input_report_key(input_dev, keycode[i],
240 buf[i / 8] & (1 << (i % 8)));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200241
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100242 switch (dev->chip.usb_id) {
243 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
244 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
Daniel Mack8e3cd082007-11-22 11:40:04 +0100245 input_report_abs(dev->input_dev, ABS_MISC, 255 - buf[4]);
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100246 break;
247 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
248 /* rotary encoders */
249 input_report_abs(dev->input_dev, ABS_X, buf[5] & 0xf);
250 input_report_abs(dev->input_dev, ABS_Y, buf[5] >> 4);
251 input_report_abs(dev->input_dev, ABS_Z, buf[6] & 0xf);
252 input_report_abs(dev->input_dev, ABS_MISC, buf[6] >> 4);
253 break;
254 }
Daniel Mack8e3cd082007-11-22 11:40:04 +0100255
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100256 input_sync(input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200257}
258
Daniel Mack15c5ab62010-09-10 17:04:57 +0800259#define TKS4_MSGBLOCK_SIZE 16
260
261static void snd_usb_caiaq_tks4_dispatch(struct snd_usb_caiaqdev *dev,
262 const unsigned char *buf,
263 unsigned int len)
264{
265 while (len) {
266 unsigned int i, block_id = (buf[0] << 8) | buf[1];
267
268 switch (block_id) {
269 case 0:
270 /* buttons */
271 for (i = 0; i < KONTROLS4_BUTTONS; i++)
272 input_report_key(dev->input_dev, KONTROLS4_BUTTON(i),
273 (buf[4 + (i / 8)] >> (i % 8)) & 1);
274 break;
275
276 case 1:
277 /* left wheel */
278 input_report_abs(dev->input_dev, KONTROLS4_ABS(36), buf[9] | ((buf[8] & 0x3) << 8));
279 /* right wheel */
280 input_report_abs(dev->input_dev, KONTROLS4_ABS(37), buf[13] | ((buf[12] & 0x3) << 8));
281
282 /* rotary encoders */
283 input_report_abs(dev->input_dev, KONTROLS4_ABS(38), buf[3] & 0xf);
284 input_report_abs(dev->input_dev, KONTROLS4_ABS(39), buf[4] >> 4);
285 input_report_abs(dev->input_dev, KONTROLS4_ABS(40), buf[4] & 0xf);
286 input_report_abs(dev->input_dev, KONTROLS4_ABS(41), buf[5] >> 4);
287 input_report_abs(dev->input_dev, KONTROLS4_ABS(42), buf[5] & 0xf);
288 input_report_abs(dev->input_dev, KONTROLS4_ABS(43), buf[6] >> 4);
289 input_report_abs(dev->input_dev, KONTROLS4_ABS(44), buf[6] & 0xf);
290 input_report_abs(dev->input_dev, KONTROLS4_ABS(45), buf[7] >> 4);
291 input_report_abs(dev->input_dev, KONTROLS4_ABS(46), buf[7] & 0xf);
292
293 break;
294 case 2:
295 /* Volume Fader Channel D */
296 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(0), buf, 1);
297 /* Volume Fader Channel B */
298 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(1), buf, 2);
299 /* Volume Fader Channel A */
300 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(2), buf, 3);
301 /* Volume Fader Channel C */
302 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(3), buf, 4);
303 /* Loop Volume */
304 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(4), buf, 6);
305 /* Crossfader */
306 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(7), buf, 7);
307
308 break;
309
310 case 3:
311 /* Tempo Fader R */
312 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(6), buf, 3);
313 /* Tempo Fader L */
314 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(5), buf, 4);
315 /* Mic Volume */
316 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(8), buf, 6);
317 /* Cue Mix */
318 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(9), buf, 7);
319
320 break;
321
322 case 4:
323 /* Wheel distance sensor L */
324 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(10), buf, 1);
325 /* Wheel distance sensor R */
326 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(11), buf, 2);
327 /* Channel D EQ - Filter */
328 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(12), buf, 3);
329 /* Channel D EQ - Low */
330 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(13), buf, 4);
331 /* Channel D EQ - Mid */
332 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(14), buf, 5);
333 /* Channel D EQ - Hi */
334 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(15), buf, 6);
335 /* FX2 - dry/wet */
336 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(16), buf, 7);
337
338 break;
339
340 case 5:
341 /* FX2 - 1 */
342 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(17), buf, 1);
343 /* FX2 - 2 */
344 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(18), buf, 2);
345 /* FX2 - 3 */
346 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(19), buf, 3);
347 /* Channel B EQ - Filter */
348 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(20), buf, 4);
349 /* Channel B EQ - Low */
350 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(21), buf, 5);
351 /* Channel B EQ - Mid */
352 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(22), buf, 6);
353 /* Channel B EQ - Hi */
354 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(23), buf, 7);
355
356 break;
357
358 case 6:
359 /* Channel A EQ - Filter */
360 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(24), buf, 1);
361 /* Channel A EQ - Low */
362 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(25), buf, 2);
363 /* Channel A EQ - Mid */
364 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(26), buf, 3);
365 /* Channel A EQ - Hi */
366 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(27), buf, 4);
367 /* Channel C EQ - Filter */
368 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(28), buf, 5);
369 /* Channel C EQ - Low */
370 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(29), buf, 6);
371 /* Channel C EQ - Mid */
372 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(30), buf, 7);
373
374 break;
375
376 case 7:
377 /* Channel C EQ - Hi */
378 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(31), buf, 1);
379 /* FX1 - wet/dry */
380 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(32), buf, 2);
381 /* FX1 - 1 */
382 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(33), buf, 3);
383 /* FX1 - 2 */
384 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(34), buf, 4);
385 /* FX1 - 3 */
386 snd_caiaq_input_report_abs(dev, KONTROLS4_ABS(35), buf, 5);
387
388 break;
389
390 default:
391 debug("%s(): bogus block (id %d)\n",
392 __func__, block_id);
393 return;
394 }
395
396 len -= TKS4_MSGBLOCK_SIZE;
397 buf += TKS4_MSGBLOCK_SIZE;
398 }
399
400 input_sync(dev->input_dev);
401}
402
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100403static void snd_usb_caiaq_ep4_reply_dispatch(struct urb *urb)
404{
405 struct snd_usb_caiaqdev *dev = urb->context;
406 unsigned char *buf = urb->transfer_buffer;
407 int ret;
408
409 if (urb->status || !dev || urb != dev->ep4_in_urb)
410 return;
411
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100412 switch (dev->chip.usb_id) {
413 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
Daniel Mack15c5ab62010-09-10 17:04:57 +0800414 if (urb->actual_length < 24)
415 goto requeue;
416
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100417 if (buf[0] & 0x3)
418 snd_caiaq_input_read_io(dev, buf + 1, 7);
419
420 if (buf[0] & 0x4)
421 snd_caiaq_input_read_analog(dev, buf + 8, 16);
422
423 break;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800424
425 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4):
426 snd_usb_caiaq_tks4_dispatch(dev, buf, urb->actual_length);
427 break;
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100428 }
429
430requeue:
431 dev->ep4_in_urb->actual_length = 0;
432 ret = usb_submit_urb(dev->ep4_in_urb, GFP_ATOMIC);
433 if (ret < 0)
434 log("unable to submit urb. OOM!?\n");
435}
436
437static int snd_usb_caiaq_input_open(struct input_dev *idev)
438{
439 struct snd_usb_caiaqdev *dev = input_get_drvdata(idev);
440
441 if (!dev)
442 return -EINVAL;
443
444 switch (dev->chip.usb_id) {
445 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
Daniel Mack15c5ab62010-09-10 17:04:57 +0800446 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4):
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100447 if (usb_submit_urb(dev->ep4_in_urb, GFP_KERNEL) != 0)
448 return -EIO;
449 break;
450 }
451
452 return 0;
453}
454
455static void snd_usb_caiaq_input_close(struct input_dev *idev)
456{
457 struct snd_usb_caiaqdev *dev = input_get_drvdata(idev);
458
459 if (!dev)
460 return;
461
462 switch (dev->chip.usb_id) {
463 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
Daniel Mack15c5ab62010-09-10 17:04:57 +0800464 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4):
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100465 usb_kill_urb(dev->ep4_in_urb);
466 break;
467 }
468}
469
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100470void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
471 char *buf,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200472 unsigned int len)
473{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100474 if (!dev->input_dev || len < 1)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200475 return;
476
477 switch (buf[0]) {
478 case EP1_CMD_READ_ANALOG:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100479 snd_caiaq_input_read_analog(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200480 break;
481 case EP1_CMD_READ_ERP:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100482 snd_caiaq_input_read_erp(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200483 break;
484 case EP1_CMD_READ_IO:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100485 snd_caiaq_input_read_io(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200486 break;
487 }
488}
489
490int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
491{
492 struct usb_device *usb_dev = dev->chip.dev;
493 struct input_dev *input;
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100494 int i, ret = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200495
496 input = input_allocate_device();
497 if (!input)
498 return -ENOMEM;
499
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100500 usb_make_path(usb_dev, dev->phys, sizeof(dev->phys));
501 strlcat(dev->phys, "/input0", sizeof(dev->phys));
502
Daniel Mack523f1dc2007-03-26 19:11:24 +0200503 input->name = dev->product_name;
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100504 input->phys = dev->phys;
505 usb_to_input_id(usb_dev, &input->id);
506 input->dev.parent = &usb_dev->dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200507
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100508 input_set_drvdata(input, dev);
509
510 switch (dev->chip.usb_id) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200511 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700512 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
513 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
514 BIT_MASK(ABS_Z);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100515 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk2));
516 memcpy(dev->keycode, keycode_rk2, sizeof(keycode_rk2));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200517 input->keycodemax = ARRAY_SIZE(keycode_rk2);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200518 input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
519 input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
520 input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
521 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
522 break;
Daniel Mackad1e34b2007-09-17 14:45:14 +0200523 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
Daniel Mack8e3cd082007-11-22 11:40:04 +0100524 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
525 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
526 BIT_MASK(ABS_Z);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100527 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk3));
528 memcpy(dev->keycode, keycode_rk3, sizeof(keycode_rk3));
Daniel Mackad1e34b2007-09-17 14:45:14 +0200529 input->keycodemax = ARRAY_SIZE(keycode_rk3);
Daniel Mackad1e34b2007-09-17 14:45:14 +0200530 input_set_abs_params(input, ABS_X, 0, 1024, 0, 10);
531 input_set_abs_params(input, ABS_Y, 0, 1024, 0, 10);
532 input_set_abs_params(input, ABS_Z, 0, 1024, 0, 10);
533 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
534 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200535 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700536 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
537 input->absbit[0] = BIT_MASK(ABS_X);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100538 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_ak1));
539 memcpy(dev->keycode, keycode_ak1, sizeof(keycode_ak1));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200540 input->keycodemax = ARRAY_SIZE(keycode_ak1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200541 input_set_abs_params(input, ABS_X, 0, 999, 0, 10);
542 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 5);
543 break;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100544 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER):
Daniel Mack7829d0e2007-11-26 09:00:56 +0100545 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_KORECONTROLLER2):
Daniel Mack8e3cd082007-11-22 11:40:04 +0100546 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
547 input->absbit[0] = BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
548 BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y) |
549 BIT_MASK(ABS_HAT2X) | BIT_MASK(ABS_HAT2Y) |
550 BIT_MASK(ABS_HAT3X) | BIT_MASK(ABS_HAT3Y) |
551 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
552 BIT_MASK(ABS_Z);
553 input->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
554 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_kore));
555 memcpy(dev->keycode, keycode_kore, sizeof(keycode_kore));
556 input->keycodemax = ARRAY_SIZE(keycode_kore);
557 input_set_abs_params(input, ABS_HAT0X, 0, 999, 0, 10);
558 input_set_abs_params(input, ABS_HAT0Y, 0, 999, 0, 10);
559 input_set_abs_params(input, ABS_HAT1X, 0, 999, 0, 10);
560 input_set_abs_params(input, ABS_HAT1Y, 0, 999, 0, 10);
561 input_set_abs_params(input, ABS_HAT2X, 0, 999, 0, 10);
562 input_set_abs_params(input, ABS_HAT2Y, 0, 999, 0, 10);
563 input_set_abs_params(input, ABS_HAT3X, 0, 999, 0, 10);
564 input_set_abs_params(input, ABS_HAT3Y, 0, 999, 0, 10);
565 input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
566 input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
567 input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
568 input_set_abs_params(input, ABS_MISC, 0, 255, 0, 1);
569 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 5);
570 break;
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100571 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLX1):
572 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
573 input->absbit[0] = BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
574 BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y) |
575 BIT_MASK(ABS_HAT2X) | BIT_MASK(ABS_HAT2Y) |
576 BIT_MASK(ABS_HAT3X) | BIT_MASK(ABS_HAT3Y) |
577 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
578 BIT_MASK(ABS_Z);
579 input->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
580 BUILD_BUG_ON(sizeof(dev->keycode) < KONTROLX1_INPUTS);
581 for (i = 0; i < KONTROLX1_INPUTS; i++)
582 dev->keycode[i] = BTN_MISC + i;
583 input->keycodemax = KONTROLX1_INPUTS;
584
585 /* analog potentiometers */
586 input_set_abs_params(input, ABS_HAT0X, 0, 4096, 0, 10);
587 input_set_abs_params(input, ABS_HAT0Y, 0, 4096, 0, 10);
588 input_set_abs_params(input, ABS_HAT1X, 0, 4096, 0, 10);
589 input_set_abs_params(input, ABS_HAT1Y, 0, 4096, 0, 10);
590 input_set_abs_params(input, ABS_HAT2X, 0, 4096, 0, 10);
591 input_set_abs_params(input, ABS_HAT2Y, 0, 4096, 0, 10);
592 input_set_abs_params(input, ABS_HAT3X, 0, 4096, 0, 10);
593 input_set_abs_params(input, ABS_HAT3Y, 0, 4096, 0, 10);
594
595 /* rotary encoders */
596 input_set_abs_params(input, ABS_X, 0, 0xf, 0, 1);
597 input_set_abs_params(input, ABS_Y, 0, 0xf, 0, 1);
598 input_set_abs_params(input, ABS_Z, 0, 0xf, 0, 1);
599 input_set_abs_params(input, ABS_MISC, 0, 0xf, 0, 1);
600
601 dev->ep4_in_urb = usb_alloc_urb(0, GFP_KERNEL);
602 if (!dev->ep4_in_urb) {
603 ret = -ENOMEM;
604 goto exit_free_idev;
605 }
606
607 usb_fill_bulk_urb(dev->ep4_in_urb, usb_dev,
608 usb_rcvbulkpipe(usb_dev, 0x4),
609 dev->ep4_in_buf, EP4_BUFSIZE,
610 snd_usb_caiaq_ep4_reply_dispatch, dev);
611
612 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 5);
613
614 break;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800615
616 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORKONTROLS4):
617 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
618 BUILD_BUG_ON(sizeof(dev->keycode) < KONTROLS4_BUTTONS);
619 for (i = 0; i < KONTROLS4_BUTTONS; i++)
620 dev->keycode[i] = KONTROLS4_BUTTON(i);
621 input->keycodemax = KONTROLS4_BUTTONS;
622
623 for (i = 0; i < KONTROLS4_AXIS; i++) {
624 int axis = KONTROLS4_ABS(i);
625 input->absbit[BIT_WORD(axis)] |= BIT_MASK(axis);
626 }
627
628 /* 36 analog potentiometers and faders */
629 for (i = 0; i < 36; i++)
630 input_set_abs_params(input, KONTROLS4_ABS(i), 0, 0xfff, 0, 10);
631
632 /* 2 encoder wheels */
633 input_set_abs_params(input, KONTROLS4_ABS(36), 0, 0x3ff, 0, 1);
634 input_set_abs_params(input, KONTROLS4_ABS(37), 0, 0x3ff, 0, 1);
635
636 /* 9 rotary encoders */
637 for (i = 0; i < 9; i++)
638 input_set_abs_params(input, KONTROLS4_ABS(38+i), 0, 0xf, 0, 1);
639
640 dev->ep4_in_urb = usb_alloc_urb(0, GFP_KERNEL);
641 if (!dev->ep4_in_urb) {
642 ret = -ENOMEM;
643 goto exit_free_idev;
644 }
645
646 usb_fill_bulk_urb(dev->ep4_in_urb, usb_dev,
647 usb_rcvbulkpipe(usb_dev, 0x4),
648 dev->ep4_in_buf, EP4_BUFSIZE,
649 snd_usb_caiaq_ep4_reply_dispatch, dev);
650
651 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 5);
652
653 break;
654
Daniel Mack523f1dc2007-03-26 19:11:24 +0200655 default:
656 /* no input methods supported on this device */
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100657 goto exit_free_idev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200658 }
659
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100660 input->open = snd_usb_caiaq_input_open;
661 input->close = snd_usb_caiaq_input_close;
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100662 input->keycode = dev->keycode;
663 input->keycodesize = sizeof(unsigned short);
664 for (i = 0; i < input->keycodemax; i++)
665 __set_bit(dev->keycode[i], input->keybit);
666
Daniel Mack523f1dc2007-03-26 19:11:24 +0200667 ret = input_register_device(input);
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100668 if (ret < 0)
669 goto exit_free_idev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200670
671 dev->input_dev = input;
672 return 0;
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100673
674exit_free_idev:
675 input_free_device(input);
676 return ret;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200677}
678
679void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev)
680{
681 if (!dev || !dev->input_dev)
682 return;
683
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100684 usb_kill_urb(dev->ep4_in_urb);
685 usb_free_urb(dev->ep4_in_urb);
686 dev->ep4_in_urb = NULL;
687
Daniel Mack523f1dc2007-03-26 19:11:24 +0200688 input_unregister_device(dev->input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200689 dev->input_dev = NULL;
690}
691