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