ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pcmcia / pdaudiocf / pdaudiocf.c
1 /*
2  * Driver for Sound Core PDAudioCF soundcard
3  *
4  * Copyright (c) 2003 by Jaroslav Kysela <perex@suse.cz>
5  *
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.
10  *
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.
15  *
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
19  */
20
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>
30
31 /*
32  */
33
34 #define CARD_NAME       "PDAudio-CF"
35
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 "}}");
41
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 };
47
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.");
61  
62
63 /*
64  */
65
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;
69
70 /*
71  * prototypes
72  */
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);
76
77 static void pdacf_release(dev_link_t *link)
78 {
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;
85         }
86 }
87
88 /*
89  * destructor
90  */
91 static int snd_pdacf_free(pdacf_t *pdacf)
92 {
93         dev_link_t *link = &pdacf->link;
94
95         pdacf_release(link);
96
97         /* Break the link with Card Services */
98         if (link->handle)
99                 pcmcia_deregister_client(link->handle);
100
101         card_list[pdacf->index] = NULL;
102         pdacf->card = NULL;
103
104         snd_magic_kfree(pdacf);
105         return 0;
106 }
107
108 static int snd_pdacf_dev_free(snd_device_t *device)
109 {
110         pdacf_t *chip = snd_magic_cast(pdacf_t, device->device_data, return -ENXIO);
111         return snd_pdacf_free(chip);
112 }
113
114 /*
115  * snd_pdacf_attach - attach callback for cs
116  */
117 static dev_link_t *snd_pdacf_attach(void)
118 {
119         client_reg_t client_reg;        /* Register with cardmgr */
120         dev_link_t *link;               /* Info for cardmgr */
121         int i, ret;
122         pdacf_t *pdacf;
123         snd_card_t *card;
124         static snd_device_ops_t ops = {
125                 .dev_free =     snd_pdacf_dev_free,
126         };
127
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++) {
131                 if (! card_list[i])
132                         break;
133         }
134         if (i >= SNDRV_CARDS) {
135                 snd_printk(KERN_ERR "pdacf: too many cards found\n");
136                 return NULL;
137         }
138         if (! enable[i])
139                 return NULL; /* disabled explicitly */
140
141         /* ok, create a card instance */
142         card = snd_card_new(index[i], id[i], THIS_MODULE, 0);
143         if (card == NULL) {
144                 snd_printk(KERN_ERR "pdacf: cannot create a card instance\n");
145                 return NULL;
146         }
147
148         pdacf = snd_pdacf_create(card);
149         if (! pdacf)
150                 return NULL;
151
152         if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, pdacf, &ops) < 0) {
153                 snd_magic_kfree(pdacf);
154                 snd_card_free(card);
155                 return NULL;
156         }
157
158         pdacf->index = i;
159         card_list[i] = card;
160
161         link = &pdacf->link;
162         link->priv = pdacf;
163
164         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
165         link->io.NumPorts1 = 16;
166
167         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT | IRQ_FORCED_PULSE;
168         // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
169
170         link->irq.IRQInfo1 = IRQ_INFO2_VALID /* | IRQ_LEVEL_ID */;
171         if (irq_list[0] == -1)
172                 link->irq.IRQInfo2 = irq_mask;
173         else
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;
182
183         /* Chain drivers */
184         link->next = dev_list;
185         dev_list = link;
186
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
192 #ifdef CONFIG_PM
193                 | CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET
194                 | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME
195 #endif
196                 ;
197         client_reg.event_handler = &pdacf_event;
198         client_reg.Version = 0x0210;
199         client_reg.event_callback_args.client_data = link;
200
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);
205                 return NULL;
206         }
207
208         return link;
209 }
210
211
212 /**
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
216  *
217  * this function assigns the specified port and irq, boot the card,
218  * create pcm and control instances, and initialize the rest hardware.
219  *
220  * returns 0 if successful, or a negative error code.
221  */
222 static int snd_pdacf_assign_resources(pdacf_t *pdacf, int port, int irq)
223 {
224         int err;
225         snd_card_t *card = pdacf->card;
226
227         snd_printdd(KERN_DEBUG "pdacf assign resources: port = 0x%x, irq = %d\n", port, irq);
228         pdacf->port = port;
229         pdacf->irq = irq;
230         pdacf->chip_status |= PDAUDIOCF_STAT_IS_CONFIGURED;
231
232         err = snd_pdacf_ak4117_create(pdacf);
233         if (err < 0)
234                 return err;     
235
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);
240
241         err = snd_pdacf_pcm_new(pdacf);
242         if (err < 0)
243                 return err;
244
245 #ifdef CONFIG_PM
246         card->power_state_private_data = pdacf;
247         card->set_power_state = snd_pdacf_set_power_state;
248 #endif
249
250         if ((err = snd_card_register(card)) < 0)
251                 return err;
252
253         return 0;
254 }
255
256
257 /*
258  * snd_pdacf_detach - detach callback for cs
259  */
260 static void snd_pdacf_detach(dev_link_t *link)
261 {
262         pdacf_t *chip = snd_magic_cast(pdacf_t, link->priv, return);
263
264         snd_printdd(KERN_DEBUG "pdacf_detach called\n");
265         /* Remove the interface data from the linked list */
266         {
267                 dev_link_t **linkp;
268                 /* Locate device structure */
269                 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
270                         if (*linkp == link)
271                                 break;
272                 if (*linkp)
273                         *linkp = link->next;
274         }
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);
280 }
281
282 /*
283  * snd_pdacf_detach_all - detach all instances linked to the hw
284  */
285 static void snd_pdacf_detach_all(void)
286 {
287         while (dev_list != NULL)
288                 snd_pdacf_detach(dev_list);
289 }
290
291 /*
292  * configuration callback
293  */
294
295 #define CS_CHECK(fn, ret) \
296 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
297
298 static void pdacf_config(dev_link_t *link)
299 {
300         client_handle_t handle = link->handle;
301         pdacf_t *pdacf = snd_magic_cast(pdacf_t, link->priv, return);
302         tuple_t tuple;
303         cisparse_t parse;
304         config_info_t conf;
305         u_short buf[32];
306         int last_fn, last_ret;
307
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;
320
321         CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
322         link->conf.Vcc = conf.Vcc;
323
324         /* Configure card */
325         link->state |= DEV_CONFIG;
326
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));
330
331         if (snd_pdacf_assign_resources(pdacf, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
332                 goto failed;
333
334         link->dev = &pdacf->node;
335         link->state &= ~DEV_CONFIG_PENDING;
336         return;
337
338 cs_failed:
339         cs_error(link->handle, last_fn, last_ret);
340 failed:
341         pcmcia_release_configuration(link->handle);
342         pcmcia_release_io(link->handle, &link->io);
343         pcmcia_release_irq(link->handle, &link->irq);
344 }
345
346 /*
347  * event callback
348  */
349 static int pdacf_event(event_t event, int priority, event_callback_args_t *args)
350 {
351         dev_link_t *link = args->client_data;
352         pdacf_t *chip = link->priv;
353
354         switch (event) {
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;
360                 }
361                 break;
362         case CS_EVENT_CARD_INSERTION:
363                 snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
364                 link->state |= DEV_PRESENT;
365                 pdacf_config(link);
366                 break;
367 #ifdef CONFIG_PM
368         case CS_EVENT_PM_SUSPEND:
369                 snd_printdd(KERN_DEBUG "SUSPEND\n");
370                 link->state |= DEV_SUSPEND;
371                 if (chip) {
372                         snd_printdd(KERN_DEBUG "snd_pdacf_suspend calling\n");
373                         snd_pdacf_suspend(chip);
374                 }
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);
380                 break;
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");
387                 if (DEV_OK(link)) {
388                         snd_printdd(KERN_DEBUG "requestconfig...\n");
389                         pcmcia_request_configuration(link->handle, &link->conf);
390                         if (chip) {
391                                 snd_printdd(KERN_DEBUG "calling snd_pdacf_resume\n");
392                                 snd_pdacf_resume(chip);
393                         }
394                 }
395                 snd_printdd(KERN_DEBUG "resume done!\n");
396                 break;
397 #endif
398         }
399         return 0;
400 }
401
402 /*
403  * Module entry points
404  */
405 static struct pcmcia_driver pdacf_cs_driver = {
406         .owner          = THIS_MODULE,
407         .drv            = {
408                 .name   = "snd-pdaudiocf",
409         },
410         .attach         = snd_pdacf_attach,
411         .detach         = snd_pdacf_detach
412 };
413
414 static int __init init_pdacf(void)
415 {
416         return pcmcia_register_driver(&pdacf_cs_driver);
417 }
418
419 static void __exit exit_pdacf(void)
420 {
421         pcmcia_unregister_driver(&pdacf_cs_driver);
422         snd_pdacf_detach_all();
423 }
424
425 module_init(init_pdacf);
426 module_exit(exit_pdacf);