Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1 | /* |
| 2 | * drivers/media/radio/radio-si470x.c |
| 3 | * |
| 4 | * Driver for USB radios for the Silicon Labs Si470x FM Radio Receivers: |
| 5 | * - Silicon Labs USB FM Radio Reference Design |
| 6 | * - ADS/Tech FM Radio Receiver (formerly Instant FM Music) (RDX-155-EF) |
| 7 | * |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 8 | * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net> |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | /* |
Tobias Lorenz | 6cc7265 | 2008-05-31 15:06:50 -0300 | [diff] [blame] | 27 | * User Notes: |
| 28 | * - USB Audio is provided by the alsa snd_usb_audio module. |
| 29 | * For listing you have to redirect the sound, for example using: |
| 30 | * arecord -D hw:1,0 -r96000 -c2 -f S16_LE | artsdsp aplay -B - |
| 31 | * - regarding module parameters in /sys/module/radio_si470x/parameters: |
| 32 | * the contents of read-only files (0444) are not updated, even if |
| 33 | * space, band and de are changed using private video controls |
| 34 | * - increase tune_timeout, if you often get -EIO errors |
| 35 | * - hw_freq_seek returns -EAGAIN, when timed out or band limit is reached |
| 36 | */ |
| 37 | |
| 38 | |
| 39 | /* |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 40 | * History: |
| 41 | * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net> |
| 42 | * Version 1.0.0 |
| 43 | * - First working version |
| 44 | * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net> |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 45 | * Version 1.0.1 |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 46 | * - Improved error handling, every function now returns errno |
| 47 | * - Improved multi user access (start/mute/stop) |
| 48 | * - Channel doesn't get lost anymore after start/mute/stop |
| 49 | * - RDS support added (polling mode via interrupt EP 1) |
| 50 | * - marked default module parameters with *value* |
| 51 | * - switched from bit structs to bit masks |
| 52 | * - header file cleaned and integrated |
| 53 | * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net> |
| 54 | * Version 1.0.2 |
| 55 | * - hex values are now lower case |
| 56 | * - commented USB ID for ADS/Tech moved on todo list |
| 57 | * - blacklisted si470x in hid-quirks.c |
| 58 | * - rds buffer handling functions integrated into *_work, *_read |
| 59 | * - rds_command in si470x_poll exchanged against simple retval |
| 60 | * - check for firmware version 15 |
| 61 | * - code order and prototypes still remain the same |
| 62 | * - spacing and bottom of band codes remain the same |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 63 | * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net> |
| 64 | * Version 1.0.3 |
| 65 | * - code reordered to avoid function prototypes |
| 66 | * - switch/case defaults are now more user-friendly |
| 67 | * - unified comment style |
| 68 | * - applied all checkpatch.pl v1.12 suggestions |
| 69 | * except the warning about the too long lines with bit comments |
| 70 | * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl) |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 71 | * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net> |
| 72 | * Version 1.0.4 |
| 73 | * - avoid poss. locking when doing copy_to_user which may sleep |
| 74 | * - RDS is automatically activated on read now |
| 75 | * - code cleaned of unnecessary rds_commands |
| 76 | * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified |
| 77 | * (thanks to Guillaume RAMOUSSE) |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 78 | * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net> |
| 79 | * Version 1.0.5 |
| 80 | * - number of seek_retries changed to tune_timeout |
| 81 | * - fixed problem with incomplete tune operations by own buffers |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 82 | * - optimization of variables and printf types |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 83 | * - improved error logging |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 84 | * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net> |
| 85 | * Oliver Neukum <oliver@neukum.org> |
| 86 | * Version 1.0.6 |
| 87 | * - fixed coverity checker warnings in *_usb_driver_disconnect |
| 88 | * - probe()/open() race by correct ordering in probe() |
| 89 | * - DMA coherency rules by separate allocation of all buffers |
| 90 | * - use of endianness macros |
| 91 | * - abuse of spinlock, replaced by mutex |
| 92 | * - racy handling of timer in disconnect, |
| 93 | * replaced by delayed_work |
| 94 | * - racy interruptible_sleep_on(), |
| 95 | * replaced with wait_event_interruptible() |
| 96 | * - handle signals in read() |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 97 | * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net> |
| 98 | * Oliver Neukum <oliver@neukum.org> |
| 99 | * Version 1.0.7 |
| 100 | * - usb autosuspend support |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 101 | * - unplugging fixed |
Tobias Lorenz | 6cc7265 | 2008-05-31 15:06:50 -0300 | [diff] [blame] | 102 | * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net> |
| 103 | * Version 1.0.8 |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 104 | * - more safety checks, let si470x_get_freq return errno |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 105 | * |
| 106 | * ToDo: |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 107 | * - add seeking support |
| 108 | * - add firmware download/update support |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 109 | * - RDS support: interrupt mode, instead of polling |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 110 | * - add LED status output (check if that's not already done in firmware) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 111 | */ |
| 112 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 113 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 114 | /* driver definitions */ |
| 115 | #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>" |
| 116 | #define DRIVER_NAME "radio-si470x" |
Tobias Lorenz | 6cc7265 | 2008-05-31 15:06:50 -0300 | [diff] [blame] | 117 | #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 8) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 118 | #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver" |
| 119 | #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers" |
Tobias Lorenz | 6cc7265 | 2008-05-31 15:06:50 -0300 | [diff] [blame] | 120 | #define DRIVER_VERSION "1.0.8" |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 121 | |
| 122 | |
| 123 | /* kernel includes */ |
| 124 | #include <linux/kernel.h> |
| 125 | #include <linux/module.h> |
| 126 | #include <linux/init.h> |
| 127 | #include <linux/slab.h> |
| 128 | #include <linux/input.h> |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 129 | #include <linux/usb.h> |
| 130 | #include <linux/hid.h> |
| 131 | #include <linux/version.h> |
Mauro Carvalho Chehab | 387d447 | 2008-01-15 11:25:10 -0300 | [diff] [blame] | 132 | #include <linux/videodev2.h> |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 133 | #include <linux/mutex.h> |
Mauro Carvalho Chehab | 387d447 | 2008-01-15 11:25:10 -0300 | [diff] [blame] | 134 | #include <media/v4l2-common.h> |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 135 | #include <media/rds.h> |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 136 | #include <asm/unaligned.h> |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 137 | |
| 138 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 139 | /* USB Device ID List */ |
| 140 | static struct usb_device_id si470x_usb_driver_id_table[] = { |
| 141 | /* Silicon Labs USB FM Radio Reference Design */ |
| 142 | { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) }, |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 143 | /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */ |
| 144 | { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) }, |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 145 | /* Terminating entry */ |
| 146 | { } |
| 147 | }; |
| 148 | MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table); |
| 149 | |
| 150 | |
| 151 | |
| 152 | /************************************************************************** |
| 153 | * Module Parameters |
| 154 | **************************************************************************/ |
| 155 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 156 | /* Radio Nr */ |
| 157 | static int radio_nr = -1; |
| 158 | module_param(radio_nr, int, 0); |
| 159 | MODULE_PARM_DESC(radio_nr, "Radio Nr"); |
| 160 | |
| 161 | /* Spacing (kHz) */ |
| 162 | /* 0: 200 kHz (USA, Australia) */ |
| 163 | /* 1: 100 kHz (Europe, Japan) */ |
| 164 | /* 2: 50 kHz */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 165 | static unsigned short space = 2; |
| 166 | module_param(space, ushort, 0); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 167 | MODULE_PARM_DESC(radio_nr, "Spacing: 0=200kHz 1=100kHz *2=50kHz*"); |
| 168 | |
| 169 | /* Bottom of Band (MHz) */ |
| 170 | /* 0: 87.5 - 108 MHz (USA, Europe)*/ |
| 171 | /* 1: 76 - 108 MHz (Japan wide band) */ |
| 172 | /* 2: 76 - 90 MHz (Japan) */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 173 | static unsigned short band = 1; |
| 174 | module_param(band, ushort, 0); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 175 | MODULE_PARM_DESC(radio_nr, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz"); |
| 176 | |
| 177 | /* De-emphasis */ |
| 178 | /* 0: 75 us (USA) */ |
| 179 | /* 1: 50 us (Europe, Australia, Japan) */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 180 | static unsigned short de = 1; |
| 181 | module_param(de, ushort, 0); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 182 | MODULE_PARM_DESC(radio_nr, "De-emphasis: 0=75us *1=50us*"); |
| 183 | |
| 184 | /* USB timeout */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 185 | static unsigned int usb_timeout = 500; |
| 186 | module_param(usb_timeout, uint, 0); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 187 | MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*"); |
| 188 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 189 | /* Tune timeout */ |
| 190 | static unsigned int tune_timeout = 3000; |
| 191 | module_param(tune_timeout, uint, 0); |
| 192 | MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*"); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 193 | |
| 194 | /* RDS buffer blocks */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 195 | static unsigned int rds_buf = 100; |
| 196 | module_param(rds_buf, uint, 0); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 197 | MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*"); |
| 198 | |
| 199 | /* RDS maximum block errors */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 200 | static unsigned short max_rds_errors = 1; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 201 | /* 0 means 0 errors requiring correction */ |
| 202 | /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */ |
| 203 | /* 2 means 3-5 errors requiring correction */ |
| 204 | /* 3 means 6+ errors or errors in checkword, correction not possible */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 205 | module_param(max_rds_errors, ushort, 0); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 206 | MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*"); |
| 207 | |
| 208 | /* RDS poll frequency */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 209 | static unsigned int rds_poll_time = 40; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 210 | /* 40 is used by the original USBRadio.exe */ |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 211 | /* 50 is used by radio-cadet */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 212 | /* 75 should be okay */ |
| 213 | /* 80 is the usual RDS receive interval */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 214 | module_param(rds_poll_time, uint, 0); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 215 | MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*"); |
| 216 | |
| 217 | |
| 218 | |
| 219 | /************************************************************************** |
| 220 | * Register Definitions |
| 221 | **************************************************************************/ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 222 | #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */ |
| 223 | #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 224 | #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */ |
| 225 | |
| 226 | #define DEVICEID 0 /* Device ID */ |
| 227 | #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */ |
| 228 | #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */ |
| 229 | |
| 230 | #define CHIPID 1 /* Chip ID */ |
| 231 | #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */ |
| 232 | #define CHIPID_DEV 0x0200 /* bits 09..09: Device */ |
| 233 | #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */ |
| 234 | |
| 235 | #define POWERCFG 2 /* Power Configuration */ |
| 236 | #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */ |
| 237 | #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */ |
| 238 | #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */ |
| 239 | #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */ |
| 240 | #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */ |
| 241 | #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */ |
| 242 | #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */ |
| 243 | #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */ |
| 244 | #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */ |
| 245 | |
| 246 | #define CHANNEL 3 /* Channel */ |
| 247 | #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */ |
| 248 | #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */ |
| 249 | |
| 250 | #define SYSCONFIG1 4 /* System Configuration 1 */ |
| 251 | #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */ |
| 252 | #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */ |
| 253 | #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */ |
| 254 | #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */ |
| 255 | #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */ |
| 256 | #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */ |
| 257 | #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */ |
| 258 | #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */ |
| 259 | #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */ |
| 260 | |
| 261 | #define SYSCONFIG2 5 /* System Configuration 2 */ |
| 262 | #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */ |
| 263 | #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */ |
| 264 | #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */ |
| 265 | #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */ |
| 266 | |
| 267 | #define SYSCONFIG3 6 /* System Configuration 3 */ |
| 268 | #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */ |
| 269 | #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */ |
| 270 | #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */ |
| 271 | #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */ |
| 272 | |
| 273 | #define TEST1 7 /* Test 1 */ |
| 274 | #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */ |
| 275 | |
| 276 | #define TEST2 8 /* Test 2 */ |
| 277 | /* TEST2 only contains reserved bits */ |
| 278 | |
| 279 | #define BOOTCONFIG 9 /* Boot Configuration */ |
| 280 | /* BOOTCONFIG only contains reserved bits */ |
| 281 | |
| 282 | #define STATUSRSSI 10 /* Status RSSI */ |
| 283 | #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */ |
| 284 | #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */ |
| 285 | #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */ |
| 286 | #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */ |
| 287 | #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */ |
| 288 | #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */ |
| 289 | #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */ |
| 290 | #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */ |
| 291 | |
| 292 | #define READCHAN 11 /* Read Channel */ |
| 293 | #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */ |
| 294 | #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */ |
| 295 | #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */ |
| 296 | #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */ |
| 297 | |
| 298 | #define RDSA 12 /* RDSA */ |
| 299 | #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */ |
| 300 | |
| 301 | #define RDSB 13 /* RDSB */ |
| 302 | #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */ |
| 303 | |
| 304 | #define RDSC 14 /* RDSC */ |
| 305 | #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */ |
| 306 | |
| 307 | #define RDSD 15 /* RDSD */ |
| 308 | #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */ |
| 309 | |
| 310 | |
| 311 | |
| 312 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 313 | * USB HID Reports |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 314 | **************************************************************************/ |
| 315 | |
| 316 | /* Reports 1-16 give direct read/write access to the 16 Si470x registers */ |
| 317 | /* with the (REPORT_ID - 1) corresponding to the register address across USB */ |
| 318 | /* endpoint 0 using GET_REPORT and SET_REPORT */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 319 | #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 320 | #define REGISTER_REPORT(reg) ((reg) + 1) |
| 321 | |
| 322 | /* Report 17 gives direct read/write access to the entire Si470x register */ |
| 323 | /* map across endpoint 0 using GET_REPORT and SET_REPORT */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 324 | #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 325 | #define ENTIRE_REPORT 17 |
| 326 | |
| 327 | /* Report 18 is used to send the lowest 6 Si470x registers up the HID */ |
| 328 | /* interrupt endpoint 1 to Windows every 20 milliseconds for status */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 329 | #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 330 | #define RDS_REPORT 18 |
| 331 | |
| 332 | /* Report 19: LED state */ |
| 333 | #define LED_REPORT_SIZE 3 |
| 334 | #define LED_REPORT 19 |
| 335 | |
| 336 | /* Report 19: stream */ |
| 337 | #define STREAM_REPORT_SIZE 3 |
| 338 | #define STREAM_REPORT 19 |
| 339 | |
| 340 | /* Report 20: scratch */ |
| 341 | #define SCRATCH_PAGE_SIZE 63 |
| 342 | #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1) |
| 343 | #define SCRATCH_REPORT 20 |
| 344 | |
| 345 | /* Reports 19-22: flash upgrade of the C8051F321 */ |
| 346 | #define WRITE_REPORT 19 |
| 347 | #define FLASH_REPORT 20 |
| 348 | #define CRC_REPORT 21 |
| 349 | #define RESPONSE_REPORT 22 |
| 350 | |
| 351 | /* Report 23: currently unused, but can accept 60 byte reports on the HID */ |
| 352 | /* interrupt out endpoint 2 every 1 millisecond */ |
| 353 | #define UNUSED_REPORT 23 |
| 354 | |
| 355 | |
| 356 | |
| 357 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 358 | * Software/Hardware Versions |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 359 | **************************************************************************/ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 360 | #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6 |
| 361 | #define RADIO_SW_VERSION 7 |
| 362 | #define RADIO_SW_VERSION_CURRENT 15 |
| 363 | #define RADIO_HW_VERSION 1 |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 364 | |
| 365 | #define SCRATCH_PAGE_SW_VERSION 1 |
| 366 | #define SCRATCH_PAGE_HW_VERSION 2 |
| 367 | |
| 368 | |
| 369 | |
| 370 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 371 | * LED State Definitions |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 372 | **************************************************************************/ |
| 373 | #define LED_COMMAND 0x35 |
| 374 | |
| 375 | #define NO_CHANGE_LED 0x00 |
| 376 | #define ALL_COLOR_LED 0x01 /* streaming state */ |
| 377 | #define BLINK_GREEN_LED 0x02 /* connect state */ |
| 378 | #define BLINK_RED_LED 0x04 |
| 379 | #define BLINK_ORANGE_LED 0x10 /* disconnect state */ |
| 380 | #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */ |
| 381 | #define SOLID_RED_LED 0x40 /* bootload state */ |
| 382 | #define SOLID_ORANGE_LED 0x80 |
| 383 | |
| 384 | |
| 385 | |
| 386 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 387 | * Stream State Definitions |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 388 | **************************************************************************/ |
| 389 | #define STREAM_COMMAND 0x36 |
| 390 | #define STREAM_VIDPID 0x00 |
| 391 | #define STREAM_AUDIO 0xff |
| 392 | |
| 393 | |
| 394 | |
| 395 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 396 | * Bootloader / Flash Commands |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 397 | **************************************************************************/ |
| 398 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 399 | /* unique id sent to bootloader and required to put into a bootload state */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 400 | #define UNIQUE_BL_ID 0x34 |
| 401 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 402 | /* mask for the flash data */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 403 | #define FLASH_DATA_MASK 0x55 |
| 404 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 405 | /* bootloader commands */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 406 | #define GET_SW_VERSION_COMMAND 0x00 |
| 407 | #define SET_PAGE_COMMAND 0x01 |
| 408 | #define ERASE_PAGE_COMMAND 0x02 |
| 409 | #define WRITE_PAGE_COMMAND 0x03 |
| 410 | #define CRC_ON_PAGE_COMMAND 0x04 |
| 411 | #define READ_FLASH_BYTE_COMMAND 0x05 |
| 412 | #define RESET_DEVICE_COMMAND 0x06 |
| 413 | #define GET_HW_VERSION_COMMAND 0x07 |
| 414 | #define BLANK 0xff |
| 415 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 416 | /* bootloader command responses */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 417 | #define COMMAND_OK 0x01 |
| 418 | #define COMMAND_FAILED 0x02 |
| 419 | #define COMMAND_PENDING 0x03 |
| 420 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 421 | /* buffer sizes */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 422 | #define COMMAND_BUFFER_SIZE 4 |
| 423 | #define RESPONSE_BUFFER_SIZE 2 |
| 424 | #define FLASH_BUFFER_SIZE 64 |
| 425 | #define CRC_BUFFER_SIZE 3 |
| 426 | |
| 427 | |
| 428 | |
| 429 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 430 | * General Driver Definitions |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 431 | **************************************************************************/ |
| 432 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 433 | /* |
| 434 | * si470x_device - private data |
| 435 | */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 436 | struct si470x_device { |
| 437 | /* reference to USB and video device */ |
| 438 | struct usb_device *usbdev; |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 439 | struct usb_interface *intf; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 440 | struct video_device *videodev; |
| 441 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 442 | /* driver management */ |
| 443 | unsigned int users; |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 444 | unsigned char disconnected; |
| 445 | struct mutex disconnect_lock; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 446 | |
| 447 | /* Silabs internal registers (0..15) */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 448 | unsigned short registers[RADIO_REGISTER_NUM]; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 449 | |
| 450 | /* RDS receive buffer */ |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 451 | struct delayed_work work; |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 452 | wait_queue_head_t read_queue; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 453 | struct mutex lock; /* buffer locking */ |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 454 | unsigned char *buffer; /* size is always multiple of three */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 455 | unsigned int buf_size; |
| 456 | unsigned int rd_index; |
| 457 | unsigned int wr_index; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 458 | }; |
| 459 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 460 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 461 | /* |
| 462 | * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW, |
| 463 | * 62.5 kHz otherwise. |
| 464 | * The tuner is able to have a channel spacing of 50, 100 or 200 kHz. |
| 465 | * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW |
| 466 | * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000 |
| 467 | */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 468 | #define FREQ_MUL (1000000 / 62.5) |
| 469 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 470 | |
| 471 | |
| 472 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 473 | * General Driver Functions |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 474 | **************************************************************************/ |
| 475 | |
| 476 | /* |
| 477 | * si470x_get_report - receive a HID report |
| 478 | */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 479 | static int si470x_get_report(struct si470x_device *radio, void *buf, int size) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 480 | { |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 481 | unsigned char *report = (unsigned char *) buf; |
| 482 | int retval; |
| 483 | |
| 484 | retval = usb_control_msg(radio->usbdev, |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 485 | usb_rcvctrlpipe(radio->usbdev, 0), |
| 486 | HID_REQ_GET_REPORT, |
| 487 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 488 | report[0], 2, |
| 489 | buf, size, usb_timeout); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 490 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 491 | if (retval < 0) |
| 492 | printk(KERN_WARNING DRIVER_NAME |
| 493 | ": si470x_get_report: usb_control_msg returned %d\n", |
| 494 | retval); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 495 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | |
| 499 | /* |
| 500 | * si470x_set_report - send a HID report |
| 501 | */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 502 | static int si470x_set_report(struct si470x_device *radio, void *buf, int size) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 503 | { |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 504 | unsigned char *report = (unsigned char *) buf; |
| 505 | int retval; |
| 506 | |
| 507 | retval = usb_control_msg(radio->usbdev, |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 508 | usb_sndctrlpipe(radio->usbdev, 0), |
| 509 | HID_REQ_SET_REPORT, |
| 510 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 511 | report[0], 2, |
| 512 | buf, size, usb_timeout); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 513 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 514 | if (retval < 0) |
| 515 | printk(KERN_WARNING DRIVER_NAME |
| 516 | ": si470x_set_report: usb_control_msg returned %d\n", |
| 517 | retval); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 518 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | |
| 522 | /* |
| 523 | * si470x_get_register - read register |
| 524 | */ |
| 525 | static int si470x_get_register(struct si470x_device *radio, int regnr) |
| 526 | { |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 527 | unsigned char buf[REGISTER_REPORT_SIZE]; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 528 | int retval; |
| 529 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 530 | buf[0] = REGISTER_REPORT(regnr); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 531 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 532 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); |
| 533 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 534 | if (retval >= 0) |
Harvey Harrison | 5ad6b81 | 2008-04-15 22:22:11 -0300 | [diff] [blame] | 535 | radio->registers[regnr] = get_unaligned_be16(&buf[1]); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 536 | |
| 537 | return (retval < 0) ? -EINVAL : 0; |
| 538 | } |
| 539 | |
| 540 | |
| 541 | /* |
| 542 | * si470x_set_register - write register |
| 543 | */ |
| 544 | static int si470x_set_register(struct si470x_device *radio, int regnr) |
| 545 | { |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 546 | unsigned char buf[REGISTER_REPORT_SIZE]; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 547 | int retval; |
| 548 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 549 | buf[0] = REGISTER_REPORT(regnr); |
Harvey Harrison | 5ad6b81 | 2008-04-15 22:22:11 -0300 | [diff] [blame] | 550 | put_unaligned_be16(radio->registers[regnr], &buf[1]); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 551 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 552 | retval = si470x_set_report(radio, (void *) &buf, sizeof(buf)); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 553 | |
| 554 | return (retval < 0) ? -EINVAL : 0; |
| 555 | } |
| 556 | |
| 557 | |
| 558 | /* |
| 559 | * si470x_get_all_registers - read entire registers |
| 560 | */ |
| 561 | static int si470x_get_all_registers(struct si470x_device *radio) |
| 562 | { |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 563 | unsigned char buf[ENTIRE_REPORT_SIZE]; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 564 | int retval; |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 565 | unsigned char regnr; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 566 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 567 | buf[0] = ENTIRE_REPORT; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 568 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 569 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 570 | |
| 571 | if (retval >= 0) |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 572 | for (regnr = 0; regnr < RADIO_REGISTER_NUM; regnr++) |
Harvey Harrison | 5ad6b81 | 2008-04-15 22:22:11 -0300 | [diff] [blame] | 573 | radio->registers[regnr] = get_unaligned_be16( |
| 574 | &buf[regnr * RADIO_REGISTER_SIZE + 1]); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 575 | |
| 576 | return (retval < 0) ? -EINVAL : 0; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | /* |
| 581 | * si470x_get_rds_registers - read rds registers |
| 582 | */ |
| 583 | static int si470x_get_rds_registers(struct si470x_device *radio) |
| 584 | { |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 585 | unsigned char buf[RDS_REPORT_SIZE]; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 586 | int retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 587 | int size; |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 588 | unsigned char regnr; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 589 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 590 | buf[0] = RDS_REPORT; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 591 | |
| 592 | retval = usb_interrupt_msg(radio->usbdev, |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 593 | usb_rcvintpipe(radio->usbdev, 1), |
| 594 | (void *) &buf, sizeof(buf), &size, usb_timeout); |
| 595 | if (size != sizeof(buf)) |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 596 | printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: " |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 597 | "return size differs: %d != %zu\n", size, sizeof(buf)); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 598 | if (retval < 0) |
| 599 | printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: " |
| 600 | "usb_interrupt_msg returned %d\n", retval); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 601 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 602 | if (retval >= 0) |
| 603 | for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 604 | radio->registers[STATUSRSSI + regnr] = |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 605 | get_unaligned_be16( |
| 606 | &buf[regnr * RADIO_REGISTER_SIZE + 1]); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 607 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 608 | return (retval < 0) ? -EINVAL : 0; |
| 609 | } |
| 610 | |
| 611 | |
| 612 | /* |
| 613 | * si470x_set_chan - set the channel |
| 614 | */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 615 | static int si470x_set_chan(struct si470x_device *radio, unsigned short chan) |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 616 | { |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 617 | int retval; |
| 618 | unsigned long timeout; |
| 619 | bool timed_out = 0; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 620 | |
| 621 | /* start tuning */ |
| 622 | radio->registers[CHANNEL] &= ~CHANNEL_CHAN; |
| 623 | radio->registers[CHANNEL] |= CHANNEL_TUNE | chan; |
| 624 | retval = si470x_set_register(radio, CHANNEL); |
| 625 | if (retval < 0) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 626 | goto done; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 627 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 628 | /* wait till tune operation has completed */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 629 | timeout = jiffies + msecs_to_jiffies(tune_timeout); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 630 | do { |
| 631 | retval = si470x_get_register(radio, STATUSRSSI); |
| 632 | if (retval < 0) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 633 | goto stop; |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 634 | timed_out = time_after(jiffies, timeout); |
| 635 | } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) && |
| 636 | (!timed_out)); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 637 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) |
| 638 | printk(KERN_WARNING DRIVER_NAME ": tune does not complete\n"); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 639 | if (timed_out) |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 640 | printk(KERN_WARNING DRIVER_NAME |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 641 | ": tune timed out after %u ms\n", tune_timeout); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 642 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 643 | stop: |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 644 | /* stop tuning */ |
| 645 | radio->registers[CHANNEL] &= ~CHANNEL_TUNE; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 646 | retval = si470x_set_register(radio, CHANNEL); |
| 647 | |
| 648 | done: |
| 649 | return retval; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | |
| 653 | /* |
| 654 | * si470x_get_freq - get the frequency |
| 655 | */ |
Tobias Lorenz | 6cc7265 | 2008-05-31 15:06:50 -0300 | [diff] [blame] | 656 | static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq) |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 657 | { |
Tobias Lorenz | 6cc7265 | 2008-05-31 15:06:50 -0300 | [diff] [blame] | 658 | unsigned int spacing, band_bottom; |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 659 | unsigned short chan; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 660 | int retval; |
| 661 | |
| 662 | /* Spacing (kHz) */ |
| 663 | switch (space) { |
| 664 | /* 0: 200 kHz (USA, Australia) */ |
| 665 | case 0 : spacing = 0.200 * FREQ_MUL; break; |
| 666 | /* 1: 100 kHz (Europe, Japan) */ |
| 667 | case 1 : spacing = 0.100 * FREQ_MUL; break; |
| 668 | /* 2: 50 kHz */ |
| 669 | default: spacing = 0.050 * FREQ_MUL; break; |
| 670 | }; |
| 671 | |
| 672 | /* Bottom of Band (MHz) */ |
| 673 | switch (band) { |
| 674 | /* 0: 87.5 - 108 MHz (USA, Europe) */ |
| 675 | case 0 : band_bottom = 87.5 * FREQ_MUL; break; |
| 676 | /* 1: 76 - 108 MHz (Japan wide band) */ |
| 677 | default: band_bottom = 76 * FREQ_MUL; break; |
| 678 | /* 2: 76 - 90 MHz (Japan) */ |
| 679 | case 2 : band_bottom = 76 * FREQ_MUL; break; |
| 680 | }; |
| 681 | |
| 682 | /* read channel */ |
| 683 | retval = si470x_get_register(radio, READCHAN); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 684 | chan = radio->registers[READCHAN] & READCHAN_READCHAN; |
| 685 | |
| 686 | /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */ |
Tobias Lorenz | 6cc7265 | 2008-05-31 15:06:50 -0300 | [diff] [blame] | 687 | *freq = chan * spacing + band_bottom; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 688 | |
Tobias Lorenz | 6cc7265 | 2008-05-31 15:06:50 -0300 | [diff] [blame] | 689 | return retval; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |
| 693 | /* |
| 694 | * si470x_set_freq - set the frequency |
| 695 | */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 696 | static int si470x_set_freq(struct si470x_device *radio, unsigned int freq) |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 697 | { |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 698 | unsigned int spacing, band_bottom; |
| 699 | unsigned short chan; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 700 | |
| 701 | /* Spacing (kHz) */ |
| 702 | switch (space) { |
| 703 | /* 0: 200 kHz (USA, Australia) */ |
| 704 | case 0 : spacing = 0.200 * FREQ_MUL; break; |
| 705 | /* 1: 100 kHz (Europe, Japan) */ |
| 706 | case 1 : spacing = 0.100 * FREQ_MUL; break; |
| 707 | /* 2: 50 kHz */ |
| 708 | default: spacing = 0.050 * FREQ_MUL; break; |
| 709 | }; |
| 710 | |
| 711 | /* Bottom of Band (MHz) */ |
| 712 | switch (band) { |
| 713 | /* 0: 87.5 - 108 MHz (USA, Europe) */ |
| 714 | case 0 : band_bottom = 87.5 * FREQ_MUL; break; |
| 715 | /* 1: 76 - 108 MHz (Japan wide band) */ |
| 716 | default: band_bottom = 76 * FREQ_MUL; break; |
| 717 | /* 2: 76 - 90 MHz (Japan) */ |
| 718 | case 2 : band_bottom = 76 * FREQ_MUL; break; |
| 719 | }; |
| 720 | |
| 721 | /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */ |
| 722 | chan = (freq - band_bottom) / spacing; |
| 723 | |
| 724 | return si470x_set_chan(radio, chan); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | |
| 728 | /* |
| 729 | * si470x_start - switch on radio |
| 730 | */ |
| 731 | static int si470x_start(struct si470x_device *radio) |
| 732 | { |
| 733 | int retval; |
| 734 | |
| 735 | /* powercfg */ |
| 736 | radio->registers[POWERCFG] = |
| 737 | POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM; |
| 738 | retval = si470x_set_register(radio, POWERCFG); |
| 739 | if (retval < 0) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 740 | goto done; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 741 | |
| 742 | /* sysconfig 1 */ |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 743 | radio->registers[SYSCONFIG1] = SYSCONFIG1_DE; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 744 | retval = si470x_set_register(radio, SYSCONFIG1); |
| 745 | if (retval < 0) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 746 | goto done; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 747 | |
| 748 | /* sysconfig 2 */ |
| 749 | radio->registers[SYSCONFIG2] = |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 750 | (0x3f << 8) | /* SEEKTH */ |
| 751 | ((band << 6) & SYSCONFIG2_BAND) | /* BAND */ |
| 752 | ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */ |
| 753 | 15; /* VOLUME (max) */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 754 | retval = si470x_set_register(radio, SYSCONFIG2); |
| 755 | if (retval < 0) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 756 | goto done; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 757 | |
| 758 | /* reset last channel */ |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 759 | retval = si470x_set_chan(radio, |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 760 | radio->registers[CHANNEL] & CHANNEL_CHAN); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 761 | |
| 762 | done: |
| 763 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | |
| 767 | /* |
| 768 | * si470x_stop - switch off radio |
| 769 | */ |
| 770 | static int si470x_stop(struct si470x_device *radio) |
| 771 | { |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 772 | int retval; |
| 773 | |
| 774 | /* sysconfig 1 */ |
| 775 | radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS; |
| 776 | retval = si470x_set_register(radio, SYSCONFIG1); |
| 777 | if (retval < 0) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 778 | goto done; |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 779 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 780 | /* powercfg */ |
| 781 | radio->registers[POWERCFG] &= ~POWERCFG_DMUTE; |
| 782 | /* POWERCFG_ENABLE has to automatically go low */ |
| 783 | radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 784 | retval = si470x_set_register(radio, POWERCFG); |
| 785 | |
| 786 | done: |
| 787 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 791 | /* |
| 792 | * si470x_rds_on - switch on rds reception |
| 793 | */ |
| 794 | static int si470x_rds_on(struct si470x_device *radio) |
| 795 | { |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 796 | int retval; |
| 797 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 798 | /* sysconfig 1 */ |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 799 | mutex_lock(&radio->lock); |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 800 | radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS; |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 801 | retval = si470x_set_register(radio, SYSCONFIG1); |
| 802 | if (retval < 0) |
| 803 | radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS; |
| 804 | mutex_unlock(&radio->lock); |
| 805 | |
| 806 | return retval; |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 810 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 811 | /************************************************************************** |
| 812 | * RDS Driver Functions |
| 813 | **************************************************************************/ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 814 | |
| 815 | /* |
| 816 | * si470x_rds - rds processing function |
| 817 | */ |
| 818 | static void si470x_rds(struct si470x_device *radio) |
| 819 | { |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 820 | unsigned char blocknum; |
| 821 | unsigned short bler; /* rds block errors */ |
| 822 | unsigned short rds; |
| 823 | unsigned char tmpbuf[3]; |
| 824 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 825 | /* get rds blocks */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 826 | if (si470x_get_rds_registers(radio) < 0) |
| 827 | return; |
| 828 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) { |
| 829 | /* No RDS group ready */ |
| 830 | return; |
| 831 | } |
| 832 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) { |
| 833 | /* RDS decoder not synchronized */ |
| 834 | return; |
| 835 | } |
| 836 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 837 | /* copy all four RDS blocks to internal buffer */ |
| 838 | mutex_lock(&radio->lock); |
| 839 | for (blocknum = 0; blocknum < 4; blocknum++) { |
| 840 | switch (blocknum) { |
| 841 | default: |
| 842 | bler = (radio->registers[STATUSRSSI] & |
| 843 | STATUSRSSI_BLERA) >> 9; |
| 844 | rds = radio->registers[RDSA]; |
| 845 | break; |
| 846 | case 1: |
| 847 | bler = (radio->registers[READCHAN] & |
| 848 | READCHAN_BLERB) >> 14; |
| 849 | rds = radio->registers[RDSB]; |
| 850 | break; |
| 851 | case 2: |
| 852 | bler = (radio->registers[READCHAN] & |
| 853 | READCHAN_BLERC) >> 12; |
| 854 | rds = radio->registers[RDSC]; |
| 855 | break; |
| 856 | case 3: |
| 857 | bler = (radio->registers[READCHAN] & |
| 858 | READCHAN_BLERD) >> 10; |
| 859 | rds = radio->registers[RDSD]; |
| 860 | break; |
| 861 | }; |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 862 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 863 | /* Fill the V4L2 RDS buffer */ |
Harvey Harrison | 5ad6b81 | 2008-04-15 22:22:11 -0300 | [diff] [blame] | 864 | put_unaligned_le16(rds, &tmpbuf); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 865 | tmpbuf[2] = blocknum; /* offset name */ |
| 866 | tmpbuf[2] |= blocknum << 3; /* received offset */ |
| 867 | if (bler > max_rds_errors) |
| 868 | tmpbuf[2] |= 0x80; /* uncorrectable errors */ |
| 869 | else if (bler > 0) |
| 870 | tmpbuf[2] |= 0x40; /* corrected error(s) */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 871 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 872 | /* copy RDS block to internal buffer */ |
| 873 | memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3); |
| 874 | radio->wr_index += 3; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 875 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 876 | /* wrap write pointer */ |
| 877 | if (radio->wr_index >= radio->buf_size) |
| 878 | radio->wr_index = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 879 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 880 | /* check for overflow */ |
| 881 | if (radio->wr_index == radio->rd_index) { |
| 882 | /* increment and wrap read pointer */ |
| 883 | radio->rd_index += 3; |
| 884 | if (radio->rd_index >= radio->buf_size) |
| 885 | radio->rd_index = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 886 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 887 | } |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 888 | mutex_unlock(&radio->lock); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 889 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 890 | /* wake up read queue */ |
| 891 | if (radio->wr_index != radio->rd_index) |
| 892 | wake_up_interruptible(&radio->read_queue); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | |
| 896 | /* |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 897 | * si470x_work - rds work function |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 898 | */ |
| 899 | static void si470x_work(struct work_struct *work) |
| 900 | { |
| 901 | struct si470x_device *radio = container_of(work, struct si470x_device, |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 902 | work.work); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 903 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 904 | /* safety checks */ |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 905 | if (radio->disconnected) |
| 906 | return; |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 907 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 908 | return; |
| 909 | |
| 910 | si470x_rds(radio); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 911 | schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time)); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | |
| 915 | |
| 916 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 917 | * File Operations Interface |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 918 | **************************************************************************/ |
| 919 | |
| 920 | /* |
| 921 | * si470x_fops_read - read RDS data |
| 922 | */ |
| 923 | static ssize_t si470x_fops_read(struct file *file, char __user *buf, |
| 924 | size_t count, loff_t *ppos) |
| 925 | { |
| 926 | struct si470x_device *radio = video_get_drvdata(video_devdata(file)); |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 927 | int retval = 0; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 928 | unsigned int block_count = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 929 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 930 | /* switch on rds reception */ |
| 931 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) { |
| 932 | si470x_rds_on(radio); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 933 | schedule_delayed_work(&radio->work, |
| 934 | msecs_to_jiffies(rds_poll_time)); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 935 | } |
| 936 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 937 | /* block if no new data available */ |
| 938 | while (radio->wr_index == radio->rd_index) { |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 939 | if (file->f_flags & O_NONBLOCK) { |
| 940 | retval = -EWOULDBLOCK; |
| 941 | goto done; |
| 942 | } |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 943 | if (wait_event_interruptible(radio->read_queue, |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 944 | radio->wr_index != radio->rd_index) < 0) { |
| 945 | retval = -EINTR; |
| 946 | goto done; |
| 947 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 948 | } |
| 949 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 950 | /* calculate block count from byte count */ |
| 951 | count /= 3; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 952 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 953 | /* copy RDS block out of internal buffer and to user buffer */ |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 954 | mutex_lock(&radio->lock); |
| 955 | while (block_count < count) { |
| 956 | if (radio->rd_index == radio->wr_index) |
| 957 | break; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 958 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 959 | /* always transfer rds complete blocks */ |
| 960 | if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3)) |
| 961 | /* retval = -EFAULT; */ |
| 962 | break; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 963 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 964 | /* increment and wrap read pointer */ |
| 965 | radio->rd_index += 3; |
| 966 | if (radio->rd_index >= radio->buf_size) |
| 967 | radio->rd_index = 0; |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 968 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 969 | /* increment counters */ |
| 970 | block_count++; |
| 971 | buf += 3; |
| 972 | retval += 3; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 973 | } |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 974 | mutex_unlock(&radio->lock); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 975 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 976 | done: |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 977 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | |
| 981 | /* |
| 982 | * si470x_fops_poll - poll RDS data |
| 983 | */ |
| 984 | static unsigned int si470x_fops_poll(struct file *file, |
| 985 | struct poll_table_struct *pts) |
| 986 | { |
| 987 | struct si470x_device *radio = video_get_drvdata(video_devdata(file)); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 988 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 989 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 990 | /* switch on rds reception */ |
| 991 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) { |
| 992 | si470x_rds_on(radio); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 993 | schedule_delayed_work(&radio->work, |
| 994 | msecs_to_jiffies(rds_poll_time)); |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 995 | } |
| 996 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 997 | poll_wait(file, &radio->read_queue, pts); |
| 998 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 999 | if (radio->rd_index != radio->wr_index) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1000 | retval = POLLIN | POLLRDNORM; |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1001 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1002 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | |
| 1006 | /* |
| 1007 | * si470x_fops_open - file open |
| 1008 | */ |
| 1009 | static int si470x_fops_open(struct inode *inode, struct file *file) |
| 1010 | { |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1011 | struct si470x_device *radio = video_get_drvdata(video_devdata(file)); |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1012 | int retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1013 | |
| 1014 | radio->users++; |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1015 | |
| 1016 | retval = usb_autopm_get_interface(radio->intf); |
| 1017 | if (retval < 0) { |
| 1018 | radio->users--; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1019 | retval = -EIO; |
| 1020 | goto done; |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | if (radio->users == 1) { |
| 1024 | retval = si470x_start(radio); |
| 1025 | if (retval < 0) |
| 1026 | usb_autopm_put_interface(radio->intf); |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1027 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1028 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1029 | done: |
| 1030 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | /* |
| 1035 | * si470x_fops_release - file release |
| 1036 | */ |
| 1037 | static int si470x_fops_release(struct inode *inode, struct file *file) |
| 1038 | { |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1039 | struct si470x_device *radio = video_get_drvdata(video_devdata(file)); |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1040 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1041 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1042 | /* safety check */ |
| 1043 | if (!radio) { |
| 1044 | retval = -ENODEV; |
| 1045 | goto done; |
| 1046 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1047 | |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1048 | mutex_lock(&radio->disconnect_lock); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1049 | radio->users--; |
| 1050 | if (radio->users == 0) { |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1051 | if (radio->disconnected) { |
| 1052 | video_unregister_device(radio->videodev); |
| 1053 | kfree(radio->buffer); |
| 1054 | kfree(radio); |
| 1055 | goto unlock; |
| 1056 | } |
Tobias Lorenz | 03dea86 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1057 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1058 | /* stop rds reception */ |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1059 | cancel_delayed_work_sync(&radio->work); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1060 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1061 | /* cancel read processes */ |
| 1062 | wake_up_interruptible(&radio->read_queue); |
| 1063 | |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1064 | retval = si470x_stop(radio); |
| 1065 | usb_autopm_put_interface(radio->intf); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1066 | } |
| 1067 | |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1068 | unlock: |
| 1069 | mutex_unlock(&radio->disconnect_lock); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1070 | |
| 1071 | done: |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1072 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | |
| 1076 | /* |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1077 | * si470x_fops - file operations interface |
| 1078 | */ |
| 1079 | static const struct file_operations si470x_fops = { |
| 1080 | .owner = THIS_MODULE, |
| 1081 | .llseek = no_llseek, |
| 1082 | .read = si470x_fops_read, |
| 1083 | .poll = si470x_fops_poll, |
| 1084 | .ioctl = video_ioctl2, |
Douglas Schilling Landgraf | 078ff79 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1085 | #ifdef CONFIG_COMPAT |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1086 | .compat_ioctl = v4l_compat_ioctl32, |
Douglas Schilling Landgraf | 078ff79 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1087 | #endif |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1088 | .open = si470x_fops_open, |
| 1089 | .release = si470x_fops_release, |
| 1090 | }; |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | /************************************************************************** |
| 1095 | * Video4Linux Interface |
| 1096 | **************************************************************************/ |
| 1097 | |
| 1098 | /* |
| 1099 | * si470x_v4l2_queryctrl - query control |
| 1100 | */ |
| 1101 | static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = { |
| 1102 | /* HINT: the disabled controls are only here to satify kradio and such apps */ |
| 1103 | { |
| 1104 | .id = V4L2_CID_AUDIO_VOLUME, |
| 1105 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 1106 | .name = "Volume", |
| 1107 | .minimum = 0, |
| 1108 | .maximum = 15, |
| 1109 | .step = 1, |
| 1110 | .default_value = 15, |
| 1111 | }, |
| 1112 | { |
| 1113 | .id = V4L2_CID_AUDIO_BALANCE, |
| 1114 | .flags = V4L2_CTRL_FLAG_DISABLED, |
| 1115 | }, |
| 1116 | { |
| 1117 | .id = V4L2_CID_AUDIO_BASS, |
| 1118 | .flags = V4L2_CTRL_FLAG_DISABLED, |
| 1119 | }, |
| 1120 | { |
| 1121 | .id = V4L2_CID_AUDIO_TREBLE, |
| 1122 | .flags = V4L2_CTRL_FLAG_DISABLED, |
| 1123 | }, |
| 1124 | { |
| 1125 | .id = V4L2_CID_AUDIO_MUTE, |
| 1126 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 1127 | .name = "Mute", |
| 1128 | .minimum = 0, |
| 1129 | .maximum = 1, |
| 1130 | .step = 1, |
| 1131 | .default_value = 1, |
| 1132 | }, |
| 1133 | { |
| 1134 | .id = V4L2_CID_AUDIO_LOUDNESS, |
| 1135 | .flags = V4L2_CTRL_FLAG_DISABLED, |
| 1136 | }, |
| 1137 | }; |
| 1138 | |
| 1139 | |
| 1140 | /* |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1141 | * si470x_vidioc_querycap - query device capabilities |
| 1142 | */ |
| 1143 | static int si470x_vidioc_querycap(struct file *file, void *priv, |
| 1144 | struct v4l2_capability *capability) |
| 1145 | { |
| 1146 | strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver)); |
| 1147 | strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card)); |
| 1148 | sprintf(capability->bus_info, "USB"); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1149 | capability->version = DRIVER_KERNEL_VERSION; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1150 | capability->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | /* |
| 1157 | * si470x_vidioc_g_input - get input |
| 1158 | */ |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1159 | static int si470x_vidioc_g_input(struct file *file, void *priv, |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1160 | unsigned int *i) |
| 1161 | { |
| 1162 | *i = 0; |
| 1163 | |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
| 1167 | |
| 1168 | /* |
| 1169 | * si470x_vidioc_s_input - set input |
| 1170 | */ |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1171 | static int si470x_vidioc_s_input(struct file *file, void *priv, unsigned int i) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1172 | { |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1173 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1174 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1175 | /* safety checks */ |
| 1176 | if (i != 0) |
| 1177 | retval = -EINVAL; |
| 1178 | |
| 1179 | if (retval < 0) |
| 1180 | printk(KERN_WARNING DRIVER_NAME |
| 1181 | ": set input failed with %d\n", retval); |
| 1182 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | |
| 1186 | /* |
| 1187 | * si470x_vidioc_queryctrl - enumerate control items |
| 1188 | */ |
| 1189 | static int si470x_vidioc_queryctrl(struct file *file, void *priv, |
| 1190 | struct v4l2_queryctrl *qc) |
| 1191 | { |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1192 | unsigned char i; |
| 1193 | int retval = -EINVAL; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1194 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1195 | /* safety checks */ |
| 1196 | if (!qc->id) |
| 1197 | goto done; |
| 1198 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1199 | for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) { |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1200 | if (qc->id == si470x_v4l2_queryctrl[i].id) { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1201 | memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc)); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1202 | retval = 0; |
| 1203 | break; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1204 | } |
| 1205 | } |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1206 | |
| 1207 | done: |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1208 | if (retval < 0) |
| 1209 | printk(KERN_WARNING DRIVER_NAME |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1210 | ": query controls failed with %d\n", retval); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1211 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | /* |
| 1216 | * si470x_vidioc_g_ctrl - get the value of a control |
| 1217 | */ |
| 1218 | static int si470x_vidioc_g_ctrl(struct file *file, void *priv, |
| 1219 | struct v4l2_control *ctrl) |
| 1220 | { |
| 1221 | struct si470x_device *radio = video_get_drvdata(video_devdata(file)); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1222 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1223 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1224 | /* safety checks */ |
| 1225 | if (radio->disconnected) { |
| 1226 | retval = -EIO; |
| 1227 | goto done; |
| 1228 | } |
Tobias Lorenz | 03dea86 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1229 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1230 | switch (ctrl->id) { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1231 | case V4L2_CID_AUDIO_VOLUME: |
| 1232 | ctrl->value = radio->registers[SYSCONFIG2] & |
| 1233 | SYSCONFIG2_VOLUME; |
| 1234 | break; |
| 1235 | case V4L2_CID_AUDIO_MUTE: |
| 1236 | ctrl->value = ((radio->registers[POWERCFG] & |
| 1237 | POWERCFG_DMUTE) == 0) ? 1 : 0; |
| 1238 | break; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1239 | default: |
| 1240 | retval = -EINVAL; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1241 | } |
| 1242 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1243 | done: |
| 1244 | if (retval < 0) |
| 1245 | printk(KERN_WARNING DRIVER_NAME |
| 1246 | ": get control failed with %d\n", retval); |
| 1247 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | /* |
| 1252 | * si470x_vidioc_s_ctrl - set the value of a control |
| 1253 | */ |
| 1254 | static int si470x_vidioc_s_ctrl(struct file *file, void *priv, |
| 1255 | struct v4l2_control *ctrl) |
| 1256 | { |
| 1257 | struct si470x_device *radio = video_get_drvdata(video_devdata(file)); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1258 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1259 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1260 | /* safety checks */ |
| 1261 | if (radio->disconnected) { |
| 1262 | retval = -EIO; |
| 1263 | goto done; |
| 1264 | } |
Tobias Lorenz | 03dea86 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1265 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1266 | switch (ctrl->id) { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1267 | case V4L2_CID_AUDIO_VOLUME: |
| 1268 | radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME; |
| 1269 | radio->registers[SYSCONFIG2] |= ctrl->value; |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1270 | retval = si470x_set_register(radio, SYSCONFIG2); |
| 1271 | break; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1272 | case V4L2_CID_AUDIO_MUTE: |
| 1273 | if (ctrl->value == 1) |
| 1274 | radio->registers[POWERCFG] &= ~POWERCFG_DMUTE; |
| 1275 | else |
| 1276 | radio->registers[POWERCFG] |= POWERCFG_DMUTE; |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1277 | retval = si470x_set_register(radio, POWERCFG); |
| 1278 | break; |
| 1279 | default: |
| 1280 | retval = -EINVAL; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1281 | } |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1282 | |
| 1283 | done: |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1284 | if (retval < 0) |
| 1285 | printk(KERN_WARNING DRIVER_NAME |
| 1286 | ": set control failed with %d\n", retval); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1287 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | |
| 1291 | /* |
| 1292 | * si470x_vidioc_g_audio - get audio attributes |
| 1293 | */ |
| 1294 | static int si470x_vidioc_g_audio(struct file *file, void *priv, |
| 1295 | struct v4l2_audio *audio) |
| 1296 | { |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1297 | int retval = 0; |
| 1298 | |
| 1299 | /* safety checks */ |
| 1300 | if (audio->index != 0) { |
| 1301 | retval = -EINVAL; |
| 1302 | goto done; |
| 1303 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1304 | |
| 1305 | strcpy(audio->name, "Radio"); |
| 1306 | audio->capability = V4L2_AUDCAP_STEREO; |
| 1307 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1308 | done: |
| 1309 | if (retval < 0) |
| 1310 | printk(KERN_WARNING DRIVER_NAME |
| 1311 | ": get audio failed with %d\n", retval); |
| 1312 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | /* |
| 1317 | * si470x_vidioc_s_audio - set audio attributes |
| 1318 | */ |
| 1319 | static int si470x_vidioc_s_audio(struct file *file, void *priv, |
| 1320 | struct v4l2_audio *audio) |
| 1321 | { |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1322 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1323 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1324 | /* safety checks */ |
| 1325 | if (audio->index != 0) { |
| 1326 | retval = -EINVAL; |
| 1327 | goto done; |
| 1328 | } |
| 1329 | |
| 1330 | done: |
| 1331 | if (retval < 0) |
| 1332 | printk(KERN_WARNING DRIVER_NAME |
| 1333 | ": set audio failed with %d\n", retval); |
| 1334 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | |
| 1338 | /* |
| 1339 | * si470x_vidioc_g_tuner - get tuner attributes |
| 1340 | */ |
| 1341 | static int si470x_vidioc_g_tuner(struct file *file, void *priv, |
| 1342 | struct v4l2_tuner *tuner) |
| 1343 | { |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1344 | struct si470x_device *radio = video_get_drvdata(video_devdata(file)); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1345 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1346 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1347 | /* safety checks */ |
| 1348 | if (radio->disconnected) { |
| 1349 | retval = -EIO; |
| 1350 | goto done; |
| 1351 | } |
| 1352 | if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) { |
| 1353 | retval = -EINVAL; |
| 1354 | goto done; |
| 1355 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1356 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1357 | retval = si470x_get_register(radio, STATUSRSSI); |
| 1358 | if (retval < 0) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1359 | goto done; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1360 | |
| 1361 | strcpy(tuner->name, "FM"); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1362 | switch (band) { |
| 1363 | /* 0: 87.5 - 108 MHz (USA, Europe, default) */ |
| 1364 | default: |
| 1365 | tuner->rangelow = 87.5 * FREQ_MUL; |
| 1366 | tuner->rangehigh = 108 * FREQ_MUL; |
| 1367 | break; |
| 1368 | /* 1: 76 - 108 MHz (Japan wide band) */ |
| 1369 | case 1 : |
| 1370 | tuner->rangelow = 76 * FREQ_MUL; |
| 1371 | tuner->rangehigh = 108 * FREQ_MUL; |
| 1372 | break; |
| 1373 | /* 2: 76 - 90 MHz (Japan) */ |
| 1374 | case 2 : |
| 1375 | tuner->rangelow = 76 * FREQ_MUL; |
| 1376 | tuner->rangehigh = 90 * FREQ_MUL; |
| 1377 | break; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1378 | }; |
| 1379 | tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; |
| 1380 | tuner->capability = V4L2_TUNER_CAP_LOW; |
| 1381 | |
| 1382 | /* Stereo indicator == Stereo (instead of Mono) */ |
| 1383 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 1) |
| 1384 | tuner->audmode = V4L2_TUNER_MODE_STEREO; |
| 1385 | else |
| 1386 | tuner->audmode = V4L2_TUNER_MODE_MONO; |
| 1387 | |
| 1388 | /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */ |
| 1389 | tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI) |
| 1390 | * 0x0101; |
| 1391 | |
| 1392 | /* automatic frequency control: -1: freq to low, 1 freq to high */ |
| 1393 | tuner->afc = 0; |
| 1394 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1395 | done: |
| 1396 | if (retval < 0) |
| 1397 | printk(KERN_WARNING DRIVER_NAME |
| 1398 | ": get tuner failed with %d\n", retval); |
| 1399 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | |
| 1403 | /* |
| 1404 | * si470x_vidioc_s_tuner - set tuner attributes |
| 1405 | */ |
| 1406 | static int si470x_vidioc_s_tuner(struct file *file, void *priv, |
| 1407 | struct v4l2_tuner *tuner) |
| 1408 | { |
| 1409 | struct si470x_device *radio = video_get_drvdata(video_devdata(file)); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1410 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1411 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1412 | /* safety checks */ |
| 1413 | if (radio->disconnected) { |
| 1414 | retval = -EIO; |
| 1415 | goto done; |
| 1416 | } |
| 1417 | if ((tuner->index != 0) && (tuner->type != V4L2_TUNER_RADIO)) { |
| 1418 | retval = -EINVAL; |
| 1419 | goto done; |
| 1420 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1421 | |
| 1422 | if (tuner->audmode == V4L2_TUNER_MODE_MONO) |
| 1423 | radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */ |
| 1424 | else |
| 1425 | radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */ |
| 1426 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1427 | retval = si470x_set_register(radio, POWERCFG); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1428 | |
| 1429 | done: |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1430 | if (retval < 0) |
| 1431 | printk(KERN_WARNING DRIVER_NAME |
| 1432 | ": set tuner failed with %d\n", retval); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1433 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | /* |
| 1438 | * si470x_vidioc_g_frequency - get tuner or modulator radio frequency |
| 1439 | */ |
| 1440 | static int si470x_vidioc_g_frequency(struct file *file, void *priv, |
| 1441 | struct v4l2_frequency *freq) |
| 1442 | { |
| 1443 | struct si470x_device *radio = video_get_drvdata(video_devdata(file)); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1444 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1445 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1446 | /* safety checks */ |
| 1447 | if (radio->disconnected) { |
| 1448 | retval = -EIO; |
| 1449 | goto done; |
| 1450 | } |
| 1451 | if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) { |
| 1452 | retval = -EINVAL; |
| 1453 | goto done; |
| 1454 | } |
Tobias Lorenz | 03dea86 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1455 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1456 | retval = si470x_get_freq(radio, &freq->frequency); |
| 1457 | |
| 1458 | done: |
| 1459 | if (retval < 0) |
| 1460 | printk(KERN_WARNING DRIVER_NAME |
| 1461 | ": get frequency failed with %d\n", retval); |
| 1462 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | |
| 1466 | /* |
| 1467 | * si470x_vidioc_s_frequency - set tuner or modulator radio frequency |
| 1468 | */ |
| 1469 | static int si470x_vidioc_s_frequency(struct file *file, void *priv, |
| 1470 | struct v4l2_frequency *freq) |
| 1471 | { |
| 1472 | struct si470x_device *radio = video_get_drvdata(video_devdata(file)); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1473 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1474 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1475 | /* safety checks */ |
| 1476 | if (radio->disconnected) { |
| 1477 | retval = -EIO; |
| 1478 | goto done; |
| 1479 | } |
| 1480 | if ((freq->tuner != 0) && (freq->type != V4L2_TUNER_RADIO)) { |
| 1481 | retval = -EINVAL; |
| 1482 | goto done; |
| 1483 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1484 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1485 | retval = si470x_set_freq(radio, freq->frequency); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1486 | |
| 1487 | done: |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1488 | if (retval < 0) |
| 1489 | printk(KERN_WARNING DRIVER_NAME |
| 1490 | ": set frequency failed with %d\n", retval); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1491 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1495 | /* |
| 1496 | * si470x_viddev_tamples - video device interface |
| 1497 | */ |
| 1498 | static struct video_device si470x_viddev_template = { |
| 1499 | .fops = &si470x_fops, |
| 1500 | .name = DRIVER_NAME, |
| 1501 | .type = VID_TYPE_TUNER, |
| 1502 | .release = video_device_release, |
| 1503 | .vidioc_querycap = si470x_vidioc_querycap, |
| 1504 | .vidioc_g_input = si470x_vidioc_g_input, |
| 1505 | .vidioc_s_input = si470x_vidioc_s_input, |
| 1506 | .vidioc_queryctrl = si470x_vidioc_queryctrl, |
| 1507 | .vidioc_g_ctrl = si470x_vidioc_g_ctrl, |
| 1508 | .vidioc_s_ctrl = si470x_vidioc_s_ctrl, |
| 1509 | .vidioc_g_audio = si470x_vidioc_g_audio, |
| 1510 | .vidioc_s_audio = si470x_vidioc_s_audio, |
| 1511 | .vidioc_g_tuner = si470x_vidioc_g_tuner, |
| 1512 | .vidioc_s_tuner = si470x_vidioc_s_tuner, |
| 1513 | .vidioc_g_frequency = si470x_vidioc_g_frequency, |
| 1514 | .vidioc_s_frequency = si470x_vidioc_s_frequency, |
| 1515 | .owner = THIS_MODULE, |
| 1516 | }; |
| 1517 | |
| 1518 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1519 | |
| 1520 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1521 | * USB Interface |
| 1522 | **************************************************************************/ |
| 1523 | |
| 1524 | /* |
| 1525 | * si470x_usb_driver_probe - probe for the device |
| 1526 | */ |
| 1527 | static int si470x_usb_driver_probe(struct usb_interface *intf, |
| 1528 | const struct usb_device_id *id) |
| 1529 | { |
| 1530 | struct si470x_device *radio; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1531 | int retval = 0; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1532 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1533 | /* private data allocation and initialization */ |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1534 | radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1535 | if (!radio) { |
| 1536 | retval = -ENOMEM; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1537 | goto err_initial; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1538 | } |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1539 | radio->users = 0; |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1540 | radio->disconnected = 0; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1541 | radio->usbdev = interface_to_usbdev(intf); |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1542 | radio->intf = intf; |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1543 | mutex_init(&radio->disconnect_lock); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1544 | mutex_init(&radio->lock); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1545 | |
| 1546 | /* video device allocation and initialization */ |
| 1547 | radio->videodev = video_device_alloc(); |
| 1548 | if (!radio->videodev) { |
| 1549 | retval = -ENOMEM; |
| 1550 | goto err_radio; |
| 1551 | } |
| 1552 | memcpy(radio->videodev, &si470x_viddev_template, |
| 1553 | sizeof(si470x_viddev_template)); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1554 | video_set_drvdata(radio->videodev, radio); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1555 | |
| 1556 | /* show some infos about the specific device */ |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1557 | if (si470x_get_all_registers(radio) < 0) { |
| 1558 | retval = -EIO; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1559 | goto err_all; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1560 | } |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1561 | printk(KERN_INFO DRIVER_NAME ": DeviceID=0x%4.4hx ChipID=0x%4.4hx\n", |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1562 | radio->registers[DEVICEID], radio->registers[CHIPID]); |
| 1563 | |
| 1564 | /* check if firmware is current */ |
| 1565 | if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1566 | < RADIO_SW_VERSION_CURRENT) { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1567 | printk(KERN_WARNING DRIVER_NAME |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1568 | ": This driver is known to work with " |
| 1569 | "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT); |
| 1570 | printk(KERN_WARNING DRIVER_NAME |
| 1571 | ": but the device has firmware version %hu.\n", |
| 1572 | radio->registers[CHIPID] & CHIPID_FIRMWARE); |
| 1573 | printk(KERN_WARNING DRIVER_NAME |
| 1574 | ": If you have some trouble using this driver,\n"); |
| 1575 | printk(KERN_WARNING DRIVER_NAME |
| 1576 | ": please report to V4L ML at " |
| 1577 | "video4linux-list@redhat.com\n"); |
| 1578 | } |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1579 | |
| 1580 | /* set initial frequency */ |
| 1581 | si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */ |
| 1582 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1583 | /* rds buffer allocation */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1584 | radio->buf_size = rds_buf * 3; |
| 1585 | radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1586 | if (!radio->buffer) { |
| 1587 | retval = -EIO; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1588 | goto err_all; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1589 | } |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1590 | |
| 1591 | /* rds buffer configuration */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1592 | radio->wr_index = 0; |
| 1593 | radio->rd_index = 0; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1594 | init_waitqueue_head(&radio->read_queue); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1595 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1596 | /* prepare rds work function */ |
| 1597 | INIT_DELAYED_WORK(&radio->work, si470x_work); |
| 1598 | |
| 1599 | /* register video device */ |
| 1600 | if (video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr)) { |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame^] | 1601 | retval = -EIO; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1602 | printk(KERN_WARNING DRIVER_NAME |
| 1603 | ": Could not register video device\n"); |
| 1604 | goto err_all; |
| 1605 | } |
| 1606 | usb_set_intfdata(intf, radio); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1607 | |
| 1608 | return 0; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1609 | err_all: |
| 1610 | video_device_release(radio->videodev); |
| 1611 | kfree(radio->buffer); |
| 1612 | err_radio: |
| 1613 | kfree(radio); |
| 1614 | err_initial: |
| 1615 | return retval; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | |
| 1619 | /* |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1620 | * si470x_usb_driver_suspend - suspend the device |
| 1621 | */ |
| 1622 | static int si470x_usb_driver_suspend(struct usb_interface *intf, |
| 1623 | pm_message_t message) |
| 1624 | { |
| 1625 | struct si470x_device *radio = usb_get_intfdata(intf); |
| 1626 | |
| 1627 | printk(KERN_INFO DRIVER_NAME ": suspending now...\n"); |
| 1628 | |
| 1629 | cancel_delayed_work_sync(&radio->work); |
| 1630 | |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
| 1634 | |
| 1635 | /* |
| 1636 | * si470x_usb_driver_resume - resume the device |
| 1637 | */ |
| 1638 | static int si470x_usb_driver_resume(struct usb_interface *intf) |
| 1639 | { |
| 1640 | struct si470x_device *radio = usb_get_intfdata(intf); |
| 1641 | |
| 1642 | printk(KERN_INFO DRIVER_NAME ": resuming now...\n"); |
| 1643 | |
| 1644 | mutex_lock(&radio->lock); |
| 1645 | if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) |
| 1646 | schedule_delayed_work(&radio->work, |
| 1647 | msecs_to_jiffies(rds_poll_time)); |
| 1648 | mutex_unlock(&radio->lock); |
| 1649 | |
| 1650 | return 0; |
| 1651 | } |
| 1652 | |
| 1653 | |
| 1654 | /* |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1655 | * si470x_usb_driver_disconnect - disconnect the device |
| 1656 | */ |
| 1657 | static void si470x_usb_driver_disconnect(struct usb_interface *intf) |
| 1658 | { |
| 1659 | struct si470x_device *radio = usb_get_intfdata(intf); |
| 1660 | |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1661 | mutex_lock(&radio->disconnect_lock); |
| 1662 | radio->disconnected = 1; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1663 | cancel_delayed_work_sync(&radio->work); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1664 | usb_set_intfdata(intf, NULL); |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1665 | if (radio->users == 0) { |
| 1666 | video_unregister_device(radio->videodev); |
| 1667 | kfree(radio->buffer); |
| 1668 | kfree(radio); |
| 1669 | } |
| 1670 | mutex_unlock(&radio->disconnect_lock); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | |
| 1674 | /* |
| 1675 | * si470x_usb_driver - usb driver interface |
| 1676 | */ |
| 1677 | static struct usb_driver si470x_usb_driver = { |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1678 | .name = DRIVER_NAME, |
| 1679 | .probe = si470x_usb_driver_probe, |
| 1680 | .disconnect = si470x_usb_driver_disconnect, |
| 1681 | .suspend = si470x_usb_driver_suspend, |
| 1682 | .resume = si470x_usb_driver_resume, |
| 1683 | .id_table = si470x_usb_driver_id_table, |
| 1684 | .supports_autosuspend = 1, |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1685 | }; |
| 1686 | |
| 1687 | |
| 1688 | |
| 1689 | /************************************************************************** |
| 1690 | * Module Interface |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1691 | **************************************************************************/ |
| 1692 | |
| 1693 | /* |
| 1694 | * si470x_module_init - module init |
| 1695 | */ |
| 1696 | static int __init si470x_module_init(void) |
| 1697 | { |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1698 | printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n"); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1699 | return usb_register(&si470x_usb_driver); |
| 1700 | } |
| 1701 | |
| 1702 | |
| 1703 | /* |
| 1704 | * si470x_module_exit - module exit |
| 1705 | */ |
| 1706 | static void __exit si470x_module_exit(void) |
| 1707 | { |
| 1708 | usb_deregister(&si470x_usb_driver); |
| 1709 | } |
| 1710 | |
| 1711 | |
| 1712 | module_init(si470x_module_init); |
| 1713 | module_exit(si470x_module_exit); |
| 1714 | |
| 1715 | MODULE_LICENSE("GPL"); |
| 1716 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1717 | MODULE_DESCRIPTION(DRIVER_DESC); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1718 | MODULE_VERSION(DRIVER_VERSION); |