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"
19 int dvb_usb_m920x_debug;
20 module_param_named(debug,dvb_usb_m920x_debug, int, 0644);
21 MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
23 static struct dvb_usb_rc_key megasky_rc_keys [] = {
24 { 0x0, 0x12, KEY_POWER },
25 { 0x0, 0x1e, KEY_CYCLEWINDOWS }, /* min/max */
26 { 0x0, 0x02, KEY_CHANNELUP },
27 { 0x0, 0x05, KEY_CHANNELDOWN },
28 { 0x0, 0x03, KEY_VOLUMEUP },
29 { 0x0, 0x06, KEY_VOLUMEDOWN },
30 { 0x0, 0x04, KEY_MUTE },
31 { 0x0, 0x07, KEY_OK }, /* TS */
32 { 0x0, 0x08, KEY_STOP },
33 { 0x0, 0x09, KEY_MENU }, /* swap */
34 { 0x0, 0x0a, KEY_REWIND },
35 { 0x0, 0x1b, KEY_PAUSE },
36 { 0x0, 0x1f, KEY_FASTFORWARD },
37 { 0x0, 0x0c, KEY_RECORD },
38 { 0x0, 0x0d, KEY_CAMERA }, /* screenshot */
39 { 0x0, 0x0e, KEY_COFFEE }, /* "MTS" */
42 static inline int m9206_read(struct usb_device *udev, u8 request, u16 value, u16 index, void *data, int size)
46 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
47 request, USB_TYPE_VENDOR | USB_DIR_IN,
48 value, index, data, size, 2000);
58 static inline int m9206_write(struct usb_device *udev, u8 request, u16 value, u16 index)
62 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
63 request, USB_TYPE_VENDOR | USB_DIR_OUT,
64 value, index, NULL, 0, 2000);
70 static int m9206_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
75 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
78 if ((ret = m9206_read(d->udev, 0x22, 0x0, 0xff51, rc_state, 1)) != 0)
81 if ((ret = m9206_read(d->udev, 0x22, 0x0, 0xff52, rc_state + 1, 1)) != 0)
84 for (i = 0; i < ARRAY_SIZE(megasky_rc_keys); i++)
85 if (megasky_rc_keys[i].data == rc_state[1]) {
86 *event = megasky_rc_keys[i].event;
90 *state = REMOTE_NO_KEY_PRESSED;
95 *state = REMOTE_KEY_PRESSED;
99 *state = REMOTE_KEY_REPEAT;
103 deb_rc("Unexpected rc response %x\n", rc_state[0]);
104 *state = REMOTE_NO_KEY_PRESSED;
109 if (rc_state[1] != 0)
110 deb_rc("Unknown rc key %x\n", rc_state[1]);
112 *state = REMOTE_NO_KEY_PRESSED;
115 mutex_unlock(&d->i2c_mutex);
122 static int m9206_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
124 struct dvb_usb_device *d = i2c_get_adapdata(adap);
128 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
134 for (i = 0; i < num; i++) {
137 if ((ret = m9206_write(d->udev, 0x23, msg[i].addr, 0x80)) != 0)
140 if ((ret = m9206_write(d->udev, 0x23, msg[i].buf[0], 0x0)) != 0)
143 if (i + 1 < num && msg[i + 1].flags & I2C_M_RD) {
144 if (msg[i].addr == 0x1e)
149 if ((ret = m9206_write(d->udev, 0x23, w_len, 0x80)) != 0)
152 if ((ret = m9206_read(d->udev, 0x23, 0x0, 0x60, msg[i + 1].buf, msg[i + 1].len)) != 0)
160 if ((ret = m9206_write(d->udev, 0x23, msg[i].buf[1], 0x40)) != 0)
166 mutex_unlock(&d->i2c_mutex);
171 static u32 m9206_i2c_func(struct i2c_adapter *adapter)
176 static struct i2c_algorithm m9206_i2c_algo = {
177 .master_xfer = m9206_i2c_xfer,
178 .functionality = m9206_i2c_func,
181 /* Callbacks for DVB USB */
182 static int megasky_identify_state(struct usb_device *udev,
183 struct dvb_usb_device_properties *props,
184 struct dvb_usb_device_description **desc,
187 struct usb_host_interface *alt;
189 alt = usb_altnum_to_altsetting(usb_ifnum_to_if(udev, 0), 1);
190 *cold = (alt == NULL) ? 1 : 0;
195 static int megasky_mt352_demod_init(struct dvb_frontend *fe)
220 for (i = 0; i < ARRAY_SIZE(buf1); i += 2)
221 mt352_write(fe, &buf1[i], 2);
223 deb_rc("Demod init!\n");
230 static struct mt352_config megasky_mt352_config = {
231 .demod_address = 0x1e,
232 .demod_init = megasky_mt352_demod_init,
235 static int megasky_frontend_attach(struct dvb_usb_adapter *adap)
237 deb_rc("megasky_frontend_attach!\n");
239 if ((adap->fe = dvb_attach(mt352_attach, &megasky_mt352_config, &adap->dev->i2c_adap)) != NULL) {
240 adap->fe->ops.tuner_ops.calc_regs = qt1010_set_params;
246 /* DVB USB Driver stuff */
247 static struct dvb_usb_device_properties megasky_properties;
249 static int m920x_probe(struct usb_interface *intf, const struct usb_device_id *id)
251 struct dvb_usb_device *d;
252 struct usb_host_interface *alt;
255 if ((ret = dvb_usb_device_init(intf, &megasky_properties, THIS_MODULE, &d)) == 0) {
258 alt = usb_altnum_to_altsetting(intf, 1);
260 deb_rc("not alt found!\n");
264 ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber, alt->desc.bAlternateSetting);
268 deb_rc("Changed to alternate setting!\n");
270 /* Remote controller init. */
271 if ((ret = m9206_write(d->udev, 0x22, 0xa8, 0xff55)) != 0)
274 if ((ret = m9206_write(d->udev, 0x22, 0x51, 0xff54)) != 0)
280 static struct usb_device_id m920x_table [] = {
281 { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580) },
282 { } /* Terminating entry */
284 MODULE_DEVICE_TABLE (usb, m920x_table);
286 static int set_filter(struct dvb_usb_adapter *adap, int type, int idx, int pid)
295 if ((ret = m9206_write(adap->dev->udev, 0x25, pid, (type << 8) | (idx * 4) )) != 0)
298 if ((ret = m9206_write(adap->dev->udev, 0x25, 0, (type << 8) | (idx * 4) )) != 0)
304 static int m9206_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
308 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
311 deb_rc("filtering %s\n", onoff ? "on" : "off");
313 if ((ret = set_filter(adap, 0x81, 1, 0x00)) != 0)
316 if ((ret = set_filter(adap, 0x82, 0, 0x02f5)) != 0)
320 mutex_unlock(&adap->dev->i2c_mutex);
325 static int m9206_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
330 return m9206_pid_filter_ctrl(adap, !onoff);
332 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
335 deb_rc("filter %d, pid %x, %s\n", index, pid, onoff ? "on" : "off");
339 if ((ret = set_filter(adap, 0x81, 1, 0x01)) != 0)
342 if ((ret = set_filter(adap, 0x81, index + 2, pid)) != 0)
345 if ((ret = set_filter(adap, 0x82, 0, 0x02f5)) != 0)
349 mutex_unlock(&adap->dev->i2c_mutex);
354 static int m9206_firmware_download(struct usb_device *udev, const struct firmware *fw)
356 u16 value, index, size;
358 int i, pass, ret = 0;
360 buff = kmalloc(65536, GFP_KERNEL);
362 if ((ret = m9206_read(udev, 0x25, 0x0, 0x8000, read, 4)) != 0)
364 deb_rc("%x %x %x %x\n", read[0], read[1], read[2], read[3]);
366 if ((ret = m9206_read(udev, 0x30, 0x0, 0x0, read, 1)) != 0)
368 deb_rc("%x\n", read[0]);
370 for (pass = 0; pass < 2; pass++) {
371 for (i = 0; i + (sizeof(u16) * 3) < fw->size;) {
372 value = le16_to_cpu(*(u16 *)(fw->data + i));
375 index = le16_to_cpu(*(u16 *)(fw->data + i));
378 size = le16_to_cpu(*(u16 *)(fw->data + i));
382 /* Will stall if using fw->data ... */
383 memcpy(buff, fw->data + i, size);
385 ret = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
386 0x30, USB_TYPE_VENDOR | USB_DIR_OUT,
387 value, index, buff, size, 20);
389 deb_rc("error while uploading fw!\n");
405 /* m9206 will disconnect itself from the bus after this. */
406 (void) m9206_write(udev, 0x22, 0x01, 0xff69);
407 deb_rc("firmware uploaded!\n");
415 static struct dvb_usb_device_properties megasky_properties = {
416 .usb_ctrl = DEVICE_SPECIFIC,
417 .firmware = "dvb-usb-megasky-02.fw",
418 .download_firmware = m9206_firmware_download,
421 .rc_key_map = megasky_rc_keys,
422 .rc_key_map_size = ARRAY_SIZE(megasky_rc_keys),
423 .rc_query = m9206_rc_query,
427 .identify_state = megasky_identify_state,
430 .caps = DVB_USB_IS_AN_I2C_ADAPTER | DVB_USB_ADAP_HAS_PID_FILTER |
431 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF |
432 DVB_USB_ADAP_NEED_PID_FILTERING,
433 .pid_filter_count = 8,
434 .pid_filter = m9206_pid_filter,
435 .pid_filter_ctrl = m9206_pid_filter_ctrl,
437 .frontend_attach = megasky_frontend_attach,
450 .i2c_algo = &m9206_i2c_algo,
452 .generic_bulk_ctrl_endpoint = 0x01,
454 .num_device_descs = 1,
456 { "MSI Mega Sky 580 DVB-T USB2.0",
457 { &m920x_table[0], NULL },
464 static struct usb_driver m920x_driver = {
465 .name = "dvb_usb_m920x",
466 .probe = m920x_probe,
467 .disconnect = dvb_usb_device_exit,
468 .id_table = m920x_table,
472 static int __init m920x_module_init(void)
476 if ((ret = usb_register(&m920x_driver))) {
477 err("usb_register failed. Error number %d", ret);
484 static void __exit m920x_module_exit(void)
486 /* deregister this driver from the USB subsystem */
487 usb_deregister(&m920x_driver);
490 module_init (m920x_module_init);
491 module_exit (m920x_module_exit);
493 MODULE_AUTHOR("Aapo Tahkola <aet@rasterburn.org>");
494 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / Uli m920x");
495 MODULE_VERSION("0.1");
496 MODULE_LICENSE("GPL");