Commit | Line | Data |
---|---|---|
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 */ | |
34 | MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>"); | |
35 | MODULE_DESCRIPTION("Keene FM Transmitter driver"); | |
36 | MODULE_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 */ | |
52 | static 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 | ||
58 | MODULE_DEVICE_TABLE(usb, usb_keene_device_table); | |
59 | ||
60 | struct 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 | ||
77 | static 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. */ | |
83 | static 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). */ | |
116 | static 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 | */ | |
147 | static 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 | ||
ff27cda3 HV |
159 | static 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 | ||
166 | static 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 | ||
1bf20c3a HV |
176 | static 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 | ||
189 | static 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 | ||
205 | static int vidioc_s_modulator(struct file *file, void *priv, | |
3f70e1f5 | 206 | const struct v4l2_modulator *v) |
1bf20c3a HV |
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 | ||
217 | static 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 | ||
229 | static 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 | ||
241 | static 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 | ||
1bf20c3a HV |
274 | /* File system interface */ |
275 | static const struct v4l2_file_operations usb_keene_fops = { | |
276 | .owner = THIS_MODULE, | |
277 | .open = v4l2_fh_open, | |
278 | .release = v4l2_fh_release, | |
279 | .poll = v4l2_ctrl_poll, | |
280 | .unlocked_ioctl = video_ioctl2, | |
281 | }; | |
282 | ||
283 | static const struct v4l2_ctrl_ops keene_ctrl_ops = { | |
284 | .s_ctrl = keene_s_ctrl, | |
285 | }; | |
286 | ||
287 | static const struct v4l2_ioctl_ops usb_keene_ioctl_ops = { | |
288 | .vidioc_querycap = vidioc_querycap, | |
289 | .vidioc_g_modulator = vidioc_g_modulator, | |
290 | .vidioc_s_modulator = vidioc_s_modulator, | |
291 | .vidioc_g_frequency = vidioc_g_frequency, | |
292 | .vidioc_s_frequency = vidioc_s_frequency, | |
293 | .vidioc_log_status = v4l2_ctrl_log_status, | |
a22d85fe | 294 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
1bf20c3a HV |
295 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
296 | }; | |
297 | ||
298 | static void usb_keene_video_device_release(struct v4l2_device *v4l2_dev) | |
299 | { | |
300 | struct keene_device *radio = to_keene_dev(v4l2_dev); | |
301 | ||
302 | /* free rest memory */ | |
303 | v4l2_ctrl_handler_free(&radio->hdl); | |
304 | kfree(radio->buffer); | |
305 | kfree(radio); | |
306 | } | |
307 | ||
308 | /* check if the device is present and register with v4l and usb if it is */ | |
309 | static int usb_keene_probe(struct usb_interface *intf, | |
310 | const struct usb_device_id *id) | |
311 | { | |
312 | struct usb_device *dev = interface_to_usbdev(intf); | |
313 | struct keene_device *radio; | |
314 | struct v4l2_ctrl_handler *hdl; | |
315 | int retval = 0; | |
316 | ||
317 | /* | |
318 | * The Keene FM transmitter USB device has the same USB ID as | |
319 | * the Logitech AudioHub Speaker, but it should ignore the hid. | |
320 | * Check if the name is that of the Keene device. | |
321 | * If not, then someone connected the AudioHub and we shouldn't | |
322 | * attempt to handle this driver. | |
323 | * For reference: the product name of the AudioHub is | |
324 | * "AudioHub Speaker". | |
325 | */ | |
326 | if (dev->product && strcmp(dev->product, "B-LINK USB Audio ")) | |
327 | return -ENODEV; | |
328 | ||
329 | radio = kzalloc(sizeof(struct keene_device), GFP_KERNEL); | |
330 | if (radio) | |
331 | radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL); | |
332 | ||
333 | if (!radio || !radio->buffer) { | |
334 | dev_err(&intf->dev, "kmalloc for keene_device failed\n"); | |
335 | kfree(radio); | |
336 | retval = -ENOMEM; | |
337 | goto err; | |
338 | } | |
339 | ||
340 | hdl = &radio->hdl; | |
341 | v4l2_ctrl_handler_init(hdl, 4); | |
342 | v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_MUTE, | |
343 | 0, 1, 1, 0); | |
344 | v4l2_ctrl_new_std_menu(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_PREEMPHASIS, | |
345 | V4L2_PREEMPHASIS_75_uS, 1, V4L2_PREEMPHASIS_50_uS); | |
346 | v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_POWER_LEVEL, | |
347 | 84, 118, 1, 118); | |
348 | v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_COMPRESSION_GAIN, | |
349 | -15, 18, 3, 0); | |
350 | radio->pa = 118; | |
351 | radio->tx = 0x32; | |
352 | radio->stereo = true; | |
353 | radio->curfreq = 95.16 * FREQ_MUL; | |
354 | if (hdl->error) { | |
355 | retval = hdl->error; | |
356 | ||
357 | v4l2_ctrl_handler_free(hdl); | |
358 | goto err_v4l2; | |
359 | } | |
360 | retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev); | |
361 | if (retval < 0) { | |
362 | dev_err(&intf->dev, "couldn't register v4l2_device\n"); | |
363 | goto err_v4l2; | |
364 | } | |
365 | ||
366 | mutex_init(&radio->lock); | |
367 | ||
368 | radio->v4l2_dev.ctrl_handler = hdl; | |
369 | radio->v4l2_dev.release = usb_keene_video_device_release; | |
370 | strlcpy(radio->vdev.name, radio->v4l2_dev.name, | |
371 | sizeof(radio->vdev.name)); | |
372 | radio->vdev.v4l2_dev = &radio->v4l2_dev; | |
373 | radio->vdev.fops = &usb_keene_fops; | |
374 | radio->vdev.ioctl_ops = &usb_keene_ioctl_ops; | |
375 | radio->vdev.lock = &radio->lock; | |
376 | radio->vdev.release = video_device_release_empty; | |
ce4a3d52 | 377 | radio->vdev.vfl_dir = VFL_DIR_TX; |
1bf20c3a HV |
378 | |
379 | radio->usbdev = interface_to_usbdev(intf); | |
380 | radio->intf = intf; | |
381 | usb_set_intfdata(intf, &radio->v4l2_dev); | |
382 | ||
383 | video_set_drvdata(&radio->vdev, radio); | |
384 | set_bit(V4L2_FL_USE_FH_PRIO, &radio->vdev.flags); | |
385 | ||
386 | retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1); | |
387 | if (retval < 0) { | |
388 | dev_err(&intf->dev, "could not register video device\n"); | |
389 | goto err_vdev; | |
390 | } | |
391 | v4l2_ctrl_handler_setup(hdl); | |
392 | dev_info(&intf->dev, "V4L2 device registered as %s\n", | |
393 | video_device_node_name(&radio->vdev)); | |
394 | return 0; | |
395 | ||
396 | err_vdev: | |
397 | v4l2_device_unregister(&radio->v4l2_dev); | |
398 | err_v4l2: | |
399 | kfree(radio->buffer); | |
400 | kfree(radio); | |
401 | err: | |
402 | return retval; | |
403 | } | |
404 | ||
405 | /* USB subsystem interface */ | |
406 | static struct usb_driver usb_keene_driver = { | |
407 | .name = "radio-keene", | |
408 | .probe = usb_keene_probe, | |
409 | .disconnect = usb_keene_disconnect, | |
410 | .id_table = usb_keene_device_table, | |
ff27cda3 HV |
411 | .suspend = usb_keene_suspend, |
412 | .resume = usb_keene_resume, | |
413 | .reset_resume = usb_keene_resume, | |
1bf20c3a HV |
414 | }; |
415 | ||
416 | static int __init keene_init(void) | |
417 | { | |
418 | int retval = usb_register(&usb_keene_driver); | |
419 | ||
420 | if (retval) | |
421 | pr_err(KBUILD_MODNAME | |
422 | ": usb_register failed. Error number %d\n", retval); | |
423 | ||
424 | return retval; | |
425 | } | |
426 | ||
427 | static void __exit keene_exit(void) | |
428 | { | |
429 | usb_deregister(&usb_keene_driver); | |
430 | } | |
431 | ||
432 | module_init(keene_init); | |
433 | module_exit(keene_exit); | |
434 |