summaryrefslogtreecommitdiffstats
path: root/drivers/char/radio.c
blob: 2b79e9872fa4acf0d2b8449832689c5b80bffb4f (plain)
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
/*
 * Radio Card Device Driver for Linux
 *
 * (c) 1997 Matthew Kirkwood <weejock@ferret.lmh.ox.ac.uk>
 *
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/fs.h>

#include <linux/miscdevice.h>

#include <asm/uaccess.h>

#include <linux/config.h>

#include <linux/radio.h>
#ifdef CONFIG_RADIO_RTRACK
#include "rtrack.h"
#endif
#ifdef CONFIG_RADIO_WINRADIO
#include "winradio.h"
#endif

int radio_open(struct inode *inode, struct file *file);
int radio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);

/* /dev/radio interface */
static struct file_operations radio_fops = {
	NULL,			/* seek */
	NULL,			/* read */
	NULL,			/* write */
	NULL,			/* readdir */
	NULL,			/* select */
	&radio_ioctl,		/* ioctl */
	NULL,			/* mmap */
	&radio_open,		/* we're not allowed NULL, it seems... */
	NULL			/* release */
};

static struct miscdevice radio_miscdevice = {
	RADIO_MINOR,		/* minor device number */
	"radio",		/* title */
	&radio_fops,		/* file operations */
	NULL, NULL		/* previous and next (not our business) */
};


static struct radio_device *firstdevice, *lastdevice;
static int numdevices;

__initfunc(void radio_init(void))
{
	/* register the handler for the device number... */
	if(misc_register(&radio_miscdevice)) {
		printk("radio: couldn't register misc device\n");
		return;
	}

	/* do some general initialisation stuff */
	lastdevice = firstdevice = NULL;
	numdevices = 0;

#ifdef	CONFIG_RADIO_RTRACK
	radiotrack_init();
#endif
#ifdef	CONFIG_RADIO_WINRADIO
	printk("oooops.  no winradio support yet... :-(\n");
#endif
/* etc.... */

	printk("radio: registered %d devices\n", numdevices);
}


/* according to drivers/char/misc.c, the "open" call must not be NULL.
 * I'm not sure if I approve, but I didn't write it, so...
 */
int radio_open(struct inode *inode, struct file *file)
{
	return 0;
}


/* append a device to the linked list... */
int radio_add_device(struct radio_device *newdev)
{
	if(firstdevice == NULL) {
		firstdevice = newdev;
	} else {
		lastdevice->next = newdev;
	}
	lastdevice = newdev; numdevices++;
	newdev->cap->dev_num=numdevices;
	newdev->next = NULL;			/* don't need, but... */
	return(numdevices);
}

struct radio_device *getdev(int index)
{
struct radio_device *retval;

	if(index > numdevices)
		return NULL;		/* let's have a bit less of that */

	retval = firstdevice;
	for(;index;index--)
		retval = retval->next;

	return retval;
}


/* interface routine */
int radio_ioctl(struct inode *inode, struct file *file,
		unsigned int cmd, unsigned long arg)
{
struct radio_device *dev;
struct radio_ctl *ctl_arg = (struct radio_ctl*)arg;
int nowrite;
int val, ret;

	if((void*)arg == NULL)
		return -EFAULT;		/* XXX - check errnos are OK */


	switch(cmd) {
	case RADIO_NUMDEVS:
		return (put_user(numdevices,(int*)arg) ? -EFAULT : 0);


	case RADIO_GETCAPS:
	/* p'raps I should verify for read then write ?? */
		if(verify_area(VERIFY_WRITE, (void*)arg, sizeof(struct radio_cap)))
			return -EFAULT;
		if((dev = getdev(((struct radio_cap*)arg)->dev_num)) == NULL)
			return -EINVAL;
		copy_to_user((void*)arg, dev->cap, sizeof(struct radio_cap));
		return 0;


	case RADIO_GETBNDCAP:
		if(verify_area(VERIFY_WRITE, (void*)arg, sizeof(struct radio_band)))
			return -EFAULT;

		if((dev = getdev(((struct radio_band*)arg)->dev_num)) == NULL)
			return -EINVAL;

		val = ((struct radio_band*)arg)->index;
		if(val >= dev->cap->num_bwidths)
			return -EINVAL;			/* XXX errno */

		copy_to_user((void*)arg, (dev->bands)+(val*sizeof(struct radio_band)),
			sizeof(struct radio_band));
		return 0;
	}


/* now, we know that arg points to a struct radio_ctl */
	/* get the requested device */
	if(verify_area(VERIFY_READ, ctl_arg, sizeof(struct radio_ctl)))
		return -EFAULT;

	if((dev = getdev(ctl_arg->dev_num)) == NULL)
		return -EINVAL;

	nowrite = verify_area(VERIFY_WRITE, ctl_arg, sizeof(struct radio_ctl));

	val = ctl_arg->value;

	switch(cmd) {
	case RADIO_SETVOL:
		if((val < dev->cap->volmin) || (val > dev->cap->volmax))
			return -EINVAL;
		if((ret = (*(dev->setvol))(dev, val)))
			return ret;
		dev->curvol = val;
		return 0;

	case RADIO_GETVOL:
		if(nowrite)
			return -EFAULT;
		ctl_arg->value = dev->curvol;
		return 0;


	case RADIO_SETBAND:
		if(val >= dev->cap->num_bwidths)
			return -EINVAL;
		if((ret = (*(dev->setband))(dev, val)))
			return ret;
		dev->curband = val;
		return 0;

	case RADIO_GETBAND:
		if(nowrite)
			return -EFAULT;
		ctl_arg->value = dev->curband;
		return 0;


	case RADIO_SETFREQ: {
	struct radio_band *bp;

		bp = (dev->bands) + ((dev->curband) * sizeof(struct radio_band));
		if((val < bp->freqmin) || (val > bp->freqmax))
			return -EINVAL;
		if((ret = (*(dev->setfreq))(dev, val)))
			return ret;
		dev->curfreq = val;
		return 0;
	}

	case RADIO_GETFREQ:
		if(nowrite)
			return -EFAULT;
		ctl_arg->value = dev->curfreq;
		return 0;


	case RADIO_GETSIGSTR:
		if(nowrite)
			return -EFAULT;
		ctl_arg->value = (*(dev->getsigstr))(dev);
		return 0;


	default:
		return -ENOSYS;
	}
}