2 * Driver for mobile USB Budget DVB-T devices based on reference
3 * design made by DiBcom (http://www.dibcom.fr/)
7 * Copyright (C) 2004 Patrick Boettcher (patrick.boettcher@desy.de)
9 * based on GPL code from DiBcom, which has
10 * Copyright (C) 2004 Amaury Demol for DiBcom (ademol@dibcom.fr)
12 * Remote control code added by David Matthews (dm@prolingua.co.uk)
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.
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.
23 * see Documentation/dvb/README.dibusb for more information
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>
36 #include "dvb_demux.h"
37 #include "dvb_filter.h"
39 #include "dvb_frontend.h"
42 #include "dvb-dibusb.h"
46 #ifdef CONFIG_DVB_DIBCOM_DEBUG
47 #define dprintk(level,args...) \
48 do { if ((debug & level)) { printk(args); } } while (0)
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]); \
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)).");
60 #define dprintk(args...)
61 #define debug_dump(b,l)
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)
72 module_param(pid_parse, int, 0x644);
73 MODULE_PARM_DESC(pid_parse, "enable pid parsing (filtering) when running at USB2.0");
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"
80 static int dibusb_readwrite_usb(struct usb_dibusb *dib,
81 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
83 int actlen,ret = -ENOMEM;
85 if (wbuf == NULL || wlen == 0)
88 if ((ret = down_interruptible(&dib->usb_sem)))
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]);
97 debug_dump(wbuf,wlen);
99 ret = usb_bulk_msg(dib->udev,usb_sndbulkpipe(dib->udev,
100 dib->dibdev->parm->cmd_pipe), wbuf,wlen,&actlen,
104 err("bulk message failed: %d (%d/%d)",ret,wlen,actlen);
106 ret = actlen != wlen ? -1 : 0;
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,
115 err("recv bulk message failed: %d",ret);
117 deb_alot("rlen: %d\n",rlen);
118 debug_dump(rbuf,actlen);
126 static int dibusb_i2c_msg(struct usb_dibusb *dib, u8 addr,
127 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
129 u8 sndbuf[wlen+4]; /* lead(1) devaddr,direction(1) addr(2) data(wlen) (len(2) (when reading)) */
131 int wo = (rbuf == NULL || rlen == 0),
132 len = 2 + wlen + (wo ? 0 : 2);
134 deb_alot("wo: %d, wlen: %d, len: %d\n",wo,wlen,len);
136 sndbuf[0] = wo ? DIBUSB_REQ_I2C_WRITE : DIBUSB_REQ_I2C_READ;
137 sndbuf[1] = (addr & 0xfe) | (wo ? 0 : 1);
139 memcpy(&sndbuf[2],wbuf,wlen);
142 sndbuf[wlen+2] = (rlen >> 8) & 0xff;
143 sndbuf[wlen+3] = rlen & 0xff;
146 return dibusb_readwrite_usb(dib,sndbuf,len,rbuf,rlen);
152 static void dibusb_urb_complete(struct urb *urb, struct pt_regs *ptregs)
154 struct usb_dibusb *dib = urb->context;
156 deb_ts("urb complete feedcount: %d, status: %d\n",dib->feedcount,urb->status);
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);
163 /* Francois recommends to drop not full-filled packets, even if they may
164 * contain valid TS packets
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);
169 deb_ts("URB dropped because of the "
170 "actual_length or !dvb_is_ready (%d).\n",dib->dvb_is_ready);
172 deb_ts("URB dropped because of feedcount or status.\n");
174 usb_submit_urb(urb,GFP_KERNEL);
177 static int dibusb_ctrl_feed(struct usb_dibusb *dib, int pid, int onoff)
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.");
187 err("fifo_ctrl is not set.");
192 dib->feedcount += onoff ? 1 : -1;
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.");
201 err("no pid ctrl callback.");
206 * start the feed, either if there is the firmware bug or
207 * if this was the first pid to set.
209 if (dib->dibdev->parm->firmware_bug || dib->feedcount == onoff) {
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");
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.");
225 err("fifo_ctrl is not set.");
232 static int dibusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
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);
240 static int dibusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
242 struct usb_dibusb *dib = (struct usb_dibusb *) dvbdmxfeed->priv;
244 err("dib in dmxfeed->priv was NULL");
247 deb_ts("dvbdmxfeed pid: 0x%04x, feedtype: %d\n",
248 dvbdmxfeed->pid, dvbdmxfeed->type);
249 return dibusb_ctrl_feed(dib,dvbdmxfeed->pid,0);
252 /* Table to map raw key codes to key events. This should not be hard-wired
254 static const struct { u8 c0, c1, c2; uint32_t key; } rc_keys [] =
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 },
325 * Read the remote control and feed the appropriate event.
326 * NEC protocol is used for remote controls
328 static int dibusb_read_remote_control(struct usb_dibusb *dib)
330 u8 b[1] = { DIBUSB_REQ_POLL_REMOTE }, rb[5];
333 if ((ret = dibusb_readwrite_usb(dib,b,1,rb,5)))
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]);
342 if ((0xff - rb[3]) != rb[4]) {
343 deb_rc("remote control checksum failed.\n");
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);
362 case DIBUSB_RC_NEC_EMPTY: /* No (more) remote control keys. */
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");
376 #define RC_QUERY_INTERVAL (100) /* milliseconds */
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)
382 struct usb_dibusb *dib = (struct usb_dibusb *) data;
383 /* TODO: need a lock here. We can simply skip checking for the remote control
385 dibusb_read_remote_control(dib);
386 schedule_delayed_work(&dib->rc_query_work,
387 msecs_to_jiffies(RC_QUERY_INTERVAL));
396 * #if 0'ing the following functions as they are not in use _now_,
397 * but probably will be sometime.
401 * do not use this, just a workaround for a bug,
402 * which will hopefully never occur :).
404 static int dibusb_interrupt_read_loop(struct usb_dibusb *dib)
406 u8 b[1] = { DIBUSB_REQ_INTR_READ };
407 return dibusb_write_usb(dib,b,1);
411 * ioctl for power control
413 static int dibusb_hw_sleep(struct usb_dibusb *dib)
415 u8 b[1] = { DIBUSB_IOCTL_POWER_SLEEP };
416 return dibusb_ioctl_cmd(dib,DIBUSB_IOCTL_CMD_POWER_MODE, b,1);
420 static int dibusb_write_usb(struct usb_dibusb *dib, u8 *buf, u16 len)
422 return dibusb_readwrite_usb(dib,buf,len,NULL,0);
426 * ioctl for the firmware
428 static int dibusb_ioctl_cmd(struct usb_dibusb *dib, u8 cmd, u8 *param, int plen)
431 int size = plen > 32 ? 32 : plen;
432 b[0] = DIBUSB_REQ_SET_IOCTL;
434 memcpy(&b[2],param,size);
436 return dibusb_write_usb(dib,b,2+size);
439 static int dibusb_hw_wakeup(struct usb_dibusb *dib)
441 u8 b[1] = { DIBUSB_IOCTL_POWER_WAKEUP };
442 return dibusb_ioctl_cmd(dib,DIBUSB_IOCTL_CMD_POWER_MODE, b,1);
448 static int dibusb_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
450 struct usb_dibusb *dib = i2c_get_adapdata(adap);
453 if (down_interruptible(&dib->i2c_sem) < 0)
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)
464 if (dibusb_i2c_msg(dib, msg[i].addr, msg[i].buf,msg[i].len,NULL,0) < 0)
472 static u32 dibusb_i2c_func(struct i2c_adapter *adapter)
477 static int thomson_cable_eu_pll_set(struct dvb_frontend* fe, struct
478 dvb_frontend_parameters* params);
480 static struct dib3000_config thomson_cable_eu_config = {
481 .demod_address = 0x10,
483 .pll_set = thomson_cable_eu_pll_set,
486 static int thomson_cable_eu_pll_set(struct dvb_frontend* fe, struct
487 dvb_frontend_parameters* params)
489 struct usb_dibusb* dib = (struct usb_dibusb*) fe->dvb->priv;
491 struct i2c_msg msg = {
492 .addr = thomson_cable_eu_config.pll_addr,
497 u32 tfreq = (params->frequency + 36125000) / 62500;
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;
509 buf[0] = (tfreq >> 8) & 0x7f;
510 buf[1] = tfreq & 0xff;
512 buf[3] = (vu << 7) | (p2 << 2) | (p1 << 1) | p0;
514 if (i2c_transfer (&dib->i2c_adap, &msg, 1) != 1)
521 static int panasonic_cofdm_env57h1xd5_pll_set(struct dvb_frontend *fe, struct
522 dvb_frontend_parameters *params);
524 static struct dib3000_config panasonic_cofdm_env57h1xd5 = {
525 .demod_address = 0x18,
527 .pll_set = panasonic_cofdm_env57h1xd5_pll_set,
530 static int panasonic_cofdm_env57h1xd5_pll_set(struct dvb_frontend *fe, struct
531 dvb_frontend_parameters *params)
533 struct usb_dibusb* dib = (struct usb_dibusb*) fe->dvb->priv;
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,
545 if (freq > 858000000) {
546 err("frequency cannot be larger than 858 MHz.");
550 // contol data 1 : 1 | T/A=1 | T2,T1,T0 = 0,0,0 | R2,R1,R0 = 0,1,0
554 ctrl1 = (1 << 7) | (TA << 6) | (T210 << 3) | R210;
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
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
572 buf[0] = (tfreq >> 8) & 0xff;
573 buf[1] = (tfreq >> 0) & 0xff;
574 buf[2] = 0xff & ctrl1;
575 buf[3] = (cp210 << 5) | (p4321);
577 if (i2c_transfer (&dib->i2c_adap, &msg, 1) != 1)
584 static struct i2c_algorithm dibusb_algo = {
585 .name = "DiBcom USB i2c algorithm",
587 .master_xfer = dibusb_i2c_xfer,
588 .functionality = dibusb_i2c_func,
591 static void frontend_init(struct usb_dibusb* dib)
593 switch (dib->dibdev->parm->type) {
595 case DIBUSB1_1_AN2235:
596 dib->fe = dib3000mb_attach(&thomson_cable_eu_config, &dib->i2c_adap,&dib->xfer_ops);
599 dib->fe = dib3000mc_attach(&panasonic_cofdm_env57h1xd5,&dib->i2c_adap, &dib->xfer_ops);
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);
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);
617 static int dibusb_dvb_init(struct usb_dibusb *dib)
621 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,4)
622 if ((ret = dvb_register_adapter(&dib->adapter, DRIVER_DESC)) < 0) {
624 if ((ret = dvb_register_adapter(&dib->adapter, DRIVER_DESC ,
627 deb_info("dvb_register_adapter failed: error %d", ret);
630 dib->adapter->priv = dib;
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,
636 dib->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
638 dib->i2c_adap.algo = &dibusb_algo;
639 dib->i2c_adap.algo_data = NULL;
640 dib->i2c_adap.id = I2C_ALGO_BIT;
642 i2c_set_adapdata(&dib->i2c_adap, dib);
644 if ((i2c_add_adapter(&dib->i2c_adap) < 0)) {
645 err("could not add i2c adapter");
649 dib->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
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);
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);
670 dvb_net_init(dib->adapter, &dib->dvb_net, &dib->demux.dmx);
674 /* Start the remote-control polling. */
675 schedule_delayed_work(&dib->rc_query_work, msecs_to_jiffies(RC_QUERY_INTERVAL));
679 dvb_dmx_release(&dib->demux);
681 i2c_del_adapter(&dib->i2c_adap);
683 dvb_unregister_adapter(dib->adapter);
687 dib->dvb_is_ready = 1;
691 static int dibusb_dvb_exit(struct usb_dibusb *dib)
693 cancel_delayed_work(&dib->rc_query_work);
694 flush_scheduled_work();
695 input_unregister_device(&dib->rc_input_dev);
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);
710 static int dibusb_exit(struct usb_dibusb *dib)
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);
719 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,7)
720 usb_unlink_urb(dib->urb_list[i]);
722 usb_kill_urb(dib->urb_list[i]);
725 deb_info("freeing URB no. %d.\n",i);
727 usb_free_urb(dib->urb_list[i]);
730 /* free the urb array */
731 kfree(dib->urb_list);
734 pci_free_consistent(NULL,
735 dib->dibdev->parm->urb_buf_size*dib->dibdev->parm->num_urbs,dib->buffer,
740 static int dibusb_init(struct usb_dibusb *dib)
743 sema_init(&dib->usb_sem, 1);
744 sema_init(&dib->i2c_sem, 1);
747 * when reloading the driver w/o replugging the device
748 * a timeout occures, this helps
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));
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)
758 memset(dib->urb_list,0,dib->dibdev->parm->num_urbs*sizeof(struct urb *));
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");
768 deb_info("allocation complete\n");
769 memset(dib->buffer,0,bufsize);
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))) {
777 deb_info("submitting URB no. %d\n",i);
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);
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;
790 if ((ret = usb_submit_urb(dib->urb_list[i],GFP_KERNEL))) {
791 err("could not submit buffer urb no. %d\n",i);
797 dib->dvb_is_ready = 0;
799 /* Initialise the remote-control structures.*/
800 init_input_dev(&dib->rc_input_dev);
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";
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);
810 input_register_device(&dib->rc_input_dev);
812 dib->rc_input_event = KEY_MAX;
814 INIT_WORK(&dib->rc_query_work, dibusb_query_rc, dib);
816 dibusb_hw_wakeup(dib);
818 if ((ret = dibusb_dvb_init(dib))) {
826 * load a firmware packet to the device
828 static int dibusb_writemem(struct usb_device *udev,u16 addr,u8 *data, u8 len)
830 return usb_control_msg(udev, usb_sndctrlpipe(udev,0),
831 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5*HZ);
834 static int dibusb_loadfirmware(struct usb_device *udev,
835 struct dibusb_device *dibdev)
837 const struct firmware *fw = NULL;
843 fws = dibdev->parm->fw_filenames;
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]);
850 deb_info("tried to find '%s' firmware - unsuccessful. (%d)\n",
855 err("did not find a valid firmware file. "
856 "Please see linux/Documentation/dvb/ for more details on firmware-problems.");
859 p = kmalloc(fw->size,GFP_KERNEL);
863 * you cannot use the fw->data as buffer for
864 * usb_control_msg, a new buffer has to be
867 memcpy(p,fw->data,fw->size);
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; ) {
875 addr = *((u16 *) &b[1]);
877 ret = dibusb_writemem(udev,addr,&b[4],b[0]);
880 err("error while transferring firmware "
881 "(transferred size: %d, block size: %d)",
891 /* restart the CPU */
893 if (ret || dibusb_writemem(udev,dibdev->parm->usb_cpu_csreg,&reset,1) != 1) {
894 err("could not restart the USB controller CPU.");
902 release_firmware(fw);
910 static int dibusb_probe(struct usb_interface *intf,
911 const struct usb_device_id *id)
913 struct usb_device *udev = interface_to_usbdev(intf);
914 struct usb_dibusb *dib = NULL;
915 struct dibusb_device *dibdev = NULL;
917 int ret = -ENOMEM,i,cold=0;
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];
924 cold = dibdev->cold_product_id == udev->descriptor.idProduct;
927 info("found a '%s' in cold state, will try to load a firmware",dibdev->name);
929 info("found a '%s' in warm state.",dibdev->name);
932 if (dibdev == NULL) {
933 err("something went very wrong, "
934 "unknown product ID: %.4x",udev->descriptor.idProduct);
939 ret = dibusb_loadfirmware(udev,dibdev);
941 dib = kmalloc(sizeof(struct usb_dibusb),GFP_KERNEL);
946 memset(dib,0,sizeof(struct usb_dibusb));
949 switch (udev->speed) {
951 err("cannot handle USB speed because it is to sLOW.");
954 info("running at FULL speed, will use pid parsing.");
959 info("running at HIGH speed, will deliver the complete TS.");
961 info("running at HIGH speed, will use pid_parsing anyway.");
963 case USB_SPEED_UNKNOWN: /* fall through */
965 err("cannot handle USB speed because it is unkown.");
970 dib->dibdev = dibdev;
972 usb_set_intfdata(intf, dib);
974 ret = dibusb_init(dib);
978 info("%s successfully initialized and connected.",dibdev->name);
980 info("%s error while loading driver (%d)",dibdev->name,ret);
984 static void dibusb_disconnect(struct usb_interface *intf)
986 struct usb_dibusb *dib = usb_get_intfdata(intf);
987 const char *name = DRIVER_DESC;
989 usb_set_intfdata(intf,NULL);
991 name = dib->dibdev->name;
992 dibusb_dvb_exit(dib);
996 info("%s successfully deinitialized and disconnected.",name);
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,
1010 static int __init usb_dibusb_init(void)
1013 if ((result = usb_register(&dibusb_driver))) {
1014 err("usb_register failed. Error number %d",result);
1021 static void __exit usb_dibusb_exit(void)
1023 /* deregister this driver from the USB subsystem */
1024 usb_deregister(&dibusb_driver);
1027 module_init (usb_dibusb_init);
1028 module_exit (usb_dibusb_exit);
1030 MODULE_AUTHOR(DRIVER_AUTHOR);
1031 MODULE_DESCRIPTION(DRIVER_DESC);
1032 MODULE_LICENSE("GPL");