2 * Driver for Sound Core PDAudioCF soundcard
4 * Copyright (c) 2003 by Jaroslav Kysela <perex@suse.cz>
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.
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.
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
21 #include <sound/driver.h>
22 #include <sound/core.h>
23 #include <linux/slab.h>
24 #include <linux/moduleparam.h>
25 #include <pcmcia/version.h>
26 #include <pcmcia/ciscode.h>
27 #include <pcmcia/cisreg.h>
28 #include "pdaudiocf.h"
29 #include <sound/initval.h>
30 #include <linux/init.h>
35 #define CARD_NAME "PDAudio-CF"
37 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
38 MODULE_DESCRIPTION("Sound Core " CARD_NAME);
39 MODULE_LICENSE("GPL");
40 MODULE_SUPPORTED_DEVICE("{{Sound Core," CARD_NAME "}}");
42 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
43 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
44 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable switches */
45 static unsigned int irq_mask = 0xffff;
46 static int irq_list[4] = { -1 };
49 module_param_array(index, int, boot_devs, 0444);
50 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
51 module_param_array(id, charp, boot_devs, 0444);
52 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
53 module_param_array(enable, bool, boot_devs, 0444);
54 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
55 module_param(irq_mask, int, 0444);
56 MODULE_PARM_DESC(irq_mask, "IRQ bitmask for " CARD_NAME " soundcard.");
57 module_param_array(irq_list, int, boot_devs, 0444);
58 MODULE_PARM_DESC(irq_list, "List of Available interrupts for " CARD_NAME " soundcard.");
64 static dev_info_t dev_info = "snd-pdaudiocf";
65 static snd_card_t *card_list[SNDRV_CARDS];
66 static dev_link_t *dev_list;
71 static void pdacf_config(dev_link_t *link);
72 static int pdacf_event(event_t event, int priority, event_callback_args_t *args);
73 static void snd_pdacf_detach(dev_link_t *link);
75 static void pdacf_release(dev_link_t *link)
77 if (link->state & DEV_CONFIG) {
78 /* release cs resources */
79 pcmcia_release_configuration(link->handle);
80 pcmcia_release_io(link->handle, &link->io);
81 pcmcia_release_irq(link->handle, &link->irq);
82 link->state &= ~DEV_CONFIG;
89 static int snd_pdacf_free(pdacf_t *pdacf)
91 dev_link_t *link = &pdacf->link;
95 /* Break the link with Card Services */
97 pcmcia_deregister_client(link->handle);
99 card_list[pdacf->index] = NULL;
106 static int snd_pdacf_dev_free(snd_device_t *device)
108 pdacf_t *chip = device->device_data;
109 return snd_pdacf_free(chip);
113 * snd_pdacf_attach - attach callback for cs
115 static dev_link_t *snd_pdacf_attach(void)
117 client_reg_t client_reg; /* Register with cardmgr */
118 dev_link_t *link; /* Info for cardmgr */
122 static snd_device_ops_t ops = {
123 .dev_free = snd_pdacf_dev_free,
126 snd_printdd(KERN_DEBUG "pdacf_attach called\n");
127 /* find an empty slot from the card list */
128 for (i = 0; i < SNDRV_CARDS; i++) {
132 if (i >= SNDRV_CARDS) {
133 snd_printk(KERN_ERR "pdacf: too many cards found\n");
137 return NULL; /* disabled explicitly */
139 /* ok, create a card instance */
140 card = snd_card_new(index[i], id[i], THIS_MODULE, 0);
142 snd_printk(KERN_ERR "pdacf: cannot create a card instance\n");
146 pdacf = snd_pdacf_create(card);
150 if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, pdacf, &ops) < 0) {
162 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
163 link->io.NumPorts1 = 16;
165 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT | IRQ_FORCED_PULSE;
166 // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
168 link->irq.IRQInfo1 = IRQ_INFO2_VALID /* | IRQ_LEVEL_ID */;
169 if (irq_list[0] == -1)
170 link->irq.IRQInfo2 = irq_mask;
172 for (i = 0; i < 4; i++)
173 link->irq.IRQInfo2 |= 1 << irq_list[i];
174 link->irq.Handler = pdacf_interrupt;
175 link->irq.Instance = pdacf;
176 link->conf.Attributes = CONF_ENABLE_IRQ;
177 link->conf.IntType = INT_MEMORY_AND_IO;
178 link->conf.ConfigIndex = 1;
179 link->conf.Present = PRESENT_OPTION;
182 link->next = dev_list;
185 /* Register with Card Services */
186 client_reg.dev_info = &dev_info;
187 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
188 client_reg.EventMask =
189 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL
191 | CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET
192 | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME
195 client_reg.event_handler = &pdacf_event;
196 client_reg.Version = 0x0210;
197 client_reg.event_callback_args.client_data = link;
199 ret = pcmcia_register_client(&link->handle, &client_reg);
200 if (ret != CS_SUCCESS) {
201 cs_error(link->handle, RegisterClient, ret);
202 snd_pdacf_detach(link);
211 * snd_pdacf_assign_resources - initialize the hardware and card instance.
212 * @port: i/o port for the card
213 * @irq: irq number for the card
215 * this function assigns the specified port and irq, boot the card,
216 * create pcm and control instances, and initialize the rest hardware.
218 * returns 0 if successful, or a negative error code.
220 static int snd_pdacf_assign_resources(pdacf_t *pdacf, int port, int irq)
223 snd_card_t *card = pdacf->card;
225 snd_printdd(KERN_DEBUG "pdacf assign resources: port = 0x%x, irq = %d\n", port, irq);
228 pdacf->chip_status |= PDAUDIOCF_STAT_IS_CONFIGURED;
230 err = snd_pdacf_ak4117_create(pdacf);
234 strcpy(card->driver, "PDAudio-CF");
235 sprintf(card->shortname, "Core Sound %s", card->driver);
236 sprintf(card->longname, "%s at 0x%x, irq %i",
237 card->shortname, port, irq);
239 err = snd_pdacf_pcm_new(pdacf);
243 snd_card_set_pm_callback(card, snd_pdacf_suspend, snd_pdacf_resume, pdacf);
245 if ((err = snd_card_register(card)) < 0)
253 * snd_pdacf_detach - detach callback for cs
255 static void snd_pdacf_detach(dev_link_t *link)
257 pdacf_t *chip = link->priv;
259 snd_printdd(KERN_DEBUG "pdacf_detach called\n");
260 /* Remove the interface data from the linked list */
263 /* Locate device structure */
264 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
270 if (chip->chip_status & PDAUDIOCF_STAT_IS_CONFIGURED)
271 snd_pdacf_powerdown(chip);
272 chip->chip_status |= PDAUDIOCF_STAT_IS_STALE; /* to be sure */
273 snd_card_disconnect(chip->card);
274 snd_card_free_in_thread(chip->card);
278 * snd_pdacf_detach_all - detach all instances linked to the hw
280 static void snd_pdacf_detach_all(void)
282 while (dev_list != NULL)
283 snd_pdacf_detach(dev_list);
287 * configuration callback
290 #define CS_CHECK(fn, ret) \
291 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
293 static void pdacf_config(dev_link_t *link)
295 client_handle_t handle = link->handle;
296 pdacf_t *pdacf = link->priv;
301 int last_fn, last_ret;
303 snd_printdd(KERN_DEBUG "pdacf_config called\n");
304 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
305 tuple.Attributes = 0;
306 tuple.TupleData = (cisdata_t *)buf;
307 tuple.TupleDataMax = sizeof(buf);
308 tuple.TupleOffset = 0;
309 tuple.DesiredTuple = CISTPL_CONFIG;
310 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
311 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
312 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
313 link->conf.ConfigBase = parse.config.base;
314 link->conf.ConfigIndex = 0x5;
316 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
317 link->conf.Vcc = conf.Vcc;
320 link->state |= DEV_CONFIG;
322 CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
323 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
324 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
326 if (snd_pdacf_assign_resources(pdacf, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
329 link->dev = &pdacf->node;
330 link->state &= ~DEV_CONFIG_PENDING;
334 cs_error(link->handle, last_fn, last_ret);
336 pcmcia_release_configuration(link->handle);
337 pcmcia_release_io(link->handle, &link->io);
338 pcmcia_release_irq(link->handle, &link->irq);
344 static int pdacf_event(event_t event, int priority, event_callback_args_t *args)
346 dev_link_t *link = args->client_data;
347 pdacf_t *chip = link->priv;
350 case CS_EVENT_CARD_REMOVAL:
351 snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
352 link->state &= ~DEV_PRESENT;
353 if (link->state & DEV_CONFIG) {
354 chip->chip_status |= PDAUDIOCF_STAT_IS_STALE;
357 case CS_EVENT_CARD_INSERTION:
358 snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
359 link->state |= DEV_PRESENT;
363 case CS_EVENT_PM_SUSPEND:
364 snd_printdd(KERN_DEBUG "SUSPEND\n");
365 link->state |= DEV_SUSPEND;
367 snd_printdd(KERN_DEBUG "snd_pdacf_suspend calling\n");
368 snd_pdacf_suspend(chip->card, 0);
370 /* Fall through... */
371 case CS_EVENT_RESET_PHYSICAL:
372 snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
373 if (link->state & DEV_CONFIG)
374 pcmcia_release_configuration(link->handle);
376 case CS_EVENT_PM_RESUME:
377 snd_printdd(KERN_DEBUG "RESUME\n");
378 link->state &= ~DEV_SUSPEND;
379 /* Fall through... */
380 case CS_EVENT_CARD_RESET:
381 snd_printdd(KERN_DEBUG "CARD_RESET\n");
383 snd_printdd(KERN_DEBUG "requestconfig...\n");
384 pcmcia_request_configuration(link->handle, &link->conf);
386 snd_printdd(KERN_DEBUG "calling snd_pdacf_resume\n");
387 snd_pdacf_resume(chip->card, 0);
390 snd_printdd(KERN_DEBUG "resume done!\n");
398 * Module entry points
400 static struct pcmcia_driver pdacf_cs_driver = {
401 .owner = THIS_MODULE,
403 .name = "snd-pdaudiocf",
405 .attach = snd_pdacf_attach,
406 .detach = snd_pdacf_detach
409 static int __init init_pdacf(void)
411 return pcmcia_register_driver(&pdacf_cs_driver);
414 static void __exit exit_pdacf(void)
416 pcmcia_unregister_driver(&pdacf_cs_driver);
417 snd_pdacf_detach_all();
420 module_init(init_pdacf);
421 module_exit(exit_pdacf);