hvc_console.c 24 KB

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