via82xx_modem.c 34 KB

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