blob: dada92a14cf3b49acf0a535dfd1cb1a10063c2d6 [file] [log] [blame]
Antti Palosaaria51e34d2008-05-17 23:05:48 -03001/*
2 * DVB USB Linux driver for Anysee E30 DVB-C & DVB-T USB2.0 receiver
3 *
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * TODO:
21 * - add smart card reader support for Conditional Access (CA)
22 *
23 * Card reader in Anysee is nothing more than ISO 7816 card reader.
24 * There is no hardware CAM in any Anysee device sold.
25 * In my understanding it should be implemented by making own module
26 * for ISO 7816 card reader, like dvb_ca_en50221 is implented. This
27 * module registers serial interface that can be used to comminicate
28 * with any ISO 7816 smart card.
29 *
30 * Any help according to implement serial smart card reader support
31 * is highly welcome!
32 */
33
34#include "anysee.h"
35#include "tda1002x.h"
36#include "mt352.h"
37#include "mt352_priv.h"
38#include "zl10353.h"
39
40/* debug */
41static int dvb_usb_anysee_debug;
42module_param_named(debug, dvb_usb_anysee_debug, int, 0644);
43MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
44DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
45
46struct mutex anysee_usb_mutex;
47
48static int anysee_ctrl_msg(struct dvb_usb_device *d, u8 *sbuf, u8 slen,
49 u8 *rbuf, u8 rlen)
50{
51 struct anysee_state *state = d->priv;
52 int act_len, ret;
53 u8 buf[64];
54
55 if (slen > sizeof(buf))
56 slen = sizeof(buf);
57 memcpy(&buf[0], sbuf, slen);
58 buf[60] = state->seq++;
59
60 if (mutex_lock_interruptible(&anysee_usb_mutex) < 0)
61 return -EAGAIN;
62
63 /* We need receive one message more after dvb_usb_generic_rw due
64 to weird transaction flow, which is 1 x send + 2 x receive. */
65 ret = dvb_usb_generic_rw(d, buf, sizeof(buf), buf, sizeof(buf), 0);
66
67 if (!ret) {
68 /* receive 2nd answer */
69 ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
70 d->props.generic_bulk_ctrl_endpoint), buf, sizeof(buf),
71 &act_len, 2000);
72 if (ret)
73 err("%s: recv bulk message failed: %d", __func__, ret);
74 else {
75 deb_xfer("<<< ");
76 debug_dump(buf, act_len, deb_xfer);
77 }
78 }
79
80 /* read request, copy returned data to return buf */
81 if (!ret && rbuf && rlen)
82 memcpy(rbuf, buf, rlen);
83
84 mutex_unlock(&anysee_usb_mutex);
85
86 return ret;
87}
88
89static int anysee_read_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
90{
91 u8 buf[] = {CMD_REG_READ, reg >> 8, reg & 0xff, 0x01};
92 int ret;
93 ret = anysee_ctrl_msg(d, buf, sizeof(buf), val, 1);
94 deb_info("%s: reg:%04x val:%02x\n", __func__, reg, *val);
95 return ret;
96}
97
98static int anysee_write_reg(struct dvb_usb_device *d, u16 reg, u8 val)
99{
100 u8 buf[] = {CMD_REG_WRITE, reg >> 8, reg & 0xff, 0x01, val};
101 deb_info("%s: reg:%04x val:%02x\n", __func__, reg, val);
102 return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
103}
104
105static int anysee_get_hw_info(struct dvb_usb_device *d, u8 *id)
106{
107 u8 buf[] = {CMD_GET_HW_INFO};
108 return anysee_ctrl_msg(d, buf, sizeof(buf), id, 3);
109}
110
111static int anysee_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
112{
113 u8 buf[] = {CMD_STREAMING_CTRL, (u8)onoff, 0x00};
114 deb_info("%s: onoff:%02x\n", __func__, onoff);
115 return anysee_ctrl_msg(adap->dev, buf, sizeof(buf), NULL, 0);
116}
117
118static int anysee_led_ctrl(struct dvb_usb_device *d, u8 mode, u8 interval)
119{
120 u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x01, mode, interval};
121 deb_info("%s: state:%02x interval:%02x\n", __func__, mode, interval);
122 return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
123}
124
125static int anysee_ir_ctrl(struct dvb_usb_device *d, u8 onoff)
126{
127 u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x02, onoff};
128 deb_info("%s: onoff:%02x\n", __func__, onoff);
129 return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
130}
131
132static int anysee_init(struct dvb_usb_device *d)
133{
134 int ret;
135 /* LED light */
136 ret = anysee_led_ctrl(d, 0x01, 0x03);
137 if (ret)
138 return ret;
139
140 /* enable IR */
141 ret = anysee_ir_ctrl(d, 1);
142 if (ret)
143 return ret;
144
145 return 0;
146}
147
148/* I2C */
149static int anysee_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
150 int num)
151{
152 struct dvb_usb_device *d = i2c_get_adapdata(adap);
153 int ret, inc, i = 0;
154
155 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
156 return -EAGAIN;
157
158 while (i < num) {
159 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
160 u8 buf[6];
161 buf[0] = CMD_I2C_READ;
162 buf[1] = msg[i].addr + 1;
163 buf[2] = msg[i].buf[0];
164 buf[3] = 0x00;
165 buf[4] = 0x00;
166 buf[5] = 0x01;
167 ret = anysee_ctrl_msg(d, buf, sizeof(buf), msg[i+1].buf,
168 msg[i+1].len);
169 inc = 2;
170 } else {
171 u8 buf[4+msg[i].len];
172 buf[0] = CMD_I2C_WRITE;
173 buf[1] = msg[i].addr;
174 buf[2] = msg[i].len;
175 buf[3] = 0x01;
176 memcpy(&buf[4], msg[i].buf, msg[i].len);
177 ret = anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
178 inc = 1;
179 }
180 if (ret)
181 return ret;
182
183 i += inc;
184 }
185
186 mutex_unlock(&d->i2c_mutex);
187
188 return i;
189}
190
191static u32 anysee_i2c_func(struct i2c_adapter *adapter)
192{
193 return I2C_FUNC_I2C;
194}
195
196static struct i2c_algorithm anysee_i2c_algo = {
197 .master_xfer = anysee_master_xfer,
198 .functionality = anysee_i2c_func,
199};
200
201static int anysee_mt352_demod_init(struct dvb_frontend *fe)
202{
203 static u8 clock_config [] = { CLOCK_CTL, 0x38, 0x28 };
204 static u8 reset [] = { RESET, 0x80 };
205 static u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
206 static u8 agc_cfg [] = { AGC_TARGET, 0x28, 0x20 };
207 static u8 gpp_ctl_cfg [] = { GPP_CTL, 0x33 };
208 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
209
210 mt352_write(fe, clock_config, sizeof(clock_config));
211 udelay(200);
212 mt352_write(fe, reset, sizeof(reset));
213 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
214
215 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
216 mt352_write(fe, gpp_ctl_cfg, sizeof(gpp_ctl_cfg));
217 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
218
219 return 0;
220}
221
222/* Callbacks for DVB USB */
223static struct tda10023_config anysee_tda10023_config = {
224 .demod_address = 0x1a,
225 .invert = 0,
226 .xtal = 16000000,
227 .pll_m = 11,
228 .pll_p = 3,
229 .pll_n = 1,
230 .deltaf = 0xfed6,
231};
232
233static struct mt352_config anysee_mt352_config = {
234 .demod_address = 0x1e,
235 .demod_init = anysee_mt352_demod_init,
236};
237
238static struct zl10353_config anysee_zl10353_config = {
239 .demod_address = 0x1e,
240 .parallel_ts = 1,
241};
242
243static int anysee_frontend_attach(struct dvb_usb_adapter *adap)
244{
245 int ret;
246 struct anysee_state *state = adap->dev->priv;
247 u8 hw_info[3];
248 u8 io_d; /* IO port D */
249
250 /* check which hardware we have
251 We must do this call two times to get reliable values (hw bug). */
252 ret = anysee_get_hw_info(adap->dev, hw_info);
253 if (ret)
254 return ret;
255 ret = anysee_get_hw_info(adap->dev, hw_info);
256 if (ret)
257 return ret;
258
259 /* Meaning of these info bytes are guessed. */
260 info("firmware version:%d.%d.%d hardware id:%d",
261 0, hw_info[1], hw_info[2], hw_info[0]);
262
263 ret = anysee_read_reg(adap->dev, 0xb0, &io_d); /* IO port D */
264 if (ret)
265 return ret;
266 deb_info("%s: IO port D:%02x\n", __func__, io_d);
267
268 /* Select demod using trial and error method. */
269
270 /* Try to attach demodulator in following order:
271 model demod hw firmware
272 1. E30 MT352 02 0.2.1
273 2. E30 ZL10353 02 0.2.1
274 3. E30 Plus ZL10353 06 0.1.0
275 4. E30C Plus TDA10023 0a 0.1.0
276 E30C Plus TDA10023 0f 0.1.2 (not working)
277 */
278
279 /* Zarlink MT352 DVB-T demod inside of Samsung DNOS404ZH102A NIM */
280 adap->fe = dvb_attach(mt352_attach, &anysee_mt352_config,
281 &adap->dev->i2c_adap);
282 if (adap->fe != NULL) {
283 state->tuner = DVB_PLL_THOMSON_DTT7579;
284 return 0;
285 }
286
287 /* Zarlink ZL10353 DVB-T demod inside of Samsung DNOS404ZH103A NIM */
288 adap->fe = dvb_attach(zl10353_attach, &anysee_zl10353_config,
289 &adap->dev->i2c_adap);
290 if (adap->fe != NULL) {
291 state->tuner = DVB_PLL_THOMSON_DTT7579;
292 return 0;
293 }
294
295 /* connect demod on IO port D for TDA10023 & ZL10353 */
296 ret = anysee_write_reg(adap->dev, 0xb0, 0x25);
297 if (ret)
298 return ret;
299
300 /* Zarlink ZL10353 DVB-T demod inside of Samsung DNOS404ZH103A NIM */
301 adap->fe = dvb_attach(zl10353_attach, &anysee_zl10353_config,
302 &adap->dev->i2c_adap);
303 if (adap->fe != NULL) {
304 state->tuner = DVB_PLL_THOMSON_DTT7579;
305 return 0;
306 }
307
308 /* known not working (E30C Plus v0.1.2) */
309 if (hw_info[0] == 0x0f) {
310 info("this version of Anysee is not supported yet");
311 /* return IO port D to init value for safe */
312 ret = anysee_write_reg(adap->dev, 0xb0, io_d);
313 return -ENODEV;
314 }
315
316 /* Philips TDA10023 DVB-C demod */
317 adap->fe = dvb_attach(tda10023_attach, &anysee_tda10023_config,
318 &adap->dev->i2c_adap, 0x48);
319 if (adap->fe != NULL) {
320 state->tuner = DVB_PLL_SAMSUNG_DTOS403IH102A;
321 return 0;
322 }
323
324 /* return IO port D to init value for safe */
325 ret = anysee_write_reg(adap->dev, 0xb0, io_d);
326 if (ret)
327 return ret;
328
329 err("Unkown Anysee version: %02x %02x %02x. "\
330 "Please report the <linux-dvb@linuxtv.org>.",
331 hw_info[0], hw_info[1], hw_info[2]);
332
333 return -ENODEV;
334}
335
336static int anysee_tuner_attach(struct dvb_usb_adapter *adap)
337{
338 struct anysee_state *state = adap->dev->priv;
339 deb_info("%s: \n", __func__);
340
341 switch (state->tuner) {
342 case DVB_PLL_THOMSON_DTT7579:
343 /* Thomson dtt7579 (not sure) PLL inside of:
344 Samsung DNOS404ZH102A NIM
345 Samsung DNOS404ZH103A NIM */
346 dvb_attach(dvb_pll_attach, adap->fe, 0x61,
347 NULL, DVB_PLL_THOMSON_DTT7579);
348 break;
349 case DVB_PLL_SAMSUNG_DTOS403IH102A:
350 /* Unknown PLL inside of Samsung DTOS403IH102A tuner module */
351 dvb_attach(dvb_pll_attach, adap->fe, 0xc0,
352 &adap->dev->i2c_adap, DVB_PLL_SAMSUNG_DTOS403IH102A);
353 break;
354 }
355
356 return 0;
357}
358
359static int anysee_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
360{
361 u8 buf[] = {CMD_GET_IR_CODE};
362 struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
363 u8 ircode[2];
364 int i, ret;
365
366 ret = anysee_ctrl_msg(d, buf, sizeof(buf), &ircode[0], 2);
367 if (ret)
368 return ret;
369
370 *event = 0;
371 *state = REMOTE_NO_KEY_PRESSED;
372
373 for (i = 0; i < d->props.rc_key_map_size; i++) {
374 if (keymap[i].custom == ircode[0] &&
375 keymap[i].data == ircode[1]) {
376 *event = keymap[i].event;
377 *state = REMOTE_KEY_PRESSED;
378 return 0;
379 }
380 }
381 return 0;
382}
383
384static struct dvb_usb_rc_key anysee_rc_keys[] = {
385 { 0x01, 0x00, KEY_0 },
386 { 0x01, 0x01, KEY_1 },
387 { 0x01, 0x02, KEY_2 },
388 { 0x01, 0x03, KEY_3 },
389 { 0x01, 0x04, KEY_4 },
390 { 0x01, 0x05, KEY_5 },
391 { 0x01, 0x06, KEY_6 },
392 { 0x01, 0x07, KEY_7 },
393 { 0x01, 0x08, KEY_8 },
394 { 0x01, 0x09, KEY_9 },
395 { 0x01, 0x0a, KEY_POWER },
396 { 0x01, 0x0b, KEY_DOCUMENTS }, /* * */
397 { 0x01, 0x19, KEY_FAVORITES },
398 { 0x01, 0x20, KEY_SLEEP },
399 { 0x01, 0x21, KEY_MODE }, /* 4:3 / 16:9 select */
400 { 0x01, 0x22, KEY_ZOOM },
401 { 0x01, 0x47, KEY_TEXT },
402 { 0x01, 0x16, KEY_TV }, /* TV / radio select */
403 { 0x01, 0x1e, KEY_LANGUAGE }, /* Second Audio Program */
404 { 0x01, 0x1a, KEY_SUBTITLE },
405 { 0x01, 0x1b, KEY_CAMERA }, /* screenshot */
406 { 0x01, 0x42, KEY_MUTE },
407 { 0x01, 0x0e, KEY_MENU },
408 { 0x01, 0x0f, KEY_EPG },
409 { 0x01, 0x17, KEY_INFO },
410 { 0x01, 0x10, KEY_EXIT },
411 { 0x01, 0x13, KEY_VOLUMEUP },
412 { 0x01, 0x12, KEY_VOLUMEDOWN },
413 { 0x01, 0x11, KEY_CHANNELUP },
414 { 0x01, 0x14, KEY_CHANNELDOWN },
415 { 0x01, 0x15, KEY_OK },
416 { 0x01, 0x1d, KEY_RED },
417 { 0x01, 0x1f, KEY_GREEN },
418 { 0x01, 0x1c, KEY_YELLOW },
419 { 0x01, 0x44, KEY_BLUE },
420 { 0x01, 0x0c, KEY_SHUFFLE }, /* snapshot */
421 { 0x01, 0x48, KEY_STOP },
422 { 0x01, 0x50, KEY_PLAY },
423 { 0x01, 0x51, KEY_PAUSE },
424 { 0x01, 0x49, KEY_RECORD },
425 { 0x01, 0x18, KEY_PREVIOUS }, /* |<< */
426 { 0x01, 0x0d, KEY_NEXT }, /* >>| */
427 { 0x01, 0x24, KEY_PROG1 }, /* F1 */
428 { 0x01, 0x25, KEY_PROG2 }, /* F2 */
429};
430
431/* DVB USB Driver stuff */
432static struct dvb_usb_device_properties anysee_properties;
433
434static int anysee_probe(struct usb_interface *intf,
435 const struct usb_device_id *id)
436{
437 struct dvb_usb_device *d;
438 struct usb_host_interface *alt;
439 int ret;
440
441 mutex_init(&anysee_usb_mutex);
442
443 /* There is one interface with two alternate settings.
444 Alternate setting 0 is for bulk transfer.
445 Alternate setting 1 is for isochronous transfer.
446 We use bulk transfer (alternate setting 0). */
447 if (intf->num_altsetting < 1)
448 return -ENODEV;
449
450 ret = dvb_usb_device_init(intf, &anysee_properties, THIS_MODULE, &d,
451 adapter_nr);
452 if (ret)
453 return ret;
454
455 alt = usb_altnum_to_altsetting(intf, 0);
456 if (alt == NULL) {
457 deb_info("%s: no alt found!\n", __func__);
458 return -ENODEV;
459 }
460
461 ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber,
462 alt->desc.bAlternateSetting);
463 if (ret)
464 return ret;
465
466 if (d)
467 ret = anysee_init(d);
468
469 return ret;
470}
471
472static struct usb_device_id anysee_table [] = {
473 { USB_DEVICE(USB_VID_CYPRESS, USB_PID_ANYSEE) },
474 { USB_DEVICE(USB_VID_AMT, USB_PID_ANYSEE) },
475 { } /* Terminating entry */
476};
477MODULE_DEVICE_TABLE(usb, anysee_table);
478
479static struct dvb_usb_device_properties anysee_properties = {
480 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
481
482 .usb_ctrl = DEVICE_SPECIFIC,
483
484 .size_of_priv = sizeof(struct anysee_state),
485
486 .num_adapters = 1,
487 .adapter = {
488 {
489 .streaming_ctrl = anysee_streaming_ctrl,
490 .frontend_attach = anysee_frontend_attach,
491 .tuner_attach = anysee_tuner_attach,
492 .stream = {
493 .type = USB_BULK,
494 .count = 8,
495 .endpoint = 0x82,
496 .u = {
497 .bulk = {
498 .buffersize = 512,
499 }
500 }
501 },
502 }
503 },
504
505 .rc_key_map = anysee_rc_keys,
506 .rc_key_map_size = ARRAY_SIZE(anysee_rc_keys),
507 .rc_query = anysee_rc_query,
508 .rc_interval = 200, /* windows driver uses 500ms */
509
510 .i2c_algo = &anysee_i2c_algo,
511
512 .generic_bulk_ctrl_endpoint = 1,
513
514 .num_device_descs = 1,
515 .devices = {
516 {
517 .name = "Anysee DVB USB2.0",
518 .cold_ids = {NULL},
519 .warm_ids = {&anysee_table[0],
520 &anysee_table[1], NULL},
521 },
522 }
523};
524
525static struct usb_driver anysee_driver = {
526 .name = "dvb_usb_anysee",
527 .probe = anysee_probe,
528 .disconnect = dvb_usb_device_exit,
529 .id_table = anysee_table,
530};
531
532/* module stuff */
533static int __init anysee_module_init(void)
534{
535 int ret;
536
537 ret = usb_register(&anysee_driver);
538 if (ret)
539 err("%s: usb_register failed. Error number %d", __func__, ret);
540
541 return ret;
542}
543
544static void __exit anysee_module_exit(void)
545{
546 /* deregister this driver from the USB subsystem */
547 usb_deregister(&anysee_driver);
548}
549
550module_init(anysee_module_init);
551module_exit(anysee_module_exit);
552
553MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
554MODULE_DESCRIPTION("Driver Anysee E30 DVB-C & DVB-T USB2.0");
555MODULE_LICENSE("GPL");