0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <linux/io.h>
0021 #include <linux/delay.h>
0022 #include <linux/interrupt.h>
0023 #include <linux/init.h>
0024 #include <linux/pci.h>
0025 #include <linux/slab.h>
0026 #include <linux/module.h>
0027 #include <sound/core.h>
0028 #include <sound/pcm.h>
0029 #include <sound/pcm_params.h>
0030 #include <sound/info.h>
0031 #include <sound/ac97_codec.h>
0032 #include <sound/initval.h>
0033
0034 #if 0
0035 #define POINTER_DEBUG
0036 #endif
0037
0038 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
0039 MODULE_DESCRIPTION("VIA VT82xx modem");
0040 MODULE_LICENSE("GPL");
0041
0042 static int index = -2;
0043 static char *id = SNDRV_DEFAULT_STR1;
0044 static int ac97_clock = 48000;
0045
0046 module_param(index, int, 0444);
0047 MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
0048 module_param(id, charp, 0444);
0049 MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
0050 module_param(ac97_clock, int, 0444);
0051 MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
0052
0053
0054 static bool enable;
0055 module_param(enable, bool, 0444);
0056
0057
0058
0059
0060
0061
0062 #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
0063 #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
0064
0065
0066 #define VIA_REG_OFFSET_STATUS 0x00
0067 #define VIA_REG_STAT_ACTIVE 0x80
0068 #define VIA_REG_STAT_PAUSED 0x40
0069 #define VIA_REG_STAT_TRIGGER_QUEUED 0x08
0070 #define VIA_REG_STAT_STOPPED 0x04
0071 #define VIA_REG_STAT_EOL 0x02
0072 #define VIA_REG_STAT_FLAG 0x01
0073 #define VIA_REG_OFFSET_CONTROL 0x01
0074 #define VIA_REG_CTRL_START 0x80
0075 #define VIA_REG_CTRL_TERMINATE 0x40
0076 #define VIA_REG_CTRL_AUTOSTART 0x20
0077 #define VIA_REG_CTRL_PAUSE 0x08
0078 #define VIA_REG_CTRL_INT_STOP 0x04
0079 #define VIA_REG_CTRL_INT_EOL 0x02
0080 #define VIA_REG_CTRL_INT_FLAG 0x01
0081 #define VIA_REG_CTRL_RESET 0x01
0082 #define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
0083 #define VIA_REG_OFFSET_TYPE 0x02
0084 #define VIA_REG_TYPE_AUTOSTART 0x80
0085 #define VIA_REG_TYPE_16BIT 0x20
0086 #define VIA_REG_TYPE_STEREO 0x10
0087 #define VIA_REG_TYPE_INT_LLINE 0x00
0088 #define VIA_REG_TYPE_INT_LSAMPLE 0x04
0089 #define VIA_REG_TYPE_INT_LESSONE 0x08
0090 #define VIA_REG_TYPE_INT_MASK 0x0c
0091 #define VIA_REG_TYPE_INT_EOL 0x02
0092 #define VIA_REG_TYPE_INT_FLAG 0x01
0093 #define VIA_REG_OFFSET_TABLE_PTR 0x04
0094 #define VIA_REG_OFFSET_CURR_PTR 0x04
0095 #define VIA_REG_OFFSET_STOP_IDX 0x08
0096 #define VIA_REG_OFFSET_CURR_COUNT 0x0c
0097 #define VIA_REG_OFFSET_CURR_INDEX 0x0f
0098
0099 #define DEFINE_VIA_REGSET(name,val) \
0100 enum {\
0101 VIA_REG_##name##_STATUS = (val),\
0102 VIA_REG_##name##_CONTROL = (val) + 0x01,\
0103 VIA_REG_##name##_TYPE = (val) + 0x02,\
0104 VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
0105 VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
0106 VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
0107 VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
0108 }
0109
0110
0111 DEFINE_VIA_REGSET(MO, 0x40);
0112 DEFINE_VIA_REGSET(MI, 0x50);
0113
0114
0115 #define VIA_REG_AC97 0x80
0116 #define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
0117 #define VIA_REG_AC97_CODEC_ID_SHIFT 30
0118 #define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
0119 #define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
0120 #define VIA_REG_AC97_SECONDARY_VALID (1<<27)
0121 #define VIA_REG_AC97_PRIMARY_VALID (1<<25)
0122 #define VIA_REG_AC97_BUSY (1<<24)
0123 #define VIA_REG_AC97_READ (1<<23)
0124 #define VIA_REG_AC97_CMD_SHIFT 16
0125 #define VIA_REG_AC97_CMD_MASK 0x7e
0126 #define VIA_REG_AC97_DATA_SHIFT 0
0127 #define VIA_REG_AC97_DATA_MASK 0xffff
0128
0129 #define VIA_REG_SGD_SHADOW 0x84
0130 #define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
0131 #define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
0132 #define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
0133 #define VIA_REG_SGD_STAT_PB_EOL (1<<4)
0134 #define VIA_REG_SGD_STAT_CP_EOL (1<<5)
0135 #define VIA_REG_SGD_STAT_FM_EOL (1<<6)
0136 #define VIA_REG_SGD_STAT_PB_STOP (1<<8)
0137 #define VIA_REG_SGD_STAT_CP_STOP (1<<9)
0138 #define VIA_REG_SGD_STAT_FM_STOP (1<<10)
0139 #define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
0140 #define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
0141 #define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
0142 #define VIA_REG_SGD_STAT_MR_FLAG (1<<16)
0143 #define VIA_REG_SGD_STAT_MW_FLAG (1<<17)
0144 #define VIA_REG_SGD_STAT_MR_EOL (1<<20)
0145 #define VIA_REG_SGD_STAT_MW_EOL (1<<21)
0146 #define VIA_REG_SGD_STAT_MR_STOP (1<<24)
0147 #define VIA_REG_SGD_STAT_MW_STOP (1<<25)
0148 #define VIA_REG_SGD_STAT_MR_ACTIVE (1<<28)
0149 #define VIA_REG_SGD_STAT_MW_ACTIVE (1<<29)
0150
0151 #define VIA_REG_GPI_STATUS 0x88
0152 #define VIA_REG_GPI_INTR 0x8c
0153
0154 #define VIA_TBL_BIT_FLAG 0x40000000
0155 #define VIA_TBL_BIT_EOL 0x80000000
0156
0157
0158 #define VIA_ACLINK_STAT 0x40
0159 #define VIA_ACLINK_C11_READY 0x20
0160 #define VIA_ACLINK_C10_READY 0x10
0161 #define VIA_ACLINK_C01_READY 0x04
0162 #define VIA_ACLINK_LOWPOWER 0x02
0163 #define VIA_ACLINK_C00_READY 0x01
0164 #define VIA_ACLINK_CTRL 0x41
0165 #define VIA_ACLINK_CTRL_ENABLE 0x80
0166 #define VIA_ACLINK_CTRL_RESET 0x40
0167 #define VIA_ACLINK_CTRL_SYNC 0x20
0168 #define VIA_ACLINK_CTRL_SDO 0x10
0169 #define VIA_ACLINK_CTRL_VRA 0x08
0170 #define VIA_ACLINK_CTRL_PCM 0x04
0171 #define VIA_ACLINK_CTRL_FM 0x02
0172 #define VIA_ACLINK_CTRL_SB 0x01
0173 #define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
0174 VIA_ACLINK_CTRL_RESET|\
0175 VIA_ACLINK_CTRL_PCM)
0176 #define VIA_FUNC_ENABLE 0x42
0177 #define VIA_FUNC_MIDI_PNP 0x80
0178 #define VIA_FUNC_MIDI_IRQMASK 0x40
0179 #define VIA_FUNC_RX2C_WRITE 0x20
0180 #define VIA_FUNC_SB_FIFO_EMPTY 0x10
0181 #define VIA_FUNC_ENABLE_GAME 0x08
0182 #define VIA_FUNC_ENABLE_FM 0x04
0183 #define VIA_FUNC_ENABLE_MIDI 0x02
0184 #define VIA_FUNC_ENABLE_SB 0x01
0185 #define VIA_PNP_CONTROL 0x43
0186 #define VIA_MC97_CTRL 0x44
0187 #define VIA_MC97_CTRL_ENABLE 0x80
0188 #define VIA_MC97_CTRL_SECONDARY 0x40
0189 #define VIA_MC97_CTRL_INIT (VIA_MC97_CTRL_ENABLE|\
0190 VIA_MC97_CTRL_SECONDARY)
0191
0192
0193
0194
0195
0196
0197 struct snd_via_sg_table {
0198 unsigned int offset;
0199 unsigned int size;
0200 } ;
0201
0202 #define VIA_TABLE_SIZE 255
0203
0204 struct viadev {
0205 unsigned int reg_offset;
0206 unsigned long port;
0207 int direction;
0208 struct snd_pcm_substream *substream;
0209 int running;
0210 unsigned int tbl_entries;
0211 struct snd_dma_buffer table;
0212 struct snd_via_sg_table *idx_table;
0213
0214 unsigned int lastpos;
0215 unsigned int bufsize;
0216 unsigned int bufsize2;
0217 };
0218
0219 enum { TYPE_CARD_VIA82XX_MODEM = 1 };
0220
0221 #define VIA_MAX_MODEM_DEVS 2
0222
0223 struct via82xx_modem {
0224 int irq;
0225
0226 unsigned long port;
0227
0228 unsigned int intr_mask;
0229
0230 struct pci_dev *pci;
0231 struct snd_card *card;
0232
0233 unsigned int num_devs;
0234 unsigned int playback_devno, capture_devno;
0235 struct viadev devs[VIA_MAX_MODEM_DEVS];
0236
0237 struct snd_pcm *pcms[2];
0238
0239 struct snd_ac97_bus *ac97_bus;
0240 struct snd_ac97 *ac97;
0241 unsigned int ac97_clock;
0242 unsigned int ac97_secondary;
0243
0244 spinlock_t reg_lock;
0245 struct snd_info_entry *proc_entry;
0246 };
0247
0248 static const struct pci_device_id snd_via82xx_modem_ids[] = {
0249 { PCI_VDEVICE(VIA, 0x3068), TYPE_CARD_VIA82XX_MODEM, },
0250 { 0, }
0251 };
0252
0253 MODULE_DEVICE_TABLE(pci, snd_via82xx_modem_ids);
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263 static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
0264 struct pci_dev *pci,
0265 unsigned int periods, unsigned int fragsize)
0266 {
0267 unsigned int i, idx, ofs, rest;
0268 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
0269 __le32 *pgtbl;
0270
0271 if (dev->table.area == NULL) {
0272
0273
0274
0275 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
0276 PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
0277 &dev->table) < 0)
0278 return -ENOMEM;
0279 }
0280 if (! dev->idx_table) {
0281 dev->idx_table = kmalloc_array(VIA_TABLE_SIZE,
0282 sizeof(*dev->idx_table),
0283 GFP_KERNEL);
0284 if (! dev->idx_table)
0285 return -ENOMEM;
0286 }
0287
0288
0289 idx = 0;
0290 ofs = 0;
0291 pgtbl = (__le32 *)dev->table.area;
0292 for (i = 0; i < periods; i++) {
0293 rest = fragsize;
0294
0295
0296
0297
0298 do {
0299 unsigned int r;
0300 unsigned int flag;
0301 unsigned int addr;
0302
0303 if (idx >= VIA_TABLE_SIZE) {
0304 dev_err(&pci->dev, "too much table size!\n");
0305 return -EINVAL;
0306 }
0307 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
0308 pgtbl[idx << 1] = cpu_to_le32(addr);
0309 r = PAGE_SIZE - (ofs % PAGE_SIZE);
0310 if (rest < r)
0311 r = rest;
0312 rest -= r;
0313 if (! rest) {
0314 if (i == periods - 1)
0315 flag = VIA_TBL_BIT_EOL;
0316 else
0317 flag = VIA_TBL_BIT_FLAG;
0318 } else
0319 flag = 0;
0320
0321
0322
0323
0324
0325 pgtbl[(idx<<1) + 1] = cpu_to_le32(r | flag);
0326 dev->idx_table[idx].offset = ofs;
0327 dev->idx_table[idx].size = r;
0328 ofs += r;
0329 idx++;
0330 } while (rest > 0);
0331 }
0332 dev->tbl_entries = idx;
0333 dev->bufsize = periods * fragsize;
0334 dev->bufsize2 = dev->bufsize / 2;
0335 return 0;
0336 }
0337
0338
0339 static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
0340 struct pci_dev *pci)
0341 {
0342 if (dev->table.area) {
0343 snd_dma_free_pages(&dev->table);
0344 dev->table.area = NULL;
0345 }
0346 kfree(dev->idx_table);
0347 dev->idx_table = NULL;
0348 return 0;
0349 }
0350
0351
0352
0353
0354
0355 static inline unsigned int snd_via82xx_codec_xread(struct via82xx_modem *chip)
0356 {
0357 return inl(VIAREG(chip, AC97));
0358 }
0359
0360 static inline void snd_via82xx_codec_xwrite(struct via82xx_modem *chip, unsigned int val)
0361 {
0362 outl(val, VIAREG(chip, AC97));
0363 }
0364
0365 static int snd_via82xx_codec_ready(struct via82xx_modem *chip, int secondary)
0366 {
0367 unsigned int timeout = 1000;
0368 unsigned int val;
0369
0370 while (timeout-- > 0) {
0371 udelay(1);
0372 val = snd_via82xx_codec_xread(chip);
0373 if (!(val & VIA_REG_AC97_BUSY))
0374 return val & 0xffff;
0375 }
0376 dev_err(chip->card->dev, "codec_ready: codec %i is not ready [0x%x]\n",
0377 secondary, snd_via82xx_codec_xread(chip));
0378 return -EIO;
0379 }
0380
0381 static int snd_via82xx_codec_valid(struct via82xx_modem *chip, int secondary)
0382 {
0383 unsigned int timeout = 1000;
0384 unsigned int val, val1;
0385 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
0386 VIA_REG_AC97_SECONDARY_VALID;
0387
0388 while (timeout-- > 0) {
0389 val = snd_via82xx_codec_xread(chip);
0390 val1 = val & (VIA_REG_AC97_BUSY | stat);
0391 if (val1 == stat)
0392 return val & 0xffff;
0393 udelay(1);
0394 }
0395 return -EIO;
0396 }
0397
0398 static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
0399 {
0400 struct via82xx_modem *chip = ac97->private_data;
0401 __always_unused int err;
0402 err = snd_via82xx_codec_ready(chip, ac97->num);
0403
0404 msleep(500);
0405 }
0406
0407 static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
0408 unsigned short reg,
0409 unsigned short val)
0410 {
0411 struct via82xx_modem *chip = ac97->private_data;
0412 unsigned int xval;
0413 if(reg == AC97_GPIO_STATUS) {
0414 outl(val, VIAREG(chip, GPI_STATUS));
0415 return;
0416 }
0417 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
0418 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
0419 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
0420 xval |= val << VIA_REG_AC97_DATA_SHIFT;
0421 snd_via82xx_codec_xwrite(chip, xval);
0422 snd_via82xx_codec_ready(chip, ac97->num);
0423 }
0424
0425 static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
0426 {
0427 struct via82xx_modem *chip = ac97->private_data;
0428 unsigned int xval, val = 0xffff;
0429 int again = 0;
0430
0431 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
0432 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
0433 xval |= VIA_REG_AC97_READ;
0434 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
0435 while (1) {
0436 if (again++ > 3) {
0437 dev_err(chip->card->dev,
0438 "codec_read: codec %i is not valid [0x%x]\n",
0439 ac97->num, snd_via82xx_codec_xread(chip));
0440 return 0xffff;
0441 }
0442 snd_via82xx_codec_xwrite(chip, xval);
0443 udelay (20);
0444 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
0445 udelay(25);
0446 val = snd_via82xx_codec_xread(chip);
0447 break;
0448 }
0449 }
0450 return val & 0xffff;
0451 }
0452
0453 static void snd_via82xx_channel_reset(struct via82xx_modem *chip, struct viadev *viadev)
0454 {
0455 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
0456 VIADEV_REG(viadev, OFFSET_CONTROL));
0457 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
0458 udelay(50);
0459
0460 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
0461
0462 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
0463 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE));
0464
0465 viadev->lastpos = 0;
0466 }
0467
0468
0469
0470
0471
0472
0473 static irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id)
0474 {
0475 struct via82xx_modem *chip = dev_id;
0476 unsigned int status;
0477 unsigned int i;
0478
0479 status = inl(VIAREG(chip, SGD_SHADOW));
0480 if (! (status & chip->intr_mask)) {
0481 return IRQ_NONE;
0482 }
0483
0484
0485
0486 spin_lock(&chip->reg_lock);
0487 for (i = 0; i < chip->num_devs; i++) {
0488 struct viadev *viadev = &chip->devs[i];
0489 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
0490 c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
0491 if (! c_status)
0492 continue;
0493 if (viadev->substream && viadev->running) {
0494 spin_unlock(&chip->reg_lock);
0495 snd_pcm_period_elapsed(viadev->substream);
0496 spin_lock(&chip->reg_lock);
0497 }
0498 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS));
0499 }
0500 spin_unlock(&chip->reg_lock);
0501 return IRQ_HANDLED;
0502 }
0503
0504
0505
0506
0507
0508
0509
0510
0511 static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
0512 {
0513 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
0514 struct viadev *viadev = substream->runtime->private_data;
0515 unsigned char val = 0;
0516
0517 switch (cmd) {
0518 case SNDRV_PCM_TRIGGER_START:
0519 case SNDRV_PCM_TRIGGER_SUSPEND:
0520 val |= VIA_REG_CTRL_START;
0521 viadev->running = 1;
0522 break;
0523 case SNDRV_PCM_TRIGGER_STOP:
0524 val = VIA_REG_CTRL_TERMINATE;
0525 viadev->running = 0;
0526 break;
0527 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0528 val |= VIA_REG_CTRL_PAUSE;
0529 viadev->running = 0;
0530 break;
0531 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0532 viadev->running = 1;
0533 break;
0534 default:
0535 return -EINVAL;
0536 }
0537 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
0538 if (cmd == SNDRV_PCM_TRIGGER_STOP)
0539 snd_via82xx_channel_reset(chip, viadev);
0540 return 0;
0541 }
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551 #define check_invalid_pos(viadev,pos) \
0552 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
0553 viadev->lastpos < viadev->bufsize2))
0554
0555 static inline unsigned int calc_linear_pos(struct via82xx_modem *chip,
0556 struct viadev *viadev,
0557 unsigned int idx,
0558 unsigned int count)
0559 {
0560 unsigned int size, res;
0561
0562 size = viadev->idx_table[idx].size;
0563 res = viadev->idx_table[idx].offset + size - count;
0564
0565
0566 if (size < count) {
0567 dev_err(chip->card->dev,
0568 "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
0569 (int)size, (int)count);
0570 res = viadev->lastpos;
0571 } else if (check_invalid_pos(viadev, res)) {
0572 #ifdef POINTER_DEBUG
0573 dev_dbg(chip->card->dev,
0574 "fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n",
0575 idx, viadev->tbl_entries, viadev->lastpos,
0576 viadev->bufsize2, viadev->idx_table[idx].offset,
0577 viadev->idx_table[idx].size, count);
0578 #endif
0579 if (count && size < count) {
0580 dev_dbg(chip->card->dev,
0581 "invalid via82xx_cur_ptr, using last valid pointer\n");
0582 res = viadev->lastpos;
0583 } else {
0584 if (! count)
0585
0586 res = viadev->idx_table[idx].offset;
0587 else
0588
0589
0590
0591 res = viadev->idx_table[idx].offset + size;
0592 if (check_invalid_pos(viadev, res)) {
0593 dev_dbg(chip->card->dev,
0594 "invalid via82xx_cur_ptr (2), using last valid pointer\n");
0595 res = viadev->lastpos;
0596 }
0597 }
0598 }
0599 viadev->lastpos = res;
0600 if (res >= viadev->bufsize)
0601 res -= viadev->bufsize;
0602 return res;
0603 }
0604
0605
0606
0607
0608 static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
0609 {
0610 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
0611 struct viadev *viadev = substream->runtime->private_data;
0612 unsigned int idx, ptr, count, res;
0613
0614 if (snd_BUG_ON(!viadev->tbl_entries))
0615 return 0;
0616 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
0617 return 0;
0618
0619 spin_lock(&chip->reg_lock);
0620 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
0621
0622
0623
0624 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
0625 if (ptr <= (unsigned int)viadev->table.addr)
0626 idx = 0;
0627 else
0628 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) %
0629 viadev->tbl_entries;
0630 res = calc_linear_pos(chip, viadev, idx, count);
0631 spin_unlock(&chip->reg_lock);
0632
0633 return bytes_to_frames(substream->runtime, res);
0634 }
0635
0636
0637
0638
0639
0640 static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
0641 struct snd_pcm_hw_params *hw_params)
0642 {
0643 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
0644 struct viadev *viadev = substream->runtime->private_data;
0645 int err;
0646
0647 err = build_via_table(viadev, substream, chip->pci,
0648 params_periods(hw_params),
0649 params_period_bytes(hw_params));
0650 if (err < 0)
0651 return err;
0652
0653 snd_ac97_write(chip->ac97, AC97_LINE1_RATE, params_rate(hw_params));
0654 snd_ac97_write(chip->ac97, AC97_LINE1_LEVEL, 0);
0655
0656 return 0;
0657 }
0658
0659
0660
0661
0662
0663 static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
0664 {
0665 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
0666 struct viadev *viadev = substream->runtime->private_data;
0667
0668 clean_via_table(viadev, substream, chip->pci);
0669 return 0;
0670 }
0671
0672
0673
0674
0675
0676 static void snd_via82xx_set_table_ptr(struct via82xx_modem *chip, struct viadev *viadev)
0677 {
0678 snd_via82xx_codec_ready(chip, chip->ac97_secondary);
0679 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
0680 udelay(20);
0681 snd_via82xx_codec_ready(chip, chip->ac97_secondary);
0682 }
0683
0684
0685
0686
0687 static int snd_via82xx_pcm_prepare(struct snd_pcm_substream *substream)
0688 {
0689 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
0690 struct viadev *viadev = substream->runtime->private_data;
0691
0692 snd_via82xx_channel_reset(chip, viadev);
0693
0694 snd_via82xx_set_table_ptr(chip, viadev);
0695 outb(VIA_REG_TYPE_AUTOSTART|VIA_REG_TYPE_INT_EOL|VIA_REG_TYPE_INT_FLAG,
0696 VIADEV_REG(viadev, OFFSET_TYPE));
0697 return 0;
0698 }
0699
0700
0701
0702
0703 static const struct snd_pcm_hardware snd_via82xx_hw =
0704 {
0705 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
0706 SNDRV_PCM_INFO_BLOCK_TRANSFER |
0707 SNDRV_PCM_INFO_MMAP_VALID |
0708
0709 SNDRV_PCM_INFO_PAUSE),
0710 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
0711 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
0712 .rate_min = 8000,
0713 .rate_max = 16000,
0714 .channels_min = 1,
0715 .channels_max = 1,
0716 .buffer_bytes_max = 128 * 1024,
0717 .period_bytes_min = 32,
0718 .period_bytes_max = 128 * 1024,
0719 .periods_min = 2,
0720 .periods_max = VIA_TABLE_SIZE / 2,
0721 .fifo_size = 0,
0722 };
0723
0724
0725
0726
0727
0728 static int snd_via82xx_modem_pcm_open(struct via82xx_modem *chip, struct viadev *viadev,
0729 struct snd_pcm_substream *substream)
0730 {
0731 struct snd_pcm_runtime *runtime = substream->runtime;
0732 int err;
0733 static const unsigned int rates[] = { 8000, 9600, 12000, 16000 };
0734 static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
0735 .count = ARRAY_SIZE(rates),
0736 .list = rates,
0737 .mask = 0,
0738 };
0739
0740 runtime->hw = snd_via82xx_hw;
0741
0742 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
0743 &hw_constraints_rates);
0744 if (err < 0)
0745 return err;
0746
0747
0748
0749 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
0750 if (err < 0)
0751 return err;
0752
0753 runtime->private_data = viadev;
0754 viadev->substream = substream;
0755
0756 return 0;
0757 }
0758
0759
0760
0761
0762
0763 static int snd_via82xx_playback_open(struct snd_pcm_substream *substream)
0764 {
0765 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
0766 struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
0767
0768 return snd_via82xx_modem_pcm_open(chip, viadev, substream);
0769 }
0770
0771
0772
0773
0774 static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
0775 {
0776 struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
0777 struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
0778
0779 return snd_via82xx_modem_pcm_open(chip, viadev, substream);
0780 }
0781
0782
0783
0784
0785 static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
0786 {
0787 struct viadev *viadev = substream->runtime->private_data;
0788
0789 viadev->substream = NULL;
0790 return 0;
0791 }
0792
0793
0794
0795 static const struct snd_pcm_ops snd_via686_playback_ops = {
0796 .open = snd_via82xx_playback_open,
0797 .close = snd_via82xx_pcm_close,
0798 .hw_params = snd_via82xx_hw_params,
0799 .hw_free = snd_via82xx_hw_free,
0800 .prepare = snd_via82xx_pcm_prepare,
0801 .trigger = snd_via82xx_pcm_trigger,
0802 .pointer = snd_via686_pcm_pointer,
0803 };
0804
0805
0806 static const struct snd_pcm_ops snd_via686_capture_ops = {
0807 .open = snd_via82xx_capture_open,
0808 .close = snd_via82xx_pcm_close,
0809 .hw_params = snd_via82xx_hw_params,
0810 .hw_free = snd_via82xx_hw_free,
0811 .prepare = snd_via82xx_pcm_prepare,
0812 .trigger = snd_via82xx_pcm_trigger,
0813 .pointer = snd_via686_pcm_pointer,
0814 };
0815
0816
0817 static void init_viadev(struct via82xx_modem *chip, int idx, unsigned int reg_offset,
0818 int direction)
0819 {
0820 chip->devs[idx].reg_offset = reg_offset;
0821 chip->devs[idx].direction = direction;
0822 chip->devs[idx].port = chip->port + reg_offset;
0823 }
0824
0825
0826
0827
0828 static int snd_via686_pcm_new(struct via82xx_modem *chip)
0829 {
0830 struct snd_pcm *pcm;
0831 int err;
0832
0833 chip->playback_devno = 0;
0834 chip->capture_devno = 1;
0835 chip->num_devs = 2;
0836 chip->intr_mask = 0x330000;
0837
0838 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
0839 if (err < 0)
0840 return err;
0841 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
0842 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
0843 pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
0844 pcm->private_data = chip;
0845 strcpy(pcm->name, chip->card->shortname);
0846 chip->pcms[0] = pcm;
0847 init_viadev(chip, 0, VIA_REG_MO_STATUS, 0);
0848 init_viadev(chip, 1, VIA_REG_MI_STATUS, 1);
0849
0850 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
0851 &chip->pci->dev, 64*1024, 128*1024);
0852 return 0;
0853 }
0854
0855
0856
0857
0858
0859
0860
0861 static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
0862 {
0863 struct via82xx_modem *chip = bus->private_data;
0864 chip->ac97_bus = NULL;
0865 }
0866
0867 static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
0868 {
0869 struct via82xx_modem *chip = ac97->private_data;
0870 chip->ac97 = NULL;
0871 }
0872
0873
0874 static int snd_via82xx_mixer_new(struct via82xx_modem *chip)
0875 {
0876 struct snd_ac97_template ac97;
0877 int err;
0878 static const struct snd_ac97_bus_ops ops = {
0879 .write = snd_via82xx_codec_write,
0880 .read = snd_via82xx_codec_read,
0881 .wait = snd_via82xx_codec_wait,
0882 };
0883
0884 err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
0885 if (err < 0)
0886 return err;
0887 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
0888 chip->ac97_bus->clock = chip->ac97_clock;
0889
0890 memset(&ac97, 0, sizeof(ac97));
0891 ac97.private_data = chip;
0892 ac97.private_free = snd_via82xx_mixer_free_ac97;
0893 ac97.pci = chip->pci;
0894 ac97.scaps = AC97_SCAP_SKIP_AUDIO | AC97_SCAP_POWER_SAVE;
0895 ac97.num = chip->ac97_secondary;
0896
0897 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
0898 if (err < 0)
0899 return err;
0900
0901 return 0;
0902 }
0903
0904
0905
0906
0907
0908 static void snd_via82xx_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
0909 {
0910 struct via82xx_modem *chip = entry->private_data;
0911 int i;
0912
0913 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
0914 for (i = 0; i < 0xa0; i += 4) {
0915 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
0916 }
0917 }
0918
0919 static void snd_via82xx_proc_init(struct via82xx_modem *chip)
0920 {
0921 snd_card_ro_proc_new(chip->card, "via82xx", chip,
0922 snd_via82xx_proc_read);
0923 }
0924
0925
0926
0927
0928
0929 static int snd_via82xx_chip_init(struct via82xx_modem *chip)
0930 {
0931 unsigned int val;
0932 unsigned long end_time;
0933 unsigned char pval;
0934
0935 pci_read_config_byte(chip->pci, VIA_MC97_CTRL, &pval);
0936 if((pval & VIA_MC97_CTRL_INIT) != VIA_MC97_CTRL_INIT) {
0937 pci_write_config_byte(chip->pci, 0x44, pval|VIA_MC97_CTRL_INIT);
0938 udelay(100);
0939 }
0940
0941 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
0942 if (! (pval & VIA_ACLINK_C00_READY)) {
0943
0944 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
0945 VIA_ACLINK_CTRL_ENABLE |
0946 VIA_ACLINK_CTRL_RESET |
0947 VIA_ACLINK_CTRL_SYNC);
0948 udelay(100);
0949 #if 1
0950 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
0951 udelay(100);
0952 #else
0953
0954 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
0955 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
0956 udelay(2);
0957 #endif
0958
0959 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
0960 udelay(100);
0961 }
0962
0963 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
0964 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
0965
0966 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
0967 udelay(100);
0968 }
0969
0970
0971 end_time = jiffies + msecs_to_jiffies(750);
0972 do {
0973 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
0974 if (pval & VIA_ACLINK_C00_READY)
0975 break;
0976 schedule_timeout_uninterruptible(1);
0977 } while (time_before(jiffies, end_time));
0978
0979 val = snd_via82xx_codec_xread(chip);
0980 if (val & VIA_REG_AC97_BUSY)
0981 dev_err(chip->card->dev,
0982 "AC'97 codec is not ready [0x%x]\n", val);
0983
0984 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
0985 VIA_REG_AC97_SECONDARY_VALID |
0986 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
0987 end_time = jiffies + msecs_to_jiffies(750);
0988 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
0989 VIA_REG_AC97_SECONDARY_VALID |
0990 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
0991 do {
0992 val = snd_via82xx_codec_xread(chip);
0993 if (val & VIA_REG_AC97_SECONDARY_VALID) {
0994 chip->ac97_secondary = 1;
0995 goto __ac97_ok2;
0996 }
0997 schedule_timeout_uninterruptible(1);
0998 } while (time_before(jiffies, end_time));
0999
1000
1001 __ac97_ok2:
1002
1003
1004
1005
1006 outl(0, VIAREG(chip, GPI_INTR));
1007
1008 return 0;
1009 }
1010
1011 #ifdef CONFIG_PM_SLEEP
1012
1013
1014
1015 static int snd_via82xx_suspend(struct device *dev)
1016 {
1017 struct snd_card *card = dev_get_drvdata(dev);
1018 struct via82xx_modem *chip = card->private_data;
1019 int i;
1020
1021 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1022 for (i = 0; i < chip->num_devs; i++)
1023 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1024 snd_ac97_suspend(chip->ac97);
1025 return 0;
1026 }
1027
1028 static int snd_via82xx_resume(struct device *dev)
1029 {
1030 struct snd_card *card = dev_get_drvdata(dev);
1031 struct via82xx_modem *chip = card->private_data;
1032 int i;
1033
1034 snd_via82xx_chip_init(chip);
1035
1036 snd_ac97_resume(chip->ac97);
1037
1038 for (i = 0; i < chip->num_devs; i++)
1039 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1040
1041 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1042 return 0;
1043 }
1044
1045 static SIMPLE_DEV_PM_OPS(snd_via82xx_pm, snd_via82xx_suspend, snd_via82xx_resume);
1046 #define SND_VIA82XX_PM_OPS &snd_via82xx_pm
1047 #else
1048 #define SND_VIA82XX_PM_OPS NULL
1049 #endif
1050
1051 static void snd_via82xx_free(struct snd_card *card)
1052 {
1053 struct via82xx_modem *chip = card->private_data;
1054 unsigned int i;
1055
1056
1057 for (i = 0; i < chip->num_devs; i++)
1058 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1059 }
1060
1061 static int snd_via82xx_create(struct snd_card *card,
1062 struct pci_dev *pci,
1063 int chip_type,
1064 int revision,
1065 unsigned int ac97_clock)
1066 {
1067 struct via82xx_modem *chip = card->private_data;
1068 int err;
1069
1070 err = pcim_enable_device(pci);
1071 if (err < 0)
1072 return err;
1073
1074 spin_lock_init(&chip->reg_lock);
1075 chip->card = card;
1076 chip->pci = pci;
1077 chip->irq = -1;
1078
1079 err = pci_request_regions(pci, card->driver);
1080 if (err < 0)
1081 return err;
1082 chip->port = pci_resource_start(pci, 0);
1083 if (devm_request_irq(&pci->dev, pci->irq, snd_via82xx_interrupt,
1084 IRQF_SHARED, KBUILD_MODNAME, chip)) {
1085 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
1086 return -EBUSY;
1087 }
1088 chip->irq = pci->irq;
1089 card->sync_irq = chip->irq;
1090 card->private_free = snd_via82xx_free;
1091 if (ac97_clock >= 8000 && ac97_clock <= 48000)
1092 chip->ac97_clock = ac97_clock;
1093
1094 err = snd_via82xx_chip_init(chip);
1095 if (err < 0)
1096 return err;
1097
1098
1099
1100
1101 pci_set_master(pci);
1102 return 0;
1103 }
1104
1105
1106 static int __snd_via82xx_probe(struct pci_dev *pci,
1107 const struct pci_device_id *pci_id)
1108 {
1109 struct snd_card *card;
1110 struct via82xx_modem *chip;
1111 int chip_type = 0, card_type;
1112 unsigned int i;
1113 int err;
1114
1115 err = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
1116 sizeof(*chip), &card);
1117 if (err < 0)
1118 return err;
1119 chip = card->private_data;
1120
1121 card_type = pci_id->driver_data;
1122 switch (card_type) {
1123 case TYPE_CARD_VIA82XX_MODEM:
1124 strcpy(card->driver, "VIA82XX-MODEM");
1125 sprintf(card->shortname, "VIA 82XX modem");
1126 break;
1127 default:
1128 dev_err(card->dev, "invalid card type %d\n", card_type);
1129 return -EINVAL;
1130 }
1131
1132 err = snd_via82xx_create(card, pci, chip_type, pci->revision,
1133 ac97_clock);
1134 if (err < 0)
1135 return err;
1136 err = snd_via82xx_mixer_new(chip);
1137 if (err < 0)
1138 return err;
1139
1140 err = snd_via686_pcm_new(chip);
1141 if (err < 0)
1142 return err;
1143
1144
1145 for (i = 0; i < chip->num_devs; i++)
1146 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1147
1148 sprintf(card->longname, "%s at 0x%lx, irq %d",
1149 card->shortname, chip->port, chip->irq);
1150
1151 snd_via82xx_proc_init(chip);
1152
1153 err = snd_card_register(card);
1154 if (err < 0)
1155 return err;
1156 pci_set_drvdata(pci, card);
1157 return 0;
1158 }
1159
1160 static int snd_via82xx_probe(struct pci_dev *pci,
1161 const struct pci_device_id *pci_id)
1162 {
1163 return snd_card_free_on_error(&pci->dev, __snd_via82xx_probe(pci, pci_id));
1164 }
1165
1166 static struct pci_driver via82xx_modem_driver = {
1167 .name = KBUILD_MODNAME,
1168 .id_table = snd_via82xx_modem_ids,
1169 .probe = snd_via82xx_probe,
1170 .driver = {
1171 .pm = SND_VIA82XX_PM_OPS,
1172 },
1173 };
1174
1175 module_pci_driver(via82xx_modem_driver);