[ALSA] semaphore -> mutex (PCI part)
[safe/jmp/linux-2.6] / 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 <linux/mutex.h>
30 #include <sound/core.h>
31 #include <sound/initval.h>
32 #include <sound/info.h>
33 #include <sound/control.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include "mixart.h"
37 #include "mixart_hwdep.h"
38 #include "mixart_core.h"
39 #include "mixart_mixer.h"
40
41 #define CARD_NAME "miXart"
42
43 MODULE_AUTHOR("Digigram <alsa@digigram.com>");
44 MODULE_DESCRIPTION("Digigram " CARD_NAME);
45 MODULE_LICENSE("GPL");
46 MODULE_SUPPORTED_DEVICE("{{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
52 module_param_array(index, int, NULL, 0444);
53 MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
54 module_param_array(id, charp, NULL, 0444);
55 MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
56 module_param_array(enable, bool, NULL, 0444);
57 MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
58
59 /*
60  */
61
62 static struct pci_device_id snd_mixart_ids[] = {
63         { 0x1057, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* MC8240 */
64         { 0, }
65 };
66
67 MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
68
69
70 static int mixart_set_pipe_state(struct mixart_mgr *mgr,
71                                  struct mixart_pipe *pipe, int start)
72 {
73         struct mixart_group_state_req group_state;
74         struct mixart_group_state_resp group_state_resp;
75         struct mixart_msg request;
76         int err;
77         u32 system_msg_uid;
78
79         switch(pipe->status) {
80         case PIPE_RUNNING:
81         case PIPE_CLOCK_SET:
82                 if(start) return 0; /* already started */
83                 break;
84         case PIPE_STOPPED:
85                 if(!start) return 0; /* already stopped */
86                 break;
87         default:
88                 snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");
89                 return -EINVAL;      /* function called with wrong pipe status */
90         }
91
92         system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
93
94         /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
95
96         request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
97         request.uid = (struct mixart_uid){0,0};
98         request.data = &system_msg_uid;
99         request.size = sizeof(system_msg_uid);
100
101         err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
102         if(err) {
103                 snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
104                 return err;
105         }
106
107         /* start or stop the pipe (1 pipe) */
108
109         memset(&group_state, 0, sizeof(group_state));
110         group_state.pipe_count = 1;
111         group_state.pipe_uid[0] = pipe->group_uid;
112
113         if(start)
114                 request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
115         else
116                 request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
117
118         request.uid = pipe->group_uid; /*(struct mixart_uid){0,0};*/
119         request.data = &group_state;
120         request.size = sizeof(group_state);
121
122         err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
123         if (err < 0 || group_state_resp.txx_status != 0) {
124                 snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
125                 return -EINVAL;
126         }
127
128         if(start) {
129                 u32 stat;
130
131                 group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
132
133                 err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
134                 if (err < 0 || group_state_resp.txx_status != 0) {
135                         snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
136                         return -EINVAL;
137                 }
138
139                 /* in case of start send a synchro top */
140
141                 request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
142                 request.uid = (struct mixart_uid){0,0};
143                 request.data = NULL;
144                 request.size = 0;
145
146                 err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
147                 if (err < 0 || stat != 0) {
148                         snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);
149                         return -EINVAL;
150                 }
151
152                 pipe->status = PIPE_RUNNING;
153         }
154         else /* !start */
155                 pipe->status = PIPE_STOPPED;
156
157         return 0;
158 }
159
160
161 static int mixart_set_clock(struct mixart_mgr *mgr,
162                             struct mixart_pipe *pipe, unsigned int rate)
163 {
164         struct mixart_msg request;
165         struct mixart_clock_properties clock_properties;
166         struct mixart_clock_properties_resp clock_prop_resp;
167         int err;
168
169         switch(pipe->status) {
170         case PIPE_CLOCK_SET:
171                 break;
172         case PIPE_RUNNING:
173                 if(rate != 0)
174                         break;
175         default:
176                 if(rate == 0)
177                         return 0; /* nothing to do */
178                 else {
179                         snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);
180                         return -EINVAL;
181                 }
182         }
183
184         memset(&clock_properties, 0, sizeof(clock_properties));
185         clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
186         clock_properties.clock_mode = CM_STANDALONE;
187         clock_properties.frequency = rate;
188         clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
189         clock_properties.uid_caller[0] = pipe->group_uid;
190
191         snd_printdd("mixart_set_clock to %d kHz\n", rate);
192
193         request.message_id = MSG_CLOCK_SET_PROPERTIES;
194         request.uid = mgr->uid_console_manager;
195         request.data = &clock_properties;
196         request.size = sizeof(clock_properties);
197
198         err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
199         if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
200                 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                 return -EINVAL;
202         }
203
204         if(rate)  pipe->status = PIPE_CLOCK_SET;
205         else      pipe->status = PIPE_RUNNING;
206
207         return 0;
208 }
209
210
211 /*
212  *  Allocate or reference output pipe for analog IOs (pcmp0/1)
213  */
214 struct mixart_pipe *
215 snd_mixart_add_ref_pipe(struct snd_mixart *chip, int pcm_number, int capture,
216                         int monitoring)
217 {
218         int stream_count;
219         struct mixart_pipe *pipe;
220         struct mixart_msg request;
221
222         if(capture) {
223                 if (pcm_number == MIXART_PCM_ANALOG) {
224                         pipe = &(chip->pipe_in_ana);  /* analog inputs */
225                 } else {
226                         pipe = &(chip->pipe_in_dig); /* digital inputs */
227                 }
228                 request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
229                 stream_count = MIXART_CAPTURE_STREAMS;
230         } else {
231                 if (pcm_number == MIXART_PCM_ANALOG) {
232                         pipe = &(chip->pipe_out_ana);  /* analog outputs */
233                 } else {
234                         pipe = &(chip->pipe_out_dig);  /* digital outputs */
235                 }
236                 request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
237                 stream_count = MIXART_PLAYBACK_STREAMS;
238         }
239
240         /* a new stream is opened and there are already all streams in use */
241         if( (monitoring == 0) && (pipe->references >= stream_count) ) {
242                 return NULL;
243         }
244
245         /* pipe is not yet defined */
246         if( pipe->status == PIPE_UNDEFINED ) {
247                 int err, i;
248                 struct {
249                         struct mixart_streaming_group_req sgroup_req;
250                         struct mixart_streaming_group sgroup_resp;
251                 } *buf;
252
253                 snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);
254
255                 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
256                 if (!buf)
257                         return NULL;
258
259                 request.uid = (struct mixart_uid){0,0};      /* should be StreamManagerUID, but zero is OK if there is only one ! */
260                 request.data = &buf->sgroup_req;
261                 request.size = sizeof(buf->sgroup_req);
262
263                 memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));
264
265                 buf->sgroup_req.stream_count = stream_count;
266                 buf->sgroup_req.channel_count = 2;
267                 buf->sgroup_req.latency = 256;
268                 buf->sgroup_req.connector = pipe->uid_left_connector;  /* the left connector */
269
270                 for (i=0; i<stream_count; i++) {
271                         int j;
272                         struct mixart_flowinfo *flowinfo;
273                         struct mixart_bufferinfo *bufferinfo;
274                         
275                         /* we don't yet know the format, so config 16 bit pcm audio for instance */
276                         buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;
277                         buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;
278                         buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
279
280                         /* find the right bufferinfo_array */
281                         j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;
282                         if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */
283
284                         buf->sgroup_req.flow_entry[i] = j;
285
286                         flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;
287                         flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(struct mixart_bufferinfo));
288                         flowinfo[j].bufferinfo_count = 1;               /* 1 will set the miXart to ring-buffer mode ! */
289
290                         bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
291                         bufferinfo[j].buffer_address = 0;               /* buffer is not yet allocated */
292                         bufferinfo[j].available_length = 0;             /* buffer is not yet allocated */
293
294                         /* construct the identifier of the stream buffer received in the interrupts ! */
295                         bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;
296                         if(capture) {
297                                 bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
298                         }
299                 }
300
301                 err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);
302                 if((err < 0) || (buf->sgroup_resp.status != 0)) {
303                         snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, buf->sgroup_resp.status);
304                         kfree(buf);
305                         return NULL;
306                 }
307
308                 pipe->group_uid = buf->sgroup_resp.group;     /* id of the pipe, as returned by embedded */
309                 pipe->stream_count = buf->sgroup_resp.stream_count;
310                 /* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */
311
312                 pipe->status = PIPE_STOPPED;
313                 kfree(buf);
314         }
315
316         if(monitoring)  pipe->monitoring = 1;
317         else            pipe->references++;
318
319         return pipe;
320 }
321
322
323 int snd_mixart_kill_ref_pipe(struct mixart_mgr *mgr,
324                              struct mixart_pipe *pipe, int monitoring)
325 {
326         int err = 0;
327
328         if(pipe->status == PIPE_UNDEFINED)
329                 return 0;
330
331         if(monitoring)
332                 pipe->monitoring = 0;
333         else
334                 pipe->references--;
335
336         if((pipe->references <= 0) && (pipe->monitoring == 0)) {
337
338                 struct mixart_msg request;
339                 struct mixart_delete_group_resp delete_resp;
340
341                 /* release the clock */
342                 err = mixart_set_clock( mgr, pipe, 0);
343                 if( err < 0 ) {
344                         snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");
345                 }
346
347                 /* stop the pipe */
348                 err = mixart_set_pipe_state(mgr, pipe, 0);
349                 if( err < 0 ) {
350                         snd_printk(KERN_ERR "error stopping pipe!\n");
351                 }
352
353                 request.message_id = MSG_STREAM_DELETE_GROUP;
354                 request.uid = (struct mixart_uid){0,0};
355                 request.data = &pipe->group_uid;            /* the streaming group ! */
356                 request.size = sizeof(pipe->group_uid);
357
358                 /* delete the pipe */
359                 err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
360                 if ((err < 0) || (delete_resp.status != 0)) {
361                         snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);
362                 }
363
364                 pipe->group_uid = (struct mixart_uid){0,0};
365                 pipe->stream_count = 0;
366                 pipe->status = PIPE_UNDEFINED;
367         }
368
369         return err;
370 }
371
372 static int mixart_set_stream_state(struct mixart_stream *stream, int start)
373 {
374         struct snd_mixart *chip;
375         struct mixart_stream_state_req stream_state_req;
376         struct mixart_msg request;
377
378         if(!stream->substream)
379                 return -EINVAL;
380
381         memset(&stream_state_req, 0, sizeof(stream_state_req));
382         stream_state_req.stream_count = 1;
383         stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
384         stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
385
386         if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
387                 request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
388         else
389                 request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
390
391         request.uid = (struct mixart_uid){0,0};
392         request.data = &stream_state_req;
393         request.size = sizeof(stream_state_req);
394
395         stream->abs_period_elapsed = 0;            /* reset stream pos      */
396         stream->buf_periods = 0;
397         stream->buf_period_frag = 0;
398
399         chip = snd_pcm_substream_chip(stream->substream);
400
401         return snd_mixart_send_msg_nonblock(chip->mgr, &request);
402 }
403
404 /*
405  *  Trigger callback
406  */
407
408 static int snd_mixart_trigger(struct snd_pcm_substream *subs, int cmd)
409 {
410         struct mixart_stream *stream = subs->runtime->private_data;
411
412         switch (cmd) {
413         case SNDRV_PCM_TRIGGER_START:
414
415                 snd_printdd("SNDRV_PCM_TRIGGER_START\n");
416
417                 /* START_STREAM */
418                 if( mixart_set_stream_state(stream, 1) )
419                         return -EINVAL;
420
421                 stream->status = MIXART_STREAM_STATUS_RUNNING;
422
423                 break;
424         case SNDRV_PCM_TRIGGER_STOP:
425
426                 /* STOP_STREAM */
427                 if( mixart_set_stream_state(stream, 0) )
428                         return -EINVAL;
429
430                 stream->status = MIXART_STREAM_STATUS_OPEN;
431
432                 snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
433
434                 break;
435
436         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
437                 /* TODO */
438                 stream->status = MIXART_STREAM_STATUS_PAUSE;
439                 snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");
440                 break;
441         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
442                 /* TODO */
443                 stream->status = MIXART_STREAM_STATUS_RUNNING;
444                 snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");
445                 break;
446         default:
447                 return -EINVAL;
448         }
449         return 0;
450 }
451
452 static int mixart_sync_nonblock_events(struct mixart_mgr *mgr)
453 {
454         unsigned long timeout = jiffies + HZ;
455         while (atomic_read(&mgr->msg_processed) > 0) {
456                 if (time_after(jiffies, timeout)) {
457                         snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");
458                         return -EBUSY;
459                 }
460                 schedule_timeout_uninterruptible(1);
461         }
462         return 0;
463 }
464
465 /*
466  *  prepare callback for all pcms
467  */
468 static int snd_mixart_prepare(struct snd_pcm_substream *subs)
469 {
470         struct snd_mixart *chip = snd_pcm_substream_chip(subs);
471         struct mixart_stream *stream = subs->runtime->private_data;
472
473         /* TODO de façon non bloquante, réappliquer les hw_params (rate, bits, codec) */
474
475         snd_printdd("snd_mixart_prepare\n");
476
477         mixart_sync_nonblock_events(chip->mgr);
478
479         /* only the first stream can choose the sample rate */
480         /* the further opened streams will be limited to its frequency (see open) */
481         if(chip->mgr->ref_count_rate == 1)
482                 chip->mgr->sample_rate = subs->runtime->rate;
483
484         /* set the clock only once (first stream) on the same pipe */
485         if(stream->pipe->references == 1) {
486                 if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) )
487                         return -EINVAL;
488         }
489
490         return 0;
491 }
492
493
494 static int mixart_set_format(struct mixart_stream *stream, snd_pcm_format_t format)
495 {
496         int err;
497         struct snd_mixart *chip;
498         struct mixart_msg request;
499         struct mixart_stream_param_desc stream_param;
500         struct mixart_return_uid resp;
501
502         chip = snd_pcm_substream_chip(stream->substream);
503
504         memset(&stream_param, 0, sizeof(stream_param));
505
506         stream_param.coding_type = CT_LINEAR;
507         stream_param.number_of_channel = stream->channels;
508
509         stream_param.sampling_freq = chip->mgr->sample_rate;
510         if(stream_param.sampling_freq == 0)
511                 stream_param.sampling_freq = 44100; /* if frequency not yet defined, use some default */
512
513         switch(format){
514         case SNDRV_PCM_FORMAT_U8:
515                 stream_param.sample_type = ST_INTEGER_8;
516                 stream_param.sample_size = 8;
517                 break;
518         case SNDRV_PCM_FORMAT_S16_LE:
519                 stream_param.sample_type = ST_INTEGER_16LE;
520                 stream_param.sample_size = 16;
521                 break;
522         case SNDRV_PCM_FORMAT_S16_BE:
523                 stream_param.sample_type = ST_INTEGER_16BE;
524                 stream_param.sample_size = 16;
525                 break;
526         case SNDRV_PCM_FORMAT_S24_3LE:
527                 stream_param.sample_type = ST_INTEGER_24LE;
528                 stream_param.sample_size = 24;
529                 break;
530         case SNDRV_PCM_FORMAT_S24_3BE:
531                 stream_param.sample_type = ST_INTEGER_24BE;
532                 stream_param.sample_size = 24;
533                 break;
534         case SNDRV_PCM_FORMAT_FLOAT_LE:
535                 stream_param.sample_type = ST_FLOATING_POINT_32LE;
536                 stream_param.sample_size = 32;
537                 break;
538         case  SNDRV_PCM_FORMAT_FLOAT_BE:
539                 stream_param.sample_type = ST_FLOATING_POINT_32BE;
540                 stream_param.sample_size = 32;
541                 break;
542         default:
543                 snd_printk(KERN_ERR "error mixart_set_format() : unknown format\n");
544                 return -EINVAL;
545         }
546
547         snd_printdd("set SNDRV_PCM_FORMAT sample_type(%d) sample_size(%d) freq(%d) channels(%d)\n",
548                    stream_param.sample_type, stream_param.sample_size, stream_param.sampling_freq, stream->channels);
549
550         /* TODO: what else to configure ? */
551         /* stream_param.samples_per_frame = 2; */
552         /* stream_param.bytes_per_frame = 4; */
553         /* stream_param.bytes_per_sample = 2; */
554
555         stream_param.pipe_count = 1;      /* set to 1 */
556         stream_param.stream_count = 1;    /* set to 1 */
557         stream_param.stream_desc[0].uid_pipe = stream->pipe->group_uid;
558         stream_param.stream_desc[0].stream_idx = stream->substream->number;
559
560         request.message_id = MSG_STREAM_SET_INPUT_STAGE_PARAM;
561         request.uid = (struct mixart_uid){0,0};
562         request.data = &stream_param;
563         request.size = sizeof(stream_param);
564
565         err = snd_mixart_send_msg(chip->mgr, &request, sizeof(resp), &resp);
566         if((err < 0) || resp.error_code) {
567                 snd_printk(KERN_ERR "MSG_STREAM_SET_INPUT_STAGE_PARAM err=%x; resp=%x\n", err, resp.error_code);
568                 return -EINVAL;
569         }
570         return 0;
571 }
572
573
574 /*
575  *  HW_PARAMS callback for all pcms
576  */
577 static int snd_mixart_hw_params(struct snd_pcm_substream *subs,
578                                 struct snd_pcm_hw_params *hw)
579 {
580         struct snd_mixart *chip = snd_pcm_substream_chip(subs);
581         struct mixart_mgr *mgr = chip->mgr;
582         struct mixart_stream *stream = subs->runtime->private_data;
583         snd_pcm_format_t format;
584         int err;
585         int channels;
586
587         /* set up channels */
588         channels = params_channels(hw);
589
590         /*  set up format for the stream */
591         format = params_format(hw);
592
593         mutex_lock(&mgr->setup_mutex);
594
595         /* update the stream levels */
596         if( stream->pcm_number <= MIXART_PCM_DIGITAL ) {
597                 int is_aes = stream->pcm_number > MIXART_PCM_ANALOG;
598                 if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK )
599                         mixart_update_playback_stream_level(chip, is_aes, subs->number);
600                 else
601                         mixart_update_capture_stream_level( chip, is_aes);
602         }
603
604         stream->channels = channels;
605
606         /* set the format to the board */
607         err = mixart_set_format(stream, format);
608         if(err < 0) {
609                 return err;
610         }
611
612         /* allocate buffer */
613         err = snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw));
614
615         if (err > 0) {
616                 struct mixart_bufferinfo *bufferinfo;
617                 int i = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (stream->pcm_number * (MIXART_PLAYBACK_STREAMS+MIXART_CAPTURE_STREAMS)) + subs->number;
618                 if( subs->stream == SNDRV_PCM_STREAM_CAPTURE ) {
619                         i += MIXART_PLAYBACK_STREAMS; /* in array capture is behind playback */
620                 }
621                 
622                 bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
623                 bufferinfo[i].buffer_address = subs->runtime->dma_addr;
624                 bufferinfo[i].available_length = subs->runtime->dma_bytes;
625                 /* bufferinfo[i].buffer_id  is already defined */
626
627                 snd_printdd("snd_mixart_hw_params(pcm %d) : dma_addr(%x) dma_bytes(%x) subs-number(%d)\n", i,
628                                 bufferinfo[i].buffer_address,
629                                 bufferinfo[i].available_length,
630                                 subs->number);
631         }
632         mutex_unlock(&mgr->setup_mutex);
633
634         return err;
635 }
636
637 static int snd_mixart_hw_free(struct snd_pcm_substream *subs)
638 {
639         struct snd_mixart *chip = snd_pcm_substream_chip(subs);
640         snd_pcm_lib_free_pages(subs);
641         mixart_sync_nonblock_events(chip->mgr);
642         return 0;
643 }
644
645
646
647 /*
648  *  TODO CONFIGURATION SPACE for all pcms, mono pcm must update channels_max
649  */
650 static struct snd_pcm_hardware snd_mixart_analog_caps =
651 {
652         .info             = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
653                               SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
654                               SNDRV_PCM_INFO_PAUSE),
655         .formats          = ( SNDRV_PCM_FMTBIT_U8 |
656                               SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
657                               SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
658                               SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
659         .rates            = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
660         .rate_min         = 8000,
661         .rate_max         = 48000,
662         .channels_min     = 1,
663         .channels_max     = 2,
664         .buffer_bytes_max = (32*1024),
665         .period_bytes_min = 256,                  /* 256 frames U8 mono*/
666         .period_bytes_max = (16*1024),
667         .periods_min      = 2,
668         .periods_max      = (32*1024/256),
669 };
670
671 static struct snd_pcm_hardware snd_mixart_digital_caps =
672 {
673         .info             = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
674                               SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
675                               SNDRV_PCM_INFO_PAUSE),
676         .formats          = ( SNDRV_PCM_FMTBIT_U8 |
677                               SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
678                               SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
679                               SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
680         .rates            = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
681         .rate_min         = 32000,
682         .rate_max         = 48000,
683         .channels_min     = 1,
684         .channels_max     = 2,
685         .buffer_bytes_max = (32*1024),
686         .period_bytes_min = 256,                  /* 256 frames U8 mono*/
687         .period_bytes_max = (16*1024),
688         .periods_min      = 2,
689         .periods_max      = (32*1024/256),
690 };
691
692
693 static int snd_mixart_playback_open(struct snd_pcm_substream *subs)
694 {
695         struct snd_mixart            *chip = snd_pcm_substream_chip(subs);
696         struct mixart_mgr        *mgr = chip->mgr;
697         struct snd_pcm_runtime *runtime = subs->runtime;
698         struct snd_pcm *pcm = subs->pcm;
699         struct mixart_stream     *stream;
700         struct mixart_pipe       *pipe;
701         int err = 0;
702         int pcm_number;
703
704         mutex_lock(&mgr->setup_mutex);
705
706         if ( pcm == chip->pcm ) {
707                 pcm_number = MIXART_PCM_ANALOG;
708                 runtime->hw = snd_mixart_analog_caps;
709         } else {
710                 snd_assert ( pcm == chip->pcm_dig ); 
711                 pcm_number = MIXART_PCM_DIGITAL;
712                 runtime->hw = snd_mixart_digital_caps;
713         }
714         snd_printdd("snd_mixart_playback_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
715
716         /* get stream info */
717         stream = &(chip->playback_stream[pcm_number][subs->number]);
718
719         if (stream->status != MIXART_STREAM_STATUS_FREE){
720                 /* streams in use */
721                 snd_printk(KERN_ERR "snd_mixart_playback_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
722                 err = -EBUSY;
723                 goto _exit_open;
724         }
725
726         /* get pipe pointer (out pipe) */
727         pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 0, 0);
728
729         if (pipe == NULL) {
730                 err = -EINVAL;
731                 goto _exit_open;
732         }
733
734         /* start the pipe if necessary */
735         err = mixart_set_pipe_state(chip->mgr, pipe, 1);
736         if( err < 0 ) {
737                 snd_printk(KERN_ERR "error starting pipe!\n");
738                 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
739                 err = -EINVAL;
740                 goto _exit_open;
741         }
742
743         stream->pipe        = pipe;
744         stream->pcm_number  = pcm_number;
745         stream->status      = MIXART_STREAM_STATUS_OPEN;
746         stream->substream   = subs;
747         stream->channels    = 0; /* not configured yet */
748
749         runtime->private_data = stream;
750
751         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
752         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
753
754         /* if a sample rate is already used, another stream cannot change */
755         if(mgr->ref_count_rate++) {
756                 if(mgr->sample_rate) {
757                         runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
758                 }
759         }
760
761  _exit_open:
762         mutex_unlock(&mgr->setup_mutex);
763
764         return err;
765 }
766
767
768 static int snd_mixart_capture_open(struct snd_pcm_substream *subs)
769 {
770         struct snd_mixart            *chip = snd_pcm_substream_chip(subs);
771         struct mixart_mgr        *mgr = chip->mgr;
772         struct snd_pcm_runtime *runtime = subs->runtime;
773         struct snd_pcm *pcm = subs->pcm;
774         struct mixart_stream     *stream;
775         struct mixart_pipe       *pipe;
776         int err = 0;
777         int pcm_number;
778
779         mutex_lock(&mgr->setup_mutex);
780
781         if ( pcm == chip->pcm ) {
782                 pcm_number = MIXART_PCM_ANALOG;
783                 runtime->hw = snd_mixart_analog_caps;
784         } else {
785                 snd_assert ( pcm == chip->pcm_dig ); 
786                 pcm_number = MIXART_PCM_DIGITAL;
787                 runtime->hw = snd_mixart_digital_caps;
788         }
789
790         runtime->hw.channels_min = 2; /* for instance, no mono */
791
792         snd_printdd("snd_mixart_capture_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
793
794         /* get stream info */
795         stream = &(chip->capture_stream[pcm_number]);
796
797         if (stream->status != MIXART_STREAM_STATUS_FREE){
798                 /* streams in use */
799                 snd_printk(KERN_ERR "snd_mixart_capture_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
800                 err = -EBUSY;
801                 goto _exit_open;
802         }
803
804         /* get pipe pointer (in pipe) */
805         pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 1, 0);
806
807         if (pipe == NULL) {
808                 err = -EINVAL;
809                 goto _exit_open;
810         }
811
812         /* start the pipe if necessary */
813         err = mixart_set_pipe_state(chip->mgr, pipe, 1);
814         if( err < 0 ) {
815                 snd_printk(KERN_ERR "error starting pipe!\n");
816                 snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
817                 err = -EINVAL;
818                 goto _exit_open;
819         }
820
821         stream->pipe        = pipe;
822         stream->pcm_number  = pcm_number;
823         stream->status      = MIXART_STREAM_STATUS_OPEN;
824         stream->substream   = subs;
825         stream->channels    = 0; /* not configured yet */
826
827         runtime->private_data = stream;
828
829         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
830         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
831
832         /* if a sample rate is already used, another stream cannot change */
833         if(mgr->ref_count_rate++) {
834                 if(mgr->sample_rate) {
835                         runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
836                 }
837         }
838
839  _exit_open:
840         mutex_unlock(&mgr->setup_mutex);
841
842         return err;
843 }
844
845
846
847 static int snd_mixart_close(struct snd_pcm_substream *subs)
848 {
849         struct snd_mixart *chip = snd_pcm_substream_chip(subs);
850         struct mixart_mgr *mgr = chip->mgr;
851         struct mixart_stream *stream = subs->runtime->private_data;
852
853         mutex_lock(&mgr->setup_mutex);
854
855         snd_printdd("snd_mixart_close C%d/P%d/Sub%d\n", chip->chip_idx, stream->pcm_number, subs->number);
856
857         /* sample rate released */
858         if(--mgr->ref_count_rate == 0) {
859                 mgr->sample_rate = 0;
860         }
861
862         /* delete pipe */
863         if (snd_mixart_kill_ref_pipe(mgr, stream->pipe, 0 ) < 0) {
864
865                 snd_printk(KERN_ERR "error snd_mixart_kill_ref_pipe C%dP%d\n", chip->chip_idx, stream->pcm_number);
866         }
867
868         stream->pipe      = NULL;
869         stream->status    = MIXART_STREAM_STATUS_FREE;
870         stream->substream = NULL;
871
872         mutex_unlock(&mgr->setup_mutex);
873         return 0;
874 }
875
876
877 static snd_pcm_uframes_t snd_mixart_stream_pointer(struct snd_pcm_substream *subs)
878 {
879         struct snd_pcm_runtime *runtime = subs->runtime;
880         struct mixart_stream   *stream  = runtime->private_data;
881
882         return (snd_pcm_uframes_t)((stream->buf_periods * runtime->period_size) + stream->buf_period_frag);
883 }
884
885
886
887 static struct snd_pcm_ops snd_mixart_playback_ops = {
888         .open      = snd_mixart_playback_open,
889         .close     = snd_mixart_close,
890         .ioctl     = snd_pcm_lib_ioctl,
891         .prepare   = snd_mixart_prepare,
892         .hw_params = snd_mixart_hw_params,
893         .hw_free   = snd_mixart_hw_free,
894         .trigger   = snd_mixart_trigger,
895         .pointer   = snd_mixart_stream_pointer,
896 };
897
898 static struct snd_pcm_ops snd_mixart_capture_ops = {
899         .open      = snd_mixart_capture_open,
900         .close     = snd_mixart_close,
901         .ioctl     = snd_pcm_lib_ioctl,
902         .prepare   = snd_mixart_prepare,
903         .hw_params = snd_mixart_hw_params,
904         .hw_free   = snd_mixart_hw_free,
905         .trigger   = snd_mixart_trigger,
906         .pointer   = snd_mixart_stream_pointer,
907 };
908
909 static void preallocate_buffers(struct snd_mixart *chip, struct snd_pcm *pcm)
910 {
911 #if 0
912         struct snd_pcm_substream *subs;
913         int stream;
914
915         for (stream = 0; stream < 2; stream++) {
916                 int idx = 0;
917                 for (subs = pcm->streams[stream].substream; subs; subs = subs->next, idx++)
918                         /* set up the unique device id with the chip index */
919                         subs->dma_device.id = subs->pcm->device << 16 |
920                                 subs->stream << 8 | (subs->number + 1) |
921                                 (chip->chip_idx + 1) << 24;
922         }
923 #endif
924         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
925                                               snd_dma_pci_data(chip->mgr->pci), 32*1024, 32*1024);
926 }
927
928 /*
929  */
930 static int snd_mixart_pcm_analog(struct snd_mixart *chip)
931 {
932         int err;
933         struct snd_pcm *pcm;
934         char name[32];
935
936         sprintf(name, "miXart analog %d", chip->chip_idx);
937         if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_ANALOG,
938                                MIXART_PLAYBACK_STREAMS,
939                                MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
940                 snd_printk(KERN_ERR "cannot create the analog pcm %d\n", chip->chip_idx);
941                 return err;
942         }
943
944         pcm->private_data = chip;
945
946         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
947         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
948
949         pcm->info_flags = 0;
950         strcpy(pcm->name, name);
951
952         preallocate_buffers(chip, pcm);
953
954         chip->pcm = pcm;
955         return 0;
956 }
957
958
959 /*
960  */
961 static int snd_mixart_pcm_digital(struct snd_mixart *chip)
962 {
963         int err;
964         struct snd_pcm *pcm;
965         char name[32];
966
967         sprintf(name, "miXart AES/EBU %d", chip->chip_idx);
968         if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_DIGITAL,
969                                MIXART_PLAYBACK_STREAMS,
970                                MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
971                 snd_printk(KERN_ERR "cannot create the digital pcm %d\n", chip->chip_idx);
972                 return err;
973         }
974
975         pcm->private_data = chip;
976
977         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
978         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
979
980         pcm->info_flags = 0;
981         strcpy(pcm->name, name);
982
983         preallocate_buffers(chip, pcm);
984
985         chip->pcm_dig = pcm;
986         return 0;
987 }
988
989 static int snd_mixart_chip_free(struct snd_mixart *chip)
990 {
991         kfree(chip);
992         return 0;
993 }
994
995 static int snd_mixart_chip_dev_free(struct snd_device *device)
996 {
997         struct snd_mixart *chip = device->device_data;
998         return snd_mixart_chip_free(chip);
999 }
1000
1001
1002 /*
1003  */
1004 static int __devinit snd_mixart_create(struct mixart_mgr *mgr, struct snd_card *card, int idx)
1005 {
1006         int err;
1007         struct snd_mixart *chip;
1008         static struct snd_device_ops ops = {
1009                 .dev_free = snd_mixart_chip_dev_free,
1010         };
1011
1012         mgr->chip[idx] = chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1013         if (! chip) {
1014                 snd_printk(KERN_ERR "cannot allocate chip\n");
1015                 return -ENOMEM;
1016         }
1017
1018         chip->card = card;
1019         chip->chip_idx = idx;
1020         chip->mgr = mgr;
1021
1022         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1023                 snd_mixart_chip_free(chip);
1024                 return err;
1025         }
1026
1027         snd_card_set_dev(card, &mgr->pci->dev);
1028
1029         return 0;
1030 }
1031
1032 int snd_mixart_create_pcm(struct snd_mixart* chip)
1033 {
1034         int err;
1035
1036         err = snd_mixart_pcm_analog(chip);
1037         if (err < 0)
1038                 return err;
1039
1040         if(chip->mgr->board_type == MIXART_DAUGHTER_TYPE_AES) {
1041
1042                 err = snd_mixart_pcm_digital(chip);
1043                 if (err < 0)
1044                         return err;
1045         }
1046         return err;
1047 }
1048
1049
1050 /*
1051  * release all the cards assigned to a manager instance
1052  */
1053 static int snd_mixart_free(struct mixart_mgr *mgr)
1054 {
1055         unsigned int i;
1056
1057         for (i = 0; i < mgr->num_cards; i++) {
1058                 if (mgr->chip[i])
1059                         snd_card_free(mgr->chip[i]->card);
1060         }
1061
1062         /* stop mailbox */
1063         snd_mixart_exit_mailbox(mgr);
1064
1065         /* release irq  */
1066         if (mgr->irq >= 0)
1067                 free_irq(mgr->irq, (void *)mgr);
1068
1069         /* reset board if some firmware was loaded */
1070         if(mgr->dsp_loaded) {
1071                 snd_mixart_reset_board(mgr);
1072                 snd_printdd("reset miXart !\n");
1073         }
1074
1075         /* release the i/o ports */
1076         for (i = 0; i < 2; i++) {
1077                 if (mgr->mem[i].virt)
1078                         iounmap(mgr->mem[i].virt);
1079         }
1080         pci_release_regions(mgr->pci);
1081
1082         /* free flowarray */
1083         if(mgr->flowinfo.area) {
1084                 snd_dma_free_pages(&mgr->flowinfo);
1085                 mgr->flowinfo.area = NULL;
1086         }
1087         /* free bufferarray */
1088         if(mgr->bufferinfo.area) {
1089                 snd_dma_free_pages(&mgr->bufferinfo);
1090                 mgr->bufferinfo.area = NULL;
1091         }
1092
1093         pci_disable_device(mgr->pci);
1094         kfree(mgr);
1095         return 0;
1096 }
1097
1098 /*
1099  * proc interface
1100  */
1101 static long long snd_mixart_BA0_llseek(struct snd_info_entry *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(struct snd_info_entry *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(struct snd_info_entry *entry, void *file_private_data,
1157                                 struct file *file, char __user *buf,
1158                                 unsigned long count, unsigned long pos)
1159 {
1160         struct mixart_mgr *mgr = entry->private_data;
1161
1162         count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1163         if(count <= 0)
1164                 return 0;
1165         if(pos + count > MIXART_BA0_SIZE)
1166                 count = (long)(MIXART_BA0_SIZE - pos);
1167         if(copy_to_user_fromio(buf, MIXART_MEM( mgr, pos ), count))
1168                 return -EFAULT;
1169         return count;
1170 }
1171
1172 /*
1173   mixart_BA1 proc interface for BAR 1 - read callback
1174  */
1175 static long snd_mixart_BA1_read(struct snd_info_entry *entry, void *file_private_data,
1176                                 struct file *file, char __user *buf,
1177                                 unsigned long count, unsigned long pos)
1178 {
1179         struct mixart_mgr *mgr = entry->private_data;
1180
1181         count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
1182         if(count <= 0)
1183                 return 0;
1184         if(pos + count > MIXART_BA1_SIZE)
1185                 count = (long)(MIXART_BA1_SIZE - pos);
1186         if(copy_to_user_fromio(buf, MIXART_REG( mgr, pos ), count))
1187                 return -EFAULT;
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(struct snd_info_entry *entry, 
1203                                  struct snd_info_buffer *buffer)
1204 {
1205         struct snd_mixart *chip = entry->private_data;        
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->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(struct snd_mixart *chip)
1239 {
1240         struct snd_info_entry *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         struct mixart_mgr *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_set_dma_mask(pci, 0xffffffff) < 0) {
1293                 snd_printk(KERN_ERR "architecture does not support 32bit PCI busmaster DMA\n");
1294                 pci_disable_device(pci);
1295                 return -ENXIO;
1296         }
1297
1298         /*
1299          */
1300         mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
1301         if (! mgr) {
1302                 pci_disable_device(pci);
1303                 return -ENOMEM;
1304         }
1305
1306         mgr->pci = pci;
1307         mgr->irq = -1;
1308
1309         /* resource assignment */
1310         if ((err = pci_request_regions(pci, CARD_NAME)) < 0) {
1311                 kfree(mgr);
1312                 pci_disable_device(pci);
1313                 return err;
1314         }
1315         for (i = 0; i < 2; i++) {
1316                 mgr->mem[i].phys = pci_resource_start(pci, i);
1317                 mgr->mem[i].virt = ioremap_nocache(mgr->mem[i].phys,
1318                                                    pci_resource_len(pci, i));
1319         }
1320
1321         if (request_irq(pci->irq, snd_mixart_interrupt, SA_INTERRUPT|SA_SHIRQ, CARD_NAME, (void *)mgr)) {
1322                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1323                 snd_mixart_free(mgr);
1324                 return -EBUSY;
1325         }
1326         mgr->irq = pci->irq;
1327
1328         sprintf(mgr->shortname, "Digigram miXart");
1329         sprintf(mgr->longname, "%s at 0x%lx & 0x%lx, irq %i", mgr->shortname, mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq);
1330
1331         /* ISR spinlock  */
1332         spin_lock_init(&mgr->lock);
1333
1334         /* init mailbox  */
1335         mgr->msg_fifo_readptr = 0;
1336         mgr->msg_fifo_writeptr = 0;
1337
1338         spin_lock_init(&mgr->msg_lock);
1339         mutex_init(&mgr->msg_mutex);
1340         init_waitqueue_head(&mgr->msg_sleep);
1341         atomic_set(&mgr->msg_processed, 0);
1342
1343         /* init setup mutex*/
1344         mutex_init(&mgr->setup_mutex);
1345
1346         /* init message taslket */
1347         tasklet_init(&mgr->msg_taskq, snd_mixart_msg_tasklet, (unsigned long) mgr);
1348
1349         /* card assignment */
1350         mgr->num_cards = MIXART_MAX_CARDS; /* 4  FIXME: configurable? */
1351         for (i = 0; i < mgr->num_cards; i++) {
1352                 struct snd_card *card;
1353                 char tmpid[16];
1354                 int idx;
1355
1356                 if (index[dev] < 0)
1357                         idx = index[dev];
1358                 else
1359                         idx = index[dev] + i;
1360                 snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : "MIXART", i);
1361                 card = snd_card_new(idx, tmpid, THIS_MODULE, 0);
1362
1363                 if (! card) {
1364                         snd_printk(KERN_ERR "cannot allocate the card %d\n", i);
1365                         snd_mixart_free(mgr);
1366                         return -ENOMEM;
1367                 }
1368
1369                 strcpy(card->driver, CARD_NAME);
1370                 sprintf(card->shortname, "%s [PCM #%d]", mgr->shortname, i);
1371                 sprintf(card->longname, "%s [PCM #%d]", mgr->longname, i);
1372
1373                 if ((err = snd_mixart_create(mgr, card, i)) < 0) {
1374                         snd_mixart_free(mgr);
1375                         return err;
1376                 }
1377
1378                 if(i==0) {
1379                         /* init proc interface only for chip0 */
1380                         snd_mixart_proc_init(mgr->chip[i]);
1381                 }
1382
1383                 if ((err = snd_card_register(card)) < 0) {
1384                         snd_mixart_free(mgr);
1385                         return err;
1386                 }
1387         }
1388
1389         /* init firmware status (mgr->dsp_loaded reset in hwdep_new) */
1390         mgr->board_type = MIXART_DAUGHTER_TYPE_NONE;
1391
1392         /* create array of streaminfo */
1393         size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
1394                             sizeof(struct mixart_flowinfo)) );
1395         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1396                                 size, &mgr->flowinfo) < 0) {
1397                 snd_mixart_free(mgr);
1398                 return -ENOMEM;
1399         }
1400         /* init streaminfo_array */
1401         memset(mgr->flowinfo.area, 0, size);
1402
1403         /* create array of bufferinfo */
1404         size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
1405                             sizeof(struct mixart_bufferinfo)) );
1406         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
1407                                 size, &mgr->bufferinfo) < 0) {
1408                 snd_mixart_free(mgr);
1409                 return -ENOMEM;
1410         }
1411         /* init bufferinfo_array */
1412         memset(mgr->bufferinfo.area, 0, size);
1413
1414         /* set up firmware */
1415         err = snd_mixart_setup_firmware(mgr);
1416         if (err < 0) {
1417                 snd_mixart_free(mgr);
1418                 return err;
1419         }
1420
1421         pci_set_drvdata(pci, mgr);
1422         dev++;
1423         return 0;
1424 }
1425
1426 static void __devexit snd_mixart_remove(struct pci_dev *pci)
1427 {
1428         snd_mixart_free(pci_get_drvdata(pci));
1429         pci_set_drvdata(pci, NULL);
1430 }
1431
1432 static struct pci_driver driver = {
1433         .name = "Digigram miXart",
1434         .id_table = snd_mixart_ids,
1435         .probe = snd_mixart_probe,
1436         .remove = __devexit_p(snd_mixart_remove),
1437 };
1438
1439 static int __init alsa_card_mixart_init(void)
1440 {
1441         return pci_register_driver(&driver);
1442 }
1443
1444 static void __exit alsa_card_mixart_exit(void)
1445 {
1446         pci_unregister_driver(&driver);
1447 }
1448
1449 module_init(alsa_card_mixart_init)
1450 module_exit(alsa_card_mixart_exit)