udl_fb.c 16 KB

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