[PATCH] pcmcia: remove references to pcmcia/version.h
[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
39
40 static void vxpocket_release(dev_link_t *link)
41 {
42         if (link->state & DEV_CONFIG) {
43                 /* release cs resources */
44                 pcmcia_release_configuration(link->handle);
45                 pcmcia_release_io(link->handle, &link->io);
46                 pcmcia_release_irq(link->handle, &link->irq);
47                 link->state &= ~DEV_CONFIG;
48         }
49 }
50
51 /*
52  * destructor
53  */
54 static int snd_vxpocket_free(vx_core_t *chip)
55 {
56         struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
57         struct snd_vxp_entry *hw;
58         dev_link_t *link = &vxp->link;
59
60         vxpocket_release(link);
61
62         /* Break the link with Card Services */
63         if (link->handle)
64                 pcmcia_deregister_client(link->handle);
65
66         hw = vxp->hw_entry;
67         if (hw)
68                 hw->card_list[vxp->index] = NULL;
69         chip->card = NULL;
70         kfree(chip->dev);
71
72         snd_vx_free_firmware(chip);
73         kfree(chip);
74         return 0;
75 }
76
77 static int snd_vxpocket_dev_free(snd_device_t *device)
78 {
79         vx_core_t *chip = device->device_data;
80         return snd_vxpocket_free(chip);
81 }
82
83 /*
84  * snd_vxpocket_attach - attach callback for cs
85  * @hw: the hardware information
86  */
87 dev_link_t *snd_vxpocket_attach(struct snd_vxp_entry *hw)
88 {
89         client_reg_t client_reg;        /* Register with cardmgr */
90         dev_link_t *link;               /* Info for cardmgr */
91         int i, ret;
92         vx_core_t *chip;
93         struct snd_vxpocket *vxp;
94         snd_card_t *card;
95         static snd_device_ops_t ops = {
96                 .dev_free =     snd_vxpocket_dev_free,
97         };
98
99         snd_printdd(KERN_DEBUG "vxpocket_attach called\n");
100         /* find an empty slot from the card list */
101         for (i = 0; i < SNDRV_CARDS; i++) {
102                 if (! hw->card_list[i])
103                         break;
104         }
105         if (i >= SNDRV_CARDS) {
106                 snd_printk(KERN_ERR "vxpocket: too many cards found\n");
107                 return NULL;
108         }
109         if (! hw->enable_table[i])
110                 return NULL; /* disabled explicitly */
111
112         /* ok, create a card instance */
113         card = snd_card_new(hw->index_table[i], hw->id_table[i], THIS_MODULE, 0);
114         if (card == NULL) {
115                 snd_printk(KERN_ERR "vxpocket: cannot create a card instance\n");
116                 return NULL;
117         }
118
119         chip = snd_vx_create(card, hw->hardware, hw->ops,
120                              sizeof(struct snd_vxpocket) - sizeof(vx_core_t));
121         if (! chip)
122                 return NULL;
123
124 #ifdef SND_VX_FW_LOADER
125         /* fake a device here since pcmcia doesn't give a valid device... */
126         chip->dev = kcalloc(1, sizeof(*chip->dev), GFP_KERNEL);
127         if (! chip->dev) {
128                 snd_printk(KERN_ERR "vxp: can't malloc chip->dev\n");
129                 kfree(chip);
130                 snd_card_free(card);
131                 return NULL;
132         }
133         device_initialize(chip->dev);
134         sprintf(chip->dev->bus_id, "vxpocket%d", i);
135 #endif
136
137         if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops) < 0) {
138                 kfree(chip);
139                 snd_card_free(card);
140                 return NULL;
141         }
142
143         vxp = (struct snd_vxpocket *)chip;
144         vxp->index = i;
145         vxp->hw_entry = hw;
146         chip->ibl.size = hw->ibl[i];
147         hw->card_list[i] = chip;
148
149         link = &vxp->link;
150         link->priv = chip;
151
152         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
153         link->io.NumPorts1 = 16;
154
155         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
156         // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
157
158         link->irq.IRQInfo1 = IRQ_LEVEL_ID;
159         link->irq.Handler = &snd_vx_irq_handler;
160         link->irq.Instance = chip;
161
162         link->conf.Attributes = CONF_ENABLE_IRQ;
163         link->conf.Vcc = 50;
164         link->conf.IntType = INT_MEMORY_AND_IO;
165         link->conf.ConfigIndex = 1;
166         link->conf.Present = PRESENT_OPTION;
167
168         /* Register with Card Services */
169         memset(&client_reg, 0, sizeof(client_reg));
170         client_reg.dev_info = hw->dev_info;
171         client_reg.Version = 0x0210;
172         client_reg.event_callback_args.client_data = link;
173
174         ret = pcmcia_register_client(&link->handle, &client_reg);
175         if (ret != CS_SUCCESS) {
176                 cs_error(link->handle, RegisterClient, ret);
177                 snd_card_free(card);
178                 return NULL;
179         }
180
181         /* Chain drivers */
182         link->next = hw->dev_list;
183         hw->dev_list = link;
184
185         /* snd_card_set_pm_callback(card, snd_vxpocket_suspend, snd_vxpocket_resume, chip); */
186
187         return link;
188 }
189
190
191 /**
192  * snd_vxpocket_assign_resources - initialize the hardware and card instance.
193  * @port: i/o port for the card
194  * @irq: irq number for the card
195  *
196  * this function assigns the specified port and irq, boot the card,
197  * create pcm and control instances, and initialize the rest hardware.
198  *
199  * returns 0 if successful, or a negative error code.
200  */
201 static int snd_vxpocket_assign_resources(vx_core_t *chip, int port, int irq)
202 {
203         int err;
204         snd_card_t *card = chip->card;
205         struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
206
207         snd_printdd(KERN_DEBUG "vxpocket assign resources: port = 0x%x, irq = %d\n", port, irq);
208         vxp->port = port;
209
210         sprintf(card->shortname, "Digigram %s", card->driver);
211         sprintf(card->longname, "%s at 0x%x, irq %i",
212                 card->shortname, port, irq);
213
214         chip->irq = irq;
215
216         if ((err = snd_vx_setup_firmware(chip)) < 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;
230
231         if (! link)
232                 return;
233
234         chip = link->priv;
235
236         snd_printdd(KERN_DEBUG "vxpocket_detach called\n");
237         /* Remove the interface data from the linked list */
238         if (hw) {
239                 dev_link_t **linkp;
240                 /* Locate device structure */
241                 for (linkp = &hw->dev_list; *linkp; linkp = &(*linkp)->next)
242                         if (*linkp == link) {
243                                 *linkp = link->next;
244                                 break;
245                         }
246         }
247         chip->chip_status |= VX_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 vxpocket_config(dev_link_t *link)
260 {
261         client_handle_t handle = link->handle;
262         vx_core_t *chip = link->priv;
263         struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
264         tuple_t tuple;
265         cisparse_t *parse = NULL;
266         u_short buf[32];
267         int last_fn, last_ret;
268
269         snd_printdd(KERN_DEBUG "vxpocket_config called\n");
270         parse = kmalloc(sizeof(*parse), GFP_KERNEL);
271         if (! parse) {
272                 snd_printk(KERN_ERR "vx: cannot allocate\n");
273                 return;
274         }
275         tuple.Attributes = 0;
276         tuple.TupleData = (cisdata_t *)buf;
277         tuple.TupleDataMax = sizeof(buf);
278         tuple.TupleOffset = 0;
279         tuple.DesiredTuple = CISTPL_CONFIG;
280         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
281         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
282         CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, parse));
283         link->conf.ConfigBase = parse->config.base;
284         link->conf.Present = parse->config.rmask[0];
285
286         /* Configure card */
287         link->state |= DEV_CONFIG;
288
289         CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
290         CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
291         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
292
293         if (snd_vxpocket_assign_resources(chip, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
294                 goto failed;
295
296         link->dev = &vxp->node;
297         link->state &= ~DEV_CONFIG_PENDING;
298         kfree(parse);
299         return;
300
301 cs_failed:
302         cs_error(link->handle, last_fn, last_ret);
303 failed:
304         pcmcia_release_configuration(link->handle);
305         pcmcia_release_io(link->handle, &link->io);
306         pcmcia_release_irq(link->handle, &link->irq);
307         link->state &= ~DEV_CONFIG;
308         kfree(parse);
309 }
310
311
312 /*
313  * event callback
314  */
315 int vxpocket_event(event_t event, int priority, event_callback_args_t *args)
316 {
317         dev_link_t *link = args->client_data;
318         vx_core_t *chip = link->priv;
319
320         switch (event) {
321         case CS_EVENT_CARD_REMOVAL:
322                 snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
323                 link->state &= ~DEV_PRESENT;
324                 if (link->state & DEV_CONFIG) {
325                         chip->chip_status |= VX_STAT_IS_STALE;
326                 }
327                 break;
328         case CS_EVENT_CARD_INSERTION:
329                 snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
330                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
331                 vxpocket_config(link);
332                 break;
333 #ifdef CONFIG_PM
334         case CS_EVENT_PM_SUSPEND:
335                 snd_printdd(KERN_DEBUG "SUSPEND\n");
336                 link->state |= DEV_SUSPEND;
337                 if (chip && chip->card->pm_suspend) {
338                         snd_printdd(KERN_DEBUG "snd_vx_suspend calling\n");
339                         chip->card->pm_suspend(chip->card, PMSG_SUSPEND);
340                 }
341                 /* Fall through... */
342         case CS_EVENT_RESET_PHYSICAL:
343                 snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
344                 if (link->state & DEV_CONFIG)
345                         pcmcia_release_configuration(link->handle);
346                 break;
347         case CS_EVENT_PM_RESUME:
348                 snd_printdd(KERN_DEBUG "RESUME\n");
349                 link->state &= ~DEV_SUSPEND;
350                 /* Fall through... */
351         case CS_EVENT_CARD_RESET:
352                 snd_printdd(KERN_DEBUG "CARD_RESET\n");
353                 if (DEV_OK(link)) {
354                         //struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
355                         snd_printdd(KERN_DEBUG "requestconfig...\n");
356                         pcmcia_request_configuration(link->handle, &link->conf);
357                         if (chip && chip->card->pm_resume) {
358                                 snd_printdd(KERN_DEBUG "calling snd_vx_resume\n");
359                                 chip->card->pm_resume(chip->card);
360                         }
361                 }
362                 snd_printdd(KERN_DEBUG "resume done!\n");
363                 break;
364 #endif
365         }
366         return 0;
367 }
368
369 /*
370  * exported stuffs
371  */
372 EXPORT_SYMBOL(snd_vxpocket_ops);
373 EXPORT_SYMBOL(snd_vxpocket_attach);
374 EXPORT_SYMBOL(vxpocket_event);
375 EXPORT_SYMBOL(snd_vxpocket_detach);