hvc_console.c 24 KB

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