hid-prodikeys.c 20 KB

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