cmi8330.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Driver for C-Media's CMI8330 and CMI8329 soundcards.
  3. * Copyright (c) by George Talusan <gstalusan@uwaterloo.ca>
  4. * http://www.undergrad.math.uwaterloo.ca/~gstalusa
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. /*
  22. * NOTES
  23. *
  24. * The extended registers contain mixer settings which are largely
  25. * untapped for the time being.
  26. *
  27. * MPU401 and SPDIF are not supported yet. I don't have the hardware
  28. * to aid in coding and testing, so I won't bother.
  29. *
  30. * To quickly load the module,
  31. *
  32. * modprobe -a snd-cmi8330 sbport=0x220 sbirq=5 sbdma8=1
  33. * sbdma16=5 wssport=0x530 wssirq=11 wssdma=0 fmport=0x388
  34. *
  35. * This card has two mixers and two PCM devices. I've cheesed it such
  36. * that recording and playback can be done through the same device.
  37. * The driver "magically" routes the capturing to the AD1848 codec,
  38. * and playback to the SB16 codec. This allows for full-duplex mode
  39. * to some extent.
  40. * The utilities in alsa-utils are aware of both devices, so passing
  41. * the appropriate parameters to amixer and alsactl will give you
  42. * full control over both mixers.
  43. */
  44. #include <linux/init.h>
  45. #include <linux/err.h>
  46. #include <linux/isa.h>
  47. #include <linux/pnp.h>
  48. #include <linux/module.h>
  49. #include <sound/core.h>
  50. #include <sound/wss.h>
  51. #include <sound/opl3.h>
  52. #include <sound/mpu401.h>
  53. #include <sound/sb.h>
  54. #include <sound/initval.h>
  55. /*
  56. */
  57. /* #define ENABLE_SB_MIXER */
  58. #define PLAYBACK_ON_SB
  59. /*
  60. */
  61. MODULE_AUTHOR("George Talusan <gstalusan@uwaterloo.ca>");
  62. MODULE_DESCRIPTION("C-Media CMI8330/CMI8329");
  63. MODULE_LICENSE("GPL");
  64. MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8330,isapnp:{CMI0001,@@@0001,@X@0001}}}");
  65. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  66. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  67. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
  68. #ifdef CONFIG_PNP
  69. static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  70. #endif
  71. static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  72. static int sbirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  73. static int sbdma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  74. static int sbdma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  75. static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  76. static int wssirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  77. static int wssdma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  78. static long fmport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  79. static long mpuport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  80. static int mpuirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  81. module_param_array(index, int, NULL, 0444);
  82. MODULE_PARM_DESC(index, "Index value for CMI8330/CMI8329 soundcard.");
  83. module_param_array(id, charp, NULL, 0444);
  84. MODULE_PARM_DESC(id, "ID string for CMI8330/CMI8329 soundcard.");
  85. module_param_array(enable, bool, NULL, 0444);
  86. MODULE_PARM_DESC(enable, "Enable CMI8330/CMI8329 soundcard.");
  87. #ifdef CONFIG_PNP
  88. module_param_array(isapnp, bool, NULL, 0444);
  89. MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
  90. #endif
  91. module_param_array(sbport, long, NULL, 0444);
  92. MODULE_PARM_DESC(sbport, "Port # for CMI8330/CMI8329 SB driver.");
  93. module_param_array(sbirq, int, NULL, 0444);
  94. MODULE_PARM_DESC(sbirq, "IRQ # for CMI8330/CMI8329 SB driver.");
  95. module_param_array(sbdma8, int, NULL, 0444);
  96. MODULE_PARM_DESC(sbdma8, "DMA8 for CMI8330/CMI8329 SB driver.");
  97. module_param_array(sbdma16, int, NULL, 0444);
  98. MODULE_PARM_DESC(sbdma16, "DMA16 for CMI8330/CMI8329 SB driver.");
  99. module_param_array(wssport, long, NULL, 0444);
  100. MODULE_PARM_DESC(wssport, "Port # for CMI8330/CMI8329 WSS driver.");
  101. module_param_array(wssirq, int, NULL, 0444);
  102. MODULE_PARM_DESC(wssirq, "IRQ # for CMI8330/CMI8329 WSS driver.");
  103. module_param_array(wssdma, int, NULL, 0444);
  104. MODULE_PARM_DESC(wssdma, "DMA for CMI8330/CMI8329 WSS driver.");
  105. module_param_array(fmport, long, NULL, 0444);
  106. MODULE_PARM_DESC(fmport, "FM port # for CMI8330/CMI8329 driver.");
  107. module_param_array(mpuport, long, NULL, 0444);
  108. MODULE_PARM_DESC(mpuport, "MPU-401 port # for CMI8330/CMI8329 driver.");
  109. module_param_array(mpuirq, int, NULL, 0444);
  110. MODULE_PARM_DESC(mpuirq, "IRQ # for CMI8330/CMI8329 MPU-401 port.");
  111. #ifdef CONFIG_PNP
  112. static int isa_registered;
  113. static int pnp_registered;
  114. #endif
  115. #define CMI8330_RMUX3D 16
  116. #define CMI8330_MUTEMUX 17
  117. #define CMI8330_OUTPUTVOL 18
  118. #define CMI8330_MASTVOL 19
  119. #define CMI8330_LINVOL 20
  120. #define CMI8330_CDINVOL 21
  121. #define CMI8330_WAVVOL 22
  122. #define CMI8330_RECMUX 23
  123. #define CMI8330_WAVGAIN 24
  124. #define CMI8330_LINGAIN 25
  125. #define CMI8330_CDINGAIN 26
  126. static unsigned char snd_cmi8330_image[((CMI8330_CDINGAIN)-16) + 1] =
  127. {
  128. 0x40, /* 16 - recording mux (SB-mixer-enabled) */
  129. #ifdef ENABLE_SB_MIXER
  130. 0x40, /* 17 - mute mux (Mode2) */
  131. #else
  132. 0x0, /* 17 - mute mux */
  133. #endif
  134. 0x0, /* 18 - vol */
  135. 0x0, /* 19 - master volume */
  136. 0x0, /* 20 - line-in volume */
  137. 0x0, /* 21 - cd-in volume */
  138. 0x0, /* 22 - wave volume */
  139. 0x0, /* 23 - mute/rec mux */
  140. 0x0, /* 24 - wave rec gain */
  141. 0x0, /* 25 - line-in rec gain */
  142. 0x0 /* 26 - cd-in rec gain */
  143. };
  144. typedef int (*snd_pcm_open_callback_t)(struct snd_pcm_substream *);
  145. enum card_type {
  146. CMI8330,
  147. CMI8329
  148. };
  149. struct snd_cmi8330 {
  150. #ifdef CONFIG_PNP
  151. struct pnp_dev *cap;
  152. struct pnp_dev *play;
  153. struct pnp_dev *mpu;
  154. #endif
  155. struct snd_card *card;
  156. struct snd_wss *wss;
  157. struct snd_sb *sb;
  158. struct snd_pcm *pcm;
  159. struct snd_cmi8330_stream {
  160. struct snd_pcm_ops ops;
  161. snd_pcm_open_callback_t open;
  162. void *private_data; /* sb or wss */
  163. } streams[2];
  164. enum card_type type;
  165. };
  166. #ifdef CONFIG_PNP
  167. static struct pnp_card_device_id snd_cmi8330_pnpids[] = {
  168. { .id = "CMI0001", .devs = { { "@X@0001" }, { "@@@0001" }, { "@H@0001" }, { "A@@0001" } } },
  169. { .id = "CMI0001", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" } } },
  170. { .id = "" }
  171. };
  172. MODULE_DEVICE_TABLE(pnp_card, snd_cmi8330_pnpids);
  173. #endif
  174. static struct snd_kcontrol_new snd_cmi8330_controls[] = {
  175. WSS_DOUBLE("Master Playback Volume", 0,
  176. CMI8330_MASTVOL, CMI8330_MASTVOL, 4, 0, 15, 0),
  177. WSS_SINGLE("Loud Playback Switch", 0,
  178. CMI8330_MUTEMUX, 6, 1, 1),
  179. WSS_DOUBLE("PCM Playback Switch", 0,
  180. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  181. WSS_DOUBLE("PCM Playback Volume", 0,
  182. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
  183. WSS_DOUBLE("Line Playback Switch", 0,
  184. CMI8330_MUTEMUX, CMI8330_MUTEMUX, 4, 3, 1, 0),
  185. WSS_DOUBLE("Line Playback Volume", 0,
  186. CMI8330_LINVOL, CMI8330_LINVOL, 4, 0, 15, 0),
  187. WSS_DOUBLE("Line Capture Switch", 0,
  188. CMI8330_RMUX3D, CMI8330_RMUX3D, 2, 1, 1, 0),
  189. WSS_DOUBLE("Line Capture Volume", 0,
  190. CMI8330_LINGAIN, CMI8330_LINGAIN, 4, 0, 15, 0),
  191. WSS_DOUBLE("CD Playback Switch", 0,
  192. CMI8330_MUTEMUX, CMI8330_MUTEMUX, 2, 1, 1, 0),
  193. WSS_DOUBLE("CD Capture Switch", 0,
  194. CMI8330_RMUX3D, CMI8330_RMUX3D, 4, 3, 1, 0),
  195. WSS_DOUBLE("CD Playback Volume", 0,
  196. CMI8330_CDINVOL, CMI8330_CDINVOL, 4, 0, 15, 0),
  197. WSS_DOUBLE("CD Capture Volume", 0,
  198. CMI8330_CDINGAIN, CMI8330_CDINGAIN, 4, 0, 15, 0),
  199. WSS_SINGLE("Mic Playback Switch", 0,
  200. CMI8330_MUTEMUX, 0, 1, 0),
  201. WSS_SINGLE("Mic Playback Volume", 0,
  202. CMI8330_OUTPUTVOL, 0, 7, 0),
  203. WSS_SINGLE("Mic Capture Switch", 0,
  204. CMI8330_RMUX3D, 0, 1, 0),
  205. WSS_SINGLE("Mic Capture Volume", 0,
  206. CMI8330_OUTPUTVOL, 5, 7, 0),
  207. WSS_DOUBLE("Wavetable Playback Switch", 0,
  208. CMI8330_RECMUX, CMI8330_RECMUX, 1, 0, 1, 0),
  209. WSS_DOUBLE("Wavetable Playback Volume", 0,
  210. CMI8330_WAVVOL, CMI8330_WAVVOL, 4, 0, 15, 0),
  211. WSS_DOUBLE("Wavetable Capture Switch", 0,
  212. CMI8330_RECMUX, CMI8330_RECMUX, 5, 4, 1, 0),
  213. WSS_DOUBLE("Wavetable Capture Volume", 0,
  214. CMI8330_WAVGAIN, CMI8330_WAVGAIN, 4, 0, 15, 0),
  215. WSS_SINGLE("3D Control - Switch", 0,
  216. CMI8330_RMUX3D, 5, 1, 1),
  217. WSS_SINGLE("Beep Playback Volume", 0,
  218. CMI8330_OUTPUTVOL, 3, 3, 0),
  219. WSS_DOUBLE("FM Playback Switch", 0,
  220. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  221. WSS_DOUBLE("FM Playback Volume", 0,
  222. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
  223. WSS_SINGLE(SNDRV_CTL_NAME_IEC958("Input ", CAPTURE, SWITCH), 0,
  224. CMI8330_RMUX3D, 7, 1, 1),
  225. WSS_SINGLE(SNDRV_CTL_NAME_IEC958("Input ", PLAYBACK, SWITCH), 0,
  226. CMI8330_MUTEMUX, 7, 1, 1),
  227. };
  228. #ifdef ENABLE_SB_MIXER
  229. static struct sbmix_elem cmi8330_sb_mixers[] = {
  230. SB_DOUBLE("SB Master Playback Volume", SB_DSP4_MASTER_DEV, (SB_DSP4_MASTER_DEV + 1), 3, 3, 31),
  231. SB_DOUBLE("Tone Control - Bass", SB_DSP4_BASS_DEV, (SB_DSP4_BASS_DEV + 1), 4, 4, 15),
  232. SB_DOUBLE("Tone Control - Treble", SB_DSP4_TREBLE_DEV, (SB_DSP4_TREBLE_DEV + 1), 4, 4, 15),
  233. SB_DOUBLE("SB PCM Playback Volume", SB_DSP4_PCM_DEV, (SB_DSP4_PCM_DEV + 1), 3, 3, 31),
  234. SB_DOUBLE("SB Synth Playback Volume", SB_DSP4_SYNTH_DEV, (SB_DSP4_SYNTH_DEV + 1), 3, 3, 31),
  235. SB_DOUBLE("SB CD Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 2, 1, 1),
  236. SB_DOUBLE("SB CD Playback Volume", SB_DSP4_CD_DEV, (SB_DSP4_CD_DEV + 1), 3, 3, 31),
  237. SB_DOUBLE("SB Line Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 4, 3, 1),
  238. SB_DOUBLE("SB Line Playback Volume", SB_DSP4_LINE_DEV, (SB_DSP4_LINE_DEV + 1), 3, 3, 31),
  239. SB_SINGLE("SB Mic Playback Switch", SB_DSP4_OUTPUT_SW, 0, 1),
  240. SB_SINGLE("SB Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
  241. SB_SINGLE("SB Beep Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
  242. SB_DOUBLE("SB Capture Volume", SB_DSP4_IGAIN_DEV, (SB_DSP4_IGAIN_DEV + 1), 6, 6, 3),
  243. SB_DOUBLE("SB Playback Volume", SB_DSP4_OGAIN_DEV, (SB_DSP4_OGAIN_DEV + 1), 6, 6, 3),
  244. SB_SINGLE("SB Mic Auto Gain", SB_DSP4_MIC_AGC, 0, 1),
  245. };
  246. static unsigned char cmi8330_sb_init_values[][2] = {
  247. { SB_DSP4_MASTER_DEV + 0, 0 },
  248. { SB_DSP4_MASTER_DEV + 1, 0 },
  249. { SB_DSP4_PCM_DEV + 0, 0 },
  250. { SB_DSP4_PCM_DEV + 1, 0 },
  251. { SB_DSP4_SYNTH_DEV + 0, 0 },
  252. { SB_DSP4_SYNTH_DEV + 1, 0 },
  253. { SB_DSP4_INPUT_LEFT, 0 },
  254. { SB_DSP4_INPUT_RIGHT, 0 },
  255. { SB_DSP4_OUTPUT_SW, 0 },
  256. { SB_DSP4_SPEAKER_DEV, 0 },
  257. };
  258. static int cmi8330_add_sb_mixers(struct snd_sb *chip)
  259. {
  260. int idx, err;
  261. unsigned long flags;
  262. spin_lock_irqsave(&chip->mixer_lock, flags);
  263. snd_sbmixer_write(chip, 0x00, 0x00); /* mixer reset */
  264. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  265. /* mute and zero volume channels */
  266. for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_init_values); idx++) {
  267. spin_lock_irqsave(&chip->mixer_lock, flags);
  268. snd_sbmixer_write(chip, cmi8330_sb_init_values[idx][0],
  269. cmi8330_sb_init_values[idx][1]);
  270. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  271. }
  272. for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_mixers); idx++) {
  273. if ((err = snd_sbmixer_add_ctl_elem(chip, &cmi8330_sb_mixers[idx])) < 0)
  274. return err;
  275. }
  276. return 0;
  277. }
  278. #endif
  279. static int snd_cmi8330_mixer(struct snd_card *card, struct snd_cmi8330 *acard)
  280. {
  281. unsigned int idx;
  282. int err;
  283. strcpy(card->mixername, (acard->type == CMI8329) ? "CMI8329" : "CMI8330/C3D");
  284. for (idx = 0; idx < ARRAY_SIZE(snd_cmi8330_controls); idx++) {
  285. err = snd_ctl_add(card,
  286. snd_ctl_new1(&snd_cmi8330_controls[idx],
  287. acard->wss));
  288. if (err < 0)
  289. return err;
  290. }
  291. #ifdef ENABLE_SB_MIXER
  292. if ((err = cmi8330_add_sb_mixers(acard->sb)) < 0)
  293. return err;
  294. #endif
  295. return 0;
  296. }
  297. #ifdef CONFIG_PNP
  298. static int snd_cmi8330_pnp(int dev, struct snd_cmi8330 *acard,
  299. struct pnp_card_link *card,
  300. const struct pnp_card_device_id *id)
  301. {
  302. struct pnp_dev *pdev;
  303. int err;
  304. /* CMI8329 has a device with ID A@@0001, CMI8330 does not */
  305. acard->type = (id->devs[3].id[0]) ? CMI8329 : CMI8330;
  306. acard->cap = pnp_request_card_device(card, id->devs[0].id, NULL);
  307. if (acard->cap == NULL)
  308. return -EBUSY;
  309. acard->play = pnp_request_card_device(card, id->devs[1].id, NULL);
  310. if (acard->play == NULL)
  311. return -EBUSY;
  312. acard->mpu = pnp_request_card_device(card, id->devs[2].id, NULL);
  313. if (acard->mpu == NULL)
  314. return -EBUSY;
  315. pdev = acard->cap;
  316. err = pnp_activate_dev(pdev);
  317. if (err < 0) {
  318. snd_printk(KERN_ERR "AD1848 PnP configure failure\n");
  319. return -EBUSY;
  320. }
  321. wssport[dev] = pnp_port_start(pdev, 0);
  322. wssdma[dev] = pnp_dma(pdev, 0);
  323. wssirq[dev] = pnp_irq(pdev, 0);
  324. if (pnp_port_start(pdev, 1))
  325. fmport[dev] = pnp_port_start(pdev, 1);
  326. /* allocate SB16 resources */
  327. pdev = acard->play;
  328. err = pnp_activate_dev(pdev);
  329. if (err < 0) {
  330. snd_printk(KERN_ERR "SB16 PnP configure failure\n");
  331. return -EBUSY;
  332. }
  333. sbport[dev] = pnp_port_start(pdev, 0);
  334. sbdma8[dev] = pnp_dma(pdev, 0);
  335. sbdma16[dev] = pnp_dma(pdev, 1);
  336. sbirq[dev] = pnp_irq(pdev, 0);
  337. /* On CMI8239, the OPL3 port might be present in SB16 PnP resources */
  338. if (fmport[dev] == SNDRV_AUTO_PORT) {
  339. if (pnp_port_start(pdev, 1))
  340. fmport[dev] = pnp_port_start(pdev, 1);
  341. else
  342. fmport[dev] = 0x388; /* Or hardwired */
  343. }
  344. /* allocate MPU-401 resources */
  345. pdev = acard->mpu;
  346. err = pnp_activate_dev(pdev);
  347. if (err < 0)
  348. snd_printk(KERN_ERR "MPU-401 PnP configure failure: will be disabled\n");
  349. else {
  350. mpuport[dev] = pnp_port_start(pdev, 0);
  351. mpuirq[dev] = pnp_irq(pdev, 0);
  352. }
  353. return 0;
  354. }
  355. #endif
  356. /*
  357. * PCM interface
  358. *
  359. * since we call the different chip interfaces for playback and capture
  360. * directions, we need a trick.
  361. *
  362. * - copy the ops for each direction into a local record.
  363. * - replace the open callback with the new one, which replaces the
  364. * substream->private_data with the corresponding chip instance
  365. * and calls again the original open callback of the chip.
  366. *
  367. */
  368. #ifdef PLAYBACK_ON_SB
  369. #define CMI_SB_STREAM SNDRV_PCM_STREAM_PLAYBACK
  370. #define CMI_AD_STREAM SNDRV_PCM_STREAM_CAPTURE
  371. #else
  372. #define CMI_SB_STREAM SNDRV_PCM_STREAM_CAPTURE
  373. #define CMI_AD_STREAM SNDRV_PCM_STREAM_PLAYBACK
  374. #endif
  375. static int snd_cmi8330_playback_open(struct snd_pcm_substream *substream)
  376. {
  377. struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
  378. /* replace the private_data and call the original open callback */
  379. substream->private_data = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].private_data;
  380. return chip->streams[SNDRV_PCM_STREAM_PLAYBACK].open(substream);
  381. }
  382. static int snd_cmi8330_capture_open(struct snd_pcm_substream *substream)
  383. {
  384. struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
  385. /* replace the private_data and call the original open callback */
  386. substream->private_data = chip->streams[SNDRV_PCM_STREAM_CAPTURE].private_data;
  387. return chip->streams[SNDRV_PCM_STREAM_CAPTURE].open(substream);
  388. }
  389. static int snd_cmi8330_pcm(struct snd_card *card, struct snd_cmi8330 *chip)
  390. {
  391. struct snd_pcm *pcm;
  392. const struct snd_pcm_ops *ops;
  393. int err;
  394. static snd_pcm_open_callback_t cmi_open_callbacks[2] = {
  395. snd_cmi8330_playback_open,
  396. snd_cmi8330_capture_open
  397. };
  398. if ((err = snd_pcm_new(card, (chip->type == CMI8329) ? "CMI8329" : "CMI8330", 0, 1, 1, &pcm)) < 0)
  399. return err;
  400. strcpy(pcm->name, (chip->type == CMI8329) ? "CMI8329" : "CMI8330");
  401. pcm->private_data = chip;
  402. /* SB16 */
  403. ops = snd_sb16dsp_get_pcm_ops(CMI_SB_STREAM);
  404. chip->streams[CMI_SB_STREAM].ops = *ops;
  405. chip->streams[CMI_SB_STREAM].open = ops->open;
  406. chip->streams[CMI_SB_STREAM].ops.open = cmi_open_callbacks[CMI_SB_STREAM];
  407. chip->streams[CMI_SB_STREAM].private_data = chip->sb;
  408. /* AD1848 */
  409. ops = snd_wss_get_pcm_ops(CMI_AD_STREAM);
  410. chip->streams[CMI_AD_STREAM].ops = *ops;
  411. chip->streams[CMI_AD_STREAM].open = ops->open;
  412. chip->streams[CMI_AD_STREAM].ops.open = cmi_open_callbacks[CMI_AD_STREAM];
  413. chip->streams[CMI_AD_STREAM].private_data = chip->wss;
  414. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK].ops);
  415. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &chip->streams[SNDRV_PCM_STREAM_CAPTURE].ops);
  416. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  417. snd_dma_isa_data(),
  418. 64*1024, 128*1024);
  419. chip->pcm = pcm;
  420. return 0;
  421. }
  422. #ifdef CONFIG_PM
  423. static int snd_cmi8330_suspend(struct snd_card *card)
  424. {
  425. struct snd_cmi8330 *acard = card->private_data;
  426. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  427. snd_pcm_suspend_all(acard->pcm);
  428. acard->wss->suspend(acard->wss);
  429. snd_sbmixer_suspend(acard->sb);
  430. return 0;
  431. }
  432. static int snd_cmi8330_resume(struct snd_card *card)
  433. {
  434. struct snd_cmi8330 *acard = card->private_data;
  435. snd_sbdsp_reset(acard->sb);
  436. snd_sbmixer_suspend(acard->sb);
  437. acard->wss->resume(acard->wss);
  438. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  439. return 0;
  440. }
  441. #endif
  442. /*
  443. */
  444. #ifdef CONFIG_PNP
  445. #define is_isapnp_selected(dev) isapnp[dev]
  446. #else
  447. #define is_isapnp_selected(dev) 0
  448. #endif
  449. #define PFX "cmi8330: "
  450. static int snd_cmi8330_card_new(struct device *pdev, int dev,
  451. struct snd_card **cardp)
  452. {
  453. struct snd_card *card;
  454. struct snd_cmi8330 *acard;
  455. int err;
  456. err = snd_card_new(pdev, index[dev], id[dev], THIS_MODULE,
  457. sizeof(struct snd_cmi8330), &card);
  458. if (err < 0) {
  459. snd_printk(KERN_ERR PFX "could not get a new card\n");
  460. return err;
  461. }
  462. acard = card->private_data;
  463. acard->card = card;
  464. *cardp = card;
  465. return 0;
  466. }
  467. static int snd_cmi8330_probe(struct snd_card *card, int dev)
  468. {
  469. struct snd_cmi8330 *acard;
  470. int i, err;
  471. struct snd_opl3 *opl3;
  472. acard = card->private_data;
  473. err = snd_wss_create(card, wssport[dev] + 4, -1,
  474. wssirq[dev],
  475. wssdma[dev], -1,
  476. WSS_HW_DETECT, 0, &acard->wss);
  477. if (err < 0) {
  478. snd_printk(KERN_ERR PFX "AD1848 device busy??\n");
  479. return err;
  480. }
  481. if (acard->wss->hardware != WSS_HW_CMI8330) {
  482. snd_printk(KERN_ERR PFX "AD1848 not found during probe\n");
  483. return -ENODEV;
  484. }
  485. if ((err = snd_sbdsp_create(card, sbport[dev],
  486. sbirq[dev],
  487. snd_sb16dsp_interrupt,
  488. sbdma8[dev],
  489. sbdma16[dev],
  490. SB_HW_AUTO, &acard->sb)) < 0) {
  491. snd_printk(KERN_ERR PFX "SB16 device busy??\n");
  492. return err;
  493. }
  494. if (acard->sb->hardware != SB_HW_16) {
  495. snd_printk(KERN_ERR PFX "SB16 not found during probe\n");
  496. return err;
  497. }
  498. snd_wss_out(acard->wss, CS4231_MISC_INFO, 0x40); /* switch on MODE2 */
  499. for (i = CMI8330_RMUX3D; i <= CMI8330_CDINGAIN; i++)
  500. snd_wss_out(acard->wss, i,
  501. snd_cmi8330_image[i - CMI8330_RMUX3D]);
  502. if ((err = snd_cmi8330_mixer(card, acard)) < 0) {
  503. snd_printk(KERN_ERR PFX "failed to create mixers\n");
  504. return err;
  505. }
  506. if ((err = snd_cmi8330_pcm(card, acard)) < 0) {
  507. snd_printk(KERN_ERR PFX "failed to create pcms\n");
  508. return err;
  509. }
  510. if (fmport[dev] != SNDRV_AUTO_PORT) {
  511. if (snd_opl3_create(card,
  512. fmport[dev], fmport[dev] + 2,
  513. OPL3_HW_AUTO, 0, &opl3) < 0) {
  514. snd_printk(KERN_ERR PFX
  515. "no OPL device at 0x%lx-0x%lx ?\n",
  516. fmport[dev], fmport[dev] + 2);
  517. } else {
  518. err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
  519. if (err < 0)
  520. return err;
  521. }
  522. }
  523. if (mpuport[dev] != SNDRV_AUTO_PORT) {
  524. if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  525. mpuport[dev], 0, mpuirq[dev],
  526. NULL) < 0)
  527. printk(KERN_ERR PFX "no MPU-401 device at 0x%lx.\n",
  528. mpuport[dev]);
  529. }
  530. strcpy(card->driver, (acard->type == CMI8329) ? "CMI8329" : "CMI8330/C3D");
  531. strcpy(card->shortname, (acard->type == CMI8329) ? "C-Media CMI8329" : "C-Media CMI8330/C3D");
  532. sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
  533. card->shortname,
  534. acard->wss->port,
  535. wssirq[dev],
  536. wssdma[dev]);
  537. return snd_card_register(card);
  538. }
  539. static int snd_cmi8330_isa_match(struct device *pdev,
  540. unsigned int dev)
  541. {
  542. if (!enable[dev] || is_isapnp_selected(dev))
  543. return 0;
  544. if (wssport[dev] == SNDRV_AUTO_PORT) {
  545. snd_printk(KERN_ERR PFX "specify wssport\n");
  546. return 0;
  547. }
  548. if (sbport[dev] == SNDRV_AUTO_PORT) {
  549. snd_printk(KERN_ERR PFX "specify sbport\n");
  550. return 0;
  551. }
  552. return 1;
  553. }
  554. static int snd_cmi8330_isa_probe(struct device *pdev,
  555. unsigned int dev)
  556. {
  557. struct snd_card *card;
  558. int err;
  559. err = snd_cmi8330_card_new(pdev, dev, &card);
  560. if (err < 0)
  561. return err;
  562. if ((err = snd_cmi8330_probe(card, dev)) < 0) {
  563. snd_card_free(card);
  564. return err;
  565. }
  566. dev_set_drvdata(pdev, card);
  567. return 0;
  568. }
  569. static int snd_cmi8330_isa_remove(struct device *devptr,
  570. unsigned int dev)
  571. {
  572. snd_card_free(dev_get_drvdata(devptr));
  573. return 0;
  574. }
  575. #ifdef CONFIG_PM
  576. static int snd_cmi8330_isa_suspend(struct device *dev, unsigned int n,
  577. pm_message_t state)
  578. {
  579. return snd_cmi8330_suspend(dev_get_drvdata(dev));
  580. }
  581. static int snd_cmi8330_isa_resume(struct device *dev, unsigned int n)
  582. {
  583. return snd_cmi8330_resume(dev_get_drvdata(dev));
  584. }
  585. #endif
  586. #define DEV_NAME "cmi8330"
  587. static struct isa_driver snd_cmi8330_driver = {
  588. .match = snd_cmi8330_isa_match,
  589. .probe = snd_cmi8330_isa_probe,
  590. .remove = snd_cmi8330_isa_remove,
  591. #ifdef CONFIG_PM
  592. .suspend = snd_cmi8330_isa_suspend,
  593. .resume = snd_cmi8330_isa_resume,
  594. #endif
  595. .driver = {
  596. .name = DEV_NAME
  597. },
  598. };
  599. #ifdef CONFIG_PNP
  600. static int snd_cmi8330_pnp_detect(struct pnp_card_link *pcard,
  601. const struct pnp_card_device_id *pid)
  602. {
  603. static int dev;
  604. struct snd_card *card;
  605. int res;
  606. for ( ; dev < SNDRV_CARDS; dev++) {
  607. if (enable[dev] && isapnp[dev])
  608. break;
  609. }
  610. if (dev >= SNDRV_CARDS)
  611. return -ENODEV;
  612. res = snd_cmi8330_card_new(&pcard->card->dev, dev, &card);
  613. if (res < 0)
  614. return res;
  615. if ((res = snd_cmi8330_pnp(dev, card->private_data, pcard, pid)) < 0) {
  616. snd_printk(KERN_ERR PFX "PnP detection failed\n");
  617. snd_card_free(card);
  618. return res;
  619. }
  620. if ((res = snd_cmi8330_probe(card, dev)) < 0) {
  621. snd_card_free(card);
  622. return res;
  623. }
  624. pnp_set_card_drvdata(pcard, card);
  625. dev++;
  626. return 0;
  627. }
  628. static void snd_cmi8330_pnp_remove(struct pnp_card_link *pcard)
  629. {
  630. snd_card_free(pnp_get_card_drvdata(pcard));
  631. pnp_set_card_drvdata(pcard, NULL);
  632. }
  633. #ifdef CONFIG_PM
  634. static int snd_cmi8330_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
  635. {
  636. return snd_cmi8330_suspend(pnp_get_card_drvdata(pcard));
  637. }
  638. static int snd_cmi8330_pnp_resume(struct pnp_card_link *pcard)
  639. {
  640. return snd_cmi8330_resume(pnp_get_card_drvdata(pcard));
  641. }
  642. #endif
  643. static struct pnp_card_driver cmi8330_pnpc_driver = {
  644. .flags = PNP_DRIVER_RES_DISABLE,
  645. .name = "cmi8330",
  646. .id_table = snd_cmi8330_pnpids,
  647. .probe = snd_cmi8330_pnp_detect,
  648. .remove = snd_cmi8330_pnp_remove,
  649. #ifdef CONFIG_PM
  650. .suspend = snd_cmi8330_pnp_suspend,
  651. .resume = snd_cmi8330_pnp_resume,
  652. #endif
  653. };
  654. #endif /* CONFIG_PNP */
  655. static int __init alsa_card_cmi8330_init(void)
  656. {
  657. int err;
  658. err = isa_register_driver(&snd_cmi8330_driver, SNDRV_CARDS);
  659. #ifdef CONFIG_PNP
  660. if (!err)
  661. isa_registered = 1;
  662. err = pnp_register_card_driver(&cmi8330_pnpc_driver);
  663. if (!err)
  664. pnp_registered = 1;
  665. if (isa_registered)
  666. err = 0;
  667. #endif
  668. return err;
  669. }
  670. static void __exit alsa_card_cmi8330_exit(void)
  671. {
  672. #ifdef CONFIG_PNP
  673. if (pnp_registered)
  674. pnp_unregister_card_driver(&cmi8330_pnpc_driver);
  675. if (isa_registered)
  676. #endif
  677. isa_unregister_driver(&snd_cmi8330_driver);
  678. }
  679. module_init(alsa_card_cmi8330_init)
  680. module_exit(alsa_card_cmi8330_exit)