Ticket #4053: usb_midi.c

File usb_midi.c, 18.3 KB (added by Pete, 15 years ago)
Line 
1/*
2 * midi usb driver
3 * usb_midi.c
4 *
5 * Copyright 2009 Haiku Inc. All rights reserved.
6 * Distributed under tthe terms of the MIT Licence.
7 *
8 * Authors:
9 * Jérôme Duval
10 * Pete Goodeve, pete.goodeve@computer.org
11 *
12 * Some portions of this code were originally derived from
13 * USB Joystick driver for BeOS R5
14 * Copyright 2000 (C) ITO, Takayuki
15 * All rights reserved
16 *
17 */
18
19
20/* #define DEBUG 1 */ /* Define this to enable DPRINTF_INFO statements */
21#include "usb_midi.h"
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include "usbdevs.h"
29#include "usbdevs_data.h"
30
31
32static int midi_device_number = 0;
33const char* midi_base_name = "midi/usb/";
34
35
36usbmidi_device_info*
37create_device(const usb_device* dev, const usb_interface_info* ii, uint16 ifno)
38{
39 usbmidi_device_info* my_dev = NULL;
40 int number;
41 area_id area;
42 sem_id sem;
43 char area_name[32];
44 const char* base_name;
45
46 assert(usb != NULL && dev != NULL);
47
48 number = midi_device_number++;
49 base_name = midi_base_name;
50
51 my_dev = malloc(sizeof(usbmidi_device_info));
52 if (my_dev == NULL)
53 return NULL;
54
55 my_dev->sem_cb = sem = create_sem(0, DRIVER_NAME "_cb");
56 if (sem < 0) {
57 DPRINTF_ERR((MY_ID "create_sem() failed %d\n", (int)sem));
58 free(my_dev);
59 return NULL;
60 }
61
62 my_dev->sem_lock = sem = create_sem(1, DRIVER_NAME "_lock");
63 if (sem < 0) {
64 DPRINTF_ERR((MY_ID "create_sem() failed %d\n", (int)sem));
65 delete_sem(my_dev->sem_cb);
66 free(my_dev);
67 return NULL;
68 }
69
70 sprintf(area_name, DRIVER_NAME "_buffer%d", number);
71 my_dev->buffer_area = area = create_area(area_name,
72 (void**)&my_dev->buffer, B_ANY_KERNEL_ADDRESS,
73 B_PAGE_SIZE, B_CONTIGUOUS, B_READ_AREA | B_WRITE_AREA);
74 if (area < 0) {
75 DPRINTF_ERR((MY_ID "create_area() failed %d\n", (int)area));
76 delete_sem(my_dev->sem_cb);
77 delete_sem(my_dev->sem_lock);
78 free(my_dev);
79 return NULL;
80 }
81 /* use half of reserved area for each of in and out buffers: */
82 my_dev->out_buffer = (usb_midi_event_packet*)((uint8*)my_dev->buffer + B_PAGE_SIZE/2);
83 my_dev->sem_send = sem = create_sem(1, DRIVER_NAME "_send");
84 if (sem < 0) {
85 DPRINTF_ERR((MY_ID "create_sem() failed %d\n", (int)sem));
86 delete_sem(my_dev->sem_cb);
87 delete_sem(my_dev->sem_lock);
88 delete_area(area);
89 free(my_dev);
90 return NULL;
91 }
92 {
93 int32 bc;
94 get_sem_count(sem, &bc);
95 DPRINTF_INFO((MY_ID "Allocated %ld write buffers\n", bc));
96 }
97
98
99 sprintf(my_dev->name, "%s%d", base_name, number);
100 my_dev->dev = dev;
101 my_dev->ifno = ifno;
102 my_dev->open = 0;
103 my_dev->open_fds = NULL;
104 my_dev->active = true;
105 my_dev->flags = 0;
106 my_dev->rbuf = create_ring_buffer(1024);
107 my_dev->buffer_size = B_PAGE_SIZE/2;
108 DPRINTF_INFO((MY_ID "Created device %p\n", my_dev);)
109
110 return my_dev;
111}
112
113
114void
115remove_device(usbmidi_device_info* my_dev)
116{
117 assert(my_dev != NULL);
118 if (my_dev->rbuf != NULL) {
119 delete_ring_buffer(my_dev->rbuf);
120 my_dev->rbuf = NULL;
121 }
122 DPRINTF_INFO((MY_ID "remove_device %p\n", my_dev);)
123
124 delete_area(my_dev->buffer_area);
125 delete_sem(my_dev->sem_cb);
126 delete_sem(my_dev->sem_lock);
127 delete_sem(my_dev->sem_send);
128 free(my_dev);
129}
130
131
132/* driver cookie (per open) */
133
134typedef struct driver_cookie
135{
136 struct driver_cookie *next;
137 usbmidi_device_info *my_dev;
138} driver_cookie;
139
140
141/* NB global variables are valid only while driver is loaded */
142
143_EXPORT int32 api_version = B_CUR_DRIVER_API_VERSION;
144
145const char* usb_midi_driver_name = "usb_midi";
146
147const int CINbytes[] = { /* See USB-MIDI Spec */
148 0, /* 0x0 -- undefined Misc -- Reserved */
149 0, /* 0x1 -- undefined Cable -- Reserved */
150 2, /* 0x2 -- 2-byte system common */
151 3, /* 0x3 -- 3-byte system common */
152 3, /* 0x4 -- SysEx start/continue */
153 1, /* 0x5 -- SysEx single-byte-end, or 1-byte Common */
154 2, /* 0x6 -- SysEx two-byte-end */
155 3, /* 0x7 -- SysEx three-byte-end */
156 3, /* 0x8 -- Note Off */
157 3, /* 0x9 -- Note On */
158 3, /* 0xA -- Poly KeyPress*/
159 3, /* 0xB -- Control Change */
160 2, /* 0xC -- Program Change */
161 2, /* 0xD -- Channel Pressure */
162 3, /* 0xE -- Pitch Bend */
163 1, /* 0xF -- Single Byte */
164};
165
166usb_module_info* usb;
167
168
169static void
170interpret_midi_buffer(usbmidi_device_info* my_dev)
171{
172 usb_midi_event_packet* packet = my_dev->buffer;
173 size_t bytes_left = my_dev->actual_length;
174 while (bytes_left) { /* buffer may have several packets */
175 int pktlen = CINbytes[packet->cin];
176 DPRINTF_INFO((MY_ID "received packet %x:%d %x %x %x\n", packet->cin, packet->cn,
177 packet->midi[0], packet->midi[1], packet->midi[2]));
178 ring_buffer_write(my_dev->rbuf, packet->midi, pktlen);
179 release_sem_etc(my_dev->sem_cb, pktlen, B_DO_NOT_RESCHEDULE);
180 packet++;
181 bytes_left -= sizeof(usb_midi_event_packet);
182 }
183}
184
185
186/*
187 callback: got a report, issue next request
188*/
189
190static void
191midi_usb_read_callback(void* cookie, status_t status,
192 void* data, size_t actual_len)
193{
194 status_t st;
195 usbmidi_device_info* my_dev = cookie;
196
197 assert(cookie != NULL);
198 DPRINTF_INFO((MY_ID "midi_usb_read_callback() -- packet length %ld\n", actual_len));
199
200 acquire_sem(my_dev->sem_lock);
201 my_dev->actual_length = actual_len;
202 my_dev->bus_status = status; /* B_USB_STATUS_* */
203 if (status != B_OK) {
204 /* request failed */
205 release_sem(my_dev->sem_lock);
206 DPRINTF_ERR((MY_ID "bus status %d\n", (int)status));
207 if (status == B_CANCELED || !my_dev->active) {
208 /* cancelled: device is unplugged */
209 delete_sem(my_dev->sem_cb); /* done here to ensure read is freed */
210 return;
211 }
212 } else {
213 /* got a report */
214#if 0
215 uint32 i;
216 char linbuf[256];
217 uint8* buffer = my_dev->buffer;
218
219 for (i = 0; i < 4; i++)
220 sprintf=&linbuf[i*3], "%02X ", buffer[i]);
221 DPRINTF_INFO((MY_ID "report: %s\n", linbuf));
222#endif
223 my_dev->timestamp = system_time();
224
225 interpret_midi_buffer(my_dev);
226 release_sem(my_dev->sem_lock);
227 }
228
229 /* issue next request */
230
231 st = usb->queue_bulk(my_dev->ept_in->handle, my_dev->buffer,
232 my_dev->buffer_size, (usb_callback_func)midi_usb_read_callback, my_dev);
233 if (st != B_OK) {
234 /* XXX probably endpoint stall */
235 DPRINTF_ERR((MY_ID "queue_bulk() error %d\n", (int)st));
236 }
237}
238
239
240static void
241midi_usb_write_callback(void* cookie, status_t status,
242 void* data, size_t actual_len)
243{
244 usbmidi_device_info* my_dev = cookie;
245#ifdef DEBUG
246 usb_midi_event_packet* pkt = data;
247#endif
248
249 assert(cookie != NULL);
250 DPRINTF_INFO((MY_ID "midi_usb_write_callback() status %ld length %ld pkt %p cin %x\n",
251 status, actual_len, pkt, pkt->cin));
252 release_sem(my_dev->sem_send); /* done with buffer */
253}
254
255
256/*
257 USB specific device hooks
258*/
259
260static status_t
261usb_midi_added(const usb_device* dev, void** cookie)
262{
263 usbmidi_device_info* my_dev;
264 const usb_device_descriptor* dev_desc;
265 const usb_configuration_info* conf;
266 const usb_interface_info* intf;
267 status_t st;
268 uint16 ifno, i;
269 int alt;
270
271 assert(dev != NULL && cookie != NULL);
272 DPRINTF_INFO((MY_ID "usb_midi_added(%p, %p)\n", dev, cookie));
273
274 dev_desc = usb->get_device_descriptor(dev);
275
276 DPRINTF_INFO((MY_ID "vendor ID 0x%04X, product ID 0x%04X\n",
277 dev_desc->vendor_id, dev_desc->product_id));
278
279 /* check interface class */
280
281 if ((conf = usb->get_nth_configuration(dev, DEFAULT_CONFIGURATION)) == NULL) {
282 DPRINTF_ERR((MY_ID "cannot get default configuration\n"));
283 return B_ERROR;
284 }
285
286 for (ifno = 0; ifno < conf->interface_count; ifno++) {
287 /* This is C; I can use "class" :-> */
288 int class, subclass, protocol;
289
290 for (alt = 0; alt < conf->interface[ifno].alt_count; alt++) {
291 intf = &conf->interface[ifno].alt[alt];
292 class = intf->descr->interface_class;
293 subclass = intf->descr->interface_subclass;
294 protocol = intf->descr->interface_protocol;
295 DPRINTF_INFO((MY_ID "interface %d, alt : %d: class %d, subclass %d, protocol %d\n",
296 ifno, alt, class, subclass, protocol));
297
298 if (class == USB_AUDIO_DEVICE_CLASS
299 && subclass == USB_AUDIO_INTERFACE_MIDISTREAMING_SUBCLASS)
300 goto got_one;
301 }
302 }
303
304 DPRINTF_INFO((MY_ID "Midi interface not found\n"));
305 return B_ERROR;
306
307got_one:
308
309 for (i=0; i < sizeof(usb_knowndevs)/sizeof(struct usb_knowndev); i++) {
310 if (usb_knowndevs[i].vendor == dev_desc->vendor_id
311 && usb_knowndevs[i].product == dev_desc->product_id) {
312 DPRINTF_INFO((MY_ID "vendor %s, product %s\n",
313 usb_knowndevs[i].vendorname, usb_knowndevs[i].productname));
314 }
315 }
316
317
318 /* configuration */
319 if ((st = usb->set_configuration(dev, conf)) != B_OK) {
320 DPRINTF_ERR((MY_ID "set_configuration() failed %d\n", (int)st));
321 return B_ERROR;
322 }
323
324 if ((my_dev = create_device(dev, intf, ifno)) == NULL) {
325 return B_ERROR;
326 }
327
328 DPRINTF_INFO((MY_ID "my_dev = %p endpoint count = %ld\n",
329 my_dev, intf->endpoint_count));
330 DPRINTF_INFO((MY_ID " input endpoint = %p\n",
331 &intf->endpoint[0]));
332 DPRINTF_INFO((MY_ID " output endpoint = %p\n",
333 &intf->endpoint[1]));
334 usb->clear_feature(intf->endpoint[0].handle, USB_FEATURE_ENDPOINT_HALT);
335 /* This may need more thought... */
336 my_dev->ept_in = &intf->endpoint[0]; /* bulk IN */
337 my_dev->ept_out = &intf->endpoint[1]; /* OUT */
338
339 my_dev->timestamp = system_time();
340
341 /* issue bulk transfer */
342 DPRINTF_INFO((MY_ID "queueing bulk xfer ep 0\n"));
343 st = usb->queue_bulk(my_dev->ept_in->handle, my_dev->buffer,
344 my_dev->buffer_size, (usb_callback_func)midi_usb_read_callback, my_dev);
345 if (st != B_OK) {
346 DPRINTF_ERR((MY_ID "queue_bulk() error %d\n", (int)st));
347 return B_ERROR;
348 }
349
350 /* create a port */
351 add_device_info(my_dev);
352
353 *cookie = my_dev;
354 DPRINTF_INFO((MY_ID "added %s\n", my_dev->name));
355
356 return B_OK;
357}
358
359
360static status_t
361usb_midi_removed(void* cookie)
362{
363 usbmidi_device_info* my_dev = cookie;
364
365 assert(cookie != NULL);
366
367 DPRINTF_INFO((MY_ID "usb_midi_removed(%s)\n", my_dev->name));
368 my_dev->active = false;
369 usb->cancel_queued_transfers(my_dev->ept_in->handle);
370 usb->cancel_queued_transfers(my_dev->ept_out->handle);
371 delete_sem(my_dev->sem_cb); /* done here to ensure read is freed */
372 if (my_dev->open == 0) {
373 DPRINTF_INFO((MY_ID "removing info & device: %s\n", my_dev->name));
374 remove_device_info(my_dev);
375 remove_device(my_dev);
376 } else {
377 DPRINTF_INFO((MY_ID "%s still open\n", my_dev->name));
378 }
379 return B_OK;
380}
381
382
383static usb_notify_hooks my_notify_hooks =
384{
385 usb_midi_added, usb_midi_removed
386};
387
388#define SUPPORTED_DEVICES 1
389usb_support_descriptor my_supported_devices[SUPPORTED_DEVICES] =
390{
391 { USB_AUDIO_DEVICE_CLASS, USB_AUDIO_INTERFACE_MIDISTREAMING_SUBCLASS, 0, 0, 0 },
392};
393
394
395/*
396 usb_midi_open - handle open() calls
397 */
398
399static status_t
400usb_midi_open(const char* name, uint32 flags,
401 driver_cookie** out_cookie)
402{
403 driver_cookie* cookie;
404 usbmidi_device_info* my_dev;
405
406 assert(name != NULL);
407 assert(out_cookie != NULL);
408 DPRINTF_INFO((MY_ID "open(%s)\n", name));
409
410 if ((my_dev = search_device_info(name)) == NULL)
411 return B_ENTRY_NOT_FOUND;
412 if ((cookie = malloc(sizeof(driver_cookie))) == NULL)
413 return B_NO_MEMORY;
414
415 acquire_sem(my_dev->sem_lock);
416 cookie->my_dev = my_dev;
417 cookie->next = my_dev->open_fds;
418 my_dev->open_fds = cookie;
419 my_dev->open++;
420 release_sem(my_dev->sem_lock);
421
422 *out_cookie = cookie;
423 DPRINTF_INFO((MY_ID "device %s open (%d)\n", name, my_dev->open));
424 return B_OK;
425}
426
427
428/*
429 usb_midi_read - handle read() calls
430 */
431
432static status_t
433usb_midi_read(driver_cookie* cookie, off_t position,
434 void* buf, size_t* num_bytes)
435{
436 status_t err = B_ERROR;
437 usbmidi_device_info* my_dev;
438
439 assert(cookie != NULL);
440 my_dev = cookie->my_dev;
441 assert(my_dev != NULL);
442
443 if (!my_dev->active)
444 return B_ERROR; /* already unplugged */
445
446 DPRINTF_INFO((MY_ID "MIDI read (%ld byte buffer at %ld cookie %p)\n",
447 *num_bytes, (int32)position, cookie));
448 while (my_dev->active) {
449 DPRINTF_INFO((MY_ID "waiting on acquire_sem_etc\n");)
450 err = acquire_sem_etc(my_dev->sem_cb, 1,
451 B_RELATIVE_TIMEOUT, 1000000);
452 if (err == B_TIMED_OUT) {
453 DPRINTF_INFO((MY_ID "acquire_sem_etc timed out\n");)
454 continue; /* see if we're still active */
455 }
456 if (err != B_OK) {
457 *num_bytes = 0;
458 DPRINTF_INFO((MY_ID "acquire_sem_etc aborted\n");)
459 break;
460 }
461 DPRINTF_INFO((MY_ID "reading from ringbuffer\n");)
462 acquire_sem(my_dev->sem_lock);
463 ring_buffer_user_read(my_dev->rbuf, buf, 1);
464 release_sem(my_dev->sem_lock);
465 *num_bytes = 1;
466 DPRINTF_INFO((MY_ID "read byte %x -- cookie %p)\n", *(uint8*)buf, cookie));
467 return B_OK;
468 }
469 DPRINTF_INFO((MY_ID "Device no longer active\n");)
470 return B_CANCELED;
471}
472
473
474/*
475 usb_midi_write - handle write() calls
476 */
477
478const uint8 CINcode[] = { /* see USB-MIDI Spec */
479 0x4, /* 0x0 - sysex start */
480 0, /* 0x1 -- undefined */
481 0x3, /* 0x2 -- song pos */
482 0x2, /* 0x3 -- song select */
483 0, /* 0x4 -- undefined */
484 0, /* 0x5 -- undefined */
485 0x2, /* 0x6 -- tune request */
486 0x5, /* 0x7 -- sysex end */
487 0x5, /* 0x8 -- clock */
488 0, /* 0x9 -- undefined */
489 0x5, /* 0xA -- start */
490 0x5, /* 0xB -- continue */
491 0x5, /* 0xC -- stop */
492 0, /* 0xD -- undefined */
493 0x5, /* 0xE -- active sensing */
494 0x5, /* 0x0 -- system reset */
495};
496
497
498static status_t
499usb_midi_write(driver_cookie* cookie, off_t position,
500 const void* buf, size_t* num_bytes)
501{
502 usbmidi_device_info* my_dev;
503 uint8* midiseq = (uint8*)buf;
504 uint8 midicode = midiseq[0]; /* preserved for reference */
505 status_t st;
506 size_t bytes_left = *num_bytes;
507 size_t buff_lim;
508 uint8 cin = ((midicode & 0xF0) == 0xF0) ? CINcode[midicode & 0x0F]
509 : (midicode >> 4);
510
511 assert(cookie != NULL);
512 my_dev = cookie->my_dev;
513 assert(my_dev != NULL);
514
515 if (!my_dev->active)
516 return B_ERROR; /* already unplugged */
517
518 buff_lim = my_dev->buffer_size * 3 / 4; /* max MIDI bytes buffer space */
519
520 DPRINTF_INFO((MY_ID "MIDI write (%ld bytes at %Ld)\n", *num_bytes, position));
521 if (*num_bytes > 3 && midicode != 0xF0) {
522 DPRINTF_ERR((MY_ID "Non-SysEx packet of %ld bytes -- too big to handle\n",
523 *num_bytes));
524 return B_ERROR;
525 }
526
527 while (bytes_left) {
528 size_t xfer_bytes = (bytes_left < buff_lim)? bytes_left : buff_lim;
529 usb_midi_event_packet* pkt = my_dev->out_buffer;
530 int packet_count = 0;
531
532 st = acquire_sem_etc(my_dev->sem_send, 1, B_RELATIVE_TIMEOUT, 2000000LL);
533 if (st != B_OK)
534 return st;
535
536 while (xfer_bytes) {
537 uint8 pkt_bytes = CINbytes[cin];
538 memset(pkt, 0, sizeof(usb_midi_event_packet));
539 pkt->cin = cin;
540 DPRINTF_INFO((MY_ID "using packet data (code %x -- %d bytes) %x %x %x\n", pkt->cin,
541 CINbytes[pkt->cin], midiseq[0], midiseq[1], midiseq[2]));
542 memcpy(pkt->midi, midiseq, pkt_bytes);
543 DPRINTF_INFO((MY_ID "built packet %p %x:%d %x %x %x\n", pkt, pkt->cin, pkt->cn,
544 pkt->midi[0], pkt->midi[1], pkt->midi[2]));
545 xfer_bytes -= pkt_bytes;
546 bytes_left -= pkt_bytes;
547 midiseq += pkt_bytes;
548 packet_count++;
549 pkt++;
550 if (midicode == 0xF0 && bytes_left < 4) cin = 4 + bytes_left; /* see USB-MIDI Spec */
551 }
552 st = usb->queue_bulk(my_dev->ept_out->handle, my_dev->out_buffer,
553 sizeof(usb_midi_event_packet) * packet_count,
554 (usb_callback_func)midi_usb_write_callback, my_dev);
555 if (st != B_OK) {
556 DPRINTF_ERR((MY_ID "midi write queue_bulk() error %d\n", (int)st));
557 return B_ERROR;
558 }
559 }
560 return B_OK;
561}
562
563
564/*
565 usb_midi_control - handle ioctl calls
566 */
567
568static status_t
569usb_midi_control(void* cookie, uint32 iop,
570 void* data, size_t len)
571{
572 return B_ERROR;
573}
574
575
576/*
577 usb_midi_close - handle close() calls
578 */
579
580static status_t
581usb_midi_close(driver_cookie* cookie)
582{
583 usbmidi_device_info* my_dev;
584
585 assert(cookie != NULL && cookie->my_dev != NULL);
586 my_dev = cookie->my_dev;
587 DPRINTF_INFO((MY_ID "close(%s)\n", my_dev->name));
588
589 /* detach the cookie from list */
590
591 acquire_sem(my_dev->sem_lock);
592 if (my_dev->open_fds == cookie)
593 my_dev->open_fds = cookie->next;
594 else {
595 driver_cookie* p;
596 for (p = my_dev->open_fds; p != NULL; p = p->next) {
597 if (p->next == cookie) {
598 p->next = cookie->next;
599 break;
600 }
601 }
602 }
603 --my_dev->open;
604 release_sem(my_dev->sem_lock);
605 if (my_dev->open == 0) {
606 DPRINTF_INFO((MY_ID "close -- removing device info %s\n", my_dev->name));
607 remove_device_info(my_dev);
608 }
609 DPRINTF_INFO((MY_ID "close complete\n");)
610
611 return B_OK;
612}
613
614
615/*
616 usb_midi_free - called after the last device is closed, and after
617 all i/o is complete.
618 */
619
620static status_t
621usb_midi_free(driver_cookie* cookie)
622{
623 usbmidi_device_info* my_dev;
624
625 assert(cookie != NULL && cookie->my_dev != NULL);
626 my_dev = cookie->my_dev;
627 DPRINTF_INFO((MY_ID "free(%s)\n", my_dev->name));
628
629 free(cookie);
630 if (my_dev->open > 0)
631 DPRINTF_INFO((MY_ID "%d opens left\n", my_dev->open));
632 else if (!my_dev->active) {
633 DPRINTF_INFO((MY_ID "free -- removing %s\n", my_dev->name));
634 remove_device(my_dev);
635 }
636
637 return B_OK;
638}
639
640
641/*
642 function pointers for the device hooks entry points
643 */
644
645static device_hooks usb_midi_hooks = {
646 (device_open_hook)usb_midi_open,
647 (device_close_hook)usb_midi_close,
648 (device_free_hook)usb_midi_free,
649 (device_control_hook)usb_midi_control,
650 (device_read_hook)usb_midi_read,
651 (device_write_hook)usb_midi_write,
652 NULL, NULL, NULL, NULL
653};
654
655
656/*
657 init_hardware - called once the first time the driver is loaded
658 */
659
660_EXPORT status_t
661init_hardware(void)
662{
663 DPRINTF_INFO((MY_ID "init_hardware() " __DATE__ " " __TIME__ "\n"));
664 return B_OK;
665}
666
667
668/*
669 init_driver - optional function - called every time the driver
670 is loaded.
671 */
672
673_EXPORT status_t
674init_driver(void)
675{
676
677 DPRINTF_INFO((MY_ID "init_driver() " __DATE__ " " __TIME__ "\n"));
678
679 if (get_module(B_USB_MODULE_NAME, (module_info**)&usb) != B_OK)
680 return B_ERROR;
681
682 if ((usbmidi_device_list_lock = create_sem(1, "dev_list_lock")) < 0) {
683 put_module(B_USB_MODULE_NAME);
684 return usbmidi_device_list_lock; /* error code */
685 }
686
687 usb->register_driver(usb_midi_driver_name, my_supported_devices,
688 SUPPORTED_DEVICES, NULL);
689 usb->install_notify(usb_midi_driver_name, &my_notify_hooks);
690 DPRINTF_INFO((MY_ID "init_driver() OK\n"));
691
692 return B_OK;
693}
694
695
696/*
697 uninit_driver - optional function - called every time the driver
698 is unloaded
699 */
700
701_EXPORT void
702uninit_driver(void)
703{
704 DPRINTF_INFO((MY_ID "uninit_driver()\n"));
705 usb->uninstall_notify(usb_midi_driver_name);
706
707 delete_sem(usbmidi_device_list_lock);
708 put_module(B_USB_MODULE_NAME);
709 free_device_names();
710 DPRINTF_INFO((MY_ID "uninit complete\n"));
711}
712
713
714/*
715 publish_devices
716 device names are generated dynamically
717*/
718
719_EXPORT const char**
720publish_devices(void)
721{
722 DPRINTF_INFO((MY_ID "publish_devices()\n"));
723
724 if (usbmidi_device_list_changed) {
725 free_device_names();
726 alloc_device_names();
727 if (usbmidi_device_names != NULL)
728 rebuild_device_names();
729 usbmidi_device_list_changed = false;
730 }
731 assert(usbmidi_device_names != NULL);
732 return (const char**)usbmidi_device_names;
733}
734
735
736/*
737 find_device - return ptr to device hooks structure for a
738 given device name
739 */
740
741_EXPORT device_hooks*
742find_device(const char* name)
743{
744 assert(name != NULL);
745 DPRINTF_INFO((MY_ID "find_device(%s)\n", name));
746 if (search_device_info(name) == NULL)
747 return NULL;
748 return &usb_midi_hooks;
749}
750