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 */
46 module_param_array(index, int, NULL, 0444);
47 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
48 module_param_array(id, charp, NULL, 0444);
49 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
50 module_param_array(enable, bool, NULL, 0444);
51 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
56 static dev_info_t dev_info = "snd-pdaudiocf";
57 static snd_card_t *card_list[SNDRV_CARDS];
58 static dev_link_t *dev_list;
63 static void pdacf_config(dev_link_t *link);
64 static int pdacf_event(event_t event, int priority, event_callback_args_t *args);
65 static void snd_pdacf_detach(dev_link_t *link);
67 static void pdacf_release(dev_link_t *link)
69 if (link->state & DEV_CONFIG) {
70 /* release cs resources */
71 pcmcia_release_configuration(link->handle);
72 pcmcia_release_io(link->handle, &link->io);
73 pcmcia_release_irq(link->handle, &link->irq);
74 link->state &= ~DEV_CONFIG;
81 static int snd_pdacf_free(pdacf_t *pdacf)
83 dev_link_t *link = &pdacf->link;
87 /* Break the link with Card Services */
89 pcmcia_deregister_client(link->handle);
91 card_list[pdacf->index] = NULL;
98 static int snd_pdacf_dev_free(snd_device_t *device)
100 pdacf_t *chip = device->device_data;
101 return snd_pdacf_free(chip);
105 * snd_pdacf_attach - attach callback for cs
107 static dev_link_t *snd_pdacf_attach(void)
109 client_reg_t client_reg; /* Register with cardmgr */
110 dev_link_t *link; /* Info for cardmgr */
114 static snd_device_ops_t ops = {
115 .dev_free = snd_pdacf_dev_free,
118 snd_printdd(KERN_DEBUG "pdacf_attach called\n");
119 /* find an empty slot from the card list */
120 for (i = 0; i < SNDRV_CARDS; i++) {
124 if (i >= SNDRV_CARDS) {
125 snd_printk(KERN_ERR "pdacf: too many cards found\n");
129 return NULL; /* disabled explicitly */
131 /* ok, create a card instance */
132 card = snd_card_new(index[i], id[i], THIS_MODULE, 0);
134 snd_printk(KERN_ERR "pdacf: cannot create a card instance\n");
138 pdacf = snd_pdacf_create(card);
142 if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, pdacf, &ops) < 0) {
154 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
155 link->io.NumPorts1 = 16;
157 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT | IRQ_FORCED_PULSE;
158 // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
160 link->irq.IRQInfo1 = 0 /* | IRQ_LEVEL_ID */;
161 link->irq.Handler = pdacf_interrupt;
162 link->irq.Instance = pdacf;
163 link->conf.Attributes = CONF_ENABLE_IRQ;
164 link->conf.IntType = INT_MEMORY_AND_IO;
165 link->conf.ConfigIndex = 1;
166 link->conf.Present = PRESENT_OPTION;
169 link->next = dev_list;
172 /* Register with Card Services */
173 client_reg.dev_info = &dev_info;
174 client_reg.Version = 0x0210;
175 client_reg.event_callback_args.client_data = link;
177 ret = pcmcia_register_client(&link->handle, &client_reg);
178 if (ret != CS_SUCCESS) {
179 cs_error(link->handle, RegisterClient, ret);
180 snd_pdacf_detach(link);
189 * snd_pdacf_assign_resources - initialize the hardware and card instance.
190 * @port: i/o port for the card
191 * @irq: irq number for the card
193 * this function assigns the specified port and irq, boot the card,
194 * create pcm and control instances, and initialize the rest hardware.
196 * returns 0 if successful, or a negative error code.
198 static int snd_pdacf_assign_resources(pdacf_t *pdacf, int port, int irq)
201 snd_card_t *card = pdacf->card;
203 snd_printdd(KERN_DEBUG "pdacf assign resources: port = 0x%x, irq = %d\n", port, irq);
206 pdacf->chip_status |= PDAUDIOCF_STAT_IS_CONFIGURED;
208 err = snd_pdacf_ak4117_create(pdacf);
212 strcpy(card->driver, "PDAudio-CF");
213 sprintf(card->shortname, "Core Sound %s", card->driver);
214 sprintf(card->longname, "%s at 0x%x, irq %i",
215 card->shortname, port, irq);
217 err = snd_pdacf_pcm_new(pdacf);
221 snd_card_set_pm_callback(card, snd_pdacf_suspend, snd_pdacf_resume, pdacf);
223 if ((err = snd_card_register(card)) < 0)
231 * snd_pdacf_detach - detach callback for cs
233 static void snd_pdacf_detach(dev_link_t *link)
235 pdacf_t *chip = link->priv;
237 snd_printdd(KERN_DEBUG "pdacf_detach called\n");
238 /* Remove the interface data from the linked list */
241 /* Locate device structure */
242 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
248 if (chip->chip_status & PDAUDIOCF_STAT_IS_CONFIGURED)
249 snd_pdacf_powerdown(chip);
250 chip->chip_status |= PDAUDIOCF_STAT_IS_STALE; /* to be sure */
251 snd_card_disconnect(chip->card);
252 snd_card_free_in_thread(chip->card);
256 * configuration callback
259 #define CS_CHECK(fn, ret) \
260 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
262 static void pdacf_config(dev_link_t *link)
264 client_handle_t handle = link->handle;
265 pdacf_t *pdacf = link->priv;
267 cisparse_t *parse = NULL;
270 int last_fn, last_ret;
272 snd_printdd(KERN_DEBUG "pdacf_config called\n");
273 parse = kmalloc(sizeof(*parse), GFP_KERNEL);
275 snd_printk(KERN_ERR "pdacf_config: cannot allocate\n");
278 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
279 tuple.Attributes = 0;
280 tuple.TupleData = (cisdata_t *)buf;
281 tuple.TupleDataMax = sizeof(buf);
282 tuple.TupleOffset = 0;
283 tuple.DesiredTuple = CISTPL_CONFIG;
284 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
285 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
286 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, parse));
287 link->conf.ConfigBase = parse->config.base;
288 link->conf.ConfigIndex = 0x5;
291 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
292 link->conf.Vcc = conf.Vcc;
295 link->state |= DEV_CONFIG;
297 CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
298 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
299 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
301 if (snd_pdacf_assign_resources(pdacf, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
304 link->dev = &pdacf->node;
305 link->state &= ~DEV_CONFIG_PENDING;
309 cs_error(link->handle, last_fn, last_ret);
311 pcmcia_release_configuration(link->handle);
312 pcmcia_release_io(link->handle, &link->io);
313 pcmcia_release_irq(link->handle, &link->irq);
319 static int pdacf_event(event_t event, int priority, event_callback_args_t *args)
321 dev_link_t *link = args->client_data;
322 pdacf_t *chip = link->priv;
325 case CS_EVENT_CARD_REMOVAL:
326 snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
327 link->state &= ~DEV_PRESENT;
328 if (link->state & DEV_CONFIG) {
329 chip->chip_status |= PDAUDIOCF_STAT_IS_STALE;
332 case CS_EVENT_CARD_INSERTION:
333 snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
334 link->state |= DEV_PRESENT;
338 case CS_EVENT_PM_SUSPEND:
339 snd_printdd(KERN_DEBUG "SUSPEND\n");
340 link->state |= DEV_SUSPEND;
342 snd_printdd(KERN_DEBUG "snd_pdacf_suspend calling\n");
343 snd_pdacf_suspend(chip->card, PMSG_SUSPEND);
345 /* Fall through... */
346 case CS_EVENT_RESET_PHYSICAL:
347 snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
348 if (link->state & DEV_CONFIG)
349 pcmcia_release_configuration(link->handle);
351 case CS_EVENT_PM_RESUME:
352 snd_printdd(KERN_DEBUG "RESUME\n");
353 link->state &= ~DEV_SUSPEND;
354 /* Fall through... */
355 case CS_EVENT_CARD_RESET:
356 snd_printdd(KERN_DEBUG "CARD_RESET\n");
358 snd_printdd(KERN_DEBUG "requestconfig...\n");
359 pcmcia_request_configuration(link->handle, &link->conf);
361 snd_printdd(KERN_DEBUG "calling snd_pdacf_resume\n");
362 snd_pdacf_resume(chip->card);
365 snd_printdd(KERN_DEBUG "resume done!\n");
373 * Module entry points
375 static struct pcmcia_device_id snd_pdacf_ids[] = {
376 PCMCIA_DEVICE_MANF_CARD(0x015d, 0x4c45),
379 MODULE_DEVICE_TABLE(pcmcia, snd_pdacf_ids);
381 static struct pcmcia_driver pdacf_cs_driver = {
382 .owner = THIS_MODULE,
384 .name = "snd-pdaudiocf",
386 .attach = snd_pdacf_attach,
387 .event = pdacf_event,
388 .detach = snd_pdacf_detach,
389 .id_table = snd_pdacf_ids,
392 static int __init init_pdacf(void)
394 return pcmcia_register_driver(&pdacf_cs_driver);
397 static void __exit exit_pdacf(void)
399 pcmcia_unregister_driver(&pdacf_cs_driver);
400 BUG_ON(dev_list != NULL);
403 module_init(init_pdacf);
404 module_exit(exit_pdacf);