xref: /linux/drivers/media/radio/radio-ma901.c (revision cc1e6315)
1 /*
2  * Driver for the MasterKit MA901 USB FM radio. This device plugs
3  * into the USB port and an analog audio input or headphones, so this thing
4  * only deals with initialization, frequency setting, volume.
5  *
6  * Copyright (c) 2012 Alexey Klimov <klimov.linux@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/input.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-event.h>
29 #include <linux/usb.h>
30 #include <linux/mutex.h>
31 
32 #define DRIVER_AUTHOR "Alexey Klimov <klimov.linux@gmail.com>"
33 #define DRIVER_DESC "Masterkit MA901 USB FM radio driver"
34 #define DRIVER_VERSION "0.0.1"
35 
36 MODULE_AUTHOR(DRIVER_AUTHOR);
37 MODULE_DESCRIPTION(DRIVER_DESC);
38 MODULE_LICENSE("GPL");
39 MODULE_VERSION(DRIVER_VERSION);
40 
41 #define USB_MA901_VENDOR  0x16c0
42 #define USB_MA901_PRODUCT 0x05df
43 
44 /* dev_warn macro with driver name */
45 #define MA901_DRIVER_NAME "radio-ma901"
46 #define ma901radio_dev_warn(dev, fmt, arg...)				\
47 		dev_warn(dev, MA901_DRIVER_NAME " - " fmt, ##arg)
48 
49 #define ma901radio_dev_err(dev, fmt, arg...) \
50 		dev_err(dev, MA901_DRIVER_NAME " - " fmt, ##arg)
51 
52 /* Probably USB_TIMEOUT should be modified in module parameter */
53 #define BUFFER_LENGTH 8
54 #define USB_TIMEOUT 500
55 
56 #define FREQ_MIN  87.5
57 #define FREQ_MAX 108.0
58 #define FREQ_MUL 16000
59 
60 #define MA901_VOLUME_MAX 16
61 #define MA901_VOLUME_MIN 0
62 
63 /* Commands that device should understand
64  * List isn't full and will be updated with implementation of new functions
65  */
66 #define MA901_RADIO_SET_FREQ		0x03
67 #define MA901_RADIO_SET_VOLUME		0x04
68 #define MA901_RADIO_SET_MONO_STEREO	0x05
69 
70 /* Comfortable defines for ma901radio_set_stereo */
71 #define MA901_WANT_STEREO		0x50
72 #define MA901_WANT_MONO			0xd0
73 
74 /* module parameter */
75 static int radio_nr = -1;
76 module_param(radio_nr, int, 0);
77 MODULE_PARM_DESC(radio_nr, "Radio file number");
78 
79 /* Data for one (physical) device */
80 struct ma901radio_device {
81 	/* reference to USB and video device */
82 	struct usb_device *usbdev;
83 	struct usb_interface *intf;
84 	struct video_device vdev;
85 	struct v4l2_device v4l2_dev;
86 	struct v4l2_ctrl_handler hdl;
87 
88 	u8 *buffer;
89 	struct mutex lock;	/* buffer locking */
90 	int curfreq;
91 	u16 volume;
92 	int stereo;
93 	bool muted;
94 };
95 
96 static inline struct ma901radio_device *to_ma901radio_dev(struct v4l2_device *v4l2_dev)
97 {
98 	return container_of(v4l2_dev, struct ma901radio_device, v4l2_dev);
99 }
100 
101 /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
102 static int ma901radio_set_freq(struct ma901radio_device *radio, int freq)
103 {
104 	unsigned int freq_send = 0x300 + (freq >> 5) / 25;
105 	int retval;
106 
107 	radio->buffer[0] = 0x0a;
108 	radio->buffer[1] = MA901_RADIO_SET_FREQ;
109 	radio->buffer[2] = ((freq_send >> 8) & 0xff) + 0x80;
110 	radio->buffer[3] = freq_send & 0xff;
111 	radio->buffer[4] = 0x00;
112 	radio->buffer[5] = 0x00;
113 	radio->buffer[6] = 0x00;
114 	radio->buffer[7] = 0x00;
115 
116 	retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
117 				9, 0x21, 0x0300, 0,
118 				radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
119 	if (retval < 0)
120 		return retval;
121 
122 	radio->curfreq = freq;
123 	return 0;
124 }
125 
126 static int ma901radio_set_volume(struct ma901radio_device *radio, u16 vol_to_set)
127 {
128 	int retval;
129 
130 	radio->buffer[0] = 0x0a;
131 	radio->buffer[1] = MA901_RADIO_SET_VOLUME;
132 	radio->buffer[2] = 0xc2;
133 	radio->buffer[3] = vol_to_set + 0x20;
134 	radio->buffer[4] = 0x00;
135 	radio->buffer[5] = 0x00;
136 	radio->buffer[6] = 0x00;
137 	radio->buffer[7] = 0x00;
138 
139 	retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
140 				9, 0x21, 0x0300, 0,
141 				radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
142 	if (retval < 0)
143 		return retval;
144 
145 	radio->volume = vol_to_set;
146 	return retval;
147 }
148 
149 static int ma901_set_stereo(struct ma901radio_device *radio, u8 stereo)
150 {
151 	int retval;
152 
153 	radio->buffer[0] = 0x0a;
154 	radio->buffer[1] = MA901_RADIO_SET_MONO_STEREO;
155 	radio->buffer[2] = stereo;
156 	radio->buffer[3] = 0x00;
157 	radio->buffer[4] = 0x00;
158 	radio->buffer[5] = 0x00;
159 	radio->buffer[6] = 0x00;
160 	radio->buffer[7] = 0x00;
161 
162 	retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
163 				9, 0x21, 0x0300, 0,
164 				radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
165 
166 	if (retval < 0)
167 		return retval;
168 
169 	if (stereo == MA901_WANT_STEREO)
170 		radio->stereo = V4L2_TUNER_MODE_STEREO;
171 	else
172 		radio->stereo = V4L2_TUNER_MODE_MONO;
173 
174 	return retval;
175 }
176 
177 /* Handle unplugging the device.
178  * We call video_unregister_device in any case.
179  * The last function called in this procedure is
180  * usb_ma901radio_device_release.
181  */
182 static void usb_ma901radio_disconnect(struct usb_interface *intf)
183 {
184 	struct ma901radio_device *radio = to_ma901radio_dev(usb_get_intfdata(intf));
185 
186 	mutex_lock(&radio->lock);
187 	video_unregister_device(&radio->vdev);
188 	usb_set_intfdata(intf, NULL);
189 	v4l2_device_disconnect(&radio->v4l2_dev);
190 	mutex_unlock(&radio->lock);
191 	v4l2_device_put(&radio->v4l2_dev);
192 }
193 
194 /* vidioc_querycap - query device capabilities */
195 static int vidioc_querycap(struct file *file, void *priv,
196 					struct v4l2_capability *v)
197 {
198 	struct ma901radio_device *radio = video_drvdata(file);
199 
200 	strscpy(v->driver, "radio-ma901", sizeof(v->driver));
201 	strscpy(v->card, "Masterkit MA901 USB FM Radio", sizeof(v->card));
202 	usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
203 	v->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
204 	v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
205 	return 0;
206 }
207 
208 /* vidioc_g_tuner - get tuner attributes */
209 static int vidioc_g_tuner(struct file *file, void *priv,
210 				struct v4l2_tuner *v)
211 {
212 	struct ma901radio_device *radio = video_drvdata(file);
213 
214 	if (v->index > 0)
215 		return -EINVAL;
216 
217 	v->signal = 0;
218 
219 	/* TODO: the same words like in _probe() goes here.
220 	 * When receiving of stats will be implemented then we can call
221 	 * ma901radio_get_stat().
222 	 * retval = ma901radio_get_stat(radio, &is_stereo, &v->signal);
223 	 */
224 
225 	strscpy(v->name, "FM", sizeof(v->name));
226 	v->type = V4L2_TUNER_RADIO;
227 	v->rangelow = FREQ_MIN * FREQ_MUL;
228 	v->rangehigh = FREQ_MAX * FREQ_MUL;
229 	v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
230 	/* v->rxsubchans = is_stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; */
231 	v->audmode = radio->stereo ?
232 		V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
233 	return 0;
234 }
235 
236 /* vidioc_s_tuner - set tuner attributes */
237 static int vidioc_s_tuner(struct file *file, void *priv,
238 				const struct v4l2_tuner *v)
239 {
240 	struct ma901radio_device *radio = video_drvdata(file);
241 
242 	if (v->index > 0)
243 		return -EINVAL;
244 
245 	/* mono/stereo selector */
246 	switch (v->audmode) {
247 	case V4L2_TUNER_MODE_MONO:
248 		return ma901_set_stereo(radio, MA901_WANT_MONO);
249 	default:
250 		return ma901_set_stereo(radio, MA901_WANT_STEREO);
251 	}
252 }
253 
254 /* vidioc_s_frequency - set tuner radio frequency */
255 static int vidioc_s_frequency(struct file *file, void *priv,
256 				const struct v4l2_frequency *f)
257 {
258 	struct ma901radio_device *radio = video_drvdata(file);
259 
260 	if (f->tuner != 0)
261 		return -EINVAL;
262 
263 	return ma901radio_set_freq(radio, clamp_t(unsigned, f->frequency,
264 				FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL));
265 }
266 
267 /* vidioc_g_frequency - get tuner radio frequency */
268 static int vidioc_g_frequency(struct file *file, void *priv,
269 				struct v4l2_frequency *f)
270 {
271 	struct ma901radio_device *radio = video_drvdata(file);
272 
273 	if (f->tuner != 0)
274 		return -EINVAL;
275 	f->frequency = radio->curfreq;
276 
277 	return 0;
278 }
279 
280 static int usb_ma901radio_s_ctrl(struct v4l2_ctrl *ctrl)
281 {
282 	struct ma901radio_device *radio =
283 		container_of(ctrl->handler, struct ma901radio_device, hdl);
284 
285 	switch (ctrl->id) {
286 	case V4L2_CID_AUDIO_VOLUME:     /* set volume */
287 		return ma901radio_set_volume(radio, (u16)ctrl->val);
288 	}
289 
290 	return -EINVAL;
291 }
292 
293 /* TODO: Should we really need to implement suspend and resume functions?
294  * Radio has it's own memory and will continue playing if power is present
295  * on usb port and on resume it will start to play again based on freq, volume
296  * values in device memory.
297  */
298 static int usb_ma901radio_suspend(struct usb_interface *intf, pm_message_t message)
299 {
300 	return 0;
301 }
302 
303 static int usb_ma901radio_resume(struct usb_interface *intf)
304 {
305 	return 0;
306 }
307 
308 static const struct v4l2_ctrl_ops usb_ma901radio_ctrl_ops = {
309 	.s_ctrl = usb_ma901radio_s_ctrl,
310 };
311 
312 /* File system interface */
313 static const struct v4l2_file_operations usb_ma901radio_fops = {
314 	.owner		= THIS_MODULE,
315 	.open		= v4l2_fh_open,
316 	.release	= v4l2_fh_release,
317 	.poll		= v4l2_ctrl_poll,
318 	.unlocked_ioctl	= video_ioctl2,
319 };
320 
321 static const struct v4l2_ioctl_ops usb_ma901radio_ioctl_ops = {
322 	.vidioc_querycap    = vidioc_querycap,
323 	.vidioc_g_tuner     = vidioc_g_tuner,
324 	.vidioc_s_tuner     = vidioc_s_tuner,
325 	.vidioc_g_frequency = vidioc_g_frequency,
326 	.vidioc_s_frequency = vidioc_s_frequency,
327 	.vidioc_log_status  = v4l2_ctrl_log_status,
328 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
329 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
330 };
331 
332 static void usb_ma901radio_release(struct v4l2_device *v4l2_dev)
333 {
334 	struct ma901radio_device *radio = to_ma901radio_dev(v4l2_dev);
335 
336 	v4l2_ctrl_handler_free(&radio->hdl);
337 	v4l2_device_unregister(&radio->v4l2_dev);
338 	kfree(radio->buffer);
339 	kfree(radio);
340 }
341 
342 /* check if the device is present and register with v4l and usb if it is */
343 static int usb_ma901radio_probe(struct usb_interface *intf,
344 				const struct usb_device_id *id)
345 {
346 	struct usb_device *dev = interface_to_usbdev(intf);
347 	struct ma901radio_device *radio;
348 	int retval = 0;
349 
350 	/* Masterkit MA901 usb radio has the same USB ID as many others
351 	 * Atmel V-USB devices. Let's make additional checks to be sure
352 	 * that this is our device.
353 	 */
354 
355 	if (dev->product && dev->manufacturer &&
356 		(strncmp(dev->product, "MA901", 5) != 0
357 		|| strncmp(dev->manufacturer, "www.masterkit.ru", 16) != 0))
358 		return -ENODEV;
359 
360 	radio = kzalloc(sizeof(struct ma901radio_device), GFP_KERNEL);
361 	if (!radio) {
362 		dev_err(&intf->dev, "kzalloc for ma901radio_device failed\n");
363 		retval = -ENOMEM;
364 		goto err;
365 	}
366 
367 	radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
368 	if (!radio->buffer) {
369 		dev_err(&intf->dev, "kmalloc for radio->buffer failed\n");
370 		retval = -ENOMEM;
371 		goto err_nobuf;
372 	}
373 
374 	retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
375 	if (retval < 0) {
376 		dev_err(&intf->dev, "couldn't register v4l2_device\n");
377 		goto err_v4l2;
378 	}
379 
380 	v4l2_ctrl_handler_init(&radio->hdl, 1);
381 
382 	/* TODO:It looks like this radio doesn't have mute/unmute control
383 	 * and windows program just emulate it using volume control.
384 	 * Let's plan to do the same in this driver.
385 	 *
386 	 * v4l2_ctrl_new_std(&radio->hdl, &usb_ma901radio_ctrl_ops,
387 	 *		  V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
388 	 */
389 
390 	v4l2_ctrl_new_std(&radio->hdl, &usb_ma901radio_ctrl_ops,
391 			  V4L2_CID_AUDIO_VOLUME, MA901_VOLUME_MIN,
392 			  MA901_VOLUME_MAX, 1, MA901_VOLUME_MAX);
393 
394 	if (radio->hdl.error) {
395 		retval = radio->hdl.error;
396 		dev_err(&intf->dev, "couldn't register control\n");
397 		goto err_ctrl;
398 	}
399 	mutex_init(&radio->lock);
400 
401 	radio->v4l2_dev.ctrl_handler = &radio->hdl;
402 	radio->v4l2_dev.release = usb_ma901radio_release;
403 	strscpy(radio->vdev.name, radio->v4l2_dev.name,
404 		sizeof(radio->vdev.name));
405 	radio->vdev.v4l2_dev = &radio->v4l2_dev;
406 	radio->vdev.fops = &usb_ma901radio_fops;
407 	radio->vdev.ioctl_ops = &usb_ma901radio_ioctl_ops;
408 	radio->vdev.release = video_device_release_empty;
409 	radio->vdev.lock = &radio->lock;
410 
411 	radio->usbdev = interface_to_usbdev(intf);
412 	radio->intf = intf;
413 	usb_set_intfdata(intf, &radio->v4l2_dev);
414 	radio->curfreq = 95.21 * FREQ_MUL;
415 
416 	video_set_drvdata(&radio->vdev, radio);
417 
418 	/* TODO: we can get some statistics (freq, volume) from device
419 	 * but it's not implemented yet. After insertion in usb-port radio
420 	 * setups frequency and starts playing without any initialization.
421 	 * So we don't call usb_ma901radio_init/get_stat() here.
422 	 * retval = usb_ma901radio_init(radio);
423 	 */
424 
425 	retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO,
426 					radio_nr);
427 	if (retval < 0) {
428 		dev_err(&intf->dev, "could not register video device\n");
429 		goto err_vdev;
430 	}
431 
432 	return 0;
433 
434 err_vdev:
435 	v4l2_ctrl_handler_free(&radio->hdl);
436 err_ctrl:
437 	v4l2_device_unregister(&radio->v4l2_dev);
438 err_v4l2:
439 	kfree(radio->buffer);
440 err_nobuf:
441 	kfree(radio);
442 err:
443 	return retval;
444 }
445 
446 /* USB Device ID List */
447 static const struct usb_device_id usb_ma901radio_device_table[] = {
448 	{ USB_DEVICE_AND_INTERFACE_INFO(USB_MA901_VENDOR, USB_MA901_PRODUCT,
449 							USB_CLASS_HID, 0, 0) },
450 	{ }						/* Terminating entry */
451 };
452 
453 MODULE_DEVICE_TABLE(usb, usb_ma901radio_device_table);
454 
455 /* USB subsystem interface */
456 static struct usb_driver usb_ma901radio_driver = {
457 	.name			= MA901_DRIVER_NAME,
458 	.probe			= usb_ma901radio_probe,
459 	.disconnect		= usb_ma901radio_disconnect,
460 	.suspend		= usb_ma901radio_suspend,
461 	.resume			= usb_ma901radio_resume,
462 	.reset_resume		= usb_ma901radio_resume,
463 	.id_table		= usb_ma901radio_device_table,
464 };
465 
466 module_usb_driver(usb_ma901radio_driver);
467