vserver 1.9.3
[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 static int boot_devs;
48
49 module_param_array(index, int, boot_devs, 0444);
50 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
51 module_param_array(id, charp, boot_devs, 0444);
52 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
53 module_param_array(enable, bool, boot_devs, 0444);
54 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
55 module_param(irq_mask, int, 0444);
56 MODULE_PARM_DESC(irq_mask, "IRQ bitmask for " CARD_NAME " soundcard.");
57 module_param_array(irq_list, int, boot_devs, 0444);
58 MODULE_PARM_DESC(irq_list, "List of Available interrupts for " CARD_NAME " soundcard.");
59  
60
61 /*
62  */
63
64 static dev_info_t dev_info = "snd-pdaudiocf";
65 static snd_card_t *card_list[SNDRV_CARDS];
66 static dev_link_t *dev_list;
67
68 /*
69  * prototypes
70  */
71 static void pdacf_config(dev_link_t *link);
72 static int pdacf_event(event_t event, int priority, event_callback_args_t *args);
73 static void snd_pdacf_detach(dev_link_t *link);
74
75 static void pdacf_release(dev_link_t *link)
76 {
77         if (link->state & DEV_CONFIG) {
78                 /* release cs resources */
79                 pcmcia_release_configuration(link->handle);
80                 pcmcia_release_io(link->handle, &link->io);
81                 pcmcia_release_irq(link->handle, &link->irq);
82                 link->state &= ~DEV_CONFIG;
83         }
84 }
85
86 /*
87  * destructor
88  */
89 static int snd_pdacf_free(pdacf_t *pdacf)
90 {
91         dev_link_t *link = &pdacf->link;
92
93         pdacf_release(link);
94
95         /* Break the link with Card Services */
96         if (link->handle)
97                 pcmcia_deregister_client(link->handle);
98
99         card_list[pdacf->index] = NULL;
100         pdacf->card = NULL;
101
102         kfree(pdacf);
103         return 0;
104 }
105
106 static int snd_pdacf_dev_free(snd_device_t *device)
107 {
108         pdacf_t *chip = device->device_data;
109         return snd_pdacf_free(chip);
110 }
111
112 /*
113  * snd_pdacf_attach - attach callback for cs
114  */
115 static dev_link_t *snd_pdacf_attach(void)
116 {
117         client_reg_t client_reg;        /* Register with cardmgr */
118         dev_link_t *link;               /* Info for cardmgr */
119         int i, ret;
120         pdacf_t *pdacf;
121         snd_card_t *card;
122         static snd_device_ops_t ops = {
123                 .dev_free =     snd_pdacf_dev_free,
124         };
125
126         snd_printdd(KERN_DEBUG "pdacf_attach called\n");
127         /* find an empty slot from the card list */
128         for (i = 0; i < SNDRV_CARDS; i++) {
129                 if (! card_list[i])
130                         break;
131         }
132         if (i >= SNDRV_CARDS) {
133                 snd_printk(KERN_ERR "pdacf: too many cards found\n");
134                 return NULL;
135         }
136         if (! enable[i])
137                 return NULL; /* disabled explicitly */
138
139         /* ok, create a card instance */
140         card = snd_card_new(index[i], id[i], THIS_MODULE, 0);
141         if (card == NULL) {
142                 snd_printk(KERN_ERR "pdacf: cannot create a card instance\n");
143                 return NULL;
144         }
145
146         pdacf = snd_pdacf_create(card);
147         if (! pdacf)
148                 return NULL;
149
150         if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, pdacf, &ops) < 0) {
151                 kfree(pdacf);
152                 snd_card_free(card);
153                 return NULL;
154         }
155
156         pdacf->index = i;
157         card_list[i] = card;
158
159         link = &pdacf->link;
160         link->priv = pdacf;
161
162         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
163         link->io.NumPorts1 = 16;
164
165         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT | IRQ_FORCED_PULSE;
166         // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
167
168         link->irq.IRQInfo1 = IRQ_INFO2_VALID /* | IRQ_LEVEL_ID */;
169         if (irq_list[0] == -1)
170                 link->irq.IRQInfo2 = irq_mask;
171         else
172                 for (i = 0; i < 4; i++)
173                         link->irq.IRQInfo2 |= 1 << irq_list[i];
174         link->irq.Handler = pdacf_interrupt;
175         link->irq.Instance = pdacf;
176         link->conf.Attributes = CONF_ENABLE_IRQ;
177         link->conf.IntType = INT_MEMORY_AND_IO;
178         link->conf.ConfigIndex = 1;
179         link->conf.Present = PRESENT_OPTION;
180
181         /* Chain drivers */
182         link->next = dev_list;
183         dev_list = link;
184
185         /* Register with Card Services */
186         client_reg.dev_info = &dev_info;
187         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
188         client_reg.EventMask = 
189                 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL
190 #ifdef CONFIG_PM
191                 | CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET
192                 | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME
193 #endif
194                 ;
195         client_reg.event_handler = &pdacf_event;
196         client_reg.Version = 0x0210;
197         client_reg.event_callback_args.client_data = link;
198
199         ret = pcmcia_register_client(&link->handle, &client_reg);
200         if (ret != CS_SUCCESS) {
201                 cs_error(link->handle, RegisterClient, ret);
202                 snd_pdacf_detach(link);
203                 return NULL;
204         }
205
206         return link;
207 }
208
209
210 /**
211  * snd_pdacf_assign_resources - initialize the hardware and card instance.
212  * @port: i/o port for the card
213  * @irq: irq number for the card
214  *
215  * this function assigns the specified port and irq, boot the card,
216  * create pcm and control instances, and initialize the rest hardware.
217  *
218  * returns 0 if successful, or a negative error code.
219  */
220 static int snd_pdacf_assign_resources(pdacf_t *pdacf, int port, int irq)
221 {
222         int err;
223         snd_card_t *card = pdacf->card;
224
225         snd_printdd(KERN_DEBUG "pdacf assign resources: port = 0x%x, irq = %d\n", port, irq);
226         pdacf->port = port;
227         pdacf->irq = irq;
228         pdacf->chip_status |= PDAUDIOCF_STAT_IS_CONFIGURED;
229
230         err = snd_pdacf_ak4117_create(pdacf);
231         if (err < 0)
232                 return err;     
233
234         strcpy(card->driver, "PDAudio-CF");
235         sprintf(card->shortname, "Core Sound %s", card->driver);
236         sprintf(card->longname, "%s at 0x%x, irq %i",
237                 card->shortname, port, irq);
238
239         err = snd_pdacf_pcm_new(pdacf);
240         if (err < 0)
241                 return err;
242
243         snd_card_set_pm_callback(card, snd_pdacf_suspend, snd_pdacf_resume, pdacf);
244
245         if ((err = snd_card_register(card)) < 0)
246                 return err;
247
248         return 0;
249 }
250
251
252 /*
253  * snd_pdacf_detach - detach callback for cs
254  */
255 static void snd_pdacf_detach(dev_link_t *link)
256 {
257         pdacf_t *chip = link->priv;
258
259         snd_printdd(KERN_DEBUG "pdacf_detach called\n");
260         /* Remove the interface data from the linked list */
261         {
262                 dev_link_t **linkp;
263                 /* Locate device structure */
264                 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
265                         if (*linkp == link)
266                                 break;
267                 if (*linkp)
268                         *linkp = link->next;
269         }
270         if (chip->chip_status & PDAUDIOCF_STAT_IS_CONFIGURED)
271                 snd_pdacf_powerdown(chip);
272         chip->chip_status |= PDAUDIOCF_STAT_IS_STALE; /* to be sure */
273         snd_card_disconnect(chip->card);
274         snd_card_free_in_thread(chip->card);
275 }
276
277 /*
278  * snd_pdacf_detach_all - detach all instances linked to the hw
279  */
280 static void snd_pdacf_detach_all(void)
281 {
282         while (dev_list != NULL)
283                 snd_pdacf_detach(dev_list);
284 }
285
286 /*
287  * configuration callback
288  */
289
290 #define CS_CHECK(fn, ret) \
291 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
292
293 static void pdacf_config(dev_link_t *link)
294 {
295         client_handle_t handle = link->handle;
296         pdacf_t *pdacf = link->priv;
297         tuple_t tuple;
298         cisparse_t parse;
299         config_info_t conf;
300         u_short buf[32];
301         int last_fn, last_ret;
302
303         snd_printdd(KERN_DEBUG "pdacf_config called\n");
304         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
305         tuple.Attributes = 0;
306         tuple.TupleData = (cisdata_t *)buf;
307         tuple.TupleDataMax = sizeof(buf);
308         tuple.TupleOffset = 0;
309         tuple.DesiredTuple = CISTPL_CONFIG;
310         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
311         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
312         CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
313         link->conf.ConfigBase = parse.config.base;
314         link->conf.ConfigIndex = 0x5;
315
316         CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
317         link->conf.Vcc = conf.Vcc;
318
319         /* Configure card */
320         link->state |= DEV_CONFIG;
321
322         CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
323         CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
324         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
325
326         if (snd_pdacf_assign_resources(pdacf, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
327                 goto failed;
328
329         link->dev = &pdacf->node;
330         link->state &= ~DEV_CONFIG_PENDING;
331         return;
332
333 cs_failed:
334         cs_error(link->handle, last_fn, last_ret);
335 failed:
336         pcmcia_release_configuration(link->handle);
337         pcmcia_release_io(link->handle, &link->io);
338         pcmcia_release_irq(link->handle, &link->irq);
339 }
340
341 /*
342  * event callback
343  */
344 static int pdacf_event(event_t event, int priority, event_callback_args_t *args)
345 {
346         dev_link_t *link = args->client_data;
347         pdacf_t *chip = link->priv;
348
349         switch (event) {
350         case CS_EVENT_CARD_REMOVAL:
351                 snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
352                 link->state &= ~DEV_PRESENT;
353                 if (link->state & DEV_CONFIG) {
354                         chip->chip_status |= PDAUDIOCF_STAT_IS_STALE;
355                 }
356                 break;
357         case CS_EVENT_CARD_INSERTION:
358                 snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
359                 link->state |= DEV_PRESENT;
360                 pdacf_config(link);
361                 break;
362 #ifdef CONFIG_PM
363         case CS_EVENT_PM_SUSPEND:
364                 snd_printdd(KERN_DEBUG "SUSPEND\n");
365                 link->state |= DEV_SUSPEND;
366                 if (chip) {
367                         snd_printdd(KERN_DEBUG "snd_pdacf_suspend calling\n");
368                         snd_pdacf_suspend(chip->card, 0);
369                 }
370                 /* Fall through... */
371         case CS_EVENT_RESET_PHYSICAL:
372                 snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
373                 if (link->state & DEV_CONFIG)
374                         pcmcia_release_configuration(link->handle);
375                 break;
376         case CS_EVENT_PM_RESUME:
377                 snd_printdd(KERN_DEBUG "RESUME\n");
378                 link->state &= ~DEV_SUSPEND;
379                 /* Fall through... */
380         case CS_EVENT_CARD_RESET:
381                 snd_printdd(KERN_DEBUG "CARD_RESET\n");
382                 if (DEV_OK(link)) {
383                         snd_printdd(KERN_DEBUG "requestconfig...\n");
384                         pcmcia_request_configuration(link->handle, &link->conf);
385                         if (chip) {
386                                 snd_printdd(KERN_DEBUG "calling snd_pdacf_resume\n");
387                                 snd_pdacf_resume(chip->card, 0);
388                         }
389                 }
390                 snd_printdd(KERN_DEBUG "resume done!\n");
391                 break;
392 #endif
393         }
394         return 0;
395 }
396
397 /*
398  * Module entry points
399  */
400 static struct pcmcia_driver pdacf_cs_driver = {
401         .owner          = THIS_MODULE,
402         .drv            = {
403                 .name   = "snd-pdaudiocf",
404         },
405         .attach         = snd_pdacf_attach,
406         .detach         = snd_pdacf_detach
407 };
408
409 static int __init init_pdacf(void)
410 {
411         return pcmcia_register_driver(&pdacf_cs_driver);
412 }
413
414 static void __exit exit_pdacf(void)
415 {
416         pcmcia_unregister_driver(&pdacf_cs_driver);
417         snd_pdacf_detach_all();
418 }
419
420 module_init(init_pdacf);
421 module_exit(exit_pdacf);