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