via82xx_modem.c 34 KB

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