upgrade to linux 2.6.10-1.12_FC2
[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_SUPPORTED_DEVICE("{{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_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.");
58  
59
60 /*
61  */
62
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;
66
67 /*
68  * prototypes
69  */
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);
73
74 static void pdacf_release(dev_link_t *link)
75 {
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;
82         }
83 }
84
85 /*
86  * destructor
87  */
88 static int snd_pdacf_free(pdacf_t *pdacf)
89 {
90         dev_link_t *link = &pdacf->link;
91
92         pdacf_release(link);
93
94         /* Break the link with Card Services */
95         if (link->handle)
96                 pcmcia_deregister_client(link->handle);
97
98         card_list[pdacf->index] = NULL;
99         pdacf->card = NULL;
100
101         kfree(pdacf);
102         return 0;
103 }
104
105 static int snd_pdacf_dev_free(snd_device_t *device)
106 {
107         pdacf_t *chip = device->device_data;
108         return snd_pdacf_free(chip);
109 }
110
111 /*
112  * snd_pdacf_attach - attach callback for cs
113  */
114 static dev_link_t *snd_pdacf_attach(void)
115 {
116         client_reg_t client_reg;        /* Register with cardmgr */
117         dev_link_t *link;               /* Info for cardmgr */
118         int i, ret;
119         pdacf_t *pdacf;
120         snd_card_t *card;
121         static snd_device_ops_t ops = {
122                 .dev_free =     snd_pdacf_dev_free,
123         };
124
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++) {
128                 if (! card_list[i])
129                         break;
130         }
131         if (i >= SNDRV_CARDS) {
132                 snd_printk(KERN_ERR "pdacf: too many cards found\n");
133                 return NULL;
134         }
135         if (! enable[i])
136                 return NULL; /* disabled explicitly */
137
138         /* ok, create a card instance */
139         card = snd_card_new(index[i], id[i], THIS_MODULE, 0);
140         if (card == NULL) {
141                 snd_printk(KERN_ERR "pdacf: cannot create a card instance\n");
142                 return NULL;
143         }
144
145         pdacf = snd_pdacf_create(card);
146         if (! pdacf)
147                 return NULL;
148
149         if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, pdacf, &ops) < 0) {
150                 kfree(pdacf);
151                 snd_card_free(card);
152                 return NULL;
153         }
154
155         pdacf->index = i;
156         card_list[i] = card;
157
158         link = &pdacf->link;
159         link->priv = pdacf;
160
161         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
162         link->io.NumPorts1 = 16;
163
164         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT | IRQ_FORCED_PULSE;
165         // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
166
167         link->irq.IRQInfo1 = IRQ_INFO2_VALID /* | IRQ_LEVEL_ID */;
168         if (irq_list[0] == -1)
169                 link->irq.IRQInfo2 = irq_mask;
170         else
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;
179
180         /* Chain drivers */
181         link->next = dev_list;
182         dev_list = link;
183
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
189 #ifdef CONFIG_PM
190                 | CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET
191                 | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME
192 #endif
193                 ;
194         client_reg.event_handler = &pdacf_event;
195         client_reg.Version = 0x0210;
196         client_reg.event_callback_args.client_data = link;
197
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);
202                 return NULL;
203         }
204
205         return link;
206 }
207
208
209 /**
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
213  *
214  * this function assigns the specified port and irq, boot the card,
215  * create pcm and control instances, and initialize the rest hardware.
216  *
217  * returns 0 if successful, or a negative error code.
218  */
219 static int snd_pdacf_assign_resources(pdacf_t *pdacf, int port, int irq)
220 {
221         int err;
222         snd_card_t *card = pdacf->card;
223
224         snd_printdd(KERN_DEBUG "pdacf assign resources: port = 0x%x, irq = %d\n", port, irq);
225         pdacf->port = port;
226         pdacf->irq = irq;
227         pdacf->chip_status |= PDAUDIOCF_STAT_IS_CONFIGURED;
228
229         err = snd_pdacf_ak4117_create(pdacf);
230         if (err < 0)
231                 return err;     
232
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);
237
238         err = snd_pdacf_pcm_new(pdacf);
239         if (err < 0)
240                 return err;
241
242         snd_card_set_pm_callback(card, snd_pdacf_suspend, snd_pdacf_resume, pdacf);
243
244         if ((err = snd_card_register(card)) < 0)
245                 return err;
246
247         return 0;
248 }
249
250
251 /*
252  * snd_pdacf_detach - detach callback for cs
253  */
254 static void snd_pdacf_detach(dev_link_t *link)
255 {
256         pdacf_t *chip = link->priv;
257
258         snd_printdd(KERN_DEBUG "pdacf_detach called\n");
259         /* Remove the interface data from the linked list */
260         {
261                 dev_link_t **linkp;
262                 /* Locate device structure */
263                 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
264                         if (*linkp == link)
265                                 break;
266                 if (*linkp)
267                         *linkp = link->next;
268         }
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);
274 }
275
276 /*
277  * snd_pdacf_detach_all - detach all instances linked to the hw
278  */
279 static void snd_pdacf_detach_all(void)
280 {
281         while (dev_list != NULL)
282                 snd_pdacf_detach(dev_list);
283 }
284
285 /*
286  * configuration callback
287  */
288
289 #define CS_CHECK(fn, ret) \
290 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
291
292 static void pdacf_config(dev_link_t *link)
293 {
294         client_handle_t handle = link->handle;
295         pdacf_t *pdacf = link->priv;
296         tuple_t tuple;
297         cisparse_t parse;
298         config_info_t conf;
299         u_short buf[32];
300         int last_fn, last_ret;
301
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;
314
315         CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
316         link->conf.Vcc = conf.Vcc;
317
318         /* Configure card */
319         link->state |= DEV_CONFIG;
320
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));
324
325         if (snd_pdacf_assign_resources(pdacf, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
326                 goto failed;
327
328         link->dev = &pdacf->node;
329         link->state &= ~DEV_CONFIG_PENDING;
330         return;
331
332 cs_failed:
333         cs_error(link->handle, last_fn, last_ret);
334 failed:
335         pcmcia_release_configuration(link->handle);
336         pcmcia_release_io(link->handle, &link->io);
337         pcmcia_release_irq(link->handle, &link->irq);
338 }
339
340 /*
341  * event callback
342  */
343 static int pdacf_event(event_t event, int priority, event_callback_args_t *args)
344 {
345         dev_link_t *link = args->client_data;
346         pdacf_t *chip = link->priv;
347
348         switch (event) {
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;
354                 }
355                 break;
356         case CS_EVENT_CARD_INSERTION:
357                 snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
358                 link->state |= DEV_PRESENT;
359                 pdacf_config(link);
360                 break;
361 #ifdef CONFIG_PM
362         case CS_EVENT_PM_SUSPEND:
363                 snd_printdd(KERN_DEBUG "SUSPEND\n");
364                 link->state |= DEV_SUSPEND;
365                 if (chip) {
366                         snd_printdd(KERN_DEBUG "snd_pdacf_suspend calling\n");
367                         snd_pdacf_suspend(chip->card, 0);
368                 }
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);
374                 break;
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");
381                 if (DEV_OK(link)) {
382                         snd_printdd(KERN_DEBUG "requestconfig...\n");
383                         pcmcia_request_configuration(link->handle, &link->conf);
384                         if (chip) {
385                                 snd_printdd(KERN_DEBUG "calling snd_pdacf_resume\n");
386                                 snd_pdacf_resume(chip->card, 0);
387                         }
388                 }
389                 snd_printdd(KERN_DEBUG "resume done!\n");
390                 break;
391 #endif
392         }
393         return 0;
394 }
395
396 /*
397  * Module entry points
398  */
399 static struct pcmcia_driver pdacf_cs_driver = {
400         .owner          = THIS_MODULE,
401         .drv            = {
402                 .name   = "snd-pdaudiocf",
403         },
404         .attach         = snd_pdacf_attach,
405         .detach         = snd_pdacf_detach
406 };
407
408 static int __init init_pdacf(void)
409 {
410         return pcmcia_register_driver(&pdacf_cs_driver);
411 }
412
413 static void __exit exit_pdacf(void)
414 {
415         pcmcia_unregister_driver(&pdacf_cs_driver);
416         snd_pdacf_detach_all();
417 }
418
419 module_init(init_pdacf);
420 module_exit(exit_pdacf);