1 /* DVB USB compliant linux driver for MSI Mega Sky 580 DVB-T USB2.0 receiver
3 * Copyright (C) 2006 Aapo Tahkola (aet@rasterburn.org)
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation, version 2.
9 * see Documentation/dvb/README.dvb-usb for more information
14 #include "mt352_priv.h"
17 int dvb_usb_megasky_debug;
18 module_param_named(debug,dvb_usb_megasky_debug, int, 0644);
19 MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
21 static struct dvb_usb_rc_key megasky_rc_keys [] = {
22 { 0x0, 0x12, KEY_POWER },
23 { 0x0, 0x1e, KEY_CYCLEWINDOWS }, /* min/max */
24 { 0x0, 0x02, KEY_CHANNELUP },
25 { 0x0, 0x05, KEY_CHANNELDOWN },
26 { 0x0, 0x03, KEY_VOLUMEUP },
27 { 0x0, 0x06, KEY_VOLUMEDOWN },
28 { 0x0, 0x04, KEY_MUTE },
29 { 0x0, 0x07, KEY_OK }, /* TS */
30 { 0x0, 0x08, KEY_STOP },
31 { 0x0, 0x09, KEY_MENU }, /* swap */
32 { 0x0, 0x0a, KEY_REWIND },
33 { 0x0, 0x1b, KEY_PAUSE },
34 { 0x0, 0x1f, KEY_FASTFORWARD },
35 { 0x0, 0x0c, KEY_RECORD },
36 { 0x0, 0x0d, KEY_CAMERA }, /* screenshot */
37 { 0x0, 0x0e, KEY_COFFEE }, /* "MTS" */
40 static inline int m9206_read(struct usb_device *udev, u8 request, u16 value, u16 index, void *data, int size)
44 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
45 request, USB_TYPE_VENDOR | USB_DIR_IN,
46 value, index, data, size, 2000);
56 static inline int m9206_write(struct usb_device *udev, u8 request, u16 value, u16 index)
60 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
61 request, USB_TYPE_VENDOR | USB_DIR_OUT,
62 value, index, NULL, 0, 2000);
68 static int m9206_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
73 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
76 if ((ret = m9206_read(d->udev, 0x22, 0x0, 0xff51, rc_state, 1)) != 0)
79 if ((ret = m9206_read(d->udev, 0x22, 0x0, 0xff52, rc_state + 1, 1)) != 0)
82 for (i = 0; i < ARRAY_SIZE(megasky_rc_keys); i++)
83 if (megasky_rc_keys[i].data == rc_state[1]) {
84 *event = megasky_rc_keys[i].event;
88 *state = REMOTE_NO_KEY_PRESSED;
93 *state = REMOTE_KEY_PRESSED;
97 *state = REMOTE_KEY_REPEAT;
101 deb_rc("Unexpected rc response %x\n", rc_state[0]);
102 *state = REMOTE_NO_KEY_PRESSED;
107 if (rc_state[1] != 0)
108 deb_rc("Unknown rc key %x\n", rc_state[1]);
110 *state = REMOTE_NO_KEY_PRESSED;
113 mutex_unlock(&d->i2c_mutex);
120 static int m9206_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
122 struct dvb_usb_device *d = i2c_get_adapdata(adap);
126 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
132 for (i = 0; i < num; i++) {
135 if ((ret = m9206_write(d->udev, 0x23, msg[i].addr, 0x80)) != 0)
138 if ((ret = m9206_write(d->udev, 0x23, msg[i].buf[0], 0x0)) != 0)
141 if (i + 1 < num && msg[i + 1].flags & I2C_M_RD) {
142 if (msg[i].addr == 0x1e)
147 if ((ret = m9206_write(d->udev, 0x23, w_len, 0x80)) != 0)
150 if ((ret = m9206_read(d->udev, 0x23, 0x0, 0x60, msg[i + 1].buf, msg[i + 1].len)) != 0)
158 if ((ret = m9206_write(d->udev, 0x23, msg[i].buf[1], 0x40)) != 0)
164 mutex_unlock(&d->i2c_mutex);
169 static u32 m9206_i2c_func(struct i2c_adapter *adapter)
174 static struct i2c_algorithm m9206_i2c_algo = {
175 .master_xfer = m9206_i2c_xfer,
176 .functionality = m9206_i2c_func,
179 /* Callbacks for DVB USB */
180 static int megasky_identify_state (struct usb_device *udev,
181 struct dvb_usb_properties *props,
182 struct dvb_usb_device_description **desc,
185 struct usb_host_interface *alt;
187 alt = usb_altnum_to_altsetting(usb_ifnum_to_if(udev, 0), 1);
188 *cold = (alt == NULL) ? 1 : 0;
193 static int megasky_mt352_demod_init(struct dvb_frontend *fe)
218 for (i = 0; i < ARRAY_SIZE(buf1); i += 2)
219 mt352_write(fe, &buf1[i], 2);
221 deb_rc("Demod init!\n");
231 /* Not actual hw limits. */
232 #define QT1010_MIN_STEP 2000000
233 #define QT1010_MIN_FREQ 48000000
235 int qt1010_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params, u8 *buf, int buf_len)
241 } rd[46] = { { W, 0x01, 0x80 },
243 { W, 0x05, 0xff }, /* c */
245 { W, 0x07, 0xff }, /* c */
247 { W, 0x09, 0xff }, /* c */
248 { W, 0x0a, 0xff }, /* c */
249 { W, 0x0b, 0xff }, /* c */
251 { W, 0x1a, 0xff }, /* 10 c */
254 { W, 0x11, 0xff }, /* c */
256 { W, 0x22, 0xff }, /* c */
259 { R, 0x22, 0xff }, /* c read */
261 { R, 0x05, 0xff }, /* 20 c read */
262 { R, 0x22, 0xff }, /* c read */
266 { R, 0x23, 0xff }, /* c read */
271 { R, 0x24, 0xff }, /* 30 c read */
275 { W, 0x05, 0xff }, /* c */
280 { W, 0x1f, 0xff }, /* c */
281 { W, 0x20, 0xff }, /* 40 c */
289 struct dvb_usb_device *d = fe->dvb->priv;
290 unsigned long freq = params->frequency;
292 if (freq % QT1010_MIN_STEP)
293 deb_rc("frequency not supported.\n");
298 div = (freq - QT1010_MIN_FREQ) / QT1010_MIN_STEP;
299 mod = (div + 16 - 9) % 16;
312 rd[4].value = (((freq - QT1010_MIN_FREQ) / 1000000) * 9975 + 12960000) / 320000;
410 deb_rc("Now tuning... ");
411 for (i = 0; i < sizeof(rd) / sizeof(*rd); i++) {
418 msg.buf = &rd[i].reg;
420 if (i2c_transfer(&d->i2c_adap, &msg, 1) != 1) {
421 deb_rc("tuner write failed\n");
432 static struct mt352_config megasky_mt352_config = {
433 .demod_address = 0x1e,
434 .demod_init = megasky_mt352_demod_init,
437 static int megasky_frontend_attach(struct dvb_usb_device *d)
439 deb_rc("megasky_frontend_attach!\n");
441 if ((d->fe = mt352_attach(&megasky_mt352_config, &d->i2c_adap)) != NULL) {
442 d->fe->ops.tuner_ops.calc_regs = qt1010_set_params;
448 /* DVB USB Driver stuff */
449 static struct dvb_usb_properties megasky_properties;
451 static int megasky_probe(struct usb_interface *intf, const struct usb_device_id *id)
453 struct dvb_usb_device *d;
454 struct usb_host_interface *alt;
457 if ((ret = dvb_usb_device_init(intf, &megasky_properties, THIS_MODULE, &d)) == 0) {
460 alt = usb_altnum_to_altsetting(intf, 1);
462 deb_rc("not alt found!\n");
466 ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber, alt->desc.bAlternateSetting);
470 deb_rc("Changed to alternate setting!\n");
472 /* Remote controller init. */
473 if ((ret = m9206_write(d->udev, 0x22, 0xa8, 0xff55)) != 0)
476 if ((ret = m9206_write(d->udev, 0x22, 0x51, 0xff54)) != 0)
482 static struct usb_device_id megasky_table [] = {
483 { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580) },
484 { } /* Terminating entry */
486 MODULE_DEVICE_TABLE (usb, megasky_table);
488 static int set_filter(struct dvb_usb_device *d, int type, int idx, int pid)
497 if ((ret = m9206_write(d->udev, 0x25, pid, (type << 8) | (idx * 4) )) != 0)
500 if ((ret = m9206_write(d->udev, 0x25, 0, (type << 8) | (idx * 4) )) != 0)
506 static int m9206_pid_filter_ctrl(struct dvb_usb_device *d, int onoff)
510 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
513 deb_rc("filtering %s\n", onoff ? "on" : "off");
515 if ((ret = set_filter(d, 0x81, 1, 0x00)) != 0)
518 if ((ret = set_filter(d, 0x82, 0, 0x02f5)) != 0)
522 mutex_unlock(&d->i2c_mutex);
527 static int m9206_pid_filter(struct dvb_usb_device *d, int index, u16 pid, int onoff)
532 return m9206_pid_filter_ctrl(d, !onoff);
534 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
537 deb_rc("filter %d, pid %x, %s\n", index, pid, onoff ? "on" : "off");
541 if ((ret = set_filter(d, 0x81, 1, 0x01)) != 0)
544 if ((ret = set_filter(d, 0x81, index + 2, pid)) != 0)
547 if ((ret = set_filter(d, 0x82, 0, 0x02f5)) != 0)
551 mutex_unlock(&d->i2c_mutex);
556 static int m9206_firmware_download(struct usb_device *udev, const struct firmware *fw)
558 u16 value, index, size;
560 int i, pass, ret = 0;
562 buff = kmalloc(65536, GFP_KERNEL);
564 if ((ret = m9206_read(udev, 0x25, 0x0, 0x8000, read, 4)) != 0)
566 deb_rc("%x %x %x %x\n", read[0], read[1], read[2], read[3]);
568 if ((ret = m9206_read(udev, 0x30, 0x0, 0x0, read, 1)) != 0)
570 deb_rc("%x\n", read[0]);
572 for (pass = 0; pass < 2; pass++) {
573 for (i = 0; i + (sizeof(u16) * 3) < fw->size;) {
574 value = le16_to_cpu(*(u16 *)(fw->data + i));
577 index = le16_to_cpu(*(u16 *)(fw->data + i));
580 size = le16_to_cpu(*(u16 *)(fw->data + i));
584 /* Will stall if using fw->data ... */
585 memcpy(buff, fw->data + i, size);
587 ret = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
588 0x30, USB_TYPE_VENDOR | USB_DIR_OUT,
589 value, index, buff, size, 20);
591 deb_rc("error while uploading fw!\n");
607 /* m9206 will disconnect itself from the bus after this. */
608 (void) m9206_write(udev, 0x22, 0x01, 0xff69);
609 deb_rc("firmware uploaded!\n");
617 static struct dvb_usb_properties megasky_properties = {
618 .caps = DVB_USB_IS_AN_I2C_ADAPTER | DVB_USB_HAS_PID_FILTER |
619 DVB_USB_PID_FILTER_CAN_BE_TURNED_OFF | DVB_USB_NEED_PID_FILTERING,
620 .pid_filter_count = 8,
622 .usb_ctrl = DEVICE_SPECIFIC,
623 .firmware = "dvb-usb-megasky-02.fw",
624 .download_firmware = m9206_firmware_download,
626 .pid_filter = m9206_pid_filter,
627 .pid_filter_ctrl = m9206_pid_filter_ctrl,
628 .frontend_attach = megasky_frontend_attach,
631 .rc_key_map = megasky_rc_keys,
632 .rc_key_map_size = ARRAY_SIZE(megasky_rc_keys),
633 .rc_query = m9206_rc_query,
637 .identify_state = megasky_identify_state,
638 .i2c_algo = &m9206_i2c_algo,
640 .generic_bulk_ctrl_endpoint = 0x01,
642 .type = DVB_USB_BULK,
651 .num_device_descs = 1,
653 { "MSI Mega Sky 580 DVB-T USB2.0",
654 { &megasky_table[0], NULL },
661 static struct usb_driver megasky_driver = {
662 .name = "dvb_usb_megasky",
663 .probe = megasky_probe,
664 .disconnect = dvb_usb_device_exit,
665 .id_table = megasky_table,
669 static int __init megasky_module_init(void)
673 if ((ret = usb_register(&megasky_driver))) {
674 err("usb_register failed. Error number %d", ret);
681 static void __exit megasky_module_exit(void)
683 /* deregister this driver from the USB subsystem */
684 usb_deregister(&megasky_driver);
687 module_init (megasky_module_init);
688 module_exit (megasky_module_exit);
690 MODULE_AUTHOR("Aapo Tahkola <aet@rasterburn.org>");
691 MODULE_DESCRIPTION("Driver for MSI Mega Sky 580 DVB-T USB2.0");
692 MODULE_VERSION("0.1");
693 MODULE_LICENSE("GPL");