input.c 26 KB

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