hvc_console.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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/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. if (!(hp = hvc_get_by_index(tty->index)))
  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_from_close(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 (test_bit(TTY_THROTTLED, &tty->flags))
  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. spin_lock_irqsave(&hp->lock, flags);
  772. if (hp->index < MAX_NR_HVC_CONSOLES) {
  773. console_lock();
  774. vtermnos[hp->index] = -1;
  775. cons_ops[hp->index] = NULL;
  776. console_unlock();
  777. }
  778. /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
  779. spin_unlock_irqrestore(&hp->lock, flags);
  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. }
  844. /* This isn't particularly necessary due to this being a console driver
  845. * but it is nice to be thorough.
  846. */
  847. static void __exit hvc_exit(void)
  848. {
  849. if (hvc_driver) {
  850. kthread_stop(hvc_task);
  851. tty_unregister_driver(hvc_driver);
  852. /* return tty_struct instances allocated in hvc_init(). */
  853. put_tty_driver(hvc_driver);
  854. unregister_console(&hvc_console);
  855. }
  856. }
  857. module_exit(hvc_exit);