ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pcmcia / vx / vx_entry.c
1 /*
2  * Driver for Digigram VXpocket soundcards
3  *
4  * PCMCIA entry part
5  *
6  * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <sound/driver.h>
24 #include <sound/core.h>
25 #include "vxpocket.h"
26 #include <pcmcia/ciscode.h>
27 #include <pcmcia/cisreg.h>
28
29
30 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
31 MODULE_DESCRIPTION("Common routines for Digigram PCMCIA VX drivers");
32 MODULE_LICENSE("GPL");
33
34 /*
35  * prototypes
36  */
37 static void vxpocket_config(dev_link_t *link);
38 static int vxpocket_event(event_t event, int priority, event_callback_args_t *args);
39
40
41 static void vxpocket_release(dev_link_t *link)
42 {
43         if (link->state & DEV_CONFIG) {
44                 /* release cs resources */
45                 pcmcia_release_configuration(link->handle);
46                 pcmcia_release_io(link->handle, &link->io);
47                 pcmcia_release_irq(link->handle, &link->irq);
48                 link->state &= ~DEV_CONFIG;
49         }
50 }
51
52 /*
53  * destructor
54  */
55 static int snd_vxpocket_free(vx_core_t *chip)
56 {
57         struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
58         struct snd_vxp_entry *hw;
59         dev_link_t *link = &vxp->link;
60
61         vxpocket_release(link);
62
63         /* Break the link with Card Services */
64         if (link->handle)
65                 pcmcia_deregister_client(link->handle);
66
67         hw = vxp->hw_entry;
68         if (hw)
69                 hw->card_list[vxp->index] = NULL;
70         chip->card = NULL;
71
72         snd_magic_kfree(chip);
73         return 0;
74 }
75
76 static int snd_vxpocket_dev_free(snd_device_t *device)
77 {
78         vx_core_t *chip = snd_magic_cast(vx_core_t, device->device_data, return -ENXIO);
79         return snd_vxpocket_free(chip);
80 }
81
82 /*
83  * snd_vxpocket_attach - attach callback for cs
84  * @hw: the hardware information
85  */
86 dev_link_t *snd_vxpocket_attach(struct snd_vxp_entry *hw)
87 {
88         client_reg_t client_reg;        /* Register with cardmgr */
89         dev_link_t *link;               /* Info for cardmgr */
90         int i, ret;
91         vx_core_t *chip;
92         struct snd_vxpocket *vxp;
93         snd_card_t *card;
94         static snd_device_ops_t ops = {
95                 .dev_free =     snd_vxpocket_dev_free,
96         };
97
98         snd_printdd(KERN_DEBUG "vxpocket_attach called\n");
99         /* find an empty slot from the card list */
100         for (i = 0; i < SNDRV_CARDS; i++) {
101                 if (! hw->card_list[i])
102                         break;
103         }
104         if (i >= SNDRV_CARDS) {
105                 snd_printk(KERN_ERR "vxpocket: too many cards found\n");
106                 return NULL;
107         }
108         if (! hw->enable_table[i])
109                 return NULL; /* disabled explicitly */
110
111         /* ok, create a card instance */
112         card = snd_card_new(hw->index_table[i], hw->id_table[i], THIS_MODULE, 0);
113         if (card == NULL) {
114                 snd_printk(KERN_ERR "vxpocket: cannot create a card instance\n");
115                 return NULL;
116         }
117
118         chip = snd_vx_create(card, hw->hardware, hw->ops,
119                              sizeof(struct snd_vxpocket) - sizeof(vx_core_t));
120         if (! chip)
121                 return NULL;
122
123         if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops) < 0) {
124                 snd_magic_kfree(chip);
125                 snd_card_free(card);
126                 return NULL;
127         }
128
129         vxp = (struct snd_vxpocket *)chip;
130         vxp->index = i;
131         vxp->hw_entry = hw;
132         chip->ibl.size = hw->ibl[i];
133         hw->card_list[i] = chip;
134
135         link = &vxp->link;
136         link->priv = chip;
137
138         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
139         link->io.NumPorts1 = 16;
140
141         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
142         // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
143
144         link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
145         if (hw->irq_list[0] == -1)
146                 link->irq.IRQInfo2 = *hw->irq_mask_p;
147         else
148                 for (i = 0; i < 4; i++)
149                         link->irq.IRQInfo2 |= 1 << hw->irq_list[i];
150         link->irq.Handler = &snd_vx_irq_handler;
151         link->irq.Instance = chip;
152
153         link->conf.Attributes = CONF_ENABLE_IRQ;
154         link->conf.Vcc = 50;
155         link->conf.IntType = INT_MEMORY_AND_IO;
156         link->conf.ConfigIndex = 1;
157         link->conf.Present = PRESENT_OPTION;
158
159         /* Chain drivers */
160         link->next = hw->dev_list;
161         hw->dev_list = link;
162
163         /* Register with Card Services */
164         client_reg.dev_info = hw->dev_info;
165         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
166         client_reg.EventMask = 
167                 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL
168 #ifdef CONFIG_PM
169                 | CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET
170                 | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME
171 #endif
172                 ;
173         client_reg.event_handler = &vxpocket_event;
174         client_reg.Version = 0x0210;
175         client_reg.event_callback_args.client_data = link;
176
177         ret = pcmcia_register_client(&link->handle, &client_reg);
178         if (ret != CS_SUCCESS) {
179                 cs_error(link->handle, RegisterClient, ret);
180                 snd_vxpocket_detach(hw, link);
181                 return NULL;
182         }
183
184         return link;
185 }
186
187
188 /**
189  * snd_vxpocket_assign_resources - initialize the hardware and card instance.
190  * @port: i/o port for the card
191  * @irq: irq number for the card
192  *
193  * this function assigns the specified port and irq, boot the card,
194  * create pcm and control instances, and initialize the rest hardware.
195  *
196  * returns 0 if successful, or a negative error code.
197  */
198 static int snd_vxpocket_assign_resources(vx_core_t *chip, int port, int irq)
199 {
200         int err;
201         snd_card_t *card = chip->card;
202         struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
203
204         snd_printdd(KERN_DEBUG "vxpocket assign resources: port = 0x%x, irq = %d\n", port, irq);
205         vxp->port = port;
206
207         sprintf(card->shortname, "Digigram %s", card->driver);
208         sprintf(card->longname, "%s at 0x%x, irq %i",
209                 card->shortname, port, irq);
210
211         if ((err = snd_vx_hwdep_new(chip)) < 0)
212                 return err;
213
214         chip->irq = irq;
215
216         if ((err = snd_card_register(chip->card)) < 0)
217                 return err;
218
219         return 0;
220 }
221
222
223 /*
224  * snd_vxpocket_detach - detach callback for cs
225  * @hw: the hardware information
226  */
227 void snd_vxpocket_detach(struct snd_vxp_entry *hw, dev_link_t *link)
228 {
229         vx_core_t *chip = snd_magic_cast(vx_core_t, link->priv, return);
230
231         snd_printdd(KERN_DEBUG "vxpocket_detach called\n");
232         /* Remove the interface data from the linked list */
233         if (hw) {
234                 dev_link_t **linkp;
235                 /* Locate device structure */
236                 for (linkp = &hw->dev_list; *linkp; linkp = &(*linkp)->next)
237                         if (*linkp == link)
238                                 break;
239                 if (*linkp)
240                         *linkp = link->next;
241         }
242         chip->chip_status |= VX_STAT_IS_STALE; /* to be sure */
243         snd_card_disconnect(chip->card);
244         snd_card_free_in_thread(chip->card);
245 }
246
247 /*
248  * snd_vxpocket_detach_all - detach all instances linked to the hw
249  */
250 void snd_vxpocket_detach_all(struct snd_vxp_entry *hw)
251 {
252         while (hw->dev_list != NULL)
253                 snd_vxpocket_detach(hw, hw->dev_list);
254 }
255
256 /*
257  * configuration callback
258  */
259
260 #define CS_CHECK(fn, ret) \
261 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
262
263 static void vxpocket_config(dev_link_t *link)
264 {
265         client_handle_t handle = link->handle;
266         vx_core_t *chip = snd_magic_cast(vx_core_t, link->priv, return);
267         struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
268         tuple_t tuple;
269         cisparse_t parse;
270         config_info_t conf;
271         u_short buf[32];
272         int last_fn, last_ret;
273
274         snd_printdd(KERN_DEBUG "vxpocket_config called\n");
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 = 1;
286
287         CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
288         link->conf.Vcc = conf.Vcc;
289
290         /* Configure card */
291         link->state |= DEV_CONFIG;
292
293         CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
294         CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
295         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
296
297         if (snd_vxpocket_assign_resources(chip, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
298                 goto failed;
299
300         link->dev = &vxp->node;
301         link->state &= ~DEV_CONFIG_PENDING;
302         return;
303
304 cs_failed:
305         cs_error(link->handle, last_fn, last_ret);
306 failed:
307         pcmcia_release_configuration(link->handle);
308         pcmcia_release_io(link->handle, &link->io);
309         pcmcia_release_irq(link->handle, &link->irq);
310 }
311
312
313 /*
314  * event callback
315  */
316 static int vxpocket_event(event_t event, int priority, event_callback_args_t *args)
317 {
318         dev_link_t *link = args->client_data;
319         vx_core_t *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 |= VX_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                 vxpocket_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_vx_suspend calling\n");
340                         snd_vx_suspend(chip);
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                         //struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
356                         snd_printdd(KERN_DEBUG "requestconfig...\n");
357                         pcmcia_request_configuration(link->handle, &link->conf);
358                         if (chip) {
359                                 snd_printdd(KERN_DEBUG "calling snd_vx_resume\n");
360                                 snd_vx_resume(chip);
361                         }
362                 }
363                 snd_printdd(KERN_DEBUG "resume done!\n");
364                 break;
365 #endif
366         }
367         return 0;
368 }
369
370 /*
371  * exported stuffs
372  */
373 EXPORT_SYMBOL(snd_vxpocket_ops);
374 EXPORT_SYMBOL(snd_vxpocket_attach);
375 EXPORT_SYMBOL(snd_vxpocket_detach);
376 EXPORT_SYMBOL(snd_vxpocket_detach_all);