File: pcsp.c

package info (click to toggle)
alsa-driver 1.0.13-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 20,108 kB
  • ctags: 50,477
  • sloc: ansic: 319,881; sh: 32,930; makefile: 2,015; python: 1,527; perl: 1,316; xml: 896; awk: 66
file content (343 lines) | stat: -rw-r--r-- 8,221 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * PC-Speaker driver for Linux
 *
 * Copyright (C) 1997-2001  David Woodhouse
 * Copyright (C) 2001-2004  Stas Sergeev
 */

#include <linux/config.h>
#include <sound/driver.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <sound/core.h>
#include <sound/initval.h>
#include <sound/pcm.h>

#include <linux/interrupt.h>
#include <linux/timex.h>
#include <linux/ioport.h>
#include <asm/io.h>
#include <asm/bitops.h>
#include <asm/i8253.h>
#ifdef CONFIG_APM_CPU_IDLE
#include <linux/pm.h>
#endif
#include "pcsp.h"
#include "pcsp_input.h"

MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>");
MODULE_DESCRIPTION("PC-Speaker driver");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{PC-Speaker, pcsp}}");

static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
static int enable = SNDRV_DEFAULT_ENABLE1;	/* Enable this card */
static int no_test_speed = 0;

module_param(id, charp, 0444);
MODULE_PARM_DESC(id, "ID string for pcsp soundcard.");
module_param(enable, bool, 0444);
MODULE_PARM_DESC(enable, "dummy");
module_param(no_test_speed, int, 0444);
MODULE_PARM_DESC(no_test_speed, "dont test the CPU speed on startup");

struct snd_pcsp *snd_pcsp_chip = NULL;

#ifdef CONFIG_APM_CPU_IDLE
static void (*saved_pm_idle)(void);
#endif

static int (*pcsp_timer_func)(struct snd_pcsp *chip) = NULL;
void *pcsp_timer_hook;


/*
 * this is the PCSP IRQ handler
 */
static int pcsp_timer(struct pt_regs *regs)
{
	if (pcsp_timer_func)
		return pcsp_timer_func(snd_pcsp_chip);
	return 0;
}

int pcsp_set_timer_hook(struct snd_pcsp *chip, int (*func)(struct snd_pcsp *chip))
{
	unsigned long flags;
	int err;
	if (!func)
		return -1;
	spin_lock_irqsave(&chip->lock, flags);
	if ((err = grab_timer_hook(pcsp_timer_hook))) {
		spin_unlock_irqrestore(&chip->lock, flags);
		printk(KERN_WARNING "PCSP: unable to grab timer!\n");
		return err;
	}
	pcsp_timer_func = func;
#ifdef CONFIG_APM_CPU_IDLE
	saved_pm_idle = pm_idle;
	pm_idle = NULL;
#endif
	spin_unlock_irqrestore(&chip->lock, flags);
	return 0;
}

void pcsp_release_timer_hook(struct snd_pcsp *chip)
{
	unsigned long flags;
	spin_lock_irqsave(&chip->lock, flags);
	pcsp_timer_func = NULL;
#ifdef CONFIG_APM_CPU_IDLE
	pm_idle = saved_pm_idle;
#endif
	ungrab_timer_hook(pcsp_timer_hook);
	spin_unlock_irqrestore(&chip->lock, flags);
}

static int tst_len[2];
volatile static int pcsp_test_running;

/*
   the timer-int for testing cpu-speed, mostly the same as
   for PC-Speaker
 */
static int pcsp_test_intspeed(struct snd_pcsp *chip)
{
	unsigned char test_buffer[256];
	if (chip->index < tst_len[chip->cur_buf]) {
		outb(chip->e,     0x61);
		outb(test_buffer[chip->index], 0x42);
		outb(chip->e ^ 1, 0x61);

		chip->index += 2;
	}
	if (chip->index >= tst_len[chip->cur_buf]) {
		chip->index = 0;
		tst_len[chip->cur_buf] = 0;
		chip->cur_buf ^= 1;
		if (tst_len[chip->cur_buf] == 0xFFFF)
			chip->cur_buf ^= 1;
	}

	++pcsp_test_running;
	return 1;
}

/*
   this routine measures the time needed for one timer-int if
   we play thru PC-Speaker. This is kind of ugly but does the
   trick.
 */
static int pcsp_measurement(struct snd_pcsp *chip, int addon, int *rticks)
{
	int count, err;
	unsigned long flags;

	chip->index	  = 0;
	tst_len[0]        = 5 + addon;
	tst_len[1] 	  = 0;
	chip->cur_buf     = 0;
	chip->e 	  = inb(0x61) & 0xFC;

	pcsp_test_running = 0;

	if ((err = pcsp_set_timer_hook(chip, pcsp_test_intspeed)))
		return err;
	/*
	  Perhaps we need some sort of timeout here, but if IRQ0
	  isn't working the system hangs later ...
	*/
	while (pcsp_test_running < 5);
	pcsp_release_timer_hook(chip);

	spin_lock_irqsave(&i8253_lock, flags);
	outb_p(0x00, 0x43);		/* latch the count ASAP */
	count = inb_p(0x40);		/* read the latched count */
	count |= inb(0x40) << 8;
	spin_unlock_irqrestore(&i8253_lock, flags);

	*rticks = LATCH - count;
	return 0;
}

