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