hvc_console.c 22 KB

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