hvc_console.c 24 KB

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