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