ALSA: oxfw: Add support for capture/playback MIDI messages
[deliverable/linux.git] / sound / firewire / oxfw / oxfw-stream.c
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 i, err, len;
65
66 if (s == &oxfw->tx_stream) {
67 formats = oxfw->tx_stream_formats;
68 dir = AVC_GENERAL_PLUG_DIR_OUT;
69 } else {
70 formats = oxfw->rx_stream_formats;
71 dir = AVC_GENERAL_PLUG_DIR_IN;
72 }
73
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);
77 if (err < 0)
78 return err;
79
80 if ((formation.rate == rate) && (formation.pcm == pcm_channels))
81 break;
82 }
83 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
84 return -EINVAL;
85
86 /* If assumed, just change rate. */
87 if (oxfw->assumed)
88 return set_rate(oxfw, rate);
89
90 /* Calculate format length. */
91 len = 5 + formats[i][4] * 2;
92
93 err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len);
94 if (err < 0)
95 return err;
96
97 /* Some requests just after changing format causes freezing. */
98 msleep(100);
99
100 return 0;
101 }
102
103 static void stop_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
104 {
105 amdtp_stream_pcm_abort(stream);
106 amdtp_stream_stop(stream);
107
108 if (stream == &oxfw->tx_stream)
109 cmp_connection_break(&oxfw->out_conn);
110 else
111 cmp_connection_break(&oxfw->in_conn);
112 }
113
114 static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream,
115 unsigned int rate, unsigned int pcm_channels)
116 {
117 u8 **formats;
118 struct cmp_connection *conn;
119 struct snd_oxfw_stream_formation formation;
120 unsigned int i, midi_ports;
121 int err;
122
123 if (stream == &oxfw->rx_stream) {
124 formats = oxfw->rx_stream_formats;
125 conn = &oxfw->in_conn;
126 } else {
127 formats = oxfw->tx_stream_formats;
128 conn = &oxfw->out_conn;
129 }
130
131 /* Get stream format */
132 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
133 if (formats[i] == NULL)
134 break;
135
136 err = snd_oxfw_stream_parse_format(formats[i], &formation);
137 if (err < 0)
138 goto end;
139 if (rate != formation.rate)
140 continue;
141 if (pcm_channels == 0 || pcm_channels == formation.pcm)
142 break;
143 }
144 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) {
145 err = -EINVAL;
146 goto end;
147 }
148
149 pcm_channels = formation.pcm;
150 midi_ports = DIV_ROUND_UP(formation.midi, 8);
151
152 /* The stream should have one pcm channels at least */
153 if (pcm_channels == 0) {
154 err = -EINVAL;
155 goto end;
156 }
157 amdtp_stream_set_parameters(stream, rate, pcm_channels, midi_ports);
158
159 err = cmp_connection_establish(conn,
160 amdtp_stream_get_max_payload(stream));
161 if (err < 0)
162 goto end;
163
164 err = amdtp_stream_start(stream,
165 conn->resources.channel,
166 conn->speed);
167 if (err < 0) {
168 cmp_connection_break(conn);
169 goto end;
170 }
171
172 /* Wait first packet */
173 err = amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT);
174 if (err < 0)
175 stop_stream(oxfw, stream);
176 end:
177 return err;
178 }
179
180 static int check_connection_used_by_others(struct snd_oxfw *oxfw,
181 struct amdtp_stream *stream)
182 {
183 struct cmp_connection *conn;
184 bool used;
185 int err;
186
187 if (stream == &oxfw->tx_stream)
188 conn = &oxfw->out_conn;
189 else
190 conn = &oxfw->in_conn;
191
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',
197 conn->pcr_index);
198 err = -EBUSY;
199 }
200
201 return err;
202 }
203
204 int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw,
205 struct amdtp_stream *stream)
206 {
207 struct cmp_connection *conn;
208 enum cmp_direction c_dir;
209 enum amdtp_stream_direction s_dir;
210 int err;
211
212 if (stream == &oxfw->tx_stream) {
213 conn = &oxfw->out_conn;
214 c_dir = CMP_OUTPUT;
215 s_dir = AMDTP_IN_STREAM;
216 } else {
217 conn = &oxfw->in_conn;
218 c_dir = CMP_INPUT;
219 s_dir = AMDTP_OUT_STREAM;
220 }
221
222 err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
223 if (err < 0)
224 goto end;
225
226 err = amdtp_stream_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING);
227 if (err < 0) {
228 amdtp_stream_destroy(stream);
229 cmp_connection_destroy(conn);
230 goto end;
231 }
232
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;
236 end:
237 return err;
238 }
239
240 int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw,
241 struct amdtp_stream *stream,
242 unsigned int rate, unsigned int pcm_channels)
243 {
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;
248 int err = 0;
249
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;
255 } else {
256 substreams = oxfw->playback_substreams;
257 opposite_substreams = oxfw->capture_substreams;
258
259 if (oxfw->has_output)
260 opposite = &oxfw->rx_stream;
261 else
262 opposite = NULL;
263
264 dir = AVC_GENERAL_PLUG_DIR_IN;
265 }
266
267 if (substreams == 0)
268 goto end;
269
270 /*
271 * Considering JACK/FFADO streaming:
272 * TODO: This can be removed hwdep functionality becomes popular.
273 */
274 err = check_connection_used_by_others(oxfw, stream);
275 if (err < 0)
276 goto end;
277
278 /* packet queueing error */
279 if (amdtp_streaming_error(stream))
280 stop_stream(oxfw, stream);
281
282 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
283 if (err < 0)
284 goto end;
285 if (rate == 0)
286 rate = formation.rate;
287 if (pcm_channels == 0)
288 pcm_channels = formation.pcm;
289
290 if ((formation.rate != rate) || (formation.pcm != pcm_channels)) {
291 if (opposite != NULL) {
292 err = check_connection_used_by_others(oxfw, opposite);
293 if (err < 0)
294 goto end;
295 stop_stream(oxfw, opposite);
296 }
297 stop_stream(oxfw, stream);
298
299 err = set_stream_format(oxfw, stream, rate, pcm_channels);
300 if (err < 0) {
301 dev_err(&oxfw->unit->device,
302 "fail to set stream format: %d\n", err);
303 goto end;
304 }
305
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);
310 if (err < 0) {
311 dev_err(&oxfw->unit->device,
312 "fail to restart stream: %d\n", err);
313 goto end;
314 }
315 }
316 }
317
318 /* Start requested stream. */
319 if (!amdtp_stream_running(stream)) {
320 err = start_stream(oxfw, stream, rate, pcm_channels);
321 if (err < 0)
322 dev_err(&oxfw->unit->device,
323 "fail to start stream: %d\n", err);
324 }
325 end:
326 return err;
327 }
328
329 void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw,
330 struct amdtp_stream *stream)
331 {
332 if (((stream == &oxfw->tx_stream) && (oxfw->capture_substreams > 0)) ||
333 ((stream == &oxfw->rx_stream) && (oxfw->playback_substreams > 0)))
334 return;
335
336 stop_stream(oxfw, stream);
337 }
338
339 void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw,
340 struct amdtp_stream *stream)
341 {
342 struct cmp_connection *conn;
343
344 if (stream == &oxfw->tx_stream)
345 conn = &oxfw->out_conn;
346 else
347 conn = &oxfw->in_conn;
348
349 stop_stream(oxfw, stream);
350
351 amdtp_stream_destroy(stream);
352 cmp_connection_destroy(conn);
353 }
354
355 void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw,
356 struct amdtp_stream *stream)
357 {
358 struct cmp_connection *conn;
359
360 if (stream == &oxfw->tx_stream)
361 conn = &oxfw->out_conn;
362 else
363 conn = &oxfw->in_conn;
364
365 if (cmp_connection_update(conn) < 0)
366 stop_stream(oxfw, stream);
367 else
368 amdtp_stream_update(stream);
369 }
370
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)
374 {
375 u8 *format;
376 unsigned int len;
377 int err;
378
379 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
380 format = kmalloc(len, GFP_KERNEL);
381 if (format == NULL)
382 return -ENOMEM;
383
384 err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
385 if (err < 0)
386 goto end;
387 if (len < 3) {
388 err = -EIO;
389 goto end;
390 }
391
392 err = snd_oxfw_stream_parse_format(format, formation);
393 end:
394 kfree(format);
395 return err;
396 }
397
398 /*
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
403 */
404 int snd_oxfw_stream_parse_format(u8 *format,
405 struct snd_oxfw_stream_formation *formation)
406 {
407 unsigned int i, e, channels, type;
408
409 memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
410
411 /*
412 * this module can support a hierarchy combination that:
413 * Root: Audio and Music (0x90)
414 * Level 1: AM824 Compound (0x40)
415 */
416 if ((format[0] != 0x90) || (format[1] != 0x40))
417 return -ENOSYS;
418
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])
422 break;
423 }
424 if (i == ARRAY_SIZE(avc_stream_rate_table))
425 return -ENOSYS;
426
427 formation->rate = oxfw_rate_table[i];
428
429 for (e = 0; e < format[4]; e++) {
430 channels = format[5 + e * 2];
431 type = format[6 + e * 2];
432
433 switch (type) {
434 /* IEC 60958 Conformant, currently handled as MBLA */
435 case 0x00:
436 /* Multi Bit Linear Audio (Raw) */
437 case 0x06:
438 formation->pcm += channels;
439 break;
440 /* MIDI Conformant */
441 case 0x0d:
442 formation->midi = channels;
443 break;
444 /* IEC 61937-3 to 7 */
445 case 0x01:
446 case 0x02:
447 case 0x03:
448 case 0x04:
449 case 0x05:
450 /* Multi Bit Linear Audio */
451 case 0x07: /* DVD-Audio */
452 case 0x0c: /* High Precision */
453 /* One Bit Audio */
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 */
459 case 0x0e:
460 /* Sample Count */
461 case 0x0f:
462 /* Anciliary Data */
463 case 0x10:
464 /* Synchronization Stream (Stereo Raw audio) */
465 case 0x40:
466 /* Don't care */
467 case 0xff:
468 default:
469 return -ENOSYS; /* not supported */
470 }
471 }
472
473 if (formation->pcm > AMDTP_MAX_CHANNELS_FOR_PCM ||
474 formation->midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
475 return -ENOSYS;
476
477 return 0;
478 }
479
480 static int
481 assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
482 unsigned int pid, u8 *buf, unsigned int *len,
483 u8 **formats)
484 {
485 struct snd_oxfw_stream_formation formation;
486 unsigned int i, eid;
487 int err;
488
489 /* get format at current sampling rate */
490 err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
491 if (err < 0) {
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",
495 pid, err);
496 goto end;
497 }
498
499 /* parse and set stream format */
500 eid = 0;
501 err = snd_oxfw_stream_parse_format(buf, &formation);
502 if (err < 0)
503 goto end;
504
505 formats[eid] = kmalloc(*len, GFP_KERNEL);
506 if (formats[eid] == NULL) {
507 err = -ENOMEM;
508 goto end;
509 }
510 memcpy(formats[eid], buf, *len);
511
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])
515 continue;
516
517 err = avc_general_inquiry_sig_fmt(oxfw->unit,
518 oxfw_rate_table[i],
519 dir, pid);
520 if (err < 0)
521 continue;
522
523 eid++;
524 formats[eid] = kmalloc(*len, GFP_KERNEL);
525 if (formats[eid] == NULL) {
526 err = -ENOMEM;
527 goto end;
528 }
529 memcpy(formats[eid], buf, *len);
530 formats[eid][2] = avc_stream_rate_table[i];
531 }
532
533 err = 0;
534 oxfw->assumed = true;
535 end:
536 return err;
537 }
538
539 static int fill_stream_formats(struct snd_oxfw *oxfw,
540 enum avc_general_plug_dir dir,
541 unsigned short pid)
542 {
543 u8 *buf, **formats;
544 unsigned int len, eid = 0;
545 struct snd_oxfw_stream_formation dummy;
546 int err;
547
548 buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
549 if (buf == NULL)
550 return -ENOMEM;
551
552 if (dir == AVC_GENERAL_PLUG_DIR_OUT)
553 formats = oxfw->tx_stream_formats;
554 else
555 formats = oxfw->rx_stream_formats;
556
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,
564 formats);
565 goto end;
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",
570 pid, err);
571 goto end;
572 }
573
574 /* LIST subfunction is implemented */
575 while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
576 /* The format is too short. */
577 if (len < 3) {
578 err = -EIO;
579 break;
580 }
581
582 /* parse and set stream format */
583 err = snd_oxfw_stream_parse_format(buf, &dummy);
584 if (err < 0)
585 break;
586
587 formats[eid] = kmalloc(len, GFP_KERNEL);
588 if (formats[eid] == NULL) {
589 err = -ENOMEM;
590 break;
591 }
592 memcpy(formats[eid], buf, len);
593
594 /* get next entry */
595 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
596 err = avc_stream_get_format_list(oxfw->unit, dir, 0,
597 buf, &len, ++eid);
598 /* No entries remained. */
599 if (err == -EINVAL) {
600 err = 0;
601 break;
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" :
606 "out",
607 pid, err);
608 break;
609 }
610 }
611 end:
612 kfree(buf);
613 return err;
614 }
615
616 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
617 {
618 u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
619 int err;
620
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);
623 if (err < 0) {
624 dev_err(&oxfw->unit->device,
625 "fail to get info for isoc/external in/out plugs: %d\n",
626 err);
627 goto end;
628 } else if ((plugs[0] == 0) && (plugs[1] == 0)) {
629 err = -ENOSYS;
630 goto end;
631 }
632
633 /* use oPCR[0] if exists */
634 if (plugs[1] > 0) {
635 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
636 if (err < 0)
637 goto end;
638 oxfw->has_output = true;
639 }
640
641 /* use iPCR[0] if exists */
642 if (plugs[0] > 0)
643 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
644 end:
645 return err;
646 }
This page took 0.04467 seconds and 5 git commands to generate.