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