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
;
64 unsigned int i
, err
, len
;
66 if (s
== &oxfw
->tx_stream
) {
67 formats
= oxfw
->tx_stream_formats
;
68 dir
= AVC_GENERAL_PLUG_DIR_OUT
;
70 formats
= oxfw
->rx_stream_formats
;
71 dir
= AVC_GENERAL_PLUG_DIR_IN
;
74 /* Seek stream format for requirements. */
75 for (i
= 0; i
< SND_OXFW_STREAM_FORMAT_ENTRIES
; i
++) {
76 err
= snd_oxfw_stream_parse_format(formats
[i
], &formation
);
80 if ((formation
.rate
== rate
) && (formation
.pcm
== pcm_channels
))
83 if (i
== SND_OXFW_STREAM_FORMAT_ENTRIES
)
86 /* If assumed, just change rate. */
88 return set_rate(oxfw
, rate
);
90 /* Calculate format length. */
91 len
= 5 + formats
[i
][4] * 2;
93 err
= avc_stream_set_format(oxfw
->unit
, dir
, 0, formats
[i
], len
);
97 /* Some requests just after changing format causes freezing. */
103 static void stop_stream(struct snd_oxfw
*oxfw
, struct amdtp_stream
*stream
)
105 amdtp_stream_pcm_abort(stream
);
106 amdtp_stream_stop(stream
);
108 if (stream
== &oxfw
->tx_stream
)
109 cmp_connection_break(&oxfw
->out_conn
);
111 cmp_connection_break(&oxfw
->in_conn
);
114 static int start_stream(struct snd_oxfw
*oxfw
, struct amdtp_stream
*stream
,
115 unsigned int rate
, unsigned int pcm_channels
)
118 struct cmp_connection
*conn
;
119 struct snd_oxfw_stream_formation formation
;
120 unsigned int i
, midi_ports
;
123 if (stream
== &oxfw
->rx_stream
) {
124 formats
= oxfw
->rx_stream_formats
;
125 conn
= &oxfw
->in_conn
;
127 formats
= oxfw
->tx_stream_formats
;
128 conn
= &oxfw
->out_conn
;
131 /* Get stream format */
132 for (i
= 0; i
< SND_OXFW_STREAM_FORMAT_ENTRIES
; i
++) {
133 if (formats
[i
] == NULL
)
136 err
= snd_oxfw_stream_parse_format(formats
[i
], &formation
);
139 if (rate
!= formation
.rate
)
141 if (pcm_channels
== 0 || pcm_channels
== formation
.pcm
)
144 if (i
== SND_OXFW_STREAM_FORMAT_ENTRIES
) {
149 pcm_channels
= formation
.pcm
;
150 midi_ports
= DIV_ROUND_UP(formation
.midi
, 8);
152 /* The stream should have one pcm channels at least */
153 if (pcm_channels
== 0) {
157 amdtp_stream_set_parameters(stream
, rate
, pcm_channels
, midi_ports
);
159 err
= cmp_connection_establish(conn
,
160 amdtp_stream_get_max_payload(stream
));
164 err
= amdtp_stream_start(stream
,
165 conn
->resources
.channel
,
168 cmp_connection_break(conn
);
172 /* Wait first packet */
173 err
= amdtp_stream_wait_callback(stream
, CALLBACK_TIMEOUT
);
175 stop_stream(oxfw
, stream
);
180 static int check_connection_used_by_others(struct snd_oxfw
*oxfw
,
181 struct amdtp_stream
*stream
)
183 struct cmp_connection
*conn
;
187 if (stream
== &oxfw
->tx_stream
)
188 conn
= &oxfw
->out_conn
;
190 conn
= &oxfw
->in_conn
;
192 err
= cmp_connection_check_used(conn
, &used
);
193 if ((err
>= 0) && used
&& !amdtp_stream_running(stream
)) {
194 dev_err(&oxfw
->unit
->device
,
195 "Connection established by others: %cPCR[%d]\n",
196 (conn
->direction
== CMP_OUTPUT
) ? 'o' : 'i',
204 int snd_oxfw_stream_init_simplex(struct snd_oxfw
*oxfw
,
205 struct amdtp_stream
*stream
)
207 struct cmp_connection
*conn
;
208 enum cmp_direction c_dir
;
209 enum amdtp_stream_direction s_dir
;
212 if (stream
== &oxfw
->tx_stream
) {
213 conn
= &oxfw
->out_conn
;
215 s_dir
= AMDTP_IN_STREAM
;
217 conn
= &oxfw
->in_conn
;
219 s_dir
= AMDTP_OUT_STREAM
;
222 err
= cmp_connection_init(conn
, oxfw
->unit
, c_dir
, 0);
226 err
= amdtp_stream_init(stream
, oxfw
->unit
, s_dir
, CIP_NONBLOCKING
);
228 amdtp_stream_destroy(stream
);
229 cmp_connection_destroy(conn
);
233 /* OXFW starts to transmit packets with non-zero dbc. */
234 if (stream
== &oxfw
->tx_stream
)
235 oxfw
->tx_stream
.flags
|= CIP_SKIP_INIT_DBC_CHECK
;
240 int snd_oxfw_stream_start_simplex(struct snd_oxfw
*oxfw
,
241 struct amdtp_stream
*stream
,
242 unsigned int rate
, unsigned int pcm_channels
)
244 struct amdtp_stream
*opposite
;
245 struct snd_oxfw_stream_formation formation
;
246 enum avc_general_plug_dir dir
;
247 unsigned int substreams
, opposite_substreams
;
250 if (stream
== &oxfw
->tx_stream
) {
251 substreams
= oxfw
->capture_substreams
;
252 opposite
= &oxfw
->rx_stream
;
253 opposite_substreams
= oxfw
->playback_substreams
;
254 dir
= AVC_GENERAL_PLUG_DIR_OUT
;
256 substreams
= oxfw
->playback_substreams
;
257 opposite_substreams
= oxfw
->capture_substreams
;
259 if (oxfw
->has_output
)
260 opposite
= &oxfw
->rx_stream
;
264 dir
= AVC_GENERAL_PLUG_DIR_IN
;
271 * Considering JACK/FFADO streaming:
272 * TODO: This can be removed hwdep functionality becomes popular.
274 err
= check_connection_used_by_others(oxfw
, stream
);
278 /* packet queueing error */
279 if (amdtp_streaming_error(stream
))
280 stop_stream(oxfw
, stream
);
282 err
= snd_oxfw_stream_get_current_formation(oxfw
, dir
, &formation
);
286 rate
= formation
.rate
;
287 if (pcm_channels
== 0)
288 pcm_channels
= formation
.pcm
;
290 if ((formation
.rate
!= rate
) || (formation
.pcm
!= pcm_channels
)) {
291 if (opposite
!= NULL
) {
292 err
= check_connection_used_by_others(oxfw
, opposite
);
295 stop_stream(oxfw
, opposite
);
297 stop_stream(oxfw
, stream
);
299 err
= set_stream_format(oxfw
, stream
, rate
, pcm_channels
);
301 dev_err(&oxfw
->unit
->device
,
302 "fail to set stream format: %d\n", err
);
306 /* Start opposite stream if needed. */
307 if (opposite
&& !amdtp_stream_running(opposite
) &&
308 (opposite_substreams
> 0)) {
309 err
= start_stream(oxfw
, opposite
, rate
, 0);
311 dev_err(&oxfw
->unit
->device
,
312 "fail to restart stream: %d\n", err
);
318 /* Start requested stream. */
319 if (!amdtp_stream_running(stream
)) {
320 err
= start_stream(oxfw
, stream
, rate
, pcm_channels
);
322 dev_err(&oxfw
->unit
->device
,
323 "fail to start stream: %d\n", err
);
329 void snd_oxfw_stream_stop_simplex(struct snd_oxfw
*oxfw
,
330 struct amdtp_stream
*stream
)
332 if (((stream
== &oxfw
->tx_stream
) && (oxfw
->capture_substreams
> 0)) ||
333 ((stream
== &oxfw
->rx_stream
) && (oxfw
->playback_substreams
> 0)))
336 stop_stream(oxfw
, stream
);
339 void snd_oxfw_stream_destroy_simplex(struct snd_oxfw
*oxfw
,
340 struct amdtp_stream
*stream
)
342 struct cmp_connection
*conn
;
344 if (stream
== &oxfw
->tx_stream
)
345 conn
= &oxfw
->out_conn
;
347 conn
= &oxfw
->in_conn
;
349 stop_stream(oxfw
, stream
);
351 amdtp_stream_destroy(stream
);
352 cmp_connection_destroy(conn
);
355 void snd_oxfw_stream_update_simplex(struct snd_oxfw
*oxfw
,
356 struct amdtp_stream
*stream
)
358 struct cmp_connection
*conn
;
360 if (stream
== &oxfw
->tx_stream
)
361 conn
= &oxfw
->out_conn
;
363 conn
= &oxfw
->in_conn
;
365 if (cmp_connection_update(conn
) < 0)
366 stop_stream(oxfw
, stream
);
368 amdtp_stream_update(stream
);
371 int snd_oxfw_stream_get_current_formation(struct snd_oxfw
*oxfw
,
372 enum avc_general_plug_dir dir
,
373 struct snd_oxfw_stream_formation
*formation
)
379 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
380 format
= kmalloc(len
, GFP_KERNEL
);
384 err
= avc_stream_get_format_single(oxfw
->unit
, dir
, 0, format
, &len
);
392 err
= snd_oxfw_stream_parse_format(format
, formation
);
399 * See Table 6.16 - AM824 Stream Format
400 * Figure 6.19 - format_information field for AM824 Compound
401 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
402 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
404 int snd_oxfw_stream_parse_format(u8
*format
,
405 struct snd_oxfw_stream_formation
*formation
)
407 unsigned int i
, e
, channels
, type
;
409 memset(formation
, 0, sizeof(struct snd_oxfw_stream_formation
));
412 * this module can support a hierarchy combination that:
413 * Root: Audio and Music (0x90)
414 * Level 1: AM824 Compound (0x40)
416 if ((format
[0] != 0x90) || (format
[1] != 0x40))
419 /* check the sampling rate */
420 for (i
= 0; i
< ARRAY_SIZE(avc_stream_rate_table
); i
++) {
421 if (format
[2] == avc_stream_rate_table
[i
])
424 if (i
== ARRAY_SIZE(avc_stream_rate_table
))
427 formation
->rate
= oxfw_rate_table
[i
];
429 for (e
= 0; e
< format
[4]; e
++) {
430 channels
= format
[5 + e
* 2];
431 type
= format
[6 + e
* 2];
434 /* IEC 60958 Conformant, currently handled as MBLA */
436 /* Multi Bit Linear Audio (Raw) */
438 formation
->pcm
+= channels
;
440 /* MIDI Conformant */
442 formation
->midi
= channels
;
444 /* IEC 61937-3 to 7 */
450 /* Multi Bit Linear Audio */
451 case 0x07: /* DVD-Audio */
452 case 0x0c: /* High Precision */
454 case 0x08: /* (Plain) Raw */
455 case 0x09: /* (Plain) SACD */
456 case 0x0a: /* (Encoded) Raw */
457 case 0x0b: /* (Encoded) SACD */
458 /* SMPTE Time-Code conformant */
464 /* Synchronization Stream (Stereo Raw audio) */
469 return -ENOSYS
; /* not supported */
473 if (formation
->pcm
> AMDTP_MAX_CHANNELS_FOR_PCM
||
474 formation
->midi
> AMDTP_MAX_CHANNELS_FOR_MIDI
)
481 assume_stream_formats(struct snd_oxfw
*oxfw
, enum avc_general_plug_dir dir
,
482 unsigned int pid
, u8
*buf
, unsigned int *len
,
485 struct snd_oxfw_stream_formation formation
;
489 /* get format at current sampling rate */
490 err
= avc_stream_get_format_single(oxfw
->unit
, dir
, pid
, buf
, len
);
492 dev_err(&oxfw
->unit
->device
,
493 "fail to get current stream format for isoc %s plug %d:%d\n",
494 (dir
== AVC_GENERAL_PLUG_DIR_IN
) ? "in" : "out",
499 /* parse and set stream format */
501 err
= snd_oxfw_stream_parse_format(buf
, &formation
);
505 formats
[eid
] = kmalloc(*len
, GFP_KERNEL
);
506 if (formats
[eid
] == NULL
) {
510 memcpy(formats
[eid
], buf
, *len
);
512 /* apply the format for each available sampling rate */
513 for (i
= 0; i
< ARRAY_SIZE(oxfw_rate_table
); i
++) {
514 if (formation
.rate
== oxfw_rate_table
[i
])
517 err
= avc_general_inquiry_sig_fmt(oxfw
->unit
,
524 formats
[eid
] = kmalloc(*len
, GFP_KERNEL
);
525 if (formats
[eid
] == NULL
) {
529 memcpy(formats
[eid
], buf
, *len
);
530 formats
[eid
][2] = avc_stream_rate_table
[i
];
534 oxfw
->assumed
= true;
539 static int fill_stream_formats(struct snd_oxfw
*oxfw
,
540 enum avc_general_plug_dir dir
,
544 unsigned int len
, eid
= 0;
545 struct snd_oxfw_stream_formation dummy
;
548 buf
= kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES
, GFP_KERNEL
);
552 if (dir
== AVC_GENERAL_PLUG_DIR_OUT
)
553 formats
= oxfw
->tx_stream_formats
;
555 formats
= oxfw
->rx_stream_formats
;
557 /* get first entry */
558 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
559 err
= avc_stream_get_format_list(oxfw
->unit
, dir
, 0, buf
, &len
, 0);
560 if (err
== -ENOSYS
) {
561 /* LIST subfunction is not implemented */
562 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
563 err
= assume_stream_formats(oxfw
, dir
, pid
, buf
, &len
,
566 } else if (err
< 0) {
567 dev_err(&oxfw
->unit
->device
,
568 "fail to get stream format %d for isoc %s plug %d:%d\n",
569 eid
, (dir
== AVC_GENERAL_PLUG_DIR_IN
) ? "in" : "out",
574 /* LIST subfunction is implemented */
575 while (eid
< SND_OXFW_STREAM_FORMAT_ENTRIES
) {
576 /* The format is too short. */
582 /* parse and set stream format */
583 err
= snd_oxfw_stream_parse_format(buf
, &dummy
);
587 formats
[eid
] = kmalloc(len
, GFP_KERNEL
);
588 if (formats
[eid
] == NULL
) {
592 memcpy(formats
[eid
], buf
, len
);
595 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
596 err
= avc_stream_get_format_list(oxfw
->unit
, dir
, 0,
598 /* No entries remained. */
599 if (err
== -EINVAL
) {
602 } else if (err
< 0) {
603 dev_err(&oxfw
->unit
->device
,
604 "fail to get stream format %d for isoc %s plug %d:%d\n",
605 eid
, (dir
== AVC_GENERAL_PLUG_DIR_IN
) ? "in" :
616 int snd_oxfw_stream_discover(struct snd_oxfw
*oxfw
)
618 u8 plugs
[AVC_PLUG_INFO_BUF_BYTES
];
621 /* the number of plugs for isoc in/out, ext in/out */
622 err
= avc_general_get_plug_info(oxfw
->unit
, 0x1f, 0x07, 0x00, plugs
);
624 dev_err(&oxfw
->unit
->device
,
625 "fail to get info for isoc/external in/out plugs: %d\n",
628 } else if ((plugs
[0] == 0) && (plugs
[1] == 0)) {
633 /* use oPCR[0] if exists */
635 err
= fill_stream_formats(oxfw
, AVC_GENERAL_PLUG_DIR_OUT
, 0);
638 oxfw
->has_output
= true;
641 /* use iPCR[0] if exists */
643 err
= fill_stream_formats(oxfw
, AVC_GENERAL_PLUG_DIR_IN
, 0);