Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Driver for C-Media CMI8328-based soundcards, such as AudioExcel AV500
  4 * Copyright (c) 2012 Ondrej Zary
  5 *
  6 * AudioExcel AV500 card consists of:
  7 *  - CMI8328 - main chip (SB Pro emulation, gameport, OPL3, MPU401, CD-ROM)
  8 *  - CS4231A - WSS codec
  9 *  - Dream SAM9233+GMS950400+RAM+ROM: Wavetable MIDI, connected to MPU401
 10 */
 11
 12#include <linux/init.h>
 13#include <linux/isa.h>
 14#include <linux/module.h>
 15#include <linux/gameport.h>
 16#include <asm/dma.h>
 17#include <sound/core.h>
 18#include <sound/wss.h>
 19#include <sound/opl3.h>
 20#include <sound/mpu401.h>
 21#define SNDRV_LEGACY_FIND_FREE_IOPORT
 22#define SNDRV_LEGACY_FIND_FREE_IRQ
 23#define SNDRV_LEGACY_FIND_FREE_DMA
 24#include <sound/initval.h>
 25
 26MODULE_AUTHOR("Ondrej Zary <linux@rainbow-software.org>");
 27MODULE_DESCRIPTION("C-Media CMI8328");
 28MODULE_LICENSE("GPL");
 29
 30#if IS_ENABLED(CONFIG_GAMEPORT)
 31#define SUPPORT_JOYSTICK 1
 32#endif
 33
 34/* I/O port is configured by jumpers on the card to one of these */
 35static const int cmi8328_ports[] = { 0x530, 0xe80, 0xf40, 0x604 };
 36#define CMI8328_MAX	ARRAY_SIZE(cmi8328_ports)
 37
 38static int index[CMI8328_MAX] =     {[0 ... (CMI8328_MAX-1)] = -1};
 39static char *id[CMI8328_MAX] =      {[0 ... (CMI8328_MAX-1)] = NULL};
 40static long port[CMI8328_MAX] =     {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT};
 41static int irq[CMI8328_MAX] =       {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ};
 42static int dma1[CMI8328_MAX] =      {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA};
 43static int dma2[CMI8328_MAX] =      {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA};
 44static long mpuport[CMI8328_MAX] =  {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT};
 45static int mpuirq[CMI8328_MAX] =    {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ};
 46#ifdef SUPPORT_JOYSTICK
 47static bool gameport[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = true};
 48#endif
 49
 50module_param_array(index, int, NULL, 0444);
 51MODULE_PARM_DESC(index, "Index value for CMI8328 soundcard.");
 52module_param_array(id, charp, NULL, 0444);
 53MODULE_PARM_DESC(id, "ID string for CMI8328 soundcard.");
 54
 55module_param_hw_array(port, long, ioport, NULL, 0444);
 56MODULE_PARM_DESC(port, "Port # for CMI8328 driver.");
 57module_param_hw_array(irq, int, irq, NULL, 0444);
 58MODULE_PARM_DESC(irq, "IRQ # for CMI8328 driver.");
 59module_param_hw_array(dma1, int, dma, NULL, 0444);
 60MODULE_PARM_DESC(dma1, "DMA1 for CMI8328 driver.");
 61module_param_hw_array(dma2, int, dma, NULL, 0444);
 62MODULE_PARM_DESC(dma2, "DMA2 for CMI8328 driver.");
 63
 64module_param_hw_array(mpuport, long, ioport, NULL, 0444);
 65MODULE_PARM_DESC(mpuport, "MPU-401 port # for CMI8328 driver.");
 66module_param_hw_array(mpuirq, int, irq, NULL, 0444);
 67MODULE_PARM_DESC(mpuirq, "IRQ # for CMI8328 MPU-401 port.");
 68#ifdef SUPPORT_JOYSTICK
 69module_param_array(gameport, bool, NULL, 0444);
 70MODULE_PARM_DESC(gameport, "Enable gameport.");
 71#endif
 72
 73struct snd_cmi8328 {
 74	u16 port;
 75	u8 cfg[3];
 76	u8 wss_cfg;
 77	struct snd_card *card;
 78	struct snd_wss *wss;
 79#ifdef SUPPORT_JOYSTICK
 80	struct gameport *gameport;
 81#endif
 82};
 83
 84/* CMI8328 configuration registers */
 85#define CFG1 0x61
 86#define CFG1_SB_DISABLE	(1 << 0)
 87#define CFG1_GAMEPORT	(1 << 1)
 88/*
 89 * bit 0:    SB: 0=enabled, 1=disabled
 90 * bit 1:    gameport: 0=disabled, 1=enabled
 91 * bits 2-4: SB IRQ: 001=3, 010=5, 011=7, 100=9, 101=10, 110=11
 92 * bits 5-6: SB DMA: 00=disabled (when SB disabled), 01=DMA0, 10=DMA1, 11=DMA3
 93 * bit 7:    SB port: 0=0x220, 1=0x240
 94 */
 95#define CFG2 0x62
 96#define CFG2_MPU_ENABLE (1 << 2)
 97/*
 98 * bits 0-1: CD-ROM mode: 00=disabled, 01=Panasonic, 10=Sony/Mitsumi/Wearnes,
 99			  11=IDE
100 * bit 2:    MPU401: 0=disabled, 1=enabled
101 * bits 3-4: MPU401 IRQ: 00=3, 01=5, 10=7, 11=9,
102 * bits 5-7: MPU401 port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x332,
103			  101=0x334, 110=0x336
104 */
105#define CFG3 0x63
106/*
107 * bits 0-2: CD-ROM IRQ: 000=disabled, 001=3, 010=5, 011=7, 100=9, 101=10,
108			 110=11
109 * bits 3-4: CD-ROM DMA: 00=disabled, 01=DMA0, 10=DMA1, 11=DMA3
110 * bits 5-7: CD-ROM port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x340,
111			  101=0x350, 110=0x360, 111=0x370
112 */
113
114static u8 snd_cmi8328_cfg_read(u16 port, u8 reg)
115{
116	outb(0x43, port + 3);
117	outb(0x21, port + 3);
118	outb(reg, port + 3);
119	return inb(port);
120}
121
122static void snd_cmi8328_cfg_write(u16 port, u8 reg, u8 val)
123{
124	outb(0x43, port + 3);
125	outb(0x21, port + 3);
126	outb(reg, port + 3);
127	outb(val, port + 3);	/* yes, value goes to the same port as index */
128}
129
130#ifdef CONFIG_PM
131static void snd_cmi8328_cfg_save(u16 port, u8 cfg[])
132{
133	cfg[0] = snd_cmi8328_cfg_read(port, CFG1);
134	cfg[1] = snd_cmi8328_cfg_read(port, CFG2);
135	cfg[2] = snd_cmi8328_cfg_read(port, CFG3);
136}
137
138static void snd_cmi8328_cfg_restore(u16 port, u8 cfg[])
139{
140	snd_cmi8328_cfg_write(port, CFG1, cfg[0]);
141	snd_cmi8328_cfg_write(port, CFG2, cfg[1]);
142	snd_cmi8328_cfg_write(port, CFG3, cfg[2]);
143}
144#endif /* CONFIG_PM */
145
146static int snd_cmi8328_mixer(struct snd_wss *chip)
147{
148	struct snd_card *card;
149	struct snd_ctl_elem_id id1, id2;
150	int err;
151
152	card = chip->card;
153
154	memset(&id1, 0, sizeof(id1));
155	memset(&id2, 0, sizeof(id2));
156	id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
157	/* rename AUX0 switch to CD */
158	strcpy(id1.name, "Aux Playback Switch");
159	strcpy(id2.name, "CD Playback Switch");
160	err = snd_ctl_rename_id(card, &id1, &id2);
161	if (err < 0) {
162		snd_printk(KERN_ERR "error renaming control\n");
163		return err;
164	}
165	/* rename AUX0 volume to CD */
166	strcpy(id1.name, "Aux Playback Volume");
167	strcpy(id2.name, "CD Playback Volume");
168	err = snd_ctl_rename_id(card, &id1, &id2);
169	if (err < 0) {
170		snd_printk(KERN_ERR "error renaming control\n");
171		return err;
172	}
173	/* rename AUX1 switch to Synth */
174	strcpy(id1.name, "Aux Playback Switch");
175	id1.index = 1;
176	strcpy(id2.name, "Synth Playback Switch");
177	err = snd_ctl_rename_id(card, &id1, &id2);
178	if (err < 0) {
179		snd_printk(KERN_ERR "error renaming control\n");
180		return err;
181	}
182	/* rename AUX1 volume to Synth */
183	strcpy(id1.name, "Aux Playback Volume");
184	id1.index = 1;
185	strcpy(id2.name, "Synth Playback Volume");
186	err = snd_ctl_rename_id(card, &id1, &id2);
187	if (err < 0) {
188		snd_printk(KERN_ERR "error renaming control\n");
189		return err;
190	}
191
192	return 0;
193}
194
195/* find index of an item in "-1"-ended array */
196static int array_find(const int array[], int item)
197{
198	int i;
199
200	for (i = 0; array[i] != -1; i++)
201		if (array[i] == item)
202			return i;
203
204	return -1;
205}
206/* the same for long */
207static int array_find_l(const long array[], long item)
208{
209	int i;
210
211	for (i = 0; array[i] != -1; i++)
212		if (array[i] == item)
213			return i;
214
215	return -1;
216}
217
218static int snd_cmi8328_probe(struct device *pdev, unsigned int ndev)
219{
220	struct snd_card *card;
221	struct snd_opl3 *opl3;
222	struct snd_cmi8328 *cmi;
223#ifdef SUPPORT_JOYSTICK
224	struct resource *res;
225#endif
226	int err, pos;
227	static const long mpu_ports[] = { 0x330, 0x300, 0x310, 0x320, 0x332, 0x334,
228				   0x336, -1 };
229	static const u8 mpu_port_bits[] = { 3, 0, 1, 2, 4, 5, 6 };
230	static const int mpu_irqs[] = { 9, 7, 5, 3, -1 };
231	static const u8 mpu_irq_bits[] = { 3, 2, 1, 0 };
232	static const int irqs[] = { 9, 10, 11, 7, -1 };
233	static const u8 irq_bits[] = { 2, 3, 4, 1 };
234	static const int dma1s[] = { 3, 1, 0, -1 };
235	static const u8 dma_bits[] = { 3, 2, 1 };
236	static const int dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1}, {0, -1} };
237	u16 port = cmi8328_ports[ndev];
238	u8 val;
239
240	/* 0xff is invalid configuration (but settable - hope it isn't set) */
241	if (snd_cmi8328_cfg_read(port, CFG1) == 0xff)
242		return -ENODEV;
243	/* the SB disable bit must NEVER EVER be cleared or the WSS dies */
244	snd_cmi8328_cfg_write(port, CFG1, CFG1_SB_DISABLE);
245	if (snd_cmi8328_cfg_read(port, CFG1) != CFG1_SB_DISABLE)
246		return -ENODEV;
247	/* disable everything first */
248	snd_cmi8328_cfg_write(port, CFG2, 0);	/* disable CDROM and MPU401 */
249	snd_cmi8328_cfg_write(port, CFG3, 0);	/* disable CDROM IRQ and DMA */
250
251	if (irq[ndev] == SNDRV_AUTO_IRQ) {
252		irq[ndev] = snd_legacy_find_free_irq(irqs);
253		if (irq[ndev] < 0) {
254			snd_printk(KERN_ERR "unable to find a free IRQ\n");
255			return -EBUSY;
256		}
257	}
258	if (dma1[ndev] == SNDRV_AUTO_DMA) {
259		dma1[ndev] = snd_legacy_find_free_dma(dma1s);
260		if (dma1[ndev] < 0) {
261			snd_printk(KERN_ERR "unable to find a free DMA1\n");
262			return -EBUSY;
263		}
264	}
265	if (dma2[ndev] == SNDRV_AUTO_DMA) {
266		dma2[ndev] = snd_legacy_find_free_dma(dma2s[dma1[ndev] % 4]);
267		if (dma2[ndev] < 0) {
268			snd_printk(KERN_WARNING "unable to find a free DMA2, full-duplex will not work\n");
269			dma2[ndev] = -1;
270		}
271	}
272	/* configure WSS IRQ... */
273	pos = array_find(irqs, irq[ndev]);
274	if (pos < 0) {
275		snd_printk(KERN_ERR "invalid IRQ %d\n", irq[ndev]);
276		return -EINVAL;
277	}
278	val = irq_bits[pos] << 3;
279	/* ...and DMA... */
280	pos = array_find(dma1s, dma1[ndev]);
281	if (pos < 0) {
282		snd_printk(KERN_ERR "invalid DMA1 %d\n", dma1[ndev]);
283		return -EINVAL;
284	}
285	val |= dma_bits[pos];
286	/* ...and DMA2 */
287	if (dma2[ndev] >= 0 && dma1[ndev] != dma2[ndev]) {
288		pos = array_find(dma2s[dma1[ndev]], dma2[ndev]);
289		if (pos < 0) {
290			snd_printk(KERN_ERR "invalid DMA2 %d\n", dma2[ndev]);
291			return -EINVAL;
292		}
293		val |= 0x04; /* enable separate capture DMA */
294	}
295	outb(val, port);
296
297	err = snd_devm_card_new(pdev, index[ndev], id[ndev], THIS_MODULE,
298				sizeof(struct snd_cmi8328), &card);
299	if (err < 0)
300		return err;
301	cmi = card->private_data;
302	cmi->card = card;
303	cmi->port = port;
304	cmi->wss_cfg = val;
305
306	err = snd_wss_create(card, port + 4, -1, irq[ndev], dma1[ndev],
307			dma2[ndev], WSS_HW_DETECT, 0, &cmi->wss);
308	if (err < 0)
309		return err;
310
311	err = snd_wss_pcm(cmi->wss, 0);
312	if (err < 0)
313		return err;
314
315	err = snd_wss_mixer(cmi->wss);
316	if (err < 0)
317		return err;
318	err = snd_cmi8328_mixer(cmi->wss);
319	if (err < 0)
320		return err;
321
322	if (snd_wss_timer(cmi->wss, 0) < 0)
323		snd_printk(KERN_WARNING "error initializing WSS timer\n");
324
325	if (mpuport[ndev] == SNDRV_AUTO_PORT) {
326		mpuport[ndev] = snd_legacy_find_free_ioport(mpu_ports, 2);
327		if (mpuport[ndev] < 0)
328			snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
329	}
330	if (mpuirq[ndev] == SNDRV_AUTO_IRQ) {
331		mpuirq[ndev] = snd_legacy_find_free_irq(mpu_irqs);
332		if (mpuirq[ndev] < 0)
333			snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
334	}
335	/* enable and configure MPU401 */
336	if (mpuport[ndev] > 0 && mpuirq[ndev] > 0) {
337		val = CFG2_MPU_ENABLE;
338		pos = array_find_l(mpu_ports, mpuport[ndev]);
339		if (pos < 0)
340			snd_printk(KERN_WARNING "invalid MPU401 port 0x%lx\n",
341								mpuport[ndev]);
342		else {
343			val |= mpu_port_bits[pos] << 5;
344			pos = array_find(mpu_irqs, mpuirq[ndev]);
345			if (pos < 0)
346				snd_printk(KERN_WARNING "invalid MPU401 IRQ %d\n",
347								mpuirq[ndev]);
348			else {
349				val |= mpu_irq_bits[pos] << 3;
350				snd_cmi8328_cfg_write(port, CFG2, val);
351				if (snd_mpu401_uart_new(card, 0,
352						MPU401_HW_MPU401, mpuport[ndev],
353						0, mpuirq[ndev], NULL) < 0)
354					snd_printk(KERN_ERR "error initializing MPU401\n");
355			}
356		}
357	}
358	/* OPL3 is hardwired to 0x388 and cannot be disabled */
359	if (snd_opl3_create(card, 0x388, 0x38a, OPL3_HW_AUTO, 0, &opl3) < 0)
360		snd_printk(KERN_ERR "error initializing OPL3\n");
361	else
362		if (snd_opl3_hwdep_new(opl3, 0, 1, NULL) < 0)
363			snd_printk(KERN_WARNING "error initializing OPL3 hwdep\n");
364
365	strcpy(card->driver, "CMI8328");
366	strcpy(card->shortname, "C-Media CMI8328");
367	sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d,%d",
368		card->shortname, cmi->wss->port, irq[ndev], dma1[ndev],
369		(dma2[ndev] >= 0) ? dma2[ndev] : dma1[ndev]);
370
371	dev_set_drvdata(pdev, card);
372	err = snd_card_register(card);
373	if (err < 0)
374		return err;
375#ifdef SUPPORT_JOYSTICK
376	if (!gameport[ndev])
377		return 0;
378	/* gameport is hardwired to 0x200 */
379	res = devm_request_region(pdev, 0x200, 8, "CMI8328 gameport");
380	if (!res)
381		snd_printk(KERN_WARNING "unable to allocate gameport I/O port\n");
382	else {
383		struct gameport *gp = cmi->gameport = gameport_allocate_port();
384		if (cmi->gameport) {
 
 
385			gameport_set_name(gp, "CMI8328 Gameport");
386			gameport_set_phys(gp, "%s/gameport0", dev_name(pdev));
387			gameport_set_dev_parent(gp, pdev);
388			gp->io = 0x200;
 
389			/* Enable gameport */
390			snd_cmi8328_cfg_write(port, CFG1,
391					CFG1_SB_DISABLE | CFG1_GAMEPORT);
392			gameport_register_port(gp);
393		}
394	}
395#endif
396	return 0;
 
 
 
 
397}
398
399static void snd_cmi8328_remove(struct device *pdev, unsigned int dev)
400{
401	struct snd_card *card = dev_get_drvdata(pdev);
402	struct snd_cmi8328 *cmi = card->private_data;
403
404#ifdef SUPPORT_JOYSTICK
405	if (cmi->gameport)
 
406		gameport_unregister_port(cmi->gameport);
 
 
407#endif
408	/* disable everything */
409	snd_cmi8328_cfg_write(cmi->port, CFG1, CFG1_SB_DISABLE);
410	snd_cmi8328_cfg_write(cmi->port, CFG2, 0);
411	snd_cmi8328_cfg_write(cmi->port, CFG3, 0);
 
 
412}
413
414#ifdef CONFIG_PM
415static int snd_cmi8328_suspend(struct device *pdev, unsigned int n,
416				pm_message_t state)
417{
418	struct snd_card *card = dev_get_drvdata(pdev);
419	struct snd_cmi8328 *cmi;
420
421	if (!card)	/* ignore absent devices */
422		return 0;
423	cmi = card->private_data;
424	snd_cmi8328_cfg_save(cmi->port, cmi->cfg);
425	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
 
426	cmi->wss->suspend(cmi->wss);
427
428	return 0;
429}
430
431static int snd_cmi8328_resume(struct device *pdev, unsigned int n)
432{
433	struct snd_card *card = dev_get_drvdata(pdev);
434	struct snd_cmi8328 *cmi;
435
436	if (!card)	/* ignore absent devices */
437		return 0;
438	cmi = card->private_data;
439	snd_cmi8328_cfg_restore(cmi->port, cmi->cfg);
440	outb(cmi->wss_cfg, cmi->port);
441	cmi->wss->resume(cmi->wss);
442	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
443
444	return 0;
445}
446#endif
447
448static struct isa_driver snd_cmi8328_driver = {
449	.probe		= snd_cmi8328_probe,
450	.remove		= snd_cmi8328_remove,
451#ifdef CONFIG_PM
452	.suspend	= snd_cmi8328_suspend,
453	.resume		= snd_cmi8328_resume,
454#endif
455	.driver		= {
456		.name	= "cmi8328"
457	},
458};
459
460module_isa_driver(snd_cmi8328_driver, CMI8328_MAX);
 
 
 
 
 
 
 
 
 
 
 
