eeepc-laptop: fix use after free
[linux-2.6/mini2440.git] / sound / pci / via82xx_modem.c
blob31f64ee398820c62ffb8c9b51318a2c4e122e9b0
1 /*
2 * ALSA modem driver for VIA VT82xx (South Bridge)
4 * VT82C686A/B/C, VT8233A/C, VT8235
6 * Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
7 * Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
8 * 2002 Takashi Iwai <tiwai@suse.de>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Changes:
29 * Sep. 2, 2004 Sasha Khapyorsky <sashak@alsa-project.org>
30 * Modified from original audio driver 'via82xx.c' to support AC97
31 * modems.
34 #include <asm/io.h>
35 #include <linux/delay.h>
36 #include <linux/interrupt.h>
37 #include <linux/init.h>
38 #include <linux/pci.h>
39 #include <linux/slab.h>
40 #include <linux/moduleparam.h>
41 #include <sound/core.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/info.h>
45 #include <sound/ac97_codec.h>
46 #include <sound/initval.h>
48 #if 0
49 #define POINTER_DEBUG
50 #endif
52 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
53 MODULE_DESCRIPTION("VIA VT82xx modem");
54 MODULE_LICENSE("GPL");
55 MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C modem,pci}}");
57 static int index = -2; /* Exclude the first card */
58 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
59 static int ac97_clock = 48000;
61 module_param(index, int, 0444);
62 MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
63 module_param(id, charp, 0444);
64 MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
65 module_param(ac97_clock, int, 0444);
66 MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
68 /* just for backward compatibility */
69 static int enable;
70 module_param(enable, bool, 0444);
74 * Direct registers
77 #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
78 #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
80 /* common offsets */
81 #define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
82 #define VIA_REG_STAT_ACTIVE 0x80 /* RO */
83 #define VIA_REG_STAT_PAUSED 0x40 /* RO */
84 #define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
85 #define VIA_REG_STAT_STOPPED 0x04 /* RWC */
86 #define VIA_REG_STAT_EOL 0x02 /* RWC */
87 #define VIA_REG_STAT_FLAG 0x01 /* RWC */
88 #define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
89 #define VIA_REG_CTRL_START 0x80 /* WO */
90 #define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
91 #define VIA_REG_CTRL_AUTOSTART 0x20
92 #define VIA_REG_CTRL_PAUSE 0x08 /* RW */
93 #define VIA_REG_CTRL_INT_STOP 0x04
94 #define VIA_REG_CTRL_INT_EOL 0x02
95 #define VIA_REG_CTRL_INT_FLAG 0x01
96 #define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
97 #define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
98 #define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
99 #define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
100 #define VIA_REG_TYPE_16BIT 0x20 /* RW */
101 #define VIA_REG_TYPE_STEREO 0x10 /* RW */
102 #define VIA_REG_TYPE_INT_LLINE 0x00
103 #define VIA_REG_TYPE_INT_LSAMPLE 0x04
104 #define VIA_REG_TYPE_INT_LESSONE 0x08
105 #define VIA_REG_TYPE_INT_MASK 0x0c
106 #define VIA_REG_TYPE_INT_EOL 0x02
107 #define VIA_REG_TYPE_INT_FLAG 0x01
108 #define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
109 #define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
110 #define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
111 #define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
112 #define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
114 #define DEFINE_VIA_REGSET(name,val) \
115 enum {\
116 VIA_REG_##name##_STATUS = (val),\
117 VIA_REG_##name##_CONTROL = (val) + 0x01,\
118 VIA_REG_##name##_TYPE = (val) + 0x02,\
119 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
120 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
121 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
122 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
125 /* modem block */
126 DEFINE_VIA_REGSET(MO, 0x40);
127 DEFINE_VIA_REGSET(MI, 0x50);
129 /* AC'97 */
130 #define VIA_REG_AC97 0x80 /* dword */
131 #define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
132 #define VIA_REG_AC97_CODEC_ID_SHIFT 30
133 #define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
134 #define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
135 #define VIA_REG_AC97_SECONDARY_VALID (1<<27)
136 #define VIA_REG_AC97_PRIMARY_VALID (1<<25)
137 #define VIA_REG_AC97_BUSY (1<<24)
138 #define VIA_REG_AC97_READ (1<<23)
139 #define VIA_REG_AC97_CMD_SHIFT 16
140 #define VIA_REG_AC97_CMD_MASK 0x7e
141 #define VIA_REG_AC97_DATA_SHIFT 0
142 #define VIA_REG_AC97_DATA_MASK 0xffff
144 #define VIA_REG_SGD_SHADOW 0x84 /* dword */
145 #define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
146 #define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
147 #define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
148 #define VIA_REG_SGD_STAT_PB_EOL (1<<4)
149 #define VIA_REG_SGD_STAT_CP_EOL (1<<5)
150 #define VIA_REG_SGD_STAT_FM_EOL (1<<6)
151 #define VIA_REG_SGD_STAT_PB_STOP (1<<8)
152 #define VIA_REG_SGD_STAT_CP_STOP (1<<9)
153 #define VIA_REG_SGD_STAT_FM_STOP (1<<10)
154 #define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
155 #define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
156 #define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
157 #define VIA_REG_SGD_STAT_MR_FLAG (1<<16)
158 #define VIA_REG_SGD_STAT_MW_FLAG (1<<17)
159 #define VIA_REG_SGD_STAT_MR_EOL (1<<20)
160 #define VIA_REG_SGD_STAT_MW_EOL (1<<21)
161 #define VIA_REG_SGD_STAT_MR_STOP (1<<24)
162 #define VIA_REG_SGD_STAT_MW_STOP (1<<25)
163 #define VIA_REG_SGD_STAT_MR_ACTIVE (1<<28)
164 #define VIA_REG_SGD_STAT_MW_ACTIVE (1<<29)
166 #define VIA_REG_GPI_STATUS 0x88
167 #define VIA_REG_GPI_INTR 0x8c
169 #define VIA_TBL_BIT_FLAG 0x40000000
170 #define VIA_TBL_BIT_EOL 0x80000000
172 /* pci space */
173 #define VIA_ACLINK_STAT 0x40
174 #define VIA_ACLINK_C11_READY 0x20
175 #define VIA_ACLINK_C10_READY 0x10
176 #define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
177 #define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
178 #define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
179 #define VIA_ACLINK_CTRL 0x41
180 #define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
181 #define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
182 #define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
183 #define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
184 #define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
185 #define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
186 #define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
187 #define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
188 #define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
189 VIA_ACLINK_CTRL_RESET|\
190 VIA_ACLINK_CTRL_PCM)
191 #define VIA_FUNC_ENABLE 0x42
192 #define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
193 #define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
194 #define VIA_FUNC_RX2C_WRITE 0x20
195 #define VIA_FUNC_SB_FIFO_EMPTY 0x10
196 #define VIA_FUNC_ENABLE_GAME 0x08
197 #define VIA_FUNC_ENABLE_FM 0x04
198 #define VIA_FUNC_ENABLE_MIDI 0x02
199 #define VIA_FUNC_ENABLE_SB 0x01
200 #define VIA_PNP_CONTROL 0x43
201 #define VIA_MC97_CTRL 0x44
202 #define VIA_MC97_CTRL_ENABLE 0x80
203 #define VIA_MC97_CTRL_SECONDARY 0x40
204 #define VIA_MC97_CTRL_INIT (VIA_MC97_CTRL_ENABLE|\
205 VIA_MC97_CTRL_SECONDARY)
209 * pcm stream
212 struct snd_via_sg_table {
213 unsigned int offset;
214 unsigned int size;
217 #define VIA_TABLE_SIZE 255
219 struct viadev {
220 unsigned int reg_offset;
221 unsigned long port;
222 int direction; /* playback = 0, capture = 1 */
223 struct snd_pcm_substream *substream;
224 int running;
225 unsigned int tbl_entries; /* # descriptors */
226 struct snd_dma_buffer table;
227 struct snd_via_sg_table *idx_table;
228 /* for recovery from the unexpected pointer */
229 unsigned int lastpos;
230 unsigned int bufsize;
231 unsigned int bufsize2;
234 enum { TYPE_CARD_VIA82XX_MODEM = 1 };
236 #define VIA_MAX_MODEM_DEVS 2
238 struct via82xx_modem {
239 int irq;
241 unsigned long port;
243 unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
245 struct pci_dev *pci;
246 struct snd_card *card;
248 unsigned int num_devs;
249 unsigned int playback_devno, capture_devno;
250 struct viadev devs[VIA_MAX_MODEM_DEVS];
252 struct snd_pcm *pcms[2];
254 struct snd_ac97_bus *ac97_bus;
255 struct snd_ac97 *ac97;
256 unsigned int ac97_clock;
257 unsigned int ac97_secondary; /* secondary AC'97 codec is present */
259 spinlock_t reg_lock;
260 struct snd_info_entry *proc_entry;
263 static struct pci_device_id snd_via82xx_modem_ids[] = {
264 { 0x1106, 0x3068, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA82XX_MODEM, },
265 { 0, }
268 MODULE_DEVICE_TABLE(pci, snd_via82xx_modem_ids);
274 * allocate and initialize the descriptor buffers
275 * periods = number of periods
276 * fragsize = period size in bytes
278 static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
279 struct pci_dev *pci,
280 unsigned int periods, unsigned int fragsize)
282 unsigned int i, idx, ofs, rest;
283 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
284 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
286 if (dev->table.area == NULL) {
287 /* the start of each lists must be aligned to 8 bytes,
288 * but the kernel pages are much bigger, so we don't care
290 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
291 PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
292 &dev->table) < 0)
293 return -ENOMEM;
295 if (! dev->idx_table) {
296 dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
297 if (! dev->idx_table)
298 return -ENOMEM;
301 /* fill the entries */
302 idx = 0;
303 ofs = 0;
304 for (i = 0; i < periods; i++) {
305 rest = fragsize;
306 /* fill descriptors for a period.
307 * a period can be split to several descriptors if it's
308 * over page boundary.
310 do {
311 unsigned int r;
312 unsigned int flag;
314 if (idx >= VIA_TABLE_SIZE) {
315 snd_printk(KERN_ERR "via82xx: too much table size!\n");
316 return -EINVAL;
318 ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
319 r = PAGE_SIZE - (ofs % PAGE_SIZE);
320 if (rest < r)
321 r = rest;
322 rest -= r;
323 if (! rest) {
324 if (i == periods - 1)
325 flag = VIA_TBL_BIT_EOL; /* buffer boundary */
326 else
327 flag = VIA_TBL_BIT_FLAG; /* period boundary */
328 } else
329 flag = 0; /* period continues to the next */
330 // printk("via: tbl %d: at %d size %d (rest %d)\n", idx, ofs, r, rest);
331 ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
332 dev->idx_table[idx].offset = ofs;
333 dev->idx_table[idx].size = r;
334 ofs += r;
335 idx++;
336 } while (rest > 0);
338 dev->tbl_entries = idx;
339 dev->bufsize = periods * fragsize;
340 dev->bufsize2 = dev->bufsize / 2;
341 return 0;
345 static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
346 struct pci_dev *pci)
348 if (dev->table.area) {
349 snd_dma_free_pages(&dev->table);
350 dev->table.area = NULL;
352 kfree(dev->idx_table);
353 dev->idx_table = NULL;
354 return 0;
358 * Basic I/O
361 static inline unsigned int snd_via82xx_codec_xread(struct via82xx_modem *chip)
363 return inl(VIAREG(chip, AC97));
366 static inline void snd_via82xx_codec_xwrite(struct via82xx_modem *chip, unsigned int val)
368 outl(val, VIAREG(chip, AC97));
371 static int snd_via82xx_codec_ready(struct via82xx_modem *chip, int secondary)
373 unsigned int timeout = 1000; /* 1ms */
374 unsigned int val;
376 while (timeout-- > 0) {
377 udelay(1);
378 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
379 return val & 0xffff;
381 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n",
382 secondary, snd_via82xx_codec_xread(chip));
383 return -EIO;
386 static int snd_via82xx_codec_valid(struct via82xx_modem *chip, int secondary)
388 unsigned int timeout = 1000; /* 1ms */
389 unsigned int val, val1;
390 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
391 VIA_REG_AC97_SECONDARY_VALID;
393 while (timeout-- > 0) {
394 val = snd_via82xx_codec_xread(chip);
395 val1 = val & (VIA_REG_AC97_BUSY | stat);
396 if (val1 == stat)
397 return val & 0xffff;
398 udelay(1);
400 return -EIO;
403 static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
405 struct via82xx_modem *chip = ac97->private_data;
406 int err;
407 err = snd_via82xx_codec_ready(chip, ac97->num);
408 /* here we need to wait fairly for long time.. */
409 msleep(500);
412 static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
413 unsigned short reg,
414 unsigned short val)
416 struct via82xx_modem *chip = ac97->private_data;
417 unsigned int xval;
418 if(reg == AC97_GPIO_STATUS) {
419 outl(val, VIAREG(chip, GPI_STATUS));
420 return;
422 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
423 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
424 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
425 xval |= val << VIA_REG_AC97_DATA_SHIFT;
426 snd_via82xx_codec_xwrite(chip, xval);
427 snd_via82xx_codec_ready(chip, ac97->num);
430 static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
432 struct via82xx_modem *chip = ac97->private_data;
433 unsigned int xval, val = 0xffff;
434 int again = 0;
436 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
437 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
438 xval |= VIA_REG_AC97_READ;
439 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
440 while (1) {
441 if (again++ > 3) {
442 snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n",
443 ac97->num, snd_via82xx_codec_xread(chip));
444 return 0xffff;
446 snd_via82xx_codec_xwrite(chip, xval);
447 udelay (20);
448 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
449 udelay(25);
450 val = snd_via82xx_codec_xread(chip);
451 break;
454 return val & 0xffff;
457 static void snd_via82xx_channel_reset(struct via82xx_modem *chip, struct viadev *viadev)
459 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
460 VIADEV_REG(viadev, OFFSET_CONTROL));
461 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
462 udelay(50);
463 /* disable interrupts */
464 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
465 /* clear interrupts */
466 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
467 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
468 // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
469 viadev->lastpos = 0;
474 * Interrupt handler
477 static irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id)
479 struct via82xx_modem *chip = dev_id;
480 unsigned int status;
481 unsigned int i;
483 status = inl(VIAREG(chip, SGD_SHADOW));
484 if (! (status & chip->intr_mask)) {
485 return IRQ_NONE;
487 // _skip_sgd:
489 /* check status for each stream */
490 spin_lock(&chip->reg_lock);
491 for (i = 0; i < chip->num_devs; i++) {
492 struct viadev *viadev = &chip->devs[i];
493 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
494 c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
495 if (! c_status)
496 continue;
497 if (viadev->substream && viadev->running) {
498 spin_unlock(&chip->reg_lock);
499 snd_pcm_period_elapsed(viadev->substream);
500 spin_lock(&chip->reg_lock);
502 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
504 spin_unlock(&chip->reg_lock);
505 return IRQ_HANDLED;
509 * PCM callbacks
513 * trigger callback
515 static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
517 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
518 struct viadev *viadev = substream->runtime->private_data;
519 unsigned char val = 0;
521 switch (cmd) {
522 case SNDRV_PCM_TRIGGER_START:
523 case SNDRV_PCM_TRIGGER_SUSPEND:
524 val |= VIA_REG_CTRL_START;
525 viadev->running = 1;
526 break;
527 case SNDRV_PCM_TRIGGER_STOP:
528 val = VIA_REG_CTRL_TERMINATE;
529 viadev->running = 0;
530 break;
531 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
532 val |= VIA_REG_CTRL_PAUSE;
533 viadev->running = 0;
534 break;
535 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
536 viadev->running = 1;
537 break;
538 default:
539 return -EINVAL;
541 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
542 if (cmd == SNDRV_PCM_TRIGGER_STOP)
543 snd_via82xx_channel_reset(chip, viadev);
544 return 0;
548 * pointer callbacks
552 * calculate the linear position at the given sg-buffer index and the rest count
555 #define check_invalid_pos(viadev,pos) \
556 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
557 viadev->lastpos < viadev->bufsize2))
559 static inline unsigned int calc_linear_pos(struct viadev *viadev, unsigned int idx,
560 unsigned int count)
562 unsigned int size, res;
564 size = viadev->idx_table[idx].size;
565 res = viadev->idx_table[idx].offset + size - count;
567 /* check the validity of the calculated position */
568 if (size < count) {
569 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
570 (int)size, (int)count);
571 res = viadev->lastpos;
572 } else if (check_invalid_pos(viadev, res)) {
573 #ifdef POINTER_DEBUG
574 printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, "
575 "bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, "
576 "count = 0x%x\n", idx, viadev->tbl_entries, viadev->lastpos,
577 viadev->bufsize2, viadev->idx_table[idx].offset,
578 viadev->idx_table[idx].size, count);
579 #endif
580 if (count && size < count) {
581 snd_printd(KERN_ERR "invalid via82xx_cur_ptr, "
582 "using last valid pointer\n");
583 res = viadev->lastpos;
584 } else {
585 if (! count)
586 /* bogus count 0 on the DMA boundary? */
587 res = viadev->idx_table[idx].offset;
588 else
589 /* count register returns full size
590 * when end of buffer is reached
592 res = viadev->idx_table[idx].offset + size;
593 if (check_invalid_pos(viadev, res)) {
594 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), "
595 "using last valid pointer\n");
596 res = viadev->lastpos;
600 viadev->lastpos = res; /* remember the last position */
601 if (res >= viadev->bufsize)
602 res -= viadev->bufsize;
603 return res;
607 * get the current pointer on via686
609 static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
611 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
612 struct viadev *viadev = substream->runtime->private_data;
613 unsigned int idx, ptr, count, res;
615 snd_assert(viadev->tbl_entries, return 0);
616 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
617 return 0;
619 spin_lock(&chip->reg_lock);
620 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
621 /* The via686a does not have the current index register,
622 * so we need to calculate the index from CURR_PTR.
624 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
625 if (ptr <= (unsigned int)viadev->table.addr)
626 idx = 0;
627 else /* CURR_PTR holds the address + 8 */
628 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) %
629 viadev->tbl_entries;
630 res = calc_linear_pos(viadev, idx, count);
631 spin_unlock(&chip->reg_lock);
633 return bytes_to_frames(substream->runtime, res);
637 * hw_params callback:
638 * allocate the buffer and build up the buffer description table
640 static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
641 struct snd_pcm_hw_params *hw_params)
643 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
644 struct viadev *viadev = substream->runtime->private_data;
645 int err;
647 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
648 if (err < 0)
649 return err;
650 err = build_via_table(viadev, substream, chip->pci,
651 params_periods(hw_params),
652 params_period_bytes(hw_params));
653 if (err < 0)
654 return err;
656 snd_ac97_write(chip->ac97, AC97_LINE1_RATE, params_rate(hw_params));
657 snd_ac97_write(chip->ac97, AC97_LINE1_LEVEL, 0);
659 return 0;
663 * hw_free callback:
664 * clean up the buffer description table and release the buffer
666 static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
668 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
669 struct viadev *viadev = substream->runtime->private_data;
671 clean_via_table(viadev, substream, chip->pci);
672 snd_pcm_lib_free_pages(substream);
673 return 0;
678 * set up the table pointer
680 static void snd_via82xx_set_table_ptr(struct via82xx_modem *chip, struct viadev *viadev)
682 snd_via82xx_codec_ready(chip, chip->ac97_secondary);
683 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
684 udelay(20);
685 snd_via82xx_codec_ready(chip, chip->ac97_secondary);
689 * prepare callback for playback and capture
691 static int snd_via82xx_pcm_prepare(struct snd_pcm_substream *substream)
693 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
694 struct viadev *viadev = substream->runtime->private_data;
696 snd_via82xx_channel_reset(chip, viadev);
697 /* this must be set after channel_reset */
698 snd_via82xx_set_table_ptr(chip, viadev);
699 outb(VIA_REG_TYPE_AUTOSTART|VIA_REG_TYPE_INT_EOL|VIA_REG_TYPE_INT_FLAG,
700 VIADEV_REG(viadev, OFFSET_TYPE));
701 return 0;
705 * pcm hardware definition, identical for both playback and capture
707 static struct snd_pcm_hardware snd_via82xx_hw =
709 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
710 SNDRV_PCM_INFO_BLOCK_TRANSFER |
711 SNDRV_PCM_INFO_MMAP_VALID |
712 /* SNDRV_PCM_INFO_RESUME | */
713 SNDRV_PCM_INFO_PAUSE),
714 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
715 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
716 .rate_min = 8000,
717 .rate_max = 16000,
718 .channels_min = 1,
719 .channels_max = 1,
720 .buffer_bytes_max = 128 * 1024,
721 .period_bytes_min = 32,
722 .period_bytes_max = 128 * 1024,
723 .periods_min = 2,
724 .periods_max = VIA_TABLE_SIZE / 2,
725 .fifo_size = 0,
730 * open callback skeleton
732 static int snd_via82xx_modem_pcm_open(struct via82xx_modem *chip, struct viadev *viadev,
733 struct snd_pcm_substream *substream)
735 struct snd_pcm_runtime *runtime = substream->runtime;
736 int err;
737 static unsigned int rates[] = { 8000, 9600, 12000, 16000 };
738 static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
739 .count = ARRAY_SIZE(rates),
740 .list = rates,
741 .mask = 0,
744 runtime->hw = snd_via82xx_hw;
746 if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
747 &hw_constraints_rates)) < 0)
748 return err;
750 /* we may remove following constaint when we modify table entries
751 in interrupt */
752 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
753 return err;
755 runtime->private_data = viadev;
756 viadev->substream = substream;
758 return 0;
763 * open callback for playback
765 static int snd_via82xx_playback_open(struct snd_pcm_substream *substream)
767 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
768 struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
770 return snd_via82xx_modem_pcm_open(chip, viadev, substream);
774 * open callback for capture
776 static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
778 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
779 struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
781 return snd_via82xx_modem_pcm_open(chip, viadev, substream);
785 * close callback
787 static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
789 struct viadev *viadev = substream->runtime->private_data;
791 viadev->substream = NULL;
792 return 0;
796 /* via686 playback callbacks */
797 static struct snd_pcm_ops snd_via686_playback_ops = {
798 .open = snd_via82xx_playback_open,
799 .close = snd_via82xx_pcm_close,
800 .ioctl = snd_pcm_lib_ioctl,
801 .hw_params = snd_via82xx_hw_params,
802 .hw_free = snd_via82xx_hw_free,
803 .prepare = snd_via82xx_pcm_prepare,
804 .trigger = snd_via82xx_pcm_trigger,
805 .pointer = snd_via686_pcm_pointer,
806 .page = snd_pcm_sgbuf_ops_page,
809 /* via686 capture callbacks */
810 static struct snd_pcm_ops snd_via686_capture_ops = {
811 .open = snd_via82xx_capture_open,
812 .close = snd_via82xx_pcm_close,
813 .ioctl = snd_pcm_lib_ioctl,
814 .hw_params = snd_via82xx_hw_params,
815 .hw_free = snd_via82xx_hw_free,
816 .prepare = snd_via82xx_pcm_prepare,
817 .trigger = snd_via82xx_pcm_trigger,
818 .pointer = snd_via686_pcm_pointer,
819 .page = snd_pcm_sgbuf_ops_page,
823 static void init_viadev(struct via82xx_modem *chip, int idx, unsigned int reg_offset,
824 int direction)
826 chip->devs[idx].reg_offset = reg_offset;
827 chip->devs[idx].direction = direction;
828 chip->devs[idx].port = chip->port + reg_offset;
832 * create a pcm instance for via686a/b
834 static int __devinit snd_via686_pcm_new(struct via82xx_modem *chip)
836 struct snd_pcm *pcm;
837 int err;
839 chip->playback_devno = 0;
840 chip->capture_devno = 1;
841 chip->num_devs = 2;
842 chip->intr_mask = 0x330000; /* FLAGS | EOL for MR, MW */
844 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
845 if (err < 0)
846 return err;
847 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
848 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
849 pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
850 pcm->private_data = chip;
851 strcpy(pcm->name, chip->card->shortname);
852 chip->pcms[0] = pcm;
853 init_viadev(chip, 0, VIA_REG_MO_STATUS, 0);
854 init_viadev(chip, 1, VIA_REG_MI_STATUS, 1);
856 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
857 snd_dma_pci_data(chip->pci),
858 64*1024, 128*1024)) < 0)
859 return err;
861 return 0;
866 * Mixer part
870 static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
872 struct via82xx_modem *chip = bus->private_data;
873 chip->ac97_bus = NULL;
876 static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
878 struct via82xx_modem *chip = ac97->private_data;
879 chip->ac97 = NULL;
883 static int __devinit snd_via82xx_mixer_new(struct via82xx_modem *chip)
885 struct snd_ac97_template ac97;
886 int err;
887 static struct snd_ac97_bus_ops ops = {
888 .write = snd_via82xx_codec_write,
889 .read = snd_via82xx_codec_read,
890 .wait = snd_via82xx_codec_wait,
893 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
894 return err;
895 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
896 chip->ac97_bus->clock = chip->ac97_clock;
898 memset(&ac97, 0, sizeof(ac97));
899 ac97.private_data = chip;
900 ac97.private_free = snd_via82xx_mixer_free_ac97;
901 ac97.pci = chip->pci;
902 ac97.scaps = AC97_SCAP_SKIP_AUDIO | AC97_SCAP_POWER_SAVE;
903 ac97.num = chip->ac97_secondary;
905 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
906 return err;
908 return 0;
913 * proc interface
915 static void snd_via82xx_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
917 struct via82xx_modem *chip = entry->private_data;
918 int i;
920 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
921 for (i = 0; i < 0xa0; i += 4) {
922 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
926 static void __devinit snd_via82xx_proc_init(struct via82xx_modem *chip)
928 struct snd_info_entry *entry;
930 if (! snd_card_proc_new(chip->card, "via82xx", &entry))
931 snd_info_set_text_ops(entry, chip, snd_via82xx_proc_read);
938 static int snd_via82xx_chip_init(struct via82xx_modem *chip)
940 unsigned int val;
941 unsigned long end_time;
942 unsigned char pval;
944 pci_read_config_byte(chip->pci, VIA_MC97_CTRL, &pval);
945 if((pval & VIA_MC97_CTRL_INIT) != VIA_MC97_CTRL_INIT) {
946 pci_write_config_byte(chip->pci, 0x44, pval|VIA_MC97_CTRL_INIT);
947 udelay(100);
950 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
951 if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
952 /* deassert ACLink reset, force SYNC */
953 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
954 VIA_ACLINK_CTRL_ENABLE |
955 VIA_ACLINK_CTRL_RESET |
956 VIA_ACLINK_CTRL_SYNC);
957 udelay(100);
958 #if 1 /* FIXME: should we do full reset here for all chip models? */
959 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
960 udelay(100);
961 #else
962 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
963 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
964 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
965 udelay(2);
966 #endif
967 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
968 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
969 udelay(100);
972 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
973 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
974 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
975 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
976 udelay(100);
979 /* wait until codec ready */
980 end_time = jiffies + msecs_to_jiffies(750);
981 do {
982 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
983 if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
984 break;
985 schedule_timeout_uninterruptible(1);
986 } while (time_before(jiffies, end_time));
988 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
989 snd_printk(KERN_ERR "AC'97 codec is not ready [0x%x]\n", val);
991 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
992 VIA_REG_AC97_SECONDARY_VALID |
993 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
994 end_time = jiffies + msecs_to_jiffies(750);
995 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
996 VIA_REG_AC97_SECONDARY_VALID |
997 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
998 do {
999 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
1000 chip->ac97_secondary = 1;
1001 goto __ac97_ok2;
1003 schedule_timeout_uninterruptible(1);
1004 } while (time_before(jiffies, end_time));
1005 /* This is ok, the most of motherboards have only one codec */
1007 __ac97_ok2:
1009 /* route FM trap to IRQ, disable FM trap */
1010 // pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
1011 /* disable all GPI interrupts */
1012 outl(0, VIAREG(chip, GPI_INTR));
1014 return 0;
1017 #ifdef CONFIG_PM
1019 * power management
1021 static int snd_via82xx_suspend(struct pci_dev *pci, pm_message_t state)
1023 struct snd_card *card = pci_get_drvdata(pci);
1024 struct via82xx_modem *chip = card->private_data;
1025 int i;
1027 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1028 for (i = 0; i < 2; i++)
1029 snd_pcm_suspend_all(chip->pcms[i]);
1030 for (i = 0; i < chip->num_devs; i++)
1031 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1032 synchronize_irq(chip->irq);
1033 snd_ac97_suspend(chip->ac97);
1035 pci_disable_device(pci);
1036 pci_save_state(pci);
1037 pci_set_power_state(pci, pci_choose_state(pci, state));
1038 return 0;
1041 static int snd_via82xx_resume(struct pci_dev *pci)
1043 struct snd_card *card = pci_get_drvdata(pci);
1044 struct via82xx_modem *chip = card->private_data;
1045 int i;
1047 pci_set_power_state(pci, PCI_D0);
1048 pci_restore_state(pci);
1049 if (pci_enable_device(pci) < 0) {
1050 printk(KERN_ERR "via82xx-modem: pci_enable_device failed, "
1051 "disabling device\n");
1052 snd_card_disconnect(card);
1053 return -EIO;
1055 pci_set_master(pci);
1057 snd_via82xx_chip_init(chip);
1059 snd_ac97_resume(chip->ac97);
1061 for (i = 0; i < chip->num_devs; i++)
1062 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1064 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1065 return 0;
1067 #endif /* CONFIG_PM */
1069 static int snd_via82xx_free(struct via82xx_modem *chip)
1071 unsigned int i;
1073 if (chip->irq < 0)
1074 goto __end_hw;
1075 /* disable interrupts */
1076 for (i = 0; i < chip->num_devs; i++)
1077 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1079 __end_hw:
1080 if (chip->irq >= 0)
1081 free_irq(chip->irq, chip);
1082 pci_release_regions(chip->pci);
1083 pci_disable_device(chip->pci);
1084 kfree(chip);
1085 return 0;
1088 static int snd_via82xx_dev_free(struct snd_device *device)
1090 struct via82xx_modem *chip = device->device_data;
1091 return snd_via82xx_free(chip);
1094 static int __devinit snd_via82xx_create(struct snd_card *card,
1095 struct pci_dev *pci,
1096 int chip_type,
1097 int revision,
1098 unsigned int ac97_clock,
1099 struct via82xx_modem ** r_via)
1101 struct via82xx_modem *chip;
1102 int err;
1103 static struct snd_device_ops ops = {
1104 .dev_free = snd_via82xx_dev_free,
1107 if ((err = pci_enable_device(pci)) < 0)
1108 return err;
1110 if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
1111 pci_disable_device(pci);
1112 return -ENOMEM;
1115 spin_lock_init(&chip->reg_lock);
1116 chip->card = card;
1117 chip->pci = pci;
1118 chip->irq = -1;
1120 if ((err = pci_request_regions(pci, card->driver)) < 0) {
1121 kfree(chip);
1122 pci_disable_device(pci);
1123 return err;
1125 chip->port = pci_resource_start(pci, 0);
1126 if (request_irq(pci->irq, snd_via82xx_interrupt, IRQF_SHARED,
1127 card->driver, chip)) {
1128 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1129 snd_via82xx_free(chip);
1130 return -EBUSY;
1132 chip->irq = pci->irq;
1133 if (ac97_clock >= 8000 && ac97_clock <= 48000)
1134 chip->ac97_clock = ac97_clock;
1135 synchronize_irq(chip->irq);
1137 if ((err = snd_via82xx_chip_init(chip)) < 0) {
1138 snd_via82xx_free(chip);
1139 return err;
1142 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1143 snd_via82xx_free(chip);
1144 return err;
1147 /* The 8233 ac97 controller does not implement the master bit
1148 * in the pci command register. IMHO this is a violation of the PCI spec.
1149 * We call pci_set_master here because it does not hurt. */
1150 pci_set_master(pci);
1152 snd_card_set_dev(card, &pci->dev);
1154 *r_via = chip;
1155 return 0;
1159 static int __devinit snd_via82xx_probe(struct pci_dev *pci,
1160 const struct pci_device_id *pci_id)
1162 struct snd_card *card;
1163 struct via82xx_modem *chip;
1164 int chip_type = 0, card_type;
1165 unsigned int i;
1166 int err;
1168 card = snd_card_new(index, id, THIS_MODULE, 0);
1169 if (card == NULL)
1170 return -ENOMEM;
1172 card_type = pci_id->driver_data;
1173 switch (card_type) {
1174 case TYPE_CARD_VIA82XX_MODEM:
1175 strcpy(card->driver, "VIA82XX-MODEM");
1176 sprintf(card->shortname, "VIA 82XX modem");
1177 break;
1178 default:
1179 snd_printk(KERN_ERR "invalid card type %d\n", card_type);
1180 err = -EINVAL;
1181 goto __error;
1184 if ((err = snd_via82xx_create(card, pci, chip_type, pci->revision,
1185 ac97_clock, &chip)) < 0)
1186 goto __error;
1187 card->private_data = chip;
1188 if ((err = snd_via82xx_mixer_new(chip)) < 0)
1189 goto __error;
1191 if ((err = snd_via686_pcm_new(chip)) < 0 )
1192 goto __error;
1194 /* disable interrupts */
1195 for (i = 0; i < chip->num_devs; i++)
1196 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1198 sprintf(card->longname, "%s at 0x%lx, irq %d",
1199 card->shortname, chip->port, chip->irq);
1201 snd_via82xx_proc_init(chip);
1203 if ((err = snd_card_register(card)) < 0) {
1204 snd_card_free(card);
1205 return err;
1207 pci_set_drvdata(pci, card);
1208 return 0;
1210 __error:
1211 snd_card_free(card);
1212 return err;
1215 static void __devexit snd_via82xx_remove(struct pci_dev *pci)
1217 snd_card_free(pci_get_drvdata(pci));
1218 pci_set_drvdata(pci, NULL);
1221 static struct pci_driver driver = {
1222 .name = "VIA 82xx Modem",
1223 .id_table = snd_via82xx_modem_ids,
1224 .probe = snd_via82xx_probe,
1225 .remove = __devexit_p(snd_via82xx_remove),
1226 #ifdef CONFIG_PM
1227 .suspend = snd_via82xx_suspend,
1228 .resume = snd_via82xx_resume,
1229 #endif
1232 static int __init alsa_card_via82xx_init(void)
1234 return pci_register_driver(&driver);
1237 static void __exit alsa_card_via82xx_exit(void)
1239 pci_unregister_driver(&driver);
1242 module_init(alsa_card_via82xx_init)
1243 module_exit(alsa_card_via82xx_exit)