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