vc_screen.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. * aeb@cwi.nl - efter Friedas begravelse - 950211
  23. *
  24. * machek@k332.feld.cvut.cz - 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 = -ENXIO;
  338. vc = vcs_vc(inode, &viewed);
  339. if (!vc)
  340. goto unlock_out;
  341. ret = -EINVAL;
  342. if (pos < 0)
  343. goto unlock_out;
  344. /* we enforce 32-bit alignment for pos and count in unicode mode */
  345. if (uni_mode && (pos | count) & 3)
  346. goto unlock_out;
  347. poll = file->private_data;
  348. if (count && poll)
  349. poll->event = 0;
  350. read = 0;
  351. ret = 0;
  352. while (count) {
  353. unsigned int this_round, skip = 0;
  354. int size;
  355. /* Check whether we are above size each round,
  356. * as copy_to_user at the end of this loop
  357. * could sleep.
  358. */
  359. size = vcs_size(vc, attr, uni_mode);
  360. if (size < 0) {
  361. if (read)
  362. break;
  363. ret = size;
  364. goto unlock_out;
  365. }
  366. if (pos >= size)
  367. break;
  368. if (count > size - pos)
  369. count = size - pos;
  370. this_round = count;
  371. if (this_round > CON_BUF_SIZE)
  372. this_round = CON_BUF_SIZE;
  373. /* Perform the whole read into the local con_buf.
  374. * Then we can drop the console spinlock and safely
  375. * attempt to move it to userspace.
  376. */
  377. if (uni_mode) {
  378. ret = vcs_read_buf_uni(vc, con_buf, pos, this_round,
  379. viewed);
  380. if (ret)
  381. break;
  382. } else if (!attr) {
  383. vcs_read_buf_noattr(vc, con_buf, pos, this_round,
  384. viewed);
  385. } else {
  386. this_round = vcs_read_buf(vc, con_buf, pos, this_round,
  387. viewed, &skip);
  388. }
  389. /* Finally, release the console semaphore while we push
  390. * all the data to userspace from our temporary buffer.
  391. *
  392. * AKPM: Even though it's a semaphore, we should drop it because
  393. * the pagefault handling code may want to call printk().
  394. */
  395. console_unlock();
  396. ret = copy_to_user(buf, con_buf + skip, this_round);
  397. console_lock();
  398. if (ret) {
  399. read += this_round - ret;
  400. ret = -EFAULT;
  401. break;
  402. }
  403. buf += this_round;
  404. pos += this_round;
  405. read += this_round;
  406. count -= this_round;
  407. }
  408. *ppos += read;
  409. if (read)
  410. ret = read;
  411. unlock_out:
  412. console_unlock();
  413. free_page((unsigned long) con_buf);
  414. return ret;
  415. }
  416. static u16 *vcs_write_buf_noattr(struct vc_data *vc, const char *con_buf,
  417. unsigned int pos, unsigned int count, bool viewed, u16 **org0)
  418. {
  419. u16 *org;
  420. unsigned int col, maxcol = vc->vc_cols;
  421. *org0 = org = screen_pos(vc, pos, viewed);
  422. col = pos % maxcol;
  423. pos += maxcol - col;
  424. while (count > 0) {
  425. unsigned char c = *con_buf++;
  426. count--;
  427. vcs_scr_writew(vc,
  428. (vcs_scr_readw(vc, org) & 0xff00) | c, org);
  429. org++;
  430. if (++col == maxcol) {
  431. org = screen_pos(vc, pos, viewed);
  432. col = 0;
  433. pos += maxcol;
  434. }
  435. }
  436. return org;
  437. }
  438. /*
  439. * Compilers (gcc 10) are unable to optimize the swap in cpu_to_le16. So do it
  440. * the poor man way.
  441. */
  442. static inline u16 vc_compile_le16(u8 hi, u8 lo)
  443. {
  444. #ifdef __BIG_ENDIAN
  445. return (lo << 8u) | hi;
  446. #else
  447. return (hi << 8u) | lo;
  448. #endif
  449. }
  450. static u16 *vcs_write_buf(struct vc_data *vc, const char *con_buf,
  451. unsigned int pos, unsigned int count, bool viewed, u16 **org0)
  452. {
  453. u16 *org;
  454. unsigned int col, maxcol = vc->vc_cols;
  455. unsigned char c;
  456. /* header */
  457. if (pos < HEADER_SIZE) {
  458. char header[HEADER_SIZE];
  459. getconsxy(vc, header + 2);
  460. while (pos < HEADER_SIZE && count > 0) {
  461. count--;
  462. header[pos++] = *con_buf++;
  463. }
  464. if (!viewed)
  465. putconsxy(vc, header + 2);
  466. }
  467. if (!count)
  468. return NULL;
  469. pos -= HEADER_SIZE;
  470. col = (pos/2) % maxcol;
  471. *org0 = org = screen_pos(vc, pos/2, viewed);
  472. /* odd pos -- the first single character */
  473. if (pos & 1) {
  474. count--;
  475. c = *con_buf++;
  476. vcs_scr_writew(vc, vc_compile_le16(c, vcs_scr_readw(vc, org)),
  477. org);
  478. org++;
  479. pos++;
  480. if (++col == maxcol) {
  481. org = screen_pos(vc, pos/2, viewed);
  482. col = 0;
  483. }
  484. }
  485. pos /= 2;
  486. pos += maxcol - col;
  487. /* even pos -- handle attr+character pairs */
  488. while (count > 1) {
  489. unsigned short w;
  490. w = get_unaligned(((unsigned short *)con_buf));
  491. vcs_scr_writew(vc, w, org++);
  492. con_buf += 2;
  493. count -= 2;
  494. if (++col == maxcol) {
  495. org = screen_pos(vc, pos, viewed);
  496. col = 0;
  497. pos += maxcol;
  498. }
  499. }
  500. if (!count)
  501. return org;
  502. /* odd pos -- the remaining character */
  503. c = *con_buf++;
  504. vcs_scr_writew(vc, vc_compile_le16(vcs_scr_readw(vc, org) >> 8, c),
  505. org);
  506. return org;
  507. }
  508. static ssize_t
  509. vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  510. {
  511. struct inode *inode = file_inode(file);
  512. struct vc_data *vc;
  513. char *con_buf;
  514. u16 *org0, *org;
  515. unsigned int written;
  516. int size;
  517. ssize_t ret;
  518. loff_t pos;
  519. bool viewed, attr;
  520. if (use_unicode(inode))
  521. return -EOPNOTSUPP;
  522. con_buf = (char *) __get_free_page(GFP_KERNEL);
  523. if (!con_buf)
  524. return -ENOMEM;
  525. pos = *ppos;
  526. /* Select the proper current console and verify
  527. * sanity of the situation under the console lock.
  528. */
  529. console_lock();
  530. attr = use_attributes(inode);
  531. ret = -ENXIO;
  532. vc = vcs_vc(inode, &viewed);
  533. if (!vc)
  534. goto unlock_out;
  535. size = vcs_size(vc, attr, false);
  536. if (size < 0) {
  537. ret = size;
  538. goto unlock_out;
  539. }
  540. ret = -EINVAL;
  541. if (pos < 0 || pos > size)
  542. goto unlock_out;
  543. if (count > size - pos)
  544. count = size - pos;
  545. written = 0;
  546. while (count) {
  547. unsigned int this_round = count;
  548. if (this_round > CON_BUF_SIZE)
  549. this_round = CON_BUF_SIZE;
  550. /* Temporarily drop the console lock so that we can read
  551. * in the write data from userspace safely.
  552. */
  553. console_unlock();
  554. ret = copy_from_user(con_buf, buf, this_round);
  555. console_lock();
  556. if (ret) {
  557. this_round -= ret;
  558. if (!this_round) {
  559. /* Abort loop if no data were copied. Otherwise
  560. * fail with -EFAULT.
  561. */
  562. if (written)
  563. break;
  564. ret = -EFAULT;
  565. goto unlock_out;
  566. }
  567. }
  568. /* The vcs_size might have changed while we slept to grab
  569. * the user buffer, so recheck.
  570. * Return data written up to now on failure.
  571. */
  572. size = vcs_size(vc, attr, false);
  573. if (size < 0) {
  574. if (written)
  575. break;
  576. ret = size;
  577. goto unlock_out;
  578. }
  579. if (pos >= size)
  580. break;
  581. if (this_round > size - pos)
  582. this_round = size - pos;
  583. /* OK, now actually push the write to the console
  584. * under the lock using the local kernel buffer.
  585. */
  586. if (attr)
  587. org = vcs_write_buf(vc, con_buf, pos, this_round,
  588. viewed, &org0);
  589. else
  590. org = vcs_write_buf_noattr(vc, con_buf, pos, this_round,
  591. viewed, &org0);
  592. count -= this_round;
  593. written += this_round;
  594. buf += this_round;
  595. pos += this_round;
  596. if (org)
  597. update_region(vc, (unsigned long)(org0), org - org0);
  598. }
  599. *ppos += written;
  600. ret = written;
  601. if (written)
  602. vcs_scr_updated(vc);
  603. unlock_out:
  604. console_unlock();
  605. free_page((unsigned long) con_buf);
  606. return ret;
  607. }
  608. static __poll_t
  609. vcs_poll(struct file *file, poll_table *wait)
  610. {
  611. struct vcs_poll_data *poll = vcs_poll_data_get(file);
  612. __poll_t ret = DEFAULT_POLLMASK|EPOLLERR;
  613. if (poll) {
  614. poll_wait(file, &poll->waitq, wait);
  615. switch (poll->event) {
  616. case VT_UPDATE:
  617. ret = DEFAULT_POLLMASK|EPOLLPRI;
  618. break;
  619. case VT_DEALLOCATE:
  620. ret = DEFAULT_POLLMASK|EPOLLHUP|EPOLLERR;
  621. break;
  622. case 0:
  623. ret = DEFAULT_POLLMASK;
  624. break;
  625. }
  626. }
  627. return ret;
  628. }
  629. static int
  630. vcs_fasync(int fd, struct file *file, int on)
  631. {
  632. struct vcs_poll_data *poll = file->private_data;
  633. if (!poll) {
  634. /* don't allocate anything if all we want is disable fasync */
  635. if (!on)
  636. return 0;
  637. poll = vcs_poll_data_get(file);
  638. if (!poll)
  639. return -ENOMEM;
  640. }
  641. return fasync_helper(fd, file, on, &poll->fasync);
  642. }
  643. static int
  644. vcs_open(struct inode *inode, struct file *filp)
  645. {
  646. unsigned int currcons = console(inode);
  647. bool attr = use_attributes(inode);
  648. bool uni_mode = use_unicode(inode);
  649. int ret = 0;
  650. /* we currently don't support attributes in unicode mode */
  651. if (attr && uni_mode)
  652. return -EOPNOTSUPP;
  653. console_lock();
  654. if(currcons && !vc_cons_allocated(currcons-1))
  655. ret = -ENXIO;
  656. console_unlock();
  657. return ret;
  658. }
  659. static int vcs_release(struct inode *inode, struct file *file)
  660. {
  661. struct vcs_poll_data *poll = file->private_data;
  662. if (poll)
  663. vcs_poll_data_free(poll);
  664. return 0;
  665. }
  666. static const struct file_operations vcs_fops = {
  667. .llseek = vcs_lseek,
  668. .read = vcs_read,
  669. .write = vcs_write,
  670. .poll = vcs_poll,
  671. .fasync = vcs_fasync,
  672. .open = vcs_open,
  673. .release = vcs_release,
  674. };
  675. static struct class *vc_class;
  676. void vcs_make_sysfs(int index)
  677. {
  678. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL,
  679. "vcs%u", index + 1);
  680. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 65), NULL,
  681. "vcsu%u", index + 1);
  682. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL,
  683. "vcsa%u", index + 1);
  684. }
  685. void vcs_remove_sysfs(int index)
  686. {
  687. device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1));
  688. device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 65));
  689. device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129));
  690. }
  691. int __init vcs_init(void)
  692. {
  693. unsigned int i;
  694. if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
  695. panic("unable to get major %d for vcs device", VCS_MAJOR);
  696. vc_class = class_create(THIS_MODULE, "vc");
  697. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
  698. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 64), NULL, "vcsu");
  699. device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
  700. for (i = 0; i < MIN_NR_CONSOLES; i++)
  701. vcs_make_sysfs(i);
  702. return 0;
  703. }