vc_screen.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Provide access to virtual console memory.
  4. * /dev/vcs: the screen as it is being viewed right now (possibly scrolled)
  5. * /dev/vcsN: the screen of /dev/ttyN (1 <= N <= 63)
  6. * [minor: N]
  7. *
  8. * /dev/vcsaN: idem, but including attributes, and prefixed with
  9. * the 4 bytes lines,columns,x,y (as screendump used to give).
  10. * Attribute/character pair is in native endianity.
  11. * [minor: N+128]
  12. *
  13. * /dev/vcsuN: similar to /dev/vcsaN but using 4-byte unicode values
  14. * instead of 1-byte screen glyph values.
  15. * [minor: N+64]
  16. *
  17. * /dev/vcsuaN: same idea as /dev/vcsaN for unicode (not yet implemented).
  18. *
  19. * This replaces screendump and part of selection, so that the system
  20. * administrator can control access using file system permissions.
  21. *
  22. * [email protected] - efter Friedas begravelse - 950211
  23. *
  24. * [email protected] - modified not to send characters to wrong console
  25. * - fixed some fatal off-by-one bugs (0-- no longer == -1 -> looping and looping and looping...)
  26. * - making it shorter - scr_readw are macros which expand in PRETTY long code
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/major.h>
  30. #include <linux/errno.h>
  31. #include <linux/export.h>
  32. #include <linux/tty.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/mm.h>
  35. #include <linux/init.h>
  36. #include <linux/vt_kern.h>
  37. #include <linux/selection.h>
  38. #include <linux/kbd_kern.h>
  39. #include <linux/console.h>
  40. #include <linux/device.h>
  41. #include <linux/sched.h>
  42. #include <linux/fs.h>
  43. #include <linux/poll.h>
  44. #include <linux/signal.h>
  45. #include <linux/slab.h>
  46. #include <linux/notifier.h>
  47. #include <linux/uaccess.h>
  48. #include <asm/byteorder.h>
  49. #include <asm/unaligned.h>
  50. #define HEADER_SIZE 4u
  51. #define CON_BUF_SIZE (CONFIG_BASE_SMALL ? 256 : PAGE_SIZE)
  52. /*
  53. * Our minor space:
  54. *
  55. * 0 ... 63 glyph mode without attributes
  56. * 64 ... 127 unicode mode without attributes
  57. * 128 ... 191 glyph mode with attributes
  58. * 192 ... 255 unused (reserved for unicode with attributes)
  59. *
  60. * This relies on MAX_NR_CONSOLES being <= 63, meaning 63 actual consoles
  61. * with minors 0, 64, 128 and 192 being proxies for the foreground console.
  62. */
  63. #if MAX_NR_CONSOLES > 63
  64. #warning "/dev/vcs* devices may not accommodate more than 63 consoles"
  65. #endif
  66. #define console(inode) (iminor(inode) & 63)
  67. #define use_unicode(inode) (iminor(inode) & 64)
  68. #define use_attributes(inode) (iminor(inode) & 128)
  69. struct vcs_poll_data {
  70. struct notifier_block notifier;
  71. unsigned int cons_num;
  72. int event;
  73. wait_queue_head_t waitq;
  74. struct fasync_struct *fasync;
  75. };
  76. static int
  77. vcs_notifier(struct notifier_block *nb, unsigned long code, void *_param)
  78. {
  79. struct vt_notifier_param *param = _param;
  80. struct vc_data *vc = param->vc;
  81. struct vcs_poll_data *poll =
  82. container_of(nb, struct vcs_poll_data, notifier);
  83. int currcons = poll->cons_num;
  84. int fa_band;
  85. switch (code) {
  86. case VT_UPDATE:
  87. fa_band = POLL_PRI;
  88. break;
  89. case VT_DEALLOCATE:
  90. fa_band = POLL_HUP;
  91. break;
  92. default:
  93. return NOTIFY_DONE;
  94. }
  95. if (currcons == 0)
  96. currcons = fg_console;
  97. else
  98. currcons--;
  99. if (currcons != vc->vc_num)
  100. return NOTIFY_DONE;
  101. poll->event = code;
  102. wake_up_interruptible(&poll->waitq);
  103. kill_fasync(&poll->fasync, SIGIO, fa_band);
  104. return NOTIFY_OK;
  105. }
  106. static void
  107. vcs_poll_data_free(struct vcs_poll_data *poll)
  108. {
  109. unregister_vt_notifier(&poll->notifier);
  110. kfree(poll);
  111. }
  112. static struct vcs_poll_data *
  113. vcs_poll_data_get(struct file *file)
  114. {
  115. struct vcs_poll_data *poll = file->private_data, *kill = NULL;
  116. if (poll)
  117. return poll;
  118. poll = kzalloc(sizeof(*poll), GFP_KERNEL);
  119. if (!poll)
  120. return NULL;
  121. poll->cons_num = console(file_inode(file));
  122. init_waitqueue_head(&poll->waitq);
  123. poll->notifier.notifier_call = vcs_notifier;
  124. /*
  125. * In order not to lose any update event, we must pretend one might
  126. * have occurred before we have a chance to register our notifier.
  127. * This is also how user space has come to detect which kernels
  128. * support POLLPRI on /dev/vcs* devices i.e. using poll() with
  129. * POLLPRI and a zero timeout.
  130. */
  131. poll->event = VT_UPDATE;
  132. if (register_vt_notifier(&poll->notifier) != 0) {
  133. kfree(poll);
  134. return NULL;
  135. }
  136. /*
  137. * This code may be called either through ->poll() or ->fasync().
  138. * If we have two threads using the same file descriptor, they could
  139. * both enter this function, both notice that the structure hasn't
  140. * been allocated yet and go ahead allocating it in parallel, but
  141. * only one of them must survive and be shared otherwise we'd leak
  142. * memory with a dangling notifier callback.
  143. */
  144. spin_lock(&file->f_lock);
  145. if (!file->private_data) {
  146. file->private_data = poll;
  147. } else {
  148. /* someone else raced ahead of us */
  149. kill = poll;
  150. poll = file->private_data;
  151. }
  152. spin_unlock(&file->f_lock);
  153. if (kill)
  154. vcs_poll_data_free(kill);
  155. return poll;
  156. }
  157. /**
  158. * vcs_vc -- return VC for @inode
  159. * @inode: inode for which to return a VC
  160. * @viewed: returns whether this console is currently foreground (viewed)
  161. *
  162. * Must be called with console_lock.
  163. */
  164. static struct vc_data *vcs_vc(struct inode *inode, bool *viewed)
  165. {
  166. unsigned int currcons = console(inode);
  167. WARN_CONSOLE_UNLOCKED();
  168. if (currcons == 0) {
  169. currcons = fg_console;
  170. if (viewed)
  171. *viewed = true;
  172. } else {
  173. currcons--;
  174. if (viewed)
  175. *viewed = false;
  176. }
  177. return vc_cons[currcons].d;
  178. }
  179. /**
  180. * vcs_size -- return size for a VC in @vc
  181. * @vc: which VC
  182. * @attr: does it use attributes?
  183. * @unicode: is it unicode?
  184. *
  185. * Must be called with console_lock.
  186. */
  187. static int vcs_size(const struct vc_data *vc, bool attr, bool unicode)
  188. {
  189. int size;
  190. WARN_CONSOLE_UNLOCKED();
  191. size = vc->vc_rows * vc->vc_cols;
  192. if (attr) {
  193. if (unicode)
  194. return -EOPNOTSUPP;
  195. size = 2 * size + HEADER_SIZE;
  196. } else if (unicode)
  197. size *= 4;
  198. return size;
  199. }
  200. static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
  201. {
  202. struct inode *inode = file_inode(file);
  203. struct vc_data *vc;
  204. int size;
  205. console_lock();
  206. vc = vcs_vc(inode, NULL);
  207. if (!vc) {
  208. console_unlock();
  209. return -ENXIO;
  210. }
  211. size = vcs_size(vc, use_attributes(inode), use_unicode(inode));
  212. console_unlock();
  213. if (size < 0)
  214. return size;
  215. return fixed_size_llseek(file, offset, orig, size);
  216. }
  217. static int vcs_read_buf_uni(struct vc_data *vc, char *con_buf,
  218. unsigned int pos, unsigned int count, bool viewed)
  219. {
  220. unsigned int nr, row, col, maxcol = vc->vc_cols;
  221. int ret;
  222. ret = vc_uniscr_check(vc);
  223. if (ret)
  224. return ret;
  225. pos /= 4;
  226. row = pos / maxcol;
  227. col = pos % maxcol;
  228. nr = maxcol - col;
  229. do {
  230. if (nr > count / 4)
  231. nr = count / 4;
  232. vc_uniscr_copy_line(vc, con_buf, viewed, row, col, nr);
  233. con_buf += nr * 4;
  234. count -= nr * 4;
  235. row++;
  236. col = 0;
  237. nr = maxcol;
  238. } while (count);
  239. return 0;
  240. }
  241. static void vcs_read_buf_noattr(const struct vc_data *vc, char *con_buf,
  242. unsigned int pos, unsigned int count, bool viewed)
  243. {
  244. u16 *org;
  245. unsigned int col, maxcol = vc->vc_cols;
  246. org = screen_pos(vc, pos, viewed);
  247. col = pos % maxcol;
  248. pos += maxcol - col;
  249. while (count-- > 0) {
  250. *con_buf++ = (vcs_scr_readw(vc, org++) & 0xff);
  251. if (++col == maxcol) {
  252. org = screen_pos(vc, pos, viewed);
  253. col = 0;
  254. pos += maxcol;
  255. }
  256. }
  257. }
  258. static unsigned int vcs_read_buf(const struct vc_data *vc, char *con_buf,
  259. unsigned int pos, unsigned int count, bool viewed,
  260. unsigned int *skip)
  261. {
  262. u16 *org, *con_buf16;
  263. unsigned int col, maxcol = vc->vc_cols;
  264. unsigned int filled = count;
  265. if (pos < HEADER_SIZE) {
  266. /* clamp header values if they don't fit */
  267. con_buf[0] = min(vc->vc_rows, 0xFFu);
  268. con_buf[1] = min(vc->vc_cols, 0xFFu);
  269. getconsxy(vc, con_buf + 2);
  270. *skip += pos;
  271. count += pos;
  272. if (count > CON_BUF_SIZE) {
  273. count = CON_BUF_SIZE;
  274. filled = count - pos;
  275. }
  276. /* Advance state pointers and move on. */
  277. count -= min(HEADER_SIZE, count);
  278. pos = HEADER_SIZE;
  279. con_buf += HEADER_SIZE;
  280. /* If count >= 0, then pos is even... */
  281. } else if (pos & 1) {
  282. /*
  283. * Skip first byte for output if start address is odd. Update
  284. * region sizes up/down depending on free space in buffer.
  285. */
  286. (*skip)++;
  287. if (count < CON_BUF_SIZE)
  288. count++;
  289. else
  290. filled--;
  291. }
  292. if (!count)
  293. return filled;
  294. pos -= HEADER_SIZE;
  295. pos /= 2;
  296. col = pos % maxcol;
  297. org = screen_pos(vc, pos, viewed);
  298. pos += maxcol - col;
  299. /*
  300. * Buffer has even length, so we can always copy character + attribute.
  301. * We do not copy last byte to userspace if count is odd.
  302. */
  303. count = (count + 1) / 2;
  304. con_buf16 = (u16 *)con_buf;
  305. while (count) {
  306. *con_buf16++ = vcs_scr_readw(vc, org++);
  307. count--;
  308. if (++col == maxcol) {
  309. org = screen_pos(vc, pos, viewed);
  310. col = 0;
  311. pos += maxcol;
  312. }
  313. }
  314. return filled;
  315. }
  316. static ssize_t
  317. vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  318. {
  319. struct inode *inode = file_inode(file);
  320. struct vc_data *vc;
  321. struct vcs_poll_data *poll;
  322. unsigned int read;
  323. ssize_t ret;
  324. char *con_buf;
  325. loff_t pos;
  326. bool viewed, attr, uni_mode;
  327. con_buf = (char *) __get_free_page(GFP_KERNEL);
  328. if (!con_buf)
  329. return -ENOMEM;
  330. pos = *ppos;
  331. /* Select the proper current console and verify
  332. * sanity of the situation under the console lock.
  333. */
  334. console_lock();
  335. uni_mode = use_unicode(inode);
  336. attr = use_attributes(inode);
  337. ret = -EINVAL;
  338. if (pos < 0)
  339. goto unlock_out;
  340. /* we enforce 32-bit alignment for pos and count in unicode mode */
  341. if (uni_mode && (pos | count) & 3)
  342. goto unlock_out;
  343. poll = file->private_data;
  344. if (count && poll)
  345. poll->event = 0;
  346. read = 0;
  347. ret = 0;
  348. while (count) {
  349. unsigned int this_round, skip = 0;
  350. int size;
  351. vc = vcs_vc(inode, &viewed);
  352. if (!vc) {
  353. ret = -ENXIO;
  354. break;
  355. }
  356. /* Check whether we are above size each round,
  357. * as copy_to_user at the end of this loop
  358. * could sleep.
  359. */
  360. size = vcs_size(vc, attr, uni_mode);
  361. if (size < 0) {
  362. ret = size;
  363. break;
  364. }
  365. if (pos >= size)
  366. break;
  367. if (count > size - pos)
  368. count = size - pos;
  369. this_round = count;
  370. if (this_round > CON_BUF_SIZE)
  371. this_round = CON_BUF_SIZE;
  372. /* Perform the whole read into the local con_buf.
  373. * Then we can drop the console spinlock and safely
  374. * attempt to move it to userspace.
  375. */
  376. if (uni_mode) {
  377. ret = vcs_read_buf_uni(vc, con_buf, pos, this_round,
  378. viewed);
  379. if (ret)
  380. break;
  381. } else if (!attr) {
  382. vcs_read_buf_noattr(vc, con_buf, pos, this_round,
  383. viewed);
  384. } else {
  385. this_round = vcs_read_buf(vc, con_buf, pos, this_round,
  386. viewed, &skip);
  387. }
  388. /* Finally, release the console semaphore while we push
  389. * all the data to userspace from our temporary buffer.
  390. *
  391. * AKPM: Even though it's a semaphore, we should drop it because
  392. * the pagefault handling code may want to call printk().
  393. */
  394. console_unlock();
  395. ret = copy_to_user(buf, con_buf + skip, this_round);
  396. console_lock();
  397. if (ret) {
  398. read += this_round - ret;
  399. ret = -EFAULT;
  400. break;
  401. }
  402. buf += this_round;
  403. pos += this_round;
  404. read += this_round;
  405. count -= this_round;
  406. }
  407. *ppos += read;
  408. if (read)
  409. ret = read;
  410. unlock_out:
  411. console_unlock();
  412. free_page((unsigned long) con_buf);
  413. return ret;
  414. }
  415. static u16 *vcs_write_buf_noattr(struct vc_data *vc, const char *con_buf,
  416. unsigned int pos, unsigned int count, bool viewed, u16 **org0)
  417. {
  418. u16 *org;
  419. unsigned int col, maxcol = vc->vc_cols;
  420. *org0 = org = screen_pos(vc, pos, viewed);
  421. col = pos % maxcol;
  422. pos += maxcol - col;
  423. while (count > 0) {
  424. unsigned char c = *con_buf++;
  425. count--;
  426. vcs_scr_writew(vc,
  427. (vcs_scr_readw(vc, org) & 0xff00) | c, org);
  428. org++;
  429. if (++col == maxcol) {
  430. org = screen_pos(vc, pos, viewed);
  431. col = 0;
  432. pos += maxcol;
  433. }
  434. }
  435. return org;
  436. }
  437. /*
  438. * Compilers (gcc 10) are unable to optimize the swap in cpu_to_le16. So do it
  439. * the poor man way.
  440. */
  441. static inline u16 vc_compile_le16(u8 hi, u8 lo)
  442. {
  443. #ifdef __BIG_ENDIAN
  444. return (lo << 8u) | hi;
  445. #else
  446. return (hi << 8u) | lo;
  447. #endif
  448. }
  449. static u16 *vcs_write_buf(struct vc_data *vc, const char *con_buf,
  450. unsigned int pos, unsigned int count, bool viewed, u16 **org0)
  451. {
  452. u16 *org;
  453. unsigned int col, maxcol = vc->vc_cols;
  454. unsigned char c;
  455. /* header */
  456. if (pos < HEADER_SIZE) {
  457. char header[HEADER_SIZE];
  458. getconsxy(vc, header + 2);
  459. while (pos < HEADER_SIZE && count > 0) {
  460. count--;
  461. header[pos++] = *con_buf++;
  462. }
  463. if (!viewed)
  464. putconsxy(vc, header + 2);
  465. }
  466. if (!count)
  467. return NULL;
  468. pos -= HEADER_SIZE;
  469. col = (pos/2) % maxcol;
  470. *org0 = org = screen_pos(vc, pos/2, viewed);
  471. /* odd pos -- the first single character */
  472. if (pos & 1) {
  473. count--;
  474. c = *con_buf++;
  475. vcs_scr_writew(vc, vc_compile_le16(c, vcs_scr_readw(vc, org)),
  476. org);
  477. org++;
  478. pos++;
  479. if (++col == maxcol) {
  480. org = screen_pos(vc, pos/2, viewed);
  481. col = 0;
  482. }
  483. }
  484. pos /= 2;
  485. pos += maxcol - col;
  486. /* even pos -- handle attr+character pairs */
  487. while (count > 1) {
  488. unsigned short w;
  489. w = get_unaligned(((unsigned short *)con_buf));
  490. vcs_scr_writew(vc, w, org++);
  491. con_buf += 2;
  492. count -= 2;
  493. if (++col == maxcol) {
  494. org = screen_pos(vc, pos, viewed);
  495. col = 0;
  496. pos += maxcol;
  497. }
  498. }
  499. if (!count)
  500. return org;
  501. /* odd pos -- the remaining character */
  502. c = *con_buf++;
  503. vcs_scr_writew(vc, vc_compile_le16(vcs_scr_readw(vc, org) >> 8, c),
  504. org);
  505. return org;
  506. }
  507. static ssize_t
  508. vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  509. {
  510. struct inode *inode = file_inode(file);
  511. struct vc_data *vc;
  512. char *con_buf;
  513. u16 *org0, *org;
  514. unsigned int written;
  515. int size;
  516. ssize_t ret;
  517. loff_t pos;
  518. bool viewed, attr;
  519. if (use_unicode(inode))
  520. return -EOPNOTSUPP;
  521. con_buf = (char *) __get_free_page(GFP_KERNEL);
  522. if (!con_buf)
  523. return -ENOMEM;
  524. pos = *ppos;
  525. /* Select the proper current console and verify
  526. * sanity of the situation under the console lock.
  527. */
  528. console_lock();
  529. attr = use_attributes(inode);
  530. ret = -ENXIO;
  531. vc = vcs_vc(inode, &viewed);
  532. if (!vc)
  533. goto unlock_out;
  534. size = vcs_size(vc, attr, false);
  535. if (size < 0) {
  536. ret = size;
  537. goto unlock_out;
  538. }
  539. ret = -EINVAL;
  540. if (pos < 0 || pos > size)
  541. goto unlock_out;
  542. if (count > size - pos)
  543. count = size - pos;
  544. written = 0;
  545. while (count) {
  546. unsigned int this_round = count;
  547. if (this_round > CON_BUF_SIZE)
  548. this_round = CON_BUF_SIZE;
  549. /* Temporarily drop the console lock so that we can read
  550. * in the write data from userspace safely.
  551. */
  552. console_unlock();
  553. ret = copy_from_user(con_buf, buf, this_round);
  554. console_lock();
  555. if (ret) {
  556. this_round -= ret;
  557. if (!this_round) {
  558. /* Abort loop if no data were copied. Otherwise
  559. * fail with -EFAULT.
  560. */
  561. if (written)
  562. break;
  563. ret = -EFAULT;
  564. goto unlock_out;
  565. }
  566. }
  567. /* The vc might have been freed or vcs_size might have changed
  568. * while we slept to grab the user buffer, so recheck.
  569. * Return data written up to now on failure.
  570. */
  571. vc = vcs_vc(inode, &viewed);
  572. if (!vc) {
  573. if (written)
  574. break;
  575. ret = -ENXIO;
  576. goto unlock_out;
  577. }
  578. size = vcs_size(vc, attr, false);
  579. if (size < 0) {
  580. if (written)
  581. break;
  582. ret = size;
  583. goto unlock_out;
  584. }
  585. if (pos >= size)
  586. break;
  587. if (this_round > size - pos)
  588. this_round = size - pos;
  589. /* OK, now actually push the write to the console
  590. * under the lock using the local kernel buffer.
  591. */
  592. if (attr)
  593. org = vcs_write_buf(vc, con_buf, pos, this_round,
  594. viewed, &org0);
  595. else
  596. org = vcs_write_buf_noattr(vc, con_buf, pos, this_round,
  597. viewed, &org0);
  598. count -= this_round;
  599. written += this_round;
  600. buf += this_round;
  601. pos += this_round;
  602. if (org)
  603. update_region(vc, (unsigned long)(org0), org - org0);
  604. }
  605. *ppos += written;
  606. ret = written;
  607. if (written)
  608. vcs_scr_updated(vc);
  609. unlock_out:
  610. console_unlock();
  611. free_page((unsigned long) con_buf);
  612. return ret;
  613. }
  614. static __poll_t
  615. vcs_poll(struct file *file, poll_table *wait)
  616. {
  617. struct vcs_poll_data *poll = vcs_poll_data_get(file);
  618. __poll_t ret = DEFAULT_POLLMASK|EPOLLERR;
  619. if (poll) {
  620. poll_wait(file, &poll->waitq, wait);
  621. switch (poll->event) {
  622. case VT_UPDATE:
  623. ret = DEFAULT_POLLMASK|EPOLLPRI;
  624. break;
  625. case VT_DEALLOCATE:
  626. ret = DEFAULT_POLLMASK|EPOLLHUP|EPOLLERR;
  627. break;
  628. case 0:
  629. ret = DEFAULT_POLLMASK;
  630. break;
  631. }
  632. }
  633. return ret;
  634. }
  635. static int
  636. vcs_fasync(int fd, struct file *file, int on)
  637. {
  638. struct vcs_poll_data *poll = file->private_data;
  639. if (!poll) {
  640. /* don't allocate anything if all we want is disable fasync */
  641. if (!on)
  642. return 0;
  643. poll = vcs_poll_data_get(file);
  644. if (!poll)
  645. return -ENOMEM;
  646. }
  647. return fasync_helper(fd, file, on, &poll->fasync);
  648. }
  649. static int
  650. vcs_open(struct inode *inode, struct file *filp)
  651. {
  652. unsigned int currcons = console(inode);
  653. bool attr = use_attributes(inode);
  654. bool uni_mode = use_unicode(inode);
  655. int ret = 0;
  656. /* we currently don't support attributes in unicode mode */
  657. if (attr && uni_mode)
  658. return -EOPNOTSUPP;
  659. console_lock();
  660. if(currcons && !vc_cons_allocated(currcons-1))
  661. ret = -ENXIO;
  662. console_unlock();
  663. return ret;
  664. }
  665. static int vcs_release(struct inode *inode, struct file *file)
  666. {
  667. struct vcs_poll_data *poll = file->private_data;
  668. if (poll)
  669. vcs_poll_data_free(poll);
  670. return 0;
  671. }
  672. static const struct file_operations vcs_fops = {
  673. .llseek = vcs_lseek,
  674. .read = vcs_read,
  675. .write = vcs_write,
  676. .poll = vcs_poll,
  677. .fasync = vcs_fasync,
  678. .open = vcs_open,
  679. .release = vcs_release,
  680. };
  681. static struct class *vc_class;
  682. void vcs_make_sysfs(int index)
  683. {
  684. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL,
  685. "vcs%u", index + 1);
  686. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 65), NULL,
  687. "vcsu%u", index + 1);
  688. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL,
  689. "vcsa%u", index + 1);
  690. }
  691. void vcs_remove_sysfs(int index)
  692. {
  693. device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1));
  694. device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 65));
  695. device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129));
  696. }
  697. int __init vcs_init(void)
  698. {
  699. unsigned int i;
  700. if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
  701. panic("unable to get major %d for vcs device", VCS_MAJOR);
  702. vc_class = class_create(THIS_MODULE, "vc");
  703. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
  704. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 64), NULL, "vcsu");
  705. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
  706. for (i = 0; i < MIN_NR_CONSOLES; i++)
  707. vcs_make_sysfs(i);
  708. return 0;
  709. }