Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Afatech AF9035 DVB USB driver |
| 3 | * |
| 4 | * Copyright (C) 2009 Antti Palosaari <crope@iki.fi> |
| 5 | * Copyright (C) 2012 Antti Palosaari <crope@iki.fi> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "af9035.h" |
| 23 | #include "af9033.h" |
| 24 | #include "tua9001.h" |
| 25 | |
| 26 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 27 | static DEFINE_MUTEX(af9035_usb_mutex); |
| 28 | static struct config af9035_config; |
| 29 | static struct dvb_usb_device_properties af9035_properties[1]; |
| 30 | static int af9035_properties_count = ARRAY_SIZE(af9035_properties); |
| 31 | static struct af9033_config af9035_af9033_config[] = { |
| 32 | { |
| 33 | .ts_mode = AF9033_TS_MODE_USB, |
| 34 | }, { |
| 35 | .ts_mode = AF9033_TS_MODE_SERIAL, |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | static int af9035_ctrl_msg(struct usb_device *udev, struct usb_req *req) |
| 40 | { |
| 41 | #define BUF_LEN 63 |
| 42 | #define REQ_HDR_LEN 4 /* send header size */ |
| 43 | #define ACK_HDR_LEN 3 /* rece header size */ |
| 44 | #define CHECKSUM_LEN 2 |
| 45 | #define USB_TIMEOUT 2000 |
| 46 | |
| 47 | int ret, i, act_len; |
| 48 | u8 buf[BUF_LEN]; |
| 49 | u32 msg_len; |
| 50 | static u8 seq; /* packet sequence number */ |
| 51 | u16 checksum = 0; |
| 52 | |
| 53 | /* buffer overflow check */ |
| 54 | if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) || |
| 55 | req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) { |
| 56 | pr_debug("%s: too much data wlen=%d rlen=%d\n", __func__, |
| 57 | req->wlen, req->rlen); |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | |
| 61 | if (mutex_lock_interruptible(&af9035_usb_mutex) < 0) |
| 62 | return -EAGAIN; |
| 63 | |
| 64 | buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1; |
| 65 | buf[1] = req->mbox; |
| 66 | buf[2] = req->cmd; |
| 67 | buf[3] = seq++; |
| 68 | if (req->wlen) |
| 69 | memcpy(&buf[4], req->wbuf, req->wlen); |
| 70 | |
| 71 | /* calc and add checksum */ |
| 72 | for (i = 1; i < buf[0]-1; i++) { |
| 73 | if (i % 2) |
| 74 | checksum += buf[i] << 8; |
| 75 | else |
| 76 | checksum += buf[i]; |
| 77 | } |
| 78 | checksum = ~checksum; |
| 79 | |
| 80 | buf[buf[0]-1] = (checksum >> 8); |
| 81 | buf[buf[0]-0] = (checksum & 0xff); |
| 82 | |
| 83 | msg_len = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN ; |
| 84 | |
| 85 | /* send req */ |
| 86 | ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len, |
| 87 | &act_len, USB_TIMEOUT); |
| 88 | if (ret < 0) |
| 89 | err("bulk message failed=%d (%d/%d)", ret, msg_len, act_len); |
| 90 | else |
| 91 | if (act_len != msg_len) |
| 92 | ret = -EIO; /* all data is not send */ |
| 93 | if (ret < 0) |
| 94 | goto err_mutex_unlock; |
| 95 | |
| 96 | /* no ack for those packets */ |
| 97 | if (req->cmd == CMD_FW_DL) |
| 98 | goto exit_mutex_unlock; |
| 99 | |
| 100 | /* receive ack and data if read req */ |
| 101 | msg_len = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN; |
| 102 | ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len, |
| 103 | &act_len, USB_TIMEOUT); |
| 104 | if (ret < 0) { |
| 105 | err("recv bulk message failed=%d", ret); |
| 106 | ret = -EIO; |
| 107 | goto err_mutex_unlock; |
| 108 | } |
| 109 | |
| 110 | /* check status */ |
| 111 | if (buf[2]) { |
| 112 | pr_debug("%s: command=%02x failed fw error=%d\n", __func__, |
| 113 | req->cmd, buf[2]); |
| 114 | ret = -EIO; |
| 115 | goto err_mutex_unlock; |
| 116 | } |
| 117 | |
| 118 | /* read request, copy returned data to return buf */ |
| 119 | if (req->rlen) |
| 120 | memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen); |
| 121 | |
| 122 | err_mutex_unlock: |
| 123 | exit_mutex_unlock: |
| 124 | mutex_unlock(&af9035_usb_mutex); |
| 125 | |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | /* write multiple registers */ |
| 130 | static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len) |
| 131 | { |
| 132 | u8 wbuf[6 + len]; |
| 133 | u8 mbox = (reg >> 16) & 0xff; |
| 134 | struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL }; |
| 135 | |
| 136 | wbuf[0] = len; |
| 137 | wbuf[1] = 2; |
| 138 | wbuf[2] = 0; |
| 139 | wbuf[3] = 0; |
| 140 | wbuf[4] = (reg >> 8) & 0xff; |
| 141 | wbuf[5] = (reg >> 0) & 0xff; |
| 142 | memcpy(&wbuf[6], val, len); |
| 143 | |
| 144 | return af9035_ctrl_msg(d->udev, &req); |
| 145 | } |
| 146 | |
| 147 | /* read multiple registers */ |
| 148 | static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len) |
| 149 | { |
| 150 | u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff }; |
| 151 | u8 mbox = (reg >> 16) & 0xff; |
| 152 | struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val }; |
| 153 | |
| 154 | return af9035_ctrl_msg(d->udev, &req); |
| 155 | } |
| 156 | |
| 157 | /* write single register */ |
| 158 | static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val) |
| 159 | { |
| 160 | return af9035_wr_regs(d, reg, &val, 1); |
| 161 | } |
| 162 | |
| 163 | /* read single register */ |
| 164 | static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val) |
| 165 | { |
| 166 | return af9035_rd_regs(d, reg, val, 1); |
| 167 | } |
| 168 | |
| 169 | /* write single register with mask */ |
| 170 | static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val, |
| 171 | u8 mask) |
| 172 | { |
| 173 | int ret; |
| 174 | u8 tmp; |
| 175 | |
| 176 | /* no need for read if whole reg is written */ |
| 177 | if (mask != 0xff) { |
| 178 | ret = af9035_rd_regs(d, reg, &tmp, 1); |
| 179 | if (ret) |
| 180 | return ret; |
| 181 | |
| 182 | val &= mask; |
| 183 | tmp &= ~mask; |
| 184 | val |= tmp; |
| 185 | } |
| 186 | |
| 187 | return af9035_wr_regs(d, reg, &val, 1); |
| 188 | } |
| 189 | |
| 190 | static int af9035_i2c_master_xfer(struct i2c_adapter *adap, |
| 191 | struct i2c_msg msg[], int num) |
| 192 | { |
| 193 | struct dvb_usb_device *d = i2c_get_adapdata(adap); |
| 194 | int ret; |
| 195 | |
| 196 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) |
| 197 | return -EAGAIN; |
| 198 | |
| 199 | if (num == 2 && !(msg[0].flags & I2C_M_RD) && |
| 200 | (msg[1].flags & I2C_M_RD)) { |
| 201 | if (msg[0].len > 40 || msg[1].len > 40) { |
| 202 | /* TODO: correct limits > 40 */ |
| 203 | ret = -EOPNOTSUPP; |
| 204 | } else if (msg[0].addr == af9035_af9033_config[0].i2c_addr) { |
| 205 | /* integrated demod */ |
| 206 | u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 | |
| 207 | msg[0].buf[2]; |
| 208 | ret = af9035_rd_regs(d, reg, &msg[1].buf[0], |
| 209 | msg[1].len); |
| 210 | } else { |
| 211 | /* I2C */ |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 212 | u8 buf[4 + msg[0].len]; |
| 213 | struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf), |
| 214 | buf, msg[1].len, msg[1].buf }; |
Hans-Frieder Vogt | 812fe6d | 2012-04-01 14:11:29 -0300 | [diff] [blame^] | 215 | buf[0] = msg[1].len; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 216 | buf[1] = msg[0].addr << 1; |
| 217 | buf[2] = 0x01; |
| 218 | buf[3] = 0x00; |
| 219 | memcpy(&buf[4], msg[0].buf, msg[0].len); |
| 220 | ret = af9035_ctrl_msg(d->udev, &req); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 221 | } |
| 222 | } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) { |
| 223 | if (msg[0].len > 40) { |
| 224 | /* TODO: correct limits > 40 */ |
| 225 | ret = -EOPNOTSUPP; |
| 226 | } else if (msg[0].addr == af9035_af9033_config[0].i2c_addr) { |
| 227 | /* integrated demod */ |
| 228 | u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 | |
| 229 | msg[0].buf[2]; |
| 230 | ret = af9035_wr_regs(d, reg, &msg[0].buf[3], |
| 231 | msg[0].len - 3); |
| 232 | } else { |
| 233 | /* I2C */ |
| 234 | u8 buf[4 + msg[0].len]; |
| 235 | struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf, |
| 236 | 0, NULL }; |
| 237 | buf[0] = msg[0].len; |
| 238 | buf[1] = msg[0].addr << 1; |
| 239 | buf[2] = 0x01; |
| 240 | buf[3] = 0x00; |
| 241 | memcpy(&buf[4], msg[0].buf, msg[0].len); |
| 242 | ret = af9035_ctrl_msg(d->udev, &req); |
| 243 | } |
| 244 | } else { |
| 245 | /* |
| 246 | * We support only two kind of I2C transactions: |
| 247 | * 1) 1 x read + 1 x write |
| 248 | * 2) 1 x write |
| 249 | */ |
| 250 | ret = -EOPNOTSUPP; |
| 251 | } |
| 252 | |
| 253 | mutex_unlock(&d->i2c_mutex); |
| 254 | |
| 255 | if (ret < 0) |
| 256 | return ret; |
| 257 | else |
| 258 | return num; |
| 259 | } |
| 260 | |
| 261 | static u32 af9035_i2c_functionality(struct i2c_adapter *adapter) |
| 262 | { |
| 263 | return I2C_FUNC_I2C; |
| 264 | } |
| 265 | |
| 266 | static struct i2c_algorithm af9035_i2c_algo = { |
| 267 | .master_xfer = af9035_i2c_master_xfer, |
| 268 | .functionality = af9035_i2c_functionality, |
| 269 | }; |
| 270 | |
| 271 | static int af9035_init(struct dvb_usb_device *d) |
| 272 | { |
| 273 | int ret, i; |
| 274 | u16 frame_size = 87 * 188 / 4; |
| 275 | u8 packet_size = 512 / 4; |
| 276 | struct reg_val_mask tab[] = { |
| 277 | { 0x80f99d, 0x01, 0x01 }, |
| 278 | { 0x80f9a4, 0x01, 0x01 }, |
| 279 | { 0x00dd11, 0x00, 0x20 }, |
| 280 | { 0x00dd11, 0x00, 0x40 }, |
| 281 | { 0x00dd13, 0x00, 0x20 }, |
| 282 | { 0x00dd13, 0x00, 0x40 }, |
| 283 | { 0x00dd11, 0x20, 0x20 }, |
| 284 | { 0x00dd88, (frame_size >> 0) & 0xff, 0xff}, |
| 285 | { 0x00dd89, (frame_size >> 8) & 0xff, 0xff}, |
| 286 | { 0x00dd0c, packet_size, 0xff}, |
| 287 | { 0x00dd11, af9035_config.dual_mode << 6, 0x40 }, |
| 288 | { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff}, |
| 289 | { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff}, |
| 290 | { 0x00dd0d, packet_size, 0xff }, |
| 291 | { 0x80f9a3, 0x00, 0x01 }, |
| 292 | { 0x80f9cd, 0x00, 0x01 }, |
| 293 | { 0x80f99d, 0x00, 0x01 }, |
| 294 | { 0x80f9a4, 0x00, 0x01 }, |
| 295 | }; |
| 296 | |
| 297 | pr_debug("%s: USB speed=%d frame_size=%04x packet_size=%02x\n", |
| 298 | __func__, d->udev->speed, frame_size, packet_size); |
| 299 | |
| 300 | /* init endpoints */ |
| 301 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 302 | ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val, |
| 303 | tab[i].mask); |
| 304 | if (ret < 0) |
| 305 | goto err; |
| 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | |
| 310 | err: |
| 311 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 312 | |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static int af9035_identify_state(struct usb_device *udev, |
| 317 | struct dvb_usb_device_properties *props, |
| 318 | struct dvb_usb_device_description **desc, |
| 319 | int *cold) |
| 320 | { |
| 321 | int ret; |
| 322 | u8 wbuf[1] = { 1 }; |
| 323 | u8 rbuf[4]; |
| 324 | struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf, |
| 325 | sizeof(rbuf), rbuf }; |
| 326 | |
| 327 | ret = af9035_ctrl_msg(udev, &req); |
| 328 | if (ret < 0) |
| 329 | goto err; |
| 330 | |
| 331 | pr_debug("%s: reply=%02x %02x %02x %02x\n", __func__, |
| 332 | rbuf[0], rbuf[1], rbuf[2], rbuf[3]); |
| 333 | if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3]) |
| 334 | *cold = 0; |
| 335 | else |
| 336 | *cold = 1; |
| 337 | |
| 338 | return 0; |
| 339 | |
| 340 | err: |
| 341 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 342 | |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | static int af9035_download_firmware(struct usb_device *udev, |
| 347 | const struct firmware *fw) |
| 348 | { |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 349 | int ret, i, j, len; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 350 | u8 wbuf[1]; |
| 351 | u8 rbuf[4]; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 352 | struct usb_req req = { 0, 0, 0, NULL, 0, NULL }; |
| 353 | struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL }; |
| 354 | struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ; |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 355 | u8 hdr_core; |
| 356 | u16 hdr_addr, hdr_data_len, hdr_checksum; |
| 357 | #define MAX_DATA 57 |
| 358 | #define HDR_SIZE 7 |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 359 | |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 360 | /* |
| 361 | * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info! |
| 362 | * |
| 363 | * byte 0: MCS 51 core |
| 364 | * There are two inside the AF9035 (1=Link and 2=OFDM) with separate |
| 365 | * address spaces |
| 366 | * byte 1-2: Big endian destination address |
| 367 | * byte 3-4: Big endian number of data bytes following the header |
| 368 | * byte 5-6: Big endian header checksum, apparently ignored by the chip |
| 369 | * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256) |
| 370 | */ |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 371 | |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 372 | for (i = fw->size; i > HDR_SIZE;) { |
| 373 | hdr_core = fw->data[fw->size - i + 0]; |
| 374 | hdr_addr = fw->data[fw->size - i + 1] << 8; |
| 375 | hdr_addr |= fw->data[fw->size - i + 2] << 0; |
| 376 | hdr_data_len = fw->data[fw->size - i + 3] << 8; |
| 377 | hdr_data_len |= fw->data[fw->size - i + 4] << 0; |
| 378 | hdr_checksum = fw->data[fw->size - i + 5] << 8; |
| 379 | hdr_checksum |= fw->data[fw->size - i + 6] << 0; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 380 | |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 381 | pr_debug("%s: core=%d addr=%04x data_len=%d checksum=%04x\n", |
| 382 | __func__, hdr_core, hdr_addr, hdr_data_len, |
| 383 | hdr_checksum); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 384 | |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 385 | if (((hdr_core != 1) && (hdr_core != 2)) || |
| 386 | (hdr_data_len > i)) { |
| 387 | pr_debug("%s: bad firmware\n", __func__); |
| 388 | break; |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 389 | } |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 390 | |
| 391 | /* download begin packet */ |
| 392 | req.cmd = CMD_FW_DL_BEGIN; |
| 393 | ret = af9035_ctrl_msg(udev, &req); |
Antti Palosaari | 41d44a8 | 2012-04-01 11:06:23 -0300 | [diff] [blame] | 394 | if (ret < 0) |
| 395 | goto err; |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 396 | |
| 397 | /* download firmware packet(s) */ |
| 398 | for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) { |
| 399 | len = j; |
| 400 | if (len > MAX_DATA) |
| 401 | len = MAX_DATA; |
| 402 | req_fw_dl.wlen = len; |
| 403 | req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i + |
| 404 | HDR_SIZE + hdr_data_len - j]; |
| 405 | ret = af9035_ctrl_msg(udev, &req_fw_dl); |
| 406 | if (ret < 0) |
| 407 | goto err; |
| 408 | } |
| 409 | |
| 410 | /* download end packet */ |
| 411 | req.cmd = CMD_FW_DL_END; |
| 412 | ret = af9035_ctrl_msg(udev, &req); |
| 413 | if (ret < 0) |
| 414 | goto err; |
| 415 | |
| 416 | i -= hdr_data_len + HDR_SIZE; |
| 417 | |
| 418 | pr_debug("%s: data uploaded=%lu\n", __func__, fw->size - i); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* firmware loaded, request boot */ |
| 422 | req.cmd = CMD_FW_BOOT; |
| 423 | ret = af9035_ctrl_msg(udev, &req); |
| 424 | if (ret < 0) |
| 425 | goto err; |
| 426 | |
| 427 | /* ensure firmware starts */ |
| 428 | wbuf[0] = 1; |
| 429 | ret = af9035_ctrl_msg(udev, &req_fw_ver); |
| 430 | if (ret < 0) |
| 431 | goto err; |
| 432 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 433 | if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) { |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 434 | info("firmware did not run"); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 435 | ret = -ENODEV; |
| 436 | goto err; |
| 437 | } |
| 438 | |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 439 | info("firmware version=%d.%d.%d.%d", rbuf[0], rbuf[1], rbuf[2], |
| 440 | rbuf[3]); |
| 441 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 442 | return 0; |
| 443 | |
| 444 | err: |
| 445 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 446 | |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | /* abuse that callback as there is no better one for reading eeprom */ |
| 451 | static int af9035_read_mac_address(struct dvb_usb_device *d, u8 mac[6]) |
| 452 | { |
| 453 | int ret, i, eeprom_shift = 0; |
| 454 | u8 tmp; |
| 455 | u16 tmp16; |
| 456 | |
| 457 | /* check if there is dual tuners */ |
| 458 | ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp); |
| 459 | if (ret < 0) |
| 460 | goto err; |
| 461 | |
| 462 | af9035_config.dual_mode = tmp; |
| 463 | pr_debug("%s: dual mode=%d\n", __func__, af9035_config.dual_mode); |
| 464 | |
| 465 | for (i = 0; i < af9035_properties[0].num_adapters; i++) { |
| 466 | /* tuner */ |
| 467 | ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp); |
| 468 | if (ret < 0) |
| 469 | goto err; |
| 470 | |
| 471 | af9035_af9033_config[i].tuner = tmp; |
| 472 | pr_debug("%s: [%d]tuner=%02x\n", __func__, i, tmp); |
| 473 | |
| 474 | switch (tmp) { |
| 475 | case AF9033_TUNER_TUA9001: |
| 476 | af9035_af9033_config[i].spec_inv = 1; |
| 477 | break; |
| 478 | default: |
Antti Palosaari | 5a9abae | 2012-03-30 17:15:16 -0300 | [diff] [blame] | 479 | af9035_config.hw_not_supported = true; |
| 480 | warn("tuner ID=%02x not supported, please report!", |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 481 | tmp); |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 482 | }; |
| 483 | |
| 484 | /* tuner IF frequency */ |
| 485 | ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp); |
| 486 | if (ret < 0) |
| 487 | goto err; |
| 488 | |
| 489 | tmp16 = tmp; |
| 490 | |
| 491 | ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp); |
| 492 | if (ret < 0) |
| 493 | goto err; |
| 494 | |
| 495 | tmp16 |= tmp << 8; |
| 496 | |
| 497 | pr_debug("%s: [%d]IF=%d\n", __func__, i, tmp16); |
| 498 | |
| 499 | eeprom_shift = 0x10; /* shift for the 2nd tuner params */ |
| 500 | } |
| 501 | |
| 502 | /* get demod clock */ |
| 503 | ret = af9035_rd_reg(d, 0x00d800, &tmp); |
| 504 | if (ret < 0) |
| 505 | goto err; |
| 506 | |
| 507 | tmp = (tmp >> 0) & 0x0f; |
| 508 | |
| 509 | for (i = 0; i < af9035_properties[0].num_adapters; i++) |
| 510 | af9035_af9033_config[i].clock = clock_lut[tmp]; |
| 511 | |
| 512 | return 0; |
| 513 | |
| 514 | err: |
| 515 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 516 | |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | static int af9035_frontend_attach(struct dvb_usb_adapter *adap) |
| 521 | { |
| 522 | int ret; |
| 523 | |
Antti Palosaari | 5a9abae | 2012-03-30 17:15:16 -0300 | [diff] [blame] | 524 | if (af9035_config.hw_not_supported) { |
| 525 | ret = -ENODEV; |
| 526 | goto err; |
| 527 | } |
| 528 | |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 529 | if (adap->id == 0) { |
| 530 | ret = af9035_wr_reg(adap->dev, 0x00417f, |
| 531 | af9035_af9033_config[1].i2c_addr); |
| 532 | if (ret < 0) |
| 533 | goto err; |
| 534 | |
| 535 | ret = af9035_wr_reg(adap->dev, 0x00d81a, |
| 536 | af9035_config.dual_mode); |
| 537 | if (ret < 0) |
| 538 | goto err; |
| 539 | } |
| 540 | |
| 541 | /* attach demodulator */ |
| 542 | adap->fe_adap[0].fe = dvb_attach(af9033_attach, |
| 543 | &af9035_af9033_config[adap->id], &adap->dev->i2c_adap); |
| 544 | if (adap->fe_adap[0].fe == NULL) { |
| 545 | ret = -ENODEV; |
| 546 | goto err; |
| 547 | } |
| 548 | |
| 549 | return 0; |
| 550 | |
| 551 | err: |
| 552 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 553 | |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | static struct tua9001_config af9035_tua9001_config = { |
| 558 | .i2c_addr = 0x60, |
| 559 | }; |
| 560 | |
| 561 | static int af9035_tuner_attach(struct dvb_usb_adapter *adap) |
| 562 | { |
| 563 | int ret; |
| 564 | struct dvb_frontend *fe; |
| 565 | |
| 566 | switch (af9035_af9033_config[adap->id].tuner) { |
| 567 | case AF9033_TUNER_TUA9001: |
| 568 | /* AF9035 gpiot3 = TUA9001 RESETN |
| 569 | AF9035 gpiot2 = TUA9001 RXEN */ |
| 570 | |
| 571 | /* configure gpiot2 and gpiot2 as output */ |
| 572 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8ec, 0x01, 0x01); |
| 573 | if (ret < 0) |
| 574 | goto err; |
| 575 | |
| 576 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8ed, 0x01, 0x01); |
| 577 | if (ret < 0) |
| 578 | goto err; |
| 579 | |
| 580 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8e8, 0x01, 0x01); |
| 581 | if (ret < 0) |
| 582 | goto err; |
| 583 | |
| 584 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8e9, 0x01, 0x01); |
| 585 | if (ret < 0) |
| 586 | goto err; |
| 587 | |
| 588 | /* reset tuner */ |
| 589 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x00, 0x01); |
| 590 | if (ret < 0) |
| 591 | goto err; |
| 592 | |
| 593 | usleep_range(2000, 20000); |
| 594 | |
| 595 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x01, 0x01); |
| 596 | if (ret < 0) |
| 597 | goto err; |
| 598 | |
| 599 | /* activate tuner RX */ |
| 600 | /* TODO: use callback for TUA9001 RXEN */ |
| 601 | ret = af9035_wr_reg_mask(adap->dev, 0x00d8eb, 0x01, 0x01); |
| 602 | if (ret < 0) |
| 603 | goto err; |
| 604 | |
| 605 | /* attach tuner */ |
| 606 | fe = dvb_attach(tua9001_attach, adap->fe_adap[0].fe, |
| 607 | &adap->dev->i2c_adap, &af9035_tua9001_config); |
| 608 | break; |
| 609 | default: |
| 610 | fe = NULL; |
| 611 | } |
| 612 | |
| 613 | if (fe == NULL) { |
| 614 | ret = -ENODEV; |
| 615 | goto err; |
| 616 | } |
| 617 | |
| 618 | return 0; |
| 619 | |
| 620 | err: |
| 621 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 622 | |
| 623 | return ret; |
| 624 | } |
| 625 | |
| 626 | enum af9035_id_entry { |
| 627 | AF9035_0CCD_0093, |
| 628 | }; |
| 629 | |
| 630 | static struct usb_device_id af9035_id[] = { |
| 631 | [AF9035_0CCD_0093] = { |
| 632 | USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK)}, |
| 633 | {}, |
| 634 | }; |
| 635 | |
| 636 | MODULE_DEVICE_TABLE(usb, af9035_id); |
| 637 | |
| 638 | static struct dvb_usb_device_properties af9035_properties[] = { |
| 639 | { |
| 640 | .caps = DVB_USB_IS_AN_I2C_ADAPTER, |
| 641 | |
| 642 | .usb_ctrl = DEVICE_SPECIFIC, |
| 643 | .download_firmware = af9035_download_firmware, |
Antti Palosaari | 77c5ff2 | 2012-04-01 01:32:23 -0300 | [diff] [blame] | 644 | .firmware = "dvb-usb-af9035-02.fw", |
Antti Palosaari | 7f882c2 | 2012-03-30 09:10:08 -0300 | [diff] [blame] | 645 | .no_reconnect = 1, |
| 646 | |
| 647 | .num_adapters = 1, |
| 648 | .adapter = { |
| 649 | { |
| 650 | .num_frontends = 1, |
| 651 | .fe = { |
| 652 | { |
| 653 | .frontend_attach = af9035_frontend_attach, |
| 654 | .tuner_attach = af9035_tuner_attach, |
| 655 | .stream = { |
| 656 | .type = USB_BULK, |
| 657 | .count = 6, |
| 658 | .endpoint = 0x84, |
| 659 | .u = { |
| 660 | .bulk = { |
| 661 | .buffersize = (87 * 188), |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | }, |
| 669 | |
| 670 | .identify_state = af9035_identify_state, |
| 671 | .read_mac_address = af9035_read_mac_address, |
| 672 | |
| 673 | .i2c_algo = &af9035_i2c_algo, |
| 674 | |
| 675 | .num_device_descs = 1, |
| 676 | .devices = { |
| 677 | { |
| 678 | .name = "TerraTec Cinergy T Stick", |
| 679 | .cold_ids = { |
| 680 | &af9035_id[AF9035_0CCD_0093], |
| 681 | }, |
| 682 | }, |
| 683 | } |
| 684 | }, |
| 685 | }; |
| 686 | |
| 687 | static int af9035_usb_probe(struct usb_interface *intf, |
| 688 | const struct usb_device_id *id) |
| 689 | { |
| 690 | int ret, i; |
| 691 | struct dvb_usb_device *d = NULL; |
| 692 | struct usb_device *udev; |
| 693 | bool found; |
| 694 | |
| 695 | pr_debug("%s: interface=%d\n", __func__, |
| 696 | intf->cur_altsetting->desc.bInterfaceNumber); |
| 697 | |
| 698 | /* interface 0 is used by DVB-T receiver and |
| 699 | interface 1 is for remote controller (HID) */ |
| 700 | if (intf->cur_altsetting->desc.bInterfaceNumber != 0) |
| 701 | return 0; |
| 702 | |
| 703 | /* Dynamic USB ID support. Replaces first device ID with current one. */ |
| 704 | udev = interface_to_usbdev(intf); |
| 705 | |
| 706 | for (i = 0, found = false; i < ARRAY_SIZE(af9035_id) - 1; i++) { |
| 707 | if (af9035_id[i].idVendor == |
| 708 | le16_to_cpu(udev->descriptor.idVendor) && |
| 709 | af9035_id[i].idProduct == |
| 710 | le16_to_cpu(udev->descriptor.idProduct)) { |
| 711 | found = true; |
| 712 | break; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | if (!found) { |
| 717 | pr_debug("%s: using dynamic ID %04x:%04x\n", __func__, |
| 718 | le16_to_cpu(udev->descriptor.idVendor), |
| 719 | le16_to_cpu(udev->descriptor.idProduct)); |
| 720 | af9035_properties[0].devices[0].cold_ids[0]->idVendor = |
| 721 | le16_to_cpu(udev->descriptor.idVendor); |
| 722 | af9035_properties[0].devices[0].cold_ids[0]->idProduct = |
| 723 | le16_to_cpu(udev->descriptor.idProduct); |
| 724 | } |
| 725 | |
| 726 | |
| 727 | for (i = 0; i < af9035_properties_count; i++) { |
| 728 | ret = dvb_usb_device_init(intf, &af9035_properties[i], |
| 729 | THIS_MODULE, &d, adapter_nr); |
| 730 | |
| 731 | if (ret == -ENODEV) |
| 732 | continue; |
| 733 | else |
| 734 | break; |
| 735 | } |
| 736 | |
| 737 | if (ret < 0) |
| 738 | goto err; |
| 739 | |
| 740 | if (d) { |
| 741 | ret = af9035_init(d); |
| 742 | if (ret < 0) |
| 743 | goto err; |
| 744 | } |
| 745 | |
| 746 | return 0; |
| 747 | |
| 748 | err: |
| 749 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 750 | |
| 751 | return ret; |
| 752 | } |
| 753 | |
| 754 | /* usb specific object needed to register this driver with the usb subsystem */ |
| 755 | static struct usb_driver af9035_usb_driver = { |
| 756 | .name = "dvb_usb_af9035", |
| 757 | .probe = af9035_usb_probe, |
| 758 | .disconnect = dvb_usb_device_exit, |
| 759 | .id_table = af9035_id, |
| 760 | }; |
| 761 | |
| 762 | /* module stuff */ |
| 763 | static int __init af9035_usb_module_init(void) |
| 764 | { |
| 765 | int ret; |
| 766 | |
| 767 | ret = usb_register(&af9035_usb_driver); |
| 768 | if (ret < 0) |
| 769 | goto err; |
| 770 | |
| 771 | return 0; |
| 772 | |
| 773 | err: |
| 774 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 775 | |
| 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | static void __exit af9035_usb_module_exit(void) |
| 780 | { |
| 781 | /* deregister this driver from the USB subsystem */ |
| 782 | usb_deregister(&af9035_usb_driver); |
| 783 | } |
| 784 | |
| 785 | module_init(af9035_usb_module_init); |
| 786 | module_exit(af9035_usb_module_exit); |
| 787 | |
| 788 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 789 | MODULE_DESCRIPTION("Afatech AF9035 driver"); |
| 790 | MODULE_LICENSE("GPL"); |