blob: 494fac061306ed498a02dff4049cb0223d45f621 [file] [log] [blame]
Joonyoung Shimb09cd162009-08-09 14:22:20 -03001/*
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>"
Joonyoung Shimb09cd162009-08-09 14:22:20 -030032#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Joonyoung Shimb09cd162009-08-09 14:22:20 -030040
41#include "radio-si470x.h"
42
43
44/* USB Device ID List */
45static 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) },
Hans de Goede11030182012-05-15 05:12:44 -030054 /* Axentia ALERT FM USB Receiver */
55 { USB_DEVICE_AND_INTERFACE_INFO(0x12cf, 0x7111, USB_CLASS_HID, 0, 0) },
Joonyoung Shimb09cd162009-08-09 14:22:20 -030056 /* Terminating entry */
57 { }
58};
59MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table);
60
61
62
63/**************************************************************************
64 * Module Parameters
65 **************************************************************************/
66
67/* Radio Nr */
68static int radio_nr = -1;
69module_param(radio_nr, int, 0444);
70MODULE_PARM_DESC(radio_nr, "Radio Nr");
71
72/* USB timeout */
73static unsigned int usb_timeout = 500;
74module_param(usb_timeout, uint, 0644);
75MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*");
76
77/* RDS buffer blocks */
78static unsigned int rds_buf = 100;
79module_param(rds_buf, uint, 0444);
80MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
81
82/* RDS maximum block errors */
83static 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 */
88module_param(max_rds_errors, ushort, 0644);
89MODULE_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
Hans Verkuil567e2e92013-12-13 08:06:07 -0300140#define MAX_REPORT_SIZE 64
141
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300142
143
144/**************************************************************************
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300145 * Software/Hardware Versions from Scratch Page
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300146 **************************************************************************/
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300147#define RADIO_HW_VERSION 1
148
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300149
150
151/**************************************************************************
152 * LED State Definitions
153 **************************************************************************/
154#define LED_COMMAND 0x35
155
156#define NO_CHANGE_LED 0x00
157#define ALL_COLOR_LED 0x01 /* streaming state */
158#define BLINK_GREEN_LED 0x02 /* connect state */
159#define BLINK_RED_LED 0x04
160#define BLINK_ORANGE_LED 0x10 /* disconnect state */
161#define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
162#define SOLID_RED_LED 0x40 /* bootload state */
163#define SOLID_ORANGE_LED 0x80
164
165
166
167/**************************************************************************
168 * Stream State Definitions
169 **************************************************************************/
170#define STREAM_COMMAND 0x36
171#define STREAM_VIDPID 0x00
172#define STREAM_AUDIO 0xff
173
174
175
176/**************************************************************************
177 * Bootloader / Flash Commands
178 **************************************************************************/
179
180/* unique id sent to bootloader and required to put into a bootload state */
181#define UNIQUE_BL_ID 0x34
182
183/* mask for the flash data */
184#define FLASH_DATA_MASK 0x55
185
186/* bootloader commands */
187#define GET_SW_VERSION_COMMAND 0x00
188#define SET_PAGE_COMMAND 0x01
189#define ERASE_PAGE_COMMAND 0x02
190#define WRITE_PAGE_COMMAND 0x03
191#define CRC_ON_PAGE_COMMAND 0x04
192#define READ_FLASH_BYTE_COMMAND 0x05
193#define RESET_DEVICE_COMMAND 0x06
194#define GET_HW_VERSION_COMMAND 0x07
195#define BLANK 0xff
196
197/* bootloader command responses */
198#define COMMAND_OK 0x01
199#define COMMAND_FAILED 0x02
200#define COMMAND_PENDING 0x03
201
202
203
204/**************************************************************************
205 * General Driver Functions - REGISTER_REPORTs
206 **************************************************************************/
207
208/*
209 * si470x_get_report - receive a HID report
210 */
211static int si470x_get_report(struct si470x_device *radio, void *buf, int size)
212{
Hans Verkuil567e2e92013-12-13 08:06:07 -0300213 unsigned char *report = buf;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300214 int retval;
215
216 retval = usb_control_msg(radio->usbdev,
217 usb_rcvctrlpipe(radio->usbdev, 0),
218 HID_REQ_GET_REPORT,
219 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
220 report[0], 2,
221 buf, size, usb_timeout);
222
223 if (retval < 0)
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300224 dev_warn(&radio->intf->dev,
225 "si470x_get_report: usb_control_msg returned %d\n",
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300226 retval);
227 return retval;
228}
229
230
231/*
232 * si470x_set_report - send a HID report
233 */
234static int si470x_set_report(struct si470x_device *radio, void *buf, int size)
235{
Hans Verkuil567e2e92013-12-13 08:06:07 -0300236 unsigned char *report = buf;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300237 int retval;
238
239 retval = usb_control_msg(radio->usbdev,
240 usb_sndctrlpipe(radio->usbdev, 0),
241 HID_REQ_SET_REPORT,
242 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
243 report[0], 2,
244 buf, size, usb_timeout);
245
246 if (retval < 0)
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300247 dev_warn(&radio->intf->dev,
248 "si470x_set_report: usb_control_msg returned %d\n",
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300249 retval);
250 return retval;
251}
252
253
254/*
255 * si470x_get_register - read register
256 */
257int si470x_get_register(struct si470x_device *radio, int regnr)
258{
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300259 int retval;
260
Hans Verkuil567e2e92013-12-13 08:06:07 -0300261 radio->usb_buf[0] = REGISTER_REPORT(regnr);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300262
Hans Verkuil567e2e92013-12-13 08:06:07 -0300263 retval = si470x_get_report(radio, radio->usb_buf, REGISTER_REPORT_SIZE);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300264
265 if (retval >= 0)
Hans Verkuil567e2e92013-12-13 08:06:07 -0300266 radio->registers[regnr] = get_unaligned_be16(&radio->usb_buf[1]);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300267
268 return (retval < 0) ? -EINVAL : 0;
269}
270
271
272/*
273 * si470x_set_register - write register
274 */
275int si470x_set_register(struct si470x_device *radio, int regnr)
276{
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300277 int retval;
278
Hans Verkuil567e2e92013-12-13 08:06:07 -0300279 radio->usb_buf[0] = REGISTER_REPORT(regnr);
280 put_unaligned_be16(radio->registers[regnr], &radio->usb_buf[1]);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300281
Hans Verkuil567e2e92013-12-13 08:06:07 -0300282 retval = si470x_set_report(radio, radio->usb_buf, REGISTER_REPORT_SIZE);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300283
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 */
296static int si470x_get_all_registers(struct si470x_device *radio)
297{
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300298 int retval;
299 unsigned char regnr;
300
Hans Verkuil567e2e92013-12-13 08:06:07 -0300301 radio->usb_buf[0] = ENTIRE_REPORT;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300302
Hans Verkuil567e2e92013-12-13 08:06:07 -0300303 retval = si470x_get_report(radio, radio->usb_buf, ENTIRE_REPORT_SIZE);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300304
305 if (retval >= 0)
306 for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++)
307 radio->registers[regnr] = get_unaligned_be16(
Hans Verkuil567e2e92013-12-13 08:06:07 -0300308 &radio->usb_buf[regnr * RADIO_REGISTER_SIZE + 1]);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300309
310 return (retval < 0) ? -EINVAL : 0;
311}
312
313
314
315/**************************************************************************
316 * General Driver Functions - LED_REPORT
317 **************************************************************************/
318
319/*
320 * si470x_set_led_state - sets the led state
321 */
322static int si470x_set_led_state(struct si470x_device *radio,
323 unsigned char led_state)
324{
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300325 int retval;
326
Hans Verkuil567e2e92013-12-13 08:06:07 -0300327 radio->usb_buf[0] = LED_REPORT;
328 radio->usb_buf[1] = LED_COMMAND;
329 radio->usb_buf[2] = led_state;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300330
Hans Verkuil567e2e92013-12-13 08:06:07 -0300331 retval = si470x_set_report(radio, radio->usb_buf, LED_REPORT_SIZE);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300332
333 return (retval < 0) ? -EINVAL : 0;
334}
335
336
337
338/**************************************************************************
339 * General Driver Functions - SCRATCH_REPORT
340 **************************************************************************/
341
342/*
343 * si470x_get_scratch_versions - gets the scratch page and version infos
344 */
345static int si470x_get_scratch_page_versions(struct si470x_device *radio)
346{
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300347 int retval;
348
Hans Verkuil567e2e92013-12-13 08:06:07 -0300349 radio->usb_buf[0] = SCRATCH_REPORT;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300350
Hans Verkuil567e2e92013-12-13 08:06:07 -0300351 retval = si470x_get_report(radio, radio->usb_buf, SCRATCH_REPORT_SIZE);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300352
353 if (retval < 0)
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300354 dev_warn(&radio->intf->dev, "si470x_get_scratch: "
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300355 "si470x_get_report returned %d\n", retval);
356 else {
Hans Verkuil567e2e92013-12-13 08:06:07 -0300357 radio->software_version = radio->usb_buf[1];
358 radio->hardware_version = radio->usb_buf[2];
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300359 }
360
361 return (retval < 0) ? -EINVAL : 0;
362}
363
364
365
366/**************************************************************************
367 * RDS Driver Functions
368 **************************************************************************/
369
370/*
371 * si470x_int_in_callback - rds callback and processing function
372 *
373 * TODO: do we need to use mutex locks in some sections?
374 */
375static void si470x_int_in_callback(struct urb *urb)
376{
377 struct si470x_device *radio = urb->context;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300378 int retval;
379 unsigned char regnr;
380 unsigned char blocknum;
381 unsigned short bler; /* rds block errors */
382 unsigned short rds;
383 unsigned char tmpbuf[3];
384
385 if (urb->status) {
386 if (urb->status == -ENOENT ||
387 urb->status == -ECONNRESET ||
388 urb->status == -ESHUTDOWN) {
389 return;
390 } else {
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300391 dev_warn(&radio->intf->dev,
392 "non-zero urb status (%d)\n", urb->status);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300393 goto resubmit; /* Maybe we can recover. */
394 }
395 }
396
Hans de Goede86ef3f72012-06-14 09:43:11 -0300397 /* Sometimes the device returns len 0 packets */
398 if (urb->actual_length != RDS_REPORT_SIZE)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300399 goto resubmit;
400
Hans de Goede86ef3f72012-06-14 09:43:11 -0300401 radio->registers[STATUSRSSI] =
402 get_unaligned_be16(&radio->int_in_buffer[1]);
403
Hans de Goede77947112012-06-14 09:43:12 -0300404 if (radio->registers[STATUSRSSI] & STATUSRSSI_STC)
405 complete(&radio->completion);
406
Hans de Goede86ef3f72012-06-14 09:43:11 -0300407 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)) {
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300408 /* Update RDS registers with URB data */
Hans de Goede86ef3f72012-06-14 09:43:11 -0300409 for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300410 radio->registers[STATUSRSSI + regnr] =
411 get_unaligned_be16(&radio->int_in_buffer[
412 regnr * RADIO_REGISTER_SIZE + 1]);
413 /* get rds blocks */
414 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) {
415 /* No RDS group ready, better luck next time */
416 goto resubmit;
417 }
418 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) {
419 /* RDS decoder not synchronized */
420 goto resubmit;
421 }
422 for (blocknum = 0; blocknum < 4; blocknum++) {
423 switch (blocknum) {
424 default:
425 bler = (radio->registers[STATUSRSSI] &
426 STATUSRSSI_BLERA) >> 9;
427 rds = radio->registers[RDSA];
428 break;
429 case 1:
430 bler = (radio->registers[READCHAN] &
431 READCHAN_BLERB) >> 14;
432 rds = radio->registers[RDSB];
433 break;
434 case 2:
435 bler = (radio->registers[READCHAN] &
436 READCHAN_BLERC) >> 12;
437 rds = radio->registers[RDSC];
438 break;
439 case 3:
440 bler = (radio->registers[READCHAN] &
441 READCHAN_BLERD) >> 10;
442 rds = radio->registers[RDSD];
443 break;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300444 }
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300445
446 /* Fill the V4L2 RDS buffer */
447 put_unaligned_le16(rds, &tmpbuf);
448 tmpbuf[2] = blocknum; /* offset name */
449 tmpbuf[2] |= blocknum << 3; /* received offset */
450 if (bler > max_rds_errors)
451 tmpbuf[2] |= 0x80; /* uncorrectable errors */
452 else if (bler > 0)
453 tmpbuf[2] |= 0x40; /* corrected error(s) */
454
455 /* copy RDS block to internal buffer */
456 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
457 radio->wr_index += 3;
458
459 /* wrap write pointer */
460 if (radio->wr_index >= radio->buf_size)
461 radio->wr_index = 0;
462
463 /* check for overflow */
464 if (radio->wr_index == radio->rd_index) {
465 /* increment and wrap read pointer */
466 radio->rd_index += 3;
467 if (radio->rd_index >= radio->buf_size)
468 radio->rd_index = 0;
469 }
470 }
471 if (radio->wr_index != radio->rd_index)
472 wake_up_interruptible(&radio->read_queue);
473 }
474
475resubmit:
476 /* Resubmit if we're still running. */
477 if (radio->int_in_running && radio->usbdev) {
478 retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC);
479 if (retval) {
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300480 dev_warn(&radio->intf->dev,
481 "resubmitting urb failed (%d)", retval);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300482 radio->int_in_running = 0;
483 }
484 }
Hans de Goede86ef3f72012-06-14 09:43:11 -0300485 radio->status_rssi_auto_update = radio->int_in_running;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300486}
487
488
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300489int si470x_fops_open(struct file *file)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300490{
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300491 return v4l2_fh_open(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300492}
493
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300494int si470x_fops_release(struct file *file)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300495{
Hans Verkuil4967d532012-05-04 09:16:57 -0300496 return v4l2_fh_release(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300497}
498
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300499static void si470x_usb_release(struct v4l2_device *v4l2_dev)
Hans Verkuil4967d532012-05-04 09:16:57 -0300500{
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300501 struct si470x_device *radio =
502 container_of(v4l2_dev, struct si470x_device, v4l2_dev);
Hans Verkuil4967d532012-05-04 09:16:57 -0300503
504 usb_free_urb(radio->int_in_urb);
505 v4l2_ctrl_handler_free(&radio->hdl);
506 v4l2_device_unregister(&radio->v4l2_dev);
507 kfree(radio->int_in_buffer);
508 kfree(radio->buffer);
Hans Verkuil567e2e92013-12-13 08:06:07 -0300509 kfree(radio->usb_buf);
Hans Verkuil4967d532012-05-04 09:16:57 -0300510 kfree(radio);
511}
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300512
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300513
514/**************************************************************************
515 * Video4Linux Interface
516 **************************************************************************/
517
518/*
519 * si470x_vidioc_querycap - query device capabilities
520 */
521int si470x_vidioc_querycap(struct file *file, void *priv,
522 struct v4l2_capability *capability)
523{
524 struct si470x_device *radio = video_drvdata(file);
525
526 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
527 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
528 usb_make_path(radio->usbdev, capability->bus_info,
529 sizeof(capability->bus_info));
Hans Verkuil32737812012-08-03 11:16:59 -0300530 capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE |
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300531 V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
Hans Verkuil4967d532012-05-04 09:16:57 -0300532 capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300533 return 0;
534}
535
536
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300537static int si470x_start_usb(struct si470x_device *radio)
538{
539 int retval;
540
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300541 /* initialize interrupt urb */
542 usb_fill_int_urb(radio->int_in_urb, radio->usbdev,
543 usb_rcvintpipe(radio->usbdev,
544 radio->int_in_endpoint->bEndpointAddress),
545 radio->int_in_buffer,
546 le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize),
547 si470x_int_in_callback,
548 radio,
549 radio->int_in_endpoint->bInterval);
550
551 radio->int_in_running = 1;
552 mb();
553
554 retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL);
555 if (retval) {
556 dev_info(&radio->intf->dev,
557 "submitting int urb failed (%d)\n", retval);
558 radio->int_in_running = 0;
559 }
Hans de Goede86ef3f72012-06-14 09:43:11 -0300560 radio->status_rssi_auto_update = radio->int_in_running;
Hans de Goede77947112012-06-14 09:43:12 -0300561
562 /* start radio */
563 retval = si470x_start(radio);
564 if (retval < 0)
565 return retval;
566
567 v4l2_ctrl_handler_setup(&radio->hdl);
568
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300569 return retval;
570}
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300571
572/**************************************************************************
573 * USB Interface
574 **************************************************************************/
575
576/*
577 * si470x_usb_driver_probe - probe for the device
578 */
579static int si470x_usb_driver_probe(struct usb_interface *intf,
580 const struct usb_device_id *id)
581{
582 struct si470x_device *radio;
583 struct usb_host_interface *iface_desc;
584 struct usb_endpoint_descriptor *endpoint;
585 int i, int_end_size, retval = 0;
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300586 unsigned char version_warning = 0;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300587
588 /* private data allocation and initialization */
589 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
590 if (!radio) {
591 retval = -ENOMEM;
592 goto err_initial;
593 }
Hans Verkuil567e2e92013-12-13 08:06:07 -0300594 radio->usb_buf = kmalloc(MAX_REPORT_SIZE, GFP_KERNEL);
595 if (radio->usb_buf == NULL) {
596 retval = -ENOMEM;
597 goto err_radio;
598 }
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300599 radio->usbdev = interface_to_usbdev(intf);
600 radio->intf = intf;
Hans de Goedef1406122012-07-12 16:55:48 -0300601 radio->band = 1; /* Default to 76 - 108 MHz */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300602 mutex_init(&radio->lock);
Hans de Goede77947112012-06-14 09:43:12 -0300603 init_completion(&radio->completion);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300604
605 iface_desc = intf->cur_altsetting;
606
607 /* Set up interrupt endpoint information. */
608 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
609 endpoint = &iface_desc->endpoint[i].desc;
610 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
611 USB_DIR_IN) && ((endpoint->bmAttributes &
612 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
613 radio->int_in_endpoint = endpoint;
614 }
615 if (!radio->int_in_endpoint) {
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300616 dev_info(&intf->dev, "could not find interrupt in endpoint\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300617 retval = -EIO;
Hans Verkuil567e2e92013-12-13 08:06:07 -0300618 goto err_usbbuf;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300619 }
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) {
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300625 dev_info(&intf->dev, "could not allocate int_in_buffer");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300626 retval = -ENOMEM;
Hans Verkuil567e2e92013-12-13 08:06:07 -0300627 goto err_usbbuf;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300628 }
629
630 radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
631 if (!radio->int_in_urb) {
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300632 dev_info(&intf->dev, "could not allocate int_in_urb");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300633 retval = -ENOMEM;
634 goto err_intbuffer;
635 }
636
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300637 radio->v4l2_dev.release = si470x_usb_release;
Hans Verkuil5df2def2013-12-13 08:06:38 -0300638
639 /*
640 * The si470x SiLabs reference design uses the same USB IDs as
641 * 'Thanko's Raremono' si4734 based receiver. So check here which we
642 * have: attempt to read the device ID from the si470x: the lower 12
643 * bits should be 0x0242 for the si470x.
644 *
645 * We use this check to determine which device we are dealing with.
646 */
647 if (id->idVendor == 0x10c4 && id->idProduct == 0x818a) {
648 retval = usb_control_msg(radio->usbdev,
649 usb_rcvctrlpipe(radio->usbdev, 0),
650 HID_REQ_GET_REPORT,
651 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
652 1, 2,
653 radio->usb_buf, 3, 500);
654 if (retval != 3 ||
655 (get_unaligned_be16(&radio->usb_buf[1]) & 0xfff) != 0x0242) {
656 dev_info(&intf->dev, "this is not a si470x device.\n");
657 retval = -ENODEV;
658 goto err_urb;
659 }
660 }
661
Hans Verkuil4967d532012-05-04 09:16:57 -0300662 retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
663 if (retval < 0) {
664 dev_err(&intf->dev, "couldn't register v4l2_device\n");
Alexey Khoroshilovf54ba7f2011-05-31 17:54:40 -0300665 goto err_urb;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300666 }
Hans Verkuil4967d532012-05-04 09:16:57 -0300667
668 v4l2_ctrl_handler_init(&radio->hdl, 2);
669 v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
670 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
671 v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
672 V4L2_CID_AUDIO_VOLUME, 0, 15, 1, 15);
673 if (radio->hdl.error) {
674 retval = radio->hdl.error;
675 dev_err(&intf->dev, "couldn't register control\n");
676 goto err_dev;
677 }
678 radio->videodev = si470x_viddev_template;
679 radio->videodev.ctrl_handler = &radio->hdl;
680 radio->videodev.lock = &radio->lock;
681 radio->videodev.v4l2_dev = &radio->v4l2_dev;
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300682 radio->videodev.release = video_device_release_empty;
Hans Verkuil4967d532012-05-04 09:16:57 -0300683 video_set_drvdata(&radio->videodev, radio);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300684
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300685 /* get device and chip versions */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300686 if (si470x_get_all_registers(radio) < 0) {
687 retval = -EIO;
Hans Verkuil4967d532012-05-04 09:16:57 -0300688 goto err_ctrl;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300689 }
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300690 dev_info(&intf->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300691 radio->registers[DEVICEID], radio->registers[CHIPID]);
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300692 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
693 dev_warn(&intf->dev,
694 "This driver is known to work with "
695 "firmware version %hu,\n", RADIO_FW_VERSION);
696 dev_warn(&intf->dev,
697 "but the device has firmware version %hu.\n",
698 radio->registers[CHIPID] & CHIPID_FIRMWARE);
699 version_warning = 1;
700 }
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300701
702 /* get software and hardware versions */
703 if (si470x_get_scratch_page_versions(radio) < 0) {
704 retval = -EIO;
Hans Verkuil4967d532012-05-04 09:16:57 -0300705 goto err_ctrl;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300706 }
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300707 dev_info(&intf->dev, "software version %d, hardware version %d\n",
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300708 radio->software_version, radio->hardware_version);
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300709 if (radio->hardware_version < RADIO_HW_VERSION) {
710 dev_warn(&intf->dev,
711 "This driver is known to work with "
712 "hardware version %hu,\n", RADIO_HW_VERSION);
713 dev_warn(&intf->dev,
714 "but the device has hardware version %hu.\n",
715 radio->hardware_version);
716 version_warning = 1;
717 }
718
719 /* give out version warning */
720 if (version_warning == 1) {
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300721 dev_warn(&intf->dev,
722 "If you have some trouble using this driver,\n");
723 dev_warn(&intf->dev,
724 "please report to V4L ML at "
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300725 "linux-media@vger.kernel.org\n");
726 }
727
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300728 /* set led to connect state */
729 si470x_set_led_state(radio, BLINK_GREEN_LED);
730
731 /* rds buffer allocation */
732 radio->buf_size = rds_buf * 3;
733 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
734 if (!radio->buffer) {
735 retval = -EIO;
Hans Verkuil4967d532012-05-04 09:16:57 -0300736 goto err_ctrl;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300737 }
738
739 /* rds buffer configuration */
740 radio->wr_index = 0;
741 radio->rd_index = 0;
742 init_waitqueue_head(&radio->read_queue);
Hans Verkuil4967d532012-05-04 09:16:57 -0300743 usb_set_intfdata(intf, radio);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300744
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300745 /* start radio */
746 retval = si470x_start_usb(radio);
747 if (retval < 0)
748 goto err_all;
749
Hans de Goede77947112012-06-14 09:43:12 -0300750 /* set initial frequency */
751 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
752
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300753 /* register video device */
Hans Verkuil4967d532012-05-04 09:16:57 -0300754 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300755 radio_nr);
756 if (retval) {
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300757 dev_err(&intf->dev, "Could not register video device\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300758 goto err_all;
759 }
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300760
761 return 0;
762err_all:
763 kfree(radio->buffer);
Hans Verkuil4967d532012-05-04 09:16:57 -0300764err_ctrl:
765 v4l2_ctrl_handler_free(&radio->hdl);
766err_dev:
767 v4l2_device_unregister(&radio->v4l2_dev);
Alexey Khoroshilovf54ba7f2011-05-31 17:54:40 -0300768err_urb:
769 usb_free_urb(radio->int_in_urb);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300770err_intbuffer:
771 kfree(radio->int_in_buffer);
Hans Verkuil567e2e92013-12-13 08:06:07 -0300772err_usbbuf:
773 kfree(radio->usb_buf);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300774err_radio:
775 kfree(radio);
776err_initial:
777 return retval;
778}
779
780
781/*
782 * si470x_usb_driver_suspend - suspend the device
783 */
784static int si470x_usb_driver_suspend(struct usb_interface *intf,
785 pm_message_t message)
786{
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300787 struct si470x_device *radio = usb_get_intfdata(intf);
788
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300789 dev_info(&intf->dev, "suspending now...\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300790
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300791 /* shutdown interrupt handler */
792 if (radio->int_in_running) {
793 radio->int_in_running = 0;
794 if (radio->int_in_urb)
795 usb_kill_urb(radio->int_in_urb);
796 }
797
798 /* cancel read processes */
799 wake_up_interruptible(&radio->read_queue);
800
801 /* stop radio */
802 si470x_stop(radio);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300803 return 0;
804}
805
806
807/*
808 * si470x_usb_driver_resume - resume the device
809 */
810static int si470x_usb_driver_resume(struct usb_interface *intf)
811{
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300812 struct si470x_device *radio = usb_get_intfdata(intf);
Hans de Goedec1af23c2012-07-12 16:55:46 -0300813 int ret;
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300814
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300815 dev_info(&intf->dev, "resuming now...\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300816
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300817 /* start radio */
Hans de Goedec1af23c2012-07-12 16:55:46 -0300818 ret = si470x_start_usb(radio);
819 if (ret == 0)
820 v4l2_ctrl_handler_setup(&radio->hdl);
821
822 return ret;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300823}
824
825
826/*
827 * si470x_usb_driver_disconnect - disconnect the device
828 */
829static void si470x_usb_driver_disconnect(struct usb_interface *intf)
830{
831 struct si470x_device *radio = usb_get_intfdata(intf);
832
Mauro Carvalho Chehabcf9b4752010-09-14 09:56:22 -0300833 mutex_lock(&radio->lock);
Hans Verkuil4967d532012-05-04 09:16:57 -0300834 v4l2_device_disconnect(&radio->v4l2_dev);
835 video_unregister_device(&radio->videodev);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300836 usb_set_intfdata(intf, NULL);
Hans Verkuil4967d532012-05-04 09:16:57 -0300837 mutex_unlock(&radio->lock);
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300838 v4l2_device_put(&radio->v4l2_dev);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300839}
840
841
842/*
843 * si470x_usb_driver - usb driver interface
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300844 *
845 * A note on suspend/resume: this driver had only empty suspend/resume
846 * functions, and when I tried to test suspend/resume it always disconnected
847 * instead of resuming (using my ADS InstantFM stick). So I've decided to
848 * remove these callbacks until someone else with better hardware can
849 * implement and test this.
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300850 */
851static struct usb_driver si470x_usb_driver = {
852 .name = DRIVER_NAME,
853 .probe = si470x_usb_driver_probe,
854 .disconnect = si470x_usb_driver_disconnect,
855 .suspend = si470x_usb_driver_suspend,
856 .resume = si470x_usb_driver_resume,
Hans Verkuil6fd522a2012-05-04 09:42:29 -0300857 .reset_resume = si470x_usb_driver_resume,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300858 .id_table = si470x_usb_driver_id_table,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300859};
860
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800861module_usb_driver(si470x_usb_driver);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300862
863MODULE_LICENSE("GPL");
864MODULE_AUTHOR(DRIVER_AUTHOR);
865MODULE_DESCRIPTION(DRIVER_DESC);
866MODULE_VERSION(DRIVER_VERSION);