via82xx_modem.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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@smlink.com>
  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[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* Exclude the first card */
  54. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  55. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  56. static int ac97_clock[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 48000};
  57. module_param_array(index, int, NULL, 0444);
  58. MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
  59. module_param_array(id, charp, NULL, 0444);
  60. MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
  61. module_param_array(enable, bool, NULL, 0444);
  62. MODULE_PARM_DESC(enable, "Enable modem part of VIA 82xx bridge.");
  63. module_param_array(ac97_clock, int, NULL, 0444);
  64. MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
  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. typedef struct _snd_via82xx_modem via82xx_t;
  190. typedef struct via_dev viadev_t;
  191. /*
  192. * pcm stream
  193. */
  194. struct snd_via_sg_table {
  195. unsigned int offset;
  196. unsigned int size;
  197. } ;
  198. #define VIA_TABLE_SIZE 255
  199. struct via_dev {
  200. unsigned int reg_offset;
  201. unsigned long port;
  202. int direction; /* playback = 0, capture = 1 */
  203. snd_pcm_substream_t *substream;
  204. int running;
  205. unsigned int tbl_entries; /* # descriptors */
  206. struct snd_dma_buffer table;
  207. struct snd_via_sg_table *idx_table;
  208. /* for recovery from the unexpected pointer */
  209. unsigned int lastpos;
  210. unsigned int bufsize;
  211. unsigned int bufsize2;
  212. };
  213. enum { TYPE_CARD_VIA82XX_MODEM = 1 };
  214. #define VIA_MAX_MODEM_DEVS 2
  215. struct _snd_via82xx_modem {
  216. int irq;
  217. unsigned long port;
  218. unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
  219. struct pci_dev *pci;
  220. snd_card_t *card;
  221. unsigned int num_devs;
  222. unsigned int playback_devno, capture_devno;
  223. viadev_t devs[VIA_MAX_MODEM_DEVS];
  224. snd_pcm_t *pcms[2];
  225. ac97_bus_t *ac97_bus;
  226. ac97_t *ac97;
  227. unsigned int ac97_clock;
  228. unsigned int ac97_secondary; /* secondary AC'97 codec is present */
  229. spinlock_t reg_lock;
  230. snd_info_entry_t *proc_entry;
  231. };
  232. static struct pci_device_id snd_via82xx_modem_ids[] = {
  233. { 0x1106, 0x3068, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA82XX_MODEM, },
  234. { 0, }
  235. };
  236. MODULE_DEVICE_TABLE(pci, snd_via82xx_modem_ids);
  237. /*
  238. */
  239. /*
  240. * allocate and initialize the descriptor buffers
  241. * periods = number of periods
  242. * fragsize = period size in bytes
  243. */
  244. static int build_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
  245. struct pci_dev *pci,
  246. unsigned int periods, unsigned int fragsize)
  247. {
  248. unsigned int i, idx, ofs, rest;
  249. via82xx_t *chip = snd_pcm_substream_chip(substream);
  250. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  251. if (dev->table.area == NULL) {
  252. /* the start of each lists must be aligned to 8 bytes,
  253. * but the kernel pages are much bigger, so we don't care
  254. */
  255. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  256. PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
  257. &dev->table) < 0)
  258. return -ENOMEM;
  259. }
  260. if (! dev->idx_table) {
  261. dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
  262. if (! dev->idx_table)
  263. return -ENOMEM;
  264. }
  265. /* fill the entries */
  266. idx = 0;
  267. ofs = 0;
  268. for (i = 0; i < periods; i++) {
  269. rest = fragsize;
  270. /* fill descriptors for a period.
  271. * a period can be split to several descriptors if it's
  272. * over page boundary.
  273. */
  274. do {
  275. unsigned int r;
  276. unsigned int flag;
  277. if (idx >= VIA_TABLE_SIZE) {
  278. snd_printk(KERN_ERR "via82xx: too much table size!\n");
  279. return -EINVAL;
  280. }
  281. ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
  282. r = PAGE_SIZE - (ofs % PAGE_SIZE);
  283. if (rest < r)
  284. r = rest;
  285. rest -= r;
  286. if (! rest) {
  287. if (i == periods - 1)
  288. flag = VIA_TBL_BIT_EOL; /* buffer boundary */
  289. else
  290. flag = VIA_TBL_BIT_FLAG; /* period boundary */
  291. } else
  292. flag = 0; /* period continues to the next */
  293. // printk("via: tbl %d: at %d size %d (rest %d)\n", idx, ofs, r, rest);
  294. ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
  295. dev->idx_table[idx].offset = ofs;
  296. dev->idx_table[idx].size = r;
  297. ofs += r;
  298. idx++;
  299. } while (rest > 0);
  300. }
  301. dev->tbl_entries = idx;
  302. dev->bufsize = periods * fragsize;
  303. dev->bufsize2 = dev->bufsize / 2;
  304. return 0;
  305. }
  306. static int clean_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
  307. struct pci_dev *pci)
  308. {
  309. if (dev->table.area) {
  310. snd_dma_free_pages(&dev->table);
  311. dev->table.area = NULL;
  312. }
  313. kfree(dev->idx_table);
  314. dev->idx_table = NULL;
  315. return 0;
  316. }
  317. /*
  318. * Basic I/O
  319. */
  320. static inline unsigned int snd_via82xx_codec_xread(via82xx_t *chip)
  321. {
  322. return inl(VIAREG(chip, AC97));
  323. }
  324. static inline void snd_via82xx_codec_xwrite(via82xx_t *chip, unsigned int val)
  325. {
  326. outl(val, VIAREG(chip, AC97));
  327. }
  328. static int snd_via82xx_codec_ready(via82xx_t *chip, int secondary)
  329. {
  330. unsigned int timeout = 1000; /* 1ms */
  331. unsigned int val;
  332. while (timeout-- > 0) {
  333. udelay(1);
  334. if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
  335. return val & 0xffff;
  336. }
  337. snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_via82xx_codec_xread(chip));
  338. return -EIO;
  339. }
  340. static int snd_via82xx_codec_valid(via82xx_t *chip, int secondary)
  341. {
  342. unsigned int timeout = 1000; /* 1ms */
  343. unsigned int val, val1;
  344. unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
  345. VIA_REG_AC97_SECONDARY_VALID;
  346. while (timeout-- > 0) {
  347. val = snd_via82xx_codec_xread(chip);
  348. val1 = val & (VIA_REG_AC97_BUSY | stat);
  349. if (val1 == stat)
  350. return val & 0xffff;
  351. udelay(1);
  352. }
  353. return -EIO;
  354. }
  355. static void snd_via82xx_codec_wait(ac97_t *ac97)
  356. {
  357. via82xx_t *chip = ac97->private_data;
  358. int err;
  359. err = snd_via82xx_codec_ready(chip, ac97->num);
  360. /* here we need to wait fairly for long time.. */
  361. msleep(500);
  362. }
  363. static void snd_via82xx_codec_write(ac97_t *ac97,
  364. unsigned short reg,
  365. unsigned short val)
  366. {
  367. via82xx_t *chip = ac97->private_data;
  368. unsigned int xval;
  369. if(reg == AC97_GPIO_STATUS) {
  370. outl(val, VIAREG(chip, GPI_STATUS));
  371. return;
  372. }
  373. xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
  374. xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
  375. xval |= reg << VIA_REG_AC97_CMD_SHIFT;
  376. xval |= val << VIA_REG_AC97_DATA_SHIFT;
  377. snd_via82xx_codec_xwrite(chip, xval);
  378. snd_via82xx_codec_ready(chip, ac97->num);
  379. }
  380. static unsigned short snd_via82xx_codec_read(ac97_t *ac97, unsigned short reg)
  381. {
  382. via82xx_t *chip = ac97->private_data;
  383. unsigned int xval, val = 0xffff;
  384. int again = 0;
  385. xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
  386. xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
  387. xval |= VIA_REG_AC97_READ;
  388. xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
  389. while (1) {
  390. if (again++ > 3) {
  391. snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n", 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(via82xx_t *chip, viadev_t *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, struct pt_regs *regs)
  422. {
  423. via82xx_t *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. viadev_t *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(snd_pcm_substream_t * substream, int cmd)
  456. {
  457. via82xx_t *chip = snd_pcm_substream_chip(substream);
  458. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  459. unsigned char val = 0;
  460. switch (cmd) {
  461. case SNDRV_PCM_TRIGGER_START:
  462. val |= VIA_REG_CTRL_START;
  463. viadev->running = 1;
  464. break;
  465. case SNDRV_PCM_TRIGGER_STOP:
  466. val = VIA_REG_CTRL_TERMINATE;
  467. viadev->running = 0;
  468. break;
  469. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  470. val |= VIA_REG_CTRL_PAUSE;
  471. viadev->running = 0;
  472. break;
  473. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  474. viadev->running = 1;
  475. break;
  476. default:
  477. return -EINVAL;
  478. }
  479. outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
  480. if (cmd == SNDRV_PCM_TRIGGER_STOP)
  481. snd_via82xx_channel_reset(chip, viadev);
  482. return 0;
  483. }
  484. /*
  485. * pointer callbacks
  486. */
  487. /*
  488. * calculate the linear position at the given sg-buffer index and the rest count
  489. */
  490. #define check_invalid_pos(viadev,pos) \
  491. ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 || viadev->lastpos < viadev->bufsize2))
  492. static inline unsigned int calc_linear_pos(viadev_t *viadev, unsigned int idx, unsigned int count)
  493. {
  494. unsigned int size, res;
  495. size = viadev->idx_table[idx].size;
  496. res = viadev->idx_table[idx].offset + size - count;
  497. /* check the validity of the calculated position */
  498. if (size < count) {
  499. snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n", (int)size, (int)count);
  500. res = viadev->lastpos;
  501. } else if (check_invalid_pos(viadev, res)) {
  502. #ifdef POINTER_DEBUG
  503. printk("fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n", idx, viadev->tbl_entries, viadev->lastpos, viadev->bufsize2, viadev->idx_table[idx].offset, viadev->idx_table[idx].size, count);
  504. #endif
  505. if (count && size < count) {
  506. snd_printd(KERN_ERR "invalid via82xx_cur_ptr, using last valid pointer\n");
  507. res = viadev->lastpos;
  508. } else {
  509. if (! count)
  510. /* bogus count 0 on the DMA boundary? */
  511. res = viadev->idx_table[idx].offset;
  512. else
  513. /* count register returns full size when end of buffer is reached */
  514. res = viadev->idx_table[idx].offset + size;
  515. if (check_invalid_pos(viadev, res)) {
  516. snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), using last valid pointer\n");
  517. res = viadev->lastpos;
  518. }
  519. }
  520. }
  521. viadev->lastpos = res; /* remember the last position */
  522. if (res >= viadev->bufsize)
  523. res -= viadev->bufsize;
  524. return res;
  525. }
  526. /*
  527. * get the current pointer on via686
  528. */
  529. static snd_pcm_uframes_t snd_via686_pcm_pointer(snd_pcm_substream_t *substream)
  530. {
  531. via82xx_t *chip = snd_pcm_substream_chip(substream);
  532. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  533. unsigned int idx, ptr, count, res;
  534. snd_assert(viadev->tbl_entries, return 0);
  535. if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
  536. return 0;
  537. spin_lock(&chip->reg_lock);
  538. count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
  539. /* The via686a does not have the current index register,
  540. * so we need to calculate the index from CURR_PTR.
  541. */
  542. ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
  543. if (ptr <= (unsigned int)viadev->table.addr)
  544. idx = 0;
  545. else /* CURR_PTR holds the address + 8 */
  546. idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
  547. res = calc_linear_pos(viadev, idx, count);
  548. spin_unlock(&chip->reg_lock);
  549. return bytes_to_frames(substream->runtime, res);
  550. }
  551. /*
  552. * hw_params callback:
  553. * allocate the buffer and build up the buffer description table
  554. */
  555. static int snd_via82xx_hw_params(snd_pcm_substream_t * substream,
  556. snd_pcm_hw_params_t * hw_params)
  557. {
  558. via82xx_t *chip = snd_pcm_substream_chip(substream);
  559. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  560. int err;
  561. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  562. if (err < 0)
  563. return err;
  564. err = build_via_table(viadev, substream, chip->pci,
  565. params_periods(hw_params),
  566. params_period_bytes(hw_params));
  567. if (err < 0)
  568. return err;
  569. snd_ac97_write(chip->ac97, AC97_LINE1_RATE, params_rate(hw_params));
  570. snd_ac97_write(chip->ac97, AC97_LINE1_LEVEL, 0);
  571. return 0;
  572. }
  573. /*
  574. * hw_free callback:
  575. * clean up the buffer description table and release the buffer
  576. */
  577. static int snd_via82xx_hw_free(snd_pcm_substream_t * substream)
  578. {
  579. via82xx_t *chip = snd_pcm_substream_chip(substream);
  580. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  581. clean_via_table(viadev, substream, chip->pci);
  582. snd_pcm_lib_free_pages(substream);
  583. return 0;
  584. }
  585. /*
  586. * set up the table pointer
  587. */
  588. static void snd_via82xx_set_table_ptr(via82xx_t *chip, viadev_t *viadev)
  589. {
  590. snd_via82xx_codec_ready(chip, chip->ac97_secondary);
  591. outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
  592. udelay(20);
  593. snd_via82xx_codec_ready(chip, chip->ac97_secondary);
  594. }
  595. /*
  596. * prepare callback for playback and capture
  597. */
  598. static int snd_via82xx_pcm_prepare(snd_pcm_substream_t *substream)
  599. {
  600. via82xx_t *chip = snd_pcm_substream_chip(substream);
  601. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  602. snd_via82xx_channel_reset(chip, viadev);
  603. /* this must be set after channel_reset */
  604. snd_via82xx_set_table_ptr(chip, viadev);
  605. outb(VIA_REG_TYPE_AUTOSTART|VIA_REG_TYPE_INT_EOL|VIA_REG_TYPE_INT_FLAG,
  606. VIADEV_REG(viadev, OFFSET_TYPE));
  607. return 0;
  608. }
  609. /*
  610. * pcm hardware definition, identical for both playback and capture
  611. */
  612. static snd_pcm_hardware_t snd_via82xx_hw =
  613. {
  614. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  615. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  616. SNDRV_PCM_INFO_MMAP_VALID |
  617. SNDRV_PCM_INFO_RESUME |
  618. SNDRV_PCM_INFO_PAUSE),
  619. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  620. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
  621. .rate_min = 8000,
  622. .rate_max = 16000,
  623. .channels_min = 1,
  624. .channels_max = 1,
  625. .buffer_bytes_max = 128 * 1024,
  626. .period_bytes_min = 32,
  627. .period_bytes_max = 128 * 1024,
  628. .periods_min = 2,
  629. .periods_max = VIA_TABLE_SIZE / 2,
  630. .fifo_size = 0,
  631. };
  632. /*
  633. * open callback skeleton
  634. */
  635. static int snd_via82xx_modem_pcm_open(via82xx_t *chip, viadev_t *viadev, snd_pcm_substream_t * substream)
  636. {
  637. snd_pcm_runtime_t *runtime = substream->runtime;
  638. int err;
  639. static unsigned int rates[] = { 8000, 9600, 12000, 16000 };
  640. static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
  641. .count = ARRAY_SIZE(rates),
  642. .list = rates,
  643. .mask = 0,
  644. };
  645. runtime->hw = snd_via82xx_hw;
  646. if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates)) < 0)
  647. return err;
  648. /* we may remove following constaint when we modify table entries
  649. in interrupt */
  650. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  651. return err;
  652. runtime->private_data = viadev;
  653. viadev->substream = substream;
  654. return 0;
  655. }
  656. /*
  657. * open callback for playback
  658. */
  659. static int snd_via82xx_playback_open(snd_pcm_substream_t * substream)
  660. {
  661. via82xx_t *chip = snd_pcm_substream_chip(substream);
  662. viadev_t *viadev = &chip->devs[chip->playback_devno + substream->number];
  663. return snd_via82xx_modem_pcm_open(chip, viadev, substream);
  664. }
  665. /*
  666. * open callback for capture
  667. */
  668. static int snd_via82xx_capture_open(snd_pcm_substream_t * substream)
  669. {
  670. via82xx_t *chip = snd_pcm_substream_chip(substream);
  671. viadev_t *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
  672. return snd_via82xx_modem_pcm_open(chip, viadev, substream);
  673. }
  674. /*
  675. * close callback
  676. */
  677. static int snd_via82xx_pcm_close(snd_pcm_substream_t * substream)
  678. {
  679. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  680. viadev->substream = NULL;
  681. return 0;
  682. }
  683. /* via686 playback callbacks */
  684. static snd_pcm_ops_t snd_via686_playback_ops = {
  685. .open = snd_via82xx_playback_open,
  686. .close = snd_via82xx_pcm_close,
  687. .ioctl = snd_pcm_lib_ioctl,
  688. .hw_params = snd_via82xx_hw_params,
  689. .hw_free = snd_via82xx_hw_free,
  690. .prepare = snd_via82xx_pcm_prepare,
  691. .trigger = snd_via82xx_pcm_trigger,
  692. .pointer = snd_via686_pcm_pointer,
  693. .page = snd_pcm_sgbuf_ops_page,
  694. };
  695. /* via686 capture callbacks */
  696. static snd_pcm_ops_t snd_via686_capture_ops = {
  697. .open = snd_via82xx_capture_open,
  698. .close = snd_via82xx_pcm_close,
  699. .ioctl = snd_pcm_lib_ioctl,
  700. .hw_params = snd_via82xx_hw_params,
  701. .hw_free = snd_via82xx_hw_free,
  702. .prepare = snd_via82xx_pcm_prepare,
  703. .trigger = snd_via82xx_pcm_trigger,
  704. .pointer = snd_via686_pcm_pointer,
  705. .page = snd_pcm_sgbuf_ops_page,
  706. };
  707. static void init_viadev(via82xx_t *chip, int idx, unsigned int reg_offset, int direction)
  708. {
  709. chip->devs[idx].reg_offset = reg_offset;
  710. chip->devs[idx].direction = direction;
  711. chip->devs[idx].port = chip->port + reg_offset;
  712. }
  713. /*
  714. * create a pcm instance for via686a/b
  715. */
  716. static int __devinit snd_via686_pcm_new(via82xx_t *chip)
  717. {
  718. snd_pcm_t *pcm;
  719. int err;
  720. chip->playback_devno = 0;
  721. chip->capture_devno = 1;
  722. chip->num_devs = 2;
  723. chip->intr_mask = 0x330000; /* FLAGS | EOL for MR, MW */
  724. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
  725. if (err < 0)
  726. return err;
  727. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
  728. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
  729. pcm->private_data = chip;
  730. strcpy(pcm->name, chip->card->shortname);
  731. chip->pcms[0] = pcm;
  732. init_viadev(chip, 0, VIA_REG_MO_STATUS, 0);
  733. init_viadev(chip, 1, VIA_REG_MI_STATUS, 1);
  734. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  735. snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
  736. return err;
  737. return 0;
  738. }
  739. /*
  740. * Mixer part
  741. */
  742. static void snd_via82xx_mixer_free_ac97_bus(ac97_bus_t *bus)
  743. {
  744. via82xx_t *chip = bus->private_data;
  745. chip->ac97_bus = NULL;
  746. }
  747. static void snd_via82xx_mixer_free_ac97(ac97_t *ac97)
  748. {
  749. via82xx_t *chip = ac97->private_data;
  750. chip->ac97 = NULL;
  751. }
  752. static int __devinit snd_via82xx_mixer_new(via82xx_t *chip)
  753. {
  754. ac97_template_t ac97;
  755. int err;
  756. static ac97_bus_ops_t ops = {
  757. .write = snd_via82xx_codec_write,
  758. .read = snd_via82xx_codec_read,
  759. .wait = snd_via82xx_codec_wait,
  760. };
  761. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
  762. return err;
  763. chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
  764. chip->ac97_bus->clock = chip->ac97_clock;
  765. chip->ac97_bus->shared_type = AC97_SHARED_TYPE_VIA;
  766. memset(&ac97, 0, sizeof(ac97));
  767. ac97.private_data = chip;
  768. ac97.private_free = snd_via82xx_mixer_free_ac97;
  769. ac97.pci = chip->pci;
  770. ac97.scaps = AC97_SCAP_SKIP_AUDIO;
  771. ac97.num = chip->ac97_secondary;
  772. if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
  773. return err;
  774. return 0;
  775. }
  776. /*
  777. * proc interface
  778. */
  779. static void snd_via82xx_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
  780. {
  781. via82xx_t *chip = entry->private_data;
  782. int i;
  783. snd_iprintf(buffer, "%s\n\n", chip->card->longname);
  784. for (i = 0; i < 0xa0; i += 4) {
  785. snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
  786. }
  787. }
  788. static void __devinit snd_via82xx_proc_init(via82xx_t *chip)
  789. {
  790. snd_info_entry_t *entry;
  791. if (! snd_card_proc_new(chip->card, "via82xx", &entry))
  792. snd_info_set_text_ops(entry, chip, 1024, snd_via82xx_proc_read);
  793. }
  794. /*
  795. *
  796. */
  797. static int snd_via82xx_chip_init(via82xx_t *chip)
  798. {
  799. unsigned int val;
  800. unsigned long end_time;
  801. unsigned char pval;
  802. pci_read_config_byte(chip->pci, VIA_MC97_CTRL, &pval);
  803. if((pval & VIA_MC97_CTRL_INIT) != VIA_MC97_CTRL_INIT) {
  804. pci_write_config_byte(chip->pci, 0x44, pval|VIA_MC97_CTRL_INIT);
  805. udelay(100);
  806. }
  807. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  808. if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
  809. /* deassert ACLink reset, force SYNC */
  810. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  811. VIA_ACLINK_CTRL_ENABLE |
  812. VIA_ACLINK_CTRL_RESET |
  813. VIA_ACLINK_CTRL_SYNC);
  814. udelay(100);
  815. #if 1 /* FIXME: should we do full reset here for all chip models? */
  816. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
  817. udelay(100);
  818. #else
  819. /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
  820. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  821. VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
  822. udelay(2);
  823. #endif
  824. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  825. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  826. udelay(100);
  827. }
  828. pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
  829. if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
  830. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  831. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  832. udelay(100);
  833. }
  834. /* wait until codec ready */
  835. end_time = jiffies + msecs_to_jiffies(750);
  836. do {
  837. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  838. if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
  839. break;
  840. set_current_state(TASK_UNINTERRUPTIBLE);
  841. schedule_timeout(1);
  842. } while (time_before(jiffies, end_time));
  843. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
  844. snd_printk("AC'97 codec is not ready [0x%x]\n", val);
  845. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  846. VIA_REG_AC97_SECONDARY_VALID |
  847. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  848. end_time = jiffies + msecs_to_jiffies(750);
  849. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  850. VIA_REG_AC97_SECONDARY_VALID |
  851. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  852. do {
  853. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
  854. chip->ac97_secondary = 1;
  855. goto __ac97_ok2;
  856. }
  857. set_current_state(TASK_INTERRUPTIBLE);
  858. schedule_timeout(1);
  859. } while (time_before(jiffies, end_time));
  860. /* This is ok, the most of motherboards have only one codec */
  861. __ac97_ok2:
  862. /* route FM trap to IRQ, disable FM trap */
  863. // pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
  864. /* disable all GPI interrupts */
  865. outl(0, VIAREG(chip, GPI_INTR));
  866. return 0;
  867. }
  868. #ifdef CONFIG_PM
  869. /*
  870. * power management
  871. */
  872. static int snd_via82xx_suspend(snd_card_t *card, pm_message_t state)
  873. {
  874. via82xx_t *chip = card->pm_private_data;
  875. int i;
  876. for (i = 0; i < 2; i++)
  877. if (chip->pcms[i])
  878. snd_pcm_suspend_all(chip->pcms[i]);
  879. for (i = 0; i < chip->num_devs; i++)
  880. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  881. synchronize_irq(chip->irq);
  882. snd_ac97_suspend(chip->ac97);
  883. pci_set_power_state(chip->pci, 3);
  884. pci_disable_device(chip->pci);
  885. return 0;
  886. }
  887. static int snd_via82xx_resume(snd_card_t *card)
  888. {
  889. via82xx_t *chip = card->pm_private_data;
  890. int i;
  891. pci_enable_device(chip->pci);
  892. pci_set_power_state(chip->pci, 0);
  893. pci_set_master(chip->pci);
  894. snd_via82xx_chip_init(chip);
  895. snd_ac97_resume(chip->ac97);
  896. for (i = 0; i < chip->num_devs; i++)
  897. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  898. return 0;
  899. }
  900. #endif /* CONFIG_PM */
  901. static int snd_via82xx_free(via82xx_t *chip)
  902. {
  903. unsigned int i;
  904. if (chip->irq < 0)
  905. goto __end_hw;
  906. /* disable interrupts */
  907. for (i = 0; i < chip->num_devs; i++)
  908. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  909. synchronize_irq(chip->irq);
  910. __end_hw:
  911. if (chip->irq >= 0)
  912. free_irq(chip->irq, (void *)chip);
  913. pci_release_regions(chip->pci);
  914. pci_disable_device(chip->pci);
  915. kfree(chip);
  916. return 0;
  917. }
  918. static int snd_via82xx_dev_free(snd_device_t *device)
  919. {
  920. via82xx_t *chip = device->device_data;
  921. return snd_via82xx_free(chip);
  922. }
  923. static int __devinit snd_via82xx_create(snd_card_t * card,
  924. struct pci_dev *pci,
  925. int chip_type,
  926. int revision,
  927. unsigned int ac97_clock,
  928. via82xx_t ** r_via)
  929. {
  930. via82xx_t *chip;
  931. int err;
  932. static snd_device_ops_t ops = {
  933. .dev_free = snd_via82xx_dev_free,
  934. };
  935. if ((err = pci_enable_device(pci)) < 0)
  936. return err;
  937. if ((chip = kcalloc(1, sizeof(*chip), GFP_KERNEL)) == NULL) {
  938. pci_disable_device(pci);
  939. return -ENOMEM;
  940. }
  941. spin_lock_init(&chip->reg_lock);
  942. chip->card = card;
  943. chip->pci = pci;
  944. chip->irq = -1;
  945. if ((err = pci_request_regions(pci, card->driver)) < 0) {
  946. kfree(chip);
  947. pci_disable_device(pci);
  948. return err;
  949. }
  950. chip->port = pci_resource_start(pci, 0);
  951. if (request_irq(pci->irq, snd_via82xx_interrupt, SA_INTERRUPT|SA_SHIRQ,
  952. card->driver, (void *)chip)) {
  953. snd_printk("unable to grab IRQ %d\n", pci->irq);
  954. snd_via82xx_free(chip);
  955. return -EBUSY;
  956. }
  957. chip->irq = pci->irq;
  958. if (ac97_clock >= 8000 && ac97_clock <= 48000)
  959. chip->ac97_clock = ac97_clock;
  960. synchronize_irq(chip->irq);
  961. if ((err = snd_via82xx_chip_init(chip)) < 0) {
  962. snd_via82xx_free(chip);
  963. return err;
  964. }
  965. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  966. snd_via82xx_free(chip);
  967. return err;
  968. }
  969. /* The 8233 ac97 controller does not implement the master bit
  970. * in the pci command register. IMHO this is a violation of the PCI spec.
  971. * We call pci_set_master here because it does not hurt. */
  972. pci_set_master(pci);
  973. snd_card_set_dev(card, &pci->dev);
  974. *r_via = chip;
  975. return 0;
  976. }
  977. static int __devinit snd_via82xx_probe(struct pci_dev *pci,
  978. const struct pci_device_id *pci_id)
  979. {
  980. static int dev;
  981. snd_card_t *card;
  982. via82xx_t *chip;
  983. unsigned char revision;
  984. int chip_type = 0, card_type;
  985. unsigned int i;
  986. int err;
  987. if (dev >= SNDRV_CARDS)
  988. return -ENODEV;
  989. if (!enable[dev]) {
  990. dev++;
  991. return -ENOENT;
  992. }
  993. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  994. if (card == NULL)
  995. return -ENOMEM;
  996. card_type = pci_id->driver_data;
  997. pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
  998. switch (card_type) {
  999. case TYPE_CARD_VIA82XX_MODEM:
  1000. strcpy(card->driver, "VIA82XX-MODEM");
  1001. sprintf(card->shortname, "VIA 82XX modem");
  1002. break;
  1003. default:
  1004. snd_printk(KERN_ERR "invalid card type %d\n", card_type);
  1005. err = -EINVAL;
  1006. goto __error;
  1007. }
  1008. if ((err = snd_via82xx_create(card, pci, chip_type, revision, ac97_clock[dev], &chip)) < 0)
  1009. goto __error;
  1010. if ((err = snd_via82xx_mixer_new(chip)) < 0)
  1011. goto __error;
  1012. if ((err = snd_via686_pcm_new(chip)) < 0 )
  1013. goto __error;
  1014. snd_card_set_pm_callback(card, snd_via82xx_suspend, snd_via82xx_resume, chip);
  1015. /* disable interrupts */
  1016. for (i = 0; i < chip->num_devs; i++)
  1017. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  1018. sprintf(card->longname, "%s at 0x%lx, irq %d",
  1019. card->shortname, chip->port, chip->irq);
  1020. snd_via82xx_proc_init(chip);
  1021. if ((err = snd_card_register(card)) < 0) {
  1022. snd_card_free(card);
  1023. return err;
  1024. }
  1025. pci_set_drvdata(pci, card);
  1026. dev++;
  1027. return 0;
  1028. __error:
  1029. snd_card_free(card);
  1030. return err;
  1031. }
  1032. static void __devexit snd_via82xx_remove(struct pci_dev *pci)
  1033. {
  1034. snd_card_free(pci_get_drvdata(pci));
  1035. pci_set_drvdata(pci, NULL);
  1036. }
  1037. static struct pci_driver driver = {
  1038. .name = "VIA 82xx Modem",
  1039. .id_table = snd_via82xx_modem_ids,
  1040. .probe = snd_via82xx_probe,
  1041. .remove = __devexit_p(snd_via82xx_remove),
  1042. SND_PCI_PM_CALLBACKS
  1043. };
  1044. static int __init alsa_card_via82xx_init(void)
  1045. {
  1046. return pci_register_driver(&driver);
  1047. }
  1048. static void __exit alsa_card_via82xx_exit(void)
  1049. {
  1050. pci_unregister_driver(&driver);
  1051. }
  1052. module_init(alsa_card_via82xx_init)
  1053. module_exit(alsa_card_via82xx_exit)