hvc_console.c 23 KB

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