Example #1
0
void bcm_timer_start(bcm_timer_t *t, unsigned long period_in_msecs)
{
   bcm_pr_debug("bcm_timer_start()\n");

   bcm_timer_stop(t);
   bcm_timer_compute_params(t, period_in_msecs * 1000);
   t->is_active = true;
#ifdef __KERNEL__
   t->kobject.expires = jiffies + t->period_in_jiffies;
#else // !__KERNEL__
   t->kobject.expires = get_jiffies() + t->period_in_jiffies;
#endif // !__KERNEL__
   t->drift = 0;
   add_timer(&(t->kobject));
}
Example #2
0
void bcm_timer_reschedule(bcm_timer_t *t)
{
   unsigned long period_in_jiffies = t->period_in_jiffies;
   unsigned long soon;

   if (bcm_timer_is_active(t)) {
      // We compute next expires
      t->drift += t->drift_increment;
      if (t->drift >= one_jiffie_to_usecs) {
         period_in_jiffies -= 1;
         t->drift -= one_jiffie_to_usecs;
      }

      // Now we test that timer will not expire too soon
      t->kobject.expires += period_in_jiffies;

#ifdef __KERNEL__
      soon = jiffies + 1;
#else // !__KERNEL__
      soon = get_jiffies() + 1;
#endif // !__KERNEL__
      if (time_before(t->kobject.expires, soon)) {
#ifdef BCMPH_DEBUG
         d_bcm_pr_debug("too soon %lu\n", (unsigned long)(t->stats.count));
         t->stats.too_soon += 1;
#endif // BCMPH_DEBUG
         t->drift += (((long)(soon) - (long)(t->kobject.expires)) * one_jiffie_to_usecs);
         if (t->drift > TIMER_MAX_DRIFT) {
#ifdef BCMPH_DEBUG
            d_bcm_pr_debug("reset drift %lu\n", (unsigned long)(t->stats.count));
            t->stats.reset_drift += 1;
#endif // BCMPH_DEBUG
            t->drift = 0;
         }
         t->kobject.expires = soon;
      }

      add_timer(&(t->kobject));
   }
}
Example #3
0
/*!
 * V4L2 interface - ioctl function
 *
 * @param inode      struct inode *
 *
 * @param file       struct file *
 *
 * @param ioctlnr    unsigned int
 *
 * @param arg        void *
 *
 * @return           0 success, ENODEV for invalid device instance,
 *                   -1 for other errors.
 */
