fbsysfs.c 14 KB

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