hvc_console.c 23 KB

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