hvc_console.c 24 KB

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