i810_audio.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:91k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Intel i810 and friends ICH driver for Linux
  3.  * Alan Cox <alan@redhat.com>
  4.  *
  5.  *  Built from:
  6.  * Low level code:  Zach Brown (original nonworking i810 OSS driver)
  7.  *  Jaroslav Kysela <perex@suse.cz> (working ALSA driver)
  8.  *
  9.  * Framework: Thomas Sailer <sailer@ife.ee.ethz.ch>
  10.  * Extended by: Zach Brown <zab@redhat.com>  
  11.  * and others..
  12.  *
  13.  *  Hardware Provided By:
  14.  * Analog Devices (A major AC97 codec maker)
  15.  * Intel Corp  (you've probably heard of them already)
  16.  *
  17.  *  AC97 clues and assistance provided by
  18.  * Analog Devices
  19.  * Zach 'Fufu' Brown
  20.  * Jeff Garzik
  21.  *
  22.  * This program is free software; you can redistribute it and/or modify
  23.  * it under the terms of the GNU General Public License as published by
  24.  * the Free Software Foundation; either version 2 of the License, or
  25.  * (at your option) any later version.
  26.  *
  27.  * This program is distributed in the hope that it will be useful,
  28.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.  * GNU General Public License for more details.
  31.  *
  32.  * You should have received a copy of the GNU General Public License
  33.  * along with this program; if not, write to the Free Software
  34.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35.  *
  36.  *
  37.  * Intel 810 theory of operation
  38.  *
  39.  * The chipset provides three DMA channels that talk to an AC97
  40.  * CODEC (AC97 is a digital/analog mixer standard). At its simplest
  41.  * you get 48Khz audio with basic volume and mixer controls. At the
  42.  * best you get rate adaption in the codec. We set the card up so
  43.  * that we never take completion interrupts but instead keep the card
  44.  * chasing its tail around a ring buffer. This is needed for mmap
  45.  * mode audio and happens to work rather well for non-mmap modes too.
  46.  *
  47.  * The board has one output channel for PCM audio (supported) and
  48.  * a stereo line in and mono microphone input. Again these are normally
  49.  * locked to 48Khz only. Right now recording is not finished.
  50.  *
  51.  * There is no midi support, no synth support. Use timidity. To get
  52.  * esd working you need to use esd -r 48000 as it won't probe 48KHz
  53.  * by default. mpg123 can't handle 48Khz only audio so use xmms.
  54.  *
  55.  * Fix The Sound On Dell
  56.  *
  57.  * Not everyone uses 48KHz. We know of no way to detect this reliably
  58.  * and certainly not to get the right data. If your i810 audio sounds
  59.  * stupid you may need to investigate other speeds. According to Analog
  60.  * they tend to use a 14.318MHz clock which gives you a base rate of
  61.  * 41194Hz.
  62.  *
  63.  * This is available via the 'ftsodell=1' option. 
  64.  *
  65.  * If you need to force a specific rate set the clocking= option
  66.  *
  67.  * This driver is cursed. (Ben LaHaise)
  68.  */
  69.  
  70. #include <linux/module.h>
  71. #include <linux/version.h>
  72. #include <linux/string.h>
  73. #include <linux/ctype.h>
  74. #include <linux/ioport.h>
  75. #include <linux/sched.h>
  76. #include <linux/delay.h>
  77. #include <linux/sound.h>
  78. #include <linux/slab.h>
  79. #include <linux/soundcard.h>
  80. #include <linux/pci.h>
  81. #include <asm/io.h>
  82. #include <asm/dma.h>
  83. #include <linux/init.h>
  84. #include <linux/poll.h>
  85. #include <linux/spinlock.h>
  86. #include <linux/smp_lock.h>
  87. #include <linux/ac97_codec.h>
  88. #include <linux/wrapper.h>
  89. #include <asm/uaccess.h>
  90. #include <asm/hardirq.h>
  91. #ifndef PCI_DEVICE_ID_INTEL_82801
  92. #define PCI_DEVICE_ID_INTEL_82801 0x2415
  93. #endif
  94. #ifndef PCI_DEVICE_ID_INTEL_82901
  95. #define PCI_DEVICE_ID_INTEL_82901 0x2425
  96. #endif
  97. #ifndef PCI_DEVICE_ID_INTEL_ICH2
  98. #define PCI_DEVICE_ID_INTEL_ICH2 0x2445
  99. #endif
  100. #ifndef PCI_DEVICE_ID_INTEL_ICH3
  101. #define PCI_DEVICE_ID_INTEL_ICH3 0x2485
  102. #endif
  103. #ifndef PCI_DEVICE_ID_INTEL_ICH4
  104. #define PCI_DEVICE_ID_INTEL_ICH4 0x24c5
  105. #endif
  106. #ifndef PCI_DEVICE_ID_INTEL_440MX
  107. #define PCI_DEVICE_ID_INTEL_440MX 0x7195
  108. #endif
  109. #ifndef PCI_DEVICE_ID_SI_7012
  110. #define PCI_DEVICE_ID_SI_7012 0x7012
  111. #endif
  112. #ifndef PCI_DEVICE_ID_NVIDIA_MCP1_AUDIO
  113. #define PCI_DEVICE_ID_NVIDIA_MCP1_AUDIO 0x01b1
  114. #endif
  115. #ifndef PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO
  116. #define PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO 0x006a
  117. #endif
  118. #ifndef PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO
  119. #define PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO 0x00da
  120. #endif
  121. #ifndef PCI_DEVICE_ID_AMD_768_AUDIO
  122. #define PCI_DEVICE_ID_AMD_768_AUDIO 0x7445
  123. #endif
  124. static int ftsodell=0;
  125. static int strict_clocking=0;
  126. static unsigned int clocking=0;
  127. static int spdif_locked=0;
  128. //#define DEBUG
  129. //#define DEBUG2
  130. //#define DEBUG_INTERRUPTS
  131. //#define DEBUG_MMAP
  132. #define ADC_RUNNING 1
  133. #define DAC_RUNNING 2
  134. #define I810_FMT_16BIT 1
  135. #define I810_FMT_STEREO 2
  136. #define I810_FMT_MASK 3
  137. #define SPDIF_ON 0x0004
  138. #define SURR_ON 0x0010
  139. #define CENTER_LFE_ON 0x0020
  140. #define VOL_MUTED 0x8000
  141. /* the 810's array of pointers to data buffers */
  142. struct sg_item {
  143. #define BUSADDR_MASK 0xFFFFFFFE
  144. u32 busaddr;
  145. #define CON_IOC  0x80000000 /* interrupt on completion */
  146. #define CON_BUFPAD 0x40000000 /* pad underrun with last sample, else 0 */
  147. #define CON_BUFLEN_MASK 0x0000ffff /* buffer length in samples */
  148. u32 control;
  149. };
  150. /* an instance of the i810 channel */
  151. #define SG_LEN 32
  152. struct i810_channel 
  153. {
  154. /* these sg guys should probably be allocated
  155.    seperately as nocache. Must be 8 byte aligned */
  156. struct sg_item sg[SG_LEN]; /* 32*8 */
  157. u32 offset; /* 4 */
  158. u32 port; /* 4 */
  159. u32 used;
  160. u32 num;
  161. };
  162. /*
  163.  * we have 3 seperate dma engines.  pcm in, pcm out, and mic.
  164.  * each dma engine has controlling registers.  These goofy
  165.  * names are from the datasheet, but make it easy to write
  166.  * code while leafing through it.
  167.  */
  168. #define ENUM_ENGINE(PRE,DIG) 
  169. enum {
  170. PRE##_BDBAR = 0x##DIG##0, /* Buffer Descriptor list Base Address */
  171. PRE##_CIV = 0x##DIG##4, /* Current Index Value */
  172. PRE##_LVI = 0x##DIG##5, /* Last Valid Index */
  173. PRE##_SR = 0x##DIG##6, /* Status Register */
  174. PRE##_PICB = 0x##DIG##8, /* Position In Current Buffer */
  175. PRE##_PIV = 0x##DIG##a, /* Prefetched Index Value */
  176. PRE##_CR = 0x##DIG##b /* Control Register */
  177. }
  178. ENUM_ENGINE(OFF,0); /* Offsets */
  179. ENUM_ENGINE(PI,0); /* PCM In */
  180. ENUM_ENGINE(PO,1); /* PCM Out */
  181. ENUM_ENGINE(MC,2); /* Mic In */
  182. enum {
  183. GLOB_CNT = 0x2c, /* Global Control */
  184. GLOB_STA =  0x30, /* Global Status */
  185. CAS  =  0x34 /* Codec Write Semaphore Register */
  186. };
  187. /* interrupts for a dma engine */
  188. #define DMA_INT_FIFO (1<<4)  /* fifo under/over flow */
  189. #define DMA_INT_COMPLETE (1<<3)  /* buffer read/write complete and ioc set */
  190. #define DMA_INT_LVI (1<<2)  /* last valid done */
  191. #define DMA_INT_CELV (1<<1)  /* last valid is current */
  192. #define DMA_INT_DCH (1) /* DMA Controller Halted (happens on LVI interrupts) */
  193. #define DMA_INT_MASK (DMA_INT_FIFO|DMA_INT_COMPLETE|DMA_INT_LVI)
  194. /* interrupts for the whole chip */
  195. #define INT_SEC (1<<11)
  196. #define INT_PRI (1<<10)
  197. #define INT_MC (1<<7)
  198. #define INT_PO (1<<6)
  199. #define INT_PI (1<<5)
  200. #define INT_MO (1<<2)
  201. #define INT_NI (1<<1)
  202. #define INT_GPI (1<<0)
  203. #define INT_MASK (INT_SEC|INT_PRI|INT_MC|INT_PO|INT_PI|INT_MO|INT_NI|INT_GPI)
  204. #define DRIVER_VERSION "0.21"
  205. /* magic numbers to protect our data structures */
  206. #define I810_CARD_MAGIC 0x5072696E /* "Prin" */
  207. #define I810_STATE_MAGIC 0x63657373 /* "cess" */
  208. #define I810_DMA_MASK 0xffffffff /* DMA buffer mask for pci_alloc_consist */
  209. #define NR_HW_CH 3
  210. /* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
  211. #define NR_AC97 2
  212. /* Please note that an 8bit mono stream is not valid on this card, you must have a 16bit */
  213. /* stream at a minimum for this card to be happy */
  214. static const unsigned sample_size[] = { 1, 2, 2, 4 };
  215. /* Samples are 16bit values, so we are shifting to a word, not to a byte, hence shift */
  216. /* values are one less than might be expected */
  217. static const unsigned sample_shift[] = { -1, 0, 0, 1 };
  218. enum {
  219. ICH82801AA = 0,
  220. ICH82901AB,
  221. INTEL440MX,
  222. INTELICH2,
  223. INTELICH3,
  224. INTELICH4,
  225. SI7012,
  226. NVIDIA_NFORCE,
  227. AMD768,
  228. AMD8111
  229. };
  230. static char * card_names[] = {
  231. "Intel ICH 82801AA",
  232. "Intel ICH 82901AB",
  233. "Intel 440MX",
  234. "Intel ICH2",
  235. "Intel ICH3",
  236. "Intel ICH4",
  237. "SiS 7012",
  238. "NVIDIA nForce Audio",
  239. "AMD 768",
  240. "AMD-8111 IOHub"
  241. };
  242. static struct pci_device_id i810_pci_tbl [] __initdata = {
  243. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801,
  244.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, ICH82801AA},
  245. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82901,
  246.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, ICH82901AB},
  247. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_440MX,
  248.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTEL440MX},
  249. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH2,
  250.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH2},
  251. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH3,
  252.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH3},
  253. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH4,
  254.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH4},
  255. {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7012,
  256.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, SI7012},
  257. {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_MCP1_AUDIO,
  258.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
  259. {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO,
  260.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
  261. {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO,
  262.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
  263. {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_768_AUDIO,
  264.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, AMD768},
  265. {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_AC97,
  266.  PCI_ANY_ID, PCI_ANY_ID, 0, 0, AMD8111},
  267. {0,}
  268. };
  269. MODULE_DEVICE_TABLE (pci, i810_pci_tbl);
  270. #ifdef CONFIG_PM
  271. #define PM_SUSPENDED(card) (card->pm_suspended)
  272. #else
  273. #define PM_SUSPENDED(card) (0)
  274. #endif
  275. /* "software" or virtual channel, an instance of opened /dev/dsp */
  276. struct i810_state {
  277. unsigned int magic;
  278. struct i810_card *card; /* Card info */
  279. /* single open lock mechanism, only used for recording */
  280. struct semaphore open_sem;
  281. wait_queue_head_t open_wait;
  282. /* file mode */
  283. mode_t open_mode;
  284. /* virtual channel number */
  285. int virt;
  286. #ifdef CONFIG_PM
  287. unsigned int pm_saved_dac_rate,pm_saved_adc_rate;
  288. #endif
  289. struct dmabuf {
  290. /* wave sample stuff */
  291. unsigned int rate;
  292. unsigned char fmt, enable, trigger;
  293. /* hardware channel */
  294. struct i810_channel *read_channel;
  295. struct i810_channel *write_channel;
  296. /* OSS buffer management stuff */
  297. void *rawbuf;
  298. dma_addr_t dma_handle;
  299. unsigned buforder;
  300. unsigned numfrag;
  301. unsigned fragshift;
  302. /* our buffer acts like a circular ring */
  303. unsigned hwptr; /* where dma last started, updated by update_ptr */
  304. unsigned swptr; /* where driver last clear/filled, updated by read/write */
  305. int count; /* bytes to be consumed or been generated by dma machine */
  306. unsigned total_bytes; /* total bytes dmaed by hardware */
  307. unsigned error; /* number of over/underruns */
  308. wait_queue_head_t wait; /* put process on wait queue when no more space in buffer */
  309. /* redundant, but makes calculations easier */
  310. /* what the hardware uses */
  311. unsigned dmasize;
  312. unsigned fragsize;
  313. unsigned fragsamples;
  314. /* what we tell the user to expect */
  315. unsigned userfrags;
  316. unsigned userfragsize;
  317. /* OSS stuff */
  318. unsigned mapped:1;
  319. unsigned ready:1;
  320. unsigned update_flag;
  321. unsigned ossfragsize;
  322. unsigned ossmaxfrags;
  323. unsigned subdivision;
  324. } dmabuf;
  325. };
  326. struct i810_card {
  327. struct i810_channel channel[3];
  328. unsigned int magic;
  329. /* We keep i810 cards in a linked list */
  330. struct i810_card *next;
  331. /* The i810 has a certain amount of cross channel interaction
  332.    so we use a single per card lock */
  333. spinlock_t lock;
  334. /* PCI device stuff */
  335. struct pci_dev * pci_dev;
  336. u16 pci_id;
  337. #ifdef CONFIG_PM
  338. u16 pm_suspended;
  339. u32 pm_save_state[64/sizeof(u32)];
  340. int pm_saved_mixer_settings[SOUND_MIXER_NRDEVICES][NR_AC97];
  341. #endif
  342. /* soundcore stuff */
  343. int dev_audio;
  344. /* structures for abstraction of hardware facilities, codecs, banks and channels*/
  345. struct ac97_codec *ac97_codec[NR_AC97];
  346. struct i810_state *states[NR_HW_CH];
  347. u16 ac97_features;
  348. u16 ac97_status;
  349. u16 channels;
  350. /* hardware resources */
  351. unsigned long iobase;
  352. unsigned long ac97base;
  353. u32 irq;
  354. /* Function support */
  355. struct i810_channel *(*alloc_pcm_channel)(struct i810_card *);
  356. struct i810_channel *(*alloc_rec_pcm_channel)(struct i810_card *);
  357. struct i810_channel *(*alloc_rec_mic_channel)(struct i810_card *);
  358. void (*free_pcm_channel)(struct i810_card *, int chan);
  359. /* We have a *very* long init time possibly, so use this to block */
  360. /* attempts to open our devices before we are ready (stops oops'es) */
  361. int initializing;
  362. };
  363. static struct i810_card *devs = NULL;
  364. static int i810_open_mixdev(struct inode *inode, struct file *file);
  365. static int i810_ioctl_mixdev(struct inode *inode, struct file *file,
  366.      unsigned int cmd, unsigned long arg);
  367. static u16 i810_ac97_get(struct ac97_codec *dev, u8 reg);
  368. static void i810_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);
  369. static struct i810_channel *i810_alloc_pcm_channel(struct i810_card *card)
  370. {
  371. if(card->channel[1].used==1)
  372. return NULL;
  373. card->channel[1].used=1;
  374. return &card->channel[1];
  375. }
  376. static struct i810_channel *i810_alloc_rec_pcm_channel(struct i810_card *card)
  377. {
  378. if(card->channel[0].used==1)
  379. return NULL;
  380. card->channel[0].used=1;
  381. return &card->channel[0];
  382. }
  383. static struct i810_channel *i810_alloc_rec_mic_channel(struct i810_card *card)
  384. {
  385. if(card->channel[2].used==1)
  386. return NULL;
  387. card->channel[2].used=1;
  388. return &card->channel[2];
  389. }
  390. static void i810_free_pcm_channel(struct i810_card *card, int channel)
  391. {
  392. card->channel[channel].used=0;
  393. }
  394. static int i810_valid_spdif_rate ( struct ac97_codec *codec, int rate )
  395. {
  396. unsigned long id = 0L;
  397. id = (i810_ac97_get(codec, AC97_VENDOR_ID1) << 16);
  398. id |= i810_ac97_get(codec, AC97_VENDOR_ID2) & 0xffff;
  399. #ifdef DEBUG
  400. printk ( "i810_audio: codec = %s, codec_id = 0x%08lxn", codec->name, id);
  401. #endif
  402. switch ( id ) {
  403. case 0x41445361: /* AD1886 */
  404. if (rate == 48000) {
  405. return 1;
  406. }
  407. break;
  408. default: /* all other codecs, until we know otherwiae */
  409. if (rate == 48000 || rate == 44100 || rate == 32000) {
  410. return 1;
  411. }
  412. break;
  413. }
  414. return (0);
  415. }
  416. /* i810_set_spdif_output
  417.  * 
  418.  *  Configure the S/PDIF output transmitter. When we turn on
  419.  *  S/PDIF, we turn off the analog output. This may not be
  420.  *  the right thing to do.
  421.  *
  422.  *  Assumptions:
  423.  *     The DSP sample rate must already be set to a supported
  424.  *     S/PDIF rate (32kHz, 44.1kHz, or 48kHz) or we abort.
  425.  */
  426. static void i810_set_spdif_output(struct i810_state *state, int slots, int rate)
  427. {
  428. int vol;
  429. int aud_reg;
  430. struct ac97_codec *codec = state->card->ac97_codec[0];
  431. if(!(state->card->ac97_features & 4)) {
  432. #ifdef DEBUG
  433. printk(KERN_WARNING "i810_audio: S/PDIF transmitter not available.n");
  434. #endif
  435. state->card->ac97_status &= ~SPDIF_ON;
  436. } else {
  437. if ( slots == -1 ) { /* Turn off S/PDIF */
  438. aud_reg = i810_ac97_get(codec, AC97_EXTENDED_STATUS);
  439. i810_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
  440. /* If the volume wasn't muted before we turned on S/PDIF, unmute it */
  441. if ( !(state->card->ac97_status & VOL_MUTED) ) {
  442. aud_reg = i810_ac97_get(codec, AC97_MASTER_VOL_STEREO);
  443. i810_ac97_set(codec, AC97_MASTER_VOL_STEREO, (aud_reg & ~VOL_MUTED));
  444. }
  445. state->card->ac97_status &= ~(VOL_MUTED | SPDIF_ON);
  446. return;
  447. }
  448. vol = i810_ac97_get(codec, AC97_MASTER_VOL_STEREO);
  449. state->card->ac97_status = vol & VOL_MUTED;
  450. /* Set S/PDIF transmitter sample rate */
  451. aud_reg = i810_ac97_get(codec, AC97_SPDIF_CONTROL);
  452. switch ( rate ) {
  453. case 32000:
  454. aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_32K; 
  455. break;
  456.  case 44100:
  457.   aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_44K; 
  458. break;
  459. case 48000:
  460.   aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_48K; 
  461. break;
  462. default:
  463. #ifdef DEBUG
  464. printk(KERN_WARNING "i810_audio: %d sample rate not supported by S/PDIF.n", rate);
  465. #endif
  466. /* turn off S/PDIF */
  467. aud_reg = i810_ac97_get(codec, AC97_EXTENDED_STATUS);
  468. i810_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
  469. state->card->ac97_status &= ~SPDIF_ON;
  470. return;
  471. }
  472. i810_ac97_set(codec, AC97_SPDIF_CONTROL, aud_reg);
  473. aud_reg = i810_ac97_get(codec, AC97_EXTENDED_STATUS);
  474. aud_reg = (aud_reg & AC97_EA_SLOT_MASK) | slots | AC97_EA_VRA | AC97_EA_SPDIF;
  475. i810_ac97_set(codec, AC97_EXTENDED_STATUS, aud_reg);
  476. state->card->ac97_status |= SPDIF_ON;
  477. /* Check to make sure the configuration is valid */
  478. aud_reg = i810_ac97_get(codec, AC97_EXTENDED_STATUS);
  479. if ( ! (aud_reg & 0x0400) ) {
  480. #ifdef DEBUG
  481. printk(KERN_WARNING "i810_audio: S/PDIF transmitter configuration not valid (0x%04x).n", aud_reg);
  482. #endif
  483. /* turn off S/PDIF */
  484. i810_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
  485. state->card->ac97_status &= ~SPDIF_ON;
  486. return;
  487. }
  488. /* Mute the analog output */
  489. /* Should this only mute the PCM volume??? */
  490. i810_ac97_set(codec, AC97_MASTER_VOL_STEREO, (vol | VOL_MUTED));
  491. }
  492. }
  493. /* i810_set_dac_channels
  494.  *
  495.  *  Configure the codec's multi-channel DACs
  496.  *
  497.  *  The logic is backwards. Setting the bit to 1 turns off the DAC. 
  498.  *
  499.  *  What about the ICH? We currently configure it using the
  500.  *  SNDCTL_DSP_CHANNELS ioctl.  If we're turnning on the DAC, 
  501.  *  does that imply that we want the ICH set to support
  502.  *  these channels?
  503.  *  
  504.  *  TODO:
  505.  *    vailidate that the codec really supports these DACs
  506.  *    before turning them on. 
  507.  */
  508. static void i810_set_dac_channels(struct i810_state *state, int channel)
  509. {
  510. int aud_reg;
  511. struct ac97_codec *codec = state->card->ac97_codec[0];
  512. aud_reg = i810_ac97_get(codec, AC97_EXTENDED_STATUS);
  513. aud_reg |= AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK;
  514. state->card->ac97_status &= ~(SURR_ON | CENTER_LFE_ON);
  515. switch ( channel ) {
  516. case 2: /* always enabled */
  517. break;
  518. case 4:
  519. aud_reg &= ~AC97_EA_PRJ;
  520. state->card->ac97_status |= SURR_ON;
  521. break;
  522. case 6:
  523. aud_reg &= ~(AC97_EA_PRJ | AC97_EA_PRI | AC97_EA_PRK);
  524. state->card->ac97_status |= SURR_ON | CENTER_LFE_ON;
  525. break;
  526. default:
  527. break;
  528. }
  529. i810_ac97_set(codec, AC97_EXTENDED_STATUS, aud_reg);
  530. }
  531. /* set playback sample rate */
  532. static unsigned int i810_set_dac_rate(struct i810_state * state, unsigned int rate)
  533. {
  534. struct dmabuf *dmabuf = &state->dmabuf;
  535. u32 new_rate;
  536. struct ac97_codec *codec=state->card->ac97_codec[0];
  537. if(!(state->card->ac97_features&0x0001))
  538. {
  539. dmabuf->rate = clocking;
  540. #ifdef DEBUG
  541. printk("Asked for %d Hz, but ac97_features says we only do %dHz.  Sorry!n",
  542.        rate,clocking);
  543. #endif        
  544. return clocking;
  545. }
  546. if (rate > 48000)
  547. rate = 48000;
  548. if (rate < 8000)
  549. rate = 8000;
  550. dmabuf->rate = rate;
  551. /*
  552.  * Adjust for misclocked crap
  553.  */
  554. rate = ( rate * clocking)/48000;
  555. if(strict_clocking && rate < 8000) {
  556. rate = 8000;
  557. dmabuf->rate = (rate * 48000)/clocking;
  558. }
  559.         new_rate=ac97_set_dac_rate(codec, rate);
  560. if(new_rate != rate) {
  561. dmabuf->rate = (new_rate * 48000)/clocking;
  562. }
  563. #ifdef DEBUG
  564. printk("i810_audio: called i810_set_dac_rate : asked for %d, got %dn", rate, dmabuf->rate);
  565. #endif
  566. rate = new_rate;
  567. return dmabuf->rate;
  568. }
  569. /* set recording sample rate */
  570. static unsigned int i810_set_adc_rate(struct i810_state * state, unsigned int rate)
  571. {
  572. struct dmabuf *dmabuf = &state->dmabuf;
  573. u32 new_rate;
  574. struct ac97_codec *codec=state->card->ac97_codec[0];
  575. if(!(state->card->ac97_features&0x0001))
  576. {
  577. dmabuf->rate = clocking;
  578. return clocking;
  579. }
  580. if (rate > 48000)
  581. rate = 48000;
  582. if (rate < 8000)
  583. rate = 8000;
  584. dmabuf->rate = rate;
  585. /*
  586.  * Adjust for misclocked crap
  587.  */
  588.  
  589. rate = ( rate * clocking)/48000;
  590. if(strict_clocking && rate < 8000) {
  591. rate = 8000;
  592. dmabuf->rate = (rate * 48000)/clocking;
  593. }
  594. new_rate = ac97_set_adc_rate(codec, rate);
  595. if(new_rate != rate) {
  596. dmabuf->rate = (new_rate * 48000)/clocking;
  597. rate = new_rate;
  598. }
  599. #ifdef DEBUG
  600. printk("i810_audio: called i810_set_adc_rate : rate = %d/%dn", dmabuf->rate, rate);
  601. #endif
  602. return dmabuf->rate;
  603. }
  604. /* get current playback/recording dma buffer pointer (byte offset from LBA),
  605.    called with spinlock held! */
  606.    
  607. static inline unsigned i810_get_dma_addr(struct i810_state *state, int rec)
  608. {
  609. struct dmabuf *dmabuf = &state->dmabuf;
  610. unsigned int civ, offset, port, port_picb, bytes = 2;
  611. if (!dmabuf->enable)
  612. return 0;
  613. if (rec)
  614. port = state->card->iobase + dmabuf->read_channel->port;
  615. else
  616. port = state->card->iobase + dmabuf->write_channel->port;
  617. if(state->card->pci_id == PCI_DEVICE_ID_SI_7012) {
  618. port_picb = port + OFF_SR;
  619. bytes = 1;
  620. } else
  621. port_picb = port + OFF_PICB;
  622. do {
  623. civ = inb(port+OFF_CIV) & 31;
  624. offset = inw(port_picb);
  625. /* Must have a delay here! */ 
  626. if(offset == 0)
  627. udelay(1);
  628. /* Reread both registers and make sure that that total
  629.  * offset from the first reading to the second is 0.
  630.  * There is an issue with SiS hardware where it will count
  631.  * picb down to 0, then update civ to the next value,
  632.  * then set the new picb to fragsize bytes.  We can catch
  633.  * it between the civ update and the picb update, making
  634.  * it look as though we are 1 fragsize ahead of where we
  635.  * are.  The next to we get the address though, it will
  636.  * be back in the right place, and we will suddenly think
  637.  * we just went forward dmasize - fragsize bytes, causing
  638.  * totally stupid *huge* dma overrun messages.  We are
  639.  * assuming that the 1us delay is more than long enough
  640.  * that we won't have to worry about the chip still being
  641.  * out of sync with reality ;-)
  642.  */
  643. } while (civ != (inb(port+OFF_CIV) & 31) || offset != inw(port_picb));
  644.  
  645. return (((civ + 1) * dmabuf->fragsize - (bytes * offset))
  646. % dmabuf->dmasize);
  647. }
  648. /* Stop recording (lock held) */
  649. static inline void __stop_adc(struct i810_state *state)
  650. {
  651. struct dmabuf *dmabuf = &state->dmabuf;
  652. struct i810_card *card = state->card;
  653. dmabuf->enable &= ~ADC_RUNNING;
  654. outb(0, card->iobase + PI_CR);
  655. // wait for the card to acknowledge shutdown
  656. while( inb(card->iobase + PI_CR) != 0 ) ;
  657. // now clear any latent interrupt bits (like the halt bit)
  658. if(card->pci_id == PCI_DEVICE_ID_SI_7012)
  659. outb( inb(card->iobase + PI_PICB), card->iobase + PI_PICB );
  660. else
  661. outb( inb(card->iobase + PI_SR), card->iobase + PI_SR );
  662. outl( inl(card->iobase + GLOB_STA) & INT_PI, card->iobase + GLOB_STA);
  663. }
  664. static void stop_adc(struct i810_state *state)
  665. {
  666. struct i810_card *card = state->card;
  667. unsigned long flags;
  668. spin_lock_irqsave(&card->lock, flags);
  669. __stop_adc(state);
  670. spin_unlock_irqrestore(&card->lock, flags);
  671. }
  672. static inline void __start_adc(struct i810_state *state)
  673. {
  674. struct dmabuf *dmabuf = &state->dmabuf;
  675. if (dmabuf->count < dmabuf->dmasize && dmabuf->ready && !dmabuf->enable &&
  676.     (dmabuf->trigger & PCM_ENABLE_INPUT)) {
  677. dmabuf->enable |= ADC_RUNNING;
  678. outb((1<<4) | (1<<2) | 1, state->card->iobase + PI_CR);
  679. }
  680. }
  681. static void start_adc(struct i810_state *state)
  682. {
  683. struct i810_card *card = state->card;
  684. unsigned long flags;
  685. spin_lock_irqsave(&card->lock, flags);
  686. __start_adc(state);
  687. spin_unlock_irqrestore(&card->lock, flags);
  688. }
  689. /* stop playback (lock held) */
  690. static inline void __stop_dac(struct i810_state *state)
  691. {
  692. struct dmabuf *dmabuf = &state->dmabuf;
  693. struct i810_card *card = state->card;
  694. dmabuf->enable &= ~DAC_RUNNING;
  695. outb(0, card->iobase + PO_CR);
  696. // wait for the card to acknowledge shutdown
  697. while( inb(card->iobase + PO_CR) != 0 ) ;
  698. // now clear any latent interrupt bits (like the halt bit)
  699. if(card->pci_id == PCI_DEVICE_ID_SI_7012)
  700. outb( inb(card->iobase + PO_PICB), card->iobase + PO_PICB );
  701. else
  702. outb( inb(card->iobase + PO_SR), card->iobase + PO_SR );
  703. outl( inl(card->iobase + GLOB_STA) & INT_PO, card->iobase + GLOB_STA);
  704. }
  705. static void stop_dac(struct i810_state *state)
  706. {
  707. struct i810_card *card = state->card;
  708. unsigned long flags;
  709. spin_lock_irqsave(&card->lock, flags);
  710. __stop_dac(state);
  711. spin_unlock_irqrestore(&card->lock, flags);
  712. }
  713. static inline void __start_dac(struct i810_state *state)
  714. {
  715. struct dmabuf *dmabuf = &state->dmabuf;
  716. if (dmabuf->count > 0 && dmabuf->ready && !dmabuf->enable &&
  717.     (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
  718. dmabuf->enable |= DAC_RUNNING;
  719. outb((1<<4) | (1<<2) | 1, state->card->iobase + PO_CR);
  720. }
  721. }
  722. static void start_dac(struct i810_state *state)
  723. {
  724. struct i810_card *card = state->card;
  725. unsigned long flags;
  726. spin_lock_irqsave(&card->lock, flags);
  727. __start_dac(state);
  728. spin_unlock_irqrestore(&card->lock, flags);
  729. }
  730. #define DMABUF_DEFAULTORDER (16-PAGE_SHIFT)
  731. #define DMABUF_MINORDER 1
  732. /* allocate DMA buffer, playback and recording buffer should be allocated seperately */
  733. static int alloc_dmabuf(struct i810_state *state)
  734. {
  735. struct dmabuf *dmabuf = &state->dmabuf;
  736. void *rawbuf= NULL;
  737. int order, size;
  738. struct page *page, *pend;
  739. /* If we don't have any oss frag params, then use our default ones */
  740. if(dmabuf->ossmaxfrags == 0)
  741. dmabuf->ossmaxfrags = 4;
  742. if(dmabuf->ossfragsize == 0)
  743. dmabuf->ossfragsize = (PAGE_SIZE<<DMABUF_DEFAULTORDER)/dmabuf->ossmaxfrags;
  744. size = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
  745. if(dmabuf->rawbuf && (PAGE_SIZE << dmabuf->buforder) == size)
  746. return 0;
  747. /* alloc enough to satisfy the oss params */
  748. for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--) {
  749. if ( (PAGE_SIZE<<order) > size )
  750. continue;
  751. if ((rawbuf = pci_alloc_consistent(state->card->pci_dev,
  752.    PAGE_SIZE << order,
  753.    &dmabuf->dma_handle)))
  754. break;
  755. }
  756. if (!rawbuf)
  757. return -ENOMEM;
  758. #ifdef DEBUG
  759. printk("i810_audio: allocated %ld (order = %d) bytes at %pn",
  760.        PAGE_SIZE << order, order, rawbuf);
  761. #endif
  762. dmabuf->ready  = dmabuf->mapped = 0;
  763. dmabuf->rawbuf = rawbuf;
  764. dmabuf->buforder = order;
  765. /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
  766. pend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
  767. for (page = virt_to_page(rawbuf); page <= pend; page++)
  768. mem_map_reserve(page);
  769. return 0;
  770. }
  771. /* free DMA buffer */
  772. static void dealloc_dmabuf(struct i810_state *state)
  773. {
  774. struct dmabuf *dmabuf = &state->dmabuf;
  775. struct page *page, *pend;
  776. if (dmabuf->rawbuf) {
  777. /* undo marking the pages as reserved */
  778. pend = virt_to_page(dmabuf->rawbuf + (PAGE_SIZE << dmabuf->buforder) - 1);
  779. for (page = virt_to_page(dmabuf->rawbuf); page <= pend; page++)
  780. mem_map_unreserve(page);
  781. pci_free_consistent(state->card->pci_dev, PAGE_SIZE << dmabuf->buforder,
  782.     dmabuf->rawbuf, dmabuf->dma_handle);
  783. }
  784. dmabuf->rawbuf = NULL;
  785. dmabuf->mapped = dmabuf->ready = 0;
  786. }
  787. static int prog_dmabuf(struct i810_state *state, unsigned rec)
  788. {
  789. struct dmabuf *dmabuf = &state->dmabuf;
  790. struct i810_channel *c;
  791. struct sg_item *sg;
  792. unsigned long flags;
  793. int ret;
  794. unsigned fragint;
  795. int i;
  796. spin_lock_irqsave(&state->card->lock, flags);
  797. if(dmabuf->enable & DAC_RUNNING)
  798. __stop_dac(state);
  799. if(dmabuf->enable & ADC_RUNNING)
  800. __stop_adc(state);
  801. dmabuf->total_bytes = 0;
  802. dmabuf->count = dmabuf->error = 0;
  803. dmabuf->swptr = dmabuf->hwptr = 0;
  804. spin_unlock_irqrestore(&state->card->lock, flags);
  805. /* allocate DMA buffer, let alloc_dmabuf determine if we are already
  806.  * allocated well enough or if we should replace the current buffer
  807.  * (assuming one is already allocated, if it isn't, then allocate it).
  808.  */
  809. if ((ret = alloc_dmabuf(state)))
  810. return ret;
  811. /* FIXME: figure out all this OSS fragment stuff */
  812. /* I did, it now does what it should according to the OSS API.  DL */
  813. /* We may not have realloced our dmabuf, but the fragment size to
  814.  * fragment number ratio may have changed, so go ahead and reprogram
  815.  * things
  816.  */
  817. dmabuf->dmasize = PAGE_SIZE << dmabuf->buforder;
  818. dmabuf->numfrag = SG_LEN;
  819. dmabuf->fragsize = dmabuf->dmasize/dmabuf->numfrag;
  820. dmabuf->fragsamples = dmabuf->fragsize >> 1;
  821. dmabuf->userfragsize = dmabuf->ossfragsize;
  822. dmabuf->userfrags = dmabuf->dmasize/dmabuf->ossfragsize;
  823. memset(dmabuf->rawbuf, 0, dmabuf->dmasize);
  824. if(dmabuf->ossmaxfrags == 4) {
  825. fragint = 8;
  826. dmabuf->fragshift = 2;
  827. } else if (dmabuf->ossmaxfrags == 8) {
  828. fragint = 4;
  829. dmabuf->fragshift = 3;
  830. } else if (dmabuf->ossmaxfrags == 16) {
  831. fragint = 2;
  832. dmabuf->fragshift = 4;
  833. } else {
  834. fragint = 1;
  835. dmabuf->fragshift = 5;
  836. }
  837. /*
  838.  * Now set up the ring 
  839.  */
  840. if(dmabuf->read_channel)
  841. c = dmabuf->read_channel;
  842. else
  843. c = dmabuf->write_channel;
  844. while(c != NULL) {
  845. sg=&c->sg[0];
  846. /*
  847.  * Load up 32 sg entries and take an interrupt at half
  848.  * way (we might want more interrupts later..) 
  849.  */
  850.   
  851. for(i=0;i<dmabuf->numfrag;i++)
  852. {
  853. sg->busaddr=virt_to_bus(dmabuf->rawbuf+dmabuf->fragsize*i);
  854. // the card will always be doing 16bit stereo
  855. sg->control=dmabuf->fragsamples;
  856. if(state->card->pci_id == PCI_DEVICE_ID_SI_7012)
  857. sg->control <<= 1;
  858. sg->control|=CON_BUFPAD;
  859. // set us up to get IOC interrupts as often as needed to
  860. // satisfy numfrag requirements, no more
  861. if( ((i+1) % fragint) == 0) {
  862. sg->control|=CON_IOC;
  863. }
  864. sg++;
  865. }
  866. spin_lock_irqsave(&state->card->lock, flags);
  867. outb(2, state->card->iobase+c->port+OFF_CR);   /* reset DMA machine */
  868. outl(virt_to_bus(&c->sg[0]), state->card->iobase+c->port+OFF_BDBAR);
  869. outb(0, state->card->iobase+c->port+OFF_CIV);
  870. outb(0, state->card->iobase+c->port+OFF_LVI);
  871. spin_unlock_irqrestore(&state->card->lock, flags);
  872. if(c != dmabuf->write_channel)
  873. c = dmabuf->write_channel;
  874. else
  875. c = NULL;
  876. }
  877. /* set the ready flag for the dma buffer */
  878. dmabuf->ready = 1;
  879. #ifdef DEBUG
  880. printk("i810_audio: prog_dmabuf, sample rate = %d, format = %d,ntnumfrag = %d, "
  881.        "fragsize = %d dmasize = %dn",
  882.        dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
  883.        dmabuf->fragsize, dmabuf->dmasize);
  884. #endif
  885. return 0;
  886. }
  887. static void __i810_update_lvi(struct i810_state *state, int rec)
  888. {
  889. struct dmabuf *dmabuf = &state->dmabuf;
  890. int x, port;
  891. port = state->card->iobase;
  892. if(rec)
  893. port += dmabuf->read_channel->port;
  894. else
  895. port += dmabuf->write_channel->port;
  896. /* if we are currently stopped, then our CIV is actually set to our
  897.  * *last* sg segment and we are ready to wrap to the next.  However,
  898.  * if we set our LVI to the last sg segment, then it won't wrap to
  899.  * the next sg segment, it won't even get a start.  So, instead, when
  900.  * we are stopped, we set both the LVI value and also we increment
  901.  * the CIV value to the next sg segment to be played so that when
  902.  * we call start_{dac,adc}, things will operate properly
  903.  */
  904. if (!dmabuf->enable && dmabuf->ready) {
  905. if(rec && dmabuf->count < dmabuf->dmasize &&
  906.    (dmabuf->trigger & PCM_ENABLE_INPUT))
  907. {
  908. outb((inb(port+OFF_CIV)+1)&31, port+OFF_LVI);
  909. __start_adc(state);
  910. while( !(inb(port + OFF_CR) & ((1<<4) | (1<<2))) ) ;
  911. } else if (!rec && dmabuf->count &&
  912.    (dmabuf->trigger & PCM_ENABLE_OUTPUT))
  913. {
  914. outb((inb(port+OFF_CIV)+1)&31, port+OFF_LVI);
  915. __start_dac(state);
  916. while( !(inb(port + OFF_CR) & ((1<<4) | (1<<2))) ) ;
  917. }
  918. }
  919. /* swptr - 1 is the tail of our transfer */
  920. x = (dmabuf->dmasize + dmabuf->swptr - 1) % dmabuf->dmasize;
  921. x /= dmabuf->fragsize;
  922. outb(x, port+OFF_LVI);
  923. }
  924. static void i810_update_lvi(struct i810_state *state, int rec)
  925. {
  926. struct dmabuf *dmabuf = &state->dmabuf;
  927. unsigned long flags;
  928. if(!dmabuf->ready)
  929. return;
  930. spin_lock_irqsave(&state->card->lock, flags);
  931. __i810_update_lvi(state, rec);
  932. spin_unlock_irqrestore(&state->card->lock, flags);
  933. }
  934. /* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
  935. static void i810_update_ptr(struct i810_state *state)
  936. {
  937. struct dmabuf *dmabuf = &state->dmabuf;
  938. unsigned hwptr;
  939. int diff;
  940. /* error handling and process wake up for DAC */
  941. if (dmabuf->enable == ADC_RUNNING) {
  942. /* update hardware pointer */
  943. hwptr = i810_get_dma_addr(state, 1);
  944. diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
  945. #if defined(DEBUG_INTERRUPTS) || defined(DEBUG_MMAP)
  946. printk("ADC HWP %d,%d,%dn", hwptr, dmabuf->hwptr, diff);
  947. #endif
  948. dmabuf->hwptr = hwptr;
  949. dmabuf->total_bytes += diff;
  950. dmabuf->count += diff;
  951. if (dmabuf->count > dmabuf->dmasize) {
  952. /* buffer underrun or buffer overrun */
  953. /* this is normal for the end of a read */
  954. /* only give an error if we went past the */
  955. /* last valid sg entry */
  956. if((inb(state->card->iobase + PI_CIV) & 31) !=
  957.    (inb(state->card->iobase + PI_LVI) & 31)) {
  958. printk(KERN_WARNING "i810_audio: DMA overrun on readn");
  959. dmabuf->error++;
  960. }
  961. }
  962. if (dmabuf->count > dmabuf->userfragsize)
  963. wake_up(&dmabuf->wait);
  964. }
  965. /* error handling and process wake up for DAC */
  966. if (dmabuf->enable == DAC_RUNNING) {
  967. /* update hardware pointer */
  968. hwptr = i810_get_dma_addr(state, 0);
  969. diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
  970. #if defined(DEBUG_INTERRUPTS) || defined(DEBUG_MMAP)
  971. printk("DAC HWP %d,%d,%dn", hwptr, dmabuf->hwptr, diff);
  972. #endif
  973. dmabuf->hwptr = hwptr;
  974. dmabuf->total_bytes += diff;
  975. dmabuf->count -= diff;
  976. if (dmabuf->count < 0) {
  977. /* buffer underrun or buffer overrun */
  978. /* this is normal for the end of a write */
  979. /* only give an error if we went past the */
  980. /* last valid sg entry */
  981. if((inb(state->card->iobase + PO_CIV) & 31) !=
  982.    (inb(state->card->iobase + PO_LVI) & 31)) {
  983. printk(KERN_WARNING "i810_audio: DMA overrun on writen");
  984. printk("i810_audio: CIV %d, LVI %d, hwptr %x, "
  985. "count %dn",
  986. inb(state->card->iobase + PO_CIV) & 31,
  987. inb(state->card->iobase + PO_LVI) & 31,
  988. dmabuf->hwptr, dmabuf->count);
  989. dmabuf->error++;
  990. }
  991. }
  992. if (dmabuf->count < (dmabuf->dmasize-dmabuf->userfragsize))
  993. wake_up(&dmabuf->wait);
  994. }
  995. }
  996. static inline int i810_get_free_write_space(struct i810_state *state)
  997. {
  998. struct dmabuf *dmabuf = &state->dmabuf;
  999. int free;
  1000. i810_update_ptr(state);
  1001. // catch underruns during playback
  1002. if (dmabuf->count < 0) {
  1003. dmabuf->count = 0;
  1004. dmabuf->swptr = dmabuf->hwptr;
  1005. }
  1006. free = dmabuf->dmasize - dmabuf->count;
  1007. free -= (dmabuf->hwptr % dmabuf->fragsize);
  1008. if(free < 0)
  1009. return(0);
  1010. return(free);
  1011. }
  1012. static inline int i810_get_available_read_data(struct i810_state *state)
  1013. {
  1014. struct dmabuf *dmabuf = &state->dmabuf;
  1015. int avail;
  1016. i810_update_ptr(state);
  1017. // catch overruns during record
  1018. if (dmabuf->count > dmabuf->dmasize) {
  1019. dmabuf->count = dmabuf->dmasize;
  1020. dmabuf->swptr = dmabuf->hwptr;
  1021. }
  1022. avail = dmabuf->count;
  1023. avail -= (dmabuf->hwptr % dmabuf->fragsize);
  1024. if(avail < 0)
  1025. return(0);
  1026. return(avail);
  1027. }
  1028. static int drain_dac(struct i810_state *state, int signals_allowed)
  1029. {
  1030. DECLARE_WAITQUEUE(wait, current);
  1031. struct dmabuf *dmabuf = &state->dmabuf;
  1032. unsigned long flags;
  1033. unsigned long tmo;
  1034. int count;
  1035. if (!dmabuf->ready)
  1036. return 0;
  1037. if(dmabuf->mapped) {
  1038. stop_dac(state);
  1039. return 0;
  1040. }
  1041. add_wait_queue(&dmabuf->wait, &wait);
  1042. for (;;) {
  1043. spin_lock_irqsave(&state->card->lock, flags);
  1044. i810_update_ptr(state);
  1045. count = dmabuf->count;
  1046. spin_unlock_irqrestore(&state->card->lock, flags);
  1047. if (count <= 0)
  1048. break;
  1049. /* 
  1050.  * This will make sure that our LVI is correct, that our
  1051.  * pointer is updated, and that the DAC is running.  We
  1052.  * have to force the setting of dmabuf->trigger to avoid
  1053.  * any possible deadlocks.
  1054.  */
  1055. if(!dmabuf->enable) {
  1056. dmabuf->trigger = PCM_ENABLE_OUTPUT;
  1057. i810_update_lvi(state,0);
  1058. }
  1059.                 if (signal_pending(current) && signals_allowed) {
  1060.                         break;
  1061.                 }
  1062. /* It seems that we have to set the current state to
  1063.  * TASK_INTERRUPTIBLE every time to make the process
  1064.  * really go to sleep.  This also has to be *after* the
  1065.  * update_ptr() call because update_ptr is likely to
  1066.  * do a wake_up() which will unset this before we ever
  1067.  * try to sleep, resuling in a tight loop in this code
  1068.  * instead of actually sleeping and waiting for an
  1069.  * interrupt to wake us up!
  1070.  */
  1071. set_current_state(TASK_INTERRUPTIBLE);
  1072. /*
  1073.  * set the timeout to significantly longer than it *should*
  1074.  * take for the DAC to drain the DMA buffer
  1075.  */
  1076. tmo = (count * HZ) / (dmabuf->rate);
  1077. if (!schedule_timeout(tmo >= 2 ? tmo : 2)){
  1078. printk(KERN_ERR "i810_audio: drain_dac, dma timeout?n");
  1079. count = 0;
  1080. break;
  1081. }
  1082. }
  1083. set_current_state(TASK_RUNNING);
  1084. remove_wait_queue(&dmabuf->wait, &wait);
  1085. if(count > 0 && signal_pending(current) && signals_allowed)
  1086. return -ERESTARTSYS;
  1087. stop_dac(state);
  1088. return 0;
  1089. }
  1090. static void i810_channel_interrupt(struct i810_card *card)
  1091. {
  1092. int i, count;
  1093. #ifdef DEBUG_INTERRUPTS
  1094. printk("CHANNEL ");
  1095. #endif
  1096. for(i=0;i<NR_HW_CH;i++)
  1097. {
  1098. struct i810_state *state = card->states[i];
  1099. struct i810_channel *c;
  1100. struct dmabuf *dmabuf;
  1101. unsigned long port = card->iobase;
  1102. u16 status;
  1103. if(!state)
  1104. continue;
  1105. if(!state->dmabuf.ready)
  1106. continue;
  1107. dmabuf = &state->dmabuf;
  1108. if(dmabuf->enable & DAC_RUNNING) {
  1109. c=dmabuf->write_channel;
  1110. } else if(dmabuf->enable & ADC_RUNNING) {
  1111. c=dmabuf->read_channel;
  1112. } else /* This can occur going from R/W to close */
  1113. continue;
  1114. port+=c->port;
  1115. if(card->pci_id == PCI_DEVICE_ID_SI_7012)
  1116. status = inw(port + OFF_PICB);
  1117. else
  1118. status = inw(port + OFF_SR);
  1119. #ifdef DEBUG_INTERRUPTS
  1120. printk("NUM %d PORT %X IRQ ( ST%d ", c->num, c->port, status);
  1121. #endif
  1122. if(status & DMA_INT_COMPLETE)
  1123. {
  1124. /* only wake_up() waiters if this interrupt signals
  1125.  * us being beyond a userfragsize of data open or
  1126.  * available, and i810_update_ptr() does that for
  1127.  * us
  1128.  */
  1129. i810_update_ptr(state);
  1130. #ifdef DEBUG_INTERRUPTS
  1131. printk("COMP %d ", dmabuf->hwptr /
  1132. dmabuf->fragsize);
  1133. #endif
  1134. }
  1135. if(status & (DMA_INT_LVI | DMA_INT_DCH))
  1136. {
  1137. /* wake_up() unconditionally on LVI and DCH */
  1138. i810_update_ptr(state);
  1139. wake_up(&dmabuf->wait);
  1140. #ifdef DEBUG_INTERRUPTS
  1141. if(status & DMA_INT_LVI)
  1142. printk("LVI ");
  1143. if(status & DMA_INT_DCH)
  1144. printk("DCH -");
  1145. #endif
  1146. if(dmabuf->enable & DAC_RUNNING)
  1147. count = dmabuf->count;
  1148. else
  1149. count = dmabuf->dmasize - dmabuf->count;
  1150. if(count > 0) {
  1151. outb(inb(port+OFF_CR) | 1, port+OFF_CR);
  1152. #ifdef DEBUG_INTERRUPTS
  1153. printk(" CONTINUE ");
  1154. #endif
  1155. } else {
  1156. if (dmabuf->enable & DAC_RUNNING)
  1157. __stop_dac(state);
  1158. if (dmabuf->enable & ADC_RUNNING)
  1159. __stop_adc(state);
  1160. dmabuf->enable = 0;
  1161. wake_up(&dmabuf->wait);
  1162. #ifdef DEBUG_INTERRUPTS
  1163. printk(" STOP ");
  1164. #endif
  1165. }
  1166. }
  1167. if(card->pci_id == PCI_DEVICE_ID_SI_7012)
  1168. outw(status & DMA_INT_MASK, port + OFF_PICB);
  1169. else
  1170. outw(status & DMA_INT_MASK, port + OFF_SR);
  1171. }
  1172. #ifdef DEBUG_INTERRUPTS
  1173. printk(")n");
  1174. #endif
  1175. }
  1176. static void i810_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1177. {
  1178. struct i810_card *card = (struct i810_card *)dev_id;
  1179. u32 status;
  1180. spin_lock(&card->lock);
  1181. status = inl(card->iobase + GLOB_STA);
  1182. if(!(status & INT_MASK)) 
  1183. {
  1184. spin_unlock(&card->lock);
  1185. return;  /* not for us */
  1186. }
  1187. if(status & (INT_PO|INT_PI|INT_MC))
  1188. i810_channel_interrupt(card);
  1189.   /* clear 'em */
  1190. outl(status & INT_MASK, card->iobase + GLOB_STA);
  1191. spin_unlock(&card->lock);
  1192. }
  1193. /* in this loop, dmabuf.count signifies the amount of data that is
  1194.    waiting to be copied to the user's buffer.  It is filled by the dma
  1195.    machine and drained by this loop. */
  1196. static ssize_t i810_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
  1197. {
  1198. struct i810_state *state = (struct i810_state *)file->private_data;
  1199. struct i810_card *card=state ? state->card : 0;
  1200. struct dmabuf *dmabuf = &state->dmabuf;
  1201. ssize_t ret;
  1202. unsigned long flags;
  1203. unsigned int swptr;
  1204. int cnt;
  1205.         DECLARE_WAITQUEUE(waita, current);
  1206. #ifdef DEBUG2
  1207. printk("i810_audio: i810_read called, count = %dn", count);
  1208. #endif
  1209. if (ppos != &file->f_pos)
  1210. return -ESPIPE;
  1211. if (dmabuf->mapped)
  1212. return -ENXIO;
  1213. if (dmabuf->enable & DAC_RUNNING)
  1214. return -ENODEV;
  1215. if (!dmabuf->read_channel) {
  1216. dmabuf->ready = 0;
  1217. dmabuf->read_channel = card->alloc_rec_pcm_channel(card);
  1218. if (!dmabuf->read_channel) {
  1219. return -EBUSY;
  1220. }
  1221. }
  1222. if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
  1223. return ret;
  1224. if (!access_ok(VERIFY_WRITE, buffer, count))
  1225. return -EFAULT;
  1226. ret = 0;
  1227.         add_wait_queue(&dmabuf->wait, &waita);
  1228. while (count > 0) {
  1229. set_current_state(TASK_INTERRUPTIBLE);
  1230. spin_lock_irqsave(&card->lock, flags);
  1231.                 if (PM_SUSPENDED(card)) {
  1232.                         spin_unlock_irqrestore(&card->lock, flags);
  1233.                         schedule();
  1234.                         if (signal_pending(current)) {
  1235.                                 if (!ret) ret = -EAGAIN;
  1236.                                 break;
  1237.                         }
  1238.                         continue;
  1239.                 }
  1240. swptr = dmabuf->swptr;
  1241. cnt = i810_get_available_read_data(state);
  1242. // this is to make the copy_to_user simpler below
  1243. if(cnt > (dmabuf->dmasize - swptr))
  1244. cnt = dmabuf->dmasize - swptr;
  1245. spin_unlock_irqrestore(&card->lock, flags);
  1246. if (cnt > count)
  1247. cnt = count;
  1248. /* Lop off the last two bits to force the code to always
  1249.  * write in full samples.  This keeps software that sets
  1250.  * O_NONBLOCK but doesn't check the return value of the
  1251.  * write call from getting things out of state where they
  1252.  * think a full 4 byte sample was written when really only
  1253.  * a portion was, resulting in odd sound and stereo
  1254.  * hysteresis.
  1255.  */
  1256. cnt &= ~0x3;
  1257. if (cnt <= 0) {
  1258. unsigned long tmo;
  1259. /*
  1260.  * Don't let us deadlock.  The ADC won't start if
  1261.  * dmabuf->trigger isn't set.  A call to SETTRIGGER
  1262.  * could have turned it off after we set it to on
  1263.  * previously.
  1264.  */
  1265. dmabuf->trigger = PCM_ENABLE_INPUT;
  1266. /*
  1267.  * This does three things.  Updates LVI to be correct,
  1268.  * makes sure the ADC is running, and updates the
  1269.  * hwptr.
  1270.  */
  1271. i810_update_lvi(state,1);
  1272. if (file->f_flags & O_NONBLOCK) {
  1273. if (!ret) ret = -EAGAIN;
  1274. goto done;
  1275. }
  1276. /* Set the timeout to how long it would take to fill
  1277.  * two of our buffers.  If we haven't been woke up
  1278.  * by then, then we know something is wrong.
  1279.  */
  1280. tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
  1281. /* There are two situations when sleep_on_timeout returns, one is when
  1282.    the interrupt is serviced correctly and the process is waked up by
  1283.    ISR ON TIME. Another is when timeout is expired, which means that
  1284.    either interrupt is NOT serviced correctly (pending interrupt) or it
  1285.    is TOO LATE for the process to be scheduled to run (scheduler latency)
  1286.    which results in a (potential) buffer overrun. And worse, there is
  1287.    NOTHING we can do to prevent it. */
  1288. if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
  1289. #ifdef DEBUG
  1290. printk(KERN_ERR "i810_audio: recording schedule timeout, "
  1291.        "dmasz %u fragsz %u count %i hwptr %u swptr %un",
  1292.        dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
  1293.        dmabuf->hwptr, dmabuf->swptr);
  1294. #endif
  1295. /* a buffer overrun, we delay the recovery until next time the
  1296.    while loop begin and we REALLY have space to record */
  1297. }
  1298. if (signal_pending(current)) {
  1299. ret = ret ? ret : -ERESTARTSYS;
  1300. goto done;
  1301. }
  1302. continue;
  1303. }
  1304. if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
  1305. if (!ret) ret = -EFAULT;
  1306. goto done;
  1307. }
  1308. swptr = (swptr + cnt) % dmabuf->dmasize;
  1309. spin_lock_irqsave(&card->lock, flags);
  1310.                 if (PM_SUSPENDED(card)) {
  1311.                         spin_unlock_irqrestore(&card->lock, flags);
  1312.                         continue;
  1313.                 }
  1314. dmabuf->swptr = swptr;
  1315. dmabuf->count -= cnt;
  1316. spin_unlock_irqrestore(&card->lock, flags);
  1317. count -= cnt;
  1318. buffer += cnt;
  1319. ret += cnt;
  1320. }
  1321.  done:
  1322. i810_update_lvi(state,1);
  1323.         set_current_state(TASK_RUNNING);
  1324.         remove_wait_queue(&dmabuf->wait, &waita);
  1325. return ret;
  1326. }
  1327. /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
  1328.    the soundcard.  it is drained by the dma machine and filled by this loop. */
  1329. static ssize_t i810_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
  1330. {
  1331. struct i810_state *state = (struct i810_state *)file->private_data;
  1332. struct i810_card *card=state ? state->card : 0;
  1333. struct dmabuf *dmabuf = &state->dmabuf;
  1334. ssize_t ret;
  1335. unsigned long flags;
  1336. unsigned int swptr = 0;
  1337. int cnt, x;
  1338.         DECLARE_WAITQUEUE(waita, current);
  1339. #ifdef DEBUG2
  1340. printk("i810_audio: i810_write called, count = %dn", count);
  1341. #endif
  1342. if (ppos != &file->f_pos)
  1343. return -ESPIPE;
  1344. if (dmabuf->mapped)
  1345. return -ENXIO;
  1346. if (dmabuf->enable & ADC_RUNNING)
  1347. return -ENODEV;
  1348. if (!dmabuf->write_channel) {
  1349. dmabuf->ready = 0;
  1350. dmabuf->write_channel = card->alloc_pcm_channel(card);
  1351. if(!dmabuf->write_channel)
  1352. return -EBUSY;
  1353. }
  1354. if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
  1355. return ret;
  1356. if (!access_ok(VERIFY_READ, buffer, count))
  1357. return -EFAULT;
  1358. ret = 0;
  1359.         add_wait_queue(&dmabuf->wait, &waita);
  1360. while (count > 0) {
  1361. set_current_state(TASK_INTERRUPTIBLE);
  1362. spin_lock_irqsave(&state->card->lock, flags);
  1363.                 if (PM_SUSPENDED(card)) {
  1364.                         spin_unlock_irqrestore(&card->lock, flags);
  1365.                         schedule();
  1366.                         if (signal_pending(current)) {
  1367.                                 if (!ret) ret = -EAGAIN;
  1368.                                 break;
  1369.                         }
  1370.                         continue;
  1371.                 }
  1372. swptr = dmabuf->swptr;
  1373. cnt = i810_get_free_write_space(state);
  1374. /* Bound the maximum size to how much we can copy to the
  1375.  * dma buffer before we hit the end.  If we have more to
  1376.  * copy then it will get done in a second pass of this
  1377.  * loop starting from the beginning of the buffer.
  1378.  */
  1379. if(cnt > (dmabuf->dmasize - swptr))
  1380. cnt = dmabuf->dmasize - swptr;
  1381. spin_unlock_irqrestore(&state->card->lock, flags);
  1382. #ifdef DEBUG2
  1383. printk(KERN_INFO "i810_audio: i810_write: %d bytes available spacen", cnt);
  1384. #endif
  1385. if (cnt > count)
  1386. cnt = count;
  1387. /* Lop off the last two bits to force the code to always
  1388.  * write in full samples.  This keeps software that sets
  1389.  * O_NONBLOCK but doesn't check the return value of the
  1390.  * write call from getting things out of state where they
  1391.  * think a full 4 byte sample was written when really only
  1392.  * a portion was, resulting in odd sound and stereo
  1393.  * hysteresis.
  1394.  */
  1395. cnt &= ~0x3;
  1396. if (cnt <= 0) {
  1397. unsigned long tmo;
  1398. // There is data waiting to be played
  1399. /*
  1400.  * Force the trigger setting since we would
  1401.  * deadlock with it set any other way
  1402.  */
  1403. dmabuf->trigger = PCM_ENABLE_OUTPUT;
  1404. i810_update_lvi(state,0);
  1405. if (file->f_flags & O_NONBLOCK) {
  1406. if (!ret) ret = -EAGAIN;
  1407. goto ret;
  1408. }
  1409. /* Not strictly correct but works */
  1410. tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
  1411. /* There are two situations when sleep_on_timeout returns, one is when
  1412.    the interrupt is serviced correctly and the process is waked up by
  1413.    ISR ON TIME. Another is when timeout is expired, which means that
  1414.    either interrupt is NOT serviced correctly (pending interrupt) or it
  1415.    is TOO LATE for the process to be scheduled to run (scheduler latency)
  1416.    which results in a (potential) buffer underrun. And worse, there is
  1417.    NOTHING we can do to prevent it. */
  1418. if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
  1419. #ifdef DEBUG
  1420. printk(KERN_ERR "i810_audio: playback schedule timeout, "
  1421.        "dmasz %u fragsz %u count %i hwptr %u swptr %un",
  1422.        dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
  1423.        dmabuf->hwptr, dmabuf->swptr);
  1424. #endif
  1425. /* a buffer underrun, we delay the recovery until next time the
  1426.    while loop begin and we REALLY have data to play */
  1427. //return ret;
  1428. }
  1429. if (signal_pending(current)) {
  1430. if (!ret) ret = -ERESTARTSYS;
  1431. goto ret;
  1432. }
  1433. continue;
  1434. }
  1435. if (copy_from_user(dmabuf->rawbuf+swptr,buffer,cnt)) {
  1436. if (!ret) ret = -EFAULT;
  1437. goto ret;
  1438. }
  1439. swptr = (swptr + cnt) % dmabuf->dmasize;
  1440. spin_lock_irqsave(&state->card->lock, flags);
  1441.                 if (PM_SUSPENDED(card)) {
  1442.                         spin_unlock_irqrestore(&card->lock, flags);
  1443.                         continue;
  1444.                 }
  1445. dmabuf->swptr = swptr;
  1446. dmabuf->count += cnt;
  1447. count -= cnt;
  1448. buffer += cnt;
  1449. ret += cnt;
  1450. spin_unlock_irqrestore(&state->card->lock, flags);
  1451. }
  1452. if (swptr % dmabuf->fragsize) {
  1453. x = dmabuf->fragsize - (swptr % dmabuf->fragsize);
  1454. memset(dmabuf->rawbuf + swptr, '', x);
  1455. }
  1456. ret:
  1457. i810_update_lvi(state,0);
  1458.         set_current_state(TASK_RUNNING);
  1459.         remove_wait_queue(&dmabuf->wait, &waita);
  1460. return ret;
  1461. }
  1462. /* No kernel lock - we have our own spinlock */
  1463. static unsigned int i810_poll(struct file *file, struct poll_table_struct *wait)
  1464. {
  1465. struct i810_state *state = (struct i810_state *)file->private_data;
  1466. struct dmabuf *dmabuf = &state->dmabuf;
  1467. unsigned long flags;
  1468. unsigned int mask = 0;
  1469. if(!dmabuf->ready)
  1470. return 0;
  1471. poll_wait(file, &dmabuf->wait, wait);
  1472. spin_lock_irqsave(&state->card->lock, flags);
  1473. if (dmabuf->enable & ADC_RUNNING ||
  1474.     dmabuf->trigger & PCM_ENABLE_INPUT) {
  1475. if (i810_get_available_read_data(state) >= 
  1476.     (signed)dmabuf->userfragsize)
  1477. mask |= POLLIN | POLLRDNORM;
  1478. }
  1479. if (dmabuf->enable & DAC_RUNNING ||
  1480.     dmabuf->trigger & PCM_ENABLE_OUTPUT) {
  1481. if (i810_get_free_write_space(state) >=
  1482.     (signed)dmabuf->userfragsize)
  1483. mask |= POLLOUT | POLLWRNORM;
  1484. }
  1485. spin_unlock_irqrestore(&state->card->lock, flags);
  1486. return mask;
  1487. }
  1488. static int i810_mmap(struct file *file, struct vm_area_struct *vma)
  1489. {
  1490. struct i810_state *state = (struct i810_state *)file->private_data;
  1491. struct dmabuf *dmabuf = &state->dmabuf;
  1492. int ret = -EINVAL;
  1493. unsigned long size;
  1494. lock_kernel();
  1495. if (vma->vm_flags & VM_WRITE) {
  1496. if (!dmabuf->write_channel &&
  1497.     (dmabuf->write_channel =
  1498.      state->card->alloc_pcm_channel(state->card)) == NULL) {
  1499. ret = -EBUSY;
  1500. goto out;
  1501. }
  1502. }
  1503. if (vma->vm_flags & VM_READ) {
  1504. if (!dmabuf->read_channel &&
  1505.     (dmabuf->read_channel = 
  1506.      state->card->alloc_rec_pcm_channel(state->card)) == NULL) {
  1507. ret = -EBUSY;
  1508. goto out;
  1509. }
  1510. }
  1511. if ((ret = prog_dmabuf(state, 0)) != 0)
  1512. goto out;
  1513. ret = -EINVAL;
  1514. if (vma->vm_pgoff != 0)
  1515. goto out;
  1516. size = vma->vm_end - vma->vm_start;
  1517. if (size > (PAGE_SIZE << dmabuf->buforder))
  1518. goto out;
  1519. ret = -EAGAIN;
  1520. if (remap_page_range(vma->vm_start, virt_to_phys(dmabuf->rawbuf),
  1521.      size, vma->vm_page_prot))
  1522. goto out;
  1523. dmabuf->mapped = 1;
  1524. dmabuf->trigger = 0;
  1525. ret = 0;
  1526. #ifdef DEBUG_MMAP
  1527. printk("i810_audio: mmap'ed %ld bytes of data spacen", size);
  1528. #endif
  1529. out:
  1530. unlock_kernel();
  1531. return ret;
  1532. }
  1533. static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  1534. {
  1535. struct i810_state *state = (struct i810_state *)file->private_data;
  1536. struct i810_channel *c = NULL;
  1537. struct dmabuf *dmabuf = &state->dmabuf;
  1538. unsigned long flags;
  1539. audio_buf_info abinfo;
  1540. count_info cinfo;
  1541. unsigned int i_glob_cnt;
  1542. int val = 0, ret;
  1543. struct ac97_codec *codec = state->card->ac97_codec[0];
  1544. #ifdef DEBUG
  1545. printk("i810_audio: i810_ioctl, arg=0x%x, cmd=", arg ? *(int *)arg : 0);
  1546. #endif
  1547. switch (cmd) 
  1548. {
  1549. case OSS_GETVERSION:
  1550. #ifdef DEBUG
  1551. printk("OSS_GETVERSIONn");
  1552. #endif
  1553. return put_user(SOUND_VERSION, (int *)arg);
  1554. case SNDCTL_DSP_RESET:
  1555. #ifdef DEBUG
  1556. printk("SNDCTL_DSP_RESETn");
  1557. #endif
  1558. spin_lock_irqsave(&state->card->lock, flags);
  1559. if (dmabuf->enable == DAC_RUNNING) {
  1560. c = dmabuf->write_channel;
  1561. __stop_dac(state);
  1562. }
  1563. if (dmabuf->enable == ADC_RUNNING) {
  1564. c = dmabuf->read_channel;
  1565. __stop_adc(state);
  1566. }
  1567. if (c != NULL) {
  1568. outb(2, state->card->iobase+c->port+OFF_CR);   /* reset DMA machine */
  1569. outl(virt_to_bus(&c->sg[0]), state->card->iobase+c->port+OFF_BDBAR);
  1570. outb(0, state->card->iobase+c->port+OFF_CIV);
  1571. outb(0, state->card->iobase+c->port+OFF_LVI);
  1572. }
  1573. spin_unlock_irqrestore(&state->card->lock, flags);
  1574. synchronize_irq();
  1575. dmabuf->ready = 0;
  1576. dmabuf->swptr = dmabuf->hwptr = 0;
  1577. dmabuf->count = dmabuf->total_bytes = 0;
  1578. return 0;
  1579. case SNDCTL_DSP_SYNC:
  1580. #ifdef DEBUG
  1581. printk("SNDCTL_DSP_SYNCn");
  1582. #endif
  1583. if (dmabuf->enable != DAC_RUNNING || file->f_flags & O_NONBLOCK)
  1584. return 0;
  1585. if((val = drain_dac(state, 1)))
  1586. return val;
  1587. dmabuf->total_bytes = 0;
  1588. return 0;
  1589. case SNDCTL_DSP_SPEED: /* set smaple rate */
  1590. #ifdef DEBUG
  1591. printk("SNDCTL_DSP_SPEEDn");
  1592. #endif
  1593. if (get_user(val, (int *)arg))
  1594. return -EFAULT;
  1595. if (val >= 0) {
  1596. if (file->f_mode & FMODE_WRITE) {
  1597. if ( (state->card->ac97_status & SPDIF_ON) ) {  /* S/PDIF Enabled */
  1598. /* AD1886 only supports 48000, need to check that */
  1599. if ( i810_valid_spdif_rate ( codec, val ) ) {
  1600. /* Set DAC rate */
  1601.                                          i810_set_spdif_output ( state, -1, 0 );
  1602. stop_dac(state);
  1603. dmabuf->ready = 0;
  1604. spin_lock_irqsave(&state->card->lock, flags);
  1605. i810_set_dac_rate(state, val);
  1606. spin_unlock_irqrestore(&state->card->lock, flags);
  1607. /* Set S/PDIF transmitter rate. */
  1608. i810_set_spdif_output ( state, AC97_EA_SPSA_3_4, val );
  1609.                                         if ( ! (state->card->ac97_status & SPDIF_ON) ) {
  1610. val = dmabuf->rate;
  1611. }
  1612. } else { /* Not a valid rate for S/PDIF, ignore it */
  1613. val = dmabuf->rate;
  1614. }
  1615. } else {
  1616. stop_dac(state);
  1617. dmabuf->ready = 0;
  1618. spin_lock_irqsave(&state->card->lock, flags);
  1619. i810_set_dac_rate(state, val);
  1620. spin_unlock_irqrestore(&state->card->lock, flags);
  1621. }
  1622. }
  1623. if (file->f_mode & FMODE_READ) {
  1624. stop_adc(state);
  1625. dmabuf->ready = 0;
  1626. spin_lock_irqsave(&state->card->lock, flags);
  1627. i810_set_adc_rate(state, val);
  1628. spin_unlock_irqrestore(&state->card->lock, flags);
  1629. }
  1630. }
  1631. return put_user(dmabuf->rate, (int *)arg);
  1632. case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
  1633. #ifdef DEBUG
  1634. printk("SNDCTL_DSP_STEREOn");
  1635. #endif
  1636. if (dmabuf->enable & DAC_RUNNING) {
  1637. stop_dac(state);
  1638. }
  1639. if (dmabuf->enable & ADC_RUNNING) {
  1640. stop_adc(state);
  1641. }
  1642. return put_user(1, (int *)arg);
  1643. case SNDCTL_DSP_GETBLKSIZE:
  1644. if (file->f_mode & FMODE_WRITE) {
  1645. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)))
  1646. return val;
  1647. }
  1648. if (file->f_mode & FMODE_READ) {
  1649. if (!dmabuf->ready && (val = prog_dmabuf(state, 1)))
  1650. return val;
  1651. }
  1652. #ifdef DEBUG
  1653. printk("SNDCTL_DSP_GETBLKSIZE %dn", dmabuf->userfragsize);
  1654. #endif
  1655. return put_user(dmabuf->userfragsize, (int *)arg);
  1656. case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
  1657. #ifdef DEBUG
  1658. printk("SNDCTL_DSP_GETFMTSn");
  1659. #endif
  1660. return put_user(AFMT_S16_LE, (int *)arg);
  1661. case SNDCTL_DSP_SETFMT: /* Select sample format */
  1662. #ifdef DEBUG
  1663. printk("SNDCTL_DSP_SETFMTn");
  1664. #endif
  1665. return put_user(AFMT_S16_LE, (int *)arg);
  1666. case SNDCTL_DSP_CHANNELS:
  1667. #ifdef DEBUG
  1668. printk("SNDCTL_DSP_CHANNELSn");
  1669. #endif
  1670. if (get_user(val, (int *)arg))
  1671. return -EFAULT;
  1672. if (val > 0) {
  1673. if (dmabuf->enable & DAC_RUNNING) {
  1674. stop_dac(state);
  1675. }
  1676. if (dmabuf->enable & ADC_RUNNING) {
  1677. stop_adc(state);
  1678. }
  1679. } else {
  1680. return put_user(state->card->channels, (int *)arg);
  1681. }
  1682. /* ICH and ICH0 only support 2 channels */
  1683. if ( state->card->pci_id == 0x2415 || state->card->pci_id == 0x2425 ) 
  1684. return put_user(2, (int *)arg);
  1685. /* Multi-channel support was added with ICH2. Bits in */
  1686. /* Global Status and Global Control register are now  */
  1687. /* used to indicate this.                             */
  1688.                 i_glob_cnt = inl(state->card->iobase + GLOB_CNT);
  1689. /* Current # of channels enabled */
  1690. if ( i_glob_cnt & 0x0100000 )
  1691. ret = 4;
  1692. else if ( i_glob_cnt & 0x0200000 )
  1693. ret = 6;
  1694. else
  1695. ret = 2;
  1696. switch ( val ) {
  1697. case 2: /* 2 channels is always supported */
  1698. outl(state->card->iobase + GLOB_CNT, (i_glob_cnt & 0xcfffff));
  1699. /* Do we need to change mixer settings????  */
  1700. break;
  1701. case 4: /* Supported on some chipsets, better check first */
  1702. if ( state->card->channels >= 4 ) {
  1703. outl(state->card->iobase + GLOB_CNT, ((i_glob_cnt & 0xcfffff) | 0x0100000));
  1704. /* Do we need to change mixer settings??? */
  1705. } else {
  1706. val = ret;
  1707. }
  1708. break;
  1709. case 6: /* Supported on some chipsets, better check first */
  1710. if ( state->card->channels >= 6 ) {
  1711. outl(state->card->iobase + GLOB_CNT, ((i_glob_cnt & 0xcfffff) | 0x0200000));
  1712. /* Do we need to change mixer settings??? */
  1713. } else {
  1714. val = ret;
  1715. }
  1716. break;
  1717. default: /* nothing else is ever supported by the chipset */
  1718. val = ret;
  1719. break;
  1720. }
  1721. return put_user(val, (int *)arg);
  1722. case SNDCTL_DSP_POST: /* the user has sent all data and is notifying us */
  1723. /* we update the swptr to the end of the last sg segment then return */
  1724. #ifdef DEBUG
  1725. printk("SNDCTL_DSP_POSTn");
  1726. #endif
  1727. if(!dmabuf->ready || (dmabuf->enable != DAC_RUNNING))
  1728. return 0;
  1729. if((dmabuf->swptr % dmabuf->fragsize) != 0) {
  1730. val = dmabuf->fragsize - (dmabuf->swptr % dmabuf->fragsize);
  1731. dmabuf->swptr += val;
  1732. dmabuf->count += val;
  1733. }
  1734. return 0;
  1735. case SNDCTL_DSP_SUBDIVIDE:
  1736. if (dmabuf->subdivision)
  1737. return -EINVAL;
  1738. if (get_user(val, (int *)arg))
  1739. return -EFAULT;
  1740. if (val != 1 && val != 2 && val != 4)
  1741. return -EINVAL;
  1742. #ifdef DEBUG
  1743. printk("SNDCTL_DSP_SUBDIVIDE %dn", val);
  1744. #endif
  1745. dmabuf->subdivision = val;
  1746. dmabuf->ready = 0;
  1747. return 0;
  1748. case SNDCTL_DSP_SETFRAGMENT:
  1749. if (get_user(val, (int *)arg))
  1750. return -EFAULT;
  1751. dmabuf->ossfragsize = 1<<(val & 0xffff);
  1752. dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
  1753. if (!dmabuf->ossfragsize || !dmabuf->ossmaxfrags)
  1754. return -EINVAL;
  1755. /*
  1756.  * Bound the frag size into our allowed range of 256 - 4096
  1757.  */
  1758. if (dmabuf->ossfragsize < 256)
  1759. dmabuf->ossfragsize = 256;
  1760. else if (dmabuf->ossfragsize > 4096)
  1761. dmabuf->ossfragsize = 4096;
  1762. /*
  1763.  * The numfrags could be something reasonable, or it could
  1764.  * be 0xffff meaning "Give me as much as possible".  So,
  1765.  * we check the numfrags * fragsize doesn't exceed our
  1766.  * 64k buffer limit, nor is it less than our 8k minimum.
  1767.  * If it fails either one of these checks, then adjust the
  1768.  * number of fragments, not the size of them.  It's OK if
  1769.  * our number of fragments doesn't equal 32 or anything
  1770.  * like our hardware based number now since we are using
  1771.  * a different frag count for the hardware.  Before we get
  1772.  * into this though, bound the maxfrags to avoid overflow
  1773.  * issues.  A reasonable bound would be 64k / 256 since our
  1774.  * maximum buffer size is 64k and our minimum frag size is
  1775.  * 256.  On the other end, our minimum buffer size is 8k and
  1776.  * our maximum frag size is 4k, so the lower bound should
  1777.  * be 2.
  1778.  */
  1779. if(dmabuf->ossmaxfrags > 256)
  1780. dmabuf->ossmaxfrags = 256;
  1781. else if (dmabuf->ossmaxfrags < 2)
  1782. dmabuf->ossmaxfrags = 2;
  1783. val = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
  1784. while (val < 8192) {
  1785.     val <<= 1;
  1786.     dmabuf->ossmaxfrags <<= 1;
  1787. }
  1788. while (val > 65536) {
  1789.     val >>= 1;
  1790.     dmabuf->ossmaxfrags >>= 1;
  1791. }
  1792. dmabuf->ready = 0;
  1793. #ifdef DEBUG
  1794. printk("SNDCTL_DSP_SETFRAGMENT 0x%x, %d, %dn", val,
  1795. dmabuf->ossfragsize, dmabuf->ossmaxfrags);
  1796. #endif
  1797. return 0;
  1798. case SNDCTL_DSP_GETOSPACE:
  1799. if (!(file->f_mode & FMODE_WRITE))
  1800. return -EINVAL;
  1801. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
  1802. return val;
  1803. spin_lock_irqsave(&state->card->lock, flags);
  1804. i810_update_ptr(state);
  1805. abinfo.fragsize = dmabuf->userfragsize;
  1806. abinfo.fragstotal = dmabuf->userfrags;
  1807. if (dmabuf->mapped)
  1808.   abinfo.bytes = dmabuf->dmasize;
  1809.    else
  1810.   abinfo.bytes = i810_get_free_write_space(state);
  1811. abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
  1812. spin_unlock_irqrestore(&state->card->lock, flags);
  1813. #if defined(DEBUG) || defined(DEBUG_MMAP)
  1814. printk("SNDCTL_DSP_GETOSPACE %d, %d, %d, %dn", abinfo.bytes,
  1815. abinfo.fragsize, abinfo.fragments, abinfo.fragstotal);
  1816. #endif
  1817. return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
  1818. case SNDCTL_DSP_GETOPTR:
  1819. if (!(file->f_mode & FMODE_WRITE))
  1820. return -EINVAL;
  1821. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
  1822. return val;
  1823. spin_lock_irqsave(&state->card->lock, flags);
  1824. val = i810_get_free_write_space(state);
  1825. cinfo.bytes = dmabuf->total_bytes;
  1826. cinfo.ptr = dmabuf->hwptr;
  1827. cinfo.blocks = val/dmabuf->userfragsize;
  1828. if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
  1829. dmabuf->count += val;
  1830. dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
  1831. __i810_update_lvi(state, 0);
  1832. }
  1833. spin_unlock_irqrestore(&state->card->lock, flags);
  1834. #if defined(DEBUG) || defined(DEBUG_MMAP)
  1835. printk("SNDCTL_DSP_GETOPTR %d, %d, %d, %dn", cinfo.bytes,
  1836. cinfo.blocks, cinfo.ptr, dmabuf->count);
  1837. #endif
  1838. return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
  1839. case SNDCTL_DSP_GETISPACE:
  1840. if (!(file->f_mode & FMODE_READ))
  1841. return -EINVAL;
  1842. if (!dmabuf->ready && (val = prog_dmabuf(state, 1)) != 0)
  1843. return val;
  1844. spin_lock_irqsave(&state->card->lock, flags);
  1845. abinfo.bytes = i810_get_available_read_data(state);
  1846. abinfo.fragsize = dmabuf->userfragsize;
  1847. abinfo.fragstotal = dmabuf->userfrags;
  1848. abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
  1849. spin_unlock_irqrestore(&state->card->lock, flags);
  1850. #if defined(DEBUG) || defined(DEBUG_MMAP)
  1851. printk("SNDCTL_DSP_GETISPACE %d, %d, %d, %dn", abinfo.bytes,
  1852. abinfo.fragsize, abinfo.fragments, abinfo.fragstotal);
  1853. #endif
  1854. return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
  1855. case SNDCTL_DSP_GETIPTR:
  1856. if (!(file->f_mode & FMODE_READ))
  1857. return -EINVAL;
  1858. if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
  1859. return val;
  1860. spin_lock_irqsave(&state->card->lock, flags);
  1861. val = i810_get_available_read_data(state);
  1862. cinfo.bytes = dmabuf->total_bytes;
  1863. cinfo.blocks = val/dmabuf->userfragsize;
  1864. cinfo.ptr = dmabuf->hwptr;
  1865. if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
  1866. dmabuf->count -= val;
  1867. dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
  1868. __i810_update_lvi(state, 1);
  1869. }
  1870. spin_unlock_irqrestore(&state->card->lock, flags);
  1871. #if defined(DEBUG) || defined(DEBUG_MMAP)
  1872. printk("SNDCTL_DSP_GETIPTR %d, %d, %d, %dn", cinfo.bytes,
  1873. cinfo.blocks, cinfo.ptr, dmabuf->count);
  1874. #endif
  1875. return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
  1876. case SNDCTL_DSP_NONBLOCK:
  1877. #ifdef DEBUG
  1878. printk("SNDCTL_DSP_NONBLOCKn");
  1879. #endif
  1880. file->f_flags |= O_NONBLOCK;
  1881. return 0;
  1882. case SNDCTL_DSP_GETCAPS:
  1883. #ifdef DEBUG
  1884. printk("SNDCTL_DSP_GETCAPSn");
  1885. #endif
  1886.     return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP|DSP_CAP_BIND,
  1887.     (int *)arg);
  1888. case SNDCTL_DSP_GETTRIGGER:
  1889. val = 0;
  1890. #ifdef DEBUG
  1891. printk("SNDCTL_DSP_GETTRIGGER 0x%xn", dmabuf->trigger);
  1892. #endif
  1893. return put_user(dmabuf->trigger, (int *)arg);
  1894. case SNDCTL_DSP_SETTRIGGER:
  1895. if (get_user(val, (int *)arg))
  1896. return -EFAULT;
  1897. #if defined(DEBUG) || defined(DEBUG_MMAP)
  1898. printk("SNDCTL_DSP_SETTRIGGER 0x%xn", val);
  1899. #endif
  1900. if( !(val & PCM_ENABLE_INPUT) && dmabuf->enable == ADC_RUNNING) {
  1901. stop_adc(state);
  1902. }
  1903. if( !(val & PCM_ENABLE_OUTPUT) && dmabuf->enable == DAC_RUNNING) {
  1904. stop_dac(state);
  1905. }
  1906. dmabuf->trigger = val;
  1907. if(val & PCM_ENABLE_OUTPUT && !(dmabuf->enable & DAC_RUNNING)) {
  1908. if (!dmabuf->write_channel) {
  1909. dmabuf->ready = 0;
  1910. dmabuf->write_channel = state->card->alloc_pcm_channel(state->card);
  1911. if (!dmabuf->write_channel)
  1912. return -EBUSY;
  1913. }
  1914. if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
  1915. return ret;
  1916. if (dmabuf->mapped) {
  1917. spin_lock_irqsave(&state->card->lock, flags);
  1918. i810_update_ptr(state);
  1919. dmabuf->count = 0;
  1920. dmabuf->swptr = dmabuf->hwptr;
  1921. dmabuf->count = i810_get_free_write_space(state);
  1922. dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
  1923. __i810_update_lvi(state, 0);
  1924. spin_unlock_irqrestore(&state->card->lock, flags);
  1925. } else
  1926. start_dac(state);
  1927. }
  1928. if(val & PCM_ENABLE_INPUT && !(dmabuf->enable & ADC_RUNNING)) {
  1929. if (!dmabuf->read_channel) {
  1930. dmabuf->ready = 0;
  1931. dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card);
  1932. if (!dmabuf->read_channel)
  1933. return -EBUSY;
  1934. }
  1935. if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
  1936. return ret;
  1937. if (dmabuf->mapped) {
  1938. spin_lock_irqsave(&state->card->lock, flags);
  1939. i810_update_ptr(state);
  1940. dmabuf->swptr = dmabuf->hwptr;
  1941. dmabuf->count = 0;
  1942. spin_unlock_irqrestore(&state->card->lock, flags);
  1943. }
  1944. i810_update_lvi(state, 1);
  1945. start_adc(state);
  1946. }
  1947. return 0;
  1948. case SNDCTL_DSP_SETDUPLEX:
  1949. #ifdef DEBUG
  1950. printk("SNDCTL_DSP_SETDUPLEXn");
  1951. #endif
  1952. return -EINVAL;
  1953. case SNDCTL_DSP_GETODELAY:
  1954. if (!(file->f_mode & FMODE_WRITE))
  1955. return -EINVAL;
  1956. spin_lock_irqsave(&state->card->lock, flags);
  1957. i810_update_ptr(state);
  1958. val = dmabuf->count;
  1959. spin_unlock_irqrestore(&state->card->lock, flags);
  1960. #ifdef DEBUG
  1961. printk("SNDCTL_DSP_GETODELAY %dn", dmabuf->count);
  1962. #endif
  1963. return put_user(val, (int *)arg);
  1964. case SOUND_PCM_READ_RATE:
  1965. #ifdef DEBUG
  1966. printk("SOUND_PCM_READ_RATE %dn", dmabuf->rate);
  1967. #endif
  1968. return put_user(dmabuf->rate, (int *)arg);
  1969. case SOUND_PCM_READ_CHANNELS:
  1970. #ifdef DEBUG
  1971. printk("SOUND_PCM_READ_CHANNELSn");
  1972. #endif
  1973. return put_user(2, (int *)arg);
  1974. case SOUND_PCM_READ_BITS:
  1975. #ifdef DEBUG
  1976. printk("SOUND_PCM_READ_BITSn");
  1977. #endif
  1978. return put_user(AFMT_S16_LE, (int *)arg);
  1979. case SNDCTL_DSP_SETSPDIF: /* Set S/PDIF Control register */
  1980. #ifdef DEBUG
  1981. printk("SNDCTL_DSP_SETSPDIFn");
  1982. #endif
  1983. if (get_user(val, (int *)arg))
  1984. return -EFAULT;
  1985. /* Check to make sure the codec supports S/PDIF transmitter */
  1986. if((state->card->ac97_features & 4)) {
  1987. /* mask out the transmitter speed bits so the user can't set them */
  1988. val &= ~0x3000;
  1989. /* Add the current transmitter speed bits to the passed value */
  1990. ret = i810_ac97_get(codec, AC97_SPDIF_CONTROL);
  1991. val |= (ret & 0x3000);
  1992. i810_ac97_set(codec, AC97_SPDIF_CONTROL, val);
  1993. if(i810_ac97_get(codec, AC97_SPDIF_CONTROL) != val ) {
  1994. printk(KERN_ERR "i810_audio: Unable to set S/PDIF configuration to 0x%04x.n", val);
  1995. return -EFAULT;
  1996. }
  1997. }
  1998. #ifdef DEBUG
  1999. else 
  2000. printk(KERN_WARNING "i810_audio: S/PDIF transmitter not avalible.n");
  2001. #endif
  2002. return put_user(val, (int *)arg);
  2003. case SNDCTL_DSP_GETSPDIF: /* Get S/PDIF Control register */
  2004. #ifdef DEBUG
  2005. printk("SNDCTL_DSP_GETSPDIFn");
  2006. #endif
  2007. if (get_user(val, (int *)arg))
  2008. return -EFAULT;
  2009. /* Check to make sure the codec supports S/PDIF transmitter */
  2010. if(!(state->card->ac97_features & 4)) {
  2011. #ifdef DEBUG
  2012. printk(KERN_WARNING "i810_audio: S/PDIF transmitter not avalible.n");
  2013. #endif
  2014. val = 0;
  2015. } else {
  2016. val = i810_ac97_get(codec, AC97_SPDIF_CONTROL);
  2017. }
  2018. //return put_user((val & 0xcfff), (int *)arg);
  2019. return put_user(val, (int *)arg);
  2020.    
  2021. case SNDCTL_DSP_GETCHANNELMASK:
  2022. #ifdef DEBUG
  2023. printk("SNDCTL_DSP_GETCHANNELMASKn");
  2024. #endif
  2025. if (get_user(val, (int *)arg))
  2026. return -EFAULT;
  2027. /* Based on AC'97 DAC support, not ICH hardware */
  2028. val = DSP_BIND_FRONT;
  2029. if ( state->card->ac97_features & 0x0004 )
  2030. val |= DSP_BIND_SPDIF;
  2031. if ( state->card->ac97_features & 0x0080 )
  2032. val |= DSP_BIND_SURR;
  2033. if ( state->card->ac97_features & 0x0140 )
  2034. val |= DSP_BIND_CENTER_LFE;
  2035. return put_user(val, (int *)arg);
  2036. case SNDCTL_DSP_BIND_CHANNEL:
  2037. #ifdef DEBUG
  2038. printk("SNDCTL_DSP_BIND_CHANNELn");
  2039. #endif
  2040. if (get_user(val, (int *)arg))
  2041. return -EFAULT;
  2042. if ( val == DSP_BIND_QUERY ) {
  2043. val = DSP_BIND_FRONT; /* Always report this as being enabled */
  2044. if ( state->card->ac97_status & SPDIF_ON ) 
  2045. val |= DSP_BIND_SPDIF;
  2046. else {
  2047. if ( state->card->ac97_status & SURR_ON )
  2048. val |= DSP_BIND_SURR;
  2049. if ( state->card->ac97_status & CENTER_LFE_ON )
  2050. val |= DSP_BIND_CENTER_LFE;
  2051. }
  2052. } else {  /* Not a query, set it */
  2053. if (!(file->f_mode & FMODE_WRITE))
  2054. return -EINVAL;
  2055. if ( dmabuf->enable == DAC_RUNNING ) {
  2056. stop_dac(state);
  2057. }
  2058. if ( val & DSP_BIND_SPDIF ) {  /* Turn on SPDIF */
  2059. /*  Ok, this should probably define what slots
  2060.  *  to use. For now, we'll only set it to the
  2061.  *  defaults:
  2062.  * 
  2063.  *   non multichannel codec maps to slots 3&4
  2064.  *   2 channel codec maps to slots 7&8
  2065.  *   4 channel codec maps to slots 6&9
  2066.  *   6 channel codec maps to slots 10&11
  2067.  *
  2068.  *  there should be some way for the app to
  2069.  *  select the slot assignment.
  2070.  */
  2071. i810_set_spdif_output ( state, AC97_EA_SPSA_3_4, dmabuf->rate );
  2072. if ( !(state->card->ac97_status & SPDIF_ON) )
  2073. val &= ~DSP_BIND_SPDIF;
  2074. } else {
  2075. int mask;
  2076. int channels;
  2077. /* Turn off S/PDIF if it was on */
  2078. if ( state->card->ac97_status & SPDIF_ON ) 
  2079. i810_set_spdif_output ( state, -1, 0 );
  2080. mask = val & (DSP_BIND_FRONT | DSP_BIND_SURR | DSP_BIND_CENTER_LFE);
  2081. switch (mask) {
  2082. case DSP_BIND_FRONT:
  2083. channels = 2;
  2084. break;
  2085. case DSP_BIND_FRONT|DSP_BIND_SURR:
  2086. channels = 4;
  2087. break;
  2088. case DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE:
  2089. channels = 6;
  2090. break;
  2091. default:
  2092. val = DSP_BIND_FRONT;
  2093. channels = 2;
  2094. break;
  2095. }
  2096. i810_set_dac_channels ( state, channels );
  2097. /* check that they really got turned on */
  2098. if ( !state->card->ac97_status & SURR_ON )
  2099. val &= ~DSP_BIND_SURR;
  2100. if ( !state->card->ac97_status & CENTER_LFE_ON )
  2101. val &= ~DSP_BIND_CENTER_LFE;
  2102. }
  2103. }
  2104. return put_user(val, (int *)arg);
  2105. case SNDCTL_DSP_MAPINBUF:
  2106. case SNDCTL_DSP_MAPOUTBUF:
  2107. case SNDCTL_DSP_SETSYNCRO:
  2108. case SOUND_PCM_WRITE_FILTER:
  2109. case SOUND_PCM_READ_FILTER:
  2110. #ifdef DEBUG
  2111. printk("SNDCTL_* -EINVALn");
  2112. #endif
  2113. return -EINVAL;
  2114. }
  2115. return -EINVAL;
  2116. }
  2117. static int i810_open(struct inode *inode, struct file *file)
  2118. {
  2119. int i = 0;
  2120. struct i810_card *card = devs;
  2121. struct i810_state *state = NULL;
  2122. struct dmabuf *dmabuf = NULL;
  2123. /* find an avaiable virtual channel (instance of /dev/dsp) */
  2124. while (card != NULL) {
  2125. /*
  2126.  * If we are initializing and then fail, card could go
  2127.  * away unuexpectedly while we are in the for() loop.
  2128.  * So, check for card on each iteration before we check
  2129.  * for card->initializing to avoid a possible oops.
  2130.  * This usually only matters for times when the driver is
  2131.  * autoloaded by kmod.
  2132.  */
  2133. for (i = 0; i < 50 && card && card->initializing; i++) {
  2134. set_current_state(TASK_UNINTERRUPTIBLE);
  2135. schedule_timeout(HZ/20);
  2136. }
  2137. for (i = 0; i < NR_HW_CH && card && !card->initializing; i++) {
  2138. if (card->states[i] == NULL) {
  2139. state = card->states[i] = (struct i810_state *)
  2140. kmalloc(sizeof(struct i810_state), GFP_KERNEL);
  2141. if (state == NULL)
  2142. return -ENOMEM;
  2143. memset(state, 0, sizeof(struct i810_state));
  2144. dmabuf = &state->dmabuf;
  2145. goto found_virt;
  2146. }
  2147. }
  2148. card = card->next;
  2149. }
  2150. /* no more virtual channel avaiable */
  2151. if (!state)
  2152. return -ENODEV;
  2153. found_virt:
  2154. /* initialize the virtual channel */
  2155. state->virt = i;
  2156. state->card = card;
  2157. state->magic = I810_STATE_MAGIC;
  2158. init_waitqueue_head(&dmabuf->wait);
  2159. init_MUTEX(&state->open_sem);
  2160. file->private_data = state;
  2161. dmabuf->trigger = 0;
  2162. /* allocate hardware channels */
  2163. if(file->f_mode & FMODE_READ) {
  2164. if((dmabuf->read_channel = card->alloc_rec_pcm_channel(card)) == NULL) {
  2165. kfree (card->states[i]);
  2166. card->states[i] = NULL;;
  2167. return -EBUSY;
  2168. }
  2169. dmabuf->trigger |= PCM_ENABLE_INPUT;
  2170. i810_set_adc_rate(state, 8000);
  2171. }
  2172. if(file->f_mode & FMODE_WRITE) {
  2173. if((dmabuf->write_channel = card->alloc_pcm_channel(card)) == NULL) {
  2174. kfree (card->states[i]);
  2175. card->states[i] = NULL;;
  2176. return -EBUSY;
  2177. }
  2178. /* Initialize to 8kHz?  What if we don't support 8kHz? */
  2179. /*  Let's change this to check for S/PDIF stuff */
  2180. dmabuf->trigger |= PCM_ENABLE_OUTPUT;
  2181. if ( spdif_locked ) {
  2182. i810_set_dac_rate(state, spdif_locked);
  2183. i810_set_spdif_output(state, AC97_EA_SPSA_3_4, spdif_locked);
  2184. } else {
  2185. i810_set_dac_rate(state, 8000);
  2186. }
  2187. }
  2188. /* set default sample format. According to OSS Programmer's Guide  /dev/dsp
  2189.    should be default to unsigned 8-bits, mono, with sample rate 8kHz and
  2190.    /dev/dspW will accept 16-bits sample, but we don't support those so we
  2191.    set it immediately to stereo and 16bit, which is all we do support */
  2192. dmabuf->fmt |= I810_FMT_16BIT | I810_FMT_STEREO;
  2193. dmabuf->ossfragsize = 0;
  2194. dmabuf->ossmaxfrags  = 0;
  2195. dmabuf->subdivision  = 0;
  2196. state->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
  2197. return 0;
  2198. }
  2199. static int i810_release(struct inode *inode, struct file *file)
  2200. {
  2201. struct i810_state *state = (struct i810_state *)file->private_data;
  2202. struct i810_card *card = state->card;
  2203. struct dmabuf *dmabuf = &state->dmabuf;
  2204. unsigned long flags;
  2205. lock_kernel();
  2206. /* stop DMA state machine and free DMA buffers/channels */
  2207. if(dmabuf->trigger & PCM_ENABLE_OUTPUT) {
  2208. drain_dac(state, 0);
  2209. }
  2210. if(dmabuf->trigger & PCM_ENABLE_INPUT) {
  2211. stop_adc(state);
  2212. }
  2213. spin_lock_irqsave(&card->lock, flags);
  2214. dealloc_dmabuf(state);
  2215. if (file->f_mode & FMODE_WRITE) {
  2216. state->card->free_pcm_channel(state->card, dmabuf->write_channel->num);
  2217. }
  2218. if (file->f_mode & FMODE_READ) {
  2219. state->card->free_pcm_channel(state->card, dmabuf->read_channel->num);
  2220. }
  2221. state->card->states[state->virt] = NULL;
  2222. kfree(state);
  2223. spin_unlock_irqrestore(&card->lock, flags);
  2224. unlock_kernel();
  2225. return 0;
  2226. }
  2227. static /*const*/ struct file_operations i810_audio_fops = {
  2228. owner: THIS_MODULE,
  2229. llseek: no_llseek,
  2230. read: i810_read,
  2231. write: i810_write,
  2232. poll: i810_poll,
  2233. ioctl: i810_ioctl,
  2234. mmap: i810_mmap,
  2235. open: i810_open,
  2236. release: i810_release,
  2237. };
  2238. /* Write AC97 codec registers */
  2239. static u16 i810_ac97_get(struct ac97_codec *dev, u8 reg)
  2240. {
  2241. struct i810_card *card = dev->private_data;
  2242. int count = 100;
  2243. u8 reg_set = ((dev->id)?((reg&0x7f)|0x80):(reg&0x7f));
  2244. while(count-- && (inb(card->iobase + CAS) & 1)) 
  2245. udelay(1);
  2246. return inw(card->ac97base + reg_set);
  2247. }
  2248. static void i810_ac97_set(struct ac97_codec *dev, u8 reg, u16 data)
  2249. {
  2250. struct i810_card *card = dev->private_data;
  2251. int count = 100;
  2252. u8 reg_set = ((dev->id)?((reg&0x7f)|0x80):(reg&0x7f));
  2253. while(count-- && (inb(card->iobase + CAS) & 1)) 
  2254. udelay(1);
  2255. outw(data, card->ac97base + reg_set);
  2256. }
  2257. /* OSS /dev/mixer file operation methods */
  2258. static int i810_open_mixdev(struct inode *inode, struct file *file)
  2259. {
  2260. int i;
  2261. int minor = MINOR(inode->i_rdev);
  2262. struct i810_card *card = devs;
  2263. for (card = devs; card != NULL; card = card->next) {
  2264. /*
  2265.  * If we are initializing and then fail, card could go
  2266.  * away unuexpectedly while we are in the for() loop.
  2267.  * So, check for card on each iteration before we check
  2268.  * for card->initializing to avoid a possible oops.
  2269.  * This usually only matters for times when the driver is
  2270.  * autoloaded by kmod.
  2271.  */
  2272. for (i = 0; i < 50 && card && card->initializing; i++) {
  2273. set_current_state(TASK_UNINTERRUPTIBLE);
  2274. schedule_timeout(HZ/20);
  2275. }
  2276. for (i = 0; i < NR_AC97 && card && !card->initializing; i++)
  2277. if (card->ac97_codec[i] != NULL &&
  2278.     card->ac97_codec[i]->dev_mixer == minor) {
  2279. file->private_data = card->ac97_codec[i];
  2280. return 0;
  2281. }
  2282. }
  2283. return -ENODEV;
  2284. }
  2285. static int i810_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
  2286. unsigned long arg)
  2287. {
  2288. struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
  2289. return codec->mixer_ioctl(codec, cmd, arg);
  2290. }
  2291. static /*const*/ struct file_operations i810_mixer_fops = {
  2292. owner: THIS_MODULE,
  2293. llseek: no_llseek,
  2294. ioctl: i810_ioctl_mixdev,
  2295. open: i810_open_mixdev,
  2296. };
  2297. /* AC97 codec initialisation.  These small functions exist so we don't
  2298.    duplicate code between module init and apm resume */
  2299. static inline int i810_ac97_exists(struct i810_card *card,int ac97_number)
  2300. {
  2301. u32 reg = inl(card->iobase + GLOB_STA);
  2302. return (reg & (0x100 << ac97_number));
  2303. }
  2304. static inline int i810_ac97_enable_variable_rate(struct ac97_codec *codec)
  2305. {
  2306. i810_ac97_set(codec, AC97_EXTENDED_STATUS, 9);
  2307. i810_ac97_set(codec,AC97_EXTENDED_STATUS,
  2308.       i810_ac97_get(codec, AC97_EXTENDED_STATUS)|0xE800);
  2309. return (i810_ac97_get(codec, AC97_EXTENDED_STATUS)&1);
  2310. }
  2311. static int i810_ac97_probe_and_powerup(struct i810_card *card,struct ac97_codec *codec)
  2312. {
  2313. /* Returns 0 on failure */
  2314. int i;
  2315. if (ac97_probe_codec(codec) == 0) return 0;
  2316. /* power it all up */
  2317. i810_ac97_set(codec, AC97_POWER_CONTROL,
  2318.       i810_ac97_get(codec, AC97_POWER_CONTROL) & ~0x7f00);
  2319. /* wait for analog ready */
  2320. for (i=10;
  2321.      i && ((i810_ac97_get(codec, AC97_POWER_CONTROL) & 0xf) != 0xf);
  2322.      i--)
  2323. {
  2324. set_current_state(TASK_UNINTERRUPTIBLE);
  2325. schedule_timeout(HZ/20);
  2326. return i;
  2327. }
  2328. /* if I knew what this did, I'd give it a better name */
  2329. static int i810_ac97_random_init_stuff(struct i810_card *card)
  2330. {
  2331. u32 reg = inl(card->iobase + GLOB_CNT);
  2332. int i;
  2333. if((reg&2)==0) /* Cold required */
  2334. reg|=2;
  2335. else
  2336. reg|=4; /* Warm */
  2337. reg&=~8; /* ACLink on */
  2338. outl(reg , card->iobase + GLOB_CNT);
  2339. for(i=0;i<10;i++)
  2340. {
  2341. if((inl(card->iobase+GLOB_CNT)&4)==0)
  2342. break;
  2343. set_current_state(TASK_UNINTERRUPTIBLE);
  2344. schedule_timeout(HZ/20);
  2345. }
  2346. if(i==10)
  2347. {
  2348. printk(KERN_ERR "i810_audio: AC'97 reset failed.n");
  2349. return 0;
  2350. }
  2351. set_current_state(TASK_UNINTERRUPTIBLE);
  2352. schedule_timeout(HZ/2);
  2353. reg = inl(card->iobase + GLOB_STA);
  2354. inw(card->ac97base);
  2355. return 1;
  2356. }
  2357. static int __init i810_ac97_init(struct i810_card *card)
  2358. {
  2359. int num_ac97 = 0;
  2360. int total_channels = 0;
  2361. struct ac97_codec *codec;
  2362. u16 eid;
  2363. u32 reg;
  2364. if(!i810_ac97_random_init_stuff(card)) return 0;
  2365. /* Number of channels supported */
  2366. /* What about the codec?  Just because the ICH supports */
  2367. /* multiple channels doesn't mean the codec does.       */
  2368. /* we'll have to modify this in the codec section below */
  2369. /* to reflect what the codec has.                       */
  2370. /* ICH and ICH0 only support 2 channels so don't bother */
  2371. /* to check....                                         */
  2372. card->channels = 2;
  2373. reg = inl(card->iobase + GLOB_STA);
  2374. if ( reg & 0x0200000 )
  2375. card->channels = 6;
  2376. else if ( reg & 0x0100000 )
  2377. card->channels = 4;
  2378. printk("i810_audio: Audio Controller supports %d channels.n", card->channels);
  2379. inw(card->ac97base);
  2380. for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
  2381. /* Assume codec isn't available until we go through the
  2382.  * gauntlet below */
  2383. card->ac97_codec[num_ac97] = NULL;
  2384. /* The ICH programmer's reference says you should   */
  2385. /* check the ready status before probing. So we chk */
  2386. /*   What do we do if it's not ready?  Wait and try */
  2387. /*   again, or abort?                               */
  2388. if (!i810_ac97_exists(card,num_ac97)) {
  2389. if(num_ac97 == 0)
  2390. printk(KERN_ERR "i810_audio: Primary codec not ready.n");
  2391. break; /* I think this works, if not ready stop */
  2392. }
  2393. if ((codec = kmalloc(sizeof(struct ac97_codec), GFP_KERNEL)) == NULL)
  2394. return -ENOMEM;
  2395. memset(codec, 0, sizeof(struct ac97_codec));
  2396. /* initialize some basic codec information, other fields will be filled
  2397.    in ac97_probe_codec */
  2398. codec->private_data = card;
  2399. codec->id = num_ac97;
  2400. codec->codec_read = i810_ac97_get;
  2401. codec->codec_write = i810_ac97_set;
  2402. if(!i810_ac97_probe_and_powerup(card,codec)) {
  2403. printk("i810_audio: timed out waiting for codec %d analog ready.n", num_ac97);
  2404. kfree(codec);
  2405. break; /* it didn't work */
  2406. }
  2407. /* Store state information about S/PDIF transmitter */
  2408. card->ac97_status = 0;
  2409. /* Don't attempt to get eid until powerup is complete */
  2410. eid = i810_ac97_get(codec, AC97_EXTENDED_ID);
  2411. if(eid==0xFFFFFF)
  2412. {
  2413. printk(KERN_WARNING "i810_audio: no codec attached ?n");
  2414. kfree(codec);
  2415. break;
  2416. }
  2417. codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0L);
  2418. if(codec->codec_read(codec, AC97_EXTENDED_MODEM_ID))
  2419. {
  2420. printk(KERN_WARNING "i810_audio: codec %d is a softmodem - skipping.n", num_ac97);
  2421. kfree(codec);
  2422. continue;
  2423. }
  2424. card->ac97_features = eid;
  2425. /* Now check the codec for useful features to make up for
  2426.    the dumbness of the 810 hardware engine */
  2427. if(!(eid&0x0001))
  2428. printk(KERN_WARNING "i810_audio: only 48Khz playback available.n");
  2429. else
  2430. {
  2431. if(!i810_ac97_enable_variable_rate(codec)) {
  2432. printk(KERN_WARNING "i810_audio: Codec refused to allow VRA, using 48Khz only.n");
  2433. card->ac97_features&=~1;
  2434. }
  2435. }
  2436.    
  2437. /* Determine how many channels the codec(s) support   */
  2438. /*   - The primary codec always supports 2            */
  2439. /*   - If the codec supports AMAP, surround DACs will */
  2440. /*     automaticlly get assigned to slots.            */
  2441. /*     * Check for surround DACs and increment if     */
  2442. /*       found.                                       */
  2443. /*   - Else check if the codec is revision 2.2        */
  2444. /*     * If surround DACs exist, assign them to slots */
  2445. /*       and increment channel count.                 */
  2446. /* All of this only applies to ICH2 and above. ICH    */
  2447. /* and ICH0 only support 2 channels.  ICH2 will only  */
  2448. /* support multiple codecs in a "split audio" config. */
  2449. /* as described above.                                */
  2450. /* TODO: Remove all the debugging messages!           */
  2451. if((eid & 0xc000) == 0) /* primary codec */
  2452. total_channels += 2; 
  2453. if(eid & 0x200) { /* GOOD, AMAP support */
  2454. if (eid & 0x0080) /* L/R Surround channels */
  2455. total_channels += 2;
  2456. if (eid & 0x0140) /* LFE and Center channels */
  2457. total_channels += 2;
  2458. printk("i810_audio: AC'97 codec %d supports AMAP, total channels = %dn", num_ac97, total_channels);
  2459. } else if (eid & 0x0400) {  /* this only works on 2.2 compliant codecs */
  2460. eid &= 0xffcf;
  2461. if((eid & 0xc000) != 0) {
  2462. switch ( total_channels ) {
  2463. case 2:
  2464. /* Set dsa1, dsa0 to 01 */
  2465. eid |= 0x0010;
  2466. break;
  2467. case 4:
  2468. /* Set dsa1, dsa0 to 10 */
  2469. eid |= 0x0020;
  2470. break;
  2471. case 6:
  2472. /* Set dsa1, dsa0 to 11 */
  2473. eid |= 0x0030;
  2474. break;
  2475. }
  2476. total_channels += 2;
  2477. }
  2478. i810_ac97_set(codec, AC97_EXTENDED_ID, eid);
  2479. eid = i810_ac97_get(codec, AC97_EXTENDED_ID);
  2480. printk("i810_audio: AC'97 codec %d, new EID value = 0x%04xn", num_ac97, eid);
  2481. if (eid & 0x0080) /* L/R Surround channels */
  2482. total_channels += 2;
  2483. if (eid & 0x0140) /* LFE and Center channels */
  2484. total_channels += 2;
  2485. printk("i810_audio: AC'97 codec %d, DAC map configured, total channels = %dn", num_ac97, total_channels);
  2486. } else {
  2487. printk("i810_audio: AC'97 codec %d Unable to map surround DAC's (or DAC's not present), total channels = %dn", num_ac97, total_channels);
  2488. }
  2489. if ((codec->dev_mixer = register_sound_mixer(&i810_mixer_fops, -1)) < 0) {
  2490. printk(KERN_ERR "i810_audio: couldn't register mixer!n");
  2491. kfree(codec);
  2492. break;
  2493. }
  2494. card->ac97_codec[num_ac97] = codec;
  2495. }
  2496. /* pick the minimum of channels supported by ICHx or codec(s) */
  2497. card->channels = (card->channels > total_channels)?total_channels:card->channels;
  2498. return num_ac97;
  2499. }
  2500. static void __init i810_configure_clocking (void)
  2501. {
  2502. struct i810_card *card;
  2503. struct i810_state *state;
  2504. struct dmabuf *dmabuf;
  2505. unsigned int i, offset, new_offset;
  2506. unsigned long flags;
  2507. card = devs;
  2508. /* We could try to set the clocking for multiple cards, but can you even have
  2509.  * more than one i810 in a machine?  Besides, clocking is global, so unless
  2510.  * someone actually thinks more than one i810 in a machine is possible and
  2511.  * decides to rewrite that little bit, setting the rate for more than one card
  2512.  * is a waste of time.
  2513.  */
  2514. if(card != NULL) {
  2515. state = card->states[0] = (struct i810_state *)
  2516. kmalloc(sizeof(struct i810_state), GFP_KERNEL);
  2517. if (state == NULL)
  2518. return;
  2519. memset(state, 0, sizeof(struct i810_state));
  2520. dmabuf = &state->dmabuf;
  2521. dmabuf->write_channel = card->alloc_pcm_channel(card);
  2522. state->virt = 0;
  2523. state->card = card;
  2524. state->magic = I810_STATE_MAGIC;
  2525. init_waitqueue_head(&dmabuf->wait);
  2526. init_MUTEX(&state->open_sem);
  2527. dmabuf->fmt = I810_FMT_STEREO | I810_FMT_16BIT;
  2528. dmabuf->trigger = PCM_ENABLE_OUTPUT;
  2529. i810_set_dac_rate(state, 48000);
  2530. if(prog_dmabuf(state, 0) != 0) {
  2531. goto config_out_nodmabuf;
  2532. }
  2533. if(dmabuf->dmasize < 16384) {
  2534. goto config_out;
  2535. }
  2536. dmabuf->count = dmabuf->dmasize;
  2537. outb(31,card->iobase+dmabuf->write_channel->port+OFF_LVI);
  2538. save_flags(flags);
  2539. cli();
  2540. start_dac(state);
  2541. offset = i810_get_dma_addr(state, 0);
  2542. mdelay(50);
  2543. new_offset = i810_get_dma_addr(state, 0);
  2544. stop_dac(state);
  2545. outb(2,card->iobase+dmabuf->write_channel->port+OFF_CR);
  2546. restore_flags(flags);
  2547. i = new_offset - offset;
  2548. #ifdef DEBUG
  2549. printk("i810_audio: %d bytes in 50 millisecondsn", i);
  2550. #endif
  2551. if(i == 0)
  2552. goto config_out;
  2553. i = i / 4 * 20;
  2554. if (i > 48500 || i < 47500) {
  2555. clocking = clocking * clocking / i;
  2556. printk("i810_audio: setting clocking to %dn", clocking);
  2557. }
  2558. config_out:
  2559. dealloc_dmabuf(state);
  2560. config_out_nodmabuf:
  2561. state->card->free_pcm_channel(state->card,state->dmabuf.write_channel->num);
  2562. kfree(state);
  2563. card->states[0] = NULL;
  2564. }
  2565. }
  2566. /* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered 
  2567.    until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
  2568.    
  2569. static int __init i810_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
  2570. {
  2571. struct i810_card *card;
  2572. if (pci_enable_device(pci_dev))
  2573. return -EIO;
  2574. if (pci_set_dma_mask(pci_dev, I810_DMA_MASK)) {
  2575. printk(KERN_ERR "intel810: architecture does not support"
  2576.        " 32bit PCI busmaster DMAn");
  2577. return -ENODEV;
  2578. }
  2579. if ((card = kmalloc(sizeof(struct i810_card), GFP_KERNEL)) == NULL) {
  2580. printk(KERN_ERR "i810_audio: out of memoryn");
  2581. return -ENOMEM;
  2582. }
  2583. memset(card, 0, sizeof(*card));
  2584. card->initializing = 1;
  2585. card->iobase = pci_resource_start (pci_dev, 1);
  2586. card->ac97base = pci_resource_start (pci_dev, 0);
  2587. card->pci_dev = pci_dev;
  2588. card->pci_id = pci_id->device;
  2589. card->irq = pci_dev->irq;
  2590. card->next = devs;
  2591. card->magic = I810_CARD_MAGIC;
  2592. #ifdef CONFIG_PM
  2593. card->pm_suspended=0;
  2594. #endif
  2595. spin_lock_init(&card->lock);
  2596. devs = card;
  2597. pci_set_master(pci_dev);
  2598. printk(KERN_INFO "i810: %s found at IO 0x%04lx and 0x%04lx, IRQ %dn",
  2599.        card_names[pci_id->driver_data], card->iobase, card->ac97base, 
  2600.        card->irq);
  2601. card->alloc_pcm_channel = i810_alloc_pcm_channel;
  2602. card->alloc_rec_pcm_channel = i810_alloc_rec_pcm_channel;
  2603. card->alloc_rec_mic_channel = i810_alloc_rec_mic_channel;
  2604. card->free_pcm_channel = i810_free_pcm_channel;
  2605. card->channel[0].offset = 0;
  2606. card->channel[0].port = 0x00;
  2607. card->channel[0].num=0;
  2608. card->channel[1].offset = 0;
  2609. card->channel[1].port = 0x10;
  2610. card->channel[1].num=1;
  2611. card->channel[2].offset = 0;
  2612. card->channel[2].port = 0x20;
  2613. card->channel[2].num=2;
  2614. /* claim our iospace and irq */
  2615. request_region(card->iobase, 64, card_names[pci_id->driver_data]);
  2616. request_region(card->ac97base, 256, card_names[pci_id->driver_data]);
  2617. if (request_irq(card->irq, &i810_interrupt, SA_SHIRQ,
  2618. card_names[pci_id->driver_data], card)) {
  2619. printk(KERN_ERR "i810_audio: unable to allocate irq %dn", card->irq);
  2620. release_region(card->iobase, 64);
  2621. release_region(card->ac97base, 256);
  2622. kfree(card);
  2623. return -ENODEV;
  2624. }
  2625. /* initialize AC97 codec and register /dev/mixer */
  2626. if (i810_ac97_init(card) <= 0) {
  2627. release_region(card->iobase, 64);
  2628. release_region(card->ac97base, 256);
  2629. free_irq(card->irq, card);
  2630. kfree(card);
  2631. return -ENODEV;
  2632. }
  2633. pci_set_drvdata(pci_dev, card);
  2634. if(clocking == 0) {
  2635. clocking = 48000;
  2636. i810_configure_clocking();
  2637. }
  2638. /* register /dev/dsp */
  2639. if ((card->dev_audio = register_sound_dsp(&i810_audio_fops, -1)) < 0) {
  2640. int i;
  2641. printk(KERN_ERR "i810_audio: couldn't register DSP device!n");
  2642. release_region(card->iobase, 64);
  2643. release_region(card->ac97base, 256);
  2644. free_irq(card->irq, card);
  2645. for (i = 0; i < NR_AC97; i++)
  2646. if (card->ac97_codec[i] != NULL) {
  2647. unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
  2648. kfree (card->ac97_codec[i]);
  2649. }
  2650. kfree(card);
  2651. return -ENODEV;
  2652. }
  2653.   card->initializing = 0;
  2654. return 0;
  2655. }
  2656. static void __devexit i810_remove(struct pci_dev *pci_dev)
  2657. {
  2658. int i;
  2659. struct i810_card *card = pci_get_drvdata(pci_dev);
  2660. /* free hardware resources */
  2661. free_irq(card->irq, devs);
  2662. release_region(card->iobase, 64);
  2663. release_region(card->ac97base, 256);
  2664. /* unregister audio devices */
  2665. for (i = 0; i < NR_AC97; i++)
  2666. if (card->ac97_codec[i] != NULL) {
  2667. unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
  2668. kfree (card->ac97_codec[i]);
  2669. card->ac97_codec[i] = NULL;
  2670. }
  2671. unregister_sound_dsp(card->dev_audio);
  2672. kfree(card);
  2673. }
  2674. #ifdef CONFIG_PM
  2675. static int i810_pm_suspend(struct pci_dev *dev, u32 pm_state)
  2676. {
  2677.         struct i810_card *card = pci_get_drvdata(dev);
  2678.         struct i810_state *state;
  2679. unsigned long flags;
  2680. struct dmabuf *dmabuf;
  2681. int i,num_ac97;
  2682. #ifdef DEBUG
  2683. printk("i810_audio: i810_pm_suspend calledn");
  2684. #endif
  2685. if(!card) return 0;
  2686. spin_lock_irqsave(&card->lock, flags);
  2687. card->pm_suspended=1;
  2688. for(i=0;i<NR_HW_CH;i++) {
  2689. state = card->states[i];
  2690. if(!state) continue;
  2691. /* this happens only if there are open files */
  2692. dmabuf = &state->dmabuf;
  2693. if(dmabuf->enable & DAC_RUNNING ||
  2694.    (dmabuf->count && (dmabuf->trigger & PCM_ENABLE_OUTPUT))) {
  2695. state->pm_saved_dac_rate=dmabuf->rate;
  2696. stop_dac(state);
  2697. } else {
  2698. state->pm_saved_dac_rate=0;
  2699. }
  2700. if(dmabuf->enable & ADC_RUNNING) {
  2701. state->pm_saved_adc_rate=dmabuf->rate;
  2702. stop_adc(state);
  2703. } else {
  2704. state->pm_saved_adc_rate=0;
  2705. }
  2706. dmabuf->ready = 0;
  2707. dmabuf->swptr = dmabuf->hwptr = 0;
  2708. dmabuf->count = dmabuf->total_bytes = 0;
  2709. }
  2710. spin_unlock_irqrestore(&card->lock, flags);
  2711. /* save mixer settings */
  2712. for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
  2713. struct ac97_codec *codec = card->ac97_codec[num_ac97];
  2714. if(!codec) continue;
  2715. for(i=0;i< SOUND_MIXER_NRDEVICES ;i++) {
  2716. if((supported_mixer(codec,i)) &&
  2717.    (codec->read_mixer)) {
  2718. card->pm_saved_mixer_settings[i][num_ac97]=
  2719. codec->read_mixer(codec,i);
  2720. }
  2721. }
  2722. }
  2723. pci_save_state(dev,card->pm_save_state); /* XXX do we need this? */
  2724. pci_disable_device(dev); /* disable busmastering */
  2725. pci_set_power_state(dev,3); /* Zzz. */
  2726. return 0;
  2727. }
  2728. static int i810_pm_resume(struct pci_dev *dev)
  2729. {
  2730. int num_ac97,i=0;
  2731. struct i810_card *card=pci_get_drvdata(dev);
  2732. pci_enable_device(dev);
  2733. pci_restore_state (dev,card->pm_save_state);
  2734. /* observation of a toshiba portege 3440ct suggests that the 
  2735.    hardware has to be more or less completely reinitialized from
  2736.    scratch after an apm suspend.  Works For Me.   -dan */
  2737. i810_ac97_random_init_stuff(card);
  2738. for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
  2739. struct ac97_codec *codec = card->ac97_codec[num_ac97];
  2740. /* check they haven't stolen the hardware while we were
  2741.    away */
  2742. if(!codec || !i810_ac97_exists(card,num_ac97)) {
  2743. if(num_ac97) continue;
  2744. else BUG();
  2745. }
  2746. if(!i810_ac97_probe_and_powerup(card,codec)) BUG();
  2747. if((card->ac97_features&0x0001)) {
  2748. /* at probe time we found we could do variable
  2749.    rates, but APM suspend has made it forget
  2750.    its magical powers */
  2751. if(!i810_ac97_enable_variable_rate(codec)) BUG();
  2752. }
  2753. /* we lost our mixer settings, so restore them */
  2754. for(i=0;i< SOUND_MIXER_NRDEVICES ;i++) {
  2755. if(supported_mixer(codec,i)){
  2756. int val=card->
  2757. pm_saved_mixer_settings[i][num_ac97];
  2758. codec->mixer_state[i]=val;
  2759. codec->write_mixer(codec,i,
  2760.    (val  & 0xff) ,
  2761.    ((val >> 8)  & 0xff) );
  2762. }
  2763. }
  2764. }
  2765. /* we need to restore the sample rate from whatever it was */
  2766. for(i=0;i<NR_HW_CH;i++) {
  2767. struct i810_state * state=card->states[i];
  2768. if(state) {
  2769. if(state->pm_saved_adc_rate)
  2770. i810_set_adc_rate(state,state->pm_saved_adc_rate);
  2771. if(state->pm_saved_dac_rate)
  2772. i810_set_dac_rate(state,state->pm_saved_dac_rate);
  2773. }
  2774. }
  2775.         card->pm_suspended = 0;
  2776. /* any processes that were reading/writing during the suspend
  2777.    probably ended up here */
  2778. for(i=0;i<NR_HW_CH;i++) {
  2779. struct i810_state *state = card->states[i];
  2780. if(state) wake_up(&state->dmabuf.wait);
  2781.         }
  2782. return 0;
  2783. }
  2784. #endif /* CONFIG_PM */
  2785. MODULE_AUTHOR("");
  2786. MODULE_DESCRIPTION("Intel 810 audio support");
  2787. MODULE_LICENSE("GPL");
  2788. MODULE_PARM(ftsodell, "i");
  2789. MODULE_PARM(clocking, "i");
  2790. MODULE_PARM(strict_clocking, "i");
  2791. MODULE_PARM(spdif_locked, "i");
  2792. #define I810_MODULE_NAME "intel810_audio"
  2793. static struct pci_driver i810_pci_driver = {
  2794. name: I810_MODULE_NAME,
  2795. id_table: i810_pci_tbl,
  2796. probe: i810_probe,
  2797. remove: __devexit_p(i810_remove),
  2798. #ifdef CONFIG_PM
  2799. suspend: i810_pm_suspend,
  2800. resume: i810_pm_resume,
  2801. #endif /* CONFIG_PM */
  2802. };
  2803. static int __init i810_init_module (void)
  2804. {
  2805. if (!pci_present())   /* No PCI bus in this machine! */
  2806. return -ENODEV;
  2807. printk(KERN_INFO "Intel 810 + AC97 Audio, version "
  2808.        DRIVER_VERSION ", " __TIME__ " " __DATE__ "n");
  2809. if (!pci_register_driver(&i810_pci_driver)) {
  2810. pci_unregister_driver(&i810_pci_driver);
  2811.                 return -ENODEV;
  2812. }
  2813. if(ftsodell != 0) {
  2814. printk("i810_audio: ftsodell is now a deprecated option.n");
  2815. }
  2816. if(spdif_locked > 0 ) {
  2817. if(spdif_locked == 32000 || spdif_locked == 44100 || spdif_locked == 48000) {
  2818. printk("i810_audio: Enabling S/PDIF at sample rate %dHz.n", spdif_locked);
  2819. } else {
  2820. printk("i810_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.n");
  2821. spdif_locked = 0;
  2822. }
  2823. }
  2824. return 0;
  2825. }
  2826. static void __exit i810_cleanup_module (void)
  2827. {
  2828. pci_unregister_driver(&i810_pci_driver);
  2829. }
  2830. module_init(i810_init_module);
  2831. module_exit(i810_cleanup_module);
  2832. /*
  2833. Local Variables:
  2834. c-basic-offset: 8
  2835. End:
  2836. */