blob: 8853f4e26092c0aef22812ffd4e007a8a47de599 [file] [log] [blame]
Steven Toth265a6512008-04-18 21:34:00 -03001/*
2 * Driver for the Auvitek USB bridge
3 *
4 * Copyright (c) 2008 Steven Toth <stoth@hauppauge.com>
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/init.h>
24#include <linux/device.h>
25#include <linux/suspend.h>
26#include <media/v4l2-common.h>
27
28#include "au0828.h"
Steven Toth265a6512008-04-18 21:34:00 -030029#include "au8522.h"
30#include "xc5000.h"
31
32DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
33
Steven Toth265a6512008-04-18 21:34:00 -030034static struct au8522_config hauppauge_hvr950q_config = {
35 .demod_address = 0x8e >> 1,
36 .status_mode = AU8522_DEMODLOCKING,
37};
38
39static struct xc5000_config hauppauge_hvr950q_tunerconfig = {
40 .i2c_address = 0x61,
41 .if_khz = 6000,
42 .tuner_callback = au0828_tuner_callback
43};
44
45/*-------------------------------------------------------------------*/
46static void urb_completion(struct urb *purb)
47{
48 u8 *ptr;
49 struct au0828_dev *dev = purb->context;
50 int ptype = usb_pipetype(purb->pipe);
51
Michael Krufkyf07e8e42008-04-18 21:42:30 -030052 dprintk(2, "%s()\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -030053
Steven Toth9c26de552008-04-02 01:10:40 -030054 if (!dev)
55 return;
56
Steven Toth265a6512008-04-18 21:34:00 -030057 if (dev->urb_streaming == 0)
58 return;
59
60 if (ptype != PIPE_BULK) {
Michael Krufkyf07e8e42008-04-18 21:42:30 -030061 printk(KERN_ERR "%s() Unsupported URB type %d\n",
62 __func__, ptype);
Steven Toth265a6512008-04-18 21:34:00 -030063 return;
64 }
65
66 ptr = (u8 *)purb->transfer_buffer;
67
68 /* Feed the transport payload into the kernel demux */
69 dvb_dmx_swfilter_packets(&dev->dvb.demux, purb->transfer_buffer, purb->actual_length / 188);
70
71 /* Clean the buffer before we requeue */
72 memset(purb->transfer_buffer, 0, URB_BUFSIZE);
73
74 /* Requeue URB */
75 usb_submit_urb(purb, GFP_ATOMIC);
76}
77
78static int stop_urb_transfer(struct au0828_dev *dev)
79{
80 int i;
81
Michael Krufkyf07e8e42008-04-18 21:42:30 -030082 dprintk(2, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -030083
84 /* FIXME: Do we need to free the transfer_buffers? */
85 for (i = 0; i < URB_COUNT; i++) {
86 usb_kill_urb(dev->urbs[i]);
87 kfree(dev->urbs[i]->transfer_buffer);
88 usb_free_urb(dev->urbs[i]);
89 }
90
91 dev->urb_streaming = 0;
92
93 return 0;
94}
95
96#define _AU0828_BULKPIPE 0x83
97#define _BULKPIPESIZE 0xe522
98
99static int start_urb_transfer(struct au0828_dev *dev)
100{
101 struct urb *purb;
102 int i, ret = -ENOMEM;
Steven Toth265a6512008-04-18 21:34:00 -0300103
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300104 dprintk(2, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300105
106 if (dev->urb_streaming) {
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300107 dprintk(2, "%s: iso xfer already running!\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300108 return 0;
109 }
110
111 for (i = 0; i < URB_COUNT; i++) {
112
113 dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
114 if (!dev->urbs[i]) {
115 goto err;
116 }
117
118 purb = dev->urbs[i];
119
120 purb->transfer_buffer = kzalloc(URB_BUFSIZE, GFP_KERNEL);
121 if (!purb->transfer_buffer) {
122 usb_free_urb(purb);
123 dev->urbs[i] = 0;
124 goto err;
125 }
126
127 purb->status = -EINPROGRESS;
128 usb_fill_bulk_urb(purb,
129 dev->usbdev,
130 usb_rcvbulkpipe(dev->usbdev, _AU0828_BULKPIPE),
131 purb->transfer_buffer,
132 URB_BUFSIZE,
133 urb_completion,
134 dev);
135
136 }
137
138 for (i = 0; i < URB_COUNT; i++) {
139 ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC);
140 if (ret != 0) {
141 stop_urb_transfer(dev);
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300142 printk(KERN_ERR "%s: failed urb submission, "
143 "err = %d\n", __func__, ret);
Steven Toth265a6512008-04-18 21:34:00 -0300144 return ret;
145 }
146 }
147
148 dev->urb_streaming = 1;
149 ret = 0;
150
151err:
152 return ret;
153}
154
155static int au0828_dvb_start_feed(struct dvb_demux_feed *feed)
156{
157 struct dvb_demux *demux = feed->demux;
158 struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
159 struct au0828_dvb *dvb = &dev->dvb;
160 int ret = 0;
161
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300162 dprintk(1, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300163
164 if (!demux->dmx.frontend)
165 return -EINVAL;
166
Steven Toth265a6512008-04-18 21:34:00 -0300167 if (dvb) {
168 mutex_lock(&dvb->lock);
169 if (dvb->feeding++ == 0) {
Steven Tothbc3c6132008-04-18 21:39:11 -0300170 /* Start transport */
Steven Toth265a6512008-04-18 21:34:00 -0300171 au0828_write(dev, 0x608, 0x90);
172 au0828_write(dev, 0x609, 0x72);
173 au0828_write(dev, 0x60a, 0x71);
174 au0828_write(dev, 0x60b, 0x01);
175 ret = start_urb_transfer(dev);
176 }
177 mutex_unlock(&dvb->lock);
178 }
179
180 return ret;
181}
182
183static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed)
184{
185 struct dvb_demux *demux = feed->demux;
186 struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
187 struct au0828_dvb *dvb = &dev->dvb;
188 int ret = 0;
189
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300190 dprintk(1, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300191
192 if (dvb) {
193 mutex_lock(&dvb->lock);
194 if (--dvb->feeding == 0) {
Steven Tothbc3c6132008-04-18 21:39:11 -0300195 /* Stop transport */
Steven Toth265a6512008-04-18 21:34:00 -0300196 au0828_write(dev, 0x608, 0x00);
197 au0828_write(dev, 0x609, 0x00);
198 au0828_write(dev, 0x60a, 0x00);
199 au0828_write(dev, 0x60b, 0x00);
200 ret = stop_urb_transfer(dev);
201 }
202 mutex_unlock(&dvb->lock);
203 }
204
205 return ret;
206}
207
208int dvb_register(struct au0828_dev *dev)
209{
210 struct au0828_dvb *dvb = &dev->dvb;
211 int result;
212
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300213 dprintk(1, "%s()\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300214
Steven Toth265a6512008-04-18 21:34:00 -0300215 /* register adapter */
216 result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
217 &dev->usbdev->dev, adapter_nr);
218 if (result < 0) {
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300219 printk(KERN_ERR "%s: dvb_register_adapter failed "
220 "(errno = %d)\n", DRIVER_NAME, result);
Steven Toth265a6512008-04-18 21:34:00 -0300221 goto fail_adapter;
222 }
223 dvb->adapter.priv = dev;
224
225 /* register frontend */
226 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
227 if (result < 0) {
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300228 printk(KERN_ERR "%s: dvb_register_frontend failed "
229 "(errno = %d)\n", DRIVER_NAME, result);
Steven Toth265a6512008-04-18 21:34:00 -0300230 goto fail_frontend;
231 }
232
233 /* register demux stuff */
234 dvb->demux.dmx.capabilities =
235 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
236 DMX_MEMORY_BASED_FILTERING;
237 dvb->demux.priv = dev;
238 dvb->demux.filternum = 256;
239 dvb->demux.feednum = 256;
240 dvb->demux.start_feed = au0828_dvb_start_feed;
241 dvb->demux.stop_feed = au0828_dvb_stop_feed;
242 result = dvb_dmx_init(&dvb->demux);
243 if (result < 0) {
Steven Tothbc3c6132008-04-18 21:39:11 -0300244 printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
Steven Toth265a6512008-04-18 21:34:00 -0300245 DRIVER_NAME, result);
246 goto fail_dmx;
247 }
248
249 dvb->dmxdev.filternum = 256;
250 dvb->dmxdev.demux = &dvb->demux.dmx;
251 dvb->dmxdev.capabilities = 0;
252 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
253 if (result < 0) {
Steven Tothbc3c6132008-04-18 21:39:11 -0300254 printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
Steven Toth265a6512008-04-18 21:34:00 -0300255 DRIVER_NAME, result);
256 goto fail_dmxdev;
257 }
258
259 dvb->fe_hw.source = DMX_FRONTEND_0;
260 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
261 if (result < 0) {
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300262 printk(KERN_ERR "%s: add_frontend failed "
263 "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
Steven Toth265a6512008-04-18 21:34:00 -0300264 goto fail_fe_hw;
265 }
266
267 dvb->fe_mem.source = DMX_MEMORY_FE;
268 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
269 if (result < 0) {
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300270 printk(KERN_ERR "%s: add_frontend failed "
271 "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
Steven Toth265a6512008-04-18 21:34:00 -0300272 goto fail_fe_mem;
273 }
274
275 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
276 if (result < 0) {
Steven Tothbc3c6132008-04-18 21:39:11 -0300277 printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
Steven Toth265a6512008-04-18 21:34:00 -0300278 DRIVER_NAME, result);
279 goto fail_fe_conn;
280 }
281
282 /* register network adapter */
283 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
284 return 0;
285
286fail_fe_conn:
287 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
288fail_fe_mem:
289 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
290fail_fe_hw:
291 dvb_dmxdev_release(&dvb->dmxdev);
292fail_dmxdev:
293 dvb_dmx_release(&dvb->demux);
294fail_dmx:
295 dvb_unregister_frontend(dvb->frontend);
296fail_frontend:
297 dvb_frontend_detach(dvb->frontend);
298 dvb_unregister_adapter(&dvb->adapter);
299fail_adapter:
300 return result;
301}
302
303void au0828_dvb_unregister(struct au0828_dev *dev)
304{
305 struct au0828_dvb *dvb = &dev->dvb;
306
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300307 dprintk(1, "%s()\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300308
Steven Toth9c26de552008-04-02 01:10:40 -0300309 if(dvb->frontend == NULL)
310 return;
311
Steven Toth265a6512008-04-18 21:34:00 -0300312 dvb_net_release(&dvb->net);
313 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
314 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
315 dvb_dmxdev_release(&dvb->dmxdev);
316 dvb_dmx_release(&dvb->demux);
317 dvb_unregister_frontend(dvb->frontend);
318 dvb_frontend_detach(dvb->frontend);
319 dvb_unregister_adapter(&dvb->adapter);
320}
321
322/* All the DVB attach calls go here, this function get's modified
323 * for each new card. No other function in this file needs
324 * to change.
325 */
326int au0828_dvb_register(struct au0828_dev *dev)
327{
328 struct au0828_dvb *dvb = &dev->dvb;
329 int ret;
330
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300331 dprintk(1, "%s()\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300332
Steven Toth265a6512008-04-18 21:34:00 -0300333 /* init frontend */
334 switch (dev->board) {
335 case AU0828_BOARD_HAUPPAUGE_HVR850:
336 case AU0828_BOARD_HAUPPAUGE_HVR950Q:
337 case AU0828_BOARD_DVICO_FUSIONHDTV7:
338 dvb->frontend = dvb_attach(au8522_attach,
339 &hauppauge_hvr950q_config,
340 &dev->i2c_adap);
341 if (dvb->frontend != NULL) {
342 hauppauge_hvr950q_tunerconfig.priv = dev;
343 dvb_attach(xc5000_attach, dvb->frontend,
344 &dev->i2c_adap,
345 &hauppauge_hvr950q_tunerconfig);
346 }
347 break;
348 default:
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300349 printk(KERN_WARNING "The frontend of your DVB/ATSC card "
350 "isn't supported yet\n");
Steven Toth265a6512008-04-18 21:34:00 -0300351 break;
352 }
353 if (NULL == dvb->frontend) {
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300354 printk(KERN_ERR "%s() Frontend initialization failed\n",
355 __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300356 return -1;
357 }
358
359 /* Put the analog decoder in standby to keep it quiet */
360 au0828_call_i2c_clients(dev, TUNER_SET_STANDBY, NULL);
361
362 if (dvb->frontend->ops.analog_ops.standby)
363 dvb->frontend->ops.analog_ops.standby(dvb->frontend);
364
365 /* register everything */
366 ret = dvb_register(dev);
367 if (ret < 0) {
368 if (dvb->frontend->ops.release)
369 dvb->frontend->ops.release(dvb->frontend);
370 return ret;
371 }
372
373 return 0;
374}