1 /*******************************************************************************
3 * "cs4281.c" -- Cirrus Logic-Crystal CS4281 linux audio driver.
5 * Copyright (C) 2000,2001 Cirrus Logic Corp.
6 * -- adapted from drivers by Thomas Sailer,
7 * -- but don't bug him; Problems should go to:
8 * -- tom woller (twoller@crystal.cirrus.com) or
9 * (audio@crystal.cirrus.com).
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Module command line parameters:
29 * /dev/dsp standard /dev/dsp device, (mostly) OSS compatible
30 * /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
31 * /dev/midi simple MIDI UART interface, no ioctl
33 * Modification History
34 * 08/20/00 trw - silence and no stopping DAC until release
35 * 08/23/00 trw - added CS_DBG statements, fix interrupt hang issue on DAC stop.
36 * 09/18/00 trw - added 16bit only record with conversion
37 * 09/24/00 trw - added Enhanced Full duplex (separate simultaneous
38 * capture/playback rates)
39 * 10/03/00 trw - fixed mmap (fixed GRECORD and the XMMS mmap test plugin
41 * 10/11/00 trw - modified for 2.4.0-test9 kernel enhancements (NR_MAP removal)
42 * 11/03/00 trw - fixed interrupt loss/stutter, added debug.
43 * 11/10/00 bkz - added __devinit to cs4281_hw_init()
44 * 11/10/00 trw - fixed SMP and capture spinlock hang.
45 * 12/04/00 trw - cleaned up CSDEBUG flags and added "defaultorder" moduleparm.
46 * 12/05/00 trw - fixed polling (myth2), and added underrun swptr fix.
47 * 12/08/00 trw - added PM support.
48 * 12/14/00 trw - added wrapper code, builds under 2.4.0, 2.2.17-20, 2.2.17-8
49 * (RH/Dell base), 2.2.18, 2.2.12. cleaned up code mods by ident.
50 * 12/19/00 trw - added PM support for 2.2 base (apm_callback). other PM cleanup.
51 * 12/21/00 trw - added fractional "defaultorder" inputs. if >100 then use
52 * defaultorder-100 as power of 2 for the buffer size. example:
53 * 106 = 2^(106-100) = 2^6 = 64 bytes for the buffer size.
55 *******************************************************************************/
57 /* uncomment the following line to disable building PM support into the driver */
58 //#define NOT_CS4281_PM 1
60 #include <linux/list.h>
61 #include <linux/module.h>
62 #include <linux/string.h>
63 #include <linux/ioport.h>
64 #include <linux/sched.h>
65 #include <linux/delay.h>
66 #include <linux/sound.h>
67 #include <linux/slab.h>
68 #include <linux/soundcard.h>
69 #include <linux/pci.h>
70 #include <linux/bitops.h>
71 #include <linux/init.h>
72 #include <linux/interrupt.h>
73 #include <linux/poll.h>
75 #include <linux/wait.h>
77 #include <asm/current.h>
81 #include <asm/uaccess.h>
84 #include "cs4281_hwdefs.h"
89 static void stop_dac(struct cs4281_state *s);
90 static void stop_adc(struct cs4281_state *s);
91 static void start_dac(struct cs4281_state *s);
92 static void start_adc(struct cs4281_state *s);
93 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
95 // ---------------------------------------------------------------------
97 #ifndef PCI_VENDOR_ID_CIRRUS
98 #define PCI_VENDOR_ID_CIRRUS 0x1013
100 #ifndef PCI_DEVICE_ID_CRYSTAL_CS4281
101 #define PCI_DEVICE_ID_CRYSTAL_CS4281 0x6005
104 #define CS4281_MAGIC ((PCI_DEVICE_ID_CRYSTAL_CS4281<<16) | PCI_VENDOR_ID_CIRRUS)
105 #define CS4281_CFLR_DEFAULT 0x00000001 /* CFLR must be in AC97 link mode */
107 // buffer order determines the size of the dma buffer for the driver.
108 // under Linux, a smaller buffer allows more responsiveness from many of the
109 // applications (e.g. games). A larger buffer allows some of the apps (esound)
110 // to not underrun the dma buffer as easily. As default, use 32k (order=3)
111 // rather than 64k as some of the games work more responsively.
112 // log base 2( buff sz = 32k).
113 static unsigned long defaultorder = 3;
114 module_param(defaultorder, ulong, 0);
117 // Turn on/off debugging compilation by commenting out "#define CSDEBUG"
121 #define CSDEBUG_INTERFACE 1
123 #undef CSDEBUG_INTERFACE
126 // cs_debugmask areas
128 #define CS_INIT 0x00000001 // initialization and probe functions
129 #define CS_ERROR 0x00000002 // tmp debugging bit placeholder
130 #define CS_INTERRUPT 0x00000004 // interrupt handler (separate from all other)
131 #define CS_FUNCTION 0x00000008 // enter/leave functions
132 #define CS_WAVE_WRITE 0x00000010 // write information for wave
133 #define CS_WAVE_READ 0x00000020 // read information for wave
134 #define CS_MIDI_WRITE 0x00000040 // write information for midi
135 #define CS_MIDI_READ 0x00000080 // read information for midi
136 #define CS_MPU401_WRITE 0x00000100 // write information for mpu401
137 #define CS_MPU401_READ 0x00000200 // read information for mpu401
138 #define CS_OPEN 0x00000400 // all open functions in the driver
139 #define CS_RELEASE 0x00000800 // all release functions in the driver
140 #define CS_PARMS 0x00001000 // functional and operational parameters
141 #define CS_IOCTL 0x00002000 // ioctl (non-mixer)
142 #define CS_PM 0x00004000 // power management
143 #define CS_TMP 0x10000000 // tmp debug mask bit
145 #define CS_IOCTL_CMD_SUSPEND 0x1 // suspend
146 #define CS_IOCTL_CMD_RESUME 0x2 // resume
148 // CSDEBUG is usual mode is set to 1, then use the
149 // cs_debuglevel and cs_debugmask to turn on or off debugging.
150 // Debug level of 1 has been defined to be kernel errors and info
151 // that should be printed on any released driver.
154 #define CS_DBGOUT(mask,level,x) if((cs_debuglevel >= (level)) && ((mask) & cs_debugmask) ) {x;}
156 #define CS_DBGOUT(mask,level,x)
160 static unsigned long cs_debuglevel = 1; // levels range from 1-9
161 static unsigned long cs_debugmask = CS_INIT | CS_ERROR; // use CS_DBGOUT with various mask values
162 module_param(cs_debuglevel, ulong, 0);
163 module_param(cs_debugmask, ulong, 0);
169 #define MIDIINBUF 500
170 #define MIDIOUTBUF 500
172 #define FMODE_MIDI_SHIFT 3
173 #define FMODE_MIDI_READ (FMODE_READ << FMODE_MIDI_SHIFT)
174 #define FMODE_MIDI_WRITE (FMODE_WRITE << FMODE_MIDI_SHIFT)
176 #define CS4281_MAJOR_VERSION 1
177 #define CS4281_MINOR_VERSION 13
179 #define CS4281_ARCH 64 //architecture key
181 #define CS4281_ARCH 32 //architecture key
184 #define CS_TYPE_ADC 0
185 #define CS_TYPE_DAC 1
188 static const char invalid_magic[] =
189 KERN_CRIT "cs4281: invalid magic value\n";
191 #define VALIDATE_STATE(s) \
193 if (!(s) || (s)->magic != CS4281_MAGIC) { \
194 printk(invalid_magic); \
199 //LIST_HEAD(cs4281_devs);
200 static struct list_head cs4281_devs = { &cs4281_devs, &cs4281_devs };
204 #include "cs4281_wrapper-24.c"
206 struct cs4281_state {
210 // we keep the cards in a linked list
211 struct cs4281_state *next;
213 // pcidev is needed to turn off the DDMA controller at driver shutdown
214 struct pci_dev *pcidev;
215 struct list_head list;
222 // hardware resources
223 unsigned int pBA0phys, pBA1phys;
230 unsigned short vol[10];
233 unsigned short micpreamp;
239 unsigned fmt_original; // original requested format
242 unsigned char clkdiv;
243 } prop_dac, prop_adc;
244 unsigned conversion:1; // conversion from 16 to 8 bit in progress
245 void *tmpbuff; // tmp buffer for sample conversions
248 struct semaphore open_sem;
249 struct semaphore open_sem_adc;
250 struct semaphore open_sem_dac;
252 wait_queue_head_t open_wait;
253 wait_queue_head_t open_wait_adc;
254 wait_queue_head_t open_wait_dac;
256 dma_addr_t dmaaddr_tmpbuff;
257 unsigned buforder_tmpbuff; // Log base 2 of 'rawbuf' size in bytes..
259 void *rawbuf; // Physical address of
261 unsigned buforder; // Log base 2 of 'rawbuf' size in bytes..
262 unsigned numfrag; // # of 'fragments' in the buffer.
263 unsigned fragshift; // Log base 2 of fragment size.
264 unsigned hwptr, swptr;
265 unsigned total_bytes; // # bytes process since open.
266 unsigned blocks; // last returned blocks value GETOPTR
267 unsigned wakeup; // interrupt occurred on block
269 unsigned underrun; // underrun flag
270 unsigned error; // over/underrun
271 wait_queue_head_t wait;
272 // redundant, but makes calculations easier
273 unsigned fragsize; // 2**fragshift..
274 unsigned dmasize; // 2**buforder.
275 unsigned fragsamples;
277 unsigned mapped:1; // Buffer mapped in cs4281_mmap()?
278 unsigned ready:1; // prog_dmabuf_dac()/adc() successful?
279 unsigned endcleared:1;
280 unsigned type:1; // adc or dac buffer (CS_TYPE_XXX)
281 unsigned ossfragshift;
283 unsigned subdivision;
288 unsigned ird, iwr, icnt;
289 unsigned ord, owr, ocnt;
290 wait_queue_head_t iwait;
291 wait_queue_head_t owait;
292 struct timer_list timer;
293 unsigned char ibuf[MIDIINBUF];
294 unsigned char obuf[MIDIOUTBUF];
298 struct cs4281_pipeline pl[CS4281_NUMBER_OF_PIPELINES];
301 #include <linux/pm_legacy.h>
302 #include "cs4281pm-24.c"
308 #define SOUND_MIXER_CS_GETDBGLEVEL _SIOWR('M',120, int)
309 #define SOUND_MIXER_CS_SETDBGLEVEL _SIOWR('M',121, int)
310 #define SOUND_MIXER_CS_GETDBGMASK _SIOWR('M',122, int)
311 #define SOUND_MIXER_CS_SETDBGMASK _SIOWR('M',123, int)
313 #define SOUND_MIXER_CS_APM _SIOWR('M',124, int)
316 static void cs_printioctl(unsigned int x)
320 // Index of mixtable1[] member is Device ID
321 // and must be <= SOUND_MIXER_NRDEVICES.
322 // Value of array member is index into s->mix.vol[]
323 static const unsigned char mixtable1[SOUND_MIXER_NRDEVICES] = {
324 [SOUND_MIXER_PCM] = 1, // voice
325 [SOUND_MIXER_LINE1] = 2, // AUX
326 [SOUND_MIXER_CD] = 3, // CD
327 [SOUND_MIXER_LINE] = 4, // Line
328 [SOUND_MIXER_SYNTH] = 5, // FM
329 [SOUND_MIXER_MIC] = 6, // Mic
330 [SOUND_MIXER_SPEAKER] = 7, // Speaker
331 [SOUND_MIXER_RECLEV] = 8, // Recording level
332 [SOUND_MIXER_VOLUME] = 9 // Master Volume
336 case SOUND_MIXER_CS_GETDBGMASK:
337 CS_DBGOUT(CS_IOCTL, 4,
338 printk("SOUND_MIXER_CS_GETDBGMASK:\n"));
340 case SOUND_MIXER_CS_GETDBGLEVEL:
341 CS_DBGOUT(CS_IOCTL, 4,
342 printk("SOUND_MIXER_CS_GETDBGLEVEL:\n"));
344 case SOUND_MIXER_CS_SETDBGMASK:
345 CS_DBGOUT(CS_IOCTL, 4,
346 printk("SOUND_MIXER_CS_SETDBGMASK:\n"));
348 case SOUND_MIXER_CS_SETDBGLEVEL:
349 CS_DBGOUT(CS_IOCTL, 4,
350 printk("SOUND_MIXER_CS_SETDBGLEVEL:\n"));
353 CS_DBGOUT(CS_IOCTL, 4, printk("OSS_GETVERSION:\n"));
355 case SNDCTL_DSP_SYNC:
356 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SYNC:\n"));
358 case SNDCTL_DSP_SETDUPLEX:
359 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETDUPLEX:\n"));
361 case SNDCTL_DSP_GETCAPS:
362 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETCAPS:\n"));
364 case SNDCTL_DSP_RESET:
365 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_RESET:\n"));
367 case SNDCTL_DSP_SPEED:
368 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SPEED:\n"));
370 case SNDCTL_DSP_STEREO:
371 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_STEREO:\n"));
373 case SNDCTL_DSP_CHANNELS:
374 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CHANNELS:\n"));
376 case SNDCTL_DSP_GETFMTS:
377 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETFMTS:\n"));
379 case SNDCTL_DSP_SETFMT:
380 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFMT:\n"));
382 case SNDCTL_DSP_POST:
383 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_POST:\n"));
385 case SNDCTL_DSP_GETTRIGGER:
386 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETTRIGGER:\n"));
388 case SNDCTL_DSP_SETTRIGGER:
389 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETTRIGGER:\n"));
391 case SNDCTL_DSP_GETOSPACE:
392 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOSPACE:\n"));
394 case SNDCTL_DSP_GETISPACE:
395 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETISPACE:\n"));
397 case SNDCTL_DSP_NONBLOCK:
398 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_NONBLOCK:\n"));
400 case SNDCTL_DSP_GETODELAY:
401 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETODELAY:\n"));
403 case SNDCTL_DSP_GETIPTR:
404 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETIPTR:\n"));
406 case SNDCTL_DSP_GETOPTR:
407 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOPTR:\n"));
409 case SNDCTL_DSP_GETBLKSIZE:
410 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETBLKSIZE:\n"));
412 case SNDCTL_DSP_SETFRAGMENT:
413 CS_DBGOUT(CS_IOCTL, 4,
414 printk("SNDCTL_DSP_SETFRAGMENT:\n"));
416 case SNDCTL_DSP_SUBDIVIDE:
417 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SUBDIVIDE:\n"));
419 case SOUND_PCM_READ_RATE:
420 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_RATE:\n"));
422 case SOUND_PCM_READ_CHANNELS:
423 CS_DBGOUT(CS_IOCTL, 4,
424 printk("SOUND_PCM_READ_CHANNELS:\n"));
426 case SOUND_PCM_READ_BITS:
427 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_BITS:\n"));
429 case SOUND_PCM_WRITE_FILTER:
430 CS_DBGOUT(CS_IOCTL, 4,
431 printk("SOUND_PCM_WRITE_FILTER:\n"));
433 case SNDCTL_DSP_SETSYNCRO:
434 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETSYNCRO:\n"));
436 case SOUND_PCM_READ_FILTER:
437 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_FILTER:\n"));
439 case SOUND_MIXER_PRIVATE1:
440 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE1:\n"));
442 case SOUND_MIXER_PRIVATE2:
443 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE2:\n"));
445 case SOUND_MIXER_PRIVATE3:
446 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE3:\n"));
448 case SOUND_MIXER_PRIVATE4:
449 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE4:\n"));
451 case SOUND_MIXER_PRIVATE5:
452 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE5:\n"));
454 case SOUND_MIXER_INFO:
455 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_INFO:\n"));
457 case SOUND_OLD_MIXER_INFO:
458 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_OLD_MIXER_INFO:\n"));
462 switch (_IOC_NR(x)) {
463 case SOUND_MIXER_VOLUME:
464 CS_DBGOUT(CS_IOCTL, 4,
465 printk("SOUND_MIXER_VOLUME:\n"));
467 case SOUND_MIXER_SPEAKER:
468 CS_DBGOUT(CS_IOCTL, 4,
469 printk("SOUND_MIXER_SPEAKER:\n"));
471 case SOUND_MIXER_RECLEV:
472 CS_DBGOUT(CS_IOCTL, 4,
473 printk("SOUND_MIXER_RECLEV:\n"));
475 case SOUND_MIXER_MIC:
476 CS_DBGOUT(CS_IOCTL, 4,
477 printk("SOUND_MIXER_MIC:\n"));
479 case SOUND_MIXER_SYNTH:
480 CS_DBGOUT(CS_IOCTL, 4,
481 printk("SOUND_MIXER_SYNTH:\n"));
483 case SOUND_MIXER_RECSRC:
484 CS_DBGOUT(CS_IOCTL, 4,
485 printk("SOUND_MIXER_RECSRC:\n"));
487 case SOUND_MIXER_DEVMASK:
488 CS_DBGOUT(CS_IOCTL, 4,
489 printk("SOUND_MIXER_DEVMASK:\n"));
491 case SOUND_MIXER_RECMASK:
492 CS_DBGOUT(CS_IOCTL, 4,
493 printk("SOUND_MIXER_RECMASK:\n"));
495 case SOUND_MIXER_STEREODEVS:
496 CS_DBGOUT(CS_IOCTL, 4,
497 printk("SOUND_MIXER_STEREODEVS:\n"));
499 case SOUND_MIXER_CAPS:
500 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CAPS:\n"));
504 if (i >= SOUND_MIXER_NRDEVICES
505 || !(vidx = mixtable1[i])) {
506 CS_DBGOUT(CS_IOCTL, 4, printk
507 ("UNKNOWN IOCTL: 0x%.8x NR=%d\n",
510 CS_DBGOUT(CS_IOCTL, 4, printk
511 ("SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d\n",
519 static int prog_dmabuf_adc(struct cs4281_state *s);
520 static void prog_codec(struct cs4281_state *s, unsigned type);
522 // ---------------------------------------------------------------------
524 // Hardware Interfaces For the CS4281
528 //******************************************************************************
529 // "delayus()-- Delay for the specified # of microseconds.
530 //******************************************************************************
531 static void delayus(struct cs4281_state *s, u32 delay)
534 if ((delay > 9999) && (s->pm.flags & CS4281_PM_IDLE)) {
535 j = (delay * HZ) / 1000000; /* calculate delay in jiffies */
537 j = 1; /* minimum one jiffy. */
538 current->state = TASK_UNINTERRUPTIBLE;
546 //******************************************************************************
547 // "cs4281_read_ac97" -- Reads a word from the specified location in the
548 // CS4281's address space(based on the BA0 register).
550 // 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
551 // 2. Write ACCDA = Command Data Register = 470h for data to write to AC97 register,
553 // 3. Write ACCTL = Control Register = 460h for initiating the write
554 // 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
555 // 5. if DCV not cleared, break and return error
556 // 6. Read ACSTS = Status Register = 464h, check VSTS bit
557 //****************************************************************************
558 static int cs4281_read_ac97(struct cs4281_state *card, u32 offset,
563 // Make sure that there is not data sitting
564 // around from a previous uncompleted access.
565 // ACSDA = Status Data Register = 47Ch
566 status = readl(card->pBA0 + BA0_ACSDA);
568 // Setup the AC97 control registers on the CS4281 to send the
569 // appropriate command to the AC97 to perform the read.
570 // ACCAD = Command Address Register = 46Ch
571 // ACCDA = Command Data Register = 470h
572 // ACCTL = Control Register = 460h
573 // bit DCV - will clear when process completed
574 // bit CRW - Read command
575 // bit VFRM - valid frame enabled
576 // bit ESYN - ASYNC generation enabled
578 // Get the actual AC97 register from the offset
579 writel(offset - BA0_AC97_RESET, card->pBA0 + BA0_ACCAD);
580 writel(0, card->pBA0 + BA0_ACCDA);
581 writel(ACCTL_DCV | ACCTL_CRW | ACCTL_VFRM | ACCTL_ESYN,
582 card->pBA0 + BA0_ACCTL);
584 // Wait for the read to occur.
585 for (count = 0; count < 10; count++) {
586 // First, we want to wait for a short time.
589 // Now, check to see if the read has completed.
590 // ACCTL = 460h, DCV should be reset by now and 460h = 17h
591 if (!(readl(card->pBA0 + BA0_ACCTL) & ACCTL_DCV))
595 // Make sure the read completed.
596 if (readl(card->pBA0 + BA0_ACCTL) & ACCTL_DCV)
599 // Wait for the valid status bit to go active.
600 for (count = 0; count < 10; count++) {
601 // Read the AC97 status register.
602 // ACSTS = Status Register = 464h
603 status = readl(card->pBA0 + BA0_ACSTS);
605 // See if we have valid status.
606 // VSTS - Valid Status
607 if (status & ACSTS_VSTS)
609 // Wait for a short while.
613 // Make sure we got valid status.
614 if (!(status & ACSTS_VSTS))
617 // Read the data returned from the AC97 register.
618 // ACSDA = Status Data Register = 474h
619 *value = readl(card->pBA0 + BA0_ACSDA);
626 //****************************************************************************
628 // "cs4281_write_ac97()"-- writes a word to the specified location in the
629 // CS461x's address space (based on the part's base address zero register).
631 // 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
632 // 2. Write ACCDA = Command Data Register = 470h for data to write to AC97 reg.
633 // 3. Write ACCTL = Control Register = 460h for initiating the write
634 // 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
635 // 5. if DCV not cleared, break and return error
637 //****************************************************************************
638 static int cs4281_write_ac97(struct cs4281_state *card, u32 offset,
643 CS_DBGOUT(CS_FUNCTION, 2,
644 printk(KERN_INFO "cs4281: cs_4281_write_ac97()+ \n"));
646 // Setup the AC97 control registers on the CS4281 to send the
647 // appropriate command to the AC97 to perform the read.
648 // ACCAD = Command Address Register = 46Ch
649 // ACCDA = Command Data Register = 470h
650 // ACCTL = Control Register = 460h
651 // set DCV - will clear when process completed
652 // reset CRW - Write command
653 // set VFRM - valid frame enabled
654 // set ESYN - ASYNC generation enabled
655 // set RSTN - ARST# inactive, AC97 codec not reset
657 // Get the actual AC97 register from the offset
659 writel(offset - BA0_AC97_RESET, card->pBA0 + BA0_ACCAD);
660 writel(value, card->pBA0 + BA0_ACCDA);
661 writel(ACCTL_DCV | ACCTL_VFRM | ACCTL_ESYN,
662 card->pBA0 + BA0_ACCTL);
664 // Wait for the write to occur.
665 for (count = 0; count < 100; count++) {
666 // First, we want to wait for a short time.
668 // Now, check to see if the write has completed.
669 // ACCTL = 460h, DCV should be reset by now and 460h = 07h
670 status = readl(card->pBA0 + BA0_ACCTL);
671 if (!(status & ACCTL_DCV))
675 // Make sure the write completed.
676 if (status & ACCTL_DCV) {
677 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
678 "cs4281: cs_4281_write_ac97()- unable to write. ACCTL_DCV active\n"));
681 CS_DBGOUT(CS_FUNCTION, 2,
682 printk(KERN_INFO "cs4281: cs_4281_write_ac97()- 0\n"));
688 //******************************************************************************
689 // "Init4281()" -- Bring up the part.
690 //******************************************************************************
691 static __devinit int cs4281_hw_init(struct cs4281_state *card)
696 CS_DBGOUT(CS_FUNCTION, 2,
697 printk(KERN_INFO "cs4281: cs4281_hw_init()+ \n"));
698 #ifndef NOT_CS4281_PM
702 temp2 = readl(card->pBA0 + BA0_CFLR);
703 CS_DBGOUT(CS_INIT | CS_ERROR | CS_PARMS, 4, printk(KERN_INFO
704 "cs4281: cs4281_hw_init() CFLR 0x%x\n", temp2));
705 if(temp2 != CS4281_CFLR_DEFAULT)
707 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_INFO
708 "cs4281: cs4281_hw_init() CFLR invalid - resetting from 0x%x to 0x%x\n",
709 temp2,CS4281_CFLR_DEFAULT));
710 writel(CS4281_CFLR_DEFAULT, card->pBA0 + BA0_CFLR);
711 temp2 = readl(card->pBA0 + BA0_CFLR);
712 if(temp2 != CS4281_CFLR_DEFAULT)
714 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_INFO
715 "cs4281: cs4281_hw_init() Invalid hardware - unable to configure CFLR\n"));
720 //***************************************7
721 // Set up the Sound System Configuration
722 //***************************************
724 // Set the 'Configuration Write Protect' register
725 // to 4281h. Allows vendor-defined configuration
726 // space between 0e4h and 0ffh to be written.
728 writel(0x4281, card->pBA0 + BA0_CWPR); // (3e0h)
730 // (0), Blast the clock control register to zero so that the
731 // PLL starts out in a known state, and blast the master serial
732 // port control register to zero so that the serial ports also
733 // start out in a known state.
735 writel(0, card->pBA0 + BA0_CLKCR1); // (400h)
736 writel(0, card->pBA0 + BA0_SERMC); // (420h)
739 // (1), Make ESYN go to zero to turn off
740 // the Sync pulse on the AC97 link.
742 writel(0, card->pBA0 + BA0_ACCTL);
746 // (2) Drive the ARST# pin low for a minimum of 1uS (as defined in
747 // the AC97 spec) and then drive it high. This is done for non
748 // AC97 modes since there might be logic external to the CS461x
749 // that uses the ARST# line for a reset.
751 writel(0, card->pBA0 + BA0_SPMC); // (3ech)
753 writel(SPMC_RSTN, card->pBA0 + BA0_SPMC);
754 delayus(card,50000); // Wait 50 ms for ABITCLK to become stable.
756 // (3) Turn on the Sound System Clocks.
757 writel(CLKCR1_PLLP, card->pBA0 + BA0_CLKCR1); // (400h)
758 delayus(card,50000); // Wait for the PLL to stabilize.
759 // Turn on clocking of the core (CLKCR1(400h) = 0x00000030)
760 writel(CLKCR1_PLLP | CLKCR1_SWCE, card->pBA0 + BA0_CLKCR1);
762 // (4) Power on everything for now..
763 writel(0x7E, card->pBA0 + BA0_SSPM); // (740h)
765 // (5) Wait for clock stabilization.
766 for (temp1 = 0; temp1 < 1000; temp1++) {
768 if (readl(card->pBA0 + BA0_CLKCR1) & CLKCR1_DLLRDY)
771 if (!(readl(card->pBA0 + BA0_CLKCR1) & CLKCR1_DLLRDY)) {
772 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
773 "cs4281: DLLRDY failed!\n"));
776 // (6) Enable ASYNC generation.
777 writel(ACCTL_ESYN, card->pBA0 + BA0_ACCTL); // (460h)
779 // Now wait 'for a short while' to allow the AC97
780 // part to start generating bit clock. (so we don't
781 // Try to start the PLL without an input clock.)
784 // Set the serial port timing configuration, so that the
785 // clock control circuit gets its clock from the right place.
786 writel(SERMC_PTC_AC97, card->pBA0 + BA0_SERMC); // (420h)=2.
788 // (7) Wait for the codec ready signal from the AC97 codec.
790 for (temp1 = 0; temp1 < 1000; temp1++) {
791 // Delay a mil to let things settle out and
792 // to prevent retrying the read too quickly.
794 if (readl(card->pBA0 + BA0_ACSTS) & ACSTS_CRDY) // If ready, (464h)
795 break; // exit the 'for' loop.
797 if (!(readl(card->pBA0 + BA0_ACSTS) & ACSTS_CRDY)) // If never came ready,
799 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_ERR
800 "cs4281: ACSTS never came ready!\n"));
801 return -EIO; // exit initialization.
803 // (8) Assert the 'valid frame' signal so we can
804 // begin sending commands to the AC97 codec.
805 writel(ACCTL_VFRM | ACCTL_ESYN, card->pBA0 + BA0_ACCTL); // (460h)
807 // (9), Wait until CODEC calibration is finished.
808 // Print an error message if it doesn't.
809 for (temp1 = 0; temp1 < 1000; temp1++) {
811 // Read the AC97 Powerdown Control/Status Register.
812 cs4281_read_ac97(card, BA0_AC97_POWERDOWN, &temp2);
813 if ((temp2 & 0x0000000F) == 0x0000000F)
816 if ((temp2 & 0x0000000F) != 0x0000000F) {
817 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_ERR
818 "cs4281: Codec failed to calibrate. Status = %.8x.\n",
822 // (10), Set the serial port timing configuration, so that the
823 // clock control circuit gets its clock from the right place.
824 writel(SERMC_PTC_AC97, card->pBA0 + BA0_SERMC); // (420h)=2.
827 // (11) Wait until we've sampled input slots 3 & 4 as valid, meaning
828 // that the codec is pumping ADC data across the AC link.
829 for (temp1 = 0; temp1 < 1000; temp1++) {
830 // Delay a mil to let things settle out and
831 // to prevent retrying the read too quickly.
832 delayus(card,1000); //(test)
834 // Read the input slot valid register; See
835 // if input slots 3 and 4 are valid yet.
837 (readl(card->pBA0 + BA0_ACISV) &
838 (ACISV_ISV3 | ACISV_ISV4)) ==
839 (ACISV_ISV3 | ACISV_ISV4)) break; // Exit the 'for' if slots are valid.
841 // If we never got valid data, exit initialization.
842 if ((readl(card->pBA0 + BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4))
843 != (ACISV_ISV3 | ACISV_ISV4)) {
844 CS_DBGOUT(CS_FUNCTION, 2,
846 "cs4281: Never got valid data!\n"));
847 return -EIO; // If no valid data, exit initialization.
849 // (12), Start digital data transfer of audio data to the codec.
850 writel(ACOSV_SLV3 | ACOSV_SLV4, card->pBA0 + BA0_ACOSV); // (468h)
853 //**************************************
854 // Unmute the Master and Alternate
855 // (headphone) volumes. Set to max.
856 //**************************************
857 cs4281_write_ac97(card, BA0_AC97_HEADPHONE_VOLUME, 0);
858 cs4281_write_ac97(card, BA0_AC97_MASTER_VOLUME, 0);
860 //******************************************
861 // Power on the DAC(AddDACUser()from main())
862 //******************************************
863 cs4281_read_ac97(card, BA0_AC97_POWERDOWN, &temp1);
864 cs4281_write_ac97(card, BA0_AC97_POWERDOWN, temp1 &= 0xfdff);
866 // Wait until we sample a DAC ready state.
867 for (temp2 = 0; temp2 < 32; temp2++) {
868 // Let's wait a mil to let things settle.
870 // Read the current state of the power control reg.
871 cs4281_read_ac97(card, BA0_AC97_POWERDOWN, &temp1);
872 // If the DAC ready state bit is set, stop waiting.
877 //******************************************
878 // Power on the ADC(AddADCUser()from main())
879 //******************************************
880 cs4281_read_ac97(card, BA0_AC97_POWERDOWN, &temp1);
881 cs4281_write_ac97(card, BA0_AC97_POWERDOWN, temp1 &= 0xfeff);
883 // Wait until we sample ADC ready state.
884 for (temp2 = 0; temp2 < 32; temp2++) {
885 // Let's wait a mil to let things settle.
887 // Read the current state of the power control reg.
888 cs4281_read_ac97(card, BA0_AC97_POWERDOWN, &temp1);
889 // If the ADC ready state bit is set, stop waiting.
893 // Set up 4281 Register contents that
894 // don't change for boot duration.
896 // For playback, we map AC97 slot 3 and 4(Left
897 // & Right PCM playback) to DMA Channel 0.
898 // Set the fifo to be 15 bytes at offset zero.
900 ac97_slotid = 0x01000f00; // FCR0.RS[4:0]=1(=>slot4, right PCM playback).
901 // FCR0.LS[4:0]=0(=>slot3, left PCM playback).
902 // FCR0.SZ[6-0]=15; FCR0.OF[6-0]=0.
903 writel(ac97_slotid, card->pBA0 + BA0_FCR0); // (180h)
904 writel(ac97_slotid | FCRn_FEN, card->pBA0 + BA0_FCR0); // Turn on FIFO Enable.
906 // For capture, we map AC97 slot 10 and 11(Left
907 // and Right PCM Record) to DMA Channel 1.
908 // Set the fifo to be 15 bytes at offset sixteen.
909 ac97_slotid = 0x0B0A0f10; // FCR1.RS[4:0]=11(=>slot11, right PCM record).
910 // FCR1.LS[4:0]=10(=>slot10, left PCM record).
911 // FCR1.SZ[6-0]=15; FCR1.OF[6-0]=16.
912 writel(ac97_slotid | FCRn_PSH, card->pBA0 + BA0_FCR1); // (184h)
913 writel(ac97_slotid | FCRn_FEN, card->pBA0 + BA0_FCR1); // Turn on FIFO Enable.
915 // Map the Playback SRC to the same AC97 slots(3 & 4--
916 // --Playback left & right)as DMA channel 0.
917 // Map the record SRC to the same AC97 slots(10 & 11--
918 // -- Record left & right) as DMA channel 1.
920 ac97_slotid = 0x0b0a0100; // SCRSA.PRSS[4:0]=1(=>slot4, right PCM playback).
921 // SCRSA.PLSS[4:0]=0(=>slot3, left PCM playback).
922 // SCRSA.CRSS[4:0]=11(=>slot11, right PCM record)
923 // SCRSA.CLSS[4:0]=10(=>slot10, left PCM record).
924 writel(ac97_slotid, card->pBA0 + BA0_SRCSA); // (75ch)
926 // Set 'Half Terminal Count Interrupt Enable' and 'Terminal
927 // Count Interrupt Enable' in DMA Control Registers 0 & 1.
928 // Set 'MSK' flag to 1 to keep the DMA engines paused.
929 temp1 = (DCRn_HTCIE | DCRn_TCIE | DCRn_MSK); // (00030001h)
930 writel(temp1, card->pBA0 + BA0_DCR0); // (154h
931 writel(temp1, card->pBA0 + BA0_DCR1); // (15ch)
933 // Set 'Auto-Initialize Control' to 'enabled'; For playback,
934 // set 'Transfer Type Control'(TR[1:0]) to 'read transfer',
935 // for record, set Transfer Type Control to 'write transfer'.
936 // All other bits set to zero; Some will be changed @ transfer start.
937 temp1 = (DMRn_DMA | DMRn_AUTO | DMRn_TR_READ); // (20000018h)
938 writel(temp1, card->pBA0 + BA0_DMR0); // (150h)
939 temp1 = (DMRn_DMA | DMRn_AUTO | DMRn_TR_WRITE); // (20000014h)
940 writel(temp1, card->pBA0 + BA0_DMR1); // (158h)
942 // Enable DMA interrupts generally, and
943 // DMA0 & DMA1 interrupts specifically.
944 temp1 = readl(card->pBA0 + BA0_HIMR) & 0xfffbfcff;
945 writel(temp1, card->pBA0 + BA0_HIMR);
947 CS_DBGOUT(CS_FUNCTION, 2,
948 printk(KERN_INFO "cs4281: cs4281_hw_init()- 0\n"));
952 #ifndef NOT_CS4281_PM
953 static void printpm(struct cs4281_state *s)
955 CS_DBGOUT(CS_PM, 9, printk("pm struct:\n"));
956 CS_DBGOUT(CS_PM, 9, printk("flags:0x%x u32CLKCR1_SAVE: 0%x u32SSPMValue: 0x%x\n",
957 (unsigned)s->pm.flags,s->pm.u32CLKCR1_SAVE,s->pm.u32SSPMValue));
958 CS_DBGOUT(CS_PM, 9, printk("u32PPLVCvalue: 0x%x u32PPRVCvalue: 0x%x\n",
959 s->pm.u32PPLVCvalue,s->pm.u32PPRVCvalue));
960 CS_DBGOUT(CS_PM, 9, printk("u32FMLVCvalue: 0x%x u32FMRVCvalue: 0x%x\n",
961 s->pm.u32FMLVCvalue,s->pm.u32FMRVCvalue));
962 CS_DBGOUT(CS_PM, 9, printk("u32GPIORvalue: 0x%x u32JSCTLvalue: 0x%x\n",
963 s->pm.u32GPIORvalue,s->pm.u32JSCTLvalue));
964 CS_DBGOUT(CS_PM, 9, printk("u32SSCR: 0x%x u32SRCSA: 0x%x\n",
965 s->pm.u32SSCR,s->pm.u32SRCSA));
966 CS_DBGOUT(CS_PM, 9, printk("u32DacASR: 0x%x u32AdcASR: 0x%x\n",
967 s->pm.u32DacASR,s->pm.u32AdcASR));
968 CS_DBGOUT(CS_PM, 9, printk("u32DacSR: 0x%x u32AdcSR: 0x%x\n",
969 s->pm.u32DacSR,s->pm.u32AdcSR));
970 CS_DBGOUT(CS_PM, 9, printk("u32MIDCR_Save: 0x%x\n",
971 s->pm.u32MIDCR_Save));
974 static void printpipe(struct cs4281_pipeline *pl)
977 CS_DBGOUT(CS_PM, 9, printk("pm struct:\n"));
978 CS_DBGOUT(CS_PM, 9, printk("flags:0x%x number: 0%x\n",
979 (unsigned)pl->flags,pl->number));
980 CS_DBGOUT(CS_PM, 9, printk("u32DBAnValue: 0%x u32DBCnValue: 0x%x\n",
981 pl->u32DBAnValue,pl->u32DBCnValue));
982 CS_DBGOUT(CS_PM, 9, printk("u32DMRnValue: 0x%x u32DCRnValue: 0x%x\n",
983 pl->u32DMRnValue,pl->u32DCRnValue));
984 CS_DBGOUT(CS_PM, 9, printk("u32DBAnAddress: 0x%x u32DBCnAddress: 0x%x\n",
985 pl->u32DBAnAddress,pl->u32DBCnAddress));
986 CS_DBGOUT(CS_PM, 9, printk("u32DCAnAddress: 0x%x u32DCCnAddress: 0x%x\n",
987 pl->u32DCCnAddress,pl->u32DCCnAddress));
988 CS_DBGOUT(CS_PM, 9, printk("u32DMRnAddress: 0x%x u32DCRnAddress: 0x%x\n",
989 pl->u32DMRnAddress,pl->u32DCRnAddress));
990 CS_DBGOUT(CS_PM, 9, printk("u32HDSRnAddress: 0x%x u32DBAn_Save: 0x%x\n",
991 pl->u32HDSRnAddress,pl->u32DBAn_Save));
992 CS_DBGOUT(CS_PM, 9, printk("u32DBCn_Save: 0x%x u32DMRn_Save: 0x%x\n",
993 pl->u32DBCn_Save,pl->u32DMRn_Save));
994 CS_DBGOUT(CS_PM, 9, printk("u32DCRn_Save: 0x%x u32DCCn_Save: 0x%x\n",
995 pl->u32DCRn_Save,pl->u32DCCn_Save));
996 CS_DBGOUT(CS_PM, 9, printk("u32DCAn_Save: 0x%x\n",
998 CS_DBGOUT(CS_PM, 9, printk("u32FCRn_Save: 0x%x u32FSICn_Save: 0x%x\n",
999 pl->u32FCRn_Save,pl->u32FSICn_Save));
1000 CS_DBGOUT(CS_PM, 9, printk("u32FCRnValue: 0x%x u32FSICnValue: 0x%x\n",
1001 pl->u32FCRnValue,pl->u32FSICnValue));
1002 CS_DBGOUT(CS_PM, 9, printk("u32FCRnAddress: 0x%x u32FSICnAddress: 0x%x\n",
1003 pl->u32FCRnAddress,pl->u32FSICnAddress));
1004 CS_DBGOUT(CS_PM, 9, printk("u32FPDRnValue: 0x%x u32FPDRnAddress: 0x%x\n",
1005 pl->u32FPDRnValue,pl->u32FPDRnAddress));
1007 static void printpipelines(struct cs4281_state *s)
1010 for(i=0;i<CS4281_NUMBER_OF_PIPELINES;i++)
1012 if(s->pl[i].flags & CS4281_PIPELINE_VALID)
1014 printpipe(&s->pl[i]);
1018 /****************************************************************************
1020 * Suspend - save the ac97 regs, mute the outputs and power down the part.
1022 ****************************************************************************/
1023 static void cs4281_ac97_suspend(struct cs4281_state *s)
1027 CS_DBGOUT(CS_PM, 9, printk("cs4281: cs4281_ac97_suspend()+\n"));
1029 * change the state, save the current hwptr, then stop the dac/adc
1031 s->pm.flags &= ~CS4281_PM_IDLE;
1032 s->pm.flags |= CS4281_PM_SUSPENDING;
1033 s->pm.u32hwptr_playback = readl(s->pBA0 + BA0_DCA0);
1034 s->pm.u32hwptr_capture = readl(s->pBA0 + BA0_DCA1);
1038 for(Count = 0x2, i=0; (Count <= CS4281_AC97_HIGHESTREGTORESTORE)
1039 && (i < CS4281_AC97_NUMBER_RESTORE_REGS);
1042 cs4281_read_ac97(s, BA0_AC97_RESET + Count, &s->pm.ac97[i]);
1045 * Save the ac97 volume registers as well as the current powerdown state.
1046 * Now, mute the all the outputs (master, headphone, and mono), as well
1047 * as the PCM volume, in preparation for powering down the entire part.
1049 cs4281_read_ac97(s, BA0_AC97_MASTER_VOLUME, &s->pm.u32AC97_master_volume);
1050 cs4281_read_ac97(s, BA0_AC97_HEADPHONE_VOLUME, &s->pm.u32AC97_headphone_volume);
1051 cs4281_read_ac97(s, BA0_AC97_MASTER_VOLUME_MONO, &s->pm.u32AC97_master_volume_mono);
1052 cs4281_read_ac97(s, BA0_AC97_PCM_OUT_VOLUME, &s->pm.u32AC97_pcm_out_volume);
1054 cs4281_write_ac97(s, BA0_AC97_MASTER_VOLUME, 0x8000);
1055 cs4281_write_ac97(s, BA0_AC97_HEADPHONE_VOLUME, 0x8000);
1056 cs4281_write_ac97(s, BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
1057 cs4281_write_ac97(s, BA0_AC97_PCM_OUT_VOLUME, 0x8000);
1059 cs4281_read_ac97(s, BA0_AC97_POWERDOWN, &s->pm.u32AC97_powerdown);
1060 cs4281_read_ac97(s, BA0_AC97_GENERAL_PURPOSE, &s->pm.u32AC97_general_purpose);
1063 * And power down everything on the AC97 codec.
1065 cs4281_write_ac97(s, BA0_AC97_POWERDOWN, 0xff00);
1066 CS_DBGOUT(CS_PM, 9, printk("cs4281: cs4281_ac97_suspend()-\n"));
1069 /****************************************************************************
1071 * Resume - power up the part and restore its registers..
1073 ****************************************************************************/
1074 static void cs4281_ac97_resume(struct cs4281_state *s)
1078 CS_DBGOUT(CS_PM, 9, printk("cs4281: cs4281_ac97_resume()+\n"));
1080 /* do not save the power state registers at this time
1082 // If we saved away the power control registers, write them into the
1083 // shadows so those saved values get restored instead of the current
1086 if( bPowerStateSaved )
1088 PokeShadow( 0x26, ulSaveReg0x26 );
1089 bPowerStateSaved = FALSE;
1094 // First, we restore the state of the general purpose register. This
1095 // contains the mic select (mic1 or mic2) and if we restore this after
1096 // we restore the mic volume/boost state and mic2 was selected at
1097 // suspend time, we will end up with a brief period of time where mic1
1098 // is selected with the volume/boost settings for mic2, causing
1099 // acoustic feedback. So we restore the general purpose register
1100 // first, thereby getting the correct mic selected before we restore
1101 // the mic volume/boost.
1103 cs4281_write_ac97(s, BA0_AC97_GENERAL_PURPOSE, s->pm.u32AC97_general_purpose);
1106 // Now, while the outputs are still muted, restore the state of power
1107 // on the AC97 part.
1109 cs4281_write_ac97(s, BA0_AC97_POWERDOWN, s->pm.u32AC97_powerdown);
1112 * Restore just the first set of registers, from register number
1113 * 0x02 to the register number that ulHighestRegToRestore specifies.
1115 for( Count = 0x2, i=0;
1116 (Count <= CS4281_AC97_HIGHESTREGTORESTORE)
1117 && (i < CS4281_AC97_NUMBER_RESTORE_REGS);
1120 cs4281_write_ac97(s, BA0_AC97_RESET + Count, s->pm.ac97[i]);
1122 CS_DBGOUT(CS_PM, 9, printk("cs4281: cs4281_ac97_resume()-\n"));
1125 /* do not save the power state registers at this time
1126 ****************************************************************************
1128 * SavePowerState - Save the power registers away.
1130 ****************************************************************************
1132 HWAC97codec::SavePowerState(void)
1134 ENTRY(TM_OBJECTCALLS, "HWAC97codec::SavePowerState()\r\n");
1136 ulSaveReg0x26 = PeekShadow(0x26);
1139 // Note that we have saved registers that need to be restored during a
1140 // resume instead of ulAC97Regs[].
1142 bPowerStateSaved = TRUE;
1147 static void cs4281_SuspendFIFO(struct cs4281_state *s, struct cs4281_pipeline *pl)
1150 * We need to save the contents of the BASIC FIFO Registers.
1152 pl->u32FCRn_Save = readl(s->pBA0 + pl->u32FCRnAddress);
1153 pl->u32FSICn_Save = readl(s->pBA0 + pl->u32FSICnAddress);
1155 static void cs4281_ResumeFIFO(struct cs4281_state *s, struct cs4281_pipeline *pl)
1158 * We need to restore the contents of the BASIC FIFO Registers.
1160 writel(pl->u32FCRn_Save,s->pBA0 + pl->u32FCRnAddress);
1161 writel(pl->u32FSICn_Save,s->pBA0 + pl->u32FSICnAddress);
1163 static void cs4281_SuspendDMAengine(struct cs4281_state *s, struct cs4281_pipeline *pl)
1166 // We need to save the contents of the BASIC DMA Registers.
1168 pl->u32DBAn_Save = readl(s->pBA0 + pl->u32DBAnAddress);
1169 pl->u32DBCn_Save = readl(s->pBA0 + pl->u32DBCnAddress);
1170 pl->u32DMRn_Save = readl(s->pBA0 + pl->u32DMRnAddress);
1171 pl->u32DCRn_Save = readl(s->pBA0 + pl->u32DCRnAddress);
1172 pl->u32DCCn_Save = readl(s->pBA0 + pl->u32DCCnAddress);
1173 pl->u32DCAn_Save = readl(s->pBA0 + pl->u32DCAnAddress);
1175 static void cs4281_ResumeDMAengine(struct cs4281_state *s, struct cs4281_pipeline *pl)
1178 // We need to save the contents of the BASIC DMA Registers.
1180 writel( pl->u32DBAn_Save, s->pBA0 + pl->u32DBAnAddress);
1181 writel( pl->u32DBCn_Save, s->pBA0 + pl->u32DBCnAddress);
1182 writel( pl->u32DMRn_Save, s->pBA0 + pl->u32DMRnAddress);
1183 writel( pl->u32DCRn_Save, s->pBA0 + pl->u32DCRnAddress);
1184 writel( pl->u32DCCn_Save, s->pBA0 + pl->u32DCCnAddress);
1185 writel( pl->u32DCAn_Save, s->pBA0 + pl->u32DCAnAddress);
1188 static int cs4281_suspend(struct cs4281_state *s)
1192 struct cs4281_pm *pm = &s->pm;
1193 CS_DBGOUT(CS_PM | CS_FUNCTION, 9,
1194 printk("cs4281: cs4281_suspend()+ flags=%d\n",
1195 (unsigned)s->pm.flags));
1197 * check the current state, only suspend if IDLE
1199 if(!(s->pm.flags & CS4281_PM_IDLE))
1201 CS_DBGOUT(CS_PM | CS_ERROR, 2,
1202 printk("cs4281: cs4281_suspend() unable to suspend, not IDLE\n"));
1205 s->pm.flags &= ~CS4281_PM_IDLE;
1206 s->pm.flags |= CS4281_PM_SUSPENDING;
1209 // Gershwin CLKRUN - Set CKRA
1211 u32CLKCR1 = readl(s->pBA0 + BA0_CLKCR1);
1213 pm->u32CLKCR1_SAVE = u32CLKCR1;
1214 if(!(u32CLKCR1 & 0x00010000 ) )
1215 writel(u32CLKCR1 | 0x00010000, s->pBA0 + BA0_CLKCR1);
1218 // First, turn on the clocks (yikes) to the devices, so that they will
1219 // respond when we try to save their state.
1221 if(!(u32CLKCR1 & CLKCR1_SWCE))
1223 writel(u32CLKCR1 | CLKCR1_SWCE , s->pBA0 + BA0_CLKCR1);
1227 // Save the power state
1229 pm->u32SSPMValue = readl(s->pBA0 + BA0_SSPM);
1232 // Disable interrupts.
1234 writel(HICR_CHGM, s->pBA0 + BA0_HICR);
1237 // Save the PCM Playback Left and Right Volume Control.
1239 pm->u32PPLVCvalue = readl(s->pBA0 + BA0_PPLVC);
1240 pm->u32PPRVCvalue = readl(s->pBA0 + BA0_PPRVC);
1243 // Save the FM Synthesis Left and Right Volume Control.
1245 pm->u32FMLVCvalue = readl(s->pBA0 + BA0_FMLVC);
1246 pm->u32FMRVCvalue = readl(s->pBA0 + BA0_FMRVC);
1249 // Save the GPIOR value.
1251 pm->u32GPIORvalue = readl(s->pBA0 + BA0_GPIOR);
1254 // Save the JSCTL value.
1256 pm->u32JSCTLvalue = readl(s->pBA0 + BA0_GPIOR);
1259 // Save Sound System Control Register
1261 pm->u32SSCR = readl(s->pBA0 + BA0_SSCR);
1264 // Save SRC Slot Assinment register
1266 pm->u32SRCSA = readl(s->pBA0 + BA0_SRCSA);
1271 pm->u32DacASR = readl(s->pBA0 + BA0_PASR);
1272 pm->u32AdcASR = readl(s->pBA0 + BA0_CASR);
1273 pm->u32DacSR = readl(s->pBA0 + BA0_DACSR);
1274 pm->u32AdcSR = readl(s->pBA0 + BA0_ADCSR);
1277 // Loop through all of the PipeLines
1279 for(i = 0; i < CS4281_NUMBER_OF_PIPELINES; i++)
1281 if(s->pl[i].flags & CS4281_PIPELINE_VALID)
1284 // Ask the DMAengines and FIFOs to Suspend.
1286 cs4281_SuspendDMAengine(s,&s->pl[i]);
1287 cs4281_SuspendFIFO(s,&s->pl[i]);
1291 // We need to save the contents of the Midi Control Register.
1293 pm->u32MIDCR_Save = readl(s->pBA0 + BA0_MIDCR);
1295 * save off the AC97 part information
1297 cs4281_ac97_suspend(s);
1300 // Turn off the serial ports.
1302 writel(0, s->pBA0 + BA0_SERMC);
1305 // Power off FM, Joystick, AC link,
1307 writel(0, s->pBA0 + BA0_SSPM);
1312 writel(0, s->pBA0 + BA0_CLKCR1);
1317 writel(0, s->pBA0 + BA0_SPMC);
1320 // Put the chip into D3(hot) state.
1322 // PokeBA0(BA0_PMCS, 0x00000003);
1325 // Gershwin CLKRUN - Clear CKRA
1327 u32CLKCR1 = readl(s->pBA0 + BA0_CLKCR1);
1328 writel(u32CLKCR1 & 0xFFFEFFFF, s->pBA0 + BA0_CLKCR1);
1335 s->pm.flags &= ~CS4281_PM_SUSPENDING;
1336 s->pm.flags |= CS4281_PM_SUSPENDED;
1338 CS_DBGOUT(CS_PM | CS_FUNCTION, 9,
1339 printk("cs4281: cs4281_suspend()- flags=%d\n",
1340 (unsigned)s->pm.flags));
1344 static int cs4281_resume(struct cs4281_state *s)
1349 struct cs4281_pm *pm = &s->pm;
1350 CS_DBGOUT(CS_PM | CS_FUNCTION, 4,
1351 printk( "cs4281: cs4281_resume()+ flags=%d\n",
1352 (unsigned)s->pm.flags));
1353 if(!(s->pm.flags & CS4281_PM_SUSPENDED))
1355 CS_DBGOUT(CS_PM | CS_ERROR, 2,
1356 printk("cs4281: cs4281_resume() unable to resume, not SUSPENDED\n"));
1359 s->pm.flags &= ~CS4281_PM_SUSPENDED;
1360 s->pm.flags |= CS4281_PM_RESUMING;
1363 // Gershwin CLKRUN - Set CKRA
1365 u32CLKCR1 = readl(s->pBA0 + BA0_CLKCR1);
1366 writel(u32CLKCR1 | 0x00010000, s->pBA0 + BA0_CLKCR1);
1369 // set the power state.
1371 //old PokeBA0(BA0_PMCS, 0);
1374 // Program the clock circuit and serial ports.
1376 temp1 = cs4281_hw_init(s);
1378 CS_DBGOUT(CS_ERROR | CS_INIT, 1,
1380 "cs4281: resume cs4281_hw_init() error.\n"));
1385 // restore the Power state
1387 writel(pm->u32SSPMValue, s->pBA0 + BA0_SSPM);
1390 // Set post SRC mix setting (FM or ALT48K)
1392 writel(pm->u32SSPM_BITS, s->pBA0 + BA0_SSPM);
1395 // Loop through all of the PipeLines
1397 for(i = 0; i < CS4281_NUMBER_OF_PIPELINES; i++)
1399 if(s->pl[i].flags & CS4281_PIPELINE_VALID)
1402 // Ask the DMAengines and FIFOs to Resume.
1404 cs4281_ResumeDMAengine(s,&s->pl[i]);
1405 cs4281_ResumeFIFO(s,&s->pl[i]);
1409 // We need to restore the contents of the Midi Control Register.
1411 writel(pm->u32MIDCR_Save, s->pBA0 + BA0_MIDCR);
1413 cs4281_ac97_resume(s);
1415 // Restore the PCM Playback Left and Right Volume Control.
1417 writel(pm->u32PPLVCvalue, s->pBA0 + BA0_PPLVC);
1418 writel(pm->u32PPRVCvalue, s->pBA0 + BA0_PPRVC);
1421 // Restore the FM Synthesis Left and Right Volume Control.
1423 writel(pm->u32FMLVCvalue, s->pBA0 + BA0_FMLVC);
1424 writel(pm->u32FMRVCvalue, s->pBA0 + BA0_FMRVC);
1427 // Restore the JSCTL value.
1429 writel(pm->u32JSCTLvalue, s->pBA0 + BA0_JSCTL);
1432 // Restore the GPIOR register value.
1434 writel(pm->u32GPIORvalue, s->pBA0 + BA0_GPIOR);
1437 // Restore Sound System Control Register
1439 writel(pm->u32SSCR, s->pBA0 + BA0_SSCR);
1442 // Restore SRC Slot Assignment register
1444 writel(pm->u32SRCSA, s->pBA0 + BA0_SRCSA);
1447 // Restore sample rate
1449 writel(pm->u32DacASR, s->pBA0 + BA0_PASR);
1450 writel(pm->u32AdcASR, s->pBA0 + BA0_CASR);
1451 writel(pm->u32DacSR, s->pBA0 + BA0_DACSR);
1452 writel(pm->u32AdcSR, s->pBA0 + BA0_ADCSR);
1455 // Restore CFL1/2 registers we saved to compensate for OEM bugs.
1457 // PokeBA0(BA0_CFLR, ulConfig);
1460 // Gershwin CLKRUN - Clear CKRA
1462 writel(pm->u32CLKCR1_SAVE, s->pBA0 + BA0_CLKCR1);
1465 // Enable interrupts on the part.
1467 writel(HICR_IEV | HICR_CHGM, s->pBA0 + BA0_HICR);
1474 * change the state, restore the current hwptrs, then stop the dac/adc
1476 s->pm.flags |= CS4281_PM_IDLE;
1477 s->pm.flags &= ~(CS4281_PM_SUSPENDING | CS4281_PM_SUSPENDED
1478 | CS4281_PM_RESUMING | CS4281_PM_RESUMED);
1480 writel(s->pm.u32hwptr_playback, s->pBA0 + BA0_DCA0);
1481 writel(s->pm.u32hwptr_capture, s->pBA0 + BA0_DCA1);
1485 CS_DBGOUT(CS_PM | CS_FUNCTION, 9, printk("cs4281: cs4281_resume()- flags=%d\n",
1486 (unsigned)s->pm.flags));
1492 //******************************************************************************
1493 // "cs4281_play_rate()" --
1494 //******************************************************************************
1495 static void cs4281_play_rate(struct cs4281_state *card, u32 playrate)
1499 // Based on the sample rate, program the DACSR register.
1500 if (playrate == 8000)
1502 if (playrate == 11025)
1504 else if (playrate == 22050)
1506 else if (playrate == 44100)
1508 else if ((playrate <= 48000) && (playrate >= 6023))
1509 DACSRvalue = 24576000 / (playrate * 16);
1510 else if (playrate < 6023)
1511 // Not allowed by open.
1513 else if (playrate > 48000)
1514 // Not allowed by open.
1516 CS_DBGOUT(CS_WAVE_WRITE | CS_PARMS, 2, printk(KERN_INFO
1517 "cs4281: cs4281_play_rate(): DACSRvalue=0x%.8x playrate=%d\n",
1518 DACSRvalue, playrate));
1519 // Write the 'sample rate select code'
1520 // to the 'DAC Sample Rate' register.
1521 writel(DACSRvalue, card->pBA0 + BA0_DACSR); // (744h)
1524 //******************************************************************************
1525 // "cs4281_record_rate()" -- Initialize the record sample rate converter.
1526 //******************************************************************************
1527 static void cs4281_record_rate(struct cs4281_state *card, u32 outrate)
1532 // Based on the sample rate, program the ADCSR register
1534 if (outrate == 8000)
1536 if (outrate == 11025)
1538 else if (outrate == 22050)
1540 else if (outrate == 44100)
1542 else if ((outrate <= 48000) && (outrate >= 6023))
1543 ADCSRvalue = 24576000 / (outrate * 16);
1544 else if (outrate < 6023) {
1545 // Not allowed by open.
1547 } else if (outrate > 48000) {
1548 // Not allowed by open.
1551 CS_DBGOUT(CS_WAVE_READ | CS_PARMS, 2, printk(KERN_INFO
1552 "cs4281: cs4281_record_rate(): ADCSRvalue=0x%.8x outrate=%d\n",
1553 ADCSRvalue, outrate));
1554 // Write the 'sample rate select code
1555 // to the 'ADC Sample Rate' register.
1556 writel(ADCSRvalue, card->pBA0 + BA0_ADCSR); // (748h)
1561 static void stop_dac(struct cs4281_state *s)
1563 unsigned long flags;
1566 CS_DBGOUT(CS_WAVE_WRITE, 3, printk(KERN_INFO "cs4281: stop_dac():\n"));
1567 spin_lock_irqsave(&s->lock, flags);
1568 s->ena &= ~FMODE_WRITE;
1569 temp1 = readl(s->pBA0 + BA0_DCR0) | DCRn_MSK;
1570 writel(temp1, s->pBA0 + BA0_DCR0);
1572 spin_unlock_irqrestore(&s->lock, flags);
1576 static void start_dac(struct cs4281_state *s)
1578 unsigned long flags;
1581 CS_DBGOUT(CS_FUNCTION, 3, printk(KERN_INFO "cs4281: start_dac()+\n"));
1582 spin_lock_irqsave(&s->lock, flags);
1583 if (!(s->ena & FMODE_WRITE) && (s->dma_dac.mapped ||
1584 (s->dma_dac.count > 0
1585 && s->dma_dac.ready))
1586 #ifndef NOT_CS4281_PM
1587 && (s->pm.flags & CS4281_PM_IDLE))
1592 s->ena |= FMODE_WRITE;
1593 temp1 = readl(s->pBA0 + BA0_DCR0) & ~DCRn_MSK; // Clear DMA0 channel mask.
1594 writel(temp1, s->pBA0 + BA0_DCR0); // Start DMA'ing.
1595 writel(HICR_IEV | HICR_CHGM, s->pBA0 + BA0_HICR); // Enable interrupts.
1597 writel(7, s->pBA0 + BA0_PPRVC);
1598 writel(7, s->pBA0 + BA0_PPLVC);
1599 CS_DBGOUT(CS_WAVE_WRITE | CS_PARMS, 8, printk(KERN_INFO
1600 "cs4281: start_dac(): writel 0x%x start dma\n", temp1));
1603 spin_unlock_irqrestore(&s->lock, flags);
1604 CS_DBGOUT(CS_FUNCTION, 3,
1605 printk(KERN_INFO "cs4281: start_dac()-\n"));
1609 static void stop_adc(struct cs4281_state *s)
1611 unsigned long flags;
1614 CS_DBGOUT(CS_FUNCTION, 3,
1615 printk(KERN_INFO "cs4281: stop_adc()+\n"));
1617 spin_lock_irqsave(&s->lock, flags);
1618 s->ena &= ~FMODE_READ;
1620 if (s->conversion == 1) {
1622 s->prop_adc.fmt = s->prop_adc.fmt_original;
1624 temp1 = readl(s->pBA0 + BA0_DCR1) | DCRn_MSK;
1625 writel(temp1, s->pBA0 + BA0_DCR1);
1626 spin_unlock_irqrestore(&s->lock, flags);
1627 CS_DBGOUT(CS_FUNCTION, 3,
1628 printk(KERN_INFO "cs4281: stop_adc()-\n"));
1632 static void start_adc(struct cs4281_state *s)
1634 unsigned long flags;
1637 CS_DBGOUT(CS_FUNCTION, 2,
1638 printk(KERN_INFO "cs4281: start_adc()+\n"));
1640 if (!(s->ena & FMODE_READ) &&
1641 (s->dma_adc.mapped || s->dma_adc.count <=
1642 (signed) (s->dma_adc.dmasize - 2 * s->dma_adc.fragsize))
1644 #ifndef NOT_CS4281_PM
1645 && (s->pm.flags & CS4281_PM_IDLE))
1650 if (s->prop_adc.fmt & AFMT_S8 || s->prop_adc.fmt & AFMT_U8) {
1652 // now only use 16 bit capture, due to truncation issue
1653 // in the chip, noticable distortion occurs.
1654 // allocate buffer and then convert from 16 bit to
1655 // 8 bit for the user buffer.
1657 s->prop_adc.fmt_original = s->prop_adc.fmt;
1658 if (s->prop_adc.fmt & AFMT_S8) {
1659 s->prop_adc.fmt &= ~AFMT_S8;
1660 s->prop_adc.fmt |= AFMT_S16_LE;
1662 if (s->prop_adc.fmt & AFMT_U8) {
1663 s->prop_adc.fmt &= ~AFMT_U8;
1664 s->prop_adc.fmt |= AFMT_U16_LE;
1667 // prog_dmabuf_adc performs a stop_adc() but that is
1668 // ok since we really haven't started the DMA yet.
1670 prog_codec(s, CS_TYPE_ADC);
1672 if (prog_dmabuf_adc(s) != 0) {
1673 CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO
1674 "cs4281: start_adc(): error in prog_dmabuf_adc\n"));
1678 spin_lock_irqsave(&s->lock, flags);
1679 s->ena |= FMODE_READ;
1680 temp1 = readl(s->pBA0 + BA0_DCR1) & ~DCRn_MSK; // Clear DMA1 channel mask bit.
1681 writel(temp1, s->pBA0 + BA0_DCR1); // Start recording
1682 writel(HICR_IEV | HICR_CHGM, s->pBA0 + BA0_HICR); // Enable interrupts.
1683 spin_unlock_irqrestore(&s->lock, flags);
1685 CS_DBGOUT(CS_PARMS, 6, printk(KERN_INFO
1686 "cs4281: start_adc(): writel 0x%x \n", temp1));
1688 CS_DBGOUT(CS_FUNCTION, 2,
1689 printk(KERN_INFO "cs4281: start_adc()-\n"));
1694 // ---------------------------------------------------------------------
1696 #define DMABUF_MINORDER 1 // ==> min buffer size = 8K.
1699 static void dealloc_dmabuf(struct cs4281_state *s, struct dmabuf *db)
1701 struct page *map, *mapend;
1704 // Undo prog_dmabuf()'s marking the pages as reserved
1706 virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) -
1708 for (map = virt_to_page(db->rawbuf); map <= mapend; map++)
1709 ClearPageReserved(map);
1712 if (s->tmpbuff && (db->type == CS_TYPE_ADC)) {
1713 // Undo prog_dmabuf()'s marking the pages as reserved
1715 virt_to_page(s->tmpbuff +
1716 (PAGE_SIZE << s->buforder_tmpbuff) - 1);
1717 for (map = virt_to_page(s->tmpbuff); map <= mapend; map++)
1718 ClearPageReserved(map);
1719 free_dmabuf2(s, db);
1723 db->mapped = db->ready = 0;
1726 static int prog_dmabuf(struct cs4281_state *s, struct dmabuf *db)
1729 unsigned bytespersec, temp1;
1730 unsigned bufs, sample_shift = 0;
1731 struct page *map, *mapend;
1734 CS_DBGOUT(CS_FUNCTION, 2,
1735 printk(KERN_INFO "cs4281: prog_dmabuf()+\n"));
1736 db->hwptr = db->swptr = db->total_bytes = db->count = db->error =
1737 db->endcleared = db->blocks = db->wakeup = db->underrun = 0;
1739 * check for order within limits, but do not overwrite value, check
1740 * later for a fractional defaultorder (i.e. 100+).
1742 if((defaultorder > 0) && (defaultorder < 12))
1748 db->ready = db->mapped = 0;
1749 for (order = df; order >= DMABUF_MINORDER; order--)
1750 if ( (db->rawbuf = (void *) pci_alloc_consistent(
1751 s->pcidev, PAGE_SIZE << order, &db-> dmaaddr)))
1754 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
1755 "cs4281: prog_dmabuf(): unable to allocate rawbuf\n"));
1758 db->buforder = order;
1759 // Now mark the pages as reserved; otherwise the
1760 // remap_pfn_range() in cs4281_mmap doesn't work.
1761 // 1. get index to last page in mem_map array for rawbuf.
1762 mapend = virt_to_page(db->rawbuf +
1763 (PAGE_SIZE << db->buforder) - 1);
1765 // 2. mark each physical page in range as 'reserved'.
1766 for (map = virt_to_page(db->rawbuf); map <= mapend; map++)
1767 SetPageReserved(map);
1769 if (!s->tmpbuff && (db->type == CS_TYPE_ADC)) {
1770 for (order = df; order >= DMABUF_MINORDER;
1772 if ( (s->tmpbuff = (void *) pci_alloc_consistent(
1773 s->pcidev, PAGE_SIZE << order,
1774 &s->dmaaddr_tmpbuff)))
1777 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
1778 "cs4281: prog_dmabuf(): unable to allocate tmpbuff\n"));
1781 s->buforder_tmpbuff = order;
1782 // Now mark the pages as reserved; otherwise the
1783 // remap_pfn_range() in cs4281_mmap doesn't work.
1784 // 1. get index to last page in mem_map array for rawbuf.
1785 mapend = virt_to_page(s->tmpbuff +
1786 (PAGE_SIZE << s->buforder_tmpbuff) - 1);
1788 // 2. mark each physical page in range as 'reserved'.
1789 for (map = virt_to_page(s->tmpbuff); map <= mapend; map++)
1790 SetPageReserved(map);
1792 if (db->type == CS_TYPE_DAC) {
1793 if (s->prop_dac.fmt & (AFMT_S16_LE | AFMT_U16_LE))
1795 if (s->prop_dac.channels > 1)
1797 bytespersec = s->prop_dac.rate << sample_shift;
1798 } else // CS_TYPE_ADC
1800 if (s->prop_adc.fmt & (AFMT_S16_LE | AFMT_U16_LE))
1802 if (s->prop_adc.channels > 1)
1804 bytespersec = s->prop_adc.rate << sample_shift;
1806 bufs = PAGE_SIZE << db->buforder;
1809 * added fractional "defaultorder" inputs. if >100 then use
1810 * defaultorder-100 as power of 2 for the buffer size. example:
1811 * 106 = 2^(106-100) = 2^6 = 64 bytes for the buffer size.
1813 if(defaultorder >= 100)
1815 bufs = 1 << (defaultorder-100);
1818 #define INTERRUPT_RATE_MS 100 // Interrupt rate in milliseconds.
1821 * Nominal frag size(bytes/interrupt)
1823 temp1 = bytespersec / (1000 / INTERRUPT_RATE_MS);
1824 db->fragshift = 8; // Min 256 bytes.
1825 while (1 << db->fragshift < temp1) // Calc power of 2 frag size.
1827 db->fragsize = 1 << db->fragshift;
1828 db->dmasize = db->fragsize * 2;
1829 db->fragsamples = db->fragsize >> sample_shift; // # samples/fragment.
1831 // If the calculated size is larger than the allocated
1832 // buffer, divide the allocated buffer into 2 fragments.
1833 if (db->dmasize > bufs) {
1835 db->numfrag = 2; // Two fragments.
1836 db->fragsize = bufs >> 1; // Each 1/2 the alloc'ed buffer.
1837 db->fragsamples = db->fragsize >> sample_shift; // # samples/fragment.
1838 db->dmasize = bufs; // Use all the alloc'ed buffer.
1840 db->fragshift = 0; // Calculate 'fragshift'.
1841 temp1 = db->fragsize; // update_ptr() uses it
1842 while ((temp1 >>= 1) > 1) // to calc 'total-bytes'
1843 db->fragshift += 1; // returned in DSP_GETI/OPTR.
1845 CS_DBGOUT(CS_PARMS, 3, printk(KERN_INFO
1846 "cs4281: prog_dmabuf(): numfrag=%d fragsize=%d fragsamples=%d fragshift=%d bufs=%d fmt=0x%x ch=%d\n",
1847 db->numfrag, db->fragsize, db->fragsamples,
1848 db->fragshift, bufs,
1849 (db->type == CS_TYPE_DAC) ? s->prop_dac.fmt :
1851 (db->type == CS_TYPE_DAC) ? s->prop_dac.channels :
1852 s->prop_adc.channels));
1853 CS_DBGOUT(CS_FUNCTION, 2,
1854 printk(KERN_INFO "cs4281: prog_dmabuf()-\n"));
1859 static int prog_dmabuf_adc(struct cs4281_state *s)
1865 s->dma_adc.type = CS_TYPE_ADC;
1866 if ((c = prog_dmabuf(s, &s->dma_adc)))
1869 if (s->dma_adc.rawbuf) {
1870 memset(s->dma_adc.rawbuf,
1872 fmt & (AFMT_U8 | AFMT_U16_LE)) ? 0x80 : 0,
1873 s->dma_adc.dmasize);
1878 fmt & (AFMT_U8 | AFMT_U16_LE)) ? 0x80 : 0,
1879 PAGE_SIZE << s->buforder_tmpbuff);
1882 va = virt_to_bus(s->dma_adc.rawbuf);
1884 count = s->dma_adc.dmasize;
1887 fmt & (AFMT_S16_LE | AFMT_U16_LE | AFMT_S16_BE | AFMT_U16_BE))
1888 count /= 2; // 16-bit.
1890 if (s->prop_adc.channels > 1)
1891 count /= 2; // Assume stereo.
1893 CS_DBGOUT(CS_WAVE_READ, 3, printk(KERN_INFO
1894 "cs4281: prog_dmabuf_adc(): count=%d va=0x%.8x\n",
1895 count, (unsigned) va));
1897 writel(va, s->pBA0 + BA0_DBA1); // Set buffer start address.
1898 writel(count - 1, s->pBA0 + BA0_DBC1); // Set count.
1899 s->dma_adc.ready = 1;
1904 static int prog_dmabuf_dac(struct cs4281_state *s)
1910 s->dma_dac.type = CS_TYPE_DAC;
1911 if ((c = prog_dmabuf(s, &s->dma_dac)))
1913 memset(s->dma_dac.rawbuf,
1914 (s->prop_dac.fmt & (AFMT_U8 | AFMT_U16_LE)) ? 0x80 : 0,
1915 s->dma_dac.dmasize);
1917 va = virt_to_bus(s->dma_dac.rawbuf);
1919 count = s->dma_dac.dmasize;
1921 fmt & (AFMT_S16_LE | AFMT_U16_LE | AFMT_S16_BE | AFMT_U16_BE))
1922 count /= 2; // 16-bit.
1924 if (s->prop_dac.channels > 1)
1925 count /= 2; // Assume stereo.
1927 writel(va, s->pBA0 + BA0_DBA0); // Set buffer start address.
1928 writel(count - 1, s->pBA0 + BA0_DBC0); // Set count.
1930 CS_DBGOUT(CS_WAVE_WRITE, 3, printk(KERN_INFO
1931 "cs4281: prog_dmabuf_dac(): count=%d va=0x%.8x\n",
1932 count, (unsigned) va));
1934 s->dma_dac.ready = 1;
1939 static void clear_advance(void *buf, unsigned bsize, unsigned bptr,
1940 unsigned len, unsigned char c)
1942 if (bptr + len > bsize) {
1943 unsigned x = bsize - bptr;
1944 memset(((char *) buf) + bptr, c, x);
1948 CS_DBGOUT(CS_WAVE_WRITE, 4, printk(KERN_INFO
1949 "cs4281: clear_advance(): memset %d at %p for %d size \n",
1950 (unsigned)c, ((char *) buf) + bptr, len));
1951 memset(((char *) buf) + bptr, c, len);
1956 // call with spinlock held!
1957 static void cs4281_update_ptr(struct cs4281_state *s, int intflag)
1962 // update ADC pointer
1963 if (s->ena & FMODE_READ) {
1964 hwptr = readl(s->pBA0 + BA0_DCA1); // Read capture DMA address.
1965 va = virt_to_bus(s->dma_adc.rawbuf);
1966 hwptr -= (unsigned) va;
1968 (s->dma_adc.dmasize + hwptr -
1969 s->dma_adc.hwptr) % s->dma_adc.dmasize;
1970 s->dma_adc.hwptr = hwptr;
1971 s->dma_adc.total_bytes += diff;
1972 s->dma_adc.count += diff;
1973 if (s->dma_adc.count > s->dma_adc.dmasize)
1974 s->dma_adc.count = s->dma_adc.dmasize;
1975 if (s->dma_adc.mapped) {
1976 if (s->dma_adc.count >=
1977 (signed) s->dma_adc.fragsize) wake_up(&s->
1981 if (s->dma_adc.count > 0)
1982 wake_up(&s->dma_adc.wait);
1984 CS_DBGOUT(CS_PARMS, 8, printk(KERN_INFO
1985 "cs4281: cs4281_update_ptr(): s=%p hwptr=%d total_bytes=%d count=%d \n",
1986 s, s->dma_adc.hwptr, s->dma_adc.total_bytes, s->dma_adc.count));
1988 // update DAC pointer
1990 // check for end of buffer, means that we are going to wait for another interrupt
1991 // to allow silence to fill the fifos on the part, to keep pops down to a minimum.
1993 if (s->ena & FMODE_WRITE) {
1994 hwptr = readl(s->pBA0 + BA0_DCA0); // Read play DMA address.
1995 va = virt_to_bus(s->dma_dac.rawbuf);
1996 hwptr -= (unsigned) va;
1997 diff = (s->dma_dac.dmasize + hwptr -
1998 s->dma_dac.hwptr) % s->dma_dac.dmasize;
1999 s->dma_dac.hwptr = hwptr;
2000 s->dma_dac.total_bytes += diff;
2001 if (s->dma_dac.mapped) {
2002 s->dma_dac.count += diff;
2003 if (s->dma_dac.count >= s->dma_dac.fragsize) {
2004 s->dma_dac.wakeup = 1;
2005 wake_up(&s->dma_dac.wait);
2006 if (s->dma_dac.count > s->dma_dac.dmasize)
2008 s->dma_dac.dmasize - 1;
2011 s->dma_dac.count -= diff;
2012 if (s->dma_dac.count <= 0) {
2014 // fill with silence, and do not shut down the DAC.
2015 // Continue to play silence until the _release.
2017 CS_DBGOUT(CS_WAVE_WRITE, 6, printk(KERN_INFO
2018 "cs4281: cs4281_update_ptr(): memset %d at %p for %d size \n",
2019 (unsigned)(s->prop_dac.fmt &
2020 (AFMT_U8 | AFMT_U16_LE)) ? 0x80 : 0,
2021 s->dma_dac.rawbuf, s->dma_dac.dmasize));
2022 memset(s->dma_dac.rawbuf,
2024 fmt & (AFMT_U8 | AFMT_U16_LE)) ?
2025 0x80 : 0, s->dma_dac.dmasize);
2026 if (s->dma_dac.count < 0) {
2027 s->dma_dac.underrun = 1;
2028 s->dma_dac.count = 0;
2029 CS_DBGOUT(CS_ERROR, 9, printk(KERN_INFO
2030 "cs4281: cs4281_update_ptr(): underrun\n"));
2032 } else if (s->dma_dac.count <=
2033 (signed) s->dma_dac.fragsize
2034 && !s->dma_dac.endcleared) {
2035 clear_advance(s->dma_dac.rawbuf,
2038 s->dma_dac.fragsize,
2041 AFMT_U16_LE)) ? 0x80
2043 s->dma_dac.endcleared = 1;
2045 if ( (s->dma_dac.count <= (signed) s->dma_dac.dmasize/2) ||
2048 wake_up(&s->dma_dac.wait);
2051 CS_DBGOUT(CS_PARMS, 8, printk(KERN_INFO
2052 "cs4281: cs4281_update_ptr(): s=%p hwptr=%d total_bytes=%d count=%d \n",
2053 s, s->dma_dac.hwptr, s->dma_dac.total_bytes, s->dma_dac.count));
2058 // ---------------------------------------------------------------------
2060 static void prog_codec(struct cs4281_state *s, unsigned type)
2062 unsigned long flags;
2063 unsigned temp1, format;
2065 CS_DBGOUT(CS_FUNCTION, 2,
2066 printk(KERN_INFO "cs4281: prog_codec()+ \n"));
2068 spin_lock_irqsave(&s->lock, flags);
2069 if (type == CS_TYPE_ADC) {
2070 temp1 = readl(s->pBA0 + BA0_DCR1);
2071 writel(temp1 | DCRn_MSK, s->pBA0 + BA0_DCR1); // Stop capture DMA, if active.
2073 // program sampling rates
2074 // Note, for CS4281, capture & play rates can be set independently.
2075 cs4281_record_rate(s, s->prop_adc.rate);
2077 // program ADC parameters
2078 format = DMRn_DMA | DMRn_AUTO | DMRn_TR_WRITE;
2080 fmt & (AFMT_S16_LE | AFMT_U16_LE | AFMT_S16_BE | AFMT_U16_BE)) { // 16-bit
2081 if (s->prop_adc.fmt & (AFMT_S16_BE | AFMT_U16_BE)) // Big-endian?
2082 format |= DMRn_BEND;
2083 if (s->prop_adc.fmt & (AFMT_U16_LE | AFMT_U16_BE))
2084 format |= DMRn_USIGN; // Unsigned.
2086 format |= DMRn_SIZE8 | DMRn_USIGN; // 8-bit, unsigned
2087 if (s->prop_adc.channels < 2)
2088 format |= DMRn_MONO;
2090 writel(format, s->pBA0 + BA0_DMR1);
2092 CS_DBGOUT(CS_PARMS, 2, printk(KERN_INFO
2093 "cs4281: prog_codec(): adc %s %s %s rate=%d DMR0 format=0x%.8x\n",
2094 (format & DMRn_SIZE8) ? "8" : "16",
2095 (format & DMRn_USIGN) ? "Unsigned" : "Signed",
2096 (format & DMRn_MONO) ? "Mono" : "Stereo",
2097 s->prop_adc.rate, format));
2099 s->ena &= ~FMODE_READ; // not capturing data yet
2103 if (type == CS_TYPE_DAC) {
2104 temp1 = readl(s->pBA0 + BA0_DCR0);
2105 writel(temp1 | DCRn_MSK, s->pBA0 + BA0_DCR0); // Stop play DMA, if active.
2107 // program sampling rates
2108 // Note, for CS4281, capture & play rates can be set independently.
2109 cs4281_play_rate(s, s->prop_dac.rate);
2111 // program DAC parameters
2112 format = DMRn_DMA | DMRn_AUTO | DMRn_TR_READ;
2114 fmt & (AFMT_S16_LE | AFMT_U16_LE | AFMT_S16_BE | AFMT_U16_BE)) { // 16-bit
2115 if (s->prop_dac.fmt & (AFMT_S16_BE | AFMT_U16_BE))
2116 format |= DMRn_BEND; // Big Endian.
2117 if (s->prop_dac.fmt & (AFMT_U16_LE | AFMT_U16_BE))
2118 format |= DMRn_USIGN; // Unsigned.
2120 format |= DMRn_SIZE8 | DMRn_USIGN; // 8-bit, unsigned
2122 if (s->prop_dac.channels < 2)
2123 format |= DMRn_MONO;
2125 writel(format, s->pBA0 + BA0_DMR0);
2128 CS_DBGOUT(CS_PARMS, 2, printk(KERN_INFO
2129 "cs4281: prog_codec(): dac %s %s %s rate=%d DMR0 format=0x%.8x\n",
2130 (format & DMRn_SIZE8) ? "8" : "16",
2131 (format & DMRn_USIGN) ? "Unsigned" : "Signed",
2132 (format & DMRn_MONO) ? "Mono" : "Stereo",
2133 s->prop_dac.rate, format));
2135 s->ena &= ~FMODE_WRITE; // not capturing data yet
2138 spin_unlock_irqrestore(&s->lock, flags);
2139 CS_DBGOUT(CS_FUNCTION, 2,
2140 printk(KERN_INFO "cs4281: prog_codec()- \n"));
2144 static int mixer_ioctl(struct cs4281_state *s, unsigned int cmd,
2147 // Index to mixer_src[] is value of AC97 Input Mux Select Reg.
2148 // Value of array member is recording source Device ID Mask.
2149 static const unsigned int mixer_src[8] = {
2150 SOUND_MASK_MIC, SOUND_MASK_CD, 0, SOUND_MASK_LINE1,
2151 SOUND_MASK_LINE, SOUND_MASK_VOLUME, 0, 0
2153 void __user *argp = (void __user *)arg;
2155 // Index of mixtable1[] member is Device ID
2156 // and must be <= SOUND_MIXER_NRDEVICES.
2157 // Value of array member is index into s->mix.vol[]
2158 static const unsigned char mixtable1[SOUND_MIXER_NRDEVICES] = {
2159 [SOUND_MIXER_PCM] = 1, // voice
2160 [SOUND_MIXER_LINE1] = 2, // AUX
2161 [SOUND_MIXER_CD] = 3, // CD
2162 [SOUND_MIXER_LINE] = 4, // Line
2163 [SOUND_MIXER_SYNTH] = 5, // FM
2164 [SOUND_MIXER_MIC] = 6, // Mic
2165 [SOUND_MIXER_SPEAKER] = 7, // Speaker
2166 [SOUND_MIXER_RECLEV] = 8, // Recording level
2167 [SOUND_MIXER_VOLUME] = 9 // Master Volume
2171 static const unsigned mixreg[] = {
2172 BA0_AC97_PCM_OUT_VOLUME,
2173 BA0_AC97_AUX_VOLUME,
2175 BA0_AC97_LINE_IN_VOLUME
2177 unsigned char l, r, rl, rr, vidx;
2178 unsigned char attentbl[11] =
2179 { 63, 42, 26, 17, 14, 11, 8, 6, 4, 2, 0 };
2184 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO
2185 "cs4281: mixer_ioctl(): s=%p cmd=0x%.8x\n", s, cmd));
2189 #if CSDEBUG_INTERFACE
2191 if ((cmd == SOUND_MIXER_CS_GETDBGMASK) ||
2192 (cmd == SOUND_MIXER_CS_SETDBGMASK) ||
2193 (cmd == SOUND_MIXER_CS_GETDBGLEVEL) ||
2194 (cmd == SOUND_MIXER_CS_SETDBGLEVEL) ||
2195 (cmd == SOUND_MIXER_CS_APM))
2199 case SOUND_MIXER_CS_GETDBGMASK:
2200 return put_user(cs_debugmask,
2201 (unsigned long __user *) argp);
2203 case SOUND_MIXER_CS_GETDBGLEVEL:
2204 return put_user(cs_debuglevel,
2205 (unsigned long __user *) argp);
2207 case SOUND_MIXER_CS_SETDBGMASK:
2208 if (get_user(val, (unsigned long __user *) argp))
2213 case SOUND_MIXER_CS_SETDBGLEVEL:
2214 if (get_user(val, (unsigned long __user *) argp))
2216 cs_debuglevel = val;
2218 #ifndef NOT_CS4281_PM
2219 case SOUND_MIXER_CS_APM:
2220 if (get_user(val, (unsigned long __user *) argp))
2222 if(val == CS_IOCTL_CMD_SUSPEND)
2224 else if(val == CS_IOCTL_CMD_RESUME)
2228 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
2229 "cs4281: mixer_ioctl(): invalid APM cmd (%d)\n",
2235 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
2236 "cs4281: mixer_ioctl(): ERROR unknown debug cmd\n"));
2242 if (cmd == SOUND_MIXER_PRIVATE1) {
2243 // enable/disable/query mixer preamp
2244 if (get_user(val, (int __user *) argp))
2247 cs4281_read_ac97(s, BA0_AC97_MIC_VOLUME, &temp1);
2248 temp1 = val ? (temp1 | 0x40) : (temp1 & 0xffbf);
2249 cs4281_write_ac97(s, BA0_AC97_MIC_VOLUME, temp1);
2251 cs4281_read_ac97(s, BA0_AC97_MIC_VOLUME, &temp1);
2252 val = (temp1 & 0x40) ? 1 : 0;
2253 return put_user(val, (int __user *) argp);
2255 if (cmd == SOUND_MIXER_PRIVATE2) {
2256 // enable/disable/query spatializer
2257 if (get_user(val, (int __user *)argp))
2260 temp1 = (val & 0x3f) >> 2;
2261 cs4281_write_ac97(s, BA0_AC97_3D_CONTROL, temp1);
2262 cs4281_read_ac97(s, BA0_AC97_GENERAL_PURPOSE,
2264 cs4281_write_ac97(s, BA0_AC97_GENERAL_PURPOSE,
2267 cs4281_read_ac97(s, BA0_AC97_3D_CONTROL, &temp1);
2268 return put_user((temp1 << 2) | 3, (int __user *)argp);
2270 if (cmd == SOUND_MIXER_INFO) {
2272 strlcpy(info.id, "CS4281", sizeof(info.id));
2273 strlcpy(info.name, "Crystal CS4281", sizeof(info.name));
2274 info.modify_counter = s->mix.modcnt;
2275 if (copy_to_user(argp, &info, sizeof(info)))
2279 if (cmd == SOUND_OLD_MIXER_INFO) {
2280 _old_mixer_info info;
2281 strlcpy(info.id, "CS4281", sizeof(info.id));
2282 strlcpy(info.name, "Crystal CS4281", sizeof(info.name));
2283 if (copy_to_user(argp, &info, sizeof(info)))
2287 if (cmd == OSS_GETVERSION)
2288 return put_user(SOUND_VERSION, (int __user *) argp);
2290 if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
2293 // If ioctl has only the SIOC_READ bit(bit 31)
2294 // on, process the only-read commands.
2295 if (_SIOC_DIR(cmd) == _SIOC_READ) {
2296 switch (_IOC_NR(cmd)) {
2297 case SOUND_MIXER_RECSRC: // Arg contains a bit for each recording source
2298 cs4281_read_ac97(s, BA0_AC97_RECORD_SELECT, &temp1);
2299 return put_user(mixer_src[temp1&7], (int __user *)argp);
2301 case SOUND_MIXER_DEVMASK: // Arg contains a bit for each supported device
2302 return put_user(SOUND_MASK_PCM | SOUND_MASK_SYNTH |
2303 SOUND_MASK_CD | SOUND_MASK_LINE |
2304 SOUND_MASK_LINE1 | SOUND_MASK_MIC |
2307 SOUND_MASK_SPEAKER, (int __user *)argp);
2309 case SOUND_MIXER_RECMASK: // Arg contains a bit for each supported recording source
2310 return put_user(SOUND_MASK_LINE | SOUND_MASK_MIC |
2311 SOUND_MASK_CD | SOUND_MASK_VOLUME |
2312 SOUND_MASK_LINE1, (int __user *) argp);
2314 case SOUND_MIXER_STEREODEVS: // Mixer channels supporting stereo
2315 return put_user(SOUND_MASK_PCM | SOUND_MASK_SYNTH |
2316 SOUND_MASK_CD | SOUND_MASK_LINE |
2317 SOUND_MASK_LINE1 | SOUND_MASK_MIC |
2319 SOUND_MASK_RECLEV, (int __user *)argp);
2321 case SOUND_MIXER_CAPS:
2322 return put_user(SOUND_CAP_EXCL_INPUT, (int __user *)argp);
2326 if (i >= SOUND_MIXER_NRDEVICES
2327 || !(vidx = mixtable1[i]))
2329 return put_user(s->mix.vol[vidx - 1], (int __user *)argp);
2332 // If ioctl doesn't have both the SIOC_READ and
2333 // the SIOC_WRITE bit set, return invalid.
2334 if (_SIOC_DIR(cmd) != (_SIOC_READ | _SIOC_WRITE))
2337 // Increment the count of volume writes.
2340 // Isolate the command; it must be a write.
2341 switch (_IOC_NR(cmd)) {
2343 case SOUND_MIXER_RECSRC: // Arg contains a bit for each recording source
2344 if (get_user(val, (int __user *)argp))
2346 i = hweight32(val); // i = # bits on in val.
2347 if (i != 1) // One & only 1 bit must be on.
2349 for (i = 0; i < sizeof(mixer_src) / sizeof(int); i++) {
2350 if (val == mixer_src[i]) {
2351 temp1 = (i << 8) | i;
2352 cs4281_write_ac97(s,
2353 BA0_AC97_RECORD_SELECT,
2360 case SOUND_MIXER_VOLUME:
2361 if (get_user(val, (int __user *)argp))
2365 l = 100; // Max soundcard.h vol is 100.
2370 rl = attentbl[(10 * l) / 100]; // Convert 0-100 vol to 63-0 atten.
2372 r = (val >> 8) & 0xff;
2374 r = 100; // Max right volume is 100, too
2379 rr = attentbl[(10 * r) / 100]; // Convert volume to attenuation.
2381 if ((rl > 60) && (rr > 60)) // If both l & r are 'low',
2382 temp1 = 0x8000; // turn on the mute bit.
2386 temp1 |= (rl << 8) | rr;
2388 cs4281_write_ac97(s, BA0_AC97_MASTER_VOLUME, temp1);
2389 cs4281_write_ac97(s, BA0_AC97_HEADPHONE_VOLUME, temp1);
2391 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
2392 s->mix.vol[8] = ((unsigned int) r << 8) | l;
2394 s->mix.vol[8] = val;
2396 return put_user(s->mix.vol[8], (int __user *)argp);
2398 case SOUND_MIXER_SPEAKER:
2399 if (get_user(val, (int __user *)argp))
2408 rl = (l * 2 - 5) / 13; // Convert 0-100 range to 0-15.
2409 l = (rl * 13 + 5) / 2;
2417 rl = 15 - rl; // Convert volume to attenuation.
2419 cs4281_write_ac97(s, BA0_AC97_PC_BEEP_VOLUME, temp1);
2421 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
2422 s->mix.vol[6] = l << 8;
2424 s->mix.vol[6] = val;
2426 return put_user(s->mix.vol[6], (int __user *)argp);
2428 case SOUND_MIXER_RECLEV:
2429 if (get_user(val, (int __user *)argp))
2434 r = (val >> 8) & 0xff;
2437 rl = (l * 2 - 5) / 13; // Convert 0-100 scale to 0-15.
2438 rr = (r * 2 - 5) / 13;
2439 if (rl < 3 && rr < 3)
2444 temp1 = temp1 | (rl << 8) | rr;
2445 cs4281_write_ac97(s, BA0_AC97_RECORD_GAIN, temp1);
2447 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
2448 s->mix.vol[7] = ((unsigned int) r << 8) | l;
2450 s->mix.vol[7] = val;
2452 return put_user(s->mix.vol[7], (int __user *)argp);
2454 case SOUND_MIXER_MIC:
2455 if (get_user(val, (int __user *)argp))
2464 rl = ((unsigned) l * 5 - 4) / 16; // Convert 0-100 range to 0-31.
2465 l = (rl * 16 + 4) / 5;
2467 cs4281_read_ac97(s, BA0_AC97_MIC_VOLUME, &temp1);
2468 temp1 &= 0x40; // Isolate 20db gain bit.
2473 rl = 31 - rl; // Convert volume to attenuation.
2475 cs4281_write_ac97(s, BA0_AC97_MIC_VOLUME, temp1);
2477 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
2478 s->mix.vol[5] = val << 8;
2480 s->mix.vol[5] = val;
2482 return put_user(s->mix.vol[5], (int __user *)argp);
2485 case SOUND_MIXER_SYNTH:
2486 if (get_user(val, (int __user *)argp))
2491 if (get_user(val, (int __user *)argp))
2493 r = (val >> 8) & 0xff;
2496 rl = (l * 2 - 11) / 3; // Convert 0-100 range to 0-63.
2497 rr = (r * 2 - 11) / 3;
2498 if (rl < 3) // If l is low, turn on
2499 temp1 = 0x0080; // the mute bit.
2503 rl = 63 - rl; // Convert vol to attenuation.
2504 writel(temp1 | rl, s->pBA0 + BA0_FMLVC);
2505 if (rr < 3) // If rr is low, turn on
2506 temp1 = 0x0080; // the mute bit.
2509 rr = 63 - rr; // Convert vol to attenuation.
2510 writel(temp1 | rr, s->pBA0 + BA0_FMRVC);
2512 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
2513 s->mix.vol[4] = (r << 8) | l;
2515 s->mix.vol[4] = val;
2517 return put_user(s->mix.vol[4], (int __user *)argp);
2521 CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
2522 "cs4281: mixer_ioctl(): default\n"));
2525 if (i >= SOUND_MIXER_NRDEVICES || !(vidx = mixtable1[i]))
2527 if (get_user(val, (int __user *)argp))
2536 rl = (attentbl[(l * 10) / 100]) >> 1;
2538 r = (val >> 8) & 0xff;
2545 rr = (attentbl[(r * 10) / 100]) >> 1;
2546 if ((rl > 30) && (rr > 30))
2550 temp1 = temp1 | (rl << 8) | rr;
2551 cs4281_write_ac97(s, mixreg[vidx - 1], temp1);
2553 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
2554 s->mix.vol[vidx - 1] = ((unsigned int) r << 8) | l;
2556 s->mix.vol[vidx - 1] = val;
2558 #ifndef NOT_CS4281_PM
2559 CS_DBGOUT(CS_PM, 9, printk(KERN_INFO
2560 "write ac97 mixreg[%d]=0x%x mix.vol[]=0x%x\n",
2561 vidx-1,temp1,s->mix.vol[vidx-1]));
2563 return put_user(s->mix.vol[vidx - 1], (int __user *)argp);
2568 // ---------------------------------------------------------------------
2570 static int cs4281_open_mixdev(struct inode *inode, struct file *file)
2572 unsigned int minor = iminor(inode);
2573 struct cs4281_state *s=NULL;
2574 struct list_head *entry;
2576 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
2577 printk(KERN_INFO "cs4281: cs4281_open_mixdev()+\n"));
2579 list_for_each(entry, &cs4281_devs)
2581 s = list_entry(entry, struct cs4281_state, list);
2582 if(s->dev_mixer == minor)
2587 CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
2588 printk(KERN_INFO "cs4281: cs4281_open_mixdev()- -ENODEV\n"));
2592 file->private_data = s;
2594 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
2595 printk(KERN_INFO "cs4281: cs4281_open_mixdev()- 0\n"));
2597 return nonseekable_open(inode, file);
2601 static int cs4281_release_mixdev(struct inode *inode, struct file *file)
2603 struct cs4281_state *s =
2604 (struct cs4281_state *) file->private_data;
2611 static int cs4281_ioctl_mixdev(struct inode *inode, struct file *file,
2612 unsigned int cmd, unsigned long arg)
2614 return mixer_ioctl((struct cs4281_state *) file->private_data, cmd,
2619 // ******************************************************************************************
2620 // Mixer file operations struct.
2621 // ******************************************************************************************
2622 static /*const */ struct file_operations cs4281_mixer_fops = {
2623 .owner = THIS_MODULE,
2624 .llseek = no_llseek,
2625 .ioctl = cs4281_ioctl_mixdev,
2626 .open = cs4281_open_mixdev,
2627 .release = cs4281_release_mixdev,
2630 // ---------------------------------------------------------------------
2633 static int drain_adc(struct cs4281_state *s, int nonblock)
2635 DECLARE_WAITQUEUE(wait, current);
2636 unsigned long flags;
2640 if (s->dma_adc.mapped)
2642 add_wait_queue(&s->dma_adc.wait, &wait);
2644 set_current_state(TASK_INTERRUPTIBLE);
2645 spin_lock_irqsave(&s->lock, flags);
2646 count = s->dma_adc.count;
2647 CS_DBGOUT(CS_FUNCTION, 2,
2648 printk(KERN_INFO "cs4281: drain_adc() %d\n", count));
2649 spin_unlock_irqrestore(&s->lock, flags);
2651 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO
2652 "cs4281: drain_adc() count<0\n"));
2655 if (signal_pending(current))
2658 remove_wait_queue(&s->dma_adc.wait, &wait);
2659 current->state = TASK_RUNNING;
2664 s->dma_adc.fragsize) / 2 / s->prop_adc.rate;
2665 if (s->prop_adc.fmt & (AFMT_S16_LE | AFMT_U16_LE))
2667 if (s->prop_adc.channels > 1)
2669 if (!schedule_timeout(tmo + 1))
2670 printk(KERN_DEBUG "cs4281: dma timed out??\n");
2672 remove_wait_queue(&s->dma_adc.wait, &wait);
2673 current->state = TASK_RUNNING;
2674 if (signal_pending(current))
2675 return -ERESTARTSYS;
2679 static int drain_dac(struct cs4281_state *s, int nonblock)
2681 DECLARE_WAITQUEUE(wait, current);
2682 unsigned long flags;
2686 if (s->dma_dac.mapped)
2688 add_wait_queue(&s->dma_dac.wait, &wait);
2690 set_current_state(TASK_INTERRUPTIBLE);
2691 spin_lock_irqsave(&s->lock, flags);
2692 count = s->dma_dac.count;
2693 spin_unlock_irqrestore(&s->lock, flags);
2696 if (signal_pending(current))
2699 remove_wait_queue(&s->dma_dac.wait, &wait);
2700 current->state = TASK_RUNNING;
2705 s->dma_dac.fragsize) / 2 / s->prop_dac.rate;
2706 if (s->prop_dac.fmt & (AFMT_S16_LE | AFMT_U16_LE))
2708 if (s->prop_dac.channels > 1)
2710 if (!schedule_timeout(tmo + 1))
2711 printk(KERN_DEBUG "cs4281: dma timed out??\n");
2713 remove_wait_queue(&s->dma_dac.wait, &wait);
2714 current->state = TASK_RUNNING;
2715 if (signal_pending(current))
2716 return -ERESTARTSYS;
2720 //****************************************************************************
2722 // CopySamples copies 16-bit stereo samples from the source to the
2723 // destination, possibly converting down to either 8-bit or mono or both.
2724 // count specifies the number of output bytes to write.
2728 // dst - Pointer to a destination buffer.
2729 // src - Pointer to a source buffer
2730 // count - The number of bytes to copy into the destination buffer.
2731 // iChannels - Stereo - 2
2733 // fmt - AFMT_xxx (soundcard.h formats)
2735 // NOTES: only call this routine for conversion to 8bit from 16bit
2737 //****************************************************************************
2738 static void CopySamples(char *dst, char *src, int count, int iChannels,
2742 unsigned short *psSrc;
2745 CS_DBGOUT(CS_FUNCTION, 2,
2746 printk(KERN_INFO "cs4281: CopySamples()+ "));
2747 CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
2748 " dst=%p src=%p count=%d iChannels=%d fmt=0x%x\n",
2749 dst, src, (unsigned) count, (unsigned) iChannels, (unsigned) fmt));
2751 // Gershwin does format conversion in hardware so normally
2752 // we don't do any host based coversion. The data formatter
2753 // truncates 16 bit data to 8 bit and that causes some hiss.
2754 // We have already forced the HW to do 16 bit sampling and
2755 // 2 channel so that we can use software to round instead
2759 // See if the data should be output as 8-bit unsigned stereo.
2760 // or if the data should be output at 8-bit unsigned mono.
2762 if ( ((iChannels == 2) && (fmt & AFMT_U8)) ||
2763 ((iChannels == 1) && (fmt & AFMT_U8)) ) {
2765 // Convert each 16-bit unsigned stereo sample to 8-bit unsigned
2766 // stereo using rounding.
2768 psSrc = (unsigned short *) src;
2771 lAudioSample = (long) psSrc[count] + (long) 0x80;
2772 if (lAudioSample > 0xffff) {
2773 lAudioSample = 0xffff;
2775 dst[count] = (char) (lAudioSample >> 8);
2779 // check for 8-bit signed stereo.
2781 else if ((iChannels == 2) && (fmt & AFMT_S8)) {
2783 // Convert each 16-bit stereo sample to 8-bit stereo using rounding.
2785 psSrc = (short *) src;
2788 (((long) psSrc[0] + (long) psSrc[1]) / 2);
2790 *dst++ = (char) ((short) lAudioSample >> 8);
2794 // Otherwise, the data should be output as 8-bit signed mono.
2796 else if ((iChannels == 1) && (fmt & AFMT_S8)) {
2798 // Convert each 16-bit signed mono sample to 8-bit signed mono
2801 psSrc = (short *) src;
2805 (((long) psSrc[0] + (long) psSrc[1]) / 2);
2806 if (lAudioSample > 0x7fff) {
2807 lAudioSample = 0x7fff;
2810 *dst++ = (char) ((short) lAudioSample >> 8);
2816 // cs_copy_to_user()
2817 // replacement for the standard copy_to_user, to allow for a conversion from
2818 // 16 bit to 8 bit if the record conversion is active. the cs4281 has some
2819 // issues with 8 bit capture, so the driver always captures data in 16 bit
2820 // and then if the user requested 8 bit, converts from 16 to 8 bit.
2822 static unsigned cs_copy_to_user(struct cs4281_state *s, void __user *dest,
2823 unsigned *hwsrc, unsigned cnt,
2826 void *src = hwsrc; //default to the standard destination buffer addr
2828 CS_DBGOUT(CS_FUNCTION, 6, printk(KERN_INFO
2829 "cs_copy_to_user()+ fmt=0x%x fmt_o=0x%x cnt=%d dest=%p\n",
2830 s->prop_adc.fmt, s->prop_adc.fmt_original,
2831 (unsigned) cnt, dest));
2833 if (cnt > s->dma_adc.dmasize) {
2834 cnt = s->dma_adc.dmasize;
2840 if (s->conversion) {
2845 CopySamples(s->tmpbuff, (void *) hwsrc, cnt,
2846 (unsigned) s->prop_adc.channels,
2847 s->prop_adc.fmt_original);
2852 if (copy_to_user(dest, src, cnt)) {
2857 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO
2858 "cs4281: cs_copy_to_user()- copied bytes is %d \n", cnt));
2862 // ---------------------------------------------------------------------
2864 static ssize_t cs4281_read(struct file *file, char __user *buffer, size_t count,
2867 struct cs4281_state *s =
2868 (struct cs4281_state *) file->private_data;
2870 unsigned long flags;
2873 unsigned copied = 0;
2875 CS_DBGOUT(CS_FUNCTION | CS_WAVE_READ, 2,
2876 printk(KERN_INFO "cs4281: cs4281_read()+ %Zu \n", count));
2879 if (s->dma_adc.mapped)
2881 if (!s->dma_adc.ready && (ret = prog_dmabuf_adc(s)))
2883 if (!access_ok(VERIFY_WRITE, buffer, count))
2887 // "count" is the amount of bytes to read (from app), is decremented each loop
2888 // by the amount of bytes that have been returned to the user buffer.
2889 // "cnt" is the running total of each read from the buffer (changes each loop)
2890 // "buffer" points to the app's buffer
2891 // "ret" keeps a running total of the amount of bytes that have been copied
2892 // to the user buffer.
2893 // "copied" is the total bytes copied into the user buffer for each loop.
2896 CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
2897 "_read() count>0 count=%Zu .count=%d .swptr=%d .hwptr=%d \n",
2898 count, s->dma_adc.count,
2899 s->dma_adc.swptr, s->dma_adc.hwptr));
2900 spin_lock_irqsave(&s->lock, flags);
2902 // get the current copy point of the sw buffer
2903 swptr = s->dma_adc.swptr;
2905 // cnt is the amount of unread bytes from the end of the
2906 // hw buffer to the current sw pointer
2907 cnt = s->dma_adc.dmasize - swptr;
2909 // dma_adc.count is the current total bytes that have not been read.
2910 // if the amount of unread bytes from the current sw pointer to the
2911 // end of the buffer is greater than the current total bytes that
2912 // have not been read, then set the "cnt" (unread bytes) to the
2913 // amount of unread bytes.
2915 if (s->dma_adc.count < cnt)
2916 cnt = s->dma_adc.count;
2917 spin_unlock_irqrestore(&s->lock, flags);
2919 // if we are converting from 8/16 then we need to copy
2920 // twice the number of 16 bit bytes then 8 bit bytes.
2922 if (s->conversion) {
2923 if (cnt > (count * 2))
2930 // "cnt" NOW is the smaller of the amount that will be read,
2931 // and the amount that is requested in this read (or partial).
2932 // if there are no bytes in the buffer to read, then start the
2933 // ADC and wait for the interrupt handler to wake us up.
2937 // start up the dma engine and then continue back to the top of
2938 // the loop when wake up occurs.
2940 if (file->f_flags & O_NONBLOCK)
2941 return ret ? ret : -EAGAIN;
2942 interruptible_sleep_on(&s->dma_adc.wait);
2943 if (signal_pending(current))
2944 return ret ? ret : -ERESTARTSYS;
2947 // there are bytes in the buffer to read.
2948 // copy from the hw buffer over to the user buffer.
2949 // user buffer is designated by "buffer"
2950 // virtual address to copy from is rawbuf+swptr
2951 // the "cnt" is the number of bytes to read.
2953 CS_DBGOUT(CS_WAVE_READ, 2, printk(KERN_INFO
2954 "_read() copy_to cnt=%d count=%Zu ", cnt, count));
2955 CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
2956 " .dmasize=%d .count=%d buffer=%p ret=%Zd\n",
2957 s->dma_adc.dmasize, s->dma_adc.count, buffer, ret));
2960 (s, buffer, s->dma_adc.rawbuf + swptr, cnt, &copied))
2961 return ret ? ret : -EFAULT;
2962 swptr = (swptr + cnt) % s->dma_adc.dmasize;
2963 spin_lock_irqsave(&s->lock, flags);
2964 s->dma_adc.swptr = swptr;
2965 s->dma_adc.count -= cnt;
2966 spin_unlock_irqrestore(&s->lock, flags);
2972 CS_DBGOUT(CS_FUNCTION | CS_WAVE_READ, 2,
2973 printk(KERN_INFO "cs4281: cs4281_read()- %Zd\n", ret));
2978 static ssize_t cs4281_write(struct file *file, const char __user *buffer,
2979 size_t count, loff_t * ppos)
2981 struct cs4281_state *s =
2982 (struct cs4281_state *) file->private_data;
2984 unsigned long flags;
2985 unsigned swptr, hwptr, busaddr;
2988 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE, 2,
2989 printk(KERN_INFO "cs4281: cs4281_write()+ count=%Zu\n",
2993 if (s->dma_dac.mapped)
2995 if (!s->dma_dac.ready && (ret = prog_dmabuf_dac(s)))
2997 if (!access_ok(VERIFY_READ, buffer, count))
3001 spin_lock_irqsave(&s->lock, flags);
3002 if (s->dma_dac.count < 0) {
3003 s->dma_dac.count = 0;
3004 s->dma_dac.swptr = s->dma_dac.hwptr;
3006 if (s->dma_dac.underrun) {
3007 s->dma_dac.underrun = 0;
3008 hwptr = readl(s->pBA0 + BA0_DCA0);
3009 busaddr = virt_to_bus(s->dma_dac.rawbuf);
3010 hwptr -= (unsigned) busaddr;
3011 s->dma_dac.swptr = s->dma_dac.hwptr = hwptr;
3013 swptr = s->dma_dac.swptr;
3014 cnt = s->dma_dac.dmasize - swptr;
3015 if (s->dma_dac.count + cnt > s->dma_dac.dmasize)
3016 cnt = s->dma_dac.dmasize - s->dma_dac.count;
3017 spin_unlock_irqrestore(&s->lock, flags);
3022 if (file->f_flags & O_NONBLOCK)
3023 return ret ? ret : -EAGAIN;
3024 interruptible_sleep_on(&s->dma_dac.wait);
3025 if (signal_pending(current))
3026 return ret ? ret : -ERESTARTSYS;
3029 if (copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt))
3030 return ret ? ret : -EFAULT;
3031 swptr = (swptr + cnt) % s->dma_dac.dmasize;
3032 spin_lock_irqsave(&s->lock, flags);
3033 s->dma_dac.swptr = swptr;
3034 s->dma_dac.count += cnt;
3035 s->dma_dac.endcleared = 0;
3036 spin_unlock_irqrestore(&s->lock, flags);
3042 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE, 2,
3043 printk(KERN_INFO "cs4281: cs4281_write()- %Zd\n", ret));
3048 static unsigned int cs4281_poll(struct file *file,
3049 struct poll_table_struct *wait)
3051 struct cs4281_state *s =
3052 (struct cs4281_state *) file->private_data;
3053 unsigned long flags;
3054 unsigned int mask = 0;
3056 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
3057 printk(KERN_INFO "cs4281: cs4281_poll()+\n"));
3059 if (file->f_mode & FMODE_WRITE) {
3060 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
3062 "cs4281: cs4281_poll() wait on FMODE_WRITE\n"));
3063 if(!s->dma_dac.ready && prog_dmabuf_dac(s))
3065 poll_wait(file, &s->dma_dac.wait, wait);
3067 if (file->f_mode & FMODE_READ) {
3068 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
3070 "cs4281: cs4281_poll() wait on FMODE_READ\n"));
3071 if(!s->dma_dac.ready && prog_dmabuf_adc(s))
3073 poll_wait(file, &s->dma_adc.wait, wait);
3075 spin_lock_irqsave(&s->lock, flags);
3076 cs4281_update_ptr(s,CS_FALSE);
3077 if (file->f_mode & FMODE_WRITE) {
3078 if (s->dma_dac.mapped) {
3079 if (s->dma_dac.count >=
3080 (signed) s->dma_dac.fragsize) {
3081 if (s->dma_dac.wakeup)
3082 mask |= POLLOUT | POLLWRNORM;
3085 s->dma_dac.wakeup = 0;
3088 if ((signed) (s->dma_dac.dmasize/2) >= s->dma_dac.count)
3089 mask |= POLLOUT | POLLWRNORM;
3091 } else if (file->f_mode & FMODE_READ) {
3092 if (s->dma_adc.mapped) {
3093 if (s->dma_adc.count >= (signed) s->dma_adc.fragsize)
3094 mask |= POLLIN | POLLRDNORM;
3096 if (s->dma_adc.count > 0)
3097 mask |= POLLIN | POLLRDNORM;
3100 spin_unlock_irqrestore(&s->lock, flags);
3101 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
3102 printk(KERN_INFO "cs4281: cs4281_poll()- 0x%.8x\n",
3108 static int cs4281_mmap(struct file *file, struct vm_area_struct *vma)
3110 struct cs4281_state *s =
3111 (struct cs4281_state *) file->private_data;
3116 CS_DBGOUT(CS_FUNCTION | CS_PARMS | CS_OPEN, 4,
3117 printk(KERN_INFO "cs4281: cs4281_mmap()+\n"));
3120 if (vma->vm_flags & VM_WRITE) {
3121 if ((ret = prog_dmabuf_dac(s)) != 0)
3124 } else if (vma->vm_flags & VM_READ) {
3125 if ((ret = prog_dmabuf_adc(s)) != 0)
3131 // only support PLAYBACK for now
3135 if (cs4x_pgoff(vma) != 0)
3137 size = vma->vm_end - vma->vm_start;
3138 if (size > (PAGE_SIZE << db->buforder))
3140 if (remap_pfn_range(vma, vma->vm_start,
3141 virt_to_phys(db->rawbuf) >> PAGE_SHIFT,
3142 size, vma->vm_page_prot))
3146 CS_DBGOUT(CS_FUNCTION | CS_PARMS | CS_OPEN, 4,
3147 printk(KERN_INFO "cs4281: cs4281_mmap()- 0 size=%d\n",
3154 static int cs4281_ioctl(struct inode *inode, struct file *file,
3155 unsigned int cmd, unsigned long arg)
3157 struct cs4281_state *s =
3158 (struct cs4281_state *) file->private_data;
3159 unsigned long flags;
3160 audio_buf_info abinfo;
3162 int val, mapped, ret;
3163 int __user *p = (int __user *)arg;
3165 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO
3166 "cs4281: cs4281_ioctl(): file=%p cmd=0x%.8x\n", file, cmd));
3171 mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
3172 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
3174 case OSS_GETVERSION:
3175 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
3176 "cs4281: cs4281_ioctl(): SOUND_VERSION=0x%.8x\n",
3178 return put_user(SOUND_VERSION, p);
3180 case SNDCTL_DSP_SYNC:
3181 CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
3182 "cs4281: cs4281_ioctl(): DSP_SYNC\n"));
3183 if (file->f_mode & FMODE_WRITE)
3185 0 /*file->f_flags & O_NONBLOCK */
3189 case SNDCTL_DSP_SETDUPLEX:
3192 case SNDCTL_DSP_GETCAPS:
3193 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME |
3194 DSP_CAP_TRIGGER | DSP_CAP_MMAP,
3197 case SNDCTL_DSP_RESET:
3198 CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
3199 "cs4281: cs4281_ioctl(): DSP_RESET\n"));
3200 if (file->f_mode & FMODE_WRITE) {
3202 synchronize_irq(s->irq);
3203 s->dma_dac.swptr = s->dma_dac.hwptr =
3204 s->dma_dac.count = s->dma_dac.total_bytes =
3205 s->dma_dac.blocks = s->dma_dac.wakeup = 0;
3206 prog_codec(s, CS_TYPE_DAC);
3208 if (file->f_mode & FMODE_READ) {
3210 synchronize_irq(s->irq);
3211 s->dma_adc.swptr = s->dma_adc.hwptr =
3212 s->dma_adc.count = s->dma_adc.total_bytes =
3213 s->dma_adc.blocks = s->dma_dac.wakeup = 0;
3214 prog_codec(s, CS_TYPE_ADC);
3218 case SNDCTL_DSP_SPEED:
3219 if (get_user(val, p))
3221 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
3222 "cs4281: cs4281_ioctl(): DSP_SPEED val=%d\n", val));
3224 // support independent capture and playback channels
3225 // assume that the file mode bit determines the
3226 // direction of the data flow.
3228 if (file->f_mode & FMODE_READ) {
3231 s->dma_adc.ready = 0;
3232 // program sampling rates
3237 s->prop_adc.rate = val;
3238 prog_codec(s, CS_TYPE_ADC);
3241 if (file->f_mode & FMODE_WRITE) {
3244 s->dma_dac.ready = 0;
3245 // program sampling rates
3250 s->prop_dac.rate = val;
3251 prog_codec(s, CS_TYPE_DAC);
3255 if (file->f_mode & FMODE_WRITE)
3256 val = s->prop_dac.rate;
3257 else if (file->f_mode & FMODE_READ)
3258 val = s->prop_adc.rate;
3260 return put_user(val, p);
3262 case SNDCTL_DSP_STEREO:
3263 if (get_user(val, p))
3265 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
3266 "cs4281: cs4281_ioctl(): DSP_STEREO val=%d\n", val));
3267 if (file->f_mode & FMODE_READ) {
3269 s->dma_adc.ready = 0;
3270 s->prop_adc.channels = val ? 2 : 1;
3271 prog_codec(s, CS_TYPE_ADC);
3273 if (file->f_mode & FMODE_WRITE) {
3275 s->dma_dac.ready = 0;
3276 s->prop_dac.channels = val ? 2 : 1;
3277 prog_codec(s, CS_TYPE_DAC);
3281 case SNDCTL_DSP_CHANNELS:
3282 if (get_user(val, p))
3284 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
3285 "cs4281: cs4281_ioctl(): DSP_CHANNELS val=%d\n",
3288 if (file->f_mode & FMODE_READ) {
3290 s->dma_adc.ready = 0;
3292 s->prop_adc.channels = 2;
3294 s->prop_adc.channels = 1;
3295 prog_codec(s, CS_TYPE_ADC);
3297 if (file->f_mode & FMODE_WRITE) {
3299 s->dma_dac.ready = 0;
3301 s->prop_dac.channels = 2;
3303 s->prop_dac.channels = 1;
3304 prog_codec(s, CS_TYPE_DAC);
3308 if (file->f_mode & FMODE_WRITE)
3309 val = s->prop_dac.channels;
3310 else if (file->f_mode & FMODE_READ)
3311 val = s->prop_adc.channels;
3313 return put_user(val, p);
3315 case SNDCTL_DSP_GETFMTS: // Returns a mask
3316 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
3317 "cs4281: cs4281_ioctl(): DSP_GETFMT val=0x%.8x\n",
3318 AFMT_S16_LE | AFMT_U16_LE | AFMT_S8 |
3320 return put_user(AFMT_S16_LE | AFMT_U16_LE | AFMT_S8 |
3323 case SNDCTL_DSP_SETFMT:
3324 if (get_user(val, p))
3326 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
3327 "cs4281: cs4281_ioctl(): DSP_SETFMT val=0x%.8x\n",
3329 if (val != AFMT_QUERY) {
3330 if (file->f_mode & FMODE_READ) {
3332 s->dma_adc.ready = 0;
3333 if (val != AFMT_S16_LE
3334 && val != AFMT_U16_LE && val != AFMT_S8
3337 s->prop_adc.fmt = val;
3338 s->prop_adc.fmt_original = s->prop_adc.fmt;
3339 prog_codec(s, CS_TYPE_ADC);
3341 if (file->f_mode & FMODE_WRITE) {
3343 s->dma_dac.ready = 0;
3344 if (val != AFMT_S16_LE
3345 && val != AFMT_U16_LE && val != AFMT_S8
3348 s->prop_dac.fmt = val;
3349 s->prop_dac.fmt_original = s->prop_dac.fmt;
3350 prog_codec(s, CS_TYPE_DAC);
3353 if (file->f_mode & FMODE_WRITE)
3354 val = s->prop_dac.fmt_original;
3355 else if (file->f_mode & FMODE_READ)
3356 val = s->prop_adc.fmt_original;
3358 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
3359 "cs4281: cs4281_ioctl(): DSP_SETFMT return val=0x%.8x\n",
3361 return put_user(val, p);
3363 case SNDCTL_DSP_POST:
3364 CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
3365 "cs4281: cs4281_ioctl(): DSP_POST\n"));
3368 case SNDCTL_DSP_GETTRIGGER:
3370 if (file->f_mode & s->ena & FMODE_READ)
3371 val |= PCM_ENABLE_INPUT;
3372 if (file->f_mode & s->ena & FMODE_WRITE)
3373 val |= PCM_ENABLE_OUTPUT;
3374 return put_user(val, p);
3376 case SNDCTL_DSP_SETTRIGGER:
3377 if (get_user(val, p))
3379 if (file->f_mode & FMODE_READ) {
3380 if (val & PCM_ENABLE_INPUT) {
3381 if (!s->dma_adc.ready
3382 && (ret = prog_dmabuf_adc(s)))
3388 if (file->f_mode & FMODE_WRITE) {
3389 if (val & PCM_ENABLE_OUTPUT) {
3390 if (!s->dma_dac.ready
3391 && (ret = prog_dmabuf_dac(s)))
3399 case SNDCTL_DSP_GETOSPACE:
3400 if (!(file->f_mode & FMODE_WRITE))
3402 if (!s->dma_dac.ready && (val = prog_dmabuf_dac(s)))
3404 spin_lock_irqsave(&s->lock, flags);
3405 cs4281_update_ptr(s,CS_FALSE);
3406 abinfo.fragsize = s->dma_dac.fragsize;
3407 if (s->dma_dac.mapped)
3408 abinfo.bytes = s->dma_dac.dmasize;
3411 s->dma_dac.dmasize - s->dma_dac.count;
3412 abinfo.fragstotal = s->dma_dac.numfrag;
3413 abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
3414 CS_DBGOUT(CS_FUNCTION | CS_PARMS, 4, printk(KERN_INFO
3415 "cs4281: cs4281_ioctl(): GETOSPACE .fragsize=%d .bytes=%d .fragstotal=%d .fragments=%d\n",
3416 abinfo.fragsize,abinfo.bytes,abinfo.fragstotal,
3418 spin_unlock_irqrestore(&s->lock, flags);
3419 return copy_to_user(p, &abinfo,
3420 sizeof(abinfo)) ? -EFAULT : 0;
3422 case SNDCTL_DSP_GETISPACE:
3423 if (!(file->f_mode & FMODE_READ))
3425 if (!s->dma_adc.ready && (val = prog_dmabuf_adc(s)))
3427 spin_lock_irqsave(&s->lock, flags);
3428 cs4281_update_ptr(s,CS_FALSE);
3429 if (s->conversion) {
3430 abinfo.fragsize = s->dma_adc.fragsize / 2;
3431 abinfo.bytes = s->dma_adc.count / 2;
3432 abinfo.fragstotal = s->dma_adc.numfrag;
3434 abinfo.bytes >> (s->dma_adc.fragshift - 1);
3436 abinfo.fragsize = s->dma_adc.fragsize;
3437 abinfo.bytes = s->dma_adc.count;
3438 abinfo.fragstotal = s->dma_adc.numfrag;
3440 abinfo.bytes >> s->dma_adc.fragshift;
3442 spin_unlock_irqrestore(&s->lock, flags);
3443 return copy_to_user(p, &abinfo,
3444 sizeof(abinfo)) ? -EFAULT : 0;
3446 case SNDCTL_DSP_NONBLOCK:
3447 file->f_flags |= O_NONBLOCK;
3450 case SNDCTL_DSP_GETODELAY:
3451 if (!(file->f_mode & FMODE_WRITE))
3453 if(!s->dma_dac.ready && prog_dmabuf_dac(s))
3455 spin_lock_irqsave(&s->lock, flags);
3456 cs4281_update_ptr(s,CS_FALSE);
3457 val = s->dma_dac.count;
3458 spin_unlock_irqrestore(&s->lock, flags);
3459 return put_user(val, p);
3461 case SNDCTL_DSP_GETIPTR:
3462 if (!(file->f_mode & FMODE_READ))
3464 if(!s->dma_adc.ready && prog_dmabuf_adc(s))
3466 spin_lock_irqsave(&s->lock, flags);
3467 cs4281_update_ptr(s,CS_FALSE);
3468 cinfo.bytes = s->dma_adc.total_bytes;
3469 if (s->dma_adc.mapped) {
3471 (cinfo.bytes >> s->dma_adc.fragshift) -
3474 cinfo.bytes >> s->dma_adc.fragshift;
3476 if (s->conversion) {
3479 2 >> (s->dma_adc.fragshift - 1);
3482 s->dma_adc.count >> s->dma_adc.
3486 cinfo.ptr = s->dma_adc.hwptr / 2;
3488 cinfo.ptr = s->dma_adc.hwptr;
3489 if (s->dma_adc.mapped)
3490 s->dma_adc.count &= s->dma_adc.fragsize - 1;
3491 spin_unlock_irqrestore(&s->lock, flags);
3492 if (copy_to_user(p, &cinfo, sizeof(cinfo)))
3496 case SNDCTL_DSP_GETOPTR:
3497 if (!(file->f_mode & FMODE_WRITE))
3499 if(!s->dma_dac.ready && prog_dmabuf_dac(s))
3501 spin_lock_irqsave(&s->lock, flags);
3502 cs4281_update_ptr(s,CS_FALSE);
3503 cinfo.bytes = s->dma_dac.total_bytes;
3504 if (s->dma_dac.mapped) {
3506 (cinfo.bytes >> s->dma_dac.fragshift) -
3509 cinfo.bytes >> s->dma_dac.fragshift;
3512 s->dma_dac.count >> s->dma_dac.fragshift;
3514 cinfo.ptr = s->dma_dac.hwptr;
3515 if (s->dma_dac.mapped)
3516 s->dma_dac.count &= s->dma_dac.fragsize - 1;
3517 spin_unlock_irqrestore(&s->lock, flags);
3518 if (copy_to_user(p, &cinfo, sizeof(cinfo)))
3522 case SNDCTL_DSP_GETBLKSIZE:
3523 if (file->f_mode & FMODE_WRITE) {
3524 if ((val = prog_dmabuf_dac(s)))
3526 return put_user(s->dma_dac.fragsize, p);
3528 if ((val = prog_dmabuf_adc(s)))
3531 return put_user(s->dma_adc.fragsize / 2, p);
3533 return put_user(s->dma_adc.fragsize, p);
3535 case SNDCTL_DSP_SETFRAGMENT:
3536 if (get_user(val, p))
3538 return 0; // Say OK, but do nothing.
3540 case SNDCTL_DSP_SUBDIVIDE:
3541 if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision)
3542 || (file->f_mode & FMODE_WRITE
3543 && s->dma_dac.subdivision)) return -EINVAL;
3544 if (get_user(val, p))
3546 if (val != 1 && val != 2 && val != 4)
3548 if (file->f_mode & FMODE_READ)
3549 s->dma_adc.subdivision = val;
3550 else if (file->f_mode & FMODE_WRITE)
3551 s->dma_dac.subdivision = val;
3554 case SOUND_PCM_READ_RATE:
3555 if (file->f_mode & FMODE_READ)
3556 return put_user(s->prop_adc.rate, p);
3557 else if (file->f_mode & FMODE_WRITE)
3558 return put_user(s->prop_dac.rate, p);
3560 case SOUND_PCM_READ_CHANNELS:
3561 if (file->f_mode & FMODE_READ)
3562 return put_user(s->prop_adc.channels, p);
3563 else if (file->f_mode & FMODE_WRITE)
3564 return put_user(s->prop_dac.channels, p);
3566 case SOUND_PCM_READ_BITS:
3567 if (file->f_mode & FMODE_READ)
3571 fmt & (AFMT_S8 | AFMT_U8)) ? 8 : 16,
3573 else if (file->f_mode & FMODE_WRITE)
3577 fmt & (AFMT_S8 | AFMT_U8)) ? 8 : 16,
3580 case SOUND_PCM_WRITE_FILTER:
3581 case SNDCTL_DSP_SETSYNCRO:
3582 case SOUND_PCM_READ_FILTER:
3585 return mixer_ioctl(s, cmd, arg);
3589 static int cs4281_release(struct inode *inode, struct file *file)
3591 struct cs4281_state *s =
3592 (struct cs4281_state *) file->private_data;
3594 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 2, printk(KERN_INFO
3595 "cs4281: cs4281_release(): inode=%p file=%p f_mode=%d\n",
3596 inode, file, file->f_mode));
3600 if (file->f_mode & FMODE_WRITE) {
3601 drain_dac(s, file->f_flags & O_NONBLOCK);
3602 down(&s->open_sem_dac);
3604 dealloc_dmabuf(s, &s->dma_dac);
3605 s->open_mode &= ~FMODE_WRITE;
3606 up(&s->open_sem_dac);
3607 wake_up(&s->open_wait_dac);
3609 if (file->f_mode & FMODE_READ) {
3610 drain_adc(s, file->f_flags & O_NONBLOCK);
3611 down(&s->open_sem_adc);
3613 dealloc_dmabuf(s, &s->dma_adc);
3614 s->open_mode &= ~FMODE_READ;
3615 up(&s->open_sem_adc);
3616 wake_up(&s->open_wait_adc);
3621 static int cs4281_open(struct inode *inode, struct file *file)
3623 unsigned int minor = iminor(inode);
3624 struct cs4281_state *s=NULL;
3625 struct list_head *entry;
3627 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
3628 "cs4281: cs4281_open(): inode=%p file=%p f_mode=0x%x\n",
3629 inode, file, file->f_mode));
3631 list_for_each(entry, &cs4281_devs)
3633 s = list_entry(entry, struct cs4281_state, list);
3635 if (!((s->dev_audio ^ minor) & ~0xf))
3638 if (entry == &cs4281_devs)
3641 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
3642 "cs4281: cs4281_open(): Error - unable to find audio state struct\n"));
3646 file->private_data = s;
3648 // wait for device to become free
3649 if (!(file->f_mode & (FMODE_WRITE | FMODE_READ))) {
3650 CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2, printk(KERN_INFO
3651 "cs4281: cs4281_open(): Error - must open READ and/or WRITE\n"));
3654 if (file->f_mode & FMODE_WRITE) {
3655 down(&s->open_sem_dac);
3656 while (s->open_mode & FMODE_WRITE) {
3657 if (file->f_flags & O_NONBLOCK) {
3658 up(&s->open_sem_dac);
3661 up(&s->open_sem_dac);
3662 interruptible_sleep_on(&s->open_wait_dac);
3664 if (signal_pending(current))
3665 return -ERESTARTSYS;
3666 down(&s->open_sem_dac);
3669 if (file->f_mode & FMODE_READ) {
3670 down(&s->open_sem_adc);
3671 while (s->open_mode & FMODE_READ) {
3672 if (file->f_flags & O_NONBLOCK) {
3673 up(&s->open_sem_adc);
3676 up(&s->open_sem_adc);
3677 interruptible_sleep_on(&s->open_wait_adc);
3679 if (signal_pending(current))
3680 return -ERESTARTSYS;
3681 down(&s->open_sem_adc);
3684 s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
3685 if (file->f_mode & FMODE_READ) {
3686 s->prop_adc.fmt = AFMT_U8;
3687 s->prop_adc.fmt_original = s->prop_adc.fmt;
3688 s->prop_adc.channels = 1;
3689 s->prop_adc.rate = 8000;
3690 s->prop_adc.clkdiv = 96 | 0x80;
3692 s->ena &= ~FMODE_READ;
3693 s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags =
3694 s->dma_adc.subdivision = 0;
3695 up(&s->open_sem_adc);
3697 if (prog_dmabuf_adc(s)) {
3698 CS_DBGOUT(CS_OPEN | CS_ERROR, 2, printk(KERN_ERR
3699 "cs4281: adc Program dmabufs failed.\n"));
3700 cs4281_release(inode, file);
3703 prog_codec(s, CS_TYPE_ADC);
3705 if (file->f_mode & FMODE_WRITE) {
3706 s->prop_dac.fmt = AFMT_U8;
3707 s->prop_dac.fmt_original = s->prop_dac.fmt;
3708 s->prop_dac.channels = 1;
3709 s->prop_dac.rate = 8000;
3710 s->prop_dac.clkdiv = 96 | 0x80;
3712 s->ena &= ~FMODE_WRITE;
3713 s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags =
3714 s->dma_dac.subdivision = 0;
3715 up(&s->open_sem_dac);
3717 if (prog_dmabuf_dac(s)) {
3718 CS_DBGOUT(CS_OPEN | CS_ERROR, 2, printk(KERN_ERR
3719 "cs4281: dac Program dmabufs failed.\n"));
3720 cs4281_release(inode, file);
3723 prog_codec(s, CS_TYPE_DAC);
3725 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2,
3726 printk(KERN_INFO "cs4281: cs4281_open()- 0\n"));
3727 return nonseekable_open(inode, file);
3731 // ******************************************************************************************
3732 // Wave (audio) file operations struct.
3733 // ******************************************************************************************
3734 static /*const */ struct file_operations cs4281_audio_fops = {
3735 .owner = THIS_MODULE,
3736 .llseek = no_llseek,
3737 .read = cs4281_read,
3738 .write = cs4281_write,
3739 .poll = cs4281_poll,
3740 .ioctl = cs4281_ioctl,
3741 .mmap = cs4281_mmap,
3742 .open = cs4281_open,
3743 .release = cs4281_release,
3746 // ---------------------------------------------------------------------
3748 // hold spinlock for the following!
3749 static void cs4281_handle_midi(struct cs4281_state *s)
3756 while (!(readl(s->pBA0 + BA0_MIDSR) & 0x80)) {
3757 ch = readl(s->pBA0 + BA0_MIDRP);
3758 if (s->midi.icnt < MIDIINBUF) {
3759 s->midi.ibuf[s->midi.iwr] = ch;
3760 s->midi.iwr = (s->midi.iwr + 1) % MIDIINBUF;
3766 wake_up(&s->midi.iwait);
3768 while (!(readl(s->pBA0 + BA0_MIDSR) & 0x40) && s->midi.ocnt > 0) {
3769 temp1 = (s->midi.obuf[s->midi.ord]) & 0x000000ff;
3770 writel(temp1, s->pBA0 + BA0_MIDWP);
3771 s->midi.ord = (s->midi.ord + 1) % MIDIOUTBUF;
3773 if (s->midi.ocnt < MIDIOUTBUF - 16)
3777 wake_up(&s->midi.owait);
3782 static irqreturn_t cs4281_interrupt(int irq, void *dev_id, struct pt_regs *regs)
3784 struct cs4281_state *s = (struct cs4281_state *) dev_id;
3787 // fastpath out, to ease interrupt sharing
3788 temp1 = readl(s->pBA0 + BA0_HISR); // Get Int Status reg.
3790 CS_DBGOUT(CS_INTERRUPT, 6, printk(KERN_INFO
3791 "cs4281: cs4281_interrupt() BA0_HISR=0x%.8x\n", temp1));
3793 * If not DMA or MIDI interrupt, then just return.
3795 if (!(temp1 & (HISR_DMA0 | HISR_DMA1 | HISR_MIDI))) {
3796 writel(HICR_IEV | HICR_CHGM, s->pBA0 + BA0_HICR);
3797 CS_DBGOUT(CS_INTERRUPT, 9, printk(KERN_INFO
3798 "cs4281: cs4281_interrupt(): returning not cs4281 interrupt.\n"));
3802 if (temp1 & HISR_DMA0) // If play interrupt,
3803 readl(s->pBA0 + BA0_HDSR0); // clear the source.
3805 if (temp1 & HISR_DMA1) // Same for play.
3806 readl(s->pBA0 + BA0_HDSR1);
3807 writel(HICR_IEV | HICR_CHGM, s->pBA0 + BA0_HICR); // Local EOI
3809 spin_lock(&s->lock);
3810 cs4281_update_ptr(s,CS_TRUE);
3811 cs4281_handle_midi(s);
3812 spin_unlock(&s->lock);
3816 // **************************************************************************
3818 static void cs4281_midi_timer(unsigned long data)
3820 struct cs4281_state *s = (struct cs4281_state *) data;
3821 unsigned long flags;
3823 spin_lock_irqsave(&s->lock, flags);
3824 cs4281_handle_midi(s);
3825 spin_unlock_irqrestore(&s->lock, flags);
3826 s->midi.timer.expires = jiffies + 1;
3827 add_timer(&s->midi.timer);
3831 // ---------------------------------------------------------------------
3833 static ssize_t cs4281_midi_read(struct file *file, char __user *buffer,
3834 size_t count, loff_t * ppos)
3836 struct cs4281_state *s =
3837 (struct cs4281_state *) file->private_data;
3839 unsigned long flags;
3844 if (!access_ok(VERIFY_WRITE, buffer, count))
3848 spin_lock_irqsave(&s->lock, flags);
3850 cnt = MIDIINBUF - ptr;
3851 if (s->midi.icnt < cnt)
3853 spin_unlock_irqrestore(&s->lock, flags);
3857 if (file->f_flags & O_NONBLOCK)
3858 return ret ? ret : -EAGAIN;
3859 interruptible_sleep_on(&s->midi.iwait);
3860 if (signal_pending(current))
3861 return ret ? ret : -ERESTARTSYS;
3864 if (copy_to_user(buffer, s->midi.ibuf + ptr, cnt))
3865 return ret ? ret : -EFAULT;
3866 ptr = (ptr + cnt) % MIDIINBUF;
3867 spin_lock_irqsave(&s->lock, flags);
3869 s->midi.icnt -= cnt;
3870 spin_unlock_irqrestore(&s->lock, flags);
3879 static ssize_t cs4281_midi_write(struct file *file, const char __user *buffer,
3880 size_t count, loff_t * ppos)
3882 struct cs4281_state *s =
3883 (struct cs4281_state *) file->private_data;
3885 unsigned long flags;
3890 if (!access_ok(VERIFY_READ, buffer, count))
3894 spin_lock_irqsave(&s->lock, flags);
3896 cnt = MIDIOUTBUF - ptr;
3897 if (s->midi.ocnt + cnt > MIDIOUTBUF)
3898 cnt = MIDIOUTBUF - s->midi.ocnt;
3900 cs4281_handle_midi(s);
3901 spin_unlock_irqrestore(&s->lock, flags);
3905 if (file->f_flags & O_NONBLOCK)
3906 return ret ? ret : -EAGAIN;
3907 interruptible_sleep_on(&s->midi.owait);
3908 if (signal_pending(current))
3909 return ret ? ret : -ERESTARTSYS;
3912 if (copy_from_user(s->midi.obuf + ptr, buffer, cnt))
3913 return ret ? ret : -EFAULT;
3914 ptr = (ptr + cnt) % MIDIOUTBUF;
3915 spin_lock_irqsave(&s->lock, flags);
3917 s->midi.ocnt += cnt;
3918 spin_unlock_irqrestore(&s->lock, flags);
3922 spin_lock_irqsave(&s->lock, flags);
3923 cs4281_handle_midi(s);
3924 spin_unlock_irqrestore(&s->lock, flags);
3930 static unsigned int cs4281_midi_poll(struct file *file,
3931 struct poll_table_struct *wait)
3933 struct cs4281_state *s =
3934 (struct cs4281_state *) file->private_data;
3935 unsigned long flags;
3936 unsigned int mask = 0;
3939 if (file->f_flags & FMODE_WRITE)
3940 poll_wait(file, &s->midi.owait, wait);
3941 if (file->f_flags & FMODE_READ)
3942 poll_wait(file, &s->midi.iwait, wait);
3943 spin_lock_irqsave(&s->lock, flags);
3944 if (file->f_flags & FMODE_READ) {
3945 if (s->midi.icnt > 0)
3946 mask |= POLLIN | POLLRDNORM;
3948 if (file->f_flags & FMODE_WRITE) {
3949 if (s->midi.ocnt < MIDIOUTBUF)
3950 mask |= POLLOUT | POLLWRNORM;
3952 spin_unlock_irqrestore(&s->lock, flags);
3957 static int cs4281_midi_open(struct inode *inode, struct file *file)
3959 unsigned long flags, temp1;
3960 unsigned int minor = iminor(inode);
3961 struct cs4281_state *s=NULL;
3962 struct list_head *entry;
3963 list_for_each(entry, &cs4281_devs)
3965 s = list_entry(entry, struct cs4281_state, list);
3967 if (s->dev_midi == minor)
3971 if (entry == &cs4281_devs)
3975 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
3976 "cs4281: cs4281_open(): Error - unable to find audio state struct\n"));
3980 file->private_data = s;
3981 // wait for device to become free
3983 while (s->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
3984 if (file->f_flags & O_NONBLOCK) {
3989 interruptible_sleep_on(&s->open_wait);
3990 if (signal_pending(current))
3991 return -ERESTARTSYS;
3994 spin_lock_irqsave(&s->lock, flags);
3995 if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
3996 s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
3997 s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
3998 writel(1, s->pBA0 + BA0_MIDCR); // Reset the interface.
3999 writel(0, s->pBA0 + BA0_MIDCR); // Return to normal mode.
4000 s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
4001 writel(0x0000000f, s->pBA0 + BA0_MIDCR); // Enable transmit, record, ints.
4002 temp1 = readl(s->pBA0 + BA0_HIMR);
4003 writel(temp1 & 0xffbfffff, s->pBA0 + BA0_HIMR); // Enable midi int. recognition.
4004 writel(HICR_IEV | HICR_CHGM, s->pBA0 + BA0_HICR); // Enable interrupts
4005 init_timer(&s->midi.timer);
4006 s->midi.timer.expires = jiffies + 1;
4007 s->midi.timer.data = (unsigned long) s;
4008 s->midi.timer.function = cs4281_midi_timer;
4009 add_timer(&s->midi.timer);
4011 if (file->f_mode & FMODE_READ) {
4012 s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
4014 if (file->f_mode & FMODE_WRITE) {
4015 s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
4017 spin_unlock_irqrestore(&s->lock, flags);
4020 f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ |
4023 return nonseekable_open(inode, file);
4027 static int cs4281_midi_release(struct inode *inode, struct file *file)
4029 struct cs4281_state *s =
4030 (struct cs4281_state *) file->private_data;
4031 DECLARE_WAITQUEUE(wait, current);
4032 unsigned long flags;
4033 unsigned count, tmo;
4037 if (file->f_mode & FMODE_WRITE) {
4038 add_wait_queue(&s->midi.owait, &wait);
4040 set_current_state(TASK_INTERRUPTIBLE);
4041 spin_lock_irqsave(&s->lock, flags);
4042 count = s->midi.ocnt;
4043 spin_unlock_irqrestore(&s->lock, flags);
4046 if (signal_pending(current))
4048 if (file->f_flags & O_NONBLOCK) {
4049 remove_wait_queue(&s->midi.owait, &wait);
4050 current->state = TASK_RUNNING;
4053 tmo = (count * HZ) / 3100;
4054 if (!schedule_timeout(tmo ? : 1) && tmo)
4056 "cs4281: midi timed out??\n");
4058 remove_wait_queue(&s->midi.owait, &wait);
4059 current->state = TASK_RUNNING;
4063 (~(file->f_mode << FMODE_MIDI_SHIFT)) & (FMODE_MIDI_READ |
4065 spin_lock_irqsave(&s->lock, flags);
4066 if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
4067 writel(0, s->pBA0 + BA0_MIDCR); // Disable Midi interrupts.
4068 del_timer(&s->midi.timer);
4070 spin_unlock_irqrestore(&s->lock, flags);
4072 wake_up(&s->open_wait);
4076 // ******************************************************************************************
4077 // Midi file operations struct.
4078 // ******************************************************************************************
4079 static /*const */ struct file_operations cs4281_midi_fops = {
4080 .owner = THIS_MODULE,
4081 .llseek = no_llseek,
4082 .read = cs4281_midi_read,
4083 .write = cs4281_midi_write,
4084 .poll = cs4281_midi_poll,
4085 .open = cs4281_midi_open,
4086 .release = cs4281_midi_release,
4090 // ---------------------------------------------------------------------
4092 // maximum number of devices
4093 #define NR_DEVICE 8 // Only eight devices supported currently.
4095 // ---------------------------------------------------------------------
4097 static struct initvol {
4100 } initvol[] __devinitdata = {
4103 SOUND_MIXER_WRITE_VOLUME, 0x4040}, {
4104 SOUND_MIXER_WRITE_PCM, 0x4040}, {
4105 SOUND_MIXER_WRITE_SYNTH, 0x4040}, {
4106 SOUND_MIXER_WRITE_CD, 0x4040}, {
4107 SOUND_MIXER_WRITE_LINE, 0x4040}, {
4108 SOUND_MIXER_WRITE_LINE1, 0x4040}, {
4109 SOUND_MIXER_WRITE_RECLEV, 0x0000}, {
4110 SOUND_MIXER_WRITE_SPEAKER, 0x4040}, {
4111 SOUND_MIXER_WRITE_MIC, 0x0000}
4115 #ifndef NOT_CS4281_PM
4116 static void __devinit cs4281_BuildFIFO(
4117 struct cs4281_pipeline *p,
4118 struct cs4281_state *s)
4122 case 0: /* playback */
4124 p->u32FCRnAddress = BA0_FCR0;
4125 p->u32FSICnAddress = BA0_FSIC0;
4126 p->u32FPDRnAddress = BA0_FPDR0;
4129 case 1: /* capture */
4131 p->u32FCRnAddress = BA0_FCR1;
4132 p->u32FSICnAddress = BA0_FSIC1;
4133 p->u32FPDRnAddress = BA0_FPDR1;
4139 p->u32FCRnAddress = BA0_FCR2;
4140 p->u32FSICnAddress = BA0_FSIC2;
4141 p->u32FPDRnAddress = BA0_FPDR2;
4146 p->u32FCRnAddress = BA0_FCR3;
4147 p->u32FSICnAddress = BA0_FSIC3;
4148 p->u32FPDRnAddress = BA0_FPDR3;
4155 // first read the hardware to initialize the member variables
4157 p->u32FCRnValue = readl(s->pBA0 + p->u32FCRnAddress);
4158 p->u32FSICnValue = readl(s->pBA0 + p->u32FSICnAddress);
4159 p->u32FPDRnValue = readl(s->pBA0 + p->u32FPDRnAddress);
4163 static void __devinit cs4281_BuildDMAengine(
4164 struct cs4281_pipeline *p,
4165 struct cs4281_state *s)
4168 * initialize all the addresses of this pipeline dma info.
4172 case 0: /* playback */
4174 p->u32DBAnAddress = BA0_DBA0;
4175 p->u32DCAnAddress = BA0_DCA0;
4176 p->u32DBCnAddress = BA0_DBC0;
4177 p->u32DCCnAddress = BA0_DCC0;
4178 p->u32DMRnAddress = BA0_DMR0;
4179 p->u32DCRnAddress = BA0_DCR0;
4180 p->u32HDSRnAddress = BA0_HDSR0;
4184 case 1: /* capture */
4186 p->u32DBAnAddress = BA0_DBA1;
4187 p->u32DCAnAddress = BA0_DCA1;
4188 p->u32DBCnAddress = BA0_DBC1;
4189 p->u32DCCnAddress = BA0_DCC1;
4190 p->u32DMRnAddress = BA0_DMR1;
4191 p->u32DCRnAddress = BA0_DCR1;
4192 p->u32HDSRnAddress = BA0_HDSR1;
4198 p->u32DBAnAddress = BA0_DBA2;
4199 p->u32DCAnAddress = BA0_DCA2;
4200 p->u32DBCnAddress = BA0_DBC2;
4201 p->u32DCCnAddress = BA0_DCC2;
4202 p->u32DMRnAddress = BA0_DMR2;
4203 p->u32DCRnAddress = BA0_DCR2;
4204 p->u32HDSRnAddress = BA0_HDSR2;
4210 p->u32DBAnAddress = BA0_DBA3;
4211 p->u32DCAnAddress = BA0_DCA3;
4212 p->u32DBCnAddress = BA0_DBC3;
4213 p->u32DCCnAddress = BA0_DCC3;
4214 p->u32DMRnAddress = BA0_DMR3;
4215 p->u32DCRnAddress = BA0_DCR3;
4216 p->u32HDSRnAddress = BA0_HDSR3;
4224 // Initialize the dma values for this pipeline
4226 p->u32DBAnValue = readl(s->pBA0 + p->u32DBAnAddress);
4227 p->u32DBCnValue = readl(s->pBA0 + p->u32DBCnAddress);
4228 p->u32DMRnValue = readl(s->pBA0 + p->u32DMRnAddress);
4229 p->u32DCRnValue = readl(s->pBA0 + p->u32DCRnAddress);
4233 static void __devinit cs4281_InitPM(struct cs4281_state *s)
4236 struct cs4281_pipeline *p;
4238 for(i=0;i<CS4281_NUMBER_OF_PIPELINES;i++)
4242 cs4281_BuildDMAengine(p,s);
4243 cs4281_BuildFIFO(p,s);
4245 * currently only 2 pipelines are used
4246 * so, only set the valid bit on the playback and capture.
4248 if( (i == CS4281_PLAYBACK_PIPELINE_NUMBER) ||
4249 (i == CS4281_CAPTURE_PIPELINE_NUMBER))
4250 p->flags |= CS4281_PIPELINE_VALID;
4252 s->pm.u32SSPM_BITS = 0x7e; /* rev c, use 0x7c for rev a or b */
4256 static int __devinit cs4281_probe(struct pci_dev *pcidev,
4257 const struct pci_device_id *pciid)
4259 #ifndef NOT_CS4281_PM
4260 struct pm_dev *pmdev;
4262 struct cs4281_state *s;
4263 dma_addr_t dma_mask;
4266 unsigned int temp1, temp2;
4268 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2,
4269 printk(KERN_INFO "cs4281: probe()+\n"));
4271 if (pci_enable_device(pcidev)) {
4272 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
4273 "cs4281: pci_enable_device() failed\n"));
4276 if (!(pci_resource_flags(pcidev, 0) & IORESOURCE_MEM) ||
4277 !(pci_resource_flags(pcidev, 1) & IORESOURCE_MEM)) {
4278 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
4279 "cs4281: probe()- Memory region not assigned\n"));
4282 if (pcidev->irq == 0) {
4283 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
4284 "cs4281: probe() IRQ not assigned\n"));
4287 dma_mask = 0xffffffff; /* this enables playback and recording */
4288 i = pci_set_dma_mask(pcidev, dma_mask);
4290 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
4291 "cs4281: probe() architecture does not support 32bit PCI busmaster DMA\n"));
4294 if (!(s = kmalloc(sizeof(struct cs4281_state), GFP_KERNEL))) {
4295 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
4296 "cs4281: probe() no memory for state struct.\n"));
4299 memset(s, 0, sizeof(struct cs4281_state));
4300 init_waitqueue_head(&s->dma_adc.wait);
4301 init_waitqueue_head(&s->dma_dac.wait);
4302 init_waitqueue_head(&s->open_wait);
4303 init_waitqueue_head(&s->open_wait_adc);
4304 init_waitqueue_head(&s->open_wait_dac);
4305 init_waitqueue_head(&s->midi.iwait);
4306 init_waitqueue_head(&s->midi.owait);
4307 init_MUTEX(&s->open_sem);
4308 init_MUTEX(&s->open_sem_adc);
4309 init_MUTEX(&s->open_sem_dac);
4310 spin_lock_init(&s->lock);
4311 s->pBA0phys = pci_resource_start(pcidev, 0);
4312 s->pBA1phys = pci_resource_start(pcidev, 1);
4314 /* Convert phys to linear. */
4315 s->pBA0 = ioremap_nocache(s->pBA0phys, 4096);
4317 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_ERR
4318 "cs4281: BA0 I/O mapping failed. Skipping part.\n"));
4321 s->pBA1 = ioremap_nocache(s->pBA1phys, 65536);
4323 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_ERR
4324 "cs4281: BA1 I/O mapping failed. Skipping part.\n"));
4328 temp1 = readl(s->pBA0 + BA0_PCICFG00);
4329 temp2 = readl(s->pBA0 + BA0_PCICFG04);
4331 CS_DBGOUT(CS_INIT, 2,
4333 "cs4281: probe() BA0=0x%.8x BA1=0x%.8x pBA0=%p pBA1=%p \n",
4334 (unsigned) temp1, (unsigned) temp2, s->pBA0, s->pBA1));
4335 CS_DBGOUT(CS_INIT, 2,
4337 "cs4281: probe() pBA0phys=0x%.8x pBA1phys=0x%.8x\n",
4338 (unsigned) s->pBA0phys, (unsigned) s->pBA1phys));
4340 #ifndef NOT_CS4281_PM
4341 s->pm.flags = CS4281_PM_IDLE;
4343 temp1 = cs4281_hw_init(s);
4345 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_ERR
4346 "cs4281: cs4281_hw_init() failed. Skipping part.\n"));
4349 s->magic = CS4281_MAGIC;
4351 s->irq = pcidev->irq;
4353 (s->irq, cs4281_interrupt, SA_SHIRQ, "Crystal CS4281", s)) {
4354 CS_DBGOUT(CS_INIT | CS_ERROR, 1,
4355 printk(KERN_ERR "cs4281: irq %u in use\n", s->irq));
4358 if ((s->dev_audio = register_sound_dsp(&cs4281_audio_fops, -1)) <
4360 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
4361 "cs4281: probe() register_sound_dsp() failed.\n"));
4364 if ((s->dev_mixer = register_sound_mixer(&cs4281_mixer_fops, -1)) <
4366 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
4367 "cs4281: probe() register_sound_mixer() failed.\n"));
4370 if ((s->dev_midi = register_sound_midi(&cs4281_midi_fops, -1)) < 0) {
4371 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
4372 "cs4281: probe() register_sound_midi() failed.\n"));
4375 #ifndef NOT_CS4281_PM
4377 pmdev = cs_pm_register(PM_PCI_DEV, PM_PCI_ID(pcidev), cs4281_pm_callback);
4380 CS_DBGOUT(CS_INIT | CS_PM, 4, printk(KERN_INFO
4381 "cs4281: probe() pm_register() succeeded (%p).\n", pmdev));
4386 CS_DBGOUT(CS_INIT | CS_PM | CS_ERROR, 0, printk(KERN_INFO
4387 "cs4281: probe() pm_register() failed (%p).\n", pmdev));
4388 s->pm.flags |= CS4281_PM_NOT_REGISTERED;
4392 pci_set_master(pcidev); // enable bus mastering
4396 val = SOUND_MASK_LINE;
4397 mixer_ioctl(s, SOUND_MIXER_WRITE_RECSRC, (unsigned long) &val);
4398 for (i = 0; i < sizeof(initvol) / sizeof(initvol[0]); i++) {
4399 val = initvol[i].vol;
4400 mixer_ioctl(s, initvol[i].mixch, (unsigned long) &val);
4402 val = 1; // enable mic preamp
4403 mixer_ioctl(s, SOUND_MIXER_PRIVATE1, (unsigned long) &val);
4406 pci_set_drvdata(pcidev, s);
4407 list_add(&s->list, &cs4281_devs);
4408 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
4409 "cs4281: probe()- device allocated successfully\n"));
4413 unregister_sound_mixer(s->dev_mixer);
4415 unregister_sound_dsp(s->dev_audio);
4417 free_irq(s->irq, s);
4425 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_INFO
4426 "cs4281: probe()- no device allocated\n"));
4431 // ---------------------------------------------------------------------
4433 static void __devexit cs4281_remove(struct pci_dev *pci_dev)
4435 struct cs4281_state *s = pci_get_drvdata(pci_dev);
4436 // stop DMA controller
4437 synchronize_irq(s->irq);
4438 free_irq(s->irq, s);
4439 unregister_sound_dsp(s->dev_audio);
4440 unregister_sound_mixer(s->dev_mixer);
4441 unregister_sound_midi(s->dev_midi);
4444 pci_set_drvdata(pci_dev,NULL);
4447 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
4448 "cs4281: cs4281_remove()-: remove successful\n"));
4451 static struct pci_device_id cs4281_pci_tbl[] = {
4453 .vendor = PCI_VENDOR_ID_CIRRUS,
4454 .device = PCI_DEVICE_ID_CRYSTAL_CS4281,
4455 .subvendor = PCI_ANY_ID,
4456 .subdevice = PCI_ANY_ID,
4461 MODULE_DEVICE_TABLE(pci, cs4281_pci_tbl);
4463 static struct pci_driver cs4281_pci_driver = {
4465 .id_table = cs4281_pci_tbl,
4466 .probe = cs4281_probe,
4467 .remove = __devexit_p(cs4281_remove),
4468 .suspend = CS4281_SUSPEND_TBL,
4469 .resume = CS4281_RESUME_TBL,
4472 static int __init cs4281_init_module(void)
4475 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
4476 "cs4281: cs4281_init_module()+ \n"));
4477 printk(KERN_INFO "cs4281: version v%d.%02d.%d time " __TIME__ " "
4478 __DATE__ "\n", CS4281_MAJOR_VERSION, CS4281_MINOR_VERSION,
4480 rtn = pci_module_init(&cs4281_pci_driver);
4482 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
4483 printk(KERN_INFO "cs4281: cs4281_init_module()- (%d)\n",rtn));
4487 static void __exit cs4281_cleanup_module(void)
4489 pci_unregister_driver(&cs4281_pci_driver);
4490 #ifndef NOT_CS4281_PM
4491 cs_pm_unregister_all(cs4281_pm_callback);
4493 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
4494 printk(KERN_INFO "cs4281: cleanup_cs4281() finished\n"));
4496 // ---------------------------------------------------------------------
4498 MODULE_AUTHOR("gw boynton, audio@crystal.cirrus.com");
4499 MODULE_DESCRIPTION("Cirrus Logic CS4281 Driver");
4500 MODULE_LICENSE("GPL");
4502 // ---------------------------------------------------------------------
4504 module_init(cs4281_init_module);
4505 module_exit(cs4281_cleanup_module);