Debian Codegraph @

usb_stream/pcm_usb_stream.c

raw symbols SCIP
1 /*
2 * PCM - USB_STREAM plugin
3 *
4 * Copyright (c) 2008, 2010 by Karsten Wiese <[email protected]>
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 #define _GNU_SOURCE
22 #include <sys/mman.h>
23 #include <sys/shm.h>
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <pthread.h>
28
29 #include <alsa/asoundlib.h>
30 #include <alsa/pcm_external.h>
31 #include <alsa/hwdep.h>
32
33 #include "usb_stream.h"
34
35 #define DEBUG
36 #ifdef DEBUG
37 #define DBG(f, ...) \
38 fprintf(stderr, "%s:%i %i "f"\n", __FUNCTION__, __LINE__, getpid(), ## __VA_ARGS__);
39 #else
40 #define DBG(f, ...)
41 #endif
42
43 #ifdef VDEBUG
44 #define VDBG(f, ...) \
45 fprintf(stderr, "%s:%i %i "f"\n", __FUNCTION__, __LINE__, getpid(), ## __VA_ARGS__);
46 #else
47 #define VDBG(f, ...)
48 #endif
49
50 #define FRAME_SIZE 6
51
52 struct user_usb_stream {
53 int card;
54 unsigned use;
55 struct usb_stream *s;
56 void *write_area;
57 struct user_usb_stream *next;
58 };
59
60 typedef struct {
61 snd_pcm_ioplug_t io;
62
63 snd_hwdep_t *hwdep;
64 struct user_usb_stream *uus;
65
66 struct pollfd pfd;
67
68 unsigned int num_ports;
69 unsigned periods_start;
70 unsigned periods_done;
71
72 unsigned channels;
73 snd_pcm_uframes_t period_size;
74 unsigned int rate;
75 } snd_pcm_us_t;
76
77 static struct user_usb_stream *uus;
78 static pthread_mutex_t uus_mutex = PTHREAD_MUTEX_INITIALIZER;
79
80 static struct user_usb_stream *get_uus(int card)
81 {
82 pthread_mutex_lock(&uus_mutex);
83
84 struct user_usb_stream **l_uus = &uus,
85 *r_uus = NULL;
86 while (*l_uus) {
87 if ((*l_uus)->card == card) {
88 r_uus = *l_uus;
89 r_uus->use++;
90 goto unlock;
91 }
92 l_uus = &(*l_uus)->next;
93 }
94 r_uus = calloc(1, sizeof(*r_uus));
95 if (r_uus) {
96 r_uus->use = 1;
97 r_uus->card = card;
98 *l_uus = r_uus;
99 }
100
101 unlock:
102 pthread_mutex_unlock(&uus_mutex);
103 return r_uus;
104 }
105
106 static void uus_free(snd_pcm_us_t *us)
107 {
108 if (!us->uus)
109 return;
110
111 pthread_mutex_lock(&uus_mutex);
112 us->uus->use--;
113 if (!us->uus->use) {
114 struct user_usb_stream **n_uus = &uus,
115 *p_uus;
116 while (us->uus != *n_uus) {
117 p_uus = *n_uus;
118 n_uus = &p_uus->next;
119 }
120 *n_uus = us->uus->next;
121 if (us->uus->s) {
122 munmap(us->uus->write_area, us->uus->s->write_size);
123 munmap(us->uus->s, us->uus->s->read_size);
124 }
125 free(us->uus);
126 }
127 pthread_mutex_unlock(&uus_mutex);
128 }
129
130 static void us_free(snd_pcm_us_t *us)
131 {
132 uus_free(us);
133 free(us);
134 }
135
136 static int snd_pcm_us_close(snd_pcm_ioplug_t *io)
137 {
138 snd_pcm_us_t *us = io->private_data;
139 snd_hwdep_close(us->hwdep);
140 us_free(us);
141 return 0;
142 }
143
144 static snd_pcm_sframes_t snd_pcm_us_pointer(snd_pcm_ioplug_t *io)
145 {
146 snd_pcm_us_t *us = io->private_data;
147 struct usb_stream *s = us->uus->s;
148 snd_pcm_sframes_t hw_pointer;
149
150 switch (io->state) {
151 case SND_PCM_STATE_RUNNING:
152 VDBG("%u %u", s->periods_done, us->periods_done);
153 if (s->periods_done - us->periods_done <= 1)
154 hw_pointer =
155 (s->periods_done - us->periods_start) & 1 ?
156 io->period_size : 0;
157 else
158 hw_pointer = -EPIPE;
159
160 break;
161 case SND_PCM_STATE_XRUN:
162 hw_pointer = -EPIPE;
163 break;
164 default:
165 hw_pointer = 0;
166 break;
167 }
168 VDBG("%li", hw_pointer);
169 return hw_pointer;
170 }
171
172 static int snd_pcm_us_prepare(snd_pcm_ioplug_t *io)
173 {
174 struct usb_stream_config us_cfg;
175 snd_pcm_us_t *us = io->private_data;
176 struct user_usb_stream *uus = us->uus;
177 int ioctl_result, err;
178
179 VDBG("");
180
181 us_cfg.version = USB_STREAM_INTERFACE_VERSION;
182 us_cfg.frame_size = FRAME_SIZE;
183 us_cfg.sample_rate = io->rate;
184 us_cfg.period_frames = io->period_size;
185
186 ioctl_result = snd_hwdep_ioctl(us->hwdep, SNDRV_USB_STREAM_IOCTL_SET_PARAMS, &us_cfg);
187 if (ioctl_result < 0) {
188 perror("Couldn't configure usb_stream\n");
189 return ioctl_result;
190 }
191
192 if (ioctl_result && uus && uus->s) {
193 err = munmap(uus->write_area, uus->s->write_size);
194 if (err < 0)
195 return -errno;
196 err = munmap(uus->s, uus->s->read_size);
197 if (err < 0)
198 return -errno;
199 uus->s = NULL;
200 }
201
202 if (!uus->s) {
203 uus->s = mmap(NULL, sizeof(struct usb_stream),
204 PROT_READ,
205 MAP_SHARED, us->pfd.fd,
206 0);
207 if (MAP_FAILED == uus->s) {
208 perror("ALSA/USX2Y: mmap");
209 return -errno;
210 }
211
212 VDBG("%p %lx %i", uus->s, uus->s, uus->s->read_size);
213
214 if (memcmp(&uus->s->cfg, &us_cfg, sizeof(us_cfg))) {
215 perror("usb_stream Configuration error usb_stream\n");
216 return -EIO;
217 }
218
219
220 uus->s = mremap(uus->s, sizeof(struct usb_stream), uus->s->read_size, MREMAP_MAYMOVE);
221 if (MAP_FAILED == uus->s) {
222 perror("ALSA/USX2Y: mmap");
223 return -EPERM;
224 }
225
226 VDBG("%p %lx %i", uus->s, uus->s, uus->s->read_size);
227
228 uus->write_area = mmap(NULL, uus->s->write_size,
229 PROT_READ|PROT_WRITE,
230 MAP_SHARED, us->pfd.fd,
231 (uus->s->read_size + 4095) & ~4095);
232 if (MAP_FAILED == uus->write_area) {
233 perror("ALSA/USX2Y: mmap");
234 return -1;
235 }
236 VDBG("%p %i", uus->write_area, uus->s->write_size);
237 }
238
239 if (uus->s->state != usb_stream_ready)
240 return -EIO;
241
242 if (poll(&us->pfd, 1, 500000) < 0)
243 return -errno;
244
245 return 0;
246 }
247
248 static int snd_pcm_us_start(snd_pcm_ioplug_t *io)
249 {
250 snd_pcm_us_t *us = io->private_data;
251 VDBG("%u", us->uus->s->periods_done);
252
253 us->periods_start = us->periods_done = us->uus->s->periods_done;
254
255 return 0;
256 }
257
258 static int snd_pcm_us_stop(snd_pcm_ioplug_t *io)
259 {
260 snd_pcm_us_t *us = io->private_data;
261
262 if (!us->uus->s)
263 return 0;
264
265 VDBG("%u", us->uus->s->periods_done);
266 if (io->stream == SND_PCM_STREAM_PLAYBACK)
267 memset(us->uus->write_area, 0, us->uus->s->write_size);
268
269 return 0;
270 }
271
272 static snd_pcm_sframes_t snd_pcm_us_write(snd_pcm_ioplug_t *io,
273 const snd_pcm_channel_area_t *areas,
274 snd_pcm_uframes_t offset,
275 snd_pcm_uframes_t size)
276 {
277 void *playback_addr;
278 snd_pcm_us_t *us = io->private_data;
279 struct user_usb_stream *uus = us->uus;
280 struct usb_stream *s = uus->s;
281 unsigned bytes;
282 void *src = areas->addr + offset * s->cfg.frame_size;
283
284 VDBG("%li %li %i %i", offset, size, areas->first, areas->step);
285
286 playback_addr = uus->write_area + s->outpacket[0].offset;
287 memcpy(playback_addr, src, s->outpacket[0].length);
288 bytes = size * s->cfg.frame_size;
289 if (bytes > s->outpacket[0].length) {
290 playback_addr = uus->write_area + s->outpacket[1].offset;
291 memcpy(playback_addr, src + s->outpacket[0].length,
292 bytes - s->outpacket[0].length);
293 }
294 us->periods_done++;
295 return size;
296 }
297
298 static int usb_stream_read(struct user_usb_stream *uus, void *to)
299 {
300 struct usb_stream *s = uus->s;
301 unsigned p = s->inpacket_split, l = 0;
302 void *i = (void *)s + s->inpacket[p].offset + s->inpacket_split_at;
303 unsigned il = s->inpacket[p].length - s->inpacket_split_at;
304
305 do {
306 if (l + il > s->period_size)
307 il = s->period_size - l;
308 memcpy(to + l, i, il);
309 l += il;
310 if (l >= s->period_size)
311 break;
312
313 p = (p + 1) % s->inpackets;
314 i = (void *)s + s->inpacket[p].offset;
315 il = s->inpacket[p].length;
316 } while (p != s->inpacket_split);
317
318 return l;
319 }
320
321 static snd_pcm_sframes_t snd_pcm_us_read(snd_pcm_ioplug_t *io,
322 const snd_pcm_channel_area_t *areas,
323 snd_pcm_uframes_t offset,
324 snd_pcm_uframes_t size)
325 {
326 snd_pcm_us_t *us = io->private_data;
327 unsigned frame_size = us->uus->s->cfg.frame_size;
328 void *to = areas->addr + offset * frame_size;
329 snd_pcm_uframes_t red;
330
331 if (size) {
332 if (size != us->uus->s->cfg.period_frames) {
333 SNDERR("usb_stream plugin only supports period_size"
334 " long reads, sorry");
335 return -EINVAL;
336 }
337 if (us->uus->s->periods_done - us->periods_done == 1) {
338 red = usb_stream_read(us->uus, to) / frame_size;
339 us->periods_done++;
340 return red;
341 }
342 } else
343 if (io->state == SND_PCM_STATE_XRUN)
344 return -EPIPE;
345 return 0;
346 }
347
348 static snd_pcm_ioplug_callback_t us_playback_callback = {
349 .close = snd_pcm_us_close,
350 .start = snd_pcm_us_start,
351 .stop = snd_pcm_us_stop,
352 .transfer = snd_pcm_us_write,
353 .pointer = snd_pcm_us_pointer,
354 .prepare = snd_pcm_us_prepare,
355 };
356 static snd_pcm_ioplug_callback_t us_capture_callback = {
357 .close = snd_pcm_us_close,
358 .start = snd_pcm_us_start,
359 .stop = snd_pcm_us_stop,
360 .transfer = snd_pcm_us_read,
361 .pointer = snd_pcm_us_pointer,
362 .prepare = snd_pcm_us_prepare,
363 };
364
365 #define ARRAY_SIZE(ary) (sizeof(ary)/sizeof(ary[0]))
366
367 static int us_set_hw_constraint(snd_pcm_us_t *us)
368 {
369 unsigned access_list[] = {
370 SND_PCM_ACCESS_MMAP_INTERLEAVED,
371 };
372 unsigned format_list[] = {
373 SND_PCM_FORMAT_S24_3LE,
374 };
375
376 int err;
377 unsigned int rate_min = us->rate ? us->rate : 44100,
378 rate_max = us->rate ? us->rate : 96000,
379 period_bytes_min = us->period_size ? FRAME_SIZE * us->period_size : 128,
380 period_bytes_max = us->period_size ? FRAME_SIZE * us->period_size : 64*4096;
381
382 if ((err = snd_pcm_ioplug_set_param_list(&us->io, SND_PCM_IOPLUG_HW_ACCESS,
383 ARRAY_SIZE(access_list), access_list)) < 0 ||
384 (err = snd_pcm_ioplug_set_param_list(&us->io, SND_PCM_IOPLUG_HW_FORMAT,
385 ARRAY_SIZE(format_list), format_list)) < 0 ||
386 (err = snd_pcm_ioplug_set_param_minmax(&us->io, SND_PCM_IOPLUG_HW_CHANNELS,
387 us->channels, us->channels)) < 0 ||
388 (err = snd_pcm_ioplug_set_param_minmax(&us->io, SND_PCM_IOPLUG_HW_RATE,
389 rate_min, rate_max)) < 0 ||
390 (err = snd_pcm_ioplug_set_param_minmax(&us->io, SND_PCM_IOPLUG_HW_PERIOD_BYTES,
391 period_bytes_min, period_bytes_max)) < 0 ||
392 (err = snd_pcm_ioplug_set_param_minmax(&us->io, SND_PCM_IOPLUG_HW_PERIODS,
393 2, 2)) < 0)
394 return err;
395
396 return 0;
397 }
398
399 static int snd_pcm_us_open(snd_pcm_t **pcmp, const char *name,
400 int card,
401 snd_pcm_stream_t stream, int mode,
402 snd_pcm_uframes_t period_size,
403 unsigned int rate)
404 {
405 snd_pcm_us_t *us;
406 int err;
407 char us_name[32];
408
409 assert(pcmp);
410 us = calloc(1, sizeof(*us));
411 if (!us)
412 return -ENOMEM;
413
414 if (snprintf(us_name, sizeof(us_name), "hw:%d", card)
415 >= (int)sizeof(us_name)) {
416 fprintf(stderr, "%s: WARNING: USB_STREAM client name '%s' truncated to %d characters, might not be unique\n",
417 __func__, us_name, (int)strlen(us_name));
418 }
419 VDBG("%i %s", stream, us_name);
420 us->uus = get_uus(card);
421 if (!us->uus) {
422 free(us);
423 return -ENOMEM;
424 }
425 err = snd_hwdep_open(&us->hwdep, us_name, O_RDWR);
426 if (err < 0) {
427 us_free(us);
428 return err;
429 }
430 snd_hwdep_poll_descriptors(us->hwdep, &us->pfd, 1);
431
432 us->channels = 2;
433 us->period_size = period_size;
434 us->rate = rate;
435
436 us->io.version = SND_PCM_IOPLUG_VERSION;
437 us->io.name = "ALSA <-> USB_STREAM PCM I/O Plugin";
438 us->io.callback = stream == SND_PCM_STREAM_PLAYBACK ?
439 &us_playback_callback : &us_capture_callback;
440 us->io.private_data = us;
441 us->io.mmap_rw = 0;
442 us->io.poll_fd = us->pfd.fd;
443 us->io.poll_events = stream == SND_PCM_STREAM_PLAYBACK ? POLLOUT : POLLIN;
444
445 err = snd_pcm_ioplug_create(&us->io, name, stream, mode);
446 if (err < 0) {
447 us_free(us);
448 return err;
449 }
450
451 err = us_set_hw_constraint(us);
452 if (err < 0) {
453 snd_pcm_ioplug_delete(&us->io);
454 return err;
455 }
456
457 VDBG("");
458 *pcmp = us->io.pcm;
459
460 return 0;
461 }
462
463
464 SND_PCM_PLUGIN_DEFINE_FUNC(usb_stream)
465 {
466 snd_config_iterator_t i, next;
467 int err, card = -1;
468 long period_size = 0, rate = 0;
469
470 snd_config_for_each(i, next, conf) {
471 snd_config_t *n = snd_config_iterator_entry(i);
472 const char *id;
473 if (snd_config_get_id(n, &id) < 0)
474 continue;
475
476 if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0)
477 continue;
478 if (strcmp(id, "card") == 0) {
479 card = snd_config_get_card(n);
480 if (card < 0) {
481 SNDERR("Invalid card '%s'", id);
482 return -EINVAL;
483 }
484 continue;
485 }
486 if (strcmp(id, "period_size") == 0) {
487 if (snd_config_get_type(n) != SND_CONFIG_TYPE_INTEGER) {
488 SNDERR("Invalid type for %s", id);
489 return -EINVAL;
490 }
491 snd_config_get_integer(n, &period_size);
492 continue;
493 }
494 if (strcmp(id, "rate") == 0) {
495 if (snd_config_get_type(n) != SND_CONFIG_TYPE_INTEGER) {
496 SNDERR("Invalid type for %s", id);
497 return -EINVAL;
498 }
499 snd_config_get_integer(n, &rate);
500 continue;
501 }
502 SNDERR("Unknown field %s", id);
503 return -EINVAL;
504 }
505
506 err = snd_pcm_us_open(pcmp, name, card, stream, mode, period_size, rate);
507
508 return err;
509 }
510
511 SND_PCM_PLUGIN_SYMBOL(usb_stream);