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_CLASSES("{sound}");
41 MODULE_DEVICES("{{Sound Core," CARD_NAME "}}");
43 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
44 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
45 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable switches */
46 static unsigned int irq_mask = 0xffff;
47 static int irq_list[4] = { -1 };
50 module_param_array(index, int, boot_devs, 0444);
51 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
52 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
53 module_param_array(id, charp, boot_devs, 0444);
54 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
55 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
56 module_param_array(enable, bool, boot_devs, 0444);
57 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
58 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
59 module_param(irq_mask, int, 0444);
60 MODULE_PARM_DESC(irq_mask, "IRQ bitmask for " CARD_NAME " soundcard.");
61 module_param_array(irq_list, int, boot_devs, 0444);
62 MODULE_PARM_DESC(irq_list, "List of Available interrupts for " CARD_NAME " soundcard.");
68 static dev_info_t dev_info = "snd-pdaudiocf";
69 static snd_card_t *card_list[SNDRV_CARDS];
70 static dev_link_t *dev_list;
75 static void pdacf_config(dev_link_t *link);
76 static int pdacf_event(event_t event, int priority, event_callback_args_t *args);
77 static void snd_pdacf_detach(dev_link_t *link);
79 static void pdacf_release(dev_link_t *link)
81 if (link->state & DEV_CONFIG) {
82 /* release cs resources */
83 pcmcia_release_configuration(link->handle);
84 pcmcia_release_io(link->handle, &link->io);
85 pcmcia_release_irq(link->handle, &link->irq);
86 link->state &= ~DEV_CONFIG;
93 static int snd_pdacf_free(pdacf_t *pdacf)
95 dev_link_t *link = &pdacf->link;
99 /* Break the link with Card Services */
101 pcmcia_deregister_client(link->handle);
103 card_list[pdacf->index] = NULL;
106 snd_magic_kfree(pdacf);
110 static int snd_pdacf_dev_free(snd_device_t *device)
112 pdacf_t *chip = snd_magic_cast(pdacf_t, device->device_data, return -ENXIO);
113 return snd_pdacf_free(chip);
117 * snd_pdacf_attach - attach callback for cs
119 static dev_link_t *snd_pdacf_attach(void)
121 client_reg_t client_reg; /* Register with cardmgr */
122 dev_link_t *link; /* Info for cardmgr */
126 static snd_device_ops_t ops = {
127 .dev_free = snd_pdacf_dev_free,
130 snd_printdd(KERN_DEBUG "pdacf_attach called\n");
131 /* find an empty slot from the card list */
132 for (i = 0; i < SNDRV_CARDS; i++) {
136 if (i >= SNDRV_CARDS) {
137 snd_printk(KERN_ERR "pdacf: too many cards found\n");
141 return NULL; /* disabled explicitly */
143 /* ok, create a card instance */
144 card = snd_card_new(index[i], id[i], THIS_MODULE, 0);
146 snd_printk(KERN_ERR "pdacf: cannot create a card instance\n");
150 pdacf = snd_pdacf_create(card);
154 if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, pdacf, &ops) < 0) {
155 snd_magic_kfree(pdacf);
166 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
167 link->io.NumPorts1 = 16;
169 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT | IRQ_FORCED_PULSE;
170 // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
172 link->irq.IRQInfo1 = IRQ_INFO2_VALID /* | IRQ_LEVEL_ID */;
173 if (irq_list[0] == -1)
174 link->irq.IRQInfo2 = irq_mask;
176 for (i = 0; i < 4; i++)
177 link->irq.IRQInfo2 |= 1 << irq_list[i];
178 link->irq.Handler = pdacf_interrupt;
179 link->irq.Instance = pdacf;
180 link->conf.Attributes = CONF_ENABLE_IRQ;
181 link->conf.IntType = INT_MEMORY_AND_IO;
182 link->conf.ConfigIndex = 1;
183 link->conf.Present = PRESENT_OPTION;
186 link->next = dev_list;
189 /* Register with Card Services */
190 client_reg.dev_info = &dev_info;
191 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
192 client_reg.EventMask =
193 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL
195 | CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET
196 | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME
199 client_reg.event_handler = &pdacf_event;
200 client_reg.Version = 0x0210;
201 client_reg.event_callback_args.client_data = link;
203 ret = pcmcia_register_client(&link->handle, &client_reg);
204 if (ret != CS_SUCCESS) {
205 cs_error(link->handle, RegisterClient, ret);
206 snd_pdacf_detach(link);
215 * snd_pdacf_assign_resources - initialize the hardware and card instance.
216 * @port: i/o port for the card
217 * @irq: irq number for the card
219 * this function assigns the specified port and irq, boot the card,
220 * create pcm and control instances, and initialize the rest hardware.
222 * returns 0 if successful, or a negative error code.
224 static int snd_pdacf_assign_resources(pdacf_t *pdacf, int port, int irq)
227 snd_card_t *card = pdacf->card;
229 snd_printdd(KERN_DEBUG "pdacf assign resources: port = 0x%x, irq = %d\n", port, irq);
232 pdacf->chip_status |= PDAUDIOCF_STAT_IS_CONFIGURED;
234 err = snd_pdacf_ak4117_create(pdacf);
238 strcpy(card->driver, "PDAudio-CF");
239 sprintf(card->shortname, "Core Sound %s", card->driver);
240 sprintf(card->longname, "%s at 0x%x, irq %i",
241 card->shortname, port, irq);
243 err = snd_pdacf_pcm_new(pdacf);
247 snd_card_set_pm_callback(card, snd_pdacf_suspend, snd_pdacf_resume, pdacf);
249 if ((err = snd_card_register(card)) < 0)
257 * snd_pdacf_detach - detach callback for cs
259 static void snd_pdacf_detach(dev_link_t *link)
261 pdacf_t *chip = snd_magic_cast(pdacf_t, link->priv, return);
263 snd_printdd(KERN_DEBUG "pdacf_detach called\n");
264 /* Remove the interface data from the linked list */
267 /* Locate device structure */
268 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
274 if (chip->chip_status & PDAUDIOCF_STAT_IS_CONFIGURED)
275 snd_pdacf_powerdown(chip);
276 chip->chip_status |= PDAUDIOCF_STAT_IS_STALE; /* to be sure */
277 snd_card_disconnect(chip->card);
278 snd_card_free_in_thread(chip->card);
282 * snd_pdacf_detach_all - detach all instances linked to the hw
284 static void snd_pdacf_detach_all(void)
286 while (dev_list != NULL)
287 snd_pdacf_detach(dev_list);
291 * configuration callback
294 #define CS_CHECK(fn, ret) \
295 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
297 static void pdacf_config(dev_link_t *link)
299 client_handle_t handle = link->handle;
300 pdacf_t *pdacf = snd_magic_cast(pdacf_t, link->priv, return);
305 int last_fn, last_ret;
307 snd_printdd(KERN_DEBUG "pdacf_config called\n");
308 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
309 tuple.Attributes = 0;
310 tuple.TupleData = (cisdata_t *)buf;
311 tuple.TupleDataMax = sizeof(buf);
312 tuple.TupleOffset = 0;
313 tuple.DesiredTuple = CISTPL_CONFIG;
314 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
315 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
316 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
317 link->conf.ConfigBase = parse.config.base;
318 link->conf.ConfigIndex = 0x5;
320 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
321 link->conf.Vcc = conf.Vcc;
324 link->state |= DEV_CONFIG;
326 CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
327 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
328 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
330 if (snd_pdacf_assign_resources(pdacf, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
333 link->dev = &pdacf->node;
334 link->state &= ~DEV_CONFIG_PENDING;
338 cs_error(link->handle, last_fn, last_ret);
340 pcmcia_release_configuration(link->handle);
341 pcmcia_release_io(link->handle, &link->io);
342 pcmcia_release_irq(link->handle, &link->irq);
348 static int pdacf_event(event_t event, int priority, event_callback_args_t *args)
350 dev_link_t *link = args->client_data;
351 pdacf_t *chip = link->priv;
354 case CS_EVENT_CARD_REMOVAL:
355 snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
356 link->state &= ~DEV_PRESENT;
357 if (link->state & DEV_CONFIG) {
358 chip->chip_status |= PDAUDIOCF_STAT_IS_STALE;
361 case CS_EVENT_CARD_INSERTION:
362 snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
363 link->state |= DEV_PRESENT;
367 case CS_EVENT_PM_SUSPEND:
368 snd_printdd(KERN_DEBUG "SUSPEND\n");
369 link->state |= DEV_SUSPEND;
371 snd_printdd(KERN_DEBUG "snd_pdacf_suspend calling\n");
372 snd_pdacf_suspend(chip->card, 0);
374 /* Fall through... */
375 case CS_EVENT_RESET_PHYSICAL:
376 snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
377 if (link->state & DEV_CONFIG)
378 pcmcia_release_configuration(link->handle);
380 case CS_EVENT_PM_RESUME:
381 snd_printdd(KERN_DEBUG "RESUME\n");
382 link->state &= ~DEV_SUSPEND;
383 /* Fall through... */
384 case CS_EVENT_CARD_RESET:
385 snd_printdd(KERN_DEBUG "CARD_RESET\n");
387 snd_printdd(KERN_DEBUG "requestconfig...\n");
388 pcmcia_request_configuration(link->handle, &link->conf);
390 snd_printdd(KERN_DEBUG "calling snd_pdacf_resume\n");
391 snd_pdacf_resume(chip->card, 0);
394 snd_printdd(KERN_DEBUG "resume done!\n");
402 * Module entry points
404 static struct pcmcia_driver pdacf_cs_driver = {
405 .owner = THIS_MODULE,
407 .name = "snd-pdaudiocf",
409 .attach = snd_pdacf_attach,
410 .detach = snd_pdacf_detach
413 static int __init init_pdacf(void)
415 return pcmcia_register_driver(&pdacf_cs_driver);
418 static void __exit exit_pdacf(void)
420 pcmcia_unregister_driver(&pdacf_cs_driver);
421 snd_pdacf_detach_all();
424 module_init(init_pdacf);
425 module_exit(exit_pdacf);