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