hvc_console.c 22 KB

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