Merge git://git.infradead.org/mtd-2.6
[pandora-kernel.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
7  *      Free 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 #include "tda1004x.h"
18 #include "tda827x.h"
19
20 /* debug */
21 static int dvb_usb_m920x_debug;
22 module_param_named(debug,dvb_usb_m920x_debug, int, 0644);
23 MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
24
25 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
26
27 static int m920x_set_filter(struct dvb_usb_device *d, int type, int idx, int pid);
28
29 static inline int m920x_read(struct usb_device *udev, u8 request, u16 value,
30                              u16 index, void *data, int size)
31 {
32         int ret;
33
34         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
35                               request, USB_TYPE_VENDOR | USB_DIR_IN,
36                               value, index, data, size, 2000);
37         if (ret < 0) {
38                 printk(KERN_INFO "m920x_read = error: %d\n", ret);
39                 return ret;
40         }
41
42         if (ret != size) {
43                 deb("m920x_read = no data\n");
44                 return -EIO;
45         }
46
47         return 0;
48 }
49
50 static inline int m920x_write(struct usb_device *udev, u8 request,
51                               u16 value, u16 index)
52 {
53         int ret;
54
55         ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
56                               request, USB_TYPE_VENDOR | USB_DIR_OUT,
57                               value, index, NULL, 0, 2000);
58
59         return ret;
60 }
61
62 static int m920x_init(struct dvb_usb_device *d, struct m920x_inits *rc_seq)
63 {
64         int ret = 0, i, epi, flags = 0;
65         int adap_enabled[M9206_MAX_ADAPTERS] = { 0 };
66
67         /* Remote controller init. */
68         if (d->props.rc_query) {
69                 deb("Initialising remote control\n");
70                 while (rc_seq->address) {
71                         if ((ret = m920x_write(d->udev, M9206_CORE,
72                                                rc_seq->data,
73                                                rc_seq->address)) != 0) {
74                                 deb("Initialising remote control failed\n");
75                                 return ret;
76                         }
77
78                         rc_seq++;
79                 }
80
81                 deb("Initialising remote control success\n");
82         }
83
84         for (i = 0; i < d->props.num_adapters; i++)
85                 flags |= d->adapter[i].props.caps;
86
87         /* Some devices(Dposh) might crash if we attempt touch at all. */
88         if (flags & DVB_USB_ADAP_HAS_PID_FILTER) {
89                 for (i = 0; i < d->props.num_adapters; i++) {
90                         epi = d->adapter[i].props.stream.endpoint - 0x81;
91
92                         if (epi < 0 || epi >= M9206_MAX_ADAPTERS) {
93                                 printk(KERN_INFO "m920x: Unexpected adapter endpoint!\n");
94                                 return -EINVAL;
95                         }
96
97                         adap_enabled[epi] = 1;
98                 }
99
100                 for (i = 0; i < M9206_MAX_ADAPTERS; i++) {
101                         if (adap_enabled[i])
102                                 continue;
103
104                         if ((ret = m920x_set_filter(d, 0x81 + i, 0, 0x0)) != 0)
105                                 return ret;
106
107                         if ((ret = m920x_set_filter(d, 0x81 + i, 0, 0x02f5)) != 0)
108                                 return ret;
109                 }
110         }
111
112         return ret;
113 }
114
115 static int m920x_init_ep(struct usb_interface *intf)
116 {
117         struct usb_device *udev = interface_to_usbdev(intf);
118         struct usb_host_interface *alt;
119
120         if ((alt = usb_altnum_to_altsetting(intf, 1)) == NULL) {
121                 deb("No alt found!\n");
122                 return -ENODEV;
123         }
124
125         return usb_set_interface(udev, alt->desc.bInterfaceNumber,
126                                  alt->desc.bAlternateSetting);
127 }
128
129 static int m920x_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
130 {
131         struct m920x_state *m = d->priv;
132         int i, ret = 0;
133         u8 rc_state[2];
134
135         if ((ret = m920x_read(d->udev, M9206_CORE, 0x0, M9206_RC_STATE, rc_state, 1)) != 0)
136                 goto unlock;
137
138         if ((ret = m920x_read(d->udev, M9206_CORE, 0x0, M9206_RC_KEY, rc_state + 1, 1)) != 0)
139                 goto unlock;
140
141         for (i = 0; i < d->props.rc_key_map_size; i++)
142                 if (d->props.rc_key_map[i].data == rc_state[1]) {
143                         *event = d->props.rc_key_map[i].event;
144
145                         switch(rc_state[0]) {
146                         case 0x80:
147                                 *state = REMOTE_NO_KEY_PRESSED;
148                                 goto unlock;
149
150                         case 0x88: /* framing error or "invalid code" */
151                         case 0x99:
152                         case 0xc0:
153                         case 0xd8:
154                                 *state = REMOTE_NO_KEY_PRESSED;
155                                 m->rep_count = 0;
156                                 goto unlock;
157
158                         case 0x93:
159                         case 0x92:
160                                 m->rep_count = 0;
161                                 *state = REMOTE_KEY_PRESSED;
162                                 goto unlock;
163
164                         case 0x91:
165                                 /* prevent immediate auto-repeat */
166                                 if (++m->rep_count > 2)
167                                         *state = REMOTE_KEY_REPEAT;
168                                 else
169                                         *state = REMOTE_NO_KEY_PRESSED;
170                                 goto unlock;
171
172                         default:
173                                 deb("Unexpected rc state %02x\n", rc_state[0]);
174                                 *state = REMOTE_NO_KEY_PRESSED;
175                                 goto unlock;
176                         }
177                 }
178
179         if (rc_state[1] != 0)
180                 deb("Unknown rc key %02x\n", rc_state[1]);
181
182         *state = REMOTE_NO_KEY_PRESSED;
183
184  unlock:
185
186         return ret;
187 }
188
189 /* I2C */
190 static int m920x_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
191 {
192         struct dvb_usb_device *d = i2c_get_adapdata(adap);
193         int i, j;
194         int ret = 0;
195
196         if (!num)
197                 return -EINVAL;
198
199         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
200                 return -EAGAIN;
201
202         for (i = 0; i < num; i++) {
203                 if (msg[i].flags & (I2C_M_NO_RD_ACK | I2C_M_IGNORE_NAK | I2C_M_TEN) || msg[i].len == 0) {
204                         /* For a 0 byte message, I think sending the address
205                          * to index 0x80|0x40 would be the correct thing to
206                          * do.  However, zero byte messages are only used for
207                          * probing, and since we don't know how to get the
208                          * slave's ack, we can't probe. */
209                         ret = -ENOTSUPP;
210                         goto unlock;
211                 }
212                 /* Send START & address/RW bit */
213                 if (!(msg[i].flags & I2C_M_NOSTART)) {
214                         if ((ret = m920x_write(d->udev, M9206_I2C,
215                                         (msg[i].addr << 1) |
216                                         (msg[i].flags & I2C_M_RD ? 0x01 : 0), 0x80)) != 0)
217                                 goto unlock;
218                         /* Should check for ack here, if we knew how. */
219                 }
220                 if (msg[i].flags & I2C_M_RD) {
221                         for (j = 0; j < msg[i].len; j++) {
222                                 /* Last byte of transaction?
223                                  * Send STOP, otherwise send ACK. */
224                                 int stop = (i+1 == num && j+1 == msg[i].len) ? 0x40 : 0x01;
225
226                                 if ((ret = m920x_read(d->udev, M9206_I2C, 0x0,
227                                                       0x20 | stop,
228                                                       &msg[i].buf[j], 1)) != 0)
229                                         goto unlock;
230                         }
231                 } else {
232                         for (j = 0; j < msg[i].len; j++) {
233                                 /* Last byte of transaction? Then send STOP. */
234                                 int stop = (i+1 == num && j+1 == msg[i].len) ? 0x40 : 0x00;
235
236                                 if ((ret = m920x_write(d->udev, M9206_I2C, msg[i].buf[j], stop)) != 0)
237                                         goto unlock;
238                                 /* Should check for ack here too. */
239                         }
240                 }
241         }
242         ret = num;
243
244  unlock:
245         mutex_unlock(&d->i2c_mutex);
246
247         return ret;
248 }
249
250 static u32 m920x_i2c_func(struct i2c_adapter *adapter)
251 {
252         return I2C_FUNC_I2C;
253 }
254
255 static struct i2c_algorithm m920x_i2c_algo = {
256         .master_xfer   = m920x_i2c_xfer,
257         .functionality = m920x_i2c_func,
258 };
259
260 /* pid filter */
261 static int m920x_set_filter(struct dvb_usb_device *d, int type, int idx, int pid)
262 {
263         int ret = 0;
264
265         if (pid >= 0x8000)
266                 return -EINVAL;
267
268         pid |= 0x8000;
269
270         if ((ret = m920x_write(d->udev, M9206_FILTER, pid, (type << 8) | (idx * 4) )) != 0)
271                 return ret;
272
273         if ((ret = m920x_write(d->udev, M9206_FILTER, 0, (type << 8) | (idx * 4) )) != 0)
274                 return ret;
275
276         return ret;
277 }
278
279 static int m920x_update_filters(struct dvb_usb_adapter *adap)
280 {
281         struct m920x_state *m = adap->dev->priv;
282         int enabled = m->filtering_enabled[adap->id];
283         int i, ret = 0, filter = 0;
284         int ep = adap->props.stream.endpoint;
285
286         for (i = 0; i < M9206_MAX_FILTERS; i++)
287                 if (m->filters[adap->id][i] == 8192)
288                         enabled = 0;
289
290         /* Disable all filters */
291         if ((ret = m920x_set_filter(adap->dev, ep, 1, enabled)) != 0)
292                 return ret;
293
294         for (i = 0; i < M9206_MAX_FILTERS; i++)
295                 if ((ret = m920x_set_filter(adap->dev, ep, i + 2, 0)) != 0)
296                         return ret;
297
298         /* Set */
299         if (enabled) {
300                 for (i = 0; i < M9206_MAX_FILTERS; i++) {
301                         if (m->filters[adap->id][i] == 0)
302                                 continue;
303
304                         if ((ret = m920x_set_filter(adap->dev, ep, filter + 2, m->filters[adap->id][i])) != 0)
305                                 return ret;
306
307                         filter++;
308                 }
309         }
310
311         return ret;
312 }
313
314 static int m920x_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
315 {
316         struct m920x_state *m = adap->dev->priv;
317
318         m->filtering_enabled[adap->id] = onoff ? 1 : 0;
319
320         return m920x_update_filters(adap);
321 }
322
323 static int m920x_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
324 {
325         struct m920x_state *m = adap->dev->priv;
326
327         m->filters[adap->id][index] = onoff ? pid : 0;
328
329         return m920x_update_filters(adap);
330 }
331
332 static int m920x_firmware_download(struct usb_device *udev, const struct firmware *fw)
333 {
334         u16 value, index, size;
335         u8 read[4], *buff;
336         int i, pass, ret = 0;
337
338         buff = kmalloc(65536, GFP_KERNEL);
339
340         if ((ret = m920x_read(udev, M9206_FILTER, 0x0, 0x8000, read, 4)) != 0)
341                 goto done;
342         deb("%x %x %x %x\n", read[0], read[1], read[2], read[3]);
343
344         if ((ret = m920x_read(udev, M9206_FW, 0x0, 0x0, read, 1)) != 0)
345                 goto done;
346         deb("%x\n", read[0]);
347
348         for (pass = 0; pass < 2; pass++) {
349                 for (i = 0; i + (sizeof(u16) * 3) < fw->size;) {
350                         value = le16_to_cpu(*(u16 *)(fw->data + i));
351                         i += sizeof(u16);
352
353                         index = le16_to_cpu(*(u16 *)(fw->data + i));
354                         i += sizeof(u16);
355
356                         size = le16_to_cpu(*(u16 *)(fw->data + i));
357                         i += sizeof(u16);
358
359                         if (pass == 1) {
360                                 /* Will stall if using fw->data ... */
361                                 memcpy(buff, fw->data + i, size);
362
363                                 ret = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
364                                                       M9206_FW,
365                                                       USB_TYPE_VENDOR | USB_DIR_OUT,
366                                                       value, index, buff, size, 20);
367                                 if (ret != size) {
368                                         deb("error while uploading fw!\n");
369                                         ret = -EIO;
370                                         goto done;
371                                 }
372                                 msleep(3);
373                         }
374                         i += size;
375                 }
376                 if (i != fw->size) {
377                         deb("bad firmware file!\n");
378                         ret = -EINVAL;
379                         goto done;
380                 }
381         }
382
383         msleep(36);
384
385         /* m920x will disconnect itself from the bus after this. */
386         (void) m920x_write(udev, M9206_CORE, 0x01, M9206_FW_GO);
387         deb("firmware uploaded!\n");
388
389  done:
390         kfree(buff);
391
392         return ret;
393 }
394
395 /* Callbacks for DVB USB */
396 static int m920x_identify_state(struct usb_device *udev,
397                                 struct dvb_usb_device_properties *props,
398                                 struct dvb_usb_device_description **desc,
399                                 int *cold)
400 {
401         struct usb_host_interface *alt;
402
403         alt = usb_altnum_to_altsetting(usb_ifnum_to_if(udev, 0), 1);
404         *cold = (alt == NULL) ? 1 : 0;
405
406         return 0;
407 }
408
409 /* demod configurations */
410 static int m920x_mt352_demod_init(struct dvb_frontend *fe)
411 {
412         int ret;
413         u8 config[] = { CONFIG, 0x3d };
414         u8 clock[] = { CLOCK_CTL, 0x30 };
415         u8 reset[] = { RESET, 0x80 };
416         u8 adc_ctl[] = { ADC_CTL_1, 0x40 };
417         u8 agc[] = { AGC_TARGET, 0x1c, 0x20 };
418         u8 sec_agc[] = { 0x69, 0x00, 0xff, 0xff, 0x40, 0xff, 0x00, 0x40, 0x40 };
419         u8 unk1[] = { 0x93, 0x1a };
420         u8 unk2[] = { 0xb5, 0x7a };
421
422         deb("Demod init!\n");
423
424         if ((ret = mt352_write(fe, config, ARRAY_SIZE(config))) != 0)
425                 return ret;
426         if ((ret = mt352_write(fe, clock, ARRAY_SIZE(clock))) != 0)
427                 return ret;
428         if ((ret = mt352_write(fe, reset, ARRAY_SIZE(reset))) != 0)
429                 return ret;
430         if ((ret = mt352_write(fe, adc_ctl, ARRAY_SIZE(adc_ctl))) != 0)
431                 return ret;
432         if ((ret = mt352_write(fe, agc, ARRAY_SIZE(agc))) != 0)
433                 return ret;
434         if ((ret = mt352_write(fe, sec_agc, ARRAY_SIZE(sec_agc))) != 0)
435                 return ret;
436         if ((ret = mt352_write(fe, unk1, ARRAY_SIZE(unk1))) != 0)
437                 return ret;
438         if ((ret = mt352_write(fe, unk2, ARRAY_SIZE(unk2))) != 0)
439                 return ret;
440
441         return 0;
442 }
443
444 static struct mt352_config m920x_mt352_config = {
445         .demod_address = 0x0f,
446         .no_tuner = 1,
447         .demod_init = m920x_mt352_demod_init,
448 };
449
450 static struct tda1004x_config m920x_tda10046_08_config = {
451         .demod_address = 0x08,
452         .invert = 0,
453         .invert_oclk = 0,
454         .ts_mode = TDA10046_TS_SERIAL,
455         .xtal_freq = TDA10046_XTAL_16M,
456         .if_freq = TDA10046_FREQ_045,
457         .agc_config = TDA10046_AGC_TDA827X,
458         .gpio_config = TDA10046_GPTRI,
459         .request_firmware = NULL,
460 };
461
462 static struct tda1004x_config m920x_tda10046_0b_config = {
463         .demod_address = 0x0b,
464         .invert = 0,
465         .invert_oclk = 0,
466         .ts_mode = TDA10046_TS_SERIAL,
467         .xtal_freq = TDA10046_XTAL_16M,
468         .if_freq = TDA10046_FREQ_045,
469         .agc_config = TDA10046_AGC_TDA827X,
470         .gpio_config = TDA10046_GPTRI,
471         .request_firmware = NULL, /* uses firmware EEPROM */
472 };
473
474 /* tuner configurations */
475 static struct qt1010_config m920x_qt1010_config = {
476         .i2c_address = 0x62
477 };
478
479 /* Callbacks for DVB USB */
480 static int m920x_mt352_frontend_attach(struct dvb_usb_adapter *adap)
481 {
482         deb("%s\n",__func__);
483
484         if ((adap->fe = dvb_attach(mt352_attach,
485                                    &m920x_mt352_config,
486                                    &adap->dev->i2c_adap)) == NULL)
487                 return -EIO;
488
489         return 0;
490 }
491
492 static int m920x_tda10046_08_frontend_attach(struct dvb_usb_adapter *adap)
493 {
494         deb("%s\n",__func__);
495
496         if ((adap->fe = dvb_attach(tda10046_attach,
497                                    &m920x_tda10046_08_config,
498                                    &adap->dev->i2c_adap)) == NULL)
499                 return -EIO;
500
501         return 0;
502 }
503
504 static int m920x_tda10046_0b_frontend_attach(struct dvb_usb_adapter *adap)
505 {
506         deb("%s\n",__func__);
507
508         if ((adap->fe = dvb_attach(tda10046_attach,
509                                    &m920x_tda10046_0b_config,
510                                    &adap->dev->i2c_adap)) == NULL)
511                 return -EIO;
512
513         return 0;
514 }
515
516 static int m920x_qt1010_tuner_attach(struct dvb_usb_adapter *adap)
517 {
518         deb("%s\n",__func__);
519
520         if (dvb_attach(qt1010_attach, adap->fe, &adap->dev->i2c_adap, &m920x_qt1010_config) == NULL)
521                 return -ENODEV;
522
523         return 0;
524 }
525
526 static int m920x_tda8275_60_tuner_attach(struct dvb_usb_adapter *adap)
527 {
528         deb("%s\n",__func__);
529
530         if (dvb_attach(tda827x_attach, adap->fe, 0x60, &adap->dev->i2c_adap, NULL) == NULL)
531                 return -ENODEV;
532
533         return 0;
534 }
535
536 static int m920x_tda8275_61_tuner_attach(struct dvb_usb_adapter *adap)
537 {
538         deb("%s\n",__func__);
539
540         if (dvb_attach(tda827x_attach, adap->fe, 0x61, &adap->dev->i2c_adap, NULL) == NULL)
541                 return -ENODEV;
542
543         return 0;
544 }
545
546 /* device-specific initialization */
547 static struct m920x_inits megasky_rc_init [] = {
548         { M9206_RC_INIT2, 0xa8 },
549         { M9206_RC_INIT1, 0x51 },
550         { } /* terminating entry */
551 };
552
553 static struct m920x_inits tvwalkertwin_rc_init [] = {
554         { M9206_RC_INIT2, 0x00 },
555         { M9206_RC_INIT1, 0xef },
556         { 0xff28,         0x00 },
557         { 0xff23,         0x00 },
558         { 0xff21,         0x30 },
559         { } /* terminating entry */
560 };
561
562 /* ir keymaps */
563 static struct dvb_usb_rc_key megasky_rc_keys [] = {
564         { 0x0, 0x12, KEY_POWER },
565         { 0x0, 0x1e, KEY_CYCLEWINDOWS }, /* min/max */
566         { 0x0, 0x02, KEY_CHANNELUP },
567         { 0x0, 0x05, KEY_CHANNELDOWN },
568         { 0x0, 0x03, KEY_VOLUMEUP },
569         { 0x0, 0x06, KEY_VOLUMEDOWN },
570         { 0x0, 0x04, KEY_MUTE },
571         { 0x0, 0x07, KEY_OK }, /* TS */
572         { 0x0, 0x08, KEY_STOP },
573         { 0x0, 0x09, KEY_MENU }, /* swap */
574         { 0x0, 0x0a, KEY_REWIND },
575         { 0x0, 0x1b, KEY_PAUSE },
576         { 0x0, 0x1f, KEY_FASTFORWARD },
577         { 0x0, 0x0c, KEY_RECORD },
578         { 0x0, 0x0d, KEY_CAMERA }, /* screenshot */
579         { 0x0, 0x0e, KEY_COFFEE }, /* "MTS" */
580 };
581
582 static struct dvb_usb_rc_key tvwalkertwin_rc_keys [] = {
583         { 0x0, 0x01, KEY_ZOOM }, /* Full Screen */
584         { 0x0, 0x02, KEY_CAMERA }, /* snapshot */
585         { 0x0, 0x03, KEY_MUTE },
586         { 0x0, 0x04, KEY_REWIND },
587         { 0x0, 0x05, KEY_PLAYPAUSE }, /* Play/Pause */
588         { 0x0, 0x06, KEY_FASTFORWARD },
589         { 0x0, 0x07, KEY_RECORD },
590         { 0x0, 0x08, KEY_STOP },
591         { 0x0, 0x09, KEY_TIME }, /* Timeshift */
592         { 0x0, 0x0c, KEY_COFFEE }, /* Recall */
593         { 0x0, 0x0e, KEY_CHANNELUP },
594         { 0x0, 0x12, KEY_POWER },
595         { 0x0, 0x15, KEY_MENU }, /* source */
596         { 0x0, 0x18, KEY_CYCLEWINDOWS }, /* TWIN PIP */
597         { 0x0, 0x1a, KEY_CHANNELDOWN },
598         { 0x0, 0x1b, KEY_VOLUMEDOWN },
599         { 0x0, 0x1e, KEY_VOLUMEUP },
600 };
601
602 /* DVB USB Driver stuff */
603 static struct dvb_usb_device_properties megasky_properties;
604 static struct dvb_usb_device_properties digivox_mini_ii_properties;
605 static struct dvb_usb_device_properties tvwalkertwin_properties;
606 static struct dvb_usb_device_properties dposh_properties;
607
608 static int m920x_probe(struct usb_interface *intf,
609                        const struct usb_device_id *id)
610 {
611         struct dvb_usb_device *d = NULL;
612         int ret;
613         struct m920x_inits *rc_init_seq = NULL;
614         int bInterfaceNumber = intf->cur_altsetting->desc.bInterfaceNumber;
615
616         deb("Probing for m920x device at interface %d\n", bInterfaceNumber);
617
618         if (bInterfaceNumber == 0) {
619                 /* Single-tuner device, or first interface on
620                  * multi-tuner device
621                  */
622
623                 ret = dvb_usb_device_init(intf, &megasky_properties,
624                                           THIS_MODULE, &d, adapter_nr);
625                 if (ret == 0) {
626                         rc_init_seq = megasky_rc_init;
627                         goto found;
628                 }
629
630                 ret = dvb_usb_device_init(intf, &digivox_mini_ii_properties,
631                                           THIS_MODULE, &d, adapter_nr);
632                 if (ret == 0) {
633                         /* No remote control, so no rc_init_seq */
634                         goto found;
635                 }
636
637                 /* This configures both tuners on the TV Walker Twin */
638                 ret = dvb_usb_device_init(intf, &tvwalkertwin_properties,
639                                           THIS_MODULE, &d, adapter_nr);
640                 if (ret == 0) {
641                         rc_init_seq = tvwalkertwin_rc_init;
642                         goto found;
643                 }
644
645                 ret = dvb_usb_device_init(intf, &dposh_properties,
646                                           THIS_MODULE, &d, adapter_nr);
647                 if (ret == 0) {
648                         /* Remote controller not supported yet. */
649                         goto found;
650                 }
651
652                 return ret;
653         } else {
654                 /* Another interface on a multi-tuner device */
655
656                 /* The LifeView TV Walker Twin gets here, but struct
657                  * tvwalkertwin_properties already configured both
658                  * tuners, so there is nothing for us to do here
659                  */
660         }
661
662  found:
663         if ((ret = m920x_init_ep(intf)) < 0)
664                 return ret;
665
666         if (d && (ret = m920x_init(d, rc_init_seq)) != 0)
667                 return ret;
668
669         return ret;
670 }
671
672 static struct usb_device_id m920x_table [] = {
673                 { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580) },
674                 { USB_DEVICE(USB_VID_ANUBIS_ELECTRONIC,
675                              USB_PID_MSI_DIGI_VOX_MINI_II) },
676                 { USB_DEVICE(USB_VID_ANUBIS_ELECTRONIC,
677                              USB_PID_LIFEVIEW_TV_WALKER_TWIN_COLD) },
678                 { USB_DEVICE(USB_VID_ANUBIS_ELECTRONIC,
679                              USB_PID_LIFEVIEW_TV_WALKER_TWIN_WARM) },
680                 { USB_DEVICE(USB_VID_DPOSH, USB_PID_DPOSH_M9206_COLD) },
681                 { USB_DEVICE(USB_VID_DPOSH, USB_PID_DPOSH_M9206_WARM) },
682                 { }             /* Terminating entry */
683 };
684 MODULE_DEVICE_TABLE (usb, m920x_table);
685
686 static struct dvb_usb_device_properties megasky_properties = {
687         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
688
689         .usb_ctrl = DEVICE_SPECIFIC,
690         .firmware = "dvb-usb-megasky-02.fw",
691         .download_firmware = m920x_firmware_download,
692
693         .rc_interval      = 100,
694         .rc_key_map       = megasky_rc_keys,
695         .rc_key_map_size  = ARRAY_SIZE(megasky_rc_keys),
696         .rc_query         = m920x_rc_query,
697
698         .size_of_priv     = sizeof(struct m920x_state),
699
700         .identify_state   = m920x_identify_state,
701         .num_adapters = 1,
702         .adapter = {{
703                 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
704                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
705
706                 .pid_filter_count = 8,
707                 .pid_filter       = m920x_pid_filter,
708                 .pid_filter_ctrl  = m920x_pid_filter_ctrl,
709
710                 .frontend_attach  = m920x_mt352_frontend_attach,
711                 .tuner_attach     = m920x_qt1010_tuner_attach,
712
713                 .stream = {
714                         .type = USB_BULK,
715                         .count = 8,
716                         .endpoint = 0x81,
717                         .u = {
718                                 .bulk = {
719                                         .buffersize = 512,
720                                 }
721                         }
722                 },
723         }},
724         .i2c_algo         = &m920x_i2c_algo,
725
726         .num_device_descs = 1,
727         .devices = {
728                 {   "MSI Mega Sky 580 DVB-T USB2.0",
729                         { &m920x_table[0], NULL },
730                         { NULL },
731                 }
732         }
733 };
734
735 static struct dvb_usb_device_properties digivox_mini_ii_properties = {
736         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
737
738         .usb_ctrl = DEVICE_SPECIFIC,
739         .firmware = "dvb-usb-digivox-02.fw",
740         .download_firmware = m920x_firmware_download,
741
742         .size_of_priv     = sizeof(struct m920x_state),
743
744         .identify_state   = m920x_identify_state,
745         .num_adapters = 1,
746         .adapter = {{
747                 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
748                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
749
750                 .pid_filter_count = 8,
751                 .pid_filter       = m920x_pid_filter,
752                 .pid_filter_ctrl  = m920x_pid_filter_ctrl,
753
754                 .frontend_attach  = m920x_tda10046_08_frontend_attach,
755                 .tuner_attach     = m920x_tda8275_60_tuner_attach,
756
757                 .stream = {
758                         .type = USB_BULK,
759                         .count = 8,
760                         .endpoint = 0x81,
761                         .u = {
762                                 .bulk = {
763                                         .buffersize = 0x4000,
764                                 }
765                         }
766                 },
767         }},
768         .i2c_algo         = &m920x_i2c_algo,
769
770         .num_device_descs = 1,
771         .devices = {
772                 {   "MSI DIGI VOX mini II DVB-T USB2.0",
773                         { &m920x_table[1], NULL },
774                         { NULL },
775                 },
776         }
777 };
778
779 /* LifeView TV Walker Twin support by Nick Andrew <nick@nick-andrew.net>
780  *
781  * LifeView TV Walker Twin has 1 x M9206, 2 x TDA10046, 2 x TDA8275A
782  * TDA10046 #0 is located at i2c address 0x08
783  * TDA10046 #1 is located at i2c address 0x0b
784  * TDA8275A #0 is located at i2c address 0x60
785  * TDA8275A #1 is located at i2c address 0x61
786  */
787 static struct dvb_usb_device_properties tvwalkertwin_properties = {
788         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
789
790         .usb_ctrl = DEVICE_SPECIFIC,
791         .firmware = "dvb-usb-tvwalkert.fw",
792         .download_firmware = m920x_firmware_download,
793
794         .rc_interval      = 100,
795         .rc_key_map       = tvwalkertwin_rc_keys,
796         .rc_key_map_size  = ARRAY_SIZE(tvwalkertwin_rc_keys),
797         .rc_query         = m920x_rc_query,
798
799         .size_of_priv     = sizeof(struct m920x_state),
800
801         .identify_state   = m920x_identify_state,
802         .num_adapters = 2,
803         .adapter = {{
804                 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
805                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
806
807                 .pid_filter_count = 8,
808                 .pid_filter       = m920x_pid_filter,
809                 .pid_filter_ctrl  = m920x_pid_filter_ctrl,
810
811                 .frontend_attach  = m920x_tda10046_08_frontend_attach,
812                 .tuner_attach     = m920x_tda8275_60_tuner_attach,
813
814                 .stream = {
815                         .type = USB_BULK,
816                         .count = 8,
817                         .endpoint = 0x81,
818                         .u = {
819                                  .bulk = {
820                                          .buffersize = 512,
821                                  }
822                         }
823                 }},{
824                 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
825                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
826
827                 .pid_filter_count = 8,
828                 .pid_filter       = m920x_pid_filter,
829                 .pid_filter_ctrl  = m920x_pid_filter_ctrl,
830
831                 .frontend_attach  = m920x_tda10046_0b_frontend_attach,
832                 .tuner_attach     = m920x_tda8275_61_tuner_attach,
833
834                 .stream = {
835                         .type = USB_BULK,
836                         .count = 8,
837                         .endpoint = 0x82,
838                         .u = {
839                                  .bulk = {
840                                          .buffersize = 512,
841                                  }
842                         }
843                 },
844         }},
845         .i2c_algo         = &m920x_i2c_algo,
846
847         .num_device_descs = 1,
848         .devices = {
849                 {   .name = "LifeView TV Walker Twin DVB-T USB2.0",
850                     .cold_ids = { &m920x_table[2], NULL },
851                     .warm_ids = { &m920x_table[3], NULL },
852                 },
853         }
854 };
855
856 static struct dvb_usb_device_properties dposh_properties = {
857         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
858
859         .usb_ctrl = DEVICE_SPECIFIC,
860         .firmware = "dvb-usb-dposh-01.fw",
861         .download_firmware = m920x_firmware_download,
862
863         .size_of_priv     = sizeof(struct m920x_state),
864
865         .identify_state   = m920x_identify_state,
866         .num_adapters = 1,
867         .adapter = {{
868                 /* Hardware pid filters don't work with this device/firmware */
869
870                 .frontend_attach  = m920x_mt352_frontend_attach,
871                 .tuner_attach     = m920x_qt1010_tuner_attach,
872
873                 .stream = {
874                         .type = USB_BULK,
875                         .count = 8,
876                         .endpoint = 0x81,
877                         .u = {
878                                  .bulk = {
879                                          .buffersize = 512,
880                                  }
881                         }
882                 },
883         }},
884         .i2c_algo         = &m920x_i2c_algo,
885
886         .num_device_descs = 1,
887         .devices = {
888                  {   .name = "Dposh DVB-T USB2.0",
889                      .cold_ids = { &m920x_table[4], NULL },
890                      .warm_ids = { &m920x_table[5], NULL },
891                  },
892          }
893 };
894
895 static struct usb_driver m920x_driver = {
896         .name           = "dvb_usb_m920x",
897         .probe          = m920x_probe,
898         .disconnect     = dvb_usb_device_exit,
899         .id_table       = m920x_table,
900 };
901
902 /* module stuff */
903 static int __init m920x_module_init(void)
904 {
905         int ret;
906
907         if ((ret = usb_register(&m920x_driver))) {
908                 err("usb_register failed. Error number %d", ret);
909                 return ret;
910         }
911
912         return 0;
913 }
914
915 static void __exit m920x_module_exit(void)
916 {
917         /* deregister this driver from the USB subsystem */
918         usb_deregister(&m920x_driver);
919 }
920
921 module_init (m920x_module_init);
922 module_exit (m920x_module_exit);
923
924 MODULE_AUTHOR("Aapo Tahkola <aet@rasterburn.org>");
925 MODULE_DESCRIPTION("DVB Driver for ULI M920x");
926 MODULE_VERSION("0.1");
927 MODULE_LICENSE("GPL");
928
929 /*
930  * Local variables:
931  * c-basic-offset: 8
932  */