Commit | Line | Data |
---|---|---|
b09cd163 JS |
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>" | |
b09cd163 JS |
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> | |
5a0e3ad6 | 39 | #include <linux/slab.h> |
b09cd163 JS |
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) }, | |
11030183 HG |
54 | /* Axentia ALERT FM USB Receiver */ |
55 | { USB_DEVICE_AND_INTERFACE_INFO(0x12cf, 0x7111, USB_CLASS_HID, 0, 0) }, | |
b09cd163 JS |
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 | /************************************************************************** | |
9dcb79c2 | 143 | * Software/Hardware Versions from Scratch Page |
b09cd163 JS |
144 | **************************************************************************/ |
145 | #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6 | |
d9264a0b | 146 | #define RADIO_SW_VERSION 1 |
b09cd163 JS |
147 | #define RADIO_HW_VERSION 1 |
148 | ||
b09cd163 JS |
149 | |
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 | */ | |
211 | static int si470x_get_report(struct si470x_device *radio, void *buf, int size) | |
212 | { | |
213 | unsigned char *report = (unsigned char *) buf; | |
214 | 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) | |
a9d6fd5e JS |
224 | dev_warn(&radio->intf->dev, |
225 | "si470x_get_report: usb_control_msg returned %d\n", | |
b09cd163 JS |
226 | retval); |
227 | return retval; | |
228 | } | |
229 | ||
230 | ||
231 | /* | |
232 | * si470x_set_report - send a HID report | |
233 | */ | |
234 | static int si470x_set_report(struct si470x_device *radio, void *buf, int size) | |
235 | { | |
236 | unsigned char *report = (unsigned char *) buf; | |
237 | 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) | |
a9d6fd5e JS |
247 | dev_warn(&radio->intf->dev, |
248 | "si470x_set_report: usb_control_msg returned %d\n", | |
b09cd163 JS |
249 | retval); |
250 | return retval; | |
251 | } | |
252 | ||
253 | ||
254 | /* | |
255 | * si470x_get_register - read register | |
256 | */ | |
257 | int si470x_get_register(struct si470x_device *radio, int regnr) | |
258 | { | |
259 | unsigned char buf[REGISTER_REPORT_SIZE]; | |
260 | int retval; | |
261 | ||
262 | buf[0] = REGISTER_REPORT(regnr); | |
263 | ||
264 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); | |
265 | ||
266 | if (retval >= 0) | |
267 | radio->registers[regnr] = get_unaligned_be16(&buf[1]); | |
268 | ||
269 | return (retval < 0) ? -EINVAL : 0; | |
270 | } | |
271 | ||
272 | ||
273 | /* | |
274 | * si470x_set_register - write register | |
275 | */ | |
276 | int si470x_set_register(struct si470x_device *radio, int regnr) | |
277 | { | |
278 | unsigned char buf[REGISTER_REPORT_SIZE]; | |
279 | int retval; | |
280 | ||
281 | buf[0] = REGISTER_REPORT(regnr); | |
282 | put_unaligned_be16(radio->registers[regnr], &buf[1]); | |
283 | ||
284 | retval = si470x_set_report(radio, (void *) &buf, sizeof(buf)); | |
285 | ||
286 | return (retval < 0) ? -EINVAL : 0; | |
287 | } | |
288 | ||
289 | ||
290 | ||
291 | /************************************************************************** | |
292 | * General Driver Functions - ENTIRE_REPORT | |
293 | **************************************************************************/ | |
294 | ||
295 | /* | |
296 | * si470x_get_all_registers - read entire registers | |
297 | */ | |
298 | static int si470x_get_all_registers(struct si470x_device *radio) | |
299 | { | |
300 | unsigned char buf[ENTIRE_REPORT_SIZE]; | |
301 | int retval; | |
302 | unsigned char regnr; | |
303 | ||
304 | buf[0] = ENTIRE_REPORT; | |
305 | ||
306 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); | |
307 | ||
308 | if (retval >= 0) | |
309 | for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++) | |
310 | radio->registers[regnr] = get_unaligned_be16( | |
311 | &buf[regnr * RADIO_REGISTER_SIZE + 1]); | |
312 | ||
313 | return (retval < 0) ? -EINVAL : 0; | |
314 | } | |
315 | ||
316 | ||
317 | ||
318 | /************************************************************************** | |
319 | * General Driver Functions - LED_REPORT | |
320 | **************************************************************************/ | |
321 | ||
322 | /* | |
323 | * si470x_set_led_state - sets the led state | |
324 | */ | |
325 | static int si470x_set_led_state(struct si470x_device *radio, | |
326 | unsigned char led_state) | |
327 | { | |
328 | unsigned char buf[LED_REPORT_SIZE]; | |
329 | int retval; | |
330 | ||
331 | buf[0] = LED_REPORT; | |
332 | buf[1] = LED_COMMAND; | |
333 | buf[2] = led_state; | |
334 | ||
335 | retval = si470x_set_report(radio, (void *) &buf, sizeof(buf)); | |
336 | ||
337 | return (retval < 0) ? -EINVAL : 0; | |
338 | } | |
339 | ||
340 | ||
341 | ||
342 | /************************************************************************** | |
343 | * General Driver Functions - SCRATCH_REPORT | |
344 | **************************************************************************/ | |
345 | ||
346 | /* | |
347 | * si470x_get_scratch_versions - gets the scratch page and version infos | |
348 | */ | |
349 | static int si470x_get_scratch_page_versions(struct si470x_device *radio) | |
350 | { | |
351 | unsigned char buf[SCRATCH_REPORT_SIZE]; | |
352 | int retval; | |
353 | ||
354 | buf[0] = SCRATCH_REPORT; | |
355 | ||
356 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); | |
357 | ||
358 | if (retval < 0) | |
a9d6fd5e | 359 | dev_warn(&radio->intf->dev, "si470x_get_scratch: " |
b09cd163 JS |
360 | "si470x_get_report returned %d\n", retval); |
361 | else { | |
362 | radio->software_version = buf[1]; | |
363 | radio->hardware_version = buf[2]; | |
364 | } | |
365 | ||
366 | return (retval < 0) ? -EINVAL : 0; | |
367 | } | |
368 | ||
369 | ||
370 | ||
371 | /************************************************************************** | |
372 | * RDS Driver Functions | |
373 | **************************************************************************/ | |
374 | ||
375 | /* | |
376 | * si470x_int_in_callback - rds callback and processing function | |
377 | * | |
378 | * TODO: do we need to use mutex locks in some sections? | |
379 | */ | |
380 | static void si470x_int_in_callback(struct urb *urb) | |
381 | { | |
382 | struct si470x_device *radio = urb->context; | |
b09cd163 JS |
383 | int retval; |
384 | unsigned char regnr; | |
385 | unsigned char blocknum; | |
386 | unsigned short bler; /* rds block errors */ | |
387 | unsigned short rds; | |
388 | unsigned char tmpbuf[3]; | |
389 | ||
390 | if (urb->status) { | |
391 | if (urb->status == -ENOENT || | |
392 | urb->status == -ECONNRESET || | |
393 | urb->status == -ESHUTDOWN) { | |
394 | return; | |
395 | } else { | |
a9d6fd5e JS |
396 | dev_warn(&radio->intf->dev, |
397 | "non-zero urb status (%d)\n", urb->status); | |
b09cd163 JS |
398 | goto resubmit; /* Maybe we can recover. */ |
399 | } | |
400 | } | |
401 | ||
86ef3f78 HG |
402 | /* Sometimes the device returns len 0 packets */ |
403 | if (urb->actual_length != RDS_REPORT_SIZE) | |
b09cd163 JS |
404 | goto resubmit; |
405 | ||
86ef3f78 HG |
406 | radio->registers[STATUSRSSI] = |
407 | get_unaligned_be16(&radio->int_in_buffer[1]); | |
408 | ||
77947111 HG |
409 | if (radio->registers[STATUSRSSI] & STATUSRSSI_STC) |
410 | complete(&radio->completion); | |
411 | ||
86ef3f78 | 412 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS)) { |
b09cd163 | 413 | /* Update RDS registers with URB data */ |
86ef3f78 | 414 | for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++) |
b09cd163 JS |
415 | radio->registers[STATUSRSSI + regnr] = |
416 | get_unaligned_be16(&radio->int_in_buffer[ | |
417 | regnr * RADIO_REGISTER_SIZE + 1]); | |
418 | /* get rds blocks */ | |
419 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) { | |
420 | /* No RDS group ready, better luck next time */ | |
421 | goto resubmit; | |
422 | } | |
423 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) { | |
424 | /* RDS decoder not synchronized */ | |
425 | goto resubmit; | |
426 | } | |
427 | for (blocknum = 0; blocknum < 4; blocknum++) { | |
428 | switch (blocknum) { | |
429 | default: | |
430 | bler = (radio->registers[STATUSRSSI] & | |
431 | STATUSRSSI_BLERA) >> 9; | |
432 | rds = radio->registers[RDSA]; | |
433 | break; | |
434 | case 1: | |
435 | bler = (radio->registers[READCHAN] & | |
436 | READCHAN_BLERB) >> 14; | |
437 | rds = radio->registers[RDSB]; | |
438 | break; | |
439 | case 2: | |
440 | bler = (radio->registers[READCHAN] & | |
441 | READCHAN_BLERC) >> 12; | |
442 | rds = radio->registers[RDSC]; | |
443 | break; | |
444 | case 3: | |
445 | bler = (radio->registers[READCHAN] & | |
446 | READCHAN_BLERD) >> 10; | |
447 | rds = radio->registers[RDSD]; | |
448 | break; | |
449 | }; | |
450 | ||
451 | /* Fill the V4L2 RDS buffer */ | |
452 | put_unaligned_le16(rds, &tmpbuf); | |
453 | tmpbuf[2] = blocknum; /* offset name */ | |
454 | tmpbuf[2] |= blocknum << 3; /* received offset */ | |
455 | if (bler > max_rds_errors) | |
456 | tmpbuf[2] |= 0x80; /* uncorrectable errors */ | |
457 | else if (bler > 0) | |
458 | tmpbuf[2] |= 0x40; /* corrected error(s) */ | |
459 | ||
460 | /* copy RDS block to internal buffer */ | |
461 | memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3); | |
462 | radio->wr_index += 3; | |
463 | ||
464 | /* wrap write pointer */ | |
465 | if (radio->wr_index >= radio->buf_size) | |
466 | radio->wr_index = 0; | |
467 | ||
468 | /* check for overflow */ | |
469 | if (radio->wr_index == radio->rd_index) { | |
470 | /* increment and wrap read pointer */ | |
471 | radio->rd_index += 3; | |
472 | if (radio->rd_index >= radio->buf_size) | |
473 | radio->rd_index = 0; | |
474 | } | |
475 | } | |
476 | if (radio->wr_index != radio->rd_index) | |
477 | wake_up_interruptible(&radio->read_queue); | |
478 | } | |
479 | ||
480 | resubmit: | |
481 | /* Resubmit if we're still running. */ | |
482 | if (radio->int_in_running && radio->usbdev) { | |
483 | retval = usb_submit_urb(radio->int_in_urb, GFP_ATOMIC); | |
484 | if (retval) { | |
a9d6fd5e JS |
485 | dev_warn(&radio->intf->dev, |
486 | "resubmitting urb failed (%d)", retval); | |
b09cd163 JS |
487 | radio->int_in_running = 0; |
488 | } | |
489 | } | |
86ef3f78 | 490 | radio->status_rssi_auto_update = radio->int_in_running; |
b09cd163 JS |
491 | } |
492 | ||
493 | ||
1aa925c9 | 494 | int si470x_fops_open(struct file *file) |
b09cd163 | 495 | { |
6fd522a6 | 496 | return v4l2_fh_open(file); |
b09cd163 JS |
497 | } |
498 | ||
1aa925c9 | 499 | int si470x_fops_release(struct file *file) |
b09cd163 | 500 | { |
4967d53d | 501 | return v4l2_fh_release(file); |
b09cd163 JS |
502 | } |
503 | ||
6fd522a6 | 504 | static void si470x_usb_release(struct v4l2_device *v4l2_dev) |
4967d53d | 505 | { |
6fd522a6 HV |
506 | struct si470x_device *radio = |
507 | container_of(v4l2_dev, struct si470x_device, v4l2_dev); | |
4967d53d HV |
508 | |
509 | usb_free_urb(radio->int_in_urb); | |
510 | v4l2_ctrl_handler_free(&radio->hdl); | |
511 | v4l2_device_unregister(&radio->v4l2_dev); | |
512 | kfree(radio->int_in_buffer); | |
513 | kfree(radio->buffer); | |
514 | kfree(radio); | |
515 | } | |
b09cd163 | 516 | |
b09cd163 JS |
517 | |
518 | /************************************************************************** | |
519 | * Video4Linux Interface | |
520 | **************************************************************************/ | |
521 | ||
522 | /* | |
523 | * si470x_vidioc_querycap - query device capabilities | |
524 | */ | |
525 | int si470x_vidioc_querycap(struct file *file, void *priv, | |
526 | struct v4l2_capability *capability) | |
527 | { | |
528 | struct si470x_device *radio = video_drvdata(file); | |
529 | ||
530 | strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver)); | |
531 | strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card)); | |
532 | usb_make_path(radio->usbdev, capability->bus_info, | |
533 | sizeof(capability->bus_info)); | |
4967d53d | 534 | capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | |
b09cd163 | 535 | V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE; |
4967d53d | 536 | capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS; |
b09cd163 JS |
537 | return 0; |
538 | } | |
539 | ||
540 | ||
6fd522a6 HV |
541 | static int si470x_start_usb(struct si470x_device *radio) |
542 | { | |
543 | int retval; | |
544 | ||
6fd522a6 HV |
545 | /* initialize interrupt urb */ |
546 | usb_fill_int_urb(radio->int_in_urb, radio->usbdev, | |
547 | usb_rcvintpipe(radio->usbdev, | |
548 | radio->int_in_endpoint->bEndpointAddress), | |
549 | radio->int_in_buffer, | |
550 | le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize), | |
551 | si470x_int_in_callback, | |
552 | radio, | |
553 | radio->int_in_endpoint->bInterval); | |
554 | ||
555 | radio->int_in_running = 1; | |
556 | mb(); | |
557 | ||
558 | retval = usb_submit_urb(radio->int_in_urb, GFP_KERNEL); | |
559 | if (retval) { | |
560 | dev_info(&radio->intf->dev, | |
561 | "submitting int urb failed (%d)\n", retval); | |
562 | radio->int_in_running = 0; | |
563 | } | |
86ef3f78 | 564 | radio->status_rssi_auto_update = radio->int_in_running; |
77947111 HG |
565 | |
566 | /* start radio */ | |
567 | retval = si470x_start(radio); | |
568 | if (retval < 0) | |
569 | return retval; | |
570 | ||
571 | v4l2_ctrl_handler_setup(&radio->hdl); | |
572 | ||
6fd522a6 HV |
573 | return retval; |
574 | } | |
b09cd163 JS |
575 | |
576 | /************************************************************************** | |
577 | * USB Interface | |
578 | **************************************************************************/ | |
579 | ||
580 | /* | |
581 | * si470x_usb_driver_probe - probe for the device | |
582 | */ | |
583 | static int si470x_usb_driver_probe(struct usb_interface *intf, | |
584 | const struct usb_device_id *id) | |
585 | { | |
586 | struct si470x_device *radio; | |
587 | struct usb_host_interface *iface_desc; | |
588 | struct usb_endpoint_descriptor *endpoint; | |
589 | int i, int_end_size, retval = 0; | |
9dcb79c2 | 590 | unsigned char version_warning = 0; |
b09cd163 JS |
591 | |
592 | /* private data allocation and initialization */ | |
593 | radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL); | |
594 | if (!radio) { | |
595 | retval = -ENOMEM; | |
596 | goto err_initial; | |
597 | } | |
b09cd163 JS |
598 | radio->usbdev = interface_to_usbdev(intf); |
599 | radio->intf = intf; | |
b09cd163 | 600 | mutex_init(&radio->lock); |
77947111 | 601 | init_completion(&radio->completion); |
b09cd163 JS |
602 | |
603 | iface_desc = intf->cur_altsetting; | |
604 | ||
605 | /* Set up interrupt endpoint information. */ | |
606 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | |
607 | endpoint = &iface_desc->endpoint[i].desc; | |
608 | if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == | |
609 | USB_DIR_IN) && ((endpoint->bmAttributes & | |
610 | USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) | |
611 | radio->int_in_endpoint = endpoint; | |
612 | } | |
613 | if (!radio->int_in_endpoint) { | |
a9d6fd5e | 614 | dev_info(&intf->dev, "could not find interrupt in endpoint\n"); |
b09cd163 JS |
615 | retval = -EIO; |
616 | goto err_radio; | |
617 | } | |
618 | ||
619 | int_end_size = le16_to_cpu(radio->int_in_endpoint->wMaxPacketSize); | |
620 | ||
621 | radio->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL); | |
622 | if (!radio->int_in_buffer) { | |
a9d6fd5e | 623 | dev_info(&intf->dev, "could not allocate int_in_buffer"); |
b09cd163 JS |
624 | retval = -ENOMEM; |
625 | goto err_radio; | |
626 | } | |
627 | ||
628 | radio->int_in_urb = usb_alloc_urb(0, GFP_KERNEL); | |
629 | if (!radio->int_in_urb) { | |
a9d6fd5e | 630 | dev_info(&intf->dev, "could not allocate int_in_urb"); |
b09cd163 JS |
631 | retval = -ENOMEM; |
632 | goto err_intbuffer; | |
633 | } | |
634 | ||
6fd522a6 | 635 | radio->v4l2_dev.release = si470x_usb_release; |
4967d53d HV |
636 | retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev); |
637 | if (retval < 0) { | |
638 | dev_err(&intf->dev, "couldn't register v4l2_device\n"); | |
f54ba7f1 | 639 | goto err_urb; |
b09cd163 | 640 | } |
4967d53d HV |
641 | |
642 | v4l2_ctrl_handler_init(&radio->hdl, 2); | |
643 | v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops, | |
644 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); | |
645 | v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops, | |
646 | V4L2_CID_AUDIO_VOLUME, 0, 15, 1, 15); | |
647 | if (radio->hdl.error) { | |
648 | retval = radio->hdl.error; | |
649 | dev_err(&intf->dev, "couldn't register control\n"); | |
650 | goto err_dev; | |
651 | } | |
652 | radio->videodev = si470x_viddev_template; | |
653 | radio->videodev.ctrl_handler = &radio->hdl; | |
654 | radio->videodev.lock = &radio->lock; | |
655 | radio->videodev.v4l2_dev = &radio->v4l2_dev; | |
6fd522a6 | 656 | radio->videodev.release = video_device_release_empty; |
4967d53d HV |
657 | set_bit(V4L2_FL_USE_FH_PRIO, &radio->videodev.flags); |
658 | video_set_drvdata(&radio->videodev, radio); | |
b09cd163 | 659 | |
9dcb79c2 | 660 | /* get device and chip versions */ |
b09cd163 JS |
661 | if (si470x_get_all_registers(radio) < 0) { |
662 | retval = -EIO; | |
4967d53d | 663 | goto err_ctrl; |
b09cd163 | 664 | } |
a9d6fd5e | 665 | dev_info(&intf->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n", |
b09cd163 | 666 | radio->registers[DEVICEID], radio->registers[CHIPID]); |
9dcb79c2 TL |
667 | if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) { |
668 | dev_warn(&intf->dev, | |
669 | "This driver is known to work with " | |
670 | "firmware version %hu,\n", RADIO_FW_VERSION); | |
671 | dev_warn(&intf->dev, | |
672 | "but the device has firmware version %hu.\n", | |
673 | radio->registers[CHIPID] & CHIPID_FIRMWARE); | |
674 | version_warning = 1; | |
675 | } | |
b09cd163 JS |
676 | |
677 | /* get software and hardware versions */ | |
678 | if (si470x_get_scratch_page_versions(radio) < 0) { | |
679 | retval = -EIO; | |
4967d53d | 680 | goto err_ctrl; |
b09cd163 | 681 | } |
a9d6fd5e | 682 | dev_info(&intf->dev, "software version %d, hardware version %d\n", |
b09cd163 | 683 | radio->software_version, radio->hardware_version); |
9dcb79c2 | 684 | if (radio->software_version < RADIO_SW_VERSION) { |
a9d6fd5e JS |
685 | dev_warn(&intf->dev, |
686 | "This driver is known to work with " | |
9dcb79c2 | 687 | "software version %hu,\n", RADIO_SW_VERSION); |
a9d6fd5e | 688 | dev_warn(&intf->dev, |
9dcb79c2 TL |
689 | "but the device has software version %hu.\n", |
690 | radio->software_version); | |
691 | version_warning = 1; | |
692 | } | |
693 | if (radio->hardware_version < RADIO_HW_VERSION) { | |
694 | dev_warn(&intf->dev, | |
695 | "This driver is known to work with " | |
696 | "hardware version %hu,\n", RADIO_HW_VERSION); | |
697 | dev_warn(&intf->dev, | |
698 | "but the device has hardware version %hu.\n", | |
699 | radio->hardware_version); | |
700 | version_warning = 1; | |
701 | } | |
702 | ||
703 | /* give out version warning */ | |
704 | if (version_warning == 1) { | |
a9d6fd5e JS |
705 | dev_warn(&intf->dev, |
706 | "If you have some trouble using this driver,\n"); | |
707 | dev_warn(&intf->dev, | |
708 | "please report to V4L ML at " | |
b09cd163 JS |
709 | "linux-media@vger.kernel.org\n"); |
710 | } | |
711 | ||
b09cd163 JS |
712 | /* set led to connect state */ |
713 | si470x_set_led_state(radio, BLINK_GREEN_LED); | |
714 | ||
715 | /* rds buffer allocation */ | |
716 | radio->buf_size = rds_buf * 3; | |
717 | radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL); | |
718 | if (!radio->buffer) { | |
719 | retval = -EIO; | |
4967d53d | 720 | goto err_ctrl; |
b09cd163 JS |
721 | } |
722 | ||
723 | /* rds buffer configuration */ | |
724 | radio->wr_index = 0; | |
725 | radio->rd_index = 0; | |
726 | init_waitqueue_head(&radio->read_queue); | |
4967d53d | 727 | usb_set_intfdata(intf, radio); |
b09cd163 | 728 | |
6fd522a6 HV |
729 | /* start radio */ |
730 | retval = si470x_start_usb(radio); | |
731 | if (retval < 0) | |
732 | goto err_all; | |
733 | ||
77947111 HG |
734 | /* set initial frequency */ |
735 | si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */ | |
736 | ||
b09cd163 | 737 | /* register video device */ |
4967d53d | 738 | retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, |
b09cd163 JS |
739 | radio_nr); |
740 | if (retval) { | |
6fd522a6 | 741 | dev_err(&intf->dev, "Could not register video device\n"); |
b09cd163 JS |
742 | goto err_all; |
743 | } | |
b09cd163 JS |
744 | |
745 | return 0; | |
746 | err_all: | |
747 | kfree(radio->buffer); | |
4967d53d HV |
748 | err_ctrl: |
749 | v4l2_ctrl_handler_free(&radio->hdl); | |
750 | err_dev: | |
751 | v4l2_device_unregister(&radio->v4l2_dev); | |
f54ba7f1 AK |
752 | err_urb: |
753 | usb_free_urb(radio->int_in_urb); | |
b09cd163 JS |
754 | err_intbuffer: |
755 | kfree(radio->int_in_buffer); | |
756 | err_radio: | |
757 | kfree(radio); | |
758 | err_initial: | |
759 | return retval; | |
760 | } | |
761 | ||
762 | ||
763 | /* | |
764 | * si470x_usb_driver_suspend - suspend the device | |
765 | */ | |
766 | static int si470x_usb_driver_suspend(struct usb_interface *intf, | |
767 | pm_message_t message) | |
768 | { | |
6fd522a6 HV |
769 | struct si470x_device *radio = usb_get_intfdata(intf); |
770 | ||
a9d6fd5e | 771 | dev_info(&intf->dev, "suspending now...\n"); |
b09cd163 | 772 | |
6fd522a6 HV |
773 | /* shutdown interrupt handler */ |
774 | if (radio->int_in_running) { | |
775 | radio->int_in_running = 0; | |
776 | if (radio->int_in_urb) | |
777 | usb_kill_urb(radio->int_in_urb); | |
778 | } | |
779 | ||
780 | /* cancel read processes */ | |
781 | wake_up_interruptible(&radio->read_queue); | |
782 | ||
783 | /* stop radio */ | |
784 | si470x_stop(radio); | |
b09cd163 JS |
785 | return 0; |
786 | } | |
787 | ||
788 | ||
789 | /* | |
790 | * si470x_usb_driver_resume - resume the device | |
791 | */ | |
792 | static int si470x_usb_driver_resume(struct usb_interface *intf) | |
793 | { | |
6fd522a6 | 794 | struct si470x_device *radio = usb_get_intfdata(intf); |
c1af23c4 | 795 | int ret; |
6fd522a6 | 796 | |
a9d6fd5e | 797 | dev_info(&intf->dev, "resuming now...\n"); |
b09cd163 | 798 | |
6fd522a6 | 799 | /* start radio */ |
c1af23c4 HG |
800 | ret = si470x_start_usb(radio); |
801 | if (ret == 0) | |
802 | v4l2_ctrl_handler_setup(&radio->hdl); | |
803 | ||
804 | return ret; | |
b09cd163 JS |
805 | } |
806 | ||
807 | ||
808 | /* | |
809 | * si470x_usb_driver_disconnect - disconnect the device | |
810 | */ | |
811 | static void si470x_usb_driver_disconnect(struct usb_interface *intf) | |
812 | { | |
813 | struct si470x_device *radio = usb_get_intfdata(intf); | |
814 | ||
cf9b475d | 815 | mutex_lock(&radio->lock); |
4967d53d HV |
816 | v4l2_device_disconnect(&radio->v4l2_dev); |
817 | video_unregister_device(&radio->videodev); | |
b09cd163 | 818 | usb_set_intfdata(intf, NULL); |
4967d53d | 819 | mutex_unlock(&radio->lock); |
6fd522a6 | 820 | v4l2_device_put(&radio->v4l2_dev); |
b09cd163 JS |
821 | } |
822 | ||
823 | ||
824 | /* | |
825 | * si470x_usb_driver - usb driver interface | |
6fd522a6 HV |
826 | * |
827 | * A note on suspend/resume: this driver had only empty suspend/resume | |
828 | * functions, and when I tried to test suspend/resume it always disconnected | |
829 | * instead of resuming (using my ADS InstantFM stick). So I've decided to | |
830 | * remove these callbacks until someone else with better hardware can | |
831 | * implement and test this. | |
b09cd163 JS |
832 | */ |
833 | static struct usb_driver si470x_usb_driver = { | |
834 | .name = DRIVER_NAME, | |
835 | .probe = si470x_usb_driver_probe, | |
836 | .disconnect = si470x_usb_driver_disconnect, | |
837 | .suspend = si470x_usb_driver_suspend, | |
838 | .resume = si470x_usb_driver_resume, | |
6fd522a6 | 839 | .reset_resume = si470x_usb_driver_resume, |
b09cd163 | 840 | .id_table = si470x_usb_driver_id_table, |
b09cd163 JS |
841 | }; |
842 | ||
ecb3b2b3 | 843 | module_usb_driver(si470x_usb_driver); |
b09cd163 JS |
844 | |
845 | MODULE_LICENSE("GPL"); | |
846 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
847 | MODULE_DESCRIPTION(DRIVER_DESC); | |
848 | MODULE_VERSION(DRIVER_VERSION); |