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 <pcmcia/version.h>
25 #include <pcmcia/ciscode.h>
26 #include <pcmcia/cisreg.h>
27 #include "pdaudiocf.h"
28 #include <sound/initval.h>
29 #include <linux/init.h>
34 #define CARD_NAME "PDAudio-CF"
36 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
37 MODULE_DESCRIPTION("Sound Core " CARD_NAME);
38 MODULE_LICENSE("GPL");
39 MODULE_CLASSES("{sound}");
40 MODULE_DEVICES("{{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 };
48 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
49 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
50 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
51 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
52 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
53 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
54 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
55 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
56 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
57 MODULE_PARM(irq_mask, "i");
58 MODULE_PARM_DESC(irq_mask, "IRQ bitmask for " CARD_NAME " soundcard.");
59 MODULE_PARM(irq_list, "1-4i");
60 MODULE_PARM_DESC(irq_list, "List of Available interrupts for " CARD_NAME " soundcard.");
66 static dev_info_t dev_info = "snd-pdaudiocf";
67 static snd_card_t *card_list[SNDRV_CARDS];
68 static dev_link_t *dev_list;
73 static void pdacf_config(dev_link_t *link);
74 static int pdacf_event(event_t event, int priority, event_callback_args_t *args);
75 static void snd_pdacf_detach(dev_link_t *link);
77 static void pdacf_release(dev_link_t *link)
79 if (link->state & DEV_CONFIG) {
80 /* release cs resources */
81 pcmcia_release_configuration(link->handle);
82 pcmcia_release_io(link->handle, &link->io);
83 pcmcia_release_irq(link->handle, &link->irq);
84 link->state &= ~DEV_CONFIG;
91 static int snd_pdacf_free(pdacf_t *pdacf)
93 dev_link_t *link = &pdacf->link;
97 /* Break the link with Card Services */
99 pcmcia_deregister_client(link->handle);
101 card_list[pdacf->index] = NULL;
104 snd_magic_kfree(pdacf);
108 static int snd_pdacf_dev_free(snd_device_t *device)
110 pdacf_t *chip = snd_magic_cast(pdacf_t, device->device_data, return -ENXIO);
111 return snd_pdacf_free(chip);
115 * snd_pdacf_attach - attach callback for cs
117 static dev_link_t *snd_pdacf_attach(void)
119 client_reg_t client_reg; /* Register with cardmgr */
120 dev_link_t *link; /* Info for cardmgr */
124 static snd_device_ops_t ops = {
125 .dev_free = snd_pdacf_dev_free,
128 snd_printdd(KERN_DEBUG "pdacf_attach called\n");
129 /* find an empty slot from the card list */
130 for (i = 0; i < SNDRV_CARDS; i++) {
134 if (i >= SNDRV_CARDS) {
135 snd_printk(KERN_ERR "pdacf: too many cards found\n");
139 return NULL; /* disabled explicitly */
141 /* ok, create a card instance */
142 card = snd_card_new(index[i], id[i], THIS_MODULE, 0);
144 snd_printk(KERN_ERR "pdacf: cannot create a card instance\n");
148 pdacf = snd_pdacf_create(card);
152 if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, pdacf, &ops) < 0) {
153 snd_magic_kfree(pdacf);
164 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
165 link->io.NumPorts1 = 16;
167 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT | IRQ_FORCED_PULSE;
168 // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
170 link->irq.IRQInfo1 = IRQ_INFO2_VALID /* | IRQ_LEVEL_ID */;
171 if (irq_list[0] == -1)
172 link->irq.IRQInfo2 = irq_mask;
174 for (i = 0; i < 4; i++)
175 link->irq.IRQInfo2 |= 1 << irq_list[i];
176 link->irq.Handler = pdacf_interrupt;
177 link->irq.Instance = pdacf;
178 link->conf.Attributes = CONF_ENABLE_IRQ;
179 link->conf.IntType = INT_MEMORY_AND_IO;
180 link->conf.ConfigIndex = 1;
181 link->conf.Present = PRESENT_OPTION;
184 link->next = dev_list;
187 /* Register with Card Services */
188 client_reg.dev_info = &dev_info;
189 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
190 client_reg.EventMask =
191 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL
193 | CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET
194 | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME
197 client_reg.event_handler = &pdacf_event;
198 client_reg.Version = 0x0210;
199 client_reg.event_callback_args.client_data = link;
201 ret = pcmcia_register_client(&link->handle, &client_reg);
202 if (ret != CS_SUCCESS) {
203 cs_error(link->handle, RegisterClient, ret);
204 snd_pdacf_detach(link);
213 * snd_pdacf_assign_resources - initialize the hardware and card instance.
214 * @port: i/o port for the card
215 * @irq: irq number for the card
217 * this function assigns the specified port and irq, boot the card,
218 * create pcm and control instances, and initialize the rest hardware.
220 * returns 0 if successful, or a negative error code.
222 static int snd_pdacf_assign_resources(pdacf_t *pdacf, int port, int irq)
225 snd_card_t *card = pdacf->card;
227 snd_printdd(KERN_DEBUG "pdacf assign resources: port = 0x%x, irq = %d\n", port, irq);
230 pdacf->chip_status |= PDAUDIOCF_STAT_IS_CONFIGURED;
232 err = snd_pdacf_ak4117_create(pdacf);
236 strcpy(card->driver, "PDAudio-CF");
237 sprintf(card->shortname, "Core Sound %s", card->driver);
238 sprintf(card->longname, "%s at 0x%x, irq %i",
239 card->shortname, port, irq);
241 err = snd_pdacf_pcm_new(pdacf);
246 card->power_state_private_data = pdacf;
247 card->set_power_state = snd_pdacf_set_power_state;
250 if ((err = snd_card_register(card)) < 0)
258 * snd_pdacf_detach - detach callback for cs
260 static void snd_pdacf_detach(dev_link_t *link)
262 pdacf_t *chip = snd_magic_cast(pdacf_t, link->priv, return);
264 snd_printdd(KERN_DEBUG "pdacf_detach called\n");
265 /* Remove the interface data from the linked list */
268 /* Locate device structure */
269 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
275 if (chip->chip_status & PDAUDIOCF_STAT_IS_CONFIGURED)
276 snd_pdacf_powerdown(chip);
277 chip->chip_status |= PDAUDIOCF_STAT_IS_STALE; /* to be sure */
278 snd_card_disconnect(chip->card);
279 snd_card_free_in_thread(chip->card);
283 * snd_pdacf_detach_all - detach all instances linked to the hw
285 static void snd_pdacf_detach_all(void)
287 while (dev_list != NULL)
288 snd_pdacf_detach(dev_list);
292 * configuration callback
295 #define CS_CHECK(fn, ret) \
296 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
298 static void pdacf_config(dev_link_t *link)
300 client_handle_t handle = link->handle;
301 pdacf_t *pdacf = snd_magic_cast(pdacf_t, link->priv, return);
306 int last_fn, last_ret;
308 snd_printdd(KERN_DEBUG "pdacf_config called\n");
309 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
310 tuple.Attributes = 0;
311 tuple.TupleData = (cisdata_t *)buf;
312 tuple.TupleDataMax = sizeof(buf);
313 tuple.TupleOffset = 0;
314 tuple.DesiredTuple = CISTPL_CONFIG;
315 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
316 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
317 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
318 link->conf.ConfigBase = parse.config.base;
319 link->conf.ConfigIndex = 0x5;
321 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
322 link->conf.Vcc = conf.Vcc;
325 link->state |= DEV_CONFIG;
327 CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
328 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
329 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
331 if (snd_pdacf_assign_resources(pdacf, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
334 link->dev = &pdacf->node;
335 link->state &= ~DEV_CONFIG_PENDING;
339 cs_error(link->handle, last_fn, last_ret);
341 pcmcia_release_configuration(link->handle);
342 pcmcia_release_io(link->handle, &link->io);
343 pcmcia_release_irq(link->handle, &link->irq);
349 static int pdacf_event(event_t event, int priority, event_callback_args_t *args)
351 dev_link_t *link = args->client_data;
352 pdacf_t *chip = link->priv;
355 case CS_EVENT_CARD_REMOVAL:
356 snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
357 link->state &= ~DEV_PRESENT;
358 if (link->state & DEV_CONFIG) {
359 chip->chip_status |= PDAUDIOCF_STAT_IS_STALE;
362 case CS_EVENT_CARD_INSERTION:
363 snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
364 link->state |= DEV_PRESENT;
368 case CS_EVENT_PM_SUSPEND:
369 snd_printdd(KERN_DEBUG "SUSPEND\n");
370 link->state |= DEV_SUSPEND;
372 snd_printdd(KERN_DEBUG "snd_pdacf_suspend calling\n");
373 snd_pdacf_suspend(chip);
375 /* Fall through... */
376 case CS_EVENT_RESET_PHYSICAL:
377 snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
378 if (link->state & DEV_CONFIG)
379 pcmcia_release_configuration(link->handle);
381 case CS_EVENT_PM_RESUME:
382 snd_printdd(KERN_DEBUG "RESUME\n");
383 link->state &= ~DEV_SUSPEND;
384 /* Fall through... */
385 case CS_EVENT_CARD_RESET:
386 snd_printdd(KERN_DEBUG "CARD_RESET\n");
388 snd_printdd(KERN_DEBUG "requestconfig...\n");
389 pcmcia_request_configuration(link->handle, &link->conf);
391 snd_printdd(KERN_DEBUG "calling snd_pdacf_resume\n");
392 snd_pdacf_resume(chip);
395 snd_printdd(KERN_DEBUG "resume done!\n");
403 * Module entry points
405 static struct pcmcia_driver pdacf_cs_driver = {
406 .owner = THIS_MODULE,
408 .name = "snd-pdaudiocf",
410 .attach = snd_pdacf_attach,
411 .detach = snd_pdacf_detach
414 static int __init init_pdacf(void)
416 return pcmcia_register_driver(&pdacf_cs_driver);
419 static void __exit exit_pdacf(void)
421 pcmcia_unregister_driver(&pdacf_cs_driver);
422 snd_pdacf_detach_all();
425 module_init(init_pdacf);
426 module_exit(exit_pdacf);