xen-fbfront.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Xen para-virtual frame buffer device
  3. *
  4. * Copyright (C) 2005-2006 Anthony Liguori <aliguori@us.ibm.com>
  5. * Copyright (C) 2006-2008 Red Hat, Inc., Markus Armbruster <armbru@redhat.com>
  6. *
  7. * Based on linux/drivers/video/q40fb.c
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of this archive for
  11. * more details.
  12. */
  13. /*
  14. * TODO:
  15. *
  16. * Switch to grant tables when they become capable of dealing with the
  17. * frame buffer.
  18. */
  19. #include <linux/console.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/fb.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/mm.h>
  27. #include <asm/xen/hypervisor.h>
  28. #include <xen/xen.h>
  29. #include <xen/events.h>
  30. #include <xen/page.h>
  31. #include <xen/interface/io/fbif.h>
  32. #include <xen/interface/io/protocols.h>
  33. #include <xen/xenbus.h>
  34. #include <xen/platform_pci.h>
  35. struct xenfb_info {
  36. unsigned char *fb;
  37. struct fb_info *fb_info;
  38. int x1, y1, x2, y2; /* dirty rectangle,
  39. protected by dirty_lock */
  40. spinlock_t dirty_lock;
  41. int nr_pages;
  42. int irq;
  43. struct xenfb_page *page;
  44. unsigned long *mfns;
  45. int update_wanted; /* XENFB_TYPE_UPDATE wanted */
  46. int feature_resize; /* XENFB_TYPE_RESIZE ok */
  47. struct xenfb_resize resize; /* protected by resize_lock */
  48. int resize_dpy; /* ditto */
  49. spinlock_t resize_lock;
  50. struct xenbus_device *xbdev;
  51. };
  52. #define XENFB_DEFAULT_FB_LEN (XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8)
  53. enum { KPARAM_MEM, KPARAM_WIDTH, KPARAM_HEIGHT, KPARAM_CNT };
  54. static int video[KPARAM_CNT] = { 2, XENFB_WIDTH, XENFB_HEIGHT };
  55. module_param_array(video, int, NULL, 0);
  56. MODULE_PARM_DESC(video,
  57. "Video memory size in MB, width, height in pixels (default 2,800,600)");
  58. static void xenfb_make_preferred_console(void);
  59. static int xenfb_remove(struct xenbus_device *);
  60. static void xenfb_init_shared_page(struct xenfb_info *, struct fb_info *);
  61. static int xenfb_connect_backend(struct xenbus_device *, struct xenfb_info *);
  62. static void xenfb_disconnect_backend(struct xenfb_info *);
  63. static void xenfb_send_event(struct xenfb_info *info,
  64. union xenfb_out_event *event)
  65. {
  66. u32 prod;
  67. prod = info->page->out_prod;
  68. /* caller ensures !xenfb_queue_full() */
  69. mb(); /* ensure ring space available */
  70. XENFB_OUT_RING_REF(info->page, prod) = *event;
  71. wmb(); /* ensure ring contents visible */
  72. info->page->out_prod = prod + 1;
  73. notify_remote_via_irq(info->irq);
  74. }
  75. static void xenfb_do_update(struct xenfb_info *info,
  76. int x, int y, int w, int h)
  77. {
  78. union xenfb_out_event event;
  79. memset(&event, 0, sizeof(event));
  80. event.type = XENFB_TYPE_UPDATE;
  81. event.update.x = x;
  82. event.update.y = y;
  83. event.update.width = w;
  84. event.update.height = h;
  85. /* caller ensures !xenfb_queue_full() */
  86. xenfb_send_event(info, &event);
  87. }
  88. static void xenfb_do_resize(struct xenfb_info *info)
  89. {
  90. union xenfb_out_event event;
  91. memset(&event, 0, sizeof(event));
  92. event.resize = info->resize;
  93. /* caller ensures !xenfb_queue_full() */
  94. xenfb_send_event(info, &event);
  95. }
  96. static int xenfb_queue_full(struct xenfb_info *info)
  97. {
  98. u32 cons, prod;
  99. prod = info->page->out_prod;
  100. cons = info->page->out_cons;
  101. return prod - cons == XENFB_OUT_RING_LEN;
  102. }
  103. static void xenfb_handle_resize_dpy(struct xenfb_info *info)
  104. {
  105. unsigned long flags;
  106. spin_lock_irqsave(&info->resize_lock, flags);
  107. if (info->resize_dpy) {
  108. if (!xenfb_queue_full(info)) {
  109. info->resize_dpy = 0;
  110. xenfb_do_resize(info);
  111. }
  112. }
  113. spin_unlock_irqrestore(&info->resize_lock, flags);
  114. }
  115. static void xenfb_refresh(struct xenfb_info *info,
  116. int x1, int y1, int w, int h)
  117. {
  118. unsigned long flags;
  119. int x2 = x1 + w - 1;
  120. int y2 = y1 + h - 1;
  121. xenfb_handle_resize_dpy(info);
  122. if (!info->update_wanted)
  123. return;
  124. spin_lock_irqsave(&info->dirty_lock, flags);
  125. /* Combine with dirty rectangle: */
  126. if (info->y1 < y1)
  127. y1 = info->y1;
  128. if (info->y2 > y2)
  129. y2 = info->y2;
  130. if (info->x1 < x1)
  131. x1 = info->x1;
  132. if (info->x2 > x2)
  133. x2 = info->x2;
  134. if (xenfb_queue_full(info)) {
  135. /* Can't send right now, stash it in the dirty rectangle */
  136. info->x1 = x1;
  137. info->x2 = x2;
  138. info->y1 = y1;
  139. info->y2 = y2;
  140. spin_unlock_irqrestore(&info->dirty_lock, flags);
  141. return;
  142. }
  143. /* Clear dirty rectangle: */
  144. info->x1 = info->y1 = INT_MAX;
  145. info->x2 = info->y2 = 0;
  146. spin_unlock_irqrestore(&info->dirty_lock, flags);
  147. if (x1 <= x2 && y1 <= y2)
  148. xenfb_do_update(info, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
  149. }
  150. static void xenfb_deferred_io(struct fb_info *fb_info,
  151. struct list_head *pagelist)
  152. {
  153. struct xenfb_info *info = fb_info->par;
  154. struct page *page;
  155. unsigned long beg, end;
  156. int y1, y2, miny, maxy;
  157. miny = INT_MAX;
  158. maxy = 0;
  159. list_for_each_entry(page, pagelist, lru) {
  160. beg = page->index << PAGE_SHIFT;
  161. end = beg + PAGE_SIZE - 1;
  162. y1 = beg / fb_info->fix.line_length;
  163. y2 = end / fb_info->fix.line_length;
  164. if (y2 >= fb_info->var.yres)
  165. y2 = fb_info->var.yres - 1;
  166. if (miny > y1)
  167. miny = y1;
  168. if (maxy < y2)
  169. maxy = y2;
  170. }
  171. xenfb_refresh(info, 0, miny, fb_info->var.xres, maxy - miny + 1);
  172. }
  173. static struct fb_deferred_io xenfb_defio = {
  174. .delay = HZ / 20,
  175. .deferred_io = xenfb_deferred_io,
  176. };
  177. static int xenfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  178. unsigned blue, unsigned transp,
  179. struct fb_info *info)
  180. {
  181. u32 v;
  182. if (regno > info->cmap.len)
  183. return 1;
  184. #define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
  185. red = CNVT_TOHW(red, info->var.red.length);
  186. green = CNVT_TOHW(green, info->var.green.length);
  187. blue = CNVT_TOHW(blue, info->var.blue.length);
  188. transp = CNVT_TOHW(transp, info->var.transp.length);
  189. #undef CNVT_TOHW
  190. v = (red << info->var.red.offset) |
  191. (green << info->var.green.offset) |
  192. (blue << info->var.blue.offset);
  193. switch (info->var.bits_per_pixel) {
  194. case 16:
  195. case 24:
  196. case 32:
  197. ((u32 *)info->pseudo_palette)[regno] = v;
  198. break;
  199. }
  200. return 0;
  201. }
  202. static void xenfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
  203. {
  204. struct xenfb_info *info = p->par;
  205. sys_fillrect(p, rect);
  206. xenfb_refresh(info, rect->dx, rect->dy, rect->width, rect->height);
  207. }
  208. static void xenfb_imageblit(struct fb_info *p, const struct fb_image *image)
  209. {
  210. struct xenfb_info *info = p->par;
  211. sys_imageblit(p, image);
  212. xenfb_refresh(info, image->dx, image->dy, image->width, image->height);
  213. }
  214. static void xenfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
  215. {
  216. struct xenfb_info *info = p->par;
  217. sys_copyarea(p, area);
  218. xenfb_refresh(info, area->dx, area->dy, area->width, area->height);
  219. }
  220. static ssize_t xenfb_write(struct fb_info *p, const char __user *buf,
  221. size_t count, loff_t *ppos)
  222. {
  223. struct xenfb_info *info = p->par;
  224. ssize_t res;
  225. res = fb_sys_write(p, buf, count, ppos);
  226. xenfb_refresh(info, 0, 0, info->page->width, info->page->height);
  227. return res;
  228. }
  229. static int
  230. xenfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  231. {
  232. struct xenfb_info *xenfb_info;
  233. int required_mem_len;
  234. xenfb_info = info->par;
  235. if (!xenfb_info->feature_resize) {
  236. if (var->xres == video[KPARAM_WIDTH] &&
  237. var->yres == video[KPARAM_HEIGHT] &&
  238. var->bits_per_pixel == xenfb_info->page->depth) {
  239. return 0;
  240. }
  241. return -EINVAL;
  242. }
  243. /* Can't resize past initial width and height */
  244. if (var->xres > video[KPARAM_WIDTH] || var->yres > video[KPARAM_HEIGHT])
  245. return -EINVAL;
  246. required_mem_len = var->xres * var->yres * xenfb_info->page->depth / 8;
  247. if (var->bits_per_pixel == xenfb_info->page->depth &&
  248. var->xres <= info->fix.line_length / (XENFB_DEPTH / 8) &&
  249. required_mem_len <= info->fix.smem_len) {
  250. var->xres_virtual = var->xres;
  251. var->yres_virtual = var->yres;
  252. return 0;
  253. }
  254. return -EINVAL;
  255. }
  256. static int xenfb_set_par(struct fb_info *info)
  257. {
  258. struct xenfb_info *xenfb_info;
  259. unsigned long flags;
  260. xenfb_info = info->par;
  261. spin_lock_irqsave(&xenfb_info->resize_lock, flags);
  262. xenfb_info->resize.type = XENFB_TYPE_RESIZE;
  263. xenfb_info->resize.width = info->var.xres;
  264. xenfb_info->resize.height = info->var.yres;
  265. xenfb_info->resize.stride = info->fix.line_length;
  266. xenfb_info->resize.depth = info->var.bits_per_pixel;
  267. xenfb_info->resize.offset = 0;
  268. xenfb_info->resize_dpy = 1;
  269. spin_unlock_irqrestore(&xenfb_info->resize_lock, flags);
  270. return 0;
  271. }
  272. static struct fb_ops xenfb_fb_ops = {
  273. .owner = THIS_MODULE,
  274. .fb_read = fb_sys_read,
  275. .fb_write = xenfb_write,
  276. .fb_setcolreg = xenfb_setcolreg,
  277. .fb_fillrect = xenfb_fillrect,
  278. .fb_copyarea = xenfb_copyarea,
  279. .fb_imageblit = xenfb_imageblit,
  280. .fb_check_var = xenfb_check_var,
  281. .fb_set_par = xenfb_set_par,
  282. };
  283. static irqreturn_t xenfb_event_handler(int rq, void *dev_id)
  284. {
  285. /*
  286. * No in events recognized, simply ignore them all.
  287. * If you need to recognize some, see xen-kbdfront's
  288. * input_handler() for how to do that.
  289. */
  290. struct xenfb_info *info = dev_id;
  291. struct xenfb_page *page = info->page;
  292. if (page->in_cons != page->in_prod) {
  293. info->page->in_cons = info->page->in_prod;
  294. notify_remote_via_irq(info->irq);
  295. }
  296. /* Flush dirty rectangle: */
  297. xenfb_refresh(info, INT_MAX, INT_MAX, -INT_MAX, -INT_MAX);
  298. return IRQ_HANDLED;
  299. }
  300. static int xenfb_probe(struct xenbus_device *dev,
  301. const struct xenbus_device_id *id)
  302. {
  303. struct xenfb_info *info;
  304. struct fb_info *fb_info;
  305. int fb_size;
  306. int val;
  307. int ret = 0;
  308. info = kzalloc(sizeof(*info), GFP_KERNEL);
  309. if (info == NULL) {
  310. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  311. return -ENOMEM;
  312. }
  313. /* Limit kernel param videoram amount to what is in xenstore */
  314. if (xenbus_scanf(XBT_NIL, dev->otherend, "videoram", "%d", &val) == 1) {
  315. if (val < video[KPARAM_MEM])
  316. video[KPARAM_MEM] = val;
  317. }
  318. /* If requested res does not fit in available memory, use default */
  319. fb_size = video[KPARAM_MEM] * 1024 * 1024;
  320. if (video[KPARAM_WIDTH] * video[KPARAM_HEIGHT] * XENFB_DEPTH / 8
  321. > fb_size) {
  322. video[KPARAM_WIDTH] = XENFB_WIDTH;
  323. video[KPARAM_HEIGHT] = XENFB_HEIGHT;
  324. fb_size = XENFB_DEFAULT_FB_LEN;
  325. }
  326. dev_set_drvdata(&dev->dev, info);
  327. info->xbdev = dev;
  328. info->irq = -1;
  329. info->x1 = info->y1 = INT_MAX;
  330. spin_lock_init(&info->dirty_lock);
  331. spin_lock_init(&info->resize_lock);
  332. info->fb = vzalloc(fb_size);
  333. if (info->fb == NULL)
  334. goto error_nomem;
  335. info->nr_pages = (fb_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  336. info->mfns = vmalloc(sizeof(unsigned long) * info->nr_pages);
  337. if (!info->mfns)
  338. goto error_nomem;
  339. /* set up shared page */
  340. info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  341. if (!info->page)
  342. goto error_nomem;
  343. /* abusing framebuffer_alloc() to allocate pseudo_palette */
  344. fb_info = framebuffer_alloc(sizeof(u32) * 256, NULL);
  345. if (fb_info == NULL)
  346. goto error_nomem;
  347. /* complete the abuse: */
  348. fb_info->pseudo_palette = fb_info->par;
  349. fb_info->par = info;
  350. fb_info->screen_base = info->fb;
  351. fb_info->fbops = &xenfb_fb_ops;
  352. fb_info->var.xres_virtual = fb_info->var.xres = video[KPARAM_WIDTH];
  353. fb_info->var.yres_virtual = fb_info->var.yres = video[KPARAM_HEIGHT];
  354. fb_info->var.bits_per_pixel = XENFB_DEPTH;
  355. fb_info->var.red = (struct fb_bitfield){16, 8, 0};
  356. fb_info->var.green = (struct fb_bitfield){8, 8, 0};
  357. fb_info->var.blue = (struct fb_bitfield){0, 8, 0};
  358. fb_info->var.activate = FB_ACTIVATE_NOW;
  359. fb_info->var.height = -1;
  360. fb_info->var.width = -1;
  361. fb_info->var.vmode = FB_VMODE_NONINTERLACED;
  362. fb_info->fix.visual = FB_VISUAL_TRUECOLOR;
  363. fb_info->fix.line_length = fb_info->var.xres * XENFB_DEPTH / 8;
  364. fb_info->fix.smem_start = 0;
  365. fb_info->fix.smem_len = fb_size;
  366. strcpy(fb_info->fix.id, "xen");
  367. fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
  368. fb_info->fix.accel = FB_ACCEL_NONE;
  369. fb_info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
  370. ret = fb_alloc_cmap(&fb_info->cmap, 256, 0);
  371. if (ret < 0) {
  372. framebuffer_release(fb_info);
  373. xenbus_dev_fatal(dev, ret, "fb_alloc_cmap");
  374. goto error;
  375. }
  376. fb_info->fbdefio = &xenfb_defio;
  377. fb_deferred_io_init(fb_info);
  378. xenfb_init_shared_page(info, fb_info);
  379. ret = xenfb_connect_backend(dev, info);
  380. if (ret < 0) {
  381. xenbus_dev_fatal(dev, ret, "xenfb_connect_backend");
  382. goto error_fb;
  383. }
  384. ret = register_framebuffer(fb_info);
  385. if (ret) {
  386. xenbus_dev_fatal(dev, ret, "register_framebuffer");
  387. goto error_fb;
  388. }
  389. info->fb_info = fb_info;
  390. xenfb_make_preferred_console();
  391. return 0;
  392. error_fb:
  393. fb_deferred_io_cleanup(fb_info);
  394. fb_dealloc_cmap(&fb_info->cmap);
  395. framebuffer_release(fb_info);
  396. error_nomem:
  397. if (!ret) {
  398. ret = -ENOMEM;
  399. xenbus_dev_fatal(dev, ret, "allocating device memory");
  400. }
  401. error:
  402. xenfb_remove(dev);
  403. return ret;
  404. }
  405. static void xenfb_make_preferred_console(void)
  406. {
  407. struct console *c;
  408. if (console_set_on_cmdline)
  409. return;
  410. console_lock();
  411. for_each_console(c) {
  412. if (!strcmp(c->name, "tty") && c->index == 0)
  413. break;
  414. }
  415. console_unlock();
  416. if (c) {
  417. unregister_console(c);
  418. c->flags |= CON_CONSDEV;
  419. c->flags &= ~CON_PRINTBUFFER; /* don't print again */
  420. register_console(c);
  421. }
  422. }
  423. static int xenfb_resume(struct xenbus_device *dev)
  424. {
  425. struct xenfb_info *info = dev_get_drvdata(&dev->dev);
  426. xenfb_disconnect_backend(info);
  427. xenfb_init_shared_page(info, info->fb_info);
  428. return xenfb_connect_backend(dev, info);
  429. }
  430. static int xenfb_remove(struct xenbus_device *dev)
  431. {
  432. struct xenfb_info *info = dev_get_drvdata(&dev->dev);
  433. xenfb_disconnect_backend(info);
  434. if (info->fb_info) {
  435. fb_deferred_io_cleanup(info->fb_info);
  436. unregister_framebuffer(info->fb_info);
  437. fb_dealloc_cmap(&info->fb_info->cmap);
  438. framebuffer_release(info->fb_info);
  439. }
  440. free_page((unsigned long)info->page);
  441. vfree(info->mfns);
  442. vfree(info->fb);
  443. kfree(info);
  444. return 0;
  445. }
  446. static unsigned long vmalloc_to_mfn(void *address)
  447. {
  448. return pfn_to_mfn(vmalloc_to_pfn(address));
  449. }
  450. static void xenfb_init_shared_page(struct xenfb_info *info,
  451. struct fb_info *fb_info)
  452. {
  453. int i;
  454. int epd = PAGE_SIZE / sizeof(info->mfns[0]);
  455. for (i = 0; i < info->nr_pages; i++)
  456. info->mfns[i] = vmalloc_to_mfn(info->fb + i * PAGE_SIZE);
  457. for (i = 0; i * epd < info->nr_pages; i++)
  458. info->page->pd[i] = vmalloc_to_mfn(&info->mfns[i * epd]);
  459. info->page->width = fb_info->var.xres;
  460. info->page->height = fb_info->var.yres;
  461. info->page->depth = fb_info->var.bits_per_pixel;
  462. info->page->line_length = fb_info->fix.line_length;
  463. info->page->mem_length = fb_info->fix.smem_len;
  464. info->page->in_cons = info->page->in_prod = 0;
  465. info->page->out_cons = info->page->out_prod = 0;
  466. }
  467. static int xenfb_connect_backend(struct xenbus_device *dev,
  468. struct xenfb_info *info)
  469. {
  470. int ret, evtchn, irq;
  471. struct xenbus_transaction xbt;
  472. ret = xenbus_alloc_evtchn(dev, &evtchn);
  473. if (ret)
  474. return ret;
  475. irq = bind_evtchn_to_irqhandler(evtchn, xenfb_event_handler,
  476. 0, dev->devicetype, info);
  477. if (irq < 0) {
  478. xenbus_free_evtchn(dev, evtchn);
  479. xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
  480. return irq;
  481. }
  482. again:
  483. ret = xenbus_transaction_start(&xbt);
  484. if (ret) {
  485. xenbus_dev_fatal(dev, ret, "starting transaction");
  486. goto unbind_irq;
  487. }
  488. ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
  489. virt_to_mfn(info->page));
  490. if (ret)
  491. goto error_xenbus;
  492. ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
  493. evtchn);
  494. if (ret)
  495. goto error_xenbus;
  496. ret = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  497. XEN_IO_PROTO_ABI_NATIVE);
  498. if (ret)
  499. goto error_xenbus;
  500. ret = xenbus_printf(xbt, dev->nodename, "feature-update", "1");
  501. if (ret)
  502. goto error_xenbus;
  503. ret = xenbus_transaction_end(xbt, 0);
  504. if (ret) {
  505. if (ret == -EAGAIN)
  506. goto again;
  507. xenbus_dev_fatal(dev, ret, "completing transaction");
  508. goto unbind_irq;
  509. }
  510. xenbus_switch_state(dev, XenbusStateInitialised);
  511. info->irq = irq;
  512. return 0;
  513. error_xenbus:
  514. xenbus_transaction_end(xbt, 1);
  515. xenbus_dev_fatal(dev, ret, "writing xenstore");
  516. unbind_irq:
  517. unbind_from_irqhandler(irq, info);
  518. return ret;
  519. }
  520. static void xenfb_disconnect_backend(struct xenfb_info *info)
  521. {
  522. /* Prevent xenfb refresh */
  523. info->update_wanted = 0;
  524. if (info->irq >= 0)
  525. unbind_from_irqhandler(info->irq, info);
  526. info->irq = -1;
  527. }
  528. static void xenfb_backend_changed(struct xenbus_device *dev,
  529. enum xenbus_state backend_state)
  530. {
  531. struct xenfb_info *info = dev_get_drvdata(&dev->dev);
  532. int val;
  533. switch (backend_state) {
  534. case XenbusStateInitialising:
  535. case XenbusStateInitialised:
  536. case XenbusStateReconfiguring:
  537. case XenbusStateReconfigured:
  538. case XenbusStateUnknown:
  539. break;
  540. case XenbusStateInitWait:
  541. InitWait:
  542. xenbus_switch_state(dev, XenbusStateConnected);
  543. break;
  544. case XenbusStateConnected:
  545. /*
  546. * Work around xenbus race condition: If backend goes
  547. * through InitWait to Connected fast enough, we can
  548. * get Connected twice here.
  549. */
  550. if (dev->state != XenbusStateConnected)
  551. goto InitWait; /* no InitWait seen yet, fudge it */
  552. if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  553. "request-update", "%d", &val) < 0)
  554. val = 0;
  555. if (val)
  556. info->update_wanted = 1;
  557. if (xenbus_scanf(XBT_NIL, dev->otherend,
  558. "feature-resize", "%d", &val) < 0)
  559. val = 0;
  560. info->feature_resize = val;
  561. break;
  562. case XenbusStateClosed:
  563. if (dev->state == XenbusStateClosed)
  564. break;
  565. /* Missed the backend's CLOSING state -- fallthrough */
  566. case XenbusStateClosing:
  567. xenbus_frontend_closed(dev);
  568. break;
  569. }
  570. }
  571. static const struct xenbus_device_id xenfb_ids[] = {
  572. { "vfb" },
  573. { "" }
  574. };
  575. static struct xenbus_driver xenfb_driver = {
  576. .ids = xenfb_ids,
  577. .probe = xenfb_probe,
  578. .remove = xenfb_remove,
  579. .resume = xenfb_resume,
  580. .otherend_changed = xenfb_backend_changed,
  581. };
  582. static int __init xenfb_init(void)
  583. {
  584. if (!xen_domain())
  585. return -ENODEV;
  586. /* Nothing to do if running in dom0. */
  587. if (xen_initial_domain())
  588. return -ENODEV;
  589. if (!xen_has_pv_devices())
  590. return -ENODEV;
  591. return xenbus_register_frontend(&xenfb_driver);
  592. }
  593. static void __exit xenfb_cleanup(void)
  594. {
  595. xenbus_unregister_driver(&xenfb_driver);
  596. }
  597. module_init(xenfb_init);
  598. module_exit(xenfb_cleanup);
  599. MODULE_DESCRIPTION("Xen virtual framebuffer device frontend");
  600. MODULE_LICENSE("GPL");
  601. MODULE_ALIAS("xen:vfb");