ba5332676b4984fe0ae484ee3eded05165790a46
[cascardo/linux.git] / drivers / media / dvb / dvb-usb / m920x.c
1 /* DVB USB compliant linux driver for MSI Mega Sky 580 DVB-T USB2.0 receiver
2  *
3  * Copyright (C) 2006 Aapo Tahkola (aet@rasterburn.org)
4  *
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.
8  *
9  * see Documentation/dvb/README.dvb-usb for more information
10  */
11
12 #include "m920x.h"
13
14 #include "mt352.h"
15 #include "mt352_priv.h"
16 #include "qt1010.h"
17
18 /* debug */
19 static 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);
22
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" */
40 };
41
42 static inline int m9206_read(struct usb_device *udev, u8 request, u16 value, u16 index, void *data, int size)
43 {
44         int ret;
45
46         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
47                               request, USB_TYPE_VENDOR | USB_DIR_IN,
48                               value, index, data, size, 2000);
49         if (ret < 0)
50                 return ret;
51
52         if (ret != size)
53                 return -EIO;
54
55         return 0;
56 }
57
58 static inline int m9206_write(struct usb_device *udev, u8 request, u16 value, u16 index)
59 {
60         int ret;
61
62         ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
63                               request, USB_TYPE_VENDOR | USB_DIR_OUT,
64                               value, index, NULL, 0, 2000);
65         return ret;
66 }
67
68 static int m9206_rc_init(struct usb_device *udev)
69 {
70         int ret = 0;
71
72         /* Remote controller init. */
73         if ((ret = m9206_write(udev, M9206_CORE, 0xa8, M9206_RC_INIT2)) != 0)
74                 return ret;
75
76         if ((ret = m9206_write(udev, M9206_CORE, 0x51, M9206_RC_INIT1)) != 0)
77                 return ret;
78
79         return ret;
80 }
81
82 static int m9206_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
83 {
84         struct m9206_state *m = d->priv;
85         int i, ret = 0;
86         u8 rc_state[2];
87
88
89         if ((ret = m9206_read(d->udev, M9206_CORE, 0x0, M9206_RC_STATE, rc_state, 1)) != 0)
90                 goto unlock;
91
92         if ((ret = m9206_read(d->udev, M9206_CORE, 0x0, M9206_RC_KEY, rc_state + 1, 1)) != 0)
93                 goto unlock;
94
95         for (i = 0; i < ARRAY_SIZE(megasky_rc_keys); i++)
96                 if (megasky_rc_keys[i].data == rc_state[1]) {
97                         *event = megasky_rc_keys[i].event;
98
99                         switch(rc_state[0]) {
100                         case 0x80:
101                                 *state = REMOTE_NO_KEY_PRESSED;
102                                 goto unlock;
103
104                         case 0x93:
105                         case 0x92:
106                                 m->rep_count = 0;
107                                 *state = REMOTE_KEY_PRESSED;
108                                 goto unlock;
109
110                         case 0x91:
111                                 /* For comfort. */
112                                 if (++m->rep_count > 2)
113                                         *state = REMOTE_KEY_REPEAT;
114                                 goto unlock;
115
116                         default:
117                                 deb_rc("Unexpected rc response %x\n", rc_state[0]);
118                                 *state = REMOTE_NO_KEY_PRESSED;
119                                 goto unlock;
120                         }
121                 }
122
123         if (rc_state[1] != 0)
124                 deb_rc("Unknown rc key %x\n", rc_state[1]);
125
126         *state = REMOTE_NO_KEY_PRESSED;
127
128         unlock:
129
130         return ret;
131 }
132
133 /* I2C */
134
135 static int m9206_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
136 {
137         struct dvb_usb_device *d = i2c_get_adapdata(adap);
138         int i;
139         int ret = 0;
140
141         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
142                 return -EAGAIN;
143
144         if (num > 2)
145                 return -EINVAL;
146
147         for (i = 0; i < num; i++) {
148                 u8 w_len;
149
150                 if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].addr, 0x80)) != 0)
151                         goto unlock;
152
153                 if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].buf[0], 0x0)) != 0)
154                         goto unlock;
155
156                 if (i + 1 < num && msg[i + 1].flags & I2C_M_RD) {
157                         /* Possibly device dependant */
158                         if (msg[i].addr == d->adapter[0].pll_addr)
159                                 w_len = 0xc5;
160                         else
161                                 w_len = 0x1f;
162
163                         if ((ret = m9206_write(d->udev, M9206_I2C, w_len, 0x80)) != 0)
164                                 goto unlock;
165
166                         if ((ret = m9206_read(d->udev, M9206_I2C, 0x0, 0x60, msg[i + 1].buf, msg[i + 1].len)) != 0)
167                                 goto unlock;
168
169                         i++;
170                 } else {
171                         if (msg[i].len != 2)
172                                 return -EINVAL;
173
174                         if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].buf[1], 0x40)) != 0)
175                                 goto unlock;
176                 }
177         }
178         ret = i;
179         unlock:
180         mutex_unlock(&d->i2c_mutex);
181
182         return ret;
183 }
184
185 static u32 m9206_i2c_func(struct i2c_adapter *adapter)
186 {
187         return I2C_FUNC_I2C;
188 }
189
190 static struct i2c_algorithm m9206_i2c_algo = {
191         .master_xfer   = m9206_i2c_xfer,
192         .functionality = m9206_i2c_func,
193 };
194
195 /* Callbacks for DVB USB */
196 static int megasky_identify_state(struct usb_device *udev,
197                                   struct dvb_usb_device_properties *props,
198                                   struct dvb_usb_device_description **desc,
199                                   int *cold)
200 {
201         struct usb_host_interface *alt;
202
203         alt = usb_altnum_to_altsetting(usb_ifnum_to_if(udev, 0), 1);
204         *cold = (alt == NULL) ? 1 : 0;
205
206         return 0;
207 }
208
209 static int megasky_mt352_demod_init(struct dvb_frontend *fe)
210 {
211         u8 config[] = { CONFIG, 0x3d };
212         u8 clock[] = { CLOCK_CTL, 0x30 };
213         u8 reset[] = { RESET, 0x80 };
214         u8 adc_ctl[] = { ADC_CTL_1, 0x40 };
215         u8 agc[] = { AGC_TARGET, 0x1c, 0x20 };
216         u8 sec_agc[] = { 0x69, 0x00, 0xff, 0xff, 0x40, 0xff, 0x00, 0x40, 0x40 };
217         u8 unk1[] = { 0x93, 0x1a };
218         u8 unk2[] = { 0xb5, 0x7a };
219
220         mt352_write(fe, config, ARRAY_SIZE(config));
221         mt352_write(fe, clock, ARRAY_SIZE(clock));
222         mt352_write(fe, reset, ARRAY_SIZE(reset));
223         mt352_write(fe, adc_ctl, ARRAY_SIZE(adc_ctl));
224         mt352_write(fe, agc, ARRAY_SIZE(agc));
225         mt352_write(fe, sec_agc, ARRAY_SIZE(sec_agc));
226         mt352_write(fe, unk1, ARRAY_SIZE(unk1));
227         mt352_write(fe, unk2, ARRAY_SIZE(unk2));
228
229         deb_rc("Demod init!\n");
230
231         return 0;
232 }
233
234 static struct mt352_config megasky_mt352_config = {
235         .demod_address = 0x1e,
236         .no_tuner = 1,
237         .demod_init = megasky_mt352_demod_init,
238 };
239
240 static int megasky_frontend_attach(struct dvb_usb_adapter *adap)
241 {
242         deb_rc("megasky_frontend_attach!\n");
243
244         if ((adap->fe = dvb_attach(mt352_attach, &megasky_mt352_config, &adap->dev->i2c_adap)) != NULL)
245                 return 0;
246         return -EIO;
247 }
248
249 static int m9206_set_filter(struct dvb_usb_adapter *adap, int type, int idx, int pid)
250 {
251         int ret = 0;
252
253         if (pid >= 0x8000)
254                 return -EINVAL;
255
256         pid |= 0x8000;
257
258         if ((ret = m9206_write(adap->dev->udev, M9206_FILTER, pid, (type << 8) | (idx * 4) )) != 0)
259                 return ret;
260
261         if ((ret = m9206_write(adap->dev->udev, M9206_FILTER, 0, (type << 8) | (idx * 4) )) != 0)
262                 return ret;
263
264         return ret;
265 }
266
267 static int m9206_update_filters(struct dvb_usb_adapter *adap)
268 {
269         struct m9206_state *m = adap->dev->priv;
270         int enabled = m->filtering_enabled;
271         int i, ret = 0, filter = 0;
272
273         for (i = 0; i < M9206_MAX_FILTERS; i++)
274                 if (m->filters[i] == 8192)
275                         enabled = 0;
276
277         /* Disable all filters */
278         if ((ret = m9206_set_filter(adap, 0x81, 1, enabled)) != 0)
279                 return ret;
280
281         for (i = 0; i < M9206_MAX_FILTERS; i++)
282                 if ((ret = m9206_set_filter(adap, 0x81, i + 2, 0)) != 0)
283                         return ret;
284
285         if ((ret = m9206_set_filter(adap, 0x82, 0, 0x0)) != 0)
286                 return ret;
287
288         /* Set */
289         if (enabled) {
290                 for (i = 0; i < M9206_MAX_FILTERS; i++) {
291                         if (m->filters[i] == 0)
292                                 continue;
293
294                         if ((ret = m9206_set_filter(adap, 0x81, filter + 2, m->filters[i])) != 0)
295                                 return ret;
296
297                         filter++;
298                 }
299         }
300
301         if ((ret = m9206_set_filter(adap, 0x82, 0, 0x02f5)) != 0)
302                 return ret;
303
304         return ret;
305 }
306
307 static int m9206_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
308 {
309         struct m9206_state *m = adap->dev->priv;
310
311         m->filtering_enabled = onoff ? 1 : 0;
312
313         return m9206_update_filters(adap);
314 }
315
316 static int m9206_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
317 {
318         struct m9206_state *m = adap->dev->priv;
319
320         m->filters[index] = onoff ? pid : 0;
321
322         return m9206_update_filters(adap);
323 }
324
325 static int m9206_firmware_download(struct usb_device *udev, const struct firmware *fw)
326 {
327         u16 value, index, size;
328         u8 read[4], *buff;
329         int i, pass, ret = 0;
330
331         buff = kmalloc(65536, GFP_KERNEL);
332
333         if ((ret = m9206_read(udev, M9206_FILTER, 0x0, 0x8000, read, 4)) != 0)
334                 goto done;
335         deb_rc("%x %x %x %x\n", read[0], read[1], read[2], read[3]);
336
337         if ((ret = m9206_read(udev, M9206_FW, 0x0, 0x0, read, 1)) != 0)
338                 goto done;
339         deb_rc("%x\n", read[0]);
340
341         for (pass = 0; pass < 2; pass++) {
342                 for (i = 0; i + (sizeof(u16) * 3) < fw->size;) {
343                         value = le16_to_cpu(*(u16 *)(fw->data + i));
344                         i += sizeof(u16);
345
346                         index = le16_to_cpu(*(u16 *)(fw->data + i));
347                         i += sizeof(u16);
348
349                         size = le16_to_cpu(*(u16 *)(fw->data + i));
350                         i += sizeof(u16);
351
352                         if (pass == 1) {
353                                 /* Will stall if using fw->data ... */
354                                 memcpy(buff, fw->data + i, size);
355
356                                 ret = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
357                                             M9206_FW,
358                                             USB_TYPE_VENDOR | USB_DIR_OUT,
359                                             value, index, buff, size, 20);
360                                 if (ret != size) {
361                                         deb_rc("error while uploading fw!\n");
362                                         ret = -EIO;
363                                         goto done;
364                                 }
365                                 msleep(3);
366                         }
367                         i += size;
368                 }
369                 if (i != fw->size) {
370                         ret = -EINVAL;
371                         goto done;
372                 }
373         }
374
375         msleep(36);
376
377         /* m9206 will disconnect itself from the bus after this. */
378         (void) m9206_write(udev, M9206_CORE, 0x01, M9206_FW_GO);
379         deb_rc("firmware uploaded!\n");
380
381         done:
382         kfree(buff);
383
384         return ret;
385 }
386
387 /* DVB USB Driver stuff */
388 static struct dvb_usb_device_properties megasky_properties;
389
390 static int m920x_probe(struct usb_interface *intf, const struct usb_device_id *id)
391 {
392         struct dvb_usb_device *d;
393         struct usb_host_interface *alt;
394         int ret;
395
396         if ((ret = dvb_usb_device_init(intf, &megasky_properties, THIS_MODULE, &d)) == 0) {
397                 deb_rc("probed!\n");
398
399                 alt = usb_altnum_to_altsetting(intf, 1);
400                 if (alt == NULL) {
401                         deb_rc("not alt found!\n");
402                         return -ENODEV;
403                 }
404
405                 ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber, alt->desc.bAlternateSetting);
406                 if (ret < 0)
407                         return ret;
408
409                 deb_rc("Changed to alternate setting!\n");
410
411                 if ((ret = m9206_rc_init(d->udev)) != 0)
412                         return ret;
413         }
414         return ret;
415 }
416
417 static struct usb_device_id m920x_table [] = {
418                 { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580) },
419                 { }             /* Terminating entry */
420 };
421 MODULE_DEVICE_TABLE (usb, m920x_table);
422
423 static struct dvb_usb_device_properties megasky_properties = {
424         .usb_ctrl = DEVICE_SPECIFIC,
425         .firmware = "dvb-usb-megasky-02.fw",
426         .download_firmware = m9206_firmware_download,
427
428         .rc_interval      = 100,
429         .rc_key_map       = megasky_rc_keys,
430         .rc_key_map_size  = ARRAY_SIZE(megasky_rc_keys),
431         .rc_query         = m9206_rc_query,
432
433         .size_of_priv     = sizeof(struct m9206_state),
434
435         .identify_state   = megasky_identify_state,
436         .num_adapters = 1,
437         .adapter = {{
438                 .caps = DVB_USB_IS_AN_I2C_ADAPTER | DVB_USB_ADAP_HAS_PID_FILTER |
439                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
440
441                 .pid_filter_count = 8,
442                 .pid_filter       = m9206_pid_filter,
443                 .pid_filter_ctrl  = m9206_pid_filter_ctrl,
444
445                 .frontend_attach  = megasky_frontend_attach,
446                 .tuner_attach     = qt1010_tuner_attach,
447
448                 .stream = {
449                         .type = USB_BULK,
450                         .count = 8,
451                         .endpoint = 0x81,
452                         .u = {
453                                 .bulk = {
454                                         .buffersize = 512,
455                                 }
456                         }
457                 },
458         }},
459         .i2c_algo         = &m9206_i2c_algo,
460
461         .num_device_descs = 1,
462         .devices = {
463                 {   "MSI Mega Sky 580 DVB-T USB2.0",
464                         { &m920x_table[0], NULL },
465                         { NULL },
466                 },
467                 { NULL },
468         }
469 };
470
471 static struct usb_driver m920x_driver = {
472         .name           = "dvb_usb_m920x",
473         .probe          = m920x_probe,
474         .disconnect     = dvb_usb_device_exit,
475         .id_table       = m920x_table,
476 };
477
478 /* module stuff */
479 static int __init m920x_module_init(void)
480 {
481         int ret;
482
483         if ((ret = usb_register(&m920x_driver))) {
484                 err("usb_register failed. Error number %d", ret);
485                 return ret;
486         }
487
488         return 0;
489 }
490
491 static void __exit m920x_module_exit(void)
492 {
493         /* deregister this driver from the USB subsystem */
494         usb_deregister(&m920x_driver);
495 }
496
497 module_init (m920x_module_init);
498 module_exit (m920x_module_exit);
499
500 MODULE_AUTHOR("Aapo Tahkola <aet@rasterburn.org>");
501 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / Uli m920x");
502 MODULE_VERSION("0.1");
503 MODULE_LICENSE("GPL");