2 * Driver for Digigram miXart soundcards
4 * main file with alsa callbacks
6 * Copyright (c) 2003 by Digigram <alsa@digigram.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/pci.h>
28 #include <linux/moduleparam.h>
29 #include <sound/core.h>
30 #include <sound/initval.h>
31 #include <sound/info.h>
32 #include <sound/control.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
36 #include "mixart_hwdep.h"
37 #include "mixart_core.h"
38 #include "mixart_mixer.h"
40 #define CARD_NAME "miXart"
42 MODULE_AUTHOR("Digigram <alsa@digigram.com>");
43 MODULE_DESCRIPTION("Digigram " CARD_NAME);
44 MODULE_LICENSE("GPL");
45 MODULE_SUPPORTED_DEVICE("{{Digigram," CARD_NAME "}}");
47 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
48 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
49 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
52 module_param_array(index, int, boot_devs, 0444);
53 MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
54 module_param_array(id, charp, boot_devs, 0444);
55 MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
56 module_param_array(enable, bool, boot_devs, 0444);
57 MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
62 static struct pci_device_id snd_mixart_ids[] = {
63 { 0x1057, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* MC8240 */
67 MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
70 static int mixart_set_pipe_state(mixart_mgr_t *mgr, mixart_pipe_t* pipe, int start)
72 mixart_group_state_req_t group_state;
73 mixart_group_state_resp_t group_state_resp;
78 switch(pipe->status) {
81 if(start) return 0; /* already started */
84 if(!start) return 0; /* already stopped */
87 snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");
88 return -EINVAL; /* function called with wrong pipe status */
91 system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
93 /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
95 request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
96 request.uid = (mixart_uid_t){0,0};
97 request.data = &system_msg_uid;
98 request.size = sizeof(system_msg_uid);
100 err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
102 snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
106 /* start or stop the pipe (1 pipe) */
108 memset(&group_state, 0, sizeof(group_state));
109 group_state.pipe_count = 1;
110 group_state.pipe_uid[0] = pipe->group_uid;
113 request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
115 request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
117 request.uid = pipe->group_uid; /*(mixart_uid_t){0,0};*/
118 request.data = &group_state;
119 request.size = sizeof(group_state);
121 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
122 if (err < 0 || group_state_resp.txx_status != 0) {
123 snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
130 group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
132 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
133 if (err < 0 || group_state_resp.txx_status != 0) {
134 snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
138 /* in case of start send a synchro top */
140 request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
141 request.uid = (mixart_uid_t){0,0};
145 err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
146 if (err < 0 || stat != 0) {
147 snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);
151 pipe->status = PIPE_RUNNING;
154 pipe->status = PIPE_STOPPED;
160 static int mixart_set_clock(mixart_mgr_t *mgr, mixart_pipe_t *pipe, unsigned int rate)
162 mixart_msg_t request;
163 mixart_clock_properties_t clock_properties;
164 mixart_clock_properties_resp_t clock_prop_resp;
167 switch(pipe->status) {
175 return 0; /* nothing to do */
177 snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);
182 memset(&clock_properties, 0, sizeof(clock_properties));
183 clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
184 clock_properties.clock_mode = CM_STANDALONE;
185 clock_properties.frequency = rate;
186 clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
187 clock_properties.uid_caller[0] = pipe->group_uid;
189 snd_printdd("mixart_set_clock to %d kHz\n", rate);
191 request.message_id = MSG_CLOCK_SET_PROPERTIES;
192 request.uid = mgr->uid_console_manager;
193 request.data = &clock_properties;
194 request.size = sizeof(clock_properties);
196 err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
197 if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
198 snd_printk(KERN_ERR "error MSG_CLOCK_SET_PROPERTIES err=%x stat=%x mod=%x !\n", err, clock_prop_resp.status, clock_prop_resp.clock_mode);
202 if(rate) pipe->status = PIPE_CLOCK_SET;
203 else pipe->status = PIPE_RUNNING;
210 * Allocate or reference output pipe for analog IOs (pcmp0/1)
212 mixart_pipe_t* snd_mixart_add_ref_pipe( mixart_t *chip, int pcm_number, int capture, int monitoring)
216 mixart_msg_t request;
219 if (pcm_number == MIXART_PCM_ANALOG) {
220 pipe = &(chip->pipe_in_ana); /* analog inputs */
222 pipe = &(chip->pipe_in_dig); /* digital inputs */
224 request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
225 stream_count = MIXART_CAPTURE_STREAMS;
227 if (pcm_number == MIXART_PCM_ANALOG) {
228 pipe = &(chip->pipe_out_ana); /* analog outputs */
230 pipe = &(chip->pipe_out_dig); /* digital outputs */
232 request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
233 stream_count = MIXART_PLAYBACK_STREAMS;
236 /* a new stream is opened and there are already all streams in use */
237 if( (monitoring == 0) && (pipe->references >= stream_count) ) {
241 /* pipe is not yet defined */
242 if( pipe->status == PIPE_UNDEFINED ) {
245 mixart_streaming_group_req_t sgroup_req;
246 mixart_streaming_group_t sgroup_resp;
249 snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);
251 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
255 request.uid = (mixart_uid_t){0,0}; /* should be StreamManagerUID, but zero is OK if there is only one ! */
256 request.data = &buf->sgroup_req;
257 request.size = sizeof(buf->sgroup_req);
259 memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));
261 buf->sgroup_req.stream_count = stream_count;
262 buf->sgroup_req.channel_count = 2;
263 buf->sgroup_req.latency = 256;
264 buf->sgroup_req.connector = pipe->uid_left_connector; /* the left connector */
266 for (i=0; i<stream_count; i++) {
268 struct mixart_flowinfo *flowinfo;
269 struct mixart_bufferinfo *bufferinfo;
271 /* we don't yet know the format, so config 16 bit pcm audio for instance */
272 buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;
273 buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;
274 buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
276 /* find the right bufferinfo_array */
277 j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;
278 if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */
280 buf->sgroup_req.flow_entry[i] = j;
282 flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;
283 flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(mixart_bufferinfo_t));
284 flowinfo[j].bufferinfo_count = 1; /* 1 will set the miXart to ring-buffer mode ! */
286 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
287 bufferinfo[j].buffer_address = 0; /* buffer is not yet allocated */
288 bufferinfo[j].available_length = 0; /* buffer is not yet allocated */
290 /* construct the identifier of the stream buffer received in the interrupts ! */
291 bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;
293 bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
297 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);
298 if((err < 0) || (buf->sgroup_resp.status != 0)) {
299 snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, buf->sgroup_resp.status);
304 pipe->group_uid = buf->sgroup_resp.group; /* id of the pipe, as returned by embedded */
305 pipe->stream_count = buf->sgroup_resp.stream_count;
306 /* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */
308 pipe->status = PIPE_STOPPED;
312 if(monitoring) pipe->monitoring = 1;
313 else pipe->references++;
319 int snd_mixart_kill_ref_pipe( mixart_mgr_t *mgr, mixart_pipe_t *pipe, int monitoring)
323 if(pipe->status == PIPE_UNDEFINED)
327 pipe->monitoring = 0;
331 if((pipe->references <= 0) && (pipe->monitoring == 0)) {
333 mixart_msg_t request;
334 mixart_delete_group_resp_t delete_resp;
336 /* release the clock */
337 err = mixart_set_clock( mgr, pipe, 0);
339 snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");
343 err = mixart_set_pipe_state(mgr, pipe, 0);
345 snd_printk(KERN_ERR "error stopping pipe!\n");
348 request.message_id = MSG_STREAM_DELETE_GROUP;
349 request.uid = (mixart_uid_t){0,0};
350 request.data = &pipe->group_uid; /* the streaming group ! */
351 request.size = sizeof(pipe->group_uid);
353 /* delete the pipe */
354 err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
355 if ((err < 0) || (delete_resp.status != 0)) {
356 snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);
359 pipe->group_uid = (mixart_uid_t){0,0};
360 pipe->stream_count = 0;
361 pipe->status = PIPE_UNDEFINED;
367 static int mixart_set_stream_state(mixart_stream_t *stream, int start)
370 mixart_stream_state_req_t stream_state_req;
371 mixart_msg_t request;
373 if(!stream->substream)
376 memset(&stream_state_req, 0, sizeof(stream_state_req));
377 stream_state_req.stream_count = 1;
378 stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
379 stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
381 if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
382 request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
384 request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
386 request.uid = (mixart_uid_t){0,0};
387 request.data = &stream_state_req;
388 request.size = sizeof(stream_state_req);
390 stream->abs_period_elapsed = 0; /* reset stream pos */
391 stream->buf_periods = 0;
392 stream->buf_period_frag = 0;
394 chip = snd_pcm_substream_chip(stream->substream);
396 return snd_mixart_send_msg_nonblock(chip->mgr, &request);
403 static int snd_mixart_trigger(snd_pcm_substream_t *subs, int cmd)
405 mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
408 case SNDRV_PCM_TRIGGER_START:
410 snd_printdd("SNDRV_PCM_TRIGGER_START\n");
413 if( mixart_set_stream_state(stream, 1) )
416 stream->status = MIXART_STREAM_STATUS_RUNNING;
419 case SNDRV_PCM_TRIGGER_STOP:
422 if( mixart_set_stream_state(stream, 0) )
425 stream->status = MIXART_STREAM_STATUS_OPEN;
427 snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
431 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
433 stream->status = MIXART_STREAM_STATUS_PAUSE;
434 snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");
436 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
438 stream->status = MIXART_STREAM_STATUS_RUNNING;
439 snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");
447 static int mixart_sync_nonblock_events(mixart_mgr_t *mgr)
450 while (atomic_read(&mgr->msg_processed) > 0) {
452 snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");
455 set_current_state(TASK_UNINTERRUPTIBLE);
462 * prepare callback for all pcms
464 static int snd_mixart_prepare(snd_pcm_substream_t *subs)
466 mixart_t *chip = snd_pcm_substream_chip(subs);
467 mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
469 /* TODO de façon non bloquante, réappliquer les hw_params (rate, bits, codec) */
471 snd_printdd("snd_mixart_prepare\n");
473 mixart_sync_nonblock_events(chip->mgr);
475 /* only the first stream can choose the sample rate */
476 /* the further opened streams will be limited to its frequency (see open) */
477 if(chip->mgr->ref_count_rate == 1)
478 chip->mgr->sample_rate = subs->runtime->rate;
480 /* set the clock only once (first stream) on the same pipe */
481 if(stream->pipe->references == 1) {
482 if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) )
490 static int mixart_set_format(mixart_stream_t *stream, snd_pcm_format_t format)
494 mixart_msg_t request;
495 mixart_stream_param_desc_t stream_param;
496 mixart_return_uid_t resp;
498 chip = snd_pcm_substream_chip(stream->substream);
500 memset(&stream_param, 0, sizeof(stream_param));
502 stream_param.coding_type = CT_LINEAR;
503 stream_param.number_of_channel = stream->channels;
505 stream_param.sampling_freq = chip->mgr->sample_rate;
506 if(stream_param.sampling_freq == 0)
507 stream_param.sampling_freq = 44100; /* if frequency not yet defined, use some default */
510 case SNDRV_PCM_FORMAT_U8:
511 stream_param.sample_type = ST_INTEGER_8;
512 stream_param.sample_size = 8;
514 case SNDRV_PCM_FORMAT_S16_LE:
515 stream_param.sample_type = ST_INTEGER_16LE;
516 stream_param.sample_size = 16;
518 case SNDRV_PCM_FORMAT_S16_BE:
519 stream_param.sample_type = ST_INTEGER_16BE;
520 stream_param.sample_size = 16;
522 case SNDRV_PCM_FORMAT_S24_3LE:
523 stream_param.sample_type = ST_INTEGER_24LE;
524 stream_param.sample_size = 24;
526 case SNDRV_PCM_FORMAT_S24_3BE:
527 stream_param.sample_type = ST_INTEGER_24BE;
528 stream_param.sample_size = 24;
530 case SNDRV_PCM_FMTBIT_FLOAT_LE:
531 stream_param.sample_type = ST_FLOATING_POINT_32LE;
532 stream_param.sample_size = 32;
534 case SNDRV_PCM_FMTBIT_FLOAT_BE:
535 stream_param.sample_type = ST_FLOATING_POINT_32BE;
536 stream_param.sample_size = 32;
539 snd_printk(KERN_ERR "error mixart_set_format() : unknown format\n");
543 snd_printdd("set SNDRV_PCM_FORMAT sample_type(%d) sample_size(%d) freq(%d) channels(%d)\n",
544 stream_param.sample_type, stream_param.sample_size, stream_param.sampling_freq, stream->channels);
546 /* TODO: what else to configure ? */
547 /* stream_param.samples_per_frame = 2; */
548 /* stream_param.bytes_per_frame = 4; */
549 /* stream_param.bytes_per_sample = 2; */
551 stream_param.pipe_count = 1; /* set to 1 */
552 stream_param.stream_count = 1; /* set to 1 */
553 stream_param.stream_desc[0].uid_pipe = stream->pipe->group_uid;
554 stream_param.stream_desc[0].stream_idx = stream->substream->number;
556 request.message_id = MSG_STREAM_SET_INPUT_STAGE_PARAM;
557 request.uid = (mixart_uid_t){0,0};
558 request.data = &stream_param;
559 request.size = sizeof(stream_param);
561 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(resp), &resp);
562 if((err < 0) || resp.error_code) {
563 snd_printk(KERN_ERR "MSG_STREAM_SET_INPUT_STAGE_PARAM err=%x; resp=%x\n", err, resp.error_code);
571 * HW_PARAMS callback for all pcms
573 static int snd_mixart_hw_params(snd_pcm_substream_t *subs,
574 snd_pcm_hw_params_t *hw)
576 mixart_t *chip = snd_pcm_substream_chip(subs);
577 mixart_mgr_t *mgr = chip->mgr;
578 mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
579 snd_pcm_format_t format;
583 /* set up channels */
584 channels = params_channels(hw);
586 /* set up format for the stream */
587 format = params_format(hw);
589 down(&mgr->setup_mutex);
591 /* update the stream levels */
592 if( stream->pcm_number <= MIXART_PCM_DIGITAL ) {
593 int is_aes = stream->pcm_number > MIXART_PCM_ANALOG;
594 if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK )
595 mixart_update_playback_stream_level(chip, is_aes, subs->number);
597 mixart_update_capture_stream_level( chip, is_aes);
600 stream->channels = channels;
602 /* set the format to the board */
603 err = mixart_set_format(stream, format);
608 /* allocate buffer */
609 err = snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw));
612 struct mixart_bufferinfo *bufferinfo;
613 int i = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (stream->pcm_number * (MIXART_PLAYBACK_STREAMS+MIXART_CAPTURE_STREAMS)) + subs->number;
614 if( subs->stream == SNDRV_PCM_STREAM_CAPTURE ) {
615 i += MIXART_PLAYBACK_STREAMS; /* in array capture is behind playback */
618 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
619 bufferinfo[i].buffer_address = subs->runtime->dma_addr;
620 bufferinfo[i].available_length = subs->runtime->dma_bytes;
621 /* bufferinfo[i].buffer_id is already defined */
623 snd_printdd("snd_mixart_hw_params(pcm %d) : dma_addr(%x) dma_bytes(%x) subs-number(%d)\n", i,
624 bufferinfo[i].buffer_address,
625 bufferinfo[i].available_length,
628 up(&mgr->setup_mutex);
633 static int snd_mixart_hw_free(snd_pcm_substream_t *subs)
635 mixart_t *chip = snd_pcm_substream_chip(subs);
636 snd_pcm_lib_free_pages(subs);
637 mixart_sync_nonblock_events(chip->mgr);
644 * TODO CONFIGURATION SPACE for all pcms, mono pcm must update channels_max
646 static snd_pcm_hardware_t snd_mixart_analog_caps =
648 .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
649 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
650 SNDRV_PCM_INFO_PAUSE),
651 .formats = ( SNDRV_PCM_FMTBIT_U8 |
652 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
653 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
654 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
655 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
660 .buffer_bytes_max = (32*1024),
661 .period_bytes_min = 256, /* 256 frames U8 mono*/
662 .period_bytes_max = (16*1024),
664 .periods_max = (32*1024/256),
667 static snd_pcm_hardware_t snd_mixart_digital_caps =
669 .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
670 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
671 SNDRV_PCM_INFO_PAUSE),
672 .formats = ( SNDRV_PCM_FMTBIT_U8 |
673 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
674 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
675 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
676 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
681 .buffer_bytes_max = (32*1024),
682 .period_bytes_min = 256, /* 256 frames U8 mono*/
683 .period_bytes_max = (16*1024),
685 .periods_max = (32*1024/256),
689 static int snd_mixart_playback_open(snd_pcm_substream_t *subs)
691 mixart_t *chip = snd_pcm_substream_chip(subs);
692 mixart_mgr_t *mgr = chip->mgr;
693 snd_pcm_runtime_t *runtime = subs->runtime;
694 snd_pcm_t *pcm = subs->pcm;
695 mixart_stream_t *stream;
700 down(&mgr->setup_mutex);
702 if ( pcm == chip->pcm ) {
703 pcm_number = MIXART_PCM_ANALOG;
704 runtime->hw = snd_mixart_analog_caps;
706 snd_assert ( pcm == chip->pcm_dig );
707 pcm_number = MIXART_PCM_DIGITAL;
708 runtime->hw = snd_mixart_digital_caps;
710 snd_printdd("snd_mixart_playback_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
712 /* get stream info */
713 stream = &(chip->playback_stream[pcm_number][subs->number]);
715 if (stream->status != MIXART_STREAM_STATUS_FREE){
717 snd_printk(KERN_ERR "snd_mixart_playback_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
722 /* get pipe pointer (out pipe) */
723 pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 0, 0);
730 /* start the pipe if necessary */
731 err = mixart_set_pipe_state(chip->mgr, pipe, 1);
733 snd_printk(KERN_ERR "error starting pipe!\n");
734 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
740 stream->pcm_number = pcm_number;
741 stream->status = MIXART_STREAM_STATUS_OPEN;
742 stream->substream = subs;
743 stream->channels = 0; /* not configured yet */
745 runtime->private_data = stream;
747 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
748 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
750 /* if a sample rate is already used, another stream cannot change */
751 if(mgr->ref_count_rate++) {
752 if(mgr->sample_rate) {
753 runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
758 up(&mgr->setup_mutex);
764 static int snd_mixart_capture_open(snd_pcm_substream_t *subs)
766 mixart_t *chip = snd_pcm_substream_chip(subs);
767 mixart_mgr_t *mgr = chip->mgr;
768 snd_pcm_runtime_t *runtime = subs->runtime;
769 snd_pcm_t *pcm = subs->pcm;
770 mixart_stream_t *stream;
775 down(&mgr->setup_mutex);
777 if ( pcm == chip->pcm ) {
778 pcm_number = MIXART_PCM_ANALOG;
779 runtime->hw = snd_mixart_analog_caps;
781 snd_assert ( pcm == chip->pcm_dig );
782 pcm_number = MIXART_PCM_DIGITAL;
783 runtime->hw = snd_mixart_digital_caps;
786 runtime->hw.channels_min = 2; /* for instance, no mono */
788 snd_printdd("snd_mixart_capture_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
790 /* get stream info */
791 stream = &(chip->capture_stream[pcm_number]);
793 if (stream->status != MIXART_STREAM_STATUS_FREE){
795 snd_printk(KERN_ERR "snd_mixart_capture_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
800 /* get pipe pointer (in pipe) */
801 pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 1, 0);
808 /* start the pipe if necessary */
809 err = mixart_set_pipe_state(chip->mgr, pipe, 1);
811 snd_printk(KERN_ERR "error starting pipe!\n");
812 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
818 stream->pcm_number = pcm_number;
819 stream->status = MIXART_STREAM_STATUS_OPEN;
820 stream->substream = subs;
821 stream->channels = 0; /* not configured yet */
823 runtime->private_data = stream;
825 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
826 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
828 /* if a sample rate is already used, another stream cannot change */
829 if(mgr->ref_count_rate++) {
830 if(mgr->sample_rate) {
831 runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
836 up(&mgr->setup_mutex);
843 static int snd_mixart_close(snd_pcm_substream_t *subs)
845 mixart_t *chip = snd_pcm_substream_chip(subs);
846 mixart_mgr_t *mgr = chip->mgr;
847 mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
849 down(&mgr->setup_mutex);
851 snd_printdd("snd_mixart_close C%d/P%d/Sub%d\n", chip->chip_idx, stream->pcm_number, subs->number);
853 /* sample rate released */
854 if(--mgr->ref_count_rate == 0) {
855 mgr->sample_rate = 0;
859 if (snd_mixart_kill_ref_pipe(mgr, stream->pipe, 0 ) < 0) {
861 snd_printk(KERN_ERR "error snd_mixart_kill_ref_pipe C%dP%d\n", chip->chip_idx, stream->pcm_number);
865 stream->status = MIXART_STREAM_STATUS_FREE;
866 stream->substream = NULL;
868 up(&mgr->setup_mutex);
873 static snd_pcm_uframes_t snd_mixart_stream_pointer(snd_pcm_substream_t * subs)
875 snd_pcm_runtime_t *runtime = subs->runtime;
876 mixart_stream_t *stream = (mixart_stream_t*)runtime->private_data;
878 return (snd_pcm_uframes_t)((stream->buf_periods * runtime->period_size) + stream->buf_period_frag);
883 static snd_pcm_ops_t snd_mixart_playback_ops = {
884 .open = snd_mixart_playback_open,
885 .close = snd_mixart_close,
886 .ioctl = snd_pcm_lib_ioctl,
887 .prepare = snd_mixart_prepare,
888 .hw_params = snd_mixart_hw_params,
889 .hw_free = snd_mixart_hw_free,
890 .trigger = snd_mixart_trigger,
891 .pointer = snd_mixart_stream_pointer,
894 static snd_pcm_ops_t snd_mixart_capture_ops = {
895 .open = snd_mixart_capture_open,
896 .close = snd_mixart_close,
897 .ioctl = snd_pcm_lib_ioctl,
898 .prepare = snd_mixart_prepare,
899 .hw_params = snd_mixart_hw_params,
900 .hw_free = snd_mixart_hw_free,
901 .trigger = snd_mixart_trigger,
902 .pointer = snd_mixart_stream_pointer,
905 static void preallocate_buffers(mixart_t *chip, snd_pcm_t *pcm)
908 snd_pcm_substream_t *subs;
911 for (stream = 0; stream < 2; stream++) {
913 for (subs = pcm->streams[stream].substream; subs; subs = subs->next, idx++)
914 /* set up the unique device id with the chip index */
915 subs->dma_device.id = subs->pcm->device << 16 |
916 subs->stream << 8 | (subs->number + 1) |
917 (chip->chip_idx + 1) << 24;
920 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
921 snd_dma_pci_data(chip->mgr->pci), 32*1024, 32*1024);
926 static int snd_mixart_pcm_analog(mixart_t *chip)
932 sprintf(name, "miXart analog %d", chip->chip_idx);
933 if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_ANALOG,
934 MIXART_PLAYBACK_STREAMS,
935 MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
936 snd_printk(KERN_ERR "cannot create the analog pcm %d\n", chip->chip_idx);
940 pcm->private_data = chip;
942 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
943 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
946 strcpy(pcm->name, name);
948 preallocate_buffers(chip, pcm);
957 static int snd_mixart_pcm_digital(mixart_t *chip)
963 sprintf(name, "miXart AES/EBU %d", chip->chip_idx);
964 if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_DIGITAL,
965 MIXART_PLAYBACK_STREAMS,
966 MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
967 snd_printk(KERN_ERR "cannot create the digital 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 strcpy(pcm->name, name);
979 preallocate_buffers(chip, pcm);
985 static int snd_mixart_chip_free(mixart_t *chip)
991 static int snd_mixart_chip_dev_free(snd_device_t *device)
993 mixart_t *chip = device->device_data;
994 return snd_mixart_chip_free(chip);
1000 static int __devinit snd_mixart_create(mixart_mgr_t *mgr, snd_card_t *card, int idx)
1004 static snd_device_ops_t ops = {
1005 .dev_free = snd_mixart_chip_dev_free,
1008 mgr->chip[idx] = chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1010 snd_printk(KERN_ERR "cannot allocate chip\n");
1015 chip->chip_idx = idx;
1018 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1019 snd_mixart_chip_free(chip);
1024 /* create a DSP loader only on first cardX*/
1025 err = snd_mixart_hwdep_new(mgr);
1030 snd_card_set_dev(card, &mgr->pci->dev);
1035 int snd_mixart_create_pcm(mixart_t* chip)
1039 err = snd_mixart_pcm_analog(chip);
1043 if(chip->mgr->board_type == MIXART_DAUGHTER_TYPE_AES) {
1045 err = snd_mixart_pcm_digital(chip);
1054 * release all the cards assigned to a manager instance
1056 static int snd_mixart_free(mixart_mgr_t *mgr)
1060 for (i = 0; i < mgr->num_cards; i++) {
1062 snd_card_free(mgr->chip[i]->card);
1066 snd_mixart_exit_mailbox(mgr);
1070 free_irq(mgr->irq, (void *)mgr);
1072 /* reset board if some firmware was loaded */
1073 if(mgr->hwdep->dsp_loaded) {
1074 snd_mixart_reset_board(mgr);
1075 snd_printdd("reset miXart !\n");
1078 /* release the i/o ports */
1079 for (i = 0; i < 2; i++) {
1080 if (mgr->mem[i].virt)
1081 iounmap((void *)mgr->mem[i].virt);
1083 pci_release_regions(mgr->pci);
1085 /* free flowarray */
1086 if(mgr->flowinfo.area) {
1087 snd_dma_free_pages(&mgr->flowinfo);
1088 mgr->flowinfo.area = NULL;
1090 /* free bufferarray */
1091 if(mgr->bufferinfo.area) {
1092 snd_dma_free_pages(&mgr->bufferinfo);
1093 mgr->bufferinfo.area = NULL;
1103 static long long snd_mixart_BA0_llseek(snd_info_entry_t *entry,
1104 void *private_file_data,
1109 offset = offset & ~3; /* 4 bytes aligned */
1112 case 0: /* SEEK_SET */
1113 file->f_pos = offset;
1115 case 1: /* SEEK_CUR */
1116 file->f_pos += offset;
1118 case 2: /* SEEK_END, offset is negative */
1119 file->f_pos = MIXART_BA0_SIZE + offset;
1124 if(file->f_pos > MIXART_BA0_SIZE)
1125 file->f_pos = MIXART_BA0_SIZE;
1129 static long long snd_mixart_BA1_llseek(snd_info_entry_t *entry,
1130 void *private_file_data,
1135 offset = offset & ~3; /* 4 bytes aligned */
1138 case 0: /* SEEK_SET */
1139 file->f_pos = offset;
1141 case 1: /* SEEK_CUR */
1142 file->f_pos += offset;
1144 case 2: /* SEEK_END, offset is negative */
1145 file->f_pos = MIXART_BA1_SIZE + offset;
1150 if(file->f_pos > MIXART_BA1_SIZE)
1151 file->f_pos = MIXART_BA1_SIZE;
1156 mixart_BA0 proc interface for BAR 0 - read callback
1158 static long snd_mixart_BA0_read(snd_info_entry_t *entry, void *file_private_data,
1159 struct file *file, char __user *buf,
1160 unsigned long count, unsigned long pos)
1162 mixart_mgr_t *mgr = entry->private_data;
1164 count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1167 if(pos + count > MIXART_BA0_SIZE)
1168 count = (long)(MIXART_BA0_SIZE - pos);
1169 if(copy_to_user_fromio(buf, MIXART_MEM( mgr, pos ), count))
1175 mixart_BA1 proc interface for BAR 1 - read callback
1177 static long snd_mixart_BA1_read(snd_info_entry_t *entry, void *file_private_data,
1178 struct file *file, char __user *buf,
1179 unsigned long count, unsigned long pos)
1181 mixart_mgr_t *mgr = entry->private_data;
1183 count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1186 if(pos + count > MIXART_BA1_SIZE)
1187 count = (long)(MIXART_BA1_SIZE - pos);
1188 if(copy_to_user_fromio(buf, MIXART_REG( mgr, pos ), count))
1193 static struct snd_info_entry_ops snd_mixart_proc_ops_BA0 = {
1194 .read = snd_mixart_BA0_read,
1195 .llseek = snd_mixart_BA0_llseek
1198 static struct snd_info_entry_ops snd_mixart_proc_ops_BA1 = {
1199 .read = snd_mixart_BA1_read,
1200 .llseek = snd_mixart_BA1_llseek
1204 static void snd_mixart_proc_read(snd_info_entry_t *entry,
1205 snd_info_buffer_t * buffer)
1207 mixart_t *chip = entry->private_data;
1210 snd_iprintf(buffer, "Digigram miXart (alsa card %d)\n\n", chip->chip_idx);
1212 /* stats available when embedded OS is running */
1213 if (chip->mgr->hwdep->dsp_loaded & ( 1 << MIXART_MOTHERBOARD_ELF_INDEX)) {
1214 snd_iprintf(buffer, "- hardware -\n");
1215 switch (chip->mgr->board_type ) {
1216 case MIXART_DAUGHTER_TYPE_NONE : snd_iprintf(buffer, "\tmiXart8 (no daughter board)\n\n"); break;
1217 case MIXART_DAUGHTER_TYPE_AES : snd_iprintf(buffer, "\tmiXart8 AES/EBU\n\n"); break;
1218 case MIXART_DAUGHTER_TYPE_COBRANET : snd_iprintf(buffer, "\tmiXart8 Cobranet\n\n"); break;
1219 default: snd_iprintf(buffer, "\tUNKNOWN!\n\n"); break;
1222 snd_iprintf(buffer, "- system load -\n");
1224 /* get perf reference */
1226 ref = readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_SYSTEM_LOAD_OFFSET));
1229 u32 mailbox = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_MAILBX_LOAD_OFFSET)) / ref;
1230 u32 streaming = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_STREAM_LOAD_OFFSET)) / ref;
1231 u32 interr = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_INTERR_LOAD_OFFSET)) / ref;
1233 snd_iprintf(buffer, "\tstreaming : %d\n", streaming);
1234 snd_iprintf(buffer, "\tmailbox : %d\n", mailbox);
1235 snd_iprintf(buffer, "\tinterrups handling : %d\n\n", interr);
1237 } /* endif elf loaded */
1240 static void __devinit snd_mixart_proc_init(mixart_t *chip)
1242 snd_info_entry_t *entry;
1244 /* text interface to read perf and temp meters */
1245 if (! snd_card_proc_new(chip->card, "board_info", &entry)) {
1246 entry->private_data = chip;
1247 entry->c.text.read_size = 1024;
1248 entry->c.text.read = snd_mixart_proc_read;
1251 if (! snd_card_proc_new(chip->card, "mixart_BA0", &entry)) {
1252 entry->content = SNDRV_INFO_CONTENT_DATA;
1253 entry->private_data = chip->mgr;
1254 entry->c.ops = &snd_mixart_proc_ops_BA0;
1255 entry->size = MIXART_BA0_SIZE;
1257 if (! snd_card_proc_new(chip->card, "mixart_BA1", &entry)) {
1258 entry->content = SNDRV_INFO_CONTENT_DATA;
1259 entry->private_data = chip->mgr;
1260 entry->c.ops = &snd_mixart_proc_ops_BA1;
1261 entry->size = MIXART_BA1_SIZE;
1264 /* end of proc interface */
1268 * probe function - creates the card manager
1270 static int __devinit snd_mixart_probe(struct pci_dev *pci,
1271 const struct pci_device_id *pci_id)
1281 if (dev >= SNDRV_CARDS)
1283 if (! enable[dev]) {
1288 /* enable PCI device */
1289 if ((err = pci_enable_device(pci)) < 0)
1291 pci_set_master(pci);
1293 /* check if we can restrict PCI DMA transfers to 32 bits */
1294 if (pci_set_dma_mask(pci, 0xffffffff) < 0) {
1295 snd_printk(KERN_ERR "architecture does not support 32bit PCI busmaster DMA\n");
1301 mgr = kcalloc(1, sizeof(*mgr), GFP_KERNEL);
1308 /* resource assignment */
1309 if ((err = pci_request_regions(pci, CARD_NAME)) < 0) {
1313 for (i = 0; i < 2; i++) {
1314 mgr->mem[i].phys = pci_resource_start(pci, i);
1315 mgr->mem[i].virt = (unsigned long)ioremap_nocache(mgr->mem[i].phys,
1316 pci_resource_len(pci, i));
1319 if (request_irq(pci->irq, snd_mixart_interrupt, SA_INTERRUPT|SA_SHIRQ, CARD_NAME, (void *)mgr)) {
1320 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1321 snd_mixart_free(mgr);
1324 mgr->irq = pci->irq;
1326 sprintf(mgr->shortname, "Digigram miXart");
1327 sprintf(mgr->longname, "%s at 0x%lx & 0x%lx, irq %i", mgr->shortname, mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq);
1330 spin_lock_init(&mgr->lock);
1333 mgr->msg_fifo_readptr = 0;
1334 mgr->msg_fifo_writeptr = 0;
1336 spin_lock_init(&mgr->msg_lock);
1337 init_MUTEX(&mgr->msg_mutex);
1338 init_waitqueue_head(&mgr->msg_sleep);
1339 atomic_set(&mgr->msg_processed, 0);
1341 /* init setup mutex*/
1342 init_MUTEX(&mgr->setup_mutex);
1344 /* init message taslket */
1345 tasklet_init( &mgr->msg_taskq, snd_mixart_msg_tasklet, (unsigned long) mgr);
1347 /* card assignment */
1348 mgr->num_cards = MIXART_MAX_CARDS; /* 4 FIXME: configurable? */
1349 for (i = 0; i < mgr->num_cards; i++) {
1357 idx = index[dev] + i;
1358 snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev], i);
1359 card = snd_card_new(idx, tmpid, THIS_MODULE, 0);
1362 snd_printk(KERN_ERR "cannot allocate the card %d\n", i);
1363 snd_mixart_free(mgr);
1367 strcpy(card->driver, CARD_NAME);
1368 sprintf(card->shortname, "%s [PCM #%d]", mgr->shortname, i);
1369 sprintf(card->longname, "%s [PCM #%d]", mgr->longname, i);
1371 if ((err = snd_mixart_create(mgr, card, i)) < 0) {
1372 snd_mixart_free(mgr);
1377 /* init proc interface only for chip0 */
1378 snd_mixart_proc_init(mgr->chip[i]);
1381 if ((err = snd_card_register(card)) < 0) {
1382 snd_mixart_free(mgr);
1387 /* init firmware status (mgr->hwdep->dsp_loaded reset in hwdep_new) */
1388 mgr->board_type = MIXART_DAUGHTER_TYPE_NONE;
1390 /* create array of streaminfo */
1391 size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS * sizeof(mixart_flowinfo_t)) );
1392 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1393 size, &mgr->flowinfo) < 0) {
1394 snd_mixart_free(mgr);
1397 /* init streaminfo_array */
1398 memset(mgr->flowinfo.area, 0, size);
1400 /* create array of bufferinfo */
1401 size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS * sizeof(mixart_bufferinfo_t)) );
1402 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1403 size, &mgr->bufferinfo) < 0) {
1404 snd_mixart_free(mgr);
1407 /* init bufferinfo_array */
1408 memset(mgr->bufferinfo.area, 0, size);
1410 pci_set_drvdata(pci, mgr);
1415 static void __devexit snd_mixart_remove(struct pci_dev *pci)
1417 snd_mixart_free(pci_get_drvdata(pci));
1418 pci_set_drvdata(pci, NULL);
1421 static struct pci_driver driver = {
1422 .name = "Digigram miXart",
1423 .id_table = snd_mixart_ids,
1424 .probe = snd_mixart_probe,
1425 .remove = __devexit_p(snd_mixart_remove),
1428 static int __init alsa_card_mixart_init(void)
1430 return pci_module_init(&driver);
1433 static void __exit alsa_card_mixart_exit(void)
1435 pci_unregister_driver(&driver);
1438 module_init(alsa_card_mixart_init)
1439 module_exit(alsa_card_mixart_exit)