Debian Codegraph @

aaf/pcm_aaf.c

raw symbols SCIP
1 /*
2 * AVTP Audio Format (AAF) PCM Plugin
3 *
4 * Copyright (c) 2018, Intel Corporation
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <alsa/asoundlib.h>
22 #include <alsa/pcm_external.h>
23 #include <arpa/inet.h>
24 #include <avtp.h>
25 #include <avtp_aaf.h>
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <linux/if_ether.h>
29 #include <linux/if_packet.h>
30 #include <linux/net_tstamp.h>
31 #include <net/if.h>
32 #include <string.h>
33 #include <stdbool.h>
34 #include <stdint.h>
35 #include <sys/ioctl.h>
36 #include <sys/timerfd.h>
37
38 #ifdef AAF_DEBUG
39 #define pr_debug(...) SNDERR(__VA_ARGS__)
40 #else
41 #define pr_debug(...) (void)0
42 #endif
43
44 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
45
46 #define NSEC_PER_USEC 1000
47 #define NSEC_PER_SEC 1000000000
48 #define TAI_OFFSET (37ULL * NSEC_PER_SEC)
49 #define TAI_TO_UTC(t) (t - TAI_OFFSET)
50
51 #define FD_COUNT_PLAYBACK 1
52 #define FD_COUNT_CAPTURE 2
53
54 typedef struct {
55 snd_pcm_ioplug_t io;
56
57 char ifname[IFNAMSIZ];
58 unsigned char addr[ETH_ALEN];
59 int prio;
60 uint64_t streamid;
61 int mtt;
62 int t_uncertainty;
63 snd_pcm_uframes_t frames_per_pdu;
64 int ptime_tolerance;
65
66 int sk_fd;
67 int timer_fd;
68
69 struct sockaddr_ll sk_addr;
70
71 struct avtp_stream_pdu *pdu;
72 int pdu_size;
73 uint8_t pdu_seq;
74
75 struct msghdr *msg;
76 struct cmsghdr *cmsg;
77
78 uint64_t timer_starttime;
79 uint64_t timer_period;
80 uint64_t timer_expirations;
81
82 const snd_pcm_channel_area_t *audiobuf_areas;
83 snd_pcm_channel_area_t *payload_areas;
84
85 snd_pcm_uframes_t hw_ptr;
86 snd_pcm_uframes_t hw_virt_ptr;
87 snd_pcm_uframes_t boundary;
88
89 uint64_t prev_ptime;
90
91 int pdu_period;
92 } snd_pcm_aaf_t;
93
94 static unsigned int alsa_to_avtp_format(snd_pcm_format_t format)
95 {
96 switch (format) {
97 case SND_PCM_FORMAT_S16_BE:
98 return AVTP_AAF_FORMAT_INT_16BIT;
99 case SND_PCM_FORMAT_S24_3BE:
100 return AVTP_AAF_FORMAT_INT_24BIT;
101 case SND_PCM_FORMAT_S32_BE:
102 return AVTP_AAF_FORMAT_INT_32BIT;
103 case SND_PCM_FORMAT_FLOAT_BE:
104 return AVTP_AAF_FORMAT_FLOAT_32BIT;
105 default:
106 return AVTP_AAF_FORMAT_USER;
107 }
108 }
109
110 static unsigned int alsa_to_avtp_rate(unsigned int rate)
111 {
112 switch (rate) {
113 case 8000:
114 return AVTP_AAF_PCM_NSR_8KHZ;
115 case 16000:
116 return AVTP_AAF_PCM_NSR_16KHZ;
117 case 24000:
118 return AVTP_AAF_PCM_NSR_24KHZ;
119 case 32000:
120 return AVTP_AAF_PCM_NSR_32KHZ;
121 case 44100:
122 return AVTP_AAF_PCM_NSR_44_1KHZ;
123 case 48000:
124 return AVTP_AAF_PCM_NSR_48KHZ;
125 case 88200:
126 return AVTP_AAF_PCM_NSR_88_2KHZ;
127 case 96000:
128 return AVTP_AAF_PCM_NSR_96KHZ;
129 case 176400:
130 return AVTP_AAF_PCM_NSR_176_4KHZ;
131 case 192000:
132 return AVTP_AAF_PCM_NSR_192KHZ;
133 default:
134 return AVTP_AAF_PCM_NSR_USER;
135 }
136 }
137
138 static int aaf_load_config(snd_pcm_aaf_t *aaf, snd_config_t *conf)
139 {
140 snd_config_iterator_t cur, next;
141
142 snd_config_for_each(cur, next, conf) {
143 snd_config_t *entry = snd_config_iterator_entry(cur);
144 const char *id;
145
146 if (snd_config_get_id(entry, &id) < 0)
147 goto err;
148
149 if (strcmp(id, "comment") == 0 ||
150 strcmp(id, "type") == 0 ||
151 strcmp(id, "hint") == 0)
152 continue;
153
154 if (strcmp(id, "ifname") == 0) {
155 const char *ifname;
156
157 if (snd_config_get_string(entry, &ifname) < 0)
158 goto err;
159
160 snprintf(aaf->ifname, sizeof(aaf->ifname), "%s",
161 ifname);
162 } else if (strcmp(id, "addr") == 0) {
163 const char *addr;
164 int n;
165
166 if (snd_config_get_string(entry, &addr) < 0)
167 goto err;
168
169 n = sscanf(addr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
170 &aaf->addr[0], &aaf->addr[1],
171 &aaf->addr[2], &aaf->addr[3],
172 &aaf->addr[4], &aaf->addr[5]);
173 if (n != 6)
174 goto err;
175 } else if (strcmp(id, "prio") == 0) {
176 long prio;
177
178 if (snd_config_get_integer(entry, &prio) < 0)
179 goto err;
180
181 if (prio < 0)
182 goto err;
183
184 aaf->prio = prio;
185 } else if (strcmp(id, "streamid") == 0) {
186 const char *streamid;
187 unsigned char addr[6];
188 unsigned short unique_id;
189 int n;
190
191 if (snd_config_get_string(entry, &streamid) < 0)
192 goto err;
193
194 n = sscanf(streamid,
195 "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hx",
196 &addr[0], &addr[1], &addr[2], &addr[3],
197 &addr[4], &addr[5], &unique_id);
198 if (n != 7)
199 goto err;
200
201 aaf->streamid = (uint64_t) addr[0] << 56 |
202 (uint64_t) addr[1] << 48 |
203 (uint64_t) addr[2] << 40 |
204 (uint64_t) addr[3] << 32 |
205 (uint64_t) addr[4] << 24 |
206 (uint64_t) addr[5] << 16 |
207 unique_id;
208 } else if (strcmp(id, "mtt") == 0) {
209 long mtt;
210
211 if (snd_config_get_integer(entry, &mtt) < 0)
212 goto err;
213
214 if (mtt < 0)
215 goto err;
216
217 aaf->mtt = mtt * NSEC_PER_USEC;
218 } else if (strcmp(id, "time_uncertainty") == 0) {
219 long t_uncertainty;
220
221 if (snd_config_get_integer(entry, &t_uncertainty) < 0)
222 goto err;
223
224 if (t_uncertainty < 0)
225 goto err;
226
227 aaf->t_uncertainty = t_uncertainty * NSEC_PER_USEC;
228 } else if (strcmp(id, "frames_per_pdu") == 0) {
229 long frames_per_pdu;
230
231 if (snd_config_get_integer(entry, &frames_per_pdu) < 0)
232 goto err;
233
234 if (frames_per_pdu < 0)
235 goto err;
236
237 aaf->frames_per_pdu = frames_per_pdu;
238 } else if (strcmp(id, "ptime_tolerance") == 0) {
239 long ptime_tolerance;
240
241 if (snd_config_get_integer(entry,
242 &ptime_tolerance) < 0)
243 goto err;
244
245 if (ptime_tolerance < 0)
246 goto err;
247
248 aaf->ptime_tolerance = ptime_tolerance * NSEC_PER_USEC;
249 } else {
250 SNDERR("Invalid configuration: %s", id);
251 goto err;
252 }
253 }
254
255 return 0;
256
257 err:
258 SNDERR("Error loading device configuration");
259 return -EINVAL;
260 }
261
262 static int aaf_init_socket(snd_pcm_aaf_t *aaf)
263 {
264 int fd, res;
265 struct ifreq req;
266 snd_pcm_ioplug_t *io = &aaf->io;
267
268 fd = socket(AF_PACKET, SOCK_DGRAM|SOCK_NONBLOCK, htons(ETH_P_TSN));
269 if (fd < 0) {
270 SNDERR("Failed to open AF_PACKET socket");
271 return -errno;
272 }
273
274 snprintf(req.ifr_name, sizeof(req.ifr_name), "%s", aaf->ifname);
275 res = ioctl(fd, SIOCGIFINDEX, &req);
276 if (res < 0) {
277 SNDERR("Failed to get network interface index");
278 res = -errno;
279 goto err;
280 }
281
282 aaf->sk_addr.sll_family = AF_PACKET;
283 aaf->sk_addr.sll_protocol = htons(ETH_P_TSN);
284 aaf->sk_addr.sll_halen = ETH_ALEN;
285 aaf->sk_addr.sll_ifindex = req.ifr_ifindex;
286 memcpy(&aaf->sk_addr.sll_addr, aaf->addr, ETH_ALEN);
287
288 if (io->stream == SND_PCM_STREAM_PLAYBACK) {
289 struct sock_txtime txtime_cfg;
290
291 res = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &aaf->prio,
292 sizeof(aaf->prio));
293 if (res < 0) {
294 SNDERR("Failed to set socket priority");
295 res = -errno;
296 goto err;
297 }
298
299 txtime_cfg.clockid = CLOCK_TAI;
300 txtime_cfg.flags = 0;
301 res = setsockopt(fd, SOL_SOCKET, SO_TXTIME, &txtime_cfg,
302 sizeof(txtime_cfg));
303 if (res < 0) {
304 SNDERR("Failed to configure txtime");
305 res = -errno;
306 goto err;
307 }
308 } else {
309 struct packet_mreq mreq = { 0 };
310
311 res = bind(fd, (struct sockaddr *) &aaf->sk_addr,
312 sizeof(aaf->sk_addr));
313 if (res < 0) {
314 SNDERR("Failed to bind socket");
315 res = -errno;
316 goto err;
317 }
318
319 mreq.mr_ifindex = req.ifr_ifindex;
320 mreq.mr_type = PACKET_MR_MULTICAST;
321 mreq.mr_alen = ETH_ALEN;
322 memcpy(&mreq.mr_address, aaf->addr, ETH_ALEN);
323 res = setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
324 &mreq, sizeof(struct packet_mreq));
325 if (res < 0) {
326 SNDERR("Failed to add multicast address");
327 res = -errno;
328 goto err;
329 }
330 }
331
332 aaf->sk_fd = fd;
333 return 0;
334
335 err:
336 close(fd);
337 return res;
338 }
339
340 static int aaf_init_timer(snd_pcm_aaf_t *aaf)
341 {
342 int fd;
343
344 fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
345 if (fd < 0)
346 return -errno;
347
348 aaf->timer_fd = fd;
349 return 0;
350 }
351
352 static int aaf_init_pdu(snd_pcm_aaf_t *aaf)
353 {
354 int res;
355 struct avtp_stream_pdu *pdu;
356 ssize_t frame_size, payload_size, pdu_size;
357 snd_pcm_ioplug_t *io = &aaf->io;
358
359 frame_size = snd_pcm_format_size(io->format, io->channels);
360 if (frame_size < 0)
361 return frame_size;
362
363 payload_size = frame_size * aaf->frames_per_pdu;
364 pdu_size = sizeof(*pdu) + payload_size;
365 pdu = calloc(1, pdu_size);
366 if (!pdu)
367 return -ENOMEM;
368
369 if (io->stream == SND_PCM_STREAM_PLAYBACK) {
370 res = avtp_aaf_pdu_init(pdu);
371 if (res < 0)
372 goto err;
373
374 res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_TV, 1);
375 if (res < 0)
376 goto err;
377
378 res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_STREAM_ID,
379 aaf->streamid);
380 if (res < 0)
381 goto err;
382
383 res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_FORMAT,
384 alsa_to_avtp_format(io->format));
385 if (res < 0)
386 goto err;
387
388 res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_NSR,
389 alsa_to_avtp_rate(io->rate));
390 if (res < 0)
391 goto err;
392
393 res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_CHAN_PER_FRAME,
394 io->channels);
395 if (res < 0)
396 goto err;
397
398 res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_BIT_DEPTH,
399 snd_pcm_format_width(io->format));
400 if (res < 0)
401 goto err;
402
403 res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_STREAM_DATA_LEN,
404 payload_size);
405 if (res < 0)
406 goto err;
407
408 res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_SP,
409 AVTP_AAF_PCM_SP_NORMAL);
410 if (res < 0)
411 goto err;
412 }
413
414 aaf->pdu = pdu;
415 aaf->pdu_size = pdu_size;
416 return 0;
417
418 err:
419 free(pdu);
420 return res;
421 }
422
423 static int aaf_init_areas(snd_pcm_aaf_t *aaf, snd_pcm_channel_area_t *areas,
424 void *buf)
425 {
426 ssize_t sample_size, frame_size;
427 snd_pcm_ioplug_t *io = &aaf->io;
428
429 sample_size = snd_pcm_format_size(io->format, 1);
430 if (sample_size < 0)
431 return sample_size;
432
433 frame_size = sample_size * io->channels;
434
435 for (unsigned int i = 0; i < io->channels; i++) {
436 areas[i].addr = buf;
437 areas[i].first = i * sample_size * 8;
438 areas[i].step = frame_size * 8;
439 }
440
441 return 0;
442 }
443
444 static int aaf_init_payload_areas(snd_pcm_aaf_t *aaf)
445 {
446 int res;
447 snd_pcm_channel_area_t *areas;
448 snd_pcm_ioplug_t *io = &aaf->io;
449
450 areas = calloc(io->channels, sizeof(snd_pcm_channel_area_t));
451 if (!areas)
452 return -ENOMEM;
453
454 res = aaf_init_areas(aaf, areas, aaf->pdu->avtp_payload);
455 if (res < 0)
456 goto err;
457
458 aaf->payload_areas = areas;
459 return 0;
460
461 err:
462 free(areas);
463 return res;
464 }
465
466 static int aaf_init_msghdr(snd_pcm_aaf_t *aaf)
467 {
468 int res;
469 struct iovec *iov;
470 char *control;
471 size_t controllen;
472 struct msghdr *msg;
473 struct cmsghdr *cmsg;
474
475 iov = malloc(sizeof(struct iovec));
476 if (!iov) {
477 SNDERR("Failed to allocate iovec");
478 return -ENOMEM;
479 }
480
481 iov->iov_base = aaf->pdu;
482 iov->iov_len = aaf->pdu_size;
483
484 controllen = CMSG_SPACE(sizeof(__u64));
485 control = malloc(controllen);
486 if (!control) {
487 SNDERR("Failed to allocate control buffer");
488 res = -ENOMEM;
489 goto err_free_iov;
490 }
491
492 msg = malloc(sizeof(struct msghdr));
493 if (!msg) {
494 SNDERR("Failed to allocate msghdr");
495 res = -ENOMEM;
496 goto err_free_control;
497 }
498
499 msg->msg_name = &aaf->sk_addr;
500 msg->msg_namelen = sizeof(aaf->sk_addr);
501 msg->msg_iov = iov;
502 msg->msg_iovlen = 1;
503 msg->msg_control = control;
504 msg->msg_controllen = controllen;
505
506 cmsg = CMSG_FIRSTHDR(msg);
507 cmsg->cmsg_level = SOL_SOCKET;
508 cmsg->cmsg_type = SCM_TXTIME;
509 cmsg->cmsg_len = CMSG_LEN(sizeof(__u64));
510
511 aaf->msg = msg;
512 aaf->cmsg = cmsg;
513 return 0;
514
515 err_free_control:
516 free(control);
517 err_free_iov:
518 free(iov);
519 return res;
520 }
521
522 static void aaf_inc_ptr(snd_pcm_uframes_t *ptr, snd_pcm_uframes_t val,
523 snd_pcm_uframes_t boundary)
524 {
525 *ptr += val;
526
527 if (*ptr > boundary)
528 *ptr -= boundary;
529 }
530
531 static int aaf_mclk_start(snd_pcm_aaf_t *aaf, uint64_t time, uint64_t period)
532 {
533 int res;
534 struct itimerspec itspec;
535 uint64_t time_utc;
536
537 aaf->timer_expirations = 0;
538 aaf->timer_period = period;
539 aaf->timer_starttime = time;
540
541 time_utc = TAI_TO_UTC(time);
542 itspec.it_value.tv_sec = time_utc / NSEC_PER_SEC;
543 itspec.it_value.tv_nsec = time_utc % NSEC_PER_SEC;
544 itspec.it_interval.tv_sec = 0;
545 itspec.it_interval.tv_nsec = aaf->timer_period;
546 res = timerfd_settime(aaf->timer_fd, TFD_TIMER_ABSTIME, &itspec, NULL);
547 if (res < 0)
548 return -errno;
549
550 return 0;
551 }
552
553 static int aaf_mclk_start_playback(snd_pcm_aaf_t *aaf)
554 {
555 int res;
556 struct timespec now;
557 uint64_t time, period;
558 snd_pcm_ioplug_t *io = &aaf->io;
559
560 res = clock_gettime(CLOCK_TAI, &now);
561 if (res < 0) {
562 SNDERR("Failed to get time from clock");
563 return -errno;
564 }
565
566 period = (uint64_t)NSEC_PER_SEC * io->period_size / io->rate;
567 time = now.tv_sec * NSEC_PER_SEC + now.tv_nsec + period;
568 res = aaf_mclk_start(aaf, time, period);
569 if (res < 0)
570 return res;
571
572 return 0;
573 }
574
575 static int aaf_mclk_start_capture(snd_pcm_aaf_t *aaf, uint32_t avtp_time)
576 {
577 int res;
578 struct timespec tspec;
579 uint64_t now, ptime, time, period;
580 snd_pcm_ioplug_t *io = &aaf->io;
581
582 res = clock_gettime(CLOCK_TAI, &tspec);
583 if (res < 0) {
584 SNDERR("Failed to get time from clock");
585 return -errno;
586 }
587
588 now = (uint64_t)tspec.tv_sec * NSEC_PER_SEC + tspec.tv_nsec;
589
590 /* The avtp_timestamp within AAF packet is the lower part (32
591 * less-significant bits) from presentation time calculated by the
592 * talker.
593 */
594 ptime = (now & 0xFFFFFFFF00000000ULL) | avtp_time;
595
596 /* If 'ptime' is less than the 'now', it means the higher part
597 * from 'ptime' needs to be incremented by 1 in order to recover the
598 * presentation time set by the talker.
599 */
600 if (ptime < now)
601 ptime += (1ULL << 32);
602
603 period = (uint64_t)NSEC_PER_SEC * io->period_size / io->rate;
604 time = ptime + period;
605 res = aaf_mclk_start(aaf, time, period);
606 if (res < 0)
607 return res;
608
609 aaf->prev_ptime = ptime;
610 return 0;
611 }
612
613 static int aaf_mclk_reset(snd_pcm_aaf_t *aaf)
614 {
615 int res;
616 struct itimerspec itspec = { 0 };
617
618 res = timerfd_settime(aaf->timer_fd, 0, &itspec, NULL);
619 if (res < 0) {
620 SNDERR("Failed to stop media clock");
621 return res;
622 }
623
624 aaf->timer_starttime = 0;
625 aaf->timer_period = 0;
626 aaf->timer_expirations = 0;
627 return 0;
628 }
629
630 static uint64_t aaf_mclk_gettime(snd_pcm_aaf_t *aaf)
631 {
632 if (aaf->timer_expirations == 0)
633 return 0;
634
635 return aaf->timer_starttime + aaf->timer_period *
636 (aaf->timer_expirations - 1);
637 }
638
639 static int aaf_tx_pdu(snd_pcm_aaf_t *aaf, snd_pcm_uframes_t ptr,
640 uint64_t ptime, __u64 txtime)
641 {
642 int res;
643 ssize_t n;
644 snd_pcm_ioplug_t *io = &aaf->io;
645 struct avtp_stream_pdu *pdu = aaf->pdu;
646
647 *(__u64 *)CMSG_DATA(aaf->cmsg) = txtime;
648
649 res = snd_pcm_areas_copy_wrap(aaf->payload_areas, 0,
650 aaf->frames_per_pdu,
651 aaf->audiobuf_areas,
652 (ptr % io->buffer_size),
653 io->buffer_size, io->channels,
654 aaf->frames_per_pdu, io->format);
655 if (res < 0) {
656 SNDERR("Failed to copy data to AVTP payload");
657 return res;
658 }
659
660 res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_SEQ_NUM, aaf->pdu_seq++);
661 if (res < 0)
662 return res;
663
664 res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_TIMESTAMP, ptime);
665 if (res < 0)
666 return res;
667
668 n = sendmsg(aaf->sk_fd, aaf->msg, 0);
669 if (n < 0 || n != aaf->pdu_size) {
670 SNDERR("Failed to send AAF PDU");
671 return -EIO;
672 }
673
674 return 0;
675 }
676
677 static int aaf_tx_pdus(snd_pcm_aaf_t *aaf, int pdu_count)
678 {
679 int res;
680 uint64_t ptime, txtime;
681 snd_pcm_uframes_t ptr;
682
683 txtime = aaf_mclk_gettime(aaf) + aaf->t_uncertainty;
684 ptime = txtime + aaf->mtt;
685 ptr = aaf->hw_ptr;
686
687 while (pdu_count--) {
688 res = aaf_tx_pdu(aaf, ptr, ptime, txtime);
689 if (res < 0)
690 return res;
691
692 txtime += aaf->pdu_period;
693 ptime += aaf->pdu_period;
694 ptr += aaf->frames_per_pdu;
695 }
696
697 return 0;
698 }
699
700 static bool is_ptime_valid(snd_pcm_aaf_t *aaf, uint32_t avtp_time)
701 {
702 const uint64_t exp_ptime = aaf->prev_ptime + aaf->pdu_period;
703 const uint64_t lower_bound = exp_ptime - aaf->ptime_tolerance;
704 const uint64_t upper_bound = exp_ptime + aaf->ptime_tolerance;
705 const uint64_t ptime = (exp_ptime & 0xFFFFFFFF00000000ULL) | avtp_time;
706
707 if (ptime < lower_bound || ptime > upper_bound) {
708 pr_debug("Presentation time not expected");
709 return false;
710 }
711
712 if (ptime < aaf_mclk_gettime(aaf)) {
713 pr_debug("Presentation time in the past");
714 return false;
715 }
716
717 aaf->prev_ptime = ptime;
718 return true;
719 }
720
721 static bool is_pdu_valid(snd_pcm_aaf_t *aaf)
722 {
723 int res;
724 uint64_t val64;
725 uint32_t val32;
726 snd_pcm_ioplug_t *io = &aaf->io;
727 snd_pcm_t *pcm = io->pcm;
728 const uint64_t data_len = snd_pcm_frames_to_bytes(pcm, aaf->frames_per_pdu);
729 const uint64_t format = alsa_to_avtp_format(io->format);
730 const uint64_t nsr = alsa_to_avtp_rate(io->rate);
731 const uint64_t depth = snd_pcm_format_width(io->format);
732 struct avtp_common_pdu *common = (struct avtp_common_pdu *) aaf->pdu;
733
734 res = avtp_pdu_get(common, AVTP_FIELD_VERSION, &val32);
735 if (res < 0)
736 return false;
737 if (val32 != 0) {
738 pr_debug("Version mismatch: expected %u, got %u", 0, val32);
739 return false;
740 }
741
742 res = avtp_aaf_pdu_get(aaf->pdu, AVTP_AAF_FIELD_STREAM_ID, &val64);
743 if (res < 0)
744 return false;
745 if (val64 != aaf->streamid) {
746 pr_debug("Streamid mismatch: expected %lu, got %lu",
747 aaf->streamid, val64);
748 return false;
749 }
750
751 res = avtp_aaf_pdu_get(aaf->pdu, AVTP_AAF_FIELD_TV, &val64);
752 if (res < 0)
753 return false;
754 if (val64 != 1) {
755 pr_debug("TV mismatch: expected %u, got %lu", 1, val64);
756 return false;
757 }
758
759 res = avtp_aaf_pdu_get(aaf->pdu, AVTP_AAF_FIELD_SP, &val64);
760 if (res < 0)
761 return false;
762 if (val64 != AVTP_AAF_PCM_SP_NORMAL) {
763 pr_debug("SP mismatch: expected %u, got %lu",
764 AVTP_AAF_PCM_SP_NORMAL, val64);
765 return false;
766 }
767
768 res = avtp_aaf_pdu_get(aaf->pdu, AVTP_AAF_FIELD_FORMAT, &val64);
769 if (res < 0)
770 return false;
771 if (val64 != format) {
772 pr_debug("Format mismatch: expected %u, got %lu", format,
773 val64);
774 return false;
775 }
776
777 res = avtp_aaf_pdu_get(aaf->pdu, AVTP_AAF_FIELD_NSR, &val64);
778 if (res < 0)
779 return false;
780 if (val64 != nsr) {
781 pr_debug("NSR mismatch: expected %u, got %lu", nsr, val64);
782 return false;
783 }
784
785 res = avtp_aaf_pdu_get(aaf->pdu, AVTP_AAF_FIELD_CHAN_PER_FRAME, &val64);
786 if (res < 0)
787 return false;
788 if (val64 != io->channels) {
789 pr_debug("Channels mismatch: expected %u, got %lu",
790 io->channels, val64);
791 return false;
792 }
793
794 res = avtp_aaf_pdu_get(aaf->pdu, AVTP_AAF_FIELD_BIT_DEPTH, &val64);
795 if (res < 0)
796 return false;
797 if (val64 != depth) {
798 pr_debug("Bit depth mismatch: expected %u, got %lu", depth,
799 val64);
800 return false;
801 }
802
803 res = avtp_aaf_pdu_get(aaf->pdu, AVTP_AAF_FIELD_STREAM_DATA_LEN, &val64);
804 if (res < 0)
805 return false;
806 if (val64 != data_len) {
807 pr_debug("Data len mismatch: expected %u, got %lu",
808 data_len, val64);
809 return false;
810 }
811
812 res = avtp_aaf_pdu_get(aaf->pdu, AVTP_AAF_FIELD_SEQ_NUM, &val64);
813 if (res < 0)
814 return false;
815 if (val64 != aaf->pdu_seq) {
816 pr_debug("Sequence mismatch: expected %u, got %lu",
817 aaf->pdu_seq, val64);
818 aaf->pdu_seq = val64;
819 }
820 aaf->pdu_seq++;
821
822 if (aaf->timer_starttime) {
823 /* If media clock has started, it means we have already
824 * received an AVTPDU, so we are able to check if the
825 * Presentation Time from this AVTPDU is valid.
826 */
827 uint64_t avtp_time;
828
829 res = avtp_aaf_pdu_get(aaf->pdu, AVTP_AAF_FIELD_TIMESTAMP,
830 &avtp_time);
831 if (res < 0)
832 return false;
833
834 if (!is_ptime_valid(aaf, avtp_time)) {
835 pr_debug("Packet dropped: PT not valid");
836 return false;
837 }
838 }
839
840 return true;
841 }
842
843 static int aaf_copy_pdu_payload(snd_pcm_aaf_t *aaf)
844 {
845 int res;
846 snd_pcm_uframes_t hw_avail;
847 snd_pcm_ioplug_t *io = &aaf->io;
848
849 hw_avail = snd_pcm_ioplug_hw_avail(io, aaf->hw_virt_ptr, io->appl_ptr);
850 if (hw_avail < aaf->frames_per_pdu) {
851 /* If there isn't enough space available on buffer to copy the
852 * samples from AVTPDU, it means we've reached an overrun
853 * state.
854 */
855 return -EPIPE;
856 }
857
858 res = snd_pcm_areas_copy_wrap(aaf->audiobuf_areas,
859 (aaf->hw_virt_ptr % io->buffer_size),
860 io->buffer_size, aaf->payload_areas,
861 0, aaf->frames_per_pdu, io->channels,
862 aaf->frames_per_pdu, io->format);
863 if (res < 0) {
864 SNDERR("Failed to copy data from AVTP payload");
865 return res;
866 }
867
868 aaf_inc_ptr(&aaf->hw_virt_ptr, aaf->frames_per_pdu, aaf->boundary);
869 return 0;
870 }
871
872 static int aaf_dispatch_pdu_aaf(snd_pcm_aaf_t *aaf)
873 {
874 int res;
875
876 if (!is_pdu_valid(aaf)) {
877 pr_debug("AAF PDU dropped: Bad field(s)");
878 return 0;
879 }
880
881 res = aaf_copy_pdu_payload(aaf);
882 if (res < 0)
883 return res;
884
885 if (aaf->timer_starttime == 0) {
886 /* If the media clock has not been started yet (which means
887 * this is the first AAF PDU received by the plugin), we start
888 * it.
889 */
890 uint64_t avtp_time;
891
892 res = avtp_aaf_pdu_get(aaf->pdu, AVTP_AAF_FIELD_TIMESTAMP,
893 &avtp_time);
894 if (res < 0)
895 return res;
896
897 res = aaf_mclk_start_capture(aaf, avtp_time);
898 if (res < 0)
899 return res;
900 }
901
902 return 0;
903 }
904
905 static int aaf_dispatch_pdu(snd_pcm_aaf_t *aaf)
906 {
907 int res;
908 uint32_t subtype;
909 struct avtp_common_pdu *common = (struct avtp_common_pdu *) aaf->pdu;
910
911 res = avtp_pdu_get(common, AVTP_FIELD_SUBTYPE, &subtype);
912 if (res < 0)
913 return res;
914
915 switch (subtype) {
916 case AVTP_SUBTYPE_AAF:
917 return aaf_dispatch_pdu_aaf(aaf);
918 default:
919 pr_debug("AVTPDU dropped: subtype not supported");
920 return 0;
921 }
922 }
923
924 static int aaf_socket_new_data(snd_pcm_aaf_t *aaf)
925 {
926 ssize_t n;
927 snd_pcm_ioplug_t *io = &aaf->io;
928
929 n = recv(aaf->sk_fd, aaf->pdu, aaf->pdu_size, 0);
930 if (n < 0) {
931 SNDERR("Failed to receive data");
932 return -errno;
933 }
934 if (n != aaf->pdu_size) {
935 pr_debug("AVTPDU dropped: Invalid size");
936 return 0;
937 }
938
939 if (io->state == SND_PCM_STATE_DRAINING) {
940 /* If device is in DRAIN state, there is no point in
941 * dispatching the AVTPDU just received so we are done
942 * here.
943 */
944 return 0;
945 }
946
947 return aaf_dispatch_pdu(aaf);
948 }
949
950 static int aaf_flush_rx_buf(snd_pcm_aaf_t *aaf)
951 {
952 char *tmp;
953 ssize_t n;
954
955 tmp = malloc(aaf->pdu_size);
956 if (!tmp)
957 return -ENOMEM;
958
959 do {
960 n = recv(aaf->sk_fd, tmp, aaf->pdu_size, 0);
961 } while (n != -1);
962
963 if (errno != EAGAIN && errno != EWOULDBLOCK) {
964 /* Something unexpected has happened while flushing the socket
965 * rx buffer so we return error.
966 */
967 free(tmp);
968 return -errno;
969 }
970
971 free(tmp);
972 return 0;
973 }
974
975 static int aaf_tx_frames(snd_pcm_aaf_t *aaf)
976 {
977 int res;
978 snd_pcm_uframes_t hw_avail;
979 int pdu_count;
980 snd_pcm_ioplug_t *io = &aaf->io;
981
982 hw_avail = snd_pcm_ioplug_hw_avail(io, aaf->hw_ptr, io->appl_ptr);
983 if (hw_avail < io->period_size) {
984 /* If the number of available frames is less than the period
985 * size, we reached an underrun state.
986 */
987 return -EPIPE;
988 }
989
990 pdu_count = io->period_size / aaf->frames_per_pdu;
991 res = aaf_tx_pdus(aaf, pdu_count);
992 if (res < 0)
993 return res;
994
995 aaf_inc_ptr(&aaf->hw_ptr, io->period_size, aaf->boundary);
996 return 0;
997 }
998
999 static int aaf_present_frames(snd_pcm_aaf_t *aaf)
1000 {
1001 snd_pcm_sframes_t len;
1002 snd_pcm_ioplug_t *io = &aaf->io;
1003
1004 len = aaf->hw_virt_ptr - aaf->hw_ptr;
1005 if (len < 0)
1006 len += aaf->boundary;
1007
1008 if ((snd_pcm_uframes_t) len > io->buffer_size) {
1009 /* If the distance between hw virtual pointer and hw
1010 * pointer is greater than the buffer size, it means we
1011 * had an overrun error so -EPIPE is returned.
1012 */
1013 return -EPIPE;
1014 }
1015
1016 aaf_inc_ptr(&aaf->hw_ptr, io->period_size, aaf->boundary);
1017 return 0;
1018 }
1019
1020 static int aaf_process_frames(snd_pcm_aaf_t *aaf)
1021 {
1022 snd_pcm_ioplug_t *io = &aaf->io;
1023
1024 return (io->stream == SND_PCM_STREAM_PLAYBACK) ?
1025 aaf_tx_frames(aaf) :
1026 aaf_present_frames(aaf);
1027 }
1028
1029 static int aaf_timer_timeout(snd_pcm_aaf_t *aaf)
1030 {
1031 int res;
1032 ssize_t n;
1033 uint64_t expirations;
1034
1035 n = read(aaf->timer_fd, &expirations, sizeof(uint64_t));
1036 if (n < 0) {
1037 SNDERR("Failed to read() timer");
1038 return -errno;
1039 }
1040
1041 if (expirations != 1)
1042 pr_debug("Missed %llu expirations ", expirations - 1);
1043
1044 while (expirations--) {
1045 aaf->timer_expirations++;
1046
1047 res = aaf_process_frames(aaf);
1048 if (res < 0)
1049 return res;
1050 }
1051
1052 return 0;
1053 }
1054
1055 static int aaf_set_hw_constraint(snd_pcm_aaf_t *aaf)
1056 {
1057 int res;
1058 snd_pcm_ioplug_t *io = &aaf->io;
1059 static const unsigned int accesses[] = {
1060 SND_PCM_ACCESS_RW_INTERLEAVED,
1061 SND_PCM_ACCESS_MMAP_INTERLEAVED,
1062 };
1063 static const unsigned int formats[] = {
1064 SND_PCM_FORMAT_S16_BE,
1065 SND_PCM_FORMAT_S24_3BE,
1066 SND_PCM_FORMAT_S32_BE,
1067 SND_PCM_FORMAT_FLOAT_BE,
1068 };
1069 static const unsigned int rates[] = {
1070 8000,
1071 16000,
1072 24000,
1073 32000,
1074 44100,
1075 48000,
1076 88200,
1077 96000,
1078 176400,
1079 192000,
1080 };
1081
1082 res = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS,
1083 ARRAY_SIZE(accesses), accesses);
1084 if (res < 0)
1085 return res;
1086
1087 res = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT,
1088 ARRAY_SIZE(formats), formats);
1089 if (res < 0)
1090 return res;
1091
1092 res = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_RATE,
1093 ARRAY_SIZE(rates), rates);
1094 if (res < 0)
1095 return res;
1096
1097 return 0;
1098 }
1099
1100 static int aaf_close(snd_pcm_ioplug_t *io)
1101 {
1102 free(io->private_data);
1103 return 0;
1104 }
1105
1106 static void aaf_dump(snd_pcm_ioplug_t *io, snd_output_t *out)
1107 {
1108 snd_pcm_aaf_t *aaf = io->private_data;
1109 snd_pcm_t *pcm = io->pcm;
1110
1111 snd_output_printf(out, "%s\n", io->name);
1112 snd_output_printf(out, "PCM setup is:\n");
1113 snd_pcm_dump_setup(pcm, out);
1114 snd_output_printf(out, "AVTP setup is:\n");
1115 snd_output_printf(out, " ifname: %s\n", aaf->ifname);
1116 snd_output_printf(out, " macaddr: %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",
1117 aaf->addr[0], aaf->addr[1], aaf->addr[2],
1118 aaf->addr[3], aaf->addr[4], aaf->addr[5]);
1119 snd_output_printf(out, " priority: %d\n", aaf->prio);
1120 snd_output_printf(out, " streamid: %"PRIX64"\n", aaf->streamid);
1121 snd_output_printf(out, " mtt: %d\n", aaf->mtt / NSEC_PER_USEC);
1122 snd_output_printf(out, " time uncertainty: %d\n",
1123 aaf->t_uncertainty / NSEC_PER_USEC);
1124 snd_output_printf(out, " frames per AVTPDU: %lu\n",
1125 aaf->frames_per_pdu);
1126 snd_output_printf(out, " ptime tolerance: %d\n",
1127 aaf->ptime_tolerance / NSEC_PER_USEC);
1128 }
1129
1130 static int aaf_hw_params(snd_pcm_ioplug_t *io,
1131 snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED)
1132 {
1133 int res;
1134 snd_pcm_aaf_t *aaf = io->private_data;
1135
1136 res = aaf_init_socket(aaf);
1137 if (res < 0)
1138 return res;
1139
1140 res = aaf_init_timer(aaf);
1141 if (res < 0)
1142 goto err_close_sk;
1143
1144 res = aaf_init_pdu(aaf);
1145 if (res < 0)
1146 goto err_close_timer;
1147
1148 res = aaf_init_payload_areas(aaf);
1149 if (res < 0)
1150 goto err_free_pdu;
1151
1152 res = aaf_init_msghdr(aaf);
1153 if (res < 0)
1154 goto err_free_areas;
1155
1156 if (io->period_size % aaf->frames_per_pdu) {
1157 /* The plugin requires that the period size is multiple of the
1158 * configuration frames_per_pdu. Return error if this
1159 * requirement isn't satisfied.
1160 */
1161 SNDERR("Period size must be multiple of frames_per_pdu");
1162 res = -EINVAL;
1163 goto err_free_msghdr;
1164 }
1165
1166 aaf->pdu_period = (uint64_t)NSEC_PER_SEC * aaf->frames_per_pdu /
1167 io->rate;
1168 return 0;
1169
1170 err_free_msghdr:
1171 free(aaf->msg->msg_iov);
1172 free(aaf->msg->msg_control);
1173 free(aaf->msg);
1174 err_free_areas:
1175 free(aaf->payload_areas);
1176 err_free_pdu:
1177 free(aaf->pdu);
1178 err_close_timer:
1179 close(aaf->timer_fd);
1180 err_close_sk:
1181 close(aaf->sk_fd);
1182 return res;
1183 }
1184
1185 static int aaf_hw_free(snd_pcm_ioplug_t *io)
1186 {
1187 snd_pcm_aaf_t *aaf = io->private_data;
1188
1189 close(aaf->sk_fd);
1190 close(aaf->timer_fd);
1191 free(aaf->pdu);
1192 free(aaf->payload_areas);
1193 free(aaf->msg->msg_iov);
1194 free(aaf->msg->msg_control);
1195 free(aaf->msg);
1196 return 0;
1197 }
1198
1199 static int aaf_sw_params(snd_pcm_ioplug_t *io, snd_pcm_sw_params_t *params)
1200 {
1201 int res;
1202 snd_pcm_aaf_t *aaf = io->private_data;
1203
1204 res = snd_pcm_sw_params_get_boundary(params, &aaf->boundary);
1205 if (res < 0)
1206 return res;
1207
1208 return 0;
1209 }
1210
1211 static snd_pcm_sframes_t aaf_pointer(snd_pcm_ioplug_t *io)
1212 {
1213 snd_pcm_aaf_t *aaf = io->private_data;
1214
1215 return aaf->hw_ptr;
1216 }
1217
1218 static int aaf_poll_descriptors_count(snd_pcm_ioplug_t *io ATTRIBUTE_UNUSED)
1219 {
1220 if (io->stream == SND_PCM_STREAM_PLAYBACK)
1221 return FD_COUNT_PLAYBACK;
1222 else
1223 return FD_COUNT_CAPTURE;
1224 }
1225
1226 static int aaf_poll_descriptors(snd_pcm_ioplug_t *io, struct pollfd *pfd,
1227 unsigned int space)
1228 {
1229 snd_pcm_aaf_t *aaf = io->private_data;
1230
1231 if (io->stream == SND_PCM_STREAM_PLAYBACK) {
1232 if (space != FD_COUNT_PLAYBACK)
1233 return -EINVAL;
1234
1235 pfd[0].fd = aaf->timer_fd;
1236 pfd[0].events = POLLIN;
1237 } else {
1238 if (space != FD_COUNT_CAPTURE)
1239 return -EINVAL;
1240
1241 pfd[0].fd = aaf->timer_fd;
1242 pfd[0].events = POLLIN;
1243 pfd[1].fd = aaf->sk_fd;
1244 pfd[1].events = POLLIN;
1245 }
1246
1247 return space;
1248 }
1249
1250 static int aaf_poll_revents(snd_pcm_ioplug_t *io, struct pollfd *pfd,
1251 unsigned int nfds, unsigned short *revents)
1252 {
1253 int res;
1254 snd_pcm_aaf_t *aaf = io->private_data;
1255
1256 if (io->stream == SND_PCM_STREAM_PLAYBACK) {
1257 if (nfds != FD_COUNT_PLAYBACK)
1258 return -EINVAL;
1259
1260 if (pfd[0].revents & POLLIN) {
1261 res = aaf_timer_timeout(aaf);
1262 if (res < 0)
1263 return res;
1264
1265 *revents = POLLIN;
1266 }
1267 } else {
1268 if (nfds != FD_COUNT_CAPTURE)
1269 return -EINVAL;
1270
1271 if (pfd[0].revents & POLLIN) {
1272 res = aaf_timer_timeout(aaf);
1273 if (res < 0)
1274 return res;
1275
1276 *revents = POLLIN;
1277 }
1278
1279 if (pfd[1].revents & POLLIN) {
1280 res = aaf_socket_new_data(aaf);
1281 if (res < 0)
1282 return res;
1283 }
1284 }
1285
1286 return 0;
1287 }
1288
1289 static int aaf_prepare(snd_pcm_ioplug_t *io)
1290 {
1291 int res;
1292 snd_pcm_aaf_t *aaf = io->private_data;
1293
1294 aaf->audiobuf_areas = snd_pcm_ioplug_mmap_areas(io);
1295 aaf->pdu_seq = 0;
1296 aaf->hw_ptr = 0;
1297 aaf->hw_virt_ptr = 0;
1298 aaf->prev_ptime = 0;
1299
1300 res = aaf_mclk_reset(aaf);
1301 if (res < 0)
1302 return res;
1303
1304 return 0;
1305 }
1306
1307 static int aaf_start(snd_pcm_ioplug_t *io)
1308 {
1309 int res;
1310 snd_pcm_aaf_t *aaf = io->private_data;
1311
1312 if (io->stream == SND_PCM_STREAM_PLAYBACK) {
1313 res = aaf_mclk_start_playback(aaf);
1314 } else {
1315 /* Discard any packet on socket buffer to ensure the plugin
1316 * process only packets that arrived after the device has
1317 * started.
1318 */
1319 res = aaf_flush_rx_buf(aaf);
1320 }
1321
1322 if (res < 0)
1323 return res;
1324
1325 return 0;
1326 }
1327
1328 static int aaf_stop(snd_pcm_ioplug_t *io)
1329 {
1330 int res;
1331 snd_pcm_aaf_t *aaf = io->private_data;
1332
1333 res = aaf_mclk_reset(aaf);
1334 if (res < 0)
1335 return res;
1336
1337 return 0;
1338 }
1339
1340 static const snd_pcm_ioplug_callback_t aaf_callback = {
1341 .close = aaf_close,
1342 .dump = aaf_dump,
1343 .hw_params = aaf_hw_params,
1344 .hw_free = aaf_hw_free,
1345 .sw_params = aaf_sw_params,
1346 .pointer = aaf_pointer,
1347 .poll_descriptors_count = aaf_poll_descriptors_count,
1348 .poll_descriptors = aaf_poll_descriptors,
1349 .poll_revents = aaf_poll_revents,
1350 .prepare = aaf_prepare,
1351 .start = aaf_start,
1352 .stop = aaf_stop,
1353 };
1354
1355 SND_PCM_PLUGIN_DEFINE_FUNC(aaf)
1356 {
1357 snd_pcm_aaf_t *aaf;
1358 int res;
1359
1360 aaf = calloc(1, sizeof(*aaf));
1361 if (!aaf) {
1362 SNDERR("Failed to allocate memory");
1363 return -ENOMEM;
1364 }
1365
1366 aaf->sk_fd = -1;
1367 aaf->timer_fd = -1;
1368
1369 res = aaf_load_config(aaf, conf);
1370 if (res < 0)
1371 goto err;
1372
1373 aaf->io.version = SND_PCM_IOPLUG_VERSION;
1374 aaf->io.name = "AVTP Audio Format (AAF) Plugin";
1375 aaf->io.callback = &aaf_callback;
1376 aaf->io.private_data = aaf;
1377 aaf->io.flags = SND_PCM_IOPLUG_FLAG_BOUNDARY_WA;
1378 aaf->io.mmap_rw = 1;
1379 res = snd_pcm_ioplug_create(&aaf->io, name, stream, mode);
1380 if (res < 0) {
1381 SNDERR("Failed to create ioplug instance");
1382 goto err;
1383 }
1384
1385 res = aaf_set_hw_constraint(aaf);
1386 if (res < 0) {
1387 SNDERR("Failed to set hw constraints");
1388 snd_pcm_ioplug_delete(&aaf->io);
1389 return res;
1390 }
1391
1392 *pcmp = aaf->io.pcm;
1393 return 0;
1394
1395 err:
1396 free(aaf);
1397 return res;
1398 }
1399
1400 SND_PCM_PLUGIN_SYMBOL(aaf);