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 	/* Axentia ALERT FM USB Receiver */
55 	{ USB_DEVICE_AND_INTERFACE_INFO(0x12cf, 0x7111, USB_CLASS_HID, 0, 0) },
56 	/* Terminating entry */
57 	{ }
58 };
59 MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
60 
61 
62 
63 /**************************************************************************
64  * Module Parameters
65  **************************************************************************/
66 
67 /* Radio Nr */
68 static int radio_nr = -1;
69 module_param(radio_nr, int, 0444);
70 MODULE_PARM_DESC(radio_nr, "Radio Nr");
71 
72 /* USB timeout */
73 static unsigned int usb_timeout = 500;
74 module_param(usb_timeout, uint, 0644);
75 MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
76 
77 /* RDS buffer blocks */
78 static unsigned int rds_buf = 100;
79 module_param(rds_buf, uint, 0444);
80 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
81 
82 /* RDS maximum block errors */
83 static unsigned short max_rds_errors = 1;
84 /* 0 means   0  errors requiring correction */
85 /* 1 means 1-2  errors requiring correction (used by original USBRadio.exe) */
86 /* 2 means 3-5  errors requiring correction */
87 /* 3 means   6+ errors or errors in checkword, correction not possible */
88 module_param(max_rds_errors, ushort, 0644);
89 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
90 
91 
92 
93 /**************************************************************************
94  * USB HID Reports
95  **************************************************************************/
96 
97 /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
98 /* with the (REPORT_ID - 1) corresponding to the register address across USB */
99 /* endpoint 0 using GET_REPORT and SET_REPORT */
100 #define REGISTER_REPORT_SIZE	(RADIO_REGISTER_SIZE + 1)
101 #define REGISTER_REPORT(reg)	((reg) + 1)
102 
103 /* Report 17 gives direct read/write access to the entire Si470x register */
104 /* map across endpoint 0 using GET_REPORT and SET_REPORT */
105 #define ENTIRE_REPORT_SIZE	(RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
106 #define ENTIRE_REPORT		17
107 
108 /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
109 /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
110 #define RDS_REPORT_SIZE		(RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
111 #define RDS_REPORT		18
112 
113 /* Report 19: LED state */
114 #define LED_REPORT_SIZE		3
115 #define LED_REPORT		19
116 
117 /* Report 19: stream */
118 #define STREAM_REPORT_SIZE	3
119 #define STREAM_REPORT		19
120 
121 /* Report 20: scratch */
122 #define SCRATCH_PAGE_SIZE	63
123 #define SCRATCH_REPORT_SIZE	(SCRATCH_PAGE_SIZE + 1)
124 #define SCRATCH_REPORT		20
125 
126 /* Reports 19-22: flash upgrade of the C8051F321 */
127 #define WRITE_REPORT_SIZE	4
128 #define WRITE_REPORT		19
129 #define FLASH_REPORT_SIZE	64
130 #define FLASH_REPORT		20
131 #define CRC_REPORT_SIZE		3
132 #define CRC_REPORT		21
133 #define RESPONSE_REPORT_SIZE	2
134 #define RESPONSE_REPORT		22
135 
136 /* Report 23: currently unused, but can accept 60 byte reports on the HID */
137 /* interrupt out endpoint 2 every 1 millisecond */
138 #define UNUSED_REPORT		23
139 
140 
141 
142 /**************************************************************************
143  * Software/Hardware Versions from Scratch Page
144  **************************************************************************/
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 	/* Sometimes the device returns len 0 packets */
401 	if (urb->actual_length != RDS_REPORT_SIZE)
402 		goto resubmit;
403 
404 	radio->registers[STATUSRSSI] =
405 		get_unaligned_be16(&radio->int_in_buffer[1]);
406 
407 	if (radio->registers[STATUSRSSI] & STATUSRSSI_STC)
408 		complete(&radio->completion);
409 
410 	if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)) {
411 		/* Update RDS registers with URB data */
412 		for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++)
413 			radio->registers[STATUSRSSI + regnr] =
414 			    get_unaligned_be16(&radio->int_in_buffer[
415 				regnr * RADIO_REGISTER_SIZE + 1]);
416 		/* get rds blocks */
417 		if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
418 			/* No RDS group ready, better luck next time */
419 			goto resubmit;
420 		}
421 		if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
422 			/* RDS decoder not synchronized */
423 			goto resubmit;
424 		}
425 		for (blocknum = 0; blocknum < 4; blocknum++) {
426 			switch (blocknum) {
427 			default:
428 				bler = (radio->registers[STATUSRSSI] &
429 						STATUSRSSI_BLERA) >> 9;
430 				rds = radio->registers[RDSA];
431 				break;
432 			case 1:
433 				bler = (radio->registers[READCHAN] &
434 						READCHAN_BLERB) >> 14;
435 				rds = radio->registers[RDSB];
436 				break;
437 			case 2:
438 				bler = (radio->registers[READCHAN] &
439 						READCHAN_BLERC) >> 12;
440 				rds = radio->registers[RDSC];
441 				break;
442 			case 3:
443 				bler = (radio->registers[READCHAN] &
444 						READCHAN_BLERD) >> 10;
445 				rds = radio->registers[RDSD];
446 				break;
447 			}
448 
449 			/* Fill the V4L2 RDS buffer */
450 			put_unaligned_le16(rds, &tmpbuf);
451 			tmpbuf[2] = blocknum;		/* offset name */
452 			tmpbuf[2] |= blocknum << 3;	/* received offset */
453 			if (bler > max_rds_errors)
454 				tmpbuf[2] |= 0x80; /* uncorrectable errors */
455 			else if (bler > 0)
456 				tmpbuf[2] |= 0x40; /* corrected error(s) */
457 
458 			/* copy RDS block to internal buffer */
459 			memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
460 			radio->wr_index += 3;
461 
462 			/* wrap write pointer */
463 			if (radio->wr_index >= radio->buf_size)
464 				radio->wr_index = 0;
465 
466 			/* check for overflow */
467 			if (radio->wr_index == radio->rd_index) {
468 				/* increment and wrap read pointer */
469 				radio->rd_index += 3;
470 				if (radio->rd_index >= radio->buf_size)
471 					radio->rd_index = 0;
472 			}
473 		}
474 		if (radio->wr_index != radio->rd_index)
475 			wake_up_interruptible(&radio->read_queue);
476 	}
477 
478 resubmit:
479 	/* Resubmit if we're still running. */
480 	if (radio->int_in_running && radio->usbdev) {
481 		retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC);
482 		if (retval) {
483 			dev_warn(&radio->intf->dev,
484 			       "resubmitting urb failed (%d)", retval);
485 			radio->int_in_running = 0;
486 		}
487 	}
488 	radio->status_rssi_auto_update = radio->int_in_running;
489 }
490 
491 
492 int si470x_fops_open(struct file *file)
493 {
494 	return v4l2_fh_open(file);
495 }
496 
497 int si470x_fops_release(struct file *file)
498 {
499 	return v4l2_fh_release(file);
500 }
501 
502 static void si470x_usb_release(struct v4l2_device *v4l2_dev)
503 {
504 	struct si470x_device *radio =
505 		container_of(v4l2_dev, struct si470x_device, v4l2_dev);
506 
507 	usb_free_urb(radio->int_in_urb);
508 	v4l2_ctrl_handler_free(&radio->hdl);
509 	v4l2_device_unregister(&radio->v4l2_dev);
510 	kfree(radio->int_in_buffer);
511 	kfree(radio->buffer);
512 	kfree(radio);
513 }
514 
515 
516 /**************************************************************************
517  * Video4Linux Interface
518  **************************************************************************/
519 
520 /*
521  * si470x_vidioc_querycap - query device capabilities
522  */
523 int si470x_vidioc_querycap(struct file *file, void *priv,
524 		struct v4l2_capability *capability)
525 {
526 	struct si470x_device *radio = video_drvdata(file);
527 
528 	strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
529 	strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
530 	usb_make_path(radio->usbdev, capability->bus_info,
531 			sizeof(capability->bus_info));
532 	capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE |
533 		V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
534 	capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
535 	return 0;
536 }
537 
538 
539 static int si470x_start_usb(struct si470x_device *radio)
540 {
541 	int retval;
542 
543 	/* initialize interrupt urb */
544 	usb_fill_int_urb(radio->int_in_urb, radio->usbdev,
545 			usb_rcvintpipe(radio->usbdev,
546 				radio->int_in_endpoint->bEndpointAddress),
547 			radio->int_in_buffer,
548 			le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize),
549 			si470x_int_in_callback,
550 			radio,
551 			radio->int_in_endpoint->bInterval);
552 
553 	radio->int_in_running = 1;
554 	mb();
555 
556 	retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL);
557 	if (retval) {
558 		dev_info(&radio->intf->dev,
559 				"submitting int urb failed (%d)\n", retval);
560 		radio->int_in_running = 0;
561 	}
562 	radio->status_rssi_auto_update = radio->int_in_running;
563 
564 	/* start radio */
565 	retval = si470x_start(radio);
566 	if (retval < 0)
567 		return retval;
568 
569 	v4l2_ctrl_handler_setup(&radio->hdl);
570 
571 	return retval;
572 }
573 
574 /**************************************************************************
575  * USB Interface
576  **************************************************************************/
577 
578 /*
579  * si470x_usb_driver_probe - probe for the device
580  */
581 static int si470x_usb_driver_probe(struct usb_interface *intf,
582 		const struct usb_device_id *id)
583 {
584 	struct si470x_device *radio;
585 	struct usb_host_interface *iface_desc;
586 	struct usb_endpoint_descriptor *endpoint;
587 	int i, int_end_size, retval = 0;
588 	unsigned char version_warning = 0;
589 
590 	/* private data allocation and initialization */
591 	radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
592 	if (!radio) {
593 		retval = -ENOMEM;
594 		goto err_initial;
595 	}
596 	radio->usbdev = interface_to_usbdev(intf);
597 	radio->intf = intf;
598 	radio->band = 1; /* Default to 76 - 108 MHz */
599 	mutex_init(&radio->lock);
600 	init_completion(&radio->completion);
601 
602 	iface_desc = intf->cur_altsetting;
603 
604 	/* Set up interrupt endpoint information. */
605 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
606 		endpoint = &iface_desc->endpoint[i].desc;
607 		if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
608 		 USB_DIR_IN) && ((endpoint->bmAttributes &
609 		 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
610 			radio->int_in_endpoint = endpoint;
611 	}
612 	if (!radio->int_in_endpoint) {
613 		dev_info(&intf->dev, "could not find interrupt in endpoint\n");
614 		retval = -EIO;
615 		goto err_radio;
616 	}
617 
618 	int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize);
619 
620 	radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL);
621 	if (!radio->int_in_buffer) {
622 		dev_info(&intf->dev, "could not allocate int_in_buffer");
623 		retval = -ENOMEM;
624 		goto err_radio;
625 	}
626 
627 	radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
628 	if (!radio->int_in_urb) {
629 		dev_info(&intf->dev, "could not allocate int_in_urb");
630 		retval = -ENOMEM;
631 		goto err_intbuffer;
632 	}
633 
634 	radio->v4l2_dev.release = si470x_usb_release;
635 	retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
636 	if (retval < 0) {
637 		dev_err(&intf->dev, "couldn't register v4l2_device\n");
638 		goto err_urb;
639 	}
640 
641 	v4l2_ctrl_handler_init(&radio->hdl, 2);
642 	v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
643 			  V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
644 	v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
645 			  V4L2_CID_AUDIO_VOLUME, 0, 15, 1, 15);
646 	if (radio->hdl.error) {
647 		retval = radio->hdl.error;
648 		dev_err(&intf->dev, "couldn't register control\n");
649 		goto err_dev;
650 	}
651 	radio->videodev = si470x_viddev_template;
652 	radio->videodev.ctrl_handler = &radio->hdl;
653 	radio->videodev.lock = &radio->lock;
654 	radio->videodev.v4l2_dev = &radio->v4l2_dev;
655 	radio->videodev.release = video_device_release_empty;
656 	set_bit(V4L2_FL_USE_FH_PRIO, &radio->videodev.flags);
657 	video_set_drvdata(&radio->videodev, radio);
658 
659 	/* get device and chip versions */
660 	if (si470x_get_all_registers(radio) < 0) {
661 		retval = -EIO;
662 		goto err_ctrl;
663 	}
664 	dev_info(&intf->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
665 			radio->registers[DEVICEID], radio->registers[CHIPID]);
666 	if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
667 		dev_warn(&intf->dev,
668 			"This driver is known to work with "
669 			"firmware version %hu,\n", RADIO_FW_VERSION);
670 		dev_warn(&intf->dev,
671 			"but the device has firmware version %hu.\n",
672 			radio->registers[CHIPID] & CHIPID_FIRMWARE);
673 		version_warning = 1;
674 	}
675 
676 	/* get software and hardware versions */
677 	if (si470x_get_scratch_page_versions(radio) < 0) {
678 		retval = -EIO;
679 		goto err_ctrl;
680 	}
681 	dev_info(&intf->dev, "software version %d, hardware version %d\n",
682 			radio->software_version, radio->hardware_version);
683 	if (radio->hardware_version < RADIO_HW_VERSION) {
684 		dev_warn(&intf->dev,
685 			"This driver is known to work with "
686 			"hardware version %hu,\n", RADIO_HW_VERSION);
687 		dev_warn(&intf->dev,
688 			"but the device has hardware version %hu.\n",
689 			radio->hardware_version);
690 		version_warning = 1;
691 	}
692 
693 	/* give out version warning */
694 	if (version_warning == 1) {
695 		dev_warn(&intf->dev,
696 			"If you have some trouble using this driver,\n");
697 		dev_warn(&intf->dev,
698 			"please report to V4L ML at "
699 			"linux-media@vger.kernel.org\n");
700 	}
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 	/* set initial frequency */
725 	si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
726 
727 	/* register video device */
728 	retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
729 			radio_nr);
730 	if (retval) {
731 		dev_err(&intf->dev, "Could not register video device\n");
732 		goto err_all;
733 	}
734 
735 	return 0;
736 err_all:
737 	kfree(radio->buffer);
738 err_ctrl:
739 	v4l2_ctrl_handler_free(&radio->hdl);
740 err_dev:
741 	v4l2_device_unregister(&radio->v4l2_dev);
742 err_urb:
743 	usb_free_urb(radio->int_in_urb);
744 err_intbuffer:
745 	kfree(radio->int_in_buffer);
746 err_radio:
747 	kfree(radio);
748 err_initial:
749 	return retval;
750 }
751 
752 
753 /*
754  * si470x_usb_driver_suspend - suspend the device
755  */
756 static int si470x_usb_driver_suspend(struct usb_interface *intf,
757 		pm_message_t message)
758 {
759 	struct si470x_device *radio = usb_get_intfdata(intf);
760 
761 	dev_info(&intf->dev, "suspending now...\n");
762 
763 	/* shutdown interrupt handler */
764 	if (radio->int_in_running) {
765 		radio->int_in_running = 0;
766 		if (radio->int_in_urb)
767 			usb_kill_urb(radio->int_in_urb);
768 	}
769 
770 	/* cancel read processes */
771 	wake_up_interruptible(&radio->read_queue);
772 
773 	/* stop radio */
774 	si470x_stop(radio);
775 	return 0;
776 }
777 
778 
779 /*
780  * si470x_usb_driver_resume - resume the device
781  */
782 static int si470x_usb_driver_resume(struct usb_interface *intf)
783 {
784 	struct si470x_device *radio = usb_get_intfdata(intf);
785 	int ret;
786 
787 	dev_info(&intf->dev, "resuming now...\n");
788 
789 	/* start radio */
790 	ret = si470x_start_usb(radio);
791 	if (ret == 0)
792 		v4l2_ctrl_handler_setup(&radio->hdl);
793 
794 	return ret;
795 }
796 
797 
798 /*
799  * si470x_usb_driver_disconnect - disconnect the device
800  */
801 static void si470x_usb_driver_disconnect(struct usb_interface *intf)
802 {
803 	struct si470x_device *radio = usb_get_intfdata(intf);
804 
805 	mutex_lock(&radio->lock);
806 	v4l2_device_disconnect(&radio->v4l2_dev);
807 	video_unregister_device(&radio->videodev);
808 	usb_set_intfdata(intf, NULL);
809 	mutex_unlock(&radio->lock);
810 	v4l2_device_put(&radio->v4l2_dev);
811 }
812 
813 
814 /*
815  * si470x_usb_driver - usb driver interface
816  *
817  * A note on suspend/resume: this driver had only empty suspend/resume
818  * functions, and when I tried to test suspend/resume it always disconnected
819  * instead of resuming (using my ADS InstantFM stick). So I've decided to
820  * remove these callbacks until someone else with better hardware can
821  * implement and test this.
822  */
823 static struct usb_driver si470x_usb_driver = {
824 	.name			= DRIVER_NAME,
825 	.probe			= si470x_usb_driver_probe,
826 	.disconnect		= si470x_usb_driver_disconnect,
827 	.suspend		= si470x_usb_driver_suspend,
828 	.resume			= si470x_usb_driver_resume,
829 	.reset_resume		= si470x_usb_driver_resume,
830 	.id_table		= si470x_usb_driver_id_table,
831 };
832 
833 module_usb_driver(si470x_usb_driver);
834 
835 MODULE_LICENSE("GPL");
836 MODULE_AUTHOR(DRIVER_AUTHOR);
837 MODULE_DESCRIPTION(DRIVER_DESC);
838 MODULE_VERSION(DRIVER_VERSION);
839