hvc_console.c 23 KB

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