Applied patch by Pete Goodeve, which adds multi-ports support to usb_midi driver.

Fixed some coding style and a few sanity checks where it make sense.

This driver code is not in a good shape and needs a wide cleanup.
unfortunatly, I still don't have any device to test with, so I can't do that anytime soon.
Intead of letting his patch collecting dust since 3 months (my bad),
I think it's better the multi-port support gets at least more exposure than
just Pete's hardware.



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41874 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Philippe Houdoin 2011-06-03 10:30:03 +00:00
parent 5d6dcc0685
commit 1291377a6e
3 changed files with 529 additions and 382 deletions

View File

@ -2,7 +2,7 @@
* midi usb driver * midi usb driver
* devlist.c * devlist.c
* *
* Copyright 2006-2009 Haiku Inc. All rights reserved. * Copyright 2006-2011 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT Licence. * Distributed under the terms of the MIT Licence.
* *
* Authors: * Authors:
@ -23,107 +23,127 @@
#include <string.h> #include <string.h>
sem_id usbmidi_device_list_lock = -1; sem_id usbmidi_port_list_lock = -1;
bool usbmidi_device_list_changed = true; /* added or removed */ bool usbmidi_port_list_changed = true; /* added or removed */
static usbmidi_device_info* usbmidi_device_list = NULL; static usbmidi_port_info* usbmidi_port_list = NULL;
static int usbmidi_device_count = 0; static int usbmidi_port_count = 0;
void void
add_device_info(usbmidi_device_info* my_dev) add_port_info(usbmidi_port_info* port)
{ {
assert(my_dev != NULL); assert(port != NULL);
acquire_sem(usbmidi_device_list_lock); acquire_sem(usbmidi_port_list_lock);
my_dev->next = usbmidi_device_list; port->next = usbmidi_port_list;
usbmidi_device_list = my_dev; usbmidi_port_list = port;
usbmidi_device_count++; usbmidi_port_count++;
usbmidi_device_list_changed = true; usbmidi_port_list_changed = true;
release_sem(usbmidi_device_list_lock); release_sem(usbmidi_port_list_lock);
} }
void void
remove_device_info(usbmidi_device_info* my_dev) remove_port_info(usbmidi_port_info* port)
{ {
assert(my_dev != NULL); assert(port != NULL);
acquire_sem(usbmidi_device_list_lock); acquire_sem(usbmidi_port_list_lock);
if (usbmidi_device_list == my_dev) { if (usbmidi_port_list == port) {
usbmidi_device_list = my_dev->next; usbmidi_port_list = port->next;
--usbmidi_device_count; --usbmidi_port_count;
usbmidi_device_list_changed = true; usbmidi_port_list_changed = true;
} else { } else {
usbmidi_device_info* d; usbmidi_port_info* d;
for (d = usbmidi_device_list; d != NULL; d = d->next) { for (d = usbmidi_port_list; d != NULL; d = d->next) {
if (d->next == my_dev) { if (d->next == port) {
d->next = my_dev->next; d->next = port->next;
--usbmidi_device_count; --usbmidi_port_count;
usbmidi_device_list_changed = true; usbmidi_port_list_changed = true;
break; break;
} }
} }
assert(d != NULL); assert(d != NULL);
} }
release_sem(usbmidi_device_list_lock); release_sem(usbmidi_port_list_lock);
} }
usbmidi_device_info* usbmidi_port_info*
search_device_info(const char* name) search_port_info(const char* name)
{ {
usbmidi_device_info* my_dev; usbmidi_port_info* port;
acquire_sem(usbmidi_device_list_lock); acquire_sem(usbmidi_port_list_lock);
for (my_dev = usbmidi_device_list; my_dev != NULL; my_dev = my_dev->next) { for (port = usbmidi_port_list; port != NULL; port = port->next) {
if (strcmp(my_dev->name, name) == 0) if (strcmp(port->name, name) == 0)
break; break;
} }
release_sem(usbmidi_device_list_lock); release_sem(usbmidi_port_list_lock);
return my_dev; return port;
} }
int
find_free_device_number(void)
{
usbmidi_port_info* port;
int number = 0;
acquire_sem(usbmidi_port_list_lock);
do {
for (port = usbmidi_port_list; port != NULL; port = port->next) {
if (port->device->devnum == number) {
number++;
break; /* try next higher */
}
}
} while (port);
release_sem(usbmidi_port_list_lock);
return number;
}
/* /*
device names device names
*/ */
/* dynamically generated */ /* dynamically generated */
char** usbmidi_device_names = NULL; char** usbmidi_port_names = NULL;
void void
alloc_device_names(void) alloc_port_names(void)
{ {
assert(usbmidi_device_names == NULL); assert(usbmidi_port_names == NULL);
usbmidi_device_names = malloc(sizeof(char*) * (usbmidi_device_count + 1)); usbmidi_port_names = malloc(sizeof(char*) * (usbmidi_port_count + 1));
} }
void void
free_device_names(void) free_port_names(void)
{ {
if (usbmidi_device_names != NULL) { if (usbmidi_port_names != NULL) {
int i; int i;
for (i = 0; usbmidi_device_names[i] != NULL; i++) for (i = 0; usbmidi_port_names[i] != NULL; i++)
free(usbmidi_device_names[i]); free(usbmidi_port_names[i]);
free(usbmidi_device_names); free(usbmidi_port_names);
usbmidi_device_names = NULL; usbmidi_port_names = NULL;
} }
} }
void void
rebuild_device_names(void) rebuild_port_names(void)
{ {
int i; int i;
usbmidi_device_info* my_dev; usbmidi_port_info* port;
assert(usbmidi_device_names != NULL); assert(usbmidi_port_names != NULL);
acquire_sem(usbmidi_device_list_lock); acquire_sem(usbmidi_port_list_lock);
for (i = 0, my_dev = usbmidi_device_list; my_dev != NULL; my_dev = my_dev->next) { for (i = 0, port = usbmidi_port_list; port != NULL; port = port->next) {
usbmidi_device_names[i++] = strdup(my_dev->name); usbmidi_port_names[i++] = strdup(port->name);
DPRINTF_INFO((MY_ID "publishing %s\n", my_dev->name)); DPRINTF_INFO((MY_ID "publishing %s\n", port->name));
} }
usbmidi_device_names[i] = NULL; usbmidi_port_names[i] = NULL;
release_sem(usbmidi_device_list_lock); release_sem(usbmidi_port_list_lock);
} }

View File

@ -2,7 +2,7 @@
* midi usb driver * midi usb driver
* usb_midi.c * usb_midi.c
* *
* Copyright 2006-2009 Haiku Inc. All rights reserved. * Copyright 2006-2011 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT Licence. * Distributed under the terms of the MIT Licence.
* *
* Authors: * Authors:
@ -17,7 +17,9 @@
*/ */
/* #define DEBUG 1 */ /* Define this to enable DPRINTF_INFO statements */ /* #define DEBUG 1 */ /* Define this to enable DPRINTF_DEBUG statements */
/* (Other categories of printout set in usb_midi.h) */
#include "usb_midi.h" #include "usb_midi.h"
#include <stdio.h> #include <stdio.h>
@ -25,104 +27,139 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "usbdevs.h"
#include "usbdevs_data.h"
static int midi_device_number = 0;
const char* midi_base_name = "midi/usb/"; const char* midi_base_name = "midi/usb/";
usbmidi_device_info*
create_device(const usb_device* dev, const usb_interface_info* ii, uint16 ifno) usbmidi_port_info*
create_usbmidi_port(usbmidi_device_info* devinfo,
int cable, bool has_in, bool has_out)
{ {
usbmidi_device_info* my_dev = NULL; usbmidi_port_info* port = NULL;
assert(usb != NULL && devinfo != NULL);
port = malloc(sizeof(usbmidi_port_info));
if (port == NULL)
return NULL;
sprintf(port->name, "%s-%d", devinfo->name, cable);
port->device = devinfo;
port->cable = cable;
port->next = NULL;
port->open = 0;
port->open_fd = NULL;
port->has_in = has_in;
port->has_out = has_out;
port->rbuf = create_ring_buffer(1024);
devinfo->ports[cable] = port;
DPRINTF_INFO((MY_ID "Created port %p cable %d: %s\n",
port, cable, port->name));
return port;
}
void
remove_port(usbmidi_port_info* port)
{
assert(port != NULL);
if (port->rbuf != NULL) {
delete_ring_buffer(port->rbuf);
port->rbuf = NULL;
}
DPRINTF_INFO((MY_ID "remove_port %p done\n", port));
free(port);
}
usbmidi_device_info*
create_device(const usb_device* dev, uint16 ifno)
{
usbmidi_device_info* midiDevice = NULL;
int number; int number;
area_id area; area_id area;
sem_id sem; sem_id sem;
char area_name[32]; char area_name[32];
const char* base_name;
assert(usb != NULL && dev != NULL); assert(usb != NULL && dev != NULL);
number = midi_device_number++; number = find_free_device_number();
base_name = midi_base_name;
my_dev = malloc(sizeof(usbmidi_device_info)); midiDevice = malloc(sizeof(usbmidi_device_info));
if (my_dev == NULL) if (midiDevice == NULL)
return NULL; return NULL;
my_dev->sem_lock = sem = create_sem(1, DRIVER_NAME "_lock"); midiDevice->sem_lock = sem = create_sem(1, DRIVER_NAME "_lock");
if (sem < 0) { if (sem < 0) {
DPRINTF_ERR((MY_ID "create_sem() failed %d\n", (int)sem)); DPRINTF_ERR((MY_ID "create_sem() failed %d\n", (int)sem));
free(my_dev); free(midiDevice);
return NULL; return NULL;
} }
sprintf(area_name, DRIVER_NAME "_buffer%d", number); sprintf(area_name, DRIVER_NAME "_buffer%d", number);
my_dev->buffer_area = area = create_area(area_name, midiDevice->buffer_area = area = create_area(area_name,
(void**)&my_dev->buffer, B_ANY_KERNEL_ADDRESS, (void**)&midiDevice->buffer, B_ANY_KERNEL_ADDRESS,
B_PAGE_SIZE, B_CONTIGUOUS, B_READ_AREA | B_WRITE_AREA); B_PAGE_SIZE, B_CONTIGUOUS, B_READ_AREA | B_WRITE_AREA);
if (area < 0) { if (area < 0) {
DPRINTF_ERR((MY_ID "create_area() failed %d\n", (int)area)); DPRINTF_ERR((MY_ID "create_area() failed %d\n", (int)area));
delete_sem(my_dev->sem_lock); delete_sem(midiDevice->sem_lock);
free(my_dev); free(midiDevice);
return NULL; return NULL;
} }
/* use half of reserved area for each of in and out buffers: */ /* use half of reserved area for each of in and out buffers: */
my_dev->out_buffer = (usb_midi_event_packet*)((uint8*)my_dev->buffer + B_PAGE_SIZE/2); midiDevice->out_buffer
my_dev->sem_send = sem = create_sem(1, DRIVER_NAME "_send"); = (usb_midi_event_packet*)((uint8*)midiDevice->buffer + B_PAGE_SIZE/2);
midiDevice->sem_send = sem = create_sem(1, DRIVER_NAME "_send");
if (sem < 0) { if (sem < 0) {
DPRINTF_ERR((MY_ID "create_sem() failed %d\n", (int)sem)); DPRINTF_ERR((MY_ID "create_sem() failed %d\n", (int)sem));
delete_sem(my_dev->sem_lock); delete_sem(midiDevice->sem_lock);
delete_area(area); delete_area(area);
free(my_dev); free(midiDevice);
return NULL; return NULL;
} }
{ {
int32 bc; int32 bc;
get_sem_count(sem, &bc); get_sem_count(sem, &bc);
DPRINTF_INFO((MY_ID "Allocated %ld write buffers\n", bc)); DPRINTF_DEBUG((MY_ID "Allocated %ld write buffers\n", bc));
} }
sprintf(my_dev->name, "%s%d", base_name, number); sprintf(midiDevice->name, "%s%d", midi_base_name, number);
my_dev->dev = dev; midiDevice->dev = dev;
my_dev->ifno = ifno; midiDevice->devnum = number;
my_dev->open = 0; midiDevice->ifno = ifno;
my_dev->open_fd = NULL; midiDevice->active = true;
my_dev->active = true; midiDevice->flags = 0;
my_dev->flags = 0; memset(midiDevice->ports, 0, sizeof(midiDevice->ports));
my_dev->rbuf = create_ring_buffer(1024); midiDevice->buffer_size = B_PAGE_SIZE/2;
my_dev->buffer_size = B_PAGE_SIZE/2; DPRINTF_INFO((MY_ID "Created device %p\n", midiDevice));
DPRINTF_INFO((MY_ID "Created device %p\n", my_dev);)
return my_dev; return midiDevice;
} }
void void
remove_device(usbmidi_device_info* my_dev) remove_device(usbmidi_device_info* midiDevice)
{ {
assert(my_dev != NULL); assert(midiDevice != NULL);
if (my_dev->rbuf != NULL) { DPRINTF_INFO((MY_ID "remove_device %p\n", midiDevice));
delete_ring_buffer(my_dev->rbuf);
my_dev->rbuf = NULL;
}
DPRINTF_INFO((MY_ID "remove_device %p\n", my_dev);)
delete_area(my_dev->buffer_area); delete_area(midiDevice->buffer_area);
delete_sem(my_dev->sem_lock); delete_sem(midiDevice->sem_lock);
delete_sem(my_dev->sem_send); delete_sem(midiDevice->sem_send);
free(my_dev); free(midiDevice);
} }
/* driver cookie (per open -- but only one open allowed!) */ /* driver cookie (per open -- but only one open per port allowed!) */
typedef struct driver_cookie typedef struct driver_cookie
{ {
struct driver_cookie *next; struct driver_cookie* next;
usbmidi_device_info *my_dev; usbmidi_device_info* device; /* a bit redundant, but convenient */
usbmidi_port_info* port;
sem_id sem_cb; sem_id sem_cb;
} driver_cookie; } driver_cookie;
@ -156,16 +193,26 @@ usb_module_info* usb;
static void static void
interpret_midi_buffer(usbmidi_device_info* my_dev) interpret_midi_buffer(usbmidi_device_info* midiDevice)
{ {
usb_midi_event_packet* packet = my_dev->buffer; usb_midi_event_packet* packet = midiDevice->buffer;
size_t bytes_left = my_dev->actual_length; size_t bytes_left = midiDevice->actual_length;
while (bytes_left) { /* buffer may have several packets */ while (bytes_left) { /* buffer may have several packets */
int pktlen = CINbytes[packet->cin]; int pktlen = CINbytes[packet->cin];
DPRINTF_INFO((MY_ID "received packet %x:%d %x %x %x\n", packet->cin, packet->cn, DPRINTF_DEBUG((MY_ID "received packet %x:%d %x %x %x\n",
packet->cin, packet->cn,
packet->midi[0], packet->midi[1], packet->midi[2])); packet->midi[0], packet->midi[1], packet->midi[2]));
ring_buffer_write(my_dev->rbuf, packet->midi, pktlen);
release_sem_etc(my_dev->open_fd->sem_cb, pktlen, B_DO_NOT_RESCHEDULE); usbmidi_port_info* port = midiDevice->ports[packet->cn];
/* port matching 'cable' */
if (port == NULL) {
DPRINTF_ERR((MY_ID "no port matching cable number %d!\n",
packet->cn));
} else {
ring_buffer_write(port->rbuf, packet->midi, pktlen);
release_sem_etc(port->open_fd->sem_cb, pktlen, B_DO_NOT_RESCHEDULE);
}
packet++; packet++;
bytes_left -= sizeof(usb_midi_event_packet); bytes_left -= sizeof(usb_midi_event_packet);
} }
@ -181,49 +228,51 @@ midi_usb_read_callback(void* cookie, status_t status,
void* data, size_t actual_len) void* data, size_t actual_len)
{ {
status_t st; status_t st;
usbmidi_device_info* my_dev = cookie; usbmidi_device_info* midiDevice = cookie;
assert(cookie != NULL); assert(cookie != NULL);
DPRINTF_INFO((MY_ID "midi_usb_read_callback() -- packet length %ld\n", actual_len)); DPRINTF_DEBUG((MY_ID "midi_usb_read_callback() -- packet length %ld\n",
actual_len));
acquire_sem(my_dev->sem_lock); acquire_sem(midiDevice->sem_lock);
my_dev->actual_length = actual_len; midiDevice->actual_length = actual_len;
my_dev->bus_status = status; /* B_USB_STATUS_* */ midiDevice->bus_status = status; /* B_USB_STATUS_* */
if (status != B_OK) { if (status != B_OK) {
/* request failed */ /* request failed */
DPRINTF_INFO((MY_ID "bus status %d\n", (int)status)); /* previously DPRINTF_ERR */ DPRINTF_DEBUG((MY_ID "bus status %d\n", (int)status));
if (status == B_CANCELED || !my_dev->active) { if (status == B_CANCELED || !midiDevice->active) {
int cable;
/* cancelled: device is unplugged */ /* cancelled: device is unplugged */
DPRINTF_INFO((MY_ID "midi_usb_read_callback: cancelled (status=%lx active=%d -- deleting sem_cb\n", DPRINTF_DEBUG((MY_ID "midi_usb_read_callback: cancelled"
status, my_dev->active)); "(status=%lx active=%d -- deleting sem_cbs\n",
delete_sem(my_dev->open_fd->sem_cb); /* done here to ensure read is freed */ status, midiDevice->active));
release_sem(my_dev->sem_lock);
// Free any read() still blocked on semaphore
for (cable = 0; cable < 16; cable++) {
usbmidi_port_info* port = midiDevice->ports[cable];
if (port == NULL)
break;
if (port->open_fd != NULL)
delete_sem(port->open_fd->sem_cb);
}
release_sem(midiDevice->sem_lock);
return; return;
} }
release_sem(my_dev->sem_lock); release_sem(midiDevice->sem_lock);
} else { } else {
/* got a report */ /* got a report */
#if 0 midiDevice->timestamp = system_time(); /* not used... */
uint32 i;
char linbuf[256];
uint8* buffer = my_dev->buffer;
for (i = 0; i < 4; i++) interpret_midi_buffer(midiDevice);
sprintf=&linbuf[i*3], "%02X ", buffer[i]); release_sem(midiDevice->sem_lock);
DPRINTF_INFO((MY_ID "report: %s\n", linbuf));
#endif
my_dev->timestamp = system_time();
interpret_midi_buffer(my_dev);
release_sem(my_dev->sem_lock);
} }
/* issue next request */ /* issue next request */
st = usb->queue_bulk(midiDevice->ept_in->handle,
st = usb->queue_bulk(my_dev->ept_in->handle, my_dev->buffer, midiDevice->buffer, midiDevice->buffer_size,
my_dev->buffer_size, (usb_callback_func)midi_usb_read_callback, my_dev); (usb_callback_func)midi_usb_read_callback, midiDevice);
if (st != B_OK) { if (st != B_OK) {
/* XXX probably endpoint stall */ /* probably endpoint stall */
DPRINTF_ERR((MY_ID "queue_bulk() error %d\n", (int)st)); DPRINTF_ERR((MY_ID "queue_bulk() error %d\n", (int)st));
} }
} }
@ -233,15 +282,16 @@ static void
midi_usb_write_callback(void* cookie, status_t status, midi_usb_write_callback(void* cookie, status_t status,
void* data, size_t actual_len) void* data, size_t actual_len)
{ {
usbmidi_device_info* my_dev = cookie; usbmidi_device_info* midiDevice = cookie;
#ifdef DEBUG #ifdef DEBUG
usb_midi_event_packet* pkt = data; usb_midi_event_packet* pkt = data;
#endif #endif
assert(cookie != NULL); assert(cookie != NULL);
DPRINTF_INFO((MY_ID "midi_usb_write_callback() status %ld length %ld pkt %p cin %x\n", DPRINTF_DEBUG((MY_ID "midi_usb_write_callback()"
" status %ld length %ld pkt %p cin %x\n",
status, actual_len, pkt, pkt->cin)); status, actual_len, pkt, pkt->cin));
release_sem(my_dev->sem_send); /* done with buffer */ release_sem(midiDevice->sem_send); /* done with buffer */
} }
@ -252,7 +302,8 @@ midi_usb_write_callback(void* cookie, status_t status,
static status_t static status_t
usb_midi_added(const usb_device* dev, void** cookie) usb_midi_added(const usb_device* dev, void** cookie)
{ {
usbmidi_device_info* my_dev; usbmidi_device_info* midiDevice;
usbmidi_port_info* port;
const usb_device_descriptor* dev_desc; const usb_device_descriptor* dev_desc;
const usb_configuration_info* conf; const usb_configuration_info* conf;
const usb_interface_info* intf; const usb_interface_info* intf;
@ -260,6 +311,12 @@ usb_midi_added(const usb_device* dev, void** cookie)
uint16 ifno, i; uint16 ifno, i;
int alt; int alt;
/* This seems overcomplicated, but endpoints can be in either order...
and could possibly have different number of connectors! */
int in_cables = 0, out_cables = 0;
int cable_count[2] = {0, 0};
int iep = 0;
assert(dev != NULL && cookie != NULL); assert(dev != NULL && cookie != NULL);
DPRINTF_INFO((MY_ID "usb_midi_added(%p, %p)\n", dev, cookie)); DPRINTF_INFO((MY_ID "usb_midi_added(%p, %p)\n", dev, cookie));
@ -270,24 +327,27 @@ usb_midi_added(const usb_device* dev, void** cookie)
/* check interface class */ /* check interface class */
if ((conf = usb->get_nth_configuration(dev, DEFAULT_CONFIGURATION)) == NULL) { if ((conf = usb->get_nth_configuration(dev, DEFAULT_CONFIGURATION))
== NULL) {
DPRINTF_ERR((MY_ID "cannot get default configuration\n")); DPRINTF_ERR((MY_ID "cannot get default configuration\n"));
return B_ERROR; return B_ERROR;
} }
DPRINTF_INFO((MY_ID "Interface count = %ld\n", conf->interface_count));
for (ifno = 0; ifno < conf->interface_count; ifno++) { for (ifno = 0; ifno < conf->interface_count; ifno++) {
/* This is C; I can use "class" :-> */ int devclass, subclass, protocol;
int class, subclass, protocol;
for (alt = 0; alt < conf->interface[ifno].alt_count; alt++) { for (alt = 0; alt < conf->interface[ifno].alt_count; alt++) {
intf = &conf->interface[ifno].alt[alt]; intf = &conf->interface[ifno].alt[alt];
class = intf->descr->interface_class; devclass = intf->descr->interface_class;
subclass = intf->descr->interface_subclass; subclass = intf->descr->interface_subclass;
protocol = intf->descr->interface_protocol; protocol = intf->descr->interface_protocol;
DPRINTF_INFO((MY_ID "interface %d, alt : %d: class %d, subclass %d, protocol %d\n", DPRINTF_INFO((
ifno, alt, class, subclass, protocol)); MY_ID "interface %d, alt : %d: class %d,"
" subclass %d, protocol %d\n",
ifno, alt, devclass, subclass, protocol));
if (class == USB_AUDIO_DEVICE_CLASS if (devclass == USB_AUDIO_DEVICE_CLASS
&& subclass == USB_AUDIO_INTERFACE_MIDISTREAMING_SUBCLASS) && subclass == USB_AUDIO_INTERFACE_MIDISTREAMING_SUBCLASS)
goto got_one; goto got_one;
} }
@ -298,52 +358,75 @@ usb_midi_added(const usb_device* dev, void** cookie)
got_one: got_one:
for (i=0; i < sizeof(usb_knowndevs)/sizeof(struct usb_knowndev); i++) {
if (usb_knowndevs[i].vendor == dev_desc->vendor_id
&& usb_knowndevs[i].product == dev_desc->product_id) {
DPRINTF_INFO((MY_ID "vendor %s, product %s\n",
usb_knowndevs[i].vendorname, usb_knowndevs[i].productname));
}
}
/* configuration */
if ((st = usb->set_configuration(dev, conf)) != B_OK) { if ((st = usb->set_configuration(dev, conf)) != B_OK) {
DPRINTF_ERR((MY_ID "set_configuration() failed %d\n", (int)st)); DPRINTF_ERR((MY_ID "set_configuration() failed %d\n", (int)st));
return B_ERROR; return B_ERROR;
} }
if ((my_dev = create_device(dev, intf, ifno)) == NULL) { if ((midiDevice = create_device(dev, ifno)) == NULL) {
return B_ERROR; return B_ERROR;
} }
DPRINTF_INFO((MY_ID "my_dev = %p endpoint count = %ld\n", /* get the actual number of ports in and out */
my_dev, intf->endpoint_count)); for (i = 0; i < intf->generic_count; i++) {
DPRINTF_INFO((MY_ID " input endpoint = %p\n", usb_generic_descriptor *generic = &intf->generic[i]->generic;
&intf->endpoint[0])); DPRINTF_DEBUG((MY_ID "descriptor %d: type %x sub %x\n",
DPRINTF_INFO((MY_ID " output endpoint = %p\n", i, generic->descriptor_type, generic->data[0]));
&intf->endpoint[1])); if (generic->descriptor_type == USB_DESCRIPTOR_CS_ENDPOINT
usb->clear_feature(intf->endpoint[0].handle, USB_FEATURE_ENDPOINT_HALT); && generic->data[0] == USB_MS_GENERAL_DESCRIPTOR) {
/* This may need more thought... */ /* These *better* be in the same order as the endpoints! */
my_dev->ept_in = &intf->endpoint[0]; /* bulk IN */ cable_count[iep] = generic->data[1];
my_dev->ept_out = &intf->endpoint[1]; /* OUT */ iep = 1;
}
}
my_dev->timestamp = system_time(); DPRINTF_DEBUG((MY_ID "midiDevice = %p endpoint count = %ld\n",
midiDevice, intf->endpoint_count));
midiDevice->ept_in = midiDevice->ept_out = NULL;
for (i = 0; i < intf->endpoint_count && i < 2; i++) {
/* we are actually assuming max one IN, one OUT endpoint... */
DPRINTF_INFO((MY_ID "endpoint %d = %p %s\n",
i, &intf->endpoint[i],
(intf->endpoint[i].descr->endpoint_address & 0x80) != 0
? "IN" : "OUT"));
if ((intf->endpoint[i].descr->endpoint_address & 0x80) != 0) {
if (midiDevice->ept_in == NULL) {
midiDevice->ept_in = &intf->endpoint[i];
in_cables = cable_count[i];
}
} else if (midiDevice->ept_out == NULL) {
midiDevice->ept_out = &intf->endpoint[i];
out_cables = cable_count[i];
}
}
midiDevice->timestamp = system_time(); /* This never seems to be used */
/* Create the actual device ports */
for (i = 0; in_cables || out_cables; i++) {
port = create_usbmidi_port(midiDevice, i,
(bool)in_cables, (bool)out_cables);
midiDevice->ports[i] = port;
if (in_cables)
in_cables--;
if (out_cables)
out_cables--;
add_port_info(port);
}
/* issue bulk transfer */ /* issue bulk transfer */
DPRINTF_INFO((MY_ID "queueing bulk xfer ep 0\n")); DPRINTF_DEBUG((MY_ID "queueing bulk xfer IN endpoint\n"));
st = usb->queue_bulk(my_dev->ept_in->handle, my_dev->buffer, st = usb->queue_bulk(midiDevice->ept_in->handle, midiDevice->buffer,
my_dev->buffer_size, (usb_callback_func)midi_usb_read_callback, my_dev); midiDevice->buffer_size,
(usb_callback_func)midi_usb_read_callback, midiDevice);
if (st != B_OK) { if (st != B_OK) {
DPRINTF_ERR((MY_ID "queue_bulk() error %d\n", (int)st)); DPRINTF_ERR((MY_ID "queue_bulk() error %d\n", (int)st));
return B_ERROR; return B_ERROR;
} }
/* create a port */ *cookie = midiDevice;
add_device_info(my_dev); DPRINTF_INFO((MY_ID "usb_midi_added: %s\n", midiDevice->name));
*cookie = my_dev;
DPRINTF_INFO((MY_ID "usb_midi_added: added %s\n", my_dev->name));
return B_OK; return B_OK;
} }
@ -352,23 +435,32 @@ got_one:
static status_t static status_t
usb_midi_removed(void* cookie) usb_midi_removed(void* cookie)
{ {
usbmidi_device_info* my_dev = cookie; usbmidi_device_info* midiDevice = cookie;
int cable;
assert(cookie != NULL); assert(cookie != NULL);
DPRINTF_INFO((MY_ID "usb_midi_removed(%s)\n", my_dev->name)); DPRINTF_INFO((MY_ID "usb_midi_removed(%s)\n", midiDevice->name));
acquire_sem(usbmidi_device_list_lock); /* convenient mutex for safety */ midiDevice->active = false;
my_dev->active = false; for (cable = 0; cable < 16; cable++) {
if (my_dev->open_fd) { usbmidi_port_info* port = midiDevice->ports[cable];
my_dev->open_fd->my_dev = NULL; if (port == NULL)
delete_sem(my_dev->open_fd->sem_cb); /* done here to ensure read is freed */ break;
DPRINTF_DEBUG((MY_ID "removing port %d\n", cable));
if (port->open_fd != NULL) {
remove_port_info(port);
port->open_fd->port = NULL;
port->open_fd->device = NULL;
delete_sem(port->open_fd->sem_cb);
/* done here to ensure read is freed */
}
remove_port(port);
} }
release_sem(usbmidi_device_list_lock); usb->cancel_queued_transfers(midiDevice->ept_in->handle);
usb->cancel_queued_transfers(my_dev->ept_in->handle); usb->cancel_queued_transfers(midiDevice->ept_out->handle);
usb->cancel_queued_transfers(my_dev->ept_out->handle); DPRINTF_DEBUG((MY_ID "usb_midi_removed: doing remove: %s\n",
DPRINTF_INFO((MY_ID "usb_midi_removed: removing info & device: %s\n", my_dev->name)); midiDevice->name));
remove_device_info(my_dev); remove_device(midiDevice);
remove_device(my_dev);
return B_OK; return B_OK;
} }
@ -381,12 +473,17 @@ static usb_notify_hooks my_notify_hooks =
#define SUPPORTED_DEVICES 1 #define SUPPORTED_DEVICES 1
usb_support_descriptor my_supported_devices[SUPPORTED_DEVICES] = usb_support_descriptor my_supported_devices[SUPPORTED_DEVICES] =
{ {
{ USB_AUDIO_DEVICE_CLASS, USB_AUDIO_INTERFACE_MIDISTREAMING_SUBCLASS, 0, 0, 0 }, {
USB_AUDIO_DEVICE_CLASS,
USB_AUDIO_INTERFACE_MIDISTREAMING_SUBCLASS,
0, 0, 0
},
}; };
/* /*
usb_midi_open - handle open() calls Device Driver Hook Functions
-- open, read, write, close, and free
*/ */
static status_t static status_t
@ -394,16 +491,21 @@ usb_midi_open(const char* name, uint32 flags,
driver_cookie** out_cookie) driver_cookie** out_cookie)
{ {
driver_cookie* cookie; driver_cookie* cookie;
usbmidi_device_info* my_dev; usbmidi_port_info* port;
int mode = flags & O_RWMASK;
assert(name != NULL); assert(name != NULL);
assert(out_cookie != NULL); assert(out_cookie != NULL);
DPRINTF_INFO((MY_ID "usb_midi_open(%s)\n", name)); DPRINTF_INFO((MY_ID "usb_midi_open(%s) flags=%lx\n", name, flags));
if ((my_dev = search_device_info(name)) == NULL) if ((port = search_port_info(name)) == NULL)
return B_ENTRY_NOT_FOUND; return B_ENTRY_NOT_FOUND;
if (my_dev->open_fd != NULL)
return B_BUSY; /* there can only be one open channel to the device */ if (!port->has_in && mode != O_RDONLY)
return B_PERMISSION_DENIED; /* == EACCES */
else if (!port->has_out && mode != O_WRONLY)
return B_PERMISSION_DENIED;
if ((cookie = malloc(sizeof(driver_cookie))) == NULL) if ((cookie = malloc(sizeof(driver_cookie))) == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
@ -414,67 +516,74 @@ usb_midi_open(const char* name, uint32 flags,
return B_ERROR; return B_ERROR;
} }
acquire_sem(usbmidi_device_list_lock); /* use global mutex now */ cookie->port = port;
cookie->my_dev = my_dev; cookie->device = port->device;
my_dev->open_fd = cookie;
my_dev->open++; acquire_sem(usbmidi_port_list_lock);
release_sem(usbmidi_device_list_lock); if (port->open_fd != NULL) {
/* there can only be one open channel to the device */
delete_sem(cookie->sem_cb);
free(cookie);
release_sem(usbmidi_port_list_lock);
return B_BUSY;
}
port->open_fd = cookie;
port->open++;
release_sem(usbmidi_port_list_lock);
*out_cookie = cookie; *out_cookie = cookie;
DPRINTF_INFO((MY_ID "usb_midi_open: device %s open (%d)\n", name, my_dev->open)); DPRINTF_INFO((MY_ID "usb_midi_open: device %s open (%d)\n",
name, port->open));
return B_OK; return B_OK;
} }
/*
usb_midi_read - handle read() calls
*/
static status_t static status_t
usb_midi_read(driver_cookie* cookie, off_t position, usb_midi_read(driver_cookie* cookie, off_t position,
void* buf, size_t* num_bytes) void* buf, size_t* num_bytes)
{ {
status_t err = B_ERROR; status_t err = B_ERROR;
usbmidi_device_info* my_dev; usbmidi_port_info* port;
usbmidi_device_info* midiDevice;
assert(cookie != NULL); assert(cookie != NULL);
my_dev = cookie->my_dev; port = cookie->port;
midiDevice = cookie->device;
if (!my_dev || !my_dev->active) if (!midiDevice || !midiDevice->active)
return B_ERROR; /* already unplugged */ return B_ERROR; /* already unplugged */
DPRINTF_INFO((MY_ID "usb_midi_read: (%ld byte buffer at %ld cookie %p)\n", DPRINTF_DEBUG((MY_ID "usb_midi_read: (%ld byte buffer at %ld cookie %p)\n",
*num_bytes, (int32)position, cookie)); *num_bytes, (int32)position, cookie));
while (my_dev && my_dev->active) { while (midiDevice && midiDevice->active) {
DPRINTF_INFOZ((MY_ID "waiting on acquire_sem_etc\n");) DPRINTF_DEBUG((MY_ID "waiting on acquire_sem_etc\n"));
err = acquire_sem_etc(cookie->sem_cb, 1, err = acquire_sem_etc(cookie->sem_cb, 1,
B_RELATIVE_TIMEOUT, 1000000); B_RELATIVE_TIMEOUT, 1000000);
if (err == B_TIMED_OUT) { if (err == B_TIMED_OUT) {
DPRINTF_INFOZ((MY_ID "acquire_sem_etc timed out\n");) DPRINTF_DEBUG((MY_ID "acquire_sem_etc timed out\n"));
continue; /* see if we're still active */ continue; /* see if we're still active */
} }
if (err != B_OK) { if (err != B_OK) {
*num_bytes = 0; *num_bytes = 0;
DPRINTF_INFO((MY_ID "acquire_sem_etc aborted\n");) DPRINTF_DEBUG((MY_ID "acquire_sem_etc aborted\n"));
break; break;
} }
DPRINTF_INFO((MY_ID "reading from ringbuffer\n");) DPRINTF_DEBUG((MY_ID "reading from ringbuffer\n"));
acquire_sem(my_dev->sem_lock); acquire_sem(midiDevice->sem_lock);
ring_buffer_user_read(my_dev->rbuf, buf, 1); /* a global semaphore -- OK, I think */
release_sem(my_dev->sem_lock); ring_buffer_user_read(port->rbuf, buf, 1);
release_sem(midiDevice->sem_lock);
*num_bytes = 1; *num_bytes = 1;
DPRINTF_INFO((MY_ID "read byte %x -- cookie %p)\n", *(uint8*)buf, cookie)); DPRINTF_DEBUG((MY_ID "read byte %x -- cookie %p)\n",
*(uint8*)buf, cookie));
return B_OK; return B_OK;
} }
DPRINTF_INFO((MY_ID "usb_midi_read: loop terminated -- Device no longer active\n");) DPRINTF_INFO((MY_ID "usb_midi_read: loop terminated"
" -- Device no longer active\n"));
return B_CANCELED; return B_CANCELED;
} }
/*
usb_midi_write - handle write() calls
*/
const uint8 CINcode[] = { /* see USB-MIDI Spec */ const uint8 CINcode[] = { /* see USB-MIDI Spec */
0x4, /* 0x0 - sysex start */ 0x4, /* 0x0 - sysex start */
0, /* 0x1 -- undefined */ 0, /* 0x1 -- undefined */
@ -499,7 +608,8 @@ static status_t
usb_midi_write(driver_cookie* cookie, off_t position, usb_midi_write(driver_cookie* cookie, off_t position,
const void* buf, size_t* num_bytes) const void* buf, size_t* num_bytes)
{ {
usbmidi_device_info* my_dev; usbmidi_port_info* port;
usbmidi_device_info* midiDevice;
uint8* midiseq = (uint8*)buf; uint8* midiseq = (uint8*)buf;
uint8 midicode = midiseq[0]; /* preserved for reference */ uint8 midicode = midiseq[0]; /* preserved for reference */
status_t st; status_t st;
@ -509,26 +619,30 @@ usb_midi_write(driver_cookie* cookie, off_t position,
: (midicode >> 4); : (midicode >> 4);
assert(cookie != NULL); assert(cookie != NULL);
my_dev = cookie->my_dev; port = cookie->port;
midiDevice = cookie->device;
if (!my_dev || !my_dev->active) if (!midiDevice || !midiDevice->active)
return B_ERROR; /* already unplugged */ return B_ERROR; /* already unplugged */
buff_lim = my_dev->buffer_size * 3 / 4; /* max MIDI bytes buffer space */ buff_lim = midiDevice->buffer_size * 3 / 4;
/* max MIDI bytes buffer space */
DPRINTF_INFO((MY_ID "MIDI write (%ld bytes at %Ld)\n", *num_bytes, position)); DPRINTF_DEBUG((MY_ID "MIDI write (%ld bytes at %Ld)\n",
*num_bytes, position));
if (*num_bytes > 3 && midicode != 0xF0) { if (*num_bytes > 3 && midicode != 0xF0) {
DPRINTF_ERR((MY_ID "Non-SysEx packet of %ld bytes -- too big to handle\n", DPRINTF_ERR((MY_ID "Non-SysEx packet of %ld bytes"
*num_bytes)); " -- too big to handle\n", *num_bytes));
return B_ERROR; return B_ERROR;
} }
while (bytes_left) { while (bytes_left) {
size_t xfer_bytes = (bytes_left < buff_lim)? bytes_left : buff_lim; size_t xfer_bytes = (bytes_left < buff_lim) ? bytes_left : buff_lim;
usb_midi_event_packet* pkt = my_dev->out_buffer; usb_midi_event_packet* pkt = midiDevice->out_buffer;
int packet_count = 0; int packet_count = 0;
st = acquire_sem_etc(my_dev->sem_send, 1, B_RELATIVE_TIMEOUT, 2000000LL); st = acquire_sem_etc(midiDevice->sem_send,
1, B_RELATIVE_TIMEOUT, 2000000LL);
if (st != B_OK) if (st != B_OK)
return st; return st;
@ -536,21 +650,26 @@ usb_midi_write(driver_cookie* cookie, off_t position,
uint8 pkt_bytes = CINbytes[cin]; uint8 pkt_bytes = CINbytes[cin];
memset(pkt, 0, sizeof(usb_midi_event_packet)); memset(pkt, 0, sizeof(usb_midi_event_packet));
pkt->cin = cin; pkt->cin = cin;
DPRINTF_INFO((MY_ID "using packet data (code %x -- %d bytes) %x %x %x\n", pkt->cin, pkt->cn = port->cable;
CINbytes[pkt->cin], midiseq[0], midiseq[1], midiseq[2])); DPRINTF_DEBUG((MY_ID "using packet data (code %x -- %d bytes)"
" %x %x %x\n", pkt->cin, CINbytes[pkt->cin],
midiseq[0], midiseq[1], midiseq[2]));
memcpy(pkt->midi, midiseq, pkt_bytes); memcpy(pkt->midi, midiseq, pkt_bytes);
DPRINTF_INFO((MY_ID "built packet %p %x:%d %x %x %x\n", pkt, pkt->cin, pkt->cn, DPRINTF_DEBUG((MY_ID "built packet %p %x:%d %x %x %x\n",
pkt, pkt->cin, pkt->cn,
pkt->midi[0], pkt->midi[1], pkt->midi[2])); pkt->midi[0], pkt->midi[1], pkt->midi[2]));
xfer_bytes -= pkt_bytes; xfer_bytes -= pkt_bytes;
bytes_left -= pkt_bytes; bytes_left -= pkt_bytes;
midiseq += pkt_bytes; midiseq += pkt_bytes;
packet_count++; packet_count++;
pkt++; pkt++;
if (midicode == 0xF0 && bytes_left < 4) cin = 4 + bytes_left; /* see USB-MIDI Spec */ if (midicode == 0xF0 && bytes_left < 4) cin = 4 + bytes_left;
/* see USB-MIDI Spec */
} }
st = usb->queue_bulk(my_dev->ept_out->handle, my_dev->out_buffer, st = usb->queue_bulk(midiDevice->ept_out->handle,
sizeof(usb_midi_event_packet) * packet_count, midiDevice->out_buffer,
(usb_callback_func)midi_usb_write_callback, my_dev); sizeof(usb_midi_event_packet) * packet_count,
(usb_callback_func)midi_usb_write_callback, midiDevice);
if (st != B_OK) { if (st != B_OK) {
DPRINTF_ERR((MY_ID "midi write queue_bulk() error %d\n", (int)st)); DPRINTF_ERR((MY_ID "midi write queue_bulk() error %d\n", (int)st));
return B_ERROR; return B_ERROR;
@ -560,10 +679,6 @@ usb_midi_write(driver_cookie* cookie, off_t position,
} }
/*
usb_midi_control - handle ioctl calls
*/
static status_t static status_t
usb_midi_control(void* cookie, uint32 iop, usb_midi_control(void* cookie, uint32 iop,
void* data, size_t len) void* data, size_t len)
@ -572,47 +687,43 @@ usb_midi_control(void* cookie, uint32 iop,
} }
/*
usb_midi_close - handle close() calls
*/
static status_t static status_t
usb_midi_close(driver_cookie* cookie) usb_midi_close(driver_cookie* cookie)
{ {
usbmidi_device_info* my_dev; usbmidi_port_info* port;
usbmidi_device_info* midiDevice;
assert(cookie != NULL); assert(cookie != NULL);
delete_sem(cookie->sem_cb); delete_sem(cookie->sem_cb);
my_dev = cookie->my_dev; port = cookie->port;
DPRINTF_INFO((MY_ID "usb_midi_close(%p device=%p)\n", cookie, my_dev)); midiDevice = cookie->device;
DPRINTF_INFO((MY_ID "usb_midi_close(%p device=%p port=%p)\n",
cookie, midiDevice, port));
acquire_sem(usbmidi_device_list_lock); acquire_sem(usbmidi_port_list_lock);
if (my_dev) { if (port) {
/* detach the cookie from device */ /* detach the cookie from port */
my_dev->open_fd = NULL; port->open_fd = NULL;
--my_dev->open; --port->open;
} }
release_sem(usbmidi_device_list_lock); release_sem(usbmidi_port_list_lock);
DPRINTF_INFO((MY_ID "usb_midi_close: complete\n");) DPRINTF_DEBUG((MY_ID "usb_midi_close: complete\n"));
return B_OK; return B_OK;
} }
/*
usb_midi_free - called after the last device is closed, and after
all i/o is complete.
*/
static status_t static status_t
usb_midi_free(driver_cookie* cookie) usb_midi_free(driver_cookie* cookie)
{ {
usbmidi_device_info* my_dev; usbmidi_port_info* port; /* all only for info */
usbmidi_device_info* midiDevice;
assert(cookie != NULL); assert(cookie != NULL);
my_dev = cookie->my_dev; port = cookie->port;
DPRINTF_INFO((MY_ID "usb_midi_free(%p device=%p)\n", cookie, my_dev)); midiDevice = cookie->device;
DPRINTF_INFO((MY_ID "usb_midi_free(%p device=%p)\n", cookie, midiDevice));
free(cookie); free(cookie);
@ -620,10 +731,6 @@ usb_midi_free(driver_cookie* cookie)
} }
/*
function pointers for the device hooks entry points
*/
static device_hooks usb_midi_hooks = { static device_hooks usb_midi_hooks = {
(device_open_hook)usb_midi_open, (device_open_hook)usb_midi_open,
(device_close_hook)usb_midi_close, (device_close_hook)usb_midi_close,
@ -636,34 +743,30 @@ static device_hooks usb_midi_hooks = {
/* /*
init_hardware - called once the first time the driver is loaded Driver Registration
*/ */
_EXPORT status_t _EXPORT status_t
init_hardware(void) init_hardware(void)
{ {
DPRINTF_INFO((MY_ID "init_hardware() " __DATE__ " " __TIME__ "\n")); DPRINTF_DEBUG((MY_ID "init_hardware() version:"
__DATE__ " " __TIME__ "\n"));
return B_OK; return B_OK;
} }
/*
init_driver - optional function - called every time the driver
is loaded.
*/
_EXPORT status_t _EXPORT status_t
init_driver(void) init_driver(void)
{ {
DPRINTF_INFO((MY_ID "init_driver() " __DATE__ " " __TIME__ "\n")); DPRINTF_INFO((MY_ID "init_driver() version:" __DATE__ " " __TIME__ "\n"));
if (get_module(B_USB_MODULE_NAME, (module_info**)&usb) != B_OK) if (get_module(B_USB_MODULE_NAME, (module_info**)&usb) != B_OK)
return B_ERROR; return B_ERROR;
if ((usbmidi_device_list_lock = create_sem(1, "dev_list_lock")) < 0) { if ((usbmidi_port_list_lock = create_sem(1, "dev_list_lock")) < 0) {
put_module(B_USB_MODULE_NAME); put_module(B_USB_MODULE_NAME);
return usbmidi_device_list_lock; /* error code */ return usbmidi_port_list_lock; /* error code */
} }
usb->register_driver(usb_midi_driver_name, my_supported_devices, usb->register_driver(usb_midi_driver_name, my_supported_devices,
@ -675,58 +778,42 @@ init_driver(void)
} }
/*
uninit_driver - optional function - called every time the driver
is unloaded
*/
_EXPORT void _EXPORT void
uninit_driver(void) uninit_driver(void)
{ {
DPRINTF_INFO((MY_ID "uninit_driver()\n")); DPRINTF_INFO((MY_ID "uninit_driver()\n"));
usb->uninstall_notify(usb_midi_driver_name); usb->uninstall_notify(usb_midi_driver_name);
delete_sem(usbmidi_device_list_lock); delete_sem(usbmidi_port_list_lock);
put_module(B_USB_MODULE_NAME); put_module(B_USB_MODULE_NAME);
free_device_names(); free_port_names();
DPRINTF_INFO((MY_ID "uninit complete\n")); DPRINTF_INFO((MY_ID "uninit complete\n"));
} }
/*
publish_devices
device names are generated dynamically
*/
_EXPORT const char** _EXPORT const char**
publish_devices(void) publish_devices(void)
{ {
DPRINTF_INFO((MY_ID "publish_devices()\n")); DPRINTF_INFO((MY_ID "publish_devices()\n"));
if (usbmidi_device_list_changed) { if (usbmidi_port_list_changed) {
free_device_names(); free_port_names();
alloc_device_names(); alloc_port_names();
if (usbmidi_device_names != NULL) if (usbmidi_port_names != NULL)
rebuild_device_names(); rebuild_port_names();
usbmidi_device_list_changed = false; usbmidi_port_list_changed = false;
} }
assert(usbmidi_device_names != NULL); assert(usbmidi_port_names != NULL);
return (const char**)usbmidi_device_names; return (const char**)usbmidi_port_names;
} }
/*
find_device - return ptr to device hooks structure for a
given device name
*/
_EXPORT device_hooks* _EXPORT device_hooks*
find_device(const char* name) find_device(const char* name)
{ {
assert(name != NULL); assert(name != NULL);
DPRINTF_INFO((MY_ID "find_device(%s)\n", name)); DPRINTF_INFO((MY_ID "find_device(%s)\n", name));
if (search_device_info(name) == NULL) if (search_port_info(name) == NULL)
return NULL; return NULL;
return &usb_midi_hooks; return &usb_midi_hooks;
} }

View File

@ -2,7 +2,7 @@
* midi usb driver * midi usb driver
* usb_midi.h * usb_midi.h
* *
* Copyright 2006-2009 Haiku Inc. All rights reserved. * Copyright 2006-2011 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT Licence. * Distributed under the terms of the MIT Licence.
* *
* Authors: * Authors:
@ -23,23 +23,32 @@
#include "ring_buffer.h" #include "ring_buffer.h"
/* Three levels of printout for convenience: */
/* #define DEBUG 1 -- more convenient to define in the code file when needed */ /* #define DEBUG 1 -- more convenient to define in the code file when needed */
#define DEBUG_INFO 1
#define DEBUG_ERR 1 #define DEBUG_ERR 1
#if DEBUG /* Normally leave this enabled to leave a record in syslog */
#define DPRINTF_INFO(x) dprintf x
#else
#define DPRINTF_INFO(x)
#endif
#if DEBUG_ERR #if DEBUG_ERR
#define DPRINTF_ERR(x) dprintf x #define DPRINTF_ERR(x) dprintf x
#else #else
#define DPRINTF_ERR(x) #define DPRINTF_ERR(x)
#endif #endif
/* For local finer debugging control: */ /* Use this for initialization etc. messages -- nothing repetitive: */
#define DPRINTF_INFOX(x) dprintf x #if DEBUG_INFO
#define DPRINTF_INFOZ(x) #define DPRINTF_INFO(x) dprintf x
#else
#define DPRINTF_INFO(x)
#endif
/* Enable this to record detailed stuff: */
#if DEBUG
#define DPRINTF_DEBUG(x) dprintf x
#else
#define DPRINTF_DEBUG(x)
#endif
/* driver specific definitions */ /* driver specific definitions */
@ -49,7 +58,8 @@
#define MY_ERR "\033[31merror:\033[m " #define MY_ERR "\033[31merror:\033[m "
#define MY_WARN "\033[31mwarning:\033[m " #define MY_WARN "\033[31mwarning:\033[m "
#define assert(x) \ #define assert(x) \
((x) ? 0 : dprintf(MY_ID "assertion failed at " __FILE__ ", line %d\n", __LINE__)) ((x) ? 0 : dprintf(MY_ID "assertion failed at " \
__FILE__ ", line %d\n", __LINE__))
#define DEFAULT_CONFIGURATION 0 #define DEFAULT_CONFIGURATION 0
@ -58,61 +68,91 @@ struct driver_cookie;
typedef struct usbmidi_device_info typedef struct usbmidi_device_info
{ {
/* list structure */ /* list structure */ /* should not be needed eventually */
struct usbmidi_device_info* next; struct usbmidi_device_info* next;
/* maintain device */ /* Set of actual ports ("cables" -- one or more) */
struct usbmidi_port_info* ports[16];
/* maintain device (common for all ports) */
sem_id sem_lock; sem_id sem_lock;
sem_id sem_send; sem_id sem_send;
area_id buffer_area; area_id buffer_area;
usb_midi_event_packet* buffer; /* input buffer & base of area */ usb_midi_event_packet* buffer; /* input buffer & base of area */
usb_midi_event_packet* out_buffer; /* above input buff */ usb_midi_event_packet* out_buffer; /* above input buffer */
size_t buffer_size; /* for each of in and out buffers */
const usb_device* dev; const usb_device* dev;
uint16 ifno; uint16 ifno;
char name[30]; int devnum; /* unique device number */
char name[20];
struct ring_buffer* rbuf; /* = "/dev/midi/usb/n" --port number will be appended to this */
bool active; bool active;
int open;
struct driver_cookie* open_fd;
/* work area for transfer */ /* work area for transfer */
int usbd_status, bus_status, cmd_status; int bus_status;
int actual_length; int actual_length;
const usb_endpoint_info* ept_in; const usb_endpoint_info* ept_in;
const usb_endpoint_info* ept_out; const usb_endpoint_info* ept_out;
size_t buffer_size; bigtime_t timestamp; /* Is this needed? Currently set but never read */
bigtime_t timestamp; uint flags; /* set to 0 but never used */
uint flags;
} usbmidi_device_info; } usbmidi_device_info;
/* usb_midi.c */ typedef struct usbmidi_port_info
{
/* list structure for manager */
struct usbmidi_port_info* next;
/* Common device that does the work */
usbmidi_device_info* device;
/* Port-specific variables */
char name[40]; /* complete pathname of this port */
struct ring_buffer* rbuf;
int cable; /* index of this port */
bool has_in, has_out;
int open;
struct driver_cookie* open_fd;
} usbmidi_port_info;
/*
usb_midi.c
*/
extern usb_module_info* usb; extern usb_module_info* usb;
extern const char* usb_midi_base_name; extern const char* usb_midi_base_name;
usbmidi_device_info* extern usbmidi_port_info* create_usbmidi_port(usbmidi_device_info* devinfo,
create_device(const usb_device* dev, const usb_interface_info* ii, uint16 ifno); int cable, bool has_in, bool has_out);
extern void remove_port(usbmidi_port_info* port);
void extern usbmidi_device_info* create_device(const usb_device* dev, uint16 ifno);
remove_device(usbmidi_device_info* my_dev); extern void remove_device(usbmidi_device_info* my_dev);
/* devlist.c */ /*
devlist.c
*/
extern sem_id usbmidi_device_list_lock; extern sem_id usbmidi_port_list_lock;
extern bool usbmidi_device_list_changed; extern bool usbmidi_port_list_changed;
void add_device_info(usbmidi_device_info* my_dev); extern void add_port_info(usbmidi_port_info* port);
void remove_device_info(usbmidi_device_info* my_dev); extern void remove_port_info(usbmidi_port_info* port);
usbmidi_device_info* search_device_info(const char* name);
extern char** usbmidi_device_names; extern usbmidi_port_info* search_port_info(const char* name);
void alloc_device_names(void); extern int find_free_device_number(void);
void free_device_names(void);
void rebuild_device_names(void); extern char** usbmidi_port_names;
extern void alloc_port_names(void);
extern void free_port_names(void);
extern void rebuild_port_names(void);