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