Commit | Line | Data |
---|---|---|
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> | |
20 | #include <linux/module.h> | |
21 | #include <linux/moduleparam.h> | |
22 | #include <linux/input.h> | |
23 | #include <linux/usb.h> | |
24 | #include <linux/spinlock.h> | |
25 | #include <sound/driver.h> | |
26 | #include <sound/core.h> | |
27 | #include <sound/rawmidi.h> | |
28 | #include <sound/pcm.h> | |
29 | #include "caiaq-device.h" | |
30 | #include "caiaq-input.h" | |
31 | ||
32 | #ifdef CONFIG_SND_USB_CAIAQ_INPUT | |
33 | ||
34 | static unsigned char keycode_ak1[] = { KEY_C, KEY_B, KEY_A }; | |
35 | static unsigned char keycode_rk2[] = { KEY_1, KEY_2, KEY_3, KEY_4, | |
36 | KEY_5, KEY_6, KEY_7 }; | |
37 | ||
38 | #define DEG90 (range/2) | |
39 | #define DEG180 (range) | |
40 | #define DEG270 (DEG90 + DEG180) | |
41 | #define DEG360 (DEG180 * 2) | |
42 | #define HIGH_PEAK (268) | |
43 | #define LOW_PEAK (-7) | |
44 | ||
45 | /* some of these devices have endless rotation potentiometers | |
46 | * built in which use two tapers, 90 degrees phase shifted. | |
47 | * this algorithm decodes them to one single value, ranging | |
48 | * from 0 to 999 */ | |
49 | static unsigned int decode_erp(unsigned char a, unsigned char b) | |
50 | { | |
51 | int weight_a, weight_b; | |
52 | int pos_a, pos_b; | |
53 | int ret; | |
54 | int range = HIGH_PEAK - LOW_PEAK; | |
55 | int mid_value = (HIGH_PEAK + LOW_PEAK) / 2; | |
56 | ||
57 | weight_b = abs(mid_value-a) - (range/2 - 100)/2; | |
58 | ||
59 | if (weight_b < 0) | |
60 | weight_b = 0; | |
61 | ||
62 | if (weight_b > 100) | |
63 | weight_b = 100; | |
64 | ||
65 | weight_a = 100 - weight_b; | |
66 | ||
67 | if (a < mid_value) { | |
68 | /* 0..90 and 270..360 degrees */ | |
69 | pos_b = b - LOW_PEAK + DEG270; | |
70 | if (pos_b >= DEG360) | |
71 | pos_b -= DEG360; | |
72 | } else | |
73 | /* 90..270 degrees */ | |
74 | pos_b = HIGH_PEAK - b + DEG90; | |
75 | ||
76 | ||
77 | if (b > mid_value) | |
78 | /* 0..180 degrees */ | |
79 | pos_a = a - LOW_PEAK; | |
80 | else | |
81 | /* 180..360 degrees */ | |
82 | pos_a = HIGH_PEAK - a + DEG180; | |
83 | ||
84 | /* interpolate both slider values, depending on weight factors */ | |
85 | /* 0..99 x DEG360 */ | |
86 | ret = pos_a * weight_a + pos_b * weight_b; | |
87 | ||
88 | /* normalize to 0..999 */ | |
89 | ret *= 10; | |
90 | ret /= DEG360; | |
91 | ||
92 | if (ret < 0) | |
93 | ret += 1000; | |
94 | ||
95 | if (ret >= 1000) | |
96 | ret -= 1000; | |
97 | ||
98 | return ret; | |
99 | } | |
100 | ||
101 | #undef DEG90 | |
102 | #undef DEG180 | |
103 | #undef DEG270 | |
104 | #undef DEG360 | |
105 | #undef HIGH_PEAK | |
106 | #undef LOW_PEAK | |
107 | ||
108 | ||
109 | static void snd_caiaq_input_read_analog(struct snd_usb_caiaqdev *dev, | |
110 | const char *buf, unsigned int len) | |
111 | { | |
112 | switch(dev->input_dev->id.product) { | |
113 | case USB_PID_RIGKONTROL2: | |
114 | input_report_abs(dev->input_dev, ABS_X, (buf[4] << 8) |buf[5]); | |
115 | input_report_abs(dev->input_dev, ABS_Y, (buf[0] << 8) |buf[1]); | |
116 | input_report_abs(dev->input_dev, ABS_Z, (buf[2] << 8) |buf[3]); | |
117 | input_sync(dev->input_dev); | |
118 | break; | |
119 | } | |
120 | } | |
121 | ||
122 | static void snd_caiaq_input_read_erp(struct snd_usb_caiaqdev *dev, | |
123 | const char *buf, unsigned int len) | |
124 | { | |
125 | int i; | |
126 | ||
127 | switch(dev->input_dev->id.product) { | |
128 | case USB_PID_AK1: | |
129 | i = decode_erp(buf[0], buf[1]); | |
130 | input_report_abs(dev->input_dev, ABS_X, i); | |
131 | input_sync(dev->input_dev); | |
132 | break; | |
133 | } | |
134 | } | |
135 | ||
136 | static void snd_caiaq_input_read_io(struct snd_usb_caiaqdev *dev, | |
137 | char *buf, unsigned int len) | |
138 | { | |
139 | int i; | |
140 | unsigned char *keycode = dev->input_dev->keycode; | |
141 | ||
142 | if (!keycode) | |
143 | return; | |
144 | ||
145 | if (dev->input_dev->id.product == USB_PID_RIGKONTROL2) | |
146 | for (i=0; i<len; i++) | |
147 | buf[i] = ~buf[i]; | |
148 | ||
149 | for (i=0; (i<dev->input_dev->keycodemax) && (i < len); i++) | |
150 | input_report_key(dev->input_dev, keycode[i], | |
151 | buf[i/8] & (1 << (i%8))); | |
152 | ||
153 | input_sync(dev->input_dev); | |
154 | } | |
155 | ||
156 | void snd_usb_caiaq_input_dispatch(struct snd_usb_caiaqdev *dev, | |
157 | char *buf, | |
158 | unsigned int len) | |
159 | { | |
160 | if (!dev->input_dev || (len < 1)) | |
161 | return; | |
162 | ||
163 | switch (buf[0]) { | |
164 | case EP1_CMD_READ_ANALOG: | |
165 | snd_caiaq_input_read_analog(dev, buf+1, len-1); | |
166 | break; | |
167 | case EP1_CMD_READ_ERP: | |
168 | snd_caiaq_input_read_erp(dev, buf+1, len-1); | |
169 | break; | |
170 | case EP1_CMD_READ_IO: | |
171 | snd_caiaq_input_read_io(dev, buf+1, len-1); | |
172 | break; | |
173 | } | |
174 | } | |
175 | ||
176 | int snd_usb_caiaq_input_init(struct snd_usb_caiaqdev *dev) | |
177 | { | |
178 | struct usb_device *usb_dev = dev->chip.dev; | |
179 | struct input_dev *input; | |
180 | int i, ret; | |
181 | ||
182 | input = input_allocate_device(); | |
183 | if (!input) | |
184 | return -ENOMEM; | |
185 | ||
186 | input->name = dev->product_name; | |
187 | input->id.bustype = BUS_USB; | |
188 | input->id.vendor = usb_dev->descriptor.idVendor; | |
189 | input->id.product = usb_dev->descriptor.idProduct; | |
190 | input->id.version = usb_dev->descriptor.bcdDevice; | |
191 | ||
192 | switch (dev->chip.usb_id) { | |
193 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2): | |
194 | input->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); | |
195 | input->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_Z); | |
196 | input->keycode = keycode_rk2; | |
197 | input->keycodesize = sizeof(char); | |
198 | input->keycodemax = ARRAY_SIZE(keycode_rk2); | |
199 | for (i=0; i<ARRAY_SIZE(keycode_rk2); i++) | |
200 | set_bit(keycode_rk2[i], input->keybit); | |
201 | ||
202 | input_set_abs_params(input, ABS_X, 0, 4096, 0, 10); | |
203 | input_set_abs_params(input, ABS_Y, 0, 4096, 0, 10); | |
204 | input_set_abs_params(input, ABS_Z, 0, 4096, 0, 10); | |
205 | snd_usb_caiaq_set_auto_msg(dev, 1, 10, 0); | |
206 | break; | |
207 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1): | |
208 | input->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); | |
209 | input->absbit[0] = BIT(ABS_X); | |
210 | input->keycode = keycode_ak1; | |
211 | input->keycodesize = sizeof(char); | |
212 | input->keycodemax = ARRAY_SIZE(keycode_ak1); | |
213 | for (i=0; i<ARRAY_SIZE(keycode_ak1); i++) | |
214 | set_bit(keycode_ak1[i], input->keybit); | |
215 | ||
216 | input_set_abs_params(input, ABS_X, 0, 999, 0, 10); | |
217 | snd_usb_caiaq_set_auto_msg(dev, 1, 0, 5); | |
218 | break; | |
219 | default: | |
220 | /* no input methods supported on this device */ | |
221 | input_free_device(input); | |
222 | return 0; | |
223 | } | |
224 | ||
225 | ret = input_register_device(input); | |
226 | if (ret < 0) { | |
227 | input_free_device(input); | |
228 | return ret; | |
229 | } | |
230 | ||
231 | dev->input_dev = input; | |
232 | return 0; | |
233 | } | |
234 | ||
235 | void snd_usb_caiaq_input_free(struct snd_usb_caiaqdev *dev) | |
236 | { | |
237 | if (!dev || !dev->input_dev) | |
238 | return; | |
239 | ||
240 | input_unregister_device(dev->input_dev); | |
241 | input_free_device(dev->input_dev); | |
242 | dev->input_dev = NULL; | |
243 | } | |
244 | ||
245 | #endif /* CONFIG_SND_USB_CAIAQ_INPUT */ | |
246 |