static int
mxc_v4l2out_do_ioctl(struct inode *inode, struct file *file,
		     unsigned int ioctlnr, void *arg)
{
	struct video_device *dev = file->private_data;
	vout_data *vout = video_get_drvdata(dev);
	int retval = 0;
	int i = 0;

	if (!vout)
		return -EBADF;

	/* make this _really_ smp-safe */
	if (down_interruptible(&vout->busy_lock))
		return -EBUSY;

	switch (ioctlnr) {
	case VIDIOC_QUERYCAP:
		{
			struct v4l2_capability *cap = arg;
			strcpy(cap->driver, "mxc_v4l2_output");
			cap->version = 0;
			cap->capabilities =
			    V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
			cap->card[0] = '\0';
			cap->bus_info[0] = '\0';
			retval = 0;
			break;
		}
	case VIDIOC_G_FMT:
		{
			struct v4l2_format *gf = arg;
			retval = mxc_v4l2out_g_fmt(vout, gf);
			break;
		}
	case VIDIOC_S_FMT:
		{
			struct v4l2_format *sf = arg;
			if (vout->state != STATE_STREAM_OFF) {
				retval = -EBUSY;
				break;
			}
			retval = mxc_v4l2out_s_fmt(vout, sf);
			break;
		}
	case VIDIOC_REQBUFS:
		{
			struct v4l2_requestbuffers *req = arg;
			if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) ||
			    (req->memory != V4L2_MEMORY_MMAP)) {
				pr_debug
				    ("VIDIOC_REQBUFS: incorrect buffer type\n");
				retval = -EINVAL;
				break;
			}

			if (req->count == 0)
				mxc_v4l2out_streamoff(vout);

			if (vout->state == STATE_STREAM_OFF) {
				if (vout->queue_buf_paddr[0] != 0) {
					mxc_free_buffers(vout->queue_buf_paddr,
							 vout->queue_buf_vaddr,
							 vout->buffer_cnt,
							 vout->queue_buf_size);
					pr_debug
					    ("VIDIOC_REQBUFS: freed buffers\n");
				}
				vout->buffer_cnt = 0;
			} else {
				pr_debug("VIDIOC_REQBUFS: Buffer is in use\n");
				retval = -EBUSY;
				break;
			}

			if (req->count == 0)
				break;

			if (req->count < MIN_FRAME_NUM) {
				req->count = MIN_FRAME_NUM;
			} else if (req->count > MAX_FRAME_NUM) {
				req->count = MAX_FRAME_NUM;
			}
			vout->buffer_cnt = req->count;
			vout->queue_buf_size =
			    PAGE_ALIGN(vout->v2f.fmt.pix.sizeimage);

			retval = mxc_allocate_buffers(vout->queue_buf_paddr,
						      vout->queue_buf_vaddr,
						      vout->buffer_cnt,
						      vout->queue_buf_size);
			if (retval < 0)
				break;

			/* Init buffer queues */
			vout->done_q.head = 0;
			vout->done_q.tail = 0;
			vout->ready_q.head = 0;
			vout->ready_q.tail = 0;

			for (i = 0; i < vout->buffer_cnt; i++) {
				memset(&(vout->v4l2_bufs[i]), 0,
				       sizeof(vout->v4l2_bufs[i]));
				vout->v4l2_bufs[i].flags = 0;
				vout->v4l2_bufs[i].memory = V4L2_MEMORY_MMAP;
				vout->v4l2_bufs[i].index = i;
				vout->v4l2_bufs[i].type =
				    V4L2_BUF_TYPE_VIDEO_OUTPUT;
				vout->v4l2_bufs[i].length =
				    PAGE_ALIGN(vout->v2f.fmt.pix.sizeimage);
				vout->v4l2_bufs[i].m.offset =
				    (unsigned long)vout->queue_buf_paddr[i];
				vout->v4l2_bufs[i].timestamp.tv_sec = 0;
				vout->v4l2_bufs[i].timestamp.tv_usec = 0;
			}
			break;
		}
	case VIDIOC_QUERYBUF:
		{
			struct v4l2_buffer *buf = arg;
			u32 type = buf->type;
			int index = buf->index;

			if ((type != V4L2_BUF_TYPE_VIDEO_OUTPUT) ||
			    (index >= vout->buffer_cnt)) {
				pr_debug
				    ("VIDIOC_QUERYBUFS: incorrect buffer type\n");
				retval = -EINVAL;
				break;
			}
			down(&vout->param_lock);
			memcpy(buf, &(vout->v4l2_bufs[index]), sizeof(*buf));
			up(&vout->param_lock);
			break;
		}
	case VIDIOC_QBUF:
		{
			struct v4l2_buffer *buf = arg;
			int index = buf->index;
			unsigned long lock_flags;
			unsigned long timeout;

			if ((buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) ||
			    (index >= vout->buffer_cnt) || (buf->flags != 0)) {
				retval = -EINVAL;
				break;
			}

			pr_debug("VIDIOC_QBUF: %d\n", buf->index);

			spin_lock_irqsave(&g_lock, lock_flags);

			memcpy(&(vout->v4l2_bufs[index]), buf, sizeof(*buf));
			vout->v4l2_bufs[index].flags |= V4L2_BUF_FLAG_QUEUED;

			g_buf_q_cnt++;
			queue_buf(&vout->ready_q, index);

			if (vout->state == STATE_STREAM_PAUSED) {
				index = peek_next_buf(&vout->ready_q);

				/* if timestamp is 0, then default to 30fps */
				if ((vout->v4l2_bufs[index].timestamp.tv_sec ==
				     0)
				    && (vout->v4l2_bufs[index].timestamp.
					tv_usec == 0))
					timeout =
					    vout->start_jiffies +
					    vout->frame_count * HZ / 30;
				else
					timeout =
					    get_jiffies(&vout->v4l2_bufs[index].
							timestamp);

				if (jiffies >= timeout) {
					pr_debug
					    ("warning: timer timeout already expired.\n");
				}

				vout->output_timer.expires = timeout;
				pr_debug
				    ("QBUF:Add timer %d timeout @ %lu jiffies, "
				     "current = %lu\n", index, timeout,
				     jiffies);
				add_timer(&vout->output_timer);
				vout->state = STATE_STREAM_ON;
			}

			spin_unlock_irqrestore(&g_lock, lock_flags);
			break;
		}
	case VIDIOC_DQBUF:
		{
			struct v4l2_buffer *buf = arg;
			int idx;

			pr_debug("VIDIOC_DQBUF: q size = %d\n",
				 queue_size(&vout->done_q));

			if ((queue_size(&vout->done_q) == 0) &&
			    (file->f_flags & O_NONBLOCK)) {
				retval = -EAGAIN;
				break;
			}

			if (!wait_event_interruptible_timeout(vout->v4l_bufq,
							      queue_size(&vout->
									 done_q)
							      != 0, 10 * HZ)) {
				pr_debug("VIDIOC_DQBUF: timeout\n");
				retval = -ETIME;
				break;
			} else if (signal_pending(current)) {
				if(dq_intr_cnt == 0)
					pr_debug("VIDIOC_DQBUF: interrupt received\n");
				dq_intr_cnt++;
				retval = -ERESTARTSYS;
				break;
			}
			idx = dequeue_buf(&vout->done_q);
			if (idx == -1) {	/* No frame free */
				pr_debug
				    ("VIDIOC_DQBUF: no free buffers, returning\n");
				retval = -EAGAIN;
				break;
			}
			if ((vout->v4l2_bufs[idx].flags & V4L2_BUF_FLAG_DONE) ==
			    0)
				pr_debug
				    ("VIDIOC_DQBUF: buffer in done q, but not "
				     "flagged as done\n");

			vout->v4l2_bufs[idx].flags = 0;
			memcpy(buf, &(vout->v4l2_bufs[idx]), sizeof(*buf));
			pr_debug("VIDIOC_DQBUF: %d\n", buf->index);
			break;
		}
	case VIDIOC_STREAMON:
		{
			retval = mxc_v4l2out_streamon(vout);
			break;
		}
	case VIDIOC_STREAMOFF:
		{
			retval = mxc_v4l2out_streamoff(vout);
			break;
		}
	case VIDIOC_G_CTRL:
		{
			retval = mxc_get_v42lout_control(vout, arg);
			break;
		}
	case VIDIOC_S_CTRL:
		{
			retval = mxc_set_v42lout_control(vout, arg);
			break;
		}
	case VIDIOC_CROPCAP:
		{
			struct v4l2_cropcap *cap = arg;

			if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
				retval = -EINVAL;
				break;
			}
			cap->bounds = vout->crop_bounds[vout->cur_disp_output];
			cap->defrect = vout->crop_bounds[vout->cur_disp_output];
			retval = 0;
			break;
		}
	case VIDIOC_G_CROP:
		{
			struct v4l2_crop *crop = arg;

			if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
				retval = -EINVAL;
				break;
			}
			crop->c = vout->crop_current;
			break;
		}
	case VIDIOC_S_CROP:
		{
			struct v4l2_crop *crop = arg;
			struct v4l2_rect *b =
			    &(vout->crop_bounds[vout->cur_disp_output]);

			if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
				retval = -EINVAL;
				break;
			}
			if (crop->c.height < 0) {
				retval = -EINVAL;
				break;
			}
			if (crop->c.width < 0) {
				retval = -EINVAL;
				break;
			}

			if (crop->c.top < b->top)
				crop->c.top = b->top;
			if (crop->c.top > b->top + b->height)
				crop->c.top = b->top + b->height;
			if (crop->c.height > b->top - crop->c.top + b->height)
				crop->c.height =
				    b->top - crop->c.top + b->height;

			if (crop->c.left < b->left)
				crop->c.top = b->left;
			if (crop->c.left > b->left + b->width)
				crop->c.top = b->left + b->width;
			if (crop->c.width > b->left - crop->c.left + b->width)
				crop->c.width =
				    b->left - crop->c.left + b->width;

			/* stride line limitation */
			crop->c.height -= crop->c.height % 8;
			crop->c.width -= crop->c.width % 8;

			vout->crop_current = crop->c;

			vout->sdc_fg_buf_size = vout->crop_current.width *
			    vout->crop_current.height;
			vout->sdc_fg_buf_size *=
			    fmt_to_bpp(SDC_FG_FB_FORMAT) / 8;
			break;
		}
	case VIDIOC_ENUMOUTPUT:
		{
			struct v4l2_output *output = arg;

			if ((output->index >= 2) ||
			    (vout->output_enabled[output->index] == false)) {
				retval = -EINVAL;
				break;
			}

			*output = mxc_outputs[0];
			output->name[4] = '0' + output->index;
			break;
		}
	case VIDIOC_G_OUTPUT:
		{
			int *p_output_num = arg;

			*p_output_num = vout->cur_disp_output;
			break;
		}
	case VIDIOC_S_OUTPUT:
		{
			int *p_output_num = arg;

			if ((*p_output_num >= 2) ||
			    (vout->output_enabled[*p_output_num] == false)) {
				retval = -EINVAL;
				break;
			}

			if (vout->state != STATE_STREAM_OFF) {
				retval = -EBUSY;
				break;
			}

			vout->cur_disp_output = *p_output_num;
			break;
		}
	case VIDIOC_G_FBUF:
		{
			struct v4l2_framebuffer *fb = arg;
			*fb = vout->v4l2_fb;
			break;
		}
	case VIDIOC_S_FBUF:
		{
			struct v4l2_framebuffer *fb = arg;
			vout->v4l2_fb = *fb;
			vout->v4l2_fb.capability = V4L2_FBUF_CAP_EXTERNOVERLAY;
			break;
		}
	case VIDIOC_ENUM_FMT:
	case VIDIOC_TRY_FMT:
	case VIDIOC_QUERYCTRL:
	case VIDIOC_G_PARM:
	case VIDIOC_ENUMSTD:
	case VIDIOC_G_STD:
	case VIDIOC_S_STD:
	case VIDIOC_G_TUNER:
	case VIDIOC_S_TUNER:
	case VIDIOC_G_FREQUENCY:
	case VIDIOC_S_FREQUENCY:
	default:
		retval = -EINVAL;
		break;
	}

	up(&vout->busy_lock);
	return retval;
}
Example #4
0
/*!
 * Start the output stream
 *
 * @param vout      structure vout_data *
 *
 * @return status  0 Success
 */
