hid-prodikeys.c 21 KB

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