1 /*
2  * oxfw_stream.c - a part of driver for OXFW970/971 based devices
3  *
4  * Copyright (c) 2014 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8 
9 #include "oxfw.h"
10 #include <linux/delay.h>
11 
12 #define AVC_GENERIC_FRAME_MAXIMUM_BYTES	512
13 #define CALLBACK_TIMEOUT	200
14 
15 /*
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
19  */
20 static const unsigned int oxfw_rate_table[] = {
21 	[0] = 32000,
22 	[1] = 44100,
23 	[2] = 48000,
24 	[3] = 88200,
25 	[4] = 96000,
26 	[5] = 192000,
27 };
28 
29 /*
30  * See Table 5.7 – Sampling frequency for Multi-bit Audio
31  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
32  */
33 static const unsigned int avc_stream_rate_table[] = {
34 	[0] = 0x02,
35 	[1] = 0x03,
36 	[2] = 0x04,
37 	[3] = 0x0a,
38 	[4] = 0x05,
39 	[5] = 0x07,
40 };
41 
42 static int set_rate(struct snd_oxfw *oxfw, unsigned int rate)
43 {
44 	int err;
45 
46 	err = avc_general_set_sig_fmt(oxfw->unit, rate,
47 				      AVC_GENERAL_PLUG_DIR_IN, 0);
48 	if (err < 0)
49 		goto end;
50 
51 	if (oxfw->has_output)
52 		err = avc_general_set_sig_fmt(oxfw->unit, rate,
53 					      AVC_GENERAL_PLUG_DIR_OUT, 0);
54 end:
55 	return err;
56 }
57 
58 static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s,
59 			     unsigned int rate, unsigned int pcm_channels)
60 {
61 	u8 **formats;
62 	struct snd_oxfw_stream_formation formation;
63 	enum avc_general_plug_dir dir;
64 	unsigned int len;
65 	int i, err;
66 
67 	if (s == &oxfw->tx_stream) {
68 		formats = oxfw->tx_stream_formats;
69 		dir = AVC_GENERAL_PLUG_DIR_OUT;
70 	} else {
71 		formats = oxfw->rx_stream_formats;
72 		dir = AVC_GENERAL_PLUG_DIR_IN;
73 	}
74 
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);
78 		if (err < 0)
79 			return err;
80 
81 		if ((formation.rate == rate) && (formation.pcm == pcm_channels))
82 			break;
83 	}
84 	if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
85 		return -EINVAL;
86 
87 	/* If assumed, just change rate. */
88 	if (oxfw->assumed)
89 		return set_rate(oxfw, rate);
90 
91 	/* Calculate format length. */
92 	len = 5 + formats[i][4] * 2;
93 
94 	err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len);
95 	if (err < 0)
96 		return err;
97 
98 	/* Some requests just after changing format causes freezing. */
99 	msleep(100);
100 
101 	return 0;
102 }
103 
104 static void stop_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
105 {
106 	amdtp_stream_pcm_abort(stream);
107 	amdtp_stream_stop(stream);
108 
109 	if (stream == &oxfw->tx_stream)
110 		cmp_connection_break(&oxfw->out_conn);
111 	else
112 		cmp_connection_break(&oxfw->in_conn);
113 }
114 
115 static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream,
116 			unsigned int rate, unsigned int pcm_channels)
117 {
118 	u8 **formats;
119 	struct cmp_connection *conn;
120 	struct snd_oxfw_stream_formation formation;
121 	unsigned int i, midi_ports;
122 	int err;
123 
124 	if (stream == &oxfw->rx_stream) {
125 		formats = oxfw->rx_stream_formats;
126 		conn = &oxfw->in_conn;
127 	} else {
128 		formats = oxfw->tx_stream_formats;
129 		conn = &oxfw->out_conn;
130 	}
131 
132 	/* Get stream format */
133 	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
134 		if (formats[i] == NULL)
135 			break;
136 
137 		err = snd_oxfw_stream_parse_format(formats[i], &formation);
138 		if (err < 0)
139 			goto end;
140 		if (rate != formation.rate)
141 			continue;
142 		if (pcm_channels == 0 ||  pcm_channels == formation.pcm)
143 			break;
144 	}
145 	if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) {
146 		err = -EINVAL;
147 		goto end;
148 	}
149 
150 	pcm_channels = formation.pcm;
151 	midi_ports = DIV_ROUND_UP(formation.midi, 8);
152 
153 	/* The stream should have one pcm channels at least */
154 	if (pcm_channels == 0) {
155 		err = -EINVAL;
156 		goto end;
157 	}
158 	amdtp_stream_set_parameters(stream, rate, pcm_channels, midi_ports);
159 
160 	err = cmp_connection_establish(conn,
161 				       amdtp_stream_get_max_payload(stream));
162 	if (err < 0)
163 		goto end;
164 
165 	err = amdtp_stream_start(stream,
166 				 conn->resources.channel,
167 				 conn->speed);
168 	if (err < 0) {
169 		cmp_connection_break(conn);
170 		goto end;
171 	}
172 
173 	/* Wait first packet */
174 	if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
175 		stop_stream(oxfw, stream);
176 		err = -ETIMEDOUT;
177 	}
178 end:
179 	return err;
180 }
181 
182 static int check_connection_used_by_others(struct snd_oxfw *oxfw,
183 					   struct amdtp_stream *stream)
184 {
185 	struct cmp_connection *conn;
186 	bool used;
187 	int err;
188 
189 	if (stream == &oxfw->tx_stream)
190 		conn = &oxfw->out_conn;
191 	else
192 		conn = &oxfw->in_conn;
193 
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',
199 			conn->pcr_index);
200 		err = -EBUSY;
201 	}
202 
203 	return err;
204 }
205 
206 int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw,
207 				 struct amdtp_stream *stream)
208 {
209 	struct cmp_connection *conn;
210 	enum cmp_direction c_dir;
211 	enum amdtp_stream_direction s_dir;
212 	int err;
213 
214 	if (stream == &oxfw->tx_stream) {
215 		conn = &oxfw->out_conn;
216 		c_dir = CMP_OUTPUT;
217 		s_dir = AMDTP_IN_STREAM;
218 	} else {
219 		conn = &oxfw->in_conn;
220 		c_dir = CMP_INPUT;
221 		s_dir = AMDTP_OUT_STREAM;
222 	}
223 
224 	err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
225 	if (err < 0)
226 		goto end;
227 
228 	err = amdtp_stream_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING);
229 	if (err < 0) {
230 		amdtp_stream_destroy(stream);
231 		cmp_connection_destroy(conn);
232 		goto end;
233 	}
234 
235 	/* OXFW starts to transmit packets with non-zero dbc. */
236 	if (stream == &oxfw->tx_stream)
237 		oxfw->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK;
238 end:
239 	return err;
240 }
241 
242 int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw,
243 				  struct amdtp_stream *stream,
244 				  unsigned int rate, unsigned int pcm_channels)
245 {
246 	struct amdtp_stream *opposite;
247 	struct snd_oxfw_stream_formation formation;
248 	enum avc_general_plug_dir dir;
249 	unsigned int substreams, opposite_substreams;
250 	int err = 0;
251 
252 	if (stream == &oxfw->tx_stream) {
253 		substreams = oxfw->capture_substreams;
254 		opposite = &oxfw->rx_stream;
255 		opposite_substreams = oxfw->playback_substreams;
256 		dir = AVC_GENERAL_PLUG_DIR_OUT;
257 	} else {
258 		substreams = oxfw->playback_substreams;
259 		opposite_substreams = oxfw->capture_substreams;
260 
261 		if (oxfw->has_output)
262 			opposite = &oxfw->rx_stream;
263 		else
264 			opposite = NULL;
265 
266 		dir = AVC_GENERAL_PLUG_DIR_IN;
267 	}
268 
269 	if (substreams == 0)
270 		goto end;
271 
272 	/*
273 	 * Considering JACK/FFADO streaming:
274 	 * TODO: This can be removed hwdep functionality becomes popular.
275 	 */
276 	err = check_connection_used_by_others(oxfw, stream);
277 	if (err < 0)
278 		goto end;
279 
280 	/* packet queueing error */
281 	if (amdtp_streaming_error(stream))
282 		stop_stream(oxfw, stream);
283 
284 	err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
285 	if (err < 0)
286 		goto end;
287 	if (rate == 0)
288 		rate = formation.rate;
289 	if (pcm_channels == 0)
290 		pcm_channels = formation.pcm;
291 
292 	if ((formation.rate != rate) || (formation.pcm != pcm_channels)) {
293 		if (opposite != NULL) {
294 			err = check_connection_used_by_others(oxfw, opposite);
295 			if (err < 0)
296 				goto end;
297 			stop_stream(oxfw, opposite);
298 		}
299 		stop_stream(oxfw, stream);
300 
301 		err = set_stream_format(oxfw, stream, rate, pcm_channels);
302 		if (err < 0) {
303 			dev_err(&oxfw->unit->device,
304 				"fail to set stream format: %d\n", err);
305 			goto end;
306 		}
307 
308 		/* Start opposite stream if needed. */
309 		if (opposite && !amdtp_stream_running(opposite) &&
310 		    (opposite_substreams > 0)) {
311 			err = start_stream(oxfw, opposite, rate, 0);
312 			if (err < 0) {
313 				dev_err(&oxfw->unit->device,
314 					"fail to restart stream: %d\n", err);
315 				goto end;
316 			}
317 		}
318 	}
319 
320 	/* Start requested stream. */
321 	if (!amdtp_stream_running(stream)) {
322 		err = start_stream(oxfw, stream, rate, pcm_channels);
323 		if (err < 0)
324 			dev_err(&oxfw->unit->device,
325 				"fail to start stream: %d\n", err);
326 	}
327 end:
328 	return err;
329 }
330 
331 void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw,
332 				  struct amdtp_stream *stream)
333 {
334 	if (((stream == &oxfw->tx_stream) && (oxfw->capture_substreams > 0)) ||
335 	    ((stream == &oxfw->rx_stream) && (oxfw->playback_substreams > 0)))
336 		return;
337 
338 	stop_stream(oxfw, stream);
339 }
340 
341 /*
342  * This function should be called before starting the stream or after stopping
343  * the streams.
344  */
345 void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw,
346 				     struct amdtp_stream *stream)
347 {
348 	struct cmp_connection *conn;
349 
350 	if (stream == &oxfw->tx_stream)
351 		conn = &oxfw->out_conn;
352 	else
353 		conn = &oxfw->in_conn;
354 
355 	amdtp_stream_destroy(stream);
356 	cmp_connection_destroy(conn);
357 }
358 
359 void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw,
360 				    struct amdtp_stream *stream)
361 {
362 	struct cmp_connection *conn;
363 
364 	if (stream == &oxfw->tx_stream)
365 		conn = &oxfw->out_conn;
366 	else
367 		conn = &oxfw->in_conn;
368 
369 	if (cmp_connection_update(conn) < 0)
370 		stop_stream(oxfw, stream);
371 	else
372 		amdtp_stream_update(stream);
373 }
374 
375 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
376 				enum avc_general_plug_dir dir,
377 				struct snd_oxfw_stream_formation *formation)
378 {
379 	u8 *format;
380 	unsigned int len;
381 	int err;
382 
383 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
384 	format = kmalloc(len, GFP_KERNEL);
385 	if (format == NULL)
386 		return -ENOMEM;
387 
388 	err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
389 	if (err < 0)
390 		goto end;
391 	if (len < 3) {
392 		err = -EIO;
393 		goto end;
394 	}
395 
396 	err = snd_oxfw_stream_parse_format(format, formation);
397 end:
398 	kfree(format);
399 	return err;
400 }
401 
402 /*
403  * See Table 6.16 - AM824 Stream Format
404  *     Figure 6.19 - format_information field for AM824 Compound
405  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
406  * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
407  */
408 int snd_oxfw_stream_parse_format(u8 *format,
409 				 struct snd_oxfw_stream_formation *formation)
410 {
411 	unsigned int i, e, channels, type;
412 
413 	memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
414 
415 	/*
416 	 * this module can support a hierarchy combination that:
417 	 *  Root:	Audio and Music (0x90)
418 	 *  Level 1:	AM824 Compound  (0x40)
419 	 */
420 	if ((format[0] != 0x90) || (format[1] != 0x40))
421 		return -ENOSYS;
422 
423 	/* check the sampling rate */
424 	for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
425 		if (format[2] == avc_stream_rate_table[i])
426 			break;
427 	}
428 	if (i == ARRAY_SIZE(avc_stream_rate_table))
429 		return -ENOSYS;
430 
431 	formation->rate = oxfw_rate_table[i];
432 
433 	for (e = 0; e < format[4]; e++) {
434 		channels = format[5 + e * 2];
435 		type = format[6 + e * 2];
436 
437 		switch (type) {
438 		/* IEC 60958 Conformant, currently handled as MBLA */
439 		case 0x00:
440 		/* Multi Bit Linear Audio (Raw) */
441 		case 0x06:
442 			formation->pcm += channels;
443 			break;
444 		/* MIDI Conformant */
445 		case 0x0d:
446 			formation->midi = channels;
447 			break;
448 		/* IEC 61937-3 to 7 */
449 		case 0x01:
450 		case 0x02:
451 		case 0x03:
452 		case 0x04:
453 		case 0x05:
454 		/* Multi Bit Linear Audio */
455 		case 0x07:	/* DVD-Audio */
456 		case 0x0c:	/* High Precision */
457 		/* One Bit Audio */
458 		case 0x08:	/* (Plain) Raw */
459 		case 0x09:	/* (Plain) SACD */
460 		case 0x0a:	/* (Encoded) Raw */
461 		case 0x0b:	/* (Encoded) SACD */
462 		/* SMPTE Time-Code conformant */
463 		case 0x0e:
464 		/* Sample Count */
465 		case 0x0f:
466 		/* Anciliary Data */
467 		case 0x10:
468 		/* Synchronization Stream (Stereo Raw audio) */
469 		case 0x40:
470 		/* Don't care */
471 		case 0xff:
472 		default:
473 			return -ENOSYS;	/* not supported */
474 		}
475 	}
476 
477 	if (formation->pcm  > AMDTP_MAX_CHANNELS_FOR_PCM ||
478 	    formation->midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
479 		return -ENOSYS;
480 
481 	return 0;
482 }
483 
484 static int
485 assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
486 		      unsigned int pid, u8 *buf, unsigned int *len,
487 		      u8 **formats)
488 {
489 	struct snd_oxfw_stream_formation formation;
490 	unsigned int i, eid;
491 	int err;
492 
493 	/* get format at current sampling rate */
494 	err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
495 	if (err < 0) {
496 		dev_err(&oxfw->unit->device,
497 		"fail to get current stream format for isoc %s plug %d:%d\n",
498 			(dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
499 			pid, err);
500 		goto end;
501 	}
502 
503 	/* parse and set stream format */
504 	eid = 0;
505 	err = snd_oxfw_stream_parse_format(buf, &formation);
506 	if (err < 0)
507 		goto end;
508 
509 	formats[eid] = kmalloc(*len, GFP_KERNEL);
510 	if (formats[eid] == NULL) {
511 		err = -ENOMEM;
512 		goto end;
513 	}
514 	memcpy(formats[eid], buf, *len);
515 
516 	/* apply the format for each available sampling rate */
517 	for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
518 		if (formation.rate == oxfw_rate_table[i])
519 			continue;
520 
521 		err = avc_general_inquiry_sig_fmt(oxfw->unit,
522 						  oxfw_rate_table[i],
523 						  dir, pid);
524 		if (err < 0)
525 			continue;
526 
527 		eid++;
528 		formats[eid] = kmalloc(*len, GFP_KERNEL);
529 		if (formats[eid] == NULL) {
530 			err = -ENOMEM;
531 			goto end;
532 		}
533 		memcpy(formats[eid], buf, *len);
534 		formats[eid][2] = avc_stream_rate_table[i];
535 	}
536 
537 	err = 0;
538 	oxfw->assumed = true;
539 end:
540 	return err;
541 }
542 
543 static int fill_stream_formats(struct snd_oxfw *oxfw,
544 			       enum avc_general_plug_dir dir,
545 			       unsigned short pid)
546 {
547 	u8 *buf, **formats;
548 	unsigned int len, eid = 0;
549 	struct snd_oxfw_stream_formation dummy;
550 	int err;
551 
552 	buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
553 	if (buf == NULL)
554 		return -ENOMEM;
555 
556 	if (dir == AVC_GENERAL_PLUG_DIR_OUT)
557 		formats = oxfw->tx_stream_formats;
558 	else
559 		formats = oxfw->rx_stream_formats;
560 
561 	/* get first entry */
562 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
563 	err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
564 	if (err == -ENOSYS) {
565 		/* LIST subfunction is not implemented */
566 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
567 		err = assume_stream_formats(oxfw, dir, pid, buf, &len,
568 					    formats);
569 		goto end;
570 	} else if (err < 0) {
571 		dev_err(&oxfw->unit->device,
572 			"fail to get stream format %d for isoc %s plug %d:%d\n",
573 			eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
574 			pid, err);
575 		goto end;
576 	}
577 
578 	/* LIST subfunction is implemented */
579 	while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
580 		/* The format is too short. */
581 		if (len < 3) {
582 			err = -EIO;
583 			break;
584 		}
585 
586 		/* parse and set stream format */
587 		err = snd_oxfw_stream_parse_format(buf, &dummy);
588 		if (err < 0)
589 			break;
590 
591 		formats[eid] = kmalloc(len, GFP_KERNEL);
592 		if (formats[eid] == NULL) {
593 			err = -ENOMEM;
594 			break;
595 		}
596 		memcpy(formats[eid], buf, len);
597 
598 		/* get next entry */
599 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
600 		err = avc_stream_get_format_list(oxfw->unit, dir, 0,
601 						 buf, &len, ++eid);
602 		/* No entries remained. */
603 		if (err == -EINVAL) {
604 			err = 0;
605 			break;
606 		} else if (err < 0) {
607 			dev_err(&oxfw->unit->device,
608 			"fail to get stream format %d for isoc %s plug %d:%d\n",
609 				eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
610 									"out",
611 				pid, err);
612 			break;
613 		}
614 	}
615 end:
616 	kfree(buf);
617 	return err;
618 }
619 
620 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
621 {
622 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
623 	int err;
624 
625 	/* the number of plugs for isoc in/out, ext in/out  */
626 	err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
627 	if (err < 0) {
628 		dev_err(&oxfw->unit->device,
629 		"fail to get info for isoc/external in/out plugs: %d\n",
630 			err);
631 		goto end;
632 	} else if ((plugs[0] == 0) && (plugs[1] == 0)) {
633 		err = -ENOSYS;
634 		goto end;
635 	}
636 
637 	/* use oPCR[0] if exists */
638 	if (plugs[1] > 0) {
639 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
640 		if (err < 0)
641 			goto end;
642 		oxfw->has_output = true;
643 	}
644 
645 	/* use iPCR[0] if exists */
646 	if (plugs[0] > 0)
647 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
648 end:
649 	return err;
650 }
651 
652 void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
653 {
654 	oxfw->dev_lock_changed = true;
655 	wake_up(&oxfw->hwdep_wait);
656 }
657 
658 int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
659 {
660 	int err;
661 
662 	spin_lock_irq(&oxfw->lock);
663 
664 	/* user land lock this */
665 	if (oxfw->dev_lock_count < 0) {
666 		err = -EBUSY;
667 		goto end;
668 	}
669 
670 	/* this is the first time */
671 	if (oxfw->dev_lock_count++ == 0)
672 		snd_oxfw_stream_lock_changed(oxfw);
673 	err = 0;
674 end:
675 	spin_unlock_irq(&oxfw->lock);
676 	return err;
677 }
678 
679 void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
680 {
681 	spin_lock_irq(&oxfw->lock);
682 
683 	if (WARN_ON(oxfw->dev_lock_count <= 0))
684 		goto end;
685 	if (--oxfw->dev_lock_count == 0)
686 		snd_oxfw_stream_lock_changed(oxfw);
687 end:
688 	spin_unlock_irq(&oxfw->lock);
689 }
690