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 */
51 module_param_array(index, int, NULL, 0444);
52 MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
53 module_param_array(id, charp, NULL, 0444);
54 MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
55 module_param_array(enable, bool, NULL, 0444);
56 MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
61 static struct pci_device_id snd_mixart_ids[] = {
62 { 0x1057, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* MC8240 */
66 MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
69 static int mixart_set_pipe_state(mixart_mgr_t *mgr, mixart_pipe_t* pipe, int start)
71 mixart_group_state_req_t group_state;
72 mixart_group_state_resp_t group_state_resp;
77 switch(pipe->status) {
80 if(start) return 0; /* already started */
83 if(!start) return 0; /* already stopped */
86 snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");
87 return -EINVAL; /* function called with wrong pipe status */
90 system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
92 /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
94 request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
95 request.uid = (mixart_uid_t){0,0};
96 request.data = &system_msg_uid;
97 request.size = sizeof(system_msg_uid);
99 err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
101 snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
105 /* start or stop the pipe (1 pipe) */
107 memset(&group_state, 0, sizeof(group_state));
108 group_state.pipe_count = 1;
109 group_state.pipe_uid[0] = pipe->group_uid;
112 request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
114 request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
116 request.uid = pipe->group_uid; /*(mixart_uid_t){0,0};*/
117 request.data = &group_state;
118 request.size = sizeof(group_state);
120 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
121 if (err < 0 || group_state_resp.txx_status != 0) {
122 snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
129 group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
131 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
132 if (err < 0 || group_state_resp.txx_status != 0) {
133 snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
137 /* in case of start send a synchro top */
139 request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
140 request.uid = (mixart_uid_t){0,0};
144 err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
145 if (err < 0 || stat != 0) {
146 snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);
150 pipe->status = PIPE_RUNNING;
153 pipe->status = PIPE_STOPPED;
159 static int mixart_set_clock(mixart_mgr_t *mgr, mixart_pipe_t *pipe, unsigned int rate)
161 mixart_msg_t request;
162 mixart_clock_properties_t clock_properties;
163 mixart_clock_properties_resp_t clock_prop_resp;
166 switch(pipe->status) {
174 return 0; /* nothing to do */
176 snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);
181 memset(&clock_properties, 0, sizeof(clock_properties));
182 clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
183 clock_properties.clock_mode = CM_STANDALONE;
184 clock_properties.frequency = rate;
185 clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
186 clock_properties.uid_caller[0] = pipe->group_uid;
188 snd_printdd("mixart_set_clock to %d kHz\n", rate);
190 request.message_id = MSG_CLOCK_SET_PROPERTIES;
191 request.uid = mgr->uid_console_manager;
192 request.data = &clock_properties;
193 request.size = sizeof(clock_properties);
195 err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
196 if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
197 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);
201 if(rate) pipe->status = PIPE_CLOCK_SET;
202 else pipe->status = PIPE_RUNNING;
209 * Allocate or reference output pipe for analog IOs (pcmp0/1)
211 mixart_pipe_t* snd_mixart_add_ref_pipe( mixart_t *chip, int pcm_number, int capture, int monitoring)
215 mixart_msg_t request;
218 if (pcm_number == MIXART_PCM_ANALOG) {
219 pipe = &(chip->pipe_in_ana); /* analog inputs */
221 pipe = &(chip->pipe_in_dig); /* digital inputs */
223 request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
224 stream_count = MIXART_CAPTURE_STREAMS;
226 if (pcm_number == MIXART_PCM_ANALOG) {
227 pipe = &(chip->pipe_out_ana); /* analog outputs */
229 pipe = &(chip->pipe_out_dig); /* digital outputs */
231 request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
232 stream_count = MIXART_PLAYBACK_STREAMS;
235 /* a new stream is opened and there are already all streams in use */
236 if( (monitoring == 0) && (pipe->references >= stream_count) ) {
240 /* pipe is not yet defined */
241 if( pipe->status == PIPE_UNDEFINED ) {
244 mixart_streaming_group_req_t sgroup_req;
245 mixart_streaming_group_t sgroup_resp;
248 snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);
250 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
254 request.uid = (mixart_uid_t){0,0}; /* should be StreamManagerUID, but zero is OK if there is only one ! */
255 request.data = &buf->sgroup_req;
256 request.size = sizeof(buf->sgroup_req);
258 memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));
260 buf->sgroup_req.stream_count = stream_count;
261 buf->sgroup_req.channel_count = 2;
262 buf->sgroup_req.latency = 256;
263 buf->sgroup_req.connector = pipe->uid_left_connector; /* the left connector */
265 for (i=0; i<stream_count; i++) {
267 struct mixart_flowinfo *flowinfo;
268 struct mixart_bufferinfo *bufferinfo;
270 /* we don't yet know the format, so config 16 bit pcm audio for instance */
271 buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;
272 buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;
273 buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
275 /* find the right bufferinfo_array */
276 j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;
277 if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */
279 buf->sgroup_req.flow_entry[i] = j;
281 flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;
282 flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(mixart_bufferinfo_t));
283 flowinfo[j].bufferinfo_count = 1; /* 1 will set the miXart to ring-buffer mode ! */
285 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
286 bufferinfo[j].buffer_address = 0; /* buffer is not yet allocated */
287 bufferinfo[j].available_length = 0; /* buffer is not yet allocated */
289 /* construct the identifier of the stream buffer received in the interrupts ! */
290 bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;
292 bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
296 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);
297 if((err < 0) || (buf->sgroup_resp.status != 0)) {
298 snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, buf->sgroup_resp.status);
303 pipe->group_uid = buf->sgroup_resp.group; /* id of the pipe, as returned by embedded */
304 pipe->stream_count = buf->sgroup_resp.stream_count;
305 /* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */
307 pipe->status = PIPE_STOPPED;
311 if(monitoring) pipe->monitoring = 1;
312 else pipe->references++;
318 int snd_mixart_kill_ref_pipe( mixart_mgr_t *mgr, mixart_pipe_t *pipe, int monitoring)
322 if(pipe->status == PIPE_UNDEFINED)
326 pipe->monitoring = 0;
330 if((pipe->references <= 0) && (pipe->monitoring == 0)) {
332 mixart_msg_t request;
333 mixart_delete_group_resp_t delete_resp;
335 /* release the clock */
336 err = mixart_set_clock( mgr, pipe, 0);
338 snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");
342 err = mixart_set_pipe_state(mgr, pipe, 0);
344 snd_printk(KERN_ERR "error stopping pipe!\n");
347 request.message_id = MSG_STREAM_DELETE_GROUP;
348 request.uid = (mixart_uid_t){0,0};
349 request.data = &pipe->group_uid; /* the streaming group ! */
350 request.size = sizeof(pipe->group_uid);
352 /* delete the pipe */
353 err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
354 if ((err < 0) || (delete_resp.status != 0)) {
355 snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);
358 pipe->group_uid = (mixart_uid_t){0,0};
359 pipe->stream_count = 0;
360 pipe->status = PIPE_UNDEFINED;
366 static int mixart_set_stream_state(mixart_stream_t *stream, int start)
369 mixart_stream_state_req_t stream_state_req;
370 mixart_msg_t request;
372 if(!stream->substream)
375 memset(&stream_state_req, 0, sizeof(stream_state_req));
376 stream_state_req.stream_count = 1;
377 stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
378 stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
380 if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
381 request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
383 request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
385 request.uid = (mixart_uid_t){0,0};
386 request.data = &stream_state_req;
387 request.size = sizeof(stream_state_req);
389 stream->abs_period_elapsed = 0; /* reset stream pos */
390 stream->buf_periods = 0;
391 stream->buf_period_frag = 0;
393 chip = snd_pcm_substream_chip(stream->substream);
395 return snd_mixart_send_msg_nonblock(chip->mgr, &request);
402 static int snd_mixart_trigger(snd_pcm_substream_t *subs, int cmd)
404 mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
407 case SNDRV_PCM_TRIGGER_START:
409 snd_printdd("SNDRV_PCM_TRIGGER_START\n");
412 if( mixart_set_stream_state(stream, 1) )
415 stream->status = MIXART_STREAM_STATUS_RUNNING;
418 case SNDRV_PCM_TRIGGER_STOP:
421 if( mixart_set_stream_state(stream, 0) )
424 stream->status = MIXART_STREAM_STATUS_OPEN;
426 snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
430 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
432 stream->status = MIXART_STREAM_STATUS_PAUSE;
433 snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");
435 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
437 stream->status = MIXART_STREAM_STATUS_RUNNING;
438 snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");
446 static int mixart_sync_nonblock_events(mixart_mgr_t *mgr)
448 unsigned long timeout = jiffies + HZ;
449 while (atomic_read(&mgr->msg_processed) > 0) {
450 if (time_after(jiffies, timeout)) {
451 snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");
454 set_current_state(TASK_UNINTERRUPTIBLE);
461 * prepare callback for all pcms
463 static int snd_mixart_prepare(snd_pcm_substream_t *subs)
465 mixart_t *chip = snd_pcm_substream_chip(subs);
466 mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
468 /* TODO de façon non bloquante, réappliquer les hw_params (rate, bits, codec) */
470 snd_printdd("snd_mixart_prepare\n");
472 mixart_sync_nonblock_events(chip->mgr);
474 /* only the first stream can choose the sample rate */
475 /* the further opened streams will be limited to its frequency (see open) */
476 if(chip->mgr->ref_count_rate == 1)
477 chip->mgr->sample_rate = subs->runtime->rate;
479 /* set the clock only once (first stream) on the same pipe */
480 if(stream->pipe->references == 1) {
481 if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) )
489 static int mixart_set_format(mixart_stream_t *stream, snd_pcm_format_t format)
493 mixart_msg_t request;
494 mixart_stream_param_desc_t stream_param;
495 mixart_return_uid_t resp;
497 chip = snd_pcm_substream_chip(stream->substream);
499 memset(&stream_param, 0, sizeof(stream_param));
501 stream_param.coding_type = CT_LINEAR;
502 stream_param.number_of_channel = stream->channels;
504 stream_param.sampling_freq = chip->mgr->sample_rate;
505 if(stream_param.sampling_freq == 0)
506 stream_param.sampling_freq = 44100; /* if frequency not yet defined, use some default */
509 case SNDRV_PCM_FORMAT_U8:
510 stream_param.sample_type = ST_INTEGER_8;
511 stream_param.sample_size = 8;
513 case SNDRV_PCM_FORMAT_S16_LE:
514 stream_param.sample_type = ST_INTEGER_16LE;
515 stream_param.sample_size = 16;
517 case SNDRV_PCM_FORMAT_S16_BE:
518 stream_param.sample_type = ST_INTEGER_16BE;
519 stream_param.sample_size = 16;
521 case SNDRV_PCM_FORMAT_S24_3LE:
522 stream_param.sample_type = ST_INTEGER_24LE;
523 stream_param.sample_size = 24;
525 case SNDRV_PCM_FORMAT_S24_3BE:
526 stream_param.sample_type = ST_INTEGER_24BE;
527 stream_param.sample_size = 24;
529 case SNDRV_PCM_FORMAT_FLOAT_LE:
530 stream_param.sample_type = ST_FLOATING_POINT_32LE;
531 stream_param.sample_size = 32;
533 case SNDRV_PCM_FORMAT_FLOAT_BE:
534 stream_param.sample_type = ST_FLOATING_POINT_32BE;
535 stream_param.sample_size = 32;
538 snd_printk(KERN_ERR "error mixart_set_format() : unknown format\n");
542 snd_printdd("set SNDRV_PCM_FORMAT sample_type(%d) sample_size(%d) freq(%d) channels(%d)\n",
543 stream_param.sample_type, stream_param.sample_size, stream_param.sampling_freq, stream->channels);
545 /* TODO: what else to configure ? */
546 /* stream_param.samples_per_frame = 2; */
547 /* stream_param.bytes_per_frame = 4; */
548 /* stream_param.bytes_per_sample = 2; */
550 stream_param.pipe_count = 1; /* set to 1 */
551 stream_param.stream_count = 1; /* set to 1 */
552 stream_param.stream_desc[0].uid_pipe = stream->pipe->group_uid;
553 stream_param.stream_desc[0].stream_idx = stream->substream->number;
555 request.message_id = MSG_STREAM_SET_INPUT_STAGE_PARAM;
556 request.uid = (mixart_uid_t){0,0};
557 request.data = &stream_param;
558 request.size = sizeof(stream_param);
560 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(resp), &resp);
561 if((err < 0) || resp.error_code) {
562 snd_printk(KERN_ERR "MSG_STREAM_SET_INPUT_STAGE_PARAM err=%x; resp=%x\n", err, resp.error_code);
570 * HW_PARAMS callback for all pcms
572 static int snd_mixart_hw_params(snd_pcm_substream_t *subs,
573 snd_pcm_hw_params_t *hw)
575 mixart_t *chip = snd_pcm_substream_chip(subs);
576 mixart_mgr_t *mgr = chip->mgr;
577 mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
578 snd_pcm_format_t format;
582 /* set up channels */
583 channels = params_channels(hw);
585 /* set up format for the stream */
586 format = params_format(hw);
588 down(&mgr->setup_mutex);
590 /* update the stream levels */
591 if( stream->pcm_number <= MIXART_PCM_DIGITAL ) {
592 int is_aes = stream->pcm_number > MIXART_PCM_ANALOG;
593 if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK )
594 mixart_update_playback_stream_level(chip, is_aes, subs->number);
596 mixart_update_capture_stream_level( chip, is_aes);
599 stream->channels = channels;
601 /* set the format to the board */
602 err = mixart_set_format(stream, format);
607 /* allocate buffer */
608 err = snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw));
611 struct mixart_bufferinfo *bufferinfo;
612 int i = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (stream->pcm_number * (MIXART_PLAYBACK_STREAMS+MIXART_CAPTURE_STREAMS)) + subs->number;
613 if( subs->stream == SNDRV_PCM_STREAM_CAPTURE ) {
614 i += MIXART_PLAYBACK_STREAMS; /* in array capture is behind playback */
617 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
618 bufferinfo[i].buffer_address = subs->runtime->dma_addr;
619 bufferinfo[i].available_length = subs->runtime->dma_bytes;
620 /* bufferinfo[i].buffer_id is already defined */
622 snd_printdd("snd_mixart_hw_params(pcm %d) : dma_addr(%x) dma_bytes(%x) subs-number(%d)\n", i,
623 bufferinfo[i].buffer_address,
624 bufferinfo[i].available_length,
627 up(&mgr->setup_mutex);
632 static int snd_mixart_hw_free(snd_pcm_substream_t *subs)
634 mixart_t *chip = snd_pcm_substream_chip(subs);
635 snd_pcm_lib_free_pages(subs);
636 mixart_sync_nonblock_events(chip->mgr);
643 * TODO CONFIGURATION SPACE for all pcms, mono pcm must update channels_max
645 static snd_pcm_hardware_t snd_mixart_analog_caps =
647 .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
648 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
649 SNDRV_PCM_INFO_PAUSE),
650 .formats = ( SNDRV_PCM_FMTBIT_U8 |
651 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
652 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
653 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
654 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
659 .buffer_bytes_max = (32*1024),
660 .period_bytes_min = 256, /* 256 frames U8 mono*/
661 .period_bytes_max = (16*1024),
663 .periods_max = (32*1024/256),
666 static snd_pcm_hardware_t snd_mixart_digital_caps =
668 .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
669 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
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_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_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),
688 static int snd_mixart_playback_open(snd_pcm_substream_t *subs)
690 mixart_t *chip = snd_pcm_substream_chip(subs);
691 mixart_mgr_t *mgr = chip->mgr;
692 snd_pcm_runtime_t *runtime = subs->runtime;
693 snd_pcm_t *pcm = subs->pcm;
694 mixart_stream_t *stream;
699 down(&mgr->setup_mutex);
701 if ( pcm == chip->pcm ) {
702 pcm_number = MIXART_PCM_ANALOG;
703 runtime->hw = snd_mixart_analog_caps;
705 snd_assert ( pcm == chip->pcm_dig );
706 pcm_number = MIXART_PCM_DIGITAL;
707 runtime->hw = snd_mixart_digital_caps;
709 snd_printdd("snd_mixart_playback_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
711 /* get stream info */
712 stream = &(chip->playback_stream[pcm_number][subs->number]);
714 if (stream->status != MIXART_STREAM_STATUS_FREE){
716 snd_printk(KERN_ERR "snd_mixart_playback_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
721 /* get pipe pointer (out pipe) */
722 pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 0, 0);
729 /* start the pipe if necessary */
730 err = mixart_set_pipe_state(chip->mgr, pipe, 1);
732 snd_printk(KERN_ERR "error starting pipe!\n");
733 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
739 stream->pcm_number = pcm_number;
740 stream->status = MIXART_STREAM_STATUS_OPEN;
741 stream->substream = subs;
742 stream->channels = 0; /* not configured yet */
744 runtime->private_data = stream;
746 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
747 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
749 /* if a sample rate is already used, another stream cannot change */
750 if(mgr->ref_count_rate++) {
751 if(mgr->sample_rate) {
752 runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
757 up(&mgr->setup_mutex);
763 static int snd_mixart_capture_open(snd_pcm_substream_t *subs)
765 mixart_t *chip = snd_pcm_substream_chip(subs);
766 mixart_mgr_t *mgr = chip->mgr;
767 snd_pcm_runtime_t *runtime = subs->runtime;
768 snd_pcm_t *pcm = subs->pcm;
769 mixart_stream_t *stream;
774 down(&mgr->setup_mutex);
776 if ( pcm == chip->pcm ) {
777 pcm_number = MIXART_PCM_ANALOG;
778 runtime->hw = snd_mixart_analog_caps;
780 snd_assert ( pcm == chip->pcm_dig );
781 pcm_number = MIXART_PCM_DIGITAL;
782 runtime->hw = snd_mixart_digital_caps;
785 runtime->hw.channels_min = 2; /* for instance, no mono */
787 snd_printdd("snd_mixart_capture_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
789 /* get stream info */
790 stream = &(chip->capture_stream[pcm_number]);
792 if (stream->status != MIXART_STREAM_STATUS_FREE){
794 snd_printk(KERN_ERR "snd_mixart_capture_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
799 /* get pipe pointer (in pipe) */
800 pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 1, 0);
807 /* start the pipe if necessary */
808 err = mixart_set_pipe_state(chip->mgr, pipe, 1);
810 snd_printk(KERN_ERR "error starting pipe!\n");
811 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
817 stream->pcm_number = pcm_number;
818 stream->status = MIXART_STREAM_STATUS_OPEN;
819 stream->substream = subs;
820 stream->channels = 0; /* not configured yet */
822 runtime->private_data = stream;
824 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
825 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
827 /* if a sample rate is already used, another stream cannot change */
828 if(mgr->ref_count_rate++) {
829 if(mgr->sample_rate) {
830 runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
835 up(&mgr->setup_mutex);
842 static int snd_mixart_close(snd_pcm_substream_t *subs)
844 mixart_t *chip = snd_pcm_substream_chip(subs);
845 mixart_mgr_t *mgr = chip->mgr;
846 mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
848 down(&mgr->setup_mutex);
850 snd_printdd("snd_mixart_close C%d/P%d/Sub%d\n", chip->chip_idx, stream->pcm_number, subs->number);
852 /* sample rate released */
853 if(--mgr->ref_count_rate == 0) {
854 mgr->sample_rate = 0;
858 if (snd_mixart_kill_ref_pipe(mgr, stream->pipe, 0 ) < 0) {
860 snd_printk(KERN_ERR "error snd_mixart_kill_ref_pipe C%dP%d\n", chip->chip_idx, stream->pcm_number);
864 stream->status = MIXART_STREAM_STATUS_FREE;
865 stream->substream = NULL;
867 up(&mgr->setup_mutex);
872 static snd_pcm_uframes_t snd_mixart_stream_pointer(snd_pcm_substream_t * subs)
874 snd_pcm_runtime_t *runtime = subs->runtime;
875 mixart_stream_t *stream = (mixart_stream_t*)runtime->private_data;
877 return (snd_pcm_uframes_t)((stream->buf_periods * runtime->period_size) + stream->buf_period_frag);
882 static snd_pcm_ops_t snd_mixart_playback_ops = {
883 .open = snd_mixart_playback_open,
884 .close = snd_mixart_close,
885 .ioctl = snd_pcm_lib_ioctl,
886 .prepare = snd_mixart_prepare,
887 .hw_params = snd_mixart_hw_params,
888 .hw_free = snd_mixart_hw_free,
889 .trigger = snd_mixart_trigger,
890 .pointer = snd_mixart_stream_pointer,
893 static snd_pcm_ops_t snd_mixart_capture_ops = {
894 .open = snd_mixart_capture_open,
895 .close = snd_mixart_close,
896 .ioctl = snd_pcm_lib_ioctl,
897 .prepare = snd_mixart_prepare,
898 .hw_params = snd_mixart_hw_params,
899 .hw_free = snd_mixart_hw_free,
900 .trigger = snd_mixart_trigger,
901 .pointer = snd_mixart_stream_pointer,
904 static void preallocate_buffers(mixart_t *chip, snd_pcm_t *pcm)
907 snd_pcm_substream_t *subs;
910 for (stream = 0; stream < 2; stream++) {
912 for (subs = pcm->streams[stream].substream; subs; subs = subs->next, idx++)
913 /* set up the unique device id with the chip index */
914 subs->dma_device.id = subs->pcm->device << 16 |
915 subs->stream << 8 | (subs->number + 1) |
916 (chip->chip_idx + 1) << 24;
919 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
920 snd_dma_pci_data(chip->mgr->pci), 32*1024, 32*1024);
925 static int snd_mixart_pcm_analog(mixart_t *chip)
931 sprintf(name, "miXart analog %d", chip->chip_idx);
932 if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_ANALOG,
933 MIXART_PLAYBACK_STREAMS,
934 MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
935 snd_printk(KERN_ERR "cannot create the analog pcm %d\n", chip->chip_idx);
939 pcm->private_data = chip;
941 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
942 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
945 strcpy(pcm->name, name);
947 preallocate_buffers(chip, pcm);
956 static int snd_mixart_pcm_digital(mixart_t *chip)
962 sprintf(name, "miXart AES/EBU %d", chip->chip_idx);
963 if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_DIGITAL,
964 MIXART_PLAYBACK_STREAMS,
965 MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
966 snd_printk(KERN_ERR "cannot create the digital pcm %d\n", chip->chip_idx);
970 pcm->private_data = chip;
972 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
973 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
976 strcpy(pcm->name, name);
978 preallocate_buffers(chip, pcm);
984 static int snd_mixart_chip_free(mixart_t *chip)
990 static int snd_mixart_chip_dev_free(snd_device_t *device)
992 mixart_t *chip = device->device_data;
993 return snd_mixart_chip_free(chip);
999 static int __devinit snd_mixart_create(mixart_mgr_t *mgr, snd_card_t *card, int idx)
1003 static snd_device_ops_t ops = {
1004 .dev_free = snd_mixart_chip_dev_free,
1007 mgr->chip[idx] = chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1009 snd_printk(KERN_ERR "cannot allocate chip\n");
1014 chip->chip_idx = idx;
1017 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1018 snd_mixart_chip_free(chip);
1022 snd_card_set_dev(card, &mgr->pci->dev);
1027 int snd_mixart_create_pcm(mixart_t* chip)
1031 err = snd_mixart_pcm_analog(chip);
1035 if(chip->mgr->board_type == MIXART_DAUGHTER_TYPE_AES) {
1037 err = snd_mixart_pcm_digital(chip);
1046 * release all the cards assigned to a manager instance
1048 static int snd_mixart_free(mixart_mgr_t *mgr)
1052 for (i = 0; i < mgr->num_cards; i++) {
1054 snd_card_free(mgr->chip[i]->card);
1058 snd_mixart_exit_mailbox(mgr);
1062 free_irq(mgr->irq, (void *)mgr);
1064 /* reset board if some firmware was loaded */
1065 if(mgr->dsp_loaded) {
1066 snd_mixart_reset_board(mgr);
1067 snd_printdd("reset miXart !\n");
1070 /* release the i/o ports */
1071 for (i = 0; i < 2; i++) {
1072 if (mgr->mem[i].virt)
1073 iounmap(mgr->mem[i].virt);
1075 pci_release_regions(mgr->pci);
1077 /* free flowarray */
1078 if(mgr->flowinfo.area) {
1079 snd_dma_free_pages(&mgr->flowinfo);
1080 mgr->flowinfo.area = NULL;
1082 /* free bufferarray */
1083 if(mgr->bufferinfo.area) {
1084 snd_dma_free_pages(&mgr->bufferinfo);
1085 mgr->bufferinfo.area = NULL;
1088 pci_disable_device(mgr->pci);
1096 static long long snd_mixart_BA0_llseek(snd_info_entry_t *entry,
1097 void *private_file_data,
1102 offset = offset & ~3; /* 4 bytes aligned */
1105 case 0: /* SEEK_SET */
1106 file->f_pos = offset;
1108 case 1: /* SEEK_CUR */
1109 file->f_pos += offset;
1111 case 2: /* SEEK_END, offset is negative */
1112 file->f_pos = MIXART_BA0_SIZE + offset;
1117 if(file->f_pos > MIXART_BA0_SIZE)
1118 file->f_pos = MIXART_BA0_SIZE;
1122 static long long snd_mixart_BA1_llseek(snd_info_entry_t *entry,
1123 void *private_file_data,
1128 offset = offset & ~3; /* 4 bytes aligned */
1131 case 0: /* SEEK_SET */
1132 file->f_pos = offset;
1134 case 1: /* SEEK_CUR */
1135 file->f_pos += offset;
1137 case 2: /* SEEK_END, offset is negative */
1138 file->f_pos = MIXART_BA1_SIZE + offset;
1143 if(file->f_pos > MIXART_BA1_SIZE)
1144 file->f_pos = MIXART_BA1_SIZE;
1149 mixart_BA0 proc interface for BAR 0 - read callback
1151 static long snd_mixart_BA0_read(snd_info_entry_t *entry, void *file_private_data,
1152 struct file *file, char __user *buf,
1153 unsigned long count, unsigned long pos)
1155 mixart_mgr_t *mgr = entry->private_data;
1157 count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1160 if(pos + count > MIXART_BA0_SIZE)
1161 count = (long)(MIXART_BA0_SIZE - pos);
1162 if(copy_to_user_fromio(buf, MIXART_MEM( mgr, pos ), count))
1168 mixart_BA1 proc interface for BAR 1 - read callback
1170 static long snd_mixart_BA1_read(snd_info_entry_t *entry, void *file_private_data,
1171 struct file *file, char __user *buf,
1172 unsigned long count, unsigned long pos)
1174 mixart_mgr_t *mgr = entry->private_data;
1176 count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1179 if(pos + count > MIXART_BA1_SIZE)
1180 count = (long)(MIXART_BA1_SIZE - pos);
1181 if(copy_to_user_fromio(buf, MIXART_REG( mgr, pos ), count))
1186 static struct snd_info_entry_ops snd_mixart_proc_ops_BA0 = {
1187 .read = snd_mixart_BA0_read,
1188 .llseek = snd_mixart_BA0_llseek
1191 static struct snd_info_entry_ops snd_mixart_proc_ops_BA1 = {
1192 .read = snd_mixart_BA1_read,
1193 .llseek = snd_mixart_BA1_llseek
1197 static void snd_mixart_proc_read(snd_info_entry_t *entry,
1198 snd_info_buffer_t * buffer)
1200 mixart_t *chip = entry->private_data;
1203 snd_iprintf(buffer, "Digigram miXart (alsa card %d)\n\n", chip->chip_idx);
1205 /* stats available when embedded OS is running */
1206 if (chip->mgr->dsp_loaded & ( 1 << MIXART_MOTHERBOARD_ELF_INDEX)) {
1207 snd_iprintf(buffer, "- hardware -\n");
1208 switch (chip->mgr->board_type ) {
1209 case MIXART_DAUGHTER_TYPE_NONE : snd_iprintf(buffer, "\tmiXart8 (no daughter board)\n\n"); break;
1210 case MIXART_DAUGHTER_TYPE_AES : snd_iprintf(buffer, "\tmiXart8 AES/EBU\n\n"); break;
1211 case MIXART_DAUGHTER_TYPE_COBRANET : snd_iprintf(buffer, "\tmiXart8 Cobranet\n\n"); break;
1212 default: snd_iprintf(buffer, "\tUNKNOWN!\n\n"); break;
1215 snd_iprintf(buffer, "- system load -\n");
1217 /* get perf reference */
1219 ref = readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_SYSTEM_LOAD_OFFSET));
1222 u32 mailbox = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_MAILBX_LOAD_OFFSET)) / ref;
1223 u32 streaming = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_STREAM_LOAD_OFFSET)) / ref;
1224 u32 interr = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_INTERR_LOAD_OFFSET)) / ref;
1226 snd_iprintf(buffer, "\tstreaming : %d\n", streaming);
1227 snd_iprintf(buffer, "\tmailbox : %d\n", mailbox);
1228 snd_iprintf(buffer, "\tinterrups handling : %d\n\n", interr);
1230 } /* endif elf loaded */
1233 static void __devinit snd_mixart_proc_init(mixart_t *chip)
1235 snd_info_entry_t *entry;
1237 /* text interface to read perf and temp meters */
1238 if (! snd_card_proc_new(chip->card, "board_info", &entry)) {
1239 entry->private_data = chip;
1240 entry->c.text.read_size = 1024;
1241 entry->c.text.read = snd_mixart_proc_read;
1244 if (! snd_card_proc_new(chip->card, "mixart_BA0", &entry)) {
1245 entry->content = SNDRV_INFO_CONTENT_DATA;
1246 entry->private_data = chip->mgr;
1247 entry->c.ops = &snd_mixart_proc_ops_BA0;
1248 entry->size = MIXART_BA0_SIZE;
1250 if (! snd_card_proc_new(chip->card, "mixart_BA1", &entry)) {
1251 entry->content = SNDRV_INFO_CONTENT_DATA;
1252 entry->private_data = chip->mgr;
1253 entry->c.ops = &snd_mixart_proc_ops_BA1;
1254 entry->size = MIXART_BA1_SIZE;
1257 /* end of proc interface */
1261 * probe function - creates the card manager
1263 static int __devinit snd_mixart_probe(struct pci_dev *pci,
1264 const struct pci_device_id *pci_id)
1274 if (dev >= SNDRV_CARDS)
1276 if (! enable[dev]) {
1281 /* enable PCI device */
1282 if ((err = pci_enable_device(pci)) < 0)
1284 pci_set_master(pci);
1286 /* check if we can restrict PCI DMA transfers to 32 bits */
1287 if (pci_set_dma_mask(pci, 0xffffffff) < 0) {
1288 snd_printk(KERN_ERR "architecture does not support 32bit PCI busmaster DMA\n");
1289 pci_disable_device(pci);
1295 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
1297 pci_disable_device(pci);
1304 /* resource assignment */
1305 if ((err = pci_request_regions(pci, CARD_NAME)) < 0) {
1307 pci_disable_device(pci);
1310 for (i = 0; i < 2; i++) {
1311 mgr->mem[i].phys = pci_resource_start(pci, i);
1312 mgr->mem[i].virt = ioremap_nocache(mgr->mem[i].phys,
1313 pci_resource_len(pci, i));
1316 if (request_irq(pci->irq, snd_mixart_interrupt, SA_INTERRUPT|SA_SHIRQ, CARD_NAME, (void *)mgr)) {
1317 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1318 snd_mixart_free(mgr);
1321 mgr->irq = pci->irq;
1323 sprintf(mgr->shortname, "Digigram miXart");
1324 sprintf(mgr->longname, "%s at 0x%lx & 0x%lx, irq %i", mgr->shortname, mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq);
1327 spin_lock_init(&mgr->lock);
1330 mgr->msg_fifo_readptr = 0;
1331 mgr->msg_fifo_writeptr = 0;
1333 spin_lock_init(&mgr->msg_lock);
1334 init_MUTEX(&mgr->msg_mutex);
1335 init_waitqueue_head(&mgr->msg_sleep);
1336 atomic_set(&mgr->msg_processed, 0);
1338 /* init setup mutex*/
1339 init_MUTEX(&mgr->setup_mutex);
1341 /* init message taslket */
1342 tasklet_init( &mgr->msg_taskq, snd_mixart_msg_tasklet, (unsigned long) mgr);
1344 /* card assignment */
1345 mgr->num_cards = MIXART_MAX_CARDS; /* 4 FIXME: configurable? */
1346 for (i = 0; i < mgr->num_cards; i++) {
1354 idx = index[dev] + i;
1355 snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : "MIXART", i);
1356 card = snd_card_new(idx, tmpid, THIS_MODULE, 0);
1359 snd_printk(KERN_ERR "cannot allocate the card %d\n", i);
1360 snd_mixart_free(mgr);
1364 strcpy(card->driver, CARD_NAME);
1365 sprintf(card->shortname, "%s [PCM #%d]", mgr->shortname, i);
1366 sprintf(card->longname, "%s [PCM #%d]", mgr->longname, i);
1368 if ((err = snd_mixart_create(mgr, card, i)) < 0) {
1369 snd_mixart_free(mgr);
1374 /* init proc interface only for chip0 */
1375 snd_mixart_proc_init(mgr->chip[i]);
1378 if ((err = snd_card_register(card)) < 0) {
1379 snd_mixart_free(mgr);
1384 /* init firmware status (mgr->dsp_loaded reset in hwdep_new) */
1385 mgr->board_type = MIXART_DAUGHTER_TYPE_NONE;
1387 /* create array of streaminfo */
1388 size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS * sizeof(mixart_flowinfo_t)) );
1389 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1390 size, &mgr->flowinfo) < 0) {
1391 snd_mixart_free(mgr);
1394 /* init streaminfo_array */
1395 memset(mgr->flowinfo.area, 0, size);
1397 /* create array of bufferinfo */
1398 size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS * sizeof(mixart_bufferinfo_t)) );
1399 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1400 size, &mgr->bufferinfo) < 0) {
1401 snd_mixart_free(mgr);
1404 /* init bufferinfo_array */
1405 memset(mgr->bufferinfo.area, 0, size);
1407 /* set up firmware */
1408 err = snd_mixart_setup_firmware(mgr);
1410 snd_mixart_free(mgr);
1414 pci_set_drvdata(pci, mgr);
1419 static void __devexit snd_mixart_remove(struct pci_dev *pci)
1421 snd_mixart_free(pci_get_drvdata(pci));
1422 pci_set_drvdata(pci, NULL);
1425 static struct pci_driver driver = {
1426 .name = "Digigram miXart",
1427 .owner = THIS_MODULE,
1428 .id_table = snd_mixart_ids,
1429 .probe = snd_mixart_probe,
1430 .remove = __devexit_p(snd_mixart_remove),
1433 static int __init alsa_card_mixart_init(void)
1435 return pci_register_driver(&driver);
1438 static void __exit alsa_card_mixart_exit(void)
1440 pci_unregister_driver(&driver);
1443 module_init(alsa_card_mixart_init)
1444 module_exit(alsa_card_mixart_exit)