static int __init pcsp_test_speed(struct snd_pcsp *chip, int *rmin_div)
{
	int worst, worst1, best, best1, min_div;

	if (pcsp_measurement(chip, 0, &worst))
		return 0;
	if (pcsp_measurement(chip, 0, &worst1))
		return 0;
	if (pcsp_measurement(chip, 5, &best))
		return 0;
	if (pcsp_measurement(chip, 5, &best1))
		return 0;

	worst = max(worst, worst1);
	best  = min(best, best1);

#if PCSP_DEBUG
	printk(KERN_INFO "PCSP-Timerint needs %d Ticks in worst case\n", worst);
	printk(KERN_INFO "PCSP-Timerint needs %d Ticks in best case\n", best);
#endif
	/* We allow a CPU-usage of 80 % for the best-case ! */
	*rmin_div = min_div = best * 10 / 8;
	printk(KERN_INFO "PCSP: Measurement: maximal modulation freq %d Hz.\n",
		CLOCK_TICK_RATE / min_div);

	if (min_div > MAX_DIV) {
		printk(KERN_WARNING "This is too SLOW! PCSP-driver DISABLED\n");
		return 0;
	}
	return 1;
}

static int snd_pcsp_free(struct snd_pcsp *chip)
{
	unregister_timer_hook(pcsp_timer_hook);
	kfree(chip);
	return 0;
}

static int snd_pcsp_dev_free(struct snd_device *device)
{
	struct snd_pcsp *chip = device->device_data;
	return snd_pcsp_free(chip);
}

static int __init snd_pcsp_create(struct snd_card *card)
{
	static struct snd_device_ops ops = {
		.dev_free = snd_pcsp_dev_free,
	};
	int err;
	int div, min_div, order;
	int pcsp_enabled = 1;

	snd_pcsp_chip = kcalloc(1, sizeof(struct snd_pcsp), GFP_KERNEL);
	if (!snd_pcsp_chip)
		return -ENOMEM;
	spin_lock_init(&snd_pcsp_chip->lock);

	snd_pcsp_chip->card = card;
	snd_pcsp_chip->port = 0x61;
	snd_pcsp_chip->irq = TIMER_IRQ;
	snd_pcsp_chip->dma = -1;

	if (!(pcsp_timer_hook = register_timer_hook(pcsp_timer))) {
		printk(KERN_WARNING "PCSP could not register timer hook!");
		snd_pcsp_free(snd_pcsp_chip);
		return -EBUSY;
	}

	if (!no_test_speed) {
		pcsp_enabled = pcsp_test_speed(snd_pcsp_chip, &min_div);
	} else {
		min_div = MAX_DIV;
	}
	if (!pcsp_enabled) {
		snd_pcsp_free(snd_pcsp_chip);
		return -EIO;
	}

	div = MAX_DIV / min_div;
	order = fls(div) - 1;

	snd_pcsp_chip->max_treble   = min(order, PCSP_MAX_POSS_TREBLE);
	snd_pcsp_chip->treble       = min(snd_pcsp_chip->max_treble, 1);
	snd_pcsp_chip->gain         = PCSP_DEFAULT_GAIN;
	snd_pcsp_chip->index	    = 0;
	snd_pcsp_chip->bass         = 0;
	snd_pcsp_chip->cur_buf	    = 0;
	snd_pcsp_chip->timer_active = 0;
	snd_pcsp_chip->last_clocks  =
	snd_pcsp_chip->timer_latch  =
	snd_pcsp_chip->clockticks   = LATCH;
	snd_pcsp_chip->volume       = PCSP_MAX_VOLUME;
	snd_pcsp_chip->enable       = 1;
	snd_pcsp_chip->pcspkr       = 1;

	/* Register device */
	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, snd_pcsp_chip, &ops)) < 0) {
		snd_pcsp_free(snd_pcsp_chip);
		return err;
	}

	return 0;
}

static int __init snd_card_pcsp_probe(int dev)
{
	struct snd_card *card;
	int err;

	if (dev != 0)
		return -EINVAL;

	card = snd_card_new(index, id, THIS_MODULE, 0);
	if (!card)
		return -ENOMEM;

	if ((err = snd_pcsp_create(card)) < 0) {
		snd_card_free(card);
		return err;
	}
	if ((err = snd_pcsp_new_pcm(snd_pcsp_chip)) < 0) {
		snd_card_free(card);
		return err;
	}
	if ((err = snd_pcsp_new_mixer(snd_pcsp_chip)) < 0) {
		snd_card_free(card);
		return err;
	}

	strcpy(card->driver, "PC-Speaker");
	strcpy(card->shortname, snd_pcsp_chip->pcm->name);
	sprintf(card->longname, "Internal PC-Speaker at port 0x%x, irq %d",
	    snd_pcsp_chip->port, snd_pcsp_chip->irq);

	if ((err = snd_card_register(card)) < 0) {
		snd_card_free(card);
		return err;
	}

	pcspkr_input_init(snd_pcsp_chip);

	return 0;
}

static int __init alsa_card_pcsp_init(void)
{
	int dev = 0, cards = 0;

#ifdef CONFIG_DEBUG_PAGEALLOC
	/* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */
	printk(KERN_WARNING "PCSP: Warning, CONFIG_DEBUG_PAGEALLOC is enabled!\n"
		"You have to disable it if you want to use the PC-Speaker driver.\n"
		"Unless it is disabled, enjoy the horrible, distorted and crackling "
		"noise.\n");
#endif

	if (enable) {
		if (snd_card_pcsp_probe(dev) >= 0)
			cards++;
	}
	if (!cards) {
#ifdef MODULE
		printk(KERN_ERR "PC-Speaker soundcard not found or device busy\n");
#endif
		return -ENODEV;
	}

	return 0;
}

static void __exit alsa_card_pcsp_exit(void)
{
	pcspkr_input_remove(snd_pcsp_chip);
	snd_card_free(snd_pcsp_chip->card);
}

module_init(alsa_card_pcsp_init);
module_exit(alsa_card_pcsp_exit);