Implement USB Audio class asynchronous sink support.
[calfbox.git] / usbio_impl.h
blobc746494707032d5941ce0047dfc7f41746b4855f
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 enum usb_sync_protocol_type
60 USBAUDIOSYNC_PROTOCOL_NONE = 0,
61 USBAUDIOSYNC_PROTOCOL_CLASS,
62 USBAUDIOSYNC_PROTOCOL_MULTIMIX8,
65 struct cbox_usb_io_impl
67 struct cbox_io_impl ioi;
69 struct libusb_context *usbctx;
70 struct libusb_context *usbctx_probe;
72 GHashTable *device_table;
74 struct libusb_device_handle *handle_audiodev;
75 int sample_rate, buffer_size, output_resolution;
76 int output_channels; // always 2 for now
78 unsigned int playback_buffers;
79 unsigned int sync_buffers;
80 int playback_counter;
81 int sync_counter;
82 enum usb_sync_protocol_type sync_protocol;
84 unsigned int iso_packets, iso_packets_multimix;
86 pthread_t thr_engine;
87 volatile gboolean stop_engine, setup_error, no_resubmit;
89 int desync;
90 uint64_t samples_played;
91 int device_removed;
92 struct usbio_transfer **playback_transfers;
93 struct usbio_transfer **sync_transfers;
94 int read_ptr;
96 GList *midi_ports;
97 GList *rt_midi_ports;
98 struct cbox_midi_merger midi_input_merger;
99 void (*play_function)(struct cbox_usb_io_impl *uii);
100 int8_t audio_output_endpoint;
101 int8_t audio_sync_endpoint;
102 gboolean is_hispeed;
103 uint32_t samples_per_sec; // current estimated sample rate / 100
104 uint32_t audio_output_pktsize;
105 int debug_sync;
108 enum cbox_usb_device_status
110 CBOX_DEVICE_STATUS_PROBING,
111 CBOX_DEVICE_STATUS_UNSUPPORTED,
112 CBOX_DEVICE_STATUS_OPENED,
115 struct cbox_usb_device_info
117 struct libusb_device *dev;
118 struct libusb_device_handle *handle;
119 enum cbox_usb_device_status status;
120 uint8_t bus;
121 uint8_t devadr;
122 uint16_t busdevadr;
123 struct {
124 uint16_t vid;
125 uint16_t pid;
127 int active_config;
128 gboolean is_midi, is_audio;
129 uint32_t configs_with_midi;
130 uint32_t configs_with_audio;
131 time_t last_probe_time;
132 int failures;
135 struct cbox_usb_audio_info
137 struct cbox_usb_device_info *udi;
138 int intf;
139 int alt_setting;
140 struct usbio_endpoint_descriptor epdesc;
141 enum usb_sync_protocol_type sync_protocol;
142 struct usbio_endpoint_descriptor sync_epdesc;
145 enum usb_midi_protocol_type
147 USBMIDI_PROTOCOL_CLASS,
148 USBMIDI_PROTOCOL_MPD16,
149 USBMIDI_PROTOCOL_NOCTURN,
152 struct cbox_usb_midi_info
154 struct cbox_usb_device_info *udi;
155 int intf;
156 int alt_setting;
157 struct usbio_endpoint_descriptor epdesc_in;
158 struct usbio_endpoint_descriptor epdesc_out;
159 enum usb_midi_protocol_type protocol;
162 #define MAX_MIDI_PACKET_SIZE 256
163 #define MAX_SYSEX_SIZE CBOX_MIDI_MAX_LONG_DATA
165 struct cbox_usb_midi_interface
167 struct cbox_usb_io_impl *uii;
168 struct cbox_usb_device_info *devinfo;
169 struct libusb_device_handle *handle;
170 int busdevadr;
171 struct usbio_endpoint_descriptor epdesc_in, epdesc_out;
172 struct usbio_transfer *transfer_in, *transfer_out;
173 uint8_t midi_recv_data[MAX_MIDI_PACKET_SIZE];
174 uint8_t sysex_data[MAX_SYSEX_SIZE];
175 uint32_t current_sysex_length;
176 enum usb_midi_protocol_type protocol;
177 struct cbox_usb_midi_input *input_port;
178 struct cbox_usb_midi_output *output_port;
181 #define USB_MIDI_OUTPUT_QUEUE 256
183 struct cbox_usb_midi_input
185 struct cbox_midi_input hdr;
186 struct cbox_usb_midi_interface *ifptr;
189 struct cbox_usb_midi_output
191 struct cbox_midi_output hdr;
192 struct cbox_usb_midi_interface *ifptr;
193 uint8_t endpoint_buffer[USB_MIDI_OUTPUT_QUEUE];
194 uint32_t endpoint_buffer_pos;
197 extern struct usbio_transfer *usbio_transfer_new(struct libusb_context *usbctx, const char *transfer_type, int index, int isopackets, void *user_data);
198 extern void usbio_transfer_shutdown(struct usbio_transfer *xfer);
199 extern int usbio_transfer_submit(struct usbio_transfer *xfer);
200 extern void usbio_transfer_destroy(struct usbio_transfer *xfer);
202 extern void cbox_usb_midi_info_init(struct cbox_usb_midi_info *umi, struct cbox_usb_device_info *udi);
203 extern void usbio_start_midi_capture(struct cbox_usb_io_impl *uii);
204 extern void usbio_stop_midi_capture(struct cbox_usb_io_impl *uii);
205 extern struct cbox_usb_midi_interface *usbio_open_midi_interface(struct cbox_usb_io_impl *uii,
206 const struct cbox_usb_midi_info *uminf, struct libusb_device_handle *handle);
207 extern void usbio_send_midi_to_output(struct cbox_usb_midi_output *umo);
208 extern void usbio_fill_midi_output_buffer(struct cbox_usb_midi_output *umo);
210 extern void cbox_usb_audio_info_init(struct cbox_usb_audio_info *uai, struct cbox_usb_device_info *udi);
211 extern void usbio_start_audio_playback(struct cbox_usb_io_impl *uii);
212 extern void usbio_stop_audio_playback(struct cbox_usb_io_impl *uii);
213 extern gboolean usbio_open_audio_interface(struct cbox_usb_io_impl *uii,
214 struct cbox_usb_audio_info *uainf, struct libusb_device_handle *handle, GError **error);
215 extern gboolean usbio_open_audio_interface_multimix(struct cbox_usb_io_impl *uii, int bus, int devadr, struct libusb_device_handle *handle, GError **error);
216 extern void usbio_update_port_routing(struct cbox_io_impl *ioi);
219 extern void usbio_play_buffer_asynchronous(struct cbox_usb_io_impl *uii);
220 extern gboolean usbio_scan_devices(struct cbox_usb_io_impl *uii, gboolean probe_only);
222 extern void usbio_forget_device(struct cbox_usb_io_impl *uii, struct cbox_usb_device_info *devinfo);
223 extern void usbio_run_idle_loop(struct cbox_usb_io_impl *uii);
225 #define USB_DEVICE_SETUP_TIMEOUT 2000
227 static inline gboolean configure_usb_interface(struct libusb_device_handle *handle, int bus, int devadr, int ifno, int altset, const char *purpose, GError **error)
229 int err = 0;
230 if (libusb_kernel_driver_active(handle, ifno) == 1)
232 err = libusb_detach_kernel_driver(handle, ifno);
233 if (err)
235 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));
236 return FALSE;
239 err = libusb_claim_interface(handle, ifno);
240 if (err)
242 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));
243 return FALSE;
245 err = altset ? libusb_set_interface_alt_setting(handle, ifno, altset) : 0;
246 if (err)
248 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));
249 return FALSE;
251 return TRUE;
255 #endif