Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 1 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 2 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | #include <media/saa7146_vv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | /****************************************************************************/ |
| 6 | /* resource management functions, shamelessly stolen from saa7134 driver */ |
| 7 | |
| 8 | int saa7146_res_get(struct saa7146_fh *fh, unsigned int bit) |
| 9 | { |
| 10 | struct saa7146_dev *dev = fh->dev; |
| 11 | struct saa7146_vv *vv = dev->vv_data; |
| 12 | |
| 13 | if (fh->resources & bit) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 14 | DEB_D("already allocated! want: 0x%02x, cur:0x%02x\n", |
| 15 | bit, vv->resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | /* have it already allocated */ |
| 17 | return 1; |
| 18 | } |
| 19 | |
| 20 | /* is it free? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | if (vv->resources & bit) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 22 | DEB_D("locked! vv->resources:0x%02x, we want:0x%02x\n", |
| 23 | vv->resources, bit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | /* no, someone else uses it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | return 0; |
| 26 | } |
| 27 | /* it's free, grab it */ |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 28 | fh->resources |= bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | vv->resources |= bit; |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 30 | DEB_D("res: get 0x%02x, cur:0x%02x\n", bit, vv->resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | return 1; |
| 32 | } |
| 33 | |
| 34 | void saa7146_res_free(struct saa7146_fh *fh, unsigned int bits) |
| 35 | { |
| 36 | struct saa7146_dev *dev = fh->dev; |
| 37 | struct saa7146_vv *vv = dev->vv_data; |
| 38 | |
Eric Sesterhenn | ae24601 | 2006-03-13 13:17:11 -0300 | [diff] [blame] | 39 | BUG_ON((fh->resources & bits) != bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 41 | fh->resources &= ~bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | vv->resources &= ~bits; |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 43 | DEB_D("res: put 0x%02x, cur:0x%02x\n", bits, vv->resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
| 47 | /********************************************************************************/ |
| 48 | /* common dma functions */ |
| 49 | |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 50 | void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q, |
| 51 | struct saa7146_buf *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Mauro Carvalho Chehab | c1accaa | 2007-08-23 16:37:49 -0300 | [diff] [blame] | 53 | struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 54 | DEB_EE("dev:%p, buf:%p\n", dev, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Eric Sesterhenn | ae24601 | 2006-03-13 13:17:11 -0300 | [diff] [blame] | 56 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 58 | videobuf_waiton(q, &buf->vb, 0, 0); |
Laurent Pinchart | 9526840 | 2010-05-11 10:36:30 -0300 | [diff] [blame] | 59 | videobuf_dma_unmap(q->dev, dma); |
Mauro Carvalho Chehab | c1accaa | 2007-08-23 16:37:49 -0300 | [diff] [blame] | 60 | videobuf_dma_free(dma); |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 61 | buf->vb.state = VIDEOBUF_NEEDS_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
| 65 | /********************************************************************************/ |
| 66 | /* common buffer functions */ |
| 67 | |
| 68 | int saa7146_buffer_queue(struct saa7146_dev *dev, |
| 69 | struct saa7146_dmaqueue *q, |
| 70 | struct saa7146_buf *buf) |
| 71 | { |
| 72 | assert_spin_locked(&dev->slock); |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 73 | DEB_EE("dev:%p, dmaq:%p, buf:%p\n", dev, q, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | BUG_ON(!q); |
| 76 | |
| 77 | if (NULL == q->curr) { |
| 78 | q->curr = buf; |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 79 | DEB_D("immediately activating buffer %p\n", buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | buf->activate(dev,buf,NULL); |
| 81 | } else { |
| 82 | list_add_tail(&buf->vb.queue,&q->queue); |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 83 | buf->vb.state = VIDEOBUF_QUEUED; |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 84 | DEB_D("adding buffer %p to queue. (active buffer present)\n", |
| 85 | buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | void saa7146_buffer_finish(struct saa7146_dev *dev, |
| 91 | struct saa7146_dmaqueue *q, |
| 92 | int state) |
| 93 | { |
| 94 | assert_spin_locked(&dev->slock); |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 95 | DEB_EE("dev:%p, dmaq:%p, state:%d\n", dev, q, state); |
| 96 | DEB_EE("q->curr:%p\n", q->curr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | BUG_ON(!q->curr); |
| 99 | |
| 100 | /* finish current buffer */ |
| 101 | if (NULL == q->curr) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 102 | DEB_D("aiii. no current buffer\n"); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 103 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | q->curr->vb.state = state; |
| 107 | do_gettimeofday(&q->curr->vb.ts); |
| 108 | wake_up(&q->curr->vb.done); |
| 109 | |
| 110 | q->curr = NULL; |
| 111 | } |
| 112 | |
| 113 | void saa7146_buffer_next(struct saa7146_dev *dev, |
| 114 | struct saa7146_dmaqueue *q, int vbi) |
| 115 | { |
| 116 | struct saa7146_buf *buf,*next = NULL; |
| 117 | |
| 118 | BUG_ON(!q); |
| 119 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 120 | DEB_INT("dev:%p, dmaq:%p, vbi:%d\n", dev, q, vbi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | assert_spin_locked(&dev->slock); |
| 123 | if (!list_empty(&q->queue)) { |
| 124 | /* activate next one from queue */ |
| 125 | buf = list_entry(q->queue.next,struct saa7146_buf,vb.queue); |
| 126 | list_del(&buf->vb.queue); |
| 127 | if (!list_empty(&q->queue)) |
| 128 | next = list_entry(q->queue.next,struct saa7146_buf, vb.queue); |
| 129 | q->curr = buf; |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 130 | DEB_INT("next buffer: buf:%p, prev:%p, next:%p\n", |
| 131 | buf, q->queue.prev, q->queue.next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | buf->activate(dev,buf,next); |
| 133 | } else { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 134 | DEB_INT("no next buffer. stopping.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | if( 0 != vbi ) { |
| 136 | /* turn off video-dma3 */ |
| 137 | saa7146_write(dev,MC1, MASK_20); |
| 138 | } else { |
| 139 | /* nothing to do -- just prevent next video-dma1 transfer |
| 140 | by lowering the protection address */ |
| 141 | |
| 142 | // fixme: fix this for vflip != 0 |
| 143 | |
| 144 | saa7146_write(dev, PROT_ADDR1, 0); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 145 | saa7146_write(dev, MC2, (MASK_02|MASK_18)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | /* write the address of the rps-program */ |
| 148 | saa7146_write(dev, RPS_ADDR0, dev->d_rps0.dma_handle); |
| 149 | /* turn on rps */ |
| 150 | saa7146_write(dev, MC1, (MASK_12 | MASK_28)); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* |
| 153 | printk("vdma%d.base_even: 0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1)); |
| 154 | printk("vdma%d.base_odd: 0x%08x\n", 1,saa7146_read(dev,BASE_ODD1)); |
| 155 | printk("vdma%d.prot_addr: 0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1)); |
| 156 | printk("vdma%d.base_page: 0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1)); |
| 157 | printk("vdma%d.pitch: 0x%08x\n", 1,saa7146_read(dev,PITCH1)); |
| 158 | printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1)); |
| 159 | */ |
| 160 | } |
| 161 | del_timer(&q->timeout); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void saa7146_buffer_timeout(unsigned long data) |
| 166 | { |
| 167 | struct saa7146_dmaqueue *q = (struct saa7146_dmaqueue*)data; |
| 168 | struct saa7146_dev *dev = q->dev; |
| 169 | unsigned long flags; |
| 170 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 171 | DEB_EE("dev:%p, dmaq:%p\n", dev, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | spin_lock_irqsave(&dev->slock,flags); |
| 174 | if (q->curr) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 175 | DEB_D("timeout on %p\n", q->curr); |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 176 | saa7146_buffer_finish(dev,q,VIDEOBUF_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* we don't restart the transfer here like other drivers do. when |
| 180 | a streaming capture is disabled, the timeout function will be |
| 181 | called for the current buffer. if we activate the next buffer now, |
| 182 | we mess up our capture logic. if a timeout occurs on another buffer, |
| 183 | then something is seriously broken before, so no need to buffer the |
| 184 | next capture IMHO... */ |
| 185 | /* |
| 186 | saa7146_buffer_next(dev,q); |
| 187 | */ |
| 188 | spin_unlock_irqrestore(&dev->slock,flags); |
| 189 | } |
| 190 | |
| 191 | /********************************************************************************/ |
| 192 | /* file operations */ |
| 193 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 194 | static int fops_open(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Laurent Pinchart | 63b0d5a | 2009-12-10 11:44:04 -0200 | [diff] [blame] | 196 | struct video_device *vdev = video_devdata(file); |
| 197 | struct saa7146_dev *dev = video_drvdata(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | struct saa7146_fh *fh = NULL; |
| 199 | int result = 0; |
| 200 | |
Laurent Pinchart | 63b0d5a | 2009-12-10 11:44:04 -0200 | [diff] [blame] | 201 | enum v4l2_buf_type type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 203 | DEB_EE("file:%p, dev:%s\n", file, video_device_node_name(vdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 205 | if (mutex_lock_interruptible(&saa7146_devices_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | return -ERESTARTSYS; |
| 207 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 208 | DEB_D("using: %p\n", dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Laurent Pinchart | 63b0d5a | 2009-12-10 11:44:04 -0200 | [diff] [blame] | 210 | type = vdev->vfl_type == VFL_TYPE_GRABBER |
| 211 | ? V4L2_BUF_TYPE_VIDEO_CAPTURE |
| 212 | : V4L2_BUF_TYPE_VBI_CAPTURE; |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | /* check if an extension is registered */ |
| 215 | if( NULL == dev->ext ) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 216 | DEB_S("no extension registered for this device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | result = -ENODEV; |
| 218 | goto out; |
| 219 | } |
| 220 | |
| 221 | /* allocate per open data */ |
Panagiotis Issaris | 7408187 | 2006-01-11 19:40:56 -0200 | [diff] [blame] | 222 | fh = kzalloc(sizeof(*fh),GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if (NULL == fh) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 224 | DEB_S("cannot allocate memory for per open data\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | result = -ENOMEM; |
| 226 | goto out; |
| 227 | } |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | file->private_data = fh; |
| 230 | fh->dev = dev; |
| 231 | fh->type = type; |
| 232 | |
| 233 | if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 234 | DEB_S("initializing vbi...\n"); |
Oliver Endriss | 5b0fa4f | 2006-01-09 18:21:37 -0200 | [diff] [blame] | 235 | if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE) |
| 236 | result = saa7146_vbi_uops.open(dev,file); |
| 237 | if (dev->ext_vv_data->vbi_fops.open) |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 238 | dev->ext_vv_data->vbi_fops.open(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } else { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 240 | DEB_S("initializing video...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | result = saa7146_video_uops.open(dev,file); |
| 242 | } |
| 243 | |
| 244 | if (0 != result) { |
| 245 | goto out; |
| 246 | } |
| 247 | |
| 248 | if( 0 == try_module_get(dev->ext->module)) { |
| 249 | result = -EINVAL; |
| 250 | goto out; |
| 251 | } |
| 252 | |
| 253 | result = 0; |
| 254 | out: |
Al Viro | 5fa1247 | 2008-03-29 03:07:38 +0000 | [diff] [blame] | 255 | if (fh && result != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | kfree(fh); |
| 257 | file->private_data = NULL; |
| 258 | } |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 259 | mutex_unlock(&saa7146_devices_lock); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 260 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 263 | static int fops_release(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
| 265 | struct saa7146_fh *fh = file->private_data; |
| 266 | struct saa7146_dev *dev = fh->dev; |
| 267 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 268 | DEB_EE("file:%p\n", file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 270 | if (mutex_lock_interruptible(&saa7146_devices_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | return -ERESTARTSYS; |
| 272 | |
| 273 | if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { |
Oliver Endriss | 5b0fa4f | 2006-01-09 18:21:37 -0200 | [diff] [blame] | 274 | if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE) |
| 275 | saa7146_vbi_uops.release(dev,file); |
| 276 | if (dev->ext_vv_data->vbi_fops.release) |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 277 | dev->ext_vv_data->vbi_fops.release(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } else { |
| 279 | saa7146_video_uops.release(dev,file); |
| 280 | } |
| 281 | |
| 282 | module_put(dev->ext->module); |
| 283 | file->private_data = NULL; |
| 284 | kfree(fh); |
| 285 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 286 | mutex_unlock(&saa7146_devices_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | static int fops_mmap(struct file *file, struct vm_area_struct * vma) |
| 292 | { |
| 293 | struct saa7146_fh *fh = file->private_data; |
| 294 | struct videobuf_queue *q; |
| 295 | |
| 296 | switch (fh->type) { |
| 297 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 298 | DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n", |
| 299 | file, vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | q = &fh->video_q; |
| 301 | break; |
| 302 | } |
| 303 | case V4L2_BUF_TYPE_VBI_CAPTURE: { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 304 | DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n", |
| 305 | file, vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | q = &fh->vbi_q; |
| 307 | break; |
| 308 | } |
| 309 | default: |
| 310 | BUG(); |
| 311 | return 0; |
| 312 | } |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | return videobuf_mmap_mapper(q,vma); |
| 315 | } |
| 316 | |
| 317 | static unsigned int fops_poll(struct file *file, struct poll_table_struct *wait) |
| 318 | { |
| 319 | struct saa7146_fh *fh = file->private_data; |
| 320 | struct videobuf_buffer *buf = NULL; |
| 321 | struct videobuf_queue *q; |
| 322 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 323 | DEB_EE("file:%p, poll:%p\n", file, wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
| 325 | if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) { |
| 326 | if( 0 == fh->vbi_q.streaming ) |
| 327 | return videobuf_poll_stream(file, &fh->vbi_q, wait); |
| 328 | q = &fh->vbi_q; |
| 329 | } else { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 330 | DEB_D("using video queue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | q = &fh->video_q; |
| 332 | } |
| 333 | |
| 334 | if (!list_empty(&q->stream)) |
| 335 | buf = list_entry(q->stream.next, struct videobuf_buffer, stream); |
| 336 | |
| 337 | if (!buf) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 338 | DEB_D("buf == NULL!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return POLLERR; |
| 340 | } |
| 341 | |
| 342 | poll_wait(file, &buf->done, wait); |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 343 | if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 344 | DEB_D("poll succeeded!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | return POLLIN|POLLRDNORM; |
| 346 | } |
| 347 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 348 | DEB_D("nothing to poll for, buf->state:%d\n", buf->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static ssize_t fops_read(struct file *file, char __user *data, size_t count, loff_t *ppos) |
| 353 | { |
| 354 | struct saa7146_fh *fh = file->private_data; |
| 355 | |
| 356 | switch (fh->type) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 357 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 358 | /* |
| 359 | DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, data:%p, count:%lun", |
| 360 | file, data, (unsigned long)count); |
| 361 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | return saa7146_video_uops.read(file,data,count,ppos); |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 363 | case V4L2_BUF_TYPE_VBI_CAPTURE: |
| 364 | /* |
| 365 | DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, data:%p, count:%lu\n", |
| 366 | file, data, (unsigned long)count); |
| 367 | */ |
Oliver Endriss | 5b0fa4f | 2006-01-09 18:21:37 -0200 | [diff] [blame] | 368 | if (fh->dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE) |
| 369 | return saa7146_vbi_uops.read(file,data,count,ppos); |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 370 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | default: |
| 372 | BUG(); |
| 373 | return 0; |
| 374 | } |
| 375 | } |
| 376 | |
Oliver Endriss | 5b0fa4f | 2006-01-09 18:21:37 -0200 | [diff] [blame] | 377 | static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos) |
| 378 | { |
| 379 | struct saa7146_fh *fh = file->private_data; |
| 380 | |
| 381 | switch (fh->type) { |
| 382 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 383 | return -EINVAL; |
| 384 | case V4L2_BUF_TYPE_VBI_CAPTURE: |
| 385 | if (fh->dev->ext_vv_data->vbi_fops.write) |
| 386 | return fh->dev->ext_vv_data->vbi_fops.write(file, data, count, ppos); |
| 387 | else |
| 388 | return -EINVAL; |
| 389 | default: |
| 390 | BUG(); |
| 391 | return -EINVAL; |
| 392 | } |
| 393 | } |
| 394 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 395 | static const struct v4l2_file_operations video_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
| 397 | .owner = THIS_MODULE, |
| 398 | .open = fops_open, |
| 399 | .release = fops_release, |
| 400 | .read = fops_read, |
Oliver Endriss | 5b0fa4f | 2006-01-09 18:21:37 -0200 | [diff] [blame] | 401 | .write = fops_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | .poll = fops_poll, |
| 403 | .mmap = fops_mmap, |
Hans Verkuil | 9af3971 | 2010-12-18 09:20:59 -0300 | [diff] [blame] | 404 | .unlocked_ioctl = video_ioctl2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
Adrian Bunk | 52c1da3 | 2005-06-23 22:05:33 -0700 | [diff] [blame] | 407 | static void vv_callback(struct saa7146_dev *dev, unsigned long status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | { |
| 409 | u32 isr = status; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 410 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 411 | DEB_INT("dev:%p, isr:0x%08x\n", dev, (u32)status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | if (0 != (isr & (MASK_27))) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 414 | DEB_INT("irq: RPS0 (0x%08x)\n", isr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | saa7146_video_uops.irq_done(dev,isr); |
| 416 | } |
| 417 | |
| 418 | if (0 != (isr & (MASK_28))) { |
| 419 | u32 mc2 = saa7146_read(dev, MC2); |
| 420 | if( 0 != (mc2 & MASK_15)) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 421 | DEB_INT("irq: RPS1 vbi workaround (0x%08x)\n", isr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | wake_up(&dev->vv_data->vbi_wq); |
| 423 | saa7146_write(dev,MC2, MASK_31); |
| 424 | return; |
| 425 | } |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 426 | DEB_INT("irq: RPS1 (0x%08x)\n", isr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | saa7146_vbi_uops.irq_done(dev,isr); |
| 428 | } |
| 429 | } |
| 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv) |
| 432 | { |
Hans Verkuil | 230b65f | 2009-02-07 20:42:33 -0300 | [diff] [blame] | 433 | struct saa7146_vv *vv; |
Hans Verkuil | 03b1930 | 2010-03-24 19:09:55 -0300 | [diff] [blame] | 434 | int err; |
| 435 | |
| 436 | err = v4l2_device_register(&dev->pci->dev, &dev->v4l2_dev); |
| 437 | if (err) |
| 438 | return err; |
Hans Verkuil | 230b65f | 2009-02-07 20:42:33 -0300 | [diff] [blame] | 439 | |
| 440 | vv = kzalloc(sizeof(struct saa7146_vv), GFP_KERNEL); |
Hans Verkuil | b960074 | 2009-01-18 19:59:11 -0300 | [diff] [blame] | 441 | if (vv == NULL) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 442 | ERR("out of memory. aborting.\n"); |
Hans Verkuil | 230b65f | 2009-02-07 20:42:33 -0300 | [diff] [blame] | 443 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
Hans Verkuil | b960074 | 2009-01-18 19:59:11 -0300 | [diff] [blame] | 445 | ext_vv->ops = saa7146_video_ioctl_ops; |
| 446 | ext_vv->core_ops = &saa7146_video_ioctl_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 448 | DEB_EE("dev:%p\n", dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | /* set default values for video parts of the saa7146 */ |
| 451 | saa7146_write(dev, BCS_CTRL, 0x80400040); |
| 452 | |
| 453 | /* enable video-port pins */ |
| 454 | saa7146_write(dev, MC1, (MASK_10 | MASK_26)); |
| 455 | |
| 456 | /* save per-device extension data (one extension can |
| 457 | handle different devices that might need different |
| 458 | configuration data) */ |
| 459 | dev->ext_vv_data = ext_vv; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 460 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 461 | vv->d_clipping.cpu_addr = pci_alloc_consistent(dev->pci, SAA7146_CLIPPING_MEM, &vv->d_clipping.dma_handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if( NULL == vv->d_clipping.cpu_addr ) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 463 | ERR("out of memory. aborting.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | kfree(vv); |
| 465 | return -1; |
| 466 | } |
| 467 | memset(vv->d_clipping.cpu_addr, 0x0, SAA7146_CLIPPING_MEM); |
| 468 | |
| 469 | saa7146_video_uops.init(dev,vv); |
Oliver Endriss | 5b0fa4f | 2006-01-09 18:21:37 -0200 | [diff] [blame] | 470 | if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE) |
| 471 | saa7146_vbi_uops.init(dev,vv); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | dev->vv_data = vv; |
| 474 | dev->vv_callback = &vv_callback; |
| 475 | |
| 476 | return 0; |
| 477 | } |
Adrian Bunk | 5d7dc8c | 2006-04-11 10:26:57 -0300 | [diff] [blame] | 478 | EXPORT_SYMBOL_GPL(saa7146_vv_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | int saa7146_vv_release(struct saa7146_dev* dev) |
| 481 | { |
| 482 | struct saa7146_vv *vv = dev->vv_data; |
| 483 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 484 | DEB_EE("dev:%p\n", dev); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 485 | |
Hans Verkuil | 230b65f | 2009-02-07 20:42:33 -0300 | [diff] [blame] | 486 | v4l2_device_unregister(&dev->v4l2_dev); |
Marco Schluessler | 58af004 | 2007-01-31 14:27:55 -0300 | [diff] [blame] | 487 | pci_free_consistent(dev->pci, SAA7146_CLIPPING_MEM, vv->d_clipping.cpu_addr, vv->d_clipping.dma_handle); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 488 | kfree(vv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | dev->vv_data = NULL; |
| 490 | dev->vv_callback = NULL; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | return 0; |
| 493 | } |
Adrian Bunk | 5d7dc8c | 2006-04-11 10:26:57 -0300 | [diff] [blame] | 494 | EXPORT_SYMBOL_GPL(saa7146_vv_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | int saa7146_register_device(struct video_device **vid, struct saa7146_dev* dev, |
| 497 | char *name, int type) |
| 498 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | struct video_device *vfd; |
Hans Verkuil | b960074 | 2009-01-18 19:59:11 -0300 | [diff] [blame] | 500 | int err; |
Hans Verkuil | d0852ed | 2009-01-26 19:13:05 -0300 | [diff] [blame] | 501 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 503 | DEB_EE("dev:%p, name:'%s', type:%d\n", dev, name, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | // released by vfd->release |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 506 | vfd = video_device_alloc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | if (vfd == NULL) |
| 508 | return -ENOMEM; |
| 509 | |
Hans Verkuil | b960074 | 2009-01-18 19:59:11 -0300 | [diff] [blame] | 510 | vfd->fops = &video_fops; |
Hans Verkuil | d0852ed | 2009-01-26 19:13:05 -0300 | [diff] [blame] | 511 | vfd->ioctl_ops = &dev->ext_vv_data->ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | vfd->release = video_device_release; |
Hans Verkuil | 9af3971 | 2010-12-18 09:20:59 -0300 | [diff] [blame] | 513 | vfd->lock = &dev->v4l2_lock; |
Hans Verkuil | d0852ed | 2009-01-26 19:13:05 -0300 | [diff] [blame] | 514 | vfd->tvnorms = 0; |
| 515 | for (i = 0; i < dev->ext_vv_data->num_stds; i++) |
| 516 | vfd->tvnorms |= dev->ext_vv_data->stds[i].id; |
Hans Verkuil | b960074 | 2009-01-18 19:59:11 -0300 | [diff] [blame] | 517 | strlcpy(vfd->name, name, sizeof(vfd->name)); |
Hans Verkuil | 601e944 | 2008-08-23 07:24:07 -0300 | [diff] [blame] | 518 | video_set_drvdata(vfd, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Hans Verkuil | b960074 | 2009-01-18 19:59:11 -0300 | [diff] [blame] | 520 | err = video_register_device(vfd, type, -1); |
| 521 | if (err < 0) { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 522 | ERR("cannot register v4l2 device. skipping.\n"); |
Julia Lawall | a2a9b1e | 2008-01-11 22:03:42 -0300 | [diff] [blame] | 523 | video_device_release(vfd); |
Hans Verkuil | b960074 | 2009-01-18 19:59:11 -0300 | [diff] [blame] | 524 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 527 | pr_info("%s: registered device %s [v4l2]\n", |
| 528 | dev->name, video_device_node_name(vfd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
| 530 | *vid = vfd; |
| 531 | return 0; |
| 532 | } |
Adrian Bunk | 5d7dc8c | 2006-04-11 10:26:57 -0300 | [diff] [blame] | 533 | EXPORT_SYMBOL_GPL(saa7146_register_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
| 535 | int saa7146_unregister_device(struct video_device **vid, struct saa7146_dev* dev) |
| 536 | { |
Joe Perches | 44d0b80 | 2011-08-21 19:56:44 -0300 | [diff] [blame^] | 537 | DEB_EE("dev:%p\n", dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | video_unregister_device(*vid); |
| 540 | *vid = NULL; |
| 541 | |
| 542 | return 0; |
| 543 | } |
Adrian Bunk | 5d7dc8c | 2006-04-11 10:26:57 -0300 | [diff] [blame] | 544 | EXPORT_SYMBOL_GPL(saa7146_unregister_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
| 546 | static int __init saa7146_vv_init_module(void) |
| 547 | { |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | static void __exit saa7146_vv_cleanup_module(void) |
| 553 | { |
| 554 | } |
| 555 | |
| 556 | module_init(saa7146_vv_init_module); |
| 557 | module_exit(saa7146_vv_cleanup_module); |
| 558 | |
| 559 | MODULE_AUTHOR("Michael Hunold <michael@mihu.de>"); |
| 560 | MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware"); |
| 561 | MODULE_LICENSE("GPL"); |