cmi8330.c 21 KB

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