hid-prodikeys.c 20 KB

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