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