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