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
15 #include "mt352_priv.h"
18 int dvb_usb_m920x_debug;
19 module_param_named(debug,dvb_usb_m920x_debug, int, 0644);
20 MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
22 static struct dvb_usb_rc_key megasky_rc_keys [] = {
23 { 0x0, 0x12, KEY_POWER },
24 { 0x0, 0x1e, KEY_CYCLEWINDOWS }, /* min/max */
25 { 0x0, 0x02, KEY_CHANNELUP },
26 { 0x0, 0x05, KEY_CHANNELDOWN },
27 { 0x0, 0x03, KEY_VOLUMEUP },
28 { 0x0, 0x06, KEY_VOLUMEDOWN },
29 { 0x0, 0x04, KEY_MUTE },
30 { 0x0, 0x07, KEY_OK }, /* TS */
31 { 0x0, 0x08, KEY_STOP },
32 { 0x0, 0x09, KEY_MENU }, /* swap */
33 { 0x0, 0x0a, KEY_REWIND },
34 { 0x0, 0x1b, KEY_PAUSE },
35 { 0x0, 0x1f, KEY_FASTFORWARD },
36 { 0x0, 0x0c, KEY_RECORD },
37 { 0x0, 0x0d, KEY_CAMERA }, /* screenshot */
38 { 0x0, 0x0e, KEY_COFFEE }, /* "MTS" */
41 static inline int m9206_read(struct usb_device *udev, u8 request, u16 value, u16 index, void *data, int size)
45 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
46 request, USB_TYPE_VENDOR | USB_DIR_IN,
47 value, index, data, size, 2000);
57 static inline int m9206_write(struct usb_device *udev, u8 request, u16 value, u16 index)
61 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
62 request, USB_TYPE_VENDOR | USB_DIR_OUT,
63 value, index, NULL, 0, 2000);
69 static int m9206_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
74 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
77 if ((ret = m9206_read(d->udev, 0x22, 0x0, 0xff51, rc_state, 1)) != 0)
80 if ((ret = m9206_read(d->udev, 0x22, 0x0, 0xff52, rc_state + 1, 1)) != 0)
83 for (i = 0; i < ARRAY_SIZE(megasky_rc_keys); i++)
84 if (megasky_rc_keys[i].data == rc_state[1]) {
85 *event = megasky_rc_keys[i].event;
89 *state = REMOTE_NO_KEY_PRESSED;
94 *state = REMOTE_KEY_PRESSED;
98 *state = REMOTE_KEY_REPEAT;
102 deb_rc("Unexpected rc response %x\n", rc_state[0]);
103 *state = REMOTE_NO_KEY_PRESSED;
108 if (rc_state[1] != 0)
109 deb_rc("Unknown rc key %x\n", rc_state[1]);
111 *state = REMOTE_NO_KEY_PRESSED;
114 mutex_unlock(&d->i2c_mutex);
121 static int m9206_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
123 struct dvb_usb_device *d = i2c_get_adapdata(adap);
127 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
133 for (i = 0; i < num; i++) {
136 if ((ret = m9206_write(d->udev, 0x23, msg[i].addr, 0x80)) != 0)
139 if ((ret = m9206_write(d->udev, 0x23, msg[i].buf[0], 0x0)) != 0)
142 if (i + 1 < num && msg[i + 1].flags & I2C_M_RD) {
143 if (msg[i].addr == 0x1e)
148 if ((ret = m9206_write(d->udev, 0x23, w_len, 0x80)) != 0)
151 if ((ret = m9206_read(d->udev, 0x23, 0x0, 0x60, msg[i + 1].buf, msg[i + 1].len)) != 0)
159 if ((ret = m9206_write(d->udev, 0x23, msg[i].buf[1], 0x40)) != 0)
165 mutex_unlock(&d->i2c_mutex);
170 static u32 m9206_i2c_func(struct i2c_adapter *adapter)
175 static struct i2c_algorithm m9206_i2c_algo = {
176 .master_xfer = m9206_i2c_xfer,
177 .functionality = m9206_i2c_func,
180 /* Callbacks for DVB USB */
181 static int megasky_identify_state (struct usb_device *udev,
182 struct dvb_usb_device_properties *props,
183 struct dvb_usb_device_description **desc,
186 struct usb_host_interface *alt;
188 alt = usb_altnum_to_altsetting(usb_ifnum_to_if(udev, 0), 1);
189 *cold = (alt == NULL) ? 1 : 0;
194 static int megasky_mt352_demod_init(struct dvb_frontend *fe)
219 for (i = 0; i < ARRAY_SIZE(buf1); i += 2)
220 mt352_write(fe, &buf1[i], 2);
222 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_adapter *adap)
439 deb_rc("megasky_frontend_attach!\n");
441 if ((adap->fe = dvb_attach(mt352_attach, &megasky_mt352_config, &adap->dev->i2c_adap)) != NULL) {
442 adap->fe->ops.tuner_ops.calc_regs = qt1010_set_params;
448 /* DVB USB Driver stuff */
449 static struct dvb_usb_device_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 m920x_table [] = {
483 { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580) },
484 { } /* Terminating entry */
486 MODULE_DEVICE_TABLE (usb, m920x_table);
488 static int set_filter(struct dvb_usb_adapter *adap, int type, int idx, int pid)
497 if ((ret = m9206_write(adap->dev->udev, 0x25, pid, (type << 8) | (idx * 4) )) != 0)
500 if ((ret = m9206_write(adap->dev->udev, 0x25, 0, (type << 8) | (idx * 4) )) != 0)
506 static int m9206_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
510 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
513 deb_rc("filtering %s\n", onoff ? "on" : "off");
515 if ((ret = set_filter(adap, 0x81, 1, 0x00)) != 0)
518 if ((ret = set_filter(adap, 0x82, 0, 0x02f5)) != 0)
522 mutex_unlock(&adap->dev->i2c_mutex);
527 static int m9206_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
532 return m9206_pid_filter_ctrl(adap, !onoff);
534 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
537 deb_rc("filter %d, pid %x, %s\n", index, pid, onoff ? "on" : "off");
541 if ((ret = set_filter(adap, 0x81, 1, 0x01)) != 0)
544 if ((ret = set_filter(adap, 0x81, index + 2, pid)) != 0)
547 if ((ret = set_filter(adap, 0x82, 0, 0x02f5)) != 0)
551 mutex_unlock(&adap->dev->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_device_properties megasky_properties = {
618 .usb_ctrl = DEVICE_SPECIFIC,
619 .firmware = "dvb-usb-megasky-02.fw",
620 .download_firmware = m9206_firmware_download,
623 .rc_key_map = megasky_rc_keys,
624 .rc_key_map_size = ARRAY_SIZE(megasky_rc_keys),
625 .rc_query = m9206_rc_query,
629 .identify_state = megasky_identify_state,
632 .caps = DVB_USB_IS_AN_I2C_ADAPTER | DVB_USB_ADAP_HAS_PID_FILTER |
633 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF |
634 DVB_USB_ADAP_NEED_PID_FILTERING,
635 .pid_filter_count = 8,
636 .pid_filter = m9206_pid_filter,
637 .pid_filter_ctrl = m9206_pid_filter_ctrl,
639 .frontend_attach = megasky_frontend_attach,
652 .i2c_algo = &m9206_i2c_algo,
654 .generic_bulk_ctrl_endpoint = 0x01,
656 .num_device_descs = 1,
658 { "MSI Mega Sky 580 DVB-T USB2.0",
659 { &m920x_table[0], NULL },
666 static struct usb_driver m920x_driver = {
667 .name = "dvb_usb_m920x",
668 .probe = megasky_probe,
669 .disconnect = dvb_usb_device_exit,
670 .id_table = m920x_table,
674 static int __init m920x_module_init(void)
678 if ((ret = usb_register(&m920x_driver))) {
679 err("usb_register failed. Error number %d", ret);
686 static void __exit m920x_module_exit(void)
688 /* deregister this driver from the USB subsystem */
689 usb_deregister(&m920x_driver);
692 module_init (m920x_module_init);
693 module_exit (m920x_module_exit);
695 MODULE_AUTHOR("Aapo Tahkola <aet@rasterburn.org>");
696 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / Uli m920x");
697 MODULE_VERSION("0.1");
698 MODULE_LICENSE("GPL");