via82xx_modem.c 32 KB

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