hvc_console.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  4. * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
  5. * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
  6. * Copyright (C) 2004 IBM Corporation
  7. *
  8. * Additional Author(s):
  9. * Ryan S. Arnold <rsa@us.ibm.com>
  10. */
  11. #include <linux/console.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/init.h>
  14. #include <linux/kbd_kern.h>
  15. #include <linux/kernel.h>
  16. #include <linux/kthread.h>
  17. #include <linux/list.h>
  18. #include <linux/major.h>
  19. #include <linux/atomic.h>
  20. #include <linux/sysrq.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/sched.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/delay.h>
  26. #include <linux/freezer.h>
  27. #include <linux/slab.h>
  28. #include <linux/serial_core.h>
  29. #include <linux/uaccess.h>
  30. #include "hvc_console.h"
  31. #define HVC_MAJOR 229
  32. #define HVC_MINOR 0
  33. /*
  34. * Wait this long per iteration while trying to push buffered data to the
  35. * hypervisor before allowing the tty to complete a close operation.
  36. */
  37. #define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
  38. /*
  39. * These sizes are most efficient for vio, because they are the
  40. * native transfer size. We could make them selectable in the
  41. * future to better deal with backends that want other buffer sizes.
  42. */
  43. #define N_OUTBUF 16
  44. #define N_INBUF 16
  45. #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
  46. static struct tty_driver *hvc_driver;
  47. static struct task_struct *hvc_task;
  48. /* Picks up late kicks after list walk but before schedule() */
  49. static int hvc_kicked;
  50. /* hvc_init is triggered from hvc_alloc, i.e. only when actually used */
  51. static atomic_t hvc_needs_init __read_mostly = ATOMIC_INIT(-1);
  52. static int hvc_init(void);
  53. #ifdef CONFIG_MAGIC_SYSRQ
  54. static int sysrq_pressed;
  55. #endif
  56. /* dynamic list of hvc_struct instances */
  57. static LIST_HEAD(hvc_structs);
  58. /*
  59. * Protect the list of hvc_struct instances from inserts and removals during
  60. * list traversal.
  61. */
  62. static DEFINE_SPINLOCK(hvc_structs_lock);
  63. /*
  64. * This value is used to assign a tty->index value to a hvc_struct based
  65. * upon order of exposure via hvc_probe(), when we can not match it to
  66. * a console candidate registered with hvc_instantiate().
  67. */
  68. static int last_hvc = -1;
  69. /*
  70. * Do not call this function with either the hvc_structs_lock or the hvc_struct
  71. * lock held. If successful, this function increments the kref reference
  72. * count against the target hvc_struct so it should be released when finished.
  73. */
  74. static struct hvc_struct *hvc_get_by_index(int index)
  75. {
  76. struct hvc_struct *hp;
  77. unsigned long flags;
  78. spin_lock(&hvc_structs_lock);
  79. list_for_each_entry(hp, &hvc_structs, next) {
  80. spin_lock_irqsave(&hp->lock, flags);
  81. if (hp->index == index) {
  82. tty_port_get(&hp->port);
  83. spin_unlock_irqrestore(&hp->lock, flags);
  84. spin_unlock(&hvc_structs_lock);
  85. return hp;
  86. }
  87. spin_unlock_irqrestore(&hp->lock, flags);
  88. }
  89. hp = NULL;
  90. spin_unlock(&hvc_structs_lock);
  91. return hp;
  92. }
  93. /*
  94. * Initial console vtermnos for console API usage prior to full console
  95. * initialization. Any vty adapter outside this range will not have usable
  96. * console interfaces but can still be used as a tty device. This has to be
  97. * static because kmalloc will not work during early console init.
  98. */
  99. static const struct hv_ops *cons_ops[MAX_NR_HVC_CONSOLES];
  100. static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] =
  101. {[0 ... MAX_NR_HVC_CONSOLES - 1] = -1};
  102. /*
  103. * Console APIs, NOT TTY. These APIs are available immediately when
  104. * hvc_console_setup() finds adapters.
  105. */
  106. static void hvc_console_print(struct console *co, const char *b,
  107. unsigned count)
  108. {
  109. char c[N_OUTBUF] __ALIGNED__;
  110. unsigned i = 0, n = 0;
  111. int r, donecr = 0, index = co->index;
  112. /* Console access attempt outside of acceptable console range. */
  113. if (index >= MAX_NR_HVC_CONSOLES)
  114. return;
  115. /* This console adapter was removed so it is not usable. */
  116. if (vtermnos[index] == -1)
  117. return;
  118. while (count > 0 || i > 0) {
  119. if (count > 0 && i < sizeof(c)) {
  120. if (b[n] == '\n' && !donecr) {
  121. c[i++] = '\r';
  122. donecr = 1;
  123. } else {
  124. c[i++] = b[n++];
  125. donecr = 0;
  126. --count;
  127. }
  128. } else {
  129. r = cons_ops[index]->put_chars(vtermnos[index], c, i);
  130. if (r <= 0) {
  131. /* throw away characters on error
  132. * but spin in case of -EAGAIN */
  133. if (r != -EAGAIN)
  134. i = 0;
  135. } else if (r > 0) {
  136. i -= r;
  137. if (i > 0)
  138. memmove(c, c+r, i);
  139. }
  140. }
  141. }
  142. }
  143. static struct tty_driver *hvc_console_device(struct console *c, int *index)
  144. {
  145. if (vtermnos[c->index] == -1)
  146. return NULL;
  147. *index = c->index;
  148. return hvc_driver;
  149. }
  150. static int hvc_console_setup(struct console *co, char *options)
  151. {
  152. if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES)
  153. return -ENODEV;
  154. if (vtermnos[co->index] == -1)
  155. return -ENODEV;
  156. return 0;
  157. }
  158. static struct console hvc_console = {
  159. .name = "hvc",
  160. .write = hvc_console_print,
  161. .device = hvc_console_device,
  162. .setup = hvc_console_setup,
  163. .flags = CON_PRINTBUFFER,
  164. .index = -1,
  165. };
  166. /*
  167. * Early console initialization. Precedes driver initialization.
  168. *
  169. * (1) we are first, and the user specified another driver
  170. * -- index will remain -1
  171. * (2) we are first and the user specified no driver
  172. * -- index will be set to 0, then we will fail setup.
  173. * (3) we are first and the user specified our driver
  174. * -- index will be set to user specified driver, and we will fail
  175. * (4) we are after driver, and this initcall will register us
  176. * -- if the user didn't specify a driver then the console will match
  177. *
  178. * Note that for cases 2 and 3, we will match later when the io driver
  179. * calls hvc_instantiate() and call register again.
  180. */
  181. static int __init hvc_console_init(void)
  182. {
  183. register_console(&hvc_console);
  184. return 0;
  185. }
  186. console_initcall(hvc_console_init);
  187. /* callback when the kboject ref count reaches zero. */
  188. static void hvc_port_destruct(struct tty_port *port)
  189. {
  190. struct hvc_struct *hp = container_of(port, struct hvc_struct, port);
  191. unsigned long flags;
  192. spin_lock(&hvc_structs_lock);
  193. spin_lock_irqsave(&hp->lock, flags);
  194. list_del(&(hp->next));
  195. spin_unlock_irqrestore(&hp->lock, flags);
  196. spin_unlock(&hvc_structs_lock);
  197. kfree(hp);
  198. }
  199. static void hvc_check_console(int index)
  200. {
  201. /* Already enabled, bail out */
  202. if (hvc_console.flags & CON_ENABLED)
  203. return;
  204. /* If this index is what the user requested, then register
  205. * now (setup won't fail at this point). It's ok to just
  206. * call register again if previously .setup failed.
  207. */
  208. if (index == hvc_console.index)
  209. register_console(&hvc_console);
  210. }
  211. /*
  212. * hvc_instantiate() is an early console discovery method which locates
  213. * consoles * prior to the vio subsystem discovering them. Hotplugged
  214. * vty adapters do NOT get an hvc_instantiate() callback since they
  215. * appear after early console init.
  216. */
  217. int hvc_instantiate(uint32_t vtermno, int index, const struct hv_ops *ops)
  218. {
  219. struct hvc_struct *hp;
  220. if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
  221. return -1;
  222. if (vtermnos[index] != -1)
  223. return -1;
  224. /* make sure no no tty has been registered in this index */
  225. hp = hvc_get_by_index(index);
  226. if (hp) {
  227. tty_port_put(&hp->port);
  228. return -1;
  229. }
  230. vtermnos[index] = vtermno;
  231. cons_ops[index] = ops;
  232. /* reserve all indices up to and including this index */
  233. if (last_hvc < index)
  234. last_hvc = index;
  235. /* check if we need to re-register the kernel console */
  236. hvc_check_console(index);
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(hvc_instantiate);
  240. /* Wake the sleeping khvcd */
  241. void hvc_kick(void)
  242. {
  243. hvc_kicked = 1;
  244. wake_up_process(hvc_task);
  245. }
  246. EXPORT_SYMBOL_GPL(hvc_kick);
  247. static void hvc_unthrottle(struct tty_struct *tty)
  248. {
  249. hvc_kick();
  250. }
  251. static int hvc_install(struct tty_driver *driver, struct tty_struct *tty)
  252. {
  253. struct hvc_struct *hp;
  254. int rc;
  255. /* Auto increments kref reference if found. */
  256. hp = hvc_get_by_index(tty->index);
  257. if (!hp)
  258. return -ENODEV;
  259. tty->driver_data = hp;
  260. rc = tty_port_install(&hp->port, driver, tty);
  261. if (rc)
  262. tty_port_put(&hp->port);
  263. return rc;
  264. }
  265. /*
  266. * The TTY interface won't be used until after the vio layer has exposed the vty
  267. * adapter to the kernel.
  268. */
  269. static int hvc_open(struct tty_struct *tty, struct file * filp)
  270. {
  271. struct hvc_struct *hp = tty->driver_data;
  272. unsigned long flags;
  273. int rc = 0;
  274. spin_lock_irqsave(&hp->port.lock, flags);
  275. /* Check and then increment for fast path open. */
  276. if (hp->port.count++ > 0) {
  277. spin_unlock_irqrestore(&hp->port.lock, flags);
  278. hvc_kick();
  279. return 0;
  280. } /* else count == 0 */
  281. spin_unlock_irqrestore(&hp->port.lock, flags);
  282. tty_port_tty_set(&hp->port, tty);
  283. if (hp->ops->notifier_add)
  284. rc = hp->ops->notifier_add(hp, hp->data);
  285. /*
  286. * If the notifier fails we return an error. The tty layer
  287. * will call hvc_close() after a failed open but we don't want to clean
  288. * up there so we'll clean up here and clear out the previously set
  289. * tty fields and return the kref reference.
  290. */
  291. if (rc) {
  292. tty_port_tty_set(&hp->port, NULL);
  293. tty->driver_data = NULL;
  294. tty_port_put(&hp->port);
  295. printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
  296. } else
  297. /* We are ready... raise DTR/RTS */
  298. if (C_BAUD(tty))
  299. if (hp->ops->dtr_rts)
  300. hp->ops->dtr_rts(hp, 1);
  301. /* Force wakeup of the polling thread */
  302. hvc_kick();
  303. return rc;
  304. }
  305. static void hvc_close(struct tty_struct *tty, struct file * filp)
  306. {
  307. struct hvc_struct *hp;
  308. unsigned long flags;
  309. if (tty_hung_up_p(filp))
  310. return;
  311. /*
  312. * No driver_data means that this close was issued after a failed
  313. * hvc_open by the tty layer's release_dev() function and we can just
  314. * exit cleanly because the kref reference wasn't made.
  315. */
  316. if (!tty->driver_data)
  317. return;
  318. hp = tty->driver_data;
  319. spin_lock_irqsave(&hp->port.lock, flags);
  320. if (--hp->port.count == 0) {
  321. spin_unlock_irqrestore(&hp->port.lock, flags);
  322. /* We are done with the tty pointer now. */
  323. tty_port_tty_set(&hp->port, NULL);
  324. if (C_HUPCL(tty))
  325. if (hp->ops->dtr_rts)
  326. hp->ops->dtr_rts(hp, 0);
  327. if (hp->ops->notifier_del)
  328. hp->ops->notifier_del(hp, hp->data);
  329. /* cancel pending tty resize work */
  330. cancel_work_sync(&hp->tty_resize);
  331. /*
  332. * Chain calls chars_in_buffer() and returns immediately if
  333. * there is no buffered data otherwise sleeps on a wait queue
  334. * waking periodically to check chars_in_buffer().
  335. */
  336. tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
  337. } else {
  338. if (hp->port.count < 0)
  339. printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
  340. hp->vtermno, hp->port.count);
  341. spin_unlock_irqrestore(&hp->port.lock, flags);
  342. }
  343. }
  344. static void hvc_cleanup(struct tty_struct *tty)
  345. {
  346. struct hvc_struct *hp = tty->driver_data;
  347. tty_port_put(&hp->port);
  348. }
  349. static void hvc_hangup(struct tty_struct *tty)
  350. {
  351. struct hvc_struct *hp = tty->driver_data;
  352. unsigned long flags;
  353. if (!hp)
  354. return;
  355. /* cancel pending tty resize work */
  356. cancel_work_sync(&hp->tty_resize);
  357. spin_lock_irqsave(&hp->port.lock, flags);
  358. /*
  359. * The N_TTY line discipline has problems such that in a close vs
  360. * open->hangup case this can be called after the final close so prevent
  361. * that from happening for now.
  362. */
  363. if (hp->port.count <= 0) {
  364. spin_unlock_irqrestore(&hp->port.lock, flags);
  365. return;
  366. }
  367. hp->port.count = 0;
  368. spin_unlock_irqrestore(&hp->port.lock, flags);
  369. tty_port_tty_set(&hp->port, NULL);
  370. hp->n_outbuf = 0;
  371. if (hp->ops->notifier_hangup)
  372. hp->ops->notifier_hangup(hp, hp->data);
  373. }
  374. /*
  375. * Push buffered characters whether they were just recently buffered or waiting
  376. * on a blocked hypervisor. Call this function with hp->lock held.
  377. */
  378. static int hvc_push(struct hvc_struct *hp)
  379. {
  380. int n;
  381. n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
  382. if (n <= 0) {
  383. if (n == 0 || n == -EAGAIN) {
  384. hp->do_wakeup = 1;
  385. return 0;
  386. }
  387. /* throw away output on error; this happens when
  388. there is no session connected to the vterm. */
  389. hp->n_outbuf = 0;
  390. } else
  391. hp->n_outbuf -= n;
  392. if (hp->n_outbuf > 0)
  393. memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
  394. else
  395. hp->do_wakeup = 1;
  396. return n;
  397. }
  398. static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
  399. {
  400. struct hvc_struct *hp = tty->driver_data;
  401. unsigned long flags;
  402. int rsize, written = 0;
  403. /* This write was probably executed during a tty close. */
  404. if (!hp)
  405. return -EPIPE;
  406. /* FIXME what's this (unprotected) check for? */
  407. if (hp->port.count <= 0)
  408. return -EIO;
  409. spin_lock_irqsave(&hp->lock, flags);
  410. /* Push pending writes */
  411. if (hp->n_outbuf > 0)
  412. hvc_push(hp);
  413. while (count > 0 && (rsize = hp->outbuf_size - hp->n_outbuf) > 0) {
  414. if (rsize > count)
  415. rsize = count;
  416. memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
  417. count -= rsize;
  418. buf += rsize;
  419. hp->n_outbuf += rsize;
  420. written += rsize;
  421. hvc_push(hp);
  422. }
  423. spin_unlock_irqrestore(&hp->lock, flags);
  424. /*
  425. * Racy, but harmless, kick thread if there is still pending data.
  426. */
  427. if (hp->n_outbuf)
  428. hvc_kick();
  429. return written;
  430. }
  431. /**
  432. * hvc_set_winsz() - Resize the hvc tty terminal window.
  433. * @work: work structure.
  434. *
  435. * The routine shall not be called within an atomic context because it
  436. * might sleep.
  437. *
  438. * Locking: hp->lock
  439. */
  440. static void hvc_set_winsz(struct work_struct *work)
  441. {
  442. struct hvc_struct *hp;
  443. unsigned long hvc_flags;
  444. struct tty_struct *tty;
  445. struct winsize ws;
  446. hp = container_of(work, struct hvc_struct, tty_resize);
  447. tty = tty_port_tty_get(&hp->port);
  448. if (!tty)
  449. return;
  450. spin_lock_irqsave(&hp->lock, hvc_flags);
  451. ws = hp->ws;
  452. spin_unlock_irqrestore(&hp->lock, hvc_flags);
  453. tty_do_resize(tty, &ws);
  454. tty_kref_put(tty);
  455. }
  456. /*
  457. * This is actually a contract between the driver and the tty layer outlining
  458. * how much write room the driver can guarantee will be sent OR BUFFERED. This
  459. * driver MUST honor the return value.
  460. */
  461. static int hvc_write_room(struct tty_struct *tty)
  462. {
  463. struct hvc_struct *hp = tty->driver_data;
  464. if (!hp)
  465. return 0;
  466. return hp->outbuf_size - hp->n_outbuf;
  467. }
  468. static int hvc_chars_in_buffer(struct tty_struct *tty)
  469. {
  470. struct hvc_struct *hp = tty->driver_data;
  471. if (!hp)
  472. return 0;
  473. return hp->n_outbuf;
  474. }
  475. /*
  476. * timeout will vary between the MIN and MAX values defined here. By default
  477. * and during console activity we will use a default MIN_TIMEOUT of 10. When
  478. * the console is idle, we increase the timeout value on each pass through
  479. * msleep until we reach the max. This may be noticeable as a brief (average
  480. * one second) delay on the console before the console responds to input when
  481. * there has been no input for some time.
  482. */
  483. #define MIN_TIMEOUT (10)
  484. #define MAX_TIMEOUT (2000)
  485. static u32 timeout = MIN_TIMEOUT;
  486. #define HVC_POLL_READ 0x00000001
  487. #define HVC_POLL_WRITE 0x00000002
  488. int hvc_poll(struct hvc_struct *hp)
  489. {
  490. struct tty_struct *tty;
  491. int i, n, poll_mask = 0;
  492. char buf[N_INBUF] __ALIGNED__;
  493. unsigned long flags;
  494. int read_total = 0;
  495. int written_total = 0;
  496. spin_lock_irqsave(&hp->lock, flags);
  497. /* Push pending writes */
  498. if (hp->n_outbuf > 0)
  499. written_total = hvc_push(hp);
  500. /* Reschedule us if still some write pending */
  501. if (hp->n_outbuf > 0) {
  502. poll_mask |= HVC_POLL_WRITE;
  503. /* If hvc_push() was not able to write, sleep a few msecs */
  504. timeout = (written_total) ? 0 : MIN_TIMEOUT;
  505. }
  506. /* No tty attached, just skip */
  507. tty = tty_port_tty_get(&hp->port);
  508. if (tty == NULL)
  509. goto bail;
  510. /* Now check if we can get data (are we throttled ?) */
  511. if (tty_throttled(tty))
  512. goto throttled;
  513. /* If we aren't notifier driven and aren't throttled, we always
  514. * request a reschedule
  515. */
  516. if (!hp->irq_requested)
  517. poll_mask |= HVC_POLL_READ;
  518. /* Read data if any */
  519. for (;;) {
  520. int count = tty_buffer_request_room(&hp->port, N_INBUF);
  521. /* If flip is full, just reschedule a later read */
  522. if (count == 0) {
  523. poll_mask |= HVC_POLL_READ;
  524. break;
  525. }
  526. n = hp->ops->get_chars(hp->vtermno, buf, count);
  527. if (n <= 0) {
  528. /* Hangup the tty when disconnected from host */
  529. if (n == -EPIPE) {
  530. spin_unlock_irqrestore(&hp->lock, flags);
  531. tty_hangup(tty);
  532. spin_lock_irqsave(&hp->lock, flags);
  533. } else if ( n == -EAGAIN ) {
  534. /*
  535. * Some back-ends can only ensure a certain min
  536. * num of bytes read, which may be > 'count'.
  537. * Let the tty clear the flip buff to make room.
  538. */
  539. poll_mask |= HVC_POLL_READ;
  540. }
  541. break;
  542. }
  543. for (i = 0; i < n; ++i) {
  544. #ifdef CONFIG_MAGIC_SYSRQ
  545. if (hp->index == hvc_console.index) {
  546. /* Handle the SysRq Hack */
  547. /* XXX should support a sequence */
  548. if (buf[i] == '\x0f') { /* ^O */
  549. /* if ^O is pressed again, reset
  550. * sysrq_pressed and flip ^O char */
  551. sysrq_pressed = !sysrq_pressed;
  552. if (sysrq_pressed)
  553. continue;
  554. } else if (sysrq_pressed) {
  555. handle_sysrq(buf[i]);
  556. sysrq_pressed = 0;
  557. continue;
  558. }
  559. }
  560. #endif /* CONFIG_MAGIC_SYSRQ */
  561. tty_insert_flip_char(&hp->port, buf[i], 0);
  562. }
  563. read_total += n;
  564. }
  565. throttled:
  566. /* Wakeup write queue if necessary */
  567. if (hp->do_wakeup) {
  568. hp->do_wakeup = 0;
  569. tty_wakeup(tty);
  570. }
  571. bail:
  572. spin_unlock_irqrestore(&hp->lock, flags);
  573. if (read_total) {
  574. /* Activity is occurring, so reset the polling backoff value to
  575. a minimum for performance. */
  576. timeout = MIN_TIMEOUT;
  577. tty_flip_buffer_push(&hp->port);
  578. }
  579. tty_kref_put(tty);
  580. return poll_mask;
  581. }
  582. EXPORT_SYMBOL_GPL(hvc_poll);
  583. /**
  584. * __hvc_resize() - Update terminal window size information.
  585. * @hp: HVC console pointer
  586. * @ws: Terminal window size structure
  587. *
  588. * Stores the specified window size information in the hvc structure of @hp.
  589. * The function schedule the tty resize update.
  590. *
  591. * Locking: Locking free; the function MUST be called holding hp->lock
  592. */
  593. void __hvc_resize(struct hvc_struct *hp, struct winsize ws)
  594. {
  595. hp->ws = ws;
  596. schedule_work(&hp->tty_resize);
  597. }
  598. EXPORT_SYMBOL_GPL(__hvc_resize);
  599. /*
  600. * This kthread is either polling or interrupt driven. This is determined by
  601. * calling hvc_poll() who determines whether a console adapter support
  602. * interrupts.
  603. */
  604. static int khvcd(void *unused)
  605. {
  606. int poll_mask;
  607. struct hvc_struct *hp;
  608. set_freezable();
  609. do {
  610. poll_mask = 0;
  611. hvc_kicked = 0;
  612. try_to_freeze();
  613. wmb();
  614. if (!cpus_are_in_xmon()) {
  615. spin_lock(&hvc_structs_lock);
  616. list_for_each_entry(hp, &hvc_structs, next) {
  617. poll_mask |= hvc_poll(hp);
  618. }
  619. spin_unlock(&hvc_structs_lock);
  620. } else
  621. poll_mask |= HVC_POLL_READ;
  622. if (hvc_kicked)
  623. continue;
  624. set_current_state(TASK_INTERRUPTIBLE);
  625. if (!hvc_kicked) {
  626. if (poll_mask == 0)
  627. schedule();
  628. else {
  629. unsigned long j_timeout;
  630. if (timeout < MAX_TIMEOUT)
  631. timeout += (timeout >> 6) + 1;
  632. /*
  633. * We don't use msleep_interruptible otherwise
  634. * "kick" will fail to wake us up
  635. */
  636. j_timeout = msecs_to_jiffies(timeout) + 1;
  637. schedule_timeout_interruptible(j_timeout);
  638. }
  639. }
  640. __set_current_state(TASK_RUNNING);
  641. } while (!kthread_should_stop());
  642. return 0;
  643. }
  644. static int hvc_tiocmget(struct tty_struct *tty)
  645. {
  646. struct hvc_struct *hp = tty->driver_data;
  647. if (!hp || !hp->ops->tiocmget)
  648. return -EINVAL;
  649. return hp->ops->tiocmget(hp);
  650. }
  651. static int hvc_tiocmset(struct tty_struct *tty,
  652. unsigned int set, unsigned int clear)
  653. {
  654. struct hvc_struct *hp = tty->driver_data;
  655. if (!hp || !hp->ops->tiocmset)
  656. return -EINVAL;
  657. return hp->ops->tiocmset(hp, set, clear);
  658. }
  659. #ifdef CONFIG_CONSOLE_POLL
  660. static int hvc_poll_init(struct tty_driver *driver, int line, char *options)
  661. {
  662. return 0;
  663. }
  664. static int hvc_poll_get_char(struct tty_driver *driver, int line)
  665. {
  666. struct tty_struct *tty = driver->ttys[0];
  667. struct hvc_struct *hp = tty->driver_data;
  668. int n;
  669. char ch;
  670. n = hp->ops->get_chars(hp->vtermno, &ch, 1);
  671. if (n <= 0)
  672. return NO_POLL_CHAR;
  673. return ch;
  674. }
  675. static void hvc_poll_put_char(struct tty_driver *driver, int line, char ch)
  676. {
  677. struct tty_struct *tty = driver->ttys[0];
  678. struct hvc_struct *hp = tty->driver_data;
  679. int n;
  680. do {
  681. n = hp->ops->put_chars(hp->vtermno, &ch, 1);
  682. } while (n <= 0);
  683. }
  684. #endif
  685. static const struct tty_operations hvc_ops = {
  686. .install = hvc_install,
  687. .open = hvc_open,
  688. .close = hvc_close,
  689. .cleanup = hvc_cleanup,
  690. .write = hvc_write,
  691. .hangup = hvc_hangup,
  692. .unthrottle = hvc_unthrottle,
  693. .write_room = hvc_write_room,
  694. .chars_in_buffer = hvc_chars_in_buffer,
  695. .tiocmget = hvc_tiocmget,
  696. .tiocmset = hvc_tiocmset,
  697. #ifdef CONFIG_CONSOLE_POLL
  698. .poll_init = hvc_poll_init,
  699. .poll_get_char = hvc_poll_get_char,
  700. .poll_put_char = hvc_poll_put_char,
  701. #endif
  702. };
  703. static const struct tty_port_operations hvc_port_ops = {
  704. .destruct = hvc_port_destruct,
  705. };
  706. struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
  707. const struct hv_ops *ops,
  708. int outbuf_size)
  709. {
  710. struct hvc_struct *hp;
  711. int i;
  712. /* We wait until a driver actually comes along */
  713. if (atomic_inc_not_zero(&hvc_needs_init)) {
  714. int err = hvc_init();
  715. if (err)
  716. return ERR_PTR(err);
  717. }
  718. hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
  719. GFP_KERNEL);
  720. if (!hp)
  721. return ERR_PTR(-ENOMEM);
  722. hp->vtermno = vtermno;
  723. hp->data = data;
  724. hp->ops = ops;
  725. hp->outbuf_size = outbuf_size;
  726. hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
  727. tty_port_init(&hp->port);
  728. hp->port.ops = &hvc_port_ops;
  729. INIT_WORK(&hp->tty_resize, hvc_set_winsz);
  730. spin_lock_init(&hp->lock);
  731. spin_lock(&hvc_structs_lock);
  732. /*
  733. * find index to use:
  734. * see if this vterm id matches one registered for console.
  735. */
  736. for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
  737. if (vtermnos[i] == hp->vtermno &&
  738. cons_ops[i] == hp->ops)
  739. break;
  740. /* no matching slot, just use a counter */
  741. if (i >= MAX_NR_HVC_CONSOLES)
  742. i = ++last_hvc;
  743. hp->index = i;
  744. cons_ops[i] = ops;
  745. vtermnos[i] = vtermno;
  746. list_add_tail(&(hp->next), &hvc_structs);
  747. spin_unlock(&hvc_structs_lock);
  748. /* check if we need to re-register the kernel console */
  749. hvc_check_console(i);
  750. return hp;
  751. }
  752. EXPORT_SYMBOL_GPL(hvc_alloc);
  753. int hvc_remove(struct hvc_struct *hp)
  754. {
  755. unsigned long flags;
  756. struct tty_struct *tty;
  757. tty = tty_port_tty_get(&hp->port);
  758. console_lock();
  759. spin_lock_irqsave(&hp->lock, flags);
  760. if (hp->index < MAX_NR_HVC_CONSOLES) {
  761. vtermnos[hp->index] = -1;
  762. cons_ops[hp->index] = NULL;
  763. }
  764. /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
  765. spin_unlock_irqrestore(&hp->lock, flags);
  766. console_unlock();
  767. /*
  768. * We 'put' the instance that was grabbed when the kref instance
  769. * was initialized using kref_init(). Let the last holder of this
  770. * kref cause it to be removed, which will probably be the tty_vhangup
  771. * below.
  772. */
  773. tty_port_put(&hp->port);
  774. /*
  775. * This function call will auto chain call hvc_hangup.
  776. */
  777. if (tty) {
  778. tty_vhangup(tty);
  779. tty_kref_put(tty);
  780. }
  781. return 0;
  782. }
  783. EXPORT_SYMBOL_GPL(hvc_remove);
  784. /* Driver initialization: called as soon as someone uses hvc_alloc(). */
  785. static int hvc_init(void)
  786. {
  787. struct tty_driver *drv;
  788. int err;
  789. /* We need more than hvc_count adapters due to hotplug additions. */
  790. drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
  791. if (!drv) {
  792. err = -ENOMEM;
  793. goto out;
  794. }
  795. drv->driver_name = "hvc";
  796. drv->name = "hvc";
  797. drv->major = HVC_MAJOR;
  798. drv->minor_start = HVC_MINOR;
  799. drv->type = TTY_DRIVER_TYPE_SYSTEM;
  800. drv->init_termios = tty_std_termios;
  801. drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  802. tty_set_operations(drv, &hvc_ops);
  803. /* Always start the kthread because there can be hotplug vty adapters
  804. * added later. */
  805. hvc_task = kthread_run(khvcd, NULL, "khvcd");
  806. if (IS_ERR(hvc_task)) {
  807. printk(KERN_ERR "Couldn't create kthread for console.\n");
  808. err = PTR_ERR(hvc_task);
  809. goto put_tty;
  810. }
  811. err = tty_register_driver(drv);
  812. if (err) {
  813. printk(KERN_ERR "Couldn't register hvc console driver\n");
  814. goto stop_thread;
  815. }
  816. /*
  817. * Make sure tty is fully registered before allowing it to be
  818. * found by hvc_console_device.
  819. */
  820. smp_mb();
  821. hvc_driver = drv;
  822. return 0;
  823. stop_thread:
  824. kthread_stop(hvc_task);
  825. hvc_task = NULL;
  826. put_tty:
  827. put_tty_driver(drv);
  828. out:
  829. return err;
  830. }