blob: bb68148b2b6df81abd5364318c10fe6579bc4a96 [file] [log] [blame]
Alexey Klimov2aa72f32008-10-01 09:40:59 -03001/*
2 * A driver for the AverMedia MR 800 USB FM radio. This device plugs
3 * into both the USB and an analog audio input, so this thing
4 * only deals with initialization and frequency setting, the
5 * audio data has to be handled by a sound driver.
6 *
7 * Copyright (c) 2008 Alexey Klimov <klimov.linux@gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24/*
25 * Big thanks to authors of dsbr100.c and radio-si470x.c
26 *
27 * When work was looked pretty good, i discover this:
28 * http://av-usbradio.sourceforge.net/index.php
29 * http://sourceforge.net/projects/av-usbradio/
30 * Latest release of theirs project was in 2005.
31 * Probably, this driver could be improved trough using their
32 * achievements (specifications given).
33 * So, we have smth to begin with.
34 *
35 * History:
36 * Version 0.01: First working version.
37 * It's required to blacklist AverMedia USB Radio
38 * in usbhid/hid-quirks.c
39 *
40 * Many things to do:
41 * - Correct power managment of device (suspend & resume)
42 * - Make x86 independance (little-endian and big-endian stuff)
43 * - Add code for scanning and smooth tuning
44 * - Checked and add stereo&mono stuff
45 * - Add code for sensitivity value
46 * - Correct mistakes
47 * - In Japan another FREQ_MIN and FREQ_MAX
48 */
49
50/* kernel includes */
51#include <linux/kernel.h>
52#include <linux/module.h>
53#include <linux/init.h>
54#include <linux/slab.h>
55#include <linux/input.h>
56#include <linux/videodev2.h>
57#include <media/v4l2-common.h>
58#include <media/v4l2-ioctl.h>
59#include <linux/usb.h>
60#include <linux/version.h> /* for KERNEL_VERSION MACRO */
61
62/* driver and module definitions */
63#define DRIVER_AUTHOR "Alexey Klimov <klimov.linux@gmail.com>"
64#define DRIVER_DESC "AverMedia MR 800 USB FM radio driver"
65#define DRIVER_VERSION "0.01"
66#define RADIO_VERSION KERNEL_VERSION(0, 0, 1)
67
68MODULE_AUTHOR(DRIVER_AUTHOR);
69MODULE_DESCRIPTION(DRIVER_DESC);
70MODULE_LICENSE("GPL");
71
72#define USB_AMRADIO_VENDOR 0x07ca
73#define USB_AMRADIO_PRODUCT 0xb800
74
Alexey Klimove60b0222008-11-04 15:02:36 -030075/* dev_warn macro with driver name */
76#define MR800_DRIVER_NAME "radio-mr800"
77#define amradio_dev_warn(dev, fmt, arg...) \
78 dev_warn(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
79
Alexey Klimov2aa72f32008-10-01 09:40:59 -030080/* Probably USB_TIMEOUT should be modified in module parameter */
81#define BUFFER_LENGTH 8
82#define USB_TIMEOUT 500
83
84/* Frequency limits in MHz -- these are European values. For Japanese
85devices, that would be 76 and 91. */
86#define FREQ_MIN 87.5
87#define FREQ_MAX 108.0
88#define FREQ_MUL 16000
89
90/* module parameter */
91static int radio_nr = -1;
92module_param(radio_nr, int, 0);
93MODULE_PARM_DESC(radio_nr, "Radio Nr");
94
95static struct v4l2_queryctrl radio_qctrl[] = {
96 {
97 .id = V4L2_CID_AUDIO_MUTE,
98 .name = "Mute",
99 .minimum = 0,
100 .maximum = 1,
101 .step = 1,
102 .default_value = 1,
103 .type = V4L2_CTRL_TYPE_BOOLEAN,
104 },
105/* HINT: the disabled controls are only here to satify kradio and such apps */
106 { .id = V4L2_CID_AUDIO_VOLUME,
107 .flags = V4L2_CTRL_FLAG_DISABLED,
108 },
109 {
110 .id = V4L2_CID_AUDIO_BALANCE,
111 .flags = V4L2_CTRL_FLAG_DISABLED,
112 },
113 {
114 .id = V4L2_CID_AUDIO_BASS,
115 .flags = V4L2_CTRL_FLAG_DISABLED,
116 },
117 {
118 .id = V4L2_CID_AUDIO_TREBLE,
119 .flags = V4L2_CTRL_FLAG_DISABLED,
120 },
121 {
122 .id = V4L2_CID_AUDIO_LOUDNESS,
123 .flags = V4L2_CTRL_FLAG_DISABLED,
124 },
125};
126
127static int usb_amradio_probe(struct usb_interface *intf,
128 const struct usb_device_id *id);
129static void usb_amradio_disconnect(struct usb_interface *intf);
130static int usb_amradio_open(struct inode *inode, struct file *file);
131static int usb_amradio_close(struct inode *inode, struct file *file);
132static int usb_amradio_suspend(struct usb_interface *intf,
133 pm_message_t message);
134static int usb_amradio_resume(struct usb_interface *intf);
135
136/* Data for one (physical) device */
137struct amradio_device {
138 /* reference to USB and video device */
139 struct usb_device *usbdev;
140 struct video_device *videodev;
141
142 unsigned char *buffer;
143 struct mutex lock; /* buffer locking */
144 int curfreq;
145 int stereo;
146 int users;
147 int removed;
148 int muted;
149};
150
151/* USB Device ID List */
152static struct usb_device_id usb_amradio_device_table[] = {
153 {USB_DEVICE_AND_INTERFACE_INFO(USB_AMRADIO_VENDOR, USB_AMRADIO_PRODUCT,
154 USB_CLASS_HID, 0, 0) },
155 { } /* Terminating entry */
156};
157
158MODULE_DEVICE_TABLE(usb, usb_amradio_device_table);
159
160/* USB subsystem interface */
161static struct usb_driver usb_amradio_driver = {
Alexey Klimove60b0222008-11-04 15:02:36 -0300162 .name = MR800_DRIVER_NAME,
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300163 .probe = usb_amradio_probe,
164 .disconnect = usb_amradio_disconnect,
165 .suspend = usb_amradio_suspend,
166 .resume = usb_amradio_resume,
167 .reset_resume = usb_amradio_resume,
168 .id_table = usb_amradio_device_table,
169 .supports_autosuspend = 1,
170};
171
172/* switch on radio. Send 8 bytes to device. */
173static int amradio_start(struct amradio_device *radio)
174{
175 int retval;
176 int size;
177
178 mutex_lock(&radio->lock);
179
180 radio->buffer[0] = 0x00;
181 radio->buffer[1] = 0x55;
182 radio->buffer[2] = 0xaa;
183 radio->buffer[3] = 0x00;
184 radio->buffer[4] = 0xab;
185 radio->buffer[5] = 0x00;
186 radio->buffer[6] = 0x00;
187 radio->buffer[7] = 0x00;
188
189 retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
190 (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
191
192 if (retval) {
193 mutex_unlock(&radio->lock);
194 return retval;
195 }
196
197 mutex_unlock(&radio->lock);
198
199 radio->muted = 0;
200
201 return retval;
202}
203
204/* switch off radio */
205static int amradio_stop(struct amradio_device *radio)
206{
207 int retval;
208 int size;
209
Alexey Klimov34801302008-11-19 00:36:29 -0300210 /* safety check */
211 if (radio->removed)
212 return -EIO;
213
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300214 mutex_lock(&radio->lock);
215
216 radio->buffer[0] = 0x00;
217 radio->buffer[1] = 0x55;
218 radio->buffer[2] = 0xaa;
219 radio->buffer[3] = 0x00;
220 radio->buffer[4] = 0xab;
221 radio->buffer[5] = 0x01;
222 radio->buffer[6] = 0x00;
223 radio->buffer[7] = 0x00;
224
225 retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
226 (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
227
228 if (retval) {
229 mutex_unlock(&radio->lock);
230 return retval;
231 }
232
233 mutex_unlock(&radio->lock);
234
235 radio->muted = 1;
236
237 return retval;
238}
239
240/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
241static int amradio_setfreq(struct amradio_device *radio, int freq)
242{
243 int retval;
244 int size;
245 unsigned short freq_send = 0x13 + (freq >> 3) / 25;
246
Alexey Klimov34801302008-11-19 00:36:29 -0300247 /* safety check */
248 if (radio->removed)
249 return -EIO;
250
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300251 mutex_lock(&radio->lock);
252
253 radio->buffer[0] = 0x00;
254 radio->buffer[1] = 0x55;
255 radio->buffer[2] = 0xaa;
256 radio->buffer[3] = 0x03;
257 radio->buffer[4] = 0xa4;
258 radio->buffer[5] = 0x00;
259 radio->buffer[6] = 0x00;
260 radio->buffer[7] = 0x08;
261
262 retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
263 (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
264
265 if (retval) {
266 mutex_unlock(&radio->lock);
267 return retval;
268 }
269
270 /* frequency is calculated from freq_send and placed in first 2 bytes */
271 radio->buffer[0] = (freq_send >> 8) & 0xff;
272 radio->buffer[1] = freq_send & 0xff;
273 radio->buffer[2] = 0x01;
274 radio->buffer[3] = 0x00;
275 radio->buffer[4] = 0x00;
276 /* 5 and 6 bytes of buffer already = 0x00 */
277 radio->buffer[7] = 0x00;
278
279 retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
280 (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
281
282 if (retval) {
283 mutex_unlock(&radio->lock);
284 return retval;
285 }
286
287 mutex_unlock(&radio->lock);
288
289 radio->stereo = 0;
290
291 return retval;
292}
293
294/* USB subsystem interface begins here */
295
296/* handle unplugging of the device, release data structures
297if nothing keeps us from doing it. If something is still
298keeping us busy, the release callback of v4l will take care
299of releasing it. */
300static void usb_amradio_disconnect(struct usb_interface *intf)
301{
302 struct amradio_device *radio = usb_get_intfdata(intf);
303
Alexey Klimovf4e90432008-12-27 21:30:34 -0300304 mutex_lock(&radio->lock);
Alexey Klimov34801302008-11-19 00:36:29 -0300305 radio->removed = 1;
Alexey Klimovf4e90432008-12-27 21:30:34 -0300306 mutex_unlock(&radio->lock);
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300307
Alexey Klimovf4e90432008-12-27 21:30:34 -0300308 usb_set_intfdata(intf, NULL);
309 video_unregister_device(radio->videodev);
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300310}
311
312/* vidioc_querycap - query device capabilities */
313static int vidioc_querycap(struct file *file, void *priv,
314 struct v4l2_capability *v)
315{
316 strlcpy(v->driver, "radio-mr800", sizeof(v->driver));
317 strlcpy(v->card, "AverMedia MR 800 USB FM Radio", sizeof(v->card));
318 sprintf(v->bus_info, "USB");
319 v->version = RADIO_VERSION;
320 v->capabilities = V4L2_CAP_TUNER;
321 return 0;
322}
323
324/* vidioc_g_tuner - get tuner attributes */
325static int vidioc_g_tuner(struct file *file, void *priv,
326 struct v4l2_tuner *v)
327{
328 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
329
Alexey Klimov34801302008-11-19 00:36:29 -0300330 /* safety check */
331 if (radio->removed)
332 return -EIO;
333
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300334 if (v->index > 0)
335 return -EINVAL;
336
337/* TODO: Add function which look is signal stereo or not
338 * amradio_getstat(radio);
339 */
340 radio->stereo = -1;
341 strcpy(v->name, "FM");
342 v->type = V4L2_TUNER_RADIO;
343 v->rangelow = FREQ_MIN * FREQ_MUL;
344 v->rangehigh = FREQ_MAX * FREQ_MUL;
345 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
346 v->capability = V4L2_TUNER_CAP_LOW;
347 if (radio->stereo)
348 v->audmode = V4L2_TUNER_MODE_STEREO;
349 else
350 v->audmode = V4L2_TUNER_MODE_MONO;
351 v->signal = 0xffff; /* Can't get the signal strength, sad.. */
352 v->afc = 0; /* Don't know what is this */
353 return 0;
354}
355
356/* vidioc_s_tuner - set tuner attributes */
357static int vidioc_s_tuner(struct file *file, void *priv,
358 struct v4l2_tuner *v)
359{
Alexey Klimov34801302008-11-19 00:36:29 -0300360 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
361
362 /* safety check */
363 if (radio->removed)
364 return -EIO;
365
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300366 if (v->index > 0)
367 return -EINVAL;
368 return 0;
369}
370
371/* vidioc_s_frequency - set tuner radio frequency */
372static int vidioc_s_frequency(struct file *file, void *priv,
373 struct v4l2_frequency *f)
374{
375 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
376
Alexey Klimov34801302008-11-19 00:36:29 -0300377 /* safety check */
378 if (radio->removed)
379 return -EIO;
380
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300381 radio->curfreq = f->frequency;
382 if (amradio_setfreq(radio, radio->curfreq) < 0)
Alexey Klimove60b0222008-11-04 15:02:36 -0300383 amradio_dev_warn(&radio->videodev->dev,
384 "set frequency failed\n");
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300385 return 0;
386}
387
388/* vidioc_g_frequency - get tuner radio frequency */
389static int vidioc_g_frequency(struct file *file, void *priv,
390 struct v4l2_frequency *f)
391{
392 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
393
Alexey Klimov34801302008-11-19 00:36:29 -0300394 /* safety check */
395 if (radio->removed)
396 return -EIO;
397
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300398 f->type = V4L2_TUNER_RADIO;
399 f->frequency = radio->curfreq;
400 return 0;
401}
402
403/* vidioc_queryctrl - enumerate control items */
404static int vidioc_queryctrl(struct file *file, void *priv,
405 struct v4l2_queryctrl *qc)
406{
407 int i;
408
409 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
410 if (qc->id && qc->id == radio_qctrl[i].id) {
Alexey Klimove60b0222008-11-04 15:02:36 -0300411 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300412 return 0;
413 }
414 }
415 return -EINVAL;
416}
417
418/* vidioc_g_ctrl - get the value of a control */
419static int vidioc_g_ctrl(struct file *file, void *priv,
420 struct v4l2_control *ctrl)
421{
422 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
423
Alexey Klimov34801302008-11-19 00:36:29 -0300424 /* safety check */
425 if (radio->removed)
426 return -EIO;
427
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300428 switch (ctrl->id) {
429 case V4L2_CID_AUDIO_MUTE:
430 ctrl->value = radio->muted;
431 return 0;
432 }
433 return -EINVAL;
434}
435
436/* vidioc_s_ctrl - set the value of a control */
437static int vidioc_s_ctrl(struct file *file, void *priv,
438 struct v4l2_control *ctrl)
439{
440 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
441
Alexey Klimov34801302008-11-19 00:36:29 -0300442 /* safety check */
443 if (radio->removed)
444 return -EIO;
445
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300446 switch (ctrl->id) {
447 case V4L2_CID_AUDIO_MUTE:
448 if (ctrl->value) {
449 if (amradio_stop(radio) < 0) {
Alexey Klimove60b0222008-11-04 15:02:36 -0300450 amradio_dev_warn(&radio->videodev->dev,
451 "amradio_stop failed\n");
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300452 return -1;
453 }
454 } else {
455 if (amradio_start(radio) < 0) {
Alexey Klimove60b0222008-11-04 15:02:36 -0300456 amradio_dev_warn(&radio->videodev->dev,
457 "amradio_start failed\n");
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300458 return -1;
459 }
460 }
461 return 0;
462 }
463 return -EINVAL;
464}
465
466/* vidioc_g_audio - get audio attributes */
467static int vidioc_g_audio(struct file *file, void *priv,
468 struct v4l2_audio *a)
469{
470 if (a->index > 1)
471 return -EINVAL;
472
473 strcpy(a->name, "Radio");
474 a->capability = V4L2_AUDCAP_STEREO;
475 return 0;
476}
477
478/* vidioc_s_audio - set audio attributes */
479static int vidioc_s_audio(struct file *file, void *priv,
480 struct v4l2_audio *a)
481{
482 if (a->index != 0)
483 return -EINVAL;
484 return 0;
485}
486
487/* vidioc_g_input - get input */
488static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
489{
490 *i = 0;
491 return 0;
492}
493
494/* vidioc_s_input - set input */
495static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
496{
497 if (i != 0)
498 return -EINVAL;
499 return 0;
500}
501
502/* open device - amradio_start() and amradio_setfreq() */
503static int usb_amradio_open(struct inode *inode, struct file *file)
504{
505 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
506
Alexey Klimov0fabb782008-10-19 23:56:23 -0300507 lock_kernel();
508
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300509 radio->users = 1;
510 radio->muted = 1;
511
512 if (amradio_start(radio) < 0) {
Alexey Klimove60b0222008-11-04 15:02:36 -0300513 amradio_dev_warn(&radio->videodev->dev,
514 "radio did not start up properly\n");
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300515 radio->users = 0;
Alexey Klimov0fabb782008-10-19 23:56:23 -0300516 unlock_kernel();
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300517 return -EIO;
518 }
519 if (amradio_setfreq(radio, radio->curfreq) < 0)
Alexey Klimove60b0222008-11-04 15:02:36 -0300520 amradio_dev_warn(&radio->videodev->dev,
521 "set frequency failed\n");
Alexey Klimov0fabb782008-10-19 23:56:23 -0300522
523 unlock_kernel();
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300524 return 0;
525}
526
Alexey Klimovf4e90432008-12-27 21:30:34 -0300527/*close device */
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300528static int usb_amradio_close(struct inode *inode, struct file *file)
529{
530 struct amradio_device *radio = video_get_drvdata(video_devdata(file));
Alexey Klimov34801302008-11-19 00:36:29 -0300531 int retval;
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300532
533 if (!radio)
534 return -ENODEV;
Alexey Klimov34801302008-11-19 00:36:29 -0300535
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300536 radio->users = 0;
Alexey Klimov34801302008-11-19 00:36:29 -0300537
Alexey Klimovf4e90432008-12-27 21:30:34 -0300538 if (!radio->removed) {
Alexey Klimov34801302008-11-19 00:36:29 -0300539 retval = amradio_stop(radio);
540 if (retval < 0)
541 amradio_dev_warn(&radio->videodev->dev,
542 "amradio_stop failed\n");
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300543 }
Alexey Klimov34801302008-11-19 00:36:29 -0300544
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300545 return 0;
546}
547
548/* Suspend device - stop device. Need to be checked and fixed */
549static int usb_amradio_suspend(struct usb_interface *intf, pm_message_t message)
550{
551 struct amradio_device *radio = usb_get_intfdata(intf);
552
553 if (amradio_stop(radio) < 0)
Alexey Klimove60b0222008-11-04 15:02:36 -0300554 dev_warn(&intf->dev, "amradio_stop failed\n");
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300555
Alexey Klimove60b0222008-11-04 15:02:36 -0300556 dev_info(&intf->dev, "going into suspend..\n");
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300557
558 return 0;
559}
560
561/* Resume device - start device. Need to be checked and fixed */
562static int usb_amradio_resume(struct usb_interface *intf)
563{
564 struct amradio_device *radio = usb_get_intfdata(intf);
565
566 if (amradio_start(radio) < 0)
Alexey Klimove60b0222008-11-04 15:02:36 -0300567 dev_warn(&intf->dev, "amradio_start failed\n");
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300568
Alexey Klimove60b0222008-11-04 15:02:36 -0300569 dev_info(&intf->dev, "coming out of suspend..\n");
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300570
571 return 0;
572}
573
574/* File system interface */
575static const struct file_operations usb_amradio_fops = {
576 .owner = THIS_MODULE,
577 .open = usb_amradio_open,
578 .release = usb_amradio_close,
579 .ioctl = video_ioctl2,
580#ifdef CONFIG_COMPAT
581 .compat_ioctl = v4l_compat_ioctl32,
582#endif
583 .llseek = no_llseek,
584};
585
586static const struct v4l2_ioctl_ops usb_amradio_ioctl_ops = {
587 .vidioc_querycap = vidioc_querycap,
588 .vidioc_g_tuner = vidioc_g_tuner,
589 .vidioc_s_tuner = vidioc_s_tuner,
590 .vidioc_g_frequency = vidioc_g_frequency,
591 .vidioc_s_frequency = vidioc_s_frequency,
592 .vidioc_queryctrl = vidioc_queryctrl,
593 .vidioc_g_ctrl = vidioc_g_ctrl,
594 .vidioc_s_ctrl = vidioc_s_ctrl,
595 .vidioc_g_audio = vidioc_g_audio,
596 .vidioc_s_audio = vidioc_s_audio,
597 .vidioc_g_input = vidioc_g_input,
598 .vidioc_s_input = vidioc_s_input,
599};
600
Alexey Klimovf4e90432008-12-27 21:30:34 -0300601static void usb_amradio_device_release(struct video_device *videodev)
602{
603 struct amradio_device *radio = video_get_drvdata(videodev);
604
605 /* we call v4l to free radio->videodev */
606 video_device_release(videodev);
607
608 /* free rest memory */
609 kfree(radio->buffer);
610 kfree(radio);
611}
612
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300613/* V4L2 interface */
614static struct video_device amradio_videodev_template = {
615 .name = "AverMedia MR 800 USB FM Radio",
616 .fops = &usb_amradio_fops,
617 .ioctl_ops = &usb_amradio_ioctl_ops,
Alexey Klimovf4e90432008-12-27 21:30:34 -0300618 .release = usb_amradio_device_release,
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300619};
620
621/* check if the device is present and register with v4l and
622usb if it is */
623static int usb_amradio_probe(struct usb_interface *intf,
624 const struct usb_device_id *id)
625{
626 struct amradio_device *radio;
627
628 radio = kmalloc(sizeof(struct amradio_device), GFP_KERNEL);
629
630 if (!(radio))
631 return -ENOMEM;
632
633 radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
634
635 if (!(radio->buffer)) {
636 kfree(radio);
637 return -ENOMEM;
638 }
639
640 radio->videodev = video_device_alloc();
641
642 if (!(radio->videodev)) {
643 kfree(radio->buffer);
644 kfree(radio);
645 return -ENOMEM;
646 }
647
648 memcpy(radio->videodev, &amradio_videodev_template,
649 sizeof(amradio_videodev_template));
650
651 radio->removed = 0;
652 radio->users = 0;
653 radio->usbdev = interface_to_usbdev(intf);
654 radio->curfreq = 95.16 * FREQ_MUL;
655
656 mutex_init(&radio->lock);
657
658 video_set_drvdata(radio->videodev, radio);
659 if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) {
Alexey Klimove60b0222008-11-04 15:02:36 -0300660 dev_warn(&intf->dev, "could not register video device\n");
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300661 video_device_release(radio->videodev);
662 kfree(radio->buffer);
663 kfree(radio);
664 return -EIO;
665 }
666
667 usb_set_intfdata(intf, radio);
668 return 0;
669}
670
671static int __init amradio_init(void)
672{
673 int retval = usb_register(&usb_amradio_driver);
674
Alexey Klimove60b0222008-11-04 15:02:36 -0300675 pr_info(KBUILD_MODNAME
676 ": version " DRIVER_VERSION " " DRIVER_DESC "\n");
677
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300678 if (retval)
Alexey Klimove60b0222008-11-04 15:02:36 -0300679 pr_err(KBUILD_MODNAME
680 ": usb_register failed. Error number %d\n", retval);
681
Alexey Klimov2aa72f32008-10-01 09:40:59 -0300682 return retval;
683}
684
685static void __exit amradio_exit(void)
686{
687 usb_deregister(&usb_amradio_driver);
688}
689
690module_init(amradio_init);
691module_exit(amradio_exit);
692