1 /*
2  * Copyright (C) 2012 Red Hat
3  *
4  * based in parts on udlfb.c:
5  * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
6  * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
7  * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License v2. See the file COPYING in the main directory of this archive for
11  * more details.
12  */
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/fb.h>
16 
17 #include "drmP.h"
18 #include "drm.h"
19 #include "drm_crtc.h"
20 #include "drm_crtc_helper.h"
21 #include "udl_drv.h"
22 
23 #include "drm_fb_helper.h"
24 
25 #define DL_DEFIO_WRITE_DELAY    (HZ/20) /* fb_deferred_io.delay in jiffies */
26 
27 static int fb_defio = 0;  /* Optionally enable experimental fb_defio mmap support */
28 static int fb_bpp = 16;
29 
30 module_param(fb_bpp, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
31 module_param(fb_defio, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
32 
33 struct udl_fbdev {
34 	struct drm_fb_helper helper;
35 	struct udl_framebuffer ufb;
36 	struct list_head fbdev_list;
37 	int fb_count;
38 };
39 
40 #define DL_ALIGN_UP(x, a) ALIGN(x, a)
41 #define DL_ALIGN_DOWN(x, a) ALIGN(x-(a-1), a)
42 
43 /** Read the red component (0..255) of a 32 bpp colour. */
44 #define DLO_RGB_GETRED(col) (uint8_t)((col) & 0xFF)
45 
46 /** Read the green component (0..255) of a 32 bpp colour. */
47 #define DLO_RGB_GETGRN(col) (uint8_t)(((col) >> 8) & 0xFF)
48 
49 /** Read the blue component (0..255) of a 32 bpp colour. */
50 #define DLO_RGB_GETBLU(col) (uint8_t)(((col) >> 16) & 0xFF)
51 
52 /** Return red/green component of a 16 bpp colour number. */
53 #define DLO_RG16(red, grn) (uint8_t)((((red) & 0xF8) | ((grn) >> 5)) & 0xFF)
54 
55 /** Return green/blue component of a 16 bpp colour number. */
56 #define DLO_GB16(grn, blu) (uint8_t)(((((grn) & 0x1C) << 3) | ((blu) >> 3)) & 0xFF)
57 
58 /** Return 8 bpp colour number from red, green and blue components. */
59 #define DLO_RGB8(red, grn, blu) ((((red) << 5) | (((grn) & 3) << 3) | ((blu) & 7)) & 0xFF)
60 
61 #if 0
62 static uint8_t rgb8(uint32_t col)
63 {
64 	uint8_t red = DLO_RGB_GETRED(col);
65 	uint8_t grn = DLO_RGB_GETGRN(col);
66 	uint8_t blu = DLO_RGB_GETBLU(col);
67 
68 	return DLO_RGB8(red, grn, blu);
69 }
70 
71 static uint16_t rgb16(uint32_t col)
72 {
73 	uint8_t red = DLO_RGB_GETRED(col);
74 	uint8_t grn = DLO_RGB_GETGRN(col);
75 	uint8_t blu = DLO_RGB_GETBLU(col);
76 
77 	return (DLO_RG16(red, grn) << 8) + DLO_GB16(grn, blu);
78 }
79 #endif
80 
81 /*
82  * NOTE: fb_defio.c is holding info->fbdefio.mutex
83  *   Touching ANY framebuffer memory that triggers a page fault
84  *   in fb_defio will cause a deadlock, when it also tries to
85  *   grab the same mutex.
86  */
udlfb_dpy_deferred_io(struct fb_info * info,struct list_head * pagelist)87 static void udlfb_dpy_deferred_io(struct fb_info *info,
88 				  struct list_head *pagelist)
89 {
90 	struct page *cur;
91 	struct fb_deferred_io *fbdefio = info->fbdefio;
92 	struct udl_fbdev *ufbdev = info->par;
93 	struct drm_device *dev = ufbdev->ufb.base.dev;
94 	struct udl_device *udl = dev->dev_private;
95 	struct urb *urb;
96 	char *cmd;
97 	cycles_t start_cycles, end_cycles;
98 	int bytes_sent = 0;
99 	int bytes_identical = 0;
100 	int bytes_rendered = 0;
101 
102 	if (!fb_defio)
103 		return;
104 
105 	start_cycles = get_cycles();
106 
107 	urb = udl_get_urb(dev);
108 	if (!urb)
109 		return;
110 
111 	cmd = urb->transfer_buffer;
112 
113 	/* walk the written page list and render each to device */
114 	list_for_each_entry(cur, &fbdefio->pagelist, lru) {
115 
116 		if (udl_render_hline(dev, (ufbdev->ufb.base.bits_per_pixel / 8),
117 				     &urb, (char *) info->fix.smem_start,
118 				     &cmd, cur->index << PAGE_SHIFT,
119 				     cur->index << PAGE_SHIFT,
120 				     PAGE_SIZE, &bytes_identical, &bytes_sent))
121 			goto error;
122 		bytes_rendered += PAGE_SIZE;
123 	}
124 
125 	if (cmd > (char *) urb->transfer_buffer) {
126 		/* Send partial buffer remaining before exiting */
127 		int len = cmd - (char *) urb->transfer_buffer;
128 		udl_submit_urb(dev, urb, len);
129 		bytes_sent += len;
130 	} else
131 		udl_urb_completion(urb);
132 
133 error:
134 	atomic_add(bytes_sent, &udl->bytes_sent);
135 	atomic_add(bytes_identical, &udl->bytes_identical);
136 	atomic_add(bytes_rendered, &udl->bytes_rendered);
137 	end_cycles = get_cycles();
138 	atomic_add(((unsigned int) ((end_cycles - start_cycles)
139 		    >> 10)), /* Kcycles */
140 		   &udl->cpu_kcycles_used);
141 }
142 
udl_handle_damage(struct udl_framebuffer * fb,int x,int y,int width,int height)143 int udl_handle_damage(struct udl_framebuffer *fb, int x, int y,
144 		      int width, int height)
145 {
146 	struct drm_device *dev = fb->base.dev;
147 	struct udl_device *udl = dev->dev_private;
148 	int i, ret;
149 	char *cmd;
150 	cycles_t start_cycles, end_cycles;
151 	int bytes_sent = 0;
152 	int bytes_identical = 0;
153 	struct urb *urb;
154 	int aligned_x;
155 	int bpp = (fb->base.bits_per_pixel / 8);
156 	int x2, y2;
157 	bool store_for_later = false;
158 	unsigned long flags;
159 
160 	if (!fb->active_16)
161 		return 0;
162 
163 	if (!fb->obj->vmapping)
164 		udl_gem_vmap(fb->obj);
165 
166 	aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
167 	width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
168 	x = aligned_x;
169 
170 	if ((width <= 0) ||
171 	    (x + width > fb->base.width) ||
172 	    (y + height > fb->base.height))
173 		return -EINVAL;
174 
175 	/* if we are in atomic just store the info
176 	   can't test inside spin lock */
177 	if (in_atomic())
178 		store_for_later = true;
179 
180 	x2 = x + width - 1;
181 	y2 = y + height - 1;
182 
183 	spin_lock_irqsave(&fb->dirty_lock, flags);
184 
185 	if (fb->y1 < y)
186 		y = fb->y1;
187 	if (fb->y2 > y2)
188 		y2 = fb->y2;
189 	if (fb->x1 < x)
190 		x = fb->x1;
191 	if (fb->x2 > x2)
192 		x2 = fb->x2;
193 
194 	if (store_for_later) {
195 		fb->x1 = x;
196 		fb->x2 = x2;
197 		fb->y1 = y;
198 		fb->y2 = y2;
199 		spin_unlock_irqrestore(&fb->dirty_lock, flags);
200 		return 0;
201 	}
202 
203 	fb->x1 = fb->y1 = INT_MAX;
204 	fb->x2 = fb->y2 = 0;
205 
206 	spin_unlock_irqrestore(&fb->dirty_lock, flags);
207 	start_cycles = get_cycles();
208 
209 	urb = udl_get_urb(dev);
210 	if (!urb)
211 		return 0;
212 	cmd = urb->transfer_buffer;
213 
214 	for (i = y; i <= y2 ; i++) {
215 		const int line_offset = fb->base.pitches[0] * i;
216 		const int byte_offset = line_offset + (x * bpp);
217 		const int dev_byte_offset = (fb->base.width * bpp * i) + (x * bpp);
218 		if (udl_render_hline(dev, bpp, &urb,
219 				     (char *) fb->obj->vmapping,
220 				     &cmd, byte_offset, dev_byte_offset,
221 				     (x2 - x + 1) * bpp,
222 				     &bytes_identical, &bytes_sent))
223 			goto error;
224 	}
225 
226 	if (cmd > (char *) urb->transfer_buffer) {
227 		/* Send partial buffer remaining before exiting */
228 		int len = cmd - (char *) urb->transfer_buffer;
229 		ret = udl_submit_urb(dev, urb, len);
230 		bytes_sent += len;
231 	} else
232 		udl_urb_completion(urb);
233 
234 error:
235 	atomic_add(bytes_sent, &udl->bytes_sent);
236 	atomic_add(bytes_identical, &udl->bytes_identical);
237 	atomic_add(width*height*bpp, &udl->bytes_rendered);
238 	end_cycles = get_cycles();
239 	atomic_add(((unsigned int) ((end_cycles - start_cycles)
240 		    >> 10)), /* Kcycles */
241 		   &udl->cpu_kcycles_used);
242 
243 	return 0;
244 }
245 
udl_fb_mmap(struct fb_info * info,struct vm_area_struct * vma)246 static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
247 {
248 	unsigned long start = vma->vm_start;
249 	unsigned long size = vma->vm_end - vma->vm_start;
250 	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
251 	unsigned long page, pos;
252 
253 	if (offset + size > info->fix.smem_len)
254 		return -EINVAL;
255 
256 	pos = (unsigned long)info->fix.smem_start + offset;
257 
258 	pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
259 		  pos, size);
260 
261 	while (size > 0) {
262 		page = vmalloc_to_pfn((void *)pos);
263 		if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
264 			return -EAGAIN;
265 
266 		start += PAGE_SIZE;
267 		pos += PAGE_SIZE;
268 		if (size > PAGE_SIZE)
269 			size -= PAGE_SIZE;
270 		else
271 			size = 0;
272 	}
273 
274 	vma->vm_flags |= VM_RESERVED;	/* avoid to swap out this VMA */
275 	return 0;
276 }
277 
udl_fb_fillrect(struct fb_info * info,const struct fb_fillrect * rect)278 static void udl_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
279 {
280 	struct udl_fbdev *ufbdev = info->par;
281 
282 	sys_fillrect(info, rect);
283 
284 	udl_handle_damage(&ufbdev->ufb, rect->dx, rect->dy, rect->width,
285 			  rect->height);
286 }
287 
udl_fb_copyarea(struct fb_info * info,const struct fb_copyarea * region)288 static void udl_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
289 {
290 	struct udl_fbdev *ufbdev = info->par;
291 
292 	sys_copyarea(info, region);
293 
294 	udl_handle_damage(&ufbdev->ufb, region->dx, region->dy, region->width,
295 			  region->height);
296 }
297 
udl_fb_imageblit(struct fb_info * info,const struct fb_image * image)298 static void udl_fb_imageblit(struct fb_info *info, const struct fb_image *image)
299 {
300 	struct udl_fbdev *ufbdev = info->par;
301 
302 	sys_imageblit(info, image);
303 
304 	udl_handle_damage(&ufbdev->ufb, image->dx, image->dy, image->width,
305 			  image->height);
306 }
307 
308 /*
309  * It's common for several clients to have framebuffer open simultaneously.
310  * e.g. both fbcon and X. Makes things interesting.
311  * Assumes caller is holding info->lock (for open and release at least)
312  */
udl_fb_open(struct fb_info * info,int user)313 static int udl_fb_open(struct fb_info *info, int user)
314 {
315 	struct udl_fbdev *ufbdev = info->par;
316 	struct drm_device *dev = ufbdev->ufb.base.dev;
317 	struct udl_device *udl = dev->dev_private;
318 
319 	/* If the USB device is gone, we don't accept new opens */
320 	if (drm_device_is_unplugged(udl->ddev))
321 		return -ENODEV;
322 
323 	ufbdev->fb_count++;
324 
325 	if (fb_defio && (info->fbdefio == NULL)) {
326 		/* enable defio at last moment if not disabled by client */
327 
328 		struct fb_deferred_io *fbdefio;
329 
330 		fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
331 
332 		if (fbdefio) {
333 			fbdefio->delay = DL_DEFIO_WRITE_DELAY;
334 			fbdefio->deferred_io = udlfb_dpy_deferred_io;
335 		}
336 
337 		info->fbdefio = fbdefio;
338 		fb_deferred_io_init(info);
339 	}
340 
341 	pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
342 		  info->node, user, info, ufbdev->fb_count);
343 
344 	return 0;
345 }
346 
347 
348 /*
349  * Assumes caller is holding info->lock mutex (for open and release at least)
350  */
udl_fb_release(struct fb_info * info,int user)351 static int udl_fb_release(struct fb_info *info, int user)
352 {
353 	struct udl_fbdev *ufbdev = info->par;
354 
355 	ufbdev->fb_count--;
356 
357 	if ((ufbdev->fb_count == 0) && (info->fbdefio)) {
358 		fb_deferred_io_cleanup(info);
359 		kfree(info->fbdefio);
360 		info->fbdefio = NULL;
361 		info->fbops->fb_mmap = udl_fb_mmap;
362 	}
363 
364 	pr_warn("released /dev/fb%d user=%d count=%d\n",
365 		info->node, user, ufbdev->fb_count);
366 
367 	return 0;
368 }
369 
370 static struct fb_ops udlfb_ops = {
371 	.owner = THIS_MODULE,
372 	.fb_check_var = drm_fb_helper_check_var,
373 	.fb_set_par = drm_fb_helper_set_par,
374 	.fb_fillrect = udl_fb_fillrect,
375 	.fb_copyarea = udl_fb_copyarea,
376 	.fb_imageblit = udl_fb_imageblit,
377 	.fb_pan_display = drm_fb_helper_pan_display,
378 	.fb_blank = drm_fb_helper_blank,
379 	.fb_setcmap = drm_fb_helper_setcmap,
380 	.fb_debug_enter = drm_fb_helper_debug_enter,
381 	.fb_debug_leave = drm_fb_helper_debug_leave,
382 	.fb_mmap = udl_fb_mmap,
383 	.fb_open = udl_fb_open,
384 	.fb_release = udl_fb_release,
385 };
386 
udl_crtc_fb_gamma_set(struct drm_crtc * crtc,u16 red,u16 green,u16 blue,int regno)387 void udl_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
388 			   u16 blue, int regno)
389 {
390 }
391 
udl_crtc_fb_gamma_get(struct drm_crtc * crtc,u16 * red,u16 * green,u16 * blue,int regno)392 void udl_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
393 			     u16 *blue, int regno)
394 {
395 	*red = 0;
396 	*green = 0;
397 	*blue = 0;
398 }
399 
udl_user_framebuffer_dirty(struct drm_framebuffer * fb,struct drm_file * file,unsigned flags,unsigned color,struct drm_clip_rect * clips,unsigned num_clips)400 static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
401 				      struct drm_file *file,
402 				      unsigned flags, unsigned color,
403 				      struct drm_clip_rect *clips,
404 				      unsigned num_clips)
405 {
406 	struct udl_framebuffer *ufb = to_udl_fb(fb);
407 	int i;
408 
409 	if (!ufb->active_16)
410 		return 0;
411 
412 	for (i = 0; i < num_clips; i++) {
413 		udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
414 				  clips[i].x2 - clips[i].x1,
415 				  clips[i].y2 - clips[i].y1);
416 	}
417 	return 0;
418 }
419 
udl_user_framebuffer_destroy(struct drm_framebuffer * fb)420 static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
421 {
422 	struct udl_framebuffer *ufb = to_udl_fb(fb);
423 
424 	if (ufb->obj)
425 		drm_gem_object_unreference_unlocked(&ufb->obj->base);
426 
427 	drm_framebuffer_cleanup(fb);
428 	kfree(ufb);
429 }
430 
431 static const struct drm_framebuffer_funcs udlfb_funcs = {
432 	.destroy = udl_user_framebuffer_destroy,
433 	.dirty = udl_user_framebuffer_dirty,
434 	.create_handle = NULL,
435 };
436 
437 
438 static int
udl_framebuffer_init(struct drm_device * dev,struct udl_framebuffer * ufb,struct drm_mode_fb_cmd2 * mode_cmd,struct udl_gem_object * obj)439 udl_framebuffer_init(struct drm_device *dev,
440 		     struct udl_framebuffer *ufb,
441 		     struct drm_mode_fb_cmd2 *mode_cmd,
442 		     struct udl_gem_object *obj)
443 {
444 	int ret;
445 
446 	spin_lock_init(&ufb->dirty_lock);
447 	ufb->obj = obj;
448 	ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
449 	drm_helper_mode_fill_fb_struct(&ufb->base, mode_cmd);
450 	return ret;
451 }
452 
453 
udlfb_create(struct udl_fbdev * ufbdev,struct drm_fb_helper_surface_size * sizes)454 static int udlfb_create(struct udl_fbdev *ufbdev,
455 			struct drm_fb_helper_surface_size *sizes)
456 {
457 	struct drm_device *dev = ufbdev->helper.dev;
458 	struct fb_info *info;
459 	struct device *device = &dev->usbdev->dev;
460 	struct drm_framebuffer *fb;
461 	struct drm_mode_fb_cmd2 mode_cmd;
462 	struct udl_gem_object *obj;
463 	uint32_t size;
464 	int ret = 0;
465 
466 	if (sizes->surface_bpp == 24)
467 		sizes->surface_bpp = 32;
468 
469 	mode_cmd.width = sizes->surface_width;
470 	mode_cmd.height = sizes->surface_height;
471 	mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
472 
473 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
474 							  sizes->surface_depth);
475 
476 	size = mode_cmd.pitches[0] * mode_cmd.height;
477 	size = ALIGN(size, PAGE_SIZE);
478 
479 	obj = udl_gem_alloc_object(dev, size);
480 	if (!obj)
481 		goto out;
482 
483 	ret = udl_gem_vmap(obj);
484 	if (ret) {
485 		DRM_ERROR("failed to vmap fb\n");
486 		goto out_gfree;
487 	}
488 
489 	info = framebuffer_alloc(0, device);
490 	if (!info) {
491 		ret = -ENOMEM;
492 		goto out_gfree;
493 	}
494 	info->par = ufbdev;
495 
496 	ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
497 	if (ret)
498 		goto out_gfree;
499 
500 	fb = &ufbdev->ufb.base;
501 
502 	ufbdev->helper.fb = fb;
503 	ufbdev->helper.fbdev = info;
504 
505 	strcpy(info->fix.id, "udldrmfb");
506 
507 	info->screen_base = ufbdev->ufb.obj->vmapping;
508 	info->fix.smem_len = size;
509 	info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
510 
511 	info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
512 	info->fbops = &udlfb_ops;
513 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
514 	drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
515 
516 	ret = fb_alloc_cmap(&info->cmap, 256, 0);
517 	if (ret) {
518 		ret = -ENOMEM;
519 		goto out_gfree;
520 	}
521 
522 
523 	DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
524 		      fb->width, fb->height,
525 		      ufbdev->ufb.obj->vmapping);
526 
527 	return ret;
528 out_gfree:
529 	drm_gem_object_unreference(&ufbdev->ufb.obj->base);
530 out:
531 	return ret;
532 }
533 
udl_fb_find_or_create_single(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)534 static int udl_fb_find_or_create_single(struct drm_fb_helper *helper,
535 					struct drm_fb_helper_surface_size *sizes)
536 {
537 	struct udl_fbdev *ufbdev = (struct udl_fbdev *)helper;
538 	int new_fb = 0;
539 	int ret;
540 
541 	if (!helper->fb) {
542 		ret = udlfb_create(ufbdev, sizes);
543 		if (ret)
544 			return ret;
545 
546 		new_fb = 1;
547 	}
548 	return new_fb;
549 }
550 
551 static struct drm_fb_helper_funcs udl_fb_helper_funcs = {
552 	.gamma_set = udl_crtc_fb_gamma_set,
553 	.gamma_get = udl_crtc_fb_gamma_get,
554 	.fb_probe = udl_fb_find_or_create_single,
555 };
556 
udl_fbdev_destroy(struct drm_device * dev,struct udl_fbdev * ufbdev)557 static void udl_fbdev_destroy(struct drm_device *dev,
558 			      struct udl_fbdev *ufbdev)
559 {
560 	struct fb_info *info;
561 	if (ufbdev->helper.fbdev) {
562 		info = ufbdev->helper.fbdev;
563 		unregister_framebuffer(info);
564 		if (info->cmap.len)
565 			fb_dealloc_cmap(&info->cmap);
566 		framebuffer_release(info);
567 	}
568 	drm_fb_helper_fini(&ufbdev->helper);
569 	drm_framebuffer_cleanup(&ufbdev->ufb.base);
570 	drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base);
571 }
572 
udl_fbdev_init(struct drm_device * dev)573 int udl_fbdev_init(struct drm_device *dev)
574 {
575 	struct udl_device *udl = dev->dev_private;
576 	int bpp_sel = fb_bpp;
577 	struct udl_fbdev *ufbdev;
578 	int ret;
579 
580 	ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
581 	if (!ufbdev)
582 		return -ENOMEM;
583 
584 	udl->fbdev = ufbdev;
585 	ufbdev->helper.funcs = &udl_fb_helper_funcs;
586 
587 	ret = drm_fb_helper_init(dev, &ufbdev->helper,
588 				 1, 1);
589 	if (ret) {
590 		kfree(ufbdev);
591 		return ret;
592 
593 	}
594 
595 	drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
596 	drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
597 	return 0;
598 }
599 
udl_fbdev_cleanup(struct drm_device * dev)600 void udl_fbdev_cleanup(struct drm_device *dev)
601 {
602 	struct udl_device *udl = dev->dev_private;
603 	if (!udl->fbdev)
604 		return;
605 
606 	udl_fbdev_destroy(dev, udl->fbdev);
607 	kfree(udl->fbdev);
608 	udl->fbdev = NULL;
609 }
610 
udl_fbdev_unplug(struct drm_device * dev)611 void udl_fbdev_unplug(struct drm_device *dev)
612 {
613 	struct udl_device *udl = dev->dev_private;
614 	struct udl_fbdev *ufbdev;
615 	if (!udl->fbdev)
616 		return;
617 
618 	ufbdev = udl->fbdev;
619 	if (ufbdev->helper.fbdev) {
620 		struct fb_info *info;
621 		info = ufbdev->helper.fbdev;
622 		unlink_framebuffer(info);
623 	}
624 }
625 
626 struct drm_framebuffer *
udl_fb_user_fb_create(struct drm_device * dev,struct drm_file * file,struct drm_mode_fb_cmd2 * mode_cmd)627 udl_fb_user_fb_create(struct drm_device *dev,
628 		   struct drm_file *file,
629 		   struct drm_mode_fb_cmd2 *mode_cmd)
630 {
631 	struct drm_gem_object *obj;
632 	struct udl_framebuffer *ufb;
633 	int ret;
634 
635 	obj = drm_gem_object_lookup(dev, file, mode_cmd->handles[0]);
636 	if (obj == NULL)
637 		return ERR_PTR(-ENOENT);
638 
639 	ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
640 	if (ufb == NULL)
641 		return ERR_PTR(-ENOMEM);
642 
643 	ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
644 	if (ret) {
645 		kfree(ufb);
646 		return ERR_PTR(-EINVAL);
647 	}
648 	return &ufb->base;
649 }
650