fbsysfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * fbsysfs.c - framebuffer device class and attributes
  3. *
  4. * Copyright (c) 2004 James Simmons <jsimmons@infradead.org>
  5. *
  6. * This program is free software you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. /*
  12. * Note: currently there's only stubs for framebuffer_alloc and
  13. * framebuffer_release here. The reson for that is that until all drivers
  14. * are converted to use it a sysfsification will open OOPSable races.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/fb.h>
  18. #include <linux/console.h>
  19. /**
  20. * framebuffer_alloc - creates a new frame buffer info structure
  21. *
  22. * @size: size of driver private data, can be zero
  23. * @dev: pointer to the device for this fb, this can be NULL
  24. *
  25. * Creates a new frame buffer info structure. Also reserves @size bytes
  26. * for driver private data (info->par). info->par (if any) will be
  27. * aligned to sizeof(long).
  28. *
  29. * Returns the new structure, or NULL if an error occured.
  30. *
  31. */
  32. struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
  33. {
  34. #define BYTES_PER_LONG (BITS_PER_LONG/8)
  35. #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
  36. int fb_info_size = sizeof(struct fb_info);
  37. struct fb_info *info;
  38. char *p;
  39. if (size)
  40. fb_info_size += PADDING;
  41. p = kmalloc(fb_info_size + size, GFP_KERNEL);
  42. if (!p)
  43. return NULL;
  44. memset(p, 0, fb_info_size + size);
  45. info = (struct fb_info *) p;
  46. if (size)
  47. info->par = p + fb_info_size;
  48. info->device = dev;
  49. return info;
  50. #undef PADDING
  51. #undef BYTES_PER_LONG
  52. }
  53. EXPORT_SYMBOL(framebuffer_alloc);
  54. /**
  55. * framebuffer_release - marks the structure available for freeing
  56. *
  57. * @info: frame buffer info structure
  58. *
  59. * Drop the reference count of the class_device embedded in the
  60. * framebuffer info structure.
  61. *
  62. */
  63. void framebuffer_release(struct fb_info *info)
  64. {
  65. kfree(info);
  66. }
  67. EXPORT_SYMBOL(framebuffer_release);
  68. static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
  69. {
  70. int err;
  71. var->activate |= FB_ACTIVATE_FORCE;
  72. acquire_console_sem();
  73. fb_info->flags |= FBINFO_MISC_USEREVENT;
  74. err = fb_set_var(fb_info, var);
  75. fb_info->flags &= ~FBINFO_MISC_USEREVENT;
  76. release_console_sem();
  77. if (err)
  78. return err;
  79. return 0;
  80. }
  81. static int mode_string(char *buf, unsigned int offset,
  82. const struct fb_videomode *mode)
  83. {
  84. char m = 'U';
  85. if (mode->flag & FB_MODE_IS_DETAILED)
  86. m = 'D';
  87. if (mode->flag & FB_MODE_IS_VESA)
  88. m = 'V';
  89. if (mode->flag & FB_MODE_IS_STANDARD)
  90. m = 'S';
  91. return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d-%d\n", m, mode->xres, mode->yres, mode->refresh);
  92. }
  93. static ssize_t store_mode(struct class_device *class_device, const char * buf,
  94. size_t count)
  95. {
  96. struct fb_info *fb_info =
  97. (struct fb_info *)class_get_devdata(class_device);
  98. char mstr[100];
  99. struct fb_var_screeninfo var;
  100. struct fb_modelist *modelist;
  101. struct fb_videomode *mode;
  102. struct list_head *pos;
  103. size_t i;
  104. int err;
  105. memset(&var, 0, sizeof(var));
  106. list_for_each(pos, &fb_info->modelist) {
  107. modelist = list_entry(pos, struct fb_modelist, list);
  108. mode = &modelist->mode;
  109. i = mode_string(mstr, 0, mode);
  110. if (strncmp(mstr, buf, max(count, i)) == 0) {
  111. var = fb_info->var;
  112. fb_videomode_to_var(&var, mode);
  113. if ((err = activate(fb_info, &var)))
  114. return err;
  115. fb_info->mode = mode;
  116. return count;
  117. }
  118. }
  119. return -EINVAL;
  120. }
  121. static ssize_t show_mode(struct class_device *class_device, char *buf)
  122. {
  123. struct fb_info *fb_info =
  124. (struct fb_info *)class_get_devdata(class_device);
  125. if (!fb_info->mode)
  126. return 0;
  127. return mode_string(buf, 0, fb_info->mode);
  128. }
  129. static ssize_t store_modes(struct class_device *class_device, const char * buf,
  130. size_t count)
  131. {
  132. struct fb_info *fb_info =
  133. (struct fb_info *)class_get_devdata(class_device);
  134. LIST_HEAD(old_list);
  135. int i = count / sizeof(struct fb_videomode);
  136. if (i * sizeof(struct fb_videomode) != count)
  137. return -EINVAL;
  138. acquire_console_sem();
  139. list_splice(&fb_info->modelist, &old_list);
  140. fb_videomode_to_modelist((struct fb_videomode *)buf, i,
  141. &fb_info->modelist);
  142. if (fb_new_modelist(fb_info)) {
  143. fb_destroy_modelist(&fb_info->modelist);
  144. list_splice(&old_list, &fb_info->modelist);
  145. } else
  146. fb_destroy_modelist(&old_list);
  147. release_console_sem();
  148. return 0;
  149. }
  150. static ssize_t show_modes(struct class_device *class_device, char *buf)
  151. {
  152. struct fb_info *fb_info =
  153. (struct fb_info *)class_get_devdata(class_device);
  154. unsigned int i;
  155. struct list_head *pos;
  156. struct fb_modelist *modelist;
  157. const struct fb_videomode *mode;
  158. i = 0;
  159. list_for_each(pos, &fb_info->modelist) {
  160. modelist = list_entry(pos, struct fb_modelist, list);
  161. mode = &modelist->mode;
  162. i += mode_string(buf, i, mode);
  163. }
  164. return i;
  165. }
  166. static ssize_t store_bpp(struct class_device *class_device, const char * buf,
  167. size_t count)
  168. {
  169. struct fb_info *fb_info =
  170. (struct fb_info *)class_get_devdata(class_device);
  171. struct fb_var_screeninfo var;
  172. char ** last = NULL;
  173. int err;
  174. var = fb_info->var;
  175. var.bits_per_pixel = simple_strtoul(buf, last, 0);
  176. if ((err = activate(fb_info, &var)))
  177. return err;
  178. return count;
  179. }
  180. static ssize_t show_bpp(struct class_device *class_device, char *buf)
  181. {
  182. struct fb_info *fb_info =
  183. (struct fb_info *)class_get_devdata(class_device);
  184. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
  185. }
  186. static ssize_t store_rotate(struct class_device *class_device, const char *buf,
  187. size_t count)
  188. {
  189. struct fb_info *fb_info = class_get_devdata(class_device);
  190. struct fb_var_screeninfo var;
  191. char **last = NULL;
  192. int err;
  193. var = fb_info->var;
  194. var.rotate = simple_strtoul(buf, last, 0);
  195. if ((err = activate(fb_info, &var)))
  196. return err;
  197. return count;
  198. }
  199. static ssize_t show_rotate(struct class_device *class_device, char *buf)
  200. {
  201. struct fb_info *fb_info = class_get_devdata(class_device);
  202. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
  203. }
  204. static ssize_t store_con_rotate(struct class_device *class_device,
  205. const char *buf, size_t count)
  206. {
  207. struct fb_info *fb_info = class_get_devdata(class_device);
  208. int rotate;
  209. char **last = NULL;
  210. acquire_console_sem();
  211. rotate = simple_strtoul(buf, last, 0);
  212. fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE, &rotate);
  213. release_console_sem();
  214. return count;
  215. }
  216. static ssize_t store_con_rotate_all(struct class_device *class_device,
  217. const char *buf, size_t count)
  218. {
  219. struct fb_info *fb_info = class_get_devdata(class_device);
  220. int rotate;
  221. char **last = NULL;
  222. acquire_console_sem();
  223. rotate = simple_strtoul(buf, last, 0);
  224. fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE_ALL, &rotate);
  225. release_console_sem();
  226. return count;
  227. }
  228. static ssize_t show_con_rotate(struct class_device *class_device, char *buf)
  229. {
  230. struct fb_info *fb_info = class_get_devdata(class_device);
  231. int rotate;
  232. acquire_console_sem();
  233. rotate = fb_con_duit(fb_info, FB_EVENT_GET_CON_ROTATE, NULL);
  234. release_console_sem();
  235. return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
  236. }
  237. static ssize_t store_virtual(struct class_device *class_device,
  238. const char * buf, size_t count)
  239. {
  240. struct fb_info *fb_info =
  241. (struct fb_info *)class_get_devdata(class_device);
  242. struct fb_var_screeninfo var;
  243. char *last = NULL;
  244. int err;
  245. var = fb_info->var;
  246. var.xres_virtual = simple_strtoul(buf, &last, 0);
  247. last++;
  248. if (last - buf >= count)
  249. return -EINVAL;
  250. var.yres_virtual = simple_strtoul(last, &last, 0);
  251. if ((err = activate(fb_info, &var)))
  252. return err;
  253. return count;
  254. }
  255. static ssize_t show_virtual(struct class_device *class_device, char *buf)
  256. {
  257. struct fb_info *fb_info =
  258. (struct fb_info *)class_get_devdata(class_device);
  259. return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
  260. fb_info->var.yres_virtual);
  261. }
  262. static ssize_t show_stride(struct class_device *class_device, char *buf)
  263. {
  264. struct fb_info *fb_info =
  265. (struct fb_info *)class_get_devdata(class_device);
  266. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
  267. }
  268. /* Format for cmap is "%02x%c%4x%4x%4x\n" */
  269. /* %02x entry %c transp %4x red %4x blue %4x green \n */
  270. /* 256 rows at 16 chars equals 4096, the normal page size */
  271. /* the code will automatically adjust for different page sizes */
  272. static ssize_t store_cmap(struct class_device *class_device, const char *buf,
  273. size_t count)
  274. {
  275. struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
  276. int rc, i, start, length, transp = 0;
  277. if ((count > PAGE_SIZE) || ((count % 16) != 0))
  278. return -EINVAL;
  279. if (!fb_info->fbops->fb_setcolreg && !fb_info->fbops->fb_setcmap)
  280. return -EINVAL;
  281. sscanf(buf, "%02x", &start);
  282. length = count / 16;
  283. for (i = 0; i < length; i++)
  284. if (buf[i * 16 + 2] != ' ')
  285. transp = 1;
  286. /* If we can batch, do it */
  287. if (fb_info->fbops->fb_setcmap && length > 1) {
  288. struct fb_cmap umap;
  289. memset(&umap, 0, sizeof(umap));
  290. if ((rc = fb_alloc_cmap(&umap, length, transp)))
  291. return rc;
  292. umap.start = start;
  293. for (i = 0; i < length; i++) {
  294. sscanf(&buf[i * 16 + 3], "%4hx", &umap.red[i]);
  295. sscanf(&buf[i * 16 + 7], "%4hx", &umap.blue[i]);
  296. sscanf(&buf[i * 16 + 11], "%4hx", &umap.green[i]);
  297. if (transp)
  298. umap.transp[i] = (buf[i * 16 + 2] != ' ');
  299. }
  300. rc = fb_info->fbops->fb_setcmap(&umap, fb_info);
  301. fb_copy_cmap(&umap, &fb_info->cmap);
  302. fb_dealloc_cmap(&umap);
  303. return rc;
  304. }
  305. for (i = 0; i < length; i++) {
  306. u16 red, blue, green, tsp;
  307. sscanf(&buf[i * 16 + 3], "%4hx", &red);
  308. sscanf(&buf[i * 16 + 7], "%4hx", &blue);
  309. sscanf(&buf[i * 16 + 11], "%4hx", &green);
  310. tsp = (buf[i * 16 + 2] != ' ');
  311. if ((rc = fb_info->fbops->fb_setcolreg(start++,
  312. red, green, blue, tsp, fb_info)))
  313. return rc;
  314. fb_info->cmap.red[i] = red;
  315. fb_info->cmap.blue[i] = blue;
  316. fb_info->cmap.green[i] = green;
  317. if (transp)
  318. fb_info->cmap.transp[i] = tsp;
  319. }
  320. return 0;
  321. }
  322. static ssize_t show_cmap(struct class_device *class_device, char *buf)
  323. {
  324. struct fb_info *fb_info =
  325. (struct fb_info *)class_get_devdata(class_device);
  326. unsigned int i;
  327. if (!fb_info->cmap.red || !fb_info->cmap.blue ||
  328. !fb_info->cmap.green)
  329. return -EINVAL;
  330. if (fb_info->cmap.len > PAGE_SIZE / 16)
  331. return -EINVAL;
  332. /* don't mess with the format, the buffer is PAGE_SIZE */
  333. /* 256 entries at 16 chars per line equals 4096 = PAGE_SIZE */
  334. for (i = 0; i < fb_info->cmap.len; i++) {
  335. snprintf(&buf[ i * 16], PAGE_SIZE - i * 16, "%02x%c%4x%4x%4x\n", i + fb_info->cmap.start,
  336. ((fb_info->cmap.transp && fb_info->cmap.transp[i]) ? '*' : ' '),
  337. fb_info->cmap.red[i], fb_info->cmap.blue[i],
  338. fb_info->cmap.green[i]);
  339. }
  340. return 16 * fb_info->cmap.len;
  341. }
  342. static ssize_t store_blank(struct class_device *class_device, const char * buf,
  343. size_t count)
  344. {
  345. struct fb_info *fb_info =
  346. (struct fb_info *)class_get_devdata(class_device);
  347. char *last = NULL;
  348. int err;
  349. acquire_console_sem();
  350. fb_info->flags |= FBINFO_MISC_USEREVENT;
  351. err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
  352. fb_info->flags &= ~FBINFO_MISC_USEREVENT;
  353. release_console_sem();
  354. if (err < 0)
  355. return err;
  356. return count;
  357. }
  358. static ssize_t show_blank(struct class_device *class_device, char *buf)
  359. {
  360. // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
  361. return 0;
  362. }
  363. static ssize_t store_console(struct class_device *class_device,
  364. const char * buf, size_t count)
  365. {
  366. // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
  367. return 0;
  368. }
  369. static ssize_t show_console(struct class_device *class_device, char *buf)
  370. {
  371. // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
  372. return 0;
  373. }
  374. static ssize_t store_cursor(struct class_device *class_device,
  375. const char * buf, size_t count)
  376. {
  377. // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
  378. return 0;
  379. }
  380. static ssize_t show_cursor(struct class_device *class_device, char *buf)
  381. {
  382. // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
  383. return 0;
  384. }
  385. static ssize_t store_pan(struct class_device *class_device, const char * buf,
  386. size_t count)
  387. {
  388. struct fb_info *fb_info =
  389. (struct fb_info *)class_get_devdata(class_device);
  390. struct fb_var_screeninfo var;
  391. char *last = NULL;
  392. int err;
  393. var = fb_info->var;
  394. var.xoffset = simple_strtoul(buf, &last, 0);
  395. last++;
  396. if (last - buf >= count)
  397. return -EINVAL;
  398. var.yoffset = simple_strtoul(last, &last, 0);
  399. acquire_console_sem();
  400. err = fb_pan_display(fb_info, &var);
  401. release_console_sem();
  402. if (err < 0)
  403. return err;
  404. return count;
  405. }
  406. static ssize_t show_pan(struct class_device *class_device, char *buf)
  407. {
  408. struct fb_info *fb_info =
  409. (struct fb_info *)class_get_devdata(class_device);
  410. return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
  411. fb_info->var.xoffset);
  412. }
  413. static ssize_t show_name(struct class_device *class_device, char *buf)
  414. {
  415. struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
  416. return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
  417. }
  418. static struct class_device_attribute class_device_attrs[] = {
  419. __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
  420. __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
  421. __ATTR(color_map, S_IRUGO|S_IWUSR, show_cmap, store_cmap),
  422. __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
  423. __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
  424. __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
  425. __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
  426. __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
  427. __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
  428. __ATTR(name, S_IRUGO, show_name, NULL),
  429. __ATTR(stride, S_IRUGO, show_stride, NULL),
  430. __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
  431. __ATTR(con_rotate, S_IRUGO|S_IWUSR, show_con_rotate, store_con_rotate),
  432. __ATTR(con_rotate_all, S_IWUSR, NULL, store_con_rotate_all),
  433. };
  434. int fb_init_class_device(struct fb_info *fb_info)
  435. {
  436. unsigned int i;
  437. class_set_devdata(fb_info->class_device, fb_info);
  438. for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
  439. class_device_create_file(fb_info->class_device,
  440. &class_device_attrs[i]);
  441. return 0;
  442. }
  443. void fb_cleanup_class_device(struct fb_info *fb_info)
  444. {
  445. unsigned int i;
  446. for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
  447. class_device_remove_file(fb_info->class_device,
  448. &class_device_attrs[i]);
  449. }