udl_fb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 "drmP.h"
  17. #include "drm.h"
  18. #include "drm_crtc.h"
  19. #include "drm_crtc_helper.h"
  20. #include "udl_drv.h"
  21. #include "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. udl_gem_vmap(fb->obj);
  136. aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
  137. width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
  138. x = aligned_x;
  139. if ((width <= 0) ||
  140. (x + width > fb->base.width) ||
  141. (y + height > fb->base.height))
  142. return -EINVAL;
  143. /* if we are in atomic just store the info
  144. can't test inside spin lock */
  145. if (in_atomic())
  146. store_for_later = true;
  147. x2 = x + width - 1;
  148. y2 = y + height - 1;
  149. spin_lock_irqsave(&fb->dirty_lock, flags);
  150. if (fb->y1 < y)
  151. y = fb->y1;
  152. if (fb->y2 > y2)
  153. y2 = fb->y2;
  154. if (fb->x1 < x)
  155. x = fb->x1;
  156. if (fb->x2 > x2)
  157. x2 = fb->x2;
  158. if (store_for_later) {
  159. fb->x1 = x;
  160. fb->x2 = x2;
  161. fb->y1 = y;
  162. fb->y2 = y2;
  163. spin_unlock_irqrestore(&fb->dirty_lock, flags);
  164. return 0;
  165. }
  166. fb->x1 = fb->y1 = INT_MAX;
  167. fb->x2 = fb->y2 = 0;
  168. spin_unlock_irqrestore(&fb->dirty_lock, flags);
  169. start_cycles = get_cycles();
  170. urb = udl_get_urb(dev);
  171. if (!urb)
  172. return 0;
  173. cmd = urb->transfer_buffer;
  174. for (i = y; i <= y2 ; i++) {
  175. const int line_offset = fb->base.pitches[0] * i;
  176. const int byte_offset = line_offset + (x * bpp);
  177. const int dev_byte_offset = (fb->base.width * bpp * i) + (x * bpp);
  178. if (udl_render_hline(dev, bpp, &urb,
  179. (char *) fb->obj->vmapping,
  180. &cmd, byte_offset, dev_byte_offset,
  181. (x2 - x + 1) * bpp,
  182. &bytes_identical, &bytes_sent))
  183. goto error;
  184. }
  185. if (cmd > (char *) urb->transfer_buffer) {
  186. /* Send partial buffer remaining before exiting */
  187. int len = cmd - (char *) urb->transfer_buffer;
  188. ret = udl_submit_urb(dev, urb, len);
  189. bytes_sent += len;
  190. } else
  191. udl_urb_completion(urb);
  192. error:
  193. atomic_add(bytes_sent, &udl->bytes_sent);
  194. atomic_add(bytes_identical, &udl->bytes_identical);
  195. atomic_add(width*height*bpp, &udl->bytes_rendered);
  196. end_cycles = get_cycles();
  197. atomic_add(((unsigned int) ((end_cycles - start_cycles)
  198. >> 10)), /* Kcycles */
  199. &udl->cpu_kcycles_used);
  200. return 0;
  201. }
  202. static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  203. {
  204. unsigned long start = vma->vm_start;
  205. unsigned long size = vma->vm_end - vma->vm_start;
  206. unsigned long offset;
  207. unsigned long page, pos;
  208. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  209. return -EINVAL;
  210. offset = vma->vm_pgoff << PAGE_SHIFT;
  211. if (offset > info->fix.smem_len || size > info->fix.smem_len - offset)
  212. return -EINVAL;
  213. pos = (unsigned long)info->fix.smem_start + offset;
  214. pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
  215. pos, size);
  216. while (size > 0) {
  217. page = vmalloc_to_pfn((void *)pos);
  218. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  219. return -EAGAIN;
  220. start += PAGE_SIZE;
  221. pos += PAGE_SIZE;
  222. if (size > PAGE_SIZE)
  223. size -= PAGE_SIZE;
  224. else
  225. size = 0;
  226. }
  227. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  228. return 0;
  229. }
  230. static void udl_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  231. {
  232. struct udl_fbdev *ufbdev = info->par;
  233. sys_fillrect(info, rect);
  234. udl_handle_damage(&ufbdev->ufb, rect->dx, rect->dy, rect->width,
  235. rect->height);
  236. }
  237. static void udl_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
  238. {
  239. struct udl_fbdev *ufbdev = info->par;
  240. sys_copyarea(info, region);
  241. udl_handle_damage(&ufbdev->ufb, region->dx, region->dy, region->width,
  242. region->height);
  243. }
  244. static void udl_fb_imageblit(struct fb_info *info, const struct fb_image *image)
  245. {
  246. struct udl_fbdev *ufbdev = info->par;
  247. sys_imageblit(info, image);
  248. udl_handle_damage(&ufbdev->ufb, image->dx, image->dy, image->width,
  249. image->height);
  250. }
  251. /*
  252. * It's common for several clients to have framebuffer open simultaneously.
  253. * e.g. both fbcon and X. Makes things interesting.
  254. * Assumes caller is holding info->lock (for open and release at least)
  255. */
  256. static int udl_fb_open(struct fb_info *info, int user)
  257. {
  258. struct udl_fbdev *ufbdev = info->par;
  259. struct drm_device *dev = ufbdev->ufb.base.dev;
  260. struct udl_device *udl = dev->dev_private;
  261. /* If the USB device is gone, we don't accept new opens */
  262. if (drm_device_is_unplugged(udl->ddev))
  263. return -ENODEV;
  264. ufbdev->fb_count++;
  265. if (fb_defio && (info->fbdefio == NULL)) {
  266. /* enable defio at last moment if not disabled by client */
  267. struct fb_deferred_io *fbdefio;
  268. fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
  269. if (fbdefio) {
  270. fbdefio->delay = DL_DEFIO_WRITE_DELAY;
  271. fbdefio->deferred_io = udlfb_dpy_deferred_io;
  272. }
  273. info->fbdefio = fbdefio;
  274. fb_deferred_io_init(info);
  275. }
  276. pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
  277. info->node, user, info, ufbdev->fb_count);
  278. return 0;
  279. }
  280. /*
  281. * Assumes caller is holding info->lock mutex (for open and release at least)
  282. */
  283. static int udl_fb_release(struct fb_info *info, int user)
  284. {
  285. struct udl_fbdev *ufbdev = info->par;
  286. ufbdev->fb_count--;
  287. if ((ufbdev->fb_count == 0) && (info->fbdefio)) {
  288. fb_deferred_io_cleanup(info);
  289. kfree(info->fbdefio);
  290. info->fbdefio = NULL;
  291. info->fbops->fb_mmap = udl_fb_mmap;
  292. }
  293. pr_warn("released /dev/fb%d user=%d count=%d\n",
  294. info->node, user, ufbdev->fb_count);
  295. return 0;
  296. }
  297. static struct fb_ops udlfb_ops = {
  298. .owner = THIS_MODULE,
  299. .fb_check_var = drm_fb_helper_check_var,
  300. .fb_set_par = drm_fb_helper_set_par,
  301. .fb_fillrect = udl_fb_fillrect,
  302. .fb_copyarea = udl_fb_copyarea,
  303. .fb_imageblit = udl_fb_imageblit,
  304. .fb_pan_display = drm_fb_helper_pan_display,
  305. .fb_blank = drm_fb_helper_blank,
  306. .fb_setcmap = drm_fb_helper_setcmap,
  307. .fb_debug_enter = drm_fb_helper_debug_enter,
  308. .fb_debug_leave = drm_fb_helper_debug_leave,
  309. .fb_mmap = udl_fb_mmap,
  310. .fb_open = udl_fb_open,
  311. .fb_release = udl_fb_release,
  312. };
  313. void udl_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  314. u16 blue, int regno)
  315. {
  316. }
  317. void udl_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  318. u16 *blue, int regno)
  319. {
  320. *red = 0;
  321. *green = 0;
  322. *blue = 0;
  323. }
  324. static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
  325. struct drm_file *file,
  326. unsigned flags, unsigned color,
  327. struct drm_clip_rect *clips,
  328. unsigned num_clips)
  329. {
  330. struct udl_framebuffer *ufb = to_udl_fb(fb);
  331. int i;
  332. if (!ufb->active_16)
  333. return 0;
  334. for (i = 0; i < num_clips; i++) {
  335. udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
  336. clips[i].x2 - clips[i].x1,
  337. clips[i].y2 - clips[i].y1);
  338. }
  339. return 0;
  340. }
  341. static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
  342. {
  343. struct udl_framebuffer *ufb = to_udl_fb(fb);
  344. if (ufb->obj)
  345. drm_gem_object_unreference_unlocked(&ufb->obj->base);
  346. drm_framebuffer_cleanup(fb);
  347. kfree(ufb);
  348. }
  349. static const struct drm_framebuffer_funcs udlfb_funcs = {
  350. .destroy = udl_user_framebuffer_destroy,
  351. .dirty = udl_user_framebuffer_dirty,
  352. .create_handle = NULL,
  353. };
  354. static int
  355. udl_framebuffer_init(struct drm_device *dev,
  356. struct udl_framebuffer *ufb,
  357. struct drm_mode_fb_cmd2 *mode_cmd,
  358. struct udl_gem_object *obj)
  359. {
  360. int ret;
  361. spin_lock_init(&ufb->dirty_lock);
  362. ufb->obj = obj;
  363. ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
  364. drm_helper_mode_fill_fb_struct(&ufb->base, mode_cmd);
  365. return ret;
  366. }
  367. static int udlfb_create(struct udl_fbdev *ufbdev,
  368. struct drm_fb_helper_surface_size *sizes)
  369. {
  370. struct drm_device *dev = ufbdev->helper.dev;
  371. struct fb_info *info;
  372. struct device *device = &dev->usbdev->dev;
  373. struct drm_framebuffer *fb;
  374. struct drm_mode_fb_cmd2 mode_cmd;
  375. struct udl_gem_object *obj;
  376. uint32_t size;
  377. int ret = 0;
  378. if (sizes->surface_bpp == 24)
  379. sizes->surface_bpp = 32;
  380. mode_cmd.width = sizes->surface_width;
  381. mode_cmd.height = sizes->surface_height;
  382. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
  383. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  384. sizes->surface_depth);
  385. size = mode_cmd.pitches[0] * mode_cmd.height;
  386. size = ALIGN(size, PAGE_SIZE);
  387. obj = udl_gem_alloc_object(dev, size);
  388. if (!obj)
  389. goto out;
  390. ret = udl_gem_vmap(obj);
  391. if (ret) {
  392. DRM_ERROR("failed to vmap fb\n");
  393. goto out_gfree;
  394. }
  395. info = framebuffer_alloc(0, device);
  396. if (!info) {
  397. ret = -ENOMEM;
  398. goto out_gfree;
  399. }
  400. info->par = ufbdev;
  401. ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
  402. if (ret)
  403. goto out_gfree;
  404. fb = &ufbdev->ufb.base;
  405. ufbdev->helper.fb = fb;
  406. ufbdev->helper.fbdev = info;
  407. strcpy(info->fix.id, "udldrmfb");
  408. info->screen_base = ufbdev->ufb.obj->vmapping;
  409. info->fix.smem_len = size;
  410. info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
  411. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  412. info->fbops = &udlfb_ops;
  413. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  414. drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
  415. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  416. if (ret) {
  417. ret = -ENOMEM;
  418. goto out_gfree;
  419. }
  420. DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
  421. fb->width, fb->height,
  422. ufbdev->ufb.obj->vmapping);
  423. return ret;
  424. out_gfree:
  425. drm_gem_object_unreference(&ufbdev->ufb.obj->base);
  426. out:
  427. return ret;
  428. }
  429. static int udl_fb_find_or_create_single(struct drm_fb_helper *helper,
  430. struct drm_fb_helper_surface_size *sizes)
  431. {
  432. struct udl_fbdev *ufbdev = (struct udl_fbdev *)helper;
  433. int new_fb = 0;
  434. int ret;
  435. if (!helper->fb) {
  436. ret = udlfb_create(ufbdev, sizes);
  437. if (ret)
  438. return ret;
  439. new_fb = 1;
  440. }
  441. return new_fb;
  442. }
  443. static struct drm_fb_helper_funcs udl_fb_helper_funcs = {
  444. .gamma_set = udl_crtc_fb_gamma_set,
  445. .gamma_get = udl_crtc_fb_gamma_get,
  446. .fb_probe = udl_fb_find_or_create_single,
  447. };
  448. static void udl_fbdev_destroy(struct drm_device *dev,
  449. struct udl_fbdev *ufbdev)
  450. {
  451. struct fb_info *info;
  452. if (ufbdev->helper.fbdev) {
  453. info = ufbdev->helper.fbdev;
  454. unregister_framebuffer(info);
  455. if (info->cmap.len)
  456. fb_dealloc_cmap(&info->cmap);
  457. framebuffer_release(info);
  458. }
  459. drm_fb_helper_fini(&ufbdev->helper);
  460. drm_framebuffer_cleanup(&ufbdev->ufb.base);
  461. drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base);
  462. }
  463. int udl_fbdev_init(struct drm_device *dev)
  464. {
  465. struct udl_device *udl = dev->dev_private;
  466. int bpp_sel = fb_bpp;
  467. struct udl_fbdev *ufbdev;
  468. int ret;
  469. ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
  470. if (!ufbdev)
  471. return -ENOMEM;
  472. udl->fbdev = ufbdev;
  473. ufbdev->helper.funcs = &udl_fb_helper_funcs;
  474. ret = drm_fb_helper_init(dev, &ufbdev->helper,
  475. 1, 1);
  476. if (ret) {
  477. kfree(ufbdev);
  478. return ret;
  479. }
  480. drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
  481. drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
  482. return 0;
  483. }
  484. void udl_fbdev_cleanup(struct drm_device *dev)
  485. {
  486. struct udl_device *udl = dev->dev_private;
  487. if (!udl->fbdev)
  488. return;
  489. udl_fbdev_destroy(dev, udl->fbdev);
  490. kfree(udl->fbdev);
  491. udl->fbdev = NULL;
  492. }
  493. void udl_fbdev_unplug(struct drm_device *dev)
  494. {
  495. struct udl_device *udl = dev->dev_private;
  496. struct udl_fbdev *ufbdev;
  497. if (!udl->fbdev)
  498. return;
  499. ufbdev = udl->fbdev;
  500. if (ufbdev->helper.fbdev) {
  501. struct fb_info *info;
  502. info = ufbdev->helper.fbdev;
  503. unlink_framebuffer(info);
  504. }
  505. }
  506. struct drm_framebuffer *
  507. udl_fb_user_fb_create(struct drm_device *dev,
  508. struct drm_file *file,
  509. struct drm_mode_fb_cmd2 *mode_cmd)
  510. {
  511. struct drm_gem_object *obj;
  512. struct udl_framebuffer *ufb;
  513. int ret;
  514. obj = drm_gem_object_lookup(dev, file, mode_cmd->handles[0]);
  515. if (obj == NULL)
  516. return ERR_PTR(-ENOENT);
  517. ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
  518. if (ufb == NULL)
  519. return ERR_PTR(-ENOMEM);
  520. ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
  521. if (ret) {
  522. kfree(ufb);
  523. return ERR_PTR(-EINVAL);
  524. }
  525. return &ufb->base;
  526. }