2 * Copyright (C) 2013 Google, Inc.
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.
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.
15 #include <linux/bitops.h>
16 #include <linux/circ_buf.h>
18 #include <linux/module.h>
19 #include <linux/poll.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
23 #include <video/adf_client.h>
24 #include <video/adf_format.h>
31 #include "adf_sysfs.h"
34 #include "adf_fops32.h"
37 static int adf_obj_set_event(struct adf_obj *obj, struct adf_file *file,
38 struct adf_set_event __user *arg)
40 struct adf_set_event data;
45 if (copy_from_user(&data, arg, sizeof(data)))
48 err = adf_obj_check_supports_event(obj, data.type);
52 spin_lock_irqsave(&obj->file_lock, flags);
54 enabled = test_and_set_bit(data.type,
55 file->event_subscriptions);
57 enabled = test_and_clear_bit(data.type,
58 file->event_subscriptions);
59 spin_unlock_irqrestore(&obj->file_lock, flags);
61 if (data.enabled == enabled)
65 adf_event_get(obj, data.type);
67 adf_event_put(obj, data.type);
72 static int adf_obj_copy_custom_data_to_user(struct adf_obj *obj,
73 void __user *dst, size_t *dst_size)
76 size_t custom_data_size;
79 if (!obj->ops || !obj->ops->custom_data) {
80 dev_dbg(&obj->dev, "%s: no custom_data op\n", __func__);
84 custom_data = kzalloc(ADF_MAX_CUSTOM_DATA_SIZE, GFP_KERNEL);
88 ret = obj->ops->custom_data(obj, custom_data, &custom_data_size);
92 if (copy_to_user(dst, custom_data, min(*dst_size, custom_data_size))) {
96 *dst_size = custom_data_size;
103 static int adf_eng_get_data(struct adf_overlay_engine *eng,
104 struct adf_overlay_engine_data __user *arg)
106 struct adf_device *dev = adf_overlay_engine_parent(eng);
107 struct adf_overlay_engine_data data;
108 size_t n_supported_formats;
109 u32 *supported_formats = NULL;
112 if (copy_from_user(&data, arg, sizeof(data)))
115 strlcpy(data.name, eng->base.name, sizeof(data.name));
117 if (data.n_supported_formats > ADF_MAX_SUPPORTED_FORMATS)
120 n_supported_formats = data.n_supported_formats;
121 data.n_supported_formats = eng->ops->n_supported_formats;
123 if (n_supported_formats) {
124 supported_formats = kzalloc(n_supported_formats *
125 sizeof(supported_formats[0]), GFP_KERNEL);
126 if (!supported_formats)
130 memcpy(supported_formats, eng->ops->supported_formats,
131 sizeof(u32) * min(n_supported_formats,
132 eng->ops->n_supported_formats));
134 mutex_lock(&dev->client_lock);
135 ret = adf_obj_copy_custom_data_to_user(&eng->base, data.custom_data,
136 &data.custom_data_size);
137 mutex_unlock(&dev->client_lock);
142 if (copy_to_user(arg, &data, sizeof(data))) {
147 if (supported_formats && copy_to_user(data.supported_formats,
149 n_supported_formats * sizeof(supported_formats[0])))
153 kfree(supported_formats);
157 static int adf_buffer_import(struct adf_device *dev,
158 struct adf_buffer_config __user *cfg, struct adf_buffer *buf)
160 struct adf_buffer_config user_buf;
164 if (copy_from_user(&user_buf, cfg, sizeof(user_buf)))
167 memset(buf, 0, sizeof(*buf));
169 if (user_buf.n_planes > ADF_MAX_PLANES) {
170 dev_err(&dev->base.dev, "invalid plane count %u\n",
175 buf->overlay_engine = idr_find(&dev->overlay_engines,
176 user_buf.overlay_engine);
177 if (!buf->overlay_engine) {
178 dev_err(&dev->base.dev, "invalid overlay engine id %u\n",
179 user_buf.overlay_engine);
185 buf->format = user_buf.format;
186 for (i = 0; i < user_buf.n_planes; i++) {
187 buf->dma_bufs[i] = dma_buf_get(user_buf.fd[i]);
188 if (IS_ERR(buf->dma_bufs[i])) {
189 ret = PTR_ERR(buf->dma_bufs[i]);
190 dev_err(&dev->base.dev, "importing dma_buf fd %d failed: %d\n",
191 user_buf.fd[i], ret);
192 buf->dma_bufs[i] = NULL;
195 buf->offset[i] = user_buf.offset[i];
196 buf->pitch[i] = user_buf.pitch[i];
198 buf->n_planes = user_buf.n_planes;
200 if (user_buf.acquire_fence >= 0) {
201 buf->acquire_fence = sync_fence_fdget(user_buf.acquire_fence);
202 if (!buf->acquire_fence) {
203 dev_err(&dev->base.dev, "getting fence fd %d failed\n",
204 user_buf.acquire_fence);
212 adf_buffer_cleanup(buf);
216 static int adf_device_post_config(struct adf_device *dev,
217 struct adf_post_config __user *arg)
219 struct sync_fence *complete_fence;
220 int complete_fence_fd;
221 struct adf_buffer *bufs = NULL;
222 struct adf_interface **intfs = NULL;
223 struct adf_post_config data;
225 void *custom_data = NULL;
228 if (copy_from_user(&data, arg, sizeof(data)))
231 complete_fence_fd = get_unused_fd_flags(O_CLOEXEC);
232 if (complete_fence_fd < 0)
233 return complete_fence_fd;
235 if (data.n_interfaces > ADF_MAX_INTERFACES) {
240 if (data.n_bufs > ADF_MAX_BUFFERS) {
245 if (data.custom_data_size > ADF_MAX_CUSTOM_DATA_SIZE) {
250 if (data.n_interfaces) {
251 intfs = kmalloc(sizeof(intfs[0]) * data.n_interfaces,
259 for (i = 0; i < data.n_interfaces; i++) {
261 if (get_user(intf_id, &data.interfaces[i])) {
266 intfs[i] = idr_find(&dev->interfaces, intf_id);
274 bufs = kzalloc(sizeof(bufs[0]) * data.n_bufs, GFP_KERNEL);
281 for (i = 0; i < data.n_bufs; i++) {
282 ret = adf_buffer_import(dev, &data.bufs[i], &bufs[i]);
284 memset(&bufs[i], 0, sizeof(bufs[i]));
289 if (data.custom_data_size) {
290 custom_data = kzalloc(data.custom_data_size, GFP_KERNEL);
296 if (copy_from_user(custom_data, data.custom_data,
297 data.custom_data_size)) {
303 if (put_user(complete_fence_fd, &arg->complete_fence)) {
308 complete_fence = adf_device_post_nocopy(dev, intfs, data.n_interfaces,
309 bufs, data.n_bufs, custom_data, data.custom_data_size);
310 if (IS_ERR(complete_fence)) {
311 ret = PTR_ERR(complete_fence);
315 sync_fence_install(complete_fence, complete_fence_fd);
319 for (i = 0; i < data.n_bufs; i++)
320 adf_buffer_cleanup(&bufs[i]);
326 put_unused_fd(complete_fence_fd);
330 static int adf_intf_simple_post_config(struct adf_interface *intf,
331 struct adf_simple_post_config __user *arg)
333 struct adf_device *dev = intf->base.parent;
334 struct sync_fence *complete_fence;
335 int complete_fence_fd;
336 struct adf_buffer buf;
339 complete_fence_fd = get_unused_fd_flags(O_CLOEXEC);
340 if (complete_fence_fd < 0)
341 return complete_fence_fd;
343 ret = adf_buffer_import(dev, &arg->buf, &buf);
347 if (put_user(complete_fence_fd, &arg->complete_fence)) {
352 complete_fence = adf_interface_simple_post(intf, &buf);
353 if (IS_ERR(complete_fence)) {
354 ret = PTR_ERR(complete_fence);
358 sync_fence_install(complete_fence, complete_fence_fd);
362 adf_buffer_cleanup(&buf);
364 put_unused_fd(complete_fence_fd);
368 static int adf_intf_simple_buffer_alloc(struct adf_interface *intf,
369 struct adf_simple_buffer_alloc __user *arg)
371 struct adf_simple_buffer_alloc data;
372 struct dma_buf *dma_buf;
375 if (copy_from_user(&data, arg, sizeof(data)))
378 data.fd = get_unused_fd_flags(O_CLOEXEC);
382 ret = adf_interface_simple_buffer_alloc(intf, data.w, data.h,
383 data.format, &dma_buf, &data.offset, &data.pitch);
387 if (copy_to_user(arg, &data, sizeof(*arg))) {
392 fd_install(data.fd, dma_buf->file);
396 dma_buf_put(dma_buf);
399 put_unused_fd(data.fd);
403 static int adf_copy_attachment_list_to_user(
404 struct adf_attachment_config __user *to, size_t n_to,
405 struct adf_attachment *from, size_t n_from)
407 struct adf_attachment_config *temp;
408 size_t n = min(n_to, n_from);
415 temp = kzalloc(n * sizeof(temp[0]), GFP_KERNEL);
419 for (i = 0; i < n; i++) {
420 temp[i].interface = from[i].interface->base.id;
421 temp[i].overlay_engine = from[i].overlay_engine->base.id;
424 if (copy_to_user(to, temp, n * sizeof(to[0]))) {
434 static int adf_device_get_data(struct adf_device *dev,
435 struct adf_device_data __user *arg)
437 struct adf_device_data data;
439 struct adf_attachment *attach = NULL;
440 size_t n_allowed_attach;
441 struct adf_attachment *allowed_attach = NULL;
444 if (copy_from_user(&data, arg, sizeof(data)))
447 if (data.n_attachments > ADF_MAX_ATTACHMENTS ||
448 data.n_allowed_attachments > ADF_MAX_ATTACHMENTS)
451 strlcpy(data.name, dev->base.name, sizeof(data.name));
453 if (data.n_attachments) {
454 attach = kzalloc(data.n_attachments * sizeof(attach[0]),
459 n_attach = adf_device_attachments(dev, attach, data.n_attachments);
461 if (data.n_allowed_attachments) {
462 allowed_attach = kzalloc(data.n_allowed_attachments *
463 sizeof(allowed_attach[0]), GFP_KERNEL);
464 if (!allowed_attach) {
469 n_allowed_attach = adf_device_attachments_allowed(dev, allowed_attach,
470 data.n_allowed_attachments);
472 mutex_lock(&dev->client_lock);
473 ret = adf_obj_copy_custom_data_to_user(&dev->base, data.custom_data,
474 &data.custom_data_size);
475 mutex_unlock(&dev->client_lock);
480 ret = adf_copy_attachment_list_to_user(data.attachments,
481 data.n_attachments, attach, n_attach);
485 ret = adf_copy_attachment_list_to_user(data.allowed_attachments,
486 data.n_allowed_attachments, allowed_attach,
491 data.n_attachments = n_attach;
492 data.n_allowed_attachments = n_allowed_attach;
494 if (copy_to_user(arg, &data, sizeof(data)))
498 kfree(allowed_attach);
503 static int adf_device_handle_attachment(struct adf_device *dev,
504 struct adf_attachment_config __user *arg, bool attach)
506 struct adf_attachment_config data;
507 struct adf_overlay_engine *eng;
508 struct adf_interface *intf;
510 if (copy_from_user(&data, arg, sizeof(data)))
513 eng = idr_find(&dev->overlay_engines, data.overlay_engine);
515 dev_err(&dev->base.dev, "invalid overlay engine id %u\n",
516 data.overlay_engine);
520 intf = idr_find(&dev->interfaces, data.interface);
522 dev_err(&dev->base.dev, "invalid interface id %u\n",
528 return adf_device_attach(dev, eng, intf);
530 return adf_device_detach(dev, eng, intf);
533 static int adf_intf_set_mode(struct adf_interface *intf,
534 struct drm_mode_modeinfo __user *arg)
536 struct drm_mode_modeinfo mode;
538 if (copy_from_user(&mode, arg, sizeof(mode)))
541 return adf_interface_set_mode(intf, &mode);
544 static int adf_intf_get_data(struct adf_interface *intf,
545 struct adf_interface_data __user *arg)
547 struct adf_device *dev = adf_interface_parent(intf);
548 struct adf_interface_data data;
549 struct drm_mode_modeinfo *modelist;
550 size_t modelist_size;
555 if (copy_from_user(&data, arg, sizeof(data)))
558 strlcpy(data.name, intf->base.name, sizeof(data.name));
560 data.type = intf->type;
562 data.flags = intf->flags;
564 err = adf_interface_get_screen_size(intf, &data.width_mm,
571 modelist = kmalloc(sizeof(modelist[0]) * ADF_MAX_MODES, GFP_KERNEL);
575 mutex_lock(&dev->client_lock);
576 read_lock_irqsave(&intf->hotplug_modelist_lock, flags);
577 data.hotplug_detect = intf->hotplug_detect;
578 modelist_size = min(data.n_available_modes, intf->n_modes) *
579 sizeof(intf->modelist[0]);
580 memcpy(modelist, intf->modelist, modelist_size);
581 data.n_available_modes = intf->n_modes;
582 read_unlock_irqrestore(&intf->hotplug_modelist_lock, flags);
584 if (copy_to_user(data.available_modes, modelist, modelist_size)) {
589 data.dpms_state = intf->dpms_state;
590 memcpy(&data.current_mode, &intf->current_mode,
591 sizeof(intf->current_mode));
593 ret = adf_obj_copy_custom_data_to_user(&intf->base, data.custom_data,
594 &data.custom_data_size);
596 mutex_unlock(&dev->client_lock);
602 if (copy_to_user(arg, &data, sizeof(data)))
608 static inline long adf_obj_custom_ioctl(struct adf_obj *obj, unsigned int cmd,
611 if (obj->ops && obj->ops->ioctl)
612 return obj->ops->ioctl(obj, cmd, arg);
616 static long adf_overlay_engine_ioctl(struct adf_overlay_engine *eng,
617 struct adf_file *file, unsigned int cmd, unsigned long arg)
621 return adf_obj_set_event(&eng->base, file,
622 (struct adf_set_event __user *)arg);
624 case ADF_GET_OVERLAY_ENGINE_DATA:
625 return adf_eng_get_data(eng,
626 (struct adf_overlay_engine_data __user *)arg);
629 case ADF_POST_CONFIG:
631 case ADF_GET_DEVICE_DATA:
632 case ADF_GET_INTERFACE_DATA:
633 case ADF_SIMPLE_POST_CONFIG:
634 case ADF_SIMPLE_BUFFER_ALLOC:
640 return adf_obj_custom_ioctl(&eng->base, cmd, arg);
644 static long adf_interface_ioctl(struct adf_interface *intf,
645 struct adf_file *file, unsigned int cmd, unsigned long arg)
649 return adf_obj_set_event(&intf->base, file,
650 (struct adf_set_event __user *)arg);
653 return adf_interface_blank(intf, arg);
656 return adf_intf_set_mode(intf,
657 (struct drm_mode_modeinfo __user *)arg);
659 case ADF_GET_INTERFACE_DATA:
660 return adf_intf_get_data(intf,
661 (struct adf_interface_data __user *)arg);
663 case ADF_SIMPLE_POST_CONFIG:
664 return adf_intf_simple_post_config(intf,
665 (struct adf_simple_post_config __user *)arg);
667 case ADF_SIMPLE_BUFFER_ALLOC:
668 return adf_intf_simple_buffer_alloc(intf,
669 (struct adf_simple_buffer_alloc __user *)arg);
671 case ADF_POST_CONFIG:
672 case ADF_GET_DEVICE_DATA:
673 case ADF_GET_OVERLAY_ENGINE_DATA:
679 return adf_obj_custom_ioctl(&intf->base, cmd, arg);
683 static long adf_device_ioctl(struct adf_device *dev, struct adf_file *file,
684 unsigned int cmd, unsigned long arg)
688 return adf_obj_set_event(&dev->base, file,
689 (struct adf_set_event __user *)arg);
691 case ADF_POST_CONFIG:
692 return adf_device_post_config(dev,
693 (struct adf_post_config __user *)arg);
695 case ADF_GET_DEVICE_DATA:
696 return adf_device_get_data(dev,
697 (struct adf_device_data __user *)arg);
700 return adf_device_handle_attachment(dev,
701 (struct adf_attachment_config __user *)arg,
705 return adf_device_handle_attachment(dev,
706 (struct adf_attachment_config __user *)arg,
711 case ADF_GET_INTERFACE_DATA:
712 case ADF_GET_OVERLAY_ENGINE_DATA:
713 case ADF_SIMPLE_POST_CONFIG:
714 case ADF_SIMPLE_BUFFER_ALLOC:
718 return adf_obj_custom_ioctl(&dev->base, cmd, arg);
722 static int adf_file_open(struct inode *inode, struct file *file)
725 struct adf_file *fpriv = NULL;
729 obj = adf_obj_sysfs_find(iminor(inode));
733 dev_dbg(&obj->dev, "opening %s\n", dev_name(&obj->dev));
735 if (!try_module_get(obj->parent->ops->owner)) {
736 dev_err(&obj->dev, "getting owner module failed\n");
740 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
746 INIT_LIST_HEAD(&fpriv->head);
748 init_waitqueue_head(&fpriv->event_wait);
750 file->private_data = fpriv;
752 if (obj->ops && obj->ops->open) {
753 ret = obj->ops->open(obj, inode, file);
758 spin_lock_irqsave(&obj->file_lock, flags);
759 list_add_tail(&fpriv->head, &obj->file_list);
760 spin_unlock_irqrestore(&obj->file_lock, flags);
765 module_put(obj->parent->ops->owner);
770 static int adf_file_release(struct inode *inode, struct file *file)
772 struct adf_file *fpriv = file->private_data;
773 struct adf_obj *obj = fpriv->obj;
774 enum adf_event_type event_type;
777 if (obj->ops && obj->ops->release)
778 obj->ops->release(obj, inode, file);
780 spin_lock_irqsave(&obj->file_lock, flags);
781 list_del(&fpriv->head);
782 spin_unlock_irqrestore(&obj->file_lock, flags);
784 for_each_set_bit(event_type, fpriv->event_subscriptions,
785 ADF_EVENT_TYPE_MAX) {
786 adf_event_put(obj, event_type);
790 module_put(obj->parent->ops->owner);
792 dev_dbg(&obj->dev, "released %s\n", dev_name(&obj->dev));
796 long adf_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
798 struct adf_file *fpriv = file->private_data;
799 struct adf_obj *obj = fpriv->obj;
802 dev_dbg(&obj->dev, "%s ioctl %u\n", dev_name(&obj->dev), _IOC_NR(cmd));
805 case ADF_OBJ_OVERLAY_ENGINE:
806 ret = adf_overlay_engine_ioctl(adf_obj_to_overlay_engine(obj),
810 case ADF_OBJ_INTERFACE:
811 ret = adf_interface_ioctl(adf_obj_to_interface(obj), fpriv, cmd,
816 ret = adf_device_ioctl(adf_obj_to_device(obj), fpriv, cmd, arg);
823 static inline bool adf_file_event_available(struct adf_file *fpriv)
825 int head = fpriv->event_head;
826 int tail = fpriv->event_tail;
827 return CIRC_CNT(head, tail, sizeof(fpriv->event_buf)) != 0;
830 void adf_file_queue_event(struct adf_file *fpriv, struct adf_event *event)
832 int head = fpriv->event_head;
833 int tail = fpriv->event_tail;
834 size_t space = CIRC_SPACE(head, tail, sizeof(fpriv->event_buf));
835 size_t space_to_end =
836 CIRC_SPACE_TO_END(head, tail, sizeof(fpriv->event_buf));
838 if (space < event->length) {
839 dev_dbg(&fpriv->obj->dev,
840 "insufficient buffer space for event %u\n",
845 if (space_to_end >= event->length) {
846 memcpy(fpriv->event_buf + head, event, event->length);
848 memcpy(fpriv->event_buf + head, event, space_to_end);
849 memcpy(fpriv->event_buf, (u8 *)event + space_to_end,
850 event->length - space_to_end);
854 fpriv->event_head = (fpriv->event_head + event->length) &
855 (sizeof(fpriv->event_buf) - 1);
856 wake_up_interruptible_all(&fpriv->event_wait);
859 static ssize_t adf_file_copy_to_user(struct adf_file *fpriv,
860 char __user *buffer, size_t buffer_size)
864 size_t cnt, cnt_to_end, copy_size = 0;
868 event_buf = kmalloc(min(buffer_size, sizeof(fpriv->event_buf)),
873 spin_lock_irqsave(&fpriv->obj->file_lock, flags);
875 if (!adf_file_event_available(fpriv))
878 head = fpriv->event_head;
879 tail = fpriv->event_tail;
881 cnt = CIRC_CNT(head, tail, sizeof(fpriv->event_buf));
882 cnt_to_end = CIRC_CNT_TO_END(head, tail, sizeof(fpriv->event_buf));
883 copy_size = min(buffer_size, cnt);
885 if (cnt_to_end >= copy_size) {
886 memcpy(event_buf, fpriv->event_buf + tail, copy_size);
888 memcpy(event_buf, fpriv->event_buf + tail, cnt_to_end);
889 memcpy(event_buf + cnt_to_end, fpriv->event_buf,
890 copy_size - cnt_to_end);
893 fpriv->event_tail = (fpriv->event_tail + copy_size) &
894 (sizeof(fpriv->event_buf) - 1);
897 spin_unlock_irqrestore(&fpriv->obj->file_lock, flags);
899 if (copy_to_user(buffer, event_buf, copy_size))
908 ssize_t adf_file_read(struct file *filp, char __user *buffer,
909 size_t count, loff_t *offset)
911 struct adf_file *fpriv = filp->private_data;
914 err = wait_event_interruptible(fpriv->event_wait,
915 adf_file_event_available(fpriv));
919 return adf_file_copy_to_user(fpriv, buffer, count);
922 unsigned int adf_file_poll(struct file *filp, struct poll_table_struct *wait)
924 struct adf_file *fpriv = filp->private_data;
925 unsigned int mask = 0;
927 poll_wait(filp, &fpriv->event_wait, wait);
929 if (adf_file_event_available(fpriv))
930 mask |= POLLIN | POLLRDNORM;
935 const struct file_operations adf_fops = {
936 .owner = THIS_MODULE,
937 .unlocked_ioctl = adf_file_ioctl,
939 .compat_ioctl = adf_file_compat_ioctl,
941 .open = adf_file_open,
942 .release = adf_file_release,
943 .llseek = default_llseek,
944 .read = adf_file_read,
945 .poll = adf_file_poll,