Apply Pete Goodeve patch, provided in #4463:

* add output support
 * fix variable lenght input: all usb_midi_event_packet bytes were 
   always returned before.
Missing features are:
 * multiport support (input from any ports are read and merged currently, 
 so beware to connect only one port!)
 * non-standard USB midi adapters, like my Roland UM-2 which don't advertize 
   themselves as Audio / Midi stream class/subclass.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33126 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Philippe Houdoin
2009-09-14 03:56:49 +00:00
parent 9b0a2ab24c
commit bfa9be00c4
3 changed files with 534 additions and 385 deletions
@@ -1,128 +1,129 @@
/*****************************************************************************/
// midi usb driver
// Written by Jérôme Duval
//
// devlist.c
//
// Copyright (c) 2006 Haiku Project
//
// Some portions of code are copyrighted by
// USB Joystick driver for BeOS R5
// Copyright 2000 (C) ITO, Takayuki
// All rights reserved
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
/*
* midi usb driver
* devlist.c
*
* Copyright 2009 Haiku Inc. All rights reserved.
* Distributed under tthe terms of the MIT Licence.
*
* Authors:
* Jérôme Duval
* Pete Goodeve, [email protected]
*
* Some portions of this code were originally derived from
* USB Joystick driver for BeOS R5
* Copyright 2000 (C) ITO, Takayuki
* All rights reserved
*
*/
#include "usb_midi.h"
#include <stdlib.h>
#include <string.h>
#include "usb_midi.h"
sem_id my_device_list_lock = -1;
bool my_device_list_changed = true; /* added or removed */
static my_device_info *my_device_list = NULL;
static int my_device_count = 0;
sem_id usbmidi_device_list_lock = -1;
bool usbmidi_device_list_changed = true; /* added or removed */
void add_device_info (my_device_info *my_dev)
static usbmidi_device_info* usbmidi_device_list = NULL;
static int usbmidi_device_count = 0;
void
add_device_info(usbmidi_device_info* my_dev)
{
assert(my_dev != NULL);
acquire_sem (my_device_list_lock);
my_dev->next = my_device_list;
my_device_list = my_dev;
my_device_count++;
my_device_list_changed = true;
release_sem (my_device_list_lock);
acquire_sem(usbmidi_device_list_lock);
my_dev->next = usbmidi_device_list;
usbmidi_device_list = my_dev;
usbmidi_device_count++;
usbmidi_device_list_changed = true;
release_sem(usbmidi_device_list_lock);
}
void remove_device_info (my_device_info *my_dev)
void
remove_device_info(usbmidi_device_info* my_dev)
{
assert(my_dev != NULL);
acquire_sem (my_device_list_lock);
if (my_device_list == my_dev) {
my_device_list = my_dev->next;
--my_device_count;
my_device_list_changed = true;
acquire_sem(usbmidi_device_list_lock);
if (usbmidi_device_list == my_dev) {
usbmidi_device_list = my_dev->next;
--usbmidi_device_count;
usbmidi_device_list_changed = true;
} else {
my_device_info *d;
for (d = my_device_list; d != NULL; d = d->next) {
usbmidi_device_info* d;
for (d = usbmidi_device_list; d != NULL; d = d->next) {
if (d->next == my_dev) {
d->next = my_dev->next;
--my_device_count;
my_device_list_changed = true;
--usbmidi_device_count;
usbmidi_device_list_changed = true;
break;
}
}
assert(d != NULL);
}
release_sem (my_device_list_lock);
release_sem(usbmidi_device_list_lock);
}
my_device_info *search_device_info (const char* name)
{
my_device_info *my_dev;
acquire_sem (my_device_list_lock);
for (my_dev = my_device_list; my_dev != NULL; my_dev = my_dev->next) {
usbmidi_device_info*
search_device_info(const char* name)
{
usbmidi_device_info* my_dev;
acquire_sem(usbmidi_device_list_lock);
for (my_dev = usbmidi_device_list; my_dev != NULL; my_dev = my_dev->next) {
if (strcmp(my_dev->name, name) == 0)
break;
}
release_sem (my_device_list_lock);
release_sem(usbmidi_device_list_lock);
return my_dev;
}
/*
device names
*/
/* dynamically generated */
char **my_device_names = NULL;
char** usbmidi_device_names = NULL;
void alloc_device_names (void)
void
alloc_device_names(void)
{
assert (my_device_names == NULL);
my_device_names = malloc (sizeof (char *) * (my_device_count + 1));
assert(usbmidi_device_names == NULL);
usbmidi_device_names = malloc(sizeof(char*) * (usbmidi_device_count + 1));
}
void free_device_names (void)
void
free_device_names(void)
{
if (my_device_names != NULL) {
if (usbmidi_device_names != NULL) {
int i;
for (i = 0; my_device_names [i] != NULL; i++)
free (my_device_names [i]);
free (my_device_names);
my_device_names = NULL;
for (i = 0; usbmidi_device_names[i] != NULL; i++)
free(usbmidi_device_names[i]);
free(usbmidi_device_names);
usbmidi_device_names = NULL;
}
}
void rebuild_device_names (void)
void
rebuild_device_names(void)
{
int i;
my_device_info *my_dev;
usbmidi_device_info* my_dev;
assert (my_device_names != NULL);
acquire_sem (my_device_list_lock);
for (i = 0, my_dev = my_device_list; my_dev != NULL; my_dev = my_dev->next) {
my_device_names [i++] = strdup(my_dev->name);
assert(usbmidi_device_names != NULL);
acquire_sem(usbmidi_device_list_lock);
for (i = 0, my_dev = usbmidi_device_list; my_dev != NULL; my_dev = my_dev->next) {
usbmidi_device_names[i++] = strdup(my_dev->name);
DPRINTF_INFO((MY_ID "publishing %s\n", my_dev->name));
}
my_device_names [i] = NULL;
release_sem (my_device_list_lock);
usbmidi_device_names[i] = NULL;
release_sem(usbmidi_device_list_lock);
}
@@ -1,49 +1,42 @@
/*****************************************************************************/
// midi usb driver
// Written by Jérôme Duval
//
// usb_midi.c
//
// Copyright (c) 2006 Haiku Project
//
// Some portions of code are copyrighted by
// USB Joystick driver for BeOS R5
// Copyright 2000 (C) ITO, Takayuki
// All rights reserved
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
/*
* midi usb driver
* usb_midi.c
*
* Copyright 2009 Haiku Inc. All rights reserved.
* Distributed under tthe terms of the MIT Licence.
*
* Authors:
* Jérôme Duval
* Pete Goodeve, [email protected]
*
* Some portions of this code were originally derived from
* USB Joystick driver for BeOS R5
* Copyright 2000 (C) ITO, Takayuki
* All rights reserved
*
*/
/*#define DEBUG 1*/ /* Define this to enable DPRINTF_INFO statements */
#include "usb_midi.h"
#include <support/Debug.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "usb_midi.h"
#include "usbdevs.h"
#include "usbdevs_data.h"
static int midi_device_number = 0;
const char* midi_base_name = "midi/usb/";
my_device_info *
usbmidi_device_info*
create_device(const usb_device* dev, const usb_interface_info* ii, uint16 ifno)
{
my_device_info *my_dev = NULL;
usbmidi_device_info* my_dev = NULL;
int number;
area_id area;
sem_id sem;
@@ -55,7 +48,7 @@ create_device(const usb_device *dev, const usb_interface_info *ii, uint16 ifno)
number = midi_device_number++;
base_name = midi_base_name;
my_dev = malloc (sizeof (my_device_info));
my_dev = malloc(sizeof(usbmidi_device_info));
if (my_dev == NULL)
return NULL;
@@ -85,6 +78,23 @@ create_device(const usb_device *dev, const usb_interface_info *ii, uint16 ifno)
free(my_dev);
return NULL;
}
/* 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);
my_dev->sem_send = sem = create_sem(1, DRIVER_NAME "_send");
if (sem < 0) {
DPRINTF_ERR((MY_ID "create_sem() failed %d\n", (int)sem));
delete_sem(my_dev->sem_cb);
delete_sem(my_dev->sem_lock);
delete_area(area);
free(my_dev);
return NULL;
}
{
int32 bc;
get_sem_count(sem, &bc);
DPRINTF_INFO((MY_ID "Allocated %d write buffers\n", bc));
}
sprintf(my_dev->name, "%s%d", base_name, number);
my_dev->dev = dev;
@@ -93,14 +103,15 @@ create_device(const usb_device *dev, const usb_interface_info *ii, uint16 ifno)
my_dev->open_fds = NULL;
my_dev->active = true;
my_dev->flags = 0;
my_dev->rbuf = create_ring_buffer(384);
my_dev->total_report_size = sizeof(usb_midi_event_packet);
my_dev->rbuf = create_ring_buffer(1024);
my_dev->buffer_size = B_PAGE_SIZE/2;
return my_dev;
}
void
remove_device (my_device_info *my_dev)
remove_device(usbmidi_device_info* my_dev)
{
assert(my_dev != NULL);
if (my_dev->rbuf != NULL) {
@@ -111,6 +122,7 @@ remove_device (my_device_info *my_dev)
delete_area(my_dev->buffer_area);
delete_sem(my_dev->sem_cb);
delete_sem(my_dev->sem_lock);
delete_sem(my_dev->sem_send);
free(my_dev);
}
@@ -120,24 +132,52 @@ remove_device (my_device_info *my_dev)
typedef struct driver_cookie
{
struct driver_cookie *next;
my_device_info *my_dev;
usbmidi_device_info *my_dev;
} driver_cookie;
/* NB global variables are valid only while driver is loaded */
_EXPORT int32 api_version = B_CUR_DRIVER_API_VERSION;
const char* usb_midi_driver_name = "usb_midi";
const int CINbytes[] = { /* See USB-MIDI Spec */
0, /* 0x0 -- undefined Misc -- Reserved */
0, /* 0x1 -- undefined Cable -- Reserved */
2, /* 0x2 -- 2-byte system common */
3, /* 0x3 -- 3-byte system common */
3, /* 0x4 -- SysEx start/continue */
1, /* 0x5 -- SysEx single-byte-end, or 1-byte Common */
2, /* 0x6 -- SysEx two-byte-end */
3, /* 0x7 -- SysEx three-byte-end */
3, /* 0x8 -- Note Off */
3, /* 0x9 -- Note On */
3, /* 0xA -- Poly KeyPress*/
3, /* 0xB -- Control Change */
2, /* 0xC -- Program Change */
2, /* 0xD -- Channel Pressure */
3, /* 0xE -- Pitch Bend */
1, /* 0xF -- Single Byte */
};
usb_module_info* usb;
static void
interpret_midi_buffer(my_device_info *my_dev)
interpret_midi_buffer(usbmidi_device_info* my_dev)
{
usb_midi_event_packet* packet = my_dev->buffer;
ring_buffer_write(my_dev->rbuf, packet->midi, sizeof(packet->midi));
release_sem_etc(my_dev->sem_cb, 3, B_DO_NOT_RESCHEDULE);
size_t bytes_left = my_dev->actual_length;
while (bytes_left) { /* buffer may have several packets */
int pktlen = CINbytes[packet->cin];
DPRINTF_INFO((MY_ID "received packet %x:%d %x %x %x\n", packet->cin, packet->cn,
packet->midi[0], packet->midi[1], packet->midi[2]));
ring_buffer_write(my_dev->rbuf, packet->midi, pktlen);
release_sem_etc(my_dev->sem_cb, pktlen, B_DO_NOT_RESCHEDULE);
packet++;
bytes_left -= sizeof(usb_midi_event_packet);
}
}
@@ -146,14 +186,14 @@ interpret_midi_buffer(my_device_info *my_dev)
*/
static void
midi_usb_callback(void *cookie, status_t status,
midi_usb_read_callback(void* cookie, status_t status,
void* data, size_t actual_len)
{
status_t st;
my_device_info *my_dev = cookie;
usbmidi_device_info* my_dev = cookie;
assert(cookie != NULL);
DPRINTF_INFO ((MY_ID "midi_usb_callback()\n"));
DPRINTF_INFO((MY_ID "midi_usb_read_callback() -- packet length %d\n", actual_len));
acquire_sem(my_dev->sem_lock);
my_dev->actual_length = actual_len;
@@ -166,22 +206,17 @@ midi_usb_callback(void *cookie, status_t status,
/* cancelled: device is unplugged */
return;
}
#if 0
st = usb->clear_feature (my_dev->ept->handle, USB_FEATURE_ENDPOINT_HALT);
if (st != B_OK)
DPRINTF_ERR ((MY_ID "clear_feature() error %d\n", (int)st));
#endif
} else {
/* got a report */
//#if 0
#if 0
uint32 i;
char linbuf[256];
uint8* buffer = my_dev->buffer;
for (i = 0; i < my_dev->total_report_size; i++)
sprintf (&linbuf[i*3], "%02X ", buffer [i]);
for (i = 0; i < 4; i++)
sprintf=&linbuf[i*3], "%02X ", buffer[i]);
DPRINTF_INFO((MY_ID "report: %s\n", linbuf));
//#endif
#endif
my_dev->timestamp = system_time();
interpret_midi_buffer(my_dev);
@@ -190,14 +225,31 @@ midi_usb_callback(void *cookie, status_t status,
/* issue next request */
st = usb->queue_bulk (my_dev->ept->handle, my_dev->buffer,
my_dev->total_report_size, midi_usb_callback, my_dev);
st = usb->queue_bulk(my_dev->ept_in->handle, my_dev->buffer,
my_dev->buffer_size, (usb_callback_func)midi_usb_read_callback, my_dev);
if (st != B_OK) {
/* XXX probably endpoint stall */
DPRINTF_ERR ((MY_ID "queue_interrupt() error %d\n", (int)st));
DPRINTF_ERR((MY_ID "queue_bulk() error %d\n", (int)st));
}
}
static void
midi_usb_write_callback(void* cookie, status_t status,
void* data, size_t actual_len)
{
usbmidi_device_info* my_dev = cookie;
#ifdef DEBUG
usb_midi_event_packet* pkt = data;
#endif
assert(cookie != NULL);
DPRINTF_INFO((MY_ID "midi_usb_write_callback() status %ld length %d pkt %p cin %x\n",
status, actual_len, pkt, pkt->cin));
release_sem(my_dev->sem_send); /* done with buffer */
}
/*
USB specific device hooks
*/
@@ -205,12 +257,13 @@ midi_usb_callback(void *cookie, status_t status,
static status_t
usb_midi_added(const usb_device* dev, void** cookie)
{
my_device_info *my_dev;
usbmidi_device_info* my_dev;
const usb_device_descriptor* dev_desc;
const usb_configuration_info* conf;
const usb_interface_info* intf;
status_t st;
uint16 ifno, i;
int alt;
assert(dev != NULL && cookie != NULL);
DPRINTF_INFO((MY_ID "device_added()\n"));
@@ -230,7 +283,6 @@ usb_midi_added(const usb_device *dev, void **cookie)
for (ifno = 0; ifno < conf->interface_count; ifno++) {
/* This is C; I can use "class" :-> */
int class, subclass, protocol;
int alt;
for (alt = 0; alt < conf->interface[ifno].alt_count; alt++) {
intf = &conf->interface[ifno].alt[alt];
@@ -259,10 +311,6 @@ got_one:
}
}
if((st = usb->set_alt_interface(dev, intf)) != B_OK) {
DPRINTF_ERR ((MY_ID "set_alt_interface() failed %d\n", (int)st));
return B_ERROR;
}
/* configuration */
if ((st = usb->set_configuration(dev, conf)) != B_OK) {
@@ -274,15 +322,23 @@ got_one:
return B_ERROR;
}
DPRINTF_INFO((MY_ID "my_dev = %p endpoint count = %ld\n",
my_dev, intf->endpoint_count));
DPRINTF_INFO((MY_ID " input endpoint = %p\n",
&intf->endpoint[0]));
DPRINTF_INFO((MY_ID " output endpoint = %p\n",
&intf->endpoint[1]));
usb->clear_feature(intf->endpoint[0].handle, USB_FEATURE_ENDPOINT_HALT);
// TODO: verify if this is necessary at all
/* This may need more thought... */
my_dev->ept_in = &intf->endpoint[0]; /* bulk IN */
my_dev->ept_out = &intf->endpoint[1]; /* OUT */
my_dev->timestamp = system_time();
/* issue bulk transfer */
my_dev->ept = &intf->endpoint [0]; /* interrupt IN */
st = usb->queue_bulk (my_dev->ept->handle, my_dev->buffer,
my_dev->total_report_size, midi_usb_callback, my_dev);
DPRINTF_INFO((MY_ID "queueing bulk xfer ep 0\n"));
st = usb->queue_bulk(my_dev->ept_in->handle, my_dev->buffer,
my_dev->buffer_size, (usb_callback_func)midi_usb_read_callback, my_dev);
if (st != B_OK) {
DPRINTF_ERR((MY_ID "queue_bulk() error %d\n", (int)st));
return B_ERROR;
@@ -297,15 +353,17 @@ got_one:
return B_OK;
}
static status_t
usb_midi_removed(void* cookie)
{
my_device_info *my_dev = cookie;
usbmidi_device_info* my_dev = cookie;
assert(cookie != NULL);
DPRINTF_INFO((MY_ID "device_removed(%s)\n", my_dev->name));
usb->cancel_queued_transfers (my_dev->ept->handle);
usb->cancel_queued_transfers(my_dev->ept_in->handle);
usb->cancel_queued_transfers(my_dev->ept_out->handle);
remove_device_info(my_dev);
if (my_dev->open == 0) {
remove_device(my_dev);
@@ -316,6 +374,7 @@ usb_midi_removed (void *cookie)
return B_OK;
}
static usb_notify_hooks my_notify_hooks =
{
usb_midi_added, usb_midi_removed
@@ -328,16 +387,16 @@ usb_support_descriptor my_supported_devices [SUPPORTED_DEVICES] =
};
/* ----------
/*
usb_midi_open - handle open() calls
----- */
*/
static status_t
usb_midi_open(const char* name, uint32 flags,
driver_cookie** out_cookie)
{
driver_cookie* cookie;
my_device_info *my_dev;
usbmidi_device_info* my_dev;
assert(name != NULL);
assert(out_cookie != NULL);
@@ -361,16 +420,16 @@ usb_midi_open(const char *name, uint32 flags,
}
/* ----------
/*
usb_midi_read - handle read() calls
----- */
*/
static status_t
usb_midi_read(driver_cookie* cookie, off_t position,
void* buf, size_t* num_bytes)
{
status_t err = B_ERROR;
my_device_info *my_dev;
usbmidi_device_info* my_dev;
assert(cookie != NULL);
my_dev = cookie->my_dev;
@@ -379,6 +438,8 @@ usb_midi_read(driver_cookie *cookie, off_t position,
if (!my_dev->active)
return B_ERROR; /* already unplugged */
DPRINTF_INFO((MY_ID "MIDI read (%d byte buffer at %ld cookie %p)\n",
*num_bytes, (int32)position, cookie));
err = acquire_sem_etc(my_dev->sem_cb, 1, B_CAN_INTERRUPT, 0LL);
if (err != B_OK)
return err;
@@ -386,24 +447,104 @@ usb_midi_read(driver_cookie *cookie, off_t position,
ring_buffer_user_read(my_dev->rbuf, buf, 1);
release_sem(my_dev->sem_lock);
*num_bytes = 1;
DPRINTF_INFO((MY_ID "read byte %x -- cookie %p)\n", *(uint8*)buf, cookie));
return err;
}
/* ----------
/*
usb_midi_write - handle write() calls
----- */
*/
const uint8 CINcode[] = { /* see USB-MIDI Spec */
0x4, /* 0x0 - sysex start */
0, /* 0x1 -- undefined */
0x3, /* 0x2 -- song pos */
0x2, /* 0x3 -- song select */
0, /* 0x4 -- undefined */
0, /* 0x5 -- undefined */
0x2, /* 0x6 -- tune request */
0x5, /* 0x7 -- sysex end */
0x5, /* 0x8 -- clock */
0, /* 0x9 -- undefined */
0x5, /* 0xA -- start */
0x5, /* 0xB -- continue */
0x5, /* 0xC -- stop */
0, /* 0xD -- undefined */
0x5, /* 0xE -- active sensing */
0x5, /* 0x0 -- system reset */
};
static status_t
usb_midi_write(driver_cookie* cookie, off_t position,
const void* buf, size_t* num_bytes)
{
usbmidi_device_info* my_dev;
uint8* midiseq = (uint8*)buf;
uint8 midicode = midiseq[0]; /* preserved for reference */
status_t st;
size_t bytes_left = *num_bytes;
size_t buff_lim;
uint8 cin = ((midicode & 0xF0) == 0xF0) ? CINcode[midicode & 0x0F]
: (midicode >> 4);
assert(cookie != NULL);
my_dev = cookie->my_dev;
assert(my_dev != NULL);
if (!my_dev->active)
return B_ERROR; /* already unplugged */
buff_lim = my_dev->buffer_size * 3 / 4; /* max MIDI bytes buffer space */
DPRINTF_INFO((MY_ID "MIDI write (%d bytes at %ld)\n", *num_bytes, position));
if (*num_bytes > 3 && midicode != 0xF0) {
DPRINTF_ERR((MY_ID "Non-SysEx packet of %ld bytes -- too big to handle\n",
*num_bytes));
return B_ERROR;
}
/* ----------
while (bytes_left) {
size_t xfer_bytes = (bytes_left < buff_lim)? bytes_left : buff_lim;
usb_midi_event_packet* pkt = my_dev->out_buffer;
int packet_count = 0;
st = acquire_sem_etc(my_dev->sem_send, 1, B_RELATIVE_TIMEOUT, 2000000LL);
if (st != B_OK)
return st;
while (xfer_bytes) {
uint8 pkt_bytes = CINbytes[cin];
memset(pkt, 0, sizeof(usb_midi_event_packet));
pkt->cin = cin;
DPRINTF_INFO((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);
DPRINTF_INFO((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]));
xfer_bytes -= pkt_bytes;
bytes_left -= pkt_bytes;
midiseq += pkt_bytes;
packet_count++;
pkt++;
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,
sizeof(usb_midi_event_packet) * packet_count,
(usb_callback_func)midi_usb_write_callback, my_dev);
if (st != B_OK) {
DPRINTF_ERR((MY_ID "midi write queue_bulk() error %d\n", (int)st));
return B_ERROR;
}
}
return B_OK;
}
/*
usb_midi_control - handle ioctl calls
----- */
*/
static status_t
usb_midi_control(void* cookie, uint32 iop,
@@ -412,14 +553,15 @@ usb_midi_control(void * cookie, uint32 iop,
return B_ERROR;
}
/* ----------
/*
usb_midi_close - handle close() calls
----- */
*/
static status_t
usb_midi_close(driver_cookie* cookie)
{
my_device_info *my_dev;
usbmidi_device_info* my_dev;
assert(cookie != NULL && cookie->my_dev != NULL);
my_dev = cookie->my_dev;
@@ -446,14 +588,15 @@ usb_midi_close(driver_cookie *cookie)
}
/* -----
/*
usb_midi_free - called after the last device is closed, and after
all i/o is complete.
----- */
*/
static status_t
usb_midi_free(driver_cookie* cookie)
{
my_device_info *my_dev;
usbmidi_device_info* my_dev;
assert(cookie != NULL && cookie->my_dev != NULL);
my_dev = cookie->my_dev;
@@ -471,9 +614,9 @@ usb_midi_free(driver_cookie *cookie)
}
/* -----
/*
function pointers for the device hooks entry points
----- */
*/
static device_hooks usb_midi_hooks = {
(device_open_hook)usb_midi_open,
@@ -486,9 +629,10 @@ static device_hooks usb_midi_hooks = {
};
/* ----------
/*
init_hardware - called once the first time the driver is loaded
----- */
*/
_EXPORT status_t
init_hardware(void)
{
@@ -496,10 +640,12 @@ init_hardware (void)
return B_OK;
}
/* ----------
/*
init_driver - optional function - called every time the driver
is loaded.
----- */
*/
_EXPORT status_t
init_driver(void)
{
@@ -509,9 +655,9 @@ init_driver (void)
if (get_module(B_USB_MODULE_NAME, (module_info**)&usb) != B_OK)
return B_ERROR;
if ((my_device_list_lock = create_sem (1, "dev_list_lock")) < 0) {
if ((usbmidi_device_list_lock = create_sem(1, "dev_list_lock")) < 0) {
put_module(B_USB_MODULE_NAME);
return my_device_list_lock; /* error code */
return usbmidi_device_list_lock; /* error code */
}
usb->register_driver(usb_midi_driver_name, my_supported_devices,
@@ -523,21 +669,23 @@ init_driver (void)
}
/* ----------
/*
uninit_driver - optional function - called every time the driver
is unloaded
----- */
*/
_EXPORT void
uninit_driver(void)
{
DPRINTF_INFO((MY_ID "uninit_driver()\n"));
usb->uninstall_notify(usb_midi_driver_name);
delete_sem(my_device_list_lock);
delete_sem(usbmidi_device_list_lock);
put_module(B_USB_MODULE_NAME);
free_device_names();
}
/*
publish_devices
device names are generated dynamically
@@ -548,21 +696,22 @@ publish_devices (void)
{
DPRINTF_INFO((MY_ID "publish_devices()\n"));
if (my_device_list_changed) {
if (usbmidi_device_list_changed) {
free_device_names();
alloc_device_names();
if (my_device_names != NULL)
if (usbmidi_device_names != NULL)
rebuild_device_names();
my_device_list_changed = false;
usbmidi_device_list_changed = false;
}
assert (my_device_names != NULL);
return (const char **) my_device_names;
assert(usbmidi_device_names != NULL);
return (const char**)usbmidi_device_names;
}
/* ----------
/*
find_device - return ptr to device hooks structure for a
given device name
----- */
*/
_EXPORT device_hooks*
find_device(const char* name)
@@ -1,34 +1,21 @@
/*****************************************************************************/
// midi usb driver
// Written by Jérôme Duval
//
// usb_midi.h
//
// Copyright (c) 2006 Haiku Project
//
// Some portions of code are copyrighted by
// USB Joystick driver for BeOS R5
// Copyright 2000 (C) ITO, Takayuki
// All rights reserved
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
/*
* midi usb driver
* usb_midi.h
*
* Copyright 2009 Haiku Inc. All rights reserved.
* Distributed under tthe terms of the MIT Licence.
*
* Authors:
* Jérôme Duval
* Pete Goodeve, pete.goodeve@computer.org
*
* Some portions of this code were originally derived from
* USB Joystick driver for BeOS R5
* Copyright 2000 (C) ITO, Takayuki
* All rights reserved
*
*/
#include <Drivers.h>
#include <USB.h>
@@ -36,14 +23,23 @@
#include "ring_buffer.h"
#define DEBUG 1
/* #define DEBUG 1 -- more convenient to define in the code file when needed */
#define DEBUG_ERR 1
#if DEBUG
#define DPRINTF_INFO(x) dprintf x
#define DPRINTF_ERR(x) dprintf x
#else
#define DPRINTF_INFO(x)
#define DPRINTF_ERR(x) dprintf x
#endif
#if DEBUG_ERR
#define DPRINTF_ERR(x) dprintf x
#else
#define DPRINTF_ERR(x)
#endif
/* For local finer debugging control: */
#define DPRINTF_INFOX(x) dprintf x
#define DPRINTF_INFOZ(x)
/* driver specific definitions */
@@ -55,23 +51,23 @@
#define assert(x) \
((x) ? 0 : dprintf(MY_ID "assertion failed at " __FILE__ ", line %d\n", __LINE__))
/* 0-origin */
#define DEFAULT_CONFIGURATION 0
#define BUF_SIZ B_PAGE_SIZE
struct driver_cookie;
typedef struct my_device_info
typedef struct usbmidi_device_info
{
/* list structure */
struct my_device_info *next;
struct usbmidi_device_info* next;
/* maintain device */
sem_id sem_cb;
sem_id sem_lock;
sem_id sem_send;
area_id buffer_area;
void *buffer;
usb_midi_event_packet* buffer; /* input buffer & base of area */
usb_midi_event_packet* out_buffer; /* above input buff */
const usb_device* dev;
uint16 ifno;
@@ -86,34 +82,37 @@ typedef struct my_device_info
/* work area for transfer */
int usbd_status, bus_status, cmd_status;
int actual_length;
const usb_endpoint_info *ept;
const usb_endpoint_info* ept_in;
const usb_endpoint_info* ept_out;
size_t total_report_size;
size_t buffer_size;
bigtime_t timestamp;
uint flags;
} my_device_info;
} usbmidi_device_info;
/* usb_midi.c */
extern usb_module_info* usb;
extern const char* usb_midi_base_name;
my_device_info *
usbmidi_device_info*
create_device(const usb_device* dev, const usb_interface_info* ii, uint16 ifno);
void
remove_device (my_device_info *my_dev);
remove_device(usbmidi_device_info* my_dev);
/* devlist.c */
extern sem_id my_device_list_lock;
extern bool my_device_list_changed;
extern sem_id usbmidi_device_list_lock;
extern bool usbmidi_device_list_changed;
void add_device_info (my_device_info *my_dev);
void remove_device_info (my_device_info *my_dev);
my_device_info *search_device_info (const char *name);
void add_device_info(usbmidi_device_info* my_dev);
void remove_device_info(usbmidi_device_info* my_dev);
usbmidi_device_info* search_device_info(const char* name);
extern char **my_device_names;
extern char** usbmidi_device_names;
void alloc_device_names(void);
void free_device_names(void);