e4a792135072a53d676d7b9b4549eb8f0e7db253
[firefly-linux-kernel-4.4.55.git] / drivers / video / adf / adf_client.c
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
15 #include <linux/kthread.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18
19 #include "sw_sync.h"
20
21 #include <video/adf.h>
22 #include <video/adf_client.h>
23 #include <video/adf_format.h>
24
25 #include "adf.h"
26
27 static inline bool vsync_active(u8 state)
28 {
29         return state == DRM_MODE_DPMS_ON || state == DRM_MODE_DPMS_STANDBY;
30 }
31
32 /**
33  * adf_interface_blank - set interface's DPMS state
34  *
35  * @intf: the interface
36  * @state: one of %DRM_MODE_DPMS_*
37  *
38  * Returns 0 on success or -errno on failure.
39  */
40 int adf_interface_blank(struct adf_interface *intf, u8 state)
41 {
42         struct adf_device *dev = adf_interface_parent(intf);
43         u8 prev_state;
44         bool disable_vsync;
45         bool enable_vsync;
46         int ret = 0;
47         struct adf_event_refcount *vsync_refcount;
48
49         if (!intf->ops || !intf->ops->blank)
50                 return -EOPNOTSUPP;
51
52         mutex_lock(&dev->client_lock);
53         if (state != DRM_MODE_DPMS_ON)
54                 flush_kthread_worker(&dev->post_worker);
55         mutex_lock(&intf->base.event_lock);
56
57         vsync_refcount = adf_obj_find_event_refcount(&intf->base,
58                         ADF_EVENT_VSYNC);
59         if (!vsync_refcount) {
60                 ret = -ENOMEM;
61                 goto done;
62         }
63
64         prev_state = intf->dpms_state;
65         if (prev_state == state) {
66                 ret = -EBUSY;
67                 goto done;
68         }
69
70         disable_vsync = vsync_active(prev_state) &&
71                         !vsync_active(state) &&
72                         vsync_refcount->refcount;
73         enable_vsync = !vsync_active(prev_state) &&
74                         vsync_active(state) &&
75                         vsync_refcount->refcount;
76
77         if (disable_vsync)
78                 intf->base.ops->set_event(&intf->base, ADF_EVENT_VSYNC,
79                                 false);
80
81         ret = intf->ops->blank(intf, state);
82         if (ret < 0) {
83                 if (disable_vsync)
84                         intf->base.ops->set_event(&intf->base, ADF_EVENT_VSYNC,
85                                         true);
86                 goto done;
87         }
88
89         if (enable_vsync)
90                 intf->base.ops->set_event(&intf->base, ADF_EVENT_VSYNC,
91                                 true);
92
93         intf->dpms_state = state;
94 done:
95         mutex_unlock(&intf->base.event_lock);
96         mutex_unlock(&dev->client_lock);
97         return ret;
98 }
99 EXPORT_SYMBOL(adf_interface_blank);
100
101 /**
102  * adf_interface_blank - get interface's current DPMS state
103  *
104  * @intf: the interface
105  *
106  * Returns one of %DRM_MODE_DPMS_*.
107  */
108 u8 adf_interface_dpms_state(struct adf_interface *intf)
109 {
110         struct adf_device *dev = adf_interface_parent(intf);
111         u8 dpms_state;
112
113         mutex_lock(&dev->client_lock);
114         dpms_state = intf->dpms_state;
115         mutex_unlock(&dev->client_lock);
116
117         return dpms_state;
118 }
119 EXPORT_SYMBOL(adf_interface_dpms_state);
120
121 /**
122  * adf_interface_current_mode - get interface's current display mode
123  *
124  * @intf: the interface
125  * @mode: returns the current mode
126  */
127 void adf_interface_current_mode(struct adf_interface *intf,
128                 struct drm_mode_modeinfo *mode)
129 {
130         struct adf_device *dev = adf_interface_parent(intf);
131
132         mutex_lock(&dev->client_lock);
133         memcpy(mode, &intf->current_mode, sizeof(*mode));
134         mutex_unlock(&dev->client_lock);
135 }
136 EXPORT_SYMBOL(adf_interface_current_mode);
137
138 /**
139  * adf_interface_modelist - get interface's modelist
140  *
141  * @intf: the interface
142  * @modelist: storage for the modelist (optional)
143  * @n_modes: length of @modelist
144  *
145  * If @modelist is not NULL, adf_interface_modelist() will copy up to @n_modes
146  * modelist entries into @modelist.
147  *
148  * Returns the length of the modelist.
149  */
150 size_t adf_interface_modelist(struct adf_interface *intf,
151                 struct drm_mode_modeinfo *modelist, size_t n_modes)
152 {
153         unsigned long flags;
154         size_t retval;
155
156         read_lock_irqsave(&intf->hotplug_modelist_lock, flags);
157         if (modelist)
158                 memcpy(modelist, intf->modelist, sizeof(modelist[0]) *
159                                 min(n_modes, intf->n_modes));
160         retval = intf->n_modes;
161         read_unlock_irqrestore(&intf->hotplug_modelist_lock, flags);
162
163         return retval;
164 }
165 EXPORT_SYMBOL(adf_interface_modelist);
166
167 /**
168  * adf_interface_set_mode - set interface's display mode
169  *
170  * @intf: the interface
171  * @mode: the new mode
172  *
173  * Returns 0 on success or -errno on failure.
174  */
175 int adf_interface_set_mode(struct adf_interface *intf,
176                 struct drm_mode_modeinfo *mode)
177 {
178         struct adf_device *dev = adf_interface_parent(intf);
179         int ret = 0;
180
181         if (!intf->ops || !intf->ops->modeset)
182                 return -EOPNOTSUPP;
183
184         mutex_lock(&dev->client_lock);
185         flush_kthread_worker(&dev->post_worker);
186
187         ret = intf->ops->modeset(intf, mode);
188         if (ret < 0)
189                 goto done;
190
191         memcpy(&intf->current_mode, mode, sizeof(*mode));
192 done:
193         mutex_unlock(&dev->client_lock);
194         return ret;
195 }
196 EXPORT_SYMBOL(adf_interface_set_mode);
197
198 /**
199  * adf_interface_screen_size - get size of screen connected to interface
200  *
201  * @intf: the interface
202  * @width_mm: returns the screen width in mm
203  * @height_mm: returns the screen width in mm
204  *
205  * Returns 0 on success or -errno on failure.
206  */
207 int adf_interface_get_screen_size(struct adf_interface *intf, u16 *width_mm,
208                 u16 *height_mm)
209 {
210         struct adf_device *dev = adf_interface_parent(intf);
211         int ret;
212
213         if (!intf->ops || !intf->ops->screen_size)
214                 return -EOPNOTSUPP;
215
216         mutex_lock(&dev->client_lock);
217         ret = intf->ops->screen_size(intf, width_mm, height_mm);
218         mutex_unlock(&dev->client_lock);
219
220         return ret;
221 }
222 EXPORT_SYMBOL(adf_interface_get_screen_size);
223
224 /**
225  * adf_overlay_engine_supports_format - returns whether a format is in an
226  * overlay engine's supported list
227  *
228  * @eng: the overlay engine
229  * @format: format fourcc
230  */
231 bool adf_overlay_engine_supports_format(struct adf_overlay_engine *eng,
232                 u32 format)
233 {
234         size_t i;
235         for (i = 0; i < eng->ops->n_supported_formats; i++)
236                 if (format == eng->ops->supported_formats[i])
237                         return true;
238
239         return false;
240 }
241 EXPORT_SYMBOL(adf_overlay_engine_supports_format);
242
243 static int adf_buffer_validate(struct adf_buffer *buf)
244 {
245         struct adf_overlay_engine *eng = buf->overlay_engine;
246         struct device *dev = &eng->base.dev;
247         struct adf_device *parent = adf_overlay_engine_parent(eng);
248         u8 hsub, vsub, num_planes, cpp[ADF_MAX_PLANES], i;
249
250         if (!adf_overlay_engine_supports_format(eng, buf->format)) {
251                 char format_str[ADF_FORMAT_STR_SIZE];
252                 adf_format_str(buf->format, format_str);
253                 dev_err(dev, "unsupported format %s\n", format_str);
254                 return -EINVAL;
255         }
256
257         if (!adf_format_is_standard(buf->format))
258                 return parent->ops->validate_custom_format(parent, buf);
259
260         hsub = adf_format_horz_chroma_subsampling(buf->format);
261         vsub = adf_format_vert_chroma_subsampling(buf->format);
262         num_planes = adf_format_num_planes(buf->format);
263         for (i = 0; i < num_planes; i++)
264                 cpp[i] = adf_format_plane_cpp(buf->format, i);
265
266         return adf_format_validate_yuv(parent, buf, num_planes, hsub, vsub,
267                         cpp);
268 }
269
270 static int adf_buffer_map(struct adf_device *dev, struct adf_buffer *buf,
271                 struct adf_buffer_mapping *mapping)
272 {
273         int ret = 0;
274         size_t i;
275
276         for (i = 0; i < buf->n_planes; i++) {
277                 struct dma_buf_attachment *attachment;
278                 struct sg_table *sg_table;
279
280                 attachment = dma_buf_attach(buf->dma_bufs[i], dev->dev);
281                 if (IS_ERR(attachment)) {
282                         ret = PTR_ERR(attachment);
283                         dev_err(&dev->base.dev, "attaching plane %u failed: %d\n",
284                                         i, ret);
285                         goto done;
286                 }
287                 mapping->attachments[i] = attachment;
288
289                 sg_table = dma_buf_map_attachment(attachment, DMA_TO_DEVICE);
290                 if (IS_ERR(sg_table)) {
291                         ret = PTR_ERR(sg_table);
292                         dev_err(&dev->base.dev, "mapping plane %u failed: %d",
293                                         i, ret);
294                         goto done;
295                 } else if (!sg_table) {
296                         ret = -ENOMEM;
297                         dev_err(&dev->base.dev, "mapping plane %u failed\n", i);
298                         goto done;
299                 }
300                 mapping->sg_tables[i] = sg_table;
301         }
302
303 done:
304         if (ret < 0)
305                 adf_buffer_mapping_cleanup(mapping, buf);
306
307         return ret;
308 }
309
310 static struct sync_fence *adf_sw_complete_fence(struct adf_device *dev)
311 {
312         struct sync_pt *pt;
313         struct sync_fence *complete_fence;
314
315         if (!dev->timeline) {
316                 dev->timeline = sw_sync_timeline_create(dev->base.name);
317                 if (!dev->timeline)
318                         return ERR_PTR(-ENOMEM);
319                 dev->timeline_max = 1;
320         }
321
322         dev->timeline_max++;
323         pt = sw_sync_pt_create(dev->timeline, dev->timeline_max);
324         if (!pt)
325                 goto err_pt_create;
326         complete_fence = sync_fence_create(dev->base.name, pt);
327         if (!complete_fence)
328                 goto err_fence_create;
329
330         return complete_fence;
331
332 err_fence_create:
333         sync_pt_free(pt);
334 err_pt_create:
335         dev->timeline_max--;
336         return ERR_PTR(-ENOSYS);
337 }
338
339 /**
340  * adf_device_post - flip to a new set of buffers
341  *
342  * @dev: device targeted by the flip
343  * @intfs: interfaces targeted by the flip
344  * @n_intfs: number of targeted interfaces
345  * @bufs: description of buffers displayed
346  * @n_bufs: number of buffers displayed
347  * @custom_data: driver-private data
348  * @custom_data_size: size of driver-private data
349  *
350  * adf_device_post() will copy @intfs, @bufs, and @custom_data, so they may
351  * point to variables on the stack.  adf_device_post() also takes its own
352  * reference on each of the dma-bufs in @bufs.  The adf_device_post_nocopy()
353  * variant transfers ownership of these resources to ADF instead.
354  *
355  * On success, returns a sync fence which signals when the buffers are removed
356  * from the screen.  On failure, returns ERR_PTR(-errno).
357  */
358 struct sync_fence *adf_device_post(struct adf_device *dev,
359                 struct adf_interface **intfs, size_t n_intfs,
360                 struct adf_buffer *bufs, size_t n_bufs, void *custom_data,
361                 size_t custom_data_size)
362 {
363         struct adf_interface **intfs_copy = NULL;
364         struct adf_buffer *bufs_copy = NULL;
365         void *custom_data_copy = NULL;
366         struct sync_fence *ret;
367         size_t i;
368
369         intfs_copy = kzalloc(sizeof(intfs_copy[0]) * n_intfs, GFP_KERNEL);
370         if (!intfs_copy)
371                 return ERR_PTR(-ENOMEM);
372
373         bufs_copy = kzalloc(sizeof(bufs_copy[0]) * n_bufs, GFP_KERNEL);
374         if (!bufs_copy) {
375                 ret = ERR_PTR(-ENOMEM);
376                 goto err_alloc;
377         }
378
379         custom_data_copy = kzalloc(custom_data_size, GFP_KERNEL);
380         if (!custom_data_copy) {
381                 ret = ERR_PTR(-ENOMEM);
382                 goto err_alloc;
383         }
384
385         for (i = 0; i < n_bufs; i++) {
386                 size_t j;
387                 for (j = 0; j < bufs[i].n_planes; j++)
388                         get_dma_buf(bufs[i].dma_bufs[j]);
389         }
390
391         memcpy(intfs_copy, intfs, sizeof(intfs_copy[0]) * n_intfs);
392         memcpy(bufs_copy, bufs, sizeof(bufs_copy[0]) * n_bufs);
393         memcpy(custom_data_copy, custom_data, custom_data_size);
394
395         ret = adf_device_post_nocopy(dev, intfs_copy, n_intfs, bufs_copy,
396                         n_bufs, custom_data_copy, custom_data_size);
397         if (IS_ERR(ret))
398                 goto err_post;
399
400         return ret;
401
402 err_post:
403         for (i = 0; i < n_bufs; i++) {
404                 size_t j;
405                 for (j = 0; j < bufs[i].n_planes; j++)
406                         dma_buf_put(bufs[i].dma_bufs[j]);
407         }
408 err_alloc:
409         kfree(custom_data_copy);
410         kfree(bufs_copy);
411         kfree(intfs_copy);
412         return ret;
413 }
414 EXPORT_SYMBOL(adf_device_post);
415
416 /**
417  * adf_device_post_nocopy - flip to a new set of buffers
418  *
419  * adf_device_post_nocopy() has the same behavior as adf_device_post(),
420  * except ADF does not copy @intfs, @bufs, or @custom_data, and it does
421  * not take an extra reference on the dma-bufs in @bufs.
422  *
423  * @intfs, @bufs, and @custom_data must point to buffers allocated by
424  * kmalloc().  On success, ADF takes ownership of these buffers and the dma-bufs
425  * in @bufs, and will kfree()/dma_buf_put() them when they are no longer needed.
426  * On failure, adf_device_post_nocopy() does NOT take ownership of these
427  * buffers or the dma-bufs, and the caller must clean them up.
428  *
429  * adf_device_post_nocopy() is mainly intended for implementing ADF's ioctls.
430  * Clients may find the nocopy variant useful in limited cases, but most should
431  * call adf_device_post() instead.
432  */
433 struct sync_fence *adf_device_post_nocopy(struct adf_device *dev,
434                 struct adf_interface **intfs, size_t n_intfs,
435                 struct adf_buffer *bufs, size_t n_bufs,
436                 void *custom_data, size_t custom_data_size)
437 {
438         struct adf_pending_post *cfg;
439         struct adf_buffer_mapping *mappings;
440         struct sync_fence *ret;
441         size_t i;
442         int err;
443
444         cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
445         if (!cfg)
446                 return ERR_PTR(-ENOMEM);
447
448         mappings = kzalloc(sizeof(mappings[0]) * n_bufs, GFP_KERNEL);
449         if (!mappings) {
450                 ret = ERR_PTR(-ENOMEM);
451                 goto err_alloc;
452         }
453
454         mutex_lock(&dev->client_lock);
455
456         for (i = 0; i < n_bufs; i++) {
457                 err = adf_buffer_validate(&bufs[i]);
458                 if (err < 0) {
459                         ret = ERR_PTR(err);
460                         goto err_buf;
461                 }
462
463                 err = adf_buffer_map(dev, &bufs[i], &mappings[i]);
464                 if (err < 0) {
465                         ret = ERR_PTR(err);
466                         goto err_buf;
467                 }
468         }
469
470         INIT_LIST_HEAD(&cfg->head);
471         cfg->config.n_bufs = n_bufs;
472         cfg->config.bufs = bufs;
473         cfg->config.mappings = mappings;
474         cfg->config.custom_data = custom_data;
475         cfg->config.custom_data_size = custom_data_size;
476
477         err = dev->ops->validate(dev, &cfg->config, &cfg->state);
478         if (err < 0) {
479                 ret = ERR_PTR(err);
480                 goto err_buf;
481         }
482
483         mutex_lock(&dev->post_lock);
484
485         if (dev->ops->complete_fence)
486                 ret = dev->ops->complete_fence(dev, &cfg->config,
487                                 cfg->state);
488         else
489                 ret = adf_sw_complete_fence(dev);
490
491         if (IS_ERR(ret))
492                 goto err_fence;
493
494         list_add_tail(&cfg->head, &dev->post_list);
495         queue_kthread_work(&dev->post_worker, &dev->post_work);
496         mutex_unlock(&dev->post_lock);
497         mutex_unlock(&dev->client_lock);
498         kfree(intfs);
499         return ret;
500
501 err_fence:
502         mutex_unlock(&dev->post_lock);
503
504 err_buf:
505         for (i = 0; i < n_bufs; i++)
506                 adf_buffer_mapping_cleanup(&mappings[i], &bufs[i]);
507
508         mutex_unlock(&dev->client_lock);
509         kfree(mappings);
510
511 err_alloc:
512         kfree(cfg);
513         return ret;
514 }
515 EXPORT_SYMBOL(adf_device_post_nocopy);
516
517 static void adf_attachment_list_to_array(struct adf_device *dev,
518                 struct list_head *src, struct adf_attachment *dst, size_t size)
519 {
520         struct adf_attachment_list *entry;
521         size_t i = 0;
522
523         if (!dst)
524                 return;
525
526         list_for_each_entry(entry, src, head) {
527                 if (i == size)
528                         return;
529                 dst[i] = entry->attachment;
530                 i++;
531         }
532 }
533
534 /**
535  * adf_device_attachments - get device's list of active attachments
536  *
537  * @dev: the device
538  * @attachments: storage for the attachment list (optional)
539  * @n_attachments: length of @attachments
540  *
541  * If @attachments is not NULL, adf_device_attachments() will copy up to
542  * @n_attachments entries into @attachments.
543  *
544  * Returns the length of the active attachment list.
545  */
546 size_t adf_device_attachments(struct adf_device *dev,
547                 struct adf_attachment *attachments, size_t n_attachments)
548 {
549         size_t retval;
550
551         mutex_lock(&dev->client_lock);
552         adf_attachment_list_to_array(dev, &dev->attached, attachments,
553                         n_attachments);
554         retval = dev->n_attached;
555         mutex_unlock(&dev->client_lock);
556
557         return retval;
558 }
559 EXPORT_SYMBOL(adf_device_attachments);
560
561 /**
562  * adf_device_attachments_allowed - get device's list of allowed attachments
563  *
564  * @dev: the device
565  * @attachments: storage for the attachment list (optional)
566  * @n_attachments: length of @attachments
567  *
568  * If @attachments is not NULL, adf_device_attachments_allowed() will copy up to
569  * @n_attachments entries into @attachments.
570  *
571  * Returns the length of the allowed attachment list.
572  */
573 size_t adf_device_attachments_allowed(struct adf_device *dev,
574                 struct adf_attachment *attachments, size_t n_attachments)
575 {
576         size_t retval;
577
578         mutex_lock(&dev->client_lock);
579         adf_attachment_list_to_array(dev, &dev->attach_allowed, attachments,
580                         n_attachments);
581         retval = dev->n_attach_allowed;
582         mutex_unlock(&dev->client_lock);
583
584         return retval;
585 }
586 EXPORT_SYMBOL(adf_device_attachments_allowed);
587
588 /**
589  * adf_device_attached - return whether an overlay engine and interface are
590  * attached
591  *
592  * @dev: the parent device
593  * @eng: the overlay engine
594  * @intf: the interface
595  */
596 bool adf_device_attached(struct adf_device *dev, struct adf_overlay_engine *eng,
597                 struct adf_interface *intf)
598 {
599         struct adf_attachment_list *attachment;
600
601         mutex_lock(&dev->client_lock);
602         attachment = adf_attachment_find(&dev->attached, eng, intf);
603         mutex_unlock(&dev->client_lock);
604
605         return attachment != NULL;
606 }
607 EXPORT_SYMBOL(adf_device_attached);
608
609 /**
610  * adf_device_attach_allowed - return whether the ADF device supports attaching
611  * an overlay engine and interface
612  *
613  * @dev: the parent device
614  * @eng: the overlay engine
615  * @intf: the interface
616  */
617 bool adf_device_attach_allowed(struct adf_device *dev,
618                 struct adf_overlay_engine *eng, struct adf_interface *intf)
619 {
620         struct adf_attachment_list *attachment;
621
622         mutex_lock(&dev->client_lock);
623         attachment = adf_attachment_find(&dev->attach_allowed, eng, intf);
624         mutex_unlock(&dev->client_lock);
625
626         return attachment != NULL;
627 }
628 EXPORT_SYMBOL(adf_device_attach_allowed);
629 /**
630  * adf_device_attach - attach an overlay engine to an interface
631  *
632  * @dev: the parent device
633  * @eng: the overlay engine
634  * @intf: the interface
635  *
636  * Returns 0 on success, -%EINVAL if attaching @intf and @eng is not allowed,
637  * -%EALREADY if @intf and @eng are already attached, or -errno on any other
638  * failure.
639  */
640 int adf_device_attach(struct adf_device *dev, struct adf_overlay_engine *eng,
641                 struct adf_interface *intf)
642 {
643         int ret;
644         struct adf_attachment_list *attachment = NULL;
645
646         ret = adf_attachment_validate(dev, eng, intf);
647         if (ret < 0)
648                 return ret;
649
650         mutex_lock(&dev->client_lock);
651
652         if (dev->n_attached == ADF_MAX_ATTACHMENTS) {
653                 ret = -ENOMEM;
654                 goto done;
655         }
656
657         if (!adf_attachment_find(&dev->attach_allowed, eng, intf)) {
658                 ret = -EINVAL;
659                 goto done;
660         }
661
662         if (adf_attachment_find(&dev->attached, eng, intf)) {
663                 ret = -EALREADY;
664                 goto done;
665         }
666
667         ret = adf_device_attach_op(dev, eng, intf);
668         if (ret < 0)
669                 goto done;
670
671         attachment = kzalloc(sizeof(*attachment), GFP_KERNEL);
672         if (!attachment) {
673                 ret = -ENOMEM;
674                 goto done;
675         }
676
677         attachment->attachment.interface = intf;
678         attachment->attachment.overlay_engine = eng;
679         list_add_tail(&attachment->head, &dev->attached);
680         dev->n_attached++;
681
682 done:
683         mutex_unlock(&dev->client_lock);
684         if (ret < 0)
685                 kfree(attachment);
686
687         return ret;
688 }
689 EXPORT_SYMBOL(adf_device_attach);
690
691 /**
692  * adf_device_detach - detach an overlay engine from an interface
693  *
694  * @dev: the parent device
695  * @eng: the overlay engine
696  * @intf: the interface
697  *
698  * Returns 0 on success, -%EINVAL if @intf and @eng are not attached,
699  * or -errno on any other failure.
700  */
701 int adf_device_detach(struct adf_device *dev, struct adf_overlay_engine *eng,
702                 struct adf_interface *intf)
703 {
704         int ret;
705         struct adf_attachment_list *attachment;
706
707         ret = adf_attachment_validate(dev, eng, intf);
708         if (ret < 0)
709                 return ret;
710
711         mutex_lock(&dev->client_lock);
712
713         attachment = adf_attachment_find(&dev->attached, eng, intf);
714         if (!attachment) {
715                 ret = -EINVAL;
716                 goto done;
717         }
718
719         ret = adf_device_detach_op(dev, eng, intf);
720         if (ret < 0)
721                 goto done;
722
723         adf_attachment_free(attachment);
724         dev->n_attached--;
725 done:
726         mutex_unlock(&dev->client_lock);
727         return ret;
728 }
729 EXPORT_SYMBOL(adf_device_detach);
730
731 /**
732  * adf_interface_simple_buffer_alloc - allocate a simple buffer
733  *
734  * @intf: target interface
735  * @w: width in pixels
736  * @h: height in pixels
737  * @format: format fourcc
738  * @dma_buf: returns the allocated buffer
739  * @offset: returns the byte offset of the allocated buffer's first pixel
740  * @pitch: returns the allocated buffer's pitch
741  *
742  * See &struct adf_simple_buffer_alloc for a description of simple buffers and
743  * their limitations.
744  *
745  * Returns 0 on success or -errno on failure.
746  */
747 int adf_interface_simple_buffer_alloc(struct adf_interface *intf, u16 w, u16 h,
748                 u32 format, struct dma_buf **dma_buf, u32 *offset, u32 *pitch)
749 {
750         if (!intf->ops || !intf->ops->alloc_simple_buffer)
751                 return -EOPNOTSUPP;
752
753         if (!adf_format_is_rgb(format))
754                 return -EINVAL;
755
756         return intf->ops->alloc_simple_buffer(intf, w, h, format, dma_buf,
757                         offset, pitch);
758 }
759 EXPORT_SYMBOL(adf_interface_simple_buffer_alloc);
760
761 /**
762  * adf_interface_simple_post - flip to a single buffer
763  *
764  * @intf: interface targeted by the flip
765  * @buf: buffer to display
766  *
767  * adf_interface_simple_post() can be used generically for simple display
768  * configurations, since the client does not need to provide any driver-private
769  * configuration data.
770  *
771  * adf_interface_simple_post() has the same copying semantics as
772  * adf_device_post().
773  *
774  * On success, returns a sync fence which signals when the buffer is removed
775  * from the screen.  On failure, returns ERR_PTR(-errno).
776  */
777 struct sync_fence *adf_interface_simple_post(struct adf_interface *intf,
778                 struct adf_buffer *buf)
779 {
780         size_t custom_data_size = 0;
781         void *custom_data = NULL;
782         struct sync_fence *ret;
783
784         if (intf->ops && intf->ops->describe_simple_post) {
785                 int err;
786
787                 custom_data = kzalloc(ADF_MAX_CUSTOM_DATA_SIZE, GFP_KERNEL);
788                 if (!custom_data) {
789                         ret = ERR_PTR(-ENOMEM);
790                         goto done;
791                 }
792
793                 err = intf->ops->describe_simple_post(intf, buf, custom_data,
794                                 &custom_data_size);
795                 if (err < 0) {
796                         ret = ERR_PTR(err);
797                         goto done;
798                 }
799         }
800
801         ret = adf_device_post(adf_interface_parent(intf), &intf, 1, buf, 1,
802                         custom_data, custom_data_size);
803 done:
804         kfree(custom_data);
805         return ret;
806 }
807 EXPORT_SYMBOL(adf_interface_simple_post);