linux/drivers/media/radio/si4713/radio-usb-si4713.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates.
   4 * All rights reserved.
   5 */
   6
   7/* kernel includes */
   8#include <linux/kernel.h>
   9#include <linux/module.h>
  10#include <linux/usb.h>
  11#include <linux/init.h>
  12#include <linux/slab.h>
  13#include <linux/input.h>
  14#include <linux/mutex.h>
  15#include <linux/i2c.h>
  16/* V4l includes */
  17#include <linux/videodev2.h>
  18#include <media/v4l2-common.h>
  19#include <media/v4l2-device.h>
  20#include <media/v4l2-ioctl.h>
  21#include <media/v4l2-event.h>
  22#include <linux/platform_data/media/si4713.h>
  23
  24#include "si4713.h"
  25
  26/* driver and module definitions */
  27MODULE_AUTHOR("Dinesh Ram <dinesh.ram@cern.ch>");
  28MODULE_DESCRIPTION("Si4713 FM Transmitter USB driver");
  29MODULE_LICENSE("GPL v2");
  30
  31/* The Device announces itself as Cygnal Integrated Products, Inc. */
  32#define USB_SI4713_VENDOR               0x10c4
  33#define USB_SI4713_PRODUCT              0x8244
  34
  35#define BUFFER_LENGTH                   64
  36#define USB_TIMEOUT                     1000
  37#define USB_RESP_TIMEOUT                50000
  38
  39/* USB Device ID List */
  40static const struct usb_device_id usb_si4713_usb_device_table[] = {
  41        {USB_DEVICE_AND_INTERFACE_INFO(USB_SI4713_VENDOR, USB_SI4713_PRODUCT,
  42                                                        USB_CLASS_HID, 0, 0) },
  43        { }                                             /* Terminating entry */
  44};
  45
  46MODULE_DEVICE_TABLE(usb, usb_si4713_usb_device_table);
  47
  48struct si4713_usb_device {
  49        struct usb_device       *usbdev;
  50        struct usb_interface    *intf;
  51        struct video_device     vdev;
  52        struct v4l2_device      v4l2_dev;
  53        struct v4l2_subdev      *v4l2_subdev;
  54        struct mutex            lock;
  55        struct i2c_adapter      i2c_adapter;
  56
  57        u8                      *buffer;
  58};
  59
  60static inline struct si4713_usb_device *to_si4713_dev(struct v4l2_device *v4l2_dev)
  61{
  62        return container_of(v4l2_dev, struct si4713_usb_device, v4l2_dev);
  63}
  64
  65static int vidioc_querycap(struct file *file, void *priv,
  66                                        struct v4l2_capability *v)
  67{
  68        struct si4713_usb_device *radio = video_drvdata(file);
  69
  70        strlcpy(v->driver, "radio-usb-si4713", sizeof(v->driver));
  71        strlcpy(v->card, "Si4713 FM Transmitter", sizeof(v->card));
  72        usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
  73        v->device_caps = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
  74        v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  75
  76        return 0;
  77}
  78
  79static int vidioc_g_modulator(struct file *file, void *priv,
  80                                struct v4l2_modulator *vm)
  81{
  82        struct si4713_usb_device *radio = video_drvdata(file);
  83
  84        return v4l2_subdev_call(radio->v4l2_subdev, tuner, g_modulator, vm);
  85}
  86
  87static int vidioc_s_modulator(struct file *file, void *priv,
  88                                const struct v4l2_modulator *vm)
  89{
  90        struct si4713_usb_device *radio = video_drvdata(file);
  91
  92        return v4l2_subdev_call(radio->v4l2_subdev, tuner, s_modulator, vm);
  93}
  94
  95static int vidioc_s_frequency(struct file *file, void *priv,
  96                                const struct v4l2_frequency *vf)
  97{
  98        struct si4713_usb_device *radio = video_drvdata(file);
  99
 100        return v4l2_subdev_call(radio->v4l2_subdev, tuner, s_frequency, vf);
 101}
 102
 103static int vidioc_g_frequency(struct file *file, void *priv,
 104                                struct v4l2_frequency *vf)
 105{
 106        struct si4713_usb_device *radio = video_drvdata(file);
 107
 108        return v4l2_subdev_call(radio->v4l2_subdev, tuner, g_frequency, vf);
 109}
 110
 111static const struct v4l2_ioctl_ops usb_si4713_ioctl_ops = {
 112        .vidioc_querycap          = vidioc_querycap,
 113        .vidioc_g_modulator       = vidioc_g_modulator,
 114        .vidioc_s_modulator       = vidioc_s_modulator,
 115        .vidioc_g_frequency       = vidioc_g_frequency,
 116        .vidioc_s_frequency       = vidioc_s_frequency,
 117        .vidioc_log_status        = v4l2_ctrl_log_status,
 118        .vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
 119        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
 120};
 121
 122/* File system interface */
 123static const struct v4l2_file_operations usb_si4713_fops = {
 124        .owner          = THIS_MODULE,
 125        .open           = v4l2_fh_open,
 126        .release        = v4l2_fh_release,
 127        .poll           = v4l2_ctrl_poll,
 128        .unlocked_ioctl = video_ioctl2,
 129};
 130
 131static void usb_si4713_video_device_release(struct v4l2_device *v4l2_dev)
 132{
 133        struct si4713_usb_device *radio = to_si4713_dev(v4l2_dev);
 134        struct i2c_adapter *adapter = &radio->i2c_adapter;
 135
 136        i2c_del_adapter(adapter);
 137        v4l2_device_unregister(&radio->v4l2_dev);
 138        kfree(radio->buffer);
 139        kfree(radio);
 140}
 141
 142/*
 143 * This command sequence emulates the behaviour of the Windows driver.
 144 * The structure of these commands was determined by sniffing the
 145 * usb traffic of the device during startup.
 146 * Most likely, these commands make some queries to the device.
 147 * Commands are sent to enquire parameters like the bus mode,
 148 * component revision, boot mode, the device serial number etc.
 149 *
 150 * These commands are necessary to be sent in this order during startup.
 151 * The device fails to powerup if these commands are not sent.
 152 *
 153 * The complete list of startup commands is given in the start_seq table below.
 154 */
 155static int si4713_send_startup_command(struct si4713_usb_device *radio)
 156{
 157        unsigned long until_jiffies = jiffies + usecs_to_jiffies(USB_RESP_TIMEOUT) + 1;
 158        u8 *buffer = radio->buffer;
 159        int retval;
 160
 161        /* send the command */
 162        retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
 163                                        0x09, 0x21, 0x033f, 0, radio->buffer,
 164                                        BUFFER_LENGTH, USB_TIMEOUT);
 165        if (retval < 0)
 166                return retval;
 167
 168        for (;;) {
 169                /* receive the response */
 170                retval = usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
 171                                0x01, 0xa1, 0x033f, 0, radio->buffer,
 172                                BUFFER_LENGTH, USB_TIMEOUT);
 173                if (retval < 0)
 174                        return retval;
 175                if (!radio->buffer[1]) {
 176                        /* USB traffic sniffing showed that some commands require
 177                         * additional checks. */
 178                        switch (buffer[1]) {
 179                        case 0x32:
 180                                if (radio->buffer[2] == 0)
 181                                        return 0;
 182                                break;
 183                        case 0x14:
 184                        case 0x12:
 185                                if (radio->buffer[2] & SI4713_CTS)
 186                                        return 0;
 187                                break;
 188                        case 0x06:
 189                                if ((radio->buffer[2] & SI4713_CTS) && radio->buffer[9] == 0x08)
 190                                        return 0;
 191                                break;
 192                        default:
 193                                return 0;
 194                        }
 195                }
 196                if (time_is_before_jiffies(until_jiffies))
 197                        return -EIO;
 198                msleep(3);
 199        }
 200
 201        return retval;
 202}
 203
 204struct si4713_start_seq_table {
 205        int len;
 206        u8 payload[8];
 207};
 208
 209/*
 210 * Some of the startup commands that could be recognized are :
 211 * (0x03): Get serial number of the board (Response : CB000-00-00)
 212 * (0x06, 0x03, 0x03, 0x08, 0x01, 0x0f) : Get Component revision
 213 */
 214static const struct si4713_start_seq_table start_seq[] = {
 215
 216        { 1, { 0x03 } },
 217        { 2, { 0x32, 0x7f } },
 218        { 6, { 0x06, 0x03, 0x03, 0x08, 0x01, 0x0f } },
 219        { 2, { 0x14, 0x02 } },
 220        { 2, { 0x09, 0x90 } },
 221        { 3, { 0x08, 0x90, 0xfa } },
 222        { 2, { 0x36, 0x01 } },
 223        { 2, { 0x05, 0x03 } },
 224        { 7, { 0x06, 0x00, 0x06, 0x0e, 0x01, 0x0f, 0x05 } },
 225        { 1, { 0x12 } },
 226        /* Commands that are sent after pressing the 'Initialize'
 227                button in the windows application */
 228        { 1, { 0x03 } },
 229        { 1, { 0x01 } },
 230        { 2, { 0x09, 0x90 } },
 231        { 3, { 0x08, 0x90, 0xfa } },
 232        { 1, { 0x34 } },
 233        { 2, { 0x35, 0x01 } },
 234        { 2, { 0x36, 0x01 } },
 235        { 2, { 0x30, 0x09 } },
 236        { 4, { 0x30, 0x06, 0x00, 0xe2 } },
 237        { 3, { 0x31, 0x01, 0x30 } },
 238        { 3, { 0x31, 0x04, 0x09 } },
 239        { 2, { 0x05, 0x02 } },
 240        { 6, { 0x06, 0x03, 0x03, 0x08, 0x01, 0x0f } },
 241};
 242
 243static int si4713_start_seq(struct si4713_usb_device *radio)
 244{
 245        int retval = 0;
 246        int i;
 247
 248        radio->buffer[0] = 0x3f;
 249
 250        for (i = 0; i < ARRAY_SIZE(start_seq); i++) {
 251                int len = start_seq[i].len;
 252                const u8 *payload = start_seq[i].payload;
 253
 254                memcpy(radio->buffer + 1, payload, len);
 255                memset(radio->buffer + len + 1, 0, BUFFER_LENGTH - 1 - len);
 256                retval = si4713_send_startup_command(radio);
 257        }
 258
 259        return retval;
 260}
 261
 262static struct i2c_board_info si4713_board_info = {
 263        I2C_BOARD_INFO("si4713", SI4713_I2C_ADDR_BUSEN_HIGH),
 264};
 265
 266struct si4713_command_table {
 267        int command_id;
 268        u8 payload[8];
 269};
 270
 271/*
 272 * Structure of a command :
 273 *      Byte 1 : 0x3f (always)
 274 *      Byte 2 : 0x06 (send a command)
 275 *      Byte 3 : Unknown
 276 *      Byte 4 : Number of arguments + 1 (for the command byte)
 277 *      Byte 5 : Number of response bytes
 278 */
 279static struct si4713_command_table command_table[] = {
 280
 281        { SI4713_CMD_POWER_UP,          { 0x00, SI4713_PWUP_NARGS + 1, SI4713_PWUP_NRESP} },
 282        { SI4713_CMD_GET_REV,           { 0x03, 0x01, SI4713_GETREV_NRESP } },
 283        { SI4713_CMD_POWER_DOWN,        { 0x00, 0x01, SI4713_PWDN_NRESP} },
 284        { SI4713_CMD_SET_PROPERTY,      { 0x00, SI4713_SET_PROP_NARGS + 1, SI4713_SET_PROP_NRESP } },
 285        { SI4713_CMD_GET_PROPERTY,      { 0x00, SI4713_GET_PROP_NARGS + 1, SI4713_GET_PROP_NRESP } },
 286        { SI4713_CMD_TX_TUNE_FREQ,      { 0x03, SI4713_TXFREQ_NARGS + 1, SI4713_TXFREQ_NRESP } },
 287        { SI4713_CMD_TX_TUNE_POWER,     { 0x03, SI4713_TXPWR_NARGS + 1, SI4713_TXPWR_NRESP } },
 288        { SI4713_CMD_TX_TUNE_MEASURE,   { 0x03, SI4713_TXMEA_NARGS + 1, SI4713_TXMEA_NRESP } },
 289        { SI4713_CMD_TX_TUNE_STATUS,    { 0x00, SI4713_TXSTATUS_NARGS + 1, SI4713_TXSTATUS_NRESP } },
 290        { SI4713_CMD_TX_ASQ_STATUS,     { 0x03, SI4713_ASQSTATUS_NARGS + 1, SI4713_ASQSTATUS_NRESP } },
 291        { SI4713_CMD_GET_INT_STATUS,    { 0x03, 0x01, SI4713_GET_STATUS_NRESP } },
 292        { SI4713_CMD_TX_RDS_BUFF,       { 0x03, SI4713_RDSBUFF_NARGS + 1, SI4713_RDSBUFF_NRESP } },
 293        { SI4713_CMD_TX_RDS_PS,         { 0x00, SI4713_RDSPS_NARGS + 1, SI4713_RDSPS_NRESP } },
 294};
 295
 296static int send_command(struct si4713_usb_device *radio, u8 *payload, char *data, int len)
 297{
 298        int retval;
 299
 300        radio->buffer[0] = 0x3f;
 301        radio->buffer[1] = 0x06;
 302
 303        memcpy(radio->buffer + 2, payload, 3);
 304        memcpy(radio->buffer + 5, data, len);
 305        memset(radio->buffer + 5 + len, 0, BUFFER_LENGTH - 5 - len);
 306
 307        /* send the command */
 308        retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
 309                                        0x09, 0x21, 0x033f, 0, radio->buffer,
 310                                        BUFFER_LENGTH, USB_TIMEOUT);
 311
 312        return retval < 0 ? retval : 0;
 313}
 314
 315static int si4713_i2c_read(struct si4713_usb_device *radio, char *data, int len)
 316{
 317        unsigned long until_jiffies = jiffies + usecs_to_jiffies(USB_RESP_TIMEOUT) + 1;
 318        int retval;
 319
 320        /* receive the response */
 321        for (;;) {
 322                retval = usb_control_msg(radio->usbdev,
 323                                        usb_rcvctrlpipe(radio->usbdev, 0),
 324                                        0x01, 0xa1, 0x033f, 0, radio->buffer,
 325                                        BUFFER_LENGTH, USB_TIMEOUT);
 326                if (retval < 0)
 327                        return retval;
 328
 329                /*
 330                 * Check that we get a valid reply back (buffer[1] == 0) and
 331                 * that CTS is set before returning, otherwise we wait and try
 332                 * again. The i2c driver also does the CTS check, but the timeouts
 333                 * used there are much too small for this USB driver, so we wait
 334                 * for it here.
 335                 */
 336                if (radio->buffer[1] == 0 && (radio->buffer[2] & SI4713_CTS)) {
 337                        memcpy(data, radio->buffer + 2, len);
 338                        return 0;
 339                }
 340                if (time_is_before_jiffies(until_jiffies)) {
 341                        /* Zero the status value, ensuring CTS isn't set */
 342                        data[0] = 0;
 343                        return 0;
 344                }
 345                msleep(3);
 346        }
 347}
 348
 349static int si4713_i2c_write(struct si4713_usb_device *radio, char *data, int len)
 350{
 351        int retval = -EINVAL;
 352        int i;
 353
 354        if (len > BUFFER_LENGTH - 5)
 355                return -EINVAL;
 356
 357        for (i = 0; i < ARRAY_SIZE(command_table); i++) {
 358                if (data[0] == command_table[i].command_id)
 359                        retval = send_command(radio, command_table[i].payload,
 360                                                data, len);
 361        }
 362
 363        return retval < 0 ? retval : 0;
 364}
 365
 366static int si4713_transfer(struct i2c_adapter *i2c_adapter,
 367                                struct i2c_msg *msgs, int num)
 368{
 369        struct si4713_usb_device *radio = i2c_get_adapdata(i2c_adapter);
 370        int retval = -EINVAL;
 371        int i;
 372
 373        if (num <= 0)
 374                return 0;
 375
 376        for (i = 0; i < num; i++) {
 377                if (msgs[i].flags & I2C_M_RD)
 378                        retval = si4713_i2c_read(radio, msgs[i].buf, msgs[i].len);
 379                else
 380                        retval = si4713_i2c_write(radio, msgs[i].buf, msgs[i].len);
 381                if (retval)
 382                        break;
 383        }
 384
 385        return retval ? retval : num;
 386}
 387
 388static u32 si4713_functionality(struct i2c_adapter *adapter)
 389{
 390        return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
 391}
 392
 393static const struct i2c_algorithm si4713_algo = {
 394        .master_xfer   = si4713_transfer,
 395        .functionality = si4713_functionality,
 396};
 397
 398/* This name value shows up in the sysfs filename associated
 399                with this I2C adapter */
 400static const struct i2c_adapter si4713_i2c_adapter_template = {
 401        .name   = "si4713-i2c",
 402        .owner  = THIS_MODULE,
 403        .algo   = &si4713_algo,
 404};
 405
 406static int si4713_register_i2c_adapter(struct si4713_usb_device *radio)
 407{
 408        radio->i2c_adapter = si4713_i2c_adapter_template;
 409        /* set up sysfs linkage to our parent device */
 410        radio->i2c_adapter.dev.parent = &radio->usbdev->dev;
 411        i2c_set_adapdata(&radio->i2c_adapter, radio);
 412
 413        return i2c_add_adapter(&radio->i2c_adapter);
 414}
 415
 416/* check if the device is present and register with v4l and usb if it is */
 417static int usb_si4713_probe(struct usb_interface *intf,
 418                                const struct usb_device_id *id)
 419{
 420        struct si4713_usb_device *radio;
 421        struct i2c_adapter *adapter;
 422        struct v4l2_subdev *sd;
 423        int retval = -ENOMEM;
 424
 425        dev_info(&intf->dev, "Si4713 development board discovered: (%04X:%04X)\n",
 426                        id->idVendor, id->idProduct);
 427
 428        /* Initialize local device structure */
 429        radio = kzalloc(sizeof(struct si4713_usb_device), GFP_KERNEL);
 430        if (radio)
 431                radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
 432
 433        if (!radio || !radio->buffer) {
 434                dev_err(&intf->dev, "kmalloc for si4713_usb_device failed\n");
 435                kfree(radio);
 436                return -ENOMEM;
 437        }
 438
 439        mutex_init(&radio->lock);
 440
 441        radio->usbdev = interface_to_usbdev(intf);
 442        radio->intf = intf;
 443        usb_set_intfdata(intf, &radio->v4l2_dev);
 444
 445        retval = si4713_start_seq(radio);
 446        if (retval < 0)
 447                goto err_v4l2;
 448
 449        retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
 450        if (retval < 0) {
 451                dev_err(&intf->dev, "couldn't register v4l2_device\n");
 452                goto err_v4l2;
 453        }
 454
 455        retval = si4713_register_i2c_adapter(radio);
 456        if (retval < 0) {
 457                dev_err(&intf->dev, "could not register i2c device\n");
 458                goto err_i2cdev;
 459        }
 460
 461        adapter = &radio->i2c_adapter;
 462        sd = v4l2_i2c_new_subdev_board(&radio->v4l2_dev, adapter,
 463                                          &si4713_board_info, NULL);
 464        radio->v4l2_subdev = sd;
 465        if (!sd) {
 466                dev_err(&intf->dev, "cannot get v4l2 subdevice\n");
 467                retval = -ENODEV;
 468                goto del_adapter;
 469        }
 470
 471        radio->vdev.ctrl_handler = sd->ctrl_handler;
 472        radio->v4l2_dev.release = usb_si4713_video_device_release;
 473        strlcpy(radio->vdev.name, radio->v4l2_dev.name,
 474                sizeof(radio->vdev.name));
 475        radio->vdev.v4l2_dev = &radio->v4l2_dev;
 476        radio->vdev.fops = &usb_si4713_fops;
 477        radio->vdev.ioctl_ops = &usb_si4713_ioctl_ops;
 478        radio->vdev.lock = &radio->lock;
 479        radio->vdev.release = video_device_release_empty;
 480        radio->vdev.vfl_dir = VFL_DIR_TX;
 481
 482        video_set_drvdata(&radio->vdev, radio);
 483
 484        retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
 485        if (retval < 0) {
 486                dev_err(&intf->dev, "could not register video device\n");
 487                goto del_adapter;
 488        }
 489
 490        dev_info(&intf->dev, "V4L2 device registered as %s\n",
 491                        video_device_node_name(&radio->vdev));
 492
 493        return 0;
 494
 495del_adapter:
 496        i2c_del_adapter(adapter);
 497err_i2cdev:
 498        v4l2_device_unregister(&radio->v4l2_dev);
 499err_v4l2:
 500        kfree(radio->buffer);
 501        kfree(radio);
 502        return retval;
 503}
 504
 505static void usb_si4713_disconnect(struct usb_interface *intf)
 506{
 507        struct si4713_usb_device *radio = to_si4713_dev(usb_get_intfdata(intf));
 508
 509        dev_info(&intf->dev, "Si4713 development board now disconnected\n");
 510
 511        mutex_lock(&radio->lock);
 512        usb_set_intfdata(intf, NULL);
 513        video_unregister_device(&radio->vdev);
 514        v4l2_device_disconnect(&radio->v4l2_dev);
 515        mutex_unlock(&radio->lock);
 516        v4l2_device_put(&radio->v4l2_dev);
 517}
 518
 519/* USB subsystem interface */
 520static struct usb_driver usb_si4713_driver = {
 521        .name                   = "radio-usb-si4713",
 522        .probe                  = usb_si4713_probe,
 523        .disconnect             = usb_si4713_disconnect,
 524        .id_table               = usb_si4713_usb_device_table,
 525};
 526
 527module_usb_driver(usb_si4713_driver);
 528