Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
au0828-dvb.c
Go to the documentation of this file.
1 /*
2  * Driver for the Auvitek USB bridge
3  *
4  * Copyright (c) 2008 Steven Toth <[email protected]>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/device.h>
26 #include <linux/suspend.h>
27 #include <media/v4l2-common.h>
28 #include <media/tuner.h>
29 
30 #include "au0828.h"
31 #include "au8522.h"
32 #include "xc5000.h"
33 #include "mxl5007t.h"
34 #include "tda18271.h"
35 
37 
38 #define _AU0828_BULKPIPE 0x83
39 #define _BULKPIPESIZE 0xe522
40 
41 static u8 hauppauge_hvr950q_led_states[] = {
42  0x00, /* off */
43  0x02, /* yellow */
44  0x04, /* green */
45 };
46 
47 static struct au8522_led_config hauppauge_hvr950q_led_cfg = {
48  .gpio_output = 0x00e0,
49  .gpio_output_enable = 0x6006,
50  .gpio_output_disable = 0x0660,
51 
52  .gpio_leds = 0x00e2,
53  .led_states = hauppauge_hvr950q_led_states,
54  .num_led_states = sizeof(hauppauge_hvr950q_led_states),
55 
56  .vsb8_strong = 20 /* dB */ * 10,
57  .qam64_strong = 25 /* dB */ * 10,
58  .qam256_strong = 32 /* dB */ * 10,
59 };
60 
61 static struct au8522_config hauppauge_hvr950q_config = {
62  .demod_address = 0x8e >> 1,
63  .status_mode = AU8522_DEMODLOCKING,
64  .qam_if = AU8522_IF_6MHZ,
65  .vsb_if = AU8522_IF_6MHZ,
66  .led_cfg = &hauppauge_hvr950q_led_cfg,
67 };
68 
69 static struct au8522_config fusionhdtv7usb_config = {
70  .demod_address = 0x8e >> 1,
71  .status_mode = AU8522_DEMODLOCKING,
72  .qam_if = AU8522_IF_6MHZ,
73  .vsb_if = AU8522_IF_6MHZ,
74 };
75 
76 static struct au8522_config hauppauge_woodbury_config = {
77  .demod_address = 0x8e >> 1,
78  .status_mode = AU8522_DEMODLOCKING,
79  .qam_if = AU8522_IF_4MHZ,
80  .vsb_if = AU8522_IF_3_25MHZ,
81 };
82 
83 static struct xc5000_config hauppauge_xc5000a_config = {
84  .i2c_address = 0x61,
85  .if_khz = 6000,
86  .chip_id = XC5000A,
87 };
88 
89 static struct xc5000_config hauppauge_xc5000c_config = {
90  .i2c_address = 0x61,
91  .if_khz = 6000,
92  .chip_id = XC5000C,
93 };
94 
95 static struct mxl5007t_config mxl5007t_hvr950q_config = {
96  .xtal_freq_hz = MxL_XTAL_24_MHZ,
97  .if_freq_hz = MxL_IF_6_MHZ,
98 };
99 
100 static struct tda18271_config hauppauge_woodbury_tunerconfig = {
101  .gate = TDA18271_GATE_DIGITAL,
102 };
103 
104 static void au0828_restart_dvb_streaming(struct work_struct *work);
105 
106 /*-------------------------------------------------------------------*/
107 static void urb_completion(struct urb *purb)
108 {
109  struct au0828_dev *dev = purb->context;
110  int ptype = usb_pipetype(purb->pipe);
111  unsigned char *ptr;
112 
113  dprintk(2, "%s()\n", __func__);
114 
115  if (!dev)
116  return;
117 
118  if (dev->urb_streaming == 0)
119  return;
120 
121  if (ptype != PIPE_BULK) {
122  printk(KERN_ERR "%s() Unsupported URB type %d\n",
123  __func__, ptype);
124  return;
125  }
126 
127  /* See if the stream is corrupted (to work around a hardware
128  bug where the stream gets misaligned */
129  ptr = purb->transfer_buffer;
130  if (purb->actual_length > 0 && ptr[0] != 0x47) {
131  dprintk(1, "Need to restart streaming %02x len=%d!\n",
132  ptr[0], purb->actual_length);
134  return;
135  }
136 
137  /* Feed the transport payload into the kernel demux */
138  dvb_dmx_swfilter_packets(&dev->dvb.demux,
139  purb->transfer_buffer, purb->actual_length / 188);
140 
141  /* Clean the buffer before we requeue */
142  memset(purb->transfer_buffer, 0, URB_BUFSIZE);
143 
144  /* Requeue URB */
145  usb_submit_urb(purb, GFP_ATOMIC);
146 }
147 
148 static int stop_urb_transfer(struct au0828_dev *dev)
149 {
150  int i;
151 
152  dprintk(2, "%s()\n", __func__);
153 
154  dev->urb_streaming = 0;
155  for (i = 0; i < URB_COUNT; i++) {
156  usb_kill_urb(dev->urbs[i]);
157  kfree(dev->urbs[i]->transfer_buffer);
158  usb_free_urb(dev->urbs[i]);
159  }
160 
161  return 0;
162 }
163 
164 static int start_urb_transfer(struct au0828_dev *dev)
165 {
166  struct urb *purb;
167  int i, ret = -ENOMEM;
168 
169  dprintk(2, "%s()\n", __func__);
170 
171  if (dev->urb_streaming) {
172  dprintk(2, "%s: bulk xfer already running!\n", __func__);
173  return 0;
174  }
175 
176  for (i = 0; i < URB_COUNT; i++) {
177 
178  dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
179  if (!dev->urbs[i])
180  goto err;
181 
182  purb = dev->urbs[i];
183 
184  purb->transfer_buffer = kzalloc(URB_BUFSIZE, GFP_KERNEL);
185  if (!purb->transfer_buffer) {
186  usb_free_urb(purb);
187  dev->urbs[i] = NULL;
188  goto err;
189  }
190 
191  purb->status = -EINPROGRESS;
192  usb_fill_bulk_urb(purb,
193  dev->usbdev,
194  usb_rcvbulkpipe(dev->usbdev,
196  purb->transfer_buffer,
197  URB_BUFSIZE,
198  urb_completion,
199  dev);
200 
201  }
202 
203  for (i = 0; i < URB_COUNT; i++) {
204  ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC);
205  if (ret != 0) {
206  stop_urb_transfer(dev);
207  printk(KERN_ERR "%s: failed urb submission, "
208  "err = %d\n", __func__, ret);
209  return ret;
210  }
211  }
212 
213  dev->urb_streaming = 1;
214  ret = 0;
215 
216 err:
217  return ret;
218 }
219 
220 static int au0828_dvb_start_feed(struct dvb_demux_feed *feed)
221 {
222  struct dvb_demux *demux = feed->demux;
223  struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
224  struct au0828_dvb *dvb = &dev->dvb;
225  int ret = 0;
226 
227  dprintk(1, "%s()\n", __func__);
228 
229  if (!demux->dmx.frontend)
230  return -EINVAL;
231 
232  if (dvb) {
233  mutex_lock(&dvb->lock);
234  if (dvb->feeding++ == 0) {
235  /* Start transport */
236  au0828_write(dev, 0x608, 0x90);
237  au0828_write(dev, 0x609, 0x72);
238  au0828_write(dev, 0x60a, 0x71);
239  au0828_write(dev, 0x60b, 0x01);
240  ret = start_urb_transfer(dev);
241  }
242  mutex_unlock(&dvb->lock);
243  }
244 
245  return ret;
246 }
247 
248 static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed)
249 {
250  struct dvb_demux *demux = feed->demux;
251  struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
252  struct au0828_dvb *dvb = &dev->dvb;
253  int ret = 0;
254 
255  dprintk(1, "%s()\n", __func__);
256 
257  if (dvb) {
258  mutex_lock(&dvb->lock);
259  if (--dvb->feeding == 0) {
260  /* Stop transport */
261  ret = stop_urb_transfer(dev);
262  au0828_write(dev, 0x60b, 0x00);
263  }
264  mutex_unlock(&dvb->lock);
265  }
266 
267  return ret;
268 }
269 
270 static void au0828_restart_dvb_streaming(struct work_struct *work)
271 {
272  struct au0828_dev *dev = container_of(work, struct au0828_dev,
274  struct au0828_dvb *dvb = &dev->dvb;
275  int ret;
276 
277  if (dev->urb_streaming == 0)
278  return;
279 
280  dprintk(1, "Restarting streaming...!\n");
281 
282  mutex_lock(&dvb->lock);
283 
284  /* Stop transport */
285  ret = stop_urb_transfer(dev);
286  au0828_write(dev, 0x608, 0x00);
287  au0828_write(dev, 0x609, 0x00);
288  au0828_write(dev, 0x60a, 0x00);
289  au0828_write(dev, 0x60b, 0x00);
290 
291  /* Start transport */
292  au0828_write(dev, 0x608, 0x90);
293  au0828_write(dev, 0x609, 0x72);
294  au0828_write(dev, 0x60a, 0x71);
295  au0828_write(dev, 0x60b, 0x01);
296  ret = start_urb_transfer(dev);
297 
298  mutex_unlock(&dvb->lock);
299 }
300 
301 static int dvb_register(struct au0828_dev *dev)
302 {
303  struct au0828_dvb *dvb = &dev->dvb;
304  int result;
305 
306  dprintk(1, "%s()\n", __func__);
307 
308  INIT_WORK(&dev->restart_streaming, au0828_restart_dvb_streaming);
309 
310  /* register adapter */
312  &dev->usbdev->dev, adapter_nr);
313  if (result < 0) {
314  printk(KERN_ERR "%s: dvb_register_adapter failed "
315  "(errno = %d)\n", DRIVER_NAME, result);
316  goto fail_adapter;
317  }
318  dvb->adapter.priv = dev;
319 
320  /* register frontend */
321  result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
322  if (result < 0) {
323  printk(KERN_ERR "%s: dvb_register_frontend failed "
324  "(errno = %d)\n", DRIVER_NAME, result);
325  goto fail_frontend;
326  }
327 
328  /* register demux stuff */
329  dvb->demux.dmx.capabilities =
332  dvb->demux.priv = dev;
333  dvb->demux.filternum = 256;
334  dvb->demux.feednum = 256;
335  dvb->demux.start_feed = au0828_dvb_start_feed;
336  dvb->demux.stop_feed = au0828_dvb_stop_feed;
337  result = dvb_dmx_init(&dvb->demux);
338  if (result < 0) {
339  printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
340  DRIVER_NAME, result);
341  goto fail_dmx;
342  }
343 
344  dvb->dmxdev.filternum = 256;
345  dvb->dmxdev.demux = &dvb->demux.dmx;
346  dvb->dmxdev.capabilities = 0;
347  result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
348  if (result < 0) {
349  printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
350  DRIVER_NAME, result);
351  goto fail_dmxdev;
352  }
353 
354  dvb->fe_hw.source = DMX_FRONTEND_0;
355  result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
356  if (result < 0) {
357  printk(KERN_ERR "%s: add_frontend failed "
358  "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
359  goto fail_fe_hw;
360  }
361 
362  dvb->fe_mem.source = DMX_MEMORY_FE;
363  result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
364  if (result < 0) {
365  printk(KERN_ERR "%s: add_frontend failed "
366  "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
367  goto fail_fe_mem;
368  }
369 
370  result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
371  if (result < 0) {
372  printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
373  DRIVER_NAME, result);
374  goto fail_fe_conn;
375  }
376 
377  /* register network adapter */
378  dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
379  return 0;
380 
381 fail_fe_conn:
382  dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
383 fail_fe_mem:
384  dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
385 fail_fe_hw:
386  dvb_dmxdev_release(&dvb->dmxdev);
387 fail_dmxdev:
388  dvb_dmx_release(&dvb->demux);
389 fail_dmx:
391 fail_frontend:
394 fail_adapter:
395  return result;
396 }
397 
399 {
400  struct au0828_dvb *dvb = &dev->dvb;
401 
402  dprintk(1, "%s()\n", __func__);
403 
404  if (dvb->frontend == NULL)
405  return;
406 
407  dvb_net_release(&dvb->net);
408  dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
409  dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
410  dvb_dmxdev_release(&dvb->dmxdev);
411  dvb_dmx_release(&dvb->demux);
415 }
416 
417 /* All the DVB attach calls go here, this function get's modified
418  * for each new card. No other function in this file needs
419  * to change.
420  */
422 {
423  struct au0828_dvb *dvb = &dev->dvb;
424  int ret;
425 
426  dprintk(1, "%s()\n", __func__);
427 
428  /* init frontend */
429  switch (dev->boardnr) {
433  &hauppauge_hvr950q_config,
434  &dev->i2c_adap);
435  if (dvb->frontend != NULL)
436  switch (dev->board.tuner_type) {
437  default:
438  case TUNER_XC5000:
440  &dev->i2c_adap,
441  &hauppauge_xc5000a_config);
442  break;
443  case TUNER_XC5000C:
445  &dev->i2c_adap,
446  &hauppauge_xc5000c_config);
447  break;
448  }
449  break;
452  &hauppauge_hvr950q_config,
453  &dev->i2c_adap);
454  if (dvb->frontend != NULL)
456  &dev->i2c_adap, 0x60,
457  &mxl5007t_hvr950q_config);
458  break;
461  &hauppauge_woodbury_config,
462  &dev->i2c_adap);
463  if (dvb->frontend != NULL)
465  0x60, &dev->i2c_adap,
466  &hauppauge_woodbury_tunerconfig);
467  break;
470  &fusionhdtv7usb_config,
471  &dev->i2c_adap);
472  if (dvb->frontend != NULL) {
474  &dev->i2c_adap,
475  &hauppauge_xc5000a_config);
476  }
477  break;
478  default:
479  printk(KERN_WARNING "The frontend of your DVB/ATSC card "
480  "isn't supported yet\n");
481  break;
482  }
483  if (NULL == dvb->frontend) {
484  printk(KERN_ERR "%s() Frontend initialization failed\n",
485  __func__);
486  return -1;
487  }
488  /* define general-purpose callback pointer */
489  dvb->frontend->callback = au0828_tuner_callback;
490 
491  /* register everything */
492  ret = dvb_register(dev);
493  if (ret < 0) {
494  if (dvb->frontend->ops.release)
495  dvb->frontend->ops.release(dvb->frontend);
496  return ret;
497  }
498 
499  return 0;
500 }