18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2012 Hans Verkuil <hverkuil@xs4all.nl> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci/* kernel includes */ 78c2ecf20Sopenharmony_ci#include <linux/kernel.h> 88c2ecf20Sopenharmony_ci#include <linux/module.h> 98c2ecf20Sopenharmony_ci#include <linux/init.h> 108c2ecf20Sopenharmony_ci#include <linux/slab.h> 118c2ecf20Sopenharmony_ci#include <linux/input.h> 128c2ecf20Sopenharmony_ci#include <linux/videodev2.h> 138c2ecf20Sopenharmony_ci#include <media/v4l2-device.h> 148c2ecf20Sopenharmony_ci#include <media/v4l2-ioctl.h> 158c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h> 168c2ecf20Sopenharmony_ci#include <media/v4l2-event.h> 178c2ecf20Sopenharmony_ci#include <linux/usb.h> 188c2ecf20Sopenharmony_ci#include <linux/mutex.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci/* driver and module definitions */ 218c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>"); 228c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Keene FM Transmitter driver"); 238c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* Actually, it advertises itself as a Logitech */ 268c2ecf20Sopenharmony_ci#define USB_KEENE_VENDOR 0x046d 278c2ecf20Sopenharmony_ci#define USB_KEENE_PRODUCT 0x0a0e 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* Probably USB_TIMEOUT should be modified in module parameter */ 308c2ecf20Sopenharmony_ci#define BUFFER_LENGTH 8 318c2ecf20Sopenharmony_ci#define USB_TIMEOUT 500 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci/* Frequency limits in MHz */ 348c2ecf20Sopenharmony_ci#define FREQ_MIN 76U 358c2ecf20Sopenharmony_ci#define FREQ_MAX 108U 368c2ecf20Sopenharmony_ci#define FREQ_MUL 16000U 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci/* USB Device ID List */ 398c2ecf20Sopenharmony_cistatic const struct usb_device_id usb_keene_device_table[] = { 408c2ecf20Sopenharmony_ci {USB_DEVICE_AND_INTERFACE_INFO(USB_KEENE_VENDOR, USB_KEENE_PRODUCT, 418c2ecf20Sopenharmony_ci USB_CLASS_HID, 0, 0) }, 428c2ecf20Sopenharmony_ci { } /* Terminating entry */ 438c2ecf20Sopenharmony_ci}; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, usb_keene_device_table); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_cistruct keene_device { 488c2ecf20Sopenharmony_ci struct usb_device *usbdev; 498c2ecf20Sopenharmony_ci struct usb_interface *intf; 508c2ecf20Sopenharmony_ci struct video_device vdev; 518c2ecf20Sopenharmony_ci struct v4l2_device v4l2_dev; 528c2ecf20Sopenharmony_ci struct v4l2_ctrl_handler hdl; 538c2ecf20Sopenharmony_ci struct mutex lock; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci u8 *buffer; 568c2ecf20Sopenharmony_ci unsigned curfreq; 578c2ecf20Sopenharmony_ci u8 tx; 588c2ecf20Sopenharmony_ci u8 pa; 598c2ecf20Sopenharmony_ci bool stereo; 608c2ecf20Sopenharmony_ci bool muted; 618c2ecf20Sopenharmony_ci bool preemph_75_us; 628c2ecf20Sopenharmony_ci}; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic inline struct keene_device *to_keene_dev(struct v4l2_device *v4l2_dev) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci return container_of(v4l2_dev, struct keene_device, v4l2_dev); 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci/* Set frequency (if non-0), PA, mute and turn on/off the FM transmitter. */ 708c2ecf20Sopenharmony_cistatic int keene_cmd_main(struct keene_device *radio, unsigned freq, bool play) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci unsigned short freq_send = freq ? (freq - 76 * 16000) / 800 : 0; 738c2ecf20Sopenharmony_ci int ret; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci radio->buffer[0] = 0x00; 768c2ecf20Sopenharmony_ci radio->buffer[1] = 0x50; 778c2ecf20Sopenharmony_ci radio->buffer[2] = (freq_send >> 8) & 0xff; 788c2ecf20Sopenharmony_ci radio->buffer[3] = freq_send & 0xff; 798c2ecf20Sopenharmony_ci radio->buffer[4] = radio->pa; 808c2ecf20Sopenharmony_ci /* If bit 4 is set, then tune to the frequency. 818c2ecf20Sopenharmony_ci If bit 3 is set, then unmute; if bit 2 is set, then mute. 828c2ecf20Sopenharmony_ci If bit 1 is set, then enter idle mode; if bit 0 is set, 838c2ecf20Sopenharmony_ci then enter transmit mode. 848c2ecf20Sopenharmony_ci */ 858c2ecf20Sopenharmony_ci radio->buffer[5] = (radio->muted ? 4 : 8) | (play ? 1 : 2) | 868c2ecf20Sopenharmony_ci (freq ? 0x10 : 0); 878c2ecf20Sopenharmony_ci radio->buffer[6] = 0x00; 888c2ecf20Sopenharmony_ci radio->buffer[7] = 0x00; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0), 918c2ecf20Sopenharmony_ci 9, 0x21, 0x200, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci if (ret < 0) { 948c2ecf20Sopenharmony_ci dev_warn(&radio->vdev.dev, "%s failed (%d)\n", __func__, ret); 958c2ecf20Sopenharmony_ci return ret; 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci if (freq) 988c2ecf20Sopenharmony_ci radio->curfreq = freq; 998c2ecf20Sopenharmony_ci return 0; 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci/* Set TX, stereo and preemphasis mode (50 us vs 75 us). */ 1038c2ecf20Sopenharmony_cistatic int keene_cmd_set(struct keene_device *radio) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci int ret; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci radio->buffer[0] = 0x00; 1088c2ecf20Sopenharmony_ci radio->buffer[1] = 0x51; 1098c2ecf20Sopenharmony_ci radio->buffer[2] = radio->tx; 1108c2ecf20Sopenharmony_ci /* If bit 0 is set, then transmit mono, otherwise stereo. 1118c2ecf20Sopenharmony_ci If bit 2 is set, then enable 75 us preemphasis, otherwise 1128c2ecf20Sopenharmony_ci it is 50 us. */ 1138c2ecf20Sopenharmony_ci radio->buffer[3] = (radio->stereo ? 0 : 1) | (radio->preemph_75_us ? 4 : 0); 1148c2ecf20Sopenharmony_ci radio->buffer[4] = 0x00; 1158c2ecf20Sopenharmony_ci radio->buffer[5] = 0x00; 1168c2ecf20Sopenharmony_ci radio->buffer[6] = 0x00; 1178c2ecf20Sopenharmony_ci radio->buffer[7] = 0x00; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0), 1208c2ecf20Sopenharmony_ci 9, 0x21, 0x200, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci if (ret < 0) { 1238c2ecf20Sopenharmony_ci dev_warn(&radio->vdev.dev, "%s failed (%d)\n", __func__, ret); 1248c2ecf20Sopenharmony_ci return ret; 1258c2ecf20Sopenharmony_ci } 1268c2ecf20Sopenharmony_ci return 0; 1278c2ecf20Sopenharmony_ci} 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci/* Handle unplugging the device. 1308c2ecf20Sopenharmony_ci * We call video_unregister_device in any case. 1318c2ecf20Sopenharmony_ci * The last function called in this procedure is 1328c2ecf20Sopenharmony_ci * usb_keene_device_release. 1338c2ecf20Sopenharmony_ci */ 1348c2ecf20Sopenharmony_cistatic void usb_keene_disconnect(struct usb_interface *intf) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf)); 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci mutex_lock(&radio->lock); 1398c2ecf20Sopenharmony_ci usb_set_intfdata(intf, NULL); 1408c2ecf20Sopenharmony_ci video_unregister_device(&radio->vdev); 1418c2ecf20Sopenharmony_ci v4l2_device_disconnect(&radio->v4l2_dev); 1428c2ecf20Sopenharmony_ci mutex_unlock(&radio->lock); 1438c2ecf20Sopenharmony_ci v4l2_device_put(&radio->v4l2_dev); 1448c2ecf20Sopenharmony_ci} 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cistatic int usb_keene_suspend(struct usb_interface *intf, pm_message_t message) 1478c2ecf20Sopenharmony_ci{ 1488c2ecf20Sopenharmony_ci struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf)); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci return keene_cmd_main(radio, 0, false); 1518c2ecf20Sopenharmony_ci} 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistatic int usb_keene_resume(struct usb_interface *intf) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf)); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci mdelay(50); 1588c2ecf20Sopenharmony_ci keene_cmd_set(radio); 1598c2ecf20Sopenharmony_ci keene_cmd_main(radio, radio->curfreq, true); 1608c2ecf20Sopenharmony_ci return 0; 1618c2ecf20Sopenharmony_ci} 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_cistatic int vidioc_querycap(struct file *file, void *priv, 1648c2ecf20Sopenharmony_ci struct v4l2_capability *v) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci struct keene_device *radio = video_drvdata(file); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci strscpy(v->driver, "radio-keene", sizeof(v->driver)); 1698c2ecf20Sopenharmony_ci strscpy(v->card, "Keene FM Transmitter", sizeof(v->card)); 1708c2ecf20Sopenharmony_ci usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info)); 1718c2ecf20Sopenharmony_ci return 0; 1728c2ecf20Sopenharmony_ci} 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_cistatic int vidioc_g_modulator(struct file *file, void *priv, 1758c2ecf20Sopenharmony_ci struct v4l2_modulator *v) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci struct keene_device *radio = video_drvdata(file); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci if (v->index > 0) 1808c2ecf20Sopenharmony_ci return -EINVAL; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci strscpy(v->name, "FM", sizeof(v->name)); 1838c2ecf20Sopenharmony_ci v->rangelow = FREQ_MIN * FREQ_MUL; 1848c2ecf20Sopenharmony_ci v->rangehigh = FREQ_MAX * FREQ_MUL; 1858c2ecf20Sopenharmony_ci v->txsubchans = radio->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; 1868c2ecf20Sopenharmony_ci v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; 1878c2ecf20Sopenharmony_ci return 0; 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic int vidioc_s_modulator(struct file *file, void *priv, 1918c2ecf20Sopenharmony_ci const struct v4l2_modulator *v) 1928c2ecf20Sopenharmony_ci{ 1938c2ecf20Sopenharmony_ci struct keene_device *radio = video_drvdata(file); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci if (v->index > 0) 1968c2ecf20Sopenharmony_ci return -EINVAL; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci radio->stereo = (v->txsubchans == V4L2_TUNER_SUB_STEREO); 1998c2ecf20Sopenharmony_ci return keene_cmd_set(radio); 2008c2ecf20Sopenharmony_ci} 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_cistatic int vidioc_s_frequency(struct file *file, void *priv, 2038c2ecf20Sopenharmony_ci const struct v4l2_frequency *f) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci struct keene_device *radio = video_drvdata(file); 2068c2ecf20Sopenharmony_ci unsigned freq = f->frequency; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) 2098c2ecf20Sopenharmony_ci return -EINVAL; 2108c2ecf20Sopenharmony_ci freq = clamp(freq, FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL); 2118c2ecf20Sopenharmony_ci return keene_cmd_main(radio, freq, true); 2128c2ecf20Sopenharmony_ci} 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_cistatic int vidioc_g_frequency(struct file *file, void *priv, 2158c2ecf20Sopenharmony_ci struct v4l2_frequency *f) 2168c2ecf20Sopenharmony_ci{ 2178c2ecf20Sopenharmony_ci struct keene_device *radio = video_drvdata(file); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci if (f->tuner != 0) 2208c2ecf20Sopenharmony_ci return -EINVAL; 2218c2ecf20Sopenharmony_ci f->type = V4L2_TUNER_RADIO; 2228c2ecf20Sopenharmony_ci f->frequency = radio->curfreq; 2238c2ecf20Sopenharmony_ci return 0; 2248c2ecf20Sopenharmony_ci} 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_cistatic int keene_s_ctrl(struct v4l2_ctrl *ctrl) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci static const u8 db2tx[] = { 2298c2ecf20Sopenharmony_ci /* -15, -12, -9, -6, -3, 0 dB */ 2308c2ecf20Sopenharmony_ci 0x03, 0x13, 0x02, 0x12, 0x22, 0x32, 2318c2ecf20Sopenharmony_ci /* 3, 6, 9, 12, 15, 18 dB */ 2328c2ecf20Sopenharmony_ci 0x21, 0x31, 0x20, 0x30, 0x40, 0x50 2338c2ecf20Sopenharmony_ci }; 2348c2ecf20Sopenharmony_ci struct keene_device *radio = 2358c2ecf20Sopenharmony_ci container_of(ctrl->handler, struct keene_device, hdl); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci switch (ctrl->id) { 2388c2ecf20Sopenharmony_ci case V4L2_CID_AUDIO_MUTE: 2398c2ecf20Sopenharmony_ci radio->muted = ctrl->val; 2408c2ecf20Sopenharmony_ci return keene_cmd_main(radio, 0, true); 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci case V4L2_CID_TUNE_POWER_LEVEL: 2438c2ecf20Sopenharmony_ci /* To go from dBuV to the register value we apply the 2448c2ecf20Sopenharmony_ci following formula: */ 2458c2ecf20Sopenharmony_ci radio->pa = (ctrl->val - 71) * 100 / 62; 2468c2ecf20Sopenharmony_ci return keene_cmd_main(radio, 0, true); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci case V4L2_CID_TUNE_PREEMPHASIS: 2498c2ecf20Sopenharmony_ci radio->preemph_75_us = ctrl->val == V4L2_PREEMPHASIS_75_uS; 2508c2ecf20Sopenharmony_ci return keene_cmd_set(radio); 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci case V4L2_CID_AUDIO_COMPRESSION_GAIN: 2538c2ecf20Sopenharmony_ci radio->tx = db2tx[(ctrl->val - (s32)ctrl->minimum) / (s32)ctrl->step]; 2548c2ecf20Sopenharmony_ci return keene_cmd_set(radio); 2558c2ecf20Sopenharmony_ci } 2568c2ecf20Sopenharmony_ci return -EINVAL; 2578c2ecf20Sopenharmony_ci} 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci/* File system interface */ 2608c2ecf20Sopenharmony_cistatic const struct v4l2_file_operations usb_keene_fops = { 2618c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 2628c2ecf20Sopenharmony_ci .open = v4l2_fh_open, 2638c2ecf20Sopenharmony_ci .release = v4l2_fh_release, 2648c2ecf20Sopenharmony_ci .poll = v4l2_ctrl_poll, 2658c2ecf20Sopenharmony_ci .unlocked_ioctl = video_ioctl2, 2668c2ecf20Sopenharmony_ci}; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops keene_ctrl_ops = { 2698c2ecf20Sopenharmony_ci .s_ctrl = keene_s_ctrl, 2708c2ecf20Sopenharmony_ci}; 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_cistatic const struct v4l2_ioctl_ops usb_keene_ioctl_ops = { 2738c2ecf20Sopenharmony_ci .vidioc_querycap = vidioc_querycap, 2748c2ecf20Sopenharmony_ci .vidioc_g_modulator = vidioc_g_modulator, 2758c2ecf20Sopenharmony_ci .vidioc_s_modulator = vidioc_s_modulator, 2768c2ecf20Sopenharmony_ci .vidioc_g_frequency = vidioc_g_frequency, 2778c2ecf20Sopenharmony_ci .vidioc_s_frequency = vidioc_s_frequency, 2788c2ecf20Sopenharmony_ci .vidioc_log_status = v4l2_ctrl_log_status, 2798c2ecf20Sopenharmony_ci .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 2808c2ecf20Sopenharmony_ci .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 2818c2ecf20Sopenharmony_ci}; 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_cistatic void usb_keene_video_device_release(struct v4l2_device *v4l2_dev) 2848c2ecf20Sopenharmony_ci{ 2858c2ecf20Sopenharmony_ci struct keene_device *radio = to_keene_dev(v4l2_dev); 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci /* free rest memory */ 2888c2ecf20Sopenharmony_ci v4l2_ctrl_handler_free(&radio->hdl); 2898c2ecf20Sopenharmony_ci kfree(radio->buffer); 2908c2ecf20Sopenharmony_ci kfree(radio); 2918c2ecf20Sopenharmony_ci} 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci/* check if the device is present and register with v4l and usb if it is */ 2948c2ecf20Sopenharmony_cistatic int usb_keene_probe(struct usb_interface *intf, 2958c2ecf20Sopenharmony_ci const struct usb_device_id *id) 2968c2ecf20Sopenharmony_ci{ 2978c2ecf20Sopenharmony_ci struct usb_device *dev = interface_to_usbdev(intf); 2988c2ecf20Sopenharmony_ci struct keene_device *radio; 2998c2ecf20Sopenharmony_ci struct v4l2_ctrl_handler *hdl; 3008c2ecf20Sopenharmony_ci int retval = 0; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci /* 3038c2ecf20Sopenharmony_ci * The Keene FM transmitter USB device has the same USB ID as 3048c2ecf20Sopenharmony_ci * the Logitech AudioHub Speaker, but it should ignore the hid. 3058c2ecf20Sopenharmony_ci * Check if the name is that of the Keene device. 3068c2ecf20Sopenharmony_ci * If not, then someone connected the AudioHub and we shouldn't 3078c2ecf20Sopenharmony_ci * attempt to handle this driver. 3088c2ecf20Sopenharmony_ci * For reference: the product name of the AudioHub is 3098c2ecf20Sopenharmony_ci * "AudioHub Speaker". 3108c2ecf20Sopenharmony_ci */ 3118c2ecf20Sopenharmony_ci if (dev->product && strcmp(dev->product, "B-LINK USB Audio ")) 3128c2ecf20Sopenharmony_ci return -ENODEV; 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci radio = kzalloc(sizeof(struct keene_device), GFP_KERNEL); 3158c2ecf20Sopenharmony_ci if (radio) 3168c2ecf20Sopenharmony_ci radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL); 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ci if (!radio || !radio->buffer) { 3198c2ecf20Sopenharmony_ci dev_err(&intf->dev, "kmalloc for keene_device failed\n"); 3208c2ecf20Sopenharmony_ci kfree(radio); 3218c2ecf20Sopenharmony_ci retval = -ENOMEM; 3228c2ecf20Sopenharmony_ci goto err; 3238c2ecf20Sopenharmony_ci } 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci hdl = &radio->hdl; 3268c2ecf20Sopenharmony_ci v4l2_ctrl_handler_init(hdl, 4); 3278c2ecf20Sopenharmony_ci v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_MUTE, 3288c2ecf20Sopenharmony_ci 0, 1, 1, 0); 3298c2ecf20Sopenharmony_ci v4l2_ctrl_new_std_menu(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_PREEMPHASIS, 3308c2ecf20Sopenharmony_ci V4L2_PREEMPHASIS_75_uS, 1, V4L2_PREEMPHASIS_50_uS); 3318c2ecf20Sopenharmony_ci v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_POWER_LEVEL, 3328c2ecf20Sopenharmony_ci 84, 118, 1, 118); 3338c2ecf20Sopenharmony_ci v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_COMPRESSION_GAIN, 3348c2ecf20Sopenharmony_ci -15, 18, 3, 0); 3358c2ecf20Sopenharmony_ci radio->pa = 118; 3368c2ecf20Sopenharmony_ci radio->tx = 0x32; 3378c2ecf20Sopenharmony_ci radio->stereo = true; 3388c2ecf20Sopenharmony_ci if (hdl->error) { 3398c2ecf20Sopenharmony_ci retval = hdl->error; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci v4l2_ctrl_handler_free(hdl); 3428c2ecf20Sopenharmony_ci goto err_v4l2; 3438c2ecf20Sopenharmony_ci } 3448c2ecf20Sopenharmony_ci retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev); 3458c2ecf20Sopenharmony_ci if (retval < 0) { 3468c2ecf20Sopenharmony_ci dev_err(&intf->dev, "couldn't register v4l2_device\n"); 3478c2ecf20Sopenharmony_ci goto err_v4l2; 3488c2ecf20Sopenharmony_ci } 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci mutex_init(&radio->lock); 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci radio->v4l2_dev.ctrl_handler = hdl; 3538c2ecf20Sopenharmony_ci radio->v4l2_dev.release = usb_keene_video_device_release; 3548c2ecf20Sopenharmony_ci strscpy(radio->vdev.name, radio->v4l2_dev.name, 3558c2ecf20Sopenharmony_ci sizeof(radio->vdev.name)); 3568c2ecf20Sopenharmony_ci radio->vdev.v4l2_dev = &radio->v4l2_dev; 3578c2ecf20Sopenharmony_ci radio->vdev.fops = &usb_keene_fops; 3588c2ecf20Sopenharmony_ci radio->vdev.ioctl_ops = &usb_keene_ioctl_ops; 3598c2ecf20Sopenharmony_ci radio->vdev.lock = &radio->lock; 3608c2ecf20Sopenharmony_ci radio->vdev.release = video_device_release_empty; 3618c2ecf20Sopenharmony_ci radio->vdev.vfl_dir = VFL_DIR_TX; 3628c2ecf20Sopenharmony_ci radio->vdev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_MODULATOR; 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci radio->usbdev = interface_to_usbdev(intf); 3658c2ecf20Sopenharmony_ci radio->intf = intf; 3668c2ecf20Sopenharmony_ci usb_set_intfdata(intf, &radio->v4l2_dev); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci video_set_drvdata(&radio->vdev, radio); 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci /* at least 11ms is needed in order to settle hardware */ 3718c2ecf20Sopenharmony_ci msleep(20); 3728c2ecf20Sopenharmony_ci keene_cmd_main(radio, 95.16 * FREQ_MUL, false); 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1); 3758c2ecf20Sopenharmony_ci if (retval < 0) { 3768c2ecf20Sopenharmony_ci dev_err(&intf->dev, "could not register video device\n"); 3778c2ecf20Sopenharmony_ci goto err_vdev; 3788c2ecf20Sopenharmony_ci } 3798c2ecf20Sopenharmony_ci v4l2_ctrl_handler_setup(hdl); 3808c2ecf20Sopenharmony_ci dev_info(&intf->dev, "V4L2 device registered as %s\n", 3818c2ecf20Sopenharmony_ci video_device_node_name(&radio->vdev)); 3828c2ecf20Sopenharmony_ci return 0; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_cierr_vdev: 3858c2ecf20Sopenharmony_ci v4l2_device_unregister(&radio->v4l2_dev); 3868c2ecf20Sopenharmony_cierr_v4l2: 3878c2ecf20Sopenharmony_ci kfree(radio->buffer); 3888c2ecf20Sopenharmony_ci kfree(radio); 3898c2ecf20Sopenharmony_cierr: 3908c2ecf20Sopenharmony_ci return retval; 3918c2ecf20Sopenharmony_ci} 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci/* USB subsystem interface */ 3948c2ecf20Sopenharmony_cistatic struct usb_driver usb_keene_driver = { 3958c2ecf20Sopenharmony_ci .name = "radio-keene", 3968c2ecf20Sopenharmony_ci .probe = usb_keene_probe, 3978c2ecf20Sopenharmony_ci .disconnect = usb_keene_disconnect, 3988c2ecf20Sopenharmony_ci .id_table = usb_keene_device_table, 3998c2ecf20Sopenharmony_ci .suspend = usb_keene_suspend, 4008c2ecf20Sopenharmony_ci .resume = usb_keene_resume, 4018c2ecf20Sopenharmony_ci .reset_resume = usb_keene_resume, 4028c2ecf20Sopenharmony_ci}; 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_cimodule_usb_driver(usb_keene_driver); 4058c2ecf20Sopenharmony_ci 406