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