Use linked list for storing sources in MIDI merger.
[calfbox.git] / usbio_impl.h
blobc3a137327d6eea01ff1ba35c4213a7068141ab41
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2013 Krzysztof Foltman
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 Note:
22 Those are private structures and functions for the USB driver implementation.
23 I'm not too happy with the organization of the code yet, it's just a first
24 try at breaking it up into manageable parts.
28 #ifndef USBIO_IMPL_H
29 #define USBIO_IMPL_H
31 #include <libusb.h>
33 #include "errors.h"
34 #include "io.h"
35 #include "midi.h"
36 #include "mididest.h"
38 struct usbio_endpoint_descriptor
40 uint8_t found:1;
41 uint8_t interrupt:1;
42 uint8_t reserved:6;
43 uint8_t bEndpointAddress;
44 uint16_t wMaxPacketSize;
47 struct usbio_transfer
49 struct libusb_context *usbctx;
50 struct libusb_transfer *transfer;
51 void *user_data;
52 gboolean pending;
53 gboolean cancel_confirm;
54 const char *transfer_type;
55 int index;
58 struct cbox_usb_io_impl
60 struct cbox_io_impl ioi;
62 struct libusb_context *usbctx;
63 struct libusb_context *usbctx_probe;
65 GHashTable *device_table;
67 struct libusb_device_handle *handle_audiodev;
68 int sample_rate, buffer_size, output_resolution;
69 int output_channels; // always 2 for now
71 unsigned int playback_buffers;
72 unsigned int sync_buffers;
73 int playback_counter;
74 int sync_counter;
76 unsigned int iso_packets, iso_packets_multimix;
78 pthread_t thr_engine;
79 volatile gboolean stop_engine, setup_error, no_resubmit;
81 int desync;
82 uint64_t samples_played;
83 int device_removed;
84 struct usbio_transfer **playback_transfers;
85 struct usbio_transfer **sync_transfers;
86 int read_ptr;
88 GList *midi_ports;
89 GList *rt_midi_ports;
90 struct cbox_midi_merger midi_input_merger;
91 void (*play_function)(struct cbox_usb_io_impl *uii);
92 int8_t audio_output_endpoint;
93 int8_t audio_sync_endpoint;
94 uint32_t sync_freq;
95 uint32_t audio_output_pktsize;
96 int debug_sync;
99 enum cbox_usb_device_status
101 CBOX_DEVICE_STATUS_PROBING,
102 CBOX_DEVICE_STATUS_UNSUPPORTED,
103 CBOX_DEVICE_STATUS_OPENED,
106 struct cbox_usb_device_info
108 struct libusb_device *dev;
109 struct libusb_device_handle *handle;
110 enum cbox_usb_device_status status;
111 uint8_t bus;
112 uint8_t devadr;
113 uint16_t busdevadr;
114 struct {
115 uint16_t vid;
116 uint16_t pid;
118 int active_config;
119 gboolean is_midi, is_audio;
120 uint32_t configs_with_midi;
121 uint32_t configs_with_audio;
122 time_t last_probe_time;
123 int failures;
126 struct cbox_usb_audio_info
128 struct cbox_usb_device_info *udi;
129 int intf;
130 int alt_setting;
131 struct usbio_endpoint_descriptor epdesc;
134 enum usb_midi_protocol_type
136 USBMIDI_PROTOCOL_CLASS,
137 USBMIDI_PROTOCOL_MPD16,
138 USBMIDI_PROTOCOL_NOCTURN,
141 struct cbox_usb_midi_info
143 struct cbox_usb_device_info *udi;
144 int intf;
145 int alt_setting;
146 struct usbio_endpoint_descriptor epdesc_in;
147 struct usbio_endpoint_descriptor epdesc_out;
148 enum usb_midi_protocol_type protocol;
151 #define MAX_MIDI_PACKET_SIZE 256
152 #define MAX_SYSEX_SIZE CBOX_MIDI_MAX_LONG_DATA
154 struct cbox_usb_midi_interface
156 struct cbox_usb_io_impl *uii;
157 struct cbox_usb_device_info *devinfo;
158 struct libusb_device_handle *handle;
159 int busdevadr;
160 struct usbio_endpoint_descriptor epdesc_in, epdesc_out;
161 struct usbio_transfer *transfer_in, *transfer_out;
162 uint8_t midi_recv_data[MAX_MIDI_PACKET_SIZE];
163 uint8_t sysex_data[MAX_SYSEX_SIZE];
164 uint32_t current_sysex_length;
165 enum usb_midi_protocol_type protocol;
166 struct cbox_usb_midi_input *input_port;
167 struct cbox_usb_midi_output *output_port;
170 #define USB_MIDI_OUTPUT_QUEUE 256
172 struct cbox_usb_midi_input
174 struct cbox_midi_input hdr;
175 struct cbox_usb_midi_interface *ifptr;
178 struct cbox_usb_midi_output
180 struct cbox_midi_output hdr;
181 struct cbox_usb_midi_interface *ifptr;
182 uint8_t endpoint_buffer[USB_MIDI_OUTPUT_QUEUE];
183 uint32_t endpoint_buffer_pos;
186 extern struct usbio_transfer *usbio_transfer_new(struct libusb_context *usbctx, const char *transfer_type, int index, int isopackets, void *user_data);
187 extern void usbio_transfer_shutdown(struct usbio_transfer *xfer);
188 extern int usbio_transfer_submit(struct usbio_transfer *xfer);
189 extern void usbio_transfer_destroy(struct usbio_transfer *xfer);
191 extern void cbox_usb_midi_info_init(struct cbox_usb_midi_info *umi, struct cbox_usb_device_info *udi);
192 extern void usbio_start_midi_capture(struct cbox_usb_io_impl *uii);
193 extern void usbio_stop_midi_capture(struct cbox_usb_io_impl *uii);
194 extern struct cbox_usb_midi_interface *usbio_open_midi_interface(struct cbox_usb_io_impl *uii,
195 const struct cbox_usb_midi_info *uminf, struct libusb_device_handle *handle);
196 extern void usbio_send_midi_to_output(struct cbox_usb_midi_output *umo);
197 extern void usbio_fill_midi_output_buffer(struct cbox_usb_midi_output *umo);
199 extern void cbox_usb_audio_info_init(struct cbox_usb_audio_info *uai, struct cbox_usb_device_info *udi);
200 extern void usbio_start_audio_playback(struct cbox_usb_io_impl *uii);
201 extern void usbio_stop_audio_playback(struct cbox_usb_io_impl *uii);
202 extern gboolean usbio_open_audio_interface(struct cbox_usb_io_impl *uii,
203 struct cbox_usb_audio_info *uainf, struct libusb_device_handle *handle, GError **error);
204 extern gboolean usbio_open_audio_interface_multimix(struct cbox_usb_io_impl *uii, int bus, int devadr, struct libusb_device_handle *handle, GError **error);
205 extern void usbio_update_port_routing(struct cbox_io_impl *ioi);
208 extern void usbio_play_buffer_asynchronous(struct cbox_usb_io_impl *uii);
209 extern gboolean usbio_scan_devices(struct cbox_usb_io_impl *uii, gboolean probe_only);
211 extern void usbio_forget_device(struct cbox_usb_io_impl *uii, struct cbox_usb_device_info *devinfo);
212 extern void usbio_run_idle_loop(struct cbox_usb_io_impl *uii);
214 #define USB_DEVICE_SETUP_TIMEOUT 2000
216 static inline gboolean configure_usb_interface(struct libusb_device_handle *handle, int bus, int devadr, int ifno, int altset, const char *purpose, GError **error)
218 int err = 0;
219 if (libusb_kernel_driver_active(handle, ifno) == 1)
221 err = libusb_detach_kernel_driver(handle, ifno);
222 if (err)
224 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Cannot detach kernel driver from interface %d on device %03d:%03d: %s. Please rmmod snd-usb-audio as root.", ifno, bus, devadr, libusb_error_name(err));
225 return FALSE;
228 err = libusb_claim_interface(handle, ifno);
229 if (err)
231 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Cannot claim interface %d on device %03d:%03d for %s: %s", ifno, bus, devadr, purpose, libusb_error_name(err));
232 return FALSE;
234 err = altset ? libusb_set_interface_alt_setting(handle, ifno, altset) : 0;
235 if (err)
237 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Cannot set alt-setting %d for interface %d on device %03d:%03d for %s: %s", altset, ifno, bus, devadr, purpose, libusb_error_name(err));
238 return FALSE;
240 return TRUE;
244 #endif