2 * Audio Command Interface (ACI) driver (sound/aci.c)
4 * ACI is a protocol used to communicate with the microcontroller on
5 * some sound cards produced by miro, e.g. the miroSOUND PCM12 and
6 * PCM20. The ACI has been developed for miro by Norberto Pellicci
7 * <pellicci@home.com>. Special thanks to both him and miro for
8 * providing the ACI specification.
10 * The main function of the ACI is to control the mixer and to get a
11 * product identification. On the PCM20, ACI also controls the radio
12 * tuner on this card, this is supported in the Video for Linux
15 * This is a fullfeatured implementation. Unsupported features
18 * It is not longer necessary to load the mad16 module first. The
19 * user is currently responsible to set the mad16 mixer correctly.
21 * To toggle the solo mode for full duplex operation just use the OSS
22 * record switch for the pcm ('wave') controller. Robert
27 * 1995-11-10 Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
28 * First version written.
29 * 1995-12-31 Markus Kuhn
30 * Second revision, general code cleanup.
31 * 1996-05-16 Hannu Savolainen
32 * Integrated with other parts of the driver.
33 * 1996-05-28 Markus Kuhn
34 * Initialize CS4231A mixer, make ACI first mixer,
35 * use new private mixer API for solo mode.
36 * 1998-08-18 Ruurd Reitsma <R.A.Reitsma@wbmt.tudelft.nl>
37 * Small modification to export ACI functions and
38 * complete modularisation.
39 * 2000-06-20 Robert Siemer <Robert.Siemer@gmx.de>
40 * Don't initialize the CS4231A mixer anymore, so the code is
41 * working again, and other small changes to fit in todays
43 * 2000-08-26 Robert Siemer
44 * Clean up and rewrite for 2.4.x. Maybe it's SMP safe now... (:
45 * ioctl bugfix, and integration of solo-mode into OSS-API,
46 * added (OSS-limited) equalizer support, return value bugfix,
47 * changed param aci_reset to reset, new params: ide, wss.
48 * 2001-04-20 Robert Siemer
49 * even more cleanups...
50 * 2001-10-08 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
51 * Get rid of check_region, .bss optimizations, use set_current_state
54 #include <linux/kernel.h>
55 #include <linux/init.h>
56 #include <linux/module.h>
57 #include <linux/proc_fs.h>
58 #include <linux/slab.h>
59 #include <linux/mutex.h>
62 #include <asm/uaccess.h>
63 #include "sound_config.h"
65 int aci_port
; /* as determined by bit 4 in the OPTi 929 MC4 register */
66 static int aci_idcode
[2]; /* manufacturer and product ID */
67 int aci_version
; /* ACI firmware version */
69 EXPORT_SYMBOL(aci_port
);
70 EXPORT_SYMBOL(aci_version
);
75 static int aci_solo
; /* status bit of the card that can't be *
76 * checked with ACI versions prior to 0xb0 */
77 static int aci_amp
; /* status bit for power-amp/line-out level
78 but I have no docs about what is what... */
79 static int aci_micpreamp
=3; /* microphone preamp-level that can't be *
80 * checked with ACI versions prior to 0xb0 */
82 static int mixer_device
;
83 static struct mutex aci_mutex
;
87 module_param(reset
, bool, 0);
88 MODULE_PARM_DESC(reset
,"When set to 1, reset aci mixer.");
94 module_param(ide
, int, 0);
95 MODULE_PARM_DESC(ide
,"1 enable, 0 disable ide-port - untested"
96 " default: do nothing");
98 module_param(wss
, int, 0);
99 MODULE_PARM_DESC(wss
,"change between ACI/WSS-mixer; use 0 and 1 - untested"
100 " default: do nothing; for PCM1-pro only");
103 static void print_bits(unsigned char c
)
106 printk(KERN_DEBUG
"aci: ");
108 for (j
=7; j
>=0; j
--) {
109 printk("%d", (c
>> j
) & 0x1);
117 * This busy wait code normally requires less than 15 loops and
118 * practically always less than 100 loops on my i486/DX2 66 MHz.
120 * Warning: Waiting on the general status flag after reseting the MUTE
121 * function can take a VERY long time, because the PCM12 does some kind
122 * of fade-in effect. For this reason, access to the MUTE function has
123 * not been implemented at all.
125 * - The OSS interface has no mute option. It takes about 3 seconds to
126 * fade-in on my PCM20. busy_wait() handles it great now... Robert
129 static int busy_wait(void)
135 for (timeout
= 1; timeout
<= MINTIME
+30; timeout
++) {
136 if (((byte
=inb(BUSY_REGISTER
)) & 1) == 0) {
137 if (timeout
>= MINTIME
)
138 printk(KERN_DEBUG
"aci: Got READYFLAG in round %ld.\n", timeout
-MINTIME
);
141 if (timeout
>= MINTIME
) {
143 switch (timeout
-MINTIME
) {
151 set_current_state(TASK_UNINTERRUPTIBLE
);
152 schedule_timeout(out
);
157 printk(KERN_WARNING
"aci: busy_wait() time out.\n");
161 /* The four ACI command types are fucked up. [-:
162 * implied is: 1w - special case for INIT
164 * read is: x(1w1r) where x is 1 or 2 (1 CHECK_SIG, 1 CHECK_STER,
165 * 1 VERSION, 2 IDCODE)
166 * the command is only in the first write, rest is protocol overhead
168 * indexed is technically a write and used for STATUS
169 * and the special case for TUNE is: 3w1r
171 * Here the new general sheme: TUNE --> aci_rw_cmd(x, y, z)
172 * indexed and write --> aci_rw_cmd(x, y, -1)
173 * implied and read (x=1) --> aci_rw_cmd(x, -1, -1)
175 * Read (x>=2) is not implemented (only used during initialization).
176 * Use aci_idcode[2] and aci_version... Robert
179 /* Some notes for error detection: theoretically it is possible.
180 * But it doubles the I/O-traffic from ww(r) to wwwrw(r) in the normal
181 * case and doesn't seem to be designed for that... Robert
184 static inline int aci_rawwrite(unsigned char byte
)
186 if (busy_wait() >= 0) {
188 printk(KERN_DEBUG
"aci_rawwrite(%d)\n", byte
);
190 outb(byte
, COMMAND_REGISTER
);
196 static inline int aci_rawread(void)
200 if (busy_wait() >= 0) {
201 byte
=inb(STATUS_REGISTER
);
203 printk(KERN_DEBUG
"%d = aci_rawread()\n", byte
);
211 int aci_rw_cmd(int write1
, int write2
, int write3
)
213 int write
[] = {write1
, write2
, write3
};
214 int read
= -EINTR
, i
;
216 if (mutex_lock_interruptible(&aci_mutex
))
219 for (i
=0; i
<3; i
++) {
220 if (write
[i
]< 0 || write
[i
] > 255)
223 read
= aci_rawwrite(write
[i
]);
230 read
= aci_rawread();
231 out_up
: mutex_unlock(&aci_mutex
);
235 EXPORT_SYMBOL(aci_rw_cmd
);
237 static int setvolume(int __user
*arg
,
238 unsigned char left_index
, unsigned char right_index
)
240 int vol
, ret
, uservol
, buf
;
242 __get_user(uservol
, arg
);
245 vol
= uservol
& 0xff;
248 vol
= SCALE(100, 0x20, vol
);
249 if ((buf
=aci_write_cmd(left_index
, 0x20 - vol
))<0)
251 ret
= SCALE(0x20, 100, vol
);
255 vol
= (uservol
>> 8) & 0xff;
258 vol
= SCALE(100, 0x20, vol
);
259 if ((buf
=aci_write_cmd(right_index
, 0x20 - vol
))<0)
261 ret
|= SCALE(0x20, 100, vol
) << 8;
263 __put_user(ret
, arg
);
268 static int getvolume(int __user
*arg
,
269 unsigned char left_index
, unsigned char right_index
)
275 if ((buf
=aci_indexed_cmd(ACI_STATUS
, left_index
))<0)
277 vol
= SCALE(0x20, 100, buf
< 0x20 ? 0x20-buf
: 0);
280 if ((buf
=aci_indexed_cmd(ACI_STATUS
, right_index
))<0)
282 vol
|= SCALE(0x20, 100, buf
< 0x20 ? 0x20-buf
: 0) << 8;
284 __put_user(vol
, arg
);
290 /* The equalizer is somewhat strange on the ACI. From -12dB to +12dB
291 * write: 0xff..down.to..0x80==0x00..up.to..0x7f
294 static inline unsigned int eq_oss2aci(unsigned int vol
)
306 ret
=SCALE(49, 0x7e, vol
)+1;
308 ret
=0xff - SCALE(50, 0x7f, vol
);
312 static inline unsigned int eq_aci2oss(unsigned int vol
)
315 return SCALE(0x7f, 50, vol
) + 50;
317 return SCALE(0x7f, 50, 0xff-vol
);
321 static int setequalizer(int __user
*arg
,
322 unsigned char left_index
, unsigned char right_index
)
327 __get_user(vol
, arg
);
330 if ((buf
=aci_write_cmd(left_index
, eq_oss2aci(vol
& 0xff)))<0)
334 if ((buf
=aci_write_cmd(right_index
, eq_oss2aci((vol
>>8) & 0xff)))<0)
337 /* the ACI equalizer is more precise */
341 static int getequalizer(int __user
*arg
,
342 unsigned char left_index
, unsigned char right_index
)
348 if ((buf
=aci_indexed_cmd(ACI_STATUS
, left_index
))<0)
350 vol
= eq_aci2oss(buf
);
353 if ((buf
=aci_indexed_cmd(ACI_STATUS
, right_index
))<0)
355 vol
|= eq_aci2oss(buf
) << 8;
357 __put_user(vol
, arg
);
362 static int aci_mixer_ioctl (int dev
, unsigned int cmd
, void __user
* arg
)
368 case SOUND_MIXER_WRITE_VOLUME
:
369 return setvolume(p
, 0x01, 0x00);
370 case SOUND_MIXER_WRITE_CD
:
371 return setvolume(p
, 0x3c, 0x34);
372 case SOUND_MIXER_WRITE_MIC
:
373 return setvolume(p
, 0x38, 0x30);
374 case SOUND_MIXER_WRITE_LINE
:
375 return setvolume(p
, 0x39, 0x31);
376 case SOUND_MIXER_WRITE_SYNTH
:
377 return setvolume(p
, 0x3b, 0x33);
378 case SOUND_MIXER_WRITE_PCM
:
379 return setvolume(p
, 0x3a, 0x32);
380 case MIXER_WRITE(SOUND_MIXER_RADIO
): /* fall through */
381 case SOUND_MIXER_WRITE_LINE1
: /* AUX1 or radio */
382 return setvolume(p
, 0x3d, 0x35);
383 case SOUND_MIXER_WRITE_LINE2
: /* AUX2 */
384 return setvolume(p
, 0x3e, 0x36);
385 case SOUND_MIXER_WRITE_BASS
: /* set band one and two */
386 if (aci_idcode
[1]=='C') {
387 if ((buf
=setequalizer(p
, 0x48, 0x40)) ||
388 (buf
=setequalizer(p
, 0x49, 0x41)));
392 case SOUND_MIXER_WRITE_TREBLE
: /* set band six and seven */
393 if (aci_idcode
[1]=='C') {
394 if ((buf
=setequalizer(p
, 0x4d, 0x45)) ||
395 (buf
=setequalizer(p
, 0x4e, 0x46)));
399 case SOUND_MIXER_WRITE_IGAIN
: /* MIC pre-amp */
400 if (aci_idcode
[1]=='B' || aci_idcode
[1]=='C') {
405 vol
= SCALE(100, 3, vol
);
406 if ((buf
=aci_write_cmd(ACI_WRITE_IGAIN
, vol
))<0)
409 vol
= SCALE(3, 100, vol
);
415 case SOUND_MIXER_WRITE_OGAIN
: /* Power-amp/line-out level */
416 if (aci_idcode
[1]=='A' || aci_idcode
[1]=='B') {
423 if ((buf
=aci_write_cmd(ACI_SET_POWERAMP
, vol
))<0)
427 buf
= (100 || 100<<8);
434 case SOUND_MIXER_WRITE_RECSRC
:
435 /* handle solo mode control */
437 /* unset solo when RECSRC for PCM is requested */
438 if (aci_idcode
[1]=='B' || aci_idcode
[1]=='C') {
439 vol
= !(buf
& SOUND_MASK_PCM
);
440 if ((buf
=aci_write_cmd(ACI_SET_SOLOMODE
, vol
))<0)
444 buf
= (SOUND_MASK_CD
| SOUND_MASK_MIC
| SOUND_MASK_LINE
|
445 SOUND_MASK_SYNTH
| SOUND_MASK_LINE2
);
446 if (aci_idcode
[1] == 'C') /* PCM20 radio */
447 buf
|= SOUND_MASK_RADIO
;
449 buf
|= SOUND_MASK_LINE1
;
451 buf
|= SOUND_MASK_PCM
;
454 case SOUND_MIXER_READ_DEVMASK
:
455 buf
= (SOUND_MASK_VOLUME
| SOUND_MASK_CD
|
456 SOUND_MASK_MIC
| SOUND_MASK_LINE
|
457 SOUND_MASK_SYNTH
| SOUND_MASK_PCM
|
459 switch (aci_idcode
[1]) {
460 case 'C': /* PCM20 radio */
461 buf
|= (SOUND_MASK_RADIO
| SOUND_MASK_IGAIN
|
462 SOUND_MASK_BASS
| SOUND_MASK_TREBLE
);
464 case 'B': /* PCM12 */
465 buf
|= (SOUND_MASK_LINE1
| SOUND_MASK_IGAIN
|
468 case 'A': /* PCM1-pro */
469 buf
|= (SOUND_MASK_LINE1
| SOUND_MASK_OGAIN
);
472 buf
|= SOUND_MASK_LINE1
;
476 case SOUND_MIXER_READ_STEREODEVS
:
477 buf
= (SOUND_MASK_VOLUME
| SOUND_MASK_CD
|
478 SOUND_MASK_MIC
| SOUND_MASK_LINE
|
479 SOUND_MASK_SYNTH
| SOUND_MASK_PCM
|
481 switch (aci_idcode
[1]) {
482 case 'C': /* PCM20 radio */
483 buf
|= (SOUND_MASK_RADIO
|
484 SOUND_MASK_BASS
| SOUND_MASK_TREBLE
);
487 buf
|= SOUND_MASK_LINE1
;
491 case SOUND_MIXER_READ_RECMASK
:
492 buf
= (SOUND_MASK_CD
| SOUND_MASK_MIC
| SOUND_MASK_LINE
|
493 SOUND_MASK_SYNTH
| SOUND_MASK_LINE2
| SOUND_MASK_PCM
);
494 if (aci_idcode
[1] == 'C') /* PCM20 radio */
495 buf
|= SOUND_MASK_RADIO
;
497 buf
|= SOUND_MASK_LINE1
;
501 case SOUND_MIXER_READ_RECSRC
:
502 buf
= (SOUND_MASK_CD
| SOUND_MASK_MIC
| SOUND_MASK_LINE
|
503 SOUND_MASK_SYNTH
| SOUND_MASK_LINE2
);
504 /* do we need aci_solo or can I get it from the ACI? */
505 switch (aci_idcode
[1]) {
506 case 'B': /* PCM12 */
507 case 'C': /* PCM20 radio */
508 if (aci_version
>= 0xb0) {
509 if ((vol
=aci_rw_cmd(ACI_STATUS
,
510 ACI_S_GENERAL
, -1))<0)
513 buf
|= SOUND_MASK_PCM
;
517 buf
|= SOUND_MASK_PCM
;
520 buf
|= SOUND_MASK_PCM
;
522 if (aci_idcode
[1] == 'C') /* PCM20 radio */
523 buf
|= SOUND_MASK_RADIO
;
525 buf
|= SOUND_MASK_LINE1
;
529 case SOUND_MIXER_READ_CAPS
:
532 case SOUND_MIXER_READ_VOLUME
:
533 return getvolume(p
, 0x04, 0x03);
534 case SOUND_MIXER_READ_CD
:
535 return getvolume(p
, 0x0a, 0x09);
536 case SOUND_MIXER_READ_MIC
:
537 return getvolume(p
, 0x06, 0x05);
538 case SOUND_MIXER_READ_LINE
:
539 return getvolume(p
, 0x08, 0x07);
540 case SOUND_MIXER_READ_SYNTH
:
541 return getvolume(p
, 0x0c, 0x0b);
542 case SOUND_MIXER_READ_PCM
:
543 return getvolume(p
, 0x0e, 0x0d);
544 case MIXER_READ(SOUND_MIXER_RADIO
): /* fall through */
545 case SOUND_MIXER_READ_LINE1
: /* AUX1 */
546 return getvolume(p
, 0x11, 0x10);
547 case SOUND_MIXER_READ_LINE2
: /* AUX2 */
548 return getvolume(p
, 0x13, 0x12);
549 case SOUND_MIXER_READ_BASS
: /* get band one */
550 if (aci_idcode
[1]=='C') {
551 return getequalizer(p
, 0x23, 0x22);
554 case SOUND_MIXER_READ_TREBLE
: /* get band seven */
555 if (aci_idcode
[1]=='C') {
556 return getequalizer(p
, 0x2f, 0x2e);
559 case SOUND_MIXER_READ_IGAIN
: /* MIC pre-amp */
560 if (aci_idcode
[1]=='B' || aci_idcode
[1]=='C') {
561 /* aci_micpreamp or ACI? */
562 if (aci_version
>= 0xb0) {
563 if ((buf
=aci_indexed_cmd(ACI_STATUS
,
564 ACI_S_READ_IGAIN
))<0)
569 vol
= SCALE(3, 100, buf
<= 3 ? buf
: 3);
575 case SOUND_MIXER_READ_OGAIN
:
577 buf
= (100 || 100<<8);
586 static struct mixer_operations aci_mixer_operations
=
588 .owner
= THIS_MODULE
,
590 .ioctl
= aci_mixer_ioctl
594 * There is also an internal mixer in the codec (CS4231A or AD1845),
595 * that deserves no purpose in an ACI based system which uses an
596 * external ACI controlled stereo mixer. Make sure that this codec
597 * mixer has the AUX1 input selected as the recording source, that the
598 * input gain is set near maximum and that the other channels going
599 * from the inputs to the codec output are muted.
602 static int __init
attach_aci(void)
607 mutex_init(&aci_mutex
);
609 outb(0xE3, 0xf8f); /* Write MAD16 password */
610 aci_port
= (inb(0xf90) & 0x10) ?
611 0x344: 0x354; /* Get aci_port from MC4_PORT */
613 if (!request_region(aci_port
, 3, "sound mixer (ACI)")) {
615 "aci: I/O area 0x%03x-0x%03x already used.\n",
616 aci_port
, aci_port
+2);
620 /* force ACI into a known state */
623 if (aci_rw_cmd(ACI_ERROR_OP
, -1, -1)<0)
624 goto out_release_region
;
626 /* official this is one aci read call: */
628 if ((aci_idcode
[0]=aci_rw_cmd(ACI_READ_IDCODE
, -1, -1))<0 ||
629 (aci_idcode
[1]=aci_rw_cmd(ACI_READ_IDCODE
, -1, -1))<0) {
630 printk(KERN_ERR
"aci: Failed to read idcode on 0x%03x.\n",
632 goto out_release_region
;
635 if ((aci_version
=aci_rw_cmd(ACI_READ_VERSION
, -1, -1))<0) {
636 printk(KERN_ERR
"aci: Failed to read version on 0x%03x.\n",
638 goto out_release_region
;
641 if (aci_idcode
[0] == 'm') {
642 /* It looks like a miro sound card. */
643 switch (aci_idcode
[1]) {
645 boardname
= "PCM1 pro / early PCM12";
651 boardname
= "PCM20 radio";
654 boardname
= "unknown miro";
657 printk(KERN_WARNING
"aci: Warning: unsupported card! - "
658 "no hardware, no specs...\n");
659 boardname
= "unknown Cardinal Technologies";
662 printk(KERN_INFO
"<ACI 0x%02x, id %02x/%02x \"%c/%c\", (%s)> at 0x%03x\n",
664 aci_idcode
[0], aci_idcode
[1],
665 aci_idcode
[0], aci_idcode
[1],
666 boardname
, aci_port
);
670 /* first write()s after reset fail with my PCM20 */
671 if (aci_rw_cmd(ACI_INIT
, -1, -1)<0 ||
672 aci_rw_cmd(ACI_ERROR_OP
, ACI_ERROR_OP
, ACI_ERROR_OP
)<0 ||
673 aci_rw_cmd(ACI_ERROR_OP
, ACI_ERROR_OP
, ACI_ERROR_OP
)<0)
674 goto out_release_region
;
677 /* the PCM20 is muted after reset (and reboot) */
678 if (aci_rw_cmd(ACI_SET_MUTE
, 0x00, -1)<0)
679 goto out_release_region
;
682 if (aci_rw_cmd(ACI_SET_IDE
, !ide
, -1)<0)
683 goto out_release_region
;
685 if (wss
>=0 && aci_idcode
[1]=='A')
686 if (aci_rw_cmd(ACI_SET_WSS
, !!wss
, -1)<0)
687 goto out_release_region
;
689 mixer_device
= sound_install_mixer(MIXER_DRIVER_VERSION
, boardname
,
690 &aci_mixer_operations
,
691 sizeof(aci_mixer_operations
), NULL
);
693 if (mixer_device
< 0) {
694 printk(KERN_ERR
"aci: Failed to install mixer.\n");
696 goto out_release_region
;
697 } /* else Maybe initialize the CS4231A mixer here... */
700 release_region(aci_port
, 3);
704 static void __exit
unload_aci(void)
706 sound_unload_mixerdev(mixer_device
);
707 release_region(aci_port
, 3);
710 module_init(attach_aci
);
711 module_exit(unload_aci
);
712 MODULE_LICENSE("GPL");