2 * oxfw_stream.c - a part of driver for OXFW970/971 based devices
4 * Copyright (c) 2014 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
10 #include <linux/delay.h>
12 #define AVC_GENERIC_FRAME_MAXIMUM_BYTES 512
13 #define CALLBACK_TIMEOUT 200
16 * According to datasheet of Oxford Semiconductor:
17 * OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O
18 * OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O
20 static const unsigned int oxfw_rate_table[] = {
30 * See Table 5.7 – Sampling frequency for Multi-bit Audio
31 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
33 static const unsigned int avc_stream_rate_table[] = {
42 static int set_rate(struct snd_oxfw *oxfw, unsigned int rate)
46 err = avc_general_set_sig_fmt(oxfw->unit, rate,
47 AVC_GENERAL_PLUG_DIR_IN, 0);
52 err = avc_general_set_sig_fmt(oxfw->unit, rate,
53 AVC_GENERAL_PLUG_DIR_OUT, 0);
58 static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s,
59 unsigned int rate, unsigned int pcm_channels)
62 struct snd_oxfw_stream_formation formation;
63 enum avc_general_plug_dir dir;
67 if (s == &oxfw->tx_stream) {
68 formats = oxfw->tx_stream_formats;
69 dir = AVC_GENERAL_PLUG_DIR_OUT;
71 formats = oxfw->rx_stream_formats;
72 dir = AVC_GENERAL_PLUG_DIR_IN;
75 /* Seek stream format for requirements. */
76 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
77 err = snd_oxfw_stream_parse_format(formats[i], &formation);
81 if ((formation.rate == rate) && (formation.pcm == pcm_channels))
84 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
87 /* If assumed, just change rate. */
89 return set_rate(oxfw, rate);
91 /* Calculate format length. */
92 len = 5 + formats[i][4] * 2;
94 err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len);
98 /* Some requests just after changing format causes freezing. */
104 static void stop_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
106 amdtp_stream_pcm_abort(stream);
107 amdtp_stream_stop(stream);
109 if (stream == &oxfw->tx_stream)
110 cmp_connection_break(&oxfw->out_conn);
112 cmp_connection_break(&oxfw->in_conn);
115 static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream,
116 unsigned int rate, unsigned int pcm_channels)
119 struct cmp_connection *conn;
120 struct snd_oxfw_stream_formation formation;
121 unsigned int i, midi_ports;
124 if (stream == &oxfw->rx_stream) {
125 formats = oxfw->rx_stream_formats;
126 conn = &oxfw->in_conn;
128 formats = oxfw->tx_stream_formats;
129 conn = &oxfw->out_conn;
132 /* Get stream format */
133 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
134 if (formats[i] == NULL)
137 err = snd_oxfw_stream_parse_format(formats[i], &formation);
140 if (rate != formation.rate)
142 if (pcm_channels == 0 || pcm_channels == formation.pcm)
145 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) {
150 pcm_channels = formation.pcm;
151 midi_ports = DIV_ROUND_UP(formation.midi, 8);
153 /* The stream should have one pcm channels at least */
154 if (pcm_channels == 0) {
158 amdtp_stream_set_parameters(stream, rate, pcm_channels, midi_ports);
160 err = cmp_connection_establish(conn,
161 amdtp_stream_get_max_payload(stream));
165 err = amdtp_stream_start(stream,
166 conn->resources.channel,
169 cmp_connection_break(conn);
173 /* Wait first packet */
174 if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
175 stop_stream(oxfw, stream);
182 static int check_connection_used_by_others(struct snd_oxfw *oxfw,
183 struct amdtp_stream *stream)
185 struct cmp_connection *conn;
189 if (stream == &oxfw->tx_stream)
190 conn = &oxfw->out_conn;
192 conn = &oxfw->in_conn;
194 err = cmp_connection_check_used(conn, &used);
195 if ((err >= 0) && used && !amdtp_stream_running(stream)) {
196 dev_err(&oxfw->unit->device,
197 "Connection established by others: %cPCR[%d]\n",
198 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
206 int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw,
207 struct amdtp_stream *stream)
209 struct cmp_connection *conn;
210 enum cmp_direction c_dir;
211 enum amdtp_stream_direction s_dir;
214 if (stream == &oxfw->tx_stream) {
215 conn = &oxfw->out_conn;
217 s_dir = AMDTP_IN_STREAM;
219 conn = &oxfw->in_conn;
221 s_dir = AMDTP_OUT_STREAM;
224 err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
228 err = amdtp_stream_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING);
230 amdtp_stream_destroy(stream);
231 cmp_connection_destroy(conn);
236 * OXFW starts to transmit packets with non-zero dbc.
237 * OXFW postpone transferring packets till handling any asynchronous
238 * packets. As a result, next isochronous packet includes more data
239 * blocks than IEC 61883-6 defines.
241 if (stream == &oxfw->tx_stream)
242 oxfw->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK |
248 int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw,
249 struct amdtp_stream *stream,
250 unsigned int rate, unsigned int pcm_channels)
252 struct amdtp_stream *opposite;
253 struct snd_oxfw_stream_formation formation;
254 enum avc_general_plug_dir dir;
255 unsigned int substreams, opposite_substreams;
258 if (stream == &oxfw->tx_stream) {
259 substreams = oxfw->capture_substreams;
260 opposite = &oxfw->rx_stream;
261 opposite_substreams = oxfw->playback_substreams;
262 dir = AVC_GENERAL_PLUG_DIR_OUT;
264 substreams = oxfw->playback_substreams;
265 opposite_substreams = oxfw->capture_substreams;
267 if (oxfw->has_output)
268 opposite = &oxfw->rx_stream;
272 dir = AVC_GENERAL_PLUG_DIR_IN;
279 * Considering JACK/FFADO streaming:
280 * TODO: This can be removed hwdep functionality becomes popular.
282 err = check_connection_used_by_others(oxfw, stream);
286 /* packet queueing error */
287 if (amdtp_streaming_error(stream))
288 stop_stream(oxfw, stream);
290 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
294 rate = formation.rate;
295 if (pcm_channels == 0)
296 pcm_channels = formation.pcm;
298 if ((formation.rate != rate) || (formation.pcm != pcm_channels)) {
299 if (opposite != NULL) {
300 err = check_connection_used_by_others(oxfw, opposite);
303 stop_stream(oxfw, opposite);
305 stop_stream(oxfw, stream);
307 err = set_stream_format(oxfw, stream, rate, pcm_channels);
309 dev_err(&oxfw->unit->device,
310 "fail to set stream format: %d\n", err);
314 /* Start opposite stream if needed. */
315 if (opposite && !amdtp_stream_running(opposite) &&
316 (opposite_substreams > 0)) {
317 err = start_stream(oxfw, opposite, rate, 0);
319 dev_err(&oxfw->unit->device,
320 "fail to restart stream: %d\n", err);
326 /* Start requested stream. */
327 if (!amdtp_stream_running(stream)) {
328 err = start_stream(oxfw, stream, rate, pcm_channels);
330 dev_err(&oxfw->unit->device,
331 "fail to start stream: %d\n", err);
337 void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw,
338 struct amdtp_stream *stream)
340 if (((stream == &oxfw->tx_stream) && (oxfw->capture_substreams > 0)) ||
341 ((stream == &oxfw->rx_stream) && (oxfw->playback_substreams > 0)))
344 stop_stream(oxfw, stream);
348 * This function should be called before starting the stream or after stopping
351 void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw,
352 struct amdtp_stream *stream)
354 struct cmp_connection *conn;
356 if (stream == &oxfw->tx_stream)
357 conn = &oxfw->out_conn;
359 conn = &oxfw->in_conn;
361 amdtp_stream_destroy(stream);
362 cmp_connection_destroy(conn);
365 void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw,
366 struct amdtp_stream *stream)
368 struct cmp_connection *conn;
370 if (stream == &oxfw->tx_stream)
371 conn = &oxfw->out_conn;
373 conn = &oxfw->in_conn;
375 if (cmp_connection_update(conn) < 0)
376 stop_stream(oxfw, stream);
378 amdtp_stream_update(stream);
381 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
382 enum avc_general_plug_dir dir,
383 struct snd_oxfw_stream_formation *formation)
389 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
390 format = kmalloc(len, GFP_KERNEL);
394 err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
402 err = snd_oxfw_stream_parse_format(format, formation);
409 * See Table 6.16 - AM824 Stream Format
410 * Figure 6.19 - format_information field for AM824 Compound
411 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
412 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
414 int snd_oxfw_stream_parse_format(u8 *format,
415 struct snd_oxfw_stream_formation *formation)
417 unsigned int i, e, channels, type;
419 memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
422 * this module can support a hierarchy combination that:
423 * Root: Audio and Music (0x90)
424 * Level 1: AM824 Compound (0x40)
426 if ((format[0] != 0x90) || (format[1] != 0x40))
429 /* check the sampling rate */
430 for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
431 if (format[2] == avc_stream_rate_table[i])
434 if (i == ARRAY_SIZE(avc_stream_rate_table))
437 formation->rate = oxfw_rate_table[i];
439 for (e = 0; e < format[4]; e++) {
440 channels = format[5 + e * 2];
441 type = format[6 + e * 2];
444 /* IEC 60958 Conformant, currently handled as MBLA */
446 /* Multi Bit Linear Audio (Raw) */
448 formation->pcm += channels;
450 /* MIDI Conformant */
452 formation->midi = channels;
454 /* IEC 61937-3 to 7 */
460 /* Multi Bit Linear Audio */
461 case 0x07: /* DVD-Audio */
462 case 0x0c: /* High Precision */
464 case 0x08: /* (Plain) Raw */
465 case 0x09: /* (Plain) SACD */
466 case 0x0a: /* (Encoded) Raw */
467 case 0x0b: /* (Encoded) SACD */
468 /* SMPTE Time-Code conformant */
474 /* Synchronization Stream (Stereo Raw audio) */
479 return -ENOSYS; /* not supported */
483 if (formation->pcm > AMDTP_MAX_CHANNELS_FOR_PCM ||
484 formation->midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
491 assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
492 unsigned int pid, u8 *buf, unsigned int *len,
495 struct snd_oxfw_stream_formation formation;
499 /* get format at current sampling rate */
500 err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
502 dev_err(&oxfw->unit->device,
503 "fail to get current stream format for isoc %s plug %d:%d\n",
504 (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
509 /* parse and set stream format */
511 err = snd_oxfw_stream_parse_format(buf, &formation);
515 formats[eid] = kmemdup(buf, *len, GFP_KERNEL);
516 if (formats[eid] == NULL) {
521 /* apply the format for each available sampling rate */
522 for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
523 if (formation.rate == oxfw_rate_table[i])
526 err = avc_general_inquiry_sig_fmt(oxfw->unit,
533 formats[eid] = kmemdup(buf, *len, GFP_KERNEL);
534 if (formats[eid] == NULL) {
538 formats[eid][2] = avc_stream_rate_table[i];
542 oxfw->assumed = true;
547 static int fill_stream_formats(struct snd_oxfw *oxfw,
548 enum avc_general_plug_dir dir,
552 unsigned int len, eid = 0;
553 struct snd_oxfw_stream_formation dummy;
556 buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
560 if (dir == AVC_GENERAL_PLUG_DIR_OUT)
561 formats = oxfw->tx_stream_formats;
563 formats = oxfw->rx_stream_formats;
565 /* get first entry */
566 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
567 err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
568 if (err == -ENOSYS) {
569 /* LIST subfunction is not implemented */
570 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
571 err = assume_stream_formats(oxfw, dir, pid, buf, &len,
574 } else if (err < 0) {
575 dev_err(&oxfw->unit->device,
576 "fail to get stream format %d for isoc %s plug %d:%d\n",
577 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
582 /* LIST subfunction is implemented */
583 while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
584 /* The format is too short. */
590 /* parse and set stream format */
591 err = snd_oxfw_stream_parse_format(buf, &dummy);
595 formats[eid] = kmemdup(buf, len, GFP_KERNEL);
596 if (formats[eid] == NULL) {
602 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
603 err = avc_stream_get_format_list(oxfw->unit, dir, 0,
605 /* No entries remained. */
606 if (err == -EINVAL) {
609 } else if (err < 0) {
610 dev_err(&oxfw->unit->device,
611 "fail to get stream format %d for isoc %s plug %d:%d\n",
612 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
623 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
625 u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
628 /* the number of plugs for isoc in/out, ext in/out */
629 err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
631 dev_err(&oxfw->unit->device,
632 "fail to get info for isoc/external in/out plugs: %d\n",
635 } else if ((plugs[0] == 0) && (plugs[1] == 0)) {
640 /* use oPCR[0] if exists */
642 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
645 oxfw->has_output = true;
648 /* use iPCR[0] if exists */
650 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
655 void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
657 oxfw->dev_lock_changed = true;
658 wake_up(&oxfw->hwdep_wait);
661 int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
665 spin_lock_irq(&oxfw->lock);
667 /* user land lock this */
668 if (oxfw->dev_lock_count < 0) {
673 /* this is the first time */
674 if (oxfw->dev_lock_count++ == 0)
675 snd_oxfw_stream_lock_changed(oxfw);
678 spin_unlock_irq(&oxfw->lock);
682 void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
684 spin_lock_irq(&oxfw->lock);
686 if (WARN_ON(oxfw->dev_lock_count <= 0))
688 if (--oxfw->dev_lock_count == 0)
689 snd_oxfw_stream_lock_changed(oxfw);
691 spin_unlock_irq(&oxfw->lock);