Linux-2.6.12-rc2
[linux-2.6/next.git] / sound / pci / mixart / mixart_core.c
blobba0027f50944fe06ad56ed0288c984c531a06e92
1 /*
2 * Driver for Digigram miXart soundcards
4 * low level interface with interrupt handling and mail box implementation
6 * Copyright (c) 2003 by Digigram <alsa@digigram.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/driver.h>
24 #include <linux/interrupt.h>
25 #include <asm/io.h>
26 #include <sound/core.h>
27 #include "mixart.h"
28 #include "mixart_hwdep.h"
29 #include "mixart_core.h"
32 #define MSG_TIMEOUT_JIFFIES (400 * HZ) / 1000 /* 400 ms */
34 #define MSG_DESCRIPTOR_SIZE 0x24
35 #define MSG_HEADER_SIZE (MSG_DESCRIPTOR_SIZE + 4)
37 #define MSG_DEFAULT_SIZE 512
39 #define MSG_TYPE_MASK 0x00000003 /* mask for following types */
40 #define MSG_TYPE_NOTIFY 0 /* embedded -> driver (only notification, do not get_msg() !) */
41 #define MSG_TYPE_COMMAND 1 /* driver <-> embedded (a command has no answer) */
42 #define MSG_TYPE_REQUEST 2 /* driver -> embedded (request will get an answer back) */
43 #define MSG_TYPE_ANSWER 3 /* embedded -> driver */
44 #define MSG_CANCEL_NOTIFY_MASK 0x80000000 /* this bit is set for a notification that has been canceled */
47 static int retrieve_msg_frame(mixart_mgr_t *mgr, u32 *msg_frame)
49 /* read the message frame fifo */
50 u32 headptr, tailptr;
52 tailptr = readl_be(MIXART_MEM(mgr, MSG_OUTBOUND_POST_TAIL));
53 headptr = readl_be(MIXART_MEM(mgr, MSG_OUTBOUND_POST_HEAD));
55 if (tailptr == headptr)
56 return 0; /* no message posted */
58 snd_assert( tailptr >= MSG_OUTBOUND_POST_STACK, return 0); /* error */
59 snd_assert( tailptr < (MSG_OUTBOUND_POST_STACK+MSG_BOUND_STACK_SIZE), return 0); /* error */
61 *msg_frame = readl_be(MIXART_MEM(mgr, tailptr));
63 /* increment the tail index */
64 tailptr += 4;
65 if( tailptr >= (MSG_OUTBOUND_POST_STACK+MSG_BOUND_STACK_SIZE) )
66 tailptr = MSG_OUTBOUND_POST_STACK;
67 writel_be(tailptr, MIXART_MEM(mgr, MSG_OUTBOUND_POST_TAIL));
69 return 1;
72 static int get_msg(mixart_mgr_t *mgr, mixart_msg_t *resp, u32 msg_frame_address )
74 unsigned long flags;
75 u32 headptr;
76 u32 size;
77 int err;
78 #ifndef __BIG_ENDIAN
79 unsigned int i;
80 #endif
82 spin_lock_irqsave(&mgr->msg_lock, flags);
83 err = 0;
85 /* copy message descriptor from miXart to driver */
86 size = readl_be(MIXART_MEM(mgr, msg_frame_address)); /* size of descriptor + response */
87 resp->message_id = readl_be(MIXART_MEM(mgr, msg_frame_address + 4)); /* dwMessageID */
88 resp->uid.object_id = readl_be(MIXART_MEM(mgr, msg_frame_address + 8)); /* uidDest */
89 resp->uid.desc = readl_be(MIXART_MEM(mgr, msg_frame_address + 12)); /* */
91 if( (size < MSG_DESCRIPTOR_SIZE) || (resp->size < (size - MSG_DESCRIPTOR_SIZE))) {
92 err = -EINVAL;
93 snd_printk(KERN_ERR "problem with response size = %d\n", size);
94 goto _clean_exit;
96 size -= MSG_DESCRIPTOR_SIZE;
98 memcpy_fromio(resp->data, MIXART_MEM(mgr, msg_frame_address + MSG_HEADER_SIZE ), size);
99 resp->size = size;
101 /* swap if necessary */
102 #ifndef __BIG_ENDIAN
103 size /= 4; /* u32 size */
104 for(i=0; i < size; i++) {
105 ((u32*)resp->data)[i] = be32_to_cpu(((u32*)resp->data)[i]);
107 #endif
110 * free message frame address
112 headptr = readl_be(MIXART_MEM(mgr, MSG_OUTBOUND_FREE_HEAD));
114 if( (headptr < MSG_OUTBOUND_FREE_STACK) || ( headptr >= (MSG_OUTBOUND_FREE_STACK+MSG_BOUND_STACK_SIZE))) {
115 err = -EINVAL;
116 goto _clean_exit;
119 /* give address back to outbound fifo */
120 writel_be(msg_frame_address, MIXART_MEM(mgr, headptr));
122 /* increment the outbound free head */
123 headptr += 4;
124 if( headptr >= (MSG_OUTBOUND_FREE_STACK+MSG_BOUND_STACK_SIZE) )
125 headptr = MSG_OUTBOUND_FREE_STACK;
127 writel_be(headptr, MIXART_MEM(mgr, MSG_OUTBOUND_FREE_HEAD));
129 _clean_exit:
130 spin_unlock_irqrestore(&mgr->msg_lock, flags);
132 return err;
137 * send a message to miXart. return: the msg_frame used for this message
139 /* call with mgr->msg_lock held! */
140 static int send_msg( mixart_mgr_t *mgr,
141 mixart_msg_t *msg,
142 int max_answersize,
143 int mark_pending,
144 u32 *msg_event)
146 u32 headptr, tailptr;
147 u32 msg_frame_address;
148 int err, i;
150 snd_assert(msg->size % 4 == 0, return -EINVAL);
152 err = 0;
154 /* get message frame address */
155 tailptr = readl_be(MIXART_MEM(mgr, MSG_INBOUND_FREE_TAIL));
156 headptr = readl_be(MIXART_MEM(mgr, MSG_INBOUND_FREE_HEAD));
158 if (tailptr == headptr) {
159 snd_printk(KERN_ERR "error: no message frame available\n");
160 return -EBUSY;
163 if( (tailptr < MSG_INBOUND_FREE_STACK) || (tailptr >= (MSG_INBOUND_FREE_STACK+MSG_BOUND_STACK_SIZE))) {
164 return -EINVAL;
167 msg_frame_address = readl_be(MIXART_MEM(mgr, tailptr));
168 writel(0, MIXART_MEM(mgr, tailptr)); /* set address to zero on this fifo position */
170 /* increment the inbound free tail */
171 tailptr += 4;
172 if( tailptr >= (MSG_INBOUND_FREE_STACK+MSG_BOUND_STACK_SIZE) )
173 tailptr = MSG_INBOUND_FREE_STACK;
175 writel_be(tailptr, MIXART_MEM(mgr, MSG_INBOUND_FREE_TAIL));
177 /* TODO : use memcpy_toio() with intermediate buffer to copy the message */
179 /* copy message descriptor to card memory */
180 writel_be( msg->size + MSG_DESCRIPTOR_SIZE, MIXART_MEM(mgr, msg_frame_address) ); /* size of descriptor + request */
181 writel_be( msg->message_id , MIXART_MEM(mgr, msg_frame_address + 4) ); /* dwMessageID */
182 writel_be( msg->uid.object_id, MIXART_MEM(mgr, msg_frame_address + 8) ); /* uidDest */
183 writel_be( msg->uid.desc, MIXART_MEM(mgr, msg_frame_address + 12) ); /* */
184 writel_be( MSG_DESCRIPTOR_SIZE, MIXART_MEM(mgr, msg_frame_address + 16) ); /* SizeHeader */
185 writel_be( MSG_DESCRIPTOR_SIZE, MIXART_MEM(mgr, msg_frame_address + 20) ); /* OffsetDLL_T16 */
186 writel_be( msg->size, MIXART_MEM(mgr, msg_frame_address + 24) ); /* SizeDLL_T16 */
187 writel_be( MSG_DESCRIPTOR_SIZE, MIXART_MEM(mgr, msg_frame_address + 28) ); /* OffsetDLL_DRV */
188 writel_be( 0, MIXART_MEM(mgr, msg_frame_address + 32) ); /* SizeDLL_DRV */
189 writel_be( MSG_DESCRIPTOR_SIZE + max_answersize, MIXART_MEM(mgr, msg_frame_address + 36) ); /* dwExpectedAnswerSize */
191 /* copy message data to card memory */
192 for( i=0; i < msg->size; i+=4 ) {
193 writel_be( *(u32*)(msg->data + i), MIXART_MEM(mgr, MSG_HEADER_SIZE + msg_frame_address + i) );
196 if( mark_pending ) {
197 if( *msg_event ) {
198 /* the pending event is the notification we wait for ! */
199 mgr->pending_event = *msg_event;
201 else {
202 /* the pending event is the answer we wait for (same address than the request)! */
203 mgr->pending_event = msg_frame_address;
205 /* copy address back to caller */
206 *msg_event = msg_frame_address;
210 /* mark the frame as a request (will have an answer) */
211 msg_frame_address |= MSG_TYPE_REQUEST;
213 /* post the frame */
214 headptr = readl_be(MIXART_MEM(mgr, MSG_INBOUND_POST_HEAD));
216 if( (headptr < MSG_INBOUND_POST_STACK) || (headptr >= (MSG_INBOUND_POST_STACK+MSG_BOUND_STACK_SIZE))) {
217 return -EINVAL;
220 writel_be(msg_frame_address, MIXART_MEM(mgr, headptr));
222 /* increment the inbound post head */
223 headptr += 4;
224 if( headptr >= (MSG_INBOUND_POST_STACK+MSG_BOUND_STACK_SIZE) )
225 headptr = MSG_INBOUND_POST_STACK;
227 writel_be(headptr, MIXART_MEM(mgr, MSG_INBOUND_POST_HEAD));
229 return 0;
233 int snd_mixart_send_msg(mixart_mgr_t *mgr, mixart_msg_t *request, int max_resp_size, void *resp_data)
235 mixart_msg_t resp;
236 u32 msg_frame = 0; /* set to 0, so it's no notification to wait for, but the answer */
237 int err;
238 wait_queue_t wait;
239 long timeout;
241 down(&mgr->msg_mutex);
243 init_waitqueue_entry(&wait, current);
245 spin_lock_irq(&mgr->msg_lock);
246 /* send the message */
247 err = send_msg(mgr, request, max_resp_size, 1, &msg_frame); /* send and mark the answer pending */
248 if (err) {
249 spin_unlock_irq(&mgr->msg_lock);
250 up(&mgr->msg_mutex);
251 return err;
254 set_current_state(TASK_UNINTERRUPTIBLE);
255 add_wait_queue(&mgr->msg_sleep, &wait);
256 spin_unlock_irq(&mgr->msg_lock);
257 timeout = schedule_timeout(MSG_TIMEOUT_JIFFIES);
258 remove_wait_queue(&mgr->msg_sleep, &wait);
260 if (! timeout) {
261 /* error - no ack */
262 up(&mgr->msg_mutex);
263 snd_printk(KERN_ERR "error: no reponse on msg %x\n", msg_frame);
264 return -EIO;
267 /* retrieve the answer into the same mixart_msg_t */
268 resp.message_id = 0;
269 resp.uid = (mixart_uid_t){0,0};
270 resp.data = resp_data;
271 resp.size = max_resp_size;
273 err = get_msg(mgr, &resp, msg_frame);
275 if( request->message_id != resp.message_id )
276 snd_printk(KERN_ERR "REPONSE ERROR!\n");
278 up(&mgr->msg_mutex);
279 return err;
283 int snd_mixart_send_msg_wait_notif(mixart_mgr_t *mgr, mixart_msg_t *request, u32 notif_event)
285 int err;
286 wait_queue_t wait;
287 long timeout;
289 snd_assert(notif_event != 0, return -EINVAL);
290 snd_assert((notif_event & MSG_TYPE_MASK) == MSG_TYPE_NOTIFY, return -EINVAL);
291 snd_assert((notif_event & MSG_CANCEL_NOTIFY_MASK) == 0, return -EINVAL);
293 down(&mgr->msg_mutex);
295 init_waitqueue_entry(&wait, current);
297 spin_lock_irq(&mgr->msg_lock);
298 /* send the message */
299 err = send_msg(mgr, request, MSG_DEFAULT_SIZE, 1, &notif_event); /* send and mark the notification event pending */
300 if(err) {
301 spin_unlock_irq(&mgr->msg_lock);
302 up(&mgr->msg_mutex);
303 return err;
306 set_current_state(TASK_UNINTERRUPTIBLE);
307 add_wait_queue(&mgr->msg_sleep, &wait);
308 spin_unlock_irq(&mgr->msg_lock);
309 timeout = schedule_timeout(MSG_TIMEOUT_JIFFIES);
310 remove_wait_queue(&mgr->msg_sleep, &wait);
312 if (! timeout) {
313 /* error - no ack */
314 up(&mgr->msg_mutex);
315 snd_printk(KERN_ERR "error: notification %x not received\n", notif_event);
316 return -EIO;
319 up(&mgr->msg_mutex);
320 return 0;
324 int snd_mixart_send_msg_nonblock(mixart_mgr_t *mgr, mixart_msg_t *request)
326 u32 message_frame;
327 unsigned long flags;
328 int err;
330 /* just send the message (do not mark it as a pending one) */
331 spin_lock_irqsave(&mgr->msg_lock, flags);
332 err = send_msg(mgr, request, MSG_DEFAULT_SIZE, 0, &message_frame);
333 spin_unlock_irqrestore(&mgr->msg_lock, flags);
335 /* the answer will be handled by snd_mixart_msg_tasklet() */
336 atomic_inc(&mgr->msg_processed);
338 return err;
342 /* common buffer of tasklet and interrupt to send/receive messages */
343 static u32 mixart_msg_data[MSG_DEFAULT_SIZE / 4];
346 void snd_mixart_msg_tasklet( unsigned long arg)
348 mixart_mgr_t *mgr = ( mixart_mgr_t*)(arg);
349 mixart_msg_t resp;
350 u32 msg, addr, type;
351 int err;
353 spin_lock(&mgr->lock);
355 while (mgr->msg_fifo_readptr != mgr->msg_fifo_writeptr) {
356 msg = mgr->msg_fifo[mgr->msg_fifo_readptr];
357 mgr->msg_fifo_readptr++;
358 mgr->msg_fifo_readptr %= MSG_FIFO_SIZE;
360 /* process the message ... */
361 addr = msg & ~MSG_TYPE_MASK;
362 type = msg & MSG_TYPE_MASK;
364 switch (type) {
365 case MSG_TYPE_ANSWER:
366 /* answer to a message on that we did not wait for (send_msg_nonblock) */
367 resp.message_id = 0;
368 resp.data = mixart_msg_data;
369 resp.size = sizeof(mixart_msg_data);
370 err = get_msg(mgr, &resp, addr);
371 if( err < 0 ) {
372 snd_printk(KERN_ERR "tasklet: error(%d) reading mf %x\n", err, msg);
373 break;
376 switch(resp.message_id) {
377 case MSG_STREAM_START_INPUT_STAGE_PACKET:
378 case MSG_STREAM_START_OUTPUT_STAGE_PACKET:
379 case MSG_STREAM_STOP_INPUT_STAGE_PACKET:
380 case MSG_STREAM_STOP_OUTPUT_STAGE_PACKET:
381 if(mixart_msg_data[0])
382 snd_printk(KERN_ERR "tasklet : error MSG_STREAM_ST***_***PUT_STAGE_PACKET status=%x\n", mixart_msg_data[0]);
383 break;
384 default:
385 snd_printdd("tasklet received mf(%x) : msg_id(%x) uid(%x, %x) size(%zd)\n",
386 msg, resp.message_id, resp.uid.object_id, resp.uid.desc, resp.size);
387 break;
389 break;
390 case MSG_TYPE_NOTIFY:
391 /* msg contains no address ! do not get_msg() ! */
392 case MSG_TYPE_COMMAND:
393 /* get_msg() necessary */
394 default:
395 snd_printk(KERN_ERR "tasklet doesn't know what to do with message %x\n", msg);
396 } /* switch type */
398 /* decrement counter */
399 atomic_dec(&mgr->msg_processed);
401 } /* while there is a msg in fifo */
403 spin_unlock(&mgr->lock);
407 irqreturn_t snd_mixart_interrupt(int irq, void *dev_id, struct pt_regs *regs)
409 mixart_mgr_t *mgr = dev_id;
410 int err;
411 mixart_msg_t resp;
413 u32 msg;
414 u32 it_reg;
416 spin_lock(&mgr->lock);
418 it_reg = readl_le(MIXART_REG(mgr, MIXART_PCI_OMISR_OFFSET));
419 if( !(it_reg & MIXART_OIDI) ) {
420 /* this device did not cause the interrupt */
421 spin_unlock(&mgr->lock);
422 return IRQ_NONE;
425 /* mask all interrupts */
426 writel_le(MIXART_HOST_ALL_INTERRUPT_MASKED, MIXART_REG(mgr, MIXART_PCI_OMIMR_OFFSET));
428 /* outdoorbell register clear */
429 it_reg = readl(MIXART_REG(mgr, MIXART_PCI_ODBR_OFFSET));
430 writel(it_reg, MIXART_REG(mgr, MIXART_PCI_ODBR_OFFSET));
432 /* clear interrupt */
433 writel_le( MIXART_OIDI, MIXART_REG(mgr, MIXART_PCI_OMISR_OFFSET) );
435 /* process interrupt */
436 while (retrieve_msg_frame(mgr, &msg)) {
438 switch (msg & MSG_TYPE_MASK) {
439 case MSG_TYPE_COMMAND:
440 resp.message_id = 0;
441 resp.data = mixart_msg_data;
442 resp.size = sizeof(mixart_msg_data);
443 err = get_msg(mgr, &resp, msg & ~MSG_TYPE_MASK);
444 if( err < 0 ) {
445 snd_printk(KERN_ERR "interrupt: error(%d) reading mf %x\n", err, msg);
446 break;
449 if(resp.message_id == MSG_SERVICES_TIMER_NOTIFY) {
450 int i;
451 mixart_timer_notify_t *notify = (mixart_timer_notify_t*)mixart_msg_data;
453 for(i=0; i<notify->stream_count; i++) {
455 u32 buffer_id = notify->streams[i].buffer_id;
456 unsigned int chip_number = (buffer_id & MIXART_NOTIFY_CARD_MASK) >> MIXART_NOTIFY_CARD_OFFSET; /* card0 to 3 */
457 unsigned int pcm_number = (buffer_id & MIXART_NOTIFY_PCM_MASK ) >> MIXART_NOTIFY_PCM_OFFSET; /* pcm0 to 3 */
458 unsigned int sub_number = buffer_id & MIXART_NOTIFY_SUBS_MASK; /* 0 to MIXART_PLAYBACK_STREAMS */
459 unsigned int is_capture = ((buffer_id & MIXART_NOTIFY_CAPT_MASK) != 0); /* playback == 0 / capture == 1 */
461 mixart_t *chip = mgr->chip[chip_number];
462 mixart_stream_t *stream;
464 if ((chip_number >= mgr->num_cards) || (pcm_number >= MIXART_PCM_TOTAL) || (sub_number >= MIXART_PLAYBACK_STREAMS)) {
465 snd_printk(KERN_ERR "error MSG_SERVICES_TIMER_NOTIFY buffer_id (%x) pos(%d)\n",
466 buffer_id, notify->streams[i].sample_pos_low_part);
467 break;
470 if (is_capture)
471 stream = &chip->capture_stream[pcm_number];
472 else
473 stream = &chip->playback_stream[pcm_number][sub_number];
475 if (stream->substream && (stream->status == MIXART_STREAM_STATUS_RUNNING)) {
476 snd_pcm_runtime_t *runtime = stream->substream->runtime;
477 int elapsed = 0;
478 u64 sample_count = ((u64)notify->streams[i].sample_pos_high_part) << 32;
479 sample_count |= notify->streams[i].sample_pos_low_part;
481 while (1) {
482 u64 new_elapse_pos = stream->abs_period_elapsed + runtime->period_size;
484 if (new_elapse_pos > sample_count) {
485 break; /* while */
487 else {
488 elapsed = 1;
489 stream->buf_periods++;
490 if (stream->buf_periods >= runtime->periods)
491 stream->buf_periods = 0;
493 stream->abs_period_elapsed = new_elapse_pos;
496 stream->buf_period_frag = (u32)( sample_count - stream->abs_period_elapsed );
498 if(elapsed) {
499 spin_unlock(&mgr->lock);
500 snd_pcm_period_elapsed(stream->substream);
501 spin_lock(&mgr->lock);
505 break;
507 if(resp.message_id == MSG_SERVICES_REPORT_TRACES) {
508 if(resp.size > 1) {
509 #ifndef __BIG_ENDIAN
510 /* Traces are text: the swapped msg_data has to be swapped back ! */
511 int i;
512 for(i=0; i<(resp.size/4); i++) {
513 (mixart_msg_data)[i] = cpu_to_be32((mixart_msg_data)[i]);
515 #endif
516 ((char*)mixart_msg_data)[resp.size - 1] = 0;
517 snd_printdd("MIXART TRACE : %s\n", (char*)mixart_msg_data);
519 break;
522 snd_printdd("command %x not handled\n", resp.message_id);
523 break;
525 case MSG_TYPE_NOTIFY:
526 if(msg & MSG_CANCEL_NOTIFY_MASK) {
527 msg &= ~MSG_CANCEL_NOTIFY_MASK;
528 snd_printk(KERN_ERR "canceled notification %x !\n", msg);
530 /* no break, continue ! */
531 case MSG_TYPE_ANSWER:
532 /* answer or notification to a message we are waiting for*/
533 spin_lock(&mgr->msg_lock);
534 if( (msg & ~MSG_TYPE_MASK) == mgr->pending_event ) {
535 wake_up(&mgr->msg_sleep);
536 mgr->pending_event = 0;
538 /* answer to a message we did't want to wait for */
539 else {
540 mgr->msg_fifo[mgr->msg_fifo_writeptr] = msg;
541 mgr->msg_fifo_writeptr++;
542 mgr->msg_fifo_writeptr %= MSG_FIFO_SIZE;
543 tasklet_hi_schedule(&mgr->msg_taskq);
545 spin_unlock(&mgr->msg_lock);
546 break;
547 case MSG_TYPE_REQUEST:
548 default:
549 snd_printdd("interrupt received request %x\n", msg);
550 /* TODO : are there things to do here ? */
551 break;
552 } /* switch on msg type */
553 } /* while there are msgs */
555 /* allow interrupt again */
556 writel_le( MIXART_ALLOW_OUTBOUND_DOORBELL, MIXART_REG( mgr, MIXART_PCI_OMIMR_OFFSET));
558 spin_unlock(&mgr->lock);
560 return IRQ_HANDLED;
564 void snd_mixart_init_mailbox(mixart_mgr_t *mgr)
566 writel( 0, MIXART_MEM( mgr, MSG_HOST_RSC_PROTECTION ) );
567 writel( 0, MIXART_MEM( mgr, MSG_AGENT_RSC_PROTECTION ) );
569 /* allow outbound messagebox to generate interrupts */
570 if(mgr->irq >= 0) {
571 writel_le( MIXART_ALLOW_OUTBOUND_DOORBELL, MIXART_REG( mgr, MIXART_PCI_OMIMR_OFFSET));
573 return;
576 void snd_mixart_exit_mailbox(mixart_mgr_t *mgr)
578 /* no more interrupts on outbound messagebox */
579 writel_le( MIXART_HOST_ALL_INTERRUPT_MASKED, MIXART_REG( mgr, MIXART_PCI_OMIMR_OFFSET));
580 return;
583 void snd_mixart_reset_board(mixart_mgr_t *mgr)
585 /* reset miXart */
586 writel_be( 1, MIXART_REG(mgr, MIXART_BA1_BRUTAL_RESET_OFFSET) );
587 return;