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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Miro PCM20 radio driver for Linux radio support
* (c) 1998 Ruurd Reitsma <R.A.Reitsma@wbmt.tudelft.nl>
* Thanks to Norberto Pellici for the ACI device interface specification
* The API part is based on the radiotrack driver by M. Kirkwood
* This driver relies on the aci mixer provided by the snd-miro
* ALSA driver.
* Look there for further info...
*
* From the original miro RDS sources:
*
* (c) 2001 Robert Siemer <Robert.Siemer@gmx.de>
*
* Many thanks to Fred Seidel <seidel@metabox.de>, the
* designer of the RDS decoder hardware. With his help
* I was able to code this driver.
* Thanks also to Norberto Pellicci, Dominic Mounteney
* <DMounteney@pinnaclesys.com> and www.teleauskunft.de
* for good hints on finding Fred. It was somewhat hard
* to locate him here in Germany... [:
*
* This code has been reintroduced and converted to use
* the new V4L2 RDS API by:
*
* Hans Verkuil <hans.verkuil@cisco.com>
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/videodev2.h>
#include <linux/kthread.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-event.h>
#include <sound/aci.h>
#define RDS_DATASHIFT 2 /* Bit 2 */
#define RDS_DATAMASK (1 << RDS_DATASHIFT)
#define RDS_BUSYMASK 0x10 /* Bit 4 */
#define RDS_CLOCKMASK 0x08 /* Bit 3 */
#define RDS_DATA(x) (((x) >> RDS_DATASHIFT) & 1)
#define RDS_STATUS 0x01
#define RDS_STATIONNAME 0x02
#define RDS_TEXT 0x03
#define RDS_ALTFREQ 0x04
#define RDS_TIMEDATE 0x05
#define RDS_PI_CODE 0x06
#define RDS_PTYTATP 0x07
#define RDS_RESET 0x08
#define RDS_RXVALUE 0x09
static int radio_nr = -1;
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
struct pcm20 {
struct v4l2_device v4l2_dev;
struct video_device vdev;
struct v4l2_ctrl_handler ctrl_handler;
struct v4l2_ctrl *rds_pty;
struct v4l2_ctrl *rds_ps_name;
struct v4l2_ctrl *rds_radio_test;
struct v4l2_ctrl *rds_ta;
struct v4l2_ctrl *rds_tp;
struct v4l2_ctrl *rds_ms;
/* thread for periodic RDS status checking */
struct task_struct *kthread;
unsigned long freq;
u32 audmode;
struct snd_miro_aci *aci;
struct mutex lock;
};
static struct pcm20 pcm20_card = {
.freq = 87 * 16000,
.audmode = V4L2_TUNER_MODE_STEREO,
};
static int rds_waitread(struct snd_miro_aci *aci)
{
u8 byte;
int i = 2000;
do {
byte = inb(aci->aci_port + ACI_REG_RDS);
i--;
} while ((byte & RDS_BUSYMASK) && i);
/*
* It's magic, but without this the data that you read later on
* is unreliable and full of bit errors. With this 1 usec delay
* everything is fine.
*/
udelay(1);
return i ? byte : -1;
}
static int rds_rawwrite(struct snd_miro_aci *aci, u8 byte)
{
if (rds_waitread(aci) >= 0) {
outb(byte, aci->aci_port + ACI_REG_RDS);
return 0;
}
return -1;
}
static int rds_write(struct snd_miro_aci *aci, u8 byte)
{
u8 sendbuffer[8];
int i;
for (i = 7; i >= 0; i--)
sendbuffer[7 - i] = (byte & (1 << i)) ? RDS_DATAMASK : 0;
sendbuffer[0] |= RDS_CLOCKMASK;
for (i = 0; i < 8; i++)
rds_rawwrite(aci, sendbuffer[i]);
return 0;
}
static int rds_readcycle_nowait(struct snd_miro_aci *aci)
{
outb(0, aci->aci_port + ACI_REG_RDS);
return rds_waitread(aci);
}
static int rds_readcycle(struct snd_miro_aci *aci)
{
if (rds_rawwrite(aci, 0) < 0)
return -1;
return rds_waitread(aci);
}
static int rds_ack(struct snd_miro_aci *aci)
{
int i = rds_readcycle(aci);
if (i < 0)
return -1;
if (i & RDS_DATAMASK)
return 0; /* ACK */
return 1; /* NACK */
}
static int rds_cmd(struct snd_miro_aci *aci, u8 cmd, u8 databuffer[], u8 datasize)
{
int i, j;
rds_write(aci, cmd);
/* RDS_RESET doesn't need further processing */
if (cmd == RDS_RESET)
return 0;
if (rds_ack(aci))
return -EIO;
if (datasize == 0)
return 0;
/* to be able to use rds_readcycle_nowait()
I have to waitread() here */
if (rds_waitread(aci) < 0)
return -1;
memset(databuffer, 0, datasize);
for (i = 0; i < 8 * datasize; i++) {
j = rds_readcycle_nowait(aci);
if (j < 0)
return -EIO;
databuffer[i / 8] |= RDS_DATA(j) << (7 - (i % 8));
}
return 0;
}
static int pcm20_setfreq(struct pcm20 *dev, unsigned long freq)
{
unsigned char freql;
unsigned char freqh;
struct snd_miro_aci *aci = dev->aci;
freq /= 160;
if (!(aci->aci_version == 0x07 || aci->aci_version >= 0xb0))
freq /= 10; /* I don't know exactly which version
* needs this hack */
freql = freq & 0xff;
freqh = freq >> 8;
rds_cmd(aci, RDS_RESET, NULL, 0);
return snd_aci_cmd(aci, ACI_WRITE_TUNE, freql, freqh);
}
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct pcm20 *dev = video_drvdata(file);
strscpy(v->driver, "Miro PCM20", sizeof(v->driver));
strscpy(v->card, "Miro PCM20", sizeof(v->card));
snprintf(v->bus_info, sizeof(v->bus_info), "ISA:%s", dev->v4l2_dev.name);
v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
return 0;
}
static bool sanitize(char *p, int size)
{
int i;
bool ret = true;
for (i = 0; i < size; i++) {
if (p[i] < 32) {
p[i] = ' ';
ret = false;
}
}
return ret;
}
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct pcm20 *dev = video_drvdata(file);
int res;
u8 buf;
if (v->index)
return -EINVAL;
strscpy(v->name, "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->rangelow = 87*16000;
v->rangehigh = 108*16000;
res = snd_aci_cmd(dev->aci, ACI_READ_TUNERSTATION, -1, -1);
v->signal = (res & 0x80) ? 0 : 0xffff;
|