2.6.12 planetlab config file
[linux-2.6.git] / drivers / media / dvb / dibusb / dvb-dibusb.c
1 /*
2  * Driver for mobile USB Budget DVB-T devices based on reference
3  * design made by DiBcom (http://www.dibcom.fr/)
4  *
5  * dvb-dibusb.c
6  *
7  * Copyright (C) 2004 Patrick Boettcher (patrick.boettcher@desy.de)
8  *
9  * based on GPL code from DiBcom, which has
10  * Copyright (C) 2004 Amaury Demol for DiBcom (ademol@dibcom.fr)
11  *
12  * Remote control code added by David Matthews (dm@prolingua.co.uk)
13  *
14  *      This program is free software; you can redistribute it and/or
15  *      modify it under the terms of the GNU General Public License as
16  *      published by the Free Software Foundation, version 2.
17  *
18  * Acknowledgements
19  *
20  *  Amaury Demol (ademol@dibcom.fr) from DiBcom for providing specs and driver
21  *  sources, on which this driver (and the dib3000mb/mc/p frontends) are based.
22  *
23  * see Documentation/dvb/README.dibusb for more information
24  */
25
26 #include <linux/config.h>
27 #include <linux/kernel.h>
28 #include <linux/usb.h>
29 #include <linux/firmware.h>
30 #include <linux/version.h>
31 #include <linux/moduleparam.h>
32 #include <linux/pci.h>
33 #include <linux/input.h>
34
35 #include "dmxdev.h"
36 #include "dvb_demux.h"
37 #include "dvb_filter.h"
38 #include "dvb_net.h"
39 #include "dvb_frontend.h"
40 #include "dib3000.h"
41
42 #include "dvb-dibusb.h"
43
44
45 /* debug */
46 #ifdef CONFIG_DVB_DIBCOM_DEBUG
47 #define dprintk(level,args...) \
48             do { if ((debug & level)) { printk(args); } } while (0)
49
50 #define debug_dump(b,l) if (debug) {\
51         int i; deb_xfer("%s: %d > ",__FUNCTION__,l); \
52         for (i = 0; i < l; i++) deb_xfer("%02x ", b[i]); \
53         deb_xfer("\n");\
54 }
55
56 static int debug;
57 module_param(debug, int, 0x644);
58 MODULE_PARM_DESC(debug, "set debugging level (1=info,2=xfer,4=alotmore,8=ts,16=err,32=rc (|-able)).");
59 #else
60 #define dprintk(args...)
61 #define debug_dump(b,l)
62 #endif
63
64 #define deb_info(args...) dprintk(0x01,args)
65 #define deb_xfer(args...) dprintk(0x02,args)
66 #define deb_alot(args...) dprintk(0x04,args)
67 #define deb_ts(args...)   dprintk(0x08,args)
68 #define deb_err(args...)   dprintk(0x10,args)
69 #define deb_rc(args...)   dprintk(0x20,args)
70
71 static int pid_parse;
72 module_param(pid_parse, int, 0x644);
73 MODULE_PARM_DESC(pid_parse, "enable pid parsing (filtering) when running at USB2.0");
74
75 /* Version information */
76 #define DRIVER_VERSION "0.1"
77 #define DRIVER_DESC "Driver for DiBcom based USB Budget DVB-T device"
78 #define DRIVER_AUTHOR "Patrick Boettcher, patrick.boettcher@desy.de"
79
80 static int dibusb_readwrite_usb(struct usb_dibusb *dib,
81                 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
82 {
83         int actlen,ret = -ENOMEM;
84
85         if (wbuf == NULL || wlen == 0)
86                 return -EINVAL;
87
88         if ((ret = down_interruptible(&dib->usb_sem)))
89                 return ret;
90
91         if (dib->feedcount &&
92                 wbuf[0] == DIBUSB_REQ_I2C_WRITE &&
93                 dib->dibdev->parm->type == DIBUSB1_1)
94                 deb_err("BUG: writing to i2c, while TS-streaming destroys the stream."
95                                 "(%x reg: %x %x)\n", wbuf[0],wbuf[2],wbuf[3]);
96                         
97         debug_dump(wbuf,wlen);
98
99         ret = usb_bulk_msg(dib->udev,usb_sndbulkpipe(dib->udev,
100                         dib->dibdev->parm->cmd_pipe), wbuf,wlen,&actlen,
101                         DIBUSB_I2C_TIMEOUT);
102
103         if (ret)
104                 err("bulk message failed: %d (%d/%d)",ret,wlen,actlen);
105         else
106                 ret = actlen != wlen ? -1 : 0;
107
108         /* an answer is expected, and no error before */
109         if (!ret && rbuf && rlen) {
110                 ret = usb_bulk_msg(dib->udev,usb_rcvbulkpipe(dib->udev,
111                                 dib->dibdev->parm->result_pipe),rbuf,rlen,&actlen,
112                                 DIBUSB_I2C_TIMEOUT);
113
114                 if (ret)
115                         err("recv bulk message failed: %d",ret);
116                 else {
117                         deb_alot("rlen: %d\n",rlen);
118                         debug_dump(rbuf,actlen);
119                 }
120         }
121
122         up(&dib->usb_sem);
123         return ret;
124 }
125
126 static int dibusb_i2c_msg(struct usb_dibusb *dib, u8 addr,
127                 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
128 {
129         u8 sndbuf[wlen+4]; /* lead(1) devaddr,direction(1) addr(2) data(wlen) (len(2) (when reading)) */
130         /* write only ? */
131         int wo = (rbuf == NULL || rlen == 0),
132                 len = 2 + wlen + (wo ? 0 : 2);
133
134         deb_alot("wo: %d, wlen: %d, len: %d\n",wo,wlen,len);
135
136         sndbuf[0] = wo ? DIBUSB_REQ_I2C_WRITE : DIBUSB_REQ_I2C_READ;
137         sndbuf[1] = (addr & 0xfe) | (wo ? 0 : 1);
138
139         memcpy(&sndbuf[2],wbuf,wlen);
140
141         if (!wo) {
142                 sndbuf[wlen+2] = (rlen >> 8) & 0xff;
143                 sndbuf[wlen+3] = rlen & 0xff;
144         }
145
146         return dibusb_readwrite_usb(dib,sndbuf,len,rbuf,rlen);
147 }
148
149 /*
150  * DVB stuff
151  */
152 static void dibusb_urb_complete(struct urb *urb, struct pt_regs *ptregs)
153 {
154         struct usb_dibusb *dib = urb->context;
155
156         deb_ts("urb complete feedcount: %d, status: %d\n",dib->feedcount,urb->status);
157
158         if (dib->feedcount > 0 && urb->status == 0) {
159                 deb_ts("URB return len: %d\n",urb->actual_length);
160                 if (urb->actual_length % 188)
161                         deb_ts("TS Packets: %d, %d\n", urb->actual_length/188,urb->actual_length % 188);
162
163                 /* Francois recommends to drop not full-filled packets, even if they may 
164                  * contain valid TS packets
165                  */
166                 if (urb->actual_length == dib->dibdev->parm->default_size && dib->dvb_is_ready)
167                 dvb_dmx_swfilter_packets(&dib->demux, (u8*) urb->transfer_buffer,urb->actual_length/188);
168                 else
169                         deb_ts("URB dropped because of the " 
170                                         "actual_length or !dvb_is_ready (%d).\n",dib->dvb_is_ready);
171         } else 
172                 deb_ts("URB dropped because of feedcount or status.\n");
173
174                 usb_submit_urb(urb,GFP_KERNEL);
175 }
176
177 static int dibusb_ctrl_feed(struct usb_dibusb *dib, int pid, int onoff)
178 {
179         if (dib->dibdev->parm->firmware_bug && dib->feedcount) {
180                 deb_ts("stop feeding\n");
181                 if (dib->xfer_ops.fifo_ctrl != NULL) {
182                         if (dib->xfer_ops.fifo_ctrl(dib->fe,0)) {
183                                 err("error while inhibiting fifo.");
184                                 return -ENODEV;
185                         }
186                 } else {
187                         err("fifo_ctrl is not set.");
188                         return -ENODEV;
189                 }
190         }
191
192         dib->feedcount += onoff ? 1 : -1;
193
194         if (dib->pid_parse) {
195         if (dib->xfer_ops.pid_ctrl != NULL) {
196                 if (dib->xfer_ops.pid_ctrl(dib->fe,pid,onoff) < 0) {
197                 err("no free pid in list.");
198                 return -ENODEV;
199         }
200         } else {
201                 err("no pid ctrl callback.");
202                 return -ENODEV;
203         }
204         }
205         /*
206          * start the feed, either if there is the firmware bug or
207          * if this was the first pid to set.
208          */
209         if (dib->dibdev->parm->firmware_bug || dib->feedcount == onoff) {
210
211                 deb_ts("controlling pid parser\n");
212                 if (dib->xfer_ops.pid_parse != NULL) {
213                         if (dib->xfer_ops.pid_parse(dib->fe,dib->pid_parse) < 0) {
214                                 err("could not handle pid_parser");
215                         }
216                 }
217
218                 deb_ts("start feeding\n");
219                 if (dib->xfer_ops.fifo_ctrl != NULL) {
220                         if (dib->xfer_ops.fifo_ctrl(dib->fe,1)) {
221                                 err("error while enabling fifo.");
222                                 return -ENODEV;
223                         }
224                 } else {
225                         err("fifo_ctrl is not set.");
226                         return -ENODEV;
227 }
228         }
229         return 0;
230 }
231
232 static int dibusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
233 {
234         struct usb_dibusb *dib = dvbdmxfeed->demux->priv;
235         deb_ts("pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,dvbdmxfeed->type);
236         dvbdmxfeed->priv = dib;
237         return dibusb_ctrl_feed(dib,dvbdmxfeed->pid,1);
238 }
239
240 static int dibusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
241 {
242         struct usb_dibusb *dib = (struct usb_dibusb *) dvbdmxfeed->priv;
243         if (dib == NULL) {
244                 err("dib in dmxfeed->priv was NULL");
245                 return -EINVAL;
246 }
247         deb_ts("dvbdmxfeed pid: 0x%04x, feedtype: %d\n",
248                         dvbdmxfeed->pid, dvbdmxfeed->type);
249         return dibusb_ctrl_feed(dib,dvbdmxfeed->pid,0);
250 }
251
252 /* Table to map raw key codes to key events.  This should not be hard-wired
253    into the kernel.  */
254 static const struct { u8 c0, c1, c2; uint32_t key; } rc_keys [] =
255 {
256         /* Key codes for the little Artec T1/Twinhan/HAMA/ remote. */
257         { 0x00, 0xff, 0x16, KEY_POWER },
258         { 0x00, 0xff, 0x10, KEY_MUTE },
259         { 0x00, 0xff, 0x03, KEY_1 },
260         { 0x00, 0xff, 0x01, KEY_2 },
261         { 0x00, 0xff, 0x06, KEY_3 },
262         { 0x00, 0xff, 0x09, KEY_4 },
263         { 0x00, 0xff, 0x1d, KEY_5 },
264         { 0x00, 0xff, 0x1f, KEY_6 },
265         { 0x00, 0xff, 0x0d, KEY_7 },
266         { 0x00, 0xff, 0x19, KEY_8 },
267         { 0x00, 0xff, 0x1b, KEY_9 },
268         { 0x00, 0xff, 0x15, KEY_0 },
269         { 0x00, 0xff, 0x05, KEY_CHANNELUP },
270         { 0x00, 0xff, 0x02, KEY_CHANNELDOWN },
271         { 0x00, 0xff, 0x1e, KEY_VOLUMEUP },
272         { 0x00, 0xff, 0x0a, KEY_VOLUMEDOWN },
273         { 0x00, 0xff, 0x11, KEY_RECORD },
274         { 0x00, 0xff, 0x17, KEY_FAVORITES }, /* Heart symbol - Channel list. */
275         { 0x00, 0xff, 0x14, KEY_PLAY },
276         { 0x00, 0xff, 0x1a, KEY_STOP },
277         { 0x00, 0xff, 0x40, KEY_REWIND },
278         { 0x00, 0xff, 0x12, KEY_FASTFORWARD },
279         { 0x00, 0xff, 0x0e, KEY_PREVIOUS }, /* Recall - Previous channel. */
280         { 0x00, 0xff, 0x4c, KEY_PAUSE },
281         { 0x00, 0xff, 0x4d, KEY_SCREEN }, /* Full screen mode. */
282         { 0x00, 0xff, 0x54, KEY_AUDIO }, /* MTS - Switch to secondary audio. */
283         /* additional keys TwinHan VisionPlus, the Artec seemingly not have */
284         { 0x00, 0xff, 0x0c, KEY_CANCEL }, /* Cancel */
285         { 0x00, 0xff, 0x1c, KEY_EPG }, /* EPG */
286         { 0x00, 0xff, 0x00, KEY_TAB }, /* Tab */
287         { 0x00, 0xff, 0x48, KEY_INFO }, /* Preview */
288         { 0x00, 0xff, 0x04, KEY_LIST }, /* RecordList */
289         { 0x00, 0xff, 0x0f, KEY_TEXT }, /* Teletext */
290         /* Key codes for the KWorld/ADSTech/JetWay remote. */
291         { 0x86, 0x6b, 0x12, KEY_POWER },
292         { 0x86, 0x6b, 0x0f, KEY_SELECT }, /* source */
293         { 0x86, 0x6b, 0x0c, KEY_UNKNOWN }, /* scan */
294         { 0x86, 0x6b, 0x0b, KEY_EPG },
295         { 0x86, 0x6b, 0x10, KEY_MUTE },
296         { 0x86, 0x6b, 0x01, KEY_1 },
297         { 0x86, 0x6b, 0x02, KEY_2 },
298         { 0x86, 0x6b, 0x03, KEY_3 },
299         { 0x86, 0x6b, 0x04, KEY_4 },
300         { 0x86, 0x6b, 0x05, KEY_5 },
301         { 0x86, 0x6b, 0x06, KEY_6 },
302         { 0x86, 0x6b, 0x07, KEY_7 },
303         { 0x86, 0x6b, 0x08, KEY_8 },
304         { 0x86, 0x6b, 0x09, KEY_9 },
305         { 0x86, 0x6b, 0x0a, KEY_0 },
306         { 0x86, 0x6b, 0x18, KEY_ZOOM },
307         { 0x86, 0x6b, 0x1c, KEY_UNKNOWN }, /* preview */
308         { 0x86, 0x6b, 0x13, KEY_UNKNOWN }, /* snap */
309         { 0x86, 0x6b, 0x00, KEY_UNDO },
310         { 0x86, 0x6b, 0x1d, KEY_RECORD },
311         { 0x86, 0x6b, 0x0d, KEY_STOP },
312         { 0x86, 0x6b, 0x0e, KEY_PAUSE },
313         { 0x86, 0x6b, 0x16, KEY_PLAY },
314         { 0x86, 0x6b, 0x11, KEY_BACK },
315         { 0x86, 0x6b, 0x19, KEY_FORWARD },
316         { 0x86, 0x6b, 0x14, KEY_UNKNOWN }, /* pip */
317         { 0x86, 0x6b, 0x15, KEY_ESC },
318         { 0x86, 0x6b, 0x1a, KEY_UP },
319         { 0x86, 0x6b, 0x1e, KEY_DOWN },
320         { 0x86, 0x6b, 0x1f, KEY_LEFT },
321         { 0x86, 0x6b, 0x1b, KEY_RIGHT },
322 };
323
324 /*
325  * Read the remote control and feed the appropriate event.
326  * NEC protocol is used for remote controls
327  */
328 static int dibusb_read_remote_control(struct usb_dibusb *dib)
329 {
330         u8 b[1] = { DIBUSB_REQ_POLL_REMOTE }, rb[5];
331         int ret;
332         int i;
333         if ((ret = dibusb_readwrite_usb(dib,b,1,rb,5)))
334                 return ret;
335
336         switch (rb[0]) {
337                 case DIBUSB_RC_NEC_KEY_PRESSED:
338                         /* rb[1-3] is the actual key, rb[4] is a checksum */
339                         deb_rc("raw key code 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
340                                 rb[1], rb[2], rb[3], rb[4]);
341
342                         if ((0xff - rb[3]) != rb[4]) {
343                                 deb_rc("remote control checksum failed.\n");
344                                 break;
345                         }
346
347                         /* See if we can match the raw key code. */
348                         for (i = 0; i < sizeof(rc_keys)/sizeof(rc_keys[0]); i++) {
349                                 if (rc_keys[i].c0 == rb[1] &&
350                                         rc_keys[i].c1 == rb[2] &&
351                                     rc_keys[i].c2 == rb[3]) {
352                                         dib->rc_input_event = rc_keys[i].key;
353                                         deb_rc("Translated key 0x%04x\n", dib->rc_input_event);
354                                         /* Signal down and up events for this key. */
355                                         input_report_key(&dib->rc_input_dev, dib->rc_input_event, 1);
356                                         input_report_key(&dib->rc_input_dev, dib->rc_input_event, 0);
357                                         input_sync(&dib->rc_input_dev);
358                                         break;
359                                 }
360                         }
361                         break;
362                 case DIBUSB_RC_NEC_EMPTY: /* No (more) remote control keys. */
363                         break;
364                 case DIBUSB_RC_NEC_KEY_REPEATED:
365                         /* rb[1]..rb[4] are always zero.*/
366                         /* Repeats often seem to occur so for the moment just ignore this. */
367                         deb_rc("Key repeat\n");
368                         break;
369                 default:
370                         break;
371         }
372         
373         return 0;
374 }
375
376 #define RC_QUERY_INTERVAL (100) /* milliseconds */
377
378 /* Remote-control poll function - called every RC_QUERY_INTERVAL ms to see
379    whether the remote control has received anything. */
380 static void dibusb_query_rc (void *data)
381 {
382         struct usb_dibusb *dib = (struct usb_dibusb *) data;
383         /* TODO: need a lock here.  We can simply skip checking for the remote control
384            if we're busy. */
385         dibusb_read_remote_control(dib);
386         schedule_delayed_work(&dib->rc_query_work,
387                               msecs_to_jiffies(RC_QUERY_INTERVAL));
388 }
389
390 /*
391  * Cypress controls
392  */
393
394 #if 0
395 /*
396  * #if 0'ing the following functions as they are not in use _now_,
397  * but probably will be sometime.
398  */
399
400 /*
401  * do not use this, just a workaround for a bug,
402  * which will hopefully never occur :).
403  */
404 static int dibusb_interrupt_read_loop(struct usb_dibusb *dib)
405 {
406         u8 b[1] = { DIBUSB_REQ_INTR_READ };
407         return dibusb_write_usb(dib,b,1);
408 }
409
410 /*
411  * ioctl for power control
412  */
413 static int dibusb_hw_sleep(struct usb_dibusb *dib)
414 {
415         u8 b[1] = { DIBUSB_IOCTL_POWER_SLEEP };
416         return dibusb_ioctl_cmd(dib,DIBUSB_IOCTL_CMD_POWER_MODE, b,1);
417 }
418
419 #endif
420 static int dibusb_write_usb(struct usb_dibusb *dib, u8 *buf, u16 len)
421 {
422         return dibusb_readwrite_usb(dib,buf,len,NULL,0);
423 }
424
425 /*
426  * ioctl for the firmware
427  */
428 static int dibusb_ioctl_cmd(struct usb_dibusb *dib, u8 cmd, u8 *param, int plen)
429 {
430         u8 b[34];
431         int size = plen > 32 ? 32 : plen;
432         b[0] = DIBUSB_REQ_SET_IOCTL;
433         b[1] = cmd;
434         memcpy(&b[2],param,size);
435
436         return dibusb_write_usb(dib,b,2+size);
437 }
438
439 static int dibusb_hw_wakeup(struct usb_dibusb *dib)
440 {
441         u8 b[1] = { DIBUSB_IOCTL_POWER_WAKEUP };
442         return dibusb_ioctl_cmd(dib,DIBUSB_IOCTL_CMD_POWER_MODE, b,1);
443 }
444
445 /*
446  * I2C
447  */
448 static int dibusb_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
449 {
450         struct usb_dibusb *dib = i2c_get_adapdata(adap);
451         int i;
452
453         if (down_interruptible(&dib->i2c_sem) < 0)
454                 return -EAGAIN;
455
456         for (i = 0; i < num; i++) {
457                 /* write/read request */
458                 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
459                         if (dibusb_i2c_msg(dib, msg[i].addr, msg[i].buf,msg[i].len,
460                                                 msg[i+1].buf,msg[i+1].len) < 0)
461                                 break;
462                         i++;
463                 } else
464                         if (dibusb_i2c_msg(dib, msg[i].addr, msg[i].buf,msg[i].len,NULL,0) < 0)
465                                 break;
466         }
467
468         up(&dib->i2c_sem);
469         return i;
470 }
471
472 static u32 dibusb_i2c_func(struct i2c_adapter *adapter)
473 {
474         return I2C_FUNC_I2C;
475 }
476
477 static int thomson_cable_eu_pll_set(struct dvb_frontend* fe, struct
478                 dvb_frontend_parameters* params);
479
480 static struct dib3000_config thomson_cable_eu_config = {
481         .demod_address = 0x10,
482         .pll_addr = 194,
483         .pll_set = thomson_cable_eu_pll_set,
484 };
485
486 static int thomson_cable_eu_pll_set(struct dvb_frontend* fe, struct
487                 dvb_frontend_parameters* params)
488 {
489         struct usb_dibusb* dib = (struct usb_dibusb*) fe->dvb->priv;
490         u8 buf[4];
491         struct i2c_msg msg = {
492                 .addr = thomson_cable_eu_config.pll_addr,
493                 .flags = 0,
494                 .buf = buf,
495                 .len = sizeof(buf)
496         };
497         u32 tfreq = (params->frequency + 36125000) / 62500;
498         int vu,p0,p1,p2;
499
500         if (params->frequency > 403250000)
501                 vu = 1, p2 = 1, p1 = 0, p0 = 1;
502         else if (params->frequency > 115750000)
503                 vu = 0, p2 = 1, p1 = 1, p0 = 0;
504         else if (params->frequency > 44250000)
505                 vu = 0, p2 = 0, p1 = 1, p0 = 1;
506         else
507                 return -EINVAL;
508
509         buf[0] = (tfreq >> 8) & 0x7f;
510         buf[1] = tfreq & 0xff;
511         buf[2] = 0x8e;
512         buf[3] = (vu << 7) | (p2 << 2) | (p1 << 1) | p0;
513
514         if (i2c_transfer (&dib->i2c_adap, &msg, 1) != 1)
515                 return -EIO;
516
517         msleep(1);
518         return 0;
519 }
520
521 static int panasonic_cofdm_env57h1xd5_pll_set(struct dvb_frontend *fe, struct
522                 dvb_frontend_parameters *params);
523
524 static struct dib3000_config panasonic_cofdm_env57h1xd5 = {
525         .demod_address = 0x18,
526         .pll_addr = 192,
527         .pll_set = panasonic_cofdm_env57h1xd5_pll_set,
528 };
529
530 static int panasonic_cofdm_env57h1xd5_pll_set(struct dvb_frontend *fe, struct
531                 dvb_frontend_parameters *params)
532 {
533         struct usb_dibusb* dib = (struct usb_dibusb*) fe->dvb->priv;
534         u8 buf[4];
535         u32 freq = params->frequency;
536         u32 tfreq = (freq + 36125000) / 1000000 * 6 + 1;
537         u8 TA, T210, R210, ctrl1, cp210, p4321;
538         struct i2c_msg msg = {
539                 .addr = panasonic_cofdm_env57h1xd5.pll_addr,
540                 .flags = 0,
541                 .buf = buf,
542                 .len = sizeof(buf)
543         };
544
545         if (freq > 858000000) {
546                 err("frequency cannot be larger than 858 MHz.");
547                 return -EINVAL;
548         }
549
550         // contol data 1 : 1 | T/A=1 | T2,T1,T0 = 0,0,0 | R2,R1,R0 = 0,1,0
551         TA = 1;
552         T210 = 0;
553         R210 = 0x2;
554         ctrl1 = (1 << 7) | (TA << 6) | (T210 << 3) | R210;
555
556 // ********    CHARGE PUMP CONFIG vs RF FREQUENCIES     *****************
557         if (freq < 470000000)
558                 cp210 = 2;  // VHF Low and High band ch E12 to E4 to E12
559         else if (freq < 526000000)
560                 cp210 = 4;  // UHF band Ch E21 to E27
561         else // if (freq < 862000000)
562                 cp210 = 5;  // UHF band ch E28 to E69
563
564 //*********************    BW select  *******************************
565         if (freq < 153000000)
566                 p4321  = 1; // BW selected for VHF low
567         else if (freq < 470000000)
568                 p4321  = 2; // BW selected for VHF high E5 to E12
569         else // if (freq < 862000000)
570                 p4321  = 4; // BW selection for UHF E21 to E69
571
572         buf[0] = (tfreq >> 8) & 0xff;
573         buf[1] = (tfreq >> 0) & 0xff;
574         buf[2] = 0xff & ctrl1;
575         buf[3] =  (cp210 << 5) | (p4321);
576
577         if (i2c_transfer (&dib->i2c_adap, &msg, 1) != 1)
578                 return -EIO;
579
580         msleep(1);
581         return 0;
582 }
583
584 static struct i2c_algorithm dibusb_algo = {
585         .name                   = "DiBcom USB i2c algorithm",
586         .id                             = I2C_ALGO_BIT,
587         .master_xfer    = dibusb_i2c_xfer,
588         .functionality  = dibusb_i2c_func,
589 };
590
591 static void frontend_init(struct usb_dibusb* dib)
592 {
593         switch (dib->dibdev->parm->type) {
594                 case DIBUSB1_1:
595                 case DIBUSB1_1_AN2235:
596         dib->fe = dib3000mb_attach(&thomson_cable_eu_config, &dib->i2c_adap,&dib->xfer_ops);
597                         break;
598                 case DIBUSB2_0:
599                         dib->fe = dib3000mc_attach(&panasonic_cofdm_env57h1xd5,&dib->i2c_adap, &dib->xfer_ops);
600                         break;
601         }
602
603         if (dib->fe == NULL) {
604                 printk("dvb-dibusb: A frontend driver was not found for device %04x/%04x\n",
605                        dib->udev->descriptor.idVendor,
606                        dib->udev->descriptor.idProduct);
607         } else {
608                 if (dvb_register_frontend(dib->adapter, dib->fe)) {
609                         printk("dvb-dibusb: Frontend registration failed!\n");
610                         if (dib->fe->ops->release)
611                                 dib->fe->ops->release(dib->fe);
612                         dib->fe = NULL;
613                 }
614         }
615 }
616
617 static int dibusb_dvb_init(struct usb_dibusb *dib)
618 {
619         int ret;
620
621 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,4)
622     if ((ret = dvb_register_adapter(&dib->adapter, DRIVER_DESC)) < 0) {
623 #else
624     if ((ret = dvb_register_adapter(&dib->adapter, DRIVER_DESC ,
625                         THIS_MODULE)) < 0) {
626 #endif
627                 deb_info("dvb_register_adapter failed: error %d", ret);
628                 goto err;
629         }
630         dib->adapter->priv = dib;
631
632         strncpy(dib->i2c_adap.name,dib->dibdev->name,I2C_NAME_SIZE);
633 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
634         dib->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL,
635 #else
636         dib->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
637 #endif
638         dib->i2c_adap.algo              = &dibusb_algo;
639         dib->i2c_adap.algo_data = NULL;
640         dib->i2c_adap.id                = I2C_ALGO_BIT;
641
642         i2c_set_adapdata(&dib->i2c_adap, dib);
643
644         if ((i2c_add_adapter(&dib->i2c_adap) < 0)) {
645                 err("could not add i2c adapter");
646                 goto err_i2c;
647         }
648
649         dib->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
650
651         dib->demux.priv = (void *)dib;
652         /* get pidcount from demod */
653         dib->demux.feednum = dib->demux.filternum = 16;
654         dib->demux.start_feed = dibusb_start_feed;
655         dib->demux.stop_feed = dibusb_stop_feed;
656         dib->demux.write_to_decoder = NULL;
657         if ((ret = dvb_dmx_init(&dib->demux)) < 0) {
658                 err("dvb_dmx_init failed: error %d",ret);
659                 goto err_dmx;
660         }
661
662         dib->dmxdev.filternum = dib->demux.filternum;
663         dib->dmxdev.demux = &dib->demux.dmx;
664         dib->dmxdev.capabilities = 0;
665         if ((ret = dvb_dmxdev_init(&dib->dmxdev, dib->adapter)) < 0) {
666                 err("dvb_dmxdev_init failed: error %d",ret);
667                 goto err_dmx_dev;
668         }
669
670         dvb_net_init(dib->adapter, &dib->dvb_net, &dib->demux.dmx);
671
672         frontend_init(dib);
673
674         /* Start the remote-control polling. */
675         schedule_delayed_work(&dib->rc_query_work, msecs_to_jiffies(RC_QUERY_INTERVAL));
676
677         goto success;
678 err_dmx_dev:
679         dvb_dmx_release(&dib->demux);
680 err_dmx:
681         i2c_del_adapter(&dib->i2c_adap);
682 err_i2c:
683         dvb_unregister_adapter(dib->adapter);
684 err:
685         return ret;
686 success:
687         dib->dvb_is_ready = 1;
688         return 0;
689 }
690
691 static int dibusb_dvb_exit(struct usb_dibusb *dib)
692 {
693         cancel_delayed_work(&dib->rc_query_work);
694         flush_scheduled_work();
695         input_unregister_device(&dib->rc_input_dev);
696
697         dib->dvb_is_ready = 0;
698         deb_info("unregistering DVB part\n");
699         dvb_net_release(&dib->dvb_net);
700         dib->demux.dmx.close(&dib->demux.dmx);
701         dvb_dmxdev_release(&dib->dmxdev);
702         dvb_dmx_release(&dib->demux);
703         if (dib->fe != NULL) dvb_unregister_frontend(dib->fe);
704         i2c_del_adapter(&dib->i2c_adap);
705         dvb_unregister_adapter(dib->adapter);
706
707         return 0;
708 }
709
710 static int dibusb_exit(struct usb_dibusb *dib)
711 {
712         int i;
713         if (dib->urb_list != NULL) {
714                 for (i = 0; i < dib->dibdev->parm->num_urbs; i++) {
715                         if (dib->urb_list[i] != NULL) {
716                         deb_info("killing URB no. %d.\n",i);
717
718                                 /* stop the URBs */
719 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,7)
720                                 usb_unlink_urb(dib->urb_list[i]);
721 #else
722                                 usb_kill_urb(dib->urb_list[i]);
723 #endif
724                         
725                         deb_info("freeing URB no. %d.\n",i);
726                                 /* free the URBs */
727                                 usb_free_urb(dib->urb_list[i]);
728                         }
729                 }
730                 /* free the urb array */
731                 kfree(dib->urb_list);
732                 }
733
734         pci_free_consistent(NULL,
735                 dib->dibdev->parm->urb_buf_size*dib->dibdev->parm->num_urbs,dib->buffer,
736                 dib->dma_handle);
737         return 0;
738 }
739
740 static int dibusb_init(struct usb_dibusb *dib)
741 {
742         int ret,i,bufsize;
743         sema_init(&dib->usb_sem, 1);
744         sema_init(&dib->i2c_sem, 1);
745
746         /*
747          * when reloading the driver w/o replugging the device
748          * a timeout occures, this helps
749          */
750         usb_clear_halt(dib->udev,usb_sndbulkpipe(dib->udev,dib->dibdev->parm->cmd_pipe));
751         usb_clear_halt(dib->udev,usb_rcvbulkpipe(dib->udev,dib->dibdev->parm->result_pipe));
752         usb_clear_halt(dib->udev,usb_rcvbulkpipe(dib->udev,dib->dibdev->parm->data_pipe));
753
754         /* allocate the array for the data transfer URBs */
755         dib->urb_list = kmalloc(dib->dibdev->parm->num_urbs*sizeof(struct urb *),GFP_KERNEL);
756         if (dib->urb_list == NULL)
757                 return -ENOMEM;
758         memset(dib->urb_list,0,dib->dibdev->parm->num_urbs*sizeof(struct urb *));
759
760         bufsize = dib->dibdev->parm->num_urbs*dib->dibdev->parm->urb_buf_size;
761         deb_info("allocate %d bytes as buffersize for all URBs\n",bufsize);
762         /* allocate the actual buffer for the URBs */
763         if ((dib->buffer = pci_alloc_consistent(NULL,bufsize,&dib->dma_handle)) == NULL) {
764                 deb_info("not enough memory.\n");
765                 dibusb_exit(dib);
766                 return -ENOMEM;
767         }
768         deb_info("allocation complete\n");
769         memset(dib->buffer,0,bufsize);
770
771         /* allocate and submit the URBs */
772         for (i = 0; i < dib->dibdev->parm->num_urbs; i++) {
773                 if (!(dib->urb_list[i] = usb_alloc_urb(0,GFP_KERNEL))) {
774                 dibusb_exit(dib);
775                 return -ENOMEM;
776         }
777                 deb_info("submitting URB no. %d\n",i);
778
779                 usb_fill_bulk_urb( dib->urb_list[i], dib->udev,
780                                 usb_rcvbulkpipe(dib->udev,dib->dibdev->parm->data_pipe),
781                                 &dib->buffer[i*dib->dibdev->parm->urb_buf_size],
782                                 dib->dibdev->parm->urb_buf_size,
783                                 dibusb_urb_complete, dib);
784
785                 dib->urb_list[i]->transfer_flags = 0;
786 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,7)
787                 dib->urb_list[i]->timeout = 0;
788 #endif
789
790                 if ((ret = usb_submit_urb(dib->urb_list[i],GFP_KERNEL))) {
791                         err("could not submit buffer urb no. %d\n",i);
792                         dibusb_exit(dib);
793                         return ret;
794                 }
795         }
796
797         dib->dvb_is_ready = 0;
798
799         /* Initialise the remote-control structures.*/
800         init_input_dev(&dib->rc_input_dev);
801
802         dib->rc_input_dev.evbit[0] = BIT(EV_KEY);
803         dib->rc_input_dev.keycodesize = sizeof(unsigned char);
804         dib->rc_input_dev.keycodemax = KEY_MAX;
805         dib->rc_input_dev.name = DRIVER_DESC " remote control";
806
807         for (i=0; i<sizeof(rc_keys)/sizeof(rc_keys[0]); i++)
808                 set_bit(rc_keys[i].key, dib->rc_input_dev.keybit);
809
810         input_register_device(&dib->rc_input_dev);
811
812         dib->rc_input_event = KEY_MAX;
813
814         INIT_WORK(&dib->rc_query_work, dibusb_query_rc, dib);
815
816         dibusb_hw_wakeup(dib);
817
818         if ((ret = dibusb_dvb_init(dib))) {
819                 dibusb_exit(dib);
820                 return ret;
821         }
822         return 0;
823 }
824
825 /*
826  * load a firmware packet to the device
827  */
828 static int dibusb_writemem(struct usb_device *udev,u16 addr,u8 *data, u8 len)
829 {
830         return usb_control_msg(udev, usb_sndctrlpipe(udev,0),
831                         0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5*HZ);
832 }
833
834 static int dibusb_loadfirmware(struct usb_device *udev,
835                 struct dibusb_device *dibdev)
836 {
837         const struct firmware *fw = NULL;
838         const char **fws;
839         u16 addr;
840         u8 *b,*p;
841         int ret = 0,i;
842
843         fws = dibdev->parm->fw_filenames;
844
845         for (i = 0; i < sizeof(fws)/sizeof(const char*); i++) {
846                 if ((ret = request_firmware(&fw, fws[i], &udev->dev)) == 0) {
847                         info("using firmware file (%s).",fws[i]);
848                         break;
849                 }
850                 deb_info("tried to find '%s' firmware - unsuccessful. (%d)\n",
851                                 fws[i],ret);
852         }
853
854         if (fw == NULL) {
855                 err("did not find a valid firmware file. "
856                         "Please see linux/Documentation/dvb/ for more details on firmware-problems.");
857                 return -EINVAL;
858         }
859         p = kmalloc(fw->size,GFP_KERNEL);
860         if (p != NULL) {
861                 u8 reset;
862                 /*
863                  * you cannot use the fw->data as buffer for
864                  * usb_control_msg, a new buffer has to be
865                  * created
866                  */
867                 memcpy(p,fw->data,fw->size);
868
869                 /* stop the CPU */
870                 reset = 1;
871                 if ((ret = dibusb_writemem(udev,dibdev->parm->usb_cpu_csreg,&reset,1)) != 1)
872                         err("could not stop the USB controller CPU.");
873                 for(i = 0; p[i+3] == 0 && i < fw->size; ) {
874                         b = (u8 *) &p[i];
875                         addr = *((u16 *) &b[1]);
876
877                         ret = dibusb_writemem(udev,addr,&b[4],b[0]);
878
879                         if (ret != b[0]) {
880                                 err("error while transferring firmware "
881                                         "(transferred size: %d, block size: %d)",
882                                         ret,b[0]);
883                                 ret = -EINVAL;
884                                 break;
885                         }
886                         i += 5 + b[0];
887                 }
888                 /* length in ret */
889                 if (ret > 0)
890                         ret = 0;
891                 /* restart the CPU */
892                 reset = 0;
893                 if (ret || dibusb_writemem(udev,dibdev->parm->usb_cpu_csreg,&reset,1) != 1) {
894                         err("could not restart the USB controller CPU.");
895                         ret = -EINVAL;
896                 }
897
898                 kfree(p);
899         } else {
900                 ret = -ENOMEM;
901         }
902         release_firmware(fw);
903
904         return ret;
905 }
906
907 /*
908  * USB
909  */
910 static int dibusb_probe(struct usb_interface *intf,
911                 const struct usb_device_id *id)
912 {
913         struct usb_device *udev = interface_to_usbdev(intf);
914         struct usb_dibusb *dib = NULL;
915         struct dibusb_device *dibdev = NULL;
916
917         int ret = -ENOMEM,i,cold=0;
918
919         for (i = 0; i < DIBUSB_SUPPORTED_DEVICES; i++)
920                 if (dibusb_devices[i].cold_product_id == udev->descriptor.idProduct ||
921                         dibusb_devices[i].warm_product_id == udev->descriptor.idProduct) {
922                         dibdev = &dibusb_devices[i];
923
924                         cold = dibdev->cold_product_id == udev->descriptor.idProduct;
925
926                         if (cold)
927                                 info("found a '%s' in cold state, will try to load a firmware",dibdev->name);
928                         else
929                                 info("found a '%s' in warm state.",dibdev->name);
930                 }
931
932         if (dibdev == NULL) {
933                 err("something went very wrong, "
934                                 "unknown product ID: %.4x",udev->descriptor.idProduct);
935                 return -ENODEV;
936         }
937
938         if (cold)
939                 ret = dibusb_loadfirmware(udev,dibdev);
940         else {
941                 dib = kmalloc(sizeof(struct usb_dibusb),GFP_KERNEL);
942                 if (dib == NULL) {
943                         err("no memory");
944                         return ret;
945                 }
946                 memset(dib,0,sizeof(struct usb_dibusb));
947
948                 dib->pid_parse = 1;
949                 switch (udev->speed) {
950                         case USB_SPEED_LOW:
951                                 err("cannot handle USB speed because it is to sLOW.");
952                                 break;
953                         case USB_SPEED_FULL:
954                                 info("running at FULL speed, will use pid parsing.");
955                                 break;
956                         case USB_SPEED_HIGH:
957                                 if (!pid_parse) {
958                                         dib->pid_parse = 0;
959                                 info("running at HIGH speed, will deliver the complete TS.");
960                                 } else
961                                         info("running at HIGH speed, will use pid_parsing anyway.");
962                                 break;
963                         case USB_SPEED_UNKNOWN: /* fall through */
964                         default:
965                                 err("cannot handle USB speed because it is unkown.");
966                                 break;
967                 }
968
969                 dib->udev = udev;
970                 dib->dibdev = dibdev;
971
972                 usb_set_intfdata(intf, dib);
973
974                 ret = dibusb_init(dib);
975         }
976
977         if (ret == 0)
978                 info("%s successfully initialized and connected.",dibdev->name);
979         else
980                 info("%s error while loading driver (%d)",dibdev->name,ret);
981         return ret;
982 }
983
984 static void dibusb_disconnect(struct usb_interface *intf)
985 {
986         struct usb_dibusb *dib = usb_get_intfdata(intf);
987         const char *name = DRIVER_DESC;
988
989         usb_set_intfdata(intf,NULL);
990         if (dib != NULL) {
991                 name = dib->dibdev->name;
992                 dibusb_dvb_exit(dib);
993                 dibusb_exit(dib);
994                 kfree(dib);
995         }
996         info("%s successfully deinitialized and disconnected.",name);
997
998 }
999
1000 /* usb specific object needed to register this driver with the usb subsystem */
1001 static struct usb_driver dibusb_driver = {
1002         .owner          = THIS_MODULE,
1003         .name           = "dvb_dibusb",
1004         .probe          = dibusb_probe,
1005         .disconnect = dibusb_disconnect,
1006         .id_table       = dibusb_table,
1007 };
1008
1009 /* module stuff */
1010 static int __init usb_dibusb_init(void)
1011 {
1012         int result;
1013         if ((result = usb_register(&dibusb_driver))) {
1014                 err("usb_register failed. Error number %d",result);
1015                 return result;
1016         }
1017
1018         return 0;
1019 }
1020
1021 static void __exit usb_dibusb_exit(void)
1022 {
1023         /* deregister this driver from the USB subsystem */
1024         usb_deregister(&dibusb_driver);
1025 }
1026
1027 module_init (usb_dibusb_init);
1028 module_exit (usb_dibusb_exit);
1029
1030 MODULE_AUTHOR(DRIVER_AUTHOR);
1031 MODULE_DESCRIPTION(DRIVER_DESC);
1032 MODULE_LICENSE("GPL");