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
|
/*
* Driver for the MasterKit MA901 USB FM radio. This device plugs
* into the USB port and an analog audio input or headphones, so this thing
* only deals with initialization, frequency setting, volume.
*
* Copyright (c) 2012 Alexey Klimov <klimov.linux@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/videodev2.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#include <linux/usb.h>
#include <linux/mutex.h>
#define DRIVER_AUTHOR "Alexey Klimov <klimov.linux@gmail.com>"
#define DRIVER_DESC "Masterkit MA901 USB FM radio driver"
#define DRIVER_VERSION "0.0.1"
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
MODULE_VERSION(DRIVER_VERSION);
#define USB_MA901_VENDOR 0x16c0
#define USB_MA901_PRODUCT 0x05df
/* dev_warn macro with driver name */
#define MA901_DRIVER_NAME "radio-ma901"
#define ma901radio_dev_warn(dev, fmt, arg...) \
dev_warn(dev, MA901_DRIVER_NAME " - " fmt, ##arg)
#define ma901radio_dev_err(dev, fmt, arg...) \
dev_err(dev, MA901_DRIVER_NAME " - " fmt, ##arg)
/* Probably USB_TIMEOUT should be modified in module parameter */
#define BUFFER_LENGTH 8
#define USB_TIMEOUT 500
#define FREQ_MIN 87.5
#define FREQ_MAX 108.0
#define FREQ_MUL 16000
#define MA901_VOLUME_MAX 16
#define MA901_VOLUME_MIN 0
/* Commands that device should understand
* List isn't full and will be updated with implementation of new functions
*/
#define MA901_RADIO_SET_FREQ 0x03
#define MA901_RADIO_SET_VOLUME 0x04
#define MA901_RADIO_SET_MONO_STEREO 0x05
/* Comfortable defines for ma901radio_set_stereo */
#define MA901_WANT_STEREO 0x50
#define MA901_WANT_MONO 0xd0
/* module parameter */
static int radio_nr = -1;
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(radio_nr, "Radio file number");
/* Data for one (physical) device */
struct ma901radio_device {
/* reference to USB and video device */
struct usb_device *usbdev;
struct usb_interface *intf;
struct video_device vdev;
struct v4l2_device v4l2_dev;
struct v4l2_ctrl_handler hdl;
u8 *buffer;
struct mutex lock; /* buffer locking */
int curfreq;
u16 volume;
int stereo;
bool muted;
};
static inline struct ma901radio_device *to_ma901radio_dev(struct v4l2_device *v4l2_dev)
{
return container_of(v4l2_dev, struct ma901radio_device, v4l2_dev);
}
/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
static int ma901radio_set_freq(struct ma901radio_device *radio, int freq)
{
unsigned int freq_send = 0x300 + (freq >> 5) / 25;
int retval;
radio->buffer[0] = 0x0a;
radio->buffer[1] = MA901_RADIO_SET_FREQ;
radio->buffer[2] = ((freq_send >> 8) & 0xff) + 0x80;
radio->buffer[3] = freq_send & 0xff;
radio->buffer[4] = 0x00;
radio->buffer[5] = 0x00;
radio->buffer[6] = 0x00;
radio->buffer[7] = 0x00;
retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
9, 0x21, 0x0300, 0,
radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
if (retval < 0)
return retval;
radio->curfreq = freq;
return 0;
}
static int ma901radio_set_volume(struct ma901radio_device *radio, u16 vol_to_set)
{
int retval;
radio->buffer[0] = 0x0a;
radio->buffer[1] = MA901_RADIO_SET_VOLUME;
radio->buffer[2] = 0xc2;
radio->buffer[3] = vol_to_set + 0x20;
radio->buffer[4] = 0x00;
radio->buffer[5] = 0x00;
radio->buffer[6] = 0x00;
radio->buffer[7] = 0x00;
retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
9, 0x21, 0x0300, 0,
radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
if (retval < 0)
return retval;
radio->volume = vol_to_set;
return retval;
}
static int ma901_set_stereo(struct ma901radio_device *radio, u8 stereo)
{
int retval;
radio->buffer[0] = 0x0a;
radio->buffer[1] = MA901_RADIO_SET_MONO_STEREO;
radio->buffer[2] = stereo;
radio->buffer[3] = 0x00;
radio->buffer[4] = 0x00;
radio->buffer[5] = 0x00;
radio->buffer[6] = 0x00;
radio->buffer[7] = 0x00;
retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
9, 0x21, 0x0300, 0,
radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
if (retval < 0)
return retval;
if (stereo == MA901_WANT_STEREO)
radio->stereo = V4L2_TUNER_MODE_STEREO;
else
radio->stereo = V4L2_TUNER_MODE_MONO;
return retval;
}
/* Handle unplugging the device.
* We call video_unregister_device in any case.
* The last function called in this procedure is
* usb_ma901radio_device_release.
*/
static void usb_ma901radio_disconnect(struct usb_interface *intf)
{
struct ma901radio_device *radio = to_ma901radio_dev(usb_get_intfdata(intf));
mutex_lock(&radio->lock);
video_unregister_device(&radio->vdev);
usb_set_intfdata(intf, NULL);
v4l2_device_disconnect(&radio->v4l2_dev);
mutex_unlock(&radio->lock);
v4l2_device_put(&radio->v4l2_dev);
}
/* vidioc_querycap - query device capabilities */
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct ma901radio_device *radio = video_drvdata(file);
strscpy(v->driver, "radio-ma901", sizeof(v->driver));
strscpy(v->card, "Masterkit MA901 USB FM Radio", sizeof(v->card));
usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
v->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
return 0;
}
/* vidioc_g_tuner - get tuner attributes */
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct ma901radio_device *radio = video_drvdata(file);
if (v->index > 0)
return -EINVAL;
v->signal = 0;
/* TODO: the same words like in _probe() goes here.
* When receiving of stats will be implemented then we can call
* ma901radio_get_stat().
* retval = ma901radio_get_stat(radio, &is_stereo, &v->signal);
*/
strscpy(v->name, "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->rangelow = FREQ_MIN * FREQ_MUL;
v->rangehigh = FREQ_MAX * FREQ_MUL;
v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
/* v->rxsubchans = is_stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; */
v->audmode = radio->stereo ?
V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
return 0;
}
/* vidioc_s_tuner - set tuner attributes */
static int vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
struct ma901radio_device *radio = video_drvdata(file);
if (v->index > 0)
return -EINVAL;
/* mono/stereo selector */
switch (v->audmode) {
case V4L2_TUNER_MODE_MONO:
return ma901_set_stereo(radio, MA901_WANT_MONO);
default:
return ma901_set_stereo(radio, MA901_WANT_STEREO);
}
}
/* vidioc_s_frequency - set tuner radio frequency */
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct ma901radio_device *radio = video_drvdata(file);
if (f
|