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