| 1 | /*****************************************************************************/
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * cmpci.c -- C-Media PCI audio driver.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (C) 1999 ChenLi Tien (cltien@cmedia.com.tw)
|
|---|
| 7 | * C-media support (support@cmedia.com.tw)
|
|---|
| 8 | *
|
|---|
| 9 | * Based on the PCI drivers by Thomas Sailer (sailer@ife.ee.ethz.ch)
|
|---|
| 10 | *
|
|---|
| 11 | * For update, visit:
|
|---|
| 12 | * http://members.home.net/puresoft/cmedia.html
|
|---|
| 13 | * http://www.cmedia.com.tw
|
|---|
| 14 | *
|
|---|
| 15 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 16 | * it under the terms of the GNU General Public License as published by
|
|---|
| 17 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 18 | * (at your option) any later version.
|
|---|
| 19 | *
|
|---|
| 20 | * This program is distributed in the hope that it will be useful,
|
|---|
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 23 | * GNU General Public License for more details.
|
|---|
| 24 | *
|
|---|
| 25 | * You should have received a copy of the GNU General Public License
|
|---|
| 26 | * along with this program; if not, write to the Free Software
|
|---|
| 27 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 28 | *
|
|---|
| 29 | * Special thanks to David C. Niemi, Jan Pfeifer
|
|---|
| 30 | *
|
|---|
| 31 | *
|
|---|
| 32 | * Module command line parameters:
|
|---|
| 33 | * none so far
|
|---|
| 34 | *
|
|---|
| 35 | *
|
|---|
| 36 | * Supported devices:
|
|---|
| 37 | * /dev/dsp standard /dev/dsp device, (mostly) OSS compatible
|
|---|
| 38 | * /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
|
|---|
| 39 | * /dev/midi simple MIDI UART interface, no ioctl
|
|---|
| 40 | *
|
|---|
| 41 | * The card has both an FM and a Wavetable synth, but I have to figure
|
|---|
| 42 | * out first how to drive them...
|
|---|
| 43 | *
|
|---|
| 44 | * Revision history
|
|---|
| 45 | * 06.05.98 0.1 Initial release
|
|---|
| 46 | * 10.05.98 0.2 Fixed many bugs, esp. ADC rate calculation
|
|---|
| 47 | * First stab at a simple midi interface (no bells&whistles)
|
|---|
| 48 | * 13.05.98 0.3 Fix stupid cut&paste error: set_adc_rate was called instead of
|
|---|
| 49 | * set_dac_rate in the FMODE_WRITE case in cm_open
|
|---|
| 50 | * Fix hwptr out of bounds (now mpg123 works)
|
|---|
| 51 | * 14.05.98 0.4 Don't allow excessive interrupt rates
|
|---|
| 52 | * 08.06.98 0.5 First release using Alan Cox' soundcore instead of miscdevice
|
|---|
| 53 | * 03.08.98 0.6 Do not include modversions.h
|
|---|
| 54 | * Now mixer behaviour can basically be selected between
|
|---|
| 55 | * "OSS documented" and "OSS actual" behaviour
|
|---|
| 56 | * 31.08.98 0.7 Fix realplayer problems - dac.count issues
|
|---|
| 57 | * 10.12.98 0.8 Fix drain_dac trying to wait on not yet initialized DMA
|
|---|
| 58 | * 16.12.98 0.9 Fix a few f_file & FMODE_ bugs
|
|---|
| 59 | * 06.01.99 0.10 remove the silly SA_INTERRUPT flag.
|
|---|
| 60 | * hopefully killed the egcs section type conflict
|
|---|
| 61 | * 12.03.99 0.11 cinfo.blocks should be reset after GETxPTR ioctl.
|
|---|
| 62 | * reported by Johan Maes <joma@telindus.be>
|
|---|
| 63 | * 22.03.99 0.12 return EAGAIN instead of EBUSY when O_NONBLOCK
|
|---|
| 64 | * read/write cannot be executed
|
|---|
| 65 | * 18.08.99 1.5 Only deallocate DMA buffer when unloading.
|
|---|
| 66 | * 02.09.99 1.6 Enable SPDIF LOOP
|
|---|
| 67 | * Change the mixer read back
|
|---|
| 68 | * 21.09.99 2.33 Use RCS version as driver version.
|
|---|
| 69 | * Add support for modem, S/PDIF loop and 4 channels.
|
|---|
| 70 | * (8738 only)
|
|---|
| 71 | * Fix bug cause x11amp cannot play.
|
|---|
| 72 | *
|
|---|
| 73 | */
|
|---|
| 74 |
|
|---|
| 75 | /*****************************************************************************/
|
|---|
| 76 |
|
|---|
| 77 | #define EXPORT_SYMTAB
|
|---|
| 78 | #include <linux/version.h>
|
|---|
| 79 | #include <linux/config.h>
|
|---|
| 80 | #include <linux/module.h>
|
|---|
| 81 | #include <linux/string.h>
|
|---|
| 82 | #include <linux/ioport.h>
|
|---|
| 83 | #include <linux/sched.h>
|
|---|
| 84 | #include <linux/delay.h>
|
|---|
| 85 | #include <linux/sound.h>
|
|---|
| 86 | #include <linux/malloc.h>
|
|---|
| 87 | #include <linux/soundcard.h>
|
|---|
| 88 | #include <linux/pci.h>
|
|---|
| 89 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 90 | #include <linux/wrapper.h>
|
|---|
| 91 | #endif
|
|---|
| 92 | #include <asm/io.h>
|
|---|
| 93 | #include <asm/dma.h>
|
|---|
| 94 | #include <linux/init.h>
|
|---|
| 95 | #include <linux/poll.h>
|
|---|
| 96 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 97 | #include <linux/spinlock.h>
|
|---|
| 98 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 99 | #include <linux/smp_lock.h>
|
|---|
| 100 | #endif
|
|---|
| 101 | #else
|
|---|
| 102 | #include <asm/spinlock.h>
|
|---|
| 103 | # define AFMT_AC3 0x00000400 /* Dolby Digital AC3 */
|
|---|
| 104 | # define DSP_CAP_BIND 0x00008000 /* channel binding to fr
|
|---|
| 105 | ont/rear/cneter/lfe */
|
|---|
| 106 | #define SNDCTL_DSP_GETCHANNELMASK _SIOWR('P', 64, int)
|
|---|
| 107 | #define SNDCTL_DSP_BIND_CHANNEL _SIOWR('P', 65, int)
|
|---|
| 108 | # define DSP_BIND_QUERY 0x00000000
|
|---|
| 109 | # define DSP_BIND_FRONT 0x00000001
|
|---|
| 110 | # define DSP_BIND_SURR 0x00000002
|
|---|
| 111 | # define DSP_BIND_CENTER_LFE 0x00000004
|
|---|
| 112 | # define DSP_BIND_HANDSET 0x00000008
|
|---|
| 113 | # define DSP_BIND_MIC 0x00000010
|
|---|
| 114 | # define DSP_BIND_MODEM1 0x00000020
|
|---|
| 115 | # define DSP_BIND_MODEM2 0x00000040
|
|---|
| 116 | # define DSP_BIND_I2S 0x00000080
|
|---|
| 117 | # define DSP_BIND_SPDIF 0x00000100
|
|---|
| 118 | #endif
|
|---|
| 119 | #include <asm/uaccess.h>
|
|---|
| 120 | #include <asm/hardirq.h>
|
|---|
| 121 |
|
|---|
| 122 | #include "dm.h"
|
|---|
| 123 |
|
|---|
| 124 | /* --------------------------------------------------------------------- */
|
|---|
| 125 |
|
|---|
| 126 | #undef OSS_DOCUMENTED_MIXER_SEMANTICS
|
|---|
| 127 |
|
|---|
| 128 | /* --------------------------------------------------------------------- */
|
|---|
| 129 |
|
|---|
| 130 | #ifndef PCI_VENDOR_ID_CMEDIA
|
|---|
| 131 | #define PCI_VENDOR_ID_CMEDIA 0x13F6
|
|---|
| 132 | #endif
|
|---|
| 133 | #ifndef PCI_DEVICE_ID_CMEDIA_CM8338A
|
|---|
| 134 | #define PCI_DEVICE_ID_CMEDIA_CM8338A 0x0100
|
|---|
| 135 | #endif
|
|---|
| 136 | #ifndef PCI_DEVICE_ID_CMEDIA_CM8338B
|
|---|
| 137 | #define PCI_DEVICE_ID_CMEDIA_CM8338B 0x0101
|
|---|
| 138 | #endif
|
|---|
| 139 | #ifndef PCI_DEVICE_ID_CMEDIA_CM8738
|
|---|
| 140 | #define PCI_DEVICE_ID_CMEDIA_CM8738 0x0111
|
|---|
| 141 | #endif
|
|---|
| 142 | #ifndef PCI_DEVICE_ID_CMEDIA_CM8738B
|
|---|
| 143 | #define PCI_DEVICE_ID_CMEDIA_CM8738B 0x0112
|
|---|
| 144 | #endif
|
|---|
| 145 |
|
|---|
| 146 | #define CM_MAGIC ((PCI_VENDOR_ID_CMEDIA<<16)|PCI_DEVICE_ID_CMEDIA_CM8338A)
|
|---|
| 147 |
|
|---|
| 148 | /*
|
|---|
| 149 | * CM8338 registers definition
|
|---|
| 150 | */
|
|---|
| 151 |
|
|---|
| 152 | #define CODEC_CMI_FUNCTRL0 (0x00)
|
|---|
| 153 | #define CODEC_CMI_FUNCTRL1 (0x04)
|
|---|
| 154 | #define CODEC_CMI_CHFORMAT (0x08)
|
|---|
| 155 | #define CODEC_CMI_INT_HLDCLR (0x0C)
|
|---|
| 156 | #define CODEC_CMI_INT_STATUS (0x10)
|
|---|
| 157 | #define CODEC_CMI_LEGACY_CTRL (0x14)
|
|---|
| 158 | #define CODEC_CMI_MISC_CTRL (0x18)
|
|---|
| 159 | #define CODEC_CMI_TDMA_POS (0x1C)
|
|---|
| 160 | #define CODEC_CMI_MIXER (0x20)
|
|---|
| 161 | #define CODEC_SB16_DATA (0x22)
|
|---|
| 162 | #define CODEC_SB16_ADDR (0x23)
|
|---|
| 163 | #define CODEC_CMI_MIXER1 (0x24)
|
|---|
| 164 | #define CODEC_CMI_MIXER2 (0x25)
|
|---|
| 165 | #define CODEC_CMI_AUX_VOL (0x26)
|
|---|
| 166 | #define CODEC_CMI_MISC (0x27)
|
|---|
| 167 | #define CODEC_CMI_AC97 (0x28)
|
|---|
| 168 |
|
|---|
| 169 | #define CODEC_CMI_CH0_FRAME1 (0x80)
|
|---|
| 170 | #define CODEC_CMI_CH0_FRAME2 (0x84)
|
|---|
| 171 | #define CODEC_CMI_CH1_FRAME1 (0x88)
|
|---|
| 172 | #define CODEC_CMI_CH1_FRAME2 (0x8C)
|
|---|
| 173 |
|
|---|
| 174 | #define CODEC_CMI_EXT_REG (0xF0)
|
|---|
| 175 | #define UCHAR unsigned char
|
|---|
| 176 | /*
|
|---|
| 177 | ** Mixer registers for SB16
|
|---|
| 178 | */
|
|---|
| 179 |
|
|---|
| 180 | #define DSP_MIX_DATARESETIDX ((UCHAR)(0x00))
|
|---|
| 181 |
|
|---|
| 182 | #define DSP_MIX_MASTERVOLIDX_L ((UCHAR)(0x30))
|
|---|
| 183 | #define DSP_MIX_MASTERVOLIDX_R ((UCHAR)(0x31))
|
|---|
| 184 | #define DSP_MIX_VOICEVOLIDX_L ((UCHAR)(0x32))
|
|---|
| 185 | #define DSP_MIX_VOICEVOLIDX_R ((UCHAR)(0x33))
|
|---|
| 186 | #define DSP_MIX_FMVOLIDX_L ((UCHAR)(0x34))
|
|---|
| 187 | #define DSP_MIX_FMVOLIDX_R ((UCHAR)(0x35))
|
|---|
| 188 | #define DSP_MIX_CDVOLIDX_L ((UCHAR)(0x36))
|
|---|
| 189 | #define DSP_MIX_CDVOLIDX_R ((UCHAR)(0x37))
|
|---|
| 190 | #define DSP_MIX_LINEVOLIDX_L ((UCHAR)(0x38))
|
|---|
| 191 | #define DSP_MIX_LINEVOLIDX_R ((UCHAR)(0x39))
|
|---|
| 192 |
|
|---|
| 193 | #define DSP_MIX_MICVOLIDX ((UCHAR)(0x3A))
|
|---|
| 194 | #define DSP_MIX_SPKRVOLIDX ((UCHAR)(0x3B))
|
|---|
| 195 |
|
|---|
| 196 | #define DSP_MIX_OUTMIXIDX ((UCHAR)(0x3C))
|
|---|
| 197 |
|
|---|
| 198 | #define DSP_MIX_ADCMIXIDX_L ((UCHAR)(0x3D))
|
|---|
| 199 | #define DSP_MIX_ADCMIXIDX_R ((UCHAR)(0x3E))
|
|---|
| 200 |
|
|---|
| 201 | #define DSP_MIX_INGAINIDX_L ((UCHAR)(0x3F))
|
|---|
| 202 | #define DSP_MIX_INGAINIDX_R ((UCHAR)(0x40))
|
|---|
| 203 | #define DSP_MIX_OUTGAINIDX_L ((UCHAR)(0x41))
|
|---|
| 204 | #define DSP_MIX_OUTGAINIDX_R ((UCHAR)(0x42))
|
|---|
| 205 |
|
|---|
| 206 | #define DSP_MIX_AGCIDX ((UCHAR)(0x43))
|
|---|
| 207 |
|
|---|
| 208 | #define DSP_MIX_TREBLEIDX_L ((UCHAR)(0x44))
|
|---|
| 209 | #define DSP_MIX_TREBLEIDX_R ((UCHAR)(0x45))
|
|---|
| 210 | #define DSP_MIX_BASSIDX_L ((UCHAR)(0x46))
|
|---|
| 211 | #define DSP_MIX_BASSIDX_R ((UCHAR)(0x47))
|
|---|
| 212 | #define CM_CH0_RESET 0x04
|
|---|
| 213 | #define CM_CH1_RESET 0x08
|
|---|
| 214 | #define CM_EXTENT_CODEC 0x100
|
|---|
| 215 | #define CM_EXTENT_MIDI 0x2
|
|---|
| 216 | #define CM_EXTENT_SYNTH 0x4
|
|---|
| 217 | #define CM_INT_CH0 1
|
|---|
| 218 | #define CM_INT_CH1 2
|
|---|
| 219 |
|
|---|
| 220 | #define CM_CFMT_STEREO 0x01
|
|---|
| 221 | #define CM_CFMT_16BIT 0x02
|
|---|
| 222 | #define CM_CFMT_MASK 0x03
|
|---|
| 223 | #define CM_CFMT_DACSHIFT 2
|
|---|
| 224 | #define CM_CFMT_ADCSHIFT 0
|
|---|
| 225 |
|
|---|
| 226 | static const unsigned sample_size[] = { 1, 2, 2, 4 };
|
|---|
| 227 | static const unsigned sample_shift[] = { 0, 1, 1, 2 };
|
|---|
| 228 |
|
|---|
| 229 | #define CM_ENABLE_CH1 0x2
|
|---|
| 230 | #define CM_ENABLE_CH0 0x1
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 | /* MIDI buffer sizes */
|
|---|
| 234 |
|
|---|
| 235 | #define MIDIINBUF 256
|
|---|
| 236 | #define MIDIOUTBUF 256
|
|---|
| 237 |
|
|---|
| 238 | #define FMODE_MIDI_SHIFT 2
|
|---|
| 239 | #define FMODE_MIDI_READ (FMODE_READ << FMODE_MIDI_SHIFT)
|
|---|
| 240 | #define FMODE_MIDI_WRITE (FMODE_WRITE << FMODE_MIDI_SHIFT)
|
|---|
| 241 |
|
|---|
| 242 | #define FMODE_DMFM 0x10
|
|---|
| 243 |
|
|---|
| 244 | #define SND_DEV_DSP16 5
|
|---|
| 245 |
|
|---|
| 246 | #define set_dac1_rate set_adc_rate
|
|---|
| 247 | #define stop_dac1 stop_adc
|
|---|
| 248 | #define get_dmadac1 get_dmaadc
|
|---|
| 249 |
|
|---|
| 250 | /* --------------------------------------------------------------------- */
|
|---|
| 251 |
|
|---|
| 252 | struct cm_state {
|
|---|
| 253 | /* magic */
|
|---|
| 254 | unsigned int magic;
|
|---|
| 255 |
|
|---|
| 256 | /* we keep cm cards in a linked list */
|
|---|
| 257 | struct cm_state *next;
|
|---|
| 258 |
|
|---|
| 259 | /* soundcore stuff */
|
|---|
| 260 | int dev_audio;
|
|---|
| 261 | int dev_mixer;
|
|---|
| 262 | int dev_midi;
|
|---|
| 263 | int dev_dmfm;
|
|---|
| 264 |
|
|---|
| 265 | /* hardware resources */
|
|---|
| 266 | unsigned int iosb, iobase, iosynth, iomidi, iogame, irq;
|
|---|
| 267 | unsigned short deviceid;
|
|---|
| 268 |
|
|---|
| 269 | /* mixer stuff */
|
|---|
| 270 | struct {
|
|---|
| 271 | unsigned int modcnt;
|
|---|
| 272 | #ifndef OSS_DOCUMENTED_MIXER_SEMANTICS
|
|---|
| 273 | unsigned short vol[13];
|
|---|
| 274 | #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
|
|---|
| 275 | } mix;
|
|---|
| 276 |
|
|---|
| 277 | /* wave stuff */
|
|---|
| 278 | unsigned int rateadc, ratedac;
|
|---|
| 279 | unsigned char fmt, enable;
|
|---|
| 280 |
|
|---|
| 281 | spinlock_t lock;
|
|---|
| 282 | struct semaphore open_sem;
|
|---|
| 283 | mode_t open_mode;
|
|---|
| 284 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 285 | wait_queue_head_t open_wait;
|
|---|
| 286 | #else
|
|---|
| 287 | struct wait_queue *open_wait;
|
|---|
| 288 | #endif
|
|---|
| 289 |
|
|---|
| 290 | struct dmabuf {
|
|---|
| 291 | void *rawbuf;
|
|---|
| 292 | unsigned rawphys;
|
|---|
| 293 | unsigned buforder;
|
|---|
| 294 | unsigned numfrag;
|
|---|
| 295 | unsigned fragshift;
|
|---|
| 296 | unsigned hwptr, swptr;
|
|---|
| 297 | unsigned total_bytes;
|
|---|
| 298 | int count;
|
|---|
| 299 | unsigned error; /* over/underrun */
|
|---|
| 300 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 301 | wait_queue_head_t wait;
|
|---|
| 302 | #else
|
|---|
| 303 | struct wait_queue *wait;
|
|---|
| 304 | #endif
|
|---|
| 305 | /* redundant, but makes calculations easier */
|
|---|
| 306 | unsigned fragsize;
|
|---|
| 307 | unsigned dmasize;
|
|---|
| 308 | unsigned fragsamples;
|
|---|
| 309 | unsigned dmasamples;
|
|---|
| 310 | /* OSS stuff */
|
|---|
| 311 | unsigned mapped:1;
|
|---|
| 312 | unsigned ready:1;
|
|---|
| 313 | unsigned endcleared:1;
|
|---|
| 314 | unsigned ossfragshift;
|
|---|
| 315 | int ossmaxfrags;
|
|---|
| 316 | unsigned subdivision;
|
|---|
| 317 | } dma_dac, dma_adc;
|
|---|
| 318 |
|
|---|
| 319 | /* midi stuff */
|
|---|
| 320 | struct {
|
|---|
| 321 | unsigned ird, iwr, icnt;
|
|---|
| 322 | unsigned ord, owr, ocnt;
|
|---|
| 323 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 324 | wait_queue_head_t iwait;
|
|---|
| 325 | wait_queue_head_t owait;
|
|---|
| 326 | #else
|
|---|
| 327 | struct wait_queue *iwait;
|
|---|
| 328 | struct wait_queue *owait;
|
|---|
| 329 | #endif
|
|---|
| 330 | struct timer_list timer;
|
|---|
| 331 | unsigned char ibuf[MIDIINBUF];
|
|---|
| 332 | unsigned char obuf[MIDIOUTBUF];
|
|---|
| 333 | } midi;
|
|---|
| 334 |
|
|---|
| 335 | /* misc stuff */
|
|---|
| 336 | int modem;
|
|---|
| 337 | int chip_version;
|
|---|
| 338 | int max_channels;
|
|---|
| 339 | int curr_channels;
|
|---|
| 340 | int speakers; // number of speakers
|
|---|
| 341 | int capability; // HW capability, various for chip versions
|
|---|
| 342 | int status; // HW or SW state
|
|---|
| 343 |
|
|---|
| 344 | /* spdif frame counter */
|
|---|
| 345 | int spdif_counter;
|
|---|
| 346 | };
|
|---|
| 347 |
|
|---|
| 348 | /* flags used for capability */
|
|---|
| 349 | #define CAN_AC3_HW 0x00000001 // 037 or later
|
|---|
| 350 | #define CAN_AC3_SW 0x00000002 // 033 or later
|
|---|
| 351 | #define CAN_AC3 (CAN_AC3_HW | CAN_AC3_SW)
|
|---|
| 352 | #define CAN_DUAL_DAC 0x00000004 // 033 or later
|
|---|
| 353 | #define CAN_MULTI_CH_HW 0x00000008 // 039 or later
|
|---|
| 354 | #define CAN_MULTI_CH (CAN_MULTI_CH_HW | CAN_DUAL_DAC)
|
|---|
| 355 | #define CAN_LINE_AS_REAR 0x00000010 // 033 or later
|
|---|
| 356 | #define CAN_LINE_AS_BASS 0x00000020 // 039 or later
|
|---|
| 357 | #define CAN_MIC_AS_BASS 0x00000040 // 039 or later
|
|---|
| 358 |
|
|---|
| 359 | /* flags used for status */
|
|---|
| 360 | #define DO_AC3_HW 0x00000001
|
|---|
| 361 | #define DO_AC3_SW 0x00000002
|
|---|
| 362 | #define DO_AC3 (DO_AC3_HW | DO_AC3_SW)
|
|---|
| 363 | #define DO_DUAL_DAC 0x00000004
|
|---|
| 364 | #define DO_MULTI_CH_HW 0x00000008
|
|---|
| 365 | #define DO_MULTI_CH (DO_MULTI_CH_HW | DO_DUAL_DAC)
|
|---|
| 366 | #define DO_LINE_AS_REAR 0x00000010 // 033 or later
|
|---|
| 367 | #define DO_LINE_AS_BASS 0x00000020 // 039 or later
|
|---|
| 368 | #define DO_MIC_AS_BASS 0x00000040 // 039 or later
|
|---|
| 369 | #define DO_SPDIF_OUT 0x00000100
|
|---|
| 370 | #define DO_SPDIF_IN 0x00000200
|
|---|
| 371 | #define DO_SPDIF_LOOP 0x00000400
|
|---|
| 372 |
|
|---|
| 373 | /* --------------------------------------------------------------------- */
|
|---|
| 374 |
|
|---|
| 375 | static struct cm_state *devs = NULL;
|
|---|
| 376 | static struct cm_state *devaudio = NULL;
|
|---|
| 377 | static unsigned long wavetable_mem = 0;
|
|---|
| 378 |
|
|---|
| 379 | /* --------------------------------------------------------------------- */
|
|---|
| 380 |
|
|---|
| 381 | extern __inline__ unsigned ld2(unsigned int x)
|
|---|
| 382 | {
|
|---|
| 383 | unsigned r = 0;
|
|---|
| 384 |
|
|---|
| 385 | if (x >= 0x10000) {
|
|---|
| 386 | x >>= 16;
|
|---|
| 387 | r += 16;
|
|---|
| 388 | }
|
|---|
| 389 | if (x >= 0x100) {
|
|---|
| 390 | x >>= 8;
|
|---|
| 391 | r += 8;
|
|---|
| 392 | }
|
|---|
| 393 | if (x >= 0x10) {
|
|---|
| 394 | x >>= 4;
|
|---|
| 395 | r += 4;
|
|---|
| 396 | }
|
|---|
| 397 | if (x >= 4) {
|
|---|
| 398 | x >>= 2;
|
|---|
| 399 | r += 2;
|
|---|
| 400 | }
|
|---|
| 401 | if (x >= 2)
|
|---|
| 402 | r++;
|
|---|
| 403 | return r;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | /*
|
|---|
| 407 | * hweightN: returns the hamming weight (i.e. the number
|
|---|
| 408 | * of bits set) of a N-bit word
|
|---|
| 409 | */
|
|---|
| 410 |
|
|---|
| 411 | #ifdef hweight32
|
|---|
| 412 | #undef hweight32
|
|---|
| 413 | #endif
|
|---|
| 414 |
|
|---|
| 415 | extern __inline__ unsigned int hweight32(unsigned int w)
|
|---|
| 416 | {
|
|---|
| 417 | unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
|
|---|
| 418 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
|
|---|
| 419 | res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
|
|---|
| 420 | res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
|
|---|
| 421 | return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | /* --------------------------------------------------------------------- */
|
|---|
| 425 |
|
|---|
| 426 | /*
|
|---|
| 427 | * Why use byte IO? Nobody knows, but S3 does it also in their Windows driver.
|
|---|
| 428 | */
|
|---|
| 429 |
|
|---|
| 430 | #undef DMABYTEIO
|
|---|
| 431 |
|
|---|
| 432 | static void maskb(unsigned int addr, unsigned int mask, unsigned int value)
|
|---|
| 433 | {
|
|---|
| 434 | outb((inb(addr) & mask) | value, addr);
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | static void maskw(unsigned int addr, unsigned int mask, unsigned int value)
|
|---|
| 438 | {
|
|---|
| 439 | outw((inw(addr) & mask) | value, addr);
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | static void maskl(unsigned int addr, unsigned int mask, unsigned int value)
|
|---|
| 443 | {
|
|---|
| 444 | outl((inl(addr) & mask) | value, addr);
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | static void set_dmadac1(struct cm_state *s, unsigned int addr, unsigned int count)
|
|---|
| 448 | {
|
|---|
| 449 | if (addr)
|
|---|
| 450 | outl(addr, s->iobase + CODEC_CMI_CH0_FRAME1);
|
|---|
| 451 | outw(count - 1, s->iobase + CODEC_CMI_CH0_FRAME2);
|
|---|
| 452 | maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~1, 0);
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | static void set_dmaadc(struct cm_state *s, unsigned int addr, unsigned int count)
|
|---|
| 456 | {
|
|---|
| 457 | outl(addr, s->iobase + CODEC_CMI_CH0_FRAME1);
|
|---|
| 458 | outw(count - 1, s->iobase + CODEC_CMI_CH0_FRAME2);
|
|---|
| 459 | maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~0, 1);
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | static void set_dmadac(struct cm_state *s, unsigned int addr, unsigned int count)
|
|---|
| 463 | {
|
|---|
| 464 | outl(addr, s->iobase + CODEC_CMI_CH1_FRAME1);
|
|---|
| 465 | outw(count - 1, s->iobase + CODEC_CMI_CH1_FRAME2);
|
|---|
| 466 | maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~2, 0);
|
|---|
| 467 | if (s->status & DO_DUAL_DAC)
|
|---|
| 468 | set_dmadac1(s, 0, count);
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | static void set_countadc(struct cm_state *s, unsigned count)
|
|---|
| 472 | {
|
|---|
| 473 | outw(count - 1, s->iobase + CODEC_CMI_CH0_FRAME2 + 2);
|
|---|
| 474 | }
|
|---|
| 475 |
|
|---|
| 476 | static void set_countdac(struct cm_state *s, unsigned count)
|
|---|
| 477 | {
|
|---|
| 478 | outw(count - 1, s->iobase + CODEC_CMI_CH1_FRAME2 + 2);
|
|---|
| 479 | if (s->status & DO_DUAL_DAC)
|
|---|
| 480 | set_countadc(s, count);
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 | extern __inline__ unsigned get_dmadac(struct cm_state *s)
|
|---|
| 484 | {
|
|---|
| 485 | unsigned int curr_addr;
|
|---|
| 486 |
|
|---|
| 487 | #if 1
|
|---|
| 488 | curr_addr = inw(s->iobase + CODEC_CMI_CH1_FRAME2) + 1;
|
|---|
| 489 | curr_addr <<= sample_shift[(s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK];
|
|---|
| 490 | curr_addr = s->dma_dac.dmasize - curr_addr;
|
|---|
| 491 | #else
|
|---|
| 492 | curr_addr = inl(s->iobase + CODEC_CMI_CH1_FRAME1);
|
|---|
| 493 | curr_addr &= ~(sample_size[(s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK]-1);
|
|---|
| 494 | curr_addr -= s->dma_dac.rawphys;
|
|---|
| 495 | #endif
|
|---|
| 496 | return curr_addr;
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | extern __inline__ unsigned get_dmaadc(struct cm_state *s)
|
|---|
| 500 | {
|
|---|
| 501 | unsigned int curr_addr;
|
|---|
| 502 |
|
|---|
| 503 | #if 1
|
|---|
| 504 | curr_addr = inw(s->iobase + CODEC_CMI_CH0_FRAME2) + 1;
|
|---|
| 505 | curr_addr <<= sample_shift[(s->fmt >> CM_CFMT_ADCSHIFT) & CM_CFMT_MASK];
|
|---|
| 506 | curr_addr = s->dma_adc.dmasize - curr_addr;
|
|---|
| 507 | #else
|
|---|
| 508 | curr_addr = inl(s->iobase + CODEC_CMI_CH0_FRAME1);
|
|---|
| 509 | curr_addr &= ~(sample_size[(s->fmt >> CM_CFMT_ADCSHIFT) & CM_CFMT_MASK]-1);
|
|---|
| 510 | curr_addr -= s->dma_adc.rawphys;
|
|---|
| 511 | #endif
|
|---|
| 512 | return curr_addr;
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | static void wrmixer(struct cm_state *s, unsigned char idx, unsigned char data)
|
|---|
| 516 | {
|
|---|
| 517 | outb(idx, s->iobase + CODEC_SB16_ADDR);
|
|---|
| 518 | outb(data, s->iobase + CODEC_SB16_DATA);
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | static unsigned char rdmixer(struct cm_state *s, unsigned char idx)
|
|---|
| 522 | {
|
|---|
| 523 | unsigned char v;
|
|---|
| 524 |
|
|---|
| 525 | outb(idx, s->iobase + CODEC_SB16_ADDR);
|
|---|
| 526 | v = inb(s->iobase + CODEC_SB16_DATA);
|
|---|
| 527 | return v;
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | static void set_fmt(struct cm_state *s, unsigned char mask, unsigned char data)
|
|---|
| 531 | {
|
|---|
| 532 | unsigned long flags;
|
|---|
| 533 |
|
|---|
| 534 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 535 | if (mask)
|
|---|
| 536 | s->fmt = inb(s->iobase + CODEC_CMI_CHFORMAT);
|
|---|
| 537 | s->fmt = (s->fmt & mask) | data;
|
|---|
| 538 | outb(s->fmt, s->iobase + CODEC_CMI_CHFORMAT);
|
|---|
| 539 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | static void frobindir(struct cm_state *s, unsigned char idx, unsigned char mask, unsigned char data)
|
|---|
| 543 | {
|
|---|
| 544 | outb(idx, s->iobase + CODEC_SB16_ADDR);
|
|---|
| 545 | outb((inb(s->iobase + CODEC_SB16_DATA) & mask) | data, s->iobase + CODEC_SB16_DATA);
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | static struct {
|
|---|
| 549 | unsigned rate;
|
|---|
| 550 | unsigned lower;
|
|---|
| 551 | unsigned upper;
|
|---|
| 552 | unsigned char freq;
|
|---|
| 553 | } rate_lookup[] =
|
|---|
| 554 | {
|
|---|
| 555 | { 5512, (0 + 5512) / 2, (5512 + 8000) / 2, 0 },
|
|---|
| 556 | { 8000, (5512 + 8000) / 2, (8000 + 11025) / 2, 4 },
|
|---|
| 557 | { 11025, (8000 + 11025) / 2, (11025 + 16000) / 2, 1 },
|
|---|
| 558 | { 16000, (11025 + 16000) / 2, (16000 + 22050) / 2, 5 },
|
|---|
| 559 | { 22050, (16000 + 22050) / 2, (22050 + 32000) / 2, 2 },
|
|---|
| 560 | { 32000, (22050 + 32000) / 2, (32000 + 44100) / 2, 6 },
|
|---|
| 561 | { 44100, (32000 + 44100) / 2, (44100 + 48000) / 2, 3 },
|
|---|
| 562 | { 48000, (44100 + 48000) / 2, 48000, 7 }
|
|---|
| 563 | };
|
|---|
| 564 |
|
|---|
| 565 | static void set_spdifout(struct cm_state *s, unsigned rate)
|
|---|
| 566 | {
|
|---|
| 567 | unsigned long flags;
|
|---|
| 568 |
|
|---|
| 569 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 570 | if (rate == 48000 || rate == 44100) {
|
|---|
| 571 | // SPDIFI48K SPDF_ACc97
|
|---|
| 572 | maskl(s->iobase + CODEC_CMI_MISC_CTRL, ~0x01008000, rate == 48000 ? 0x01008000 : 0);
|
|---|
| 573 | // ENSPDOUT
|
|---|
| 574 | maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 2, ~0, 0x80);
|
|---|
| 575 | // SPDF_1 SPD2DAC
|
|---|
| 576 | maskw(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x240);
|
|---|
| 577 | // CDPLAY
|
|---|
| 578 | if (s->chip_version >= 39)
|
|---|
| 579 | maskb(s->iobase + CODEC_CMI_MIXER1, ~0, 1);
|
|---|
| 580 | s->status |= DO_SPDIF_OUT;
|
|---|
| 581 | } else {
|
|---|
| 582 | maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 2, ~0x80, 0);
|
|---|
| 583 | maskw(s->iobase + CODEC_CMI_FUNCTRL1, ~0x240, 0);
|
|---|
| 584 | if (s->chip_version >= 39)
|
|---|
| 585 | maskb(s->iobase + CODEC_CMI_MIXER1, ~1, 0);
|
|---|
| 586 | s->status &= ~DO_SPDIF_OUT;
|
|---|
| 587 | }
|
|---|
| 588 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 589 | }
|
|---|
| 590 |
|
|---|
| 591 | /* find parity for bit 4~30 */
|
|---|
| 592 | static unsigned parity(unsigned data)
|
|---|
| 593 | {
|
|---|
| 594 | unsigned parity = 0;
|
|---|
| 595 | int counter = 4;
|
|---|
| 596 |
|
|---|
| 597 | data >>= 4; // start from bit 4
|
|---|
| 598 | while (counter <= 30) {
|
|---|
| 599 | if (data & 1)
|
|---|
| 600 | parity++;
|
|---|
| 601 | data >>= 1;
|
|---|
| 602 | counter++;
|
|---|
| 603 | }
|
|---|
| 604 | return parity & 1;
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 | static void set_ac3(struct cm_state *s, unsigned rate)
|
|---|
| 608 | {
|
|---|
| 609 | unsigned long flags;
|
|---|
| 610 |
|
|---|
| 611 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 612 | set_spdifout(s, rate);
|
|---|
| 613 | /* enable AC3 */
|
|---|
| 614 | if (rate == 48000 || rate == 44100) {
|
|---|
| 615 | // mute DAC
|
|---|
| 616 | maskb(s->iobase + CODEC_CMI_MIXER1, ~0, 0x40);
|
|---|
| 617 | // AC3EN for 037, 0x10
|
|---|
| 618 | maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0, 0x10);
|
|---|
| 619 | // AC3EN for 039, 0x04
|
|---|
| 620 | maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, 0x04);
|
|---|
| 621 | if (s->capability & CAN_AC3_HW) {
|
|---|
| 622 | // SPD24SEL for 037, 0x02
|
|---|
| 623 | // SPD24SEL for 039, 0x20, but cannot be set
|
|---|
| 624 | maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0, 0x02);
|
|---|
| 625 | s->status |= DO_AC3_HW;
|
|---|
| 626 | if (s->chip_version >= 39)
|
|---|
| 627 | maskb(s->iobase + CODEC_CMI_MIXER1, ~1, 0);
|
|---|
| 628 | } else {
|
|---|
| 629 | // SPD32SEL for 037 & 039, 0x20
|
|---|
| 630 | maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, 0x20);
|
|---|
| 631 | // set 176K sample rate to fix 033 HW bug
|
|---|
| 632 | if (s->chip_version == 33) {
|
|---|
| 633 | if (rate == 48000)
|
|---|
| 634 | maskb(s->iobase + CODEC_CMI_CHFORMAT + 1, ~0, 0x08);
|
|---|
| 635 | else
|
|---|
| 636 | maskb(s->iobase + CODEC_CMI_CHFORMAT + 1, ~0x08, 0);
|
|---|
| 637 | }
|
|---|
| 638 | s->status |= DO_AC3_SW;
|
|---|
| 639 | }
|
|---|
| 640 | } else {
|
|---|
| 641 | maskb(s->iobase + CODEC_CMI_MIXER1, ~0x40, 0);
|
|---|
| 642 | maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0x32, 0);
|
|---|
| 643 | maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0x24, 0);
|
|---|
| 644 | maskb(s->iobase + CODEC_CMI_CHFORMAT + 1, ~0x08, 0);
|
|---|
| 645 | if (s->chip_version == 33)
|
|---|
| 646 | maskb(s->iobase + CODEC_CMI_CHFORMAT + 1, ~0x08, 0);
|
|---|
| 647 | if (s->chip_version >= 39)
|
|---|
| 648 | maskb(s->iobase + CODEC_CMI_MIXER1, ~0, 1);
|
|---|
| 649 | s->status &= ~DO_AC3;
|
|---|
| 650 | }
|
|---|
| 651 | s->spdif_counter = 0;
|
|---|
| 652 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | static void trans_ac3(struct cm_state *s, void *dest, const char *source, int size)
|
|---|
| 656 | {
|
|---|
| 657 | int i = size / 2;
|
|---|
| 658 | unsigned long data;
|
|---|
| 659 | unsigned long *dst = (unsigned long *) dest;
|
|---|
| 660 | unsigned short *src = (unsigned short *)source;
|
|---|
| 661 |
|
|---|
| 662 | do {
|
|---|
| 663 | data = (unsigned long) *src++;
|
|---|
| 664 | data <<= 12; // ok for 16-bit data
|
|---|
| 665 | if (s->spdif_counter == 2 || s->spdif_counter == 3)
|
|---|
| 666 | data |= 0x40000000; // indicate AC-3 raw data
|
|---|
| 667 | if (parity(data))
|
|---|
| 668 | data |= 0x80000000; // parity
|
|---|
| 669 | if (s->spdif_counter == 0)
|
|---|
| 670 | data |= 3; // preamble 'M'
|
|---|
| 671 | else if (s->spdif_counter & 1)
|
|---|
| 672 | data |= 5; // odd, 'W'
|
|---|
| 673 | else
|
|---|
| 674 | data |= 9; // even, 'M'
|
|---|
| 675 | *dst++ = data;
|
|---|
| 676 | s->spdif_counter++;
|
|---|
| 677 | if (s->spdif_counter == 384)
|
|---|
| 678 | s->spdif_counter = 0;
|
|---|
| 679 | } while (--i);
|
|---|
| 680 | }
|
|---|
| 681 |
|
|---|
| 682 | static void set_adc_rate(struct cm_state *s, unsigned rate)
|
|---|
| 683 | {
|
|---|
| 684 | unsigned long flags;
|
|---|
| 685 | unsigned char freq = 4;
|
|---|
| 686 | int i;
|
|---|
| 687 |
|
|---|
| 688 | if (rate > 48000)
|
|---|
| 689 | rate = 48000;
|
|---|
| 690 | if (rate < 8000)
|
|---|
| 691 | rate = 8000;
|
|---|
| 692 | for (i = 0; i < sizeof(rate_lookup) / sizeof(rate_lookup[0]); i++) {
|
|---|
| 693 | if (rate > rate_lookup[i].lower && rate <= rate_lookup[i].upper) {
|
|---|
| 694 | rate = rate_lookup[i].rate;
|
|---|
| 695 | freq = rate_lookup[i].freq;
|
|---|
| 696 | break;
|
|---|
| 697 | }
|
|---|
| 698 | }
|
|---|
| 699 | s->rateadc = rate;
|
|---|
| 700 | freq <<= 2;
|
|---|
| 701 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 702 | maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~0x1c, freq);
|
|---|
| 703 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 704 | }
|
|---|
| 705 |
|
|---|
| 706 | static void set_dac_rate(struct cm_state *s, unsigned rate)
|
|---|
| 707 | {
|
|---|
| 708 | unsigned long flags;
|
|---|
| 709 | unsigned char freq = 4;
|
|---|
| 710 | int i;
|
|---|
| 711 |
|
|---|
| 712 | if (rate > 48000)
|
|---|
| 713 | rate = 48000;
|
|---|
| 714 | if (rate < 8000)
|
|---|
| 715 | rate = 8000;
|
|---|
| 716 | for (i = 0; i < sizeof(rate_lookup) / sizeof(rate_lookup[0]); i++) {
|
|---|
| 717 | if (rate > rate_lookup[i].lower && rate <= rate_lookup[i].upper) {
|
|---|
| 718 | rate = rate_lookup[i].rate;
|
|---|
| 719 | freq = rate_lookup[i].freq;
|
|---|
| 720 | break;
|
|---|
| 721 | }
|
|---|
| 722 | }
|
|---|
| 723 | s->ratedac = rate;
|
|---|
| 724 | freq <<= 5;
|
|---|
| 725 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 726 | maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~0xe0, freq);
|
|---|
| 727 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 728 | if (s->curr_channels <= 2)
|
|---|
| 729 | set_spdifout(s, rate);
|
|---|
| 730 | if (s->status & DO_DUAL_DAC)
|
|---|
| 731 | set_dac1_rate(s, rate);
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | /* --------------------------------------------------------------------- */
|
|---|
| 735 | static inline void reset_adc(struct cm_state *s)
|
|---|
| 736 | {
|
|---|
| 737 | /* reset bus master */
|
|---|
| 738 | outb(s->enable | CM_CH0_RESET, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
|
|---|
| 739 | outb(s->enable & ~CM_CH0_RESET, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| 742 | static inline void reset_dac(struct cm_state *s)
|
|---|
| 743 | {
|
|---|
| 744 | /* reset bus master */
|
|---|
| 745 | outb(s->enable | CM_CH1_RESET, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
|
|---|
| 746 | outb(s->enable & ~CM_CH1_RESET, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
|
|---|
| 747 | if (s->status & DO_DUAL_DAC)
|
|---|
| 748 | reset_adc(s);
|
|---|
| 749 | }
|
|---|
| 750 |
|
|---|
| 751 | static inline void pause_adc(struct cm_state *s)
|
|---|
| 752 | {
|
|---|
| 753 | maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~0, 4);
|
|---|
| 754 | }
|
|---|
| 755 |
|
|---|
| 756 | static inline void pause_dac(struct cm_state *s)
|
|---|
| 757 | {
|
|---|
| 758 | maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~0, 8);
|
|---|
| 759 | if (s->status & DO_DUAL_DAC)
|
|---|
| 760 | pause_adc(s);
|
|---|
| 761 | }
|
|---|
| 762 |
|
|---|
| 763 | extern inline void disable_adc(struct cm_state *s)
|
|---|
| 764 | {
|
|---|
| 765 | /* disable channel */
|
|---|
| 766 | s->enable &= ~CM_ENABLE_CH0;
|
|---|
| 767 | outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
|
|---|
| 768 | reset_adc(s);
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | extern inline void disable_dac(struct cm_state *s)
|
|---|
| 772 | {
|
|---|
| 773 | /* disable channel */
|
|---|
| 774 | s->enable &= ~CM_ENABLE_CH1;
|
|---|
| 775 | outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
|
|---|
| 776 | reset_dac(s);
|
|---|
| 777 | if (s->status & DO_DUAL_DAC)
|
|---|
| 778 | disable_adc(s);
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | extern inline void enable_adc(struct cm_state *s)
|
|---|
| 782 | {
|
|---|
| 783 | if (!(s->enable & CM_ENABLE_CH0)) {
|
|---|
| 784 | /* enable channel */
|
|---|
| 785 | s->enable |= CM_ENABLE_CH0;
|
|---|
| 786 | outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
|
|---|
| 787 | }
|
|---|
| 788 | maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~4, 0);
|
|---|
| 789 | }
|
|---|
| 790 |
|
|---|
| 791 | extern inline void enable_dac(struct cm_state *s)
|
|---|
| 792 | {
|
|---|
| 793 | if (!(s->enable & CM_ENABLE_CH1)) {
|
|---|
| 794 | /* enable channel */
|
|---|
| 795 | s->enable |= CM_ENABLE_CH1;
|
|---|
| 796 | outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
|
|---|
| 797 | }
|
|---|
| 798 | maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~8, 0);
|
|---|
| 799 | if (s->status & DO_DUAL_DAC)
|
|---|
| 800 | enable_adc(s);
|
|---|
| 801 | }
|
|---|
| 802 |
|
|---|
| 803 | extern inline void stop_adc(struct cm_state *s)
|
|---|
| 804 | {
|
|---|
| 805 | unsigned long flags;
|
|---|
| 806 |
|
|---|
| 807 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 808 | if (s->enable & CM_ENABLE_CH0) {
|
|---|
| 809 | /* disable interrupt */
|
|---|
| 810 | maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~1, 0);
|
|---|
| 811 | disable_adc(s);
|
|---|
| 812 | }
|
|---|
| 813 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 814 | }
|
|---|
| 815 |
|
|---|
| 816 | extern inline void stop_dac(struct cm_state *s)
|
|---|
| 817 | {
|
|---|
| 818 | unsigned long flags;
|
|---|
| 819 |
|
|---|
| 820 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 821 | if (s->enable & CM_ENABLE_CH1) {
|
|---|
| 822 | /* disable interrupt */
|
|---|
| 823 | maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~2, 0);
|
|---|
| 824 | disable_dac(s);
|
|---|
| 825 | }
|
|---|
| 826 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 827 | if (s->status & DO_DUAL_DAC)
|
|---|
| 828 | stop_dac1(s);
|
|---|
| 829 | }
|
|---|
| 830 |
|
|---|
| 831 | static void start_adc(struct cm_state *s)
|
|---|
| 832 | {
|
|---|
| 833 | unsigned long flags;
|
|---|
| 834 |
|
|---|
| 835 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 836 | if ((s->dma_adc.mapped || s->dma_adc.count < (signed)(s->dma_adc.dmasize - 2*s->dma_adc.fragsize))
|
|---|
| 837 | && s->dma_adc.ready) {
|
|---|
| 838 | /* enable interrupt */
|
|---|
| 839 | maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~0, 1);
|
|---|
| 840 | enable_adc(s);
|
|---|
| 841 | }
|
|---|
| 842 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 843 | }
|
|---|
| 844 |
|
|---|
| 845 | static void start_dac1(struct cm_state *s)
|
|---|
| 846 | {
|
|---|
| 847 | unsigned long flags;
|
|---|
| 848 |
|
|---|
| 849 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 850 | if ((s->dma_adc.mapped || s->dma_adc.count > 0) && s->dma_adc.ready) {
|
|---|
| 851 | /* enable interrupt */
|
|---|
| 852 | // maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~0, 1);
|
|---|
| 853 | enable_dac(s);
|
|---|
| 854 | }
|
|---|
| 855 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 856 | }
|
|---|
| 857 |
|
|---|
| 858 | static void start_dac(struct cm_state *s)
|
|---|
| 859 | {
|
|---|
| 860 | unsigned long flags;
|
|---|
| 861 |
|
|---|
| 862 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 863 | if ((s->dma_dac.mapped || s->dma_dac.count > 0) && s->dma_dac.ready) {
|
|---|
| 864 | /* enable interrupt */
|
|---|
| 865 | maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~0, 2);
|
|---|
| 866 | enable_dac(s);
|
|---|
| 867 | }
|
|---|
| 868 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 869 | if (s->status & DO_DUAL_DAC)
|
|---|
| 870 | start_dac1(s);
|
|---|
| 871 | }
|
|---|
| 872 |
|
|---|
| 873 | static int prog_dmabuf(struct cm_state *s, unsigned rec);
|
|---|
| 874 |
|
|---|
| 875 | static int set_dac_channels(struct cm_state *s, int channels)
|
|---|
| 876 | {
|
|---|
| 877 | unsigned long flags;
|
|---|
| 878 |
|
|---|
| 879 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 880 | if ((channels > 2) && (channels <= s->max_channels)
|
|---|
| 881 | && (((s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK) == (CM_CFMT_STEREO | CM_CFMT_16BIT))) {
|
|---|
| 882 | set_spdifout(s, 0);
|
|---|
| 883 | if (s->capability & CAN_MULTI_CH_HW) {
|
|---|
| 884 | // NXCHG
|
|---|
| 885 | maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~0, 0x80);
|
|---|
| 886 | // CHB3D or CHB3D5C
|
|---|
| 887 | maskb(s->iobase + CODEC_CMI_CHFORMAT + 3, ~0xa0, channels > 4 ? 0x80 : 0x20);
|
|---|
| 888 | // CHB3D6C
|
|---|
| 889 | maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~0x80, channels == 6 ? 0x80 : 0);
|
|---|
| 890 | // ENCENTER
|
|---|
| 891 | maskb(s->iobase + CODEC_CMI_MISC_CTRL, ~0x80, channels == 6 ? 0x80 : 0);
|
|---|
| 892 | s->status |= DO_MULTI_CH_HW;
|
|---|
| 893 | } else if (s->capability & CAN_DUAL_DAC) {
|
|---|
| 894 | unsigned char fmtm = ~0, fmts = 0;
|
|---|
| 895 | ssize_t ret;
|
|---|
| 896 |
|
|---|
| 897 | // ENDBDAC, turn on double DAC mode
|
|---|
| 898 | // XCHGDAC, CH0 -> back, CH1->front
|
|---|
| 899 | maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, 0xC0);
|
|---|
| 900 | s->status |= DO_DUAL_DAC;
|
|---|
| 901 | // prepare secondary buffer
|
|---|
| 902 | ret = prog_dmabuf(s, 1);
|
|---|
| 903 | if (ret) return ret;
|
|---|
| 904 | // copy the hw state
|
|---|
| 905 | fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_DACSHIFT);
|
|---|
| 906 | fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_ADCSHIFT);
|
|---|
| 907 | // the HW only support 16-bit stereo
|
|---|
| 908 | fmts |= CM_CFMT_16BIT << CM_CFMT_DACSHIFT;
|
|---|
| 909 | fmts |= CM_CFMT_16BIT << CM_CFMT_ADCSHIFT;
|
|---|
| 910 | fmts |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
|
|---|
| 911 | fmts |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
|
|---|
| 912 | set_fmt(s, fmtm, fmts);
|
|---|
| 913 | set_dac1_rate(s, s->ratedac);
|
|---|
| 914 | }
|
|---|
| 915 | // N4SPK3D, disable 4 speaker mode (analog duplicate)
|
|---|
| 916 | if (s->speakers > 2)
|
|---|
| 917 | maskb(s->iobase + CODEC_CMI_MISC_CTRL + 3, ~0x04, 0);
|
|---|
| 918 | s->curr_channels = channels;
|
|---|
| 919 | } else {
|
|---|
| 920 | if (s->status & DO_MULTI_CH_HW) {
|
|---|
| 921 | maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~0x80, 0);
|
|---|
| 922 | maskb(s->iobase + CODEC_CMI_CHFORMAT + 3, ~0xa0, 0);
|
|---|
| 923 | maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~0x80, 0);
|
|---|
| 924 | } else if (s->status & DO_DUAL_DAC) {
|
|---|
| 925 | maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0x80, 0);
|
|---|
| 926 | }
|
|---|
| 927 | // N4SPK3D, enable 4 speaker mode (analog duplicate)
|
|---|
| 928 | if (s->speakers > 2)
|
|---|
| 929 | maskb(s->iobase + CODEC_CMI_MISC_CTRL + 3, ~0, 0x04);
|
|---|
| 930 | s->status &= ~DO_MULTI_CH;
|
|---|
| 931 | s->curr_channels = s->fmt & (CM_CFMT_STEREO << CM_CFMT_DACSHIFT) ? 2 : 1;
|
|---|
| 932 | }
|
|---|
| 933 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 934 | return s->curr_channels;
|
|---|
| 935 | }
|
|---|
| 936 |
|
|---|
| 937 | /* --------------------------------------------------------------------- */
|
|---|
| 938 |
|
|---|
| 939 | #define DMABUF_DEFAULTORDER (16-PAGE_SHIFT)
|
|---|
| 940 | #define DMABUF_MINORDER 1
|
|---|
| 941 |
|
|---|
| 942 | static void dealloc_dmabuf(struct dmabuf *db)
|
|---|
| 943 | {
|
|---|
| 944 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 945 | struct page *pstart, *pend;
|
|---|
| 946 | #else
|
|---|
| 947 | unsigned long map, mapend;
|
|---|
| 948 | #endif
|
|---|
| 949 |
|
|---|
| 950 | if (db->rawbuf) {
|
|---|
| 951 | /* undo marking the pages as reserved */
|
|---|
| 952 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 953 | pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
|
|---|
| 954 | for (pstart = virt_to_page(db->rawbuf); pstart <= pend; pstart++)
|
|---|
| 955 | mem_map_unreserve(pstart);
|
|---|
| 956 | #else
|
|---|
| 957 | mapend = MAP_NR(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
|
|---|
| 958 | for (map = MAP_NR(db->rawbuf); map <= mapend; map++)
|
|---|
| 959 | clear_bit(PG_reserved, &mem_map[map].flags);
|
|---|
| 960 | #endif
|
|---|
| 961 | free_pages((unsigned long)db->rawbuf, db->buforder);
|
|---|
| 962 | }
|
|---|
| 963 | db->rawbuf = NULL;
|
|---|
| 964 | db->mapped = db->ready = 0;
|
|---|
| 965 | }
|
|---|
| 966 |
|
|---|
| 967 |
|
|---|
| 968 | /* Ch1 is used for playback, Ch0 is used for recording */
|
|---|
| 969 |
|
|---|
| 970 | static int prog_dmabuf(struct cm_state *s, unsigned rec)
|
|---|
| 971 | {
|
|---|
| 972 | struct dmabuf *db = rec ? &s->dma_adc : &s->dma_dac;
|
|---|
| 973 | unsigned rate = rec ? s->rateadc : s->ratedac;
|
|---|
| 974 | int order;
|
|---|
| 975 | unsigned bytepersec;
|
|---|
| 976 | unsigned bufs;
|
|---|
| 977 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 978 | struct page *pstart, *pend;
|
|---|
| 979 | #else
|
|---|
| 980 | unsigned long map, mapend;
|
|---|
| 981 | #endif
|
|---|
| 982 | unsigned char fmt;
|
|---|
| 983 | unsigned long flags;
|
|---|
| 984 |
|
|---|
| 985 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 986 | fmt = s->fmt;
|
|---|
| 987 | if (rec) {
|
|---|
| 988 | stop_adc(s);
|
|---|
| 989 | fmt >>= CM_CFMT_ADCSHIFT;
|
|---|
| 990 | } else {
|
|---|
| 991 | stop_dac(s);
|
|---|
| 992 | fmt >>= CM_CFMT_DACSHIFT;
|
|---|
| 993 | }
|
|---|
| 994 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 995 | fmt &= CM_CFMT_MASK;
|
|---|
| 996 | db->hwptr = db->swptr = db->total_bytes = db->count = db->error = db->endcleared = 0;
|
|---|
| 997 | if (!db->rawbuf) {
|
|---|
| 998 | db->ready = db->mapped = 0;
|
|---|
| 999 | for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--)
|
|---|
| 1000 | if ((db->rawbuf = (void *)__get_free_pages(GFP_KERNEL | GFP_DMA, order)))
|
|---|
| 1001 | break;
|
|---|
| 1002 | if (!db->rawbuf)
|
|---|
| 1003 | return -ENOMEM;
|
|---|
| 1004 | db->buforder = order;
|
|---|
| 1005 | db->rawphys = virt_to_bus(db->rawbuf);
|
|---|
| 1006 | if ((db->rawphys ^ (db->rawphys + (PAGE_SIZE << db->buforder) - 1)) & ~0xffff)
|
|---|
| 1007 | printk(KERN_DEBUG "cm: DMA buffer crosses 64k boundary: busaddr 0x%lx size %ld\n",
|
|---|
| 1008 | (long) db->rawphys, PAGE_SIZE << db->buforder);
|
|---|
| 1009 | if ((db->rawphys + (PAGE_SIZE << db->buforder) - 1) & ~0xffffff)
|
|---|
| 1010 | printk(KERN_DEBUG "cm: DMA buffer beyond 16MB: busaddr 0x%lx size %ld\n",
|
|---|
| 1011 | (long) db->rawphys, PAGE_SIZE << db->buforder);
|
|---|
| 1012 | /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
|
|---|
| 1013 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 1014 | pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
|
|---|
| 1015 | for (pstart = virt_to_page(db->rawbuf); pstart <= pend; pstart++)
|
|---|
| 1016 | mem_map_reserve(pstart);
|
|---|
| 1017 | #else
|
|---|
| 1018 | mapend = MAP_NR(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
|
|---|
| 1019 | for (map = MAP_NR(db->rawbuf); map <= mapend; map++)
|
|---|
| 1020 | set_bit(PG_reserved, &mem_map[map].flags);
|
|---|
| 1021 | #endif
|
|---|
| 1022 | }
|
|---|
| 1023 | bytepersec = rate << sample_shift[fmt];
|
|---|
| 1024 | bufs = PAGE_SIZE << db->buforder;
|
|---|
| 1025 | if (db->ossfragshift) {
|
|---|
| 1026 | if ((1000 << db->ossfragshift) < bytepersec)
|
|---|
| 1027 | db->fragshift = ld2(bytepersec/1000);
|
|---|
| 1028 | else
|
|---|
| 1029 | db->fragshift = db->ossfragshift;
|
|---|
| 1030 | } else {
|
|---|
| 1031 | db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
|
|---|
| 1032 | if (db->fragshift < 3)
|
|---|
| 1033 | db->fragshift = 3;
|
|---|
| 1034 | }
|
|---|
| 1035 | db->numfrag = bufs >> db->fragshift;
|
|---|
| 1036 | while (db->numfrag < 4 && db->fragshift > 3) {
|
|---|
| 1037 | db->fragshift--;
|
|---|
| 1038 | db->numfrag = bufs >> db->fragshift;
|
|---|
| 1039 | }
|
|---|
| 1040 | db->fragsize = 1 << db->fragshift;
|
|---|
| 1041 | if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
|
|---|
| 1042 | db->numfrag = db->ossmaxfrags;
|
|---|
| 1043 | /* to make fragsize >= 4096 */
|
|---|
| 1044 | if (s->modem) {
|
|---|
| 1045 | while (db->fragsize < 4096 && db->numfrag >= 4) {
|
|---|
| 1046 | db->fragsize *= 2;
|
|---|
| 1047 | db->fragshift++;
|
|---|
| 1048 | db->numfrag /= 2;
|
|---|
| 1049 | }
|
|---|
| 1050 | }
|
|---|
| 1051 | db->fragsamples = db->fragsize >> sample_shift[fmt];
|
|---|
| 1052 | db->dmasize = db->numfrag << db->fragshift;
|
|---|
| 1053 | db->dmasamples = db->dmasize >> sample_shift[fmt];
|
|---|
| 1054 | memset(db->rawbuf, (fmt & CM_CFMT_16BIT) ? 0 : 0x80, db->dmasize);
|
|---|
| 1055 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1056 | if (rec) {
|
|---|
| 1057 | if (s->status & DO_DUAL_DAC)
|
|---|
| 1058 | set_dmadac1(s, db->rawphys, db->dmasize >> sample_shift[fmt]);
|
|---|
| 1059 | else
|
|---|
| 1060 | set_dmaadc(s, db->rawphys, db->dmasize >> sample_shift[fmt]);
|
|---|
| 1061 | /* program sample counts */
|
|---|
| 1062 | set_countdac(s, db->fragsamples);
|
|---|
| 1063 | } else {
|
|---|
| 1064 | set_dmadac(s, db->rawphys, db->dmasize >> sample_shift[fmt]);
|
|---|
| 1065 | /* program sample counts */
|
|---|
| 1066 | set_countdac(s, db->fragsamples);
|
|---|
| 1067 | }
|
|---|
| 1068 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1069 | db->ready = 1;
|
|---|
| 1070 | return 0;
|
|---|
| 1071 | }
|
|---|
| 1072 |
|
|---|
| 1073 | extern __inline__ void clear_advance(struct cm_state *s)
|
|---|
| 1074 | {
|
|---|
| 1075 | unsigned char c = (s->fmt & (CM_CFMT_16BIT << CM_CFMT_DACSHIFT)) ? 0 : 0x80;
|
|---|
| 1076 | unsigned char *buf = s->dma_dac.rawbuf;
|
|---|
| 1077 | unsigned char *buf1 = s->dma_adc.rawbuf;
|
|---|
| 1078 | unsigned bsize = s->dma_dac.dmasize;
|
|---|
| 1079 | unsigned bptr = s->dma_dac.swptr;
|
|---|
| 1080 | unsigned len = s->dma_dac.fragsize;
|
|---|
| 1081 |
|
|---|
| 1082 | if (bptr + len > bsize) {
|
|---|
| 1083 | unsigned x = bsize - bptr;
|
|---|
| 1084 | memset(buf + bptr, c, x);
|
|---|
| 1085 | if (s->status & DO_DUAL_DAC)
|
|---|
| 1086 | memset(buf1 + bptr, c, x);
|
|---|
| 1087 | bptr = 0;
|
|---|
| 1088 | len -= x;
|
|---|
| 1089 | }
|
|---|
| 1090 | memset(buf + bptr, c, len);
|
|---|
| 1091 | if (s->status & DO_DUAL_DAC)
|
|---|
| 1092 | memset(buf1 + bptr, c, len);
|
|---|
| 1093 | }
|
|---|
| 1094 |
|
|---|
| 1095 | /* call with spinlock held! */
|
|---|
| 1096 | static void cm_update_ptr(struct cm_state *s)
|
|---|
| 1097 | {
|
|---|
| 1098 | unsigned hwptr;
|
|---|
| 1099 | int diff;
|
|---|
| 1100 |
|
|---|
| 1101 | /* update ADC pointer */
|
|---|
| 1102 | if (s->dma_adc.ready) {
|
|---|
| 1103 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 1104 | hwptr = get_dmaadc(s) % s->dma_adc.dmasize;
|
|---|
| 1105 | diff = (s->dma_adc.dmasize + hwptr - s->dma_adc.hwptr) % s->dma_adc.dmasize;
|
|---|
| 1106 | s->dma_adc.hwptr = hwptr;
|
|---|
| 1107 | s->dma_adc.total_bytes += diff;
|
|---|
| 1108 | if (s->dma_adc.mapped) {
|
|---|
| 1109 | s->dma_adc.count += diff;
|
|---|
| 1110 | if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
|
|---|
| 1111 | wake_up(&s->dma_adc.wait);
|
|---|
| 1112 | } else {
|
|---|
| 1113 | s->dma_adc.count -= diff;
|
|---|
| 1114 | if (s->dma_adc.count <= 0) {
|
|---|
| 1115 | pause_adc(s);
|
|---|
| 1116 | s->dma_adc.error++;
|
|---|
| 1117 | } else if (s->dma_adc.count <= (signed)s->dma_adc.fragsize && !s->dma_adc.endcleared) {
|
|---|
| 1118 | clear_advance(s);
|
|---|
| 1119 | s->dma_adc.endcleared = 1;
|
|---|
| 1120 | }
|
|---|
| 1121 | if (s->dma_dac.count + (signed)s->dma_dac.fragsize <= (signed)s->dma_dac.dmasize)
|
|---|
| 1122 | wake_up(&s->dma_adc.wait);
|
|---|
| 1123 | }
|
|---|
| 1124 | } else {
|
|---|
| 1125 | hwptr = get_dmaadc(s) % s->dma_adc.dmasize;
|
|---|
| 1126 | diff = (s->dma_adc.dmasize + hwptr - s->dma_adc.hwptr) % s->dma_adc.dmasize;
|
|---|
| 1127 | s->dma_adc.hwptr = hwptr;
|
|---|
| 1128 | s->dma_adc.total_bytes += diff;
|
|---|
| 1129 | s->dma_adc.count += diff;
|
|---|
| 1130 | if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
|
|---|
| 1131 | wake_up(&s->dma_adc.wait);
|
|---|
| 1132 | if (!s->dma_adc.mapped) {
|
|---|
| 1133 | if (s->dma_adc.count > (signed)(s->dma_adc.dmasize - ((3 * s->dma_adc.fragsize) >> 1))) {
|
|---|
| 1134 | pause_adc(s);
|
|---|
| 1135 | s->dma_adc.error++;
|
|---|
| 1136 | }
|
|---|
| 1137 | }
|
|---|
| 1138 | }
|
|---|
| 1139 | }
|
|---|
| 1140 | /* update DAC pointer */
|
|---|
| 1141 | if (s->dma_dac.ready) {
|
|---|
| 1142 | hwptr = get_dmadac(s) % s->dma_dac.dmasize;
|
|---|
| 1143 | diff = (s->dma_dac.dmasize + hwptr - s->dma_dac.hwptr) % s->dma_dac.dmasize;
|
|---|
| 1144 | s->dma_dac.hwptr = hwptr;
|
|---|
| 1145 | s->dma_dac.total_bytes += diff;
|
|---|
| 1146 | if (s->dma_dac.mapped) {
|
|---|
| 1147 | s->dma_dac.count += diff;
|
|---|
| 1148 | if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
|
|---|
| 1149 | wake_up(&s->dma_dac.wait);
|
|---|
| 1150 | } else {
|
|---|
| 1151 | s->dma_dac.count -= diff;
|
|---|
| 1152 | if (s->dma_dac.count <= 0) {
|
|---|
| 1153 | pause_dac(s);
|
|---|
| 1154 | s->dma_dac.error++;
|
|---|
| 1155 | } else if (s->dma_dac.count <= (signed)s->dma_dac.fragsize && !s->dma_dac.endcleared) {
|
|---|
| 1156 | clear_advance(s);
|
|---|
| 1157 | s->dma_dac.endcleared = 1;
|
|---|
| 1158 | }
|
|---|
| 1159 | if (s->dma_dac.count + (signed)s->dma_dac.fragsize <= (signed)s->dma_dac.dmasize)
|
|---|
| 1160 | wake_up(&s->dma_dac.wait);
|
|---|
| 1161 | }
|
|---|
| 1162 | }
|
|---|
| 1163 | }
|
|---|
| 1164 |
|
|---|
| 1165 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 1166 | /* hold spinlock for the following! */
|
|---|
| 1167 | static void cm_handle_midi(struct cm_state *s)
|
|---|
| 1168 | {
|
|---|
| 1169 | unsigned char ch;
|
|---|
| 1170 | int wake;
|
|---|
| 1171 |
|
|---|
| 1172 | wake = 0;
|
|---|
| 1173 | while (!(inb(s->iomidi+1) & 0x80)) {
|
|---|
| 1174 | ch = inb(s->iomidi);
|
|---|
| 1175 | if (s->midi.icnt < MIDIINBUF) {
|
|---|
| 1176 | s->midi.ibuf[s->midi.iwr] = ch;
|
|---|
| 1177 | s->midi.iwr = (s->midi.iwr + 1) % MIDIINBUF;
|
|---|
| 1178 | s->midi.icnt++;
|
|---|
| 1179 | }
|
|---|
| 1180 | wake = 1;
|
|---|
| 1181 | }
|
|---|
| 1182 | if (wake)
|
|---|
| 1183 | wake_up(&s->midi.iwait);
|
|---|
| 1184 | wake = 0;
|
|---|
| 1185 | while (!(inb(s->iomidi+1) & 0x40) && s->midi.ocnt > 0) {
|
|---|
| 1186 | outb(s->midi.obuf[s->midi.ord], s->iomidi);
|
|---|
| 1187 | s->midi.ord = (s->midi.ord + 1) % MIDIOUTBUF;
|
|---|
| 1188 | s->midi.ocnt--;
|
|---|
| 1189 | if (s->midi.ocnt < MIDIOUTBUF-16)
|
|---|
| 1190 | wake = 1;
|
|---|
| 1191 | }
|
|---|
| 1192 | if (wake)
|
|---|
| 1193 | wake_up(&s->midi.owait);
|
|---|
| 1194 | }
|
|---|
| 1195 | #endif
|
|---|
| 1196 |
|
|---|
| 1197 | static void cm_interrupt(int irq, void *dev_id, struct pt_regs *regs)
|
|---|
| 1198 | {
|
|---|
| 1199 | struct cm_state *s = (struct cm_state *)dev_id;
|
|---|
| 1200 | unsigned int intsrc, intstat;
|
|---|
| 1201 | unsigned char mask = 0;
|
|---|
| 1202 |
|
|---|
| 1203 | /* fastpath out, to ease interrupt sharing */
|
|---|
| 1204 | intsrc = inl(s->iobase + CODEC_CMI_INT_STATUS);
|
|---|
| 1205 | if (!(intsrc & 0x80000000))
|
|---|
| 1206 | return;
|
|---|
| 1207 | spin_lock(&s->lock);
|
|---|
| 1208 | intstat = inb(s->iobase + CODEC_CMI_INT_HLDCLR + 2);
|
|---|
| 1209 | /* acknowledge interrupt */
|
|---|
| 1210 | if (intsrc & CM_INT_CH0)
|
|---|
| 1211 | mask |= 1;
|
|---|
| 1212 | if (intsrc & CM_INT_CH1)
|
|---|
| 1213 | mask |= 2;
|
|---|
| 1214 | outb(intstat & ~mask, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
|
|---|
| 1215 | outb(intstat | mask, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
|
|---|
| 1216 | cm_update_ptr(s);
|
|---|
| 1217 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 1218 | cm_handle_midi(s);
|
|---|
| 1219 | #endif
|
|---|
| 1220 | spin_unlock(&s->lock);
|
|---|
| 1221 | }
|
|---|
| 1222 |
|
|---|
| 1223 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 1224 | static void cm_midi_timer(unsigned long data)
|
|---|
| 1225 | {
|
|---|
| 1226 | struct cm_state *s = (struct cm_state *)data;
|
|---|
| 1227 | unsigned long flags;
|
|---|
| 1228 |
|
|---|
| 1229 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1230 | cm_handle_midi(s);
|
|---|
| 1231 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1232 | s->midi.timer.expires = jiffies+1;
|
|---|
| 1233 | add_timer(&s->midi.timer);
|
|---|
| 1234 | }
|
|---|
| 1235 | #endif
|
|---|
| 1236 |
|
|---|
| 1237 | /* --------------------------------------------------------------------- */
|
|---|
| 1238 |
|
|---|
| 1239 | static const char invalid_magic[] = KERN_CRIT "cm: invalid magic value\n";
|
|---|
| 1240 |
|
|---|
| 1241 | #ifdef CONFIG_SOUND_CMPCI /* support multiple chips */
|
|---|
| 1242 | #define VALIDATE_STATE(s)
|
|---|
| 1243 | #else
|
|---|
| 1244 | #define VALIDATE_STATE(s) \
|
|---|
| 1245 | ({ \
|
|---|
| 1246 | if (!(s) || (s)->magic != CM_MAGIC) { \
|
|---|
| 1247 | printk(invalid_magic); \
|
|---|
| 1248 | return -ENXIO; \
|
|---|
| 1249 | } \
|
|---|
| 1250 | })
|
|---|
| 1251 | #endif
|
|---|
| 1252 |
|
|---|
| 1253 | /* --------------------------------------------------------------------- */
|
|---|
| 1254 |
|
|---|
| 1255 | #define MT_4 1
|
|---|
| 1256 | #define MT_5MUTE 2
|
|---|
| 1257 | #define MT_4MUTEMONO 3
|
|---|
| 1258 | #define MT_6MUTE 4
|
|---|
| 1259 | #define MT_5MUTEMONO 5
|
|---|
| 1260 |
|
|---|
| 1261 | static const struct {
|
|---|
| 1262 | unsigned left;
|
|---|
| 1263 | unsigned right;
|
|---|
| 1264 | unsigned type;
|
|---|
| 1265 | unsigned rec;
|
|---|
| 1266 | unsigned play;
|
|---|
| 1267 | } mixtable[SOUND_MIXER_NRDEVICES] = {
|
|---|
| 1268 | [SOUND_MIXER_CD] = { DSP_MIX_CDVOLIDX_L, DSP_MIX_CDVOLIDX_R, MT_5MUTE, 0x04, 0x02 },
|
|---|
| 1269 | [SOUND_MIXER_LINE] = { DSP_MIX_LINEVOLIDX_L, DSP_MIX_LINEVOLIDX_R, MT_5MUTE, 0x10, 0x08 },
|
|---|
| 1270 | [SOUND_MIXER_MIC] = { DSP_MIX_MICVOLIDX, DSP_MIX_MICVOLIDX, MT_5MUTEMONO, 0x01, 0x01 },
|
|---|
| 1271 | [SOUND_MIXER_SYNTH] = { DSP_MIX_FMVOLIDX_L, DSP_MIX_FMVOLIDX_R, MT_5MUTE, 0x40, 0x00 },
|
|---|
| 1272 | [SOUND_MIXER_VOLUME] = { DSP_MIX_MASTERVOLIDX_L, DSP_MIX_MASTERVOLIDX_R, MT_5MUTE, 0x00, 0x00 },
|
|---|
| 1273 | [SOUND_MIXER_PCM] = { DSP_MIX_VOICEVOLIDX_L, DSP_MIX_VOICEVOLIDX_R, MT_5MUTE, 0x00, 0x00 }
|
|---|
| 1274 | };
|
|---|
| 1275 |
|
|---|
| 1276 | #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
|
|---|
| 1277 |
|
|---|
| 1278 | static int return_mixval(struct cm_state *s, unsigned i, int *arg)
|
|---|
| 1279 | {
|
|---|
| 1280 | unsigned long flags;
|
|---|
| 1281 | unsigned char l, r, rl, rr;
|
|---|
| 1282 |
|
|---|
| 1283 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1284 | l = rdmixer(s, mixtable[i].left);
|
|---|
| 1285 | r = rdmixer(s, mixtable[i].right);
|
|---|
| 1286 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1287 | switch (mixtable[i].type) {
|
|---|
| 1288 | case MT_4:
|
|---|
| 1289 | r &= 0xf;
|
|---|
| 1290 | l &= 0xf;
|
|---|
| 1291 | rl = 10 + 6 * (l & 15);
|
|---|
| 1292 | rr = 10 + 6 * (r & 15);
|
|---|
| 1293 | break;
|
|---|
| 1294 |
|
|---|
| 1295 | case MT_4MUTEMONO:
|
|---|
| 1296 | rl = 55 - 3 * (l & 15);
|
|---|
| 1297 | if (r & 0x10)
|
|---|
| 1298 | rl += 45;
|
|---|
| 1299 | rr = rl;
|
|---|
| 1300 | r = l;
|
|---|
| 1301 | break;
|
|---|
| 1302 |
|
|---|
| 1303 | case MT_5MUTEMONO:
|
|---|
| 1304 | r = l;
|
|---|
| 1305 | rl = 100 - 3 * ((l >> 3) & 31);
|
|---|
| 1306 | rr = rl;
|
|---|
| 1307 | break;
|
|---|
| 1308 |
|
|---|
| 1309 | case MT_5MUTE:
|
|---|
| 1310 | default:
|
|---|
| 1311 | rl = 100 - 3 * ((l >> 3) & 31);
|
|---|
| 1312 | rr = 100 - 3 * ((r >> 3) & 31);
|
|---|
| 1313 | break;
|
|---|
| 1314 |
|
|---|
| 1315 | case MT_6MUTE:
|
|---|
| 1316 | rl = 100 - 3 * (l & 63) / 2;
|
|---|
| 1317 | rr = 100 - 3 * (r & 63) / 2;
|
|---|
| 1318 | break;
|
|---|
| 1319 | }
|
|---|
| 1320 | if (l & 0x80)
|
|---|
| 1321 | rl = 0;
|
|---|
| 1322 | if (r & 0x80)
|
|---|
| 1323 | rr = 0;
|
|---|
| 1324 | return put_user((rr << 8) | rl, arg);
|
|---|
| 1325 | }
|
|---|
| 1326 |
|
|---|
| 1327 | #else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
|
|---|
| 1328 |
|
|---|
| 1329 | static const unsigned char volidx[SOUND_MIXER_NRDEVICES] =
|
|---|
| 1330 | {
|
|---|
| 1331 | [SOUND_MIXER_CD] = 1,
|
|---|
| 1332 | [SOUND_MIXER_LINE] = 2,
|
|---|
| 1333 | [SOUND_MIXER_MIC] = 3,
|
|---|
| 1334 | [SOUND_MIXER_SYNTH] = 4,
|
|---|
| 1335 | [SOUND_MIXER_VOLUME] = 5,
|
|---|
| 1336 | [SOUND_MIXER_PCM] = 6
|
|---|
| 1337 | };
|
|---|
| 1338 |
|
|---|
| 1339 | #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
|
|---|
| 1340 |
|
|---|
| 1341 | static unsigned mixer_recmask(struct cm_state *s)
|
|---|
| 1342 | {
|
|---|
| 1343 | unsigned long flags;
|
|---|
| 1344 | int i, j, k;
|
|---|
| 1345 |
|
|---|
| 1346 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1347 | j = rdmixer(s, DSP_MIX_ADCMIXIDX_L);
|
|---|
| 1348 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1349 | j &= 0x7f;
|
|---|
| 1350 | for (k = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
|
|---|
| 1351 | if (j & mixtable[i].rec)
|
|---|
| 1352 | k |= 1 << i;
|
|---|
| 1353 | return k;
|
|---|
| 1354 | }
|
|---|
| 1355 |
|
|---|
| 1356 | static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
|
|---|
| 1357 | {
|
|---|
| 1358 | unsigned long flags;
|
|---|
| 1359 | int i, val, j;
|
|---|
| 1360 | unsigned char l, r, rl, rr;
|
|---|
| 1361 |
|
|---|
| 1362 | VALIDATE_STATE(s);
|
|---|
| 1363 | if (cmd == SOUND_MIXER_INFO) {
|
|---|
| 1364 | mixer_info info;
|
|---|
| 1365 | strncpy(info.id, "cmpci", sizeof(info.id));
|
|---|
| 1366 | strncpy(info.name, "C-Media PCI", sizeof(info.name));
|
|---|
| 1367 | info.modify_counter = s->mix.modcnt;
|
|---|
| 1368 | if (copy_to_user((void *)arg, &info, sizeof(info)))
|
|---|
| 1369 | return -EFAULT;
|
|---|
| 1370 | return 0;
|
|---|
| 1371 | }
|
|---|
| 1372 | if (cmd == SOUND_OLD_MIXER_INFO) {
|
|---|
| 1373 | _old_mixer_info info;
|
|---|
| 1374 | strncpy(info.id, "cmpci", sizeof(info.id));
|
|---|
| 1375 | strncpy(info.name, "C-Media cmpci", sizeof(info.name));
|
|---|
| 1376 | if (copy_to_user((void *)arg, &info, sizeof(info)))
|
|---|
| 1377 | return -EFAULT;
|
|---|
| 1378 | return 0;
|
|---|
| 1379 | }
|
|---|
| 1380 | if (cmd == OSS_GETVERSION)
|
|---|
| 1381 | return put_user(SOUND_VERSION, (int *)arg);
|
|---|
| 1382 | if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int))
|
|---|
| 1383 | return -EINVAL;
|
|---|
| 1384 | if (_IOC_DIR(cmd) == _IOC_READ) {
|
|---|
| 1385 | switch (_IOC_NR(cmd)) {
|
|---|
| 1386 | case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
|
|---|
| 1387 | return put_user(mixer_recmask(s), (int *)arg);
|
|---|
| 1388 |
|
|---|
| 1389 | case SOUND_MIXER_OUTSRC: /* Arg contains a bit for each recording source */
|
|---|
| 1390 | return put_user(mixer_recmask(s), (int *)arg);//need fix
|
|---|
| 1391 |
|
|---|
| 1392 | case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
|
|---|
| 1393 | for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
|
|---|
| 1394 | if (mixtable[i].type)
|
|---|
| 1395 | val |= 1 << i;
|
|---|
| 1396 | return put_user(val, (int *)arg);
|
|---|
| 1397 |
|
|---|
| 1398 | case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
|
|---|
| 1399 | for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
|
|---|
| 1400 | if (mixtable[i].rec)
|
|---|
| 1401 | val |= 1 << i;
|
|---|
| 1402 | return put_user(val, (int *)arg);
|
|---|
| 1403 |
|
|---|
| 1404 | case SOUND_MIXER_OUTMASK: /* Arg contains a bit for each supported recording source */
|
|---|
| 1405 | for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
|
|---|
| 1406 | if (mixtable[i].play)
|
|---|
| 1407 | val |= 1 << i;
|
|---|
| 1408 | return put_user(val, (int *)arg);
|
|---|
| 1409 |
|
|---|
| 1410 | case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
|
|---|
| 1411 | for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
|
|---|
| 1412 | if (mixtable[i].type && mixtable[i].type != MT_4MUTEMONO)
|
|---|
| 1413 | val |= 1 << i;
|
|---|
| 1414 | return put_user(val, (int *)arg);
|
|---|
| 1415 |
|
|---|
| 1416 | case SOUND_MIXER_CAPS:
|
|---|
| 1417 | return put_user(0, (int *)arg);
|
|---|
| 1418 |
|
|---|
| 1419 | default:
|
|---|
| 1420 | i = _IOC_NR(cmd);
|
|---|
| 1421 | if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
|
|---|
| 1422 | return -EINVAL;
|
|---|
| 1423 | #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
|
|---|
| 1424 | return return_mixval(s, i, (int *)arg);
|
|---|
| 1425 | #else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
|
|---|
| 1426 | if (!volidx[i])
|
|---|
| 1427 | return -EINVAL;
|
|---|
| 1428 | return put_user(s->mix.vol[volidx[i]-1], (int *)arg);
|
|---|
| 1429 | #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
|
|---|
| 1430 | }
|
|---|
| 1431 | }
|
|---|
| 1432 | if (_IOC_DIR(cmd) != (_IOC_READ|_IOC_WRITE))
|
|---|
| 1433 | return -EINVAL;
|
|---|
| 1434 | s->mix.modcnt++;
|
|---|
| 1435 | switch (_IOC_NR(cmd)) {
|
|---|
| 1436 | case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
|
|---|
| 1437 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 1438 | if (get_user(val, (int *)arg))
|
|---|
| 1439 | return -EFAULT;
|
|---|
| 1440 | #else
|
|---|
| 1441 | get_user_ret(val, (int *)arg, -EFAULT);
|
|---|
| 1442 | #endif
|
|---|
| 1443 | i = hweight32(val);
|
|---|
| 1444 | for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
|
|---|
| 1445 | if (!(val & (1 << i)))
|
|---|
| 1446 | continue;
|
|---|
| 1447 | if (!mixtable[i].rec) {
|
|---|
| 1448 | val &= ~(1 << i);
|
|---|
| 1449 | continue;
|
|---|
| 1450 | }
|
|---|
| 1451 | j |= mixtable[i].rec;
|
|---|
| 1452 | }
|
|---|
| 1453 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1454 | wrmixer(s, DSP_MIX_ADCMIXIDX_L, j);
|
|---|
| 1455 | wrmixer(s, DSP_MIX_ADCMIXIDX_R, (j & 1) | (j>>1));
|
|---|
| 1456 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1457 | return 0;
|
|---|
| 1458 |
|
|---|
| 1459 | case SOUND_MIXER_OUTSRC: /* Arg contains a bit for each recording source */
|
|---|
| 1460 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 1461 | if (get_user(val, (int *)arg))
|
|---|
| 1462 | return -EFAULT;
|
|---|
| 1463 | #else
|
|---|
| 1464 | get_user_ret(val, (int *)arg, -EFAULT);
|
|---|
| 1465 | #endif
|
|---|
| 1466 | for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
|
|---|
| 1467 | if (!(val & (1 << i)))
|
|---|
| 1468 | continue;
|
|---|
| 1469 | if (!mixtable[i].play) {
|
|---|
| 1470 | val &= ~(1 << i);
|
|---|
| 1471 | continue;
|
|---|
| 1472 | }
|
|---|
| 1473 | j |= mixtable[i].play;
|
|---|
| 1474 | }
|
|---|
| 1475 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1476 | frobindir(s, DSP_MIX_OUTMIXIDX, 0x1f, j);
|
|---|
| 1477 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1478 | return 0;
|
|---|
| 1479 |
|
|---|
| 1480 | default:
|
|---|
| 1481 | i = _IOC_NR(cmd);
|
|---|
| 1482 | if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
|
|---|
| 1483 | return -EINVAL;
|
|---|
| 1484 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 1485 | if (get_user(val, (int *)arg))
|
|---|
| 1486 | return -EFAULT;
|
|---|
| 1487 | #else
|
|---|
| 1488 | get_user_ret(val, (int *)arg, -EFAULT);
|
|---|
| 1489 | #endif
|
|---|
| 1490 | l = val & 0xff;
|
|---|
| 1491 | r = (val >> 8) & 0xff;
|
|---|
| 1492 | if (l > 100)
|
|---|
| 1493 | l = 100;
|
|---|
| 1494 | if (r > 100)
|
|---|
| 1495 | r = 100;
|
|---|
| 1496 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1497 | switch (mixtable[i].type) {
|
|---|
| 1498 | case MT_4:
|
|---|
| 1499 | if (l >= 10)
|
|---|
| 1500 | l -= 10;
|
|---|
| 1501 | if (r >= 10)
|
|---|
| 1502 | r -= 10;
|
|---|
| 1503 | frobindir(s, mixtable[i].left, 0xf0, l / 6);
|
|---|
| 1504 | frobindir(s, mixtable[i].right, 0xf0, l / 6);
|
|---|
| 1505 | break;
|
|---|
| 1506 |
|
|---|
| 1507 | case MT_4MUTEMONO:
|
|---|
| 1508 | rl = (l < 4 ? 0 : (l - 5) / 3) & 31;
|
|---|
| 1509 | rr = (rl >> 2) & 7;
|
|---|
| 1510 | wrmixer(s, mixtable[i].left, rl<<3);
|
|---|
| 1511 | maskb(s->iobase + CODEC_CMI_MIXER2, ~0x0e, rr<<1);
|
|---|
| 1512 | break;
|
|---|
| 1513 |
|
|---|
| 1514 | case MT_5MUTEMONO:
|
|---|
| 1515 | r = l;
|
|---|
| 1516 | rl = l < 4 ? 0 : (l - 5) / 3;
|
|---|
| 1517 | rr = rl >> 2;
|
|---|
| 1518 | wrmixer(s, mixtable[i].left, rl<<3);
|
|---|
| 1519 | maskb(s->iobase + CODEC_CMI_MIXER2, ~0x0e, rr<<1);
|
|---|
| 1520 | break;
|
|---|
| 1521 |
|
|---|
| 1522 | case MT_5MUTE:
|
|---|
| 1523 | rl = l < 4 ? 0 : (l - 5) / 3;
|
|---|
| 1524 | rr = r < 4 ? 0 : (r - 5) / 3;
|
|---|
| 1525 | wrmixer(s, mixtable[i].left, rl<<3);
|
|---|
| 1526 | wrmixer(s, mixtable[i].right, rr<<3);
|
|---|
| 1527 | break;
|
|---|
| 1528 |
|
|---|
| 1529 | case MT_6MUTE:
|
|---|
| 1530 | if (l < 6)
|
|---|
| 1531 | rl = 0x00;
|
|---|
| 1532 | else
|
|---|
| 1533 | rl = l * 2 / 3;
|
|---|
| 1534 | if (r < 6)
|
|---|
| 1535 | rr = 0x00;
|
|---|
| 1536 | else
|
|---|
| 1537 | rr = r * 2 / 3;
|
|---|
| 1538 | wrmixer(s, mixtable[i].left, rl);
|
|---|
| 1539 | wrmixer(s, mixtable[i].right, rr);
|
|---|
| 1540 | break;
|
|---|
| 1541 | }
|
|---|
| 1542 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1543 | #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
|
|---|
| 1544 | return return_mixval(s, i, (int *)arg);
|
|---|
| 1545 | #else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
|
|---|
| 1546 | if (!volidx[i])
|
|---|
| 1547 | return -EINVAL;
|
|---|
| 1548 | s->mix.vol[volidx[i]-1] = val;
|
|---|
| 1549 | return put_user(s->mix.vol[volidx[i]-1], (int *)arg);
|
|---|
| 1550 | #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
|
|---|
| 1551 | }
|
|---|
| 1552 | }
|
|---|
| 1553 |
|
|---|
| 1554 | /* --------------------------------------------------------------------- */
|
|---|
| 1555 |
|
|---|
| 1556 | static loff_t cm_llseek(struct file *file, loff_t offset, int origin)
|
|---|
| 1557 | {
|
|---|
| 1558 | return -ESPIPE;
|
|---|
| 1559 | }
|
|---|
| 1560 |
|
|---|
| 1561 | /* --------------------------------------------------------------------- */
|
|---|
| 1562 |
|
|---|
| 1563 | static int cm_open_mixdev(struct inode *inode, struct file *file)
|
|---|
| 1564 | {
|
|---|
| 1565 | int minor = MINOR(inode->i_rdev);
|
|---|
| 1566 | struct cm_state *s = devs;
|
|---|
| 1567 |
|
|---|
| 1568 | while (s && s->dev_mixer != minor)
|
|---|
| 1569 | s = s->next;
|
|---|
| 1570 | if (!s)
|
|---|
| 1571 | return -ENODEV;
|
|---|
| 1572 | VALIDATE_STATE(s);
|
|---|
| 1573 | file->private_data = s;
|
|---|
| 1574 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
|
|---|
| 1575 | MOD_INC_USE_COUNT;
|
|---|
| 1576 | #endif
|
|---|
| 1577 | return 0;
|
|---|
| 1578 | }
|
|---|
| 1579 |
|
|---|
| 1580 | static int cm_release_mixdev(struct inode *inode, struct file *file)
|
|---|
| 1581 | {
|
|---|
| 1582 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 1583 |
|
|---|
| 1584 | VALIDATE_STATE(s);
|
|---|
| 1585 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
|
|---|
| 1586 | MOD_DEC_USE_COUNT;
|
|---|
| 1587 | #endif
|
|---|
| 1588 | return 0;
|
|---|
| 1589 | }
|
|---|
| 1590 |
|
|---|
| 1591 | static int cm_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
|
|---|
| 1592 | {
|
|---|
| 1593 | return mixer_ioctl((struct cm_state *)file->private_data, cmd, arg);
|
|---|
| 1594 | }
|
|---|
| 1595 |
|
|---|
| 1596 | static /*const*/ struct file_operations cm_mixer_fops = {
|
|---|
| 1597 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 1598 | owner: THIS_MODULE,
|
|---|
| 1599 | #endif
|
|---|
| 1600 | llseek: cm_llseek,
|
|---|
| 1601 | ioctl: cm_ioctl_mixdev,
|
|---|
| 1602 | open: cm_open_mixdev,
|
|---|
| 1603 | release: cm_release_mixdev,
|
|---|
| 1604 | };
|
|---|
| 1605 |
|
|---|
| 1606 | int IntrOpen(void)
|
|---|
| 1607 | {
|
|---|
| 1608 | struct cm_state *s = devs;
|
|---|
| 1609 | unsigned char fmtm = ~0, fmts = 0;
|
|---|
| 1610 |
|
|---|
| 1611 | /* Locate the /dev/dsp file descriptor */
|
|---|
| 1612 | while (s && ((s->dev_audio ^ 3) & ~0xf))
|
|---|
| 1613 | s = s->next;
|
|---|
| 1614 |
|
|---|
| 1615 | devaudio = s;
|
|---|
| 1616 | down(&s->open_sem);
|
|---|
| 1617 | if (s->open_mode & FMODE_WRITE) {
|
|---|
| 1618 | up(&s->open_sem);
|
|---|
| 1619 | devaudio = NULL;
|
|---|
| 1620 | return -EBUSY;
|
|---|
| 1621 | }
|
|---|
| 1622 |
|
|---|
| 1623 | if (!s->dma_dac.ready) {
|
|---|
| 1624 | set_dac_rate(s, 8000);
|
|---|
| 1625 | fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_DACSHIFT);
|
|---|
| 1626 | set_fmt(s, fmtm, fmts);
|
|---|
| 1627 | s->modem = 1;
|
|---|
| 1628 | }
|
|---|
| 1629 |
|
|---|
| 1630 | s->open_mode |= FMODE_WRITE;
|
|---|
| 1631 | up(&s->open_sem);
|
|---|
| 1632 | MOD_INC_USE_COUNT;
|
|---|
| 1633 | return 0;
|
|---|
| 1634 | }
|
|---|
| 1635 | EXPORT_SYMBOL(IntrOpen);
|
|---|
| 1636 |
|
|---|
| 1637 | int IntrClose(void)
|
|---|
| 1638 | {
|
|---|
| 1639 | struct cm_state *s = devaudio;
|
|---|
| 1640 |
|
|---|
| 1641 | if (!s)
|
|---|
| 1642 | return -ENODEV;
|
|---|
| 1643 | down(&s->open_sem);
|
|---|
| 1644 | stop_dac(s);
|
|---|
| 1645 | #ifndef FIXEDDMA
|
|---|
| 1646 | dealloc_dmabuf(&s->dma_dac);
|
|---|
| 1647 | #endif
|
|---|
| 1648 |
|
|---|
| 1649 | s->open_mode &= ~FMODE_WRITE;
|
|---|
| 1650 | s->modem = 0;
|
|---|
| 1651 | up(&s->open_sem);
|
|---|
| 1652 | wake_up(&s->open_wait);
|
|---|
| 1653 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
|
|---|
| 1654 | MOD_DEC_USE_COUNT;
|
|---|
| 1655 | #endif
|
|---|
| 1656 | devaudio = NULL;
|
|---|
| 1657 | return 0;
|
|---|
| 1658 | }
|
|---|
| 1659 | EXPORT_SYMBOL(IntrClose);
|
|---|
| 1660 |
|
|---|
| 1661 | int IntrWrite(const char *buffer, int count)
|
|---|
| 1662 | {
|
|---|
| 1663 | struct cm_state *s = devaudio;
|
|---|
| 1664 | ssize_t ret = 0;
|
|---|
| 1665 | unsigned long flags;
|
|---|
| 1666 | unsigned swptr;
|
|---|
| 1667 | int cnt;
|
|---|
| 1668 |
|
|---|
| 1669 | if (!s)
|
|---|
| 1670 | return -ENODEV;
|
|---|
| 1671 | VALIDATE_STATE(s);
|
|---|
| 1672 | if (s->dma_dac.mapped)
|
|---|
| 1673 | return -ENXIO;
|
|---|
| 1674 |
|
|---|
| 1675 | if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
|
|---|
| 1676 | return ret;
|
|---|
| 1677 |
|
|---|
| 1678 | s->dma_dac.ossfragshift = 8;
|
|---|
| 1679 | s->dma_dac.ossmaxfrags = 16;
|
|---|
| 1680 | s->dma_dac.subdivision = 0;
|
|---|
| 1681 |
|
|---|
| 1682 | while (count > 0) {
|
|---|
| 1683 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1684 | if (s->dma_dac.count < 0) {
|
|---|
| 1685 | s->dma_dac.count = 0;
|
|---|
| 1686 | s->dma_dac.swptr = s->dma_dac.hwptr;
|
|---|
| 1687 | }
|
|---|
| 1688 | swptr = s->dma_dac.swptr;
|
|---|
| 1689 | cnt = s->dma_dac.dmasize-swptr;
|
|---|
| 1690 | if (s->dma_dac.count + cnt > s->dma_dac.dmasize)
|
|---|
| 1691 | cnt = s->dma_dac.dmasize - s->dma_dac.count;
|
|---|
| 1692 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1693 | if (cnt > count)
|
|---|
| 1694 | cnt = count;
|
|---|
| 1695 | if (cnt <= 0) {
|
|---|
| 1696 | start_dac(s);
|
|---|
| 1697 | return ret;
|
|---|
| 1698 | }
|
|---|
| 1699 | if (__copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt))
|
|---|
| 1700 | return ret ? ret : -EFAULT;
|
|---|
| 1701 | swptr = (swptr + cnt) % s->dma_dac.dmasize;
|
|---|
| 1702 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1703 | s->dma_dac.swptr = swptr;
|
|---|
| 1704 | s->dma_dac.count += cnt;
|
|---|
| 1705 | s->dma_dac.endcleared = 0;
|
|---|
| 1706 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1707 | count -= cnt;
|
|---|
| 1708 | buffer += cnt;
|
|---|
| 1709 | ret += cnt;
|
|---|
| 1710 | start_dac(s);
|
|---|
| 1711 | }
|
|---|
| 1712 | return ret;
|
|---|
| 1713 | }
|
|---|
| 1714 | EXPORT_SYMBOL(IntrWrite);
|
|---|
| 1715 |
|
|---|
| 1716 | /* --------------------------------------------------------------------- */
|
|---|
| 1717 |
|
|---|
| 1718 | static int drain_dac(struct cm_state *s, int nonblock)
|
|---|
| 1719 | {
|
|---|
| 1720 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 1721 | DECLARE_WAITQUEUE(wait, current);
|
|---|
| 1722 | #else
|
|---|
| 1723 | struct wait_queue wait = { current, NULL };
|
|---|
| 1724 | #endif
|
|---|
| 1725 | unsigned long flags;
|
|---|
| 1726 | int count, tmo;
|
|---|
| 1727 |
|
|---|
| 1728 | if (s->dma_dac.mapped || !s->dma_dac.ready)
|
|---|
| 1729 | return 0;
|
|---|
| 1730 | current->state = TASK_INTERRUPTIBLE;
|
|---|
| 1731 | add_wait_queue(&s->dma_dac.wait, &wait);
|
|---|
| 1732 | for (;;) {
|
|---|
| 1733 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1734 | count = s->dma_dac.count;
|
|---|
| 1735 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1736 | if (count <= 0)
|
|---|
| 1737 | break;
|
|---|
| 1738 | if (signal_pending(current))
|
|---|
| 1739 | break;
|
|---|
| 1740 | if (nonblock) {
|
|---|
| 1741 | remove_wait_queue(&s->dma_dac.wait, &wait);
|
|---|
| 1742 | current->state = TASK_RUNNING;
|
|---|
| 1743 | return -EBUSY;
|
|---|
| 1744 | }
|
|---|
| 1745 | tmo = 3 * HZ * (count + s->dma_dac.fragsize) / 2 / s->ratedac;
|
|---|
| 1746 | tmo >>= sample_shift[(s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK];
|
|---|
| 1747 | if (!schedule_timeout(tmo + 1))
|
|---|
| 1748 | printk(KERN_DEBUG "cm: dma timed out??\n");
|
|---|
| 1749 | }
|
|---|
| 1750 | remove_wait_queue(&s->dma_dac.wait, &wait);
|
|---|
| 1751 | current->state = TASK_RUNNING;
|
|---|
| 1752 | if (signal_pending(current))
|
|---|
| 1753 | return -ERESTARTSYS;
|
|---|
| 1754 | return 0;
|
|---|
| 1755 | }
|
|---|
| 1756 |
|
|---|
| 1757 | /* --------------------------------------------------------------------- */
|
|---|
| 1758 |
|
|---|
| 1759 | static ssize_t cm_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
|
|---|
| 1760 | {
|
|---|
| 1761 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 1762 | ssize_t ret;
|
|---|
| 1763 | unsigned long flags;
|
|---|
| 1764 | unsigned swptr;
|
|---|
| 1765 | int cnt;
|
|---|
| 1766 |
|
|---|
| 1767 | VALIDATE_STATE(s);
|
|---|
| 1768 | if (ppos != &file->f_pos)
|
|---|
| 1769 | return -ESPIPE;
|
|---|
| 1770 | if (s->dma_adc.mapped)
|
|---|
| 1771 | return -ENXIO;
|
|---|
| 1772 | if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
|
|---|
| 1773 | return ret;
|
|---|
| 1774 | if (!access_ok(VERIFY_WRITE, buffer, count))
|
|---|
| 1775 | return -EFAULT;
|
|---|
| 1776 | ret = 0;
|
|---|
| 1777 | #if 0
|
|---|
| 1778 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1779 | cm_update_ptr(s);
|
|---|
| 1780 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1781 | #endif
|
|---|
| 1782 | while (count > 0) {
|
|---|
| 1783 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1784 | swptr = s->dma_adc.swptr;
|
|---|
| 1785 | cnt = s->dma_adc.dmasize-swptr;
|
|---|
| 1786 | if (s->dma_adc.count < cnt)
|
|---|
| 1787 | cnt = s->dma_adc.count;
|
|---|
| 1788 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1789 | if (cnt > count)
|
|---|
| 1790 | cnt = count;
|
|---|
| 1791 | if (cnt <= 0) {
|
|---|
| 1792 | start_adc(s);
|
|---|
| 1793 | if (file->f_flags & O_NONBLOCK)
|
|---|
| 1794 | return ret ? ret : -EAGAIN;
|
|---|
| 1795 | if (!interruptible_sleep_on_timeout(&s->dma_adc.wait, HZ)) {
|
|---|
| 1796 | printk(KERN_DEBUG "cm: read: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
|
|---|
| 1797 | s->dma_adc.dmasize, s->dma_adc.fragsize, s->dma_adc.count,
|
|---|
| 1798 | s->dma_adc.hwptr, s->dma_adc.swptr);
|
|---|
| 1799 | stop_adc(s);
|
|---|
| 1800 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1801 | set_dmaadc(s, s->dma_adc.rawphys, s->dma_adc.dmasamples);
|
|---|
| 1802 | /* program sample counts */
|
|---|
| 1803 | set_countadc(s, s->dma_adc.fragsamples);
|
|---|
| 1804 | s->dma_adc.count = s->dma_adc.hwptr = s->dma_adc.swptr = 0;
|
|---|
| 1805 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1806 | }
|
|---|
| 1807 | if (signal_pending(current))
|
|---|
| 1808 | return ret ? ret : -ERESTARTSYS;
|
|---|
| 1809 | continue;
|
|---|
| 1810 | }
|
|---|
| 1811 | if (copy_to_user(buffer, s->dma_adc.rawbuf + swptr, cnt))
|
|---|
| 1812 | return ret ? ret : -EFAULT;
|
|---|
| 1813 | swptr = (swptr + cnt) % s->dma_adc.dmasize;
|
|---|
| 1814 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1815 | s->dma_adc.swptr = swptr;
|
|---|
| 1816 | s->dma_adc.count -= cnt;
|
|---|
| 1817 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1818 | count -= cnt;
|
|---|
| 1819 | buffer += cnt;
|
|---|
| 1820 | ret += cnt;
|
|---|
| 1821 | start_adc(s);
|
|---|
| 1822 | }
|
|---|
| 1823 | return ret;
|
|---|
| 1824 | }
|
|---|
| 1825 |
|
|---|
| 1826 | static ssize_t cm_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
|
|---|
| 1827 | {
|
|---|
| 1828 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 1829 | ssize_t ret;
|
|---|
| 1830 | unsigned long flags;
|
|---|
| 1831 | unsigned swptr;
|
|---|
| 1832 | int cnt;
|
|---|
| 1833 |
|
|---|
| 1834 | VALIDATE_STATE(s);
|
|---|
| 1835 | if (ppos != &file->f_pos)
|
|---|
| 1836 | return -ESPIPE;
|
|---|
| 1837 | if (s->dma_dac.mapped)
|
|---|
| 1838 | return -ENXIO;
|
|---|
| 1839 | if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
|
|---|
| 1840 | return ret;
|
|---|
| 1841 | if (!access_ok(VERIFY_READ, buffer, count))
|
|---|
| 1842 | return -EFAULT;
|
|---|
| 1843 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 1844 | if (s->dma_adc.mapped)
|
|---|
| 1845 | return -ENXIO;
|
|---|
| 1846 | if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
|
|---|
| 1847 | return ret;
|
|---|
| 1848 | if (!access_ok(VERIFY_READ, buffer, count))
|
|---|
| 1849 | return -EFAULT;
|
|---|
| 1850 | }
|
|---|
| 1851 | ret = 0;
|
|---|
| 1852 | #if 0
|
|---|
| 1853 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1854 | cm_update_ptr(s);
|
|---|
| 1855 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1856 | #endif
|
|---|
| 1857 | while (count > 0) {
|
|---|
| 1858 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1859 | if (s->dma_dac.count < 0) {
|
|---|
| 1860 | s->dma_dac.count = 0;
|
|---|
| 1861 | s->dma_dac.swptr = s->dma_dac.hwptr;
|
|---|
| 1862 | }
|
|---|
| 1863 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 1864 | s->dma_adc.swptr = s->dma_dac.swptr;
|
|---|
| 1865 | s->dma_adc.count = s->dma_dac.count;
|
|---|
| 1866 | s->dma_adc.endcleared = s->dma_dac.endcleared;
|
|---|
| 1867 | }
|
|---|
| 1868 | swptr = s->dma_dac.swptr;
|
|---|
| 1869 | cnt = s->dma_dac.dmasize-swptr;
|
|---|
| 1870 | if (s->status & DO_AC3_SW) {
|
|---|
| 1871 | if (s->dma_dac.count + 2 * cnt > s->dma_dac.dmasize)
|
|---|
| 1872 | cnt = (s->dma_dac.dmasize - s->dma_dac.count) / 2;
|
|---|
| 1873 | } else {
|
|---|
| 1874 | if (s->dma_dac.count + cnt > s->dma_dac.dmasize)
|
|---|
| 1875 | cnt = s->dma_dac.dmasize - s->dma_dac.count;
|
|---|
| 1876 | }
|
|---|
| 1877 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1878 | if (cnt > count)
|
|---|
| 1879 | cnt = count;
|
|---|
| 1880 | if ((s->status & DO_DUAL_DAC) && (cnt > count / 2))
|
|---|
| 1881 | cnt = count / 2;
|
|---|
| 1882 | if (cnt <= 0) {
|
|---|
| 1883 | start_dac(s);
|
|---|
| 1884 | if (file->f_flags & O_NONBLOCK)
|
|---|
| 1885 | return ret ? ret : -EAGAIN;
|
|---|
| 1886 | if (!interruptible_sleep_on_timeout(&s->dma_dac.wait, HZ)) {
|
|---|
| 1887 | printk(KERN_DEBUG "cm: write: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
|
|---|
| 1888 | s->dma_dac.dmasize, s->dma_dac.fragsize, s->dma_dac.count,
|
|---|
| 1889 | s->dma_dac.hwptr, s->dma_dac.swptr);
|
|---|
| 1890 | stop_dac(s);
|
|---|
| 1891 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1892 | set_dmadac(s, s->dma_dac.rawphys, s->dma_dac.dmasamples);
|
|---|
| 1893 | /* program sample counts */
|
|---|
| 1894 | set_countdac(s, s->dma_dac.fragsamples);
|
|---|
| 1895 | s->dma_dac.count = s->dma_dac.hwptr = s->dma_dac.swptr = 0;
|
|---|
| 1896 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 1897 | set_dmadac1(s, s->dma_adc.rawphys, s->dma_adc.dmasamples);
|
|---|
| 1898 | s->dma_adc.count = s->dma_adc.hwptr = s->dma_adc.swptr = 0;
|
|---|
| 1899 | }
|
|---|
| 1900 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1901 | }
|
|---|
| 1902 | if (signal_pending(current))
|
|---|
| 1903 | return ret ? ret : -ERESTARTSYS;
|
|---|
| 1904 | continue;
|
|---|
| 1905 | }
|
|---|
| 1906 | if (s->status & DO_AC3_SW) {
|
|---|
| 1907 | // clip exceeded data, caught by 033 and 037
|
|---|
| 1908 | if (swptr + 2 * cnt > s->dma_dac.dmasize)
|
|---|
| 1909 | cnt = (s->dma_dac.dmasize - swptr) / 2;
|
|---|
| 1910 | trans_ac3(s, s->dma_dac.rawbuf + swptr, buffer, cnt);
|
|---|
| 1911 | swptr = (swptr + 2 * cnt) % s->dma_dac.dmasize;
|
|---|
| 1912 | } else if (s->status & DO_DUAL_DAC) {
|
|---|
| 1913 | int i;
|
|---|
| 1914 | unsigned long *src, *dst0, *dst1;
|
|---|
| 1915 |
|
|---|
| 1916 | src = (unsigned long *) buffer;
|
|---|
| 1917 | dst0 = (unsigned long *) (s->dma_dac.rawbuf + swptr);
|
|---|
| 1918 | dst1 = (unsigned long *) (s->dma_adc.rawbuf + swptr);
|
|---|
| 1919 | // copy left/right sample at one time
|
|---|
| 1920 | for (i = 0; i <= cnt / 4; i++) {
|
|---|
| 1921 | *dst0++ = *src++;
|
|---|
| 1922 | *dst1++ = *src++;
|
|---|
| 1923 | }
|
|---|
| 1924 | swptr = (swptr + cnt) % s->dma_dac.dmasize;
|
|---|
| 1925 | } else {
|
|---|
| 1926 | if (copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt))
|
|---|
| 1927 | return ret ? ret : -EFAULT;
|
|---|
| 1928 | swptr = (swptr + cnt) % s->dma_dac.dmasize;
|
|---|
| 1929 | }
|
|---|
| 1930 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1931 | s->dma_dac.swptr = swptr;
|
|---|
| 1932 | s->dma_dac.count += cnt;
|
|---|
| 1933 | if (s->status & DO_AC3_SW)
|
|---|
| 1934 | s->dma_dac.count += cnt;
|
|---|
| 1935 | s->dma_dac.endcleared = 0;
|
|---|
| 1936 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1937 | count -= cnt;
|
|---|
| 1938 | buffer += cnt;
|
|---|
| 1939 | ret += cnt;
|
|---|
| 1940 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 1941 | count -= cnt;
|
|---|
| 1942 | buffer += cnt;
|
|---|
| 1943 | ret += cnt;
|
|---|
| 1944 | }
|
|---|
| 1945 | start_dac(s);
|
|---|
| 1946 | }
|
|---|
| 1947 | return ret;
|
|---|
| 1948 | }
|
|---|
| 1949 |
|
|---|
| 1950 | static unsigned int cm_poll(struct file *file, struct poll_table_struct *wait)
|
|---|
| 1951 | {
|
|---|
| 1952 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 1953 | unsigned long flags;
|
|---|
| 1954 | unsigned int mask = 0;
|
|---|
| 1955 |
|
|---|
| 1956 | VALIDATE_STATE(s);
|
|---|
| 1957 | if (file->f_mode & FMODE_WRITE)
|
|---|
| 1958 | poll_wait(file, &s->dma_dac.wait, wait);
|
|---|
| 1959 | if (file->f_mode & FMODE_READ)
|
|---|
| 1960 | poll_wait(file, &s->dma_adc.wait, wait);
|
|---|
| 1961 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 1962 | cm_update_ptr(s);
|
|---|
| 1963 | if (file->f_mode & FMODE_READ) {
|
|---|
| 1964 | if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
|
|---|
| 1965 | mask |= POLLIN | POLLRDNORM;
|
|---|
| 1966 | }
|
|---|
| 1967 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 1968 | if (s->dma_dac.mapped) {
|
|---|
| 1969 | if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
|
|---|
| 1970 | mask |= POLLOUT | POLLWRNORM;
|
|---|
| 1971 | } else {
|
|---|
| 1972 | if ((signed)s->dma_dac.dmasize >= s->dma_dac.count + (signed)s->dma_dac.fragsize)
|
|---|
| 1973 | mask |= POLLOUT | POLLWRNORM;
|
|---|
| 1974 | }
|
|---|
| 1975 | }
|
|---|
| 1976 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 1977 | return mask;
|
|---|
| 1978 | }
|
|---|
| 1979 |
|
|---|
| 1980 | static int cm_mmap(struct file *file, struct vm_area_struct *vma)
|
|---|
| 1981 | {
|
|---|
| 1982 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 1983 | struct dmabuf *db;
|
|---|
| 1984 | int ret = -EINVAL;
|
|---|
| 1985 | unsigned long size;
|
|---|
| 1986 |
|
|---|
| 1987 | VALIDATE_STATE(s);
|
|---|
| 1988 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 1989 | lock_kernel();
|
|---|
| 1990 | #endif
|
|---|
| 1991 | if (vma->vm_flags & VM_WRITE) {
|
|---|
| 1992 | if ((ret = prog_dmabuf(s, 0)) != 0)
|
|---|
| 1993 | goto out;
|
|---|
| 1994 | db = &s->dma_dac;
|
|---|
| 1995 | } else if (vma->vm_flags & VM_READ) {
|
|---|
| 1996 | if ((ret = prog_dmabuf(s, 1)) != 0)
|
|---|
| 1997 | goto out;
|
|---|
| 1998 | db = &s->dma_adc;
|
|---|
| 1999 | } else
|
|---|
| 2000 | goto out;
|
|---|
| 2001 | ret = -EINVAL;
|
|---|
| 2002 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2003 | if (vma->vm_pgoff != 0)
|
|---|
| 2004 | #else
|
|---|
| 2005 | if (vma->vm_offset != 0)
|
|---|
| 2006 | #endif
|
|---|
| 2007 | goto out;
|
|---|
| 2008 | size = vma->vm_end - vma->vm_start;
|
|---|
| 2009 | if (size > (PAGE_SIZE << db->buforder))
|
|---|
| 2010 | goto out;
|
|---|
| 2011 | ret = -EINVAL;
|
|---|
| 2012 | if (remap_page_range(vma->vm_start, virt_to_phys(db->rawbuf), size, vma->vm_page_prot))
|
|---|
| 2013 | goto out;
|
|---|
| 2014 | db->mapped = 1;
|
|---|
| 2015 | ret = 0;
|
|---|
| 2016 | out:
|
|---|
| 2017 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2018 | unlock_kernel();
|
|---|
| 2019 | #endif
|
|---|
| 2020 | return ret;
|
|---|
| 2021 | }
|
|---|
| 2022 |
|
|---|
| 2023 | static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
|
|---|
| 2024 | {
|
|---|
| 2025 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 2026 | unsigned long flags;
|
|---|
| 2027 | audio_buf_info abinfo;
|
|---|
| 2028 | count_info cinfo;
|
|---|
| 2029 | int val, mapped, ret;
|
|---|
| 2030 | unsigned char fmtm, fmtd;
|
|---|
| 2031 |
|
|---|
| 2032 | VALIDATE_STATE(s);
|
|---|
| 2033 | mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
|
|---|
| 2034 | ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
|
|---|
| 2035 | switch (cmd) {
|
|---|
| 2036 | case OSS_GETVERSION:
|
|---|
| 2037 | return put_user(SOUND_VERSION, (int *)arg);
|
|---|
| 2038 |
|
|---|
| 2039 | case SNDCTL_DSP_SYNC:
|
|---|
| 2040 | if (file->f_mode & FMODE_WRITE)
|
|---|
| 2041 | return drain_dac(s, 0/*file->f_flags & O_NONBLOCK*/);
|
|---|
| 2042 | return 0;
|
|---|
| 2043 |
|
|---|
| 2044 | case SNDCTL_DSP_SETDUPLEX:
|
|---|
| 2045 | return 0;
|
|---|
| 2046 |
|
|---|
| 2047 | case SNDCTL_DSP_GETCAPS:
|
|---|
| 2048 | return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_BIND, (int *)arg);
|
|---|
| 2049 |
|
|---|
| 2050 | case SNDCTL_DSP_RESET:
|
|---|
| 2051 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2052 | stop_dac(s);
|
|---|
| 2053 | synchronize_irq();
|
|---|
| 2054 | s->dma_dac.swptr = s->dma_dac.hwptr = s->dma_dac.count = s->dma_dac.total_bytes = 0;
|
|---|
| 2055 | if (s->status & DO_DUAL_DAC)
|
|---|
| 2056 | s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
|
|---|
| 2057 | }
|
|---|
| 2058 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2059 | stop_adc(s);
|
|---|
| 2060 | synchronize_irq();
|
|---|
| 2061 | s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
|
|---|
| 2062 | }
|
|---|
| 2063 | return 0;
|
|---|
| 2064 |
|
|---|
| 2065 | case SNDCTL_DSP_SPEED:
|
|---|
| 2066 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2067 | if (get_user(val, (int *)arg))
|
|---|
| 2068 | return -EFAULT;
|
|---|
| 2069 | #else
|
|---|
| 2070 | get_user_ret(val, (int *)arg, -EFAULT);
|
|---|
| 2071 | #endif
|
|---|
| 2072 | if (val >= 0) {
|
|---|
| 2073 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2074 | stop_adc(s);
|
|---|
| 2075 | s->dma_adc.ready = 0;
|
|---|
| 2076 | set_adc_rate(s, val);
|
|---|
| 2077 | }
|
|---|
| 2078 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2079 | stop_dac(s);
|
|---|
| 2080 | s->dma_dac.ready = 0;
|
|---|
| 2081 | if (s->status & DO_DUAL_DAC)
|
|---|
| 2082 | s->dma_adc.ready = 0;
|
|---|
| 2083 | set_dac_rate(s, val);
|
|---|
| 2084 | }
|
|---|
| 2085 | }
|
|---|
| 2086 | return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int *)arg);
|
|---|
| 2087 |
|
|---|
| 2088 | case SNDCTL_DSP_STEREO:
|
|---|
| 2089 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2090 | if (get_user(val, (int *)arg))
|
|---|
| 2091 | return -EFAULT;
|
|---|
| 2092 | #else
|
|---|
| 2093 | get_user_ret(val, (int *)arg, -EFAULT);
|
|---|
| 2094 | #endif
|
|---|
| 2095 | fmtd = 0;
|
|---|
| 2096 | fmtm = ~0;
|
|---|
| 2097 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2098 | stop_adc(s);
|
|---|
| 2099 | s->dma_adc.ready = 0;
|
|---|
| 2100 | if (val)
|
|---|
| 2101 | fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
|
|---|
| 2102 | else
|
|---|
| 2103 | fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
|
|---|
| 2104 | }
|
|---|
| 2105 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2106 | stop_dac(s);
|
|---|
| 2107 | s->dma_dac.ready = 0;
|
|---|
| 2108 | if (val)
|
|---|
| 2109 | fmtd |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
|
|---|
| 2110 | else
|
|---|
| 2111 | fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_DACSHIFT);
|
|---|
| 2112 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 2113 | s->dma_adc.ready = 0;
|
|---|
| 2114 | if (val)
|
|---|
| 2115 | fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
|
|---|
| 2116 | else
|
|---|
| 2117 | fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
|
|---|
| 2118 | }
|
|---|
| 2119 | }
|
|---|
| 2120 | set_fmt(s, fmtm, fmtd);
|
|---|
| 2121 | return 0;
|
|---|
| 2122 |
|
|---|
| 2123 | case SNDCTL_DSP_CHANNELS:
|
|---|
| 2124 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2125 | if (get_user(val, (int *)arg))
|
|---|
| 2126 | return -EFAULT;
|
|---|
| 2127 | #else
|
|---|
| 2128 | get_user_ret(val, (int *)arg, -EFAULT);
|
|---|
| 2129 | #endif
|
|---|
| 2130 | if (val != 0) {
|
|---|
| 2131 | fmtd = 0;
|
|---|
| 2132 | fmtm = ~0;
|
|---|
| 2133 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2134 | stop_adc(s);
|
|---|
| 2135 | s->dma_adc.ready = 0;
|
|---|
| 2136 | if (val >= 2)
|
|---|
| 2137 | fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
|
|---|
| 2138 | else
|
|---|
| 2139 | fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
|
|---|
| 2140 | }
|
|---|
| 2141 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2142 | stop_dac(s);
|
|---|
| 2143 | s->dma_dac.ready = 0;
|
|---|
| 2144 | if (val >= 2)
|
|---|
| 2145 | fmtd |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
|
|---|
| 2146 | else
|
|---|
| 2147 | fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_DACSHIFT);
|
|---|
| 2148 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 2149 | s->dma_adc.ready = 0;
|
|---|
| 2150 | if (val >= 2)
|
|---|
| 2151 | fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
|
|---|
| 2152 | else
|
|---|
| 2153 | fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
|
|---|
| 2154 | }
|
|---|
| 2155 | }
|
|---|
| 2156 | set_fmt(s, fmtm, fmtd);
|
|---|
| 2157 | if ((s->capability & CAN_MULTI_CH)
|
|---|
| 2158 | && (file->f_mode & FMODE_WRITE)) {
|
|---|
| 2159 | val = set_dac_channels(s, val);
|
|---|
| 2160 | return put_user(val, (int *)arg);
|
|---|
| 2161 | }
|
|---|
| 2162 | }
|
|---|
| 2163 | return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_STEREO << CM_CFMT_ADCSHIFT)
|
|---|
| 2164 | : (CM_CFMT_STEREO << CM_CFMT_DACSHIFT))) ? 2 : 1, (int *)arg);
|
|---|
| 2165 |
|
|---|
| 2166 | case SNDCTL_DSP_GETFMTS: /* Returns a mask */
|
|---|
| 2167 | return put_user(AFMT_S16_LE|AFMT_U8|AFMT_AC3, (int *)arg);
|
|---|
| 2168 |
|
|---|
| 2169 | case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
|
|---|
| 2170 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2171 | if (get_user(val, (int *)arg))
|
|---|
| 2172 | return -EFAULT;
|
|---|
| 2173 | #else
|
|---|
| 2174 | get_user_ret(val, (int *)arg, -EFAULT);
|
|---|
| 2175 | #endif
|
|---|
| 2176 | if (val != AFMT_QUERY) {
|
|---|
| 2177 | fmtd = 0;
|
|---|
| 2178 | fmtm = ~0;
|
|---|
| 2179 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2180 | stop_adc(s);
|
|---|
| 2181 | s->dma_adc.ready = 0;
|
|---|
| 2182 | if (val == AFMT_S16_LE)
|
|---|
| 2183 | fmtd |= CM_CFMT_16BIT << CM_CFMT_ADCSHIFT;
|
|---|
| 2184 | else
|
|---|
| 2185 | fmtm &= ~(CM_CFMT_16BIT << CM_CFMT_ADCSHIFT);
|
|---|
| 2186 | }
|
|---|
| 2187 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2188 | stop_dac(s);
|
|---|
| 2189 | s->dma_dac.ready = 0;
|
|---|
| 2190 | if (val == AFMT_S16_LE || val == AFMT_AC3)
|
|---|
| 2191 | fmtd |= CM_CFMT_16BIT << CM_CFMT_DACSHIFT;
|
|---|
| 2192 | else
|
|---|
| 2193 | fmtm &= ~(CM_CFMT_16BIT << CM_CFMT_DACSHIFT);
|
|---|
| 2194 | if (val == AFMT_AC3) {
|
|---|
| 2195 | fmtd |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
|
|---|
| 2196 | set_ac3(s, s->ratedac);
|
|---|
| 2197 | } else
|
|---|
| 2198 | set_ac3(s, 0);
|
|---|
| 2199 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 2200 | s->dma_adc.ready = 0;
|
|---|
| 2201 | if (val == AFMT_S16_LE)
|
|---|
| 2202 | fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
|
|---|
| 2203 | else
|
|---|
| 2204 | fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
|
|---|
| 2205 | }
|
|---|
| 2206 | }
|
|---|
| 2207 | set_fmt(s, fmtm, fmtd);
|
|---|
| 2208 | }
|
|---|
| 2209 | if (s->status & DO_AC3) return put_user(AFMT_AC3, (int *)arg);
|
|---|
| 2210 | return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_16BIT << CM_CFMT_ADCSHIFT)
|
|---|
| 2211 | : (CM_CFMT_16BIT << CM_CFMT_DACSHIFT))) ? AFMT_S16_LE : AFMT_U8, (int *)arg);
|
|---|
| 2212 |
|
|---|
| 2213 | case SNDCTL_DSP_POST:
|
|---|
| 2214 | return 0;
|
|---|
| 2215 |
|
|---|
| 2216 | case SNDCTL_DSP_GETTRIGGER:
|
|---|
| 2217 | val = 0;
|
|---|
| 2218 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 2219 | if (file->f_mode & FMODE_WRITE &&
|
|---|
| 2220 | (s->enable & CM_ENABLE_CH1) &&
|
|---|
| 2221 | (s->enable & CM_ENABLE_CH0))
|
|---|
| 2222 | val |= PCM_ENABLE_OUTPUT;
|
|---|
| 2223 | return put_user(val, (int *)arg);
|
|---|
| 2224 | }
|
|---|
| 2225 | if (file->f_mode & FMODE_READ && s->enable & CM_ENABLE_CH0)
|
|---|
| 2226 | val |= PCM_ENABLE_INPUT;
|
|---|
| 2227 | if (file->f_mode & FMODE_WRITE && s->enable & CM_ENABLE_CH1)
|
|---|
| 2228 | val |= PCM_ENABLE_OUTPUT;
|
|---|
| 2229 | return put_user(val, (int *)arg);
|
|---|
| 2230 |
|
|---|
| 2231 | case SNDCTL_DSP_SETTRIGGER:
|
|---|
| 2232 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2233 | if (get_user(val, (int *)arg))
|
|---|
| 2234 | return -EFAULT;
|
|---|
| 2235 | #else
|
|---|
| 2236 | get_user_ret(val, (int *)arg, -EFAULT);
|
|---|
| 2237 | #endif
|
|---|
| 2238 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2239 | if (val & PCM_ENABLE_INPUT) {
|
|---|
| 2240 | if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
|
|---|
| 2241 | return ret;
|
|---|
| 2242 | start_adc(s);
|
|---|
| 2243 | } else
|
|---|
| 2244 | stop_adc(s);
|
|---|
| 2245 | }
|
|---|
| 2246 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2247 | if (val & PCM_ENABLE_OUTPUT) {
|
|---|
| 2248 | if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
|
|---|
| 2249 | return ret;
|
|---|
| 2250 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 2251 | if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
|
|---|
| 2252 | return ret;
|
|---|
| 2253 | }
|
|---|
| 2254 | start_dac(s);
|
|---|
| 2255 | } else
|
|---|
| 2256 | stop_dac(s);
|
|---|
| 2257 | }
|
|---|
| 2258 | return 0;
|
|---|
| 2259 |
|
|---|
| 2260 | case SNDCTL_DSP_GETOSPACE:
|
|---|
| 2261 | if (!(file->f_mode & FMODE_WRITE))
|
|---|
| 2262 | return -EINVAL;
|
|---|
| 2263 | if (!(s->enable & CM_ENABLE_CH1) && (val = prog_dmabuf(s, 0)) != 0)
|
|---|
| 2264 | return val;
|
|---|
| 2265 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2266 | cm_update_ptr(s);
|
|---|
| 2267 | abinfo.fragsize = s->dma_dac.fragsize;
|
|---|
| 2268 | abinfo.bytes = s->dma_dac.dmasize - s->dma_dac.count;
|
|---|
| 2269 | abinfo.fragstotal = s->dma_dac.numfrag;
|
|---|
| 2270 | abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
|
|---|
| 2271 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2272 | return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
|
|---|
| 2273 |
|
|---|
| 2274 | case SNDCTL_DSP_GETISPACE:
|
|---|
| 2275 | if (!(file->f_mode & FMODE_READ))
|
|---|
| 2276 | return -EINVAL;
|
|---|
| 2277 | if (!(s->enable & CM_ENABLE_CH0) && (val = prog_dmabuf(s, 1)) != 0)
|
|---|
| 2278 | return val;
|
|---|
| 2279 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2280 | cm_update_ptr(s);
|
|---|
| 2281 | abinfo.fragsize = s->dma_adc.fragsize;
|
|---|
| 2282 | abinfo.bytes = s->dma_adc.count;
|
|---|
| 2283 | abinfo.fragstotal = s->dma_adc.numfrag;
|
|---|
| 2284 | abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
|
|---|
| 2285 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2286 | return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
|
|---|
| 2287 |
|
|---|
| 2288 | case SNDCTL_DSP_NONBLOCK:
|
|---|
| 2289 | file->f_flags |= O_NONBLOCK;
|
|---|
| 2290 | return 0;
|
|---|
| 2291 |
|
|---|
| 2292 | case SNDCTL_DSP_GETODELAY:
|
|---|
| 2293 | if (!(file->f_mode & FMODE_WRITE))
|
|---|
| 2294 | return -EINVAL;
|
|---|
| 2295 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2296 | cm_update_ptr(s);
|
|---|
| 2297 | val = s->dma_dac.count;
|
|---|
| 2298 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2299 | return put_user(val, (int *)arg);
|
|---|
| 2300 |
|
|---|
| 2301 | case SNDCTL_DSP_GETIPTR:
|
|---|
| 2302 | if (!(file->f_mode & FMODE_READ))
|
|---|
| 2303 | return -EINVAL;
|
|---|
| 2304 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2305 | cm_update_ptr(s);
|
|---|
| 2306 | cinfo.bytes = s->dma_adc.total_bytes;
|
|---|
| 2307 | cinfo.blocks = s->dma_adc.count >> s->dma_adc.fragshift;
|
|---|
| 2308 | cinfo.ptr = s->dma_adc.hwptr;
|
|---|
| 2309 | if (s->dma_adc.mapped)
|
|---|
| 2310 | s->dma_adc.count &= s->dma_adc.fragsize-1;
|
|---|
| 2311 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2312 | return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
|
|---|
| 2313 |
|
|---|
| 2314 | case SNDCTL_DSP_GETOPTR:
|
|---|
| 2315 | if (!(file->f_mode & FMODE_WRITE))
|
|---|
| 2316 | return -EINVAL;
|
|---|
| 2317 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2318 | cm_update_ptr(s);
|
|---|
| 2319 | cinfo.bytes = s->dma_dac.total_bytes;
|
|---|
| 2320 | cinfo.blocks = s->dma_dac.count >> s->dma_dac.fragshift;
|
|---|
| 2321 | cinfo.ptr = s->dma_dac.hwptr;
|
|---|
| 2322 | if (s->dma_dac.mapped)
|
|---|
| 2323 | s->dma_dac.count &= s->dma_dac.fragsize-1;
|
|---|
| 2324 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 2325 | if (s->dma_adc.mapped)
|
|---|
| 2326 | s->dma_adc.count &= s->dma_adc.fragsize-1;
|
|---|
| 2327 | }
|
|---|
| 2328 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2329 | return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
|
|---|
| 2330 |
|
|---|
| 2331 | case SNDCTL_DSP_GETBLKSIZE:
|
|---|
| 2332 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2333 | if ((val = prog_dmabuf(s, 0)))
|
|---|
| 2334 | return val;
|
|---|
| 2335 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 2336 | if ((val = prog_dmabuf(s, 1)))
|
|---|
| 2337 | return val;
|
|---|
| 2338 | return put_user(2 * s->dma_dac.fragsize, (int *)arg);
|
|---|
| 2339 | }
|
|---|
| 2340 | return put_user(s->dma_dac.fragsize, (int *)arg);
|
|---|
| 2341 | }
|
|---|
| 2342 | if ((val = prog_dmabuf(s, 1)))
|
|---|
| 2343 | return val;
|
|---|
| 2344 | return put_user(s->dma_adc.fragsize, (int *)arg);
|
|---|
| 2345 |
|
|---|
| 2346 | case SNDCTL_DSP_SETFRAGMENT:
|
|---|
| 2347 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2348 | if (get_user(val, (int *)arg))
|
|---|
| 2349 | return -EFAULT;
|
|---|
| 2350 | #else
|
|---|
| 2351 | get_user_ret(val, (int *)arg, -EFAULT);
|
|---|
| 2352 | #endif
|
|---|
| 2353 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2354 | s->dma_adc.ossfragshift = val & 0xffff;
|
|---|
| 2355 | s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
|
|---|
| 2356 | if (s->dma_adc.ossfragshift < 4)
|
|---|
| 2357 | s->dma_adc.ossfragshift = 4;
|
|---|
| 2358 | if (s->dma_adc.ossfragshift > 15)
|
|---|
| 2359 | s->dma_adc.ossfragshift = 15;
|
|---|
| 2360 | if (s->dma_adc.ossmaxfrags < 4)
|
|---|
| 2361 | s->dma_adc.ossmaxfrags = 4;
|
|---|
| 2362 | }
|
|---|
| 2363 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2364 | s->dma_dac.ossfragshift = val & 0xffff;
|
|---|
| 2365 | s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
|
|---|
| 2366 | if (s->dma_dac.ossfragshift < 4)
|
|---|
| 2367 | s->dma_dac.ossfragshift = 4;
|
|---|
| 2368 | if (s->dma_dac.ossfragshift > 15)
|
|---|
| 2369 | s->dma_dac.ossfragshift = 15;
|
|---|
| 2370 | if (s->dma_dac.ossmaxfrags < 4)
|
|---|
| 2371 | s->dma_dac.ossmaxfrags = 4;
|
|---|
| 2372 | if (s->status & DO_DUAL_DAC) {
|
|---|
| 2373 | s->dma_adc.ossfragshift = s->dma_dac.ossfragshift;
|
|---|
| 2374 | s->dma_adc.ossmaxfrags = s->dma_dac.ossmaxfrags;
|
|---|
| 2375 | }
|
|---|
| 2376 | }
|
|---|
| 2377 | return 0;
|
|---|
| 2378 |
|
|---|
| 2379 | case SNDCTL_DSP_SUBDIVIDE:
|
|---|
| 2380 | if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
|
|---|
| 2381 | (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
|
|---|
| 2382 | return -EINVAL;
|
|---|
| 2383 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2384 | if (get_user(val, (int *)arg))
|
|---|
| 2385 | return -EFAULT;
|
|---|
| 2386 | #else
|
|---|
| 2387 | get_user_ret(val, (int *)arg, -EFAULT);
|
|---|
| 2388 | #endif
|
|---|
| 2389 | if (val != 1 && val != 2 && val != 4)
|
|---|
| 2390 | return -EINVAL;
|
|---|
| 2391 | if (file->f_mode & FMODE_READ)
|
|---|
| 2392 | s->dma_adc.subdivision = val;
|
|---|
| 2393 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2394 | s->dma_dac.subdivision = val;
|
|---|
| 2395 | if (s->status & DO_DUAL_DAC)
|
|---|
| 2396 | s->dma_adc.subdivision = val;
|
|---|
| 2397 | }
|
|---|
| 2398 | return 0;
|
|---|
| 2399 |
|
|---|
| 2400 | case SOUND_PCM_READ_RATE:
|
|---|
| 2401 | return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int *)arg);
|
|---|
| 2402 |
|
|---|
| 2403 | case SOUND_PCM_READ_CHANNELS:
|
|---|
| 2404 | return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_STEREO << CM_CFMT_ADCSHIFT) : (CM_CFMT_STEREO << CM_CFMT_DACSHIFT))) ? 2 : 1, (int *)arg);
|
|---|
| 2405 |
|
|---|
| 2406 | case SOUND_PCM_READ_BITS:
|
|---|
| 2407 | return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_16BIT << CM_CFMT_ADCSHIFT) : (CM_CFMT_16BIT << CM_CFMT_DACSHIFT))) ? 16 : 8, (int *)arg);
|
|---|
| 2408 |
|
|---|
| 2409 | case SOUND_PCM_READ_FILTER:
|
|---|
| 2410 | return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int *)arg);
|
|---|
| 2411 |
|
|---|
| 2412 | case SNDCTL_DSP_GETCHANNELMASK:
|
|---|
| 2413 | return put_user(DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE|DSP_BIND_SPDIF, (int *)arg);
|
|---|
| 2414 |
|
|---|
| 2415 | case SNDCTL_DSP_BIND_CHANNEL:
|
|---|
| 2416 | if (get_user(val, (int *)arg))
|
|---|
| 2417 | return -EFAULT;
|
|---|
| 2418 | if (val == DSP_BIND_QUERY) {
|
|---|
| 2419 | val = DSP_BIND_FRONT;
|
|---|
| 2420 | if (s->status & DO_SPDIF_OUT)
|
|---|
| 2421 | val |= DSP_BIND_SPDIF;
|
|---|
| 2422 | else {
|
|---|
| 2423 | if (s->curr_channels == 4)
|
|---|
| 2424 | val |= DSP_BIND_SURR;
|
|---|
| 2425 | if (s->curr_channels > 4)
|
|---|
| 2426 | val |= DSP_BIND_CENTER_LFE;
|
|---|
| 2427 | }
|
|---|
| 2428 | } else {
|
|---|
| 2429 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2430 | stop_adc(s);
|
|---|
| 2431 | s->dma_adc.ready = 0;
|
|---|
| 2432 | }
|
|---|
| 2433 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2434 | stop_dac(s);
|
|---|
| 2435 | s->dma_dac.ready = 0;
|
|---|
| 2436 | if (val & DSP_BIND_SPDIF) {
|
|---|
| 2437 | set_spdifout(s, s->ratedac);
|
|---|
| 2438 | set_dac_channels(s, s->fmt & (CM_CFMT_STEREO << CM_CFMT_DACSHIFT) ? 2 : 1);
|
|---|
| 2439 | if (!(s->status & DO_SPDIF_OUT))
|
|---|
| 2440 | val &= ~DSP_BIND_SPDIF;
|
|---|
| 2441 | } else {
|
|---|
| 2442 | int channels;
|
|---|
| 2443 | int mask;
|
|---|
| 2444 |
|
|---|
| 2445 | mask = val & (DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE);
|
|---|
| 2446 | switch (mask) {
|
|---|
| 2447 | case DSP_BIND_FRONT:
|
|---|
| 2448 | channels = 2;
|
|---|
| 2449 | break;
|
|---|
| 2450 | case DSP_BIND_FRONT|DSP_BIND_SURR:
|
|---|
| 2451 | channels = 4;
|
|---|
| 2452 | break;
|
|---|
| 2453 | case DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE:
|
|---|
| 2454 | channels = 6;
|
|---|
| 2455 | break;
|
|---|
| 2456 | default:
|
|---|
| 2457 | channels = s->fmt & (CM_CFMT_STEREO << CM_CFMT_DACSHIFT) ? 2 : 1;
|
|---|
| 2458 | break;
|
|---|
| 2459 | }
|
|---|
| 2460 | set_dac_channels(s, channels);
|
|---|
| 2461 | }
|
|---|
| 2462 | }
|
|---|
| 2463 | }
|
|---|
| 2464 | return put_user(val, (int *)arg);
|
|---|
| 2465 |
|
|---|
| 2466 | case SOUND_PCM_WRITE_FILTER:
|
|---|
| 2467 | case SNDCTL_DSP_MAPINBUF:
|
|---|
| 2468 | case SNDCTL_DSP_MAPOUTBUF:
|
|---|
| 2469 | case SNDCTL_DSP_SETSYNCRO:
|
|---|
| 2470 | return -EINVAL;
|
|---|
| 2471 |
|
|---|
| 2472 | }
|
|---|
| 2473 | return mixer_ioctl(s, cmd, arg);
|
|---|
| 2474 | }
|
|---|
| 2475 |
|
|---|
| 2476 | static int cm_open(struct inode *inode, struct file *file)
|
|---|
| 2477 | {
|
|---|
| 2478 | int minor = MINOR(inode->i_rdev);
|
|---|
| 2479 | struct cm_state *s = devs;
|
|---|
| 2480 | unsigned char fmtm = ~0, fmts = 0;
|
|---|
| 2481 |
|
|---|
| 2482 | while (s && ((s->dev_audio ^ minor) & ~0xf))
|
|---|
| 2483 | s = s->next;
|
|---|
| 2484 | if (!s)
|
|---|
| 2485 | return -ENODEV;
|
|---|
| 2486 | VALIDATE_STATE(s);
|
|---|
| 2487 | file->private_data = s;
|
|---|
| 2488 | /* wait for device to become free */
|
|---|
| 2489 | down(&s->open_sem);
|
|---|
| 2490 | while (s->open_mode & file->f_mode) {
|
|---|
| 2491 | if (file->f_flags & O_NONBLOCK) {
|
|---|
| 2492 | up(&s->open_sem);
|
|---|
| 2493 | return -EBUSY;
|
|---|
| 2494 | }
|
|---|
| 2495 | up(&s->open_sem);
|
|---|
| 2496 | interruptible_sleep_on(&s->open_wait);
|
|---|
| 2497 | if (signal_pending(current))
|
|---|
| 2498 | return -ERESTARTSYS;
|
|---|
| 2499 | down(&s->open_sem);
|
|---|
| 2500 | }
|
|---|
| 2501 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2502 | fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_ADCSHIFT);
|
|---|
| 2503 | if ((minor & 0xf) == SND_DEV_DSP16)
|
|---|
| 2504 | fmts |= CM_CFMT_16BIT << CM_CFMT_ADCSHIFT;
|
|---|
| 2505 | s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags = s->dma_adc.subdivision = 0;
|
|---|
| 2506 | set_adc_rate(s, 8000);
|
|---|
| 2507 | }
|
|---|
| 2508 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2509 | fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_DACSHIFT);
|
|---|
| 2510 | if ((minor & 0xf) == SND_DEV_DSP16)
|
|---|
| 2511 | fmts |= CM_CFMT_16BIT << CM_CFMT_DACSHIFT;
|
|---|
| 2512 | s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags = s->dma_dac.subdivision = 0;
|
|---|
| 2513 | set_dac_rate(s, 8000);
|
|---|
| 2514 | // clear previous multichannel, spdif, ac3 state
|
|---|
| 2515 | set_spdifout(s, 0);
|
|---|
| 2516 | if (s->deviceid == PCI_DEVICE_ID_CMEDIA_CM8738) {
|
|---|
| 2517 | set_ac3(s, 0);
|
|---|
| 2518 | set_dac_channels(s, 1);
|
|---|
| 2519 | }
|
|---|
| 2520 | }
|
|---|
| 2521 | set_fmt(s, fmtm, fmts);
|
|---|
| 2522 | s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
|
|---|
| 2523 | up(&s->open_sem);
|
|---|
| 2524 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
|
|---|
| 2525 | MOD_INC_USE_COUNT;
|
|---|
| 2526 | #endif
|
|---|
| 2527 | return 0;
|
|---|
| 2528 | }
|
|---|
| 2529 |
|
|---|
| 2530 | static int cm_release(struct inode *inode, struct file *file)
|
|---|
| 2531 | {
|
|---|
| 2532 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 2533 |
|
|---|
| 2534 | VALIDATE_STATE(s);
|
|---|
| 2535 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2536 | lock_kernel();
|
|---|
| 2537 | #endif
|
|---|
| 2538 | if (file->f_mode & FMODE_WRITE)
|
|---|
| 2539 | drain_dac(s, file->f_flags & O_NONBLOCK);
|
|---|
| 2540 | down(&s->open_sem);
|
|---|
| 2541 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2542 | stop_dac(s);
|
|---|
| 2543 | #ifndef FIXEDDMA
|
|---|
| 2544 | dealloc_dmabuf(&s->dma_dac);
|
|---|
| 2545 | if (s->status & DO_DUAL_DAC)
|
|---|
| 2546 | dealloc_dmabuf(&s->dma_adc);
|
|---|
| 2547 | #endif
|
|---|
| 2548 | if (s->status & DO_MULTI_CH)
|
|---|
| 2549 | set_dac_channels(s, 0);
|
|---|
| 2550 | if (s->status & DO_AC3)
|
|---|
| 2551 | set_ac3(s, 0);
|
|---|
| 2552 | if (s->status & DO_SPDIF_OUT)
|
|---|
| 2553 | set_spdifout(s, 0);
|
|---|
| 2554 | }
|
|---|
| 2555 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2556 | stop_adc(s);
|
|---|
| 2557 | #ifndef FIXEDDMA
|
|---|
| 2558 | dealloc_dmabuf(&s->dma_adc);
|
|---|
| 2559 | #endif
|
|---|
| 2560 | }
|
|---|
| 2561 | s->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
|
|---|
| 2562 | up(&s->open_sem);
|
|---|
| 2563 | wake_up(&s->open_wait);
|
|---|
| 2564 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2565 | unlock_kernel();
|
|---|
| 2566 | #else
|
|---|
| 2567 | MOD_DEC_USE_COUNT;
|
|---|
| 2568 | #endif
|
|---|
| 2569 | return 0;
|
|---|
| 2570 | }
|
|---|
| 2571 |
|
|---|
| 2572 | static /*const*/ struct file_operations cm_audio_fops = {
|
|---|
| 2573 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2574 | owner: THIS_MODULE,
|
|---|
| 2575 | #endif
|
|---|
| 2576 | llseek: cm_llseek,
|
|---|
| 2577 | read: cm_read,
|
|---|
| 2578 | write: cm_write,
|
|---|
| 2579 | poll: cm_poll,
|
|---|
| 2580 | ioctl: cm_ioctl,
|
|---|
| 2581 | mmap: cm_mmap,
|
|---|
| 2582 | open: cm_open,
|
|---|
| 2583 | release: cm_release,
|
|---|
| 2584 | };
|
|---|
| 2585 |
|
|---|
| 2586 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 2587 | /* --------------------------------------------------------------------- */
|
|---|
| 2588 |
|
|---|
| 2589 | static ssize_t cm_midi_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
|
|---|
| 2590 | {
|
|---|
| 2591 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 2592 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2593 | DECLARE_WAITQUEUE(wait, current);
|
|---|
| 2594 | #endif
|
|---|
| 2595 | ssize_t ret;
|
|---|
| 2596 | unsigned long flags;
|
|---|
| 2597 | unsigned ptr;
|
|---|
| 2598 | int cnt;
|
|---|
| 2599 |
|
|---|
| 2600 | VALIDATE_STATE(s);
|
|---|
| 2601 | if (ppos != &file->f_pos)
|
|---|
| 2602 | return -ESPIPE;
|
|---|
| 2603 | if (!access_ok(VERIFY_WRITE, buffer, count))
|
|---|
| 2604 | return -EFAULT;
|
|---|
| 2605 | ret = 0;
|
|---|
| 2606 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2607 | add_wait_queue(&s->midi.iwait, &wait);
|
|---|
| 2608 | #endif
|
|---|
| 2609 | while (count > 0) {
|
|---|
| 2610 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2611 | ptr = s->midi.ird;
|
|---|
| 2612 | cnt = MIDIINBUF - ptr;
|
|---|
| 2613 | if (s->midi.icnt < cnt)
|
|---|
| 2614 | cnt = s->midi.icnt;
|
|---|
| 2615 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2616 | if (cnt > count)
|
|---|
| 2617 | cnt = count;
|
|---|
| 2618 | if (cnt <= 0) {
|
|---|
| 2619 | if (file->f_flags & O_NONBLOCK)
|
|---|
| 2620 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2621 | {
|
|---|
| 2622 | if (!ret)
|
|---|
| 2623 | ret = -EAGAIN;
|
|---|
| 2624 | break;
|
|---|
| 2625 | }
|
|---|
| 2626 | __set_current_state(TASK_INTERRUPTIBLE);
|
|---|
| 2627 | schedule();
|
|---|
| 2628 | if (signal_pending(current))
|
|---|
| 2629 | {
|
|---|
| 2630 | if (!ret)
|
|---|
| 2631 | ret = -ERESTARTSYS;
|
|---|
| 2632 | break;
|
|---|
| 2633 | }
|
|---|
| 2634 | #else
|
|---|
| 2635 | return ret ? ret : -EAGAIN;
|
|---|
| 2636 | interruptible_sleep_on(&s->midi.iwait);
|
|---|
| 2637 | if (signal_pending(current))
|
|---|
| 2638 | return ret ? ret : -ERESTARTSYS;
|
|---|
| 2639 | #endif
|
|---|
| 2640 | continue;
|
|---|
| 2641 | }
|
|---|
| 2642 | if (copy_to_user(buffer, s->midi.ibuf + ptr, cnt))
|
|---|
| 2643 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2644 | {
|
|---|
| 2645 | if (!ret)
|
|---|
| 2646 | ret = -EFAULT;
|
|---|
| 2647 | break;
|
|---|
| 2648 | }
|
|---|
| 2649 | #else
|
|---|
| 2650 | return ret ? ret : -EFAULT;
|
|---|
| 2651 | #endif
|
|---|
| 2652 | ptr = (ptr + cnt) % MIDIINBUF;
|
|---|
| 2653 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2654 | s->midi.ird = ptr;
|
|---|
| 2655 | s->midi.icnt -= cnt;
|
|---|
| 2656 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2657 | count -= cnt;
|
|---|
| 2658 | buffer += cnt;
|
|---|
| 2659 | ret += cnt;
|
|---|
| 2660 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2661 | break;
|
|---|
| 2662 | #endif
|
|---|
| 2663 | }
|
|---|
| 2664 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2665 | __set_current_state(TASK_RUNNING);
|
|---|
| 2666 | remove_wait_queue(&s->midi.iwait, &wait);
|
|---|
| 2667 | #endif
|
|---|
| 2668 | return ret;
|
|---|
| 2669 | }
|
|---|
| 2670 |
|
|---|
| 2671 | static ssize_t cm_midi_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
|
|---|
| 2672 | {
|
|---|
| 2673 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 2674 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2675 | DECLARE_WAITQUEUE(wait, current);
|
|---|
| 2676 | #endif
|
|---|
| 2677 | ssize_t ret;
|
|---|
| 2678 | unsigned long flags;
|
|---|
| 2679 | unsigned ptr;
|
|---|
| 2680 | int cnt;
|
|---|
| 2681 |
|
|---|
| 2682 | VALIDATE_STATE(s);
|
|---|
| 2683 | if (ppos != &file->f_pos)
|
|---|
| 2684 | return -ESPIPE;
|
|---|
| 2685 | if (!access_ok(VERIFY_READ, buffer, count))
|
|---|
| 2686 | return -EFAULT;
|
|---|
| 2687 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2688 | if (count == 0)
|
|---|
| 2689 | return 0;
|
|---|
| 2690 | #endif
|
|---|
| 2691 | ret = 0;
|
|---|
| 2692 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2693 | add_wait_queue(&s->midi.owait, &wait);
|
|---|
| 2694 | #endif
|
|---|
| 2695 | while (count > 0) {
|
|---|
| 2696 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2697 | ptr = s->midi.owr;
|
|---|
| 2698 | cnt = MIDIOUTBUF - ptr;
|
|---|
| 2699 | if (s->midi.ocnt + cnt > MIDIOUTBUF)
|
|---|
| 2700 | cnt = MIDIOUTBUF - s->midi.ocnt;
|
|---|
| 2701 | if (cnt <= 0)
|
|---|
| 2702 | cm_handle_midi(s);
|
|---|
| 2703 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2704 | if (cnt > count)
|
|---|
| 2705 | cnt = count;
|
|---|
| 2706 | if (cnt <= 0) {
|
|---|
| 2707 | if (file->f_flags & O_NONBLOCK)
|
|---|
| 2708 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2709 | {
|
|---|
| 2710 | if (!ret)
|
|---|
| 2711 | ret = -EAGAIN;
|
|---|
| 2712 | break;
|
|---|
| 2713 | }
|
|---|
| 2714 | __set_current_state(TASK_INTERRUPTIBLE);
|
|---|
| 2715 | schedule();
|
|---|
| 2716 | if (signal_pending(current)) {
|
|---|
| 2717 | if (!ret)
|
|---|
| 2718 | ret = -ERESTARTSYS;
|
|---|
| 2719 | break;
|
|---|
| 2720 | }
|
|---|
| 2721 | #else
|
|---|
| 2722 | return ret ? ret : -EAGAIN;
|
|---|
| 2723 | interruptible_sleep_on(&s->midi.owait);
|
|---|
| 2724 | if (signal_pending(current))
|
|---|
| 2725 | return ret ? ret : -ERESTARTSYS;
|
|---|
| 2726 | #endif
|
|---|
| 2727 | continue;
|
|---|
| 2728 | }
|
|---|
| 2729 | if (copy_from_user(s->midi.obuf + ptr, buffer, cnt))
|
|---|
| 2730 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2731 | {
|
|---|
| 2732 | if (!ret)
|
|---|
| 2733 | ret = -EFAULT;
|
|---|
| 2734 | break;
|
|---|
| 2735 | }
|
|---|
| 2736 | #else
|
|---|
| 2737 | return ret ? ret : -EFAULT;
|
|---|
| 2738 | #endif
|
|---|
| 2739 | ptr = (ptr + cnt) % MIDIOUTBUF;
|
|---|
| 2740 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2741 | s->midi.owr = ptr;
|
|---|
| 2742 | s->midi.ocnt += cnt;
|
|---|
| 2743 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2744 | count -= cnt;
|
|---|
| 2745 | buffer += cnt;
|
|---|
| 2746 | ret += cnt;
|
|---|
| 2747 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2748 | cm_handle_midi(s);
|
|---|
| 2749 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2750 | }
|
|---|
| 2751 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2752 | __set_current_state(TASK_RUNNING);
|
|---|
| 2753 | remove_wait_queue(&s->midi.owait, &wait);
|
|---|
| 2754 | #endif
|
|---|
| 2755 | return ret;
|
|---|
| 2756 | }
|
|---|
| 2757 |
|
|---|
| 2758 | static unsigned int cm_midi_poll(struct file *file, struct poll_table_struct *wait)
|
|---|
| 2759 | {
|
|---|
| 2760 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 2761 | unsigned long flags;
|
|---|
| 2762 | unsigned int mask = 0;
|
|---|
| 2763 |
|
|---|
| 2764 | VALIDATE_STATE(s);
|
|---|
| 2765 | if (file->f_mode & FMODE_WRITE)
|
|---|
| 2766 | poll_wait(file, &s->midi.owait, wait);
|
|---|
| 2767 | if (file->f_mode & FMODE_READ)
|
|---|
| 2768 | poll_wait(file, &s->midi.iwait, wait);
|
|---|
| 2769 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2770 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2771 | if (s->midi.icnt > 0)
|
|---|
| 2772 | mask |= POLLIN | POLLRDNORM;
|
|---|
| 2773 | }
|
|---|
| 2774 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2775 | if (s->midi.ocnt < MIDIOUTBUF)
|
|---|
| 2776 | mask |= POLLOUT | POLLWRNORM;
|
|---|
| 2777 | }
|
|---|
| 2778 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2779 | return mask;
|
|---|
| 2780 | }
|
|---|
| 2781 |
|
|---|
| 2782 | static int cm_midi_open(struct inode *inode, struct file *file)
|
|---|
| 2783 | {
|
|---|
| 2784 | int minor = MINOR(inode->i_rdev);
|
|---|
| 2785 | struct cm_state *s = devs;
|
|---|
| 2786 | unsigned long flags;
|
|---|
| 2787 |
|
|---|
| 2788 | while (s && s->dev_midi != minor)
|
|---|
| 2789 | s = s->next;
|
|---|
| 2790 | if (!s)
|
|---|
| 2791 | return -ENODEV;
|
|---|
| 2792 | VALIDATE_STATE(s);
|
|---|
| 2793 | file->private_data = s;
|
|---|
| 2794 | /* wait for device to become free */
|
|---|
| 2795 | down(&s->open_sem);
|
|---|
| 2796 | while (s->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
|
|---|
| 2797 | if (file->f_flags & O_NONBLOCK) {
|
|---|
| 2798 | up(&s->open_sem);
|
|---|
| 2799 | return -EBUSY;
|
|---|
| 2800 | }
|
|---|
| 2801 | up(&s->open_sem);
|
|---|
| 2802 | interruptible_sleep_on(&s->open_wait);
|
|---|
| 2803 | if (signal_pending(current))
|
|---|
| 2804 | return -ERESTARTSYS;
|
|---|
| 2805 | down(&s->open_sem);
|
|---|
| 2806 | }
|
|---|
| 2807 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2808 | if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
|
|---|
| 2809 | s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
|
|---|
| 2810 | s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
|
|---|
| 2811 | /* enable MPU-401 */
|
|---|
| 2812 | maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 4);
|
|---|
| 2813 | outb(0xff, s->iomidi+1); /* reset command */
|
|---|
| 2814 | if (!(inb(s->iomidi+1) & 0x80))
|
|---|
| 2815 | inb(s->iomidi);
|
|---|
| 2816 | outb(0x3f, s->iomidi+1); /* uart command */
|
|---|
| 2817 | if (!(inb(s->iomidi+1) & 0x80))
|
|---|
| 2818 | inb(s->iomidi);
|
|---|
| 2819 | s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
|
|---|
| 2820 | init_timer(&s->midi.timer);
|
|---|
| 2821 | s->midi.timer.expires = jiffies+1;
|
|---|
| 2822 | s->midi.timer.data = (unsigned long)s;
|
|---|
| 2823 | s->midi.timer.function = cm_midi_timer;
|
|---|
| 2824 | add_timer(&s->midi.timer);
|
|---|
| 2825 | }
|
|---|
| 2826 | if (file->f_mode & FMODE_READ) {
|
|---|
| 2827 | s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
|
|---|
| 2828 | }
|
|---|
| 2829 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2830 | s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
|
|---|
| 2831 | }
|
|---|
| 2832 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2833 | s->open_mode |= (file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE);
|
|---|
| 2834 | up(&s->open_sem);
|
|---|
| 2835 | MOD_INC_USE_COUNT;
|
|---|
| 2836 | return 0;
|
|---|
| 2837 | }
|
|---|
| 2838 |
|
|---|
| 2839 | static int cm_midi_release(struct inode *inode, struct file *file)
|
|---|
| 2840 | {
|
|---|
| 2841 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 2842 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2843 | DECLARE_WAITQUEUE(wait, current);
|
|---|
| 2844 | #else
|
|---|
| 2845 | struct wait_queue wait = { current, NULL };
|
|---|
| 2846 | #endif
|
|---|
| 2847 | unsigned long flags;
|
|---|
| 2848 | unsigned count, tmo;
|
|---|
| 2849 |
|
|---|
| 2850 | VALIDATE_STATE(s);
|
|---|
| 2851 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2852 | lock_kernel();
|
|---|
| 2853 | #endif
|
|---|
| 2854 |
|
|---|
| 2855 | if (file->f_mode & FMODE_WRITE) {
|
|---|
| 2856 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2857 | __set_current_state(TASK_INTERRUPTIBLE);
|
|---|
| 2858 | #else
|
|---|
| 2859 | current->state = TASK_INTERRUPTIBLE;
|
|---|
| 2860 | #endif
|
|---|
| 2861 | add_wait_queue(&s->midi.owait, &wait);
|
|---|
| 2862 | for (;;) {
|
|---|
| 2863 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2864 | count = s->midi.ocnt;
|
|---|
| 2865 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2866 | if (count <= 0)
|
|---|
| 2867 | break;
|
|---|
| 2868 | if (signal_pending(current))
|
|---|
| 2869 | break;
|
|---|
| 2870 | if (file->f_flags & O_NONBLOCK) {
|
|---|
| 2871 | remove_wait_queue(&s->midi.owait, &wait);
|
|---|
| 2872 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2873 | set_current_state(TASK_RUNNING);
|
|---|
| 2874 | #else
|
|---|
| 2875 | current->state = TASK_RUNNING;
|
|---|
| 2876 | #endif
|
|---|
| 2877 | return -EBUSY;
|
|---|
| 2878 | }
|
|---|
| 2879 | tmo = (count * HZ) / 3100;
|
|---|
| 2880 | if (!schedule_timeout(tmo ? : 1) && tmo)
|
|---|
| 2881 | printk(KERN_DEBUG "cm: midi timed out??\n");
|
|---|
| 2882 | }
|
|---|
| 2883 | remove_wait_queue(&s->midi.owait, &wait);
|
|---|
| 2884 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 2885 | set_current_state(TASK_RUNNING);
|
|---|
| 2886 | #else
|
|---|
| 2887 | current->state = TASK_RUNNING;
|
|---|
| 2888 | #endif
|
|---|
| 2889 | }
|
|---|
| 2890 | down(&s->open_sem);
|
|---|
| 2891 | s->open_mode &= (~(file->f_mode << FMODE_MIDI_SHIFT)) & (FMODE_MIDI_READ|FMODE_MIDI_WRITE);
|
|---|
| 2892 | spin_lock_irqsave(&s->lock, flags);
|
|---|
| 2893 | if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
|
|---|
| 2894 | del_timer(&s->midi.timer);
|
|---|
| 2895 | outb(0xff, s->iomidi+1); /* reset command */
|
|---|
| 2896 | if (!(inb(s->iomidi+1) & 0x80))
|
|---|
| 2897 | inb(s->iomidi);
|
|---|
| 2898 | /* disable MPU-401 */
|
|---|
| 2899 | maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~4, 0);
|
|---|
| 2900 | }
|
|---|
| 2901 | spin_unlock_irqrestore(&s->lock, flags);
|
|---|
| 2902 | up(&s->open_sem);
|
|---|
| 2903 | wake_up(&s->open_wait);
|
|---|
| 2904 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2905 | unlock_kernel();
|
|---|
| 2906 | #else
|
|---|
| 2907 | MOD_DEC_USE_COUNT;
|
|---|
| 2908 | #endif
|
|---|
| 2909 | return 0;
|
|---|
| 2910 | }
|
|---|
| 2911 |
|
|---|
| 2912 | static /*const*/ struct file_operations cm_midi_fops = {
|
|---|
| 2913 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 2914 | owner: THIS_MODULE,
|
|---|
| 2915 | #endif
|
|---|
| 2916 | llseek: cm_llseek,
|
|---|
| 2917 | read: cm_midi_read,
|
|---|
| 2918 | write: cm_midi_write,
|
|---|
| 2919 | poll: cm_midi_poll,
|
|---|
| 2920 | open: cm_midi_open,
|
|---|
| 2921 | release: cm_midi_release,
|
|---|
| 2922 | };
|
|---|
| 2923 | #endif
|
|---|
| 2924 |
|
|---|
| 2925 | /* --------------------------------------------------------------------- */
|
|---|
| 2926 |
|
|---|
| 2927 | #ifdef CONFIG_SOUND_CMPCI_FM
|
|---|
| 2928 | static int cm_dmfm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
|
|---|
| 2929 | {
|
|---|
| 2930 | static const unsigned char op_offset[18] = {
|
|---|
| 2931 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
|
|---|
| 2932 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
|
|---|
| 2933 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15
|
|---|
| 2934 | };
|
|---|
| 2935 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 2936 | struct dm_fm_voice v;
|
|---|
| 2937 | struct dm_fm_note n;
|
|---|
| 2938 | struct dm_fm_params p;
|
|---|
| 2939 | unsigned int io;
|
|---|
| 2940 | unsigned int regb;
|
|---|
| 2941 |
|
|---|
| 2942 | switch (cmd) {
|
|---|
| 2943 | case FM_IOCTL_RESET:
|
|---|
| 2944 | for (regb = 0xb0; regb < 0xb9; regb++) {
|
|---|
| 2945 | outb(regb, s->iosynth);
|
|---|
| 2946 | outb(0, s->iosynth+1);
|
|---|
| 2947 | outb(regb, s->iosynth+2);
|
|---|
| 2948 | outb(0, s->iosynth+3);
|
|---|
| 2949 | }
|
|---|
| 2950 | return 0;
|
|---|
| 2951 |
|
|---|
| 2952 | case FM_IOCTL_PLAY_NOTE:
|
|---|
| 2953 | if (copy_from_user(&n, (void *)arg, sizeof(n)))
|
|---|
| 2954 | return -EFAULT;
|
|---|
| 2955 | if (n.voice >= 18)
|
|---|
| 2956 | return -EINVAL;
|
|---|
| 2957 | if (n.voice >= 9) {
|
|---|
| 2958 | regb = n.voice - 9;
|
|---|
| 2959 | io = s->iosynth+2;
|
|---|
| 2960 | } else {
|
|---|
| 2961 | regb = n.voice;
|
|---|
| 2962 | io = s->iosynth;
|
|---|
| 2963 | }
|
|---|
| 2964 | outb(0xa0 + regb, io);
|
|---|
| 2965 | outb(n.fnum & 0xff, io+1);
|
|---|
| 2966 | outb(0xb0 + regb, io);
|
|---|
| 2967 | outb(((n.fnum >> 8) & 3) | ((n.octave & 7) << 2) | ((n.key_on & 1) << 5), io+1);
|
|---|
| 2968 | return 0;
|
|---|
| 2969 |
|
|---|
| 2970 | case FM_IOCTL_SET_VOICE:
|
|---|
| 2971 | if (copy_from_user(&v, (void *)arg, sizeof(v)))
|
|---|
| 2972 | return -EFAULT;
|
|---|
| 2973 | if (v.voice >= 18)
|
|---|
| 2974 | return -EINVAL;
|
|---|
| 2975 | regb = op_offset[v.voice];
|
|---|
| 2976 | io = s->iosynth + ((v.op & 1) << 1);
|
|---|
| 2977 | outb(0x20 + regb, io);
|
|---|
| 2978 | outb(((v.am & 1) << 7) | ((v.vibrato & 1) << 6) | ((v.do_sustain & 1) << 5) |
|
|---|
| 2979 | ((v.kbd_scale & 1) << 4) | (v.harmonic & 0xf), io+1);
|
|---|
| 2980 | outb(0x40 + regb, io);
|
|---|
| 2981 | outb(((v.scale_level & 0x3) << 6) | (v.volume & 0x3f), io+1);
|
|---|
| 2982 | outb(0x60 + regb, io);
|
|---|
| 2983 | outb(((v.attack & 0xf) << 4) | (v.decay & 0xf), io+1);
|
|---|
| 2984 | outb(0x80 + regb, io);
|
|---|
| 2985 | outb(((v.sustain & 0xf) << 4) | (v.release & 0xf), io+1);
|
|---|
| 2986 | outb(0xe0 + regb, io);
|
|---|
| 2987 | outb(v.waveform & 0x7, io+1);
|
|---|
| 2988 | if (n.voice >= 9) {
|
|---|
| 2989 | regb = n.voice - 9;
|
|---|
| 2990 | io = s->iosynth+2;
|
|---|
| 2991 | } else {
|
|---|
| 2992 | regb = n.voice;
|
|---|
| 2993 | io = s->iosynth;
|
|---|
| 2994 | }
|
|---|
| 2995 | outb(0xc0 + regb, io);
|
|---|
| 2996 | outb(((v.right & 1) << 5) | ((v.left & 1) << 4) | ((v.feedback & 7) << 1) |
|
|---|
| 2997 | (v.connection & 1), io+1);
|
|---|
| 2998 | return 0;
|
|---|
| 2999 |
|
|---|
| 3000 | case FM_IOCTL_SET_PARAMS:
|
|---|
| 3001 | if (copy_from_user(&p, (void *)arg, sizeof(p)))
|
|---|
| 3002 | return -EFAULT;
|
|---|
| 3003 | outb(0x08, s->iosynth);
|
|---|
| 3004 | outb((p.kbd_split & 1) << 6, s->iosynth+1);
|
|---|
| 3005 | outb(0xbd, s->iosynth);
|
|---|
| 3006 | outb(((p.am_depth & 1) << 7) | ((p.vib_depth & 1) << 6) | ((p.rhythm & 1) << 5) | ((p.bass & 1) << 4) |
|
|---|
| 3007 | ((p.snare & 1) << 3) | ((p.tomtom & 1) << 2) | ((p.cymbal & 1) << 1) | (p.hihat & 1), s->iosynth+1);
|
|---|
| 3008 | return 0;
|
|---|
| 3009 |
|
|---|
| 3010 | case FM_IOCTL_SET_OPL:
|
|---|
| 3011 | outb(4, s->iosynth+2);
|
|---|
| 3012 | outb(arg, s->iosynth+3);
|
|---|
| 3013 | return 0;
|
|---|
| 3014 |
|
|---|
| 3015 | case FM_IOCTL_SET_MODE:
|
|---|
| 3016 | outb(5, s->iosynth+2);
|
|---|
| 3017 | outb(arg & 1, s->iosynth+3);
|
|---|
| 3018 | return 0;
|
|---|
| 3019 |
|
|---|
| 3020 | default:
|
|---|
| 3021 | return -EINVAL;
|
|---|
| 3022 | }
|
|---|
| 3023 | }
|
|---|
| 3024 |
|
|---|
| 3025 | static int cm_dmfm_open(struct inode *inode, struct file *file)
|
|---|
| 3026 | {
|
|---|
| 3027 | int minor = MINOR(inode->i_rdev);
|
|---|
| 3028 | struct cm_state *s = devs;
|
|---|
| 3029 |
|
|---|
| 3030 | while (s && s->dev_dmfm != minor)
|
|---|
| 3031 | s = s->next;
|
|---|
| 3032 | if (!s)
|
|---|
| 3033 | return -ENODEV;
|
|---|
| 3034 | VALIDATE_STATE(s);
|
|---|
| 3035 | file->private_data = s;
|
|---|
| 3036 | /* wait for device to become free */
|
|---|
| 3037 | down(&s->open_sem);
|
|---|
| 3038 | while (s->open_mode & FMODE_DMFM) {
|
|---|
| 3039 | if (file->f_flags & O_NONBLOCK) {
|
|---|
| 3040 | up(&s->open_sem);
|
|---|
| 3041 | return -EBUSY;
|
|---|
| 3042 | }
|
|---|
| 3043 | up(&s->open_sem);
|
|---|
| 3044 | interruptible_sleep_on(&s->open_wait);
|
|---|
| 3045 | if (signal_pending(current))
|
|---|
| 3046 | return -ERESTARTSYS;
|
|---|
| 3047 | down(&s->open_sem);
|
|---|
| 3048 | }
|
|---|
| 3049 | /* init the stuff */
|
|---|
| 3050 | outb(1, s->iosynth);
|
|---|
| 3051 | outb(0x20, s->iosynth+1); /* enable waveforms */
|
|---|
| 3052 | outb(4, s->iosynth+2);
|
|---|
| 3053 | outb(0, s->iosynth+3); /* no 4op enabled */
|
|---|
| 3054 | outb(5, s->iosynth+2);
|
|---|
| 3055 | outb(1, s->iosynth+3); /* enable OPL3 */
|
|---|
| 3056 | s->open_mode |= FMODE_DMFM;
|
|---|
| 3057 | up(&s->open_sem);
|
|---|
| 3058 | MOD_INC_USE_COUNT;
|
|---|
| 3059 | return 0;
|
|---|
| 3060 | }
|
|---|
| 3061 |
|
|---|
| 3062 | static int cm_dmfm_release(struct inode *inode, struct file *file)
|
|---|
| 3063 | {
|
|---|
| 3064 | struct cm_state *s = (struct cm_state *)file->private_data;
|
|---|
| 3065 | unsigned int regb;
|
|---|
| 3066 |
|
|---|
| 3067 | VALIDATE_STATE(s);
|
|---|
| 3068 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3069 | lock_kernel();
|
|---|
| 3070 | #endif
|
|---|
| 3071 | down(&s->open_sem);
|
|---|
| 3072 | s->open_mode &= ~FMODE_DMFM;
|
|---|
| 3073 | for (regb = 0xb0; regb < 0xb9; regb++) {
|
|---|
| 3074 | outb(regb, s->iosynth);
|
|---|
| 3075 | outb(0, s->iosynth+1);
|
|---|
| 3076 | outb(regb, s->iosynth+2);
|
|---|
| 3077 | outb(0, s->iosynth+3);
|
|---|
| 3078 | }
|
|---|
| 3079 | up(&s->open_sem);
|
|---|
| 3080 | wake_up(&s->open_wait);
|
|---|
| 3081 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3082 | unlock_kernel();
|
|---|
| 3083 | #else
|
|---|
| 3084 | MOD_DEC_USE_COUNT;
|
|---|
| 3085 | #endif
|
|---|
| 3086 | return 0;
|
|---|
| 3087 | }
|
|---|
| 3088 |
|
|---|
| 3089 | static /*const*/ struct file_operations cm_dmfm_fops = {
|
|---|
| 3090 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3091 | owner: THIS_MODULE,
|
|---|
| 3092 | #endif
|
|---|
| 3093 | llseek: cm_llseek,
|
|---|
| 3094 | ioctl: cm_dmfm_ioctl,
|
|---|
| 3095 | open: cm_dmfm_open,
|
|---|
| 3096 | release: cm_dmfm_release,
|
|---|
| 3097 | };
|
|---|
| 3098 | #endif /* CONFIG_SOUND_CMPCI_FM */
|
|---|
| 3099 |
|
|---|
| 3100 | /* --------------------------------------------------------------------- */
|
|---|
| 3101 |
|
|---|
| 3102 | /* maximum number of devices */
|
|---|
| 3103 | #define NR_DEVICE 5
|
|---|
| 3104 |
|
|---|
| 3105 | #if 0
|
|---|
| 3106 | static int reverb[NR_DEVICE] = { 0, };
|
|---|
| 3107 |
|
|---|
| 3108 | static int wavetable[NR_DEVICE] = { 0, };
|
|---|
| 3109 | #endif
|
|---|
| 3110 |
|
|---|
| 3111 | /* --------------------------------------------------------------------- */
|
|---|
| 3112 |
|
|---|
| 3113 | static struct initvol {
|
|---|
| 3114 | int mixch;
|
|---|
| 3115 | int vol;
|
|---|
| 3116 | } initvol[] __initdata = {
|
|---|
| 3117 | { SOUND_MIXER_WRITE_CD, 0x4f4f },
|
|---|
| 3118 | { SOUND_MIXER_WRITE_LINE, 0x4f4f },
|
|---|
| 3119 | { SOUND_MIXER_WRITE_MIC, 0x4f4f },
|
|---|
| 3120 | { SOUND_MIXER_WRITE_SYNTH, 0x4f4f },
|
|---|
| 3121 | { SOUND_MIXER_WRITE_VOLUME, 0x4f4f },
|
|---|
| 3122 | { SOUND_MIXER_WRITE_PCM, 0x4f4f }
|
|---|
| 3123 | };
|
|---|
| 3124 |
|
|---|
| 3125 | /* check chip version and capability */
|
|---|
| 3126 | static int query_chip(struct cm_state *s)
|
|---|
| 3127 | {
|
|---|
| 3128 | int ChipVersion = -1;
|
|---|
| 3129 | unsigned char RegValue;
|
|---|
| 3130 |
|
|---|
| 3131 | // check reg 0Ch, bit 24-31
|
|---|
| 3132 | RegValue = inb(s->iobase + CODEC_CMI_INT_HLDCLR + 3);
|
|---|
| 3133 | if (RegValue == 0) {
|
|---|
| 3134 | // check reg 08h, bit 24-28
|
|---|
| 3135 | RegValue = inb(s->iobase + CODEC_CMI_CHFORMAT + 3);
|
|---|
| 3136 | RegValue &= 0x1f;
|
|---|
| 3137 | if (RegValue == 0) {
|
|---|
| 3138 | ChipVersion = 33;
|
|---|
| 3139 | s->max_channels = 4;
|
|---|
| 3140 | s->capability |= CAN_AC3_SW;
|
|---|
| 3141 | s->capability |= CAN_DUAL_DAC;
|
|---|
| 3142 | } else {
|
|---|
| 3143 | ChipVersion = 37;
|
|---|
| 3144 | s->max_channels = 4;
|
|---|
| 3145 | s->capability |= CAN_AC3_HW;
|
|---|
| 3146 | s->capability |= CAN_DUAL_DAC;
|
|---|
| 3147 | }
|
|---|
| 3148 | } else {
|
|---|
| 3149 | // check reg 0Ch, bit 26
|
|---|
| 3150 | if (RegValue & (1 << (26-24))) {
|
|---|
| 3151 | ChipVersion = 39;
|
|---|
| 3152 | if (RegValue & (1 << (24-24)))
|
|---|
| 3153 | s->max_channels = 6;
|
|---|
| 3154 | else
|
|---|
| 3155 | s->max_channels = 4;
|
|---|
| 3156 | s->capability |= CAN_AC3_HW;
|
|---|
| 3157 | s->capability |= CAN_DUAL_DAC;
|
|---|
| 3158 | s->capability |= CAN_MULTI_CH_HW;
|
|---|
| 3159 | } else {
|
|---|
| 3160 | ChipVersion = 55; // 4 or 6 channels
|
|---|
| 3161 | s->max_channels = 6;
|
|---|
| 3162 | s->capability |= CAN_AC3_HW;
|
|---|
| 3163 | s->capability |= CAN_DUAL_DAC;
|
|---|
| 3164 | s->capability |= CAN_MULTI_CH_HW;
|
|---|
| 3165 | }
|
|---|
| 3166 | }
|
|---|
| 3167 | // still limited to number of speakers
|
|---|
| 3168 | if (s->max_channels > s->speakers)
|
|---|
| 3169 | s->max_channels = s->speakers;
|
|---|
| 3170 | return ChipVersion;
|
|---|
| 3171 | }
|
|---|
| 3172 |
|
|---|
| 3173 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 3174 | static int mpu_io = CONFIG_SOUND_CMPCI_MPUIO;
|
|---|
| 3175 | #else
|
|---|
| 3176 | static int mpu_io = 0;
|
|---|
| 3177 | #endif
|
|---|
| 3178 | #ifdef CONFIG_SOUND_CMPCI_FM
|
|---|
| 3179 | static int fm_io = CONFIG_SOUND_CMPCI_FMIO;
|
|---|
| 3180 | #else
|
|---|
| 3181 | static int fm_io = 0;
|
|---|
| 3182 | #endif
|
|---|
| 3183 | #ifdef CONFIG_SOUND_CMPCI_SPDIFINVERSE
|
|---|
| 3184 | static int spdif_inverse = 1;
|
|---|
| 3185 | #else
|
|---|
| 3186 | static int spdif_inverse = 0;
|
|---|
| 3187 | #endif
|
|---|
| 3188 | #ifdef CONFIG_SOUND_CMPCI_SPDIFLOOP
|
|---|
| 3189 | static int spdif_loop = 1;
|
|---|
| 3190 | #else
|
|---|
| 3191 | static int spdif_loop = 0;
|
|---|
| 3192 | #endif
|
|---|
| 3193 | #ifdef CONFIG_SOUND_CMPCI_SPEAKERS
|
|---|
| 3194 | static int speakers = CONFIG_SOUND_CMPCI_SPEAKERS;
|
|---|
| 3195 | #else
|
|---|
| 3196 | static int speakers = 2;
|
|---|
| 3197 | #endif
|
|---|
| 3198 | #ifdef CONFIG_SOUND_CMPCI_LINE_REAR
|
|---|
| 3199 | static int use_line_as_rear = 1;
|
|---|
| 3200 | #else
|
|---|
| 3201 | static int use_line_as_rear = 0;
|
|---|
| 3202 | #endif
|
|---|
| 3203 | #ifdef CONFIG_SOUND_CMPCI_LINE_BASS
|
|---|
| 3204 | static int use_line_as_bass = 1;
|
|---|
| 3205 | #else
|
|---|
| 3206 | static int use_line_as_bass = 0;
|
|---|
| 3207 | #endif
|
|---|
| 3208 | #ifdef CONFIG_SOUND_CMPCI_PCTEL
|
|---|
| 3209 | static int modem = 1;
|
|---|
| 3210 | #else
|
|---|
| 3211 | static int modem = 0;
|
|---|
| 3212 | #endif
|
|---|
| 3213 | #ifdef CONFIG_SOUND_CMPCI_JOYSTICK
|
|---|
| 3214 | static int joystick = 1;
|
|---|
| 3215 | #else
|
|---|
| 3216 | static int joystick = 0;
|
|---|
| 3217 | #endif
|
|---|
| 3218 | MODULE_PARM(mpu_io, "i");
|
|---|
| 3219 | MODULE_PARM(fm_io, "i");
|
|---|
| 3220 | MODULE_PARM(spdif_inverse, "i");
|
|---|
| 3221 | MODULE_PARM(spdif_loop, "i");
|
|---|
| 3222 | MODULE_PARM(speakers, "i");
|
|---|
| 3223 | MODULE_PARM(use_line_as_rear, "i");
|
|---|
| 3224 | MODULE_PARM(use_line_as_bass, "i");
|
|---|
| 3225 | MODULE_PARM(modem, "i");
|
|---|
| 3226 | MODULE_PARM(joystick, "i");
|
|---|
| 3227 | MODULE_PARM_DESC(mpu_io, "(0x330, 0x320, 0x310, 0x300) Base of MPU-401, 0 to disable");
|
|---|
| 3228 | MODULE_PARM_DESC(fm_io, "(0x388, 0x3C8, 0x3E0) Base of OPL3, 0 to disable");
|
|---|
| 3229 | MODULE_PARM_DESC(spdif_inverse, "(1/0) Invert S/PDIF-in signal");
|
|---|
| 3230 | MODULE_PARM_DESC(spdif_loop, "(1/0) Route S/PDIF-in to S/PDIF-out directly");
|
|---|
| 3231 | MODULE_PARM_DESC(speakers, "(2-6) Number of speakers you connect");
|
|---|
| 3232 | MODULE_PARM_DESC(use_line_as_rear, "(1/0) Use line-in jack as rear-out");
|
|---|
| 3233 | MODULE_PARM_DESC(use_line_as_bass, "(1/0) Use line-in jack as bass/center");
|
|---|
| 3234 | MODULE_PARM_DESC(modem, "(1/0) Use HSP modem, still need PCTel modem driver");
|
|---|
| 3235 | MODULE_PARM_DESC(joystick, "(1/0) Enable joystick interface, still need joystick driver");
|
|---|
| 3236 |
|
|---|
| 3237 | void initialize_chip(struct pci_dev *pcidev)
|
|---|
| 3238 | {
|
|---|
| 3239 | struct cm_state *s;
|
|---|
| 3240 | mm_segment_t fs;
|
|---|
| 3241 | int i, val;
|
|---|
| 3242 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 3243 | unsigned char reg_mask = 0;
|
|---|
| 3244 | #endif
|
|---|
| 3245 | struct {
|
|---|
| 3246 | unsigned short deviceid;
|
|---|
| 3247 | char *devicename;
|
|---|
| 3248 | } devicetable[] =
|
|---|
| 3249 | {
|
|---|
| 3250 | { PCI_DEVICE_ID_CMEDIA_CM8338A, "CM8338A" },
|
|---|
| 3251 | { PCI_DEVICE_ID_CMEDIA_CM8338B, "CM8338B" },
|
|---|
| 3252 | { PCI_DEVICE_ID_CMEDIA_CM8738, "CM8738" },
|
|---|
| 3253 | { PCI_DEVICE_ID_CMEDIA_CM8738B, "CM8738B" },
|
|---|
| 3254 | };
|
|---|
| 3255 | char *devicename = "unknown";
|
|---|
| 3256 | {
|
|---|
| 3257 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3258 | if (pci_enable_device(pcidev))
|
|---|
| 3259 | return;
|
|---|
| 3260 | #endif
|
|---|
| 3261 | if (pcidev->irq == 0)
|
|---|
| 3262 | return;
|
|---|
| 3263 | if (!(s = kmalloc(sizeof(struct cm_state), GFP_KERNEL))) {
|
|---|
| 3264 | printk(KERN_WARNING "cm: out of memory\n");
|
|---|
| 3265 | return;
|
|---|
| 3266 | }
|
|---|
| 3267 | /* search device name */
|
|---|
| 3268 | for (i = 0; i < sizeof(devicetable) / sizeof(devicetable[0]); i++) {
|
|---|
| 3269 | if (devicetable[i].deviceid == pcidev->device)
|
|---|
| 3270 | {
|
|---|
| 3271 | devicename = devicetable[i].devicename;
|
|---|
| 3272 | break;
|
|---|
| 3273 | }
|
|---|
| 3274 | }
|
|---|
| 3275 | memset(s, 0, sizeof(struct cm_state));
|
|---|
| 3276 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 3277 | init_waitqueue_head(&s->dma_adc.wait);
|
|---|
| 3278 | init_waitqueue_head(&s->dma_dac.wait);
|
|---|
| 3279 | init_waitqueue_head(&s->open_wait);
|
|---|
| 3280 | init_waitqueue_head(&s->midi.iwait);
|
|---|
| 3281 | init_waitqueue_head(&s->midi.owait);
|
|---|
| 3282 | init_MUTEX(&s->open_sem);
|
|---|
| 3283 | #else
|
|---|
| 3284 | init_waitqueue(&s->dma_adc.wait);
|
|---|
| 3285 | init_waitqueue(&s->dma_dac.wait);
|
|---|
| 3286 | init_waitqueue(&s->open_wait);
|
|---|
| 3287 | init_waitqueue(&s->midi.iwait);
|
|---|
| 3288 | init_waitqueue(&s->midi.owait);
|
|---|
| 3289 | s->open_sem = MUTEX;
|
|---|
| 3290 | #endif
|
|---|
| 3291 | spin_lock_init(&s->lock);
|
|---|
| 3292 | s->magic = CM_MAGIC;
|
|---|
| 3293 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3294 | s->iobase = pci_resource_start(pcidev, 0);
|
|---|
| 3295 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
|
|---|
| 3296 | s->iobase = pcidev->resource[0].start;
|
|---|
| 3297 | #else
|
|---|
| 3298 | s->iobase = pcidev->base_address[0] & PCI_BASE_ADDRESS_IO_MASK;
|
|---|
| 3299 | #endif
|
|---|
| 3300 | s->iosynth = fm_io;
|
|---|
| 3301 | s->iomidi = mpu_io;
|
|---|
| 3302 | s->status = 0;
|
|---|
| 3303 | /* range check */
|
|---|
| 3304 | if (speakers < 2)
|
|---|
| 3305 | speakers = 2;
|
|---|
| 3306 | else if (speakers > 6)
|
|---|
| 3307 | speakers = 6;
|
|---|
| 3308 | s->speakers = speakers;
|
|---|
| 3309 | if (s->iobase == 0)
|
|---|
| 3310 | return;
|
|---|
| 3311 | s->irq = pcidev->irq;
|
|---|
| 3312 |
|
|---|
| 3313 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3314 | if (!request_region(s->iobase, CM_EXTENT_CODEC, "cmpci")) {
|
|---|
| 3315 | #else
|
|---|
| 3316 | if (check_region(s->iobase, CM_EXTENT_CODEC)) {
|
|---|
| 3317 | #endif
|
|---|
| 3318 | printk(KERN_ERR "cm: io ports %#x-%#x in use\n", s->iobase, s->iobase+CM_EXTENT_CODEC-1);
|
|---|
| 3319 | goto err_region5;
|
|---|
| 3320 | }
|
|---|
| 3321 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
|
|---|
| 3322 | request_region(s->iobase, CM_EXTENT_CODEC, "cmpci");
|
|---|
| 3323 | #endif
|
|---|
| 3324 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 3325 | /* disable MPU-401 */
|
|---|
| 3326 | maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x04, 0);
|
|---|
| 3327 | if (s->iomidi) {
|
|---|
| 3328 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3329 | if (!request_region(s->iomidi, CM_EXTENT_MIDI, "cmpci Midi")) {
|
|---|
| 3330 | #else
|
|---|
| 3331 | if (check_region(s->iomidi, CM_EXTENT_MIDI)) {
|
|---|
| 3332 | #endif
|
|---|
| 3333 | printk(KERN_ERR "cm: io ports %#x-%#x in use\n", s->iomidi, s->iomidi+CM_EXTENT_MIDI-1);
|
|---|
| 3334 | s->iomidi = 0;
|
|---|
| 3335 | } else {
|
|---|
| 3336 | /* set IO based at 0x330 */
|
|---|
| 3337 | switch (s->iomidi) {
|
|---|
| 3338 | case 0x330:
|
|---|
| 3339 | reg_mask = 0;
|
|---|
| 3340 | break;
|
|---|
| 3341 | case 0x320:
|
|---|
| 3342 | reg_mask = 0x20;
|
|---|
| 3343 | break;
|
|---|
| 3344 | case 0x310:
|
|---|
| 3345 | reg_mask = 0x40;
|
|---|
| 3346 | break;
|
|---|
| 3347 | case 0x300:
|
|---|
| 3348 | reg_mask = 0x60;
|
|---|
| 3349 | break;
|
|---|
| 3350 | default:
|
|---|
| 3351 | s->iomidi = 0;
|
|---|
| 3352 | break;
|
|---|
| 3353 | }
|
|---|
| 3354 | outb((inb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3) & ~0x60) | reg_mask, s->iobase + CODEC_CMI_LEGACY_CTRL + 3);
|
|---|
| 3355 | /* enable MPU-401 */
|
|---|
| 3356 | if (s->iomidi) {
|
|---|
| 3357 | maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x04);
|
|---|
| 3358 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
|
|---|
| 3359 | request_region(s->iomidi, CM_EXTENT_MIDI, "cmpci MPU");
|
|---|
| 3360 | #endif
|
|---|
| 3361 | }
|
|---|
| 3362 | }
|
|---|
| 3363 | }
|
|---|
| 3364 | #endif
|
|---|
| 3365 | #ifdef CONFIG_SOUND_CMPCI_FM
|
|---|
| 3366 | /* disable FM */
|
|---|
| 3367 | maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~8, 0);
|
|---|
| 3368 | if (s->iosynth) {
|
|---|
| 3369 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3370 | if (!request_region(s->iosynth, CM_EXTENT_SYNTH, "cmpci FM")) {
|
|---|
| 3371 | #else
|
|---|
| 3372 | if (check_region(s->iosynth, CM_EXTENT_SYNTH)) {
|
|---|
| 3373 | #endif
|
|---|
| 3374 | printk(KERN_ERR "cm: io ports %#x-%#x in use\n", s->iosynth, s->iosynth+CM_EXTENT_SYNTH-1);
|
|---|
| 3375 | s->iosynth = 0;
|
|---|
| 3376 | } else {
|
|---|
| 3377 | /* set IO based at 0x388 */
|
|---|
| 3378 | switch (s->iosynth) {
|
|---|
| 3379 | case 0x388:
|
|---|
| 3380 | reg_mask = 0;
|
|---|
| 3381 | break;
|
|---|
| 3382 | case 0x3C8:
|
|---|
| 3383 | reg_mask = 0x01;
|
|---|
| 3384 | break;
|
|---|
| 3385 | case 0x3E0:
|
|---|
| 3386 | reg_mask = 0x02;
|
|---|
| 3387 | break;
|
|---|
| 3388 | case 0x3E8:
|
|---|
| 3389 | reg_mask = 0x03;
|
|---|
| 3390 | break;
|
|---|
| 3391 | default:
|
|---|
| 3392 | s->iosynth = 0;
|
|---|
| 3393 | break;
|
|---|
| 3394 | }
|
|---|
| 3395 | maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~0x03, reg_mask);
|
|---|
| 3396 | /* enable FM */
|
|---|
| 3397 | if (s->iosynth) {
|
|---|
| 3398 | maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, 8);
|
|---|
| 3399 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
|
|---|
| 3400 | request_region(s->iosynth, CM_EXTENT_SYNTH, "cmpci FM");
|
|---|
| 3401 | #endif
|
|---|
| 3402 | }
|
|---|
| 3403 | }
|
|---|
| 3404 | }
|
|---|
| 3405 | #endif
|
|---|
| 3406 | /* enable joystick */
|
|---|
| 3407 | if (joystick)
|
|---|
| 3408 | maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x02);
|
|---|
| 3409 | else
|
|---|
| 3410 | maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x02, 0);
|
|---|
| 3411 | /* initialize codec registers */
|
|---|
| 3412 | outb(0, s->iobase + CODEC_CMI_INT_HLDCLR + 2); /* disable ints */
|
|---|
| 3413 | outb(0, s->iobase + CODEC_CMI_FUNCTRL0 + 2); /* disable channels */
|
|---|
| 3414 | /* reset mixer */
|
|---|
| 3415 | wrmixer(s, DSP_MIX_DATARESETIDX, 0);
|
|---|
| 3416 |
|
|---|
| 3417 | /* request irq */
|
|---|
| 3418 | if (request_irq(s->irq, cm_interrupt, SA_SHIRQ, "cmpci", s)) {
|
|---|
| 3419 | printk(KERN_ERR "cm: irq %u in use\n", s->irq);
|
|---|
| 3420 | goto err_irq;
|
|---|
| 3421 | }
|
|---|
| 3422 | printk(KERN_INFO "cm: found %s adapter at io %#06x irq %u\n",
|
|---|
| 3423 | devicename, s->iobase, s->irq);
|
|---|
| 3424 | /* register devices */
|
|---|
| 3425 | if ((s->dev_audio = register_sound_dsp(&cm_audio_fops, -1)) < 0)
|
|---|
| 3426 | goto err_dev1;
|
|---|
| 3427 | if ((s->dev_mixer = register_sound_mixer(&cm_mixer_fops, -1)) < 0)
|
|---|
| 3428 | goto err_dev2;
|
|---|
| 3429 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 3430 | if ((s->dev_midi = register_sound_midi(&cm_midi_fops, -1)) < 0)
|
|---|
| 3431 | goto err_dev3;
|
|---|
| 3432 | #endif
|
|---|
| 3433 | #ifdef CONFIG_SOUND_CMPCI_FM
|
|---|
| 3434 | if ((s->dev_dmfm = register_sound_special(&cm_dmfm_fops, 15 /* ?? */)) < 0)
|
|---|
| 3435 | goto err_dev4;
|
|---|
| 3436 | #endif
|
|---|
| 3437 | pci_set_master(pcidev); /* enable bus mastering */
|
|---|
| 3438 | /* initialize the chips */
|
|---|
| 3439 | fs = get_fs();
|
|---|
| 3440 | set_fs(KERNEL_DS);
|
|---|
| 3441 | /* set mixer output */
|
|---|
| 3442 | frobindir(s, DSP_MIX_OUTMIXIDX, 0x1f, 0x1f);
|
|---|
| 3443 | /* set mixer input */
|
|---|
| 3444 | val = SOUND_MASK_LINE|SOUND_MASK_SYNTH|SOUND_MASK_CD|SOUND_MASK_MIC;
|
|---|
| 3445 | mixer_ioctl(s, SOUND_MIXER_WRITE_RECSRC, (unsigned long)&val);
|
|---|
| 3446 | for (i = 0; i < sizeof(initvol)/sizeof(initvol[0]); i++) {
|
|---|
| 3447 | val = initvol[i].vol;
|
|---|
| 3448 | mixer_ioctl(s, initvol[i].mixch, (unsigned long)&val);
|
|---|
| 3449 | }
|
|---|
| 3450 | /* use channel 0 for record, channel 1 for play */
|
|---|
| 3451 | maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~2, 1);
|
|---|
| 3452 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
|
|---|
| 3453 | set_fs(fs);
|
|---|
| 3454 | #endif
|
|---|
| 3455 | s->deviceid = pcidev->device;
|
|---|
| 3456 | if (pcidev->device == PCI_DEVICE_ID_CMEDIA_CM8738) {
|
|---|
| 3457 | /* chip version and hw capability check */
|
|---|
| 3458 | s->chip_version = query_chip(s);
|
|---|
| 3459 | printk(KERN_INFO "chip version = 0%d\n", s->chip_version);
|
|---|
| 3460 | s->modem = modem;
|
|---|
| 3461 | if (modem) {
|
|---|
| 3462 | /* enable FLINKON and disable FLINKOFF */
|
|---|
| 3463 | maskb(s->iobase + CODEC_CMI_MISC_CTRL, ~0x40, 0x80);
|
|---|
| 3464 | printk(KERN_INFO "cm: modem function supported\n");
|
|---|
| 3465 | }
|
|---|
| 3466 | /* seet SPDIF-in inverse before enable SPDIF loop */
|
|---|
| 3467 | if (spdif_inverse) {
|
|---|
| 3468 | /* turn on spdif-in inverse */
|
|---|
| 3469 | maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0, 1);
|
|---|
| 3470 | printk(KERN_INFO "cm: Inverse SPDIF-in\n");
|
|---|
| 3471 | } else {
|
|---|
| 3472 | /* turn off spdif-ininverse */
|
|---|
| 3473 | maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~1, 0);
|
|---|
| 3474 | }
|
|---|
| 3475 |
|
|---|
| 3476 | /* enable SPDIF loop */
|
|---|
| 3477 | if (spdif_loop) {
|
|---|
| 3478 | s->status |= DO_SPDIF_LOOP;
|
|---|
| 3479 | /* turn on spdif-in to spdif-out */
|
|---|
| 3480 | maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x80);
|
|---|
| 3481 | printk(KERN_INFO "cm: Enable SPDIF loop\n");
|
|---|
| 3482 | } else {
|
|---|
| 3483 | s->status &= ~DO_SPDIF_LOOP;
|
|---|
| 3484 | /* turn off spdif-in to spdif-out */
|
|---|
| 3485 | maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x80, 0);
|
|---|
| 3486 | }
|
|---|
| 3487 | if (use_line_as_rear) {
|
|---|
| 3488 | s->capability |= CAN_LINE_AS_REAR;
|
|---|
| 3489 | s->status |= DO_LINE_AS_REAR;
|
|---|
| 3490 | maskb(s->iobase + CODEC_CMI_MIXER1, ~0, 0x20);
|
|---|
| 3491 | } else
|
|---|
| 3492 | maskb(s->iobase + CODEC_CMI_MIXER1, ~0x20, 0);
|
|---|
| 3493 | if (s->chip_version >= 39) {
|
|---|
| 3494 | if (use_line_as_bass) {
|
|---|
| 3495 | s->capability |= CAN_LINE_AS_BASS;
|
|---|
| 3496 | s->status |= DO_LINE_AS_BASS;
|
|---|
| 3497 | maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~0, 0x60);
|
|---|
| 3498 | } else
|
|---|
| 3499 | maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~0x60, 0);
|
|---|
| 3500 | }
|
|---|
| 3501 | } else {
|
|---|
| 3502 | /* 8338 will fall here */
|
|---|
| 3503 | s->max_channels = 2;
|
|---|
| 3504 | }
|
|---|
| 3505 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
|
|---|
| 3506 | set_fs(fs);
|
|---|
| 3507 | #endif
|
|---|
| 3508 | /* queue it for later freeing */
|
|---|
| 3509 | s->next = devs;
|
|---|
| 3510 | devs = s;
|
|---|
| 3511 | return;
|
|---|
| 3512 |
|
|---|
| 3513 | #ifdef CONFIG_SOUND_CMPCI_FM
|
|---|
| 3514 | unregister_sound_special(s->dev_dmfm);
|
|---|
| 3515 | err_dev4:
|
|---|
| 3516 | #endif
|
|---|
| 3517 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 3518 | unregister_sound_midi(s->dev_midi);
|
|---|
| 3519 | err_dev3:
|
|---|
| 3520 | #endif
|
|---|
| 3521 | unregister_sound_mixer(s->dev_mixer);
|
|---|
| 3522 | err_dev2:
|
|---|
| 3523 | unregister_sound_dsp(s->dev_audio);
|
|---|
| 3524 | err_dev1:
|
|---|
| 3525 | printk(KERN_ERR "cm: cannot register misc device\n");
|
|---|
| 3526 | free_irq(s->irq, s);
|
|---|
| 3527 | err_irq:
|
|---|
| 3528 | #ifdef CONFIG_SOUND_CMPCI_FM
|
|---|
| 3529 | if (s->iosynth) release_region(s->iosynth, CM_EXTENT_SYNTH);
|
|---|
| 3530 | #endif
|
|---|
| 3531 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 3532 | if (s->iomidi) release_region(s->iomidi, CM_EXTENT_MIDI);
|
|---|
| 3533 | #endif
|
|---|
| 3534 | release_region(s->iobase, CM_EXTENT_CODEC);
|
|---|
| 3535 | err_region5:
|
|---|
| 3536 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3537 | kfree(s);
|
|---|
| 3538 | #else
|
|---|
| 3539 | kfree_s(s, sizeof(struct cm_state));
|
|---|
| 3540 | #endif
|
|---|
| 3541 | }
|
|---|
| 3542 | if (!devs) {
|
|---|
| 3543 | if (wavetable_mem)
|
|---|
| 3544 | free_pages(wavetable_mem, 20-PAGE_SHIFT);
|
|---|
| 3545 | return;
|
|---|
| 3546 | }
|
|---|
| 3547 | return;
|
|---|
| 3548 | }
|
|---|
| 3549 |
|
|---|
| 3550 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3551 | static int __init init_cmpci(void)
|
|---|
| 3552 | #else
|
|---|
| 3553 | #ifdef MODULE
|
|---|
| 3554 | int __init init_module(void)
|
|---|
| 3555 | #else
|
|---|
| 3556 | int __init init_cmpci(void)
|
|---|
| 3557 | #endif
|
|---|
| 3558 | #endif
|
|---|
| 3559 | {
|
|---|
| 3560 | struct pci_dev *pcidev = NULL;
|
|---|
| 3561 | int index = 0;
|
|---|
| 3562 |
|
|---|
| 3563 | #ifdef CONFIG_PCI
|
|---|
| 3564 | if (!pci_present()) /* No PCI bus in this machine! */
|
|---|
| 3565 | #endif
|
|---|
| 3566 | return -ENODEV;
|
|---|
| 3567 | printk(KERN_INFO "cm: version $Revision: 5.64 $ time " __TIME__ " " __DATE__ "\n");
|
|---|
| 3568 | #if 0
|
|---|
| 3569 | if (!(wavetable_mem = __get_free_pages(GFP_KERNEL, 20-PAGE_SHIFT)))
|
|---|
| 3570 | printk(KERN_INFO "cm: cannot allocate 1MB of contiguous nonpageable memory for wavetable data\n");
|
|---|
| 3571 | #endif
|
|---|
| 3572 | while (index < NR_DEVICE && (
|
|---|
| 3573 | (pcidev = pci_find_device(PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738, pcidev)))) {
|
|---|
| 3574 | initialize_chip(pcidev);
|
|---|
| 3575 | index++;
|
|---|
| 3576 | }
|
|---|
| 3577 | while (index < NR_DEVICE && (
|
|---|
| 3578 | (pcidev = pci_find_device(PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A, pcidev)))) {
|
|---|
| 3579 | initialize_chip(pcidev);
|
|---|
| 3580 | index++;
|
|---|
| 3581 | }
|
|---|
| 3582 | while (index < NR_DEVICE && (
|
|---|
| 3583 | (pcidev = pci_find_device(PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B, pcidev)))) {
|
|---|
| 3584 | initialize_chip(pcidev);
|
|---|
| 3585 | index++;
|
|---|
| 3586 | }
|
|---|
| 3587 | return 0;
|
|---|
| 3588 | }
|
|---|
| 3589 |
|
|---|
| 3590 | /* --------------------------------------------------------------------- */
|
|---|
| 3591 |
|
|---|
| 3592 | MODULE_AUTHOR("ChenLi Tien, cltien@cmedia.com.tw");
|
|---|
| 3593 | MODULE_DESCRIPTION("CM8x38 Audio Driver");
|
|---|
| 3594 |
|
|---|
| 3595 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3596 | static void __exit cleanup_cmpci(void)
|
|---|
| 3597 | #else
|
|---|
| 3598 | void cleanup_module(void)
|
|---|
| 3599 | #endif
|
|---|
| 3600 | {
|
|---|
| 3601 | struct cm_state *s;
|
|---|
| 3602 |
|
|---|
| 3603 | while ((s = devs)) {
|
|---|
| 3604 | devs = devs->next;
|
|---|
| 3605 | outb(0, s->iobase + CODEC_CMI_INT_HLDCLR + 2); /* disable ints */
|
|---|
| 3606 | synchronize_irq();
|
|---|
| 3607 | outb(0, s->iobase + CODEC_CMI_FUNCTRL0 + 2); /* disable channels */
|
|---|
| 3608 | free_irq(s->irq, s);
|
|---|
| 3609 | #ifdef FIXEDDMA
|
|---|
| 3610 | dealloc_dmabuf(&s->dma_dac);
|
|---|
| 3611 | dealloc_dmabuf(&s->dma_adc);
|
|---|
| 3612 | #endif
|
|---|
| 3613 |
|
|---|
| 3614 | /* reset mixer */
|
|---|
| 3615 | wrmixer(s, DSP_MIX_DATARESETIDX, 0);
|
|---|
| 3616 |
|
|---|
| 3617 | release_region(s->iobase, CM_EXTENT_CODEC);
|
|---|
| 3618 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 3619 | if (s->iomidi) release_region(s->iomidi, CM_EXTENT_MIDI);
|
|---|
| 3620 | #endif
|
|---|
| 3621 | #ifdef CONFIG_SOUND_CMPCI_FM
|
|---|
| 3622 | if (s->iosynth) release_region(s->iosynth, CM_EXTENT_SYNTH);
|
|---|
| 3623 | #endif
|
|---|
| 3624 | unregister_sound_dsp(s->dev_audio);
|
|---|
| 3625 | unregister_sound_mixer(s->dev_mixer);
|
|---|
| 3626 | #ifdef CONFIG_SOUND_CMPCI_MIDI
|
|---|
| 3627 | unregister_sound_midi(s->dev_midi);
|
|---|
| 3628 | #endif
|
|---|
| 3629 | #ifdef CONFIG_SOUND_CMPCI_FM
|
|---|
| 3630 | unregister_sound_special(s->dev_dmfm);
|
|---|
| 3631 | #endif
|
|---|
| 3632 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3633 | kfree(s);
|
|---|
| 3634 | #else
|
|---|
| 3635 | kfree_s(s, sizeof(struct cm_state));
|
|---|
| 3636 | #endif
|
|---|
| 3637 | }
|
|---|
| 3638 | if (wavetable_mem)
|
|---|
| 3639 | free_pages(wavetable_mem, 20-PAGE_SHIFT);
|
|---|
| 3640 | printk(KERN_INFO "cm: unloading\n");
|
|---|
| 3641 | }
|
|---|
| 3642 |
|
|---|
| 3643 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
|
|---|
| 3644 | module_init(init_cmpci);
|
|---|
| 3645 | module_exit(cleanup_cmpci);
|
|---|
| 3646 | #endif
|
|---|