hid-prodikeys.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HID driver for the Prodikeys PC-MIDI Keyboard
  4. * providing midi & extra multimedia keys functionality
  5. *
  6. * Copyright (c) 2009 Don Prince <dhprince.devel@yahoo.co.uk>
  7. *
  8. * Controls for Octave Shift Up/Down, Channel, and
  9. * Sustain Duration available via sysfs.
  10. */
  11. /*
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/device.h>
  15. #include <linux/module.h>
  16. #include <linux/usb.h>
  17. #include <linux/mutex.h>
  18. #include <linux/hid.h>
  19. #include <sound/core.h>
  20. #include <sound/initval.h>
  21. #include <sound/rawmidi.h>
  22. #include "hid-ids.h"
  23. #define pk_debug(format, arg...) \
  24. pr_debug("hid-prodikeys: " format "\n" , ## arg)
  25. #define pk_error(format, arg...) \
  26. pr_err("hid-prodikeys: " format "\n" , ## arg)
  27. struct pcmidi_snd;
  28. struct pcmidi_sustain {
  29. unsigned long in_use;
  30. struct pcmidi_snd *pm;
  31. struct timer_list timer;
  32. unsigned char status;
  33. unsigned char note;
  34. unsigned char velocity;
  35. };
  36. #define PCMIDI_SUSTAINED_MAX 32
  37. struct pcmidi_snd {
  38. struct hid_device *hdev;
  39. unsigned short ifnum;
  40. struct hid_report *pcmidi_report6;
  41. struct input_dev *input_ep82;
  42. unsigned short midi_mode;
  43. unsigned short midi_sustain_mode;
  44. unsigned short midi_sustain;
  45. unsigned short midi_channel;
  46. short midi_octave;
  47. struct pcmidi_sustain sustained_notes[PCMIDI_SUSTAINED_MAX];
  48. unsigned short fn_state;
  49. unsigned short last_key[24];
  50. spinlock_t rawmidi_in_lock;
  51. struct snd_card *card;
  52. struct snd_rawmidi *rwmidi;
  53. struct snd_rawmidi_substream *in_substream;
  54. unsigned long in_triggered;
  55. };
  56. #define PK_QUIRK_NOGET 0x00010000
  57. #define PCMIDI_MIDDLE_C 60
  58. #define PCMIDI_CHANNEL_MIN 0
  59. #define PCMIDI_CHANNEL_MAX 15
  60. #define PCMIDI_OCTAVE_MIN (-2)
  61. #define PCMIDI_OCTAVE_MAX 2
  62. #define PCMIDI_SUSTAIN_MIN 0
  63. #define PCMIDI_SUSTAIN_MAX 5000
  64. static const char shortname[] = "PC-MIDI";
  65. static const char longname[] = "Prodikeys PC-MIDI Keyboard";
  66. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  67. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  68. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  69. module_param_array(index, int, NULL, 0444);
  70. module_param_array(id, charp, NULL, 0444);
  71. module_param_array(enable, bool, NULL, 0444);
  72. MODULE_PARM_DESC(index, "Index value for the PC-MIDI virtual audio driver");
  73. MODULE_PARM_DESC(id, "ID string for the PC-MIDI virtual audio driver");
  74. MODULE_PARM_DESC(enable, "Enable for the PC-MIDI virtual audio driver");
  75. /* Output routine for the sysfs channel file */
  76. static ssize_t show_channel(struct device *dev,
  77. struct device_attribute *attr, char *buf)
  78. {
  79. struct hid_device *hdev = to_hid_device(dev);
  80. struct pcmidi_snd *pm = hid_get_drvdata(hdev);
  81. dbg_hid("pcmidi sysfs read channel=%u\n", pm->midi_channel);
  82. return sprintf(buf, "%u (min:%u, max:%u)\n", pm->midi_channel,
  83. PCMIDI_CHANNEL_MIN, PCMIDI_CHANNEL_MAX);
  84. }
  85. /* Input routine for the sysfs channel file */
  86. static ssize_t store_channel(struct device *dev,
  87. struct device_attribute *attr, const char *buf, size_t count)
  88. {
  89. struct hid_device *hdev = to_hid_device(dev);
  90. struct pcmidi_snd *pm = hid_get_drvdata(hdev);
  91. unsigned channel = 0;
  92. if (sscanf(buf, "%u", &channel) > 0 && channel <= PCMIDI_CHANNEL_MAX) {
  93. dbg_hid("pcmidi sysfs write channel=%u\n", channel);
  94. pm->midi_channel = channel;
  95. return strlen(buf);
  96. }
  97. return -EINVAL;
  98. }
  99. static DEVICE_ATTR(channel, S_IRUGO | S_IWUSR | S_IWGRP , show_channel,
  100. store_channel);
  101. static struct device_attribute *sysfs_device_attr_channel = {
  102. &dev_attr_channel,
  103. };
  104. /* Output routine for the sysfs sustain file */
  105. static ssize_t show_sustain(struct device *dev,
  106. struct device_attribute *attr, char *buf)
  107. {
  108. struct hid_device *hdev = to_hid_device(dev);
  109. struct pcmidi_snd *pm = hid_get_drvdata(hdev);
  110. dbg_hid("pcmidi sysfs read sustain=%u\n", pm->midi_sustain);
  111. return sprintf(buf, "%u (off:%u, max:%u (ms))\n", pm->midi_sustain,
  112. PCMIDI_SUSTAIN_MIN, PCMIDI_SUSTAIN_MAX);
  113. }
  114. /* Input routine for the sysfs sustain file */
  115. static ssize_t store_sustain(struct device *dev,
  116. struct device_attribute *attr, const char *buf, size_t count)
  117. {
  118. struct hid_device *hdev = to_hid_device(dev);
  119. struct pcmidi_snd *pm = hid_get_drvdata(hdev);
  120. unsigned sustain = 0;
  121. if (sscanf(buf, "%u", &sustain) > 0 && sustain <= PCMIDI_SUSTAIN_MAX) {
  122. dbg_hid("pcmidi sysfs write sustain=%u\n", sustain);
  123. pm->midi_sustain = sustain;
  124. pm->midi_sustain_mode = (0 == sustain || !pm->midi_mode) ? 0 : 1;
  125. return strlen(buf);
  126. }
  127. return -EINVAL;
  128. }
  129. static DEVICE_ATTR(sustain, S_IRUGO | S_IWUSR | S_IWGRP, show_sustain,
  130. store_sustain);
  131. static struct device_attribute *sysfs_device_attr_sustain = {
  132. &dev_attr_sustain,
  133. };
  134. /* Output routine for the sysfs octave file */
  135. static ssize_t show_octave(struct device *dev,
  136. struct device_attribute *attr, char *buf)
  137. {
  138. struct hid_device *hdev = to_hid_device(dev);
  139. struct pcmidi_snd *pm = hid_get_drvdata(hdev);
  140. dbg_hid("pcmidi sysfs read octave=%d\n", pm->midi_octave);
  141. return sprintf(buf, "%d (min:%d, max:%d)\n", pm->midi_octave,
  142. PCMIDI_OCTAVE_MIN, PCMIDI_OCTAVE_MAX);
  143. }
  144. /* Input routine for the sysfs octave file */
  145. static ssize_t store_octave(struct device *dev,
  146. struct device_attribute *attr, const char *buf, size_t count)
  147. {
  148. struct hid_device *hdev = to_hid_device(dev);
  149. struct pcmidi_snd *pm = hid_get_drvdata(hdev);
  150. int octave = 0;
  151. if (sscanf(buf, "%d", &octave) > 0 &&
  152. octave >= PCMIDI_OCTAVE_MIN && octave <= PCMIDI_OCTAVE_MAX) {
  153. dbg_hid("pcmidi sysfs write octave=%d\n", octave);
  154. pm->midi_octave = octave;
  155. return strlen(buf);
  156. }
  157. return -EINVAL;
  158. }
  159. static DEVICE_ATTR(octave, S_IRUGO | S_IWUSR | S_IWGRP, show_octave,
  160. store_octave);
  161. static struct device_attribute *sysfs_device_attr_octave = {
  162. &dev_attr_octave,
  163. };
  164. static void pcmidi_send_note(struct pcmidi_snd *pm,
  165. unsigned char status, unsigned char note, unsigned char velocity)
  166. {
  167. unsigned long flags;
  168. unsigned char buffer[3];
  169. buffer[0] = status;
  170. buffer[1] = note;
  171. buffer[2] = velocity;
  172. spin_lock_irqsave(&pm->rawmidi_in_lock, flags);
  173. if (!pm->in_substream)
  174. goto drop_note;
  175. if (!test_bit(pm->in_substream->number, &pm->in_triggered))
  176. goto drop_note;
  177. snd_rawmidi_receive(pm->in_substream, buffer, 3);
  178. drop_note:
  179. spin_unlock_irqrestore(&pm->rawmidi_in_lock, flags);
  180. return;
  181. }
  182. static void pcmidi_sustained_note_release(struct timer_list *t)
  183. {
  184. struct pcmidi_sustain *pms = from_timer(pms, t, timer);
  185. pcmidi_send_note(pms->pm, pms->status, pms->note, pms->velocity);
  186. pms->in_use = 0;
  187. }
  188. static void init_sustain_timers(struct pcmidi_snd *pm)
  189. {
  190. struct pcmidi_sustain *pms;
  191. unsigned i;
  192. for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) {
  193. pms = &pm->sustained_notes[i];
  194. pms->in_use = 0;
  195. pms->pm = pm;
  196. timer_setup(&pms->timer, pcmidi_sustained_note_release, 0);
  197. }
  198. }
  199. static void stop_sustain_timers(struct pcmidi_snd *pm)
  200. {
  201. struct pcmidi_sustain *pms;
  202. unsigned i;
  203. for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) {
  204. pms = &pm->sustained_notes[i];
  205. pms->in_use = 1;
  206. del_timer_sync(&pms->timer);
  207. }
  208. }
  209. static int pcmidi_get_output_report(struct pcmidi_snd *pm)
  210. {
  211. struct hid_device *hdev = pm->hdev;
  212. struct hid_report *report;
  213. list_for_each_entry(report,
  214. &hdev->report_enum[HID_OUTPUT_REPORT].report_list, list) {
  215. if (!(6 == report->id))
  216. continue;
  217. if (report->maxfield < 1) {
  218. hid_err(hdev, "output report is empty\n");
  219. break;
  220. }
  221. if (report->field[0]->report_count != 2) {
  222. hid_err(hdev, "field count too low\n");
  223. break;
  224. }
  225. pm->pcmidi_report6 = report;
  226. return 0;
  227. }
  228. /* should never get here */
  229. return -ENODEV;
  230. }
  231. static void pcmidi_submit_output_report(struct pcmidi_snd *pm, int state)
  232. {
  233. struct hid_device *hdev = pm->hdev;
  234. struct hid_report *report = pm->pcmidi_report6;
  235. report->field[0]->value[0] = 0x01;
  236. report->field[0]->value[1] = state;
  237. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  238. }
  239. static int pcmidi_handle_report1(struct pcmidi_snd *pm, u8 *data)
  240. {
  241. u32 bit_mask;
  242. bit_mask = data[1];
  243. bit_mask = (bit_mask << 8) | data[2];
  244. bit_mask = (bit_mask << 8) | data[3];
  245. dbg_hid("pcmidi mode: %d\n", pm->midi_mode);
  246. /*KEY_MAIL or octave down*/
  247. if (pm->midi_mode && bit_mask == 0x004000) {
  248. /* octave down */
  249. pm->midi_octave--;
  250. if (pm->midi_octave < -2)
  251. pm->midi_octave = -2;
  252. dbg_hid("pcmidi mode: %d octave: %d\n",
  253. pm->midi_mode, pm->midi_octave);
  254. return 1;
  255. }
  256. /*KEY_WWW or sustain*/
  257. else if (pm->midi_mode && bit_mask == 0x000004) {
  258. /* sustain on/off*/
  259. pm->midi_sustain_mode ^= 0x1;
  260. return 1;
  261. }
  262. return 0; /* continue key processing */
  263. }
  264. static int pcmidi_handle_report3(struct pcmidi_snd *pm, u8 *data, int size)
  265. {
  266. struct pcmidi_sustain *pms;
  267. unsigned i, j;
  268. unsigned char status, note, velocity;
  269. unsigned num_notes = (size-1)/2;
  270. for (j = 0; j < num_notes; j++) {
  271. note = data[j*2+1];
  272. velocity = data[j*2+2];
  273. if (note < 0x81) { /* note on */
  274. status = 128 + 16 + pm->midi_channel; /* 1001nnnn */
  275. note = note - 0x54 + PCMIDI_MIDDLE_C +
  276. (pm->midi_octave * 12);
  277. if (0 == velocity)
  278. velocity = 1; /* force note on */
  279. } else { /* note off */
  280. status = 128 + pm->midi_channel; /* 1000nnnn */
  281. note = note - 0x94 + PCMIDI_MIDDLE_C +
  282. (pm->midi_octave*12);
  283. if (pm->midi_sustain_mode) {
  284. for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) {
  285. pms = &pm->sustained_notes[i];
  286. if (!pms->in_use) {
  287. pms->status = status;
  288. pms->note = note;
  289. pms->velocity = velocity;
  290. pms->in_use = 1;
  291. mod_timer(&pms->timer,
  292. jiffies +
  293. msecs_to_jiffies(pm->midi_sustain));
  294. return 1;
  295. }
  296. }
  297. }
  298. }
  299. pcmidi_send_note(pm, status, note, velocity);
  300. }
  301. return 1;
  302. }
  303. static int pcmidi_handle_report4(struct pcmidi_snd *pm, u8 *data)
  304. {
  305. unsigned key;
  306. u32 bit_mask;
  307. u32 bit_index;
  308. bit_mask = data[1];
  309. bit_mask = (bit_mask << 8) | data[2];
  310. bit_mask = (bit_mask << 8) | data[3];
  311. /* break keys */
  312. for (bit_index = 0; bit_index < 24; bit_index++) {
  313. if (!((0x01 << bit_index) & bit_mask)) {
  314. input_event(pm->input_ep82, EV_KEY,
  315. pm->last_key[bit_index], 0);
  316. pm->last_key[bit_index] = 0;
  317. }
  318. }
  319. /* make keys */
  320. for (bit_index = 0; bit_index < 24; bit_index++) {
  321. key = 0;
  322. switch ((0x01 << bit_index) & bit_mask) {
  323. case 0x000010: /* Fn lock*/
  324. pm->fn_state ^= 0x000010;
  325. if (pm->fn_state)
  326. pcmidi_submit_output_report(pm, 0xc5);
  327. else
  328. pcmidi_submit_output_report(pm, 0xc6);
  329. continue;
  330. case 0x020000: /* midi launcher..send a key (qwerty) or not? */
  331. pcmidi_submit_output_report(pm, 0xc1);
  332. pm->midi_mode ^= 0x01;
  333. dbg_hid("pcmidi mode: %d\n", pm->midi_mode);
  334. continue;
  335. case 0x100000: /* KEY_MESSENGER or octave up */
  336. dbg_hid("pcmidi mode: %d\n", pm->midi_mode);
  337. if (pm->midi_mode) {
  338. pm->midi_octave++;
  339. if (pm->midi_octave > 2)
  340. pm->midi_octave = 2;
  341. dbg_hid("pcmidi mode: %d octave: %d\n",
  342. pm->midi_mode, pm->midi_octave);
  343. continue;
  344. } else
  345. key = KEY_MESSENGER;
  346. break;
  347. case 0x400000:
  348. key = KEY_CALENDAR;
  349. break;
  350. case 0x080000:
  351. key = KEY_ADDRESSBOOK;
  352. break;
  353. case 0x040000:
  354. key = KEY_DOCUMENTS;
  355. break;
  356. case 0x800000:
  357. key = KEY_WORDPROCESSOR;
  358. break;
  359. case 0x200000:
  360. key = KEY_SPREADSHEET;
  361. break;
  362. case 0x010000:
  363. key = KEY_COFFEE;
  364. break;
  365. case 0x000100:
  366. key = KEY_HELP;
  367. break;
  368. case 0x000200:
  369. key = KEY_SEND;
  370. break;
  371. case 0x000400:
  372. key = KEY_REPLY;
  373. break;
  374. case 0x000800:
  375. key = KEY_FORWARDMAIL;
  376. break;
  377. case 0x001000:
  378. key = KEY_NEW;
  379. break;
  380. case 0x002000:
  381. key = KEY_OPEN;
  382. break;
  383. case 0x004000:
  384. key = KEY_CLOSE;
  385. break;
  386. case 0x008000:
  387. key = KEY_SAVE;
  388. break;
  389. case 0x000001:
  390. key = KEY_UNDO;
  391. break;
  392. case 0x000002:
  393. key = KEY_REDO;
  394. break;
  395. case 0x000004:
  396. key = KEY_SPELLCHECK;
  397. break;
  398. case 0x000008:
  399. key = KEY_PRINT;
  400. break;
  401. }
  402. if (key) {
  403. input_event(pm->input_ep82, EV_KEY, key, 1);
  404. pm->last_key[bit_index] = key;
  405. }
  406. }
  407. return 1;
  408. }
  409. static int pcmidi_handle_report(
  410. struct pcmidi_snd *pm, unsigned report_id, u8 *data, int size)
  411. {
  412. int ret = 0;
  413. switch (report_id) {
  414. case 0x01: /* midi keys (qwerty)*/
  415. ret = pcmidi_handle_report1(pm, data);
  416. break;
  417. case 0x03: /* midi keyboard (musical)*/
  418. ret = pcmidi_handle_report3(pm, data, size);
  419. break;
  420. case 0x04: /* multimedia/midi keys (qwerty)*/
  421. ret = pcmidi_handle_report4(pm, data);
  422. break;
  423. }
  424. return ret;
  425. }
  426. static void pcmidi_setup_extra_keys(
  427. struct pcmidi_snd *pm, struct input_dev *input)
  428. {
  429. /* reassigned functionality for N/A keys
  430. MY PICTURES => KEY_WORDPROCESSOR
  431. MY MUSIC=> KEY_SPREADSHEET
  432. */
  433. static const unsigned int keys[] = {
  434. KEY_FN,
  435. KEY_MESSENGER, KEY_CALENDAR,
  436. KEY_ADDRESSBOOK, KEY_DOCUMENTS,
  437. KEY_WORDPROCESSOR,
  438. KEY_SPREADSHEET,
  439. KEY_COFFEE,
  440. KEY_HELP, KEY_SEND,
  441. KEY_REPLY, KEY_FORWARDMAIL,
  442. KEY_NEW, KEY_OPEN,
  443. KEY_CLOSE, KEY_SAVE,
  444. KEY_UNDO, KEY_REDO,
  445. KEY_SPELLCHECK, KEY_PRINT,
  446. 0
  447. };
  448. const unsigned int *pkeys = &keys[0];
  449. unsigned short i;
  450. if (pm->ifnum != 1) /* only set up ONCE for interace 1 */
  451. return;
  452. pm->input_ep82 = input;
  453. for (i = 0; i < 24; i++)
  454. pm->last_key[i] = 0;
  455. while (*pkeys != 0) {
  456. set_bit(*pkeys, pm->input_ep82->keybit);
  457. ++pkeys;
  458. }
  459. }
  460. static int pcmidi_set_operational(struct pcmidi_snd *pm)
  461. {
  462. int rc;
  463. if (pm->ifnum != 1)
  464. return 0; /* only set up ONCE for interace 1 */
  465. rc = pcmidi_get_output_report(pm);
  466. if (rc < 0)
  467. return rc;
  468. pcmidi_submit_output_report(pm, 0xc1);
  469. return 0;
  470. }
  471. static int pcmidi_snd_free(struct snd_device *dev)
  472. {
  473. return 0;
  474. }
  475. static int pcmidi_in_open(struct snd_rawmidi_substream *substream)
  476. {
  477. struct pcmidi_snd *pm = substream->rmidi->private_data;
  478. dbg_hid("pcmidi in open\n");
  479. pm->in_substream = substream;
  480. return 0;
  481. }
  482. static int pcmidi_in_close(struct snd_rawmidi_substream *substream)
  483. {
  484. dbg_hid("pcmidi in close\n");
  485. return 0;
  486. }
  487. static void pcmidi_in_trigger(struct snd_rawmidi_substream *substream, int up)
  488. {
  489. struct pcmidi_snd *pm = substream->rmidi->private_data;
  490. dbg_hid("pcmidi in trigger %d\n", up);
  491. pm->in_triggered = up;
  492. }
  493. static const struct snd_rawmidi_ops pcmidi_in_ops = {
  494. .open = pcmidi_in_open,
  495. .close = pcmidi_in_close,
  496. .trigger = pcmidi_in_trigger
  497. };
  498. static int pcmidi_snd_initialise(struct pcmidi_snd *pm)
  499. {
  500. static int dev;
  501. struct snd_card *card;
  502. struct snd_rawmidi *rwmidi;
  503. int err;
  504. static struct snd_device_ops ops = {
  505. .dev_free = pcmidi_snd_free,
  506. };
  507. if (pm->ifnum != 1)
  508. return 0; /* only set up midi device ONCE for interace 1 */
  509. if (dev >= SNDRV_CARDS)
  510. return -ENODEV;
  511. if (!enable[dev]) {
  512. dev++;
  513. return -ENOENT;
  514. }
  515. /* Setup sound card */
  516. err = snd_card_new(&pm->hdev->dev, index[dev], id[dev],
  517. THIS_MODULE, 0, &card);
  518. if (err < 0) {
  519. pk_error("failed to create pc-midi sound card\n");
  520. err = -ENOMEM;
  521. goto fail;
  522. }
  523. pm->card = card;
  524. /* Setup sound device */
  525. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, pm, &ops);
  526. if (err < 0) {
  527. pk_error("failed to create pc-midi sound device: error %d\n",
  528. err);
  529. goto fail;
  530. }
  531. strscpy(card->driver, shortname, sizeof(card->driver));
  532. strscpy(card->shortname, shortname, sizeof(card->shortname));
  533. strscpy(card->longname, longname, sizeof(card->longname));
  534. /* Set up rawmidi */
  535. err = snd_rawmidi_new(card, card->shortname, 0,
  536. 0, 1, &rwmidi);
  537. if (err < 0) {
  538. pk_error("failed to create pc-midi rawmidi device: error %d\n",
  539. err);
  540. goto fail;
  541. }
  542. pm->rwmidi = rwmidi;
  543. strscpy(rwmidi->name, card->shortname, sizeof(rwmidi->name));
  544. rwmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT;
  545. rwmidi->private_data = pm;
  546. snd_rawmidi_set_ops(rwmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  547. &pcmidi_in_ops);
  548. /* create sysfs variables */
  549. err = device_create_file(&pm->hdev->dev,
  550. sysfs_device_attr_channel);
  551. if (err < 0) {
  552. pk_error("failed to create sysfs attribute channel: error %d\n",
  553. err);
  554. goto fail;
  555. }
  556. err = device_create_file(&pm->hdev->dev,
  557. sysfs_device_attr_sustain);
  558. if (err < 0) {
  559. pk_error("failed to create sysfs attribute sustain: error %d\n",
  560. err);
  561. goto fail_attr_sustain;
  562. }
  563. err = device_create_file(&pm->hdev->dev,
  564. sysfs_device_attr_octave);
  565. if (err < 0) {
  566. pk_error("failed to create sysfs attribute octave: error %d\n",
  567. err);
  568. goto fail_attr_octave;
  569. }
  570. spin_lock_init(&pm->rawmidi_in_lock);
  571. init_sustain_timers(pm);
  572. err = pcmidi_set_operational(pm);
  573. if (err < 0) {
  574. pk_error("failed to find output report\n");
  575. goto fail_register;
  576. }
  577. /* register it */
  578. err = snd_card_register(card);
  579. if (err < 0) {
  580. pk_error("failed to register pc-midi sound card: error %d\n",
  581. err);
  582. goto fail_register;
  583. }
  584. dbg_hid("pcmidi_snd_initialise finished ok\n");
  585. return 0;
  586. fail_register:
  587. stop_sustain_timers(pm);
  588. device_remove_file(&pm->hdev->dev, sysfs_device_attr_octave);
  589. fail_attr_octave:
  590. device_remove_file(&pm->hdev->dev, sysfs_device_attr_sustain);
  591. fail_attr_sustain:
  592. device_remove_file(&pm->hdev->dev, sysfs_device_attr_channel);
  593. fail:
  594. if (pm->card) {
  595. snd_card_free(pm->card);
  596. pm->card = NULL;
  597. }
  598. return err;
  599. }
  600. static int pcmidi_snd_terminate(struct pcmidi_snd *pm)
  601. {
  602. if (pm->card) {
  603. stop_sustain_timers(pm);
  604. device_remove_file(&pm->hdev->dev, sysfs_device_attr_channel);
  605. device_remove_file(&pm->hdev->dev, sysfs_device_attr_sustain);
  606. device_remove_file(&pm->hdev->dev, sysfs_device_attr_octave);
  607. snd_card_disconnect(pm->card);
  608. snd_card_free_when_closed(pm->card);
  609. }
  610. return 0;
  611. }
  612. /*
  613. * PC-MIDI report descriptor for report id is wrong.
  614. */
  615. static const __u8 *pk_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  616. unsigned int *rsize)
  617. {
  618. if (*rsize == 178 &&
  619. rdesc[111] == 0x06 && rdesc[112] == 0x00 &&
  620. rdesc[113] == 0xff) {
  621. hid_info(hdev,
  622. "fixing up pc-midi keyboard report descriptor\n");
  623. rdesc[144] = 0x18; /* report 4: was 0x10 report count */
  624. }
  625. return rdesc;
  626. }
  627. static int pk_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  628. struct hid_field *field, struct hid_usage *usage,
  629. unsigned long **bit, int *max)
  630. {
  631. struct pcmidi_snd *pm = hid_get_drvdata(hdev);
  632. if (HID_UP_MSVENDOR == (usage->hid & HID_USAGE_PAGE) &&
  633. 1 == pm->ifnum) {
  634. pcmidi_setup_extra_keys(pm, hi->input);
  635. return 0;
  636. }
  637. return 0;
  638. }
  639. static int pk_raw_event(struct hid_device *hdev, struct hid_report *report,
  640. u8 *data, int size)
  641. {
  642. struct pcmidi_snd *pm = hid_get_drvdata(hdev);
  643. int ret = 0;
  644. if (1 == pm->ifnum) {
  645. if (report->id == data[0])
  646. switch (report->id) {
  647. case 0x01: /* midi keys (qwerty)*/
  648. case 0x03: /* midi keyboard (musical)*/
  649. case 0x04: /* extra/midi keys (qwerty)*/
  650. ret = pcmidi_handle_report(pm,
  651. report->id, data, size);
  652. break;
  653. }
  654. }
  655. return ret;
  656. }
  657. static int pk_probe(struct hid_device *hdev, const struct hid_device_id *id)
  658. {
  659. int ret;
  660. struct usb_interface *intf;
  661. unsigned short ifnum;
  662. unsigned long quirks = id->driver_data;
  663. struct pcmidi_snd *pm;
  664. if (!hid_is_usb(hdev))
  665. return -EINVAL;
  666. intf = to_usb_interface(hdev->dev.parent);
  667. ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  668. pm = kzalloc(sizeof(*pm), GFP_KERNEL);
  669. if (pm == NULL) {
  670. hid_err(hdev, "can't alloc descriptor\n");
  671. return -ENOMEM;
  672. }
  673. pm->hdev = hdev;
  674. pm->ifnum = ifnum;
  675. hid_set_drvdata(hdev, pm);
  676. ret = hid_parse(hdev);
  677. if (ret) {
  678. hid_err(hdev, "hid parse failed\n");
  679. goto err_free;
  680. }
  681. if (quirks & PK_QUIRK_NOGET) { /* hid_parse cleared all the quirks */
  682. hdev->quirks |= HID_QUIRK_NOGET;
  683. }
  684. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  685. if (ret) {
  686. hid_err(hdev, "hw start failed\n");
  687. goto err_free;
  688. }
  689. ret = pcmidi_snd_initialise(pm);
  690. if (ret < 0)
  691. goto err_stop;
  692. return 0;
  693. err_stop:
  694. hid_hw_stop(hdev);
  695. err_free:
  696. kfree(pm);
  697. return ret;
  698. }
  699. static void pk_remove(struct hid_device *hdev)
  700. {
  701. struct pcmidi_snd *pm = hid_get_drvdata(hdev);
  702. pcmidi_snd_terminate(pm);
  703. hid_hw_stop(hdev);
  704. kfree(pm);
  705. }
  706. static const struct hid_device_id pk_devices[] = {
  707. {HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS,
  708. USB_DEVICE_ID_PRODIKEYS_PCMIDI),
  709. .driver_data = PK_QUIRK_NOGET},
  710. { }
  711. };
  712. MODULE_DEVICE_TABLE(hid, pk_devices);
  713. static struct hid_driver pk_driver = {
  714. .name = "prodikeys",
  715. .id_table = pk_devices,
  716. .report_fixup = pk_report_fixup,
  717. .input_mapping = pk_input_mapping,
  718. .raw_event = pk_raw_event,
  719. .probe = pk_probe,
  720. .remove = pk_remove,
  721. };
  722. module_hid_driver(pk_driver);
  723. MODULE_DESCRIPTION("HID driver for the Prodikeys PC-MIDI Keyboard");
  724. MODULE_LICENSE("GPL");