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"
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 */
41 static 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 };
55 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
56
57
58
59 /**************************************************************************
60 * Module Parameters
61 **************************************************************************/
62
63 /* Radio Nr */
64 static int radio_nr = -1;
65 module_param(radio_nr, int, 0444);
66 MODULE_PARM_DESC(radio_nr, "Radio Nr");
67
68 /* USB timeout */
69 static unsigned int usb_timeout = 500;
70 module_param(usb_timeout, uint, 0644);
71 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
72
73 /* RDS buffer blocks */
74 static unsigned int rds_buf = 100;
75 module_param(rds_buf, uint, 0444);
76 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
77
78 /* RDS maximum block errors */
79 static 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 */
84 module_param(max_rds_errors, ushort, 0644);
85 MODULE_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 */
si470x_get_report(struct si470x_device * radio,void * buf,int size)207 static 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 */
si470x_set_report(struct si470x_device * radio,void * buf,int size)230 static 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 */
si470x_get_register(struct si470x_device * radio,int regnr)253 static int 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 */
si470x_set_register(struct si470x_device * radio,int regnr)271 static int 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 */
si470x_get_all_registers(struct si470x_device * radio)292 static 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 */
si470x_set_led_state(struct si470x_device * radio,unsigned char led_state)318 static 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 */
si470x_get_scratch_page_versions(struct si470x_device * radio)341 static 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 */
si470x_int_in_callback(struct urb * urb)371 static 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
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 radio->status_rssi_auto_update = radio->int_in_running;
482 }
483
484
si470x_fops_open(struct file * file)485 static int si470x_fops_open(struct file *file)
486 {
487 return v4l2_fh_open(file);
488 }
489
si470x_fops_release(struct file * file)490 static int si470x_fops_release(struct file *file)
491 {
492 return v4l2_fh_release(file);
493 }
494
si470x_usb_release(struct v4l2_device * v4l2_dev)495 static 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 */
si470x_vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * capability)517 static int 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
si470x_start_usb(struct si470x_device * radio)533 static 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 */
si470x_usb_driver_probe(struct usb_interface * intf,const struct usb_device_id * id)575 static 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 radio->get_register = si470x_get_register;
602 radio->set_register = si470x_set_register;
603 radio->fops_open = si470x_fops_open;
604 radio->fops_release = si470x_fops_release;
605 radio->vidioc_querycap = si470x_vidioc_querycap;
606
607 iface_desc = intf->cur_altsetting;
608
609 /* Set up interrupt endpoint information. */
610 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
611 endpoint = &iface_desc->endpoint[i].desc;
612 if (usb_endpoint_is_int_in(endpoint))
613 radio->int_in_endpoint = endpoint;
614 }
615 if (!radio->int_in_endpoint) {
616 dev_info(&intf->dev, "could not find interrupt in endpoint\n");
617 retval = -EIO;
618 goto err_usbbuf;
619 }
620
621 int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize);
622
623 radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL);
624 if (!radio->int_in_buffer) {
625 dev_info(&intf->dev, "could not allocate int_in_buffer");
626 retval = -ENOMEM;
627 goto err_usbbuf;
628 }
629
630 radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
631 if (!radio->int_in_urb) {
632 retval = -ENOMEM;
633 goto err_intbuffer;
634 }
635
636 radio->v4l2_dev.release = si470x_usb_release;
637
638 /*
639 * The si470x SiLabs reference design uses the same USB IDs as
640 * 'Thanko's Raremono' si4734 based receiver. So check here which we
641 * have: attempt to read the device ID from the si470x: the lower 12
642 * bits should be 0x0242 for the si470x.
643 *
644 * We use this check to determine which device we are dealing with.
645 */
646 if (id->idVendor == 0x10c4 && id->idProduct == 0x818a) {
647 retval = usb_control_msg(radio->usbdev,
648 usb_rcvctrlpipe(radio->usbdev, 0),
649 HID_REQ_GET_REPORT,
650 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
651 1, 2,
652 radio->usb_buf, 3, 500);
653 if (retval != 3 ||
654 (get_unaligned_be16(&radio->usb_buf[1]) & 0xfff) != 0x0242) {
655 dev_info(&intf->dev, "this is not a si470x device.\n");
656 retval = -ENODEV;
657 goto err_urb;
658 }
659 }
660
661 retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
662 if (retval < 0) {
663 dev_err(&intf->dev, "couldn't register v4l2_device\n");
664 goto err_urb;
665 }
666
667 v4l2_ctrl_handler_init(&radio->hdl, 2);
668 v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
669 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
670 v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
671 V4L2_CID_AUDIO_VOLUME, 0, 15, 1, 15);
672 if (radio->hdl.error) {
673 retval = radio->hdl.error;
674 dev_err(&intf->dev, "couldn't register control\n");
675 goto err_dev;
676 }
677 radio->videodev = si470x_viddev_template;
678 radio->videodev.ctrl_handler = &radio->hdl;
679 radio->videodev.lock = &radio->lock;
680 radio->videodev.v4l2_dev = &radio->v4l2_dev;
681 radio->videodev.release = video_device_release_empty;
682 video_set_drvdata(&radio->videodev, radio);
683
684 /* get device and chip versions */
685 if (si470x_get_all_registers(radio) < 0) {
686 retval = -EIO;
687 goto err_ctrl;
688 }
689 dev_info(&intf->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
690 radio->registers[DEVICEID], radio->registers[SI_CHIPID]);
691 if ((radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
692 dev_warn(&intf->dev,
693 "This driver is known to work with firmware version %hu,\n",
694 RADIO_FW_VERSION);
695 dev_warn(&intf->dev,
696 "but the device has firmware version %hu.\n",
697 radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE);
698 version_warning = 1;
699 }
700
701 /* get software and hardware versions */
702 if (si470x_get_scratch_page_versions(radio) < 0) {
703 retval = -EIO;
704 goto err_ctrl;
705 }
706 dev_info(&intf->dev, "software version %d, hardware version %d\n",
707 radio->software_version, radio->hardware_version);
708 if (radio->hardware_version < RADIO_HW_VERSION) {
709 dev_warn(&intf->dev,
710 "This driver is known to work with hardware version %hu,\n",
711 RADIO_HW_VERSION);
712 dev_warn(&intf->dev,
713 "but the device has hardware version %hu.\n",
714 radio->hardware_version);
715 version_warning = 1;
716 }
717
718 /* give out version warning */
719 if (version_warning == 1) {
720 dev_warn(&intf->dev,
721 "If you have some trouble using this driver,\n");
722 dev_warn(&intf->dev,
723 "please report to V4L ML at linux-media@vger.kernel.org\n");
724 }
725
726 /* set led to connect state */
727 si470x_set_led_state(radio, BLINK_GREEN_LED);
728
729 /* rds buffer allocation */
730 radio->buf_size = rds_buf * 3;
731 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
732 if (!radio->buffer) {
733 retval = -EIO;
734 goto err_ctrl;
735 }
736
737 /* rds buffer configuration */
738 radio->wr_index = 0;
739 radio->rd_index = 0;
740 init_waitqueue_head(&radio->read_queue);
741 usb_set_intfdata(intf, radio);
742
743 /* start radio */
744 retval = si470x_start_usb(radio);
745 if (retval < 0 && !radio->int_in_running)
746 goto err_buf;
747 else if (retval < 0) /* in case of radio->int_in_running == 1 */
748 goto err_all;
749
750 /* set initial frequency */
751 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
752
753 /* register video device */
754 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
755 radio_nr);
756 if (retval) {
757 dev_err(&intf->dev, "Could not register video device\n");
758 goto err_all;
759 }
760
761 return 0;
762 err_all:
763 usb_kill_urb(radio->int_in_urb);
764 err_buf:
765 kfree(radio->buffer);
766 err_ctrl:
767 v4l2_ctrl_handler_free(&radio->hdl);
768 err_dev:
769 v4l2_device_unregister(&radio->v4l2_dev);
770 err_urb:
771 usb_free_urb(radio->int_in_urb);
772 err_intbuffer:
773 kfree(radio->int_in_buffer);
774 err_usbbuf:
775 kfree(radio->usb_buf);
776 err_radio:
777 kfree(radio);
778 err_initial:
779 return retval;
780 }
781
782
783 /*
784 * si470x_usb_driver_suspend - suspend the device
785 */
si470x_usb_driver_suspend(struct usb_interface * intf,pm_message_t message)786 static int si470x_usb_driver_suspend(struct usb_interface *intf,
787 pm_message_t message)
788 {
789 struct si470x_device *radio = usb_get_intfdata(intf);
790
791 dev_info(&intf->dev, "suspending now...\n");
792
793 /* shutdown interrupt handler */
794 if (radio->int_in_running) {
795 radio->int_in_running = 0;
796 if (radio->int_in_urb)
797 usb_kill_urb(radio->int_in_urb);
798 }
799
800 /* cancel read processes */
801 wake_up_interruptible(&radio->read_queue);
802
803 /* stop radio */
804 si470x_stop(radio);
805 return 0;
806 }
807
808
809 /*
810 * si470x_usb_driver_resume - resume the device
811 */
si470x_usb_driver_resume(struct usb_interface * intf)812 static int si470x_usb_driver_resume(struct usb_interface *intf)
813 {
814 struct si470x_device *radio = usb_get_intfdata(intf);
815 int ret;
816
817 dev_info(&intf->dev, "resuming now...\n");
818
819 /* start radio */
820 ret = si470x_start_usb(radio);
821 if (ret == 0)
822 v4l2_ctrl_handler_setup(&radio->hdl);
823
824 return ret;
825 }
826
827
828 /*
829 * si470x_usb_driver_disconnect - disconnect the device
830 */
si470x_usb_driver_disconnect(struct usb_interface * intf)831 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
832 {
833 struct si470x_device *radio = usb_get_intfdata(intf);
834
835 mutex_lock(&radio->lock);
836 v4l2_device_disconnect(&radio->v4l2_dev);
837 video_unregister_device(&radio->videodev);
838 usb_kill_urb(radio->int_in_urb);
839 usb_set_intfdata(intf, NULL);
840 mutex_unlock(&radio->lock);
841 v4l2_device_put(&radio->v4l2_dev);
842 }
843
844
845 /*
846 * si470x_usb_driver - usb driver interface
847 *
848 * A note on suspend/resume: this driver had only empty suspend/resume
849 * functions, and when I tried to test suspend/resume it always disconnected
850 * instead of resuming (using my ADS InstantFM stick). So I've decided to
851 * remove these callbacks until someone else with better hardware can
852 * implement and test this.
853 */
854 static struct usb_driver si470x_usb_driver = {
855 .name = DRIVER_NAME,
856 .probe = si470x_usb_driver_probe,
857 .disconnect = si470x_usb_driver_disconnect,
858 .suspend = si470x_usb_driver_suspend,
859 .resume = si470x_usb_driver_resume,
860 .reset_resume = si470x_usb_driver_resume,
861 .id_table = si470x_usb_driver_id_table,
862 };
863
864 module_usb_driver(si470x_usb_driver);
865
866 MODULE_LICENSE("GPL");
867 MODULE_AUTHOR(DRIVER_AUTHOR);
868 MODULE_DESCRIPTION(DRIVER_DESC);
869 MODULE_VERSION(DRIVER_VERSION);
870