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 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
;
76 unsigned int iso_packets
, iso_packets_multimix
;
79 volatile gboolean stop_engine
, setup_error
, no_resubmit
;
82 uint64_t samples_played
;
84 struct usbio_transfer
**playback_transfers
;
85 struct usbio_transfer
**sync_transfers
;
90 struct cbox_midi_merger midi_input_merger
;
92 struct cbox_midi_buffer **midi_input_port_buffers;
93 int *midi_input_port_pos;
94 int midi_input_port_count;
96 void (*play_function
)(struct cbox_usb_io_impl
*uii
);
97 int8_t audio_output_endpoint
;
98 int8_t audio_sync_endpoint
;
100 uint32_t audio_output_pktsize
;
104 enum cbox_usb_device_status
106 CBOX_DEVICE_STATUS_PROBING
,
107 CBOX_DEVICE_STATUS_UNSUPPORTED
,
108 CBOX_DEVICE_STATUS_OPENED
,
111 struct cbox_usb_device_info
113 struct libusb_device
*dev
;
114 struct libusb_device_handle
*handle
;
115 enum cbox_usb_device_status status
;
124 gboolean is_midi
, is_audio
;
125 uint32_t configs_with_midi
;
126 uint32_t configs_with_audio
;
127 time_t last_probe_time
;
131 struct cbox_usb_audio_info
133 struct cbox_usb_device_info
*udi
;
136 struct usbio_endpoint_descriptor epdesc
;
139 enum usb_midi_protocol_type
141 USBMIDI_PROTOCOL_CLASS
,
142 USBMIDI_PROTOCOL_MPD16
,
143 USBMIDI_PROTOCOL_NOCTURN
,
146 struct cbox_usb_midi_info
148 struct cbox_usb_device_info
*udi
;
151 struct usbio_endpoint_descriptor epdesc_in
;
152 struct usbio_endpoint_descriptor epdesc_out
;
153 enum usb_midi_protocol_type protocol
;
156 #define MAX_MIDI_PACKET_SIZE 256
157 #define MAX_SYSEX_SIZE CBOX_MIDI_MAX_LONG_DATA
159 struct cbox_usb_midi_interface
161 struct cbox_usb_io_impl
*uii
;
162 struct cbox_usb_device_info
*devinfo
;
163 struct libusb_device_handle
*handle
;
165 struct usbio_endpoint_descriptor epdesc_in
, epdesc_out
;
166 struct usbio_transfer
*transfer_in
, *transfer_out
;
167 struct cbox_midi_buffer midi_buffer
;
168 uint8_t midi_recv_data
[MAX_MIDI_PACKET_SIZE
];
169 uint8_t sysex_data
[MAX_SYSEX_SIZE
];
170 uint32_t current_sysex_length
;
171 enum usb_midi_protocol_type protocol
;
174 #define USB_MIDI_OUTPUT_QUEUE 256
176 struct cbox_usb_midi_output
178 struct cbox_midi_output hdr
;
179 struct cbox_usb_midi_interface
*ifptr
;
180 uint8_t endpoint_buffer
[USB_MIDI_OUTPUT_QUEUE
];
181 uint32_t endpoint_buffer_pos
;
184 extern struct usbio_transfer
*usbio_transfer_new(struct libusb_context
*usbctx
, const char *transfer_type
, int index
, int isopackets
, void *user_data
);
185 extern void usbio_transfer_shutdown(struct usbio_transfer
*xfer
);
186 extern int usbio_transfer_submit(struct usbio_transfer
*xfer
);
187 extern void usbio_transfer_destroy(struct usbio_transfer
*xfer
);
189 extern void cbox_usb_midi_info_init(struct cbox_usb_midi_info
*umi
, struct cbox_usb_device_info
*udi
);
190 extern void usbio_start_midi_capture(struct cbox_usb_io_impl
*uii
);
191 extern void usbio_stop_midi_capture(struct cbox_usb_io_impl
*uii
);
192 extern struct cbox_usb_midi_interface
*usbio_open_midi_interface(struct cbox_usb_io_impl
*uii
,
193 const struct cbox_usb_midi_info
*uminf
, struct libusb_device_handle
*handle
);
194 extern void usbio_send_midi_to_output(struct cbox_usb_midi_output
*umo
);
195 extern void usbio_fill_midi_output_buffer(struct cbox_usb_midi_output
*umo
);
197 extern void cbox_usb_audio_info_init(struct cbox_usb_audio_info
*uai
, struct cbox_usb_device_info
*udi
);
198 extern void usbio_start_audio_playback(struct cbox_usb_io_impl
*uii
);
199 extern void usbio_stop_audio_playback(struct cbox_usb_io_impl
*uii
);
200 extern gboolean
usbio_open_audio_interface(struct cbox_usb_io_impl
*uii
,
201 struct cbox_usb_audio_info
*uainf
, struct libusb_device_handle
*handle
, GError
**error
);
202 extern gboolean
usbio_open_audio_interface_multimix(struct cbox_usb_io_impl
*uii
, int bus
, int devadr
, struct libusb_device_handle
*handle
, GError
**error
);
204 extern void usbio_play_buffer_asynchronous(struct cbox_usb_io_impl
*uii
);
205 extern gboolean
usbio_scan_devices(struct cbox_usb_io_impl
*uii
, gboolean probe_only
);
207 extern void usbio_forget_device(struct cbox_usb_io_impl
*uii
, struct cbox_usb_device_info
*devinfo
);
208 extern void usbio_run_idle_loop(struct cbox_usb_io_impl
*uii
);
210 #define USB_DEVICE_SETUP_TIMEOUT 2000
212 static inline gboolean
configure_usb_interface(struct libusb_device_handle
*handle
, int bus
, int devadr
, int ifno
, int altset
, const char *purpose
, GError
**error
)
215 if (libusb_kernel_driver_active(handle
, ifno
) == 1)
217 err
= libusb_detach_kernel_driver(handle
, ifno
);
220 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
));
224 err
= libusb_claim_interface(handle
, ifno
);
227 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
));
230 err
= altset
? libusb_set_interface_alt_setting(handle
, ifno
, altset
) : 0;
233 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
));