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