via82xx_modem.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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. kfree(dev->idx_table);
  314. dev->idx_table = NULL;
  315. return 0;
  316. }
  317. /*
  318. * Basic I/O
  319. */
  320. static inline unsigned int snd_via82xx_codec_xread(via82xx_t *chip)
  321. {
  322. return inl(VIAREG(chip, AC97));
  323. }
  324. static inline void snd_via82xx_codec_xwrite(via82xx_t *chip, unsigned int val)
  325. {
  326. outl(val, VIAREG(chip, AC97));
  327. }
  328. static int snd_via82xx_codec_ready(via82xx_t *chip, int secondary)
  329. {
  330. unsigned int timeout = 1000; /* 1ms */
  331. unsigned int val;
  332. while (timeout-- > 0) {
  333. udelay(1);
  334. if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
  335. return val & 0xffff;
  336. }
  337. snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_via82xx_codec_xread(chip));
  338. return -EIO;
  339. }
  340. static int snd_via82xx_codec_valid(via82xx_t *chip, int secondary)
  341. {
  342. unsigned int timeout = 1000; /* 1ms */
  343. unsigned int val, val1;
  344. unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
  345. VIA_REG_AC97_SECONDARY_VALID;
  346. while (timeout-- > 0) {
  347. val = snd_via82xx_codec_xread(chip);
  348. val1 = val & (VIA_REG_AC97_BUSY | stat);
  349. if (val1 == stat)
  350. return val & 0xffff;
  351. udelay(1);
  352. }
  353. return -EIO;
  354. }
  355. static void snd_via82xx_codec_wait(ac97_t *ac97)
  356. {
  357. via82xx_t *chip = ac97->private_data;
  358. int err;
  359. err = snd_via82xx_codec_ready(chip, ac97->num);
  360. /* here we need to wait fairly for long time.. */
  361. msleep(500);
  362. }
  363. static void snd_via82xx_codec_write(ac97_t *ac97,
  364. unsigned short reg,
  365. unsigned short val)
  366. {
  367. via82xx_t *chip = ac97->private_data;
  368. unsigned int xval;
  369. if(reg == AC97_GPIO_STATUS) {
  370. outl(val, VIAREG(chip, GPI_STATUS));
  371. return;
  372. }
  373. xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
  374. xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
  375. xval |= reg << VIA_REG_AC97_CMD_SHIFT;
  376. xval |= val << VIA_REG_AC97_DATA_SHIFT;
  377. snd_via82xx_codec_xwrite(chip, xval);
  378. snd_via82xx_codec_ready(chip, ac97->num);
  379. }
  380. static unsigned short snd_via82xx_codec_read(ac97_t *ac97, unsigned short reg)
  381. {
  382. via82xx_t *chip = ac97->private_data;
  383. unsigned int xval, val = 0xffff;
  384. int again = 0;
  385. xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
  386. xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
  387. xval |= VIA_REG_AC97_READ;
  388. xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
  389. while (1) {
  390. if (again++ > 3) {
  391. snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n", ac97->num, snd_via82xx_codec_xread(chip));
  392. return 0xffff;
  393. }
  394. snd_via82xx_codec_xwrite(chip, xval);
  395. udelay (20);
  396. if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
  397. udelay(25);
  398. val = snd_via82xx_codec_xread(chip);
  399. break;
  400. }
  401. }
  402. return val & 0xffff;
  403. }
  404. static void snd_via82xx_channel_reset(via82xx_t *chip, viadev_t *viadev)
  405. {
  406. outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
  407. VIADEV_REG(viadev, OFFSET_CONTROL));
  408. inb(VIADEV_REG(viadev, OFFSET_CONTROL));
  409. udelay(50);
  410. /* disable interrupts */
  411. outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
  412. /* clear interrupts */
  413. outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
  414. outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
  415. // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
  416. viadev->lastpos = 0;
  417. }
  418. /*
  419. * Interrupt handler
  420. */
  421. static irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  422. {
  423. via82xx_t *chip = dev_id;
  424. unsigned int status;
  425. unsigned int i;
  426. status = inl(VIAREG(chip, SGD_SHADOW));
  427. if (! (status & chip->intr_mask)) {
  428. return IRQ_NONE;
  429. }
  430. // _skip_sgd:
  431. /* check status for each stream */
  432. spin_lock(&chip->reg_lock);
  433. for (i = 0; i < chip->num_devs; i++) {
  434. viadev_t *viadev = &chip->devs[i];
  435. unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
  436. c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
  437. if (! c_status)
  438. continue;
  439. if (viadev->substream && viadev->running) {
  440. spin_unlock(&chip->reg_lock);
  441. snd_pcm_period_elapsed(viadev->substream);
  442. spin_lock(&chip->reg_lock);
  443. }
  444. outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
  445. }
  446. spin_unlock(&chip->reg_lock);
  447. return IRQ_HANDLED;
  448. }
  449. /*
  450. * PCM callbacks
  451. */
  452. /*
  453. * trigger callback
  454. */
  455. static int snd_via82xx_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
  456. {
  457. via82xx_t *chip = snd_pcm_substream_chip(substream);
  458. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  459. unsigned char val = 0;
  460. switch (cmd) {
  461. case SNDRV_PCM_TRIGGER_START:
  462. case SNDRV_PCM_TRIGGER_SUSPEND:
  463. val |= VIA_REG_CTRL_START;
  464. viadev->running = 1;
  465. break;
  466. case SNDRV_PCM_TRIGGER_STOP:
  467. val = VIA_REG_CTRL_TERMINATE;
  468. viadev->running = 0;
  469. break;
  470. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  471. val |= VIA_REG_CTRL_PAUSE;
  472. viadev->running = 0;
  473. break;
  474. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  475. viadev->running = 1;
  476. break;
  477. default:
  478. return -EINVAL;
  479. }
  480. outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
  481. if (cmd == SNDRV_PCM_TRIGGER_STOP)
  482. snd_via82xx_channel_reset(chip, viadev);
  483. return 0;
  484. }
  485. /*
  486. * pointer callbacks
  487. */
  488. /*
  489. * calculate the linear position at the given sg-buffer index and the rest count
  490. */
  491. #define check_invalid_pos(viadev,pos) \
  492. ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 || viadev->lastpos < viadev->bufsize2))
  493. static inline unsigned int calc_linear_pos(viadev_t *viadev, unsigned int idx, unsigned int count)
  494. {
  495. unsigned int size, res;
  496. size = viadev->idx_table[idx].size;
  497. res = viadev->idx_table[idx].offset + size - count;
  498. /* check the validity of the calculated position */
  499. if (size < count) {
  500. snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n", (int)size, (int)count);
  501. res = viadev->lastpos;
  502. } else if (check_invalid_pos(viadev, res)) {
  503. #ifdef POINTER_DEBUG
  504. 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);
  505. #endif
  506. if (count && size < count) {
  507. snd_printd(KERN_ERR "invalid via82xx_cur_ptr, using last valid pointer\n");
  508. res = viadev->lastpos;
  509. } else {
  510. if (! count)
  511. /* bogus count 0 on the DMA boundary? */
  512. res = viadev->idx_table[idx].offset;
  513. else
  514. /* count register returns full size when end of buffer is reached */
  515. res = viadev->idx_table[idx].offset + size;
  516. if (check_invalid_pos(viadev, res)) {
  517. snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), using last valid pointer\n");
  518. res = viadev->lastpos;
  519. }
  520. }
  521. }
  522. viadev->lastpos = res; /* remember the last position */
  523. if (res >= viadev->bufsize)
  524. res -= viadev->bufsize;
  525. return res;
  526. }
  527. /*
  528. * get the current pointer on via686
  529. */
  530. static snd_pcm_uframes_t snd_via686_pcm_pointer(snd_pcm_substream_t *substream)
  531. {
  532. via82xx_t *chip = snd_pcm_substream_chip(substream);
  533. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  534. unsigned int idx, ptr, count, res;
  535. snd_assert(viadev->tbl_entries, return 0);
  536. if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
  537. return 0;
  538. spin_lock(&chip->reg_lock);
  539. count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
  540. /* The via686a does not have the current index register,
  541. * so we need to calculate the index from CURR_PTR.
  542. */
  543. ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
  544. if (ptr <= (unsigned int)viadev->table.addr)
  545. idx = 0;
  546. else /* CURR_PTR holds the address + 8 */
  547. idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
  548. res = calc_linear_pos(viadev, idx, count);
  549. spin_unlock(&chip->reg_lock);
  550. return bytes_to_frames(substream->runtime, res);
  551. }
  552. /*
  553. * hw_params callback:
  554. * allocate the buffer and build up the buffer description table
  555. */
  556. static int snd_via82xx_hw_params(snd_pcm_substream_t * substream,
  557. snd_pcm_hw_params_t * hw_params)
  558. {
  559. via82xx_t *chip = snd_pcm_substream_chip(substream);
  560. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  561. int err;
  562. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  563. if (err < 0)
  564. return err;
  565. err = build_via_table(viadev, substream, chip->pci,
  566. params_periods(hw_params),
  567. params_period_bytes(hw_params));
  568. if (err < 0)
  569. return err;
  570. snd_ac97_write(chip->ac97, AC97_LINE1_RATE, params_rate(hw_params));
  571. snd_ac97_write(chip->ac97, AC97_LINE1_LEVEL, 0);
  572. return 0;
  573. }
  574. /*
  575. * hw_free callback:
  576. * clean up the buffer description table and release the buffer
  577. */
  578. static int snd_via82xx_hw_free(snd_pcm_substream_t * substream)
  579. {
  580. via82xx_t *chip = snd_pcm_substream_chip(substream);
  581. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  582. clean_via_table(viadev, substream, chip->pci);
  583. snd_pcm_lib_free_pages(substream);
  584. return 0;
  585. }
  586. /*
  587. * set up the table pointer
  588. */
  589. static void snd_via82xx_set_table_ptr(via82xx_t *chip, viadev_t *viadev)
  590. {
  591. snd_via82xx_codec_ready(chip, chip->ac97_secondary);
  592. outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
  593. udelay(20);
  594. snd_via82xx_codec_ready(chip, chip->ac97_secondary);
  595. }
  596. /*
  597. * prepare callback for playback and capture
  598. */
  599. static int snd_via82xx_pcm_prepare(snd_pcm_substream_t *substream)
  600. {
  601. via82xx_t *chip = snd_pcm_substream_chip(substream);
  602. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  603. snd_via82xx_channel_reset(chip, viadev);
  604. /* this must be set after channel_reset */
  605. snd_via82xx_set_table_ptr(chip, viadev);
  606. outb(VIA_REG_TYPE_AUTOSTART|VIA_REG_TYPE_INT_EOL|VIA_REG_TYPE_INT_FLAG,
  607. VIADEV_REG(viadev, OFFSET_TYPE));
  608. return 0;
  609. }
  610. /*
  611. * pcm hardware definition, identical for both playback and capture
  612. */
  613. static snd_pcm_hardware_t snd_via82xx_hw =
  614. {
  615. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  616. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  617. SNDRV_PCM_INFO_MMAP_VALID |
  618. /* SNDRV_PCM_INFO_RESUME | */
  619. SNDRV_PCM_INFO_PAUSE),
  620. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  621. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
  622. .rate_min = 8000,
  623. .rate_max = 16000,
  624. .channels_min = 1,
  625. .channels_max = 1,
  626. .buffer_bytes_max = 128 * 1024,
  627. .period_bytes_min = 32,
  628. .period_bytes_max = 128 * 1024,
  629. .periods_min = 2,
  630. .periods_max = VIA_TABLE_SIZE / 2,
  631. .fifo_size = 0,
  632. };
  633. /*
  634. * open callback skeleton
  635. */
  636. static int snd_via82xx_modem_pcm_open(via82xx_t *chip, viadev_t *viadev, snd_pcm_substream_t * substream)
  637. {
  638. snd_pcm_runtime_t *runtime = substream->runtime;
  639. int err;
  640. static unsigned int rates[] = { 8000, 9600, 12000, 16000 };
  641. static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
  642. .count = ARRAY_SIZE(rates),
  643. .list = rates,
  644. .mask = 0,
  645. };
  646. runtime->hw = snd_via82xx_hw;
  647. if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates)) < 0)
  648. return err;
  649. /* we may remove following constaint when we modify table entries
  650. in interrupt */
  651. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  652. return err;
  653. runtime->private_data = viadev;
  654. viadev->substream = substream;
  655. return 0;
  656. }
  657. /*
  658. * open callback for playback
  659. */
  660. static int snd_via82xx_playback_open(snd_pcm_substream_t * substream)
  661. {
  662. via82xx_t *chip = snd_pcm_substream_chip(substream);
  663. viadev_t *viadev = &chip->devs[chip->playback_devno + substream->number];
  664. return snd_via82xx_modem_pcm_open(chip, viadev, substream);
  665. }
  666. /*
  667. * open callback for capture
  668. */
  669. static int snd_via82xx_capture_open(snd_pcm_substream_t * substream)
  670. {
  671. via82xx_t *chip = snd_pcm_substream_chip(substream);
  672. viadev_t *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
  673. return snd_via82xx_modem_pcm_open(chip, viadev, substream);
  674. }
  675. /*
  676. * close callback
  677. */
  678. static int snd_via82xx_pcm_close(snd_pcm_substream_t * substream)
  679. {
  680. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  681. viadev->substream = NULL;
  682. return 0;
  683. }
  684. /* via686 playback callbacks */
  685. static snd_pcm_ops_t snd_via686_playback_ops = {
  686. .open = snd_via82xx_playback_open,
  687. .close = snd_via82xx_pcm_close,
  688. .ioctl = snd_pcm_lib_ioctl,
  689. .hw_params = snd_via82xx_hw_params,
  690. .hw_free = snd_via82xx_hw_free,
  691. .prepare = snd_via82xx_pcm_prepare,
  692. .trigger = snd_via82xx_pcm_trigger,
  693. .pointer = snd_via686_pcm_pointer,
  694. .page = snd_pcm_sgbuf_ops_page,
  695. };
  696. /* via686 capture callbacks */
  697. static snd_pcm_ops_t snd_via686_capture_ops = {
  698. .open = snd_via82xx_capture_open,
  699. .close = snd_via82xx_pcm_close,
  700. .ioctl = snd_pcm_lib_ioctl,
  701. .hw_params = snd_via82xx_hw_params,
  702. .hw_free = snd_via82xx_hw_free,
  703. .prepare = snd_via82xx_pcm_prepare,
  704. .trigger = snd_via82xx_pcm_trigger,
  705. .pointer = snd_via686_pcm_pointer,
  706. .page = snd_pcm_sgbuf_ops_page,
  707. };
  708. static void init_viadev(via82xx_t *chip, int idx, unsigned int reg_offset, int direction)
  709. {
  710. chip->devs[idx].reg_offset = reg_offset;
  711. chip->devs[idx].direction = direction;
  712. chip->devs[idx].port = chip->port + reg_offset;
  713. }
  714. /*
  715. * create a pcm instance for via686a/b
  716. */
  717. static int __devinit snd_via686_pcm_new(via82xx_t *chip)
  718. {
  719. snd_pcm_t *pcm;
  720. int err;
  721. chip->playback_devno = 0;
  722. chip->capture_devno = 1;
  723. chip->num_devs = 2;
  724. chip->intr_mask = 0x330000; /* FLAGS | EOL for MR, MW */
  725. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
  726. if (err < 0)
  727. return err;
  728. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
  729. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
  730. pcm->private_data = chip;
  731. strcpy(pcm->name, chip->card->shortname);
  732. chip->pcms[0] = pcm;
  733. init_viadev(chip, 0, VIA_REG_MO_STATUS, 0);
  734. init_viadev(chip, 1, VIA_REG_MI_STATUS, 1);
  735. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  736. snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
  737. return err;
  738. return 0;
  739. }
  740. /*
  741. * Mixer part
  742. */
  743. static void snd_via82xx_mixer_free_ac97_bus(ac97_bus_t *bus)
  744. {
  745. via82xx_t *chip = bus->private_data;
  746. chip->ac97_bus = NULL;
  747. }
  748. static void snd_via82xx_mixer_free_ac97(ac97_t *ac97)
  749. {
  750. via82xx_t *chip = ac97->private_data;
  751. chip->ac97 = NULL;
  752. }
  753. static int __devinit snd_via82xx_mixer_new(via82xx_t *chip)
  754. {
  755. ac97_template_t ac97;
  756. int err;
  757. static ac97_bus_ops_t ops = {
  758. .write = snd_via82xx_codec_write,
  759. .read = snd_via82xx_codec_read,
  760. .wait = snd_via82xx_codec_wait,
  761. };
  762. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
  763. return err;
  764. chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
  765. chip->ac97_bus->clock = chip->ac97_clock;
  766. chip->ac97_bus->shared_type = AC97_SHARED_TYPE_VIA;
  767. memset(&ac97, 0, sizeof(ac97));
  768. ac97.private_data = chip;
  769. ac97.private_free = snd_via82xx_mixer_free_ac97;
  770. ac97.pci = chip->pci;
  771. ac97.scaps = AC97_SCAP_SKIP_AUDIO;
  772. ac97.num = chip->ac97_secondary;
  773. if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
  774. return err;
  775. return 0;
  776. }
  777. /*
  778. * proc interface
  779. */
  780. static void snd_via82xx_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
  781. {
  782. via82xx_t *chip = entry->private_data;
  783. int i;
  784. snd_iprintf(buffer, "%s\n\n", chip->card->longname);
  785. for (i = 0; i < 0xa0; i += 4) {
  786. snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
  787. }
  788. }
  789. static void __devinit snd_via82xx_proc_init(via82xx_t *chip)
  790. {
  791. snd_info_entry_t *entry;
  792. if (! snd_card_proc_new(chip->card, "via82xx", &entry))
  793. snd_info_set_text_ops(entry, chip, 1024, snd_via82xx_proc_read);
  794. }
  795. /*
  796. *
  797. */
  798. static int snd_via82xx_chip_init(via82xx_t *chip)
  799. {
  800. unsigned int val;
  801. unsigned long end_time;
  802. unsigned char pval;
  803. pci_read_config_byte(chip->pci, VIA_MC97_CTRL, &pval);
  804. if((pval & VIA_MC97_CTRL_INIT) != VIA_MC97_CTRL_INIT) {
  805. pci_write_config_byte(chip->pci, 0x44, pval|VIA_MC97_CTRL_INIT);
  806. udelay(100);
  807. }
  808. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  809. if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
  810. /* deassert ACLink reset, force SYNC */
  811. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  812. VIA_ACLINK_CTRL_ENABLE |
  813. VIA_ACLINK_CTRL_RESET |
  814. VIA_ACLINK_CTRL_SYNC);
  815. udelay(100);
  816. #if 1 /* FIXME: should we do full reset here for all chip models? */
  817. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
  818. udelay(100);
  819. #else
  820. /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
  821. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  822. VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
  823. udelay(2);
  824. #endif
  825. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  826. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  827. udelay(100);
  828. }
  829. pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
  830. if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
  831. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  832. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  833. udelay(100);
  834. }
  835. /* wait until codec ready */
  836. end_time = jiffies + msecs_to_jiffies(750);
  837. do {
  838. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  839. if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
  840. break;
  841. set_current_state(TASK_UNINTERRUPTIBLE);
  842. schedule_timeout(1);
  843. } while (time_before(jiffies, end_time));
  844. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
  845. snd_printk("AC'97 codec is not ready [0x%x]\n", val);
  846. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  847. VIA_REG_AC97_SECONDARY_VALID |
  848. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  849. end_time = jiffies + msecs_to_jiffies(750);
  850. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  851. VIA_REG_AC97_SECONDARY_VALID |
  852. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  853. do {
  854. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
  855. chip->ac97_secondary = 1;
  856. goto __ac97_ok2;
  857. }
  858. set_current_state(TASK_INTERRUPTIBLE);
  859. schedule_timeout(1);
  860. } while (time_before(jiffies, end_time));
  861. /* This is ok, the most of motherboards have only one codec */
  862. __ac97_ok2:
  863. /* route FM trap to IRQ, disable FM trap */
  864. // pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
  865. /* disable all GPI interrupts */
  866. outl(0, VIAREG(chip, GPI_INTR));
  867. return 0;
  868. }
  869. #ifdef CONFIG_PM
  870. /*
  871. * power management
  872. */
  873. static int snd_via82xx_suspend(snd_card_t *card, pm_message_t state)
  874. {
  875. via82xx_t *chip = card->pm_private_data;
  876. int i;
  877. for (i = 0; i < 2; i++)
  878. if (chip->pcms[i])
  879. snd_pcm_suspend_all(chip->pcms[i]);
  880. for (i = 0; i < chip->num_devs; i++)
  881. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  882. synchronize_irq(chip->irq);
  883. snd_ac97_suspend(chip->ac97);
  884. pci_set_power_state(chip->pci, 3);
  885. pci_disable_device(chip->pci);
  886. return 0;
  887. }
  888. static int snd_via82xx_resume(snd_card_t *card)
  889. {
  890. via82xx_t *chip = card->pm_private_data;
  891. int i;
  892. pci_enable_device(chip->pci);
  893. pci_set_power_state(chip->pci, 0);
  894. pci_set_master(chip->pci);
  895. snd_via82xx_chip_init(chip);
  896. snd_ac97_resume(chip->ac97);
  897. for (i = 0; i < chip->num_devs; i++)
  898. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  899. return 0;
  900. }
  901. #endif /* CONFIG_PM */
  902. static int snd_via82xx_free(via82xx_t *chip)
  903. {
  904. unsigned int i;
  905. if (chip->irq < 0)
  906. goto __end_hw;
  907. /* disable interrupts */
  908. for (i = 0; i < chip->num_devs; i++)
  909. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  910. synchronize_irq(chip->irq);
  911. __end_hw:
  912. if (chip->irq >= 0)
  913. free_irq(chip->irq, (void *)chip);
  914. pci_release_regions(chip->pci);
  915. pci_disable_device(chip->pci);
  916. kfree(chip);
  917. return 0;
  918. }
  919. static int snd_via82xx_dev_free(snd_device_t *device)
  920. {
  921. via82xx_t *chip = device->device_data;
  922. return snd_via82xx_free(chip);
  923. }
  924. static int __devinit snd_via82xx_create(snd_card_t * card,
  925. struct pci_dev *pci,
  926. int chip_type,
  927. int revision,
  928. unsigned int ac97_clock,
  929. via82xx_t ** r_via)
  930. {
  931. via82xx_t *chip;
  932. int err;
  933. static snd_device_ops_t ops = {
  934. .dev_free = snd_via82xx_dev_free,
  935. };
  936. if ((err = pci_enable_device(pci)) < 0)
  937. return err;
  938. if ((chip = kcalloc(1, sizeof(*chip), GFP_KERNEL)) == NULL) {
  939. pci_disable_device(pci);
  940. return -ENOMEM;
  941. }
  942. spin_lock_init(&chip->reg_lock);
  943. chip->card = card;
  944. chip->pci = pci;
  945. chip->irq = -1;
  946. if ((err = pci_request_regions(pci, card->driver)) < 0) {
  947. kfree(chip);
  948. pci_disable_device(pci);
  949. return err;
  950. }
  951. chip->port = pci_resource_start(pci, 0);
  952. if (request_irq(pci->irq, snd_via82xx_interrupt, SA_INTERRUPT|SA_SHIRQ,
  953. card->driver, (void *)chip)) {
  954. snd_printk("unable to grab IRQ %d\n", pci->irq);
  955. snd_via82xx_free(chip);
  956. return -EBUSY;
  957. }
  958. chip->irq = pci->irq;
  959. if (ac97_clock >= 8000 && ac97_clock <= 48000)
  960. chip->ac97_clock = ac97_clock;
  961. synchronize_irq(chip->irq);
  962. if ((err = snd_via82xx_chip_init(chip)) < 0) {
  963. snd_via82xx_free(chip);
  964. return err;
  965. }
  966. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  967. snd_via82xx_free(chip);
  968. return err;
  969. }
  970. /* The 8233 ac97 controller does not implement the master bit
  971. * in the pci command register. IMHO this is a violation of the PCI spec.
  972. * We call pci_set_master here because it does not hurt. */
  973. pci_set_master(pci);
  974. snd_card_set_dev(card, &pci->dev);
  975. *r_via = chip;
  976. return 0;
  977. }
  978. static int __devinit snd_via82xx_probe(struct pci_dev *pci,
  979. const struct pci_device_id *pci_id)
  980. {
  981. static int dev;
  982. snd_card_t *card;
  983. via82xx_t *chip;
  984. unsigned char revision;
  985. int chip_type = 0, card_type;
  986. unsigned int i;
  987. int err;
  988. if (dev >= SNDRV_CARDS)
  989. return -ENODEV;
  990. if (!enable[dev]) {
  991. dev++;
  992. return -ENOENT;
  993. }
  994. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  995. if (card == NULL)
  996. return -ENOMEM;
  997. card_type = pci_id->driver_data;
  998. pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
  999. switch (card_type) {
  1000. case TYPE_CARD_VIA82XX_MODEM:
  1001. strcpy(card->driver, "VIA82XX-MODEM");
  1002. sprintf(card->shortname, "VIA 82XX modem");
  1003. break;
  1004. default:
  1005. snd_printk(KERN_ERR "invalid card type %d\n", card_type);
  1006. err = -EINVAL;
  1007. goto __error;
  1008. }
  1009. if ((err = snd_via82xx_create(card, pci, chip_type, revision, ac97_clock[dev], &chip)) < 0)
  1010. goto __error;
  1011. if ((err = snd_via82xx_mixer_new(chip)) < 0)
  1012. goto __error;
  1013. if ((err = snd_via686_pcm_new(chip)) < 0 )
  1014. goto __error;
  1015. snd_card_set_pm_callback(card, snd_via82xx_suspend, snd_via82xx_resume, chip);
  1016. /* disable interrupts */
  1017. for (i = 0; i < chip->num_devs; i++)
  1018. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  1019. sprintf(card->longname, "%s at 0x%lx, irq %d",
  1020. card->shortname, chip->port, chip->irq);
  1021. snd_via82xx_proc_init(chip);
  1022. if ((err = snd_card_register(card)) < 0) {
  1023. snd_card_free(card);
  1024. return err;
  1025. }
  1026. pci_set_drvdata(pci, card);
  1027. dev++;
  1028. return 0;
  1029. __error:
  1030. snd_card_free(card);
  1031. return err;
  1032. }
  1033. static void __devexit snd_via82xx_remove(struct pci_dev *pci)
  1034. {
  1035. snd_card_free(pci_get_drvdata(pci));
  1036. pci_set_drvdata(pci, NULL);
  1037. }
  1038. static struct pci_driver driver = {
  1039. .name = "VIA 82xx Modem",
  1040. .id_table = snd_via82xx_modem_ids,
  1041. .probe = snd_via82xx_probe,
  1042. .remove = __devexit_p(snd_via82xx_remove),
  1043. SND_PCI_PM_CALLBACKS
  1044. };
  1045. static int __init alsa_card_via82xx_init(void)
  1046. {
  1047. return pci_register_driver(&driver);
  1048. }
  1049. static void __exit alsa_card_via82xx_exit(void)
  1050. {
  1051. pci_unregister_driver(&driver);
  1052. }
  1053. module_init(alsa_card_via82xx_init)
  1054. module_exit(alsa_card_via82xx_exit)