Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 1 | /* |
| 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 Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 29 | #include "au8522.h" |
| 30 | #include "xc5000.h" |
| 31 | |
| 32 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 33 | |
Steven Toth | a9c36aa | 2008-04-17 21:41:28 -0300 | [diff] [blame] | 34 | #define _AU0828_BULKPIPE 0x83 |
| 35 | #define _BULKPIPESIZE 0xe522 |
| 36 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 37 | static struct au8522_config hauppauge_hvr950q_config = { |
| 38 | .demod_address = 0x8e >> 1, |
| 39 | .status_mode = AU8522_DEMODLOCKING, |
| 40 | }; |
| 41 | |
| 42 | static struct xc5000_config hauppauge_hvr950q_tunerconfig = { |
| 43 | .i2c_address = 0x61, |
| 44 | .if_khz = 6000, |
| 45 | .tuner_callback = au0828_tuner_callback |
| 46 | }; |
| 47 | |
| 48 | /*-------------------------------------------------------------------*/ |
| 49 | static void urb_completion(struct urb *purb) |
| 50 | { |
| 51 | u8 *ptr; |
| 52 | struct au0828_dev *dev = purb->context; |
| 53 | int ptype = usb_pipetype(purb->pipe); |
| 54 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 55 | dprintk(2, "%s()\n", __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 56 | |
Steven Toth | 9c26de55 | 2008-04-02 01:10:40 -0300 | [diff] [blame] | 57 | if (!dev) |
| 58 | return; |
| 59 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 60 | if (dev->urb_streaming == 0) |
| 61 | return; |
| 62 | |
| 63 | if (ptype != PIPE_BULK) { |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 64 | printk(KERN_ERR "%s() Unsupported URB type %d\n", |
| 65 | __func__, ptype); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 66 | return; |
| 67 | } |
| 68 | |
| 69 | ptr = (u8 *)purb->transfer_buffer; |
| 70 | |
| 71 | /* Feed the transport payload into the kernel demux */ |
Steven Toth | a9c36aa | 2008-04-17 21:41:28 -0300 | [diff] [blame] | 72 | dvb_dmx_swfilter_packets(&dev->dvb.demux, |
| 73 | purb->transfer_buffer, purb->actual_length / 188); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 74 | |
| 75 | /* Clean the buffer before we requeue */ |
| 76 | memset(purb->transfer_buffer, 0, URB_BUFSIZE); |
| 77 | |
| 78 | /* Requeue URB */ |
| 79 | usb_submit_urb(purb, GFP_ATOMIC); |
| 80 | } |
| 81 | |
| 82 | static int stop_urb_transfer(struct au0828_dev *dev) |
| 83 | { |
| 84 | int i; |
| 85 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 86 | dprintk(2, "%s()\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 87 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 88 | for (i = 0; i < URB_COUNT; i++) { |
| 89 | usb_kill_urb(dev->urbs[i]); |
| 90 | kfree(dev->urbs[i]->transfer_buffer); |
| 91 | usb_free_urb(dev->urbs[i]); |
| 92 | } |
| 93 | |
| 94 | dev->urb_streaming = 0; |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 99 | static int start_urb_transfer(struct au0828_dev *dev) |
| 100 | { |
| 101 | struct urb *purb; |
| 102 | int i, ret = -ENOMEM; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 103 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 104 | dprintk(2, "%s()\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 105 | |
| 106 | if (dev->urb_streaming) { |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 107 | dprintk(2, "%s: iso xfer already running!\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | for (i = 0; i < URB_COUNT; i++) { |
| 112 | |
| 113 | dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL); |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 114 | if (!dev->urbs[i]) |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 115 | goto err; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 116 | |
| 117 | purb = dev->urbs[i]; |
| 118 | |
| 119 | purb->transfer_buffer = kzalloc(URB_BUFSIZE, GFP_KERNEL); |
| 120 | if (!purb->transfer_buffer) { |
| 121 | usb_free_urb(purb); |
Harvey Harrison | a6a3a17 | 2008-04-28 16:50:03 -0700 | [diff] [blame] | 122 | dev->urbs[i] = NULL; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 123 | goto err; |
| 124 | } |
| 125 | |
| 126 | purb->status = -EINPROGRESS; |
| 127 | usb_fill_bulk_urb(purb, |
| 128 | dev->usbdev, |
| 129 | usb_rcvbulkpipe(dev->usbdev, _AU0828_BULKPIPE), |
| 130 | purb->transfer_buffer, |
| 131 | URB_BUFSIZE, |
| 132 | urb_completion, |
| 133 | dev); |
| 134 | |
| 135 | } |
| 136 | |
| 137 | for (i = 0; i < URB_COUNT; i++) { |
| 138 | ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC); |
| 139 | if (ret != 0) { |
| 140 | stop_urb_transfer(dev); |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 141 | printk(KERN_ERR "%s: failed urb submission, " |
| 142 | "err = %d\n", __func__, ret); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 143 | return ret; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | dev->urb_streaming = 1; |
| 148 | ret = 0; |
| 149 | |
| 150 | err: |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | static int au0828_dvb_start_feed(struct dvb_demux_feed *feed) |
| 155 | { |
| 156 | struct dvb_demux *demux = feed->demux; |
| 157 | struct au0828_dev *dev = (struct au0828_dev *) demux->priv; |
| 158 | struct au0828_dvb *dvb = &dev->dvb; |
| 159 | int ret = 0; |
| 160 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 161 | dprintk(1, "%s()\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 162 | |
| 163 | if (!demux->dmx.frontend) |
| 164 | return -EINVAL; |
| 165 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 166 | if (dvb) { |
| 167 | mutex_lock(&dvb->lock); |
| 168 | if (dvb->feeding++ == 0) { |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 169 | /* Start transport */ |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 170 | au0828_write(dev, 0x608, 0x90); |
| 171 | au0828_write(dev, 0x609, 0x72); |
| 172 | au0828_write(dev, 0x60a, 0x71); |
| 173 | au0828_write(dev, 0x60b, 0x01); |
| 174 | ret = start_urb_transfer(dev); |
| 175 | } |
| 176 | mutex_unlock(&dvb->lock); |
| 177 | } |
| 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed) |
| 183 | { |
| 184 | struct dvb_demux *demux = feed->demux; |
| 185 | struct au0828_dev *dev = (struct au0828_dev *) demux->priv; |
| 186 | struct au0828_dvb *dvb = &dev->dvb; |
| 187 | int ret = 0; |
| 188 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 189 | dprintk(1, "%s()\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 190 | |
| 191 | if (dvb) { |
| 192 | mutex_lock(&dvb->lock); |
| 193 | if (--dvb->feeding == 0) { |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 194 | /* Stop transport */ |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 195 | au0828_write(dev, 0x608, 0x00); |
| 196 | au0828_write(dev, 0x609, 0x00); |
| 197 | au0828_write(dev, 0x60a, 0x00); |
| 198 | au0828_write(dev, 0x60b, 0x00); |
| 199 | ret = stop_urb_transfer(dev); |
| 200 | } |
| 201 | mutex_unlock(&dvb->lock); |
| 202 | } |
| 203 | |
| 204 | return ret; |
| 205 | } |
| 206 | |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 207 | static int dvb_register(struct au0828_dev *dev) |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 208 | { |
| 209 | struct au0828_dvb *dvb = &dev->dvb; |
| 210 | int result; |
| 211 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 212 | dprintk(1, "%s()\n", __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 213 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 214 | /* register adapter */ |
| 215 | result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE, |
| 216 | &dev->usbdev->dev, adapter_nr); |
| 217 | if (result < 0) { |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 218 | printk(KERN_ERR "%s: dvb_register_adapter failed " |
| 219 | "(errno = %d)\n", DRIVER_NAME, result); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 220 | goto fail_adapter; |
| 221 | } |
| 222 | dvb->adapter.priv = dev; |
| 223 | |
| 224 | /* register frontend */ |
| 225 | result = dvb_register_frontend(&dvb->adapter, dvb->frontend); |
| 226 | if (result < 0) { |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 227 | printk(KERN_ERR "%s: dvb_register_frontend failed " |
| 228 | "(errno = %d)\n", DRIVER_NAME, result); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 229 | goto fail_frontend; |
| 230 | } |
| 231 | |
| 232 | /* register demux stuff */ |
| 233 | dvb->demux.dmx.capabilities = |
| 234 | DMX_TS_FILTERING | DMX_SECTION_FILTERING | |
| 235 | DMX_MEMORY_BASED_FILTERING; |
| 236 | dvb->demux.priv = dev; |
| 237 | dvb->demux.filternum = 256; |
| 238 | dvb->demux.feednum = 256; |
| 239 | dvb->demux.start_feed = au0828_dvb_start_feed; |
| 240 | dvb->demux.stop_feed = au0828_dvb_stop_feed; |
| 241 | result = dvb_dmx_init(&dvb->demux); |
| 242 | if (result < 0) { |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 243 | printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n", |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 244 | DRIVER_NAME, result); |
| 245 | goto fail_dmx; |
| 246 | } |
| 247 | |
| 248 | dvb->dmxdev.filternum = 256; |
| 249 | dvb->dmxdev.demux = &dvb->demux.dmx; |
| 250 | dvb->dmxdev.capabilities = 0; |
| 251 | result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter); |
| 252 | if (result < 0) { |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 253 | printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n", |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 254 | DRIVER_NAME, result); |
| 255 | goto fail_dmxdev; |
| 256 | } |
| 257 | |
| 258 | dvb->fe_hw.source = DMX_FRONTEND_0; |
| 259 | result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw); |
| 260 | if (result < 0) { |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 261 | printk(KERN_ERR "%s: add_frontend failed " |
| 262 | "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 263 | goto fail_fe_hw; |
| 264 | } |
| 265 | |
| 266 | dvb->fe_mem.source = DMX_MEMORY_FE; |
| 267 | result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem); |
| 268 | if (result < 0) { |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 269 | printk(KERN_ERR "%s: add_frontend failed " |
| 270 | "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 271 | goto fail_fe_mem; |
| 272 | } |
| 273 | |
| 274 | result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw); |
| 275 | if (result < 0) { |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 276 | printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n", |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 277 | DRIVER_NAME, result); |
| 278 | goto fail_fe_conn; |
| 279 | } |
| 280 | |
| 281 | /* register network adapter */ |
| 282 | dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx); |
| 283 | return 0; |
| 284 | |
| 285 | fail_fe_conn: |
| 286 | dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem); |
| 287 | fail_fe_mem: |
| 288 | dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw); |
| 289 | fail_fe_hw: |
| 290 | dvb_dmxdev_release(&dvb->dmxdev); |
| 291 | fail_dmxdev: |
| 292 | dvb_dmx_release(&dvb->demux); |
| 293 | fail_dmx: |
| 294 | dvb_unregister_frontend(dvb->frontend); |
| 295 | fail_frontend: |
| 296 | dvb_frontend_detach(dvb->frontend); |
| 297 | dvb_unregister_adapter(&dvb->adapter); |
| 298 | fail_adapter: |
| 299 | return result; |
| 300 | } |
| 301 | |
| 302 | void au0828_dvb_unregister(struct au0828_dev *dev) |
| 303 | { |
| 304 | struct au0828_dvb *dvb = &dev->dvb; |
| 305 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 306 | dprintk(1, "%s()\n", __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 307 | |
Steven Toth | a9c36aa | 2008-04-17 21:41:28 -0300 | [diff] [blame] | 308 | if (dvb->frontend == NULL) |
Steven Toth | 9c26de55 | 2008-04-02 01:10:40 -0300 | [diff] [blame] | 309 | return; |
| 310 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 311 | dvb_net_release(&dvb->net); |
| 312 | dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem); |
| 313 | dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw); |
| 314 | dvb_dmxdev_release(&dvb->dmxdev); |
| 315 | dvb_dmx_release(&dvb->demux); |
| 316 | dvb_unregister_frontend(dvb->frontend); |
| 317 | dvb_frontend_detach(dvb->frontend); |
| 318 | dvb_unregister_adapter(&dvb->adapter); |
| 319 | } |
| 320 | |
| 321 | /* All the DVB attach calls go here, this function get's modified |
| 322 | * for each new card. No other function in this file needs |
| 323 | * to change. |
| 324 | */ |
| 325 | int au0828_dvb_register(struct au0828_dev *dev) |
| 326 | { |
| 327 | struct au0828_dvb *dvb = &dev->dvb; |
| 328 | int ret; |
| 329 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 330 | dprintk(1, "%s()\n", __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 331 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 332 | /* init frontend */ |
| 333 | switch (dev->board) { |
| 334 | case AU0828_BOARD_HAUPPAUGE_HVR850: |
| 335 | case AU0828_BOARD_HAUPPAUGE_HVR950Q: |
| 336 | case AU0828_BOARD_DVICO_FUSIONHDTV7: |
| 337 | dvb->frontend = dvb_attach(au8522_attach, |
| 338 | &hauppauge_hvr950q_config, |
| 339 | &dev->i2c_adap); |
Michael Krufky | 4872354 | 2008-05-10 14:34:09 -0300 | [diff] [blame] | 340 | if (dvb->frontend != NULL) |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 341 | dvb_attach(xc5000_attach, dvb->frontend, |
| 342 | &dev->i2c_adap, |
Michael Krufky | 4872354 | 2008-05-10 14:34:09 -0300 | [diff] [blame] | 343 | &hauppauge_hvr950q_tunerconfig, dev); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 344 | break; |
| 345 | default: |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 346 | printk(KERN_WARNING "The frontend of your DVB/ATSC card " |
| 347 | "isn't supported yet\n"); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 348 | break; |
| 349 | } |
| 350 | if (NULL == dvb->frontend) { |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 351 | printk(KERN_ERR "%s() Frontend initialization failed\n", |
| 352 | __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 353 | return -1; |
| 354 | } |
| 355 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 356 | /* register everything */ |
| 357 | ret = dvb_register(dev); |
| 358 | if (ret < 0) { |
| 359 | if (dvb->frontend->ops.release) |
| 360 | dvb->frontend->ops.release(dvb->frontend); |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |