hvc_console.c 24 KB

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