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/>.
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.
38 struct usbio_endpoint_descriptor
43 uint8_t bEndpointAddress
;
44 uint16_t wMaxPacketSize
;
49 struct libusb_context
*usbctx
;
50 struct libusb_transfer
*transfer
;
53 gboolean cancel_confirm
;
54 const char *transfer_type
;
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
;
82 enum usb_sync_protocol_type sync_protocol
;
84 unsigned int iso_packets
, iso_packets_multimix
;
87 volatile gboolean stop_engine
, setup_error
, no_resubmit
;
90 uint64_t samples_played
;
92 struct usbio_transfer
**playback_transfers
;
93 struct usbio_transfer
**sync_transfers
;
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
;
103 uint32_t samples_per_sec
; // current estimated sample rate / 100
104 uint32_t audio_output_pktsize
;
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
;
128 gboolean is_midi
, is_audio
;
129 uint32_t configs_with_midi
;
130 uint32_t configs_with_audio
;
131 time_t last_probe_time
;
135 struct cbox_usb_audio_info
137 struct cbox_usb_device_info
*udi
;
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
;
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
;
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
)
230 if (libusb_kernel_driver_active(handle
, ifno
) == 1)
232 err
= libusb_detach_kernel_driver(handle
, ifno
);
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
));
239 err
= libusb_claim_interface(handle
, ifno
);
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
));
245 err
= altset
? libusb_set_interface_alt_setting(handle
, ifno
, altset
) : 0;
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
));