Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 1 | /* tuner-xc2028 |
| 2 | * |
Mauro Carvalho Chehab | 33e5316 | 2008-04-21 06:58:48 -0300 | [diff] [blame] | 3 | * Copyright (c) 2007-2008 Mauro Carvalho Chehab (mchehab@infradead.org) |
Mauro Carvalho Chehab | 983d214 | 2007-10-29 23:44:18 -0300 | [diff] [blame] | 4 | * |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 5 | * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com) |
| 6 | * - frontend interface |
Mauro Carvalho Chehab | 983d214 | 2007-10-29 23:44:18 -0300 | [diff] [blame] | 7 | * |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 8 | * This code is placed under the terms of the GNU General Public License v2 |
| 9 | */ |
| 10 | |
| 11 | #include <linux/i2c.h> |
| 12 | #include <asm/div64.h> |
| 13 | #include <linux/firmware.h> |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 14 | #include <linux/videodev2.h> |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 15 | #include <linux/delay.h> |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 16 | #include <media/tuner.h> |
Mauro Carvalho Chehab | 3b20532 | 2007-09-27 18:27:03 -0300 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 18 | #include "tuner-i2c.h" |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 19 | #include "tuner-xc2028.h" |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 20 | #include "tuner-xc2028-types.h" |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 21 | |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 22 | #include <linux/dvb/frontend.h> |
| 23 | #include "dvb_frontend.h" |
| 24 | |
Mauro Carvalho Chehab | ef8c188 | 2007-11-16 16:28:21 -0300 | [diff] [blame] | 25 | |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 26 | static int debug; |
| 27 | module_param(debug, int, 0644); |
| 28 | MODULE_PARM_DESC(debug, "enable verbose debug messages"); |
| 29 | |
Mauro Carvalho Chehab | a82200f | 2007-11-15 11:58:00 -0300 | [diff] [blame] | 30 | static char audio_std[8]; |
| 31 | module_param_string(audio_std, audio_std, sizeof(audio_std), 0); |
| 32 | MODULE_PARM_DESC(audio_std, |
| 33 | "Audio standard. XC3028 audio decoder explicitly " |
| 34 | "needs to know what audio\n" |
| 35 | "standard is needed for some video standards with audio A2 or NICAM.\n" |
| 36 | "The valid values are:\n" |
| 37 | "A2\n" |
| 38 | "A2/A\n" |
| 39 | "A2/B\n" |
| 40 | "NICAM\n" |
| 41 | "NICAM/A\n" |
| 42 | "NICAM/B\n"); |
| 43 | |
Mauro Carvalho Chehab | 5c913c0 | 2008-04-22 14:46:24 -0300 | [diff] [blame] | 44 | static char firmware_name[FIRMWARE_NAME_MAX]; |
| 45 | module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0); |
| 46 | MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the " |
| 47 | "default firmware name\n"); |
| 48 | |
Michael Krufky | c663d03 | 2008-04-18 21:22:50 -0300 | [diff] [blame^] | 49 | static LIST_HEAD(hybrid_tuner_instance_list); |
Chris Pascoe | aa501be | 2007-11-19 04:45:38 -0300 | [diff] [blame] | 50 | static DEFINE_MUTEX(xc2028_list_mutex); |
| 51 | |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 52 | /* struct for storing firmware table */ |
| 53 | struct firmware_description { |
| 54 | unsigned int type; |
| 55 | v4l2_std_id id; |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 56 | __u16 int_freq; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 57 | unsigned char *ptr; |
| 58 | unsigned int size; |
| 59 | }; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 60 | |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 61 | struct firmware_properties { |
| 62 | unsigned int type; |
| 63 | v4l2_std_id id; |
| 64 | v4l2_std_id std_req; |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 65 | __u16 int_freq; |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 66 | unsigned int scode_table; |
| 67 | int scode_nr; |
| 68 | }; |
| 69 | |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 70 | struct xc2028_data { |
Michael Krufky | c663d03 | 2008-04-18 21:22:50 -0300 | [diff] [blame^] | 71 | struct list_head hybrid_tuner_instance_list; |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 72 | struct tuner_i2c_props i2c_props; |
| 73 | int (*tuner_callback) (void *dev, |
| 74 | int command, int arg); |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 75 | void *video_dev; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 76 | __u32 frequency; |
| 77 | |
| 78 | struct firmware_description *firm; |
| 79 | int firm_size; |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 80 | __u16 firm_version; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 81 | |
Chris Pascoe | 8bf799a | 2007-11-19 11:35:45 -0300 | [diff] [blame] | 82 | __u16 hwmodel; |
| 83 | __u16 hwvers; |
| 84 | |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 85 | struct xc2028_ctrl ctrl; |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 86 | |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 87 | struct firmware_properties cur_fw; |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 88 | |
| 89 | struct mutex lock; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 90 | }; |
| 91 | |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 92 | #define i2c_send(priv, buf, size) ({ \ |
| 93 | int _rc; \ |
| 94 | _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \ |
| 95 | if (size != _rc) \ |
| 96 | tuner_info("i2c output error: rc = %d (should be %d)\n",\ |
| 97 | _rc, (int)size); \ |
| 98 | _rc; \ |
| 99 | }) |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 100 | |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 101 | #define i2c_rcv(priv, buf, size) ({ \ |
| 102 | int _rc; \ |
| 103 | _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \ |
| 104 | if (size != _rc) \ |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 105 | tuner_err("i2c input error: rc = %d (should be %d)\n", \ |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 106 | _rc, (int)size); \ |
| 107 | _rc; \ |
| 108 | }) |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 109 | |
Chris Pascoe | 7d58d11 | 2007-11-19 04:31:58 -0300 | [diff] [blame] | 110 | #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \ |
| 111 | int _rc; \ |
| 112 | _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \ |
| 113 | ibuf, isize); \ |
| 114 | if (isize != _rc) \ |
| 115 | tuner_err("i2c input error: rc = %d (should be %d)\n", \ |
| 116 | _rc, (int)isize); \ |
| 117 | _rc; \ |
| 118 | }) |
| 119 | |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 120 | #define send_seq(priv, data...) ({ \ |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 121 | static u8 _val[] = data; \ |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 122 | int _rc; \ |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 123 | if (sizeof(_val) != \ |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 124 | (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \ |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 125 | _val, sizeof(_val)))) { \ |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 126 | tuner_err("Error on line %d: %d\n", __LINE__, _rc); \ |
| 127 | } else \ |
| 128 | msleep(10); \ |
| 129 | _rc; \ |
| 130 | }) |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 131 | |
Devin Heitmueller | 8324402 | 2008-04-17 21:41:16 -0300 | [diff] [blame] | 132 | static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val) |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 133 | { |
Mauro Carvalho Chehab | b873e1a | 2007-11-05 08:41:50 -0300 | [diff] [blame] | 134 | unsigned char buf[2]; |
Chris Pascoe | 7d58d11 | 2007-11-19 04:31:58 -0300 | [diff] [blame] | 135 | unsigned char ibuf[2]; |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 136 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 137 | tuner_dbg("%s %04x called\n", __func__, reg); |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 138 | |
Chris Pascoe | 7d58d11 | 2007-11-19 04:31:58 -0300 | [diff] [blame] | 139 | buf[0] = reg >> 8; |
Mauro Carvalho Chehab | 80b5220 | 2007-11-05 09:07:13 -0300 | [diff] [blame] | 140 | buf[1] = (unsigned char) reg; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 141 | |
Chris Pascoe | 7d58d11 | 2007-11-19 04:31:58 -0300 | [diff] [blame] | 142 | if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2) |
| 143 | return -EIO; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 144 | |
Chris Pascoe | 7d58d11 | 2007-11-19 04:31:58 -0300 | [diff] [blame] | 145 | *val = (ibuf[1]) | (ibuf[0] << 8); |
| 146 | return 0; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 147 | } |
| 148 | |
Chris Pascoe | e026268 | 2007-12-02 06:30:50 -0300 | [diff] [blame] | 149 | #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0) |
Adrian Bunk | 29bec0b | 2008-04-22 14:41:45 -0300 | [diff] [blame] | 150 | static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq) |
Mauro Carvalho Chehab | 43efe70 | 2007-11-14 19:30:28 -0300 | [diff] [blame] | 151 | { |
| 152 | if (type & BASE) |
| 153 | printk("BASE "); |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 154 | if (type & INIT1) |
| 155 | printk("INIT1 "); |
Mauro Carvalho Chehab | 43efe70 | 2007-11-14 19:30:28 -0300 | [diff] [blame] | 156 | if (type & F8MHZ) |
| 157 | printk("F8MHZ "); |
| 158 | if (type & MTS) |
| 159 | printk("MTS "); |
| 160 | if (type & D2620) |
| 161 | printk("D2620 "); |
| 162 | if (type & D2633) |
| 163 | printk("D2633 "); |
| 164 | if (type & DTV6) |
| 165 | printk("DTV6 "); |
| 166 | if (type & QAM) |
| 167 | printk("QAM "); |
| 168 | if (type & DTV7) |
| 169 | printk("DTV7 "); |
| 170 | if (type & DTV78) |
| 171 | printk("DTV78 "); |
| 172 | if (type & DTV8) |
| 173 | printk("DTV8 "); |
| 174 | if (type & FM) |
| 175 | printk("FM "); |
| 176 | if (type & INPUT1) |
| 177 | printk("INPUT1 "); |
| 178 | if (type & LCD) |
| 179 | printk("LCD "); |
| 180 | if (type & NOGD) |
| 181 | printk("NOGD "); |
| 182 | if (type & MONO) |
| 183 | printk("MONO "); |
| 184 | if (type & ATSC) |
| 185 | printk("ATSC "); |
| 186 | if (type & IF) |
| 187 | printk("IF "); |
| 188 | if (type & LG60) |
| 189 | printk("LG60 "); |
| 190 | if (type & ATI638) |
| 191 | printk("ATI638 "); |
| 192 | if (type & OREN538) |
| 193 | printk("OREN538 "); |
| 194 | if (type & OREN36) |
| 195 | printk("OREN36 "); |
| 196 | if (type & TOYOTA388) |
| 197 | printk("TOYOTA388 "); |
| 198 | if (type & TOYOTA794) |
| 199 | printk("TOYOTA794 "); |
| 200 | if (type & DIBCOM52) |
| 201 | printk("DIBCOM52 "); |
| 202 | if (type & ZARLINK456) |
| 203 | printk("ZARLINK456 "); |
| 204 | if (type & CHINA) |
| 205 | printk("CHINA "); |
| 206 | if (type & F6MHZ) |
| 207 | printk("F6MHZ "); |
| 208 | if (type & INPUT2) |
| 209 | printk("INPUT2 "); |
| 210 | if (type & SCODE) |
| 211 | printk("SCODE "); |
Chris Pascoe | e026268 | 2007-12-02 06:30:50 -0300 | [diff] [blame] | 212 | if (type & HAS_IF) |
| 213 | printk("HAS_IF_%d ", int_freq); |
Mauro Carvalho Chehab | 43efe70 | 2007-11-14 19:30:28 -0300 | [diff] [blame] | 214 | } |
| 215 | |
Mauro Carvalho Chehab | ef8c188 | 2007-11-16 16:28:21 -0300 | [diff] [blame] | 216 | static v4l2_std_id parse_audio_std_option(void) |
Mauro Carvalho Chehab | a82200f | 2007-11-15 11:58:00 -0300 | [diff] [blame] | 217 | { |
Chris Pascoe | e155d90 | 2007-11-19 04:16:47 -0300 | [diff] [blame] | 218 | if (strcasecmp(audio_std, "A2") == 0) |
Mauro Carvalho Chehab | a82200f | 2007-11-15 11:58:00 -0300 | [diff] [blame] | 219 | return V4L2_STD_A2; |
Chris Pascoe | e155d90 | 2007-11-19 04:16:47 -0300 | [diff] [blame] | 220 | if (strcasecmp(audio_std, "A2/A") == 0) |
Mauro Carvalho Chehab | a82200f | 2007-11-15 11:58:00 -0300 | [diff] [blame] | 221 | return V4L2_STD_A2_A; |
Chris Pascoe | e155d90 | 2007-11-19 04:16:47 -0300 | [diff] [blame] | 222 | if (strcasecmp(audio_std, "A2/B") == 0) |
Mauro Carvalho Chehab | a82200f | 2007-11-15 11:58:00 -0300 | [diff] [blame] | 223 | return V4L2_STD_A2_B; |
Chris Pascoe | e155d90 | 2007-11-19 04:16:47 -0300 | [diff] [blame] | 224 | if (strcasecmp(audio_std, "NICAM") == 0) |
Mauro Carvalho Chehab | a82200f | 2007-11-15 11:58:00 -0300 | [diff] [blame] | 225 | return V4L2_STD_NICAM; |
Chris Pascoe | e155d90 | 2007-11-19 04:16:47 -0300 | [diff] [blame] | 226 | if (strcasecmp(audio_std, "NICAM/A") == 0) |
Mauro Carvalho Chehab | a82200f | 2007-11-15 11:58:00 -0300 | [diff] [blame] | 227 | return V4L2_STD_NICAM_A; |
Chris Pascoe | e155d90 | 2007-11-19 04:16:47 -0300 | [diff] [blame] | 228 | if (strcasecmp(audio_std, "NICAM/B") == 0) |
Mauro Carvalho Chehab | a82200f | 2007-11-15 11:58:00 -0300 | [diff] [blame] | 229 | return V4L2_STD_NICAM_B; |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 234 | static void free_firmware(struct xc2028_data *priv) |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 235 | { |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 236 | int i; |
Mauro Carvalho Chehab | 92b75ab | 2008-04-17 21:40:53 -0300 | [diff] [blame] | 237 | tuner_dbg("%s called\n", __func__); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 238 | |
| 239 | if (!priv->firm) |
| 240 | return; |
| 241 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 242 | for (i = 0; i < priv->firm_size; i++) |
| 243 | kfree(priv->firm[i].ptr); |
| 244 | |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 245 | kfree(priv->firm); |
| 246 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 247 | priv->firm = NULL; |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 248 | priv->firm_size = 0; |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 249 | |
| 250 | memset(&priv->cur_fw, 0, sizeof(priv->cur_fw)); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 251 | } |
| 252 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 253 | static int load_all_firmwares(struct dvb_frontend *fe) |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 254 | { |
| 255 | struct xc2028_data *priv = fe->tuner_priv; |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 256 | const struct firmware *fw = NULL; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 257 | unsigned char *p, *endp; |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 258 | int rc = 0; |
| 259 | int n, n_array; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 260 | char name[33]; |
Mauro Carvalho Chehab | 5c913c0 | 2008-04-22 14:46:24 -0300 | [diff] [blame] | 261 | char *fname; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 262 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 263 | tuner_dbg("%s called\n", __func__); |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 264 | |
Mauro Carvalho Chehab | 5c913c0 | 2008-04-22 14:46:24 -0300 | [diff] [blame] | 265 | if (!firmware_name[0]) |
| 266 | fname = priv->ctrl.fname; |
| 267 | else |
| 268 | fname = firmware_name; |
| 269 | |
| 270 | tuner_dbg("Reading firmware %s\n", fname); |
| 271 | rc = request_firmware(&fw, fname, &priv->i2c_props.adap->dev); |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 272 | if (rc < 0) { |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 273 | if (rc == -ENOENT) |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 274 | tuner_err("Error: firmware %s not found.\n", |
Mauro Carvalho Chehab | 5c913c0 | 2008-04-22 14:46:24 -0300 | [diff] [blame] | 275 | fname); |
Mauro Carvalho Chehab | 2e4160c | 2007-07-18 13:33:23 -0300 | [diff] [blame] | 276 | else |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 277 | tuner_err("Error %d while requesting firmware %s \n", |
Mauro Carvalho Chehab | 5c913c0 | 2008-04-22 14:46:24 -0300 | [diff] [blame] | 278 | rc, fname); |
Mauro Carvalho Chehab | 2e4160c | 2007-07-18 13:33:23 -0300 | [diff] [blame] | 279 | |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 280 | return rc; |
| 281 | } |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 282 | p = fw->data; |
| 283 | endp = p + fw->size; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 284 | |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 285 | if (fw->size < sizeof(name) - 1 + 2 + 2) { |
| 286 | tuner_err("Error: firmware file %s has invalid size!\n", |
Mauro Carvalho Chehab | 5c913c0 | 2008-04-22 14:46:24 -0300 | [diff] [blame] | 287 | fname); |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 288 | goto corrupt; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 289 | } |
| 290 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 291 | memcpy(name, p, sizeof(name) - 1); |
| 292 | name[sizeof(name) - 1] = 0; |
| 293 | p += sizeof(name) - 1; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 294 | |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 295 | priv->firm_version = le16_to_cpu(*(__u16 *) p); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 296 | p += 2; |
| 297 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 298 | n_array = le16_to_cpu(*(__u16 *) p); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 299 | p += 2; |
| 300 | |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 301 | tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n", |
Mauro Carvalho Chehab | 5c913c0 | 2008-04-22 14:46:24 -0300 | [diff] [blame] | 302 | n_array, fname, name, |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 303 | priv->firm_version >> 8, priv->firm_version & 0xff); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 304 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 305 | priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL); |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 306 | if (priv->firm == NULL) { |
| 307 | tuner_err("Not enough memory to load firmware file.\n"); |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 308 | rc = -ENOMEM; |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 309 | goto err; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 310 | } |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 311 | priv->firm_size = n_array; |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 312 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 313 | n = -1; |
| 314 | while (p < endp) { |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 315 | __u32 type, size; |
| 316 | v4l2_std_id id; |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 317 | __u16 int_freq = 0; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 318 | |
| 319 | n++; |
| 320 | if (n >= n_array) { |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 321 | tuner_err("More firmware images in file than " |
| 322 | "were expected!\n"); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 323 | goto corrupt; |
| 324 | } |
| 325 | |
| 326 | /* Checks if there's enough bytes to read */ |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 327 | if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) { |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 328 | tuner_err("Firmware header is incomplete!\n"); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 329 | goto corrupt; |
| 330 | } |
| 331 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 332 | type = le32_to_cpu(*(__u32 *) p); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 333 | p += sizeof(type); |
| 334 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 335 | id = le64_to_cpu(*(v4l2_std_id *) p); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 336 | p += sizeof(id); |
| 337 | |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 338 | if (type & HAS_IF) { |
| 339 | int_freq = le16_to_cpu(*(__u16 *) p); |
| 340 | p += sizeof(int_freq); |
| 341 | } |
| 342 | |
Michel Ludwig | 2fc580f | 2007-11-16 07:19:35 -0300 | [diff] [blame] | 343 | size = le32_to_cpu(*(__u32 *) p); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 344 | p += sizeof(size); |
| 345 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 346 | if ((!size) || (size + p > endp)) { |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 347 | tuner_err("Firmware type "); |
Mauro Carvalho Chehab | 43efe70 | 2007-11-14 19:30:28 -0300 | [diff] [blame] | 348 | dump_firm_type(type); |
Mauro Carvalho Chehab | ef8c188 | 2007-11-16 16:28:21 -0300 | [diff] [blame] | 349 | printk("(%x), id %llx is corrupted " |
| 350 | "(size=%d, expected %d)\n", |
Chris Pascoe | 91240dd | 2007-11-19 04:38:53 -0300 | [diff] [blame] | 351 | type, (unsigned long long)id, |
Mauro Carvalho Chehab | ef8c188 | 2007-11-16 16:28:21 -0300 | [diff] [blame] | 352 | (unsigned)(endp - p), size); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 353 | goto corrupt; |
| 354 | } |
| 355 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 356 | priv->firm[n].ptr = kzalloc(size, GFP_KERNEL); |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 357 | if (priv->firm[n].ptr == NULL) { |
| 358 | tuner_err("Not enough memory to load firmware file.\n"); |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 359 | rc = -ENOMEM; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 360 | goto err; |
| 361 | } |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 362 | tuner_dbg("Reading firmware type "); |
| 363 | if (debug) { |
Chris Pascoe | e026268 | 2007-12-02 06:30:50 -0300 | [diff] [blame] | 364 | dump_firm_type_and_int_freq(type, int_freq); |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 365 | printk("(%x), id %llx, size=%d.\n", |
Chris Pascoe | e026268 | 2007-12-02 06:30:50 -0300 | [diff] [blame] | 366 | type, (unsigned long long)id, size); |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 367 | } |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 368 | |
| 369 | memcpy(priv->firm[n].ptr, p, size); |
| 370 | priv->firm[n].type = type; |
| 371 | priv->firm[n].id = id; |
| 372 | priv->firm[n].size = size; |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 373 | priv->firm[n].int_freq = int_freq; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 374 | |
| 375 | p += size; |
| 376 | } |
| 377 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 378 | if (n + 1 != priv->firm_size) { |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 379 | tuner_err("Firmware file is incomplete!\n"); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 380 | goto corrupt; |
| 381 | } |
| 382 | |
| 383 | goto done; |
| 384 | |
| 385 | corrupt: |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 386 | rc = -EINVAL; |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 387 | tuner_err("Error: firmware file is corrupted!\n"); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 388 | |
| 389 | err: |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 390 | tuner_info("Releasing partially loaded firmware file.\n"); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 391 | free_firmware(priv); |
| 392 | |
| 393 | done: |
| 394 | release_firmware(fw); |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 395 | if (rc == 0) |
| 396 | tuner_dbg("Firmware files loaded.\n"); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 397 | |
| 398 | return rc; |
| 399 | } |
| 400 | |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 401 | static int seek_firmware(struct dvb_frontend *fe, unsigned int type, |
| 402 | v4l2_std_id *id) |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 403 | { |
| 404 | struct xc2028_data *priv = fe->tuner_priv; |
Chris Pascoe | b153529 | 2007-11-19 10:04:06 -0300 | [diff] [blame] | 405 | int i, best_i = -1, best_nr_matches = 0; |
Mauro Carvalho Chehab | 33e5316 | 2008-04-21 06:58:48 -0300 | [diff] [blame] | 406 | unsigned int type_mask = 0; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 407 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 408 | tuner_dbg("%s called, want type=", __func__); |
Chris Pascoe | b153529 | 2007-11-19 10:04:06 -0300 | [diff] [blame] | 409 | if (debug) { |
| 410 | dump_firm_type(type); |
| 411 | printk("(%x), id %016llx.\n", type, (unsigned long long)*id); |
| 412 | } |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 413 | |
| 414 | if (!priv->firm) { |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 415 | tuner_err("Error! firmware not loaded\n"); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 416 | return -EINVAL; |
| 417 | } |
| 418 | |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 419 | if (((type & ~SCODE) == 0) && (*id == 0)) |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 420 | *id = V4L2_STD_PAL; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 421 | |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 422 | if (type & BASE) |
Mauro Carvalho Chehab | 33e5316 | 2008-04-21 06:58:48 -0300 | [diff] [blame] | 423 | type_mask = BASE_TYPES; |
Chris Pascoe | ef207fe | 2007-12-02 09:30:55 -0300 | [diff] [blame] | 424 | else if (type & SCODE) { |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 425 | type &= SCODE_TYPES; |
Mauro Carvalho Chehab | 33e5316 | 2008-04-21 06:58:48 -0300 | [diff] [blame] | 426 | type_mask = SCODE_TYPES & ~HAS_IF; |
Chris Pascoe | ef207fe | 2007-12-02 09:30:55 -0300 | [diff] [blame] | 427 | } else if (type & DTV_TYPES) |
Mauro Carvalho Chehab | 33e5316 | 2008-04-21 06:58:48 -0300 | [diff] [blame] | 428 | type_mask = DTV_TYPES; |
Chris Pascoe | 11a9eff | 2007-11-19 23:18:36 -0300 | [diff] [blame] | 429 | else if (type & STD_SPECIFIC_TYPES) |
Mauro Carvalho Chehab | 33e5316 | 2008-04-21 06:58:48 -0300 | [diff] [blame] | 430 | type_mask = STD_SPECIFIC_TYPES; |
| 431 | |
| 432 | type &= type_mask; |
| 433 | |
Harvey Harrison | 8367fe2 | 2008-04-25 01:28:10 -0300 | [diff] [blame] | 434 | if (!(type & SCODE)) |
Mauro Carvalho Chehab | 33e5316 | 2008-04-21 06:58:48 -0300 | [diff] [blame] | 435 | type_mask = ~0; |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 436 | |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 437 | /* Seek for exact match */ |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 438 | for (i = 0; i < priv->firm_size; i++) { |
Mauro Carvalho Chehab | 33e5316 | 2008-04-21 06:58:48 -0300 | [diff] [blame] | 439 | if ((type == (priv->firm[i].type & type_mask)) && |
Chris Pascoe | ef207fe | 2007-12-02 09:30:55 -0300 | [diff] [blame] | 440 | (*id == priv->firm[i].id)) |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 441 | goto found; |
| 442 | } |
| 443 | |
| 444 | /* Seek for generic video standard match */ |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 445 | for (i = 0; i < priv->firm_size; i++) { |
Chris Pascoe | b153529 | 2007-11-19 10:04:06 -0300 | [diff] [blame] | 446 | v4l2_std_id match_mask; |
| 447 | int nr_matches; |
| 448 | |
Mauro Carvalho Chehab | 33e5316 | 2008-04-21 06:58:48 -0300 | [diff] [blame] | 449 | if (type != (priv->firm[i].type & type_mask)) |
Chris Pascoe | b153529 | 2007-11-19 10:04:06 -0300 | [diff] [blame] | 450 | continue; |
| 451 | |
| 452 | match_mask = *id & priv->firm[i].id; |
| 453 | if (!match_mask) |
| 454 | continue; |
| 455 | |
| 456 | if ((*id & match_mask) == *id) |
| 457 | goto found; /* Supports all the requested standards */ |
| 458 | |
| 459 | nr_matches = hweight64(match_mask); |
| 460 | if (nr_matches > best_nr_matches) { |
| 461 | best_nr_matches = nr_matches; |
| 462 | best_i = i; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if (best_nr_matches > 0) { |
| 467 | tuner_dbg("Selecting best matching firmware (%d bits) for " |
| 468 | "type=", best_nr_matches); |
| 469 | dump_firm_type(type); |
| 470 | printk("(%x), id %016llx:\n", type, (unsigned long long)*id); |
| 471 | i = best_i; |
| 472 | goto found; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /*FIXME: Would make sense to seek for type "hint" match ? */ |
| 476 | |
Chris Pascoe | b153529 | 2007-11-19 10:04:06 -0300 | [diff] [blame] | 477 | i = -ENOENT; |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 478 | goto ret; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 479 | |
| 480 | found: |
| 481 | *id = priv->firm[i].id; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 482 | |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 483 | ret: |
Chris Pascoe | b153529 | 2007-11-19 10:04:06 -0300 | [diff] [blame] | 484 | tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found"); |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 485 | if (debug) { |
| 486 | dump_firm_type(type); |
Chris Pascoe | 91240dd | 2007-11-19 04:38:53 -0300 | [diff] [blame] | 487 | printk("(%x), id %016llx.\n", type, (unsigned long long)*id); |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 488 | } |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 489 | return i; |
| 490 | } |
| 491 | |
| 492 | static int load_firmware(struct dvb_frontend *fe, unsigned int type, |
| 493 | v4l2_std_id *id) |
| 494 | { |
| 495 | struct xc2028_data *priv = fe->tuner_priv; |
| 496 | int pos, rc; |
Chris Pascoe | 0a196b6 | 2007-11-19 09:29:59 -0300 | [diff] [blame] | 497 | unsigned char *p, *endp, buf[priv->ctrl.max_len]; |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 498 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 499 | tuner_dbg("%s called\n", __func__); |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 500 | |
| 501 | pos = seek_firmware(fe, type, id); |
| 502 | if (pos < 0) |
| 503 | return pos; |
| 504 | |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 505 | tuner_info("Loading firmware for type="); |
Chris Pascoe | b153529 | 2007-11-19 10:04:06 -0300 | [diff] [blame] | 506 | dump_firm_type(priv->firm[pos].type); |
| 507 | printk("(%x), id %016llx.\n", priv->firm[pos].type, |
| 508 | (unsigned long long)*id); |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 509 | |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 510 | p = priv->firm[pos].ptr; |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 511 | endp = p + priv->firm[pos].size; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 512 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 513 | while (p < endp) { |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 514 | __u16 size; |
| 515 | |
| 516 | /* Checks if there's enough bytes to read */ |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 517 | if (p + sizeof(size) > endp) { |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 518 | tuner_err("Firmware chunk size is wrong\n"); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 519 | return -EINVAL; |
| 520 | } |
| 521 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 522 | size = le16_to_cpu(*(__u16 *) p); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 523 | p += sizeof(size); |
| 524 | |
| 525 | if (size == 0xffff) |
| 526 | return 0; |
| 527 | |
| 528 | if (!size) { |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 529 | /* Special callback command received */ |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 530 | rc = priv->tuner_callback(priv->video_dev, |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 531 | XC2028_TUNER_RESET, 0); |
| 532 | if (rc < 0) { |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 533 | tuner_err("Error at RESET code %d\n", |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 534 | (*p) & 0x7f); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 535 | return -EINVAL; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 536 | } |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 537 | continue; |
| 538 | } |
Michel Ludwig | 5403bba | 2007-11-16 07:49:49 -0300 | [diff] [blame] | 539 | if (size >= 0xff00) { |
| 540 | switch (size) { |
| 541 | case 0xff00: |
| 542 | rc = priv->tuner_callback(priv->video_dev, |
| 543 | XC2028_RESET_CLK, 0); |
| 544 | if (rc < 0) { |
| 545 | tuner_err("Error at RESET code %d\n", |
| 546 | (*p) & 0x7f); |
| 547 | return -EINVAL; |
| 548 | } |
Chris Pascoe | b32f9fb | 2007-11-19 04:53:50 -0300 | [diff] [blame] | 549 | break; |
Michel Ludwig | 5403bba | 2007-11-16 07:49:49 -0300 | [diff] [blame] | 550 | default: |
| 551 | tuner_info("Invalid RESET code %d\n", |
| 552 | size & 0x7f); |
| 553 | return -EINVAL; |
| 554 | |
| 555 | } |
Mauro Carvalho Chehab | 2d4c0ac | 2007-11-16 09:43:19 -0300 | [diff] [blame] | 556 | continue; |
Michel Ludwig | 5403bba | 2007-11-16 07:49:49 -0300 | [diff] [blame] | 557 | } |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 558 | |
| 559 | /* Checks for a sleep command */ |
| 560 | if (size & 0x8000) { |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 561 | msleep(size & 0x7fff); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 562 | continue; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 563 | } |
| 564 | |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 565 | if ((size + p > endp)) { |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 566 | tuner_err("missing bytes: need %d, have %d\n", |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 567 | size, (int)(endp - p)); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 568 | return -EINVAL; |
| 569 | } |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 570 | |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 571 | buf[0] = *p; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 572 | p++; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 573 | size--; |
| 574 | |
| 575 | /* Sends message chunks */ |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 576 | while (size > 0) { |
Chris Pascoe | 0a196b6 | 2007-11-19 09:29:59 -0300 | [diff] [blame] | 577 | int len = (size < priv->ctrl.max_len - 1) ? |
| 578 | size : priv->ctrl.max_len - 1; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 579 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 580 | memcpy(buf + 1, p, len); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 581 | |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 582 | rc = i2c_send(priv, buf, len + 1); |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 583 | if (rc < 0) { |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 584 | tuner_err("%d returned from send\n", rc); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 585 | return -EINVAL; |
| 586 | } |
| 587 | |
| 588 | p += len; |
| 589 | size -= len; |
| 590 | } |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 591 | } |
Mauro Carvalho Chehab | 43efe70 | 2007-11-14 19:30:28 -0300 | [diff] [blame] | 592 | return 0; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 593 | } |
| 594 | |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 595 | static int load_scode(struct dvb_frontend *fe, unsigned int type, |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 596 | v4l2_std_id *id, __u16 int_freq, int scode) |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 597 | { |
| 598 | struct xc2028_data *priv = fe->tuner_priv; |
| 599 | int pos, rc; |
| 600 | unsigned char *p; |
| 601 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 602 | tuner_dbg("%s called\n", __func__); |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 603 | |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 604 | if (!int_freq) { |
| 605 | pos = seek_firmware(fe, type, id); |
| 606 | if (pos < 0) |
| 607 | return pos; |
| 608 | } else { |
| 609 | for (pos = 0; pos < priv->firm_size; pos++) { |
| 610 | if ((priv->firm[pos].int_freq == int_freq) && |
Chris Pascoe | 9ca01e7 | 2007-12-02 06:54:17 -0300 | [diff] [blame] | 611 | (priv->firm[pos].type & HAS_IF)) |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 612 | break; |
| 613 | } |
| 614 | if (pos == priv->firm_size) |
| 615 | return -ENOENT; |
| 616 | } |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 617 | |
| 618 | p = priv->firm[pos].ptr; |
| 619 | |
Chris Pascoe | 9ca01e7 | 2007-12-02 06:54:17 -0300 | [diff] [blame] | 620 | if (priv->firm[pos].type & HAS_IF) { |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 621 | if (priv->firm[pos].size != 12 * 16 || scode >= 16) |
| 622 | return -EINVAL; |
| 623 | p += 12 * scode; |
| 624 | } else { |
| 625 | /* 16 SCODE entries per file; each SCODE entry is 12 bytes and |
| 626 | * has a 2-byte size header in the firmware format. */ |
| 627 | if (priv->firm[pos].size != 14 * 16 || scode >= 16 || |
| 628 | le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12) |
| 629 | return -EINVAL; |
| 630 | p += 14 * scode + 2; |
| 631 | } |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 632 | |
Chris Pascoe | d7b22c5 | 2007-11-19 10:12:45 -0300 | [diff] [blame] | 633 | tuner_info("Loading SCODE for type="); |
Chris Pascoe | e026268 | 2007-12-02 06:30:50 -0300 | [diff] [blame] | 634 | dump_firm_type_and_int_freq(priv->firm[pos].type, |
| 635 | priv->firm[pos].int_freq); |
Chris Pascoe | d7b22c5 | 2007-11-19 10:12:45 -0300 | [diff] [blame] | 636 | printk("(%x), id %016llx.\n", priv->firm[pos].type, |
| 637 | (unsigned long long)*id); |
| 638 | |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 639 | if (priv->firm_version < 0x0202) |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 640 | rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00}); |
| 641 | else |
| 642 | rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00}); |
| 643 | if (rc < 0) |
| 644 | return -EIO; |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 645 | |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 646 | rc = i2c_send(priv, p, 12); |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 647 | if (rc < 0) |
| 648 | return -EIO; |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 649 | |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 650 | rc = send_seq(priv, {0x00, 0x8c}); |
| 651 | if (rc < 0) |
| 652 | return -EIO; |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 657 | static int check_firmware(struct dvb_frontend *fe, unsigned int type, |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 658 | v4l2_std_id std, __u16 int_freq) |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 659 | { |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 660 | struct xc2028_data *priv = fe->tuner_priv; |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 661 | struct firmware_properties new_fw; |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 662 | int rc = 0, is_retry = 0; |
| 663 | u16 version, hwmodel; |
| 664 | v4l2_std_id std0; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 665 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 666 | tuner_dbg("%s called\n", __func__); |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 667 | |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 668 | if (!priv->firm) { |
Michel Ludwig | a37b4c9 | 2007-11-16 07:46:14 -0300 | [diff] [blame] | 669 | if (!priv->ctrl.fname) { |
| 670 | tuner_info("xc2028/3028 firmware name not set!\n"); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 671 | return -EINVAL; |
Michel Ludwig | a37b4c9 | 2007-11-16 07:46:14 -0300 | [diff] [blame] | 672 | } |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 673 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 674 | rc = load_all_firmwares(fe); |
| 675 | if (rc < 0) |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 676 | return rc; |
| 677 | } |
| 678 | |
Mauro Carvalho Chehab | 0f6dac1 | 2008-01-05 16:47:16 -0300 | [diff] [blame] | 679 | if (priv->ctrl.mts && !(type & FM)) |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 680 | type |= MTS; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 681 | |
Chris Pascoe | 8bf799a | 2007-11-19 11:35:45 -0300 | [diff] [blame] | 682 | retry: |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 683 | new_fw.type = type; |
| 684 | new_fw.id = std; |
| 685 | new_fw.std_req = std; |
| 686 | new_fw.scode_table = SCODE | priv->ctrl.scode_table; |
| 687 | new_fw.scode_nr = 0; |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 688 | new_fw.int_freq = int_freq; |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 689 | |
| 690 | tuner_dbg("checking firmware, user requested type="); |
| 691 | if (debug) { |
| 692 | dump_firm_type(new_fw.type); |
Chris Pascoe | e026268 | 2007-12-02 06:30:50 -0300 | [diff] [blame] | 693 | printk("(%x), id %016llx, ", new_fw.type, |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 694 | (unsigned long long)new_fw.std_req); |
Chris Pascoe | e026268 | 2007-12-02 06:30:50 -0300 | [diff] [blame] | 695 | if (!int_freq) { |
| 696 | printk("scode_tbl "); |
| 697 | dump_firm_type(priv->ctrl.scode_table); |
| 698 | printk("(%x), ", priv->ctrl.scode_table); |
| 699 | } else |
| 700 | printk("int_freq %d, ", new_fw.int_freq); |
| 701 | printk("scode_nr %d\n", new_fw.scode_nr); |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /* No need to reload base firmware if it matches */ |
| 705 | if (((BASE | new_fw.type) & BASE_TYPES) == |
| 706 | (priv->cur_fw.type & BASE_TYPES)) { |
| 707 | tuner_dbg("BASE firmware not changed.\n"); |
| 708 | goto skip_base; |
| 709 | } |
| 710 | |
| 711 | /* Updating BASE - forget about all currently loaded firmware */ |
| 712 | memset(&priv->cur_fw, 0, sizeof(priv->cur_fw)); |
| 713 | |
| 714 | /* Reset is needed before loading firmware */ |
| 715 | rc = priv->tuner_callback(priv->video_dev, |
| 716 | XC2028_TUNER_RESET, 0); |
| 717 | if (rc < 0) |
| 718 | goto fail; |
| 719 | |
Chris Pascoe | 47bd5bc | 2007-11-19 23:11:37 -0300 | [diff] [blame] | 720 | /* BASE firmwares are all std0 */ |
| 721 | std0 = 0; |
| 722 | rc = load_firmware(fe, BASE | new_fw.type, &std0); |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 723 | if (rc < 0) { |
| 724 | tuner_err("Error %d while loading base firmware\n", |
| 725 | rc); |
| 726 | goto fail; |
| 727 | } |
Michel Ludwig | 5403bba | 2007-11-16 07:49:49 -0300 | [diff] [blame] | 728 | |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 729 | /* Load INIT1, if needed */ |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 730 | tuner_dbg("Load init1 firmware, if exists\n"); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 731 | |
Chris Pascoe | 47bd5bc | 2007-11-19 23:11:37 -0300 | [diff] [blame] | 732 | rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0); |
Chris Pascoe | 1ad0b79 | 2007-11-19 23:43:13 -0300 | [diff] [blame] | 733 | if (rc == -ENOENT) |
| 734 | rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ, |
| 735 | &std0); |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 736 | if (rc < 0 && rc != -ENOENT) { |
| 737 | tuner_err("Error %d while loading init1 firmware\n", |
| 738 | rc); |
| 739 | goto fail; |
Mauro Carvalho Chehab | 2e4160c | 2007-07-18 13:33:23 -0300 | [diff] [blame] | 740 | } |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 741 | |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 742 | skip_base: |
| 743 | /* |
| 744 | * No need to reload standard specific firmware if base firmware |
| 745 | * was not reloaded and requested video standards have not changed. |
| 746 | */ |
| 747 | if (priv->cur_fw.type == (BASE | new_fw.type) && |
| 748 | priv->cur_fw.std_req == std) { |
| 749 | tuner_dbg("Std-specific firmware already loaded.\n"); |
| 750 | goto skip_std_specific; |
| 751 | } |
Mauro Carvalho Chehab | a82200f | 2007-11-15 11:58:00 -0300 | [diff] [blame] | 752 | |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 753 | /* Reloading std-specific firmware forces a SCODE update */ |
| 754 | priv->cur_fw.scode_table = 0; |
| 755 | |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 756 | rc = load_firmware(fe, new_fw.type, &new_fw.id); |
Mauro Carvalho Chehab | cca8379 | 2007-11-22 11:47:18 -0300 | [diff] [blame] | 757 | if (rc == -ENOENT) |
| 758 | rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id); |
| 759 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 760 | if (rc < 0) |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 761 | goto fail; |
| 762 | |
| 763 | skip_std_specific: |
| 764 | if (priv->cur_fw.scode_table == new_fw.scode_table && |
| 765 | priv->cur_fw.scode_nr == new_fw.scode_nr) { |
| 766 | tuner_dbg("SCODE firmware already loaded.\n"); |
| 767 | goto check_device; |
| 768 | } |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 769 | |
Mauro Carvalho Chehab | 40ae91a | 2008-02-14 01:52:48 -0300 | [diff] [blame] | 770 | if (new_fw.type & FM) |
| 771 | goto check_device; |
| 772 | |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 773 | /* Load SCODE firmware, if exists */ |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 774 | tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr); |
Mauro Carvalho Chehab | f380e1d | 2007-11-15 08:43:53 -0300 | [diff] [blame] | 775 | |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 776 | rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id, |
| 777 | new_fw.int_freq, new_fw.scode_nr); |
Mauro Carvalho Chehab | 43efe70 | 2007-11-14 19:30:28 -0300 | [diff] [blame] | 778 | |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 779 | check_device: |
Chris Pascoe | 8bf799a | 2007-11-19 11:35:45 -0300 | [diff] [blame] | 780 | if (xc2028_get_reg(priv, 0x0004, &version) < 0 || |
| 781 | xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) { |
| 782 | tuner_err("Unable to read tuner registers.\n"); |
| 783 | goto fail; |
| 784 | } |
Mauro Carvalho Chehab | 80b5220 | 2007-11-05 09:07:13 -0300 | [diff] [blame] | 785 | |
Devin Heitmueller | b37f2d6 | 2008-04-21 07:02:09 -0300 | [diff] [blame] | 786 | tuner_dbg("Device is Xceive %d version %d.%d, " |
| 787 | "firmware version %d.%d\n", |
| 788 | hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8, |
| 789 | (version & 0xf0) >> 4, version & 0xf); |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 790 | |
Chris Pascoe | 8bf799a | 2007-11-19 11:35:45 -0300 | [diff] [blame] | 791 | /* Check firmware version against what we downloaded. */ |
| 792 | if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) { |
| 793 | tuner_err("Incorrect readback of firmware version.\n"); |
| 794 | goto fail; |
| 795 | } |
| 796 | |
| 797 | /* Check that the tuner hardware model remains consistent over time. */ |
| 798 | if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) { |
| 799 | priv->hwmodel = hwmodel; |
| 800 | priv->hwvers = version & 0xff00; |
| 801 | } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel || |
| 802 | priv->hwvers != (version & 0xff00)) { |
| 803 | tuner_err("Read invalid device hardware information - tuner " |
| 804 | "hung?\n"); |
| 805 | goto fail; |
| 806 | } |
| 807 | |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 808 | memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw)); |
| 809 | |
| 810 | /* |
| 811 | * By setting BASE in cur_fw.type only after successfully loading all |
| 812 | * firmwares, we can: |
| 813 | * 1. Identify that BASE firmware with type=0 has been loaded; |
| 814 | * 2. Tell whether BASE firmware was just changed the next time through. |
| 815 | */ |
| 816 | priv->cur_fw.type |= BASE; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 817 | |
| 818 | return 0; |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 819 | |
| 820 | fail: |
| 821 | memset(&priv->cur_fw, 0, sizeof(priv->cur_fw)); |
Chris Pascoe | 8bf799a | 2007-11-19 11:35:45 -0300 | [diff] [blame] | 822 | if (!is_retry) { |
| 823 | msleep(50); |
| 824 | is_retry = 1; |
| 825 | tuner_dbg("Retrying firmware load\n"); |
| 826 | goto retry; |
| 827 | } |
| 828 | |
Chris Pascoe | e0f0b37 | 2007-11-19 11:22:03 -0300 | [diff] [blame] | 829 | if (rc == -ENOENT) |
| 830 | rc = -EINVAL; |
| 831 | return rc; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 832 | } |
| 833 | |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 834 | static int xc2028_signal(struct dvb_frontend *fe, u16 *strength) |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 835 | { |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 836 | struct xc2028_data *priv = fe->tuner_priv; |
Chris Pascoe | 7d58d11 | 2007-11-19 04:31:58 -0300 | [diff] [blame] | 837 | u16 frq_lock, signal = 0; |
| 838 | int rc; |
Mauro Carvalho Chehab | 3b20532 | 2007-09-27 18:27:03 -0300 | [diff] [blame] | 839 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 840 | tuner_dbg("%s called\n", __func__); |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 841 | |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 842 | mutex_lock(&priv->lock); |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 843 | |
Mauro Carvalho Chehab | 80b5220 | 2007-11-05 09:07:13 -0300 | [diff] [blame] | 844 | /* Sync Lock Indicator */ |
Chris Pascoe | 7d58d11 | 2007-11-19 04:31:58 -0300 | [diff] [blame] | 845 | rc = xc2028_get_reg(priv, 0x0002, &frq_lock); |
Mauro Carvalho Chehab | b0166ab | 2008-04-24 11:19:55 -0300 | [diff] [blame] | 846 | if (rc < 0) |
Mauro Carvalho Chehab | 3b20532 | 2007-09-27 18:27:03 -0300 | [diff] [blame] | 847 | goto ret; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 848 | |
Mauro Carvalho Chehab | b0166ab | 2008-04-24 11:19:55 -0300 | [diff] [blame] | 849 | /* Frequency is locked */ |
| 850 | if (frq_lock == 1) |
| 851 | signal = 32768; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 852 | |
Mauro Carvalho Chehab | 80b5220 | 2007-11-05 09:07:13 -0300 | [diff] [blame] | 853 | /* Get SNR of the video signal */ |
Chris Pascoe | 7d58d11 | 2007-11-19 04:31:58 -0300 | [diff] [blame] | 854 | rc = xc2028_get_reg(priv, 0x0040, &signal); |
| 855 | if (rc < 0) |
Mauro Carvalho Chehab | b0166ab | 2008-04-24 11:19:55 -0300 | [diff] [blame] | 856 | goto ret; |
| 857 | |
| 858 | /* Use both frq_lock and signal to generate the result */ |
| 859 | signal = signal || ((signal & 0x07) << 12); |
Mauro Carvalho Chehab | 3b20532 | 2007-09-27 18:27:03 -0300 | [diff] [blame] | 860 | |
| 861 | ret: |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 862 | mutex_unlock(&priv->lock); |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 863 | |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 864 | *strength = signal; |
| 865 | |
Mauro Carvalho Chehab | b0166ab | 2008-04-24 11:19:55 -0300 | [diff] [blame] | 866 | tuner_dbg("signal strength is %d\n", signal); |
| 867 | |
Chris Pascoe | 7d58d11 | 2007-11-19 04:31:58 -0300 | [diff] [blame] | 868 | return rc; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | #define DIV 15625 |
| 872 | |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 873 | static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */, |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 874 | enum tuner_mode new_mode, |
| 875 | unsigned int type, |
| 876 | v4l2_std_id std, |
| 877 | u16 int_freq) |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 878 | { |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 879 | struct xc2028_data *priv = fe->tuner_priv; |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 880 | int rc = -EINVAL; |
Chris Pascoe | 2ce4b3a | 2007-11-19 06:20:17 -0300 | [diff] [blame] | 881 | unsigned char buf[4]; |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 882 | u32 div, offset = 0; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 883 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 884 | tuner_dbg("%s called\n", __func__); |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 885 | |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 886 | mutex_lock(&priv->lock); |
| 887 | |
Chris Pascoe | 2ce4b3a | 2007-11-19 06:20:17 -0300 | [diff] [blame] | 888 | tuner_dbg("should set frequency %d kHz\n", freq / 1000); |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 889 | |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 890 | if (check_firmware(fe, type, std, int_freq) < 0) |
Mauro Carvalho Chehab | 3b20532 | 2007-09-27 18:27:03 -0300 | [diff] [blame] | 891 | goto ret; |
Mauro Carvalho Chehab | 2e4160c | 2007-07-18 13:33:23 -0300 | [diff] [blame] | 892 | |
Mauro Carvalho Chehab | 2800ae9 | 2007-11-22 12:48:04 -0300 | [diff] [blame] | 893 | /* On some cases xc2028 can disable video output, if |
| 894 | * very weak signals are received. By sending a soft |
| 895 | * reset, this is re-enabled. So, it is better to always |
| 896 | * send a soft reset before changing channels, to be sure |
| 897 | * that xc2028 will be in a safe state. |
| 898 | * Maybe this might also be needed for DTV. |
| 899 | */ |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 900 | if (new_mode == T_ANALOG_TV) { |
Mauro Carvalho Chehab | 2800ae9 | 2007-11-22 12:48:04 -0300 | [diff] [blame] | 901 | rc = send_seq(priv, {0x00, 0x00}); |
Michael Krufky | d536c9d | 2007-12-18 10:42:33 -0300 | [diff] [blame] | 902 | } else if (priv->cur_fw.type & ATSC) { |
| 903 | offset = 1750000; |
| 904 | } else { |
Mauro Carvalho Chehab | d4e7668 | 2007-07-18 23:14:25 -0300 | [diff] [blame] | 905 | offset = 2750000; |
Chris Pascoe | 897b842 | 2007-12-02 09:39:18 -0300 | [diff] [blame] | 906 | /* |
| 907 | * We must adjust the offset by 500kHz in two cases in order |
| 908 | * to correctly center the IF output: |
| 909 | * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly |
| 910 | * selected and a 7MHz channel is tuned; |
| 911 | * 2) When tuning a VHF channel with DTV78 firmware. |
| 912 | */ |
| 913 | if (((priv->cur_fw.type & DTV7) && |
| 914 | (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) || |
| 915 | ((priv->cur_fw.type & DTV78) && freq < 470000000)) |
Chris Pascoe | a44f1c4 | 2007-11-19 06:35:26 -0300 | [diff] [blame] | 916 | offset -= 500000; |
| 917 | } |
Mauro Carvalho Chehab | 2e4160c | 2007-07-18 13:33:23 -0300 | [diff] [blame] | 918 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 919 | div = (freq - offset + DIV / 2) / DIV; |
Mauro Carvalho Chehab | 2e4160c | 2007-07-18 13:33:23 -0300 | [diff] [blame] | 920 | |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 921 | /* CMD= Set frequency */ |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 922 | if (priv->firm_version < 0x0202) |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 923 | rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00}); |
| 924 | else |
| 925 | rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00}); |
| 926 | if (rc < 0) |
| 927 | goto ret; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 928 | |
Mauro Carvalho Chehab | 1fe87369 | 2008-04-22 14:45:20 -0300 | [diff] [blame] | 929 | /* Return code shouldn't be checked. |
| 930 | The reset CLK is needed only with tm6000. |
| 931 | Driver should work fine even if this fails. |
| 932 | */ |
| 933 | priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1); |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 934 | |
| 935 | msleep(10); |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 936 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 937 | buf[0] = 0xff & (div >> 24); |
| 938 | buf[1] = 0xff & (div >> 16); |
| 939 | buf[2] = 0xff & (div >> 8); |
| 940 | buf[3] = 0xff & (div); |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 941 | |
Chris Pascoe | 47cc5b7 | 2007-11-19 04:14:23 -0300 | [diff] [blame] | 942 | rc = i2c_send(priv, buf, sizeof(buf)); |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 943 | if (rc < 0) |
Mauro Carvalho Chehab | 3b20532 | 2007-09-27 18:27:03 -0300 | [diff] [blame] | 944 | goto ret; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 945 | msleep(100); |
| 946 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 947 | priv->frequency = freq; |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 948 | |
Chris Pascoe | 2ce4b3a | 2007-11-19 06:20:17 -0300 | [diff] [blame] | 949 | tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n", |
| 950 | buf[0], buf[1], buf[2], buf[3], |
| 951 | freq / 1000000, (freq % 1000000) / 1000); |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 952 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 953 | rc = 0; |
Mauro Carvalho Chehab | 3b20532 | 2007-09-27 18:27:03 -0300 | [diff] [blame] | 954 | |
| 955 | ret: |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 956 | mutex_unlock(&priv->lock); |
| 957 | |
| 958 | return rc; |
Mauro Carvalho Chehab | 6cb4587 | 2007-10-02 11:57:03 -0300 | [diff] [blame] | 959 | } |
| 960 | |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 961 | static int xc2028_set_analog_freq(struct dvb_frontend *fe, |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 962 | struct analog_parameters *p) |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 963 | { |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 964 | struct xc2028_data *priv = fe->tuner_priv; |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 965 | unsigned int type=0; |
| 966 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 967 | tuner_dbg("%s called\n", __func__); |
Mauro Carvalho Chehab | c71d4bc | 2007-11-22 11:47:18 -0300 | [diff] [blame] | 968 | |
Mauro Carvalho Chehab | d74cb25 | 2007-11-24 10:20:15 -0300 | [diff] [blame] | 969 | if (p->mode == V4L2_TUNER_RADIO) { |
| 970 | type |= FM; |
| 971 | if (priv->ctrl.input1) |
| 972 | type |= INPUT1; |
| 973 | return generic_set_freq(fe, (625l * p->frequency) / 10, |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 974 | T_ANALOG_TV, type, 0, 0); |
Mauro Carvalho Chehab | d74cb25 | 2007-11-24 10:20:15 -0300 | [diff] [blame] | 975 | } |
| 976 | |
Mauro Carvalho Chehab | a5e9fe1 | 2007-11-23 11:36:18 -0300 | [diff] [blame] | 977 | /* if std is not defined, choose one */ |
| 978 | if (!p->std) |
| 979 | p->std = V4L2_STD_MN; |
| 980 | |
| 981 | /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */ |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 982 | if (!(p->std & V4L2_STD_MN)) |
| 983 | type |= F8MHZ; |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 984 | |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 985 | /* Add audio hack to std mask */ |
| 986 | p->std |= parse_audio_std_option(); |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 987 | |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 988 | return generic_set_freq(fe, 62500l * p->frequency, |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 989 | T_ANALOG_TV, type, p->std, 0); |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 990 | } |
| 991 | |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 992 | static int xc2028_set_params(struct dvb_frontend *fe, |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 993 | struct dvb_frontend_parameters *p) |
| 994 | { |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 995 | struct xc2028_data *priv = fe->tuner_priv; |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 996 | unsigned int type=0; |
Mauro Carvalho Chehab | d04aa54 | 2007-11-24 10:47:03 -0300 | [diff] [blame] | 997 | fe_bandwidth_t bw = BANDWIDTH_8_MHZ; |
Chris Pascoe | ad35ce9 | 2007-12-02 06:36:42 -0300 | [diff] [blame] | 998 | u16 demod = 0; |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 999 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1000 | tuner_dbg("%s called\n", __func__); |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1001 | |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 1002 | if (priv->ctrl.d2633) |
| 1003 | type |= D2633; |
| 1004 | else |
| 1005 | type |= D2620; |
| 1006 | |
Mauro Carvalho Chehab | d04aa54 | 2007-11-24 10:47:03 -0300 | [diff] [blame] | 1007 | switch(fe->ops.info.type) { |
Mauro Carvalho Chehab | d04aa54 | 2007-11-24 10:47:03 -0300 | [diff] [blame] | 1008 | case FE_OFDM: |
| 1009 | bw = p->u.ofdm.bandwidth; |
| 1010 | break; |
| 1011 | case FE_QAM: |
Mauro Carvalho Chehab | 5c15648 | 2007-12-02 00:02:18 -0300 | [diff] [blame] | 1012 | tuner_info("WARN: There are some reports that " |
| 1013 | "QAM 6 MHz doesn't work.\n" |
| 1014 | "If this works for you, please report by " |
| 1015 | "e-mail to: v4l-dvb-maintainer@linuxtv.org\n"); |
Mauro Carvalho Chehab | d04aa54 | 2007-11-24 10:47:03 -0300 | [diff] [blame] | 1016 | bw = BANDWIDTH_6_MHZ; |
| 1017 | type |= QAM; |
| 1018 | break; |
| 1019 | case FE_ATSC: |
| 1020 | bw = BANDWIDTH_6_MHZ; |
Mauro Carvalho Chehab | 4bfae52 | 2007-12-16 09:24:30 -0300 | [diff] [blame] | 1021 | /* The only ATSC firmware (at least on v2.7) is D2633, |
| 1022 | so overrides ctrl->d2633 */ |
Mauro Carvalho Chehab | d04aa54 | 2007-11-24 10:47:03 -0300 | [diff] [blame] | 1023 | type |= ATSC| D2633; |
Mauro Carvalho Chehab | 4bfae52 | 2007-12-16 09:24:30 -0300 | [diff] [blame] | 1024 | type &= ~D2620; |
Mauro Carvalho Chehab | d04aa54 | 2007-11-24 10:47:03 -0300 | [diff] [blame] | 1025 | break; |
Mauro Carvalho Chehab | 5c15648 | 2007-12-02 00:02:18 -0300 | [diff] [blame] | 1026 | /* DVB-S is not supported */ |
| 1027 | default: |
| 1028 | return -EINVAL; |
Mauro Carvalho Chehab | d04aa54 | 2007-11-24 10:47:03 -0300 | [diff] [blame] | 1029 | } |
| 1030 | |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 1031 | switch (bw) { |
| 1032 | case BANDWIDTH_8_MHZ: |
Chris Pascoe | 3dfefc5 | 2007-12-02 10:07:06 -0300 | [diff] [blame] | 1033 | if (p->frequency < 470000000) |
| 1034 | priv->ctrl.vhfbw7 = 0; |
| 1035 | else |
| 1036 | priv->ctrl.uhfbw8 = 1; |
| 1037 | type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8; |
| 1038 | type |= F8MHZ; |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 1039 | break; |
| 1040 | case BANDWIDTH_7_MHZ: |
Chris Pascoe | 3dfefc5 | 2007-12-02 10:07:06 -0300 | [diff] [blame] | 1041 | if (p->frequency < 470000000) |
| 1042 | priv->ctrl.vhfbw7 = 1; |
| 1043 | else |
| 1044 | priv->ctrl.uhfbw8 = 0; |
| 1045 | type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7; |
| 1046 | type |= F8MHZ; |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 1047 | break; |
| 1048 | case BANDWIDTH_6_MHZ: |
Chris Pascoe | 3dfefc5 | 2007-12-02 10:07:06 -0300 | [diff] [blame] | 1049 | type |= DTV6; |
| 1050 | priv->ctrl.vhfbw7 = 0; |
| 1051 | priv->ctrl.uhfbw8 = 0; |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 1052 | break; |
| 1053 | default: |
| 1054 | tuner_err("error: bandwidth not supported.\n"); |
| 1055 | }; |
| 1056 | |
Mauro Carvalho Chehab | 66c2d53 | 2007-11-25 19:26:36 -0300 | [diff] [blame] | 1057 | /* All S-code tables need a 200kHz shift */ |
| 1058 | if (priv->ctrl.demod) |
Chris Pascoe | ad35ce9 | 2007-12-02 06:36:42 -0300 | [diff] [blame] | 1059 | demod = priv->ctrl.demod + 200; |
Mauro Carvalho Chehab | b542dfd | 2007-11-24 11:07:12 -0300 | [diff] [blame] | 1060 | |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 1061 | return generic_set_freq(fe, p->frequency, |
Chris Pascoe | ad35ce9 | 2007-12-02 06:36:42 -0300 | [diff] [blame] | 1062 | T_DIGITAL_TV, type, 0, demod); |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1063 | } |
| 1064 | |
Chris Pascoe | 45819c3 | 2007-11-19 11:41:20 -0300 | [diff] [blame] | 1065 | |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1066 | static int xc2028_dvb_release(struct dvb_frontend *fe) |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1067 | { |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1068 | struct xc2028_data *priv = fe->tuner_priv; |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1069 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1070 | tuner_dbg("%s called\n", __func__); |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1071 | |
Chris Pascoe | aa501be | 2007-11-19 04:45:38 -0300 | [diff] [blame] | 1072 | mutex_lock(&xc2028_list_mutex); |
| 1073 | |
Michael Krufky | c663d03 | 2008-04-18 21:22:50 -0300 | [diff] [blame^] | 1074 | /* only perform final cleanup if this is the last instance */ |
| 1075 | if (hybrid_tuner_report_instance_count(priv) == 1) { |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 1076 | kfree(priv->ctrl.fname); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 1077 | free_firmware(priv); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 1078 | } |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1079 | |
Michael Krufky | c663d03 | 2008-04-18 21:22:50 -0300 | [diff] [blame^] | 1080 | if (priv) |
| 1081 | hybrid_tuner_release_state(priv); |
| 1082 | |
Chris Pascoe | aa501be | 2007-11-19 04:45:38 -0300 | [diff] [blame] | 1083 | mutex_unlock(&xc2028_list_mutex); |
| 1084 | |
Michael Krufky | c663d03 | 2008-04-18 21:22:50 -0300 | [diff] [blame^] | 1085 | fe->tuner_priv = NULL; |
| 1086 | |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1087 | return 0; |
| 1088 | } |
| 1089 | |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1090 | static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1091 | { |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1092 | struct xc2028_data *priv = fe->tuner_priv; |
| 1093 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1094 | tuner_dbg("%s called\n", __func__); |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1095 | |
| 1096 | *frequency = priv->frequency; |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1097 | |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 1101 | static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg) |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 1102 | { |
| 1103 | struct xc2028_data *priv = fe->tuner_priv; |
| 1104 | struct xc2028_ctrl *p = priv_cfg; |
Chris Pascoe | 0a196b6 | 2007-11-19 09:29:59 -0300 | [diff] [blame] | 1105 | int rc = 0; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 1106 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1107 | tuner_dbg("%s called\n", __func__); |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 1108 | |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 1109 | mutex_lock(&priv->lock); |
| 1110 | |
Chris Pascoe | 0a196b6 | 2007-11-19 09:29:59 -0300 | [diff] [blame] | 1111 | memcpy(&priv->ctrl, p, sizeof(priv->ctrl)); |
Mauro Carvalho Chehab | 92b75ab | 2008-04-17 21:40:53 -0300 | [diff] [blame] | 1112 | if (priv->ctrl.max_len < 9) |
| 1113 | priv->ctrl.max_len = 13; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 1114 | |
| 1115 | if (p->fname) { |
Mauro Carvalho Chehab | 92b75ab | 2008-04-17 21:40:53 -0300 | [diff] [blame] | 1116 | if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) { |
| 1117 | kfree(priv->ctrl.fname); |
| 1118 | free_firmware(priv); |
| 1119 | } |
| 1120 | |
Chris Pascoe | 0a196b6 | 2007-11-19 09:29:59 -0300 | [diff] [blame] | 1121 | priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL); |
| 1122 | if (priv->ctrl.fname == NULL) |
| 1123 | rc = -ENOMEM; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 1124 | } |
| 1125 | |
Chris Pascoe | 06fd82d | 2007-11-19 06:06:08 -0300 | [diff] [blame] | 1126 | mutex_unlock(&priv->lock); |
| 1127 | |
Chris Pascoe | 0a196b6 | 2007-11-19 09:29:59 -0300 | [diff] [blame] | 1128 | return rc; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 1129 | } |
| 1130 | |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1131 | static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = { |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1132 | .info = { |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 1133 | .name = "Xceive XC3028", |
| 1134 | .frequency_min = 42000000, |
| 1135 | .frequency_max = 864000000, |
| 1136 | .frequency_step = 50000, |
| 1137 | }, |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1138 | |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 1139 | .set_config = xc2028_set_config, |
Mauro Carvalho Chehab | 00deff1 | 2007-11-24 10:13:42 -0300 | [diff] [blame] | 1140 | .set_analog_params = xc2028_set_analog_freq, |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1141 | .release = xc2028_dvb_release, |
| 1142 | .get_frequency = xc2028_get_frequency, |
| 1143 | .get_rf_strength = xc2028_signal, |
| 1144 | .set_params = xc2028_set_params, |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1145 | }; |
| 1146 | |
Michael Krufky | 7972f98 | 2007-12-21 16:12:09 -0300 | [diff] [blame] | 1147 | struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe, |
| 1148 | struct xc2028_config *cfg) |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1149 | { |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1150 | struct xc2028_data *priv; |
Michael Krufky | c663d03 | 2008-04-18 21:22:50 -0300 | [diff] [blame^] | 1151 | int instance; |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1152 | |
Mauro Carvalho Chehab | 83fb340 | 2007-11-15 09:44:30 -0300 | [diff] [blame] | 1153 | if (debug) |
Michael Krufky | 2756665 | 2008-04-22 14:41:53 -0300 | [diff] [blame] | 1154 | printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n"); |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1155 | |
Mauro Carvalho Chehab | b412ba7 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1156 | if (NULL == cfg) |
Michel Ludwig | a37b4c9 | 2007-11-16 07:46:14 -0300 | [diff] [blame] | 1157 | return NULL; |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1158 | |
Michel Ludwig | a37b4c9 | 2007-11-16 07:46:14 -0300 | [diff] [blame] | 1159 | if (!fe) { |
Michael Krufky | 2756665 | 2008-04-22 14:41:53 -0300 | [diff] [blame] | 1160 | printk(KERN_ERR "xc2028: No frontend!\n"); |
Michel Ludwig | a37b4c9 | 2007-11-16 07:46:14 -0300 | [diff] [blame] | 1161 | return NULL; |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1162 | } |
| 1163 | |
Chris Pascoe | aa501be | 2007-11-19 04:45:38 -0300 | [diff] [blame] | 1164 | mutex_lock(&xc2028_list_mutex); |
| 1165 | |
Michael Krufky | c663d03 | 2008-04-18 21:22:50 -0300 | [diff] [blame^] | 1166 | instance = hybrid_tuner_request_state(struct xc2028_data, priv, |
| 1167 | hybrid_tuner_instance_list, |
| 1168 | cfg->i2c_adap, cfg->i2c_addr, |
| 1169 | "xc2028"); |
| 1170 | switch (instance) { |
| 1171 | case 0: |
| 1172 | /* memory allocation failure */ |
| 1173 | goto fail; |
| 1174 | break; |
| 1175 | case 1: |
| 1176 | /* new tuner instance */ |
Michel Ludwig | a37b4c9 | 2007-11-16 07:46:14 -0300 | [diff] [blame] | 1177 | priv->tuner_callback = cfg->callback; |
Chris Pascoe | 0a196b6 | 2007-11-19 09:29:59 -0300 | [diff] [blame] | 1178 | priv->ctrl.max_len = 13; |
Mauro Carvalho Chehab | de3fe21 | 2007-10-24 09:22:08 -0300 | [diff] [blame] | 1179 | |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1180 | mutex_init(&priv->lock); |
| 1181 | |
Michael Krufky | c663d03 | 2008-04-18 21:22:50 -0300 | [diff] [blame^] | 1182 | /* analog side (tuner-core) uses i2c_adap->algo_data. |
| 1183 | * digital side is not guaranteed to have algo_data defined. |
| 1184 | * |
| 1185 | * digital side will always have fe->dvb defined. |
| 1186 | * analog side (tuner-core) doesn't (yet) define fe->dvb. |
| 1187 | */ |
| 1188 | priv->video_dev = ((fe->dvb) && (fe->dvb->priv)) ? |
| 1189 | fe->dvb->priv : cfg->i2c_adap->algo_data; |
| 1190 | |
| 1191 | fe->tuner_priv = priv; |
| 1192 | break; |
| 1193 | case 2: |
| 1194 | /* existing tuner instance */ |
| 1195 | fe->tuner_priv = priv; |
| 1196 | break; |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1197 | } |
Michel Ludwig | a37b4c9 | 2007-11-16 07:46:14 -0300 | [diff] [blame] | 1198 | |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1199 | memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops, |
Mauro Carvalho Chehab | ab0b9fc | 2007-11-01 17:47:42 -0300 | [diff] [blame] | 1200 | sizeof(xc2028_dvb_tuner_ops)); |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1201 | |
| 1202 | tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner"); |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1203 | |
Mauro Carvalho Chehab | 71a2ee3 | 2007-11-22 12:19:37 -0300 | [diff] [blame] | 1204 | if (cfg->ctrl) |
| 1205 | xc2028_set_config(fe, cfg->ctrl); |
| 1206 | |
Chris Pascoe | aa501be | 2007-11-19 04:45:38 -0300 | [diff] [blame] | 1207 | mutex_unlock(&xc2028_list_mutex); |
| 1208 | |
Michel Ludwig | a37b4c9 | 2007-11-16 07:46:14 -0300 | [diff] [blame] | 1209 | return fe; |
Michael Krufky | c663d03 | 2008-04-18 21:22:50 -0300 | [diff] [blame^] | 1210 | fail: |
| 1211 | mutex_unlock(&xc2028_list_mutex); |
| 1212 | |
| 1213 | xc2028_dvb_release(fe); |
| 1214 | return NULL; |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1215 | } |
Michel Ludwig | a37b4c9 | 2007-11-16 07:46:14 -0300 | [diff] [blame] | 1216 | |
Michel Ludwig | 701672e | 2007-07-18 10:29:10 -0300 | [diff] [blame] | 1217 | EXPORT_SYMBOL(xc2028_attach); |
| 1218 | |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1219 | MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver"); |
Mauro Carvalho Chehab | 983d214 | 2007-10-29 23:44:18 -0300 | [diff] [blame] | 1220 | MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>"); |
Mauro Carvalho Chehab | 215b95b | 2007-10-23 15:24:06 -0300 | [diff] [blame] | 1221 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); |
| 1222 | MODULE_LICENSE("GPL"); |