V4L/DVB (5131): M920x: more cleanups
[safe/jmp/linux-2.6] / 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         /* Need to access d->adapter[0] */
142         if (d->num_adapters_initialized != 1) {
143                 deb_rc("Impossible happened!\n");
144                 return -EINVAL;
145         }
146
147         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
148                 return -EAGAIN;
149
150         if (num > 2)
151                 return -EINVAL;
152
153         for (i = 0; i < num; i++) {
154                 u8 w_len;
155
156                 if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].addr, 0x80)) != 0)
157                         goto unlock;
158
159                 if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].buf[0], 0x0)) != 0)
160                         goto unlock;
161
162                 if (i + 1 < num && msg[i + 1].flags & I2C_M_RD) {
163                         /* Possibly device dependant */
164                         if (msg[i].addr == d->adapter[0].pll_addr)
165                                 w_len = 0xc5;
166                         else
167                                 w_len = 0x1f;
168
169                         if ((ret = m9206_write(d->udev, M9206_I2C, w_len, 0x80)) != 0)
170                                 goto unlock;
171
172                         if ((ret = m9206_read(d->udev, M9206_I2C, 0x0, 0x60, msg[i + 1].buf, msg[i + 1].len)) != 0)
173                                 goto unlock;
174
175                         i++;
176                 } else {
177                         if (msg[i].len != 2)
178                                 return -EINVAL;
179
180                         if ((ret = m9206_write(d->udev, M9206_I2C, msg[i].buf[1], 0x40)) != 0)
181                                 goto unlock;
182                 }
183         }
184         ret = i;
185         unlock:
186         mutex_unlock(&d->i2c_mutex);
187
188         return ret;
189 }
190
191 static u32 m9206_i2c_func(struct i2c_adapter *adapter)
192 {
193         return I2C_FUNC_I2C;
194 }
195
196 static struct i2c_algorithm m9206_i2c_algo = {
197         .master_xfer   = m9206_i2c_xfer,
198         .functionality = m9206_i2c_func,
199 };
200
201 /* Callbacks for DVB USB */
202 static int megasky_identify_state(struct usb_device *udev,
203                                   struct dvb_usb_device_properties *props,
204                                   struct dvb_usb_device_description **desc,
205                                   int *cold)
206 {
207         struct usb_host_interface *alt;
208
209         alt = usb_altnum_to_altsetting(usb_ifnum_to_if(udev, 0), 1);
210         *cold = (alt == NULL) ? 1 : 0;
211
212         return 0;
213 }
214
215 static int megasky_mt352_demod_init(struct dvb_frontend *fe)
216 {
217         u8 config[] = { CONFIG, 0x3d };
218         u8 clock[] = { CLOCK_CTL, 0x30 };
219         u8 reset[] = { RESET, 0x80 };
220         u8 adc_ctl[] = { ADC_CTL_1, 0x40 };
221         u8 agc[] = { AGC_TARGET, 0x1c, 0x20 };
222         u8 sec_agc[] = { 0x69, 0x00, 0xff, 0xff, 0x40, 0xff, 0x00, 0x40, 0x40 };
223         u8 unk1[] = { 0x93, 0x1a };
224         u8 unk2[] = { 0xb5, 0x7a };
225
226         mt352_write(fe, config, ARRAY_SIZE(config));
227         mt352_write(fe, clock, ARRAY_SIZE(clock));
228         mt352_write(fe, reset, ARRAY_SIZE(reset));
229         mt352_write(fe, adc_ctl, ARRAY_SIZE(adc_ctl));
230         mt352_write(fe, agc, ARRAY_SIZE(agc));
231         mt352_write(fe, sec_agc, ARRAY_SIZE(sec_agc));
232         mt352_write(fe, unk1, ARRAY_SIZE(unk1));
233         mt352_write(fe, unk2, ARRAY_SIZE(unk2));
234
235         deb_rc("Demod init!\n");
236
237         return 0;
238 }
239
240 static struct mt352_config megasky_mt352_config = {
241         .demod_address = 0x1e,
242         .no_tuner = 1,
243         .demod_init = megasky_mt352_demod_init,
244 };
245
246 static int megasky_frontend_attach(struct dvb_usb_adapter *adap)
247 {
248         deb_rc("megasky_frontend_attach!\n");
249
250         if ((adap->fe = dvb_attach(mt352_attach, &megasky_mt352_config, &adap->dev->i2c_adap)) != NULL) {
251                 return 0;
252         }
253         return -EIO;
254 }
255
256 static int megasky_tuner_attach(struct dvb_usb_adapter *adap)
257 {
258         adap->pll_addr = 0xc4;
259         adap->pll_desc = NULL;
260         adap->fe->ops.tuner_ops.set_params = qt1010_set_params;
261
262         return 0;
263 }
264
265 static int m9206_set_filter(struct dvb_usb_adapter *adap, int type, int idx, int pid)
266 {
267         int ret = 0;
268
269         if (pid >= 0x8000)
270                 return -EINVAL;
271
272         pid |= 0x8000;
273
274         if ((ret = m9206_write(adap->dev->udev, M9206_FILTER, pid, (type << 8) | (idx * 4) )) != 0)
275                 return ret;
276
277         if ((ret = m9206_write(adap->dev->udev, M9206_FILTER, 0, (type << 8) | (idx * 4) )) != 0)
278                 return ret;
279
280         return ret;
281 }
282
283 static int m9206_update_filters(struct dvb_usb_adapter *adap)
284 {
285         struct m9206_state *m = adap->dev->priv;
286         int enabled = m->filtering_enabled;
287         int i, ret = 0, filter = 0;
288
289         for (i = 0; i < M9206_MAX_FILTERS; i++)
290                 if (m->filters[i] == 8192)
291                         enabled = 0;
292
293         /* Disable all filters */
294         if ((ret = m9206_set_filter(adap, 0x81, 1, enabled)) != 0)
295                 return ret;
296
297         for (i = 0; i < M9206_MAX_FILTERS; i++)
298                 if ((ret = m9206_set_filter(adap, 0x81, i + 2, 0)) != 0)
299                         return ret;
300
301         if ((ret = m9206_set_filter(adap, 0x82, 0, 0x0)) != 0)
302                 return ret;
303
304         /* Set */
305         if (enabled) {
306                 for (i = 0; i < M9206_MAX_FILTERS; i++) {
307                         if (m->filters[i] == 0)
308                                 continue;
309
310                         if ((ret = m9206_set_filter(adap, 0x81, filter + 2, m->filters[i])) != 0)
311                                 return ret;
312
313                         filter++;
314                 }
315         }
316
317         if ((ret = m9206_set_filter(adap, 0x82, 0, 0x02f5)) != 0)
318                 return ret;
319
320         return ret;
321 }
322
323 static int m9206_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
324 {
325         struct m9206_state *m = adap->dev->priv;
326
327         m->filtering_enabled = onoff ? 1 : 0;
328
329         return m9206_update_filters(adap);
330 }
331
332 static int m9206_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
333 {
334         struct m9206_state *m = adap->dev->priv;
335
336         m->filters[index] = onoff ? pid : 0;
337
338         return m9206_update_filters(adap);
339 }
340
341 static int m9206_firmware_download(struct usb_device *udev, const struct firmware *fw)
342 {
343         u16 value, index, size;
344         u8 read[4], *buff;
345         int i, pass, ret = 0;
346
347         buff = kmalloc(65536, GFP_KERNEL);
348
349         if ((ret = m9206_read(udev, M9206_FILTER, 0x0, 0x8000, read, 4)) != 0)
350                 goto done;
351         deb_rc("%x %x %x %x\n", read[0], read[1], read[2], read[3]);
352
353         if ((ret = m9206_read(udev, M9206_FW, 0x0, 0x0, read, 1)) != 0)
354                 goto done;
355         deb_rc("%x\n", read[0]);
356
357         for (pass = 0; pass < 2; pass++) {
358                 for (i = 0; i + (sizeof(u16) * 3) < fw->size;) {
359                         value = le16_to_cpu(*(u16 *)(fw->data + i));
360                         i += sizeof(u16);
361
362                         index = le16_to_cpu(*(u16 *)(fw->data + i));
363                         i += sizeof(u16);
364
365                         size = le16_to_cpu(*(u16 *)(fw->data + i));
366                         i += sizeof(u16);
367
368                         if (pass == 1) {
369                                 /* Will stall if using fw->data ... */
370                                 memcpy(buff, fw->data + i, size);
371
372                                 ret = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
373                                             M9206_FW,
374                                             USB_TYPE_VENDOR | USB_DIR_OUT,
375                                             value, index, buff, size, 20);
376                                 if (ret != size) {
377                                         deb_rc("error while uploading fw!\n");
378                                         ret = -EIO;
379                                         goto done;
380                                 }
381                                 msleep(3);
382                         }
383                         i += size;
384                 }
385                 if (i != fw->size) {
386                         ret = -EINVAL;
387                         goto done;
388                 }
389         }
390
391         msleep(36);
392
393         /* m9206 will disconnect itself from the bus after this. */
394         (void) m9206_write(udev, M9206_CORE, 0x01, M9206_FW_GO);
395         deb_rc("firmware uploaded!\n");
396
397         done:
398         kfree(buff);
399
400         return ret;
401 }
402
403 /* DVB USB Driver stuff */
404 static struct dvb_usb_device_properties megasky_properties;
405
406 static int m920x_probe(struct usb_interface *intf, const struct usb_device_id *id)
407 {
408         struct dvb_usb_device *d;
409         struct usb_host_interface *alt;
410         int ret;
411
412         if ((ret = dvb_usb_device_init(intf, &megasky_properties, THIS_MODULE, &d)) == 0) {
413                 deb_rc("probed!\n");
414
415                 alt = usb_altnum_to_altsetting(intf, 1);
416                 if (alt == NULL) {
417                         deb_rc("not alt found!\n");
418                         return -ENODEV;
419                 }
420
421                 ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber, alt->desc.bAlternateSetting);
422                 if (ret < 0)
423                         return ret;
424
425                 deb_rc("Changed to alternate setting!\n");
426
427                 if ((ret = m9206_rc_init(d->udev)) != 0)
428                         return ret;
429         }
430         return ret;
431 }
432
433 static struct usb_device_id m920x_table [] = {
434                 { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580) },
435                 { }             /* Terminating entry */
436 };
437 MODULE_DEVICE_TABLE (usb, m920x_table);
438
439 static struct dvb_usb_device_properties megasky_properties = {
440         .usb_ctrl = DEVICE_SPECIFIC,
441         .firmware = "dvb-usb-megasky-02.fw",
442         .download_firmware = m9206_firmware_download,
443
444         .rc_interval      = 100,
445         .rc_key_map       = megasky_rc_keys,
446         .rc_key_map_size  = ARRAY_SIZE(megasky_rc_keys),
447         .rc_query         = m9206_rc_query,
448
449         .size_of_priv     = sizeof(struct m9206_state),
450
451         .identify_state   = megasky_identify_state,
452         .num_adapters = 1,
453         .adapter = {{
454                 .caps = DVB_USB_IS_AN_I2C_ADAPTER | DVB_USB_ADAP_HAS_PID_FILTER |
455                         DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
456
457                 .pid_filter_count = 8,
458                 .pid_filter       = m9206_pid_filter,
459                 .pid_filter_ctrl  = m9206_pid_filter_ctrl,
460
461                 .frontend_attach  = megasky_frontend_attach,
462                 .tuner_attach     = megasky_tuner_attach,
463
464                 .stream = {
465                         .type = USB_BULK,
466                         .count = 8,
467                         .endpoint = 0x81,
468                         .u = {
469                                 .bulk = {
470                                         .buffersize = 512,
471                                 }
472                         }
473                 },
474         }},
475         .i2c_algo         = &m9206_i2c_algo,
476
477         .generic_bulk_ctrl_endpoint = 0x01,
478
479         .num_device_descs = 1,
480         .devices = {
481                 {   "MSI Mega Sky 580 DVB-T USB2.0",
482                         { &m920x_table[0], NULL },
483                         { NULL },
484                 },
485                 { NULL },
486         }
487 };
488
489 static struct usb_driver m920x_driver = {
490         .name           = "dvb_usb_m920x",
491         .probe          = m920x_probe,
492         .disconnect     = dvb_usb_device_exit,
493         .id_table       = m920x_table,
494 };
495
496 /* module stuff */
497 static int __init m920x_module_init(void)
498 {
499         int ret;
500
501         if ((ret = usb_register(&m920x_driver))) {
502                 err("usb_register failed. Error number %d", ret);
503                 return ret;
504         }
505
506         return 0;
507 }
508
509 static void __exit m920x_module_exit(void)
510 {
511         /* deregister this driver from the USB subsystem */
512         usb_deregister(&m920x_driver);
513 }
514
515 module_init (m920x_module_init);
516 module_exit (m920x_module_exit);
517
518 MODULE_AUTHOR("Aapo Tahkola <aet@rasterburn.org>");
519 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / Uli m920x");
520 MODULE_VERSION("0.1");
521 MODULE_LICENSE("GPL");