adf_fops.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. * Copyright (C) 2013 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/circ_buf.h>
  16. #include <linux/fs.h>
  17. #include <linux/module.h>
  18. #include <linux/poll.h>
  19. #include <linux/slab.h>
  20. #include <linux/uaccess.h>
  21. #include <video/adf_client.h>
  22. #include <video/adf_format.h>
  23. #include "sw_sync.h"
  24. #include "sync.h"
  25. #include "adf.h"
  26. #include "adf_fops.h"
  27. #include "adf_sysfs.h"
  28. #ifdef CONFIG_COMPAT
  29. #include "adf_fops32.h"
  30. #endif
  31. static int adf_obj_set_event(struct adf_obj *obj, struct adf_file *file,
  32. struct adf_set_event __user *arg)
  33. {
  34. struct adf_set_event data;
  35. bool enabled;
  36. unsigned long flags;
  37. int err;
  38. if (copy_from_user(&data, arg, sizeof(data)))
  39. return -EFAULT;
  40. err = adf_obj_check_supports_event(obj, data.type);
  41. if (err < 0)
  42. return err;
  43. spin_lock_irqsave(&obj->file_lock, flags);
  44. if (data.enabled)
  45. enabled = test_and_set_bit(data.type,
  46. file->event_subscriptions);
  47. else
  48. enabled = test_and_clear_bit(data.type,
  49. file->event_subscriptions);
  50. spin_unlock_irqrestore(&obj->file_lock, flags);
  51. if (data.enabled == enabled)
  52. return -EALREADY;
  53. if (data.enabled)
  54. adf_event_get(obj, data.type);
  55. else
  56. adf_event_put(obj, data.type);
  57. return 0;
  58. }
  59. static int adf_obj_copy_custom_data_to_user(struct adf_obj *obj,
  60. void __user *dst, size_t *dst_size)
  61. {
  62. void *custom_data;
  63. size_t custom_data_size;
  64. int ret;
  65. if (!obj->ops || !obj->ops->custom_data) {
  66. dev_dbg(&obj->dev, "%s: no custom_data op\n", __func__);
  67. return 0;
  68. }
  69. custom_data = kzalloc(ADF_MAX_CUSTOM_DATA_SIZE, GFP_KERNEL);
  70. if (!custom_data)
  71. return -ENOMEM;
  72. ret = obj->ops->custom_data(obj, custom_data, &custom_data_size);
  73. if (ret < 0)
  74. goto done;
  75. if (copy_to_user(dst, custom_data, min(*dst_size, custom_data_size))) {
  76. ret = -EFAULT;
  77. goto done;
  78. }
  79. *dst_size = custom_data_size;
  80. done:
  81. kfree(custom_data);
  82. return ret;
  83. }
  84. static int adf_eng_get_data(struct adf_overlay_engine *eng,
  85. struct adf_overlay_engine_data __user *arg)
  86. {
  87. struct adf_device *dev = adf_overlay_engine_parent(eng);
  88. struct adf_overlay_engine_data data;
  89. size_t n_supported_formats;
  90. u32 *supported_formats = NULL;
  91. int ret = 0;
  92. if (copy_from_user(&data, arg, sizeof(data)))
  93. return -EFAULT;
  94. strlcpy(data.name, eng->base.name, sizeof(data.name));
  95. if (data.n_supported_formats > ADF_MAX_SUPPORTED_FORMATS)
  96. return -EINVAL;
  97. n_supported_formats = data.n_supported_formats;
  98. data.n_supported_formats = eng->ops->n_supported_formats;
  99. if (n_supported_formats) {
  100. supported_formats = kzalloc(n_supported_formats *
  101. sizeof(supported_formats[0]), GFP_KERNEL);
  102. if (!supported_formats)
  103. return -ENOMEM;
  104. }
  105. memcpy(supported_formats, eng->ops->supported_formats,
  106. sizeof(u32) * min(n_supported_formats,
  107. eng->ops->n_supported_formats));
  108. mutex_lock(&dev->client_lock);
  109. ret = adf_obj_copy_custom_data_to_user(&eng->base, arg->custom_data,
  110. &data.custom_data_size);
  111. mutex_unlock(&dev->client_lock);
  112. if (ret < 0)
  113. goto done;
  114. if (copy_to_user(arg, &data, sizeof(data))) {
  115. ret = -EFAULT;
  116. goto done;
  117. }
  118. if (supported_formats && copy_to_user(arg->supported_formats,
  119. supported_formats,
  120. n_supported_formats * sizeof(supported_formats[0])))
  121. ret = -EFAULT;
  122. done:
  123. kfree(supported_formats);
  124. return ret;
  125. }
  126. static int adf_buffer_import(struct adf_device *dev,
  127. struct adf_buffer_config __user *cfg, struct adf_buffer *buf)
  128. {
  129. struct adf_buffer_config user_buf;
  130. size_t i;
  131. int ret = 0;
  132. if (copy_from_user(&user_buf, cfg, sizeof(user_buf)))
  133. return -EFAULT;
  134. memset(buf, 0, sizeof(*buf));
  135. if (user_buf.n_planes > ADF_MAX_PLANES) {
  136. dev_err(&dev->base.dev, "invalid plane count %u\n",
  137. user_buf.n_planes);
  138. return -EINVAL;
  139. }
  140. buf->overlay_engine = idr_find(&dev->overlay_engines,
  141. user_buf.overlay_engine);
  142. if (!buf->overlay_engine) {
  143. dev_err(&dev->base.dev, "invalid overlay engine id %u\n",
  144. user_buf.overlay_engine);
  145. return -ENOENT;
  146. }
  147. buf->w = user_buf.w;
  148. buf->h = user_buf.h;
  149. buf->format = user_buf.format;
  150. for (i = 0; i < user_buf.n_planes; i++) {
  151. buf->dma_bufs[i] = dma_buf_get(user_buf.fd[i]);
  152. if (IS_ERR(buf->dma_bufs[i])) {
  153. ret = PTR_ERR(buf->dma_bufs[i]);
  154. dev_err(&dev->base.dev, "importing dma_buf fd %d failed: %d\n",
  155. user_buf.fd[i], ret);
  156. buf->dma_bufs[i] = NULL;
  157. goto done;
  158. }
  159. buf->offset[i] = user_buf.offset[i];
  160. buf->pitch[i] = user_buf.pitch[i];
  161. }
  162. buf->n_planes = user_buf.n_planes;
  163. if (user_buf.acquire_fence >= 0) {
  164. buf->acquire_fence = sync_fence_fdget(user_buf.acquire_fence);
  165. if (!buf->acquire_fence) {
  166. dev_err(&dev->base.dev, "getting fence fd %d failed\n",
  167. user_buf.acquire_fence);
  168. ret = -EINVAL;
  169. goto done;
  170. }
  171. }
  172. done:
  173. if (ret < 0)
  174. adf_buffer_cleanup(buf);
  175. return ret;
  176. }
  177. static int adf_device_post_config(struct adf_device *dev,
  178. struct adf_post_config __user *arg)
  179. {
  180. struct sync_fence *complete_fence;
  181. int complete_fence_fd;
  182. struct adf_buffer *bufs = NULL;
  183. struct adf_interface **intfs = NULL;
  184. size_t n_intfs, n_bufs, i;
  185. void *custom_data = NULL;
  186. size_t custom_data_size;
  187. int ret = 0;
  188. complete_fence_fd = get_unused_fd_flags(O_CLOEXEC);
  189. if (complete_fence_fd < 0)
  190. return complete_fence_fd;
  191. if (get_user(n_intfs, &arg->n_interfaces)) {
  192. ret = -EFAULT;
  193. goto err_get_user;
  194. }
  195. if (n_intfs > ADF_MAX_INTERFACES) {
  196. ret = -EINVAL;
  197. goto err_get_user;
  198. }
  199. if (get_user(n_bufs, &arg->n_bufs)) {
  200. ret = -EFAULT;
  201. goto err_get_user;
  202. }
  203. if (n_bufs > ADF_MAX_BUFFERS) {
  204. ret = -EINVAL;
  205. goto err_get_user;
  206. }
  207. if (get_user(custom_data_size, &arg->custom_data_size)) {
  208. ret = -EFAULT;
  209. goto err_get_user;
  210. }
  211. if (custom_data_size > ADF_MAX_CUSTOM_DATA_SIZE) {
  212. ret = -EINVAL;
  213. goto err_get_user;
  214. }
  215. if (n_intfs) {
  216. intfs = kmalloc(sizeof(intfs[0]) * n_intfs, GFP_KERNEL);
  217. if (!intfs) {
  218. ret = -ENOMEM;
  219. goto err_get_user;
  220. }
  221. }
  222. for (i = 0; i < n_intfs; i++) {
  223. u32 intf_id;
  224. if (get_user(intf_id, &arg->interfaces[i])) {
  225. ret = -EFAULT;
  226. goto err_get_user;
  227. }
  228. intfs[i] = idr_find(&dev->interfaces, intf_id);
  229. if (!intfs[i]) {
  230. ret = -EINVAL;
  231. goto err_get_user;
  232. }
  233. }
  234. if (n_bufs) {
  235. bufs = kzalloc(sizeof(bufs[0]) * n_bufs, GFP_KERNEL);
  236. if (!bufs) {
  237. ret = -ENOMEM;
  238. goto err_get_user;
  239. }
  240. }
  241. for (i = 0; i < n_bufs; i++) {
  242. ret = adf_buffer_import(dev, &arg->bufs[i], &bufs[i]);
  243. if (ret < 0) {
  244. memset(&bufs[i], 0, sizeof(bufs[i]));
  245. goto err_import;
  246. }
  247. }
  248. if (custom_data_size) {
  249. custom_data = kzalloc(custom_data_size, GFP_KERNEL);
  250. if (!custom_data) {
  251. ret = -ENOMEM;
  252. goto err_import;
  253. }
  254. if (copy_from_user(custom_data, arg->custom_data,
  255. custom_data_size)) {
  256. ret = -EFAULT;
  257. goto err_import;
  258. }
  259. }
  260. if (put_user(complete_fence_fd, &arg->complete_fence)) {
  261. ret = -EFAULT;
  262. goto err_import;
  263. }
  264. complete_fence = adf_device_post_nocopy(dev, intfs, n_intfs, bufs,
  265. n_bufs, custom_data, custom_data_size);
  266. if (IS_ERR(complete_fence)) {
  267. ret = PTR_ERR(complete_fence);
  268. goto err_import;
  269. }
  270. sync_fence_install(complete_fence, complete_fence_fd);
  271. return 0;
  272. err_import:
  273. for (i = 0; i < n_bufs; i++)
  274. adf_buffer_cleanup(&bufs[i]);
  275. err_get_user:
  276. kfree(custom_data);
  277. kfree(bufs);
  278. kfree(intfs);
  279. put_unused_fd(complete_fence_fd);
  280. return ret;
  281. }
  282. static int adf_intf_simple_post_config(struct adf_interface *intf,
  283. struct adf_simple_post_config __user *arg)
  284. {
  285. struct adf_device *dev = intf->base.parent;
  286. struct sync_fence *complete_fence;
  287. int complete_fence_fd;
  288. struct adf_buffer buf;
  289. int ret = 0;
  290. complete_fence_fd = get_unused_fd_flags(O_CLOEXEC);
  291. if (complete_fence_fd < 0)
  292. return complete_fence_fd;
  293. ret = adf_buffer_import(dev, &arg->buf, &buf);
  294. if (ret < 0)
  295. goto err_import;
  296. if (put_user(complete_fence_fd, &arg->complete_fence)) {
  297. ret = -EFAULT;
  298. goto err_put_user;
  299. }
  300. complete_fence = adf_interface_simple_post(intf, &buf);
  301. if (IS_ERR(complete_fence)) {
  302. ret = PTR_ERR(complete_fence);
  303. goto err_put_user;
  304. }
  305. sync_fence_install(complete_fence, complete_fence_fd);
  306. return 0;
  307. err_put_user:
  308. adf_buffer_cleanup(&buf);
  309. err_import:
  310. put_unused_fd(complete_fence_fd);
  311. return ret;
  312. }
  313. static int adf_intf_simple_buffer_alloc(struct adf_interface *intf,
  314. struct adf_simple_buffer_alloc __user *arg)
  315. {
  316. struct adf_simple_buffer_alloc data;
  317. struct dma_buf *dma_buf;
  318. int ret = 0;
  319. if (copy_from_user(&data, arg, sizeof(data)))
  320. return -EFAULT;
  321. data.fd = get_unused_fd_flags(O_CLOEXEC);
  322. if (data.fd < 0)
  323. return data.fd;
  324. ret = adf_interface_simple_buffer_alloc(intf, data.w, data.h,
  325. data.format, &dma_buf, &data.offset, &data.pitch);
  326. if (ret < 0)
  327. goto err_alloc;
  328. if (copy_to_user(arg, &data, sizeof(*arg))) {
  329. ret = -EFAULT;
  330. goto err_copy;
  331. }
  332. fd_install(data.fd, dma_buf->file);
  333. return 0;
  334. err_copy:
  335. dma_buf_put(dma_buf);
  336. err_alloc:
  337. put_unused_fd(data.fd);
  338. return ret;
  339. }
  340. static int adf_copy_attachment_list_to_user(
  341. struct adf_attachment_config __user *to, size_t n_to,
  342. struct adf_attachment *from, size_t n_from)
  343. {
  344. struct adf_attachment_config *temp;
  345. size_t n = min(n_to, n_from);
  346. size_t i;
  347. int ret = 0;
  348. if (!n)
  349. return 0;
  350. temp = kzalloc(n * sizeof(temp[0]), GFP_KERNEL);
  351. if (!temp)
  352. return -ENOMEM;
  353. for (i = 0; i < n; i++) {
  354. temp[i].interface = from[i].interface->base.id;
  355. temp[i].overlay_engine = from[i].overlay_engine->base.id;
  356. }
  357. if (copy_to_user(to, temp, n * sizeof(to[0]))) {
  358. ret = -EFAULT;
  359. goto done;
  360. }
  361. done:
  362. kfree(temp);
  363. return ret;
  364. }
  365. static int adf_device_get_data(struct adf_device *dev,
  366. struct adf_device_data __user *arg)
  367. {
  368. struct adf_device_data data;
  369. size_t n_attach;
  370. struct adf_attachment *attach = NULL;
  371. size_t n_allowed_attach;
  372. struct adf_attachment *allowed_attach = NULL;
  373. int ret = 0;
  374. if (copy_from_user(&data, arg, sizeof(data)))
  375. return -EFAULT;
  376. if (data.n_attachments > ADF_MAX_ATTACHMENTS ||
  377. data.n_allowed_attachments > ADF_MAX_ATTACHMENTS)
  378. return -EINVAL;
  379. strlcpy(data.name, dev->base.name, sizeof(data.name));
  380. if (data.n_attachments) {
  381. attach = kzalloc(data.n_attachments * sizeof(attach[0]),
  382. GFP_KERNEL);
  383. if (!attach)
  384. return -ENOMEM;
  385. }
  386. n_attach = adf_device_attachments(dev, attach, data.n_attachments);
  387. if (data.n_allowed_attachments) {
  388. allowed_attach = kzalloc(data.n_allowed_attachments *
  389. sizeof(allowed_attach[0]), GFP_KERNEL);
  390. if (!allowed_attach) {
  391. ret = -ENOMEM;
  392. goto done;
  393. }
  394. }
  395. n_allowed_attach = adf_device_attachments_allowed(dev, allowed_attach,
  396. data.n_allowed_attachments);
  397. mutex_lock(&dev->client_lock);
  398. ret = adf_obj_copy_custom_data_to_user(&dev->base, arg->custom_data,
  399. &data.custom_data_size);
  400. mutex_unlock(&dev->client_lock);
  401. if (ret < 0)
  402. goto done;
  403. ret = adf_copy_attachment_list_to_user(arg->attachments,
  404. data.n_attachments, attach, n_attach);
  405. if (ret < 0)
  406. goto done;
  407. ret = adf_copy_attachment_list_to_user(arg->allowed_attachments,
  408. data.n_allowed_attachments, allowed_attach,
  409. n_allowed_attach);
  410. if (ret < 0)
  411. goto done;
  412. data.n_attachments = n_attach;
  413. data.n_allowed_attachments = n_allowed_attach;
  414. if (copy_to_user(arg, &data, sizeof(data)))
  415. ret = -EFAULT;
  416. done:
  417. kfree(allowed_attach);
  418. kfree(attach);
  419. return ret;
  420. }
  421. static int adf_device_handle_attachment(struct adf_device *dev,
  422. struct adf_attachment_config __user *arg, bool attach)
  423. {
  424. struct adf_attachment_config data;
  425. struct adf_overlay_engine *eng;
  426. struct adf_interface *intf;
  427. if (copy_from_user(&data, arg, sizeof(data)))
  428. return -EFAULT;
  429. eng = idr_find(&dev->overlay_engines, data.overlay_engine);
  430. if (!eng) {
  431. dev_err(&dev->base.dev, "invalid overlay engine id %u\n",
  432. data.overlay_engine);
  433. return -EINVAL;
  434. }
  435. intf = idr_find(&dev->interfaces, data.interface);
  436. if (!intf) {
  437. dev_err(&dev->base.dev, "invalid interface id %u\n",
  438. data.interface);
  439. return -EINVAL;
  440. }
  441. if (attach)
  442. return adf_device_attach(dev, eng, intf);
  443. else
  444. return adf_device_detach(dev, eng, intf);
  445. }
  446. static int adf_intf_set_mode(struct adf_interface *intf,
  447. struct drm_mode_modeinfo __user *arg)
  448. {
  449. struct drm_mode_modeinfo mode;
  450. if (copy_from_user(&mode, arg, sizeof(mode)))
  451. return -EFAULT;
  452. return adf_interface_set_mode(intf, &mode);
  453. }
  454. static int adf_intf_get_data(struct adf_interface *intf,
  455. struct adf_interface_data __user *arg)
  456. {
  457. struct adf_device *dev = adf_interface_parent(intf);
  458. struct adf_interface_data data;
  459. struct drm_mode_modeinfo *modelist;
  460. size_t modelist_size;
  461. int err;
  462. int ret = 0;
  463. unsigned long flags;
  464. if (copy_from_user(&data, arg, sizeof(data)))
  465. return -EFAULT;
  466. strlcpy(data.name, intf->base.name, sizeof(data.name));
  467. data.type = intf->type;
  468. data.id = intf->idx;
  469. data.flags = intf->flags;
  470. err = adf_interface_get_screen_size(intf, &data.width_mm,
  471. &data.height_mm);
  472. if (err < 0) {
  473. data.width_mm = 0;
  474. data.height_mm = 0;
  475. }
  476. modelist = kmalloc(sizeof(modelist[0]) * ADF_MAX_MODES, GFP_KERNEL);
  477. if (!modelist)
  478. return -ENOMEM;
  479. mutex_lock(&dev->client_lock);
  480. read_lock_irqsave(&intf->hotplug_modelist_lock, flags);
  481. data.hotplug_detect = intf->hotplug_detect;
  482. modelist_size = min(data.n_available_modes, intf->n_modes) *
  483. sizeof(intf->modelist[0]);
  484. memcpy(modelist, intf->modelist, modelist_size);
  485. data.n_available_modes = intf->n_modes;
  486. read_unlock_irqrestore(&intf->hotplug_modelist_lock, flags);
  487. if (copy_to_user(arg->available_modes, modelist, modelist_size)) {
  488. ret = -EFAULT;
  489. goto done;
  490. }
  491. data.dpms_state = intf->dpms_state;
  492. memcpy(&data.current_mode, &intf->current_mode,
  493. sizeof(intf->current_mode));
  494. ret = adf_obj_copy_custom_data_to_user(&intf->base, arg->custom_data,
  495. &data.custom_data_size);
  496. done:
  497. mutex_unlock(&dev->client_lock);
  498. kfree(modelist);
  499. if (ret < 0)
  500. return ret;
  501. if (copy_to_user(arg, &data, sizeof(data)))
  502. ret = -EFAULT;
  503. return ret;
  504. }
  505. static inline long adf_obj_custom_ioctl(struct adf_obj *obj, unsigned int cmd,
  506. unsigned long arg)
  507. {
  508. if (obj->ops && obj->ops->ioctl)
  509. return obj->ops->ioctl(obj, cmd, arg);
  510. return -ENOTTY;
  511. }
  512. static long adf_overlay_engine_ioctl(struct adf_overlay_engine *eng,
  513. struct adf_file *file, unsigned int cmd, unsigned long arg)
  514. {
  515. switch (cmd) {
  516. case ADF_SET_EVENT:
  517. return adf_obj_set_event(&eng->base, file,
  518. (struct adf_set_event __user *)arg);
  519. case ADF_GET_OVERLAY_ENGINE_DATA:
  520. return adf_eng_get_data(eng,
  521. (struct adf_overlay_engine_data __user *)arg);
  522. case ADF_BLANK:
  523. case ADF_POST_CONFIG:
  524. case ADF_SET_MODE:
  525. case ADF_GET_DEVICE_DATA:
  526. case ADF_GET_INTERFACE_DATA:
  527. case ADF_SIMPLE_POST_CONFIG:
  528. case ADF_SIMPLE_BUFFER_ALLOC:
  529. case ADF_ATTACH:
  530. case ADF_DETACH:
  531. return -EINVAL;
  532. default:
  533. return adf_obj_custom_ioctl(&eng->base, cmd, arg);
  534. }
  535. }
  536. static long adf_interface_ioctl(struct adf_interface *intf,
  537. struct adf_file *file, unsigned int cmd, unsigned long arg)
  538. {
  539. switch (cmd) {
  540. case ADF_SET_EVENT:
  541. return adf_obj_set_event(&intf->base, file,
  542. (struct adf_set_event __user *)arg);
  543. case ADF_BLANK:
  544. return adf_interface_blank(intf, arg);
  545. case ADF_SET_MODE:
  546. return adf_intf_set_mode(intf,
  547. (struct drm_mode_modeinfo __user *)arg);
  548. case ADF_GET_INTERFACE_DATA:
  549. return adf_intf_get_data(intf,
  550. (struct adf_interface_data __user *)arg);
  551. case ADF_SIMPLE_POST_CONFIG:
  552. return adf_intf_simple_post_config(intf,
  553. (struct adf_simple_post_config __user *)arg);
  554. case ADF_SIMPLE_BUFFER_ALLOC:
  555. return adf_intf_simple_buffer_alloc(intf,
  556. (struct adf_simple_buffer_alloc __user *)arg);
  557. case ADF_POST_CONFIG:
  558. case ADF_GET_DEVICE_DATA:
  559. case ADF_GET_OVERLAY_ENGINE_DATA:
  560. case ADF_ATTACH:
  561. case ADF_DETACH:
  562. return -EINVAL;
  563. default:
  564. return adf_obj_custom_ioctl(&intf->base, cmd, arg);
  565. }
  566. }
  567. static long adf_device_ioctl(struct adf_device *dev, struct adf_file *file,
  568. unsigned int cmd, unsigned long arg)
  569. {
  570. switch (cmd) {
  571. case ADF_SET_EVENT:
  572. return adf_obj_set_event(&dev->base, file,
  573. (struct adf_set_event __user *)arg);
  574. case ADF_POST_CONFIG:
  575. return adf_device_post_config(dev,
  576. (struct adf_post_config __user *)arg);
  577. case ADF_GET_DEVICE_DATA:
  578. return adf_device_get_data(dev,
  579. (struct adf_device_data __user *)arg);
  580. case ADF_ATTACH:
  581. return adf_device_handle_attachment(dev,
  582. (struct adf_attachment_config __user *)arg,
  583. true);
  584. case ADF_DETACH:
  585. return adf_device_handle_attachment(dev,
  586. (struct adf_attachment_config __user *)arg,
  587. false);
  588. case ADF_BLANK:
  589. case ADF_SET_MODE:
  590. case ADF_GET_INTERFACE_DATA:
  591. case ADF_GET_OVERLAY_ENGINE_DATA:
  592. case ADF_SIMPLE_POST_CONFIG:
  593. case ADF_SIMPLE_BUFFER_ALLOC:
  594. return -EINVAL;
  595. default:
  596. return adf_obj_custom_ioctl(&dev->base, cmd, arg);
  597. }
  598. }
  599. static int adf_file_open(struct inode *inode, struct file *file)
  600. {
  601. struct adf_obj *obj;
  602. struct adf_file *fpriv = NULL;
  603. unsigned long flags;
  604. int ret = 0;
  605. obj = adf_obj_sysfs_find(iminor(inode));
  606. if (!obj)
  607. return -ENODEV;
  608. dev_dbg(&obj->dev, "opening %s\n", dev_name(&obj->dev));
  609. if (!try_module_get(obj->parent->ops->owner)) {
  610. dev_err(&obj->dev, "getting owner module failed\n");
  611. return -ENODEV;
  612. }
  613. fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
  614. if (!fpriv) {
  615. ret = -ENOMEM;
  616. goto done;
  617. }
  618. INIT_LIST_HEAD(&fpriv->head);
  619. fpriv->obj = obj;
  620. init_waitqueue_head(&fpriv->event_wait);
  621. file->private_data = fpriv;
  622. if (obj->ops && obj->ops->open) {
  623. ret = obj->ops->open(obj, inode, file);
  624. if (ret < 0)
  625. goto done;
  626. }
  627. spin_lock_irqsave(&obj->file_lock, flags);
  628. list_add_tail(&fpriv->head, &obj->file_list);
  629. spin_unlock_irqrestore(&obj->file_lock, flags);
  630. done:
  631. if (ret < 0) {
  632. kfree(fpriv);
  633. module_put(obj->parent->ops->owner);
  634. }
  635. return ret;
  636. }
  637. static int adf_file_release(struct inode *inode, struct file *file)
  638. {
  639. struct adf_file *fpriv = file->private_data;
  640. struct adf_obj *obj = fpriv->obj;
  641. enum adf_event_type event_type;
  642. unsigned long flags;
  643. if (obj->ops && obj->ops->release)
  644. obj->ops->release(obj, inode, file);
  645. spin_lock_irqsave(&obj->file_lock, flags);
  646. list_del(&fpriv->head);
  647. spin_unlock_irqrestore(&obj->file_lock, flags);
  648. for_each_set_bit(event_type, fpriv->event_subscriptions,
  649. ADF_EVENT_TYPE_MAX) {
  650. adf_event_put(obj, event_type);
  651. }
  652. kfree(fpriv);
  653. module_put(obj->parent->ops->owner);
  654. dev_dbg(&obj->dev, "released %s\n", dev_name(&obj->dev));
  655. return 0;
  656. }
  657. long adf_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  658. {
  659. struct adf_file *fpriv = file->private_data;
  660. struct adf_obj *obj = fpriv->obj;
  661. long ret = -EINVAL;
  662. dev_dbg(&obj->dev, "%s ioctl %u\n", dev_name(&obj->dev), _IOC_NR(cmd));
  663. switch (obj->type) {
  664. case ADF_OBJ_OVERLAY_ENGINE:
  665. ret = adf_overlay_engine_ioctl(adf_obj_to_overlay_engine(obj),
  666. fpriv, cmd, arg);
  667. break;
  668. case ADF_OBJ_INTERFACE:
  669. ret = adf_interface_ioctl(adf_obj_to_interface(obj), fpriv, cmd,
  670. arg);
  671. break;
  672. case ADF_OBJ_DEVICE:
  673. ret = adf_device_ioctl(adf_obj_to_device(obj), fpriv, cmd, arg);
  674. break;
  675. }
  676. return ret;
  677. }
  678. static inline bool adf_file_event_available(struct adf_file *fpriv)
  679. {
  680. int head = fpriv->event_head;
  681. int tail = fpriv->event_tail;
  682. return CIRC_CNT(head, tail, sizeof(fpriv->event_buf)) != 0;
  683. }
  684. void adf_file_queue_event(struct adf_file *fpriv, struct adf_event *event)
  685. {
  686. int head = fpriv->event_head;
  687. int tail = fpriv->event_tail;
  688. size_t space = CIRC_SPACE(head, tail, sizeof(fpriv->event_buf));
  689. size_t space_to_end =
  690. CIRC_SPACE_TO_END(head, tail, sizeof(fpriv->event_buf));
  691. if (space < event->length) {
  692. dev_dbg(&fpriv->obj->dev,
  693. "insufficient buffer space for event %u\n",
  694. event->type);
  695. return;
  696. }
  697. if (space_to_end >= event->length) {
  698. memcpy(fpriv->event_buf + head, event, event->length);
  699. } else {
  700. memcpy(fpriv->event_buf + head, event, space_to_end);
  701. memcpy(fpriv->event_buf, (u8 *)event + space_to_end,
  702. event->length - space_to_end);
  703. }
  704. smp_wmb();
  705. fpriv->event_head = (fpriv->event_head + event->length) &
  706. (sizeof(fpriv->event_buf) - 1);
  707. wake_up_interruptible_all(&fpriv->event_wait);
  708. }
  709. static ssize_t adf_file_copy_to_user(struct adf_file *fpriv,
  710. char __user *buffer, size_t buffer_size)
  711. {
  712. int head, tail;
  713. u8 *event_buf;
  714. size_t cnt, cnt_to_end, copy_size = 0;
  715. ssize_t ret = 0;
  716. unsigned long flags;
  717. event_buf = kmalloc(min(buffer_size, sizeof(fpriv->event_buf)),
  718. GFP_KERNEL);
  719. if (!event_buf)
  720. return -ENOMEM;
  721. spin_lock_irqsave(&fpriv->obj->file_lock, flags);
  722. if (!adf_file_event_available(fpriv))
  723. goto out;
  724. head = fpriv->event_head;
  725. tail = fpriv->event_tail;
  726. cnt = CIRC_CNT(head, tail, sizeof(fpriv->event_buf));
  727. cnt_to_end = CIRC_CNT_TO_END(head, tail, sizeof(fpriv->event_buf));
  728. copy_size = min(buffer_size, cnt);
  729. if (cnt_to_end >= copy_size) {
  730. memcpy(event_buf, fpriv->event_buf + tail, copy_size);
  731. } else {
  732. memcpy(event_buf, fpriv->event_buf + tail, cnt_to_end);
  733. memcpy(event_buf + cnt_to_end, fpriv->event_buf,
  734. copy_size - cnt_to_end);
  735. }
  736. fpriv->event_tail = (fpriv->event_tail + copy_size) &
  737. (sizeof(fpriv->event_buf) - 1);
  738. out:
  739. spin_unlock_irqrestore(&fpriv->obj->file_lock, flags);
  740. if (copy_size) {
  741. if (copy_to_user(buffer, event_buf, copy_size))
  742. ret = -EFAULT;
  743. else
  744. ret = copy_size;
  745. }
  746. kfree(event_buf);
  747. return ret;
  748. }
  749. ssize_t adf_file_read(struct file *filp, char __user *buffer,
  750. size_t count, loff_t *offset)
  751. {
  752. struct adf_file *fpriv = filp->private_data;
  753. int err;
  754. err = wait_event_interruptible(fpriv->event_wait,
  755. adf_file_event_available(fpriv));
  756. if (err < 0)
  757. return err;
  758. return adf_file_copy_to_user(fpriv, buffer, count);
  759. }
  760. unsigned int adf_file_poll(struct file *filp, struct poll_table_struct *wait)
  761. {
  762. struct adf_file *fpriv = filp->private_data;
  763. unsigned int mask = 0;
  764. poll_wait(filp, &fpriv->event_wait, wait);
  765. if (adf_file_event_available(fpriv))
  766. mask |= POLLIN | POLLRDNORM;
  767. return mask;
  768. }
  769. const struct file_operations adf_fops = {
  770. .owner = THIS_MODULE,
  771. .unlocked_ioctl = adf_file_ioctl,
  772. #ifdef CONFIG_COMPAT
  773. .compat_ioctl = adf_file_compat_ioctl,
  774. #endif
  775. .open = adf_file_open,
  776. .release = adf_file_release,
  777. .llseek = default_llseek,
  778. .read = adf_file_read,
  779. .poll = adf_file_poll,
  780. };