via82xx_modem.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. /*
  2. * ALSA modem driver for VIA VT82xx (South Bridge)
  3. *
  4. * VT82C686A/B/C, VT8233A/C, VT8235
  5. *
  6. * Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
  7. * Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
  8. * 2002 Takashi Iwai <tiwai@suse.de>
  9. *
  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.
  14. *
  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.
  19. *
  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
  23. *
  24. */
  25. /*
  26. * Changes:
  27. *
  28. * Sep. 2, 2004 Sasha Khapyorsky <sashak@alsa-project.org>
  29. * Modified from original audio driver 'via82xx.c' to support AC97
  30. * modems.
  31. */
  32. #include <sound/driver.h>
  33. #include <asm/io.h>
  34. #include <linux/delay.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/init.h>
  37. #include <linux/pci.h>
  38. #include <linux/slab.h>
  39. #include <linux/moduleparam.h>
  40. #include <sound/core.h>
  41. #include <sound/pcm.h>
  42. #include <sound/pcm_params.h>
  43. #include <sound/info.h>
  44. #include <sound/ac97_codec.h>
  45. #include <sound/initval.h>
  46. #if 0
  47. #define POINTER_DEBUG
  48. #endif
  49. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  50. MODULE_DESCRIPTION("VIA VT82xx modem");
  51. MODULE_LICENSE("GPL");
  52. MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C modem,pci}}");
  53. static int index = -2; /* Exclude the first card */
  54. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  55. static int ac97_clock = 48000;
  56. module_param(index, int, 0444);
  57. MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
  58. module_param(id, charp, 0444);
  59. MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
  60. module_param(ac97_clock, int, 0444);
  61. MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
  62. /* just for backward compatibility */
  63. static int enable;
  64. module_param(enable, bool, 0444);
  65. /*
  66. * Direct registers
  67. */
  68. #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
  69. #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
  70. /* common offsets */
  71. #define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
  72. #define VIA_REG_STAT_ACTIVE 0x80 /* RO */
  73. #define VIA_REG_STAT_PAUSED 0x40 /* RO */
  74. #define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
  75. #define VIA_REG_STAT_STOPPED 0x04 /* RWC */
  76. #define VIA_REG_STAT_EOL 0x02 /* RWC */
  77. #define VIA_REG_STAT_FLAG 0x01 /* RWC */
  78. #define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
  79. #define VIA_REG_CTRL_START 0x80 /* WO */
  80. #define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
  81. #define VIA_REG_CTRL_AUTOSTART 0x20
  82. #define VIA_REG_CTRL_PAUSE 0x08 /* RW */
  83. #define VIA_REG_CTRL_INT_STOP 0x04
  84. #define VIA_REG_CTRL_INT_EOL 0x02
  85. #define VIA_REG_CTRL_INT_FLAG 0x01
  86. #define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
  87. #define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
  88. #define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
  89. #define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
  90. #define VIA_REG_TYPE_16BIT 0x20 /* RW */
  91. #define VIA_REG_TYPE_STEREO 0x10 /* RW */
  92. #define VIA_REG_TYPE_INT_LLINE 0x00
  93. #define VIA_REG_TYPE_INT_LSAMPLE 0x04
  94. #define VIA_REG_TYPE_INT_LESSONE 0x08
  95. #define VIA_REG_TYPE_INT_MASK 0x0c
  96. #define VIA_REG_TYPE_INT_EOL 0x02
  97. #define VIA_REG_TYPE_INT_FLAG 0x01
  98. #define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
  99. #define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
  100. #define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
  101. #define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
  102. #define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
  103. #define DEFINE_VIA_REGSET(name,val) \
  104. enum {\
  105. VIA_REG_##name##_STATUS = (val),\
  106. VIA_REG_##name##_CONTROL = (val) + 0x01,\
  107. VIA_REG_##name##_TYPE = (val) + 0x02,\
  108. VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
  109. VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
  110. VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
  111. VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
  112. }
  113. /* modem block */
  114. DEFINE_VIA_REGSET(MO, 0x40);
  115. DEFINE_VIA_REGSET(MI, 0x50);
  116. /* AC'97 */
  117. #define VIA_REG_AC97 0x80 /* dword */
  118. #define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
  119. #define VIA_REG_AC97_CODEC_ID_SHIFT 30
  120. #define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
  121. #define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
  122. #define VIA_REG_AC97_SECONDARY_VALID (1<<27)
  123. #define VIA_REG_AC97_PRIMARY_VALID (1<<25)
  124. #define VIA_REG_AC97_BUSY (1<<24)
  125. #define VIA_REG_AC97_READ (1<<23)
  126. #define VIA_REG_AC97_CMD_SHIFT 16
  127. #define VIA_REG_AC97_CMD_MASK 0x7e
  128. #define VIA_REG_AC97_DATA_SHIFT 0
  129. #define VIA_REG_AC97_DATA_MASK 0xffff
  130. #define VIA_REG_SGD_SHADOW 0x84 /* dword */
  131. #define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
  132. #define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
  133. #define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
  134. #define VIA_REG_SGD_STAT_PB_EOL (1<<4)
  135. #define VIA_REG_SGD_STAT_CP_EOL (1<<5)
  136. #define VIA_REG_SGD_STAT_FM_EOL (1<<6)
  137. #define VIA_REG_SGD_STAT_PB_STOP (1<<8)
  138. #define VIA_REG_SGD_STAT_CP_STOP (1<<9)
  139. #define VIA_REG_SGD_STAT_FM_STOP (1<<10)
  140. #define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
  141. #define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
  142. #define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
  143. #define VIA_REG_SGD_STAT_MR_FLAG (1<<16)
  144. #define VIA_REG_SGD_STAT_MW_FLAG (1<<17)
  145. #define VIA_REG_SGD_STAT_MR_EOL (1<<20)
  146. #define VIA_REG_SGD_STAT_MW_EOL (1<<21)
  147. #define VIA_REG_SGD_STAT_MR_STOP (1<<24)
  148. #define VIA_REG_SGD_STAT_MW_STOP (1<<25)
  149. #define VIA_REG_SGD_STAT_MR_ACTIVE (1<<28)
  150. #define VIA_REG_SGD_STAT_MW_ACTIVE (1<<29)
  151. #define VIA_REG_GPI_STATUS 0x88
  152. #define VIA_REG_GPI_INTR 0x8c
  153. #define VIA_TBL_BIT_FLAG 0x40000000
  154. #define VIA_TBL_BIT_EOL 0x80000000
  155. /* pci space */
  156. #define VIA_ACLINK_STAT 0x40
  157. #define VIA_ACLINK_C11_READY 0x20
  158. #define VIA_ACLINK_C10_READY 0x10
  159. #define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
  160. #define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
  161. #define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
  162. #define VIA_ACLINK_CTRL 0x41
  163. #define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
  164. #define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
  165. #define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
  166. #define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
  167. #define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
  168. #define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
  169. #define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
  170. #define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
  171. #define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
  172. VIA_ACLINK_CTRL_RESET|\
  173. VIA_ACLINK_CTRL_PCM)
  174. #define VIA_FUNC_ENABLE 0x42
  175. #define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
  176. #define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
  177. #define VIA_FUNC_RX2C_WRITE 0x20
  178. #define VIA_FUNC_SB_FIFO_EMPTY 0x10
  179. #define VIA_FUNC_ENABLE_GAME 0x08
  180. #define VIA_FUNC_ENABLE_FM 0x04
  181. #define VIA_FUNC_ENABLE_MIDI 0x02
  182. #define VIA_FUNC_ENABLE_SB 0x01
  183. #define VIA_PNP_CONTROL 0x43
  184. #define VIA_MC97_CTRL 0x44
  185. #define VIA_MC97_CTRL_ENABLE 0x80
  186. #define VIA_MC97_CTRL_SECONDARY 0x40
  187. #define VIA_MC97_CTRL_INIT (VIA_MC97_CTRL_ENABLE|\
  188. VIA_MC97_CTRL_SECONDARY)
  189. /*
  190. * pcm stream
  191. */
  192. struct snd_via_sg_table {
  193. unsigned int offset;
  194. unsigned int size;
  195. } ;
  196. #define VIA_TABLE_SIZE 255
  197. struct viadev {
  198. unsigned int reg_offset;
  199. unsigned long port;
  200. int direction; /* playback = 0, capture = 1 */
  201. struct snd_pcm_substream *substream;
  202. int running;
  203. unsigned int tbl_entries; /* # descriptors */
  204. struct snd_dma_buffer table;
  205. struct snd_via_sg_table *idx_table;
  206. /* for recovery from the unexpected pointer */
  207. unsigned int lastpos;
  208. unsigned int bufsize;
  209. unsigned int bufsize2;
  210. };
  211. enum { TYPE_CARD_VIA82XX_MODEM = 1 };
  212. #define VIA_MAX_MODEM_DEVS 2
  213. struct via82xx_modem {
  214. int irq;
  215. unsigned long port;
  216. unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
  217. struct pci_dev *pci;
  218. struct snd_card *card;
  219. unsigned int num_devs;
  220. unsigned int playback_devno, capture_devno;
  221. struct viadev devs[VIA_MAX_MODEM_DEVS];
  222. struct snd_pcm *pcms[2];
  223. struct snd_ac97_bus *ac97_bus;
  224. struct snd_ac97 *ac97;
  225. unsigned int ac97_clock;
  226. unsigned int ac97_secondary; /* secondary AC'97 codec is present */
  227. spinlock_t reg_lock;
  228. struct snd_info_entry *proc_entry;
  229. };
  230. static struct pci_device_id snd_via82xx_modem_ids[] = {
  231. { 0x1106, 0x3068, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA82XX_MODEM, },
  232. { 0, }
  233. };
  234. MODULE_DEVICE_TABLE(pci, snd_via82xx_modem_ids);
  235. /*
  236. */
  237. /*
  238. * allocate and initialize the descriptor buffers
  239. * periods = number of periods
  240. * fragsize = period size in bytes
  241. */
  242. static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
  243. struct pci_dev *pci,
  244. unsigned int periods, unsigned int fragsize)
  245. {
  246. unsigned int i, idx, ofs, rest;
  247. struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
  248. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  249. if (dev->table.area == NULL) {
  250. /* the start of each lists must be aligned to 8 bytes,
  251. * but the kernel pages are much bigger, so we don't care
  252. */
  253. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  254. PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
  255. &dev->table) < 0)
  256. return -ENOMEM;
  257. }
  258. if (! dev->idx_table) {
  259. dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
  260. if (! dev->idx_table)
  261. return -ENOMEM;
  262. }
  263. /* fill the entries */
  264. idx = 0;
  265. ofs = 0;
  266. for (i = 0; i < periods; i++) {
  267. rest = fragsize;
  268. /* fill descriptors for a period.
  269. * a period can be split to several descriptors if it's
  270. * over page boundary.
  271. */
  272. do {
  273. unsigned int r;
  274. unsigned int flag;
  275. if (idx >= VIA_TABLE_SIZE) {
  276. snd_printk(KERN_ERR "via82xx: too much table size!\n");
  277. return -EINVAL;
  278. }
  279. ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
  280. r = PAGE_SIZE - (ofs % PAGE_SIZE);
  281. if (rest < r)
  282. r = rest;
  283. rest -= r;
  284. if (! rest) {
  285. if (i == periods - 1)
  286. flag = VIA_TBL_BIT_EOL; /* buffer boundary */
  287. else
  288. flag = VIA_TBL_BIT_FLAG; /* period boundary */
  289. } else
  290. flag = 0; /* period continues to the next */
  291. // printk("via: tbl %d: at %d size %d (rest %d)\n", idx, ofs, r, rest);
  292. ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
  293. dev->idx_table[idx].offset = ofs;
  294. dev->idx_table[idx].size = r;
  295. ofs += r;
  296. idx++;
  297. } while (rest > 0);
  298. }
  299. dev->tbl_entries = idx;
  300. dev->bufsize = periods * fragsize;
  301. dev->bufsize2 = dev->bufsize / 2;
  302. return 0;
  303. }
  304. static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
  305. struct pci_dev *pci)
  306. {
  307. if (dev->table.area) {
  308. snd_dma_free_pages(&dev->table);
  309. dev->table.area = NULL;
  310. }
  311. kfree(dev->idx_table);
  312. dev->idx_table = NULL;
  313. return 0;
  314. }
  315. /*
  316. * Basic I/O
  317. */
  318. static inline unsigned int snd_via82xx_codec_xread(struct via82xx_modem *chip)
  319. {
  320. return inl(VIAREG(chip, AC97));
  321. }
  322. static inline void snd_via82xx_codec_xwrite(struct via82xx_modem *chip, unsigned int val)
  323. {
  324. outl(val, VIAREG(chip, AC97));
  325. }
  326. static int snd_via82xx_codec_ready(struct via82xx_modem *chip, int secondary)
  327. {
  328. unsigned int timeout = 1000; /* 1ms */
  329. unsigned int val;
  330. while (timeout-- > 0) {
  331. udelay(1);
  332. if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
  333. return val & 0xffff;
  334. }
  335. snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n",
  336. secondary, snd_via82xx_codec_xread(chip));
  337. return -EIO;
  338. }
  339. static int snd_via82xx_codec_valid(struct via82xx_modem *chip, int secondary)
  340. {
  341. unsigned int timeout = 1000; /* 1ms */
  342. unsigned int val, val1;
  343. unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
  344. VIA_REG_AC97_SECONDARY_VALID;
  345. while (timeout-- > 0) {
  346. val = snd_via82xx_codec_xread(chip);
  347. val1 = val & (VIA_REG_AC97_BUSY | stat);
  348. if (val1 == stat)
  349. return val & 0xffff;
  350. udelay(1);
  351. }
  352. return -EIO;
  353. }
  354. static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
  355. {
  356. struct via82xx_modem *chip = ac97->private_data;
  357. int err;
  358. err = snd_via82xx_codec_ready(chip, ac97->num);
  359. /* here we need to wait fairly for long time.. */
  360. msleep(500);
  361. }
  362. static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
  363. unsigned short reg,
  364. unsigned short val)
  365. {
  366. struct via82xx_modem *chip = ac97->private_data;
  367. unsigned int xval;
  368. if(reg == AC97_GPIO_STATUS) {
  369. outl(val, VIAREG(chip, GPI_STATUS));
  370. return;
  371. }
  372. xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
  373. xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
  374. xval |= reg << VIA_REG_AC97_CMD_SHIFT;
  375. xval |= val << VIA_REG_AC97_DATA_SHIFT;
  376. snd_via82xx_codec_xwrite(chip, xval);
  377. snd_via82xx_codec_ready(chip, ac97->num);
  378. }
  379. static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
  380. {
  381. struct via82xx_modem *chip = ac97->private_data;
  382. unsigned int xval, val = 0xffff;
  383. int again = 0;
  384. xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
  385. xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
  386. xval |= VIA_REG_AC97_READ;
  387. xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
  388. while (1) {
  389. if (again++ > 3) {
  390. snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n",
  391. ac97->num, snd_via82xx_codec_xread(chip));
  392. return 0xffff;
  393. }
  394. snd_via82xx_codec_xwrite(chip, xval);
  395. udelay (20);
  396. if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
  397. udelay(25);
  398. val = snd_via82xx_codec_xread(chip);
  399. break;
  400. }
  401. }
  402. return val & 0xffff;
  403. }
  404. static void snd_via82xx_channel_reset(struct via82xx_modem *chip, struct viadev *viadev)
  405. {
  406. outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
  407. VIADEV_REG(viadev, OFFSET_CONTROL));
  408. inb(VIADEV_REG(viadev, OFFSET_CONTROL));
  409. udelay(50);
  410. /* disable interrupts */
  411. outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
  412. /* clear interrupts */
  413. outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
  414. outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
  415. // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
  416. viadev->lastpos = 0;
  417. }
  418. /*
  419. * Interrupt handler
  420. */
  421. static irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id)
  422. {
  423. struct via82xx_modem *chip = dev_id;
  424. unsigned int status;
  425. unsigned int i;
  426. status = inl(VIAREG(chip, SGD_SHADOW));
  427. if (! (status & chip->intr_mask)) {
  428. return IRQ_NONE;
  429. }
  430. // _skip_sgd:
  431. /* check status for each stream */
  432. spin_lock(&chip->reg_lock);
  433. for (i = 0; i < chip->num_devs; i++) {
  434. struct viadev *viadev = &chip->devs[i];
  435. unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
  436. c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
  437. if (! c_status)
  438. continue;
  439. if (viadev->substream && viadev->running) {
  440. spin_unlock(&chip->reg_lock);
  441. snd_pcm_period_elapsed(viadev->substream);
  442. spin_lock(&chip->reg_lock);
  443. }
  444. outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
  445. }
  446. spin_unlock(&chip->reg_lock);
  447. return IRQ_HANDLED;
  448. }
  449. /*
  450. * PCM callbacks
  451. */
  452. /*
  453. * trigger callback
  454. */
  455. static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  456. {
  457. struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
  458. struct viadev *viadev = substream->runtime->private_data;
  459. unsigned char val = 0;
  460. switch (cmd) {
  461. case SNDRV_PCM_TRIGGER_START:
  462. case SNDRV_PCM_TRIGGER_SUSPEND:
  463. val |= VIA_REG_CTRL_START;
  464. viadev->running = 1;
  465. break;
  466. case SNDRV_PCM_TRIGGER_STOP:
  467. val = VIA_REG_CTRL_TERMINATE;
  468. viadev->running = 0;
  469. break;
  470. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  471. val |= VIA_REG_CTRL_PAUSE;
  472. viadev->running = 0;
  473. break;
  474. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  475. viadev->running = 1;
  476. break;
  477. default:
  478. return -EINVAL;
  479. }
  480. outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
  481. if (cmd == SNDRV_PCM_TRIGGER_STOP)
  482. snd_via82xx_channel_reset(chip, viadev);
  483. return 0;
  484. }
  485. /*
  486. * pointer callbacks
  487. */
  488. /*
  489. * calculate the linear position at the given sg-buffer index and the rest count
  490. */
  491. #define check_invalid_pos(viadev,pos) \
  492. ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
  493. viadev->lastpos < viadev->bufsize2))
  494. static inline unsigned int calc_linear_pos(struct viadev *viadev, unsigned int idx,
  495. unsigned int count)
  496. {
  497. unsigned int size, res;
  498. size = viadev->idx_table[idx].size;
  499. res = viadev->idx_table[idx].offset + size - count;
  500. /* check the validity of the calculated position */
  501. if (size < count) {
  502. snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
  503. (int)size, (int)count);
  504. res = viadev->lastpos;
  505. } else if (check_invalid_pos(viadev, res)) {
  506. #ifdef POINTER_DEBUG
  507. printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, "
  508. "bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, "
  509. "count = 0x%x\n", idx, viadev->tbl_entries, viadev->lastpos,
  510. viadev->bufsize2, viadev->idx_table[idx].offset,
  511. viadev->idx_table[idx].size, count);
  512. #endif
  513. if (count && size < count) {
  514. snd_printd(KERN_ERR "invalid via82xx_cur_ptr, "
  515. "using last valid pointer\n");
  516. res = viadev->lastpos;
  517. } else {
  518. if (! count)
  519. /* bogus count 0 on the DMA boundary? */
  520. res = viadev->idx_table[idx].offset;
  521. else
  522. /* count register returns full size
  523. * when end of buffer is reached
  524. */
  525. res = viadev->idx_table[idx].offset + size;
  526. if (check_invalid_pos(viadev, res)) {
  527. snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), "
  528. "using last valid pointer\n");
  529. res = viadev->lastpos;
  530. }
  531. }
  532. }
  533. viadev->lastpos = res; /* remember the last position */
  534. if (res >= viadev->bufsize)
  535. res -= viadev->bufsize;
  536. return res;
  537. }
  538. /*
  539. * get the current pointer on via686
  540. */
  541. static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
  542. {
  543. struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
  544. struct viadev *viadev = substream->runtime->private_data;
  545. unsigned int idx, ptr, count, res;
  546. snd_assert(viadev->tbl_entries, return 0);
  547. if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
  548. return 0;
  549. spin_lock(&chip->reg_lock);
  550. count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
  551. /* The via686a does not have the current index register,
  552. * so we need to calculate the index from CURR_PTR.
  553. */
  554. ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
  555. if (ptr <= (unsigned int)viadev->table.addr)
  556. idx = 0;
  557. else /* CURR_PTR holds the address + 8 */
  558. idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) %
  559. viadev->tbl_entries;
  560. res = calc_linear_pos(viadev, idx, count);
  561. spin_unlock(&chip->reg_lock);
  562. return bytes_to_frames(substream->runtime, res);
  563. }
  564. /*
  565. * hw_params callback:
  566. * allocate the buffer and build up the buffer description table
  567. */
  568. static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
  569. struct snd_pcm_hw_params *hw_params)
  570. {
  571. struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
  572. struct viadev *viadev = substream->runtime->private_data;
  573. int err;
  574. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  575. if (err < 0)
  576. return err;
  577. err = build_via_table(viadev, substream, chip->pci,
  578. params_periods(hw_params),
  579. params_period_bytes(hw_params));
  580. if (err < 0)
  581. return err;
  582. snd_ac97_write(chip->ac97, AC97_LINE1_RATE, params_rate(hw_params));
  583. snd_ac97_write(chip->ac97, AC97_LINE1_LEVEL, 0);
  584. return 0;
  585. }
  586. /*
  587. * hw_free callback:
  588. * clean up the buffer description table and release the buffer
  589. */
  590. static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
  591. {
  592. struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
  593. struct viadev *viadev = substream->runtime->private_data;
  594. clean_via_table(viadev, substream, chip->pci);
  595. snd_pcm_lib_free_pages(substream);
  596. return 0;
  597. }
  598. /*
  599. * set up the table pointer
  600. */
  601. static void snd_via82xx_set_table_ptr(struct via82xx_modem *chip, struct viadev *viadev)
  602. {
  603. snd_via82xx_codec_ready(chip, chip->ac97_secondary);
  604. outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
  605. udelay(20);
  606. snd_via82xx_codec_ready(chip, chip->ac97_secondary);
  607. }
  608. /*
  609. * prepare callback for playback and capture
  610. */
  611. static int snd_via82xx_pcm_prepare(struct snd_pcm_substream *substream)
  612. {
  613. struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
  614. struct viadev *viadev = substream->runtime->private_data;
  615. snd_via82xx_channel_reset(chip, viadev);
  616. /* this must be set after channel_reset */
  617. snd_via82xx_set_table_ptr(chip, viadev);
  618. outb(VIA_REG_TYPE_AUTOSTART|VIA_REG_TYPE_INT_EOL|VIA_REG_TYPE_INT_FLAG,
  619. VIADEV_REG(viadev, OFFSET_TYPE));
  620. return 0;
  621. }
  622. /*
  623. * pcm hardware definition, identical for both playback and capture
  624. */
  625. static struct snd_pcm_hardware snd_via82xx_hw =
  626. {
  627. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  628. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  629. SNDRV_PCM_INFO_MMAP_VALID |
  630. /* SNDRV_PCM_INFO_RESUME | */
  631. SNDRV_PCM_INFO_PAUSE),
  632. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  633. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
  634. .rate_min = 8000,
  635. .rate_max = 16000,
  636. .channels_min = 1,
  637. .channels_max = 1,
  638. .buffer_bytes_max = 128 * 1024,
  639. .period_bytes_min = 32,
  640. .period_bytes_max = 128 * 1024,
  641. .periods_min = 2,
  642. .periods_max = VIA_TABLE_SIZE / 2,
  643. .fifo_size = 0,
  644. };
  645. /*
  646. * open callback skeleton
  647. */
  648. static int snd_via82xx_modem_pcm_open(struct via82xx_modem *chip, struct viadev *viadev,
  649. struct snd_pcm_substream *substream)
  650. {
  651. struct snd_pcm_runtime *runtime = substream->runtime;
  652. int err;
  653. static unsigned int rates[] = { 8000, 9600, 12000, 16000 };
  654. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  655. .count = ARRAY_SIZE(rates),
  656. .list = rates,
  657. .mask = 0,
  658. };
  659. runtime->hw = snd_via82xx_hw;
  660. if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  661. &hw_constraints_rates)) < 0)
  662. return err;
  663. /* we may remove following constaint when we modify table entries
  664. in interrupt */
  665. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  666. return err;
  667. runtime->private_data = viadev;
  668. viadev->substream = substream;
  669. return 0;
  670. }
  671. /*
  672. * open callback for playback
  673. */
  674. static int snd_via82xx_playback_open(struct snd_pcm_substream *substream)
  675. {
  676. struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
  677. struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
  678. return snd_via82xx_modem_pcm_open(chip, viadev, substream);
  679. }
  680. /*
  681. * open callback for capture
  682. */
  683. static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
  684. {
  685. struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
  686. struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
  687. return snd_via82xx_modem_pcm_open(chip, viadev, substream);
  688. }
  689. /*
  690. * close callback
  691. */
  692. static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
  693. {
  694. struct viadev *viadev = substream->runtime->private_data;
  695. viadev->substream = NULL;
  696. return 0;
  697. }
  698. /* via686 playback callbacks */
  699. static struct snd_pcm_ops snd_via686_playback_ops = {
  700. .open = snd_via82xx_playback_open,
  701. .close = snd_via82xx_pcm_close,
  702. .ioctl = snd_pcm_lib_ioctl,
  703. .hw_params = snd_via82xx_hw_params,
  704. .hw_free = snd_via82xx_hw_free,
  705. .prepare = snd_via82xx_pcm_prepare,
  706. .trigger = snd_via82xx_pcm_trigger,
  707. .pointer = snd_via686_pcm_pointer,
  708. .page = snd_pcm_sgbuf_ops_page,
  709. };
  710. /* via686 capture callbacks */
  711. static struct snd_pcm_ops snd_via686_capture_ops = {
  712. .open = snd_via82xx_capture_open,
  713. .close = snd_via82xx_pcm_close,
  714. .ioctl = snd_pcm_lib_ioctl,
  715. .hw_params = snd_via82xx_hw_params,
  716. .hw_free = snd_via82xx_hw_free,
  717. .prepare = snd_via82xx_pcm_prepare,
  718. .trigger = snd_via82xx_pcm_trigger,
  719. .pointer = snd_via686_pcm_pointer,
  720. .page = snd_pcm_sgbuf_ops_page,
  721. };
  722. static void init_viadev(struct via82xx_modem *chip, int idx, unsigned int reg_offset,
  723. int direction)
  724. {
  725. chip->devs[idx].reg_offset = reg_offset;
  726. chip->devs[idx].direction = direction;
  727. chip->devs[idx].port = chip->port + reg_offset;
  728. }
  729. /*
  730. * create a pcm instance for via686a/b
  731. */
  732. static int __devinit snd_via686_pcm_new(struct via82xx_modem *chip)
  733. {
  734. struct snd_pcm *pcm;
  735. int err;
  736. chip->playback_devno = 0;
  737. chip->capture_devno = 1;
  738. chip->num_devs = 2;
  739. chip->intr_mask = 0x330000; /* FLAGS | EOL for MR, MW */
  740. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
  741. if (err < 0)
  742. return err;
  743. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
  744. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
  745. pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
  746. pcm->private_data = chip;
  747. strcpy(pcm->name, chip->card->shortname);
  748. chip->pcms[0] = pcm;
  749. init_viadev(chip, 0, VIA_REG_MO_STATUS, 0);
  750. init_viadev(chip, 1, VIA_REG_MI_STATUS, 1);
  751. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  752. snd_dma_pci_data(chip->pci),
  753. 64*1024, 128*1024)) < 0)
  754. return err;
  755. return 0;
  756. }
  757. /*
  758. * Mixer part
  759. */
  760. static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
  761. {
  762. struct via82xx_modem *chip = bus->private_data;
  763. chip->ac97_bus = NULL;
  764. }
  765. static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
  766. {
  767. struct via82xx_modem *chip = ac97->private_data;
  768. chip->ac97 = NULL;
  769. }
  770. static int __devinit snd_via82xx_mixer_new(struct via82xx_modem *chip)
  771. {
  772. struct snd_ac97_template ac97;
  773. int err;
  774. static struct snd_ac97_bus_ops ops = {
  775. .write = snd_via82xx_codec_write,
  776. .read = snd_via82xx_codec_read,
  777. .wait = snd_via82xx_codec_wait,
  778. };
  779. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
  780. return err;
  781. chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
  782. chip->ac97_bus->clock = chip->ac97_clock;
  783. memset(&ac97, 0, sizeof(ac97));
  784. ac97.private_data = chip;
  785. ac97.private_free = snd_via82xx_mixer_free_ac97;
  786. ac97.pci = chip->pci;
  787. ac97.scaps = AC97_SCAP_SKIP_AUDIO | AC97_SCAP_POWER_SAVE;
  788. ac97.num = chip->ac97_secondary;
  789. if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
  790. return err;
  791. return 0;
  792. }
  793. /*
  794. * proc interface
  795. */
  796. static void snd_via82xx_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  797. {
  798. struct via82xx_modem *chip = entry->private_data;
  799. int i;
  800. snd_iprintf(buffer, "%s\n\n", chip->card->longname);
  801. for (i = 0; i < 0xa0; i += 4) {
  802. snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
  803. }
  804. }
  805. static void __devinit snd_via82xx_proc_init(struct via82xx_modem *chip)
  806. {
  807. struct snd_info_entry *entry;
  808. if (! snd_card_proc_new(chip->card, "via82xx", &entry))
  809. snd_info_set_text_ops(entry, chip, snd_via82xx_proc_read);
  810. }
  811. /*
  812. *
  813. */
  814. static int snd_via82xx_chip_init(struct via82xx_modem *chip)
  815. {
  816. unsigned int val;
  817. unsigned long end_time;
  818. unsigned char pval;
  819. pci_read_config_byte(chip->pci, VIA_MC97_CTRL, &pval);
  820. if((pval & VIA_MC97_CTRL_INIT) != VIA_MC97_CTRL_INIT) {
  821. pci_write_config_byte(chip->pci, 0x44, pval|VIA_MC97_CTRL_INIT);
  822. udelay(100);
  823. }
  824. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  825. if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
  826. /* deassert ACLink reset, force SYNC */
  827. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  828. VIA_ACLINK_CTRL_ENABLE |
  829. VIA_ACLINK_CTRL_RESET |
  830. VIA_ACLINK_CTRL_SYNC);
  831. udelay(100);
  832. #if 1 /* FIXME: should we do full reset here for all chip models? */
  833. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
  834. udelay(100);
  835. #else
  836. /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
  837. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  838. VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
  839. udelay(2);
  840. #endif
  841. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  842. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  843. udelay(100);
  844. }
  845. pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
  846. if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
  847. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  848. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  849. udelay(100);
  850. }
  851. /* wait until codec ready */
  852. end_time = jiffies + msecs_to_jiffies(750);
  853. do {
  854. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  855. if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
  856. break;
  857. schedule_timeout_uninterruptible(1);
  858. } while (time_before(jiffies, end_time));
  859. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
  860. snd_printk(KERN_ERR "AC'97 codec is not ready [0x%x]\n", val);
  861. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  862. VIA_REG_AC97_SECONDARY_VALID |
  863. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  864. end_time = jiffies + msecs_to_jiffies(750);
  865. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  866. VIA_REG_AC97_SECONDARY_VALID |
  867. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  868. do {
  869. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
  870. chip->ac97_secondary = 1;
  871. goto __ac97_ok2;
  872. }
  873. schedule_timeout_interruptible(1);
  874. } while (time_before(jiffies, end_time));
  875. /* This is ok, the most of motherboards have only one codec */
  876. __ac97_ok2:
  877. /* route FM trap to IRQ, disable FM trap */
  878. // pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
  879. /* disable all GPI interrupts */
  880. outl(0, VIAREG(chip, GPI_INTR));
  881. return 0;
  882. }
  883. #ifdef CONFIG_PM
  884. /*
  885. * power management
  886. */
  887. static int snd_via82xx_suspend(struct pci_dev *pci, pm_message_t state)
  888. {
  889. struct snd_card *card = pci_get_drvdata(pci);
  890. struct via82xx_modem *chip = card->private_data;
  891. int i;
  892. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  893. for (i = 0; i < 2; i++)
  894. snd_pcm_suspend_all(chip->pcms[i]);
  895. for (i = 0; i < chip->num_devs; i++)
  896. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  897. synchronize_irq(chip->irq);
  898. snd_ac97_suspend(chip->ac97);
  899. pci_disable_device(pci);
  900. pci_save_state(pci);
  901. pci_set_power_state(pci, pci_choose_state(pci, state));
  902. return 0;
  903. }
  904. static int snd_via82xx_resume(struct pci_dev *pci)
  905. {
  906. struct snd_card *card = pci_get_drvdata(pci);
  907. struct via82xx_modem *chip = card->private_data;
  908. int i;
  909. pci_set_power_state(pci, PCI_D0);
  910. pci_restore_state(pci);
  911. if (pci_enable_device(pci) < 0) {
  912. printk(KERN_ERR "via82xx-modem: pci_enable_device failed, "
  913. "disabling device\n");
  914. snd_card_disconnect(card);
  915. return -EIO;
  916. }
  917. pci_set_master(pci);
  918. snd_via82xx_chip_init(chip);
  919. snd_ac97_resume(chip->ac97);
  920. for (i = 0; i < chip->num_devs; i++)
  921. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  922. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  923. return 0;
  924. }
  925. #endif /* CONFIG_PM */
  926. static int snd_via82xx_free(struct via82xx_modem *chip)
  927. {
  928. unsigned int i;
  929. if (chip->irq < 0)
  930. goto __end_hw;
  931. /* disable interrupts */
  932. for (i = 0; i < chip->num_devs; i++)
  933. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  934. synchronize_irq(chip->irq);
  935. __end_hw:
  936. if (chip->irq >= 0)
  937. free_irq(chip->irq, chip);
  938. pci_release_regions(chip->pci);
  939. pci_disable_device(chip->pci);
  940. kfree(chip);
  941. return 0;
  942. }
  943. static int snd_via82xx_dev_free(struct snd_device *device)
  944. {
  945. struct via82xx_modem *chip = device->device_data;
  946. return snd_via82xx_free(chip);
  947. }
  948. static int __devinit snd_via82xx_create(struct snd_card *card,
  949. struct pci_dev *pci,
  950. int chip_type,
  951. int revision,
  952. unsigned int ac97_clock,
  953. struct via82xx_modem ** r_via)
  954. {
  955. struct via82xx_modem *chip;
  956. int err;
  957. static struct snd_device_ops ops = {
  958. .dev_free = snd_via82xx_dev_free,
  959. };
  960. if ((err = pci_enable_device(pci)) < 0)
  961. return err;
  962. if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
  963. pci_disable_device(pci);
  964. return -ENOMEM;
  965. }
  966. spin_lock_init(&chip->reg_lock);
  967. chip->card = card;
  968. chip->pci = pci;
  969. chip->irq = -1;
  970. if ((err = pci_request_regions(pci, card->driver)) < 0) {
  971. kfree(chip);
  972. pci_disable_device(pci);
  973. return err;
  974. }
  975. chip->port = pci_resource_start(pci, 0);
  976. if (request_irq(pci->irq, snd_via82xx_interrupt, IRQF_SHARED,
  977. card->driver, chip)) {
  978. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  979. snd_via82xx_free(chip);
  980. return -EBUSY;
  981. }
  982. chip->irq = pci->irq;
  983. if (ac97_clock >= 8000 && ac97_clock <= 48000)
  984. chip->ac97_clock = ac97_clock;
  985. synchronize_irq(chip->irq);
  986. if ((err = snd_via82xx_chip_init(chip)) < 0) {
  987. snd_via82xx_free(chip);
  988. return err;
  989. }
  990. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  991. snd_via82xx_free(chip);
  992. return err;
  993. }
  994. /* The 8233 ac97 controller does not implement the master bit
  995. * in the pci command register. IMHO this is a violation of the PCI spec.
  996. * We call pci_set_master here because it does not hurt. */
  997. pci_set_master(pci);
  998. snd_card_set_dev(card, &pci->dev);
  999. *r_via = chip;
  1000. return 0;
  1001. }
  1002. static int __devinit snd_via82xx_probe(struct pci_dev *pci,
  1003. const struct pci_device_id *pci_id)
  1004. {
  1005. struct snd_card *card;
  1006. struct via82xx_modem *chip;
  1007. unsigned char revision;
  1008. int chip_type = 0, card_type;
  1009. unsigned int i;
  1010. int err;
  1011. card = snd_card_new(index, id, THIS_MODULE, 0);
  1012. if (card == NULL)
  1013. return -ENOMEM;
  1014. card_type = pci_id->driver_data;
  1015. pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
  1016. switch (card_type) {
  1017. case TYPE_CARD_VIA82XX_MODEM:
  1018. strcpy(card->driver, "VIA82XX-MODEM");
  1019. sprintf(card->shortname, "VIA 82XX modem");
  1020. break;
  1021. default:
  1022. snd_printk(KERN_ERR "invalid card type %d\n", card_type);
  1023. err = -EINVAL;
  1024. goto __error;
  1025. }
  1026. if ((err = snd_via82xx_create(card, pci, chip_type, revision,
  1027. ac97_clock, &chip)) < 0)
  1028. goto __error;
  1029. card->private_data = chip;
  1030. if ((err = snd_via82xx_mixer_new(chip)) < 0)
  1031. goto __error;
  1032. if ((err = snd_via686_pcm_new(chip)) < 0 )
  1033. goto __error;
  1034. /* disable interrupts */
  1035. for (i = 0; i < chip->num_devs; i++)
  1036. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  1037. sprintf(card->longname, "%s at 0x%lx, irq %d",
  1038. card->shortname, chip->port, chip->irq);
  1039. snd_via82xx_proc_init(chip);
  1040. if ((err = snd_card_register(card)) < 0) {
  1041. snd_card_free(card);
  1042. return err;
  1043. }
  1044. pci_set_drvdata(pci, card);
  1045. return 0;
  1046. __error:
  1047. snd_card_free(card);
  1048. return err;
  1049. }
  1050. static void __devexit snd_via82xx_remove(struct pci_dev *pci)
  1051. {
  1052. snd_card_free(pci_get_drvdata(pci));
  1053. pci_set_drvdata(pci, NULL);
  1054. }
  1055. static struct pci_driver driver = {
  1056. .name = "VIA 82xx Modem",
  1057. .id_table = snd_via82xx_modem_ids,
  1058. .probe = snd_via82xx_probe,
  1059. .remove = __devexit_p(snd_via82xx_remove),
  1060. #ifdef CONFIG_PM
  1061. .suspend = snd_via82xx_suspend,
  1062. .resume = snd_via82xx_resume,
  1063. #endif
  1064. };
  1065. static int __init alsa_card_via82xx_init(void)
  1066. {
  1067. return pci_register_driver(&driver);
  1068. }
  1069. static void __exit alsa_card_via82xx_exit(void)
  1070. {
  1071. pci_unregister_driver(&driver);
  1072. }
  1073. module_init(alsa_card_via82xx_init)
  1074. module_exit(alsa_card_via82xx_exit)