hvc_console.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. } else
  307. /* We are ready... raise DTR/RTS */
  308. if (C_BAUD(tty))
  309. if (hp->ops->dtr_rts)
  310. hp->ops->dtr_rts(hp, 1);
  311. /* Force wakeup of the polling thread */
  312. hvc_kick();
  313. return rc;
  314. }
  315. static void hvc_close(struct tty_struct *tty, struct file * filp)
  316. {
  317. struct hvc_struct *hp;
  318. unsigned long flags;
  319. if (tty_hung_up_p(filp))
  320. return;
  321. /*
  322. * No driver_data means that this close was issued after a failed
  323. * hvc_open by the tty layer's release_dev() function and we can just
  324. * exit cleanly because the kref reference wasn't made.
  325. */
  326. if (!tty->driver_data)
  327. return;
  328. hp = tty->driver_data;
  329. spin_lock_irqsave(&hp->port.lock, flags);
  330. if (--hp->port.count == 0) {
  331. spin_unlock_irqrestore(&hp->port.lock, flags);
  332. /* We are done with the tty pointer now. */
  333. tty_port_tty_set(&hp->port, NULL);
  334. if (C_HUPCL(tty))
  335. if (hp->ops->dtr_rts)
  336. hp->ops->dtr_rts(hp, 0);
  337. if (hp->ops->notifier_del)
  338. hp->ops->notifier_del(hp, hp->data);
  339. /* cancel pending tty resize work */
  340. cancel_work_sync(&hp->tty_resize);
  341. /*
  342. * Chain calls chars_in_buffer() and returns immediately if
  343. * there is no buffered data otherwise sleeps on a wait queue
  344. * waking periodically to check chars_in_buffer().
  345. */
  346. tty_wait_until_sent_from_close(tty, HVC_CLOSE_WAIT);
  347. } else {
  348. if (hp->port.count < 0)
  349. printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
  350. hp->vtermno, hp->port.count);
  351. spin_unlock_irqrestore(&hp->port.lock, flags);
  352. }
  353. }
  354. static void hvc_cleanup(struct tty_struct *tty)
  355. {
  356. struct hvc_struct *hp = tty->driver_data;
  357. tty_port_put(&hp->port);
  358. }
  359. static void hvc_hangup(struct tty_struct *tty)
  360. {
  361. struct hvc_struct *hp = tty->driver_data;
  362. unsigned long flags;
  363. if (!hp)
  364. return;
  365. /* cancel pending tty resize work */
  366. cancel_work_sync(&hp->tty_resize);
  367. spin_lock_irqsave(&hp->port.lock, flags);
  368. /*
  369. * The N_TTY line discipline has problems such that in a close vs
  370. * open->hangup case this can be called after the final close so prevent
  371. * that from happening for now.
  372. */
  373. if (hp->port.count <= 0) {
  374. spin_unlock_irqrestore(&hp->port.lock, flags);
  375. return;
  376. }
  377. hp->port.count = 0;
  378. spin_unlock_irqrestore(&hp->port.lock, flags);
  379. tty_port_tty_set(&hp->port, NULL);
  380. hp->n_outbuf = 0;
  381. if (hp->ops->notifier_hangup)
  382. hp->ops->notifier_hangup(hp, hp->data);
  383. }
  384. /*
  385. * Push buffered characters whether they were just recently buffered or waiting
  386. * on a blocked hypervisor. Call this function with hp->lock held.
  387. */
  388. static int hvc_push(struct hvc_struct *hp)
  389. {
  390. int n;
  391. n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
  392. if (n <= 0) {
  393. if (n == 0 || n == -EAGAIN) {
  394. hp->do_wakeup = 1;
  395. return 0;
  396. }
  397. /* throw away output on error; this happens when
  398. there is no session connected to the vterm. */
  399. hp->n_outbuf = 0;
  400. } else
  401. hp->n_outbuf -= n;
  402. if (hp->n_outbuf > 0)
  403. memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
  404. else
  405. hp->do_wakeup = 1;
  406. return n;
  407. }
  408. static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
  409. {
  410. struct hvc_struct *hp = tty->driver_data;
  411. unsigned long flags;
  412. int rsize, written = 0;
  413. /* This write was probably executed during a tty close. */
  414. if (!hp)
  415. return -EPIPE;
  416. /* FIXME what's this (unprotected) check for? */
  417. if (hp->port.count <= 0)
  418. return -EIO;
  419. spin_lock_irqsave(&hp->lock, flags);
  420. /* Push pending writes */
  421. if (hp->n_outbuf > 0)
  422. hvc_push(hp);
  423. while (count > 0 && (rsize = hp->outbuf_size - hp->n_outbuf) > 0) {
  424. if (rsize > count)
  425. rsize = count;
  426. memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
  427. count -= rsize;
  428. buf += rsize;
  429. hp->n_outbuf += rsize;
  430. written += rsize;
  431. hvc_push(hp);
  432. }
  433. spin_unlock_irqrestore(&hp->lock, flags);
  434. /*
  435. * Racy, but harmless, kick thread if there is still pending data.
  436. */
  437. if (hp->n_outbuf)
  438. hvc_kick();
  439. return written;
  440. }
  441. /**
  442. * hvc_set_winsz() - Resize the hvc tty terminal window.
  443. * @work: work structure.
  444. *
  445. * The routine shall not be called within an atomic context because it
  446. * might sleep.
  447. *
  448. * Locking: hp->lock
  449. */
  450. static void hvc_set_winsz(struct work_struct *work)
  451. {
  452. struct hvc_struct *hp;
  453. unsigned long hvc_flags;
  454. struct tty_struct *tty;
  455. struct winsize ws;
  456. hp = container_of(work, struct hvc_struct, tty_resize);
  457. tty = tty_port_tty_get(&hp->port);
  458. if (!tty)
  459. return;
  460. spin_lock_irqsave(&hp->lock, hvc_flags);
  461. ws = hp->ws;
  462. spin_unlock_irqrestore(&hp->lock, hvc_flags);
  463. tty_do_resize(tty, &ws);
  464. tty_kref_put(tty);
  465. }
  466. /*
  467. * This is actually a contract between the driver and the tty layer outlining
  468. * how much write room the driver can guarantee will be sent OR BUFFERED. This
  469. * driver MUST honor the return value.
  470. */
  471. static int hvc_write_room(struct tty_struct *tty)
  472. {
  473. struct hvc_struct *hp = tty->driver_data;
  474. if (!hp)
  475. return 0;
  476. return hp->outbuf_size - hp->n_outbuf;
  477. }
  478. static int hvc_chars_in_buffer(struct tty_struct *tty)
  479. {
  480. struct hvc_struct *hp = tty->driver_data;
  481. if (!hp)
  482. return 0;
  483. return hp->n_outbuf;
  484. }
  485. /*
  486. * timeout will vary between the MIN and MAX values defined here. By default
  487. * and during console activity we will use a default MIN_TIMEOUT of 10. When
  488. * the console is idle, we increase the timeout value on each pass through
  489. * msleep until we reach the max. This may be noticeable as a brief (average
  490. * one second) delay on the console before the console responds to input when
  491. * there has been no input for some time.
  492. */
  493. #define MIN_TIMEOUT (10)
  494. #define MAX_TIMEOUT (2000)
  495. static u32 timeout = MIN_TIMEOUT;
  496. #define HVC_POLL_READ 0x00000001
  497. #define HVC_POLL_WRITE 0x00000002
  498. int hvc_poll(struct hvc_struct *hp)
  499. {
  500. struct tty_struct *tty;
  501. int i, n, poll_mask = 0;
  502. char buf[N_INBUF] __ALIGNED__;
  503. unsigned long flags;
  504. int read_total = 0;
  505. int written_total = 0;
  506. spin_lock_irqsave(&hp->lock, flags);
  507. /* Push pending writes */
  508. if (hp->n_outbuf > 0)
  509. written_total = hvc_push(hp);
  510. /* Reschedule us if still some write pending */
  511. if (hp->n_outbuf > 0) {
  512. poll_mask |= HVC_POLL_WRITE;
  513. /* If hvc_push() was not able to write, sleep a few msecs */
  514. timeout = (written_total) ? 0 : MIN_TIMEOUT;
  515. }
  516. /* No tty attached, just skip */
  517. tty = tty_port_tty_get(&hp->port);
  518. if (tty == NULL)
  519. goto bail;
  520. /* Now check if we can get data (are we throttled ?) */
  521. if (test_bit(TTY_THROTTLED, &tty->flags))
  522. goto throttled;
  523. /* If we aren't notifier driven and aren't throttled, we always
  524. * request a reschedule
  525. */
  526. if (!hp->irq_requested)
  527. poll_mask |= HVC_POLL_READ;
  528. /* Read data if any */
  529. for (;;) {
  530. int count = tty_buffer_request_room(&hp->port, N_INBUF);
  531. /* If flip is full, just reschedule a later read */
  532. if (count == 0) {
  533. poll_mask |= HVC_POLL_READ;
  534. break;
  535. }
  536. n = hp->ops->get_chars(hp->vtermno, buf, count);
  537. if (n <= 0) {
  538. /* Hangup the tty when disconnected from host */
  539. if (n == -EPIPE) {
  540. spin_unlock_irqrestore(&hp->lock, flags);
  541. tty_hangup(tty);
  542. spin_lock_irqsave(&hp->lock, flags);
  543. } else if ( n == -EAGAIN ) {
  544. /*
  545. * Some back-ends can only ensure a certain min
  546. * num of bytes read, which may be > 'count'.
  547. * Let the tty clear the flip buff to make room.
  548. */
  549. poll_mask |= HVC_POLL_READ;
  550. }
  551. break;
  552. }
  553. for (i = 0; i < n; ++i) {
  554. #ifdef CONFIG_MAGIC_SYSRQ
  555. if (hp->index == hvc_console.index) {
  556. /* Handle the SysRq Hack */
  557. /* XXX should support a sequence */
  558. if (buf[i] == '\x0f') { /* ^O */
  559. /* if ^O is pressed again, reset
  560. * sysrq_pressed and flip ^O char */
  561. sysrq_pressed = !sysrq_pressed;
  562. if (sysrq_pressed)
  563. continue;
  564. } else if (sysrq_pressed) {
  565. handle_sysrq(buf[i]);
  566. sysrq_pressed = 0;
  567. continue;
  568. }
  569. }
  570. #endif /* CONFIG_MAGIC_SYSRQ */
  571. tty_insert_flip_char(&hp->port, buf[i], 0);
  572. }
  573. read_total += n;
  574. }
  575. throttled:
  576. /* Wakeup write queue if necessary */
  577. if (hp->do_wakeup) {
  578. hp->do_wakeup = 0;
  579. tty_wakeup(tty);
  580. }
  581. bail:
  582. spin_unlock_irqrestore(&hp->lock, flags);
  583. if (read_total) {
  584. /* Activity is occurring, so reset the polling backoff value to
  585. a minimum for performance. */
  586. timeout = MIN_TIMEOUT;
  587. tty_flip_buffer_push(&hp->port);
  588. }
  589. tty_kref_put(tty);
  590. return poll_mask;
  591. }
  592. EXPORT_SYMBOL_GPL(hvc_poll);
  593. /**
  594. * __hvc_resize() - Update terminal window size information.
  595. * @hp: HVC console pointer
  596. * @ws: Terminal window size structure
  597. *
  598. * Stores the specified window size information in the hvc structure of @hp.
  599. * The function schedule the tty resize update.
  600. *
  601. * Locking: Locking free; the function MUST be called holding hp->lock
  602. */
  603. void __hvc_resize(struct hvc_struct *hp, struct winsize ws)
  604. {
  605. hp->ws = ws;
  606. schedule_work(&hp->tty_resize);
  607. }
  608. EXPORT_SYMBOL_GPL(__hvc_resize);
  609. /*
  610. * This kthread is either polling or interrupt driven. This is determined by
  611. * calling hvc_poll() who determines whether a console adapter support
  612. * interrupts.
  613. */
  614. static int khvcd(void *unused)
  615. {
  616. int poll_mask;
  617. struct hvc_struct *hp;
  618. set_freezable();
  619. do {
  620. poll_mask = 0;
  621. hvc_kicked = 0;
  622. try_to_freeze();
  623. wmb();
  624. if (!cpus_are_in_xmon()) {
  625. spin_lock(&hvc_structs_lock);
  626. list_for_each_entry(hp, &hvc_structs, next) {
  627. poll_mask |= hvc_poll(hp);
  628. }
  629. spin_unlock(&hvc_structs_lock);
  630. } else
  631. poll_mask |= HVC_POLL_READ;
  632. if (hvc_kicked)
  633. continue;
  634. set_current_state(TASK_INTERRUPTIBLE);
  635. if (!hvc_kicked) {
  636. if (poll_mask == 0)
  637. schedule();
  638. else {
  639. if (timeout < MAX_TIMEOUT)
  640. timeout += (timeout >> 6) + 1;
  641. msleep_interruptible(timeout);
  642. }
  643. }
  644. __set_current_state(TASK_RUNNING);
  645. } while (!kthread_should_stop());
  646. return 0;
  647. }
  648. static int hvc_tiocmget(struct tty_struct *tty)
  649. {
  650. struct hvc_struct *hp = tty->driver_data;
  651. if (!hp || !hp->ops->tiocmget)
  652. return -EINVAL;
  653. return hp->ops->tiocmget(hp);
  654. }
  655. static int hvc_tiocmset(struct tty_struct *tty,
  656. unsigned int set, unsigned int clear)
  657. {
  658. struct hvc_struct *hp = tty->driver_data;
  659. if (!hp || !hp->ops->tiocmset)
  660. return -EINVAL;
  661. return hp->ops->tiocmset(hp, set, clear);
  662. }
  663. #ifdef CONFIG_CONSOLE_POLL
  664. int hvc_poll_init(struct tty_driver *driver, int line, char *options)
  665. {
  666. return 0;
  667. }
  668. static int hvc_poll_get_char(struct tty_driver *driver, int line)
  669. {
  670. struct tty_struct *tty = driver->ttys[0];
  671. struct hvc_struct *hp = tty->driver_data;
  672. int n;
  673. char ch;
  674. n = hp->ops->get_chars(hp->vtermno, &ch, 1);
  675. if (n == 0)
  676. return NO_POLL_CHAR;
  677. return ch;
  678. }
  679. static void hvc_poll_put_char(struct tty_driver *driver, int line, char ch)
  680. {
  681. struct tty_struct *tty = driver->ttys[0];
  682. struct hvc_struct *hp = tty->driver_data;
  683. int n;
  684. do {
  685. n = hp->ops->put_chars(hp->vtermno, &ch, 1);
  686. } while (n <= 0);
  687. }
  688. #endif
  689. static const struct tty_operations hvc_ops = {
  690. .install = hvc_install,
  691. .open = hvc_open,
  692. .close = hvc_close,
  693. .cleanup = hvc_cleanup,
  694. .write = hvc_write,
  695. .hangup = hvc_hangup,
  696. .unthrottle = hvc_unthrottle,
  697. .write_room = hvc_write_room,
  698. .chars_in_buffer = hvc_chars_in_buffer,
  699. .tiocmget = hvc_tiocmget,
  700. .tiocmset = hvc_tiocmset,
  701. #ifdef CONFIG_CONSOLE_POLL
  702. .poll_init = hvc_poll_init,
  703. .poll_get_char = hvc_poll_get_char,
  704. .poll_put_char = hvc_poll_put_char,
  705. #endif
  706. };
  707. static const struct tty_port_operations hvc_port_ops = {
  708. .destruct = hvc_port_destruct,
  709. };
  710. struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
  711. const struct hv_ops *ops,
  712. int outbuf_size)
  713. {
  714. struct hvc_struct *hp;
  715. int i;
  716. /* We wait until a driver actually comes along */
  717. if (!hvc_driver) {
  718. int err = hvc_init();
  719. if (err)
  720. return ERR_PTR(err);
  721. }
  722. hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
  723. GFP_KERNEL);
  724. if (!hp)
  725. return ERR_PTR(-ENOMEM);
  726. hp->vtermno = vtermno;
  727. hp->data = data;
  728. hp->ops = ops;
  729. hp->outbuf_size = outbuf_size;
  730. hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
  731. tty_port_init(&hp->port);
  732. hp->port.ops = &hvc_port_ops;
  733. INIT_WORK(&hp->tty_resize, hvc_set_winsz);
  734. spin_lock_init(&hp->lock);
  735. spin_lock(&hvc_structs_lock);
  736. /*
  737. * find index to use:
  738. * see if this vterm id matches one registered for console.
  739. */
  740. for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
  741. if (vtermnos[i] == hp->vtermno &&
  742. cons_ops[i] == hp->ops)
  743. break;
  744. /* no matching slot, just use a counter */
  745. if (i >= MAX_NR_HVC_CONSOLES)
  746. i = ++last_hvc;
  747. hp->index = i;
  748. cons_ops[i] = ops;
  749. vtermnos[i] = vtermno;
  750. list_add_tail(&(hp->next), &hvc_structs);
  751. spin_unlock(&hvc_structs_lock);
  752. /* check if we need to re-register the kernel console */
  753. hvc_check_console(i);
  754. return hp;
  755. }
  756. EXPORT_SYMBOL_GPL(hvc_alloc);
  757. int hvc_remove(struct hvc_struct *hp)
  758. {
  759. unsigned long flags;
  760. struct tty_struct *tty;
  761. tty = tty_port_tty_get(&hp->port);
  762. spin_lock_irqsave(&hp->lock, flags);
  763. if (hp->index < MAX_NR_HVC_CONSOLES) {
  764. console_lock();
  765. vtermnos[hp->index] = -1;
  766. cons_ops[hp->index] = NULL;
  767. console_unlock();
  768. }
  769. /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
  770. spin_unlock_irqrestore(&hp->lock, flags);
  771. /*
  772. * We 'put' the instance that was grabbed when the kref instance
  773. * was initialized using kref_init(). Let the last holder of this
  774. * kref cause it to be removed, which will probably be the tty_vhangup
  775. * below.
  776. */
  777. tty_port_put(&hp->port);
  778. /*
  779. * This function call will auto chain call hvc_hangup.
  780. */
  781. if (tty) {
  782. tty_vhangup(tty);
  783. tty_kref_put(tty);
  784. }
  785. return 0;
  786. }
  787. EXPORT_SYMBOL_GPL(hvc_remove);
  788. /* Driver initialization: called as soon as someone uses hvc_alloc(). */
  789. static int hvc_init(void)
  790. {
  791. struct tty_driver *drv;
  792. int err;
  793. /* We need more than hvc_count adapters due to hotplug additions. */
  794. drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
  795. if (!drv) {
  796. err = -ENOMEM;
  797. goto out;
  798. }
  799. drv->driver_name = "hvc";
  800. drv->name = "hvc";
  801. drv->major = HVC_MAJOR;
  802. drv->minor_start = HVC_MINOR;
  803. drv->type = TTY_DRIVER_TYPE_SYSTEM;
  804. drv->init_termios = tty_std_termios;
  805. drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  806. tty_set_operations(drv, &hvc_ops);
  807. /* Always start the kthread because there can be hotplug vty adapters
  808. * added later. */
  809. hvc_task = kthread_run(khvcd, NULL, "khvcd");
  810. if (IS_ERR(hvc_task)) {
  811. printk(KERN_ERR "Couldn't create kthread for console.\n");
  812. err = PTR_ERR(hvc_task);
  813. goto put_tty;
  814. }
  815. err = tty_register_driver(drv);
  816. if (err) {
  817. printk(KERN_ERR "Couldn't register hvc console driver\n");
  818. goto stop_thread;
  819. }
  820. /*
  821. * Make sure tty is fully registered before allowing it to be
  822. * found by hvc_console_device.
  823. */
  824. smp_mb();
  825. hvc_driver = drv;
  826. return 0;
  827. stop_thread:
  828. kthread_stop(hvc_task);
  829. hvc_task = NULL;
  830. put_tty:
  831. put_tty_driver(drv);
  832. out:
  833. return err;
  834. }
  835. /* This isn't particularly necessary due to this being a console driver
  836. * but it is nice to be thorough.
  837. */
  838. static void __exit hvc_exit(void)
  839. {
  840. if (hvc_driver) {
  841. kthread_stop(hvc_task);
  842. tty_unregister_driver(hvc_driver);
  843. /* return tty_struct instances allocated in hvc_init(). */
  844. put_tty_driver(hvc_driver);
  845. unregister_console(&hvc_console);
  846. }
  847. }
  848. module_exit(hvc_exit);