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 | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 109 | * |
| 110 | * ToDo: |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 111 | * - add firmware download/update support |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 112 | * - RDS support: interrupt mode, instead of polling |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 113 | */ |
| 114 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 115 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 116 | /* driver definitions */ |
| 117 | #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>" |
| 118 | #define DRIVER_NAME "radio-si470x" |
Tobias Lorenz | 10711d0 | 2009-02-12 14:56:30 -0300 | [diff] [blame] | 119 | #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 9) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 120 | #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver" |
| 121 | #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers" |
Tobias Lorenz | 10711d0 | 2009-02-12 14:56:30 -0300 | [diff] [blame] | 122 | #define DRIVER_VERSION "1.0.9" |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 123 | |
| 124 | |
| 125 | /* kernel includes */ |
| 126 | #include <linux/kernel.h> |
| 127 | #include <linux/module.h> |
| 128 | #include <linux/init.h> |
| 129 | #include <linux/slab.h> |
| 130 | #include <linux/input.h> |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 131 | #include <linux/usb.h> |
| 132 | #include <linux/hid.h> |
| 133 | #include <linux/version.h> |
Mauro Carvalho Chehab | 387d447 | 2008-01-15 11:25:10 -0300 | [diff] [blame] | 134 | #include <linux/videodev2.h> |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 135 | #include <linux/mutex.h> |
Mauro Carvalho Chehab | 387d447 | 2008-01-15 11:25:10 -0300 | [diff] [blame] | 136 | #include <media/v4l2-common.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 137 | #include <media/v4l2-ioctl.h> |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 138 | #include <media/rds.h> |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 139 | #include <asm/unaligned.h> |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 140 | |
| 141 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 142 | /* USB Device ID List */ |
| 143 | static struct usb_device_id si470x_usb_driver_id_table[] = { |
| 144 | /* Silicon Labs USB FM Radio Reference Design */ |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 145 | { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) }, |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 146 | /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */ |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 147 | { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID, 0, 0) }, |
Alexey Klimov | 69df96c | 2008-10-23 09:20:27 -0300 | [diff] [blame] | 148 | /* KWorld USB FM Radio SnapMusic Mobile 700 (FM700) */ |
| 149 | { USB_DEVICE_AND_INTERFACE_INFO(0x1b80, 0xd700, USB_CLASS_HID, 0, 0) }, |
Tobias Lorenz | 00ec8d0 | 2009-02-12 14:55:45 -0300 | [diff] [blame] | 150 | /* Sanei Electric, Inc. FM USB Radio (sold as DealExtreme.com PCear) */ |
Mark Lord | 5e6de7d | 2008-12-03 15:26:15 -0300 | [diff] [blame] | 151 | { USB_DEVICE_AND_INTERFACE_INFO(0x10c5, 0x819a, USB_CLASS_HID, 0, 0) }, |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 152 | /* Terminating entry */ |
| 153 | { } |
| 154 | }; |
| 155 | MODULE_DEVICE_TABLE(usb, si470x_usb_driver_id_table); |
| 156 | |
| 157 | |
| 158 | |
| 159 | /************************************************************************** |
| 160 | * Module Parameters |
| 161 | **************************************************************************/ |
| 162 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 163 | /* Radio Nr */ |
| 164 | static int radio_nr = -1; |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 165 | module_param(radio_nr, int, 0444); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 166 | MODULE_PARM_DESC(radio_nr, "Radio Nr"); |
| 167 | |
| 168 | /* Spacing (kHz) */ |
| 169 | /* 0: 200 kHz (USA, Australia) */ |
| 170 | /* 1: 100 kHz (Europe, Japan) */ |
| 171 | /* 2: 50 kHz */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 172 | static unsigned short space = 2; |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 173 | module_param(space, ushort, 0444); |
| 174 | MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*"); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 175 | |
| 176 | /* Bottom of Band (MHz) */ |
| 177 | /* 0: 87.5 - 108 MHz (USA, Europe)*/ |
| 178 | /* 1: 76 - 108 MHz (Japan wide band) */ |
| 179 | /* 2: 76 - 90 MHz (Japan) */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 180 | static unsigned short band = 1; |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 181 | module_param(band, ushort, 0444); |
| 182 | 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] | 183 | |
| 184 | /* De-emphasis */ |
| 185 | /* 0: 75 us (USA) */ |
| 186 | /* 1: 50 us (Europe, Australia, Japan) */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 187 | static unsigned short de = 1; |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 188 | module_param(de, ushort, 0444); |
| 189 | MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*"); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 190 | |
| 191 | /* USB timeout */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 192 | static unsigned int usb_timeout = 500; |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 193 | module_param(usb_timeout, uint, 0644); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 194 | MODULE_PARM_DESC(usb_timeout, "USB timeout (ms): *500*"); |
| 195 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 196 | /* Tune timeout */ |
| 197 | static unsigned int tune_timeout = 3000; |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 198 | module_param(tune_timeout, uint, 0644); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 199 | MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*"); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 200 | |
Tobias Lorenz | bbc8995 | 2008-05-31 15:11:32 -0300 | [diff] [blame] | 201 | /* Seek timeout */ |
| 202 | static unsigned int seek_timeout = 5000; |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 203 | module_param(seek_timeout, uint, 0644); |
Tobias Lorenz | bbc8995 | 2008-05-31 15:11:32 -0300 | [diff] [blame] | 204 | MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*"); |
| 205 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 206 | /* RDS buffer blocks */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 207 | static unsigned int rds_buf = 100; |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 208 | module_param(rds_buf, uint, 0444); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 209 | MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*"); |
| 210 | |
| 211 | /* RDS maximum block errors */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 212 | static unsigned short max_rds_errors = 1; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 213 | /* 0 means 0 errors requiring correction */ |
| 214 | /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */ |
| 215 | /* 2 means 3-5 errors requiring correction */ |
| 216 | /* 3 means 6+ errors or errors in checkword, correction not possible */ |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 217 | module_param(max_rds_errors, ushort, 0644); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 218 | MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*"); |
| 219 | |
| 220 | /* RDS poll frequency */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 221 | static unsigned int rds_poll_time = 40; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 222 | /* 40 is used by the original USBRadio.exe */ |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 223 | /* 50 is used by radio-cadet */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 224 | /* 75 should be okay */ |
| 225 | /* 80 is the usual RDS receive interval */ |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 226 | module_param(rds_poll_time, uint, 0644); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 227 | MODULE_PARM_DESC(rds_poll_time, "RDS poll time (ms): *40*"); |
| 228 | |
| 229 | |
| 230 | |
| 231 | /************************************************************************** |
| 232 | * Register Definitions |
| 233 | **************************************************************************/ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 234 | #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */ |
| 235 | #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 236 | #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */ |
| 237 | |
| 238 | #define DEVICEID 0 /* Device ID */ |
| 239 | #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */ |
| 240 | #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */ |
| 241 | |
| 242 | #define CHIPID 1 /* Chip ID */ |
| 243 | #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */ |
| 244 | #define CHIPID_DEV 0x0200 /* bits 09..09: Device */ |
| 245 | #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */ |
| 246 | |
| 247 | #define POWERCFG 2 /* Power Configuration */ |
| 248 | #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */ |
| 249 | #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */ |
| 250 | #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */ |
| 251 | #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */ |
| 252 | #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */ |
| 253 | #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */ |
| 254 | #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */ |
| 255 | #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */ |
| 256 | #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */ |
| 257 | |
| 258 | #define CHANNEL 3 /* Channel */ |
| 259 | #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */ |
| 260 | #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */ |
| 261 | |
| 262 | #define SYSCONFIG1 4 /* System Configuration 1 */ |
| 263 | #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */ |
| 264 | #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */ |
| 265 | #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */ |
| 266 | #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */ |
| 267 | #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */ |
| 268 | #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */ |
| 269 | #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */ |
| 270 | #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */ |
| 271 | #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */ |
| 272 | |
| 273 | #define SYSCONFIG2 5 /* System Configuration 2 */ |
| 274 | #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */ |
| 275 | #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */ |
| 276 | #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */ |
| 277 | #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */ |
| 278 | |
| 279 | #define SYSCONFIG3 6 /* System Configuration 3 */ |
| 280 | #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */ |
| 281 | #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */ |
| 282 | #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */ |
| 283 | #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */ |
| 284 | |
| 285 | #define TEST1 7 /* Test 1 */ |
| 286 | #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */ |
| 287 | |
| 288 | #define TEST2 8 /* Test 2 */ |
| 289 | /* TEST2 only contains reserved bits */ |
| 290 | |
| 291 | #define BOOTCONFIG 9 /* Boot Configuration */ |
| 292 | /* BOOTCONFIG only contains reserved bits */ |
| 293 | |
| 294 | #define STATUSRSSI 10 /* Status RSSI */ |
| 295 | #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */ |
| 296 | #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */ |
| 297 | #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */ |
| 298 | #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */ |
| 299 | #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */ |
| 300 | #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */ |
| 301 | #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */ |
| 302 | #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */ |
| 303 | |
| 304 | #define READCHAN 11 /* Read Channel */ |
| 305 | #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */ |
| 306 | #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */ |
| 307 | #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */ |
| 308 | #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */ |
| 309 | |
| 310 | #define RDSA 12 /* RDSA */ |
| 311 | #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */ |
| 312 | |
| 313 | #define RDSB 13 /* RDSB */ |
| 314 | #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */ |
| 315 | |
| 316 | #define RDSC 14 /* RDSC */ |
| 317 | #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */ |
| 318 | |
| 319 | #define RDSD 15 /* RDSD */ |
| 320 | #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */ |
| 321 | |
| 322 | |
| 323 | |
| 324 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 325 | * USB HID Reports |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 326 | **************************************************************************/ |
| 327 | |
| 328 | /* Reports 1-16 give direct read/write access to the 16 Si470x registers */ |
| 329 | /* with the (REPORT_ID - 1) corresponding to the register address across USB */ |
| 330 | /* endpoint 0 using GET_REPORT and SET_REPORT */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 331 | #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 332 | #define REGISTER_REPORT(reg) ((reg) + 1) |
| 333 | |
| 334 | /* Report 17 gives direct read/write access to the entire Si470x register */ |
| 335 | /* map across endpoint 0 using GET_REPORT and SET_REPORT */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 336 | #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 337 | #define ENTIRE_REPORT 17 |
| 338 | |
| 339 | /* Report 18 is used to send the lowest 6 Si470x registers up the HID */ |
| 340 | /* interrupt endpoint 1 to Windows every 20 milliseconds for status */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 341 | #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 342 | #define RDS_REPORT 18 |
| 343 | |
| 344 | /* Report 19: LED state */ |
| 345 | #define LED_REPORT_SIZE 3 |
| 346 | #define LED_REPORT 19 |
| 347 | |
| 348 | /* Report 19: stream */ |
| 349 | #define STREAM_REPORT_SIZE 3 |
Tobias Lorenz | 00ec8d0 | 2009-02-12 14:55:45 -0300 | [diff] [blame] | 350 | #define STREAM_REPORT 19 |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 351 | |
| 352 | /* Report 20: scratch */ |
| 353 | #define SCRATCH_PAGE_SIZE 63 |
| 354 | #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1) |
| 355 | #define SCRATCH_REPORT 20 |
| 356 | |
| 357 | /* Reports 19-22: flash upgrade of the C8051F321 */ |
Tobias Lorenz | 10711d0 | 2009-02-12 14:56:30 -0300 | [diff] [blame] | 358 | #define WRITE_REPORT_SIZE 4 |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 359 | #define WRITE_REPORT 19 |
Tobias Lorenz | 10711d0 | 2009-02-12 14:56:30 -0300 | [diff] [blame] | 360 | #define FLASH_REPORT_SIZE 64 |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 361 | #define FLASH_REPORT 20 |
Tobias Lorenz | 10711d0 | 2009-02-12 14:56:30 -0300 | [diff] [blame] | 362 | #define CRC_REPORT_SIZE 3 |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 363 | #define CRC_REPORT 21 |
Tobias Lorenz | 10711d0 | 2009-02-12 14:56:30 -0300 | [diff] [blame] | 364 | #define RESPONSE_REPORT_SIZE 2 |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 365 | #define RESPONSE_REPORT 22 |
| 366 | |
| 367 | /* Report 23: currently unused, but can accept 60 byte reports on the HID */ |
| 368 | /* interrupt out endpoint 2 every 1 millisecond */ |
| 369 | #define UNUSED_REPORT 23 |
| 370 | |
| 371 | |
| 372 | |
| 373 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 374 | * Software/Hardware Versions |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 375 | **************************************************************************/ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 376 | #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6 |
| 377 | #define RADIO_SW_VERSION 7 |
| 378 | #define RADIO_SW_VERSION_CURRENT 15 |
| 379 | #define RADIO_HW_VERSION 1 |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 380 | |
| 381 | #define SCRATCH_PAGE_SW_VERSION 1 |
| 382 | #define SCRATCH_PAGE_HW_VERSION 2 |
| 383 | |
| 384 | |
| 385 | |
| 386 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 387 | * LED State Definitions |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 388 | **************************************************************************/ |
| 389 | #define LED_COMMAND 0x35 |
| 390 | |
| 391 | #define NO_CHANGE_LED 0x00 |
| 392 | #define ALL_COLOR_LED 0x01 /* streaming state */ |
| 393 | #define BLINK_GREEN_LED 0x02 /* connect state */ |
| 394 | #define BLINK_RED_LED 0x04 |
| 395 | #define BLINK_ORANGE_LED 0x10 /* disconnect state */ |
| 396 | #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */ |
| 397 | #define SOLID_RED_LED 0x40 /* bootload state */ |
| 398 | #define SOLID_ORANGE_LED 0x80 |
| 399 | |
| 400 | |
| 401 | |
| 402 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 403 | * Stream State Definitions |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 404 | **************************************************************************/ |
| 405 | #define STREAM_COMMAND 0x36 |
| 406 | #define STREAM_VIDPID 0x00 |
| 407 | #define STREAM_AUDIO 0xff |
| 408 | |
| 409 | |
| 410 | |
| 411 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 412 | * Bootloader / Flash Commands |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 413 | **************************************************************************/ |
| 414 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 415 | /* 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] | 416 | #define UNIQUE_BL_ID 0x34 |
| 417 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 418 | /* mask for the flash data */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 419 | #define FLASH_DATA_MASK 0x55 |
| 420 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 421 | /* bootloader commands */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 422 | #define GET_SW_VERSION_COMMAND 0x00 |
Tobias Lorenz | 00ec8d0 | 2009-02-12 14:55:45 -0300 | [diff] [blame] | 423 | #define SET_PAGE_COMMAND 0x01 |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 424 | #define ERASE_PAGE_COMMAND 0x02 |
| 425 | #define WRITE_PAGE_COMMAND 0x03 |
| 426 | #define CRC_ON_PAGE_COMMAND 0x04 |
| 427 | #define READ_FLASH_BYTE_COMMAND 0x05 |
| 428 | #define RESET_DEVICE_COMMAND 0x06 |
| 429 | #define GET_HW_VERSION_COMMAND 0x07 |
| 430 | #define BLANK 0xff |
| 431 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 432 | /* bootloader command responses */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 433 | #define COMMAND_OK 0x01 |
| 434 | #define COMMAND_FAILED 0x02 |
| 435 | #define COMMAND_PENDING 0x03 |
| 436 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 437 | |
| 438 | |
| 439 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 440 | * General Driver Definitions |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 441 | **************************************************************************/ |
| 442 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 443 | /* |
| 444 | * si470x_device - private data |
| 445 | */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 446 | struct si470x_device { |
| 447 | /* reference to USB and video device */ |
| 448 | struct usb_device *usbdev; |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 449 | struct usb_interface *intf; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 450 | struct video_device *videodev; |
| 451 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 452 | /* driver management */ |
| 453 | unsigned int users; |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 454 | unsigned char disconnected; |
| 455 | struct mutex disconnect_lock; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 456 | |
| 457 | /* Silabs internal registers (0..15) */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 458 | unsigned short registers[RADIO_REGISTER_NUM]; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 459 | |
| 460 | /* RDS receive buffer */ |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 461 | struct delayed_work work; |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 462 | wait_queue_head_t read_queue; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 463 | struct mutex lock; /* buffer locking */ |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 464 | unsigned char *buffer; /* size is always multiple of three */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 465 | unsigned int buf_size; |
| 466 | unsigned int rd_index; |
| 467 | unsigned int wr_index; |
Tobias Lorenz | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | a17c001 | 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 | /************************************************************************** |
| 867 | * General Driver Functions - RDS_REPORT |
| 868 | **************************************************************************/ |
| 869 | |
| 870 | /* |
| 871 | * si470x_get_rds_registers - read rds registers |
| 872 | */ |
| 873 | static int si470x_get_rds_registers(struct si470x_device *radio) |
| 874 | { |
| 875 | unsigned char buf[RDS_REPORT_SIZE]; |
| 876 | int retval; |
| 877 | int size; |
| 878 | unsigned char regnr; |
| 879 | |
| 880 | buf[0] = RDS_REPORT; |
| 881 | |
| 882 | retval = usb_interrupt_msg(radio->usbdev, |
| 883 | usb_rcvintpipe(radio->usbdev, 1), |
| 884 | (void *) &buf, sizeof(buf), &size, usb_timeout); |
| 885 | if (size != sizeof(buf)) |
| 886 | printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: " |
| 887 | "return size differs: %d != %zu\n", size, sizeof(buf)); |
| 888 | if (retval < 0) |
| 889 | printk(KERN_WARNING DRIVER_NAME ": si470x_get_rds_registers: " |
| 890 | "usb_interrupt_msg returned %d\n", retval); |
| 891 | |
| 892 | if (retval >= 0) |
| 893 | for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++) |
| 894 | radio->registers[STATUSRSSI + regnr] = |
| 895 | get_unaligned_be16( |
| 896 | &buf[regnr * RADIO_REGISTER_SIZE + 1]); |
| 897 | |
| 898 | return (retval < 0) ? -EINVAL : 0; |
| 899 | } |
| 900 | |
| 901 | |
| 902 | |
| 903 | /************************************************************************** |
Tobias Lorenz | 10711d0 | 2009-02-12 14:56:30 -0300 | [diff] [blame] | 904 | * General Driver Functions - LED_REPORT |
| 905 | **************************************************************************/ |
| 906 | |
| 907 | /* |
| 908 | * si470x_set_led_state - sets the led state |
| 909 | */ |
| 910 | static int si470x_set_led_state(struct si470x_device *radio, |
| 911 | unsigned char led_state) |
| 912 | { |
| 913 | unsigned char buf[LED_REPORT_SIZE]; |
| 914 | int retval; |
| 915 | |
| 916 | buf[0] = LED_REPORT; |
| 917 | buf[1] = LED_COMMAND; |
| 918 | buf[2] = led_state; |
| 919 | |
| 920 | retval = si470x_set_report(radio, (void *) &buf, sizeof(buf)); |
| 921 | |
| 922 | return (retval < 0) ? -EINVAL : 0; |
| 923 | } |
| 924 | |
| 925 | |
| 926 | |
| 927 | /************************************************************************** |
| 928 | * General Driver Functions - SCRATCH_REPORT |
| 929 | **************************************************************************/ |
| 930 | |
| 931 | /* |
| 932 | * si470x_get_scratch_versions - gets the scratch page and version infos |
| 933 | */ |
| 934 | static int si470x_get_scratch_page_versions(struct si470x_device *radio) |
| 935 | { |
| 936 | unsigned char buf[SCRATCH_REPORT_SIZE]; |
| 937 | int retval; |
| 938 | |
| 939 | buf[0] = SCRATCH_REPORT; |
| 940 | |
| 941 | retval = si470x_get_report(radio, (void *) &buf, sizeof(buf)); |
| 942 | |
| 943 | if (retval < 0) |
| 944 | printk(KERN_WARNING DRIVER_NAME ": si470x_get_scratch: " |
| 945 | "si470x_get_report returned %d\n", retval); |
| 946 | else { |
| 947 | radio->software_version = buf[1]; |
| 948 | radio->hardware_version = buf[2]; |
| 949 | } |
| 950 | |
| 951 | return (retval < 0) ? -EINVAL : 0; |
| 952 | } |
| 953 | |
| 954 | |
| 955 | |
| 956 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 957 | * RDS Driver Functions |
| 958 | **************************************************************************/ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 959 | |
| 960 | /* |
| 961 | * si470x_rds - rds processing function |
| 962 | */ |
| 963 | static void si470x_rds(struct si470x_device *radio) |
| 964 | { |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 965 | unsigned char blocknum; |
| 966 | unsigned short bler; /* rds block errors */ |
| 967 | unsigned short rds; |
| 968 | unsigned char tmpbuf[3]; |
| 969 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 970 | /* get rds blocks */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 971 | if (si470x_get_rds_registers(radio) < 0) |
| 972 | return; |
| 973 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) { |
| 974 | /* No RDS group ready */ |
| 975 | return; |
| 976 | } |
| 977 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSS) == 0) { |
| 978 | /* RDS decoder not synchronized */ |
| 979 | return; |
| 980 | } |
| 981 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 982 | /* copy all four RDS blocks to internal buffer */ |
| 983 | mutex_lock(&radio->lock); |
| 984 | for (blocknum = 0; blocknum < 4; blocknum++) { |
| 985 | switch (blocknum) { |
| 986 | default: |
| 987 | bler = (radio->registers[STATUSRSSI] & |
| 988 | STATUSRSSI_BLERA) >> 9; |
| 989 | rds = radio->registers[RDSA]; |
| 990 | break; |
| 991 | case 1: |
| 992 | bler = (radio->registers[READCHAN] & |
| 993 | READCHAN_BLERB) >> 14; |
| 994 | rds = radio->registers[RDSB]; |
| 995 | break; |
| 996 | case 2: |
| 997 | bler = (radio->registers[READCHAN] & |
| 998 | READCHAN_BLERC) >> 12; |
| 999 | rds = radio->registers[RDSC]; |
| 1000 | break; |
| 1001 | case 3: |
| 1002 | bler = (radio->registers[READCHAN] & |
| 1003 | READCHAN_BLERD) >> 10; |
| 1004 | rds = radio->registers[RDSD]; |
| 1005 | break; |
| 1006 | }; |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1007 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1008 | /* Fill the V4L2 RDS buffer */ |
Harvey Harrison | 5ad6b81 | 2008-04-15 22:22:11 -0300 | [diff] [blame] | 1009 | put_unaligned_le16(rds, &tmpbuf); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1010 | tmpbuf[2] = blocknum; /* offset name */ |
| 1011 | tmpbuf[2] |= blocknum << 3; /* received offset */ |
| 1012 | if (bler > max_rds_errors) |
| 1013 | tmpbuf[2] |= 0x80; /* uncorrectable errors */ |
| 1014 | else if (bler > 0) |
| 1015 | tmpbuf[2] |= 0x40; /* corrected error(s) */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1016 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1017 | /* copy RDS block to internal buffer */ |
| 1018 | memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3); |
| 1019 | radio->wr_index += 3; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1020 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1021 | /* wrap write pointer */ |
| 1022 | if (radio->wr_index >= radio->buf_size) |
| 1023 | radio->wr_index = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1024 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1025 | /* check for overflow */ |
| 1026 | if (radio->wr_index == radio->rd_index) { |
| 1027 | /* increment and wrap read pointer */ |
| 1028 | radio->rd_index += 3; |
| 1029 | if (radio->rd_index >= radio->buf_size) |
| 1030 | radio->rd_index = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1031 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1032 | } |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1033 | mutex_unlock(&radio->lock); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1034 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1035 | /* wake up read queue */ |
| 1036 | if (radio->wr_index != radio->rd_index) |
| 1037 | wake_up_interruptible(&radio->read_queue); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | |
| 1041 | /* |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1042 | * si470x_work - rds work function |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1043 | */ |
| 1044 | static void si470x_work(struct work_struct *work) |
| 1045 | { |
| 1046 | struct si470x_device *radio = container_of(work, struct si470x_device, |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1047 | work.work); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1048 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1049 | /* safety checks */ |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1050 | if (radio->disconnected) |
| 1051 | return; |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1052 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1053 | return; |
| 1054 | |
| 1055 | si470x_rds(radio); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1056 | schedule_delayed_work(&radio->work, msecs_to_jiffies(rds_poll_time)); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1062 | * File Operations Interface |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1063 | **************************************************************************/ |
| 1064 | |
| 1065 | /* |
| 1066 | * si470x_fops_read - read RDS data |
| 1067 | */ |
| 1068 | static ssize_t si470x_fops_read(struct file *file, char __user *buf, |
| 1069 | size_t count, loff_t *ppos) |
| 1070 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1071 | struct si470x_device *radio = video_drvdata(file); |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1072 | int retval = 0; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1073 | unsigned int block_count = 0; |
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 | /* switch on rds reception */ |
| 1076 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) { |
| 1077 | si470x_rds_on(radio); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1078 | schedule_delayed_work(&radio->work, |
| 1079 | msecs_to_jiffies(rds_poll_time)); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1080 | } |
| 1081 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1082 | /* block if no new data available */ |
| 1083 | while (radio->wr_index == radio->rd_index) { |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1084 | if (file->f_flags & O_NONBLOCK) { |
| 1085 | retval = -EWOULDBLOCK; |
| 1086 | goto done; |
| 1087 | } |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1088 | if (wait_event_interruptible(radio->read_queue, |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1089 | radio->wr_index != radio->rd_index) < 0) { |
| 1090 | retval = -EINTR; |
| 1091 | goto done; |
| 1092 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1093 | } |
| 1094 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1095 | /* calculate block count from byte count */ |
| 1096 | count /= 3; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1097 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1098 | /* copy RDS block out of internal buffer and to user buffer */ |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1099 | mutex_lock(&radio->lock); |
| 1100 | while (block_count < count) { |
| 1101 | if (radio->rd_index == radio->wr_index) |
| 1102 | break; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1103 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1104 | /* always transfer rds complete blocks */ |
| 1105 | if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3)) |
| 1106 | /* retval = -EFAULT; */ |
| 1107 | break; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1108 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1109 | /* increment and wrap read pointer */ |
| 1110 | radio->rd_index += 3; |
| 1111 | if (radio->rd_index >= radio->buf_size) |
| 1112 | radio->rd_index = 0; |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1113 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1114 | /* increment counters */ |
| 1115 | block_count++; |
| 1116 | buf += 3; |
| 1117 | retval += 3; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1118 | } |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1119 | mutex_unlock(&radio->lock); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1120 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1121 | done: |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -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_poll - poll RDS data |
| 1128 | */ |
| 1129 | static unsigned int si470x_fops_poll(struct file *file, |
| 1130 | struct poll_table_struct *pts) |
| 1131 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1132 | struct si470x_device *radio = video_drvdata(file); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1133 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1134 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1135 | /* switch on rds reception */ |
| 1136 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) { |
| 1137 | si470x_rds_on(radio); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1138 | schedule_delayed_work(&radio->work, |
| 1139 | msecs_to_jiffies(rds_poll_time)); |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1140 | } |
| 1141 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1142 | poll_wait(file, &radio->read_queue, pts); |
| 1143 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1144 | if (radio->rd_index != radio->wr_index) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1145 | retval = POLLIN | POLLRDNORM; |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1146 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1147 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | |
| 1151 | /* |
| 1152 | * si470x_fops_open - file open |
| 1153 | */ |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1154 | static int si470x_fops_open(struct file *file) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1155 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1156 | struct si470x_device *radio = video_drvdata(file); |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1157 | int retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1158 | |
Hans Verkuil | d56dc61 | 2008-07-30 08:43:36 -0300 | [diff] [blame] | 1159 | lock_kernel(); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1160 | radio->users++; |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1161 | |
| 1162 | retval = usb_autopm_get_interface(radio->intf); |
| 1163 | if (retval < 0) { |
| 1164 | radio->users--; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1165 | retval = -EIO; |
| 1166 | goto done; |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | if (radio->users == 1) { |
Tobias Lorenz | 7f53b35 | 2009-02-12 14:55:56 -0300 | [diff] [blame] | 1170 | /* start radio */ |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1171 | retval = si470x_start(radio); |
| 1172 | if (retval < 0) |
| 1173 | usb_autopm_put_interface(radio->intf); |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1174 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1175 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1176 | done: |
Hans Verkuil | d56dc61 | 2008-07-30 08:43:36 -0300 | [diff] [blame] | 1177 | unlock_kernel(); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1178 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | /* |
| 1183 | * si470x_fops_release - file release |
| 1184 | */ |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1185 | static int si470x_fops_release(struct file *file) |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1186 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1187 | struct si470x_device *radio = video_drvdata(file); |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1188 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1189 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1190 | /* safety check */ |
| 1191 | if (!radio) { |
| 1192 | retval = -ENODEV; |
| 1193 | goto done; |
| 1194 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1195 | |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1196 | mutex_lock(&radio->disconnect_lock); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1197 | radio->users--; |
| 1198 | if (radio->users == 0) { |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1199 | if (radio->disconnected) { |
| 1200 | video_unregister_device(radio->videodev); |
| 1201 | kfree(radio->buffer); |
| 1202 | kfree(radio); |
Jiri Slaby | a5ca3a1 | 2009-07-05 15:44:20 -0300 | [diff] [blame] | 1203 | goto unlock; |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1204 | } |
Tobias Lorenz | 03dea86 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1205 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1206 | /* stop rds reception */ |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1207 | cancel_delayed_work_sync(&radio->work); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1208 | |
Tobias Lorenz | 2fb8840 | 2008-01-25 05:14:57 -0300 | [diff] [blame] | 1209 | /* cancel read processes */ |
| 1210 | wake_up_interruptible(&radio->read_queue); |
| 1211 | |
Tobias Lorenz | 7f53b35 | 2009-02-12 14:55:56 -0300 | [diff] [blame] | 1212 | /* stop radio */ |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1213 | retval = si470x_stop(radio); |
| 1214 | usb_autopm_put_interface(radio->intf); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1215 | } |
Jiri Slaby | a5ca3a1 | 2009-07-05 15:44:20 -0300 | [diff] [blame] | 1216 | unlock: |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1217 | mutex_unlock(&radio->disconnect_lock); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1218 | done: |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1219 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | |
| 1223 | /* |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1224 | * si470x_fops - file operations interface |
| 1225 | */ |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1226 | static const struct v4l2_file_operations si470x_fops = { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1227 | .owner = THIS_MODULE, |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1228 | .read = si470x_fops_read, |
| 1229 | .poll = si470x_fops_poll, |
| 1230 | .ioctl = video_ioctl2, |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1231 | .open = si470x_fops_open, |
| 1232 | .release = si470x_fops_release, |
| 1233 | }; |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | /************************************************************************** |
| 1238 | * Video4Linux Interface |
| 1239 | **************************************************************************/ |
| 1240 | |
| 1241 | /* |
| 1242 | * si470x_v4l2_queryctrl - query control |
| 1243 | */ |
| 1244 | static struct v4l2_queryctrl si470x_v4l2_queryctrl[] = { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1245 | { |
| 1246 | .id = V4L2_CID_AUDIO_VOLUME, |
| 1247 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 1248 | .name = "Volume", |
| 1249 | .minimum = 0, |
| 1250 | .maximum = 15, |
| 1251 | .step = 1, |
| 1252 | .default_value = 15, |
| 1253 | }, |
| 1254 | { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1255 | .id = V4L2_CID_AUDIO_MUTE, |
| 1256 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 1257 | .name = "Mute", |
| 1258 | .minimum = 0, |
| 1259 | .maximum = 1, |
| 1260 | .step = 1, |
| 1261 | .default_value = 1, |
| 1262 | }, |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1263 | }; |
| 1264 | |
| 1265 | |
| 1266 | /* |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1267 | * si470x_vidioc_querycap - query device capabilities |
| 1268 | */ |
| 1269 | static int si470x_vidioc_querycap(struct file *file, void *priv, |
| 1270 | struct v4l2_capability *capability) |
| 1271 | { |
Alexey Klimov | dc025d4 | 2009-02-05 22:52:29 -0300 | [diff] [blame] | 1272 | struct si470x_device *radio = video_drvdata(file); |
| 1273 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1274 | strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver)); |
| 1275 | strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card)); |
Alexey Klimov | dc025d4 | 2009-02-05 22:52:29 -0300 | [diff] [blame] | 1276 | usb_make_path(radio->usbdev, capability->bus_info, sizeof(capability->bus_info)); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1277 | capability->version = DRIVER_KERNEL_VERSION; |
Tobias Lorenz | bbc8995 | 2008-05-31 15:11:32 -0300 | [diff] [blame] | 1278 | capability->capabilities = V4L2_CAP_HW_FREQ_SEEK | |
| 1279 | V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1280 | |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | |
| 1285 | /* |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1286 | * si470x_vidioc_queryctrl - enumerate control items |
| 1287 | */ |
| 1288 | static int si470x_vidioc_queryctrl(struct file *file, void *priv, |
| 1289 | struct v4l2_queryctrl *qc) |
| 1290 | { |
Tobias Lorenz | eb6e3b8 | 2008-09-24 19:25:39 -0300 | [diff] [blame] | 1291 | unsigned char i = 0; |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1292 | int retval = -EINVAL; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1293 | |
Tobias Lorenz | eb6e3b8 | 2008-09-24 19:25:39 -0300 | [diff] [blame] | 1294 | /* abort if qc->id is below V4L2_CID_BASE */ |
| 1295 | if (qc->id < V4L2_CID_BASE) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1296 | goto done; |
| 1297 | |
Tobias Lorenz | eb6e3b8 | 2008-09-24 19:25:39 -0300 | [diff] [blame] | 1298 | /* search video control */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1299 | for (i = 0; i < ARRAY_SIZE(si470x_v4l2_queryctrl); i++) { |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1300 | if (qc->id == si470x_v4l2_queryctrl[i].id) { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1301 | memcpy(qc, &(si470x_v4l2_queryctrl[i]), sizeof(*qc)); |
Tobias Lorenz | eb6e3b8 | 2008-09-24 19:25:39 -0300 | [diff] [blame] | 1302 | retval = 0; /* found */ |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1303 | break; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1304 | } |
| 1305 | } |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1306 | |
Tobias Lorenz | eb6e3b8 | 2008-09-24 19:25:39 -0300 | [diff] [blame] | 1307 | /* disable unsupported base controls */ |
| 1308 | /* to satisfy kradio and such apps */ |
| 1309 | if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) { |
| 1310 | qc->flags = V4L2_CTRL_FLAG_DISABLED; |
| 1311 | retval = 0; |
| 1312 | } |
| 1313 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1314 | done: |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1315 | if (retval < 0) |
| 1316 | printk(KERN_WARNING DRIVER_NAME |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1317 | ": query controls failed with %d\n", retval); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1318 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | |
| 1322 | /* |
| 1323 | * si470x_vidioc_g_ctrl - get the value of a control |
| 1324 | */ |
| 1325 | static int si470x_vidioc_g_ctrl(struct file *file, void *priv, |
| 1326 | struct v4l2_control *ctrl) |
| 1327 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1328 | struct si470x_device *radio = video_drvdata(file); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1329 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1330 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1331 | /* safety checks */ |
| 1332 | if (radio->disconnected) { |
| 1333 | retval = -EIO; |
| 1334 | goto done; |
| 1335 | } |
Tobias Lorenz | 03dea86 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1336 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1337 | switch (ctrl->id) { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1338 | case V4L2_CID_AUDIO_VOLUME: |
| 1339 | ctrl->value = radio->registers[SYSCONFIG2] & |
| 1340 | SYSCONFIG2_VOLUME; |
| 1341 | break; |
| 1342 | case V4L2_CID_AUDIO_MUTE: |
| 1343 | ctrl->value = ((radio->registers[POWERCFG] & |
| 1344 | POWERCFG_DMUTE) == 0) ? 1 : 0; |
| 1345 | break; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1346 | default: |
| 1347 | retval = -EINVAL; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1348 | } |
| 1349 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1350 | done: |
| 1351 | if (retval < 0) |
| 1352 | printk(KERN_WARNING DRIVER_NAME |
| 1353 | ": get control failed with %d\n", retval); |
| 1354 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | |
| 1358 | /* |
| 1359 | * si470x_vidioc_s_ctrl - set the value of a control |
| 1360 | */ |
| 1361 | static int si470x_vidioc_s_ctrl(struct file *file, void *priv, |
| 1362 | struct v4l2_control *ctrl) |
| 1363 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1364 | struct si470x_device *radio = video_drvdata(file); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1365 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1366 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1367 | /* safety checks */ |
| 1368 | if (radio->disconnected) { |
| 1369 | retval = -EIO; |
| 1370 | goto done; |
| 1371 | } |
Tobias Lorenz | 03dea86 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1372 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1373 | switch (ctrl->id) { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1374 | case V4L2_CID_AUDIO_VOLUME: |
| 1375 | radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME; |
| 1376 | radio->registers[SYSCONFIG2] |= ctrl->value; |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1377 | retval = si470x_set_register(radio, SYSCONFIG2); |
| 1378 | break; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1379 | case V4L2_CID_AUDIO_MUTE: |
| 1380 | if (ctrl->value == 1) |
| 1381 | radio->registers[POWERCFG] &= ~POWERCFG_DMUTE; |
| 1382 | else |
| 1383 | radio->registers[POWERCFG] |= POWERCFG_DMUTE; |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1384 | retval = si470x_set_register(radio, POWERCFG); |
| 1385 | break; |
| 1386 | default: |
| 1387 | retval = -EINVAL; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1388 | } |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1389 | |
| 1390 | done: |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1391 | if (retval < 0) |
| 1392 | printk(KERN_WARNING DRIVER_NAME |
| 1393 | ": set control failed with %d\n", retval); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1394 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | /* |
| 1399 | * si470x_vidioc_g_audio - get audio attributes |
| 1400 | */ |
| 1401 | static int si470x_vidioc_g_audio(struct file *file, void *priv, |
| 1402 | struct v4l2_audio *audio) |
| 1403 | { |
Tobias Lorenz | 617f533 | 2008-09-24 19:33:09 -0300 | [diff] [blame] | 1404 | /* driver constants */ |
| 1405 | audio->index = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1406 | strcpy(audio->name, "Radio"); |
| 1407 | audio->capability = V4L2_AUDCAP_STEREO; |
Tobias Lorenz | 617f533 | 2008-09-24 19:33:09 -0300 | [diff] [blame] | 1408 | audio->mode = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1409 | |
Tobias Lorenz | 617f533 | 2008-09-24 19:33:09 -0300 | [diff] [blame] | 1410 | return 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | /* |
| 1415 | * si470x_vidioc_g_tuner - get tuner attributes |
| 1416 | */ |
| 1417 | static int si470x_vidioc_g_tuner(struct file *file, void *priv, |
| 1418 | struct v4l2_tuner *tuner) |
| 1419 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1420 | struct si470x_device *radio = video_drvdata(file); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1421 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1422 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1423 | /* safety checks */ |
| 1424 | if (radio->disconnected) { |
| 1425 | retval = -EIO; |
| 1426 | goto done; |
| 1427 | } |
Tobias Lorenz | 33632d4 | 2008-09-24 19:30:26 -0300 | [diff] [blame] | 1428 | if (tuner->index != 0) { |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1429 | retval = -EINVAL; |
| 1430 | goto done; |
| 1431 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1432 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1433 | retval = si470x_get_register(radio, STATUSRSSI); |
| 1434 | if (retval < 0) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1435 | goto done; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1436 | |
Tobias Lorenz | 33632d4 | 2008-09-24 19:30:26 -0300 | [diff] [blame] | 1437 | /* driver constants */ |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1438 | strcpy(tuner->name, "FM"); |
Tobias Lorenz | 33632d4 | 2008-09-24 19:30:26 -0300 | [diff] [blame] | 1439 | tuner->type = V4L2_TUNER_RADIO; |
Tobias Lorenz | 73a529f | 2008-09-24 19:33:41 -0300 | [diff] [blame] | 1440 | tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; |
Tobias Lorenz | 33632d4 | 2008-09-24 19:30:26 -0300 | [diff] [blame] | 1441 | |
Tobias Lorenz | a17c001 | 2008-09-24 19:21:50 -0300 | [diff] [blame] | 1442 | /* range limits */ |
| 1443 | switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1444 | /* 0: 87.5 - 108 MHz (USA, Europe, default) */ |
| 1445 | default: |
| 1446 | tuner->rangelow = 87.5 * FREQ_MUL; |
| 1447 | tuner->rangehigh = 108 * FREQ_MUL; |
| 1448 | break; |
| 1449 | /* 1: 76 - 108 MHz (Japan wide band) */ |
| 1450 | case 1 : |
| 1451 | tuner->rangelow = 76 * FREQ_MUL; |
| 1452 | tuner->rangehigh = 108 * FREQ_MUL; |
| 1453 | break; |
| 1454 | /* 2: 76 - 90 MHz (Japan) */ |
| 1455 | case 2 : |
| 1456 | tuner->rangelow = 76 * FREQ_MUL; |
| 1457 | tuner->rangehigh = 90 * FREQ_MUL; |
| 1458 | break; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1459 | }; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1460 | |
Tobias Lorenz | 73a529f | 2008-09-24 19:33:41 -0300 | [diff] [blame] | 1461 | /* stereo indicator == stereo (instead of mono) */ |
Tobias Lorenz | d807dec | 2009-02-12 14:56:10 -0300 | [diff] [blame] | 1462 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0) |
Tobias Lorenz | 73a529f | 2008-09-24 19:33:41 -0300 | [diff] [blame] | 1463 | tuner->rxsubchans = V4L2_TUNER_SUB_MONO; |
Tobias Lorenz | d807dec | 2009-02-12 14:56:10 -0300 | [diff] [blame] | 1464 | else |
| 1465 | tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; |
Tobias Lorenz | 73a529f | 2008-09-24 19:33:41 -0300 | [diff] [blame] | 1466 | |
| 1467 | /* mono/stereo selector */ |
Tobias Lorenz | d807dec | 2009-02-12 14:56:10 -0300 | [diff] [blame] | 1468 | if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0) |
Tobias Lorenz | 73a529f | 2008-09-24 19:33:41 -0300 | [diff] [blame] | 1469 | tuner->audmode = V4L2_TUNER_MODE_STEREO; |
Tobias Lorenz | d807dec | 2009-02-12 14:56:10 -0300 | [diff] [blame] | 1470 | else |
| 1471 | tuner->audmode = V4L2_TUNER_MODE_MONO; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1472 | |
| 1473 | /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */ |
Tobias Lorenz | d807dec | 2009-02-12 14:56:10 -0300 | [diff] [blame] | 1474 | /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */ |
| 1475 | tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI); |
| 1476 | /* the ideal factor is 0xffff/75 = 873,8 */ |
| 1477 | tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1478 | |
| 1479 | /* automatic frequency control: -1: freq to low, 1 freq to high */ |
Tobias Lorenz | ace7d4b | 2008-05-31 15:09:07 -0300 | [diff] [blame] | 1480 | /* AFCRL does only indicate that freq. differs, not if too low/high */ |
| 1481 | tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1482 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1483 | done: |
| 1484 | if (retval < 0) |
| 1485 | printk(KERN_WARNING DRIVER_NAME |
| 1486 | ": get tuner failed with %d\n", retval); |
| 1487 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | |
| 1491 | /* |
| 1492 | * si470x_vidioc_s_tuner - set tuner attributes |
| 1493 | */ |
| 1494 | static int si470x_vidioc_s_tuner(struct file *file, void *priv, |
| 1495 | struct v4l2_tuner *tuner) |
| 1496 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1497 | struct si470x_device *radio = video_drvdata(file); |
Tobias Lorenz | 33632d4 | 2008-09-24 19:30:26 -0300 | [diff] [blame] | 1498 | int retval = -EINVAL; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1499 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1500 | /* safety checks */ |
| 1501 | if (radio->disconnected) { |
| 1502 | retval = -EIO; |
| 1503 | goto done; |
| 1504 | } |
Tobias Lorenz | 33632d4 | 2008-09-24 19:30:26 -0300 | [diff] [blame] | 1505 | if (tuner->index != 0) |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1506 | goto done; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1507 | |
Tobias Lorenz | 73a529f | 2008-09-24 19:33:41 -0300 | [diff] [blame] | 1508 | /* mono/stereo selector */ |
| 1509 | switch (tuner->audmode) { |
| 1510 | case V4L2_TUNER_MODE_MONO: |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1511 | radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */ |
Tobias Lorenz | 73a529f | 2008-09-24 19:33:41 -0300 | [diff] [blame] | 1512 | break; |
| 1513 | case V4L2_TUNER_MODE_STEREO: |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1514 | radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */ |
Tobias Lorenz | 73a529f | 2008-09-24 19:33:41 -0300 | [diff] [blame] | 1515 | break; |
| 1516 | default: |
| 1517 | goto done; |
| 1518 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1519 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1520 | retval = si470x_set_register(radio, POWERCFG); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1521 | |
| 1522 | done: |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1523 | if (retval < 0) |
| 1524 | printk(KERN_WARNING DRIVER_NAME |
| 1525 | ": set tuner failed with %d\n", retval); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1526 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | |
| 1530 | /* |
| 1531 | * si470x_vidioc_g_frequency - get tuner or modulator radio frequency |
| 1532 | */ |
| 1533 | static int si470x_vidioc_g_frequency(struct file *file, void *priv, |
| 1534 | struct v4l2_frequency *freq) |
| 1535 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1536 | struct si470x_device *radio = video_drvdata(file); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1537 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1538 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1539 | /* safety checks */ |
| 1540 | if (radio->disconnected) { |
| 1541 | retval = -EIO; |
| 1542 | goto done; |
| 1543 | } |
Tobias Lorenz | 33632d4 | 2008-09-24 19:30:26 -0300 | [diff] [blame] | 1544 | if (freq->tuner != 0) { |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1545 | retval = -EINVAL; |
| 1546 | goto done; |
| 1547 | } |
Tobias Lorenz | 03dea86 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1548 | |
Tobias Lorenz | 33632d4 | 2008-09-24 19:30:26 -0300 | [diff] [blame] | 1549 | freq->type = V4L2_TUNER_RADIO; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1550 | retval = si470x_get_freq(radio, &freq->frequency); |
| 1551 | |
| 1552 | done: |
| 1553 | if (retval < 0) |
| 1554 | printk(KERN_WARNING DRIVER_NAME |
| 1555 | ": get frequency failed with %d\n", retval); |
| 1556 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | |
| 1560 | /* |
| 1561 | * si470x_vidioc_s_frequency - set tuner or modulator radio frequency |
| 1562 | */ |
| 1563 | static int si470x_vidioc_s_frequency(struct file *file, void *priv, |
| 1564 | struct v4l2_frequency *freq) |
| 1565 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1566 | struct si470x_device *radio = video_drvdata(file); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1567 | int retval = 0; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1568 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1569 | /* safety checks */ |
| 1570 | if (radio->disconnected) { |
| 1571 | retval = -EIO; |
| 1572 | goto done; |
| 1573 | } |
Tobias Lorenz | 33632d4 | 2008-09-24 19:30:26 -0300 | [diff] [blame] | 1574 | if (freq->tuner != 0) { |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1575 | retval = -EINVAL; |
| 1576 | goto done; |
| 1577 | } |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1578 | |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1579 | retval = si470x_set_freq(radio, freq->frequency); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1580 | |
| 1581 | done: |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1582 | if (retval < 0) |
| 1583 | printk(KERN_WARNING DRIVER_NAME |
| 1584 | ": set frequency failed with %d\n", retval); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1585 | return retval; |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1589 | /* |
Tobias Lorenz | bbc8995 | 2008-05-31 15:11:32 -0300 | [diff] [blame] | 1590 | * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek |
| 1591 | */ |
| 1592 | static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv, |
| 1593 | struct v4l2_hw_freq_seek *seek) |
| 1594 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 1595 | struct si470x_device *radio = video_drvdata(file); |
Tobias Lorenz | bbc8995 | 2008-05-31 15:11:32 -0300 | [diff] [blame] | 1596 | int retval = 0; |
| 1597 | |
| 1598 | /* safety checks */ |
| 1599 | if (radio->disconnected) { |
| 1600 | retval = -EIO; |
| 1601 | goto done; |
| 1602 | } |
Tobias Lorenz | 33632d4 | 2008-09-24 19:30:26 -0300 | [diff] [blame] | 1603 | if (seek->tuner != 0) { |
Tobias Lorenz | bbc8995 | 2008-05-31 15:11:32 -0300 | [diff] [blame] | 1604 | retval = -EINVAL; |
| 1605 | goto done; |
| 1606 | } |
| 1607 | |
| 1608 | retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward); |
| 1609 | |
| 1610 | done: |
| 1611 | if (retval < 0) |
| 1612 | printk(KERN_WARNING DRIVER_NAME |
| 1613 | ": set hardware frequency seek failed with %d\n", |
| 1614 | retval); |
| 1615 | return retval; |
| 1616 | } |
| 1617 | |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 1618 | |
| 1619 | /* |
| 1620 | * si470x_ioctl_ops - video device ioctl operations |
| 1621 | */ |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1622 | static const struct v4l2_ioctl_ops si470x_ioctl_ops = { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1623 | .vidioc_querycap = si470x_vidioc_querycap, |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1624 | .vidioc_queryctrl = si470x_vidioc_queryctrl, |
| 1625 | .vidioc_g_ctrl = si470x_vidioc_g_ctrl, |
| 1626 | .vidioc_s_ctrl = si470x_vidioc_s_ctrl, |
| 1627 | .vidioc_g_audio = si470x_vidioc_g_audio, |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1628 | .vidioc_g_tuner = si470x_vidioc_g_tuner, |
| 1629 | .vidioc_s_tuner = si470x_vidioc_s_tuner, |
| 1630 | .vidioc_g_frequency = si470x_vidioc_g_frequency, |
| 1631 | .vidioc_s_frequency = si470x_vidioc_s_frequency, |
Tobias Lorenz | bbc8995 | 2008-05-31 15:11:32 -0300 | [diff] [blame] | 1632 | .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek, |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1633 | }; |
| 1634 | |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 1635 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1636 | /* |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 1637 | * si470x_viddev_template - video device interface |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1638 | */ |
| 1639 | static struct video_device si470x_viddev_template = { |
| 1640 | .fops = &si470x_fops, |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1641 | .name = DRIVER_NAME, |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1642 | .release = video_device_release, |
Tobias Lorenz | 374ab68 | 2008-09-24 19:17:54 -0300 | [diff] [blame] | 1643 | .ioctl_ops = &si470x_ioctl_ops, |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1644 | }; |
| 1645 | |
| 1646 | |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1647 | |
| 1648 | /************************************************************************** |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1649 | * USB Interface |
| 1650 | **************************************************************************/ |
| 1651 | |
| 1652 | /* |
| 1653 | * si470x_usb_driver_probe - probe for the device |
| 1654 | */ |
| 1655 | static int si470x_usb_driver_probe(struct usb_interface *intf, |
| 1656 | const struct usb_device_id *id) |
| 1657 | { |
| 1658 | struct si470x_device *radio; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1659 | int retval = 0; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1660 | |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1661 | /* private data allocation and initialization */ |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1662 | radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1663 | if (!radio) { |
| 1664 | retval = -ENOMEM; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1665 | goto err_initial; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1666 | } |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1667 | radio->users = 0; |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1668 | radio->disconnected = 0; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1669 | radio->usbdev = interface_to_usbdev(intf); |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1670 | radio->intf = intf; |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1671 | mutex_init(&radio->disconnect_lock); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1672 | mutex_init(&radio->lock); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1673 | |
| 1674 | /* video device allocation and initialization */ |
| 1675 | radio->videodev = video_device_alloc(); |
| 1676 | if (!radio->videodev) { |
| 1677 | retval = -ENOMEM; |
| 1678 | goto err_radio; |
| 1679 | } |
| 1680 | memcpy(radio->videodev, &si470x_viddev_template, |
| 1681 | sizeof(si470x_viddev_template)); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1682 | video_set_drvdata(radio->videodev, radio); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1683 | |
Tobias Lorenz | 10711d0 | 2009-02-12 14:56:30 -0300 | [diff] [blame] | 1684 | /* show some infos about the specific si470x device */ |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1685 | if (si470x_get_all_registers(radio) < 0) { |
| 1686 | retval = -EIO; |
Alexey Klimov | 81d1d09 | 2009-03-31 21:01:04 -0300 | [diff] [blame] | 1687 | goto err_video; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1688 | } |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1689 | 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] | 1690 | radio->registers[DEVICEID], radio->registers[CHIPID]); |
| 1691 | |
Tobias Lorenz | 10711d0 | 2009-02-12 14:56:30 -0300 | [diff] [blame] | 1692 | /* get software and hardware versions */ |
| 1693 | if (si470x_get_scratch_page_versions(radio) < 0) { |
| 1694 | retval = -EIO; |
Alexey Klimov | 81d1d09 | 2009-03-31 21:01:04 -0300 | [diff] [blame] | 1695 | goto err_video; |
Tobias Lorenz | 10711d0 | 2009-02-12 14:56:30 -0300 | [diff] [blame] | 1696 | } |
| 1697 | printk(KERN_INFO DRIVER_NAME |
| 1698 | ": software version %d, hardware version %d\n", |
| 1699 | radio->software_version, radio->hardware_version); |
| 1700 | |
| 1701 | /* check if device and firmware is current */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1702 | if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1703 | < RADIO_SW_VERSION_CURRENT) { |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1704 | printk(KERN_WARNING DRIVER_NAME |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1705 | ": This driver is known to work with " |
| 1706 | "firmware version %hu,\n", RADIO_SW_VERSION_CURRENT); |
| 1707 | printk(KERN_WARNING DRIVER_NAME |
| 1708 | ": but the device has firmware version %hu.\n", |
| 1709 | radio->registers[CHIPID] & CHIPID_FIRMWARE); |
| 1710 | printk(KERN_WARNING DRIVER_NAME |
| 1711 | ": If you have some trouble using this driver,\n"); |
| 1712 | printk(KERN_WARNING DRIVER_NAME |
| 1713 | ": please report to V4L ML at " |
Hans Verkuil | 15965f0 | 2009-03-14 15:06:08 -0300 | [diff] [blame] | 1714 | "linux-media@vger.kernel.org\n"); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1715 | } |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1716 | |
| 1717 | /* set initial frequency */ |
| 1718 | si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */ |
| 1719 | |
Tobias Lorenz | 2f94fc4 | 2009-02-12 14:56:19 -0300 | [diff] [blame] | 1720 | /* set led to connect state */ |
| 1721 | si470x_set_led_state(radio, BLINK_GREEN_LED); |
| 1722 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1723 | /* rds buffer allocation */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1724 | radio->buf_size = rds_buf * 3; |
| 1725 | radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL); |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1726 | if (!radio->buffer) { |
| 1727 | retval = -EIO; |
Alexey Klimov | 81d1d09 | 2009-03-31 21:01:04 -0300 | [diff] [blame] | 1728 | goto err_video; |
Tobias Lorenz | a7c850a | 2008-05-31 15:07:52 -0300 | [diff] [blame] | 1729 | } |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1730 | |
| 1731 | /* rds buffer configuration */ |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1732 | radio->wr_index = 0; |
| 1733 | radio->rd_index = 0; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1734 | init_waitqueue_head(&radio->read_queue); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1735 | |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1736 | /* prepare rds work function */ |
| 1737 | INIT_DELAYED_WORK(&radio->work, si470x_work); |
| 1738 | |
| 1739 | /* register video device */ |
Hans Verkuil | cba99ae | 2008-09-03 17:11:58 -0300 | [diff] [blame] | 1740 | retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr); |
| 1741 | if (retval) { |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1742 | printk(KERN_WARNING DRIVER_NAME |
| 1743 | ": Could not register video device\n"); |
| 1744 | goto err_all; |
| 1745 | } |
| 1746 | usb_set_intfdata(intf, radio); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1747 | |
| 1748 | return 0; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1749 | err_all: |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1750 | kfree(radio->buffer); |
Alexey Klimov | 81d1d09 | 2009-03-31 21:01:04 -0300 | [diff] [blame] | 1751 | err_video: |
| 1752 | video_device_release(radio->videodev); |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1753 | err_radio: |
| 1754 | kfree(radio); |
| 1755 | err_initial: |
| 1756 | return retval; |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | |
| 1760 | /* |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1761 | * si470x_usb_driver_suspend - suspend the device |
| 1762 | */ |
| 1763 | static int si470x_usb_driver_suspend(struct usb_interface *intf, |
| 1764 | pm_message_t message) |
| 1765 | { |
| 1766 | struct si470x_device *radio = usb_get_intfdata(intf); |
| 1767 | |
| 1768 | printk(KERN_INFO DRIVER_NAME ": suspending now...\n"); |
| 1769 | |
| 1770 | cancel_delayed_work_sync(&radio->work); |
| 1771 | |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
| 1775 | |
| 1776 | /* |
| 1777 | * si470x_usb_driver_resume - resume the device |
| 1778 | */ |
| 1779 | static int si470x_usb_driver_resume(struct usb_interface *intf) |
| 1780 | { |
| 1781 | struct si470x_device *radio = usb_get_intfdata(intf); |
| 1782 | |
| 1783 | printk(KERN_INFO DRIVER_NAME ": resuming now...\n"); |
| 1784 | |
| 1785 | mutex_lock(&radio->lock); |
| 1786 | if (radio->users && radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) |
| 1787 | schedule_delayed_work(&radio->work, |
| 1788 | msecs_to_jiffies(rds_poll_time)); |
| 1789 | mutex_unlock(&radio->lock); |
| 1790 | |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | /* |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1796 | * si470x_usb_driver_disconnect - disconnect the device |
| 1797 | */ |
| 1798 | static void si470x_usb_driver_disconnect(struct usb_interface *intf) |
| 1799 | { |
| 1800 | struct si470x_device *radio = usb_get_intfdata(intf); |
| 1801 | |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1802 | mutex_lock(&radio->disconnect_lock); |
| 1803 | radio->disconnected = 1; |
Tobias Lorenz | 5caf513 | 2008-02-04 22:26:08 -0300 | [diff] [blame] | 1804 | cancel_delayed_work_sync(&radio->work); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1805 | usb_set_intfdata(intf, NULL); |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1806 | if (radio->users == 0) { |
Tobias Lorenz | 2f94fc4 | 2009-02-12 14:56:19 -0300 | [diff] [blame] | 1807 | /* set led to disconnect state */ |
| 1808 | si470x_set_led_state(radio, BLINK_ORANGE_LED); |
| 1809 | |
Tobias Lorenz | ce5829e | 2008-05-31 15:04:32 -0300 | [diff] [blame] | 1810 | video_unregister_device(radio->videodev); |
| 1811 | kfree(radio->buffer); |
| 1812 | kfree(radio); |
| 1813 | } |
| 1814 | mutex_unlock(&radio->disconnect_lock); |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | /* |
| 1819 | * si470x_usb_driver - usb driver interface |
| 1820 | */ |
| 1821 | static struct usb_driver si470x_usb_driver = { |
Tobias Lorenz | 57566ad | 2008-02-09 16:08:24 -0300 | [diff] [blame] | 1822 | .name = DRIVER_NAME, |
| 1823 | .probe = si470x_usb_driver_probe, |
| 1824 | .disconnect = si470x_usb_driver_disconnect, |
| 1825 | .suspend = si470x_usb_driver_suspend, |
| 1826 | .resume = si470x_usb_driver_resume, |
| 1827 | .id_table = si470x_usb_driver_id_table, |
| 1828 | .supports_autosuspend = 1, |
Tobias Lorenz | 8bf5e5c | 2008-01-25 04:19:48 -0300 | [diff] [blame] | 1829 | }; |
| 1830 | |
| 1831 | |
| 1832 | |
| 1833 | /************************************************************************** |
| 1834 | * Module Interface |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1835 | **************************************************************************/ |
| 1836 | |
| 1837 | /* |
| 1838 | * si470x_module_init - module init |
| 1839 | */ |
| 1840 | static int __init si470x_module_init(void) |
| 1841 | { |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1842 | printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n"); |
Tobias Lorenz | 78656ac | 2008-01-14 21:55:27 -0300 | [diff] [blame] | 1843 | return usb_register(&si470x_usb_driver); |
| 1844 | } |
| 1845 | |
| 1846 | |
| 1847 | /* |
| 1848 | * si470x_module_exit - module exit |
| 1849 | */ |
| 1850 | static void __exit si470x_module_exit(void) |
| 1851 | { |
| 1852 | usb_deregister(&si470x_usb_driver); |
| 1853 | } |
| 1854 | |
| 1855 | |
| 1856 | module_init(si470x_module_init); |
| 1857 | module_exit(si470x_module_exit); |
| 1858 | |
| 1859 | MODULE_LICENSE("GPL"); |
| 1860 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1861 | MODULE_DESCRIPTION(DRIVER_DESC); |
Tobias Lorenz | 0e3301e | 2008-01-27 14:54:07 -0300 | [diff] [blame] | 1862 | MODULE_VERSION(DRIVER_VERSION); |