v4.6
 
  1/*
  2 * Driver for C-Media CMI8328-based soundcards, such as AudioExcel AV500
  3 * Copyright (c) 2012 Ondrej Zary
  4 *
  5 * AudioExcel AV500 card consists of:
  6 *  - CMI8328 - main chip (SB Pro emulation, gameport, OPL3, MPU401, CD-ROM)
  7 *  - CS4231A - WSS codec
  8 *  - Dream SAM9233+GMS950400+RAM+ROM: Wavetable MIDI, connected to MPU401
  9 */
 10
 11#include <linux/init.h>
 12#include <linux/isa.h>
 13#include <linux/module.h>
 14#include <linux/gameport.h>
 15#include <asm/dma.h>
 16#include <sound/core.h>
 17#include <sound/wss.h>
 18#include <sound/opl3.h>
 19#include <sound/mpu401.h>
 20#define SNDRV_LEGACY_FIND_FREE_IOPORT
 21#define SNDRV_LEGACY_FIND_FREE_IRQ
 22#define SNDRV_LEGACY_FIND_FREE_DMA
 23#include <sound/initval.h>
 24
 25MODULE_AUTHOR("Ondrej Zary <linux@rainbow-software.org>");
 26MODULE_DESCRIPTION("C-Media CMI8328");
 27MODULE_LICENSE("GPL");
 28
 29#if defined(CONFIG_GAMEPORT) || defined(CONFIG_GAMEPORT_MODULE)
 30#define SUPPORT_JOYSTICK 1
 31#endif
 32
 33/* I/O port is configured by jumpers on the card to one of these */
 34static int cmi8328_ports[] = { 0x530, 0xe80, 0xf40, 0x604 };
 35#define CMI8328_MAX	ARRAY_SIZE(cmi8328_ports)
 36
 37static int index[CMI8328_MAX] =     {[0 ... (CMI8328_MAX-1)] = -1};
 38static char *id[CMI8328_MAX] =      {[0 ... (CMI8328_MAX-1)] = NULL};
 39static long port[CMI8328_MAX] =     {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT};
 40static int irq[CMI8328_MAX] =       {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ};
 41static int dma1[CMI8328_MAX] =      {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA};
 42static int dma2[CMI8328_MAX] =      {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA};
 43static long mpuport[CMI8328_MAX] =  {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT};
 44static int mpuirq[CMI8328_MAX] =    {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ};
 45#ifdef SUPPORT_JOYSTICK
 46static bool gameport[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = true};
 47#endif
 48
 49module_param_array(index, int, NULL, 0444);
 50MODULE_PARM_DESC(index, "Index value for CMI8328 soundcard.");
 51module_param_array(id, charp, NULL, 0444);
 52MODULE_PARM_DESC(id, "ID string for CMI8328 soundcard.");
 53
 54module_param_array(port, long, NULL, 0444);
 55MODULE_PARM_DESC(port, "Port # for CMI8328 driver.");
 56module_param_array(irq, int, NULL, 0444);
 57MODULE_PARM_DESC(irq, "IRQ # for CMI8328 driver.");
 58module_param_array(dma1, int, NULL, 0444);
 59MODULE_PARM_DESC(dma1, "DMA1 for CMI8328 driver.");
 60module_param_array(dma2, int, NULL, 0444);
 61MODULE_PARM_DESC(dma2, "DMA2 for CMI8328 driver.");
 62
 63module_param_array(mpuport, long, NULL, 0444);
 64MODULE_PARM_DESC(mpuport, "MPU-401 port # for CMI8328 driver.");
 65module_param_array(mpuirq, int, NULL, 0444);
 66MODULE_PARM_DESC(mpuirq, "IRQ # for CMI8328 MPU-401 port.");
 67#ifdef SUPPORT_JOYSTICK
 68module_param_array(gameport, bool, NULL, 0444);
 69MODULE_PARM_DESC(gameport, "Enable gameport.");
 70#endif
 71
 72struct snd_cmi8328 {
 73	u16 port;
 74	u8 cfg[3];
 75	u8 wss_cfg;
 76	struct snd_card *card;
 77	struct snd_wss *wss;
 78#ifdef SUPPORT_JOYSTICK
 79	struct gameport *gameport;
 80#endif
 81};
 82
 83/* CMI8328 configuration registers */
 84#define CFG1 0x61
 85#define CFG1_SB_DISABLE	(1 << 0)
 86#define CFG1_GAMEPORT	(1 << 1)
 87/*
 88 * bit 0:    SB: 0=enabled, 1=disabled
 89 * bit 1:    gameport: 0=disabled, 1=enabled
 90 * bits 2-4: SB IRQ: 001=3, 010=5, 011=7, 100=9, 101=10, 110=11
 91 * bits 5-6: SB DMA: 00=disabled (when SB disabled), 01=DMA0, 10=DMA1, 11=DMA3
 92 * bit 7:    SB port: 0=0x220, 1=0x240
 93 */
 94#define CFG2 0x62
 95#define CFG2_MPU_ENABLE (1 << 2)
 96/*
 97 * bits 0-1: CD-ROM mode: 00=disabled, 01=Panasonic, 10=Sony/Mitsumi/Wearnes,
 98			  11=IDE
 99 * bit 2:    MPU401: 0=disabled, 1=enabled
100 * bits 3-4: MPU401 IRQ: 00=3, 01=5, 10=7, 11=9,
101 * bits 5-7: MPU401 port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x332,
102			  101=0x334, 110=0x336
103 */
104#define CFG3 0x63
105/*
106 * bits 0-2: CD-ROM IRQ: 000=disabled, 001=3, 010=5, 011=7, 100=9, 101=10,
107			 110=11
108 * bits 3-4: CD-ROM DMA: 00=disabled, 01=DMA0, 10=DMA1, 11=DMA3
109 * bits 5-7: CD-ROM port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x340,
110			  101=0x350, 110=0x360, 111=0x370
111 */
112
113static u8 snd_cmi8328_cfg_read(u16 port, u8 reg)
114{
115	outb(0x43, port + 3);
116	outb(0x21, port + 3);
117	outb(reg, port + 3);
118	return inb(port);
119}
120
121static void snd_cmi8328_cfg_write(u16 port, u8 reg, u8 val)
122{
123	outb(0x43, port + 3);
124	outb(0x21, port + 3);
125	outb(reg, port + 3);
126	outb(val, port + 3);	/* yes, value goes to the same port as index */
127}
128
129#ifdef CONFIG_PM
130static void snd_cmi8328_cfg_save(u16 port, u8 cfg[])
131{
132	cfg[0] = snd_cmi8328_cfg_read(port, CFG1);
133	cfg[1] = snd_cmi8328_cfg_read(port, CFG2);
134	cfg[2] = snd_cmi8328_cfg_read(port, CFG3);
135}
136
137static void snd_cmi8328_cfg_restore(u16 port, u8 cfg[])
138{
139	snd_cmi8328_cfg_write(port, CFG1, cfg[0]);
140	snd_cmi8328_cfg_write(port, CFG2, cfg[1]);
141	snd_cmi8328_cfg_write(port, CFG3, cfg[2]);
142}
143#endif /* CONFIG_PM */
144
145static int snd_cmi8328_mixer(struct snd_wss *chip)
146{
147	struct snd_card *card;
148	struct snd_ctl_elem_id id1, id2;
149	int err;
150
151	card = chip->card;
152
153	memset(&id1, 0, sizeof(id1));
154	memset(&id2, 0, sizeof(id2));
155	id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
156	/* rename AUX0 switch to CD */
157	strcpy(id1.name, "Aux Playback Switch");
158	strcpy(id2.name, "CD Playback Switch");
159	err = snd_ctl_rename_id(card, &id1, &id2);
160	if (err < 0) {
161		snd_printk(KERN_ERR "error renaming control\n");
162		return err;
163	}
164	/* rename AUX0 volume to CD */
165	strcpy(id1.name, "Aux Playback Volume");
166	strcpy(id2.name, "CD Playback Volume");
167	err = snd_ctl_rename_id(card, &id1, &id2);
168	if (err < 0) {
169		snd_printk(KERN_ERR "error renaming control\n");
170		return err;
171	}
172	/* rename AUX1 switch to Synth */
173	strcpy(id1.name, "Aux Playback Switch");
174	id1.index = 1;
175	strcpy(id2.name, "Synth Playback Switch");
176	err = snd_ctl_rename_id(card, &id1, &id2);
177	if (err < 0) {
178		snd_printk(KERN_ERR "error renaming control\n");
179		return err;
180	}
181	/* rename AUX1 volume to Synth */
182	strcpy(id1.name, "Aux Playback Volume");
183	id1.index = 1;
184	strcpy(id2.name, "Synth Playback Volume");
185	err = snd_ctl_rename_id(card, &id1, &id2);
186	if (err < 0) {
187		snd_printk(KERN_ERR "error renaming control\n");
188		return err;
189	}
190
191	return 0;
192}
193
194/* find index of an item in "-1"-ended array */
195int array_find(int array[], int item)
196{
197	int i;
198
199	for (i = 0; array[i] != -1; i++)
200		if (array[i] == item)
201			return i;
202
203	return -1;
204}
205/* the same for long */
206int array_find_l(long array[], long item)
207{
208	int i;
209
210	for (i = 0; array[i] != -1; i++)
211		if (array[i] == item)
212			return i;
213
214	return -1;
215}
216
217static int snd_cmi8328_probe(struct device *pdev, unsigned int ndev)
218{
219	struct snd_card *card;
220	struct snd_opl3 *opl3;
221	struct snd_cmi8328 *cmi;
222#ifdef SUPPORT_JOYSTICK
223	struct resource *res;
224#endif
225	int err, pos;
226	static long mpu_ports[] = { 0x330, 0x300, 0x310, 0x320, 0x332, 0x334,
227				   0x336, -1 };
228	static u8 mpu_port_bits[] = { 3, 0, 1, 2, 4, 5, 6 };
229	static int mpu_irqs[] = { 9, 7, 5, 3, -1 };
230	static u8 mpu_irq_bits[] = { 3, 2, 1, 0 };
231	static int irqs[] = { 9, 10, 11, 7, -1 };
232	static u8 irq_bits[] = { 2, 3, 4, 1 };
233	static int dma1s[] = { 3, 1, 0, -1 };
234	static u8 dma_bits[] = { 3, 2, 1 };
235	static int dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1}, {0, -1} };
236	u16 port = cmi8328_ports[ndev];
237	u8 val;
238
239	/* 0xff is invalid configuration (but settable - hope it isn't set) */
240	if (snd_cmi8328_cfg_read(port, CFG1) == 0xff)
241		return -ENODEV;
242	/* the SB disable bit must NEVER EVER be cleared or the WSS dies */
243	snd_cmi8328_cfg_write(port, CFG1, CFG1_SB_DISABLE);
244	if (snd_cmi8328_cfg_read(port, CFG1) != CFG1_SB_DISABLE)
245		return -ENODEV;
246	/* disable everything first */
247	snd_cmi8328_cfg_write(port, CFG2, 0);	/* disable CDROM and MPU401 */
248	snd_cmi8328_cfg_write(port, CFG3, 0);	/* disable CDROM IRQ and DMA */
249
250	if (irq[ndev] == SNDRV_AUTO_IRQ) {
251		irq[ndev] = snd_legacy_find_free_irq(irqs);
252		if (irq[ndev] < 0) {
253			snd_printk(KERN_ERR "unable to find a free IRQ\n");
254			return -EBUSY;
255		}
256	}
257	if (dma1[ndev] == SNDRV_AUTO_DMA) {
258		dma1[ndev] = snd_legacy_find_free_dma(dma1s);
259		if (dma1[ndev] < 0) {
260			snd_printk(KERN_ERR "unable to find a free DMA1\n");
261			return -EBUSY;
262		}
263	}
264	if (dma2[ndev] == SNDRV_AUTO_DMA) {
265		dma2[ndev] = snd_legacy_find_free_dma(dma2s[dma1[ndev] % 4]);
266		if (dma2[ndev] < 0) {
267			snd_printk(KERN_WARNING "unable to find a free DMA2, full-duplex will not work\n");
268			dma2[ndev] = -1;
269		}
270	}
271	/* configure WSS IRQ... */
272	pos = array_find(irqs, irq[ndev]);
273	if (pos < 0) {
274		snd_printk(KERN_ERR "invalid IRQ %d\n", irq[ndev]);
275		return -EINVAL;
276	}
277	val = irq_bits[pos] << 3;
278	/* ...and DMA... */
279	pos = array_find(dma1s, dma1[ndev]);
280	if (pos < 0) {
281		snd_printk(KERN_ERR "invalid DMA1 %d\n", dma1[ndev]);
282		return -EINVAL;
283	}
284	val |= dma_bits[pos];
285	/* ...and DMA2 */
286	if (dma2[ndev] >= 0 && dma1[ndev] != dma2[ndev]) {
287		pos = array_find(dma2s[dma1[ndev]], dma2[ndev]);
288		if (pos < 0) {
289			snd_printk(KERN_ERR "invalid DMA2 %d\n", dma2[ndev]);
290			return -EINVAL;
291		}
292		val |= 0x04; /* enable separate capture DMA */
293	}
294	outb(val, port);
295
296	err = snd_card_new(pdev, index[ndev], id[ndev], THIS_MODULE,
297			   sizeof(struct snd_cmi8328), &card);
298	if (err < 0)
299		return err;
300	cmi = card->private_data;
301	cmi->card = card;
302	cmi->port = port;
303	cmi->wss_cfg = val;
304
305	err = snd_wss_create(card, port + 4, -1, irq[ndev], dma1[ndev],
306			dma2[ndev], WSS_HW_DETECT, 0, &cmi->wss);
307	if (err < 0)
308		goto error;
309
310	err = snd_wss_pcm(cmi->wss, 0);
311	if (err < 0)
312		goto error;
313
314	err = snd_wss_mixer(cmi->wss);
315	if (err < 0)
316		goto error;
317	err = snd_cmi8328_mixer(cmi->wss);
318	if (err < 0)
319		goto error;
320
321	if (snd_wss_timer(cmi->wss, 0) < 0)
322		snd_printk(KERN_WARNING "error initializing WSS timer\n");
323
324	if (mpuport[ndev] == SNDRV_AUTO_PORT) {
325		mpuport[ndev] = snd_legacy_find_free_ioport(mpu_ports, 2);
326		if (mpuport[ndev] < 0)
327			snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
328	}
329	if (mpuirq[ndev] == SNDRV_AUTO_IRQ) {
330		mpuirq[ndev] = snd_legacy_find_free_irq(mpu_irqs);
331		if (mpuirq[ndev] < 0)
332			snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
333	}
334	/* enable and configure MPU401 */
335	if (mpuport[ndev] > 0 && mpuirq[ndev] > 0) {
336		val = CFG2_MPU_ENABLE;
337		pos = array_find_l(mpu_ports, mpuport[ndev]);
338		if (pos < 0)
339			snd_printk(KERN_WARNING "invalid MPU401 port 0x%lx\n",
340								mpuport[ndev]);
341		else {
342			val |= mpu_port_bits[pos] << 5;
343			pos = array_find(mpu_irqs, mpuirq[ndev]);
344			if (pos < 0)
345				snd_printk(KERN_WARNING "invalid MPU401 IRQ %d\n",
346								mpuirq[ndev]);
347			else {
348				val |= mpu_irq_bits[pos] << 3;
349				snd_cmi8328_cfg_write(port, CFG2, val);
350				if (snd_mpu401_uart_new(card, 0,
351						MPU401_HW_MPU401, mpuport[ndev],
352						0, mpuirq[ndev], NULL) < 0)
353					snd_printk(KERN_ERR "error initializing MPU401\n");
354			}
355		}
356	}
357	/* OPL3 is hardwired to 0x388 and cannot be disabled */
358	if (snd_opl3_create(card, 0x388, 0x38a, OPL3_HW_AUTO, 0, &opl3) < 0)
359		snd_printk(KERN_ERR "error initializing OPL3\n");
360	else
361		if (snd_opl3_hwdep_new(opl3, 0, 1, NULL) < 0)
362			snd_printk(KERN_WARNING "error initializing OPL3 hwdep\n");
363
364	strcpy(card->driver, "CMI8328");
365	strcpy(card->shortname, "C-Media CMI8328");
366	sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d,%d",
367		card->shortname, cmi->wss->port, irq[ndev], dma1[ndev],
368		(dma2[ndev] >= 0) ? dma2[ndev] : dma1[ndev]);
369
370	dev_set_drvdata(pdev, card);
371	err = snd_card_register(card);
372	if (err < 0)
373		goto error;
374#ifdef SUPPORT_JOYSTICK
375	if (!gameport[ndev])
376		return 0;
377	/* gameport is hardwired to 0x200 */
378	res = request_region(0x200, 8, "CMI8328 gameport");
379	if (!res)
380		snd_printk(KERN_WARNING "unable to allocate gameport I/O port\n");
381	else {
382		struct gameport *gp = cmi->gameport = gameport_allocate_port();
383		if (!cmi->gameport)
384			release_and_free_resource(res);
385		else {
386			gameport_set_name(gp, "CMI8328 Gameport");
387			gameport_set_phys(gp, "%s/gameport0", dev_name(pdev));
388			gameport_set_dev_parent(gp, pdev);
389			gp->io = 0x200;
390			gameport_set_port_data(gp, res);
391			/* Enable gameport */
392			snd_cmi8328_cfg_write(port, CFG1,
393					CFG1_SB_DISABLE | CFG1_GAMEPORT);
394			gameport_register_port(gp);
395		}
396	}
397#endif
398	return 0;
399error:
400	snd_card_free(card);
401
402	return err;
403}
404
405static int snd_cmi8328_remove(struct device *pdev, unsigned int dev)
406{
407	struct snd_card *card = dev_get_drvdata(pdev);
408	struct snd_cmi8328 *cmi = card->private_data;
409
410#ifdef SUPPORT_JOYSTICK
411	if (cmi->gameport) {
412		struct resource *res = gameport_get_port_data(cmi->gameport);
413		gameport_unregister_port(cmi->gameport);
414		release_and_free_resource(res);
415	}
416#endif
417	/* disable everything */
418	snd_cmi8328_cfg_write(cmi->port, CFG1, CFG1_SB_DISABLE);
419	snd_cmi8328_cfg_write(cmi->port, CFG2, 0);
420	snd_cmi8328_cfg_write(cmi->port, CFG3, 0);
421	snd_card_free(card);
422	return 0;
423}
424
425#ifdef CONFIG_PM
426static int snd_cmi8328_suspend(struct device *pdev, unsigned int n,
427				pm_message_t state)
428{
429	struct snd_card *card = dev_get_drvdata(pdev);
430	struct snd_cmi8328 *cmi;
431
432	if (!card)	/* ignore absent devices */
433		return 0;
434	cmi = card->private_data;
435	snd_cmi8328_cfg_save(cmi->port, cmi->cfg);
436	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
437	snd_pcm_suspend_all(cmi->wss->pcm);
438	cmi->wss->suspend(cmi->wss);
439
440	return 0;
441}
442
443static int snd_cmi8328_resume(struct device *pdev, unsigned int n)
444{
445	struct snd_card *card = dev_get_drvdata(pdev);
446	struct snd_cmi8328 *cmi;
447
448	if (!card)	/* ignore absent devices */
449		return 0;
450	cmi = card->private_data;
451	snd_cmi8328_cfg_restore(cmi->port, cmi->cfg);
452	outb(cmi->wss_cfg, cmi->port);
453	cmi->wss->resume(cmi->wss);
454	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
455
456	return 0;
457}
458#endif
459
460static struct isa_driver snd_cmi8328_driver = {
461	.probe		= snd_cmi8328_probe,
462	.remove		= snd_cmi8328_remove,
463#ifdef CONFIG_PM
464	.suspend	= snd_cmi8328_suspend,
465	.resume		= snd_cmi8328_resume,
466#endif
467	.driver		= {
468		.name	= "cmi8328"
469	},
470};
471
472static int __init alsa_card_cmi8328_init(void)
473{
474	return isa_register_driver(&snd_cmi8328_driver, CMI8328_MAX);
475}
476
477static void __exit alsa_card_cmi8328_exit(void)
478{
479	isa_unregister_driver(&snd_cmi8328_driver);
480}
481
482module_init(alsa_card_cmi8328_init)
483module_exit(alsa_card_cmi8328_exit)