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