input.c 26 KB

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