1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Digigram miXart soundcards
5 * main file with alsa callbacks
7 * Copyright (c) 2003 by Digigram <alsa@digigram.com>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/pci.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
19 #include <sound/core.h>
20 #include <sound/initval.h>
21 #include <sound/info.h>
22 #include <sound/control.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
26 #include "mixart_hwdep.h"
27 #include "mixart_core.h"
28 #include "mixart_mixer.h"
30 #define CARD_NAME "miXart"
32 MODULE_AUTHOR("Digigram <alsa@digigram.com>");
33 MODULE_DESCRIPTION("Digigram " CARD_NAME);
34 MODULE_LICENSE("GPL");
35 MODULE_SUPPORTED_DEVICE("{{Digigram," CARD_NAME "}}");
37 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
38 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
39 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
41 module_param_array(index, int, NULL, 0444);
42 MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
43 module_param_array(id, charp, NULL, 0444);
44 MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
45 module_param_array(enable, bool, NULL, 0444);
46 MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
51 static const struct pci_device_id snd_mixart_ids[] = {
52 { PCI_VDEVICE(MOTOROLA, 0x0003), 0, }, /* MC8240 */
56 MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
59 static int mixart_set_pipe_state(struct mixart_mgr *mgr,
60 struct mixart_pipe *pipe, int start)
62 struct mixart_group_state_req group_state;
63 struct mixart_group_state_resp group_state_resp;
64 struct mixart_msg request;
68 switch(pipe->status) {
71 if(start) return 0; /* already started */
74 if(!start) return 0; /* already stopped */
77 dev_err(&mgr->pci->dev,
78 "error mixart_set_pipe_state called with wrong pipe->status!\n");
79 return -EINVAL; /* function called with wrong pipe status */
82 system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
84 /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
86 request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
87 request.uid = (struct mixart_uid){0,0};
88 request.data = &system_msg_uid;
89 request.size = sizeof(system_msg_uid);
91 err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
93 dev_err(&mgr->pci->dev,
94 "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
98 /* start or stop the pipe (1 pipe) */
100 memset(&group_state, 0, sizeof(group_state));
101 group_state.pipe_count = 1;
102 group_state.pipe_uid[0] = pipe->group_uid;
105 request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
107 request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
109 request.uid = pipe->group_uid; /*(struct mixart_uid){0,0};*/
110 request.data = &group_state;
111 request.size = sizeof(group_state);
113 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
114 if (err < 0 || group_state_resp.txx_status != 0) {
115 dev_err(&mgr->pci->dev,
116 "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n",
117 err, group_state_resp.txx_status);
124 group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
126 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
127 if (err < 0 || group_state_resp.txx_status != 0) {
128 dev_err(&mgr->pci->dev,
129 "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n",
130 err, group_state_resp.txx_status);
134 /* in case of start send a synchro top */
136 request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
137 request.uid = (struct mixart_uid){0,0};
141 err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
142 if (err < 0 || stat != 0) {
143 dev_err(&mgr->pci->dev,
144 "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n",
149 pipe->status = PIPE_RUNNING;
152 pipe->status = PIPE_STOPPED;
158 static int mixart_set_clock(struct mixart_mgr *mgr,
159 struct mixart_pipe *pipe, unsigned int rate)
161 struct mixart_msg request;
162 struct mixart_clock_properties clock_properties;
163 struct mixart_clock_properties_resp clock_prop_resp;
166 switch(pipe->status) {
175 return 0; /* nothing to do */
177 dev_err(&mgr->pci->dev,
178 "error mixart_set_clock(%d) called with wrong pipe->status !\n",
184 memset(&clock_properties, 0, sizeof(clock_properties));
185 clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
186 clock_properties.clock_mode = CM_STANDALONE;
187 clock_properties.frequency = rate;
188 clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
189 clock_properties.uid_caller[0] = pipe->group_uid;
191 dev_dbg(&mgr->pci->dev, "mixart_set_clock to %d kHz\n", rate);
193 request.message_id = MSG_CLOCK_SET_PROPERTIES;
194 request.uid = mgr->uid_console_manager;
195 request.data = &clock_properties;
196 request.size = sizeof(clock_properties);
198 err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
199 if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
200 dev_err(&mgr->pci->dev,
201 "error MSG_CLOCK_SET_PROPERTIES err=%x stat=%x mod=%x !\n",
202 err, clock_prop_resp.status, clock_prop_resp.clock_mode);
206 if(rate) pipe->status = PIPE_CLOCK_SET;
207 else pipe->status = PIPE_RUNNING;
214 * Allocate or reference output pipe for analog IOs (pcmp0/1)
217 snd_mixart_add_ref_pipe(struct snd_mixart *chip, int pcm_number, int capture,
221 struct mixart_pipe *pipe;
222 struct mixart_msg request;
225 if (pcm_number == MIXART_PCM_ANALOG) {
226 pipe = &(chip->pipe_in_ana); /* analog inputs */
228 pipe = &(chip->pipe_in_dig); /* digital inputs */
230 request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
231 stream_count = MIXART_CAPTURE_STREAMS;
233 if (pcm_number == MIXART_PCM_ANALOG) {
234 pipe = &(chip->pipe_out_ana); /* analog outputs */
236 pipe = &(chip->pipe_out_dig); /* digital outputs */
238 request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
239 stream_count = MIXART_PLAYBACK_STREAMS;
242 /* a new stream is opened and there are already all streams in use */
243 if( (monitoring == 0) && (pipe->references >= stream_count) ) {
247 /* pipe is not yet defined */
248 if( pipe->status == PIPE_UNDEFINED ) {
251 struct mixart_streaming_group_req sgroup_req;
252 struct mixart_streaming_group sgroup_resp;
255 dev_dbg(chip->card->dev,
256 "add_ref_pipe audio chip(%d) pcm(%d)\n",
257 chip->chip_idx, pcm_number);
259 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
263 request.uid = (struct mixart_uid){0,0}; /* should be StreamManagerUID, but zero is OK if there is only one ! */
264 request.data = &buf->sgroup_req;
265 request.size = sizeof(buf->sgroup_req);
267 memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));
269 buf->sgroup_req.stream_count = stream_count;
270 buf->sgroup_req.channel_count = 2;
271 buf->sgroup_req.latency = 256;
272 buf->sgroup_req.connector = pipe->uid_left_connector; /* the left connector */
274 for (i=0; i<stream_count; i++) {
276 struct mixart_flowinfo *flowinfo;
277 struct mixart_bufferinfo *bufferinfo;
279 /* we don't yet know the format, so config 16 bit pcm audio for instance */
280 buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;
281 buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;
282 buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
284 /* find the right bufferinfo_array */
285 j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;
286 if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */
288 buf->sgroup_req.flow_entry[i] = j;
290 flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;
291 flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(struct mixart_bufferinfo));
292 flowinfo[j].bufferinfo_count = 1; /* 1 will set the miXart to ring-buffer mode ! */
294 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
295 bufferinfo[j].buffer_address = 0; /* buffer is not yet allocated */
296 bufferinfo[j].available_length = 0; /* buffer is not yet allocated */
298 /* construct the identifier of the stream buffer received in the interrupts ! */
299 bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;
301 bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
305 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);
306 if((err < 0) || (buf->sgroup_resp.status != 0)) {
307 dev_err(chip->card->dev,
308 "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n",
309 err, buf->sgroup_resp.status);
314 pipe->group_uid = buf->sgroup_resp.group; /* id of the pipe, as returned by embedded */
315 pipe->stream_count = buf->sgroup_resp.stream_count;
316 /* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */
318 pipe->status = PIPE_STOPPED;
322 if(monitoring) pipe->monitoring = 1;
323 else pipe->references++;
329 int snd_mixart_kill_ref_pipe(struct mixart_mgr *mgr,
330 struct mixart_pipe *pipe, int monitoring)
334 if(pipe->status == PIPE_UNDEFINED)
338 pipe->monitoring = 0;
342 if((pipe->references <= 0) && (pipe->monitoring == 0)) {
344 struct mixart_msg request;
345 struct mixart_delete_group_resp delete_resp;
347 /* release the clock */
348 err = mixart_set_clock( mgr, pipe, 0);
350 dev_err(&mgr->pci->dev,
351 "mixart_set_clock(0) return error!\n");
355 err = mixart_set_pipe_state(mgr, pipe, 0);
357 dev_err(&mgr->pci->dev, "error stopping pipe!\n");
360 request.message_id = MSG_STREAM_DELETE_GROUP;
361 request.uid = (struct mixart_uid){0,0};
362 request.data = &pipe->group_uid; /* the streaming group ! */
363 request.size = sizeof(pipe->group_uid);
365 /* delete the pipe */
366 err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
367 if ((err < 0) || (delete_resp.status != 0)) {
368 dev_err(&mgr->pci->dev,
369 "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n",
370 err, delete_resp.status);
373 pipe->group_uid = (struct mixart_uid){0,0};
374 pipe->stream_count = 0;
375 pipe->status = PIPE_UNDEFINED;
381 static int mixart_set_stream_state(struct mixart_stream *stream, int start)
383 struct snd_mixart *chip;
384 struct mixart_stream_state_req stream_state_req;
385 struct mixart_msg request;
387 if(!stream->substream)
390 memset(&stream_state_req, 0, sizeof(stream_state_req));
391 stream_state_req.stream_count = 1;
392 stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
393 stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
395 if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
396 request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
398 request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
400 request.uid = (struct mixart_uid){0,0};
401 request.data = &stream_state_req;
402 request.size = sizeof(stream_state_req);
404 stream->abs_period_elapsed = 0; /* reset stream pos */
405 stream->buf_periods = 0;
406 stream->buf_period_frag = 0;
408 chip = snd_pcm_substream_chip(stream->substream);
410 return snd_mixart_send_msg_nonblock(chip->mgr, &request);
417 static int snd_mixart_trigger(struct snd_pcm_substream *subs, int cmd)
419 struct mixart_stream *stream = subs->runtime->private_data;
422 case SNDRV_PCM_TRIGGER_START:
424 dev_dbg(subs->pcm->card->dev, "SNDRV_PCM_TRIGGER_START\n");
427 if( mixart_set_stream_state(stream, 1) )
430 stream->status = MIXART_STREAM_STATUS_RUNNING;
433 case SNDRV_PCM_TRIGGER_STOP:
436 if( mixart_set_stream_state(stream, 0) )
439 stream->status = MIXART_STREAM_STATUS_OPEN;
441 dev_dbg(subs->pcm->card->dev, "SNDRV_PCM_TRIGGER_STOP\n");
445 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
447 stream->status = MIXART_STREAM_STATUS_PAUSE;
448 dev_dbg(subs->pcm->card->dev, "SNDRV_PCM_PAUSE_PUSH\n");
450 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
452 stream->status = MIXART_STREAM_STATUS_RUNNING;
453 dev_dbg(subs->pcm->card->dev, "SNDRV_PCM_PAUSE_RELEASE\n");
461 static int mixart_sync_nonblock_events(struct mixart_mgr *mgr)
463 unsigned long timeout = jiffies + HZ;
464 while (atomic_read(&mgr->msg_processed) > 0) {
465 if (time_after(jiffies, timeout)) {
466 dev_err(&mgr->pci->dev,
467 "mixart: cannot process nonblock events!\n");
470 schedule_timeout_uninterruptible(1);
476 * prepare callback for all pcms
478 static int snd_mixart_prepare(struct snd_pcm_substream *subs)
480 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
481 struct mixart_stream *stream = subs->runtime->private_data;
483 /* TODO de façon non bloquante, réappliquer les hw_params (rate, bits, codec) */
485 dev_dbg(chip->card->dev, "snd_mixart_prepare\n");
487 mixart_sync_nonblock_events(chip->mgr);
489 /* only the first stream can choose the sample rate */
490 /* the further opened streams will be limited to its frequency (see open) */
491 if(chip->mgr->ref_count_rate == 1)
492 chip->mgr->sample_rate = subs->runtime->rate;
494 /* set the clock only once (first stream) on the same pipe */
495 if(stream->pipe->references == 1) {
496 if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) )
504 static int mixart_set_format(struct mixart_stream *stream, snd_pcm_format_t format)
507 struct snd_mixart *chip;
508 struct mixart_msg request;
509 struct mixart_stream_param_desc stream_param;
510 struct mixart_return_uid resp;
512 chip = snd_pcm_substream_chip(stream->substream);
514 memset(&stream_param, 0, sizeof(stream_param));
516 stream_param.coding_type = CT_LINEAR;
517 stream_param.number_of_channel = stream->channels;
519 stream_param.sampling_freq = chip->mgr->sample_rate;
520 if(stream_param.sampling_freq == 0)
521 stream_param.sampling_freq = 44100; /* if frequency not yet defined, use some default */
524 case SNDRV_PCM_FORMAT_U8:
525 stream_param.sample_type = ST_INTEGER_8;
526 stream_param.sample_size = 8;
528 case SNDRV_PCM_FORMAT_S16_LE:
529 stream_param.sample_type = ST_INTEGER_16LE;
530 stream_param.sample_size = 16;
532 case SNDRV_PCM_FORMAT_S16_BE:
533 stream_param.sample_type = ST_INTEGER_16BE;
534 stream_param.sample_size = 16;
536 case SNDRV_PCM_FORMAT_S24_3LE:
537 stream_param.sample_type = ST_INTEGER_24LE;
538 stream_param.sample_size = 24;
540 case SNDRV_PCM_FORMAT_S24_3BE:
541 stream_param.sample_type = ST_INTEGER_24BE;
542 stream_param.sample_size = 24;
544 case SNDRV_PCM_FORMAT_FLOAT_LE:
545 stream_param.sample_type = ST_FLOATING_POINT_32LE;
546 stream_param.sample_size = 32;
548 case SNDRV_PCM_FORMAT_FLOAT_BE:
549 stream_param.sample_type = ST_FLOATING_POINT_32BE;
550 stream_param.sample_size = 32;
553 dev_err(chip->card->dev,
554 "error mixart_set_format() : unknown format\n");
558 dev_dbg(chip->card->dev,
559 "set SNDRV_PCM_FORMAT sample_type(%d) sample_size(%d) freq(%d) channels(%d)\n",
560 stream_param.sample_type, stream_param.sample_size, stream_param.sampling_freq, stream->channels);
562 /* TODO: what else to configure ? */
563 /* stream_param.samples_per_frame = 2; */
564 /* stream_param.bytes_per_frame = 4; */
565 /* stream_param.bytes_per_sample = 2; */
567 stream_param.pipe_count = 1; /* set to 1 */
568 stream_param.stream_count = 1; /* set to 1 */
569 stream_param.stream_desc[0].uid_pipe = stream->pipe->group_uid;
570 stream_param.stream_desc[0].stream_idx = stream->substream->number;
572 request.message_id = MSG_STREAM_SET_INPUT_STAGE_PARAM;
573 request.uid = (struct mixart_uid){0,0};
574 request.data = &stream_param;
575 request.size = sizeof(stream_param);
577 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(resp), &resp);
578 if((err < 0) || resp.error_code) {
579 dev_err(chip->card->dev,
580 "MSG_STREAM_SET_INPUT_STAGE_PARAM err=%x; resp=%x\n",
581 err, resp.error_code);
589 * HW_PARAMS callback for all pcms
591 static int snd_mixart_hw_params(struct snd_pcm_substream *subs,
592 struct snd_pcm_hw_params *hw)
594 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
595 struct mixart_mgr *mgr = chip->mgr;
596 struct mixart_stream *stream = subs->runtime->private_data;
597 snd_pcm_format_t format;
601 /* set up channels */
602 channels = params_channels(hw);
604 /* set up format for the stream */
605 format = params_format(hw);
607 mutex_lock(&mgr->setup_mutex);
609 /* update the stream levels */
610 if( stream->pcm_number <= MIXART_PCM_DIGITAL ) {
611 int is_aes = stream->pcm_number > MIXART_PCM_ANALOG;
612 if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK )
613 mixart_update_playback_stream_level(chip, is_aes, subs->number);
615 mixart_update_capture_stream_level( chip, is_aes);
618 stream->channels = channels;
620 /* set the format to the board */
621 err = mixart_set_format(stream, format);
623 mutex_unlock(&mgr->setup_mutex);
627 /* allocate buffer */
628 err = snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw));
631 struct mixart_bufferinfo *bufferinfo;
632 int i = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (stream->pcm_number * (MIXART_PLAYBACK_STREAMS+MIXART_CAPTURE_STREAMS)) + subs->number;
633 if( subs->stream == SNDRV_PCM_STREAM_CAPTURE ) {
634 i += MIXART_PLAYBACK_STREAMS; /* in array capture is behind playback */
637 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
638 bufferinfo[i].buffer_address = subs->runtime->dma_addr;
639 bufferinfo[i].available_length = subs->runtime->dma_bytes;
640 /* bufferinfo[i].buffer_id is already defined */
642 dev_dbg(chip->card->dev,
643 "snd_mixart_hw_params(pcm %d) : dma_addr(%x) dma_bytes(%x) subs-number(%d)\n",
644 i, bufferinfo[i].buffer_address,
645 bufferinfo[i].available_length,
648 mutex_unlock(&mgr->setup_mutex);
653 static int snd_mixart_hw_free(struct snd_pcm_substream *subs)
655 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
656 snd_pcm_lib_free_pages(subs);
657 mixart_sync_nonblock_events(chip->mgr);
664 * TODO CONFIGURATION SPACE for all pcms, mono pcm must update channels_max
666 static const struct snd_pcm_hardware snd_mixart_analog_caps =
668 .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
669 SNDRV_PCM_INFO_MMAP_VALID |
670 SNDRV_PCM_INFO_PAUSE),
671 .formats = ( SNDRV_PCM_FMTBIT_U8 |
672 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
673 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
674 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
675 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
680 .buffer_bytes_max = (32*1024),
681 .period_bytes_min = 256, /* 256 frames U8 mono*/
682 .period_bytes_max = (16*1024),
684 .periods_max = (32*1024/256),
687 static const struct snd_pcm_hardware snd_mixart_digital_caps =
689 .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
690 SNDRV_PCM_INFO_MMAP_VALID |
691 SNDRV_PCM_INFO_PAUSE),
692 .formats = ( SNDRV_PCM_FMTBIT_U8 |
693 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
694 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
695 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
696 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
701 .buffer_bytes_max = (32*1024),
702 .period_bytes_min = 256, /* 256 frames U8 mono*/
703 .period_bytes_max = (16*1024),
705 .periods_max = (32*1024/256),
709 static int snd_mixart_playback_open(struct snd_pcm_substream *subs)
711 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
712 struct mixart_mgr *mgr = chip->mgr;
713 struct snd_pcm_runtime *runtime = subs->runtime;
714 struct snd_pcm *pcm = subs->pcm;
715 struct mixart_stream *stream;
716 struct mixart_pipe *pipe;
720 mutex_lock(&mgr->setup_mutex);
722 if ( pcm == chip->pcm ) {
723 pcm_number = MIXART_PCM_ANALOG;
724 runtime->hw = snd_mixart_analog_caps;
726 snd_BUG_ON(pcm != chip->pcm_dig);
727 pcm_number = MIXART_PCM_DIGITAL;
728 runtime->hw = snd_mixart_digital_caps;
730 dev_dbg(chip->card->dev,
731 "snd_mixart_playback_open C%d/P%d/Sub%d\n",
732 chip->chip_idx, pcm_number, subs->number);
734 /* get stream info */
735 stream = &(chip->playback_stream[pcm_number][subs->number]);
737 if (stream->status != MIXART_STREAM_STATUS_FREE){
739 dev_err(chip->card->dev,
740 "snd_mixart_playback_open C%d/P%d/Sub%d in use\n",
741 chip->chip_idx, pcm_number, subs->number);
746 /* get pipe pointer (out pipe) */
747 pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 0, 0);
754 /* start the pipe if necessary */
755 err = mixart_set_pipe_state(chip->mgr, pipe, 1);
757 dev_err(chip->card->dev, "error starting pipe!\n");
758 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
764 stream->pcm_number = pcm_number;
765 stream->status = MIXART_STREAM_STATUS_OPEN;
766 stream->substream = subs;
767 stream->channels = 0; /* not configured yet */
769 runtime->private_data = stream;
771 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
772 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
774 /* if a sample rate is already used, another stream cannot change */
775 if(mgr->ref_count_rate++) {
776 if(mgr->sample_rate) {
777 runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
782 mutex_unlock(&mgr->setup_mutex);
788 static int snd_mixart_capture_open(struct snd_pcm_substream *subs)
790 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
791 struct mixart_mgr *mgr = chip->mgr;
792 struct snd_pcm_runtime *runtime = subs->runtime;
793 struct snd_pcm *pcm = subs->pcm;
794 struct mixart_stream *stream;
795 struct mixart_pipe *pipe;
799 mutex_lock(&mgr->setup_mutex);
801 if ( pcm == chip->pcm ) {
802 pcm_number = MIXART_PCM_ANALOG;
803 runtime->hw = snd_mixart_analog_caps;
805 snd_BUG_ON(pcm != chip->pcm_dig);
806 pcm_number = MIXART_PCM_DIGITAL;
807 runtime->hw = snd_mixart_digital_caps;
810 runtime->hw.channels_min = 2; /* for instance, no mono */
812 dev_dbg(chip->card->dev, "snd_mixart_capture_open C%d/P%d/Sub%d\n",
813 chip->chip_idx, pcm_number, subs->number);
815 /* get stream info */
816 stream = &(chip->capture_stream[pcm_number]);
818 if (stream->status != MIXART_STREAM_STATUS_FREE){
820 dev_err(chip->card->dev,
821 "snd_mixart_capture_open C%d/P%d/Sub%d in use\n",
822 chip->chip_idx, pcm_number, subs->number);
827 /* get pipe pointer (in pipe) */
828 pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 1, 0);
835 /* start the pipe if necessary */
836 err = mixart_set_pipe_state(chip->mgr, pipe, 1);
838 dev_err(chip->card->dev, "error starting pipe!\n");
839 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
845 stream->pcm_number = pcm_number;
846 stream->status = MIXART_STREAM_STATUS_OPEN;
847 stream->substream = subs;
848 stream->channels = 0; /* not configured yet */
850 runtime->private_data = stream;
852 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
853 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
855 /* if a sample rate is already used, another stream cannot change */
856 if(mgr->ref_count_rate++) {
857 if(mgr->sample_rate) {
858 runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
863 mutex_unlock(&mgr->setup_mutex);
870 static int snd_mixart_close(struct snd_pcm_substream *subs)
872 struct snd_mixart *chip = snd_pcm_substream_chip(subs);
873 struct mixart_mgr *mgr = chip->mgr;
874 struct mixart_stream *stream = subs->runtime->private_data;
876 mutex_lock(&mgr->setup_mutex);
878 dev_dbg(chip->card->dev, "snd_mixart_close C%d/P%d/Sub%d\n",
879 chip->chip_idx, stream->pcm_number, subs->number);
881 /* sample rate released */
882 if(--mgr->ref_count_rate == 0) {
883 mgr->sample_rate = 0;
887 if (snd_mixart_kill_ref_pipe(mgr, stream->pipe, 0 ) < 0) {
889 dev_err(chip->card->dev,
890 "error snd_mixart_kill_ref_pipe C%dP%d\n",
891 chip->chip_idx, stream->pcm_number);
895 stream->status = MIXART_STREAM_STATUS_FREE;
896 stream->substream = NULL;
898 mutex_unlock(&mgr->setup_mutex);
903 static snd_pcm_uframes_t snd_mixart_stream_pointer(struct snd_pcm_substream *subs)
905 struct snd_pcm_runtime *runtime = subs->runtime;
906 struct mixart_stream *stream = runtime->private_data;
908 return (snd_pcm_uframes_t)((stream->buf_periods * runtime->period_size) + stream->buf_period_frag);
913 static const struct snd_pcm_ops snd_mixart_playback_ops = {
914 .open = snd_mixart_playback_open,
915 .close = snd_mixart_close,
916 .ioctl = snd_pcm_lib_ioctl,
917 .prepare = snd_mixart_prepare,
918 .hw_params = snd_mixart_hw_params,
919 .hw_free = snd_mixart_hw_free,
920 .trigger = snd_mixart_trigger,
921 .pointer = snd_mixart_stream_pointer,
924 static const struct snd_pcm_ops snd_mixart_capture_ops = {
925 .open = snd_mixart_capture_open,
926 .close = snd_mixart_close,
927 .ioctl = snd_pcm_lib_ioctl,
928 .prepare = snd_mixart_prepare,
929 .hw_params = snd_mixart_hw_params,
930 .hw_free = snd_mixart_hw_free,
931 .trigger = snd_mixart_trigger,
932 .pointer = snd_mixart_stream_pointer,
935 static void preallocate_buffers(struct snd_mixart *chip, struct snd_pcm *pcm)
938 struct snd_pcm_substream *subs;
941 for (stream = 0; stream < 2; stream++) {
943 for (subs = pcm->streams[stream].substream; subs; subs = subs->next, idx++)
944 /* set up the unique device id with the chip index */
945 subs->dma_device.id = subs->pcm->device << 16 |
946 subs->stream << 8 | (subs->number + 1) |
947 (chip->chip_idx + 1) << 24;
950 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
951 snd_dma_pci_data(chip->mgr->pci), 32*1024, 32*1024);
956 static int snd_mixart_pcm_analog(struct snd_mixart *chip)
962 sprintf(name, "miXart analog %d", chip->chip_idx);
963 if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_ANALOG,
964 MIXART_PLAYBACK_STREAMS,
965 MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
966 dev_err(chip->card->dev,
967 "cannot create the analog pcm %d\n", chip->chip_idx);
971 pcm->private_data = chip;
973 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
974 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
977 pcm->nonatomic = true;
978 strcpy(pcm->name, name);
980 preallocate_buffers(chip, pcm);
989 static int snd_mixart_pcm_digital(struct snd_mixart *chip)
995 sprintf(name, "miXart AES/EBU %d", chip->chip_idx);
996 if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_DIGITAL,
997 MIXART_PLAYBACK_STREAMS,
998 MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
999 dev_err(chip->card->dev,
1000 "cannot create the digital pcm %d\n", chip->chip_idx);
1004 pcm->private_data = chip;
1006 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
1007 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
1009 pcm->info_flags = 0;
1010 pcm->nonatomic = true;
1011 strcpy(pcm->name, name);
1013 preallocate_buffers(chip, pcm);
1015 chip->pcm_dig = pcm;
1019 static int snd_mixart_chip_free(struct snd_mixart *chip)
1025 static int snd_mixart_chip_dev_free(struct snd_device *device)
1027 struct snd_mixart *chip = device->device_data;
1028 return snd_mixart_chip_free(chip);
1034 static int snd_mixart_create(struct mixart_mgr *mgr, struct snd_card *card, int idx)
1037 struct snd_mixart *chip;
1038 static struct snd_device_ops ops = {
1039 .dev_free = snd_mixart_chip_dev_free,
1042 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1047 chip->chip_idx = idx;
1050 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1051 snd_mixart_chip_free(chip);
1055 mgr->chip[idx] = chip;
1059 int snd_mixart_create_pcm(struct snd_mixart* chip)
1063 err = snd_mixart_pcm_analog(chip);
1067 if(chip->mgr->board_type == MIXART_DAUGHTER_TYPE_AES) {
1069 err = snd_mixart_pcm_digital(chip);
1078 * release all the cards assigned to a manager instance
1080 static int snd_mixart_free(struct mixart_mgr *mgr)
1084 for (i = 0; i < mgr->num_cards; i++) {
1086 snd_card_free(mgr->chip[i]->card);
1090 snd_mixart_exit_mailbox(mgr);
1094 free_irq(mgr->irq, mgr);
1096 /* reset board if some firmware was loaded */
1097 if(mgr->dsp_loaded) {
1098 snd_mixart_reset_board(mgr);
1099 dev_dbg(&mgr->pci->dev, "reset miXart !\n");
1102 /* release the i/o ports */
1103 for (i = 0; i < 2; ++i)
1104 iounmap(mgr->mem[i].virt);
1106 pci_release_regions(mgr->pci);
1108 /* free flowarray */
1109 if(mgr->flowinfo.area) {
1110 snd_dma_free_pages(&mgr->flowinfo);
1111 mgr->flowinfo.area = NULL;
1113 /* free bufferarray */
1114 if(mgr->bufferinfo.area) {
1115 snd_dma_free_pages(&mgr->bufferinfo);
1116 mgr->bufferinfo.area = NULL;
1119 pci_disable_device(mgr->pci);
1129 mixart_BA0 proc interface for BAR 0 - read callback
1131 static ssize_t snd_mixart_BA0_read(struct snd_info_entry *entry,
1132 void *file_private_data,
1133 struct file *file, char __user *buf,
1134 size_t count, loff_t pos)
1136 struct mixart_mgr *mgr = entry->private_data;
1138 count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1139 if (copy_to_user_fromio(buf, MIXART_MEM(mgr, pos), count))
1145 mixart_BA1 proc interface for BAR 1 - read callback
1147 static ssize_t snd_mixart_BA1_read(struct snd_info_entry *entry,
1148 void *file_private_data,
1149 struct file *file, char __user *buf,
1150 size_t count, loff_t pos)
1152 struct mixart_mgr *mgr = entry->private_data;
1154 count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1155 if (copy_to_user_fromio(buf, MIXART_REG(mgr, pos), count))
1160 static struct snd_info_entry_ops snd_mixart_proc_ops_BA0 = {
1161 .read = snd_mixart_BA0_read,
1164 static struct snd_info_entry_ops snd_mixart_proc_ops_BA1 = {
1165 .read = snd_mixart_BA1_read,
1169 static void snd_mixart_proc_read(struct snd_info_entry *entry,
1170 struct snd_info_buffer *buffer)
1172 struct snd_mixart *chip = entry->private_data;
1175 snd_iprintf(buffer, "Digigram miXart (alsa card %d)\n\n", chip->chip_idx);
1177 /* stats available when embedded OS is running */
1178 if (chip->mgr->dsp_loaded & ( 1 << MIXART_MOTHERBOARD_ELF_INDEX)) {
1179 snd_iprintf(buffer, "- hardware -\n");
1180 switch (chip->mgr->board_type ) {
1181 case MIXART_DAUGHTER_TYPE_NONE : snd_iprintf(buffer, "\tmiXart8 (no daughter board)\n\n"); break;
1182 case MIXART_DAUGHTER_TYPE_AES : snd_iprintf(buffer, "\tmiXart8 AES/EBU\n\n"); break;
1183 case MIXART_DAUGHTER_TYPE_COBRANET : snd_iprintf(buffer, "\tmiXart8 Cobranet\n\n"); break;
1184 default: snd_iprintf(buffer, "\tUNKNOWN!\n\n"); break;
1187 snd_iprintf(buffer, "- system load -\n");
1189 /* get perf reference */
1191 ref = readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_SYSTEM_LOAD_OFFSET));
1194 u32 mailbox = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_MAILBX_LOAD_OFFSET)) / ref;
1195 u32 streaming = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_STREAM_LOAD_OFFSET)) / ref;
1196 u32 interr = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_INTERR_LOAD_OFFSET)) / ref;
1198 snd_iprintf(buffer, "\tstreaming : %d\n", streaming);
1199 snd_iprintf(buffer, "\tmailbox : %d\n", mailbox);
1200 snd_iprintf(buffer, "\tinterrupts handling : %d\n\n", interr);
1202 } /* endif elf loaded */
1205 static void snd_mixart_proc_init(struct snd_mixart *chip)
1207 struct snd_info_entry *entry;
1209 /* text interface to read perf and temp meters */
1210 snd_card_ro_proc_new(chip->card, "board_info", chip,
1211 snd_mixart_proc_read);
1213 if (! snd_card_proc_new(chip->card, "mixart_BA0", &entry)) {
1214 entry->content = SNDRV_INFO_CONTENT_DATA;
1215 entry->private_data = chip->mgr;
1216 entry->c.ops = &snd_mixart_proc_ops_BA0;
1217 entry->size = MIXART_BA0_SIZE;
1219 if (! snd_card_proc_new(chip->card, "mixart_BA1", &entry)) {
1220 entry->content = SNDRV_INFO_CONTENT_DATA;
1221 entry->private_data = chip->mgr;
1222 entry->c.ops = &snd_mixart_proc_ops_BA1;
1223 entry->size = MIXART_BA1_SIZE;
1226 /* end of proc interface */
1230 * probe function - creates the card manager
1232 static int snd_mixart_probe(struct pci_dev *pci,
1233 const struct pci_device_id *pci_id)
1236 struct mixart_mgr *mgr;
1243 if (dev >= SNDRV_CARDS)
1245 if (! enable[dev]) {
1250 /* enable PCI device */
1251 if ((err = pci_enable_device(pci)) < 0)
1253 pci_set_master(pci);
1255 /* check if we can restrict PCI DMA transfers to 32 bits */
1256 if (dma_set_mask(&pci->dev, DMA_BIT_MASK(32)) < 0) {
1258 "architecture does not support 32bit PCI busmaster DMA\n");
1259 pci_disable_device(pci);
1265 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
1267 pci_disable_device(pci);
1274 /* resource assignment */
1275 if ((err = pci_request_regions(pci, CARD_NAME)) < 0) {
1277 pci_disable_device(pci);
1280 for (i = 0; i < 2; i++) {
1281 mgr->mem[i].phys = pci_resource_start(pci, i);
1282 mgr->mem[i].virt = pci_ioremap_bar(pci, i);
1283 if (!mgr->mem[i].virt) {
1284 dev_err(&pci->dev, "unable to remap resource 0x%lx\n",
1286 snd_mixart_free(mgr);
1291 if (request_threaded_irq(pci->irq, snd_mixart_interrupt,
1292 snd_mixart_threaded_irq, IRQF_SHARED,
1293 KBUILD_MODNAME, mgr)) {
1294 dev_err(&pci->dev, "unable to grab IRQ %d\n", pci->irq);
1295 snd_mixart_free(mgr);
1298 mgr->irq = pci->irq;
1301 mgr->msg_fifo_readptr = 0;
1302 mgr->msg_fifo_writeptr = 0;
1304 mutex_init(&mgr->lock);
1305 mutex_init(&mgr->msg_lock);
1306 init_waitqueue_head(&mgr->msg_sleep);
1307 atomic_set(&mgr->msg_processed, 0);
1309 /* init setup mutex*/
1310 mutex_init(&mgr->setup_mutex);
1312 /* card assignment */
1313 mgr->num_cards = MIXART_MAX_CARDS; /* 4 FIXME: configurable? */
1314 for (i = 0; i < mgr->num_cards; i++) {
1315 struct snd_card *card;
1322 idx = index[dev] + i;
1323 snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : "MIXART", i);
1324 err = snd_card_new(&pci->dev, idx, tmpid, THIS_MODULE,
1328 dev_err(&pci->dev, "cannot allocate the card %d\n", i);
1329 snd_mixart_free(mgr);
1333 strcpy(card->driver, CARD_NAME);
1334 snprintf(card->shortname, sizeof(card->shortname),
1335 "Digigram miXart [PCM #%d]", i);
1336 snprintf(card->longname, sizeof(card->longname),
1337 "Digigram miXart at 0x%lx & 0x%lx, irq %i [PCM #%d]",
1338 mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq, i);
1340 if ((err = snd_mixart_create(mgr, card, i)) < 0) {
1341 snd_card_free(card);
1342 snd_mixart_free(mgr);
1347 /* init proc interface only for chip0 */
1348 snd_mixart_proc_init(mgr->chip[i]);
1351 if ((err = snd_card_register(card)) < 0) {
1352 snd_mixart_free(mgr);
1357 /* init firmware status (mgr->dsp_loaded reset in hwdep_new) */
1358 mgr->board_type = MIXART_DAUGHTER_TYPE_NONE;
1360 /* create array of streaminfo */
1361 size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
1362 sizeof(struct mixart_flowinfo)) );
1363 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1364 size, &mgr->flowinfo) < 0) {
1365 snd_mixart_free(mgr);
1368 /* init streaminfo_array */
1369 memset(mgr->flowinfo.area, 0, size);
1371 /* create array of bufferinfo */
1372 size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
1373 sizeof(struct mixart_bufferinfo)) );
1374 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1375 size, &mgr->bufferinfo) < 0) {
1376 snd_mixart_free(mgr);
1379 /* init bufferinfo_array */
1380 memset(mgr->bufferinfo.area, 0, size);
1382 /* set up firmware */
1383 err = snd_mixart_setup_firmware(mgr);
1385 snd_mixart_free(mgr);
1389 pci_set_drvdata(pci, mgr);
1394 static void snd_mixart_remove(struct pci_dev *pci)
1396 snd_mixart_free(pci_get_drvdata(pci));
1399 static struct pci_driver mixart_driver = {
1400 .name = KBUILD_MODNAME,
1401 .id_table = snd_mixart_ids,
1402 .probe = snd_mixart_probe,
1403 .remove = snd_mixart_remove,
1406 module_pci_driver(mixart_driver);