hvc_console.c 22 KB

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