Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 1 | /* |
| 2 | * bebob_stream.c - a part of driver for BeBoB based devices |
| 3 | * |
| 4 | * Copyright (c) 2013-2014 Takashi Sakamoto |
| 5 | * |
| 6 | * Licensed under the terms of the GNU General Public License, version 2. |
| 7 | */ |
| 8 | |
| 9 | #include "./bebob.h" |
| 10 | |
Takashi Sakamoto | 9a73195 | 2015-06-14 12:49:34 +0900 | [diff] [blame] | 11 | #define CALLBACK_TIMEOUT 2000 |
Takashi Sakamoto | b6bc812 | 2014-04-25 22:45:16 +0900 | [diff] [blame] | 12 | #define FW_ISO_RESOURCE_DELAY 1000 |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 13 | |
| 14 | /* |
| 15 | * NOTE; |
| 16 | * For BeBoB streams, Both of input and output CMP connection are important. |
| 17 | * |
| 18 | * For most devices, each CMP connection starts to transmit/receive a |
| 19 | * corresponding stream. But for a few devices, both of CMP connection needs |
| 20 | * to start transmitting stream. An example is 'M-Audio Firewire 410'. |
| 21 | */ |
| 22 | |
| 23 | /* 128 is an arbitrary length but it seems to be enough */ |
| 24 | #define FORMAT_MAXIMUM_LENGTH 128 |
| 25 | |
| 26 | const unsigned int snd_bebob_rate_table[SND_BEBOB_STRM_FMT_ENTRIES] = { |
| 27 | [0] = 32000, |
| 28 | [1] = 44100, |
| 29 | [2] = 48000, |
| 30 | [3] = 88200, |
| 31 | [4] = 96000, |
| 32 | [5] = 176400, |
| 33 | [6] = 192000, |
| 34 | }; |
| 35 | |
| 36 | /* |
| 37 | * See: Table 51: Extended Stream Format Info ‘Sampling Frequency’ |
| 38 | * in Additional AVC commands (Nov 2003, BridgeCo) |
| 39 | */ |
| 40 | static const unsigned int bridgeco_freq_table[] = { |
| 41 | [0] = 0x02, |
| 42 | [1] = 0x03, |
| 43 | [2] = 0x04, |
| 44 | [3] = 0x0a, |
| 45 | [4] = 0x05, |
| 46 | [5] = 0x06, |
| 47 | [6] = 0x07, |
| 48 | }; |
| 49 | |
Lucas Tanure | 0790529 | 2016-01-25 19:30:23 -0200 | [diff] [blame] | 50 | static int |
| 51 | get_formation_index(unsigned int rate, unsigned int *index) |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 52 | { |
| 53 | unsigned int i; |
| 54 | |
| 55 | for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) { |
Lucas Tanure | 0790529 | 2016-01-25 19:30:23 -0200 | [diff] [blame] | 56 | if (snd_bebob_rate_table[i] == rate) { |
| 57 | *index = i; |
| 58 | return 0; |
| 59 | } |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 60 | } |
| 61 | return -EINVAL; |
| 62 | } |
| 63 | |
| 64 | int |
| 65 | snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *curr_rate) |
| 66 | { |
| 67 | unsigned int tx_rate, rx_rate, trials; |
| 68 | int err; |
| 69 | |
| 70 | trials = 0; |
| 71 | do { |
| 72 | err = avc_general_get_sig_fmt(bebob->unit, &tx_rate, |
| 73 | AVC_GENERAL_PLUG_DIR_OUT, 0); |
| 74 | } while (err == -EAGAIN && ++trials < 3); |
| 75 | if (err < 0) |
| 76 | goto end; |
| 77 | |
| 78 | trials = 0; |
| 79 | do { |
| 80 | err = avc_general_get_sig_fmt(bebob->unit, &rx_rate, |
| 81 | AVC_GENERAL_PLUG_DIR_IN, 0); |
| 82 | } while (err == -EAGAIN && ++trials < 3); |
| 83 | if (err < 0) |
| 84 | goto end; |
| 85 | |
| 86 | *curr_rate = rx_rate; |
| 87 | if (rx_rate == tx_rate) |
| 88 | goto end; |
| 89 | |
| 90 | /* synchronize receive stream rate to transmit stream rate */ |
| 91 | err = avc_general_set_sig_fmt(bebob->unit, rx_rate, |
| 92 | AVC_GENERAL_PLUG_DIR_IN, 0); |
| 93 | end: |
| 94 | return err; |
| 95 | } |
| 96 | |
| 97 | int |
| 98 | snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate) |
| 99 | { |
| 100 | int err; |
| 101 | |
| 102 | err = avc_general_set_sig_fmt(bebob->unit, rate, |
| 103 | AVC_GENERAL_PLUG_DIR_OUT, 0); |
| 104 | if (err < 0) |
| 105 | goto end; |
| 106 | |
| 107 | err = avc_general_set_sig_fmt(bebob->unit, rate, |
| 108 | AVC_GENERAL_PLUG_DIR_IN, 0); |
| 109 | if (err < 0) |
| 110 | goto end; |
| 111 | |
| 112 | /* |
| 113 | * Some devices need a bit time for transition. |
| 114 | * 300msec is got by some experiments. |
| 115 | */ |
| 116 | msleep(300); |
| 117 | end: |
| 118 | return err; |
| 119 | } |
| 120 | |
Takashi Sakamoto | 3e254b1 | 2015-06-14 12:49:30 +0900 | [diff] [blame] | 121 | int snd_bebob_stream_get_clock_src(struct snd_bebob *bebob, |
| 122 | enum snd_bebob_clock_type *src) |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 123 | { |
Julia Lawall | 6b9866c | 2015-10-11 08:10:55 +0200 | [diff] [blame] | 124 | const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 125 | u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7]; |
Takashi Sakamoto | 1fc9522 | 2014-04-25 22:45:21 +0900 | [diff] [blame] | 126 | unsigned int id; |
Takashi Sakamoto | 5a66881 | 2015-06-14 12:49:27 +0900 | [diff] [blame] | 127 | enum avc_bridgeco_plug_type type; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 128 | int err = 0; |
| 129 | |
Takashi Sakamoto | 1fc9522 | 2014-04-25 22:45:21 +0900 | [diff] [blame] | 130 | /* 1.The device has its own operation to switch source of clock */ |
| 131 | if (clk_spec) { |
| 132 | err = clk_spec->get(bebob, &id); |
Christian Vogel | d1d0b6b | 2014-10-25 13:40:41 +0200 | [diff] [blame] | 133 | if (err < 0) { |
Takashi Sakamoto | 1fc9522 | 2014-04-25 22:45:21 +0900 | [diff] [blame] | 134 | dev_err(&bebob->unit->device, |
| 135 | "fail to get clock source: %d\n", err); |
Christian Vogel | d1d0b6b | 2014-10-25 13:40:41 +0200 | [diff] [blame] | 136 | goto end; |
| 137 | } |
| 138 | |
| 139 | if (id >= clk_spec->num) { |
| 140 | dev_err(&bebob->unit->device, |
| 141 | "clock source %d out of range 0..%d\n", |
| 142 | id, clk_spec->num - 1); |
| 143 | err = -EIO; |
| 144 | goto end; |
| 145 | } |
| 146 | |
Takashi Sakamoto | 3e254b1 | 2015-06-14 12:49:30 +0900 | [diff] [blame] | 147 | *src = clk_spec->types[id]; |
Takashi Sakamoto | 1fc9522 | 2014-04-25 22:45:21 +0900 | [diff] [blame] | 148 | goto end; |
| 149 | } |
| 150 | |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 151 | /* |
Takashi Sakamoto | 1fc9522 | 2014-04-25 22:45:21 +0900 | [diff] [blame] | 152 | * 2.The device don't support to switch source of clock then assumed |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 153 | * to use internal clock always |
| 154 | */ |
| 155 | if (bebob->sync_input_plug < 0) { |
Takashi Sakamoto | 3e254b1 | 2015-06-14 12:49:30 +0900 | [diff] [blame] | 156 | *src = SND_BEBOB_CLOCK_TYPE_INTERNAL; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 157 | goto end; |
| 158 | } |
| 159 | |
| 160 | /* |
Takashi Sakamoto | 1fc9522 | 2014-04-25 22:45:21 +0900 | [diff] [blame] | 161 | * 3.The device supports to switch source of clock by an usual way. |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 162 | * Let's check input for 'Music Sub Unit Sync Input' plug. |
| 163 | */ |
| 164 | avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, |
| 165 | bebob->sync_input_plug); |
| 166 | err = avc_bridgeco_get_plug_input(bebob->unit, addr, input); |
| 167 | if (err < 0) { |
| 168 | dev_err(&bebob->unit->device, |
| 169 | "fail to get an input for MSU in plug %d: %d\n", |
| 170 | bebob->sync_input_plug, err); |
| 171 | goto end; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * If there are no input plugs, all of fields are 0xff. |
| 176 | * Here check the first field. This field is used for direction. |
| 177 | */ |
| 178 | if (input[0] == 0xff) { |
Takashi Sakamoto | 3e254b1 | 2015-06-14 12:49:30 +0900 | [diff] [blame] | 179 | *src = SND_BEBOB_CLOCK_TYPE_INTERNAL; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 180 | goto end; |
| 181 | } |
| 182 | |
Takashi Sakamoto | 5a66881 | 2015-06-14 12:49:27 +0900 | [diff] [blame] | 183 | /* The source from any output plugs is for one purpose only. */ |
| 184 | if (input[0] == AVC_BRIDGECO_PLUG_DIR_OUT) { |
| 185 | /* |
| 186 | * In BeBoB architecture, the source from music subunit may |
| 187 | * bypass from oPCR[0]. This means that this source gives |
| 188 | * synchronization to IEEE 1394 cycle start packet. |
| 189 | */ |
| 190 | if (input[1] == AVC_BRIDGECO_PLUG_MODE_SUBUNIT && |
| 191 | input[2] == 0x0c) { |
Takashi Sakamoto | 3e254b1 | 2015-06-14 12:49:30 +0900 | [diff] [blame] | 192 | *src = SND_BEBOB_CLOCK_TYPE_INTERNAL; |
Takashi Sakamoto | 5a66881 | 2015-06-14 12:49:27 +0900 | [diff] [blame] | 193 | goto end; |
| 194 | } |
| 195 | /* The source from any input units is for several purposes. */ |
| 196 | } else if (input[1] == AVC_BRIDGECO_PLUG_MODE_UNIT) { |
| 197 | if (input[2] == AVC_BRIDGECO_PLUG_UNIT_ISOC) { |
| 198 | if (input[3] == 0x00) { |
| 199 | /* |
| 200 | * This source comes from iPCR[0]. This means |
| 201 | * that presentation timestamp calculated by |
| 202 | * SYT series of the received packets. In |
| 203 | * short, this driver is the master of |
| 204 | * synchronization. |
| 205 | */ |
Takashi Sakamoto | 3e254b1 | 2015-06-14 12:49:30 +0900 | [diff] [blame] | 206 | *src = SND_BEBOB_CLOCK_TYPE_SYT; |
Takashi Sakamoto | 5a66881 | 2015-06-14 12:49:27 +0900 | [diff] [blame] | 207 | goto end; |
| 208 | } else { |
| 209 | /* |
| 210 | * This source comes from iPCR[1-29]. This |
| 211 | * means that the synchronization stream is not |
| 212 | * the Audio/MIDI compound stream. |
| 213 | */ |
Takashi Sakamoto | 3e254b1 | 2015-06-14 12:49:30 +0900 | [diff] [blame] | 214 | *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL; |
Takashi Sakamoto | 5a66881 | 2015-06-14 12:49:27 +0900 | [diff] [blame] | 215 | goto end; |
| 216 | } |
| 217 | } else if (input[2] == AVC_BRIDGECO_PLUG_UNIT_EXT) { |
| 218 | /* Check type of this plug. */ |
| 219 | avc_bridgeco_fill_unit_addr(addr, |
| 220 | AVC_BRIDGECO_PLUG_DIR_IN, |
| 221 | AVC_BRIDGECO_PLUG_UNIT_EXT, |
| 222 | input[3]); |
| 223 | err = avc_bridgeco_get_plug_type(bebob->unit, addr, |
| 224 | &type); |
| 225 | if (err < 0) |
| 226 | goto end; |
| 227 | |
| 228 | if (type == AVC_BRIDGECO_PLUG_TYPE_DIG) { |
| 229 | /* |
| 230 | * SPDIF/ADAT or sometimes (not always) word |
| 231 | * clock. |
| 232 | */ |
Takashi Sakamoto | 3e254b1 | 2015-06-14 12:49:30 +0900 | [diff] [blame] | 233 | *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL; |
Takashi Sakamoto | 5a66881 | 2015-06-14 12:49:27 +0900 | [diff] [blame] | 234 | goto end; |
| 235 | } else if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) { |
| 236 | /* Often word clock. */ |
Takashi Sakamoto | 3e254b1 | 2015-06-14 12:49:30 +0900 | [diff] [blame] | 237 | *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL; |
Takashi Sakamoto | 5a66881 | 2015-06-14 12:49:27 +0900 | [diff] [blame] | 238 | goto end; |
| 239 | } else if (type == AVC_BRIDGECO_PLUG_TYPE_ADDITION) { |
| 240 | /* |
| 241 | * Not standard. |
| 242 | * Mostly, additional internal clock. |
| 243 | */ |
Takashi Sakamoto | 3e254b1 | 2015-06-14 12:49:30 +0900 | [diff] [blame] | 244 | *src = SND_BEBOB_CLOCK_TYPE_INTERNAL; |
Takashi Sakamoto | 5a66881 | 2015-06-14 12:49:27 +0900 | [diff] [blame] | 245 | goto end; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* Not supported. */ |
| 251 | err = -EIO; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 252 | end: |
| 253 | return err; |
| 254 | } |
| 255 | |
Takashi Sakamoto | 9bb93ca | 2019-10-26 12:06:20 +0900 | [diff] [blame] | 256 | static int map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s) |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 257 | { |
| 258 | unsigned int sec, sections, ch, channels; |
| 259 | unsigned int pcm, midi, location; |
| 260 | unsigned int stm_pos, sec_loc, pos; |
| 261 | u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type; |
| 262 | enum avc_bridgeco_plug_dir dir; |
| 263 | int err; |
| 264 | |
| 265 | /* |
| 266 | * The length of return value of this command cannot be expected. Here |
| 267 | * use the maximum length of FCP. |
| 268 | */ |
| 269 | buf = kzalloc(256, GFP_KERNEL); |
| 270 | if (buf == NULL) |
| 271 | return -ENOMEM; |
| 272 | |
| 273 | if (s == &bebob->tx_stream) |
| 274 | dir = AVC_BRIDGECO_PLUG_DIR_OUT; |
| 275 | else |
| 276 | dir = AVC_BRIDGECO_PLUG_DIR_IN; |
| 277 | |
| 278 | avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0); |
| 279 | err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256); |
| 280 | if (err < 0) { |
| 281 | dev_err(&bebob->unit->device, |
| 282 | "fail to get channel position for isoc %s plug 0: %d\n", |
| 283 | (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out", |
| 284 | err); |
| 285 | goto end; |
| 286 | } |
| 287 | pos = 0; |
| 288 | |
| 289 | /* positions in I/O buffer */ |
| 290 | pcm = 0; |
| 291 | midi = 0; |
| 292 | |
| 293 | /* the number of sections in AMDTP packet */ |
| 294 | sections = buf[pos++]; |
| 295 | |
| 296 | for (sec = 0; sec < sections; sec++) { |
| 297 | /* type of this section */ |
| 298 | avc_bridgeco_fill_unit_addr(addr, dir, |
| 299 | AVC_BRIDGECO_PLUG_UNIT_ISOC, 0); |
| 300 | err = avc_bridgeco_get_plug_section_type(bebob->unit, addr, |
| 301 | sec, &type); |
| 302 | if (err < 0) { |
| 303 | dev_err(&bebob->unit->device, |
| 304 | "fail to get section type for isoc %s plug 0: %d\n", |
| 305 | (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : |
| 306 | "out", |
| 307 | err); |
| 308 | goto end; |
| 309 | } |
| 310 | /* NoType */ |
| 311 | if (type == 0xff) { |
| 312 | err = -ENOSYS; |
| 313 | goto end; |
| 314 | } |
| 315 | |
| 316 | /* the number of channels in this section */ |
| 317 | channels = buf[pos++]; |
| 318 | |
| 319 | for (ch = 0; ch < channels; ch++) { |
| 320 | /* position of this channel in AMDTP packet */ |
| 321 | stm_pos = buf[pos++] - 1; |
| 322 | /* location of this channel in this section */ |
| 323 | sec_loc = buf[pos++] - 1; |
| 324 | |
Takashi Sakamoto | 9076c22 | 2014-04-25 22:45:25 +0900 | [diff] [blame] | 325 | /* |
| 326 | * Basically the number of location is within the |
| 327 | * number of channels in this section. But some models |
| 328 | * of M-Audio don't follow this. Its location for MIDI |
| 329 | * is the position of MIDI channels in AMDTP packet. |
| 330 | */ |
| 331 | if (sec_loc >= channels) |
| 332 | sec_loc = ch; |
| 333 | |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 334 | switch (type) { |
| 335 | /* for MIDI conformant data channel */ |
| 336 | case 0x0a: |
| 337 | /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */ |
| 338 | if ((midi > 0) && (stm_pos != midi)) { |
| 339 | err = -ENOSYS; |
| 340 | goto end; |
| 341 | } |
Takashi Sakamoto | f65be91 | 2015-09-19 11:21:58 +0900 | [diff] [blame] | 342 | amdtp_am824_set_midi_position(s, stm_pos); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 343 | midi = stm_pos; |
| 344 | break; |
| 345 | /* for PCM data channel */ |
| 346 | case 0x01: /* Headphone */ |
| 347 | case 0x02: /* Microphone */ |
| 348 | case 0x03: /* Line */ |
| 349 | case 0x04: /* SPDIF */ |
| 350 | case 0x05: /* ADAT */ |
| 351 | case 0x06: /* TDIF */ |
| 352 | case 0x07: /* MADI */ |
| 353 | /* for undefined/changeable signal */ |
| 354 | case 0x08: /* Analog */ |
| 355 | case 0x09: /* Digital */ |
| 356 | default: |
| 357 | location = pcm + sec_loc; |
Takashi Sakamoto | 49c7b3f | 2015-09-19 11:22:01 +0900 | [diff] [blame] | 358 | if (location >= AM824_MAX_CHANNELS_FOR_PCM) { |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 359 | err = -ENOSYS; |
| 360 | goto end; |
| 361 | } |
Takashi Sakamoto | f65be91 | 2015-09-19 11:21:58 +0900 | [diff] [blame] | 362 | amdtp_am824_set_pcm_position(s, location, |
| 363 | stm_pos); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (type != 0x0a) |
| 369 | pcm += channels; |
| 370 | else |
| 371 | midi += channels; |
| 372 | } |
| 373 | end: |
| 374 | kfree(buf); |
| 375 | return err; |
| 376 | } |
| 377 | |
| 378 | static int |
| 379 | init_both_connections(struct snd_bebob *bebob) |
| 380 | { |
| 381 | int err; |
| 382 | |
| 383 | err = cmp_connection_init(&bebob->in_conn, |
| 384 | bebob->unit, CMP_INPUT, 0); |
| 385 | if (err < 0) |
| 386 | goto end; |
| 387 | |
| 388 | err = cmp_connection_init(&bebob->out_conn, |
| 389 | bebob->unit, CMP_OUTPUT, 0); |
| 390 | if (err < 0) |
| 391 | cmp_connection_destroy(&bebob->in_conn); |
| 392 | end: |
| 393 | return err; |
| 394 | } |
| 395 | |
| 396 | static int |
| 397 | check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s) |
| 398 | { |
| 399 | struct cmp_connection *conn; |
| 400 | bool used; |
| 401 | int err; |
| 402 | |
| 403 | if (s == &bebob->tx_stream) |
| 404 | conn = &bebob->out_conn; |
| 405 | else |
| 406 | conn = &bebob->in_conn; |
| 407 | |
| 408 | err = cmp_connection_check_used(conn, &used); |
| 409 | if ((err >= 0) && used && !amdtp_stream_running(s)) { |
| 410 | dev_err(&bebob->unit->device, |
| 411 | "Connection established by others: %cPCR[%d]\n", |
| 412 | (conn->direction == CMP_OUTPUT) ? 'o' : 'i', |
| 413 | conn->pcr_index); |
| 414 | err = -EBUSY; |
| 415 | } |
| 416 | |
| 417 | return err; |
| 418 | } |
| 419 | |
| 420 | static int |
| 421 | make_both_connections(struct snd_bebob *bebob, unsigned int rate) |
| 422 | { |
Takashi Sakamoto | b6bc812 | 2014-04-25 22:45:16 +0900 | [diff] [blame] | 423 | int index, pcm_channels, midi_channels, err = 0; |
| 424 | |
| 425 | if (bebob->connected) |
| 426 | goto end; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 427 | |
| 428 | /* confirm params for both streams */ |
Lucas Tanure | 0790529 | 2016-01-25 19:30:23 -0200 | [diff] [blame] | 429 | err = get_formation_index(rate, &index); |
| 430 | if (err < 0) |
| 431 | goto end; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 432 | pcm_channels = bebob->tx_stream_formations[index].pcm; |
| 433 | midi_channels = bebob->tx_stream_formations[index].midi; |
Takashi Sakamoto | 51c29fd | 2015-09-19 11:21:56 +0900 | [diff] [blame] | 434 | err = amdtp_am824_set_parameters(&bebob->tx_stream, rate, |
| 435 | pcm_channels, midi_channels * 8, |
| 436 | false); |
Takashi Sakamoto | 547e631 | 2015-09-19 11:21:49 +0900 | [diff] [blame] | 437 | if (err < 0) |
| 438 | goto end; |
| 439 | |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 440 | pcm_channels = bebob->rx_stream_formations[index].pcm; |
| 441 | midi_channels = bebob->rx_stream_formations[index].midi; |
Takashi Sakamoto | 51c29fd | 2015-09-19 11:21:56 +0900 | [diff] [blame] | 442 | err = amdtp_am824_set_parameters(&bebob->rx_stream, rate, |
| 443 | pcm_channels, midi_channels * 8, |
| 444 | false); |
Takashi Sakamoto | 547e631 | 2015-09-19 11:21:49 +0900 | [diff] [blame] | 445 | if (err < 0) |
| 446 | goto end; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 447 | |
| 448 | /* establish connections for both streams */ |
| 449 | err = cmp_connection_establish(&bebob->out_conn, |
| 450 | amdtp_stream_get_max_payload(&bebob->tx_stream)); |
| 451 | if (err < 0) |
| 452 | goto end; |
| 453 | err = cmp_connection_establish(&bebob->in_conn, |
| 454 | amdtp_stream_get_max_payload(&bebob->rx_stream)); |
Takashi Sakamoto | b6bc812 | 2014-04-25 22:45:16 +0900 | [diff] [blame] | 455 | if (err < 0) { |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 456 | cmp_connection_break(&bebob->out_conn); |
Takashi Sakamoto | b6bc812 | 2014-04-25 22:45:16 +0900 | [diff] [blame] | 457 | goto end; |
| 458 | } |
| 459 | |
| 460 | bebob->connected = true; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 461 | end: |
| 462 | return err; |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | break_both_connections(struct snd_bebob *bebob) |
| 467 | { |
| 468 | cmp_connection_break(&bebob->in_conn); |
| 469 | cmp_connection_break(&bebob->out_conn); |
Takashi Sakamoto | b6bc812 | 2014-04-25 22:45:16 +0900 | [diff] [blame] | 470 | |
| 471 | bebob->connected = false; |
Takashi Sakamoto | 3149ac4 | 2014-04-25 22:45:26 +0900 | [diff] [blame] | 472 | |
| 473 | /* These models seems to be in transition state for a longer time. */ |
| 474 | if (bebob->maudio_special_quirk != NULL) |
| 475 | msleep(200); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static void |
| 479 | destroy_both_connections(struct snd_bebob *bebob) |
| 480 | { |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 481 | cmp_connection_destroy(&bebob->in_conn); |
| 482 | cmp_connection_destroy(&bebob->out_conn); |
| 483 | } |
| 484 | |
| 485 | static int |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 486 | start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream, |
| 487 | unsigned int rate) |
| 488 | { |
| 489 | struct cmp_connection *conn; |
| 490 | int err = 0; |
| 491 | |
| 492 | if (stream == &bebob->rx_stream) |
| 493 | conn = &bebob->in_conn; |
| 494 | else |
| 495 | conn = &bebob->out_conn; |
| 496 | |
| 497 | /* channel mapping */ |
Takashi Sakamoto | 3149ac4 | 2014-04-25 22:45:26 +0900 | [diff] [blame] | 498 | if (bebob->maudio_special_quirk == NULL) { |
| 499 | err = map_data_channels(bebob, stream); |
| 500 | if (err < 0) |
| 501 | goto end; |
| 502 | } |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 503 | |
| 504 | /* start amdtp stream */ |
| 505 | err = amdtp_stream_start(stream, |
| 506 | conn->resources.channel, |
| 507 | conn->speed); |
| 508 | end: |
| 509 | return err; |
| 510 | } |
| 511 | |
| 512 | int snd_bebob_stream_init_duplex(struct snd_bebob *bebob) |
| 513 | { |
| 514 | int err; |
| 515 | |
| 516 | err = init_both_connections(bebob); |
| 517 | if (err < 0) |
| 518 | goto end; |
| 519 | |
Takashi Sakamoto | 5955815 | 2015-09-19 11:21:55 +0900 | [diff] [blame] | 520 | err = amdtp_am824_init(&bebob->tx_stream, bebob->unit, |
| 521 | AMDTP_IN_STREAM, CIP_BLOCKING); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 522 | if (err < 0) { |
| 523 | amdtp_stream_destroy(&bebob->tx_stream); |
| 524 | destroy_both_connections(bebob); |
| 525 | goto end; |
| 526 | } |
Takashi Sakamoto | 14a37ac | 2016-02-20 16:18:56 +0900 | [diff] [blame] | 527 | |
Takashi Sakamoto | c4d860a | 2015-06-14 12:49:35 +0900 | [diff] [blame] | 528 | /* |
| 529 | * BeBoB v3 transfers packets with these qurks: |
| 530 | * - In the beginning of streaming, the value of dbc is incremented |
| 531 | * even if no data blocks are transferred. |
| 532 | * - The value of dbc is reset suddenly. |
| 533 | */ |
| 534 | if (bebob->version > 2) |
| 535 | bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC | |
| 536 | CIP_SKIP_DBC_ZERO_CHECK; |
| 537 | |
Takashi Sakamoto | 9d59124 | 2014-04-25 22:45:27 +0900 | [diff] [blame] | 538 | /* |
| 539 | * At high sampling rate, M-Audio special firmware transmits empty |
| 540 | * packet with the value of dbc incremented by 8 but the others are |
| 541 | * valid to IEC 61883-1. |
| 542 | */ |
| 543 | if (bebob->maudio_special_quirk) |
| 544 | bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 545 | |
Takashi Sakamoto | 5955815 | 2015-09-19 11:21:55 +0900 | [diff] [blame] | 546 | err = amdtp_am824_init(&bebob->rx_stream, bebob->unit, |
| 547 | AMDTP_OUT_STREAM, CIP_BLOCKING); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 548 | if (err < 0) { |
| 549 | amdtp_stream_destroy(&bebob->tx_stream); |
| 550 | amdtp_stream_destroy(&bebob->rx_stream); |
| 551 | destroy_both_connections(bebob); |
| 552 | } |
| 553 | end: |
| 554 | return err; |
| 555 | } |
| 556 | |
Takashi Sakamoto | 4a286d5 | 2014-05-28 00:14:40 +0900 | [diff] [blame] | 557 | int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, unsigned int rate) |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 558 | { |
Julia Lawall | 6b9866c | 2015-10-11 08:10:55 +0200 | [diff] [blame] | 559 | const struct snd_bebob_rate_spec *rate_spec = bebob->spec->rate; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 560 | unsigned int curr_rate; |
| 561 | int err = 0; |
| 562 | |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 563 | /* Need no substreams */ |
Takashi Sakamoto | 4fd6c6c | 2016-02-20 16:18:58 +0900 | [diff] [blame] | 564 | if (bebob->substreams_counter == 0) |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 565 | goto end; |
| 566 | |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 567 | /* |
| 568 | * Considering JACK/FFADO streaming: |
| 569 | * TODO: This can be removed hwdep functionality becomes popular. |
| 570 | */ |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 571 | err = check_connection_used_by_others(bebob, &bebob->rx_stream); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 572 | if (err < 0) |
| 573 | goto end; |
| 574 | |
Takashi Sakamoto | b6bc812 | 2014-04-25 22:45:16 +0900 | [diff] [blame] | 575 | /* |
| 576 | * packet queueing error or detecting discontinuity |
| 577 | * |
| 578 | * At bus reset, connections should not be broken here. So streams need |
| 579 | * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag. |
| 580 | */ |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 581 | if (amdtp_streaming_error(&bebob->rx_stream)) |
| 582 | amdtp_stream_stop(&bebob->rx_stream); |
| 583 | if (amdtp_streaming_error(&bebob->tx_stream)) |
| 584 | amdtp_stream_stop(&bebob->tx_stream); |
| 585 | if (!amdtp_stream_running(&bebob->rx_stream) && |
| 586 | !amdtp_stream_running(&bebob->tx_stream)) |
Takashi Sakamoto | b6bc812 | 2014-04-25 22:45:16 +0900 | [diff] [blame] | 587 | break_both_connections(bebob); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 588 | |
| 589 | /* stop streams if rate is different */ |
Takashi Sakamoto | 1fc9522 | 2014-04-25 22:45:21 +0900 | [diff] [blame] | 590 | err = rate_spec->get(bebob, &curr_rate); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 591 | if (err < 0) { |
| 592 | dev_err(&bebob->unit->device, |
| 593 | "fail to get sampling rate: %d\n", err); |
| 594 | goto end; |
| 595 | } |
| 596 | if (rate == 0) |
| 597 | rate = curr_rate; |
| 598 | if (rate != curr_rate) { |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 599 | amdtp_stream_stop(&bebob->rx_stream); |
| 600 | amdtp_stream_stop(&bebob->tx_stream); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 601 | break_both_connections(bebob); |
| 602 | } |
| 603 | |
| 604 | /* master should be always running */ |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 605 | if (!amdtp_stream_running(&bebob->rx_stream)) { |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 606 | /* |
| 607 | * NOTE: |
| 608 | * If establishing connections at first, Yamaha GO46 |
| 609 | * (and maybe Terratec X24) don't generate sound. |
Takashi Sakamoto | 3149ac4 | 2014-04-25 22:45:26 +0900 | [diff] [blame] | 610 | * |
| 611 | * For firmware customized by M-Audio, refer to next NOTE. |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 612 | */ |
Takashi Sakamoto | 3149ac4 | 2014-04-25 22:45:26 +0900 | [diff] [blame] | 613 | if (bebob->maudio_special_quirk == NULL) { |
| 614 | err = rate_spec->set(bebob, rate); |
| 615 | if (err < 0) { |
| 616 | dev_err(&bebob->unit->device, |
| 617 | "fail to set sampling rate: %d\n", |
| 618 | err); |
| 619 | goto end; |
| 620 | } |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | err = make_both_connections(bebob, rate); |
| 624 | if (err < 0) |
| 625 | goto end; |
| 626 | |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 627 | err = start_stream(bebob, &bebob->rx_stream, rate); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 628 | if (err < 0) { |
| 629 | dev_err(&bebob->unit->device, |
| 630 | "fail to run AMDTP master stream:%d\n", err); |
| 631 | break_both_connections(bebob); |
| 632 | goto end; |
| 633 | } |
| 634 | |
Takashi Sakamoto | 3149ac4 | 2014-04-25 22:45:26 +0900 | [diff] [blame] | 635 | /* |
| 636 | * NOTE: |
| 637 | * The firmware customized by M-Audio uses these commands to |
| 638 | * start transmitting stream. This is not usual way. |
| 639 | */ |
| 640 | if (bebob->maudio_special_quirk != NULL) { |
| 641 | err = rate_spec->set(bebob, rate); |
| 642 | if (err < 0) { |
| 643 | dev_err(&bebob->unit->device, |
| 644 | "fail to ensure sampling rate: %d\n", |
| 645 | err); |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 646 | amdtp_stream_stop(&bebob->rx_stream); |
Takashi Sakamoto | 3149ac4 | 2014-04-25 22:45:26 +0900 | [diff] [blame] | 647 | break_both_connections(bebob); |
| 648 | goto end; |
| 649 | } |
| 650 | } |
| 651 | |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 652 | /* wait first callback */ |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 653 | if (!amdtp_stream_wait_callback(&bebob->rx_stream, |
| 654 | CALLBACK_TIMEOUT)) { |
| 655 | amdtp_stream_stop(&bebob->rx_stream); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 656 | break_both_connections(bebob); |
| 657 | err = -ETIMEDOUT; |
| 658 | goto end; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /* start slave if needed */ |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 663 | if (!amdtp_stream_running(&bebob->tx_stream)) { |
| 664 | err = start_stream(bebob, &bebob->tx_stream, rate); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 665 | if (err < 0) { |
| 666 | dev_err(&bebob->unit->device, |
| 667 | "fail to run AMDTP slave stream:%d\n", err); |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 668 | amdtp_stream_stop(&bebob->rx_stream); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 669 | break_both_connections(bebob); |
| 670 | goto end; |
| 671 | } |
| 672 | |
| 673 | /* wait first callback */ |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 674 | if (!amdtp_stream_wait_callback(&bebob->tx_stream, |
| 675 | CALLBACK_TIMEOUT)) { |
| 676 | amdtp_stream_stop(&bebob->tx_stream); |
| 677 | amdtp_stream_stop(&bebob->rx_stream); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 678 | break_both_connections(bebob); |
| 679 | err = -ETIMEDOUT; |
| 680 | } |
| 681 | } |
| 682 | end: |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 683 | return err; |
| 684 | } |
| 685 | |
| 686 | void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob) |
| 687 | { |
Takashi Sakamoto | 4fd6c6c | 2016-02-20 16:18:58 +0900 | [diff] [blame] | 688 | if (bebob->substreams_counter == 0) { |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 689 | amdtp_stream_pcm_abort(&bebob->rx_stream); |
| 690 | amdtp_stream_stop(&bebob->rx_stream); |
Takashi Sakamoto | 8d1c269 | 2015-06-14 12:49:36 +0900 | [diff] [blame] | 691 | |
Takashi Sakamoto | c71283c | 2016-05-09 23:15:50 +0900 | [diff] [blame] | 692 | amdtp_stream_pcm_abort(&bebob->tx_stream); |
| 693 | amdtp_stream_stop(&bebob->tx_stream); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 694 | |
Takashi Sakamoto | 8d1c269 | 2015-06-14 12:49:36 +0900 | [diff] [blame] | 695 | break_both_connections(bebob); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 696 | } |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 697 | } |
| 698 | |
Takashi Sakamoto | d23c2cc | 2015-02-21 23:54:59 +0900 | [diff] [blame] | 699 | /* |
| 700 | * This function should be called before starting streams or after stopping |
| 701 | * streams. |
| 702 | */ |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 703 | void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob) |
| 704 | { |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 705 | amdtp_stream_destroy(&bebob->rx_stream); |
| 706 | amdtp_stream_destroy(&bebob->tx_stream); |
| 707 | |
| 708 | destroy_both_connections(bebob); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /* |
| 712 | * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’ |
| 713 | * in Additional AVC commands (Nov 2003, BridgeCo) |
| 714 | * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005 |
| 715 | */ |
| 716 | static int |
| 717 | parse_stream_formation(u8 *buf, unsigned int len, |
| 718 | struct snd_bebob_stream_formation *formation) |
| 719 | { |
| 720 | unsigned int i, e, channels, format; |
| 721 | |
| 722 | /* |
| 723 | * this module can support a hierarchy combination that: |
| 724 | * Root: Audio and Music (0x90) |
| 725 | * Level 1: AM824 Compound (0x40) |
| 726 | */ |
| 727 | if ((buf[0] != 0x90) || (buf[1] != 0x40)) |
| 728 | return -ENOSYS; |
| 729 | |
| 730 | /* check sampling rate */ |
| 731 | for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) { |
| 732 | if (buf[2] == bridgeco_freq_table[i]) |
| 733 | break; |
| 734 | } |
Dan Carpenter | 33a5f98 | 2014-05-28 19:43:30 +0300 | [diff] [blame] | 735 | if (i == ARRAY_SIZE(bridgeco_freq_table)) |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 736 | return -ENOSYS; |
| 737 | |
| 738 | /* Avoid double count by different entries for the same rate. */ |
| 739 | memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation)); |
| 740 | |
| 741 | for (e = 0; e < buf[4]; e++) { |
| 742 | channels = buf[5 + e * 2]; |
| 743 | format = buf[6 + e * 2]; |
| 744 | |
| 745 | switch (format) { |
Takashi Sakamoto | 51fa31d | 2014-05-28 00:14:47 +0900 | [diff] [blame] | 746 | /* IEC 60958 Conformant, currently handled as MBLA */ |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 747 | case 0x00: |
| 748 | /* Multi bit linear audio */ |
| 749 | case 0x06: /* Raw */ |
| 750 | formation[i].pcm += channels; |
| 751 | break; |
| 752 | /* MIDI Conformant */ |
| 753 | case 0x0d: |
| 754 | formation[i].midi += channels; |
| 755 | break; |
| 756 | /* IEC 61937-3 to 7 */ |
| 757 | case 0x01: |
| 758 | case 0x02: |
| 759 | case 0x03: |
| 760 | case 0x04: |
| 761 | case 0x05: |
| 762 | /* Multi bit linear audio */ |
| 763 | case 0x07: /* DVD-Audio */ |
| 764 | case 0x0c: /* High Precision */ |
| 765 | /* One Bit Audio */ |
| 766 | case 0x08: /* (Plain) Raw */ |
| 767 | case 0x09: /* (Plain) SACD */ |
| 768 | case 0x0a: /* (Encoded) Raw */ |
| 769 | case 0x0b: /* (Encoded) SACD */ |
| 770 | /* Synchronization Stream (Stereo Raw audio) */ |
| 771 | case 0x40: |
| 772 | /* Don't care */ |
| 773 | case 0xff: |
| 774 | default: |
| 775 | return -ENOSYS; /* not supported */ |
| 776 | } |
| 777 | } |
| 778 | |
Takashi Sakamoto | 49c7b3f | 2015-09-19 11:22:01 +0900 | [diff] [blame] | 779 | if (formation[i].pcm > AM824_MAX_CHANNELS_FOR_PCM || |
| 780 | formation[i].midi > AM824_MAX_CHANNELS_FOR_MIDI) |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 781 | return -ENOSYS; |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | static int |
| 787 | fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir, |
| 788 | unsigned short pid) |
| 789 | { |
| 790 | u8 *buf; |
| 791 | struct snd_bebob_stream_formation *formations; |
| 792 | unsigned int len, eid; |
| 793 | u8 addr[AVC_BRIDGECO_ADDR_BYTES]; |
| 794 | int err; |
| 795 | |
| 796 | buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL); |
| 797 | if (buf == NULL) |
| 798 | return -ENOMEM; |
| 799 | |
| 800 | if (dir == AVC_BRIDGECO_PLUG_DIR_IN) |
| 801 | formations = bebob->rx_stream_formations; |
| 802 | else |
| 803 | formations = bebob->tx_stream_formations; |
| 804 | |
| 805 | for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) { |
| 806 | len = FORMAT_MAXIMUM_LENGTH; |
| 807 | avc_bridgeco_fill_unit_addr(addr, dir, |
| 808 | AVC_BRIDGECO_PLUG_UNIT_ISOC, pid); |
| 809 | err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf, |
| 810 | &len, eid); |
| 811 | /* No entries remained. */ |
| 812 | if (err == -EINVAL && eid > 0) { |
| 813 | err = 0; |
| 814 | break; |
| 815 | } else if (err < 0) { |
| 816 | dev_err(&bebob->unit->device, |
| 817 | "fail to get stream format %d for isoc %s plug %d:%d\n", |
| 818 | eid, |
| 819 | (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : |
| 820 | "out", |
| 821 | pid, err); |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | err = parse_stream_formation(buf, len, formations); |
| 826 | if (err < 0) |
| 827 | break; |
| 828 | } |
| 829 | |
| 830 | kfree(buf); |
| 831 | return err; |
| 832 | } |
| 833 | |
| 834 | static int |
| 835 | seek_msu_sync_input_plug(struct snd_bebob *bebob) |
| 836 | { |
| 837 | u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES]; |
Takashi Sakamoto | a6b598b | 2014-05-28 00:14:41 +0900 | [diff] [blame] | 838 | unsigned int i; |
| 839 | enum avc_bridgeco_plug_type type; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 840 | int err; |
| 841 | |
| 842 | /* Get the number of Music Sub Unit for both direction. */ |
| 843 | err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs); |
| 844 | if (err < 0) { |
| 845 | dev_err(&bebob->unit->device, |
| 846 | "fail to get info for MSU in/out plugs: %d\n", |
| 847 | err); |
| 848 | goto end; |
| 849 | } |
| 850 | |
| 851 | /* seek destination plugs for 'MSU sync input' */ |
| 852 | bebob->sync_input_plug = -1; |
| 853 | for (i = 0; i < plugs[0]; i++) { |
| 854 | avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i); |
| 855 | err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type); |
| 856 | if (err < 0) { |
| 857 | dev_err(&bebob->unit->device, |
| 858 | "fail to get type for MSU in plug %d: %d\n", |
| 859 | i, err); |
| 860 | goto end; |
| 861 | } |
| 862 | |
| 863 | if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) { |
| 864 | bebob->sync_input_plug = i; |
| 865 | break; |
| 866 | } |
| 867 | } |
| 868 | end: |
| 869 | return err; |
| 870 | } |
| 871 | |
| 872 | int snd_bebob_stream_discover(struct snd_bebob *bebob) |
| 873 | { |
Julia Lawall | 6b9866c | 2015-10-11 08:10:55 +0200 | [diff] [blame] | 874 | const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock; |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 875 | u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES]; |
| 876 | enum avc_bridgeco_plug_type type; |
| 877 | unsigned int i; |
| 878 | int err; |
| 879 | |
| 880 | /* the number of plugs for isoc in/out, ext in/out */ |
| 881 | err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs); |
| 882 | if (err < 0) { |
| 883 | dev_err(&bebob->unit->device, |
| 884 | "fail to get info for isoc/external in/out plugs: %d\n", |
| 885 | err); |
| 886 | goto end; |
| 887 | } |
| 888 | |
| 889 | /* |
| 890 | * This module supports at least one isoc input plug and one isoc |
| 891 | * output plug. |
| 892 | */ |
| 893 | if ((plugs[0] == 0) || (plugs[1] == 0)) { |
| 894 | err = -ENOSYS; |
| 895 | goto end; |
| 896 | } |
| 897 | |
| 898 | avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, |
| 899 | AVC_BRIDGECO_PLUG_UNIT_ISOC, 0); |
| 900 | err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type); |
| 901 | if (err < 0) { |
| 902 | dev_err(&bebob->unit->device, |
| 903 | "fail to get type for isoc in plug 0: %d\n", err); |
| 904 | goto end; |
| 905 | } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) { |
| 906 | err = -ENOSYS; |
| 907 | goto end; |
| 908 | } |
| 909 | err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0); |
| 910 | if (err < 0) |
| 911 | goto end; |
| 912 | |
| 913 | avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT, |
| 914 | AVC_BRIDGECO_PLUG_UNIT_ISOC, 0); |
| 915 | err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type); |
| 916 | if (err < 0) { |
| 917 | dev_err(&bebob->unit->device, |
| 918 | "fail to get type for isoc out plug 0: %d\n", err); |
| 919 | goto end; |
| 920 | } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) { |
| 921 | err = -ENOSYS; |
| 922 | goto end; |
| 923 | } |
| 924 | err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0); |
| 925 | if (err < 0) |
| 926 | goto end; |
| 927 | |
| 928 | /* count external input plugs for MIDI */ |
| 929 | bebob->midi_input_ports = 0; |
| 930 | for (i = 0; i < plugs[2]; i++) { |
| 931 | avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, |
| 932 | AVC_BRIDGECO_PLUG_UNIT_EXT, i); |
| 933 | err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type); |
| 934 | if (err < 0) { |
| 935 | dev_err(&bebob->unit->device, |
| 936 | "fail to get type for external in plug %d: %d\n", |
| 937 | i, err); |
| 938 | goto end; |
| 939 | } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) { |
| 940 | bebob->midi_input_ports++; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | /* count external output plugs for MIDI */ |
| 945 | bebob->midi_output_ports = 0; |
| 946 | for (i = 0; i < plugs[3]; i++) { |
| 947 | avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT, |
| 948 | AVC_BRIDGECO_PLUG_UNIT_EXT, i); |
| 949 | err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type); |
| 950 | if (err < 0) { |
| 951 | dev_err(&bebob->unit->device, |
| 952 | "fail to get type for external out plug %d: %d\n", |
| 953 | i, err); |
| 954 | goto end; |
| 955 | } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) { |
| 956 | bebob->midi_output_ports++; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | /* for check source of clock later */ |
Takashi Sakamoto | 1fc9522 | 2014-04-25 22:45:21 +0900 | [diff] [blame] | 961 | if (!clk_spec) |
| 962 | err = seek_msu_sync_input_plug(bebob); |
Takashi Sakamoto | eb7b3a0 | 2014-04-25 22:45:15 +0900 | [diff] [blame] | 963 | end: |
| 964 | return err; |
| 965 | } |
Takashi Sakamoto | 618eabe | 2014-04-25 22:45:20 +0900 | [diff] [blame] | 966 | |
| 967 | void snd_bebob_stream_lock_changed(struct snd_bebob *bebob) |
| 968 | { |
| 969 | bebob->dev_lock_changed = true; |
| 970 | wake_up(&bebob->hwdep_wait); |
| 971 | } |
| 972 | |
| 973 | int snd_bebob_stream_lock_try(struct snd_bebob *bebob) |
| 974 | { |
| 975 | int err; |
| 976 | |
| 977 | spin_lock_irq(&bebob->lock); |
| 978 | |
| 979 | /* user land lock this */ |
| 980 | if (bebob->dev_lock_count < 0) { |
| 981 | err = -EBUSY; |
| 982 | goto end; |
| 983 | } |
| 984 | |
| 985 | /* this is the first time */ |
| 986 | if (bebob->dev_lock_count++ == 0) |
| 987 | snd_bebob_stream_lock_changed(bebob); |
| 988 | err = 0; |
| 989 | end: |
| 990 | spin_unlock_irq(&bebob->lock); |
| 991 | return err; |
| 992 | } |
| 993 | |
| 994 | void snd_bebob_stream_lock_release(struct snd_bebob *bebob) |
| 995 | { |
| 996 | spin_lock_irq(&bebob->lock); |
| 997 | |
| 998 | if (WARN_ON(bebob->dev_lock_count <= 0)) |
| 999 | goto end; |
| 1000 | if (--bebob->dev_lock_count == 0) |
| 1001 | snd_bebob_stream_lock_changed(bebob); |
| 1002 | end: |
| 1003 | spin_unlock_irq(&bebob->lock); |
| 1004 | } |