blob: 62d32c4946f71b7939ebb7e01ed2c31f038948f9 [file] [log] [blame]
Hans Verkuil1bf20c32012-02-02 08:44:40 -03001/*
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>
Hans Verkuil1bf20c32012-02-02 08:44:40 -030031#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
Hans Verkuil1bf20c32012-02-02 08:44:40 -0300151 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
Hans Verkuilff27cda2012-04-18 06:47:02 -0300159static int usb_keene_suspend(struct usb_interface *intf, pm_message_t message)
160{
161 struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf));
162
163 return keene_cmd_main(radio, 0, false);
164}
165
166static int usb_keene_resume(struct usb_interface *intf)
167{
168 struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf));
169
170 mdelay(50);
171 keene_cmd_set(radio);
172 keene_cmd_main(radio, radio->curfreq, true);
173 return 0;
174}
175
Hans Verkuil1bf20c32012-02-02 08:44:40 -0300176static int vidioc_querycap(struct file *file, void *priv,
177 struct v4l2_capability *v)
178{
179 struct keene_device *radio = video_drvdata(file);
180
181 strlcpy(v->driver, "radio-keene", sizeof(v->driver));
182 strlcpy(v->card, "Keene FM Transmitter", sizeof(v->card));
183 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
184 v->device_caps = V4L2_CAP_RADIO | V4L2_CAP_MODULATOR;
185 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
186 return 0;
187}
188
189static int vidioc_g_modulator(struct file *file, void *priv,
190 struct v4l2_modulator *v)
191{
192 struct keene_device *radio = video_drvdata(file);
193
194 if (v->index > 0)
195 return -EINVAL;
196
197 strlcpy(v->name, "FM", sizeof(v->name));
198 v->rangelow = FREQ_MIN * FREQ_MUL;
199 v->rangehigh = FREQ_MAX * FREQ_MUL;
200 v->txsubchans = radio->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
201 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
202 return 0;
203}
204
205static int vidioc_s_modulator(struct file *file, void *priv,
206 struct v4l2_modulator *v)
207{
208 struct keene_device *radio = video_drvdata(file);
209
210 if (v->index > 0)
211 return -EINVAL;
212
213 radio->stereo = (v->txsubchans == V4L2_TUNER_SUB_STEREO);
214 return keene_cmd_set(radio);
215}
216
217static int vidioc_s_frequency(struct file *file, void *priv,
218 struct v4l2_frequency *f)
219{
220 struct keene_device *radio = video_drvdata(file);
221
222 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
223 return -EINVAL;
224 f->frequency = clamp(f->frequency,
225 FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL);
226 return keene_cmd_main(radio, f->frequency, true);
227}
228
229static int vidioc_g_frequency(struct file *file, void *priv,
230 struct v4l2_frequency *f)
231{
232 struct keene_device *radio = video_drvdata(file);
233
234 if (f->tuner != 0)
235 return -EINVAL;
236 f->type = V4L2_TUNER_RADIO;
237 f->frequency = radio->curfreq;
238 return 0;
239}
240
241static int keene_s_ctrl(struct v4l2_ctrl *ctrl)
242{
243 static const u8 db2tx[] = {
244 /* -15, -12, -9, -6, -3, 0 dB */
245 0x03, 0x13, 0x02, 0x12, 0x22, 0x32,
246 /* 3, 6, 9, 12, 15, 18 dB */
247 0x21, 0x31, 0x20, 0x30, 0x40, 0x50
248 };
249 struct keene_device *radio =
250 container_of(ctrl->handler, struct keene_device, hdl);
251
252 switch (ctrl->id) {
253 case V4L2_CID_AUDIO_MUTE:
254 radio->muted = ctrl->val;
255 return keene_cmd_main(radio, 0, true);
256
257 case V4L2_CID_TUNE_POWER_LEVEL:
258 /* To go from dBuV to the register value we apply the
259 following formula: */
260 radio->pa = (ctrl->val - 71) * 100 / 62;
261 return keene_cmd_main(radio, 0, true);
262
263 case V4L2_CID_TUNE_PREEMPHASIS:
264 radio->preemph_75_us = ctrl->val == V4L2_PREEMPHASIS_75_uS;
265 return keene_cmd_set(radio);
266
267 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
268 radio->tx = db2tx[(ctrl->val - ctrl->minimum) / ctrl->step];
269 return keene_cmd_set(radio);
270 }
271 return -EINVAL;
272}
273
274static int vidioc_subscribe_event(struct v4l2_fh *fh,
275 struct v4l2_event_subscription *sub)
276{
277 switch (sub->type) {
278 case V4L2_EVENT_CTRL:
279 return v4l2_event_subscribe(fh, sub, 0);
280 default:
281 return -EINVAL;
282 }
283}
284
285
286/* File system interface */
287static const struct v4l2_file_operations usb_keene_fops = {
288 .owner = THIS_MODULE,
289 .open = v4l2_fh_open,
290 .release = v4l2_fh_release,
291 .poll = v4l2_ctrl_poll,
292 .unlocked_ioctl = video_ioctl2,
293};
294
295static const struct v4l2_ctrl_ops keene_ctrl_ops = {
296 .s_ctrl = keene_s_ctrl,
297};
298
299static const struct v4l2_ioctl_ops usb_keene_ioctl_ops = {
300 .vidioc_querycap = vidioc_querycap,
301 .vidioc_g_modulator = vidioc_g_modulator,
302 .vidioc_s_modulator = vidioc_s_modulator,
303 .vidioc_g_frequency = vidioc_g_frequency,
304 .vidioc_s_frequency = vidioc_s_frequency,
305 .vidioc_log_status = v4l2_ctrl_log_status,
306 .vidioc_subscribe_event = vidioc_subscribe_event,
307 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
308};
309
310static void usb_keene_video_device_release(struct v4l2_device *v4l2_dev)
311{
312 struct keene_device *radio = to_keene_dev(v4l2_dev);
313
314 /* free rest memory */
315 v4l2_ctrl_handler_free(&radio->hdl);
316 kfree(radio->buffer);
317 kfree(radio);
318}
319
320/* check if the device is present and register with v4l and usb if it is */
321static int usb_keene_probe(struct usb_interface *intf,
322 const struct usb_device_id *id)
323{
324 struct usb_device *dev = interface_to_usbdev(intf);
325 struct keene_device *radio;
326 struct v4l2_ctrl_handler *hdl;
327 int retval = 0;
328
329 /*
330 * The Keene FM transmitter USB device has the same USB ID as
331 * the Logitech AudioHub Speaker, but it should ignore the hid.
332 * Check if the name is that of the Keene device.
333 * If not, then someone connected the AudioHub and we shouldn't
334 * attempt to handle this driver.
335 * For reference: the product name of the AudioHub is
336 * "AudioHub Speaker".
337 */
338 if (dev->product && strcmp(dev->product, "B-LINK USB Audio "))
339 return -ENODEV;
340
341 radio = kzalloc(sizeof(struct keene_device), GFP_KERNEL);
342 if (radio)
343 radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
344
345 if (!radio || !radio->buffer) {
346 dev_err(&intf->dev, "kmalloc for keene_device failed\n");
347 kfree(radio);
348 retval = -ENOMEM;
349 goto err;
350 }
351
352 hdl = &radio->hdl;
353 v4l2_ctrl_handler_init(hdl, 4);
354 v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_MUTE,
355 0, 1, 1, 0);
356 v4l2_ctrl_new_std_menu(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_PREEMPHASIS,
357 V4L2_PREEMPHASIS_75_uS, 1, V4L2_PREEMPHASIS_50_uS);
358 v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_POWER_LEVEL,
359 84, 118, 1, 118);
360 v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_COMPRESSION_GAIN,
361 -15, 18, 3, 0);
362 radio->pa = 118;
363 radio->tx = 0x32;
364 radio->stereo = true;
365 radio->curfreq = 95.16 * FREQ_MUL;
366 if (hdl->error) {
367 retval = hdl->error;
368
369 v4l2_ctrl_handler_free(hdl);
370 goto err_v4l2;
371 }
372 retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
373 if (retval < 0) {
374 dev_err(&intf->dev, "couldn't register v4l2_device\n");
375 goto err_v4l2;
376 }
377
378 mutex_init(&radio->lock);
379
380 radio->v4l2_dev.ctrl_handler = hdl;
381 radio->v4l2_dev.release = usb_keene_video_device_release;
382 strlcpy(radio->vdev.name, radio->v4l2_dev.name,
383 sizeof(radio->vdev.name));
384 radio->vdev.v4l2_dev = &radio->v4l2_dev;
385 radio->vdev.fops = &usb_keene_fops;
386 radio->vdev.ioctl_ops = &usb_keene_ioctl_ops;
387 radio->vdev.lock = &radio->lock;
388 radio->vdev.release = video_device_release_empty;
389
390 radio->usbdev = interface_to_usbdev(intf);
391 radio->intf = intf;
392 usb_set_intfdata(intf, &radio->v4l2_dev);
393
394 video_set_drvdata(&radio->vdev, radio);
395 set_bit(V4L2_FL_USE_FH_PRIO, &radio->vdev.flags);
396
397 retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
398 if (retval < 0) {
399 dev_err(&intf->dev, "could not register video device\n");
400 goto err_vdev;
401 }
402 v4l2_ctrl_handler_setup(hdl);
403 dev_info(&intf->dev, "V4L2 device registered as %s\n",
404 video_device_node_name(&radio->vdev));
405 return 0;
406
407err_vdev:
408 v4l2_device_unregister(&radio->v4l2_dev);
409err_v4l2:
410 kfree(radio->buffer);
411 kfree(radio);
412err:
413 return retval;
414}
415
416/* USB subsystem interface */
417static struct usb_driver usb_keene_driver = {
418 .name = "radio-keene",
419 .probe = usb_keene_probe,
420 .disconnect = usb_keene_disconnect,
421 .id_table = usb_keene_device_table,
Hans Verkuilff27cda2012-04-18 06:47:02 -0300422 .suspend = usb_keene_suspend,
423 .resume = usb_keene_resume,
424 .reset_resume = usb_keene_resume,
Hans Verkuil1bf20c32012-02-02 08:44:40 -0300425};
426
427static int __init keene_init(void)
428{
429 int retval = usb_register(&usb_keene_driver);
430
431 if (retval)
432 pr_err(KBUILD_MODNAME
433 ": usb_register failed. Error number %d\n", retval);
434
435 return retval;
436}
437
438static void __exit keene_exit(void)
439{
440 usb_deregister(&usb_keene_driver);
441}
442
443module_init(keene_init);
444module_exit(keene_exit);
445