linux/drivers/media/radio/si470x/radio-si470x-usb.c
<<
>>
Prefs
   1/*
   2 *  drivers/media/radio/si470x/radio-si470x-usb.c
   3 *
   4 *  USB driver for radios with Silicon Labs Si470x FM Radio Receivers
   5 *
   6 *  Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 */
  18
  19
  20/*
  21 * ToDo:
  22 * - add firmware download/update support
  23 */
  24
  25
  26/* driver definitions */
  27#define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
  28#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
  29#define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
  30#define DRIVER_VERSION "1.0.10"
  31
  32/* kernel includes */
  33#include <linux/usb.h>
  34#include <linux/hid.h>
  35#include <linux/slab.h>
  36
  37#include "radio-si470x.h"
  38
  39
  40/* USB Device ID List */
  41static const struct usb_device_id si470x_usb_driver_id_table[] = {
  42        /* Silicon Labs USB FM Radio Reference Design */
  43        { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
  44        /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
  45        { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) },
  46        /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */
  47        { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) },
  48        /* Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) */
  49        { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) },
  50        /* Axentia ALERT FM USB Receiver */
  51        { USB_DEVICE_AND_INTERFACE_INFO(0x12cf, 0x7111, USB_CLASS_HID, 0, 0) },
  52        /* Terminating entry */
  53        { }
  54};
  55MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
  56
  57
  58
  59/**************************************************************************
  60 * Module Parameters
  61 **************************************************************************/
  62
  63/* Radio Nr */
  64static int radio_nr = -1;
  65module_param(radio_nr, int, 0444);
  66MODULE_PARM_DESC(radio_nr, "Radio Nr");
  67
  68/* USB timeout */
  69static unsigned int usb_timeout = 500;
  70module_param(usb_timeout, uint, 0644);
  71MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
  72
  73/* RDS buffer blocks */
  74static unsigned int rds_buf = 100;
  75module_param(rds_buf, uint, 0444);
  76MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
  77
  78/* RDS maximum block errors */
  79static unsigned short max_rds_errors = 1;
  80/* 0 means   0  errors requiring correction */
  81/* 1 means 1-2  errors requiring correction (used by original USBRadio.exe) */
  82/* 2 means 3-5  errors requiring correction */
  83/* 3 means   6+ errors or errors in checkword, correction not possible */
  84module_param(max_rds_errors, ushort, 0644);
  85MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
  86
  87
  88
  89/**************************************************************************
  90 * USB HID Reports
  91 **************************************************************************/
  92
  93/* Reports 1-16 give direct read/write access to the 16 Si470x registers */
  94/* with the (REPORT_ID - 1) corresponding to the register address across USB */
  95/* endpoint 0 using GET_REPORT and SET_REPORT */
  96#define REGISTER_REPORT_SIZE    (RADIO_REGISTER_SIZE + 1)
  97#define REGISTER_REPORT(reg)    ((reg) + 1)
  98
  99/* Report 17 gives direct read/write access to the entire Si470x register */
 100/* map across endpoint 0 using GET_REPORT and SET_REPORT */
 101#define ENTIRE_REPORT_SIZE      (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
 102#define ENTIRE_REPORT           17
 103
 104/* Report 18 is used to send the lowest 6 Si470x registers up the HID */
 105/* interrupt endpoint 1 to Windows every 20 milliseconds for status */
 106#define RDS_REPORT_SIZE         (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
 107#define RDS_REPORT              18
 108
 109/* Report 19: LED state */
 110#define LED_REPORT_SIZE         3
 111#define LED_REPORT              19
 112
 113/* Report 19: stream */
 114#define STREAM_REPORT_SIZE      3
 115#define STREAM_REPORT           19
 116
 117/* Report 20: scratch */
 118#define SCRATCH_PAGE_SIZE       63
 119#define SCRATCH_REPORT_SIZE     (SCRATCH_PAGE_SIZE + 1)
 120#define SCRATCH_REPORT          20
 121
 122/* Reports 19-22: flash upgrade of the C8051F321 */
 123#define WRITE_REPORT_SIZE       4
 124#define WRITE_REPORT            19
 125#define FLASH_REPORT_SIZE       64
 126#define FLASH_REPORT            20
 127#define CRC_REPORT_SIZE         3
 128#define CRC_REPORT              21
 129#define RESPONSE_REPORT_SIZE    2
 130#define RESPONSE_REPORT         22
 131
 132/* Report 23: currently unused, but can accept 60 byte reports on the HID */
 133/* interrupt out endpoint 2 every 1 millisecond */
 134#define UNUSED_REPORT           23
 135
 136#define MAX_REPORT_SIZE         64
 137
 138
 139
 140/**************************************************************************
 141 * Software/Hardware Versions from Scratch Page
 142 **************************************************************************/
 143#define RADIO_HW_VERSION                        1
 144
 145
 146
 147/**************************************************************************
 148 * LED State Definitions
 149 **************************************************************************/
 150#define LED_COMMAND             0x35
 151
 152#define NO_CHANGE_LED           0x00
 153#define ALL_COLOR_LED           0x01    /* streaming state */
 154#define BLINK_GREEN_LED         0x02    /* connect state */
 155#define BLINK_RED_LED           0x04
 156#define BLINK_ORANGE_LED        0x10    /* disconnect state */
 157#define SOLID_GREEN_LED         0x20    /* tuning/seeking state */
 158#define SOLID_RED_LED           0x40    /* bootload state */
 159#define SOLID_ORANGE_LED        0x80
 160
 161
 162
 163/**************************************************************************
 164 * Stream State Definitions
 165 **************************************************************************/
 166#define STREAM_COMMAND  0x36
 167#define STREAM_VIDPID   0x00
 168#define STREAM_AUDIO    0xff
 169
 170
 171
 172/**************************************************************************
 173 * Bootloader / Flash Commands
 174 **************************************************************************/
 175
 176/* unique id sent to bootloader and required to put into a bootload state */
 177#define UNIQUE_BL_ID            0x34
 178
 179/* mask for the flash data */
 180#define FLASH_DATA_MASK         0x55
 181
 182/* bootloader commands */
 183#define GET_SW_VERSION_COMMAND  0x00
 184#define SET_PAGE_COMMAND        0x01
 185#define ERASE_PAGE_COMMAND      0x02
 186#define WRITE_PAGE_COMMAND      0x03
 187#define CRC_ON_PAGE_COMMAND     0x04
 188#define READ_FLASH_BYTE_COMMAND 0x05
 189#define RESET_DEVICE_COMMAND    0x06
 190#define GET_HW_VERSION_COMMAND  0x07
 191#define BLANK                   0xff
 192
 193/* bootloader command responses */
 194#define COMMAND_OK              0x01
 195#define COMMAND_FAILED          0x02
 196#define COMMAND_PENDING         0x03
 197
 198
 199
 200/**************************************************************************
 201 * General Driver Functions - REGISTER_REPORTs
 202 **************************************************************************/
 203
 204/*
 205 * si470x_get_report - receive a HID report
 206 */
 207static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
 208{
 209        unsigned char *report = buf;
 210        int retval;
 211
 212        retval = usb_control_msg(radio->usbdev,
 213                usb_rcvctrlpipe(radio->usbdev, 0),
 214                HID_REQ_GET_REPORT,
 215                USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
 216                report[0], 2,
 217                buf, size, usb_timeout);
 218
 219        if (retval < 0)
 220                dev_warn(&radio->intf->dev,
 221                        "si470x_get_report: usb_control_msg returned %d\n",
 222                        retval);
 223        return retval;
 224}
 225
 226
 227/*
 228 * si470x_set_report - send a HID report
 229 */
 230static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
 231{
 232        unsigned char *report = buf;
 233        int retval;
 234
 235        retval = usb_control_msg(radio->usbdev,
 236                usb_sndctrlpipe(radio->usbdev, 0),
 237                HID_REQ_SET_REPORT,
 238                USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
 239                report[0], 2,
 240                buf, size, usb_timeout);
 241
 242        if (retval < 0)
 243                dev_warn(&radio->intf->dev,
 244                        "si470x_set_report: usb_control_msg returned %d\n",
 245                        retval);
 246        return retval;
 247}
 248
 249
 250/*
 251 * si470x_get_register - read register
 252 */
 253int si470x_get_register(struct si470x_device *radio, int regnr)
 254{
 255        int retval;
 256
 257        radio->usb_buf[0] = REGISTER_REPORT(regnr);
 258
 259        retval = si470x_get_report(radio, radio->usb_buf, REGISTER_REPORT_SIZE);
 260
 261        if (retval >= 0)
 262                radio->registers[regnr] = get_unaligned_be16(&radio->usb_buf[1]);
 263
 264        return (retval < 0) ? -EINVAL : 0;
 265}
 266
 267
 268/*
 269 * si470x_set_register - write register
 270 */
 271int si470x_set_register(struct si470x_device *radio, int regnr)
 272{
 273        int retval;
 274
 275        radio->usb_buf[0] = REGISTER_REPORT(regnr);
 276        put_unaligned_be16(radio->registers[regnr], &radio->usb_buf[1]);
 277
 278        retval = si470x_set_report(radio, radio->usb_buf, REGISTER_REPORT_SIZE);
 279
 280        return (retval < 0) ? -EINVAL : 0;
 281}
 282
 283
 284
 285/**************************************************************************
 286 * General Driver Functions - ENTIRE_REPORT
 287 **************************************************************************/
 288
 289/*
 290 * si470x_get_all_registers - read entire registers
 291 */
 292static int si470x_get_all_registers(struct si470x_device *radio)
 293{
 294        int retval;
 295        unsigned char regnr;
 296
 297        radio->usb_buf[0] = ENTIRE_REPORT;
 298
 299        retval = si470x_get_report(radio, radio->usb_buf, ENTIRE_REPORT_SIZE);
 300
 301        if (retval >= 0)
 302                for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
 303                        radio->registers[regnr] = get_unaligned_be16(
 304                                &radio->usb_buf[regnr * RADIO_REGISTER_SIZE + 1]);
 305
 306        return (retval < 0) ? -EINVAL : 0;
 307}
 308
 309
 310
 311/**************************************************************************
 312 * General Driver Functions - LED_REPORT
 313 **************************************************************************/
 314
 315/*
 316 * si470x_set_led_state - sets the led state
 317 */
 318static int si470x_set_led_state(struct si470x_device *radio,
 319                unsigned char led_state)
 320{
 321        int retval;
 322
 323        radio->usb_buf[0] = LED_REPORT;
 324        radio->usb_buf[1] = LED_COMMAND;
 325        radio->usb_buf[2] = led_state;
 326
 327        retval = si470x_set_report(radio, radio->usb_buf, LED_REPORT_SIZE);
 328
 329        return (retval < 0) ? -EINVAL : 0;
 330}
 331
 332
 333
 334/**************************************************************************
 335 * General Driver Functions - SCRATCH_REPORT
 336 **************************************************************************/
 337
 338/*
 339 * si470x_get_scratch_versions - gets the scratch page and version infos
 340 */
 341static int si470x_get_scratch_page_versions(struct si470x_device *radio)
 342{
 343        int retval;
 344
 345        radio->usb_buf[0] = SCRATCH_REPORT;
 346
 347        retval = si470x_get_report(radio, radio->usb_buf, SCRATCH_REPORT_SIZE);
 348
 349        if (retval < 0)
 350                dev_warn(&radio->intf->dev, "si470x_get_scratch: si470x_get_report returned %d\n",
 351                         retval);
 352        else {
 353                radio->software_version = radio->usb_buf[1];
 354                radio->hardware_version = radio->usb_buf[2];
 355        }
 356
 357        return (retval < 0) ? -EINVAL : 0;
 358}
 359
 360
 361
 362/**************************************************************************
 363 * RDS Driver Functions
 364 **************************************************************************/
 365
 366/*
 367 * si470x_int_in_callback - rds callback and processing function
 368 *
 369 * TODO: do we need to use mutex locks in some sections?
 370 */
 371static void si470x_int_in_callback(struct urb *urb)
 372{
 373        struct si470x_device *radio = urb->context;
 374        int retval;
 375        unsigned char regnr;
 376        unsigned char blocknum;
 377        unsigned short bler; /* rds block errors */
 378        unsigned short rds;
 379        unsigned char tmpbuf[3];
 380
 381        if (urb->status) {
 382                if (urb->status == -ENOENT ||
 383                                urb->status == -ECONNRESET ||
 384                                urb->status == -ESHUTDOWN) {
 385                        return;
 386                } else {
 387                        dev_warn(&radio->intf->dev,
 388                         "non-zero urb status (%d)\n", urb->status);
 389                        goto resubmit; /* Maybe we can recover. */
 390                }
 391        }
 392
 393        /* Sometimes the device returns len 0 packets */
 394        if (urb->actual_length != RDS_REPORT_SIZE)
 395                goto resubmit;
 396
 397        radio->registers[STATUSRSSI] =
 398                get_unaligned_be16(&radio->int_in_buffer[1]);
 399
 400        if (radio->registers[STATUSRSSI] & STATUSRSSI_STC)
 401                complete(&radio->completion);
 402
 403        if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)) {
 404                /* Update RDS registers with URB data */
 405                for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++)
 406                        radio->registers[STATUSRSSI + regnr] =
 407                            get_unaligned_be16(&radio->int_in_buffer[
 408                                regnr * RADIO_REGISTER_SIZE + 1]);
 409                /* get rds blocks */
 410                if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
 411                        /* No RDS group ready, better luck next time */
 412                        goto resubmit;
 413                }
 414                if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
 415                        /* RDS decoder not synchronized */
 416                        goto resubmit;
 417                }
 418                for (blocknum = 0; blocknum < 4; blocknum++) {
 419                        switch (blocknum) {
 420                        default:
 421                                bler = (radio->registers[STATUSRSSI] &
 422                                                STATUSRSSI_BLERA) >> 9;
 423                                rds = radio->registers[RDSA];
 424                                break;
 425                        case 1:
 426                                bler = (radio->registers[READCHAN] &
 427                                                READCHAN_BLERB) >> 14;
 428                                rds = radio->registers[RDSB];
 429                                break;
 430                        case 2:
 431                                bler = (radio->registers[READCHAN] &
 432                                                READCHAN_BLERC) >> 12;
 433                                rds = radio->registers[RDSC];
 434                                break;
 435                        case 3:
 436                                bler = (radio->registers[READCHAN] &
 437                                                READCHAN_BLERD) >> 10;
 438                                rds = radio->registers[RDSD];
 439                                break;
 440                        }
 441
 442                        /* Fill the V4L2 RDS buffer */
 443                        put_unaligned_le16(rds, &tmpbuf);
 444                        tmpbuf[2] = blocknum;           /* offset name */
 445                        tmpbuf[2] |= blocknum << 3;     /* received offset */
 446                        if (bler > max_rds_errors)
 447                                tmpbuf[2] |= 0x80; /* uncorrectable errors */
 448                        else if (bler > 0)
 449                                tmpbuf[2] |= 0x40; /* corrected error(s) */
 450
 451                        /* copy RDS block to internal buffer */
 452                        memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
 453                        radio->wr_index += 3;
 454
 455                        /* wrap write pointer */
 456                        if (radio->wr_index >= radio->buf_size)
 457                                radio->wr_index = 0;
 458
 459                        /* check for overflow */
 460                        if (radio->wr_index == radio->rd_index) {
 461                                /* increment and wrap read pointer */
 462                                radio->rd_index += 3;
 463                                if (radio->rd_index >= radio->buf_size)
 464                                        radio->rd_index = 0;
 465                        }
 466                }
 467                if (radio->wr_index != radio->rd_index)
 468                        wake_up_interruptible(&radio->read_queue);
 469        }
 470
 471resubmit:
 472        /* Resubmit if we're still running. */
 473        if (radio->int_in_running && radio->usbdev) {
 474                retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC);
 475                if (retval) {
 476                        dev_warn(&radio->intf->dev,
 477                               "resubmitting urb failed (%d)", retval);
 478                        radio->int_in_running = 0;
 479                }
 480        }
 481        radio->status_rssi_auto_update = radio->int_in_running;
 482}
 483
 484
 485int si470x_fops_open(struct file *file)
 486{
 487        return v4l2_fh_open(file);
 488}
 489
 490int si470x_fops_release(struct file *file)
 491{
 492        return v4l2_fh_release(file);
 493}
 494
 495static void si470x_usb_release(struct v4l2_device *v4l2_dev)
 496{
 497        struct si470x_device *radio =
 498                container_of(v4l2_dev, struct si470x_device, v4l2_dev);
 499
 500        usb_free_urb(radio->int_in_urb);
 501        v4l2_ctrl_handler_free(&radio->hdl);
 502        v4l2_device_unregister(&radio->v4l2_dev);
 503        kfree(radio->int_in_buffer);
 504        kfree(radio->buffer);
 505        kfree(radio->usb_buf);
 506        kfree(radio);
 507}
 508
 509
 510/**************************************************************************
 511 * Video4Linux Interface
 512 **************************************************************************/
 513
 514/*
 515 * si470x_vidioc_querycap - query device capabilities
 516 */
 517int si470x_vidioc_querycap(struct file *file, void *priv,
 518                struct v4l2_capability *capability)
 519{
 520        struct si470x_device *radio = video_drvdata(file);
 521
 522        strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
 523        strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
 524        usb_make_path(radio->usbdev, capability->bus_info,
 525                        sizeof(capability->bus_info));
 526        capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE |
 527                V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
 528        capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
 529        return 0;
 530}
 531
 532
 533static int si470x_start_usb(struct si470x_device *radio)
 534{
 535        int retval;
 536
 537        /* initialize interrupt urb */
 538        usb_fill_int_urb(radio->int_in_urb, radio->usbdev,
 539                        usb_rcvintpipe(radio->usbdev,
 540                                radio->int_in_endpoint->bEndpointAddress),
 541                        radio->int_in_buffer,
 542                        le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize),
 543                        si470x_int_in_callback,
 544                        radio,
 545                        radio->int_in_endpoint->bInterval);
 546
 547        radio->int_in_running = 1;
 548        mb();
 549
 550        retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL);
 551        if (retval) {
 552                dev_info(&radio->intf->dev,
 553                                "submitting int urb failed (%d)\n", retval);
 554                radio->int_in_running = 0;
 555        }
 556        radio->status_rssi_auto_update = radio->int_in_running;
 557
 558        /* start radio */
 559        retval = si470x_start(radio);
 560        if (retval < 0)
 561                return retval;
 562
 563        v4l2_ctrl_handler_setup(&radio->hdl);
 564
 565        return retval;
 566}
 567
 568/**************************************************************************
 569 * USB Interface
 570 **************************************************************************/
 571
 572/*
 573 * si470x_usb_driver_probe - probe for the device
 574 */
 575static int si470x_usb_driver_probe(struct usb_interface *intf,
 576                const struct usb_device_id *id)
 577{
 578        struct si470x_device *radio;
 579        struct usb_host_interface *iface_desc;
 580        struct usb_endpoint_descriptor *endpoint;
 581        int i, int_end_size, retval;
 582        unsigned char version_warning = 0;
 583
 584        /* private data allocation and initialization */
 585        radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
 586        if (!radio) {
 587                retval = -ENOMEM;
 588                goto err_initial;
 589        }
 590        radio->usb_buf = kmalloc(MAX_REPORT_SIZE, GFP_KERNEL);
 591        if (radio->usb_buf == NULL) {
 592                retval = -ENOMEM;
 593                goto err_radio;
 594        }
 595        radio->usbdev = interface_to_usbdev(intf);
 596        radio->intf = intf;
 597        radio->band = 1; /* Default to 76 - 108 MHz */
 598        mutex_init(&radio->lock);
 599        init_completion(&radio->completion);
 600
 601        iface_desc = intf->cur_altsetting;
 602
 603        /* Set up interrupt endpoint information. */
 604        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
 605                endpoint = &iface_desc->endpoint[i].desc;
 606                if (usb_endpoint_is_int_in(endpoint))
 607                        radio->int_in_endpoint = endpoint;
 608        }
 609        if (!radio->int_in_endpoint) {
 610                dev_info(&intf->dev, "could not find interrupt in endpoint\n");
 611                retval = -EIO;
 612                goto err_usbbuf;
 613        }
 614
 615        int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize);
 616
 617        radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL);
 618        if (!radio->int_in_buffer) {
 619                dev_info(&intf->dev, "could not allocate int_in_buffer");
 620                retval = -ENOMEM;
 621                goto err_usbbuf;
 622        }
 623
 624        radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
 625        if (!radio->int_in_urb) {
 626                retval = -ENOMEM;
 627                goto err_intbuffer;
 628        }
 629
 630        radio->v4l2_dev.release = si470x_usb_release;
 631
 632        /*
 633         * The si470x SiLabs reference design uses the same USB IDs as
 634         * 'Thanko's Raremono' si4734 based receiver. So check here which we
 635         * have: attempt to read the device ID from the si470x: the lower 12
 636         * bits should be 0x0242 for the si470x.
 637         *
 638         * We use this check to determine which device we are dealing with.
 639         */
 640        if (id->idVendor == 0x10c4 && id->idProduct == 0x818a) {
 641                retval = usb_control_msg(radio->usbdev,
 642                                usb_rcvctrlpipe(radio->usbdev, 0),
 643                                HID_REQ_GET_REPORT,
 644                                USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
 645                                1, 2,
 646                                radio->usb_buf, 3, 500);
 647                if (retval != 3 ||
 648                    (get_unaligned_be16(&radio->usb_buf[1]) & 0xfff) != 0x0242) {
 649                        dev_info(&intf->dev, "this is not a si470x device.\n");
 650                        retval = -ENODEV;
 651                        goto err_urb;
 652                }
 653        }
 654
 655        retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
 656        if (retval < 0) {
 657                dev_err(&intf->dev, "couldn't register v4l2_device\n");
 658                goto err_urb;
 659        }
 660
 661        v4l2_ctrl_handler_init(&radio->hdl, 2);
 662        v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
 663                          V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
 664        v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
 665                          V4L2_CID_AUDIO_VOLUME, 0, 15, 1, 15);
 666        if (radio->hdl.error) {
 667                retval = radio->hdl.error;
 668                dev_err(&intf->dev, "couldn't register control\n");
 669                goto err_dev;
 670        }
 671        radio->videodev = si470x_viddev_template;
 672        radio->videodev.ctrl_handler = &radio->hdl;
 673        radio->videodev.lock = &radio->lock;
 674        radio->videodev.v4l2_dev = &radio->v4l2_dev;
 675        radio->videodev.release = video_device_release_empty;
 676        video_set_drvdata(&radio->videodev, radio);
 677
 678        /* get device and chip versions */
 679        if (si470x_get_all_registers(radio) < 0) {
 680                retval = -EIO;
 681                goto err_ctrl;
 682        }
 683        dev_info(&intf->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
 684                        radio->registers[DEVICEID], radio->registers[SI_CHIPID]);
 685        if ((radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
 686                dev_warn(&intf->dev,
 687                        "This driver is known to work with firmware version %hu,\n",
 688                        RADIO_FW_VERSION);
 689                dev_warn(&intf->dev,
 690                        "but the device has firmware version %hu.\n",
 691                        radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE);
 692                version_warning = 1;
 693        }
 694
 695        /* get software and hardware versions */
 696        if (si470x_get_scratch_page_versions(radio) < 0) {
 697                retval = -EIO;
 698                goto err_ctrl;
 699        }
 700        dev_info(&intf->dev, "software version %d, hardware version %d\n",
 701                        radio->software_version, radio->hardware_version);
 702        if (radio->hardware_version < RADIO_HW_VERSION) {
 703                dev_warn(&intf->dev,
 704                        "This driver is known to work with hardware version %hu,\n",
 705                        RADIO_HW_VERSION);
 706                dev_warn(&intf->dev,
 707                        "but the device has hardware version %hu.\n",
 708                        radio->hardware_version);
 709                version_warning = 1;
 710        }
 711
 712        /* give out version warning */
 713        if (version_warning == 1) {
 714                dev_warn(&intf->dev,
 715                        "If you have some trouble using this driver,\n");
 716                dev_warn(&intf->dev,
 717                        "please report to V4L ML at linux-media@vger.kernel.org\n");
 718        }
 719
 720        /* set led to connect state */
 721        si470x_set_led_state(radio, BLINK_GREEN_LED);
 722
 723        /* rds buffer allocation */
 724        radio->buf_size = rds_buf * 3;
 725        radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
 726        if (!radio->buffer) {
 727                retval = -EIO;
 728                goto err_ctrl;
 729        }
 730
 731        /* rds buffer configuration */
 732        radio->wr_index = 0;
 733        radio->rd_index = 0;
 734        init_waitqueue_head(&radio->read_queue);
 735        usb_set_intfdata(intf, radio);
 736
 737        /* start radio */
 738        retval = si470x_start_usb(radio);
 739        if (retval < 0)
 740                goto err_all;
 741
 742        /* set initial frequency */
 743        si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
 744
 745        /* register video device */
 746        retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
 747                        radio_nr);
 748        if (retval) {
 749                dev_err(&intf->dev, "Could not register video device\n");
 750                goto err_all;
 751        }
 752
 753        return 0;
 754err_all:
 755        kfree(radio->buffer);
 756err_ctrl:
 757        v4l2_ctrl_handler_free(&radio->hdl);
 758err_dev:
 759        v4l2_device_unregister(&radio->v4l2_dev);
 760err_urb:
 761        usb_free_urb(radio->int_in_urb);
 762err_intbuffer:
 763        kfree(radio->int_in_buffer);
 764err_usbbuf:
 765        kfree(radio->usb_buf);
 766err_radio:
 767        kfree(radio);
 768err_initial:
 769        return retval;
 770}
 771
 772
 773/*
 774 * si470x_usb_driver_suspend - suspend the device
 775 */
 776static int si470x_usb_driver_suspend(struct usb_interface *intf,
 777                pm_message_t message)
 778{
 779        struct si470x_device *radio = usb_get_intfdata(intf);
 780
 781        dev_info(&intf->dev, "suspending now...\n");
 782
 783        /* shutdown interrupt handler */
 784        if (radio->int_in_running) {
 785                radio->int_in_running = 0;
 786                if (radio->int_in_urb)
 787                        usb_kill_urb(radio->int_in_urb);
 788        }
 789
 790        /* cancel read processes */
 791        wake_up_interruptible(&radio->read_queue);
 792
 793        /* stop radio */
 794        si470x_stop(radio);
 795        return 0;
 796}
 797
 798
 799/*
 800 * si470x_usb_driver_resume - resume the device
 801 */
 802static int si470x_usb_driver_resume(struct usb_interface *intf)
 803{
 804        struct si470x_device *radio = usb_get_intfdata(intf);
 805        int ret;
 806
 807        dev_info(&intf->dev, "resuming now...\n");
 808
 809        /* start radio */
 810        ret = si470x_start_usb(radio);
 811        if (ret == 0)
 812                v4l2_ctrl_handler_setup(&radio->hdl);
 813
 814        return ret;
 815}
 816
 817
 818/*
 819 * si470x_usb_driver_disconnect - disconnect the device
 820 */
 821static void si470x_usb_driver_disconnect(struct usb_interface *intf)
 822{
 823        struct si470x_device *radio = usb_get_intfdata(intf);
 824
 825        mutex_lock(&radio->lock);
 826        v4l2_device_disconnect(&radio->v4l2_dev);
 827        video_unregister_device(&radio->videodev);
 828        usb_set_intfdata(intf, NULL);
 829        mutex_unlock(&radio->lock);
 830        v4l2_device_put(&radio->v4l2_dev);
 831}
 832
 833
 834/*
 835 * si470x_usb_driver - usb driver interface
 836 *
 837 * A note on suspend/resume: this driver had only empty suspend/resume
 838 * functions, and when I tried to test suspend/resume it always disconnected
 839 * instead of resuming (using my ADS InstantFM stick). So I've decided to
 840 * remove these callbacks until someone else with better hardware can
 841 * implement and test this.
 842 */
 843static struct usb_driver si470x_usb_driver = {
 844        .name                   = DRIVER_NAME,
 845        .probe                  = si470x_usb_driver_probe,
 846        .disconnect             = si470x_usb_driver_disconnect,
 847        .suspend                = si470x_usb_driver_suspend,
 848        .resume                 = si470x_usb_driver_resume,
 849        .reset_resume           = si470x_usb_driver_resume,
 850        .id_table               = si470x_usb_driver_id_table,
 851};
 852
 853module_usb_driver(si470x_usb_driver);
 854
 855MODULE_LICENSE("GPL");
 856MODULE_AUTHOR(DRIVER_AUTHOR);
 857MODULE_DESCRIPTION(DRIVER_DESC);
 858MODULE_VERSION(DRIVER_VERSION);
 859