cam_sync.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/irqflags.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/debugfs.h>
  11. #if IS_REACHABLE(CONFIG_MSM_GLOBAL_SYNX)
  12. #include <synx_api.h>
  13. #endif
  14. #include "cam_sync_util.h"
  15. #include "cam_debug_util.h"
  16. #include "cam_common_util.h"
  17. #include "cam_compat.h"
  18. #include "camera_main.h"
  19. #include "cam_req_mgr_workq.h"
  20. struct sync_device *sync_dev;
  21. /*
  22. * Flag to determine whether to enqueue cb of a
  23. * signaled fence onto the workq or invoke it
  24. * directly in the same context
  25. */
  26. static bool trigger_cb_without_switch;
  27. static void cam_sync_print_fence_table(void)
  28. {
  29. int idx;
  30. for (idx = 0; idx < CAM_SYNC_MAX_OBJS; idx++) {
  31. spin_lock_bh(&sync_dev->row_spinlocks[idx]);
  32. CAM_INFO(CAM_SYNC,
  33. "index[%u]: sync_id=%d, name=%s, type=%d, state=%d, ref_cnt=%d",
  34. idx,
  35. sync_dev->sync_table[idx].sync_id,
  36. sync_dev->sync_table[idx].name,
  37. sync_dev->sync_table[idx].type,
  38. sync_dev->sync_table[idx].state,
  39. atomic_read(&sync_dev->sync_table[idx].ref_cnt));
  40. spin_unlock_bh(&sync_dev->row_spinlocks[idx]);
  41. }
  42. }
  43. int cam_sync_create(int32_t *sync_obj, const char *name)
  44. {
  45. int rc;
  46. long idx;
  47. bool bit;
  48. do {
  49. idx = find_first_zero_bit(sync_dev->bitmap, CAM_SYNC_MAX_OBJS);
  50. if (idx >= CAM_SYNC_MAX_OBJS) {
  51. CAM_ERR(CAM_SYNC,
  52. "Error: Unable to create sync idx = %d sync name = %s reached max!",
  53. idx, name);
  54. cam_sync_print_fence_table();
  55. return -ENOMEM;
  56. }
  57. CAM_DBG(CAM_SYNC, "Index location available at idx: %ld", idx);
  58. bit = test_and_set_bit(idx, sync_dev->bitmap);
  59. } while (bit);
  60. spin_lock_bh(&sync_dev->row_spinlocks[idx]);
  61. rc = cam_sync_init_row(sync_dev->sync_table, idx, name,
  62. CAM_SYNC_TYPE_INDV);
  63. if (rc) {
  64. CAM_ERR(CAM_SYNC, "Error: Unable to init row at idx = %ld",
  65. idx);
  66. clear_bit(idx, sync_dev->bitmap);
  67. spin_unlock_bh(&sync_dev->row_spinlocks[idx]);
  68. return -EINVAL;
  69. }
  70. *sync_obj = idx;
  71. CAM_DBG(CAM_SYNC, "sync_obj: %s[%i]", name, *sync_obj);
  72. spin_unlock_bh(&sync_dev->row_spinlocks[idx]);
  73. return rc;
  74. }
  75. int cam_sync_register_callback(sync_callback cb_func,
  76. void *userdata, int32_t sync_obj)
  77. {
  78. struct sync_callback_info *sync_cb;
  79. struct sync_table_row *row = NULL;
  80. int status = 0;
  81. if (sync_obj >= CAM_SYNC_MAX_OBJS || sync_obj <= 0 || !cb_func)
  82. return -EINVAL;
  83. spin_lock_bh(&sync_dev->row_spinlocks[sync_obj]);
  84. row = sync_dev->sync_table + sync_obj;
  85. if (row->state == CAM_SYNC_STATE_INVALID) {
  86. CAM_ERR(CAM_SYNC,
  87. "Error: accessing an uninitialized sync obj %s[%d]",
  88. row->name,
  89. sync_obj);
  90. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  91. return -EINVAL;
  92. }
  93. sync_cb = kzalloc(sizeof(*sync_cb), GFP_ATOMIC);
  94. if (!sync_cb) {
  95. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  96. return -ENOMEM;
  97. }
  98. /* Trigger callback if sync object is already in SIGNALED state */
  99. if (((row->state == CAM_SYNC_STATE_SIGNALED_SUCCESS) ||
  100. (row->state == CAM_SYNC_STATE_SIGNALED_ERROR) ||
  101. (row->state == CAM_SYNC_STATE_SIGNALED_CANCEL)) &&
  102. (!row->remaining)) {
  103. if (trigger_cb_without_switch) {
  104. CAM_DBG(CAM_SYNC, "Invoke callback for sync object:%s[%d]",
  105. row->name,
  106. sync_obj);
  107. status = row->state;
  108. kfree(sync_cb);
  109. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  110. cb_func(sync_obj, status, userdata);
  111. } else {
  112. sync_cb->callback_func = cb_func;
  113. sync_cb->cb_data = userdata;
  114. sync_cb->sync_obj = sync_obj;
  115. INIT_WORK(&sync_cb->cb_dispatch_work,
  116. cam_sync_util_cb_dispatch);
  117. sync_cb->status = row->state;
  118. CAM_DBG(CAM_SYNC, "Enqueue callback for sync object:%s[%d]",
  119. row->name,
  120. sync_cb->sync_obj);
  121. sync_cb->workq_scheduled_ts = ktime_get();
  122. queue_work(sync_dev->work_queue,
  123. &sync_cb->cb_dispatch_work);
  124. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  125. }
  126. return 0;
  127. }
  128. sync_cb->callback_func = cb_func;
  129. sync_cb->cb_data = userdata;
  130. sync_cb->sync_obj = sync_obj;
  131. INIT_WORK(&sync_cb->cb_dispatch_work, cam_sync_util_cb_dispatch);
  132. list_add_tail(&sync_cb->list, &row->callback_list);
  133. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  134. return 0;
  135. }
  136. int cam_sync_deregister_callback(sync_callback cb_func,
  137. void *userdata, int32_t sync_obj)
  138. {
  139. struct sync_table_row *row = NULL;
  140. struct sync_callback_info *sync_cb, *temp;
  141. bool found = false;
  142. if (sync_obj >= CAM_SYNC_MAX_OBJS || sync_obj <= 0)
  143. return -EINVAL;
  144. spin_lock_bh(&sync_dev->row_spinlocks[sync_obj]);
  145. row = sync_dev->sync_table + sync_obj;
  146. if (row->state == CAM_SYNC_STATE_INVALID) {
  147. CAM_ERR(CAM_SYNC,
  148. "Error: accessing an uninitialized sync obj = %s[%d]",
  149. row->name,
  150. sync_obj);
  151. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  152. return -EINVAL;
  153. }
  154. CAM_DBG(CAM_SYNC, "deregistered callback for sync object:%s[%d]",
  155. row->name,
  156. sync_obj);
  157. list_for_each_entry_safe(sync_cb, temp, &row->callback_list, list) {
  158. if (sync_cb->callback_func == cb_func &&
  159. sync_cb->cb_data == userdata) {
  160. list_del_init(&sync_cb->list);
  161. kfree(sync_cb);
  162. found = true;
  163. }
  164. }
  165. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  166. return found ? 0 : -ENOENT;
  167. }
  168. int cam_sync_signal(int32_t sync_obj, uint32_t status, uint32_t event_cause)
  169. {
  170. struct sync_table_row *row = NULL;
  171. struct sync_table_row *parent_row = NULL;
  172. struct sync_parent_info *parent_info, *temp_parent_info;
  173. struct list_head parents_list;
  174. int rc = 0;
  175. if (sync_obj >= CAM_SYNC_MAX_OBJS || sync_obj <= 0) {
  176. CAM_ERR(CAM_SYNC, "Error: Out of range sync obj (0 <= %d < %d)",
  177. sync_obj, CAM_SYNC_MAX_OBJS);
  178. return -EINVAL;
  179. }
  180. row = sync_dev->sync_table + sync_obj;
  181. spin_lock_bh(&sync_dev->row_spinlocks[sync_obj]);
  182. if (row->state == CAM_SYNC_STATE_INVALID) {
  183. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  184. CAM_ERR(CAM_SYNC,
  185. "Error: accessing an uninitialized sync obj = %s[%d]",
  186. row->name,
  187. sync_obj);
  188. return -EINVAL;
  189. }
  190. if (row->type == CAM_SYNC_TYPE_GROUP) {
  191. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  192. CAM_ERR(CAM_SYNC,
  193. "Error: Signaling a GROUP sync object = %s[%d]",
  194. row->name,
  195. sync_obj);
  196. return -EINVAL;
  197. }
  198. if (row->state != CAM_SYNC_STATE_ACTIVE) {
  199. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  200. CAM_ERR(CAM_SYNC,
  201. "Error: Sync object already signaled sync_obj = %s[%d]",
  202. row->name,
  203. sync_obj);
  204. return -EALREADY;
  205. }
  206. if ((status != CAM_SYNC_STATE_SIGNALED_SUCCESS) &&
  207. (status != CAM_SYNC_STATE_SIGNALED_ERROR) &&
  208. (status != CAM_SYNC_STATE_SIGNALED_CANCEL)) {
  209. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  210. CAM_ERR(CAM_SYNC,
  211. "Error: signaling with undefined status = %d event reason = %u",
  212. status, event_cause);
  213. return -EINVAL;
  214. }
  215. if (!atomic_dec_and_test(&row->ref_cnt)) {
  216. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  217. return 0;
  218. }
  219. row->state = status;
  220. cam_sync_util_dispatch_signaled_cb(sync_obj, status, event_cause);
  221. /* copy parent list to local and release child lock */
  222. INIT_LIST_HEAD(&parents_list);
  223. list_splice_init(&row->parents_list, &parents_list);
  224. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  225. if (list_empty(&parents_list))
  226. return 0;
  227. /*
  228. * Now iterate over all parents of this object and if they too need to
  229. * be signaled dispatch cb's
  230. */
  231. list_for_each_entry_safe(parent_info,
  232. temp_parent_info,
  233. &parents_list,
  234. list) {
  235. parent_row = sync_dev->sync_table + parent_info->sync_id;
  236. spin_lock_bh(&sync_dev->row_spinlocks[parent_info->sync_id]);
  237. parent_row->remaining--;
  238. rc = cam_sync_util_update_parent_state(
  239. parent_row,
  240. status);
  241. if (rc) {
  242. CAM_ERR(CAM_SYNC, "Invalid parent state %d",
  243. parent_row->state);
  244. spin_unlock_bh(
  245. &sync_dev->row_spinlocks[parent_info->sync_id]);
  246. kfree(parent_info);
  247. continue;
  248. }
  249. if (!parent_row->remaining)
  250. cam_sync_util_dispatch_signaled_cb(
  251. parent_info->sync_id, parent_row->state,
  252. event_cause);
  253. spin_unlock_bh(&sync_dev->row_spinlocks[parent_info->sync_id]);
  254. list_del_init(&parent_info->list);
  255. kfree(parent_info);
  256. }
  257. return 0;
  258. }
  259. int cam_sync_merge(int32_t *sync_obj, uint32_t num_objs, int32_t *merged_obj)
  260. {
  261. int rc;
  262. long idx = 0;
  263. bool bit;
  264. int i = 0;
  265. if (!sync_obj || !merged_obj) {
  266. CAM_ERR(CAM_SYNC, "Invalid pointer(s)");
  267. return -EINVAL;
  268. }
  269. if (num_objs <= 1) {
  270. CAM_ERR(CAM_SYNC, "Single object merge is not allowed");
  271. return -EINVAL;
  272. }
  273. if (cam_common_util_remove_duplicate_arr(sync_obj, num_objs)
  274. != num_objs) {
  275. CAM_ERR(CAM_SYNC, "The obj list has duplicate fence");
  276. return -EINVAL;
  277. }
  278. for (i = 0; i < num_objs; i++) {
  279. rc = cam_sync_check_valid(sync_obj[i]);
  280. if (rc) {
  281. CAM_ERR(CAM_SYNC, "Sync_obj[%d] %d valid check fail",
  282. i, sync_obj[i]);
  283. return rc;
  284. }
  285. }
  286. do {
  287. idx = find_first_zero_bit(sync_dev->bitmap, CAM_SYNC_MAX_OBJS);
  288. if (idx >= CAM_SYNC_MAX_OBJS)
  289. return -ENOMEM;
  290. bit = test_and_set_bit(idx, sync_dev->bitmap);
  291. } while (bit);
  292. spin_lock_bh(&sync_dev->row_spinlocks[idx]);
  293. rc = cam_sync_init_group_object(sync_dev->sync_table,
  294. idx, sync_obj,
  295. num_objs);
  296. if (rc < 0) {
  297. CAM_ERR(CAM_SYNC, "Error: Unable to init row at idx = %ld",
  298. idx);
  299. clear_bit(idx, sync_dev->bitmap);
  300. spin_unlock_bh(&sync_dev->row_spinlocks[idx]);
  301. return -EINVAL;
  302. }
  303. CAM_DBG(CAM_SYNC, "Init row at idx:%ld to merge objects", idx);
  304. *merged_obj = idx;
  305. spin_unlock_bh(&sync_dev->row_spinlocks[idx]);
  306. return 0;
  307. }
  308. int cam_sync_get_obj_ref(int32_t sync_obj)
  309. {
  310. struct sync_table_row *row = NULL;
  311. if (sync_obj >= CAM_SYNC_MAX_OBJS || sync_obj <= 0)
  312. return -EINVAL;
  313. row = sync_dev->sync_table + sync_obj;
  314. spin_lock(&sync_dev->row_spinlocks[sync_obj]);
  315. if (row->state != CAM_SYNC_STATE_ACTIVE) {
  316. spin_unlock(&sync_dev->row_spinlocks[sync_obj]);
  317. CAM_ERR(CAM_SYNC,
  318. "Error: accessing an uninitialized sync obj = %s[%d]",
  319. row->name,
  320. sync_obj);
  321. return -EINVAL;
  322. }
  323. atomic_inc(&row->ref_cnt);
  324. spin_unlock(&sync_dev->row_spinlocks[sync_obj]);
  325. CAM_DBG(CAM_SYNC, "get ref for obj %d", sync_obj);
  326. return 0;
  327. }
  328. int cam_sync_put_obj_ref(int32_t sync_obj)
  329. {
  330. struct sync_table_row *row = NULL;
  331. if (sync_obj >= CAM_SYNC_MAX_OBJS || sync_obj <= 0)
  332. return -EINVAL;
  333. row = sync_dev->sync_table + sync_obj;
  334. atomic_dec(&row->ref_cnt);
  335. CAM_DBG(CAM_SYNC, "put ref for obj %d", sync_obj);
  336. return 0;
  337. }
  338. int cam_sync_destroy(int32_t sync_obj)
  339. {
  340. return cam_sync_deinit_object(sync_dev->sync_table, sync_obj);
  341. }
  342. int cam_sync_check_valid(int32_t sync_obj)
  343. {
  344. struct sync_table_row *row = NULL;
  345. if (sync_obj >= CAM_SYNC_MAX_OBJS || sync_obj <= 0)
  346. return -EINVAL;
  347. row = sync_dev->sync_table + sync_obj;
  348. if (!test_bit(sync_obj, sync_dev->bitmap)) {
  349. CAM_ERR(CAM_SYNC, "Error: Released sync obj received %s[%d]",
  350. row->name,
  351. sync_obj);
  352. return -EINVAL;
  353. }
  354. if (row->state == CAM_SYNC_STATE_INVALID) {
  355. CAM_ERR(CAM_SYNC,
  356. "Error: accessing an uninitialized sync obj = %s[%d]",
  357. row->name,
  358. sync_obj);
  359. return -EINVAL;
  360. }
  361. return 0;
  362. }
  363. int cam_sync_wait(int32_t sync_obj, uint64_t timeout_ms)
  364. {
  365. unsigned long timeleft;
  366. int rc = -EINVAL;
  367. struct sync_table_row *row = NULL;
  368. if (sync_obj >= CAM_SYNC_MAX_OBJS || sync_obj <= 0)
  369. return -EINVAL;
  370. row = sync_dev->sync_table + sync_obj;
  371. if (row->state == CAM_SYNC_STATE_INVALID) {
  372. CAM_ERR(CAM_SYNC,
  373. "Error: accessing an uninitialized sync obj = %s[%d]",
  374. row->name,
  375. sync_obj);
  376. return -EINVAL;
  377. }
  378. timeleft = cam_common_wait_for_completion_timeout(&row->signaled,
  379. msecs_to_jiffies(timeout_ms));
  380. if (!timeleft) {
  381. CAM_ERR(CAM_SYNC,
  382. "Error: timed out for sync obj = %s[%d]", row->name, sync_obj);
  383. rc = -ETIMEDOUT;
  384. } else {
  385. switch (row->state) {
  386. case CAM_SYNC_STATE_INVALID:
  387. case CAM_SYNC_STATE_ACTIVE:
  388. case CAM_SYNC_STATE_SIGNALED_ERROR:
  389. case CAM_SYNC_STATE_SIGNALED_CANCEL:
  390. CAM_ERR(CAM_SYNC,
  391. "Error: Wait on invalid state = %d, obj = %d, name = %s",
  392. row->state, sync_obj, row->name);
  393. rc = -EINVAL;
  394. break;
  395. case CAM_SYNC_STATE_SIGNALED_SUCCESS:
  396. rc = 0;
  397. break;
  398. default:
  399. rc = -EINVAL;
  400. break;
  401. }
  402. }
  403. return rc;
  404. }
  405. static int cam_sync_handle_create(struct cam_private_ioctl_arg *k_ioctl)
  406. {
  407. struct cam_sync_info sync_create;
  408. int result;
  409. if (k_ioctl->size != sizeof(struct cam_sync_info))
  410. return -EINVAL;
  411. if (!k_ioctl->ioctl_ptr)
  412. return -EINVAL;
  413. if (copy_from_user(&sync_create,
  414. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  415. k_ioctl->size))
  416. return -EFAULT;
  417. sync_create.name[SYNC_DEBUG_NAME_LEN] = '\0';
  418. result = cam_sync_create(&sync_create.sync_obj,
  419. sync_create.name);
  420. if (!result)
  421. if (copy_to_user(
  422. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  423. &sync_create,
  424. k_ioctl->size))
  425. return -EFAULT;
  426. return result;
  427. }
  428. static int cam_sync_handle_signal(struct cam_private_ioctl_arg *k_ioctl)
  429. {
  430. int rc = 0;
  431. struct cam_sync_signal sync_signal;
  432. if (k_ioctl->size != sizeof(struct cam_sync_signal))
  433. return -EINVAL;
  434. if (!k_ioctl->ioctl_ptr)
  435. return -EINVAL;
  436. if (copy_from_user(&sync_signal,
  437. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  438. k_ioctl->size))
  439. return -EFAULT;
  440. /* need to get ref for UMD signaled fences */
  441. rc = cam_sync_get_obj_ref(sync_signal.sync_obj);
  442. if (rc) {
  443. CAM_DBG(CAM_SYNC,
  444. "Error: cannot signal an uninitialized sync obj = %d",
  445. sync_signal.sync_obj);
  446. return rc;
  447. }
  448. return cam_sync_signal(sync_signal.sync_obj,
  449. sync_signal.sync_state,
  450. CAM_SYNC_COMMON_SYNC_SIGNAL_EVENT);
  451. }
  452. static int cam_sync_handle_merge(struct cam_private_ioctl_arg *k_ioctl)
  453. {
  454. struct cam_sync_merge sync_merge;
  455. uint32_t *sync_objs;
  456. uint32_t num_objs;
  457. uint32_t size;
  458. int result;
  459. if (k_ioctl->size != sizeof(struct cam_sync_merge))
  460. return -EINVAL;
  461. if (!k_ioctl->ioctl_ptr)
  462. return -EINVAL;
  463. if (copy_from_user(&sync_merge,
  464. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  465. k_ioctl->size))
  466. return -EFAULT;
  467. if (sync_merge.num_objs >= CAM_SYNC_MAX_OBJS)
  468. return -EINVAL;
  469. size = sizeof(uint32_t) * sync_merge.num_objs;
  470. sync_objs = kzalloc(size, GFP_ATOMIC);
  471. if (!sync_objs)
  472. return -ENOMEM;
  473. if (copy_from_user(sync_objs,
  474. u64_to_user_ptr(sync_merge.sync_objs),
  475. sizeof(uint32_t) * sync_merge.num_objs)) {
  476. kfree(sync_objs);
  477. return -EFAULT;
  478. }
  479. num_objs = sync_merge.num_objs;
  480. result = cam_sync_merge(sync_objs,
  481. num_objs,
  482. &sync_merge.merged);
  483. if (!result)
  484. if (copy_to_user(
  485. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  486. &sync_merge,
  487. k_ioctl->size)) {
  488. kfree(sync_objs);
  489. return -EFAULT;
  490. }
  491. kfree(sync_objs);
  492. return result;
  493. }
  494. static int cam_sync_handle_wait(struct cam_private_ioctl_arg *k_ioctl)
  495. {
  496. struct cam_sync_wait sync_wait;
  497. if (k_ioctl->size != sizeof(struct cam_sync_wait))
  498. return -EINVAL;
  499. if (!k_ioctl->ioctl_ptr)
  500. return -EINVAL;
  501. if (copy_from_user(&sync_wait,
  502. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  503. k_ioctl->size))
  504. return -EFAULT;
  505. k_ioctl->result = cam_sync_wait(sync_wait.sync_obj,
  506. sync_wait.timeout_ms);
  507. return 0;
  508. }
  509. static int cam_sync_handle_destroy(struct cam_private_ioctl_arg *k_ioctl)
  510. {
  511. struct cam_sync_info sync_create;
  512. if (k_ioctl->size != sizeof(struct cam_sync_info))
  513. return -EINVAL;
  514. if (!k_ioctl->ioctl_ptr)
  515. return -EINVAL;
  516. if (copy_from_user(&sync_create,
  517. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  518. k_ioctl->size))
  519. return -EFAULT;
  520. return cam_sync_destroy(sync_create.sync_obj);
  521. }
  522. static int cam_sync_handle_register_user_payload(
  523. struct cam_private_ioctl_arg *k_ioctl)
  524. {
  525. struct cam_sync_userpayload_info userpayload_info;
  526. struct sync_user_payload *user_payload_kernel;
  527. struct sync_user_payload *user_payload_iter;
  528. struct sync_user_payload *temp_upayload_kernel;
  529. uint32_t sync_obj;
  530. struct sync_table_row *row = NULL;
  531. if (k_ioctl->size != sizeof(struct cam_sync_userpayload_info))
  532. return -EINVAL;
  533. if (!k_ioctl->ioctl_ptr)
  534. return -EINVAL;
  535. if (copy_from_user(&userpayload_info,
  536. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  537. k_ioctl->size))
  538. return -EFAULT;
  539. sync_obj = userpayload_info.sync_obj;
  540. if (sync_obj >= CAM_SYNC_MAX_OBJS || sync_obj <= 0)
  541. return -EINVAL;
  542. user_payload_kernel = kzalloc(sizeof(*user_payload_kernel), GFP_KERNEL);
  543. if (!user_payload_kernel)
  544. return -ENOMEM;
  545. memcpy(user_payload_kernel->payload_data,
  546. userpayload_info.payload,
  547. CAM_SYNC_PAYLOAD_WORDS * sizeof(__u64));
  548. spin_lock_bh(&sync_dev->row_spinlocks[sync_obj]);
  549. row = sync_dev->sync_table + sync_obj;
  550. if (row->state == CAM_SYNC_STATE_INVALID) {
  551. CAM_ERR(CAM_SYNC,
  552. "Error: accessing an uninitialized sync obj = %s[%d]",
  553. row->name,
  554. sync_obj);
  555. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  556. kfree(user_payload_kernel);
  557. return -EINVAL;
  558. }
  559. if ((row->state == CAM_SYNC_STATE_SIGNALED_SUCCESS) ||
  560. (row->state == CAM_SYNC_STATE_SIGNALED_ERROR) ||
  561. (row->state == CAM_SYNC_STATE_SIGNALED_CANCEL)) {
  562. cam_sync_util_send_v4l2_event(CAM_SYNC_V4L_EVENT_ID_CB_TRIG,
  563. sync_obj,
  564. row->state,
  565. user_payload_kernel->payload_data,
  566. CAM_SYNC_USER_PAYLOAD_SIZE * sizeof(__u64),
  567. CAM_SYNC_COMMON_REG_PAYLOAD_EVENT);
  568. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  569. kfree(user_payload_kernel);
  570. return 0;
  571. }
  572. list_for_each_entry_safe(user_payload_iter,
  573. temp_upayload_kernel,
  574. &row->user_payload_list,
  575. list) {
  576. if (user_payload_iter->payload_data[0] ==
  577. user_payload_kernel->payload_data[0] &&
  578. user_payload_iter->payload_data[1] ==
  579. user_payload_kernel->payload_data[1]) {
  580. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  581. kfree(user_payload_kernel);
  582. return -EALREADY;
  583. }
  584. }
  585. list_add_tail(&user_payload_kernel->list, &row->user_payload_list);
  586. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  587. return 0;
  588. }
  589. static int cam_sync_handle_deregister_user_payload(
  590. struct cam_private_ioctl_arg *k_ioctl)
  591. {
  592. struct cam_sync_userpayload_info userpayload_info;
  593. struct sync_user_payload *user_payload_kernel, *temp;
  594. uint32_t sync_obj;
  595. struct sync_table_row *row = NULL;
  596. if (k_ioctl->size != sizeof(struct cam_sync_userpayload_info)) {
  597. CAM_ERR(CAM_SYNC, "Incorrect ioctl size");
  598. return -EINVAL;
  599. }
  600. if (!k_ioctl->ioctl_ptr) {
  601. CAM_ERR(CAM_SYNC, "Invalid embedded ioctl ptr");
  602. return -EINVAL;
  603. }
  604. if (copy_from_user(&userpayload_info,
  605. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  606. k_ioctl->size))
  607. return -EFAULT;
  608. sync_obj = userpayload_info.sync_obj;
  609. if (sync_obj >= CAM_SYNC_MAX_OBJS || sync_obj <= 0)
  610. return -EINVAL;
  611. spin_lock_bh(&sync_dev->row_spinlocks[sync_obj]);
  612. row = sync_dev->sync_table + sync_obj;
  613. if (row->state == CAM_SYNC_STATE_INVALID) {
  614. CAM_ERR(CAM_SYNC,
  615. "Error: accessing an uninitialized sync obj = %s[%d]",
  616. row->name,
  617. sync_obj);
  618. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  619. return -EINVAL;
  620. }
  621. list_for_each_entry_safe(user_payload_kernel, temp,
  622. &row->user_payload_list, list) {
  623. if (user_payload_kernel->payload_data[0] ==
  624. userpayload_info.payload[0] &&
  625. user_payload_kernel->payload_data[1] ==
  626. userpayload_info.payload[1]) {
  627. list_del_init(&user_payload_kernel->list);
  628. kfree(user_payload_kernel);
  629. }
  630. }
  631. spin_unlock_bh(&sync_dev->row_spinlocks[sync_obj]);
  632. return 0;
  633. }
  634. static long cam_sync_dev_ioctl(struct file *filep, void *fh,
  635. bool valid_prio, unsigned int cmd, void *arg)
  636. {
  637. int32_t rc;
  638. struct sync_device *sync_dev = video_drvdata(filep);
  639. struct cam_private_ioctl_arg k_ioctl;
  640. if (!sync_dev) {
  641. CAM_ERR(CAM_SYNC, "sync_dev NULL");
  642. return -EINVAL;
  643. }
  644. if (!arg)
  645. return -EINVAL;
  646. if (cmd != CAM_PRIVATE_IOCTL_CMD)
  647. return -ENOIOCTLCMD;
  648. k_ioctl = *(struct cam_private_ioctl_arg *)arg;
  649. switch (k_ioctl.id) {
  650. case CAM_SYNC_CREATE:
  651. rc = cam_sync_handle_create(&k_ioctl);
  652. break;
  653. case CAM_SYNC_DESTROY:
  654. rc = cam_sync_handle_destroy(&k_ioctl);
  655. break;
  656. case CAM_SYNC_REGISTER_PAYLOAD:
  657. rc = cam_sync_handle_register_user_payload(
  658. &k_ioctl);
  659. break;
  660. case CAM_SYNC_DEREGISTER_PAYLOAD:
  661. rc = cam_sync_handle_deregister_user_payload(
  662. &k_ioctl);
  663. break;
  664. case CAM_SYNC_SIGNAL:
  665. rc = cam_sync_handle_signal(&k_ioctl);
  666. break;
  667. case CAM_SYNC_MERGE:
  668. rc = cam_sync_handle_merge(&k_ioctl);
  669. break;
  670. case CAM_SYNC_WAIT:
  671. rc = cam_sync_handle_wait(&k_ioctl);
  672. ((struct cam_private_ioctl_arg *)arg)->result =
  673. k_ioctl.result;
  674. break;
  675. default:
  676. rc = -ENOIOCTLCMD;
  677. }
  678. return rc;
  679. }
  680. static unsigned int cam_sync_poll(struct file *f,
  681. struct poll_table_struct *pll_table)
  682. {
  683. int rc = 0;
  684. struct v4l2_fh *eventq = f->private_data;
  685. if (!eventq)
  686. return -EINVAL;
  687. poll_wait(f, &eventq->wait, pll_table);
  688. if (v4l2_event_pending(eventq))
  689. rc = POLLPRI;
  690. return rc;
  691. }
  692. static int cam_sync_open(struct file *filep)
  693. {
  694. int rc;
  695. struct sync_device *sync_dev = video_drvdata(filep);
  696. if (!sync_dev) {
  697. CAM_ERR(CAM_SYNC, "Sync device NULL");
  698. return -ENODEV;
  699. }
  700. mutex_lock(&sync_dev->table_lock);
  701. if (sync_dev->open_cnt >= 1) {
  702. mutex_unlock(&sync_dev->table_lock);
  703. return -EALREADY;
  704. }
  705. rc = v4l2_fh_open(filep);
  706. if (!rc) {
  707. sync_dev->open_cnt++;
  708. spin_lock_bh(&sync_dev->cam_sync_eventq_lock);
  709. sync_dev->cam_sync_eventq = filep->private_data;
  710. spin_unlock_bh(&sync_dev->cam_sync_eventq_lock);
  711. } else {
  712. CAM_ERR(CAM_SYNC, "v4l2_fh_open failed : %d", rc);
  713. }
  714. mutex_unlock(&sync_dev->table_lock);
  715. return rc;
  716. }
  717. static int cam_sync_close(struct file *filep)
  718. {
  719. int rc = 0;
  720. int i;
  721. struct sync_device *sync_dev = video_drvdata(filep);
  722. if (!sync_dev) {
  723. CAM_ERR(CAM_SYNC, "Sync device NULL");
  724. rc = -ENODEV;
  725. return rc;
  726. }
  727. mutex_lock(&sync_dev->table_lock);
  728. sync_dev->open_cnt--;
  729. if (!sync_dev->open_cnt) {
  730. for (i = 1; i < CAM_SYNC_MAX_OBJS; i++) {
  731. struct sync_table_row *row =
  732. sync_dev->sync_table + i;
  733. /*
  734. * Signal all ACTIVE objects as ERR, but we don't
  735. * care about the return status here apart from logging
  736. * it.
  737. */
  738. if (row->state == CAM_SYNC_STATE_ACTIVE) {
  739. rc = cam_sync_signal(i,
  740. CAM_SYNC_STATE_SIGNALED_ERROR,
  741. CAM_SYNC_COMMON_RELEASE_EVENT);
  742. if (rc < 0)
  743. CAM_ERR(CAM_SYNC,
  744. "Cleanup signal fail idx:%d\n",
  745. i);
  746. }
  747. }
  748. /*
  749. * Flush the work queue to wait for pending signal callbacks to
  750. * finish
  751. */
  752. flush_workqueue(sync_dev->work_queue);
  753. /*
  754. * Now that all callbacks worker threads have finished,
  755. * destroy the sync objects
  756. */
  757. for (i = 1; i < CAM_SYNC_MAX_OBJS; i++) {
  758. struct sync_table_row *row =
  759. sync_dev->sync_table + i;
  760. if (row->state != CAM_SYNC_STATE_INVALID) {
  761. rc = cam_sync_destroy(i);
  762. if (rc < 0)
  763. CAM_ERR(CAM_SYNC,
  764. "Cleanup destroy fail:idx:%d\n",
  765. i);
  766. }
  767. }
  768. }
  769. mutex_unlock(&sync_dev->table_lock);
  770. spin_lock_bh(&sync_dev->cam_sync_eventq_lock);
  771. sync_dev->cam_sync_eventq = NULL;
  772. spin_unlock_bh(&sync_dev->cam_sync_eventq_lock);
  773. v4l2_fh_release(filep);
  774. return rc;
  775. }
  776. static void cam_sync_event_queue_notify_error(const struct v4l2_event *old,
  777. struct v4l2_event *new)
  778. {
  779. if (sync_dev->version == CAM_SYNC_V4L_EVENT_V2) {
  780. struct cam_sync_ev_header_v2 *ev_header;
  781. ev_header = CAM_SYNC_GET_HEADER_PTR_V2((*old));
  782. CAM_ERR(CAM_CRM,
  783. "Failed to notify event id %d fence %d statue %d reason %u %u %u %u",
  784. old->id, ev_header->sync_obj, ev_header->status,
  785. ev_header->evt_param[0], ev_header->evt_param[1],
  786. ev_header->evt_param[2], ev_header->evt_param[3]);
  787. } else {
  788. struct cam_sync_ev_header *ev_header;
  789. ev_header = CAM_SYNC_GET_HEADER_PTR((*old));
  790. CAM_ERR(CAM_CRM,
  791. "Failed to notify event id %d fence %d statue %d",
  792. old->id, ev_header->sync_obj, ev_header->status);
  793. }
  794. }
  795. static struct v4l2_subscribed_event_ops cam_sync_v4l2_ops = {
  796. .merge = cam_sync_event_queue_notify_error,
  797. };
  798. int cam_sync_subscribe_event(struct v4l2_fh *fh,
  799. const struct v4l2_event_subscription *sub)
  800. {
  801. if (!((sub->type == CAM_SYNC_V4L_EVENT) ||
  802. (sub->type == CAM_SYNC_V4L_EVENT_V2))) {
  803. CAM_ERR(CAM_SYNC, "Non supported event type 0x%x", sub->type);
  804. return -EINVAL;
  805. }
  806. sync_dev->version = sub->type;
  807. CAM_DBG(CAM_SYNC, "Sync event verion type 0x%x", sync_dev->version);
  808. return v4l2_event_subscribe(fh, sub, CAM_SYNC_MAX_V4L2_EVENTS,
  809. &cam_sync_v4l2_ops);
  810. }
  811. int cam_sync_unsubscribe_event(struct v4l2_fh *fh,
  812. const struct v4l2_event_subscription *sub)
  813. {
  814. if (!((sub->type == CAM_SYNC_V4L_EVENT) ||
  815. (sub->type == CAM_SYNC_V4L_EVENT_V2))) {
  816. CAM_ERR(CAM_SYNC, "Non supported event type 0x%x", sub->type);
  817. return -EINVAL;
  818. }
  819. return v4l2_event_unsubscribe(fh, sub);
  820. }
  821. static const struct v4l2_ioctl_ops g_cam_sync_ioctl_ops = {
  822. .vidioc_subscribe_event = cam_sync_subscribe_event,
  823. .vidioc_unsubscribe_event = cam_sync_unsubscribe_event,
  824. .vidioc_default = cam_sync_dev_ioctl,
  825. };
  826. static struct v4l2_file_operations cam_sync_v4l2_fops = {
  827. .owner = THIS_MODULE,
  828. .open = cam_sync_open,
  829. .release = cam_sync_close,
  830. .poll = cam_sync_poll,
  831. .unlocked_ioctl = video_ioctl2,
  832. #ifdef CONFIG_COMPAT
  833. .compat_ioctl32 = video_ioctl2,
  834. #endif
  835. };
  836. #if IS_REACHABLE(CONFIG_MEDIA_CONTROLLER)
  837. static int cam_sync_media_controller_init(struct sync_device *sync_dev,
  838. struct platform_device *pdev)
  839. {
  840. int rc;
  841. sync_dev->v4l2_dev.mdev = kzalloc(sizeof(struct media_device),
  842. GFP_KERNEL);
  843. if (!sync_dev->v4l2_dev.mdev)
  844. return -ENOMEM;
  845. media_device_init(sync_dev->v4l2_dev.mdev);
  846. strlcpy(sync_dev->v4l2_dev.mdev->model, CAM_SYNC_DEVICE_NAME,
  847. sizeof(sync_dev->v4l2_dev.mdev->model));
  848. sync_dev->v4l2_dev.mdev->dev = &(pdev->dev);
  849. rc = media_device_register(sync_dev->v4l2_dev.mdev);
  850. if (rc < 0)
  851. goto register_fail;
  852. rc = media_entity_pads_init(&sync_dev->vdev->entity, 0, NULL);
  853. if (rc < 0)
  854. goto entity_fail;
  855. return 0;
  856. entity_fail:
  857. media_device_unregister(sync_dev->v4l2_dev.mdev);
  858. register_fail:
  859. media_device_cleanup(sync_dev->v4l2_dev.mdev);
  860. return rc;
  861. }
  862. static void cam_sync_media_controller_cleanup(struct sync_device *sync_dev)
  863. {
  864. media_entity_cleanup(&sync_dev->vdev->entity);
  865. media_device_unregister(sync_dev->v4l2_dev.mdev);
  866. media_device_cleanup(sync_dev->v4l2_dev.mdev);
  867. kfree(sync_dev->v4l2_dev.mdev);
  868. }
  869. static void cam_sync_init_entity(struct sync_device *sync_dev)
  870. {
  871. sync_dev->vdev->entity.function = CAM_SYNC_DEVICE_TYPE;
  872. sync_dev->vdev->entity.name =
  873. video_device_node_name(sync_dev->vdev);
  874. }
  875. #else
  876. static int cam_sync_media_controller_init(struct sync_device *sync_dev,
  877. struct platform_device *pdev)
  878. {
  879. return 0;
  880. }
  881. static void cam_sync_media_controller_cleanup(struct sync_device *sync_dev)
  882. {
  883. }
  884. static void cam_sync_init_entity(struct sync_device *sync_dev)
  885. {
  886. }
  887. #endif
  888. static int cam_sync_create_debugfs(void)
  889. {
  890. int rc = 0;
  891. struct dentry *dbgfileptr = NULL;
  892. dbgfileptr = debugfs_create_dir("camera_sync", NULL);
  893. if (!dbgfileptr) {
  894. CAM_ERR(CAM_SYNC,"DebugFS could not create directory!");
  895. rc = -ENOENT;
  896. goto end;
  897. }
  898. /* Store parent inode for cleanup in caller */
  899. sync_dev->dentry = dbgfileptr;
  900. debugfs_create_bool("trigger_cb_without_switch", 0644,
  901. sync_dev->dentry, &trigger_cb_without_switch);
  902. end:
  903. return rc;
  904. }
  905. #if IS_REACHABLE(CONFIG_MSM_GLOBAL_SYNX)
  906. int cam_synx_sync_signal(int32_t sync_obj, uint32_t synx_status)
  907. {
  908. int rc = 0;
  909. uint32_t sync_status = synx_status;
  910. switch (synx_status) {
  911. case SYNX_STATE_ACTIVE:
  912. sync_status = CAM_SYNC_STATE_ACTIVE;
  913. break;
  914. case SYNX_STATE_SIGNALED_SUCCESS:
  915. sync_status = CAM_SYNC_STATE_SIGNALED_SUCCESS;
  916. break;
  917. case SYNX_STATE_SIGNALED_ERROR:
  918. sync_status = CAM_SYNC_STATE_SIGNALED_ERROR;
  919. break;
  920. case 4: /* SYNX_STATE_SIGNALED_CANCEL: */
  921. sync_status = CAM_SYNC_STATE_SIGNALED_CANCEL;
  922. break;
  923. default:
  924. CAM_ERR(CAM_SYNC, "Invalid synx status %d for obj %d",
  925. synx_status, sync_obj);
  926. sync_status = CAM_SYNC_STATE_SIGNALED_ERROR;
  927. break;
  928. }
  929. rc = cam_sync_signal(sync_obj, sync_status, CAM_SYNC_COMMON_EVENT_SYNX);
  930. if (rc) {
  931. CAM_ERR(CAM_SYNC,
  932. "synx signal failed with %d, sync_obj=%d, synx_status=%d, sync_status=%d",
  933. sync_obj, synx_status, sync_status, rc);
  934. }
  935. return rc;
  936. }
  937. static int cam_sync_register_synx_bind_ops(
  938. struct synx_register_params *object)
  939. {
  940. int rc = 0;
  941. rc = synx_register_ops(object);
  942. if (rc)
  943. CAM_ERR(CAM_SYNC, "synx registration fail with rc=%d", rc);
  944. return rc;
  945. }
  946. static void cam_sync_unregister_synx_bind_ops(
  947. struct synx_register_params *object)
  948. {
  949. int rc = 0;
  950. rc = synx_deregister_ops(object);
  951. if (rc)
  952. CAM_ERR(CAM_SYNC, "sync unregistration fail with %d", rc);
  953. }
  954. static void cam_sync_configure_synx_obj(struct synx_register_params *object)
  955. {
  956. struct synx_register_params *params = object;
  957. params->name = CAM_SYNC_NAME;
  958. params->type = SYNX_TYPE_CSL;
  959. params->ops.register_callback = cam_sync_register_callback;
  960. params->ops.deregister_callback = cam_sync_deregister_callback;
  961. params->ops.enable_signaling = cam_sync_get_obj_ref;
  962. params->ops.signal = cam_synx_sync_signal;
  963. }
  964. #endif
  965. static int cam_sync_component_bind(struct device *dev,
  966. struct device *master_dev, void *data)
  967. {
  968. int rc;
  969. int idx;
  970. struct platform_device *pdev = to_platform_device(dev);
  971. sync_dev = kzalloc(sizeof(*sync_dev), GFP_KERNEL);
  972. if (!sync_dev)
  973. return -ENOMEM;
  974. mutex_init(&sync_dev->table_lock);
  975. spin_lock_init(&sync_dev->cam_sync_eventq_lock);
  976. for (idx = 0; idx < CAM_SYNC_MAX_OBJS; idx++)
  977. spin_lock_init(&sync_dev->row_spinlocks[idx]);
  978. sync_dev->vdev = video_device_alloc();
  979. if (!sync_dev->vdev) {
  980. rc = -ENOMEM;
  981. goto vdev_fail;
  982. }
  983. rc = cam_sync_media_controller_init(sync_dev, pdev);
  984. if (rc < 0)
  985. goto mcinit_fail;
  986. sync_dev->vdev->v4l2_dev = &sync_dev->v4l2_dev;
  987. rc = v4l2_device_register(&(pdev->dev), sync_dev->vdev->v4l2_dev);
  988. if (rc < 0)
  989. goto register_fail;
  990. strlcpy(sync_dev->vdev->name, CAM_SYNC_NAME,
  991. sizeof(sync_dev->vdev->name));
  992. sync_dev->vdev->release = video_device_release_empty;
  993. sync_dev->vdev->fops = &cam_sync_v4l2_fops;
  994. sync_dev->vdev->ioctl_ops = &g_cam_sync_ioctl_ops;
  995. sync_dev->vdev->minor = -1;
  996. sync_dev->vdev->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  997. sync_dev->vdev->vfl_type = VFL_TYPE_VIDEO;
  998. rc = video_register_device(sync_dev->vdev, VFL_TYPE_VIDEO, -1);
  999. if (rc < 0) {
  1000. CAM_ERR(CAM_SYNC,
  1001. "video device registration failure rc = %d, name = %s, device_caps = %d",
  1002. rc, sync_dev->vdev->name, sync_dev->vdev->device_caps);
  1003. goto v4l2_fail;
  1004. }
  1005. cam_sync_init_entity(sync_dev);
  1006. video_set_drvdata(sync_dev->vdev, sync_dev);
  1007. memset(&sync_dev->sync_table, 0, sizeof(sync_dev->sync_table));
  1008. memset(&sync_dev->bitmap, 0, sizeof(sync_dev->bitmap));
  1009. bitmap_zero(sync_dev->bitmap, CAM_SYNC_MAX_OBJS);
  1010. /*
  1011. * We treat zero as invalid handle, so we will keep the 0th bit set
  1012. * always
  1013. */
  1014. set_bit(0, sync_dev->bitmap);
  1015. sync_dev->work_queue = alloc_workqueue(CAM_SYNC_WORKQUEUE_NAME,
  1016. WQ_HIGHPRI | WQ_UNBOUND, 1);
  1017. if (!sync_dev->work_queue) {
  1018. CAM_ERR(CAM_SYNC,
  1019. "Error: high priority work queue creation failed");
  1020. rc = -ENOMEM;
  1021. goto v4l2_fail;
  1022. }
  1023. trigger_cb_without_switch = false;
  1024. cam_sync_create_debugfs();
  1025. #if IS_REACHABLE(CONFIG_MSM_GLOBAL_SYNX)
  1026. CAM_DBG(CAM_SYNC, "Registering with synx driver");
  1027. cam_sync_configure_synx_obj(&sync_dev->params);
  1028. rc = cam_sync_register_synx_bind_ops(&sync_dev->params);
  1029. if (rc)
  1030. goto v4l2_fail;
  1031. #endif
  1032. CAM_DBG(CAM_SYNC, "Component bound successfully");
  1033. return rc;
  1034. v4l2_fail:
  1035. v4l2_device_unregister(sync_dev->vdev->v4l2_dev);
  1036. register_fail:
  1037. cam_sync_media_controller_cleanup(sync_dev);
  1038. mcinit_fail:
  1039. video_unregister_device(sync_dev->vdev);
  1040. video_device_release(sync_dev->vdev);
  1041. vdev_fail:
  1042. mutex_destroy(&sync_dev->table_lock);
  1043. kfree(sync_dev);
  1044. return rc;
  1045. }
  1046. static void cam_sync_component_unbind(struct device *dev,
  1047. struct device *master_dev, void *data)
  1048. {
  1049. int i;
  1050. v4l2_device_unregister(sync_dev->vdev->v4l2_dev);
  1051. cam_sync_media_controller_cleanup(sync_dev);
  1052. #if IS_REACHABLE(CONFIG_MSM_GLOBAL_SYNX)
  1053. cam_sync_unregister_synx_bind_ops(&sync_dev->params);
  1054. #endif
  1055. video_unregister_device(sync_dev->vdev);
  1056. video_device_release(sync_dev->vdev);
  1057. debugfs_remove_recursive(sync_dev->dentry);
  1058. sync_dev->dentry = NULL;
  1059. for (i = 0; i < CAM_SYNC_MAX_OBJS; i++)
  1060. spin_lock_init(&sync_dev->row_spinlocks[i]);
  1061. kfree(sync_dev);
  1062. sync_dev = NULL;
  1063. }
  1064. const static struct component_ops cam_sync_component_ops = {
  1065. .bind = cam_sync_component_bind,
  1066. .unbind = cam_sync_component_unbind,
  1067. };
  1068. static int cam_sync_probe(struct platform_device *pdev)
  1069. {
  1070. int rc = 0;
  1071. CAM_DBG(CAM_SYNC, "Adding Sync component");
  1072. rc = component_add(&pdev->dev, &cam_sync_component_ops);
  1073. if (rc)
  1074. CAM_ERR(CAM_SYNC, "failed to add component rc: %d", rc);
  1075. return rc;
  1076. }
  1077. static int cam_sync_remove(struct platform_device *pdev)
  1078. {
  1079. component_del(&pdev->dev, &cam_sync_component_ops);
  1080. return 0;
  1081. }
  1082. static const struct of_device_id cam_sync_dt_match[] = {
  1083. {.compatible = "qcom,cam-sync"},
  1084. {}
  1085. };
  1086. MODULE_DEVICE_TABLE(of, cam_sync_dt_match);
  1087. struct platform_driver cam_sync_driver = {
  1088. .probe = cam_sync_probe,
  1089. .remove = cam_sync_remove,
  1090. .driver = {
  1091. .name = "cam_sync",
  1092. .owner = THIS_MODULE,
  1093. .of_match_table = cam_sync_dt_match,
  1094. .suppress_bind_attrs = true,
  1095. },
  1096. };
  1097. int cam_sync_init(void)
  1098. {
  1099. return platform_driver_register(&cam_sync_driver);
  1100. }
  1101. void cam_sync_exit(void)
  1102. {
  1103. platform_driver_unregister(&cam_sync_driver);
  1104. }
  1105. MODULE_DESCRIPTION("Camera sync driver");
  1106. MODULE_LICENSE("GPL v2");