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 };
48 module_param_array(index, int, NULL, 0444);
49 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
50 module_param_array(id, charp, NULL, 0444);
51 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
52 module_param_array(enable, bool, NULL, 0444);
53 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
54 module_param(irq_mask, int, 0444);
55 MODULE_PARM_DESC(irq_mask, "IRQ bitmask for " CARD_NAME " soundcard.");
56 module_param_array(irq_list, int, NULL, 0444);
57 MODULE_PARM_DESC(irq_list, "List of Available interrupts for " CARD_NAME " soundcard.");
63 static dev_info_t dev_info = "snd-pdaudiocf";
64 static snd_card_t *card_list[SNDRV_CARDS];
65 static dev_link_t *dev_list;
70 static void pdacf_config(dev_link_t *link);
71 static int pdacf_event(event_t event, int priority, event_callback_args_t *args);
72 static void snd_pdacf_detach(dev_link_t *link);
74 static void pdacf_release(dev_link_t *link)
76 if (link->state & DEV_CONFIG) {
77 /* release cs resources */
78 pcmcia_release_configuration(link->handle);
79 pcmcia_release_io(link->handle, &link->io);
80 pcmcia_release_irq(link->handle, &link->irq);
81 link->state &= ~DEV_CONFIG;
88 static int snd_pdacf_free(pdacf_t *pdacf)
90 dev_link_t *link = &pdacf->link;
94 /* Break the link with Card Services */
96 pcmcia_deregister_client(link->handle);
98 card_list[pdacf->index] = NULL;
105 static int snd_pdacf_dev_free(snd_device_t *device)
107 pdacf_t *chip = device->device_data;
108 return snd_pdacf_free(chip);
112 * snd_pdacf_attach - attach callback for cs
114 static dev_link_t *snd_pdacf_attach(void)
116 client_reg_t client_reg; /* Register with cardmgr */
117 dev_link_t *link; /* Info for cardmgr */
121 static snd_device_ops_t ops = {
122 .dev_free = snd_pdacf_dev_free,
125 snd_printdd(KERN_DEBUG "pdacf_attach called\n");
126 /* find an empty slot from the card list */
127 for (i = 0; i < SNDRV_CARDS; i++) {
131 if (i >= SNDRV_CARDS) {
132 snd_printk(KERN_ERR "pdacf: too many cards found\n");
136 return NULL; /* disabled explicitly */
138 /* ok, create a card instance */
139 card = snd_card_new(index[i], id[i], THIS_MODULE, 0);
141 snd_printk(KERN_ERR "pdacf: cannot create a card instance\n");
145 pdacf = snd_pdacf_create(card);
149 if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, pdacf, &ops) < 0) {
161 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
162 link->io.NumPorts1 = 16;
164 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT | IRQ_FORCED_PULSE;
165 // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
167 link->irq.IRQInfo1 = IRQ_INFO2_VALID /* | IRQ_LEVEL_ID */;
168 if (irq_list[0] == -1)
169 link->irq.IRQInfo2 = irq_mask;
171 for (i = 0; i < 4; i++)
172 link->irq.IRQInfo2 |= 1 << irq_list[i];
173 link->irq.Handler = pdacf_interrupt;
174 link->irq.Instance = pdacf;
175 link->conf.Attributes = CONF_ENABLE_IRQ;
176 link->conf.IntType = INT_MEMORY_AND_IO;
177 link->conf.ConfigIndex = 1;
178 link->conf.Present = PRESENT_OPTION;
181 link->next = dev_list;
184 /* Register with Card Services */
185 client_reg.dev_info = &dev_info;
186 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
187 client_reg.EventMask =
188 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL
190 | CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET
191 | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME
194 client_reg.event_handler = &pdacf_event;
195 client_reg.Version = 0x0210;
196 client_reg.event_callback_args.client_data = link;
198 ret = pcmcia_register_client(&link->handle, &client_reg);
199 if (ret != CS_SUCCESS) {
200 cs_error(link->handle, RegisterClient, ret);
201 snd_pdacf_detach(link);
210 * snd_pdacf_assign_resources - initialize the hardware and card instance.
211 * @port: i/o port for the card
212 * @irq: irq number for the card
214 * this function assigns the specified port and irq, boot the card,
215 * create pcm and control instances, and initialize the rest hardware.
217 * returns 0 if successful, or a negative error code.
219 static int snd_pdacf_assign_resources(pdacf_t *pdacf, int port, int irq)
222 snd_card_t *card = pdacf->card;
224 snd_printdd(KERN_DEBUG "pdacf assign resources: port = 0x%x, irq = %d\n", port, irq);
227 pdacf->chip_status |= PDAUDIOCF_STAT_IS_CONFIGURED;
229 err = snd_pdacf_ak4117_create(pdacf);
233 strcpy(card->driver, "PDAudio-CF");
234 sprintf(card->shortname, "Core Sound %s", card->driver);
235 sprintf(card->longname, "%s at 0x%x, irq %i",
236 card->shortname, port, irq);
238 err = snd_pdacf_pcm_new(pdacf);
242 snd_card_set_pm_callback(card, snd_pdacf_suspend, snd_pdacf_resume, pdacf);
244 if ((err = snd_card_register(card)) < 0)
252 * snd_pdacf_detach - detach callback for cs
254 static void snd_pdacf_detach(dev_link_t *link)
256 pdacf_t *chip = link->priv;
258 snd_printdd(KERN_DEBUG "pdacf_detach called\n");
259 /* Remove the interface data from the linked list */
262 /* Locate device structure */
263 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
269 if (chip->chip_status & PDAUDIOCF_STAT_IS_CONFIGURED)
270 snd_pdacf_powerdown(chip);
271 chip->chip_status |= PDAUDIOCF_STAT_IS_STALE; /* to be sure */
272 snd_card_disconnect(chip->card);
273 snd_card_free_in_thread(chip->card);
277 * snd_pdacf_detach_all - detach all instances linked to the hw
279 static void snd_pdacf_detach_all(void)
281 while (dev_list != NULL)
282 snd_pdacf_detach(dev_list);
286 * configuration callback
289 #define CS_CHECK(fn, ret) \
290 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
292 static void pdacf_config(dev_link_t *link)
294 client_handle_t handle = link->handle;
295 pdacf_t *pdacf = link->priv;
300 int last_fn, last_ret;
302 snd_printdd(KERN_DEBUG "pdacf_config called\n");
303 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
304 tuple.Attributes = 0;
305 tuple.TupleData = (cisdata_t *)buf;
306 tuple.TupleDataMax = sizeof(buf);
307 tuple.TupleOffset = 0;
308 tuple.DesiredTuple = CISTPL_CONFIG;
309 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
310 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
311 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
312 link->conf.ConfigBase = parse.config.base;
313 link->conf.ConfigIndex = 0x5;
315 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
316 link->conf.Vcc = conf.Vcc;
319 link->state |= DEV_CONFIG;
321 CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
322 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
323 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
325 if (snd_pdacf_assign_resources(pdacf, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
328 link->dev = &pdacf->node;
329 link->state &= ~DEV_CONFIG_PENDING;
333 cs_error(link->handle, last_fn, last_ret);
335 pcmcia_release_configuration(link->handle);
336 pcmcia_release_io(link->handle, &link->io);
337 pcmcia_release_irq(link->handle, &link->irq);
343 static int pdacf_event(event_t event, int priority, event_callback_args_t *args)
345 dev_link_t *link = args->client_data;
346 pdacf_t *chip = link->priv;
349 case CS_EVENT_CARD_REMOVAL:
350 snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
351 link->state &= ~DEV_PRESENT;
352 if (link->state & DEV_CONFIG) {
353 chip->chip_status |= PDAUDIOCF_STAT_IS_STALE;
356 case CS_EVENT_CARD_INSERTION:
357 snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
358 link->state |= DEV_PRESENT;
362 case CS_EVENT_PM_SUSPEND:
363 snd_printdd(KERN_DEBUG "SUSPEND\n");
364 link->state |= DEV_SUSPEND;
366 snd_printdd(KERN_DEBUG "snd_pdacf_suspend calling\n");
367 snd_pdacf_suspend(chip->card, 0);
369 /* Fall through... */
370 case CS_EVENT_RESET_PHYSICAL:
371 snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
372 if (link->state & DEV_CONFIG)
373 pcmcia_release_configuration(link->handle);
375 case CS_EVENT_PM_RESUME:
376 snd_printdd(KERN_DEBUG "RESUME\n");
377 link->state &= ~DEV_SUSPEND;
378 /* Fall through... */
379 case CS_EVENT_CARD_RESET:
380 snd_printdd(KERN_DEBUG "CARD_RESET\n");
382 snd_printdd(KERN_DEBUG "requestconfig...\n");
383 pcmcia_request_configuration(link->handle, &link->conf);
385 snd_printdd(KERN_DEBUG "calling snd_pdacf_resume\n");
386 snd_pdacf_resume(chip->card, 0);
389 snd_printdd(KERN_DEBUG "resume done!\n");
397 * Module entry points
399 static struct pcmcia_driver pdacf_cs_driver = {
400 .owner = THIS_MODULE,
402 .name = "snd-pdaudiocf",
404 .attach = snd_pdacf_attach,
405 .detach = snd_pdacf_detach
408 static int __init init_pdacf(void)
410 return pcmcia_register_driver(&pdacf_cs_driver);
413 static void __exit exit_pdacf(void)
415 pcmcia_unregister_driver(&pdacf_cs_driver);
416 snd_pdacf_detach_all();
419 module_init(init_pdacf);
420 module_exit(exit_pdacf);