static int mxc_v4l2out_streamon(vout_data * vout)
{
	unsigned long timeout;
	int index;

	if (!vout)
		return -EINVAL;

	if (vout->state != STATE_STREAM_OFF)
		return -EBUSY;

	if (queue_size(&vout->ready_q) < 1) {
		pr_debug("no buffers queued yet!\n");
		return -EINVAL;
	}

	vout->ipu_buf[0] = -1;

	if (vout->v4l2_fb.flags == V4L2_FBUF_FLAG_OVERLAY) {
		/* Free previously allocated buffer */
		mxc_free_buffers(vout->display_bufs, vout->display_bufs_vaddr,
				 2, vout->sdc_fg_buf_size);
		/* Allocate buffers for foreground */
		if (mxc_allocate_buffers(vout->display_bufs,
					 vout->display_bufs_vaddr, 2,
					 vout->sdc_fg_buf_size) < 0) {
			pr_debug("unable to allocate SDC FG buffers\n");
			return -ENOMEM;
		}
	}

	/* Configure PP */
	if (pp_cfg(vout)) {
		/* Free previously allocated buffer */
		mxc_free_buffers(vout->display_bufs, vout->display_bufs_vaddr,
				 2, vout->sdc_fg_buf_size);
		pr_debug("failed to config PP.\n");
		return -EINVAL;
	}
#ifdef CONFIG_VIDEO_MXC_OUTPUT_FBSYNC
	if (vout->tear_protection == TEARING_PROTECTION_ACTIVE) {
		g_output_fb = vout->output_fb_num[vout->cur_disp_output];
		g_fb_enabled = 0;
		g_pp_ready = 0;
		fb_register_client(&fb_event_notifier);
		mx2fb_register_client(&mx2fb_event_notifier);
	}
#endif
	vout->frame_count = 0;
	vout->state = STATE_STREAM_ON;
	index = peek_next_buf(&vout->ready_q);

	/* if timestamp is 0, then default to 30fps */
	if ((vout->v4l2_bufs[index].timestamp.tv_sec == 0)
	    && (vout->v4l2_bufs[index].timestamp.tv_usec == 0))
		timeout = jiffies;
	else
		timeout = get_jiffies(&vout->v4l2_bufs[index].timestamp);

	if (jiffies >= timeout) {
		pr_debug("warning: timer timeout already expired.\n");
	}

	vout->start_jiffies = vout->output_timer.expires = timeout;
	pr_debug("STREAMON:Add timer %d timeout @ %lu jiffies, current = %lu\n",
		 index, timeout, jiffies);
	add_timer(&vout->output_timer);

	return 0;
}
Example #5
0
irqreturn_t mxc_v4l2out_pp_in_irq_handler(int irq, void *dev_id)
{
	int last_buf;
	int index;
	unsigned long timeout;
	unsigned long lock_flags;
	vout_data *vout = dev_id;

	spin_lock_irqsave(&g_lock, lock_flags);

	g_irq_cnt++;

	if ((vout->state == STATE_STREAM_OFF)
	    || (vout->state == STATE_STREAM_STOPPING)) {
		spin_unlock_irqrestore(&g_lock, lock_flags);
		return IRQ_HANDLED;
	}

	if (vout->v4l2_fb.flags == V4L2_FBUF_FLAG_OVERLAY) {
		struct fb_gwinfo gwinfo;

		gwinfo.enabled = 1;
		gwinfo.alpha_value = 255;
		gwinfo.ck_enabled = 0;
		gwinfo.xpos = vout->crop_current.left;
		gwinfo.ypos = vout->crop_current.top;
		gwinfo.base = (unsigned long)vout->display_bufs[pp_num_last()];
		gwinfo.xres = vout->crop_current.width;
		gwinfo.yres = vout->crop_current.height;
		gwinfo.xres_virtual = vout->crop_current.width;
		gwinfo.vs_reversed = 0;

		mx2_gw_set(&gwinfo);
	}

	/* Process previous buffer */
	last_buf = vout->ipu_buf[0];
	pr_debug("last_buf %d g_irq_cnt %d\n", last_buf, g_irq_cnt);
	if (last_buf != -1) {
		g_buf_output_cnt++;
		vout->v4l2_bufs[last_buf].flags = V4L2_BUF_FLAG_DONE;
		queue_buf(&vout->done_q, last_buf);
		vout->ipu_buf[0] = -1;
		wake_up_interruptible(&vout->v4l_bufq);
	}

	/* Setup timer for next buffer, when stream has been paused */
	if ((vout->state == STATE_STREAM_PAUSED)
	    && ((index = peek_next_buf(&vout->ready_q)) != -1)) {
		pr_debug("next index %d\n", index);
		/* if timestamp is 0, then default to 30fps */
		if ((vout->v4l2_bufs[index].timestamp.tv_sec == 0)
		    && (vout->v4l2_bufs[index].timestamp.tv_usec == 0))
			timeout =
			    vout->start_jiffies + vout->frame_count * HZ / 30;
		else
			timeout =
			    get_jiffies(&vout->v4l2_bufs[index].timestamp);

		if (jiffies >= timeout) {
			pr_debug("warning: timer timeout already expired.\n");
		}

		vout->state = STATE_STREAM_ON;

		if (mod_timer(&vout->output_timer, timeout))
			pr_debug("warning: timer was already set\n");

		pr_debug("timer handler next schedule: %lu\n", timeout);
	}

	spin_unlock_irqrestore(&g_lock, lock_flags);

	return IRQ_HANDLED;
}
Example #6
0
static void mxc_v4l2out_timer_handler(unsigned long arg)
{
	int index;
	unsigned long timeout;
	unsigned long lock_flags;
	vout_data *vout = (vout_data *) arg;

	pr_debug("timer handler: %lu\n", jiffies);

	spin_lock_irqsave(&g_lock, lock_flags);

	if ((vout->state == STATE_STREAM_OFF)
	    || (vout->state == STATE_STREAM_STOPPING)) {
		pr_debug("stream has stopped\n");
		goto exit0;
	}

	/*
	 * If timer occurs before PP h/w is ready, then set the state to
	 * paused and the timer will be set again when next buffer is queued
	 * or PP completes.
	 */
	if (vout->ipu_buf[0] != -1) {
		pr_debug("buffer is busy\n");
		vout->state = STATE_STREAM_PAUSED;
		goto exit0;
	}

	/* Dequeue buffer and pass to PP */
	index = dequeue_buf(&vout->ready_q);
	if (index == -1) {	/* no buffers ready, should never occur */
		pr_debug("mxc_v4l2out: timer - no queued buffers ready\n");
		goto exit0;
	}

	g_buf_dq_cnt++;
	vout->frame_count++;
	vout->ipu_buf[0] = index;

	if (pp_ptr((unsigned int)vout->queue_buf_paddr[index])) {
		pr_debug("unable to update buffer\n");
		goto exit0;
	}
#ifdef CONFIG_VIDEO_MXC_OUTPUT_FBSYNC
	if (vout->tear_protection == TEARING_PROTECTION_ACTIVE) {
		if (g_fb_enabled
		    && (vout->v4l2_fb.flags != V4L2_FBUF_FLAG_OVERLAY))
			g_pp_ready = 1;
		else if (pp_enable(1)) {
			pr_debug("unable to enable PP\n");
			goto exit0;
		}
	} else if (pp_enable(1)) {
		pr_debug("unable to enable PP\n");
		goto exit0;
	}
#else
	if (pp_enable(1)) {
		pr_debug("unable to enable PP\n");
		goto exit0;
	}
#endif
	pr_debug("enabled index %d\n", index);

	/* Setup timer for next buffer */
	index = peek_next_buf(&vout->ready_q);
	pr_debug("next index %d\n", index);
	if (index != -1) {
		/* if timestamp is 0, then default to 30fps */
		if ((vout->v4l2_bufs[index].timestamp.tv_sec == 0)
		    && (vout->v4l2_bufs[index].timestamp.tv_usec == 0))
			timeout =
			    vout->start_jiffies + vout->frame_count * HZ / 30;
		else
			timeout =
			    get_jiffies(&vout->v4l2_bufs[index].timestamp);

		if (jiffies >= timeout) {
			pr_debug("warning: timer timeout already expired.\n");
		}

		if (mod_timer(&vout->output_timer, timeout))
			pr_debug("warning: timer was already set\n");

		pr_debug("timer handler next schedule: %lu\n", timeout);
	} else {
		vout->state = STATE_STREAM_PAUSED;
		pr_debug("timer handler paused\n");
	}

      exit0:
	spin_unlock_irqrestore(&g_lock, lock_flags);
}