via82xx_modem.c 34 KB

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