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