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