blob: 03bf4c63b5efaa7e6604af53a4c1ea89765b8719 [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
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>
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010024#include <linux/usb/input.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020025#include <linux/spinlock.h>
26#include <sound/driver.h>
27#include <sound/core.h>
28#include <sound/rawmidi.h>
29#include <sound/pcm.h>
30#include "caiaq-device.h"
31#include "caiaq-input.h"
32
33#ifdef CONFIG_SND_USB_CAIAQ_INPUT
34
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010035static unsigned short keycode_ak1[] = { KEY_C, KEY_B, KEY_A };
36static unsigned short keycode_rk2[] = { KEY_1, KEY_2, KEY_3, KEY_4,
37 KEY_5, KEY_6, KEY_7 };
38static unsigned short keycode_rk3[] = { KEY_1, KEY_2, KEY_3, KEY_4,
39 KEY_5, KEY_6, KEY_7, KEY_5, KEY_6 };
Daniel Mack523f1dc2007-03-26 19:11:24 +020040
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010041#define DEG90 (range / 2)
42#define DEG180 (range)
43#define DEG270 (DEG90 + DEG180)
44#define DEG360 (DEG180 * 2)
45#define HIGH_PEAK (268)
46#define LOW_PEAK (-7)
Daniel Mack523f1dc2007-03-26 19:11:24 +020047
48/* some of these devices have endless rotation potentiometers
49 * built in which use two tapers, 90 degrees phase shifted.
50 * this algorithm decodes them to one single value, ranging
51 * from 0 to 999 */
52static unsigned int decode_erp(unsigned char a, unsigned char b)
53{
54 int weight_a, weight_b;
55 int pos_a, pos_b;
56 int ret;
57 int range = HIGH_PEAK - LOW_PEAK;
58 int mid_value = (HIGH_PEAK + LOW_PEAK) / 2;
59
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010060 weight_b = abs(mid_value - a) - (range / 2 - 100) / 2;
61
Daniel Mack523f1dc2007-03-26 19:11:24 +020062 if (weight_b < 0)
63 weight_b = 0;
64
65 if (weight_b > 100)
66 weight_b = 100;
67
68 weight_a = 100 - weight_b;
69
70 if (a < mid_value) {
71 /* 0..90 and 270..360 degrees */
72 pos_b = b - LOW_PEAK + DEG270;
73 if (pos_b >= DEG360)
74 pos_b -= DEG360;
75 } else
76 /* 90..270 degrees */
77 pos_b = HIGH_PEAK - b + DEG90;
78
79
80 if (b > mid_value)
81 /* 0..180 degrees */
82 pos_a = a - LOW_PEAK;
83 else
84 /* 180..360 degrees */
85 pos_a = HIGH_PEAK - a + DEG180;
86
87 /* interpolate both slider values, depending on weight factors */
88 /* 0..99 x DEG360 */
89 ret = pos_a * weight_a + pos_b * weight_b;
90
91 /* normalize to 0..999 */
92 ret *= 10;
93 ret /= DEG360;
94
95 if (ret < 0)
96 ret += 1000;
Dmitry Torokhovb18b4932007-11-21 16:45:23 +010097
Daniel Mack523f1dc2007-03-26 19:11:24 +020098 if (ret >= 1000)
99 ret -= 1000;
100
101 return ret;
102}
103
104#undef DEG90
105#undef DEG180
106#undef DEG270
107#undef DEG360
108#undef HIGH_PEAK
109#undef LOW_PEAK
110
111
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100112static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev,
Daniel Mackad1e34b2007-09-17 14:45:14 +0200113 const unsigned char *buf,
114 unsigned int len)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200115{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100116 struct input_dev *input_dev = dev->input_dev;
117
118 switch (input_dev->id.product) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200119 case USB_PID_RIGKONTROL2:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100120 input_report_abs(input_dev, ABS_X, (buf[4] << 8) | buf[5]);
121 input_report_abs(input_dev, ABS_Y, (buf[0] << 8) | buf[1]);
122 input_report_abs(input_dev, ABS_Z, (buf[2] << 8) | buf[3]);
123 input_sync(input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200124 break;
Daniel Mackad1e34b2007-09-17 14:45:14 +0200125 case USB_PID_RIGKONTROL3:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100126 input_report_abs(input_dev, ABS_X, (buf[0] << 8) | buf[1]);
127 input_report_abs(input_dev, ABS_Y, (buf[2] << 8) | buf[3]);
128 input_report_abs(input_dev, ABS_Z, (buf[4] << 8) | buf[5]);
129 input_sync(input_dev);
Daniel Mackad1e34b2007-09-17 14:45:14 +0200130 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200131 }
132}
133
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100134static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200135 const char *buf, unsigned int len)
136{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100137 struct input_dev *input_dev = dev->input_dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200138 int i;
139
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100140 switch (input_dev->id.product) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200141 case USB_PID_AK1:
142 i = decode_erp(buf[0], buf[1]);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100143 input_report_abs(input_dev, ABS_X, i);
144 input_sync(input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200145 break;
146 }
147}
148
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100149static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200150 char *buf, unsigned int len)
151{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100152 struct input_dev *input_dev = dev->input_dev;
153 unsigned short *keycode = input_dev->keycode;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200154 int i;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200155
156 if (!keycode)
157 return;
158
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100159 if (input_dev->id.product == USB_PID_RIGKONTROL2)
160 for (i = 0; i < len; i++)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200161 buf[i] = ~buf[i];
162
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100163 for (i = 0; i < input_dev->keycodemax && i < len; i++)
164 input_report_key(input_dev, keycode[i],
165 buf[i / 8] & (1 << (i % 8)));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200166
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100167 input_sync(input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200168}
169
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100170void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev,
171 char *buf,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200172 unsigned int len)
173{
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100174 if (!dev->input_dev || len < 1)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200175 return;
176
177 switch (buf[0]) {
178 case EP1_CMD_READ_ANALOG:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100179 snd_caiaq_input_read_analog(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200180 break;
181 case EP1_CMD_READ_ERP:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100182 snd_caiaq_input_read_erp(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200183 break;
184 case EP1_CMD_READ_IO:
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100185 snd_caiaq_input_read_io(dev, buf + 1, len - 1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200186 break;
187 }
188}
189
190int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev)
191{
192 struct usb_device *usb_dev = dev->chip.dev;
193 struct input_dev *input;
194 int i, ret;
195
196 input = input_allocate_device();
197 if (!input)
198 return -ENOMEM;
199
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100200 usb_make_path(usb_dev, dev->phys, sizeof(dev->phys));
201 strlcat(dev->phys, "/input0", sizeof(dev->phys));
202
Daniel Mack523f1dc2007-03-26 19:11:24 +0200203 input->name = dev->product_name;
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100204 input->phys = dev->phys;
205 usb_to_input_id(usb_dev, &input->id);
206 input->dev.parent = &usb_dev->dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200207
208 switch (dev->chip.usb_id) {
209 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700210 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
211 input->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
212 BIT_MASK(ABS_Z);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100213 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk2));
214 memcpy(dev->keycode, keycode_rk2, sizeof(keycode_rk2));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200215 input->keycodemax = ARRAY_SIZE(keycode_rk2);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200216 input_set_abs_params(input, ABS_X, 0, 4096, 0, 10);
217 input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10);
218 input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10);
219 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
220 break;
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100221
Daniel Mackad1e34b2007-09-17 14:45:14 +0200222 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
223 input->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
224 input->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_Z);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100225 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_rk3));
226 memcpy(dev->keycode, keycode_rk3, sizeof(keycode_rk3));
Daniel Mackad1e34b2007-09-17 14:45:14 +0200227 input->keycodemax = ARRAY_SIZE(keycode_rk3);
Daniel Mackad1e34b2007-09-17 14:45:14 +0200228 input_set_abs_params(input, ABS_X, 0, 1024, 0, 10);
229 input_set_abs_params(input, ABS_Y, 0, 1024, 0, 10);
230 input_set_abs_params(input, ABS_Z, 0, 1024, 0, 10);
231 snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0);
232 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200233 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700234 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
235 input->absbit[0] = BIT_MASK(ABS_X);
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100236 BUILD_BUG_ON(sizeof(dev->keycode) < sizeof(keycode_ak1));
237 memcpy(dev->keycode, keycode_ak1, sizeof(keycode_ak1));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200238 input->keycodemax = ARRAY_SIZE(keycode_ak1);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200239 input_set_abs_params(input, ABS_X, 0, 999, 0, 10);
240 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 5);
241 break;
242 default:
243 /* no input methods supported on this device */
244 input_free_device(input);
245 return 0;
246 }
247
Dmitry Torokhovb18b4932007-11-21 16:45:23 +0100248 input->keycode = dev->keycode;
249 input->keycodesize = sizeof(unsigned short);
250 for (i = 0; i < input->keycodemax; i++)
251 __set_bit(dev->keycode[i], input->keybit);
252
Daniel Mack523f1dc2007-03-26 19:11:24 +0200253 ret = input_register_device(input);
254 if (ret < 0) {
255 input_free_device(input);
256 return ret;
257 }
258
259 dev->input_dev = input;
260 return 0;
261}
262
263void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev)
264{
265 if (!dev || !dev->input_dev)
266 return;
267
268 input_unregister_device(dev->input_dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200269 dev->input_dev = NULL;
270}
271
272#endif /* CONFIG_SND_USB_CAIAQ_INPUT */
273