via82xx_modem.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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 <perex@perex.cz>
  8. * Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
  9. * 2002 Takashi Iwai <tiwai@suse.de>
  10. */
  11. /*
  12. * Changes:
  13. *
  14. * Sep. 2, 2004 Sasha Khapyorsky <sashak@alsa-project.org>
  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 <perex@perex.cz>");
  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. /*
  879. * power management
  880. */
  881. static int snd_via82xx_suspend(struct device *dev)
  882. {
  883. struct snd_card *card = dev_get_drvdata(dev);
  884. struct via82xx_modem *chip = card->private_data;
  885. int i;
  886. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  887. for (i = 0; i < chip->num_devs; i++)
  888. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  889. snd_ac97_suspend(chip->ac97);
  890. return 0;
  891. }
  892. static int snd_via82xx_resume(struct device *dev)
  893. {
  894. struct snd_card *card = dev_get_drvdata(dev);
  895. struct via82xx_modem *chip = card->private_data;
  896. int i;
  897. snd_via82xx_chip_init(chip);
  898. snd_ac97_resume(chip->ac97);
  899. for (i = 0; i < chip->num_devs; i++)
  900. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  901. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  902. return 0;
  903. }
  904. static DEFINE_SIMPLE_DEV_PM_OPS(snd_via82xx_pm, snd_via82xx_suspend, snd_via82xx_resume);
  905. static void snd_via82xx_free(struct snd_card *card)
  906. {
  907. struct via82xx_modem *chip = card->private_data;
  908. unsigned int i;
  909. /* disable interrupts */
  910. for (i = 0; i < chip->num_devs; i++)
  911. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  912. }
  913. static int snd_via82xx_create(struct snd_card *card,
  914. struct pci_dev *pci,
  915. int chip_type,
  916. int revision,
  917. unsigned int ac97_clock)
  918. {
  919. struct via82xx_modem *chip = card->private_data;
  920. int err;
  921. err = pcim_enable_device(pci);
  922. if (err < 0)
  923. return err;
  924. spin_lock_init(&chip->reg_lock);
  925. chip->card = card;
  926. chip->pci = pci;
  927. chip->irq = -1;
  928. err = pci_request_regions(pci, card->driver);
  929. if (err < 0)
  930. return err;
  931. chip->port = pci_resource_start(pci, 0);
  932. if (devm_request_irq(&pci->dev, pci->irq, snd_via82xx_interrupt,
  933. IRQF_SHARED, KBUILD_MODNAME, chip)) {
  934. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  935. return -EBUSY;
  936. }
  937. chip->irq = pci->irq;
  938. card->sync_irq = chip->irq;
  939. card->private_free = snd_via82xx_free;
  940. if (ac97_clock >= 8000 && ac97_clock <= 48000)
  941. chip->ac97_clock = ac97_clock;
  942. err = snd_via82xx_chip_init(chip);
  943. if (err < 0)
  944. return err;
  945. /* The 8233 ac97 controller does not implement the master bit
  946. * in the pci command register. IMHO this is a violation of the PCI spec.
  947. * We call pci_set_master here because it does not hurt. */
  948. pci_set_master(pci);
  949. return 0;
  950. }
  951. static int __snd_via82xx_probe(struct pci_dev *pci,
  952. const struct pci_device_id *pci_id)
  953. {
  954. struct snd_card *card;
  955. struct via82xx_modem *chip;
  956. int chip_type = 0, card_type;
  957. unsigned int i;
  958. int err;
  959. err = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
  960. sizeof(*chip), &card);
  961. if (err < 0)
  962. return err;
  963. chip = card->private_data;
  964. card_type = pci_id->driver_data;
  965. switch (card_type) {
  966. case TYPE_CARD_VIA82XX_MODEM:
  967. strcpy(card->driver, "VIA82XX-MODEM");
  968. sprintf(card->shortname, "VIA 82XX modem");
  969. break;
  970. default:
  971. dev_err(card->dev, "invalid card type %d\n", card_type);
  972. return -EINVAL;
  973. }
  974. err = snd_via82xx_create(card, pci, chip_type, pci->revision,
  975. ac97_clock);
  976. if (err < 0)
  977. return err;
  978. err = snd_via82xx_mixer_new(chip);
  979. if (err < 0)
  980. return err;
  981. err = snd_via686_pcm_new(chip);
  982. if (err < 0)
  983. return err;
  984. /* disable interrupts */
  985. for (i = 0; i < chip->num_devs; i++)
  986. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  987. sprintf(card->longname, "%s at 0x%lx, irq %d",
  988. card->shortname, chip->port, chip->irq);
  989. snd_via82xx_proc_init(chip);
  990. err = snd_card_register(card);
  991. if (err < 0)
  992. return err;
  993. pci_set_drvdata(pci, card);
  994. return 0;
  995. }
  996. static int snd_via82xx_probe(struct pci_dev *pci,
  997. const struct pci_device_id *pci_id)
  998. {
  999. return snd_card_free_on_error(&pci->dev, __snd_via82xx_probe(pci, pci_id));
  1000. }
  1001. static struct pci_driver via82xx_modem_driver = {
  1002. .name = KBUILD_MODNAME,
  1003. .id_table = snd_via82xx_modem_ids,
  1004. .probe = snd_via82xx_probe,
  1005. .driver = {
  1006. .pm = &snd_via82xx_pm,
  1007. },
  1008. };
  1009. module_pci_driver(via82xx_modem_driver);