1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * vivid-kthread-touch.c - touch capture thread support functions. 4 * 5 */ 6 7 #include <linux/freezer.h> 8 #include "vivid-core.h" 9 #include "vivid-kthread-touch.h" 10 #include "vivid-touch-cap.h" 11 12 static noinline_for_stack void vivid_thread_tch_cap_tick(struct vivid_dev *dev, 13 int dropped_bufs) 14 { 15 struct vivid_buffer *tch_cap_buf = NULL; 16 17 spin_lock(&dev->slock); 18 if (!list_empty(&dev->touch_cap_active)) { 19 tch_cap_buf = list_entry(dev->touch_cap_active.next, 20 struct vivid_buffer, list); 21 list_del(&tch_cap_buf->list); 22 } 23 24 spin_unlock(&dev->slock); 25 26 if (tch_cap_buf) { 27 v4l2_ctrl_request_setup(tch_cap_buf->vb.vb2_buf.req_obj.req, 28 &dev->ctrl_hdl_touch_cap); 29 30 vivid_fillbuff_tch(dev, tch_cap_buf); 31 v4l2_ctrl_request_complete(tch_cap_buf->vb.vb2_buf.req_obj.req, 32 &dev->ctrl_hdl_touch_cap); 33 vb2_buffer_done(&tch_cap_buf->vb.vb2_buf, dev->dqbuf_error ? 34 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); 35 dprintk(dev, 2, "touch_cap buffer %d done\n", 36 tch_cap_buf->vb.vb2_buf.index); 37 38 tch_cap_buf->vb.vb2_buf.timestamp = ktime_get_ns() + dev->time_wrap_offset; 39 } 40 dev->dqbuf_error = false; 41 } 42 43 static int vivid_thread_touch_cap(void *data) 44 { 45 struct vivid_dev *dev = data; 46 u64 numerators_since_start; 47 u64 buffers_since_start; 48 u64 next_jiffies_since_start; 49 unsigned long jiffies_since_start; 50 unsigned long cur_jiffies; 51 unsigned int wait_jiffies; 52 unsigned int numerator; 53 unsigned int denominator; 54 int dropped_bufs; 55 56 dprintk(dev, 1, "Touch Capture Thread Start\n"); 57 58 set_freezable(); 59 60 /* Resets frame counters */ 61 dev->touch_cap_seq_offset = 0; 62 dev->touch_cap_seq_count = 0; 63 dev->touch_cap_seq_resync = false; 64 dev->jiffies_touch_cap = jiffies; 65 if (dev->time_wrap) 66 dev->time_wrap_offset = dev->time_wrap - ktime_get_ns(); 67 else 68 dev->time_wrap_offset = 0; 69 70 for (;;) { 71 try_to_freeze(); 72 if (kthread_should_stop()) 73 break; 74 75 if (!mutex_trylock(&dev->mutex)) { 76 schedule(); 77 continue; 78 } 79 cur_jiffies = jiffies; 80 if (dev->touch_cap_seq_resync) { 81 dev->jiffies_touch_cap = cur_jiffies; 82 dev->touch_cap_seq_offset = dev->touch_cap_seq_count + 1; 83 dev->touch_cap_seq_count = 0; 84 dev->cap_seq_resync = false; 85 } 86 denominator = dev->timeperframe_tch_cap.denominator; 87 numerator = dev->timeperframe_tch_cap.numerator; 88 89 /* Calculate the number of jiffies since we started streaming */ 90 jiffies_since_start = cur_jiffies - dev->jiffies_touch_cap; 91 /* Get the number of buffers streamed since the start */ 92 buffers_since_start = (u64)jiffies_since_start * denominator + 93 (HZ * numerator) / 2; 94 do_div(buffers_since_start, HZ * numerator); 95 96 /* 97 * After more than 0xf0000000 (rounded down to a multiple of 98 * 'jiffies-per-day' to ease jiffies_to_msecs calculation) 99 * jiffies have passed since we started streaming reset the 100 * counters and keep track of the sequence offset. 101 */ 102 if (jiffies_since_start > JIFFIES_RESYNC) { 103 dev->jiffies_touch_cap = cur_jiffies; 104 dev->cap_seq_offset = buffers_since_start; 105 buffers_since_start = 0; 106 } 107 dropped_bufs = buffers_since_start + dev->touch_cap_seq_offset - dev->touch_cap_seq_count; 108 dev->touch_cap_seq_count = buffers_since_start + dev->touch_cap_seq_offset; 109 dev->touch_cap_with_seq_wrap_count = 110 dev->touch_cap_seq_count - dev->touch_cap_seq_start; 111 112 vivid_thread_tch_cap_tick(dev, dropped_bufs); 113 114 /* 115 * Calculate the number of 'numerators' streamed 116 * since we started, including the current buffer. 117 */ 118 numerators_since_start = ++buffers_since_start * numerator; 119 120 /* And the number of jiffies since we started */ 121 jiffies_since_start = jiffies - dev->jiffies_touch_cap; 122 123 mutex_unlock(&dev->mutex); 124 125 /* 126 * Calculate when that next buffer is supposed to start 127 * in jiffies since we started streaming. 128 */ 129 next_jiffies_since_start = numerators_since_start * HZ + 130 denominator / 2; 131 do_div(next_jiffies_since_start, denominator); 132 /* If it is in the past, then just schedule asap */ 133 if (next_jiffies_since_start < jiffies_since_start) 134 next_jiffies_since_start = jiffies_since_start; 135 136 wait_jiffies = next_jiffies_since_start - jiffies_since_start; 137 while (jiffies - cur_jiffies < wait_jiffies && 138 !kthread_should_stop()) 139 schedule(); 140 } 141 dprintk(dev, 1, "Touch Capture Thread End\n"); 142 return 0; 143 } 144 145 int vivid_start_generating_touch_cap(struct vivid_dev *dev) 146 { 147 if (dev->kthread_touch_cap) { 148 dev->touch_cap_streaming = true; 149 return 0; 150 } 151 152 dev->touch_cap_seq_start = dev->seq_wrap * 128; 153 dev->kthread_touch_cap = kthread_run(vivid_thread_touch_cap, dev, 154 "%s-tch-cap", dev->v4l2_dev.name); 155 156 if (IS_ERR(dev->kthread_touch_cap)) { 157 int err = PTR_ERR(dev->kthread_touch_cap); 158 159 dev->kthread_touch_cap = NULL; 160 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n"); 161 return err; 162 } 163 dev->touch_cap_streaming = true; 164 dprintk(dev, 1, "returning from %s\n", __func__); 165 return 0; 166 } 167 168 void vivid_stop_generating_touch_cap(struct vivid_dev *dev) 169 { 170 if (!dev->kthread_touch_cap) 171 return; 172 173 dev->touch_cap_streaming = false; 174 175 while (!list_empty(&dev->touch_cap_active)) { 176 struct vivid_buffer *buf; 177 178 buf = list_entry(dev->touch_cap_active.next, 179 struct vivid_buffer, list); 180 list_del(&buf->list); 181 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req, 182 &dev->ctrl_hdl_touch_cap); 183 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 184 dprintk(dev, 2, "touch_cap buffer %d done\n", 185 buf->vb.vb2_buf.index); 186 } 187 188 kthread_stop(dev->kthread_touch_cap); 189 dev->kthread_touch_cap = NULL; 190 } 191