patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / pci / mixart / mixart.c
1 /*
2  * Driver for Digigram miXart soundcards
3  *
4  * main file with alsa callbacks
5  *
6  * Copyright (c) 2003 by Digigram <alsa@digigram.com>
7  *
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.
12  *
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.
17  *
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
21  */
22
23
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>
35 #include "mixart.h"
36 #include "mixart_hwdep.h"
37 #include "mixart_core.h"
38 #include "mixart_mixer.h"
39
40 #define CARD_NAME "miXart"
41
42 MODULE_AUTHOR("Digigram <alsa@digigram.com>");
43 MODULE_DESCRIPTION("Digigram " CARD_NAME);
44 MODULE_LICENSE("GPL");
45 MODULE_CLASSES("{sound}");
46 MODULE_DEVICES("{{Digigram," CARD_NAME "}}");
47
48 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;             /* Index 0-MAX */
49 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;              /* ID for this card */
50 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;     /* Enable this card */
51 static int boot_devs;
52
53 #define chip_t mixart_t
54
55 module_param_array(index, int, boot_devs, 0444);
56 MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
57 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
58 module_param_array(id, charp, boot_devs, 0444);
59 MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
60 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
61 module_param_array(enable, bool, boot_devs, 0444);
62 MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
63 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
64
65 /*
66  */
67
68 static struct pci_device_id snd_mixart_ids[] = {
69         { 0x1057, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* MC8240 */
70         { 0, }
71 };
72
73 MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
74
75
76 static int mixart_set_pipe_state(mixart_mgr_t *mgr, mixart_pipe_t* pipe, int start)
77 {
78         mixart_group_state_req_t group_state;
79         mixart_group_state_resp_t group_state_resp;
80         mixart_msg_t request;
81         int err;
82         u32 system_msg_uid;
83
84         switch(pipe->status) {
85         case PIPE_RUNNING:
86         case PIPE_CLOCK_SET:
87                 if(start) return 0; /* already started */
88                 break;
89         case PIPE_STOPPED:
90                 if(!start) return 0; /* already stopped */
91                 break;
92         default:
93                 snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");
94                 return -EINVAL;      /* function called with wrong pipe status */
95         }
96
97         system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
98
99         /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
100
101         request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
102         request.uid = (mixart_uid_t){0,0};
103         request.data = &system_msg_uid;
104         request.size = sizeof(system_msg_uid);
105
106         err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
107         if(err) {
108                 snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
109                 return err;
110         }
111
112         /* start or stop the pipe (1 pipe) */
113
114         memset(&group_state, 0, sizeof(group_state));
115         group_state.pipe_count = 1;
116         group_state.pipe_uid[0] = pipe->group_uid;
117
118         if(start)
119                 request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
120         else
121                 request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
122
123         request.uid = pipe->group_uid; /*(mixart_uid_t){0,0};*/
124         request.data = &group_state;
125         request.size = sizeof(group_state);
126
127         err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
128         if (err < 0 || group_state_resp.txx_status != 0) {
129                 snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
130                 return -EINVAL;
131         }
132
133         if(start) {
134                 u32 stat;
135
136                 group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
137
138                 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
139                 if (err < 0 || group_state_resp.txx_status != 0) {
140                         snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
141                         return -EINVAL;
142                 }
143
144                 /* in case of start send a synchro top */
145
146                 request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
147                 request.uid = (mixart_uid_t){0,0};
148                 request.data = NULL;
149                 request.size = 0;
150
151                 err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
152                 if (err < 0 || stat != 0) {
153                         snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);
154                         return -EINVAL;
155                 }
156
157                 pipe->status = PIPE_RUNNING;
158         }
159         else /* !start */
160                 pipe->status = PIPE_STOPPED;
161
162         return 0;
163 }
164
165
166 static int mixart_set_clock(mixart_mgr_t *mgr, mixart_pipe_t *pipe, unsigned int rate)
167 {
168         mixart_msg_t request;
169         mixart_clock_properties_t clock_properties;
170         mixart_clock_properties_resp_t clock_prop_resp;
171         int err;
172
173         switch(pipe->status) {
174         case PIPE_CLOCK_SET:
175                 break;
176         case PIPE_RUNNING:
177                 if(rate != 0)
178                         break;
179         default:
180                 if(rate == 0)
181                         return 0; /* nothing to do */
182                 else {
183                         snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);
184                         return -EINVAL;
185                 }
186         }
187
188         memset(&clock_properties, 0, sizeof(clock_properties));
189         clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
190         clock_properties.clock_mode = CM_STANDALONE;
191         clock_properties.frequency = rate;
192         clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
193         clock_properties.uid_caller[0] = pipe->group_uid;
194
195         snd_printdd("mixart_set_clock to %d kHz\n", rate);
196
197         request.message_id = MSG_CLOCK_SET_PROPERTIES;
198         request.uid = mgr->uid_console_manager;
199         request.data = &clock_properties;
200         request.size = sizeof(clock_properties);
201
202         err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
203         if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
204                 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);
205                 return -EINVAL;
206         }
207
208         if(rate)  pipe->status = PIPE_CLOCK_SET;
209         else      pipe->status = PIPE_RUNNING;
210
211         return 0;
212 }
213
214
215 /*
216  *  Allocate or reference output pipe for analog IOs (pcmp0/1)
217  */
218 mixart_pipe_t* snd_mixart_add_ref_pipe( mixart_t *chip, int pcm_number, int capture, int monitoring)
219 {
220         int stream_count;
221         mixart_pipe_t *pipe;
222         mixart_msg_t request;
223
224         if(capture) {
225                 if (pcm_number == MIXART_PCM_ANALOG) {
226                         pipe = &(chip->pipe_in_ana);  /* analog inputs */
227                 } else {
228                         pipe = &(chip->pipe_in_dig); /* digital inputs */
229                 }
230                 request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
231                 stream_count = MIXART_CAPTURE_STREAMS;
232         } else {
233                 if (pcm_number == MIXART_PCM_ANALOG) {
234                         pipe = &(chip->pipe_out_ana);  /* analog outputs */
235                 } else {
236                         pipe = &(chip->pipe_out_dig);  /* digital outputs */
237                 }
238                 request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
239                 stream_count = MIXART_PLAYBACK_STREAMS;
240         }
241
242         /* a new stream is opened and there are already all streams in use */
243         if( (monitoring == 0) && (pipe->references >= stream_count) ) {
244                 return NULL;
245         }
246
247         /* pipe is not yet defined */
248         if( pipe->status == PIPE_UNDEFINED ) {
249                 int err, i;
250                 mixart_streaming_group_t streaming_group_resp;
251                 mixart_streaming_group_req_t streaming_group_req;
252
253                 snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);
254
255                 request.uid = (mixart_uid_t){0,0};      /* should be StreamManagerUID, but zero is OK if there is only one ! */
256                 request.data = &streaming_group_req;
257                 request.size = sizeof(streaming_group_req);
258
259                 memset(&streaming_group_req, 0, sizeof(streaming_group_req));
260
261                 streaming_group_req.stream_count = stream_count;
262                 streaming_group_req.channel_count = 2;
263                 streaming_group_req.latency = 256;
264                 streaming_group_req.connector = pipe->uid_left_connector;  /* the left connector */
265
266                 for (i=0; i<stream_count; i++) {
267                         int j;
268                         struct mixart_flowinfo *flowinfo;
269                         struct mixart_bufferinfo *bufferinfo;
270                         
271                         /* we don't yet know the format, so config 16 bit pcm audio for instance */
272                         streaming_group_req.stream_info[i].size_max_byte_frame = 1024;
273                         streaming_group_req.stream_info[i].size_max_sample_frame = 256;
274                         streaming_group_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
275
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 */
279
280                         streaming_group_req.flow_entry[i] = j;
281
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 ! */
285
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 */
289
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;
292                         if(capture) {
293                                 bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
294                         }
295                 }
296
297                 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(streaming_group_resp), &streaming_group_resp);
298                 if((err < 0) || (streaming_group_resp.status != 0)) {
299                         snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, streaming_group_resp.status);
300                         return NULL;
301                 }
302
303                 pipe->group_uid = streaming_group_resp.group;     /* id of the pipe, as returned by embedded */
304                 pipe->stream_count = streaming_group_resp.stream_count;
305                 /* pipe->stream_uid[i] = streaming_group_resp.stream[i].stream_uid; */
306
307                 pipe->status = PIPE_STOPPED;
308         }
309
310         if(monitoring)  pipe->monitoring = 1;
311         else            pipe->references++;
312
313         return pipe;
314 }
315
316
317 int snd_mixart_kill_ref_pipe( mixart_mgr_t *mgr, mixart_pipe_t *pipe, int monitoring)
318 {
319         int err = 0;
320
321         if(pipe->status == PIPE_UNDEFINED)
322                 return 0;
323
324         if(monitoring)
325                 pipe->monitoring = 0;
326         else
327                 pipe->references--;
328
329         if((pipe->references <= 0) && (pipe->monitoring == 0)) {
330
331                 mixart_msg_t request;
332                 mixart_delete_group_resp_t delete_resp;
333
334                 /* release the clock */
335                 err = mixart_set_clock( mgr, pipe, 0);
336                 if( err < 0 ) {
337                         snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");
338                 }
339
340                 /* stop the pipe */
341                 err = mixart_set_pipe_state(mgr, pipe, 0);
342                 if( err < 0 ) {
343                         snd_printk(KERN_ERR "error stopping pipe!\n");
344                 }
345
346                 request.message_id = MSG_STREAM_DELETE_GROUP;
347                 request.uid = (mixart_uid_t){0,0};
348                 request.data = &pipe->group_uid;            /* the streaming group ! */
349                 request.size = sizeof(pipe->group_uid);
350
351                 /* delete the pipe */
352                 err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
353                 if ((err < 0) || (delete_resp.status != 0)) {
354                         snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);
355                 }
356
357                 pipe->group_uid = (mixart_uid_t){0,0};
358                 pipe->stream_count = 0;
359                 pipe->status = PIPE_UNDEFINED;
360         }
361
362         return err;
363 }
364
365 static int mixart_set_stream_state(mixart_stream_t *stream, int start)
366 {
367         mixart_t *chip;
368         mixart_stream_state_req_t stream_state_req;
369         mixart_msg_t request;
370
371         if(!stream->substream)
372                 return -EINVAL;
373
374         memset(&stream_state_req, 0, sizeof(stream_state_req));
375         stream_state_req.stream_count = 1;
376         stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
377         stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
378
379         if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
380                 request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
381         else
382                 request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
383
384         request.uid = (mixart_uid_t){0,0};
385         request.data = &stream_state_req;
386         request.size = sizeof(stream_state_req);
387
388         stream->abs_period_elapsed = 0;            /* reset stream pos      */
389         stream->buf_periods = 0;
390         stream->buf_period_frag = 0;
391
392         chip = snd_pcm_substream_chip(stream->substream);
393
394         return snd_mixart_send_msg_nonblock(chip->mgr, &request);
395 }
396
397 /*
398  *  Trigger callback
399  */
400
401 static int snd_mixart_trigger(snd_pcm_substream_t *subs, int cmd)
402 {
403         mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
404
405         switch (cmd) {
406         case SNDRV_PCM_TRIGGER_START:
407
408                 snd_printdd("SNDRV_PCM_TRIGGER_START\n");
409
410                 /* START_STREAM */
411                 if( mixart_set_stream_state(stream, 1) )
412                         return -EINVAL;
413
414                 stream->status = MIXART_STREAM_STATUS_RUNNING;
415
416                 break;
417         case SNDRV_PCM_TRIGGER_STOP:
418
419                 /* STOP_STREAM */
420                 if( mixart_set_stream_state(stream, 0) )
421                         return -EINVAL;
422
423                 stream->status = MIXART_STREAM_STATUS_OPEN;
424
425                 snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
426
427                 break;
428
429         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
430                 /* TODO */
431                 stream->status = MIXART_STREAM_STATUS_PAUSE;
432                 snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");
433                 break;
434         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
435                 /* TODO */
436                 stream->status = MIXART_STREAM_STATUS_RUNNING;
437                 snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");
438                 break;
439         default:
440                 return -EINVAL;
441         }
442         return 0;
443 }
444
445 static int mixart_sync_nonblock_events(mixart_mgr_t *mgr)
446 {
447         int timeout = HZ;
448         while (atomic_read(&mgr->msg_processed) > 0) {
449                 if (! timeout--) {
450                         snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");
451                         return -EBUSY;
452                 }
453                 set_current_state(TASK_UNINTERRUPTIBLE);
454                 schedule_timeout(1);
455         }
456         return 0;
457 }
458
459 /*
460  *  prepare callback for all pcms
461  *
462  *  NOTE: this callback is non-atomic (pcm->info_flags |= SNDRV_PCM_INFO_NONATOMIC_OPS)
463  */
464 static int snd_mixart_prepare(snd_pcm_substream_t *subs)
465 {
466         mixart_t *chip = snd_pcm_substream_chip(subs);
467         mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
468
469         /* TODO de façon non bloquante, réappliquer les hw_params (rate, bits, codec) */
470
471         snd_printdd("snd_mixart_prepare\n");
472
473         mixart_sync_nonblock_events(chip->mgr);
474
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;
479
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) )
483                         return -EINVAL;
484         }
485
486         return 0;
487 }
488
489
490 static int mixart_set_format(mixart_stream_t *stream, snd_pcm_format_t format)
491 {
492         int err;
493         mixart_t *chip;
494         mixart_msg_t request;
495         mixart_stream_param_desc_t stream_param;
496         mixart_return_uid_t resp;
497
498         chip = snd_pcm_substream_chip(stream->substream);
499
500         memset(&stream_param, 0, sizeof(stream_param));
501
502         stream_param.coding_type = CT_LINEAR;
503         stream_param.number_of_channel = stream->channels;
504
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 */
508
509         switch(format){
510         case SNDRV_PCM_FORMAT_U8:
511                 stream_param.sample_type = ST_INTEGER_8;
512                 stream_param.sample_size = 8;
513                 break;
514         case SNDRV_PCM_FORMAT_S16_LE:
515                 stream_param.sample_type = ST_INTEGER_16LE;
516                 stream_param.sample_size = 16;
517                 break;
518         case SNDRV_PCM_FORMAT_S16_BE:
519                 stream_param.sample_type = ST_INTEGER_16BE;
520                 stream_param.sample_size = 16;
521                 break;
522         case SNDRV_PCM_FORMAT_S24_3LE:
523                 stream_param.sample_type = ST_INTEGER_24LE;
524                 stream_param.sample_size = 24;
525                 break;
526         case SNDRV_PCM_FORMAT_S24_3BE:
527                 stream_param.sample_type = ST_INTEGER_24BE;
528                 stream_param.sample_size = 24;
529                 break;
530         case SNDRV_PCM_FMTBIT_FLOAT_LE:
531                 stream_param.sample_type = ST_FLOATING_POINT_32LE;
532                 stream_param.sample_size = 32;
533                 break;
534         case  SNDRV_PCM_FMTBIT_FLOAT_BE:
535                 stream_param.sample_type = ST_FLOATING_POINT_32BE;
536                 stream_param.sample_size = 32;
537                 break;
538         default:
539                 snd_printk(KERN_ERR "error mixart_set_format() : unknown format\n");
540                 return -EINVAL;
541         }
542
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);
545
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; */
550
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;
555
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);
560
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);
564                 return -EINVAL;
565         }
566         return 0;
567 }
568
569
570 /*
571  *  HW_PARAMS callback for all pcms
572  */
573 static int snd_mixart_hw_params(snd_pcm_substream_t *subs,
574                                 snd_pcm_hw_params_t *hw)
575 {
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;
580         int err;
581         int channels;
582
583         /* set up channels */
584         channels = params_channels(hw);
585
586         /*  set up format for the stream */
587         format = params_format(hw);
588
589         down(&mgr->setup_mutex);
590
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);
596                 else
597                         mixart_update_capture_stream_level( chip, is_aes);
598         }
599
600         stream->channels = channels;
601
602         /* set the format to the board */
603         err = mixart_set_format(stream, format);
604         if(err < 0) {
605                 return err;
606         }
607
608         /* allocate buffer */
609         err = snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw));
610
611         if (err > 0) {
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 */
616                 }
617                 
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 */
622
623                 snd_printdd("snd_mixart_hw_params(pcm %d) : dma_addr(%x) dma_bytes(%x) subs-number(%d)\n", i, subs->runtime->dma_addr, subs->runtime->dma_bytes, subs->number);
624         }
625         up(&mgr->setup_mutex);
626
627         return err;
628 }
629
630 static int snd_mixart_hw_free(snd_pcm_substream_t *subs)
631 {
632         mixart_t *chip = snd_pcm_substream_chip(subs);
633         snd_pcm_lib_free_pages(subs);
634         mixart_sync_nonblock_events(chip->mgr);
635         return 0;
636 }
637
638
639
640 /*
641  *  TODO CONFIGURATION SPACE for all pcms, mono pcm must update channels_max
642  */
643 static snd_pcm_hardware_t snd_mixart_analog_caps =
644 {
645         .info             = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
646                               SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
647                               SNDRV_PCM_INFO_PAUSE),
648         .formats          = ( SNDRV_PCM_FMTBIT_U8 |
649                               SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
650                               SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
651                               SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
652         .rates            = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
653         .rate_min         = 8000,
654         .rate_max         = 48000,
655         .channels_min     = 1,
656         .channels_max     = 2,
657         .buffer_bytes_max = (32*1024),
658         .period_bytes_min = 256,                  /* 256 frames U8 mono*/
659         .period_bytes_max = (16*1024),
660         .periods_min      = 2,
661         .periods_max      = (32*1024/256),
662 };
663
664 static snd_pcm_hardware_t snd_mixart_digital_caps =
665 {
666         .info             = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
667                               SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
668                               SNDRV_PCM_INFO_PAUSE),
669         .formats          = ( SNDRV_PCM_FMTBIT_U8 |
670                               SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
671                               SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
672                               SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
673         .rates            = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
674         .rate_min         = 32000,
675         .rate_max         = 48000,
676         .channels_min     = 1,
677         .channels_max     = 2,
678         .buffer_bytes_max = (32*1024),
679         .period_bytes_min = 256,                  /* 256 frames U8 mono*/
680         .period_bytes_max = (16*1024),
681         .periods_min      = 2,
682         .periods_max      = (32*1024/256),
683 };
684
685
686 static int snd_mixart_playback_open(snd_pcm_substream_t *subs)
687 {
688         mixart_t            *chip = snd_pcm_substream_chip(subs);
689         mixart_mgr_t        *mgr = chip->mgr;
690         snd_pcm_runtime_t   *runtime = subs->runtime;
691         snd_pcm_t           *pcm = subs->pcm;
692         mixart_stream_t     *stream;
693         mixart_pipe_t       *pipe;
694         int err = 0;
695         int pcm_number;
696
697         down(&mgr->setup_mutex);
698
699         if ( pcm == chip->pcm ) {
700                 pcm_number = MIXART_PCM_ANALOG;
701                 runtime->hw = snd_mixart_analog_caps;
702         } else {
703                 snd_assert ( pcm == chip->pcm_dig ); 
704                 pcm_number = MIXART_PCM_DIGITAL;
705                 runtime->hw = snd_mixart_digital_caps;
706         }
707         snd_printdd("snd_mixart_playback_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
708
709         /* get stream info */
710         stream = &(chip->playback_stream[pcm_number][subs->number]);
711
712         if (stream->status != MIXART_STREAM_STATUS_FREE){
713                 /* streams in use */
714                 snd_printk(KERN_ERR "snd_mixart_playback_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
715                 err = -EBUSY;
716                 goto _exit_open;
717         }
718
719         /* get pipe pointer (out pipe) */
720         pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 0, 0);
721
722         if (pipe == NULL) {
723                 err = -EINVAL;
724                 goto _exit_open;
725         }
726
727         /* start the pipe if necessary */
728         err = mixart_set_pipe_state(chip->mgr, pipe, 1);
729         if( err < 0 ) {
730                 snd_printk(KERN_ERR "error starting pipe!\n");
731                 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
732                 err = -EINVAL;
733                 goto _exit_open;
734         }
735
736         stream->pipe        = pipe;
737         stream->pcm_number  = pcm_number;
738         stream->status      = MIXART_STREAM_STATUS_OPEN;
739         stream->substream   = subs;
740         stream->channels    = 0; /* not configured yet */
741
742         runtime->private_data = stream;
743
744         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
745         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
746
747         /* if a sample rate is already used, another stream cannot change */
748         if(mgr->ref_count_rate++) {
749                 if(mgr->sample_rate) {
750                         runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
751                 }
752         }
753
754  _exit_open:
755         up(&mgr->setup_mutex);
756
757         return err;
758 }
759
760
761 static int snd_mixart_capture_open(snd_pcm_substream_t *subs)
762 {
763         mixart_t            *chip = snd_pcm_substream_chip(subs);
764         mixart_mgr_t        *mgr = chip->mgr;
765         snd_pcm_runtime_t   *runtime = subs->runtime;
766         snd_pcm_t           *pcm = subs->pcm;
767         mixart_stream_t     *stream;
768         mixart_pipe_t       *pipe;
769         int err = 0;
770         int pcm_number;
771
772         down(&mgr->setup_mutex);
773
774         if ( pcm == chip->pcm ) {
775                 pcm_number = MIXART_PCM_ANALOG;
776                 runtime->hw = snd_mixart_analog_caps;
777         } else {
778                 snd_assert ( pcm == chip->pcm_dig ); 
779                 pcm_number = MIXART_PCM_DIGITAL;
780                 runtime->hw = snd_mixart_digital_caps;
781         }
782
783         runtime->hw.channels_min = 2; /* for instance, no mono */
784
785         snd_printdd("snd_mixart_capture_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
786
787         /* get stream info */
788         stream = &(chip->capture_stream[pcm_number]);
789
790         if (stream->status != MIXART_STREAM_STATUS_FREE){
791                 /* streams in use */
792                 snd_printk(KERN_ERR "snd_mixart_capture_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
793                 err = -EBUSY;
794                 goto _exit_open;
795         }
796
797         /* get pipe pointer (in pipe) */
798         pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 1, 0);
799
800         if (pipe == NULL) {
801                 err = -EINVAL;
802                 goto _exit_open;
803         }
804
805         /* start the pipe if necessary */
806         err = mixart_set_pipe_state(chip->mgr, pipe, 1);
807         if( err < 0 ) {
808                 snd_printk(KERN_ERR "error starting pipe!\n");
809                 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
810                 err = -EINVAL;
811                 goto _exit_open;
812         }
813
814         stream->pipe        = pipe;
815         stream->pcm_number  = pcm_number;
816         stream->status      = MIXART_STREAM_STATUS_OPEN;
817         stream->substream   = subs;
818         stream->channels    = 0; /* not configured yet */
819
820         runtime->private_data = stream;
821
822         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
823         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
824
825         /* if a sample rate is already used, another stream cannot change */
826         if(mgr->ref_count_rate++) {
827                 if(mgr->sample_rate) {
828                         runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
829                 }
830         }
831
832  _exit_open:
833         up(&mgr->setup_mutex);
834
835         return err;
836 }
837
838
839
840 static int snd_mixart_close(snd_pcm_substream_t *subs)
841 {
842         mixart_t *chip = snd_pcm_substream_chip(subs);
843         mixart_mgr_t *mgr = chip->mgr;
844         mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;
845
846         down(&mgr->setup_mutex);
847
848         snd_printdd("snd_mixart_close C%d/P%d/Sub%d\n", chip->chip_idx, stream->pcm_number, subs->number);
849
850         /* sample rate released */
851         if(--mgr->ref_count_rate == 0) {
852                 mgr->sample_rate = 0;
853         }
854
855         /* delete pipe */
856         if (snd_mixart_kill_ref_pipe(mgr, stream->pipe, 0 ) < 0) {
857
858                 snd_printk(KERN_ERR "error snd_mixart_kill_ref_pipe C%dP%d\n", chip->chip_idx, stream->pcm_number);
859         }
860
861         stream->pipe      = NULL;
862         stream->status    = MIXART_STREAM_STATUS_FREE;
863         stream->substream = NULL;
864
865         up(&mgr->setup_mutex);
866         return 0;
867 }
868
869
870 static snd_pcm_uframes_t snd_mixart_stream_pointer(snd_pcm_substream_t * subs)
871 {
872         snd_pcm_runtime_t *runtime = subs->runtime;
873         mixart_stream_t   *stream  = (mixart_stream_t*)runtime->private_data;
874
875         return (snd_pcm_uframes_t)((stream->buf_periods * runtime->period_size) + stream->buf_period_frag);
876 }
877
878
879
880 static snd_pcm_ops_t snd_mixart_playback_ops = {
881         .open      = snd_mixart_playback_open,
882         .close     = snd_mixart_close,
883         .ioctl     = snd_pcm_lib_ioctl,
884         .prepare   = snd_mixart_prepare,
885         .hw_params = snd_mixart_hw_params,
886         .hw_free   = snd_mixart_hw_free,
887         .trigger   = snd_mixart_trigger,
888         .pointer   = snd_mixart_stream_pointer,
889 };
890
891 static snd_pcm_ops_t snd_mixart_capture_ops = {
892         .open      = snd_mixart_capture_open,
893         .close     = snd_mixart_close,
894         .ioctl     = snd_pcm_lib_ioctl,
895         .prepare   = snd_mixart_prepare,
896         .hw_params = snd_mixart_hw_params,
897         .hw_free   = snd_mixart_hw_free,
898         .trigger   = snd_mixart_trigger,
899         .pointer   = snd_mixart_stream_pointer,
900 };
901
902 static void preallocate_buffers(mixart_t *chip, snd_pcm_t *pcm)
903 {
904         snd_pcm_substream_t *subs;
905         int stream;
906
907         for (stream = 0; stream < 2; stream++) {
908                 int idx = 0;
909                 for (subs = pcm->streams[stream].substream; subs; subs = subs->next, idx++)
910                         /* set up the unique device id with the chip index */
911                         subs->dma_device.id = subs->pcm->device << 16 |
912                                 subs->stream << 8 | (subs->number + 1) |
913                                 (chip->chip_idx + 1) << 24;
914         }
915         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
916                                               snd_dma_pci_data(chip->mgr->pci), 32*1024, 32*1024);
917 }
918
919 /*
920  */
921 static int snd_mixart_pcm_analog(mixart_t *chip)
922 {
923         int err;
924         snd_pcm_t *pcm;
925         char name[32];
926
927         sprintf(name, "miXart analog %d", chip->chip_idx);
928         if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_ANALOG,
929                                MIXART_PLAYBACK_STREAMS,
930                                MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
931                 snd_printk(KERN_ERR "cannot create the analog pcm %d\n", chip->chip_idx);
932                 return err;
933         }
934
935         pcm->private_data = chip;
936
937         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
938         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
939
940         pcm->info_flags = SNDRV_PCM_INFO_NONATOMIC_OPS;
941         strcpy(pcm->name, name);
942
943         preallocate_buffers(chip, pcm);
944
945         chip->pcm = pcm;
946         return 0;
947 }
948
949
950 /*
951  */
952 static int snd_mixart_pcm_digital(mixart_t *chip)
953 {
954         int err;
955         snd_pcm_t *pcm;
956         char name[32];
957
958         sprintf(name, "miXart AES/EBU %d", chip->chip_idx);
959         if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_DIGITAL,
960                                MIXART_PLAYBACK_STREAMS,
961                                MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
962                 snd_printk(KERN_ERR "cannot create the digital pcm %d\n", chip->chip_idx);
963                 return err;
964         }
965
966         pcm->private_data = chip;
967
968         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
969         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
970
971         pcm->info_flags = SNDRV_PCM_INFO_NONATOMIC_OPS;
972         strcpy(pcm->name, name);
973
974         preallocate_buffers(chip, pcm);
975
976         chip->pcm_dig = pcm;
977         return 0;
978 }
979
980 static int snd_mixart_chip_free(mixart_t *chip)
981 {
982         snd_magic_kfree(chip);
983         return 0;
984 }
985
986 static int snd_mixart_chip_dev_free(snd_device_t *device)
987 {
988         mixart_t *chip = snd_magic_cast(mixart_t, device->device_data, return -ENXIO);
989         return snd_mixart_chip_free(chip);
990 }
991
992
993 /*
994  */
995 static int __devinit snd_mixart_create(mixart_mgr_t *mgr, snd_card_t *card, int idx)
996 {
997         int err;
998         mixart_t *chip;
999         static snd_device_ops_t ops = {
1000                 .dev_free = snd_mixart_chip_dev_free,
1001         };
1002
1003         mgr->chip[idx] = chip = snd_magic_kcalloc(mixart_t, 0, GFP_KERNEL);
1004         if (! chip) {
1005                 snd_printk(KERN_ERR "cannot allocate chip\n");
1006                 return -ENOMEM;
1007         }
1008
1009         chip->card = card;
1010         chip->chip_idx = idx;
1011         chip->mgr = mgr;
1012
1013         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1014                 snd_mixart_chip_free(chip);
1015                 return err;
1016         }
1017
1018         if (idx == 0) {
1019                 /* create a DSP loader only on first cardX*/
1020                 err = snd_mixart_hwdep_new(mgr);
1021                 if (err < 0)
1022                         return err;
1023         }
1024
1025         snd_card_set_dev(card, &mgr->pci->dev);
1026
1027         return 0;
1028 }
1029
1030 int snd_mixart_create_pcm(mixart_t* chip)
1031 {
1032         int err;
1033
1034         err = snd_mixart_pcm_analog(chip);
1035         if (err < 0)
1036                 return err;
1037
1038         if(chip->mgr->board_type == MIXART_DAUGHTER_TYPE_AES) {
1039
1040                 err = snd_mixart_pcm_digital(chip);
1041                 if (err < 0)
1042                         return err;
1043         }
1044         return err;
1045 }
1046
1047
1048 /*
1049  * release all the cards assigned to a manager instance
1050  */
1051 static int snd_mixart_free(mixart_mgr_t *mgr)
1052 {
1053         unsigned int i;
1054
1055         for (i = 0; i < mgr->num_cards; i++) {
1056                 if (mgr->chip[i])
1057                         snd_card_free(mgr->chip[i]->card);
1058         }
1059
1060         /* stop mailbox */
1061         snd_mixart_exit_mailbox(mgr);
1062
1063         /* release irq  */
1064         if (mgr->irq >= 0)
1065                 free_irq(mgr->irq, (void *)mgr);
1066
1067         /* reset board if some firmware was loaded */
1068         if(mgr->hwdep->dsp_loaded) {
1069                 snd_mixart_reset_board(mgr);
1070                 snd_printdd("reset miXart !\n");
1071         }
1072
1073         /* release the i/o ports */
1074         for (i = 0; i < 2; i++) {
1075                 if (mgr->mem[i].virt)
1076                         iounmap((void *)mgr->mem[i].virt);
1077                 if (mgr->mem[i].res) {
1078                         release_resource(mgr->mem[i].res);
1079                         kfree_nocheck(mgr->mem[i].res);
1080                 }
1081         }
1082
1083         /* free flowarray */
1084         if(mgr->flowinfo.area) {
1085                 snd_dma_free_pages(&mgr->dma_dev, &mgr->flowinfo);
1086                 mgr->flowinfo.area = NULL;
1087         }
1088         /* free bufferarray */
1089         if(mgr->bufferinfo.area) {
1090                 snd_dma_free_pages(&mgr->dma_dev, &mgr->bufferinfo);
1091                 mgr->bufferinfo.area = NULL;
1092         }
1093
1094         snd_magic_kfree(mgr);
1095         return 0;
1096 }
1097
1098 /*
1099  * proc interface
1100  */
1101 static long long snd_mixart_BA0_llseek(snd_info_entry_t *entry,
1102                                        void *private_file_data,
1103                                        struct file *file,
1104                                        long long offset,
1105                                        int orig)
1106 {
1107         offset = offset & ~3; /* 4 bytes aligned */
1108
1109         switch(orig) {
1110         case 0:  /* SEEK_SET */
1111                 file->f_pos = offset;
1112                 break;
1113         case 1:  /* SEEK_CUR */
1114                 file->f_pos += offset;
1115                 break;
1116         case 2:  /* SEEK_END, offset is negative */
1117                 file->f_pos = MIXART_BA0_SIZE + offset;
1118                 break;
1119         default:
1120                 return -EINVAL;
1121         }
1122         if(file->f_pos > MIXART_BA0_SIZE)
1123                 file->f_pos = MIXART_BA0_SIZE;
1124         return file->f_pos;
1125 }
1126
1127 static long long snd_mixart_BA1_llseek(snd_info_entry_t *entry,
1128                                        void *private_file_data,
1129                                        struct file *file,
1130                                        long long offset,
1131                                        int orig)
1132 {
1133         offset = offset & ~3; /* 4 bytes aligned */
1134
1135         switch(orig) {
1136         case 0:  /* SEEK_SET */
1137                 file->f_pos = offset;
1138                 break;
1139         case 1:  /* SEEK_CUR */
1140                 file->f_pos += offset;
1141                 break;
1142         case 2: /* SEEK_END, offset is negative */
1143                 file->f_pos = MIXART_BA1_SIZE + offset;
1144                 break;
1145         default:
1146                 return -EINVAL;
1147         }
1148         if(file->f_pos > MIXART_BA1_SIZE)
1149                 file->f_pos = MIXART_BA1_SIZE;
1150         return file->f_pos;
1151 }
1152
1153 /*
1154   mixart_BA0 proc interface for BAR 0 - read callback
1155  */
1156 static long snd_mixart_BA0_read(snd_info_entry_t *entry, void *file_private_data,
1157                                 struct file *file, char __user *buf, long count)
1158 {
1159         mixart_mgr_t *mgr = snd_magic_cast(mixart_mgr_t, entry->private_data, return -ENXIO);
1160
1161         count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1162         if(count <= 0)
1163                 return 0;
1164         if(file->f_pos + count > MIXART_BA0_SIZE)
1165                 count = (long)(MIXART_BA0_SIZE - file->f_pos);
1166         if(copy_to_user_fromio(buf, MIXART_MEM( mgr, file->f_pos ), count))
1167                 return -EFAULT;
1168         file->f_pos += count;
1169         return count;
1170 }
1171
1172 /*
1173   mixart_BA1 proc interface for BAR 1 - read callback
1174  */
1175 static long snd_mixart_BA1_read(snd_info_entry_t *entry, void *file_private_data,
1176                                 struct file *file, char __user *buf, long count)
1177 {
1178         mixart_mgr_t *mgr = snd_magic_cast(mixart_mgr_t, entry->private_data, return -ENXIO);
1179
1180         count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1181         if(count <= 0)
1182                 return 0;
1183         if(file->f_pos + count > MIXART_BA1_SIZE)
1184                 count = (long)(MIXART_BA1_SIZE - file->f_pos);
1185         if(copy_to_user_fromio(buf, MIXART_REG( mgr, file->f_pos ), count))
1186                 return -EFAULT;
1187         file->f_pos += count;
1188         return count;
1189 }
1190
1191 static struct snd_info_entry_ops snd_mixart_proc_ops_BA0 = {
1192         .read   = snd_mixart_BA0_read,
1193         .llseek = snd_mixart_BA0_llseek
1194 };
1195
1196 static struct snd_info_entry_ops snd_mixart_proc_ops_BA1 = {
1197         .read   = snd_mixart_BA1_read,
1198         .llseek = snd_mixart_BA1_llseek
1199 };
1200
1201
1202 static void snd_mixart_proc_read(snd_info_entry_t *entry, 
1203                                  snd_info_buffer_t * buffer)
1204 {
1205         mixart_t *chip = snd_magic_cast(mixart_t, entry->private_data, return);        
1206         u32 ref; 
1207
1208         snd_iprintf(buffer, "Digigram miXart (alsa card %d)\n\n", chip->chip_idx);
1209
1210         /* stats available when embedded OS is running */
1211         if (chip->mgr->hwdep->dsp_loaded & ( 1 << MIXART_MOTHERBOARD_ELF_INDEX)) {
1212                 snd_iprintf(buffer, "- hardware -\n");
1213                 switch (chip->mgr->board_type ) {
1214                 case MIXART_DAUGHTER_TYPE_NONE     : snd_iprintf(buffer, "\tmiXart8 (no daughter board)\n\n"); break;
1215                 case MIXART_DAUGHTER_TYPE_AES      : snd_iprintf(buffer, "\tmiXart8 AES/EBU\n\n"); break;
1216                 case MIXART_DAUGHTER_TYPE_COBRANET : snd_iprintf(buffer, "\tmiXart8 Cobranet\n\n"); break;
1217                 default:                             snd_iprintf(buffer, "\tUNKNOWN!\n\n"); break;
1218                 }
1219
1220                 snd_iprintf(buffer, "- system load -\n");        
1221
1222                 /* get perf reference */
1223
1224                 ref = readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_SYSTEM_LOAD_OFFSET));
1225
1226                 if (ref) {
1227                         u32 mailbox   = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_MAILBX_LOAD_OFFSET)) / ref;
1228                         u32 streaming = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_STREAM_LOAD_OFFSET)) / ref;
1229                         u32 interr    = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_INTERR_LOAD_OFFSET)) / ref;
1230
1231                         snd_iprintf(buffer, "\tstreaming          : %d\n", streaming);
1232                         snd_iprintf(buffer, "\tmailbox            : %d\n", mailbox);
1233                         snd_iprintf(buffer, "\tinterrups handling : %d\n\n", interr);
1234                 }
1235         } /* endif elf loaded */
1236 }
1237
1238 static void __devinit snd_mixart_proc_init(mixart_t *chip)
1239 {
1240         snd_info_entry_t *entry;
1241
1242         /* text interface to read perf and temp meters */
1243         if (! snd_card_proc_new(chip->card, "board_info", &entry)) {
1244                 entry->private_data = chip;
1245                 entry->c.text.read_size = 1024;
1246                 entry->c.text.read = snd_mixart_proc_read;
1247         }
1248
1249         if (! snd_card_proc_new(chip->card, "mixart_BA0", &entry)) {
1250                 entry->content = SNDRV_INFO_CONTENT_DATA;
1251                 entry->private_data = chip->mgr;        
1252                 entry->c.ops = &snd_mixart_proc_ops_BA0;
1253                 entry->size = MIXART_BA0_SIZE;
1254         }
1255         if (! snd_card_proc_new(chip->card, "mixart_BA1", &entry)) {
1256                 entry->content = SNDRV_INFO_CONTENT_DATA;
1257                 entry->private_data = chip->mgr;
1258                 entry->c.ops = &snd_mixart_proc_ops_BA1;
1259                 entry->size = MIXART_BA1_SIZE;
1260         }
1261 }
1262 /* end of proc interface */
1263
1264
1265 /*
1266  *    probe function - creates the card manager
1267  */
1268 static int __devinit snd_mixart_probe(struct pci_dev *pci,
1269                                       const struct pci_device_id *pci_id)
1270 {
1271         static int dev;
1272         mixart_mgr_t *mgr;
1273         unsigned int i;
1274         int err;
1275         size_t size;
1276
1277         /*
1278          */
1279         if (dev >= SNDRV_CARDS)
1280                 return -ENODEV;
1281         if (! enable[dev]) {
1282                 dev++;
1283                 return -ENOENT;
1284         }
1285
1286         /* enable PCI device */
1287         if ((err = pci_enable_device(pci)) < 0)
1288                 return err;
1289         pci_set_master(pci);
1290
1291         /* check if we can restrict PCI DMA transfers to 32 bits */
1292         if (!pci_dma_supported(pci, 0xffffffff)) {
1293                 snd_printk(KERN_ERR "architecture does not support 32bit PCI busmaster DMA\n");
1294                 return -ENXIO;
1295         }
1296         pci_set_dma_mask(pci, 0xffffffff);
1297
1298         /*
1299          */
1300         mgr = snd_magic_kcalloc(mixart_mgr_t, 0, GFP_KERNEL);
1301         if (! mgr)
1302                 return -ENOMEM;
1303
1304         mgr->pci = pci;
1305         mgr->irq = -1;
1306
1307         /* resource assignment */
1308         for (i = 0; i < 2; i++) {
1309                 static int memory_sizes[2] = {
1310                         MIXART_BA0_SIZE, /* 16M */        
1311                         MIXART_BA1_SIZE  /* 4 k */
1312                 };
1313                 mgr->mem[i].phys = pci_resource_start(pci, i);
1314                 mgr->mem[i].res = request_mem_region(mgr->mem[i].phys, memory_sizes[i], CARD_NAME);
1315                 if (! mgr->mem[i].res) {
1316                         snd_printk(KERN_ERR "unable to grab memory 0x%lx\n", mgr->mem[i].phys);
1317                         snd_mixart_free(mgr);
1318                         return -EBUSY;
1319                 }
1320                 mgr->mem[i].virt = (unsigned long)ioremap_nocache(mgr->mem[i].phys, memory_sizes[i]);
1321         }
1322
1323         if (request_irq(pci->irq, snd_mixart_interrupt, SA_INTERRUPT|SA_SHIRQ, CARD_NAME, (void *)mgr)) {
1324                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1325                 snd_mixart_free(mgr);
1326                 return -EBUSY;
1327         }
1328         mgr->irq = pci->irq;
1329
1330         sprintf(mgr->shortname, "Digigram miXart");
1331         sprintf(mgr->longname, "%s at 0x%lx & 0x%lx, irq %i", mgr->shortname, mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq);
1332
1333         /* ISR spinlock  */
1334         mgr->lock = SPIN_LOCK_UNLOCKED;
1335
1336         /* init mailbox  */
1337         mgr->msg_fifo_readptr = 0;
1338         mgr->msg_fifo_writeptr = 0;
1339
1340         mgr->msg_lock = SPIN_LOCK_UNLOCKED;
1341         init_MUTEX(&mgr->msg_mutex);
1342         init_waitqueue_head(&mgr->msg_sleep);
1343         atomic_set(&mgr->msg_processed, 0);
1344
1345         /* init setup mutex*/
1346         init_MUTEX(&mgr->setup_mutex);
1347
1348         /* init message taslket */
1349         tasklet_init( &mgr->msg_taskq, snd_mixart_msg_tasklet, (unsigned long) mgr);
1350
1351         /* card assignment */
1352         mgr->num_cards = MIXART_MAX_CARDS; /* 4  FIXME: configurable? */
1353         for (i = 0; i < mgr->num_cards; i++) {
1354                 snd_card_t *card;
1355                 char tmpid[16];
1356                 int idx;
1357
1358                 if (index[dev] < 0)
1359                         idx = index[dev];
1360                 else
1361                         idx = index[dev] + i;
1362                 snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev], i);
1363                 card = snd_card_new(idx, tmpid, THIS_MODULE, 0);
1364
1365                 if (! card) {
1366                         snd_printk(KERN_ERR "cannot allocate the card %d\n", i);
1367                         snd_mixart_free(mgr);
1368                         return -ENOMEM;
1369                 }
1370
1371                 strcpy(card->driver, CARD_NAME);
1372                 sprintf(card->shortname, "%s [PCM #%d]", mgr->shortname, i);
1373                 sprintf(card->longname, "%s [PCM #%d]", mgr->longname, i);
1374
1375                 if ((err = snd_mixart_create(mgr, card, i)) < 0) {
1376                         snd_mixart_free(mgr);
1377                         return err;
1378                 }
1379
1380                 if(i==0) {
1381                         /* init proc interface only for chip0 */
1382                         snd_mixart_proc_init(mgr->chip[i]);
1383                 }
1384
1385                 if ((err = snd_card_register(card)) < 0) {
1386                         snd_mixart_free(mgr);
1387                         return err;
1388                 }
1389         }
1390
1391         /* init firmware status (mgr->hwdep->dsp_loaded reset in hwdep_new) */
1392         mgr->board_type = MIXART_DAUGHTER_TYPE_NONE;
1393
1394         memset(&mgr->dma_dev, 0, sizeof(mgr->dma_dev));
1395         mgr->dma_dev.type = SNDRV_DMA_TYPE_DEV;
1396         mgr->dma_dev.dev = snd_dma_pci_data(mgr->pci);
1397
1398         /* create array of streaminfo */
1399         size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS * sizeof(mixart_flowinfo_t)) );
1400         if (snd_dma_alloc_pages(&mgr->dma_dev, size, &mgr->flowinfo) < 0) {
1401                 snd_mixart_free(mgr);
1402                 return -ENOMEM;
1403         }
1404         /* init streaminfo_array */
1405         memset(mgr->flowinfo.area, 0, size);
1406
1407         /* create array of bufferinfo */
1408         size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS * sizeof(mixart_bufferinfo_t)) );
1409         if (snd_dma_alloc_pages(&mgr->dma_dev, size, &mgr->bufferinfo) < 0) {
1410                 snd_mixart_free(mgr);
1411                 return -ENOMEM;
1412         }
1413         /* init bufferinfo_array */
1414         memset(mgr->bufferinfo.area, 0, size);
1415
1416         pci_set_drvdata(pci, mgr);
1417         dev++;
1418         return 0;
1419 }
1420
1421 static void __devexit snd_mixart_remove(struct pci_dev *pci)
1422 {
1423         snd_mixart_free(pci_get_drvdata(pci));
1424         pci_set_drvdata(pci, NULL);
1425 }
1426
1427 static struct pci_driver driver = {
1428         .name = "Digigram miXart",
1429         .id_table = snd_mixart_ids,
1430         .probe = snd_mixart_probe,
1431         .remove = __devexit_p(snd_mixart_remove),
1432 };
1433
1434 static int __init alsa_card_mixart_init(void)
1435 {
1436         return pci_module_init(&driver);
1437 }
1438
1439 static void __exit alsa_card_mixart_exit(void)
1440 {
1441         pci_unregister_driver(&driver);
1442 }
1443
1444 module_init(alsa_card_mixart_init)
1445 module_exit(alsa_card_mixart_exit)