vsg-subdev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/hrtimer.h>
  14. #include <linux/time.h>
  15. #include <linux/list.h>
  16. #include <linux/slab.h>
  17. #include <media/videobuf2-core.h>
  18. #include "enc-subdev.h"
  19. #include "vsg-subdev.h"
  20. #include "wfd-util.h"
  21. #define DEFAULT_FRAME_INTERVAL (66*NSEC_PER_MSEC)
  22. #define DEFAULT_MAX_FRAME_INTERVAL (1*NSEC_PER_SEC)
  23. #define DEFAULT_MODE ((enum vsg_modes)VSG_MODE_CFR)
  24. #define MAX_BUFS_BUSY_WITH_ENC 5
  25. #define TICKS_PER_TIMEOUT 2
  26. static int vsg_release_input_buffer(struct vsg_context *context,
  27. struct vsg_buf_info *buf)
  28. {
  29. WFD_MSG_DBG("Releasing frame with ts %lld ms, paddr %p\n",
  30. timespec_to_ns(&buf->time),
  31. (void *)buf->mdp_buf_info.paddr);
  32. if (buf->flags & VSG_NEVER_RELEASE)
  33. WFD_MSG_WARN("Warning releasing buffer that's"
  34. "not supposed to be released\n");
  35. return context->vmops.release_input_frame(context->vmops.cbdata,
  36. buf);
  37. }
  38. static int vsg_encode_frame(struct vsg_context *context,
  39. struct vsg_buf_info *buf)
  40. {
  41. WFD_MSG_DBG("Encoding frame with ts %lld ms, paddr %p\n",
  42. timespec_to_ns(&buf->time),
  43. (void *)buf->mdp_buf_info.paddr);
  44. return context->vmops.encode_frame(context->vmops.cbdata,
  45. buf);
  46. }
  47. static void vsg_set_last_buffer(struct vsg_context *context,
  48. struct vsg_buf_info *buf)
  49. {
  50. if (buf->flags & VSG_NEVER_SET_LAST_BUFFER)
  51. WFD_MSG_WARN("Shouldn't be setting this to last buffer\n");
  52. context->last_buffer = buf;
  53. WFD_MSG_DBG("Setting last buffer to paddr %p\n",
  54. (void *)buf->mdp_buf_info.paddr);
  55. }
  56. static void vsg_encode_helper_func(struct work_struct *task)
  57. {
  58. struct vsg_encode_work *work =
  59. container_of(task, struct vsg_encode_work, work);
  60. /*
  61. * Note: don't need to lock for context below as we only
  62. * access fields that are "static".
  63. */
  64. int rc = vsg_encode_frame(work->context, work->buf);
  65. if (rc < 0) {
  66. mutex_lock(&work->context->mutex);
  67. work->context->state = VSG_STATE_ERROR;
  68. mutex_unlock(&work->context->mutex);
  69. }
  70. kfree(work);
  71. }
  72. static void vsg_work_func(struct work_struct *task)
  73. {
  74. struct vsg_work *work =
  75. container_of(task, struct vsg_work, work);
  76. struct vsg_encode_work *encode_work;
  77. struct vsg_context *context = work->context;
  78. struct vsg_buf_info *buf_info = NULL, *temp = NULL;
  79. int rc = 0, count = 0;
  80. mutex_lock(&context->mutex);
  81. if (list_empty(&context->free_queue.node)) {
  82. WFD_MSG_DBG("%s: queue empty doing nothing\n", __func__);
  83. goto err_skip_encode;
  84. } else if (context->state != VSG_STATE_STARTED) {
  85. WFD_MSG_DBG("%s: vsg is stopped or in error state "
  86. "doing nothing\n", __func__);
  87. goto err_skip_encode;
  88. }
  89. list_for_each_entry(temp, &context->busy_queue.node, node) {
  90. if (++count > MAX_BUFS_BUSY_WITH_ENC) {
  91. WFD_MSG_WARN(
  92. "Skipping encode, too many buffers with encoder\n");
  93. goto err_skip_encode;
  94. }
  95. }
  96. buf_info = list_first_entry(&context->free_queue.node,
  97. struct vsg_buf_info, node);
  98. list_del(&buf_info->node);
  99. INIT_LIST_HEAD(&buf_info->node);
  100. ktime_get_ts(&buf_info->time);
  101. if (work->work_delayed) {
  102. buf_info->time = timespec_sub(buf_info->time,
  103. context->delayed_frame_interval);
  104. }
  105. temp = NULL;
  106. list_for_each_entry(temp, &context->busy_queue.node, node) {
  107. if (mdp_buf_info_equals(&temp->mdp_buf_info,
  108. &buf_info->mdp_buf_info)) {
  109. temp->flags |= VSG_NEVER_RELEASE;
  110. }
  111. }
  112. if (context->last_buffer &&
  113. mdp_buf_info_equals(&context->last_buffer->mdp_buf_info,
  114. &buf_info->mdp_buf_info)) {
  115. context->last_buffer->flags |= VSG_NEVER_RELEASE;
  116. }
  117. encode_work = kmalloc(sizeof(*encode_work), GFP_KERNEL);
  118. encode_work->buf = buf_info;
  119. encode_work->context = context;
  120. INIT_WORK(&encode_work->work, vsg_encode_helper_func);
  121. rc = queue_work(context->work_queue, &encode_work->work);
  122. if (!rc) {
  123. WFD_MSG_ERR("Queueing buffer for encode failed\n");
  124. kfree(encode_work);
  125. encode_work = NULL;
  126. goto err_skip_encode;
  127. }
  128. buf_info->flags |= VSG_BUF_BEING_ENCODED;
  129. if (!(buf_info->flags & VSG_NEVER_SET_LAST_BUFFER)) {
  130. if (context->last_buffer) {
  131. struct vsg_buf_info *old_last_buffer =
  132. context->last_buffer;
  133. bool last_buf_with_us = old_last_buffer &&
  134. !(old_last_buffer->flags &
  135. VSG_BUF_BEING_ENCODED);
  136. bool can_release = old_last_buffer &&
  137. !(old_last_buffer->flags &
  138. VSG_NEVER_RELEASE);
  139. if (old_last_buffer && last_buf_with_us
  140. && can_release) {
  141. vsg_release_input_buffer(context,
  142. old_last_buffer);
  143. }
  144. if (last_buf_with_us)
  145. kfree(old_last_buffer);
  146. }
  147. vsg_set_last_buffer(context, buf_info);
  148. }
  149. list_add_tail(&buf_info->node, &context->busy_queue.node);
  150. err_skip_encode:
  151. mutex_unlock(&context->mutex);
  152. kfree(work);
  153. }
  154. static void vsg_timer_helper_func(struct work_struct *task)
  155. {
  156. struct vsg_work *work =
  157. container_of(task, struct vsg_work, work);
  158. struct vsg_work *new_work = NULL;
  159. struct vsg_context *context = work->context;
  160. int num_bufs_to_queue = 1, c = 0;
  161. mutex_lock(&context->mutex);
  162. if (context->state != VSG_STATE_STARTED)
  163. goto err_locked;
  164. if (list_empty(&context->free_queue.node)
  165. && context->last_buffer) {
  166. struct vsg_buf_info *info = NULL, *buf_to_encode = NULL;
  167. if (context->mode == VSG_MODE_CFR)
  168. num_bufs_to_queue = 1;
  169. else if (context->mode == VSG_MODE_VFR)
  170. num_bufs_to_queue = 2;
  171. for (c = 0; c < num_bufs_to_queue; ++c) {
  172. info = kzalloc(sizeof(*info), GFP_KERNEL);
  173. if (!info) {
  174. WFD_MSG_ERR("Couldn't allocate memory in %s\n",
  175. __func__);
  176. goto err_locked;
  177. }
  178. buf_to_encode = context->last_buffer;
  179. info->mdp_buf_info = buf_to_encode->mdp_buf_info;
  180. info->flags = 0;
  181. INIT_LIST_HEAD(&info->node);
  182. list_add_tail(&info->node, &context->free_queue.node);
  183. WFD_MSG_DBG("Regenerated frame with paddr %p\n",
  184. (void *)info->mdp_buf_info.paddr);
  185. }
  186. }
  187. for (c = 0; c < num_bufs_to_queue; ++c) {
  188. new_work = kzalloc(sizeof(*new_work), GFP_KERNEL);
  189. if (!new_work) {
  190. WFD_MSG_ERR("Unable to allocate memory"
  191. "to queue buffer\n");
  192. goto err_locked;
  193. }
  194. INIT_WORK(&new_work->work, vsg_work_func);
  195. new_work->context = context;
  196. new_work->work_delayed = work->work_delayed;
  197. queue_work(context->work_queue, &new_work->work);
  198. }
  199. err_locked:
  200. mutex_unlock(&context->mutex);
  201. kfree(work);
  202. }
  203. static enum hrtimer_restart vsg_threshold_timeout_func(struct hrtimer *timer)
  204. {
  205. struct vsg_context *context = NULL;
  206. struct vsg_work *task = NULL;
  207. int64_t max_frame_interval = 0;
  208. context = container_of(timer, struct vsg_context,
  209. threshold_timer);
  210. if (!context) {
  211. WFD_MSG_ERR("Context not proper in %s", __func__);
  212. goto threshold_err_no_context;
  213. }
  214. max_frame_interval = context->max_frame_interval;
  215. if (list_empty(&context->free_queue.node) && !context->vsync_wait) {
  216. context->vsync_wait = true;
  217. max_frame_interval = context->max_frame_interval /
  218. TICKS_PER_TIMEOUT;
  219. goto restart_timer;
  220. } else if (context->vsync_wait) {
  221. max_frame_interval = context->max_frame_interval /
  222. TICKS_PER_TIMEOUT;
  223. context->vsync_wait = false;
  224. }
  225. task = kzalloc(sizeof(*task), GFP_ATOMIC);
  226. if (!task) {
  227. WFD_MSG_ERR("Out of memory in %s", __func__);
  228. goto threshold_err_bad_param;
  229. }
  230. INIT_WORK(&task->work, vsg_timer_helper_func);
  231. task->context = context;
  232. task->work_delayed = false;
  233. if (max_frame_interval < context->max_frame_interval)
  234. task->work_delayed = true;
  235. queue_work(context->work_queue, &task->work);
  236. restart_timer:
  237. threshold_err_bad_param:
  238. hrtimer_forward_now(&context->threshold_timer, ns_to_ktime(
  239. max_frame_interval));
  240. return HRTIMER_RESTART;
  241. threshold_err_no_context:
  242. return HRTIMER_NORESTART;
  243. }
  244. int vsg_init(struct v4l2_subdev *sd, u32 val)
  245. {
  246. return 0;
  247. }
  248. static int vsg_open(struct v4l2_subdev *sd, void *arg)
  249. {
  250. struct vsg_context *context = NULL;
  251. if (!arg || !sd)
  252. return -EINVAL;
  253. context = kzalloc(sizeof(*context), GFP_KERNEL);
  254. INIT_LIST_HEAD(&context->free_queue.node);
  255. INIT_LIST_HEAD(&context->busy_queue.node);
  256. context->vmops = *(struct vsg_msg_ops *)arg;
  257. context->work_queue = create_singlethread_workqueue("v4l-vsg");
  258. context->frame_interval = DEFAULT_FRAME_INTERVAL;
  259. context->max_frame_interval = DEFAULT_MAX_FRAME_INTERVAL;
  260. hrtimer_init(&context->threshold_timer, CLOCK_MONOTONIC,
  261. HRTIMER_MODE_REL);
  262. context->threshold_timer.function = vsg_threshold_timeout_func;
  263. context->last_buffer = NULL;
  264. context->mode = DEFAULT_MODE;
  265. context->state = VSG_STATE_NONE;
  266. context->vsync_wait = false;
  267. mutex_init(&context->mutex);
  268. sd->dev_priv = context;
  269. return 0;
  270. }
  271. static int vsg_close(struct v4l2_subdev *sd)
  272. {
  273. struct vsg_context *context = NULL;
  274. if (!sd)
  275. return -EINVAL;
  276. context = (struct vsg_context *)sd->dev_priv;
  277. destroy_workqueue(context->work_queue);
  278. kfree(context);
  279. return 0;
  280. }
  281. static int vsg_start(struct v4l2_subdev *sd)
  282. {
  283. struct vsg_context *context = NULL;
  284. int rc = 0;
  285. if (!sd) {
  286. WFD_MSG_ERR("ERROR, invalid arguments into %s\n", __func__);
  287. return -EINVAL;
  288. }
  289. context = (struct vsg_context *)sd->dev_priv;
  290. mutex_lock(&context->mutex);
  291. if (context->state == VSG_STATE_STARTED) {
  292. WFD_MSG_ERR("VSG not stopped, start not allowed\n");
  293. rc = -EINPROGRESS;
  294. goto err_bad_state;
  295. } else if (context->state == VSG_STATE_ERROR) {
  296. WFD_MSG_ERR("VSG in error state, not allowed to restart\n");
  297. rc = -ENOTRECOVERABLE;
  298. goto err_bad_state;
  299. }
  300. context->state = VSG_STATE_STARTED;
  301. hrtimer_start(&context->threshold_timer, ns_to_ktime(context->
  302. max_frame_interval), HRTIMER_MODE_REL);
  303. err_bad_state:
  304. mutex_unlock(&context->mutex);
  305. return rc;
  306. }
  307. static int vsg_stop(struct v4l2_subdev *sd)
  308. {
  309. struct vsg_context *context = NULL;
  310. if (!sd) {
  311. WFD_MSG_ERR("ERROR, invalid arguments into %s\n", __func__);
  312. return -EINVAL;
  313. }
  314. context = (struct vsg_context *)sd->dev_priv;
  315. mutex_lock(&context->mutex);
  316. context->state = VSG_STATE_STOPPED;
  317. { /*delete pending buffers as we're not going to encode them*/
  318. struct list_head *pos, *next;
  319. list_for_each_safe(pos, next, &context->free_queue.node) {
  320. struct vsg_buf_info *temp =
  321. list_entry(pos, struct vsg_buf_info, node);
  322. list_del(&temp->node);
  323. kfree(temp);
  324. }
  325. }
  326. hrtimer_cancel(&context->threshold_timer);
  327. mutex_unlock(&context->mutex);
  328. flush_workqueue(context->work_queue);
  329. return 0;
  330. }
  331. static long vsg_queue_buffer(struct v4l2_subdev *sd, void *arg)
  332. {
  333. struct vsg_context *context = NULL;
  334. struct vsg_buf_info *buf_info = kzalloc(sizeof(*buf_info), GFP_KERNEL);
  335. int rc = 0;
  336. bool push = false;
  337. if (!arg || !sd) {
  338. WFD_MSG_ERR("ERROR, invalid arguments into %s\n", __func__);
  339. rc = -EINVAL;
  340. goto queue_err_bad_param;
  341. } else if (!buf_info) {
  342. WFD_MSG_ERR("ERROR, out of memory in %s\n", __func__);
  343. rc = -ENOMEM;
  344. goto queue_err_bad_param;
  345. }
  346. context = (struct vsg_context *)sd->dev_priv;
  347. mutex_lock(&context->mutex);
  348. *buf_info = *(struct vsg_buf_info *)arg;
  349. INIT_LIST_HEAD(&buf_info->node);
  350. buf_info->flags = 0;
  351. ktime_get_ts(&buf_info->time);
  352. WFD_MSG_DBG("Queue frame with paddr %p\n",
  353. (void *)buf_info->mdp_buf_info.paddr);
  354. { /*return pending buffers as we're not going to encode them*/
  355. struct list_head *pos, *next;
  356. list_for_each_safe(pos, next, &context->free_queue.node) {
  357. struct vsg_buf_info *temp =
  358. list_entry(pos, struct vsg_buf_info, node);
  359. bool is_last_buffer = context->last_buffer &&
  360. mdp_buf_info_equals(
  361. &context->last_buffer->mdp_buf_info,
  362. &temp->mdp_buf_info);
  363. list_del(&temp->node);
  364. if (!is_last_buffer &&
  365. !(temp->flags & VSG_NEVER_RELEASE)) {
  366. vsg_release_input_buffer(context, temp);
  367. }
  368. kfree(temp);
  369. }
  370. }
  371. list_add_tail(&buf_info->node, &context->free_queue.node);
  372. if (context->mode == VSG_MODE_VFR) {
  373. if (!context->last_buffer)
  374. push = true;
  375. else {
  376. struct timespec diff = timespec_sub(buf_info->time,
  377. context->last_buffer->time);
  378. struct timespec temp = ns_to_timespec(
  379. context->frame_interval -
  380. context->frame_interval_variance);
  381. if (timespec_compare(&diff, &temp) >= 0)
  382. push = true;
  383. }
  384. } else if (context->mode == VSG_MODE_CFR) {
  385. if (!context->last_buffer) {
  386. push = true;
  387. /*
  388. * We need to reset the timer after pushing the buffer
  389. * otherwise, diff between two consecutive frames might
  390. * be less than max_frame_interval (for just one sample)
  391. */
  392. hrtimer_forward_now(&context->threshold_timer,
  393. ns_to_ktime(context->max_frame_interval));
  394. }
  395. }
  396. if (push) {
  397. struct vsg_work *new_work =
  398. kzalloc(sizeof(*new_work), GFP_KERNEL);
  399. INIT_WORK(&new_work->work, vsg_work_func);
  400. new_work->context = context;
  401. queue_work(context->work_queue, &new_work->work);
  402. }
  403. mutex_unlock(&context->mutex);
  404. queue_err_bad_param:
  405. if (rc < 0)
  406. kfree(buf_info);
  407. return rc;
  408. }
  409. static long vsg_return_ip_buffer(struct v4l2_subdev *sd, void *arg)
  410. {
  411. struct vsg_context *context = NULL;
  412. struct vsg_buf_info *buf_info = NULL, *temp = NULL,
  413. /* last buffer sent for encoding */
  414. *last_buffer = NULL,
  415. /* buffer we expected to get back, ideally ==
  416. * last_buffer, but might not be if sequence is
  417. * encode, encode, return */
  418. *expected_buffer = NULL,
  419. /* buffer that we've sent for encoding at some point */
  420. *known_buffer = NULL;
  421. bool is_last_buffer = false;
  422. int rc = 0;
  423. if (!arg || !sd) {
  424. WFD_MSG_ERR("ERROR, invalid arguments into %s\n", __func__);
  425. rc = -EINVAL;
  426. goto return_ip_buf_err_bad_param;
  427. }
  428. context = (struct vsg_context *)sd->dev_priv;
  429. mutex_lock(&context->mutex);
  430. buf_info = (struct vsg_buf_info *)arg;
  431. last_buffer = context->last_buffer;
  432. WFD_MSG_DBG("Return frame with paddr %p\n",
  433. (void *)buf_info->mdp_buf_info.paddr);
  434. if (!list_empty(&context->busy_queue.node)) {
  435. expected_buffer = list_first_entry(&context->busy_queue.node,
  436. struct vsg_buf_info, node);
  437. }
  438. list_for_each_entry(temp, &context->busy_queue.node, node) {
  439. if (mdp_buf_info_equals(&temp->mdp_buf_info,
  440. &buf_info->mdp_buf_info)) {
  441. known_buffer = temp;
  442. break;
  443. }
  444. }
  445. if (!expected_buffer || !known_buffer) {
  446. WFD_MSG_ERR("Unexpectedly received buffer from enc with "
  447. "paddr %p\n", (void *)buf_info->mdp_buf_info.paddr);
  448. rc = -EBADHANDLE;
  449. goto return_ip_buf_bad_buf;
  450. } else if (known_buffer != expected_buffer) {
  451. /* Buffers can come back out of order if encoder decides to drop
  452. * a frame */
  453. WFD_MSG_DBG(
  454. "Got a buffer (%p) out of order. Preferred to get %p\n",
  455. (void *)known_buffer->mdp_buf_info.paddr,
  456. (void *)expected_buffer->mdp_buf_info.paddr);
  457. }
  458. known_buffer->flags &= ~VSG_BUF_BEING_ENCODED;
  459. is_last_buffer = context->last_buffer &&
  460. mdp_buf_info_equals(
  461. &context->last_buffer->mdp_buf_info,
  462. &known_buffer->mdp_buf_info);
  463. list_del(&known_buffer->node);
  464. if (!is_last_buffer &&
  465. !(known_buffer->flags & VSG_NEVER_RELEASE)) {
  466. vsg_release_input_buffer(context, known_buffer);
  467. kfree(known_buffer);
  468. }
  469. return_ip_buf_bad_buf:
  470. mutex_unlock(&context->mutex);
  471. return_ip_buf_err_bad_param:
  472. return rc;
  473. }
  474. static long vsg_set_frame_interval(struct v4l2_subdev *sd, void *arg)
  475. {
  476. struct vsg_context *context = NULL;
  477. int64_t interval;
  478. if (!arg || !sd) {
  479. WFD_MSG_ERR("ERROR, invalid arguments into %s\n", __func__);
  480. return -EINVAL;
  481. }
  482. context = (struct vsg_context *)sd->dev_priv;
  483. interval = *(int64_t *)arg;
  484. if (interval <= 0) {
  485. WFD_MSG_ERR("ERROR, invalid interval %lld into %s\n",
  486. interval, __func__);
  487. return -EINVAL;
  488. }
  489. mutex_lock(&context->mutex);
  490. context->frame_interval = interval;
  491. if (interval > context->max_frame_interval) {
  492. WFD_MSG_WARN("Changing max frame interval from %lld to %lld\n",
  493. context->max_frame_interval, interval);
  494. context->max_frame_interval = interval;
  495. }
  496. context->delayed_frame_interval =
  497. ns_to_timespec(context->frame_interval / TICKS_PER_TIMEOUT);
  498. mutex_unlock(&context->mutex);
  499. return 0;
  500. }
  501. static long vsg_get_frame_interval(struct v4l2_subdev *sd, void *arg)
  502. {
  503. struct vsg_context *context = NULL;
  504. if (!arg || !sd) {
  505. WFD_MSG_ERR("ERROR, invalid arguments into %s\n", __func__);
  506. return -EINVAL;
  507. }
  508. context = (struct vsg_context *)sd->dev_priv;
  509. mutex_lock(&context->mutex);
  510. *(int64_t *)arg = context->frame_interval;
  511. mutex_unlock(&context->mutex);
  512. return 0;
  513. }
  514. static long vsg_set_max_frame_interval(struct v4l2_subdev *sd, void *arg)
  515. {
  516. struct vsg_context *context = NULL;
  517. int64_t interval;
  518. if (!arg || !sd) {
  519. WFD_MSG_ERR("ERROR, invalid arguments into %s\n", __func__);
  520. return -EINVAL;
  521. }
  522. context = (struct vsg_context *)sd->dev_priv;
  523. interval = *(int64_t *)arg;
  524. if (interval <= 0) {
  525. WFD_MSG_ERR("ERROR, invalid interval %lld into %s\n",
  526. interval, __func__);
  527. return -EINVAL;
  528. }
  529. mutex_lock(&context->mutex);
  530. context->max_frame_interval = interval;
  531. if (interval < context->frame_interval) {
  532. WFD_MSG_WARN("Changing frame interval from %lld to %lld\n",
  533. context->frame_interval, interval);
  534. context->frame_interval = interval;
  535. }
  536. mutex_unlock(&context->mutex);
  537. return 0;
  538. }
  539. static long vsg_get_max_frame_interval(struct v4l2_subdev *sd, void *arg)
  540. {
  541. struct vsg_context *context = NULL;
  542. if (!arg || !sd) {
  543. WFD_MSG_ERR("ERROR, invalid arguments into %s\n", __func__);
  544. return -EINVAL;
  545. }
  546. context = (struct vsg_context *)sd->dev_priv;
  547. mutex_lock(&context->mutex);
  548. *(int64_t *)arg = context->max_frame_interval;
  549. mutex_unlock(&context->mutex);
  550. return 0;
  551. }
  552. static long vsg_set_frame_interval_variance(struct v4l2_subdev *sd, void *arg)
  553. {
  554. struct vsg_context *context = NULL;
  555. int64_t variance;
  556. if (!arg || !sd) {
  557. WFD_MSG_ERR("ERROR, invalid arguments into %s\n", __func__);
  558. return -EINVAL;
  559. }
  560. context = (struct vsg_context *)sd->dev_priv;
  561. variance = *(int64_t *)arg;
  562. if (variance < 0 || variance > 100) {
  563. WFD_MSG_ERR("ERROR, invalid variance %lld%% into %s\n",
  564. variance, __func__);
  565. return -EINVAL;
  566. } else if (context->mode == VSG_MODE_CFR) {
  567. WFD_MSG_ERR("Setting FPS variance not supported in CFR mode\n");
  568. return -ENOTSUPP;
  569. }
  570. mutex_lock(&context->mutex);
  571. /* Convert from percentage to a value in nano seconds */
  572. variance *= context->frame_interval;
  573. do_div(variance, 100);
  574. context->frame_interval_variance = variance;
  575. mutex_unlock(&context->mutex);
  576. return 0;
  577. }
  578. static long vsg_get_frame_interval_variance(struct v4l2_subdev *sd, void *arg)
  579. {
  580. struct vsg_context *context = NULL;
  581. int64_t variance;
  582. if (!arg || !sd) {
  583. WFD_MSG_ERR("ERROR, invalid arguments into %s\n", __func__);
  584. return -EINVAL;
  585. }
  586. context = (struct vsg_context *)sd->dev_priv;
  587. mutex_lock(&context->mutex);
  588. variance = context->frame_interval_variance * 100;
  589. do_div(variance, context->frame_interval);
  590. *(int64_t *)arg = variance;
  591. mutex_unlock(&context->mutex);
  592. return 0;
  593. }
  594. static long vsg_set_mode(struct v4l2_subdev *sd, void *arg)
  595. {
  596. struct vsg_context *context = NULL;
  597. enum vsg_modes *mode = NULL;
  598. int rc = 0;
  599. if (!arg || !sd) {
  600. WFD_MSG_ERR("ERROR, invalid arguments into %s\n", __func__);
  601. rc = -EINVAL;
  602. goto set_mode_err_bad_parm;
  603. }
  604. context = (struct vsg_context *)sd->dev_priv;
  605. mutex_lock(&context->mutex);
  606. mode = arg;
  607. switch (*mode) {
  608. case VSG_MODE_CFR:
  609. context->max_frame_interval = context->frame_interval;
  610. /*fall through*/
  611. case VSG_MODE_VFR:
  612. context->mode = *mode;
  613. break;
  614. default:
  615. context->mode = DEFAULT_MODE;
  616. rc = -EINVAL;
  617. goto set_mode_err_bad_mode;
  618. break;
  619. }
  620. set_mode_err_bad_mode:
  621. mutex_unlock(&context->mutex);
  622. set_mode_err_bad_parm:
  623. return rc;
  624. }
  625. long vsg_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  626. {
  627. int rc = 0;
  628. WFD_MSG_DBG("VSG ioctl: %d\n", cmd);
  629. if (sd == NULL)
  630. return -EINVAL;
  631. switch (cmd) {
  632. case VSG_OPEN:
  633. rc = vsg_open(sd, arg);
  634. break;
  635. case VSG_CLOSE:
  636. rc = vsg_close(sd);
  637. break;
  638. case VSG_START:
  639. rc = vsg_start(sd);
  640. break;
  641. case VSG_STOP:
  642. rc = vsg_stop(sd);
  643. break;
  644. case VSG_Q_BUFFER:
  645. rc = vsg_queue_buffer(sd, arg);
  646. break;
  647. case VSG_RETURN_IP_BUFFER:
  648. rc = vsg_return_ip_buffer(sd, arg);
  649. break;
  650. case VSG_GET_FRAME_INTERVAL:
  651. rc = vsg_get_frame_interval(sd, arg);
  652. break;
  653. case VSG_SET_FRAME_INTERVAL:
  654. rc = vsg_set_frame_interval(sd, arg);
  655. break;
  656. case VSG_SET_FRAME_INTERVAL_VARIANCE:
  657. rc = vsg_set_frame_interval_variance(sd, arg);
  658. break;
  659. case VSG_GET_FRAME_INTERVAL_VARIANCE:
  660. rc = vsg_get_frame_interval_variance(sd, arg);
  661. break;
  662. case VSG_GET_MAX_FRAME_INTERVAL:
  663. rc = vsg_get_max_frame_interval(sd, arg);
  664. break;
  665. case VSG_SET_MAX_FRAME_INTERVAL:
  666. rc = vsg_set_max_frame_interval(sd, arg);
  667. break;
  668. case VSG_SET_MODE:
  669. rc = vsg_set_mode(sd, arg);
  670. break;
  671. default:
  672. rc = -ENOTSUPP;
  673. break;
  674. }
  675. return rc;
  676. }