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