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