[media] ivtv: set max/step to 0 for PTS and FRAME controls
[deliverable/linux.git] / drivers / media / radio / radio-keene.c
CommitLineData
1bf20c3a
HV
1/*
2 * Copyright (c) 2012 Hans Verkuil <hverkuil@xs4all.nl>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19/* kernel includes */
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/input.h>
25#include <linux/videodev2.h>
26#include <media/v4l2-device.h>
27#include <media/v4l2-ioctl.h>
28#include <media/v4l2-ctrls.h>
29#include <media/v4l2-event.h>
30#include <linux/usb.h>
1bf20c3a
HV
31#include <linux/mutex.h>
32
33/* driver and module definitions */
34MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
35MODULE_DESCRIPTION("Keene FM Transmitter driver");
36MODULE_LICENSE("GPL");
37
38/* Actually, it advertises itself as a Logitech */
39#define USB_KEENE_VENDOR 0x046d
40#define USB_KEENE_PRODUCT 0x0a0e
41
42/* Probably USB_TIMEOUT should be modified in module parameter */
43#define BUFFER_LENGTH 8
44#define USB_TIMEOUT 500
45
46/* Frequency limits in MHz */
47#define FREQ_MIN 76U
48#define FREQ_MAX 108U
49#define FREQ_MUL 16000U
50
51/* USB Device ID List */
52static struct usb_device_id usb_keene_device_table[] = {
53 {USB_DEVICE_AND_INTERFACE_INFO(USB_KEENE_VENDOR, USB_KEENE_PRODUCT,
54 USB_CLASS_HID, 0, 0) },
55 { } /* Terminating entry */
56};
57
58MODULE_DEVICE_TABLE(usb, usb_keene_device_table);
59
60struct keene_device {
61 struct usb_device *usbdev;
62 struct usb_interface *intf;
63 struct video_device vdev;
64 struct v4l2_device v4l2_dev;
65 struct v4l2_ctrl_handler hdl;
66 struct mutex lock;
67
68 u8 *buffer;
69 unsigned curfreq;
70 u8 tx;
71 u8 pa;
72 bool stereo;
73 bool muted;
74 bool preemph_75_us;
75};
76
77static inline struct keene_device *to_keene_dev(struct v4l2_device *v4l2_dev)
78{
79 return container_of(v4l2_dev, struct keene_device, v4l2_dev);
80}
81
82/* Set frequency (if non-0), PA, mute and turn on/off the FM transmitter. */
83static int keene_cmd_main(struct keene_device *radio, unsigned freq, bool play)
84{
85 unsigned short freq_send = freq ? (freq - 76 * 16000) / 800 : 0;
86 int ret;
87
88 radio->buffer[0] = 0x00;
89 radio->buffer[1] = 0x50;
90 radio->buffer[2] = (freq_send >> 8) & 0xff;
91 radio->buffer[3] = freq_send & 0xff;
92 radio->buffer[4] = radio->pa;
93 /* If bit 4 is set, then tune to the frequency.
94 If bit 3 is set, then unmute; if bit 2 is set, then mute.
95 If bit 1 is set, then enter idle mode; if bit 0 is set,
96 then enter transit mode.
97 */
98 radio->buffer[5] = (radio->muted ? 4 : 8) | (play ? 1 : 2) |
99 (freq ? 0x10 : 0);
100 radio->buffer[6] = 0x00;
101 radio->buffer[7] = 0x00;
102
103 ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
104 9, 0x21, 0x200, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
105
106 if (ret < 0) {
107 dev_warn(&radio->vdev.dev, "%s failed (%d)\n", __func__, ret);
108 return ret;
109 }
110 if (freq)
111 radio->curfreq = freq;
112 return 0;
113}
114
115/* Set TX, stereo and preemphasis mode (50 us vs 75 us). */
116static int keene_cmd_set(struct keene_device *radio)
117{
118 int ret;
119
120 radio->buffer[0] = 0x00;
121 radio->buffer[1] = 0x51;
122 radio->buffer[2] = radio->tx;
123 /* If bit 0 is set, then transmit mono, otherwise stereo.
124 If bit 2 is set, then enable 75 us preemphasis, otherwise
125 it is 50 us. */
126 radio->buffer[3] = (!radio->stereo) | (radio->preemph_75_us ? 4 : 0);
127 radio->buffer[4] = 0x00;
128 radio->buffer[5] = 0x00;
129 radio->buffer[6] = 0x00;
130 radio->buffer[7] = 0x00;
131
132 ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
133 9, 0x21, 0x200, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
134
135 if (ret < 0) {
136 dev_warn(&radio->vdev.dev, "%s failed (%d)\n", __func__, ret);
137 return ret;
138 }
139 return 0;
140}
141
142/* Handle unplugging the device.
143 * We call video_unregister_device in any case.
144 * The last function called in this procedure is
145 * usb_keene_device_release.
146 */
147static void usb_keene_disconnect(struct usb_interface *intf)
148{
149 struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf));
150
1bf20c3a
HV
151 mutex_lock(&radio->lock);
152 usb_set_intfdata(intf, NULL);
153 video_unregister_device(&radio->vdev);
154 v4l2_device_disconnect(&radio->v4l2_dev);
155 mutex_unlock(&radio->lock);
156 v4l2_device_put(&radio->v4l2_dev);
157}
158
159static int vidioc_querycap(struct file *file, void *priv,
160 struct v4l2_capability *v)
161{
162 struct keene_device *radio = video_drvdata(file);
163
164 strlcpy(v->driver, "radio-keene", sizeof(v->driver));
165 strlcpy(v->card, "Keene FM Transmitter", sizeof(v->card));
166 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
167 v->device_caps = V4L2_CAP_RADIO | V4L2_CAP_MODULATOR;
168 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
169 return 0;
170}
171
172static int vidioc_g_modulator(struct file *file, void *priv,
173 struct v4l2_modulator *v)
174{
175 struct keene_device *radio = video_drvdata(file);
176
177 if (v->index > 0)
178 return -EINVAL;
179
180 strlcpy(v->name, "FM", sizeof(v->name));
181 v->rangelow = FREQ_MIN * FREQ_MUL;
182 v->rangehigh = FREQ_MAX * FREQ_MUL;
183 v->txsubchans = radio->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
184 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
185 return 0;
186}
187
188static int vidioc_s_modulator(struct file *file, void *priv,
189 struct v4l2_modulator *v)
190{
191 struct keene_device *radio = video_drvdata(file);
192
193 if (v->index > 0)
194 return -EINVAL;
195
196 radio->stereo = (v->txsubchans == V4L2_TUNER_SUB_STEREO);
197 return keene_cmd_set(radio);
198}
199
200static int vidioc_s_frequency(struct file *file, void *priv,
201 struct v4l2_frequency *f)
202{
203 struct keene_device *radio = video_drvdata(file);
204
205 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
206 return -EINVAL;
207 f->frequency = clamp(f->frequency,
208 FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL);
209 return keene_cmd_main(radio, f->frequency, true);
210}
211
212static int vidioc_g_frequency(struct file *file, void *priv,
213 struct v4l2_frequency *f)
214{
215 struct keene_device *radio = video_drvdata(file);
216
217 if (f->tuner != 0)
218 return -EINVAL;
219 f->type = V4L2_TUNER_RADIO;
220 f->frequency = radio->curfreq;
221 return 0;
222}
223
224static int keene_s_ctrl(struct v4l2_ctrl *ctrl)
225{
226 static const u8 db2tx[] = {
227 /* -15, -12, -9, -6, -3, 0 dB */
228 0x03, 0x13, 0x02, 0x12, 0x22, 0x32,
229 /* 3, 6, 9, 12, 15, 18 dB */
230 0x21, 0x31, 0x20, 0x30, 0x40, 0x50
231 };
232 struct keene_device *radio =
233 container_of(ctrl->handler, struct keene_device, hdl);
234
235 switch (ctrl->id) {
236 case V4L2_CID_AUDIO_MUTE:
237 radio->muted = ctrl->val;
238 return keene_cmd_main(radio, 0, true);
239
240 case V4L2_CID_TUNE_POWER_LEVEL:
241 /* To go from dBuV to the register value we apply the
242 following formula: */
243 radio->pa = (ctrl->val - 71) * 100 / 62;
244 return keene_cmd_main(radio, 0, true);
245
246 case V4L2_CID_TUNE_PREEMPHASIS:
247 radio->preemph_75_us = ctrl->val == V4L2_PREEMPHASIS_75_uS;
248 return keene_cmd_set(radio);
249
250 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
251 radio->tx = db2tx[(ctrl->val - ctrl->minimum) / ctrl->step];
252 return keene_cmd_set(radio);
253 }
254 return -EINVAL;
255}
256
257static int vidioc_subscribe_event(struct v4l2_fh *fh,
258 struct v4l2_event_subscription *sub)
259{
260 switch (sub->type) {
261 case V4L2_EVENT_CTRL:
262 return v4l2_event_subscribe(fh, sub, 0);
263 default:
264 return -EINVAL;
265 }
266}
267
268
269/* File system interface */
270static const struct v4l2_file_operations usb_keene_fops = {
271 .owner = THIS_MODULE,
272 .open = v4l2_fh_open,
273 .release = v4l2_fh_release,
274 .poll = v4l2_ctrl_poll,
275 .unlocked_ioctl = video_ioctl2,
276};
277
278static const struct v4l2_ctrl_ops keene_ctrl_ops = {
279 .s_ctrl = keene_s_ctrl,
280};
281
282static const struct v4l2_ioctl_ops usb_keene_ioctl_ops = {
283 .vidioc_querycap = vidioc_querycap,
284 .vidioc_g_modulator = vidioc_g_modulator,
285 .vidioc_s_modulator = vidioc_s_modulator,
286 .vidioc_g_frequency = vidioc_g_frequency,
287 .vidioc_s_frequency = vidioc_s_frequency,
288 .vidioc_log_status = v4l2_ctrl_log_status,
289 .vidioc_subscribe_event = vidioc_subscribe_event,
290 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
291};
292
293static void usb_keene_video_device_release(struct v4l2_device *v4l2_dev)
294{
295 struct keene_device *radio = to_keene_dev(v4l2_dev);
296
297 /* free rest memory */
298 v4l2_ctrl_handler_free(&radio->hdl);
299 kfree(radio->buffer);
300 kfree(radio);
301}
302
303/* check if the device is present and register with v4l and usb if it is */
304static int usb_keene_probe(struct usb_interface *intf,
305 const struct usb_device_id *id)
306{
307 struct usb_device *dev = interface_to_usbdev(intf);
308 struct keene_device *radio;
309 struct v4l2_ctrl_handler *hdl;
310 int retval = 0;
311
312 /*
313 * The Keene FM transmitter USB device has the same USB ID as
314 * the Logitech AudioHub Speaker, but it should ignore the hid.
315 * Check if the name is that of the Keene device.
316 * If not, then someone connected the AudioHub and we shouldn't
317 * attempt to handle this driver.
318 * For reference: the product name of the AudioHub is
319 * "AudioHub Speaker".
320 */
321 if (dev->product && strcmp(dev->product, "B-LINK USB Audio "))
322 return -ENODEV;
323
324 radio = kzalloc(sizeof(struct keene_device), GFP_KERNEL);
325 if (radio)
326 radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
327
328 if (!radio || !radio->buffer) {
329 dev_err(&intf->dev, "kmalloc for keene_device failed\n");
330 kfree(radio);
331 retval = -ENOMEM;
332 goto err;
333 }
334
335 hdl = &radio->hdl;
336 v4l2_ctrl_handler_init(hdl, 4);
337 v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_MUTE,
338 0, 1, 1, 0);
339 v4l2_ctrl_new_std_menu(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_PREEMPHASIS,
340 V4L2_PREEMPHASIS_75_uS, 1, V4L2_PREEMPHASIS_50_uS);
341 v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_POWER_LEVEL,
342 84, 118, 1, 118);
343 v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_COMPRESSION_GAIN,
344 -15, 18, 3, 0);
345 radio->pa = 118;
346 radio->tx = 0x32;
347 radio->stereo = true;
348 radio->curfreq = 95.16 * FREQ_MUL;
349 if (hdl->error) {
350 retval = hdl->error;
351
352 v4l2_ctrl_handler_free(hdl);
353 goto err_v4l2;
354 }
355 retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
356 if (retval < 0) {
357 dev_err(&intf->dev, "couldn't register v4l2_device\n");
358 goto err_v4l2;
359 }
360
361 mutex_init(&radio->lock);
362
363 radio->v4l2_dev.ctrl_handler = hdl;
364 radio->v4l2_dev.release = usb_keene_video_device_release;
365 strlcpy(radio->vdev.name, radio->v4l2_dev.name,
366 sizeof(radio->vdev.name));
367 radio->vdev.v4l2_dev = &radio->v4l2_dev;
368 radio->vdev.fops = &usb_keene_fops;
369 radio->vdev.ioctl_ops = &usb_keene_ioctl_ops;
370 radio->vdev.lock = &radio->lock;
371 radio->vdev.release = video_device_release_empty;
372
373 radio->usbdev = interface_to_usbdev(intf);
374 radio->intf = intf;
375 usb_set_intfdata(intf, &radio->v4l2_dev);
376
377 video_set_drvdata(&radio->vdev, radio);
378 set_bit(V4L2_FL_USE_FH_PRIO, &radio->vdev.flags);
379
380 retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
381 if (retval < 0) {
382 dev_err(&intf->dev, "could not register video device\n");
383 goto err_vdev;
384 }
385 v4l2_ctrl_handler_setup(hdl);
386 dev_info(&intf->dev, "V4L2 device registered as %s\n",
387 video_device_node_name(&radio->vdev));
388 return 0;
389
390err_vdev:
391 v4l2_device_unregister(&radio->v4l2_dev);
392err_v4l2:
393 kfree(radio->buffer);
394 kfree(radio);
395err:
396 return retval;
397}
398
399/* USB subsystem interface */
400static struct usb_driver usb_keene_driver = {
401 .name = "radio-keene",
402 .probe = usb_keene_probe,
403 .disconnect = usb_keene_disconnect,
404 .id_table = usb_keene_device_table,
405};
406
407static int __init keene_init(void)
408{
409 int retval = usb_register(&usb_keene_driver);
410
411 if (retval)
412 pr_err(KBUILD_MODNAME
413 ": usb_register failed. Error number %d\n", retval);
414
415 return retval;
416}
417
418static void __exit keene_exit(void)
419{
420 usb_deregister(&usb_keene_driver);
421}
422
423module_init(keene_init);
424module_exit(keene_exit);
425
This page took 0.050779 seconds and 5 git commands to generate.