hvc_console.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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/kref.h>
  30. #include <linux/kthread.h>
  31. #include <linux/list.h>
  32. #include <linux/module.h>
  33. #include <linux/major.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 <asm/uaccess.h>
  42. #include "hvc_console.h"
  43. #define HVC_MAJOR 229
  44. #define HVC_MINOR 0
  45. /*
  46. * Wait this long per iteration while trying to push buffered data to the
  47. * hypervisor before allowing the tty to complete a close operation.
  48. */
  49. #define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
  50. /*
  51. * These sizes are most efficient for vio, because they are the
  52. * native transfer size. We could make them selectable in the
  53. * future to better deal with backends that want other buffer sizes.
  54. */
  55. #define N_OUTBUF 16
  56. #define N_INBUF 16
  57. #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
  58. static struct tty_driver *hvc_driver;
  59. static struct task_struct *hvc_task;
  60. /* Picks up late kicks after list walk but before schedule() */
  61. static int hvc_kicked;
  62. static int hvc_init(void);
  63. #ifdef CONFIG_MAGIC_SYSRQ
  64. static int sysrq_pressed;
  65. #endif
  66. struct hvc_struct {
  67. spinlock_t lock;
  68. int index;
  69. struct tty_struct *tty;
  70. unsigned int count;
  71. int do_wakeup;
  72. char *outbuf;
  73. int outbuf_size;
  74. int n_outbuf;
  75. uint32_t vtermno;
  76. struct hv_ops *ops;
  77. int irq_requested;
  78. int irq;
  79. struct list_head next;
  80. struct kref kref; /* ref count & hvc_struct lifetime */
  81. };
  82. /* dynamic list of hvc_struct instances */
  83. static struct list_head hvc_structs = LIST_HEAD_INIT(hvc_structs);
  84. /*
  85. * Protect the list of hvc_struct instances from inserts and removals during
  86. * list traversal.
  87. */
  88. static DEFINE_SPINLOCK(hvc_structs_lock);
  89. /*
  90. * This value is used to assign a tty->index value to a hvc_struct based
  91. * upon order of exposure via hvc_probe(), when we can not match it to
  92. * a console candidate registered with hvc_instantiate().
  93. */
  94. static int last_hvc = -1;
  95. /*
  96. * Do not call this function with either the hvc_structs_lock or the hvc_struct
  97. * lock held. If successful, this function increments the kref reference
  98. * count against the target hvc_struct so it should be released when finished.
  99. */
  100. static struct hvc_struct *hvc_get_by_index(int index)
  101. {
  102. struct hvc_struct *hp;
  103. unsigned long flags;
  104. spin_lock(&hvc_structs_lock);
  105. list_for_each_entry(hp, &hvc_structs, next) {
  106. spin_lock_irqsave(&hp->lock, flags);
  107. if (hp->index == index) {
  108. kref_get(&hp->kref);
  109. spin_unlock_irqrestore(&hp->lock, flags);
  110. spin_unlock(&hvc_structs_lock);
  111. return hp;
  112. }
  113. spin_unlock_irqrestore(&hp->lock, flags);
  114. }
  115. hp = NULL;
  116. spin_unlock(&hvc_structs_lock);
  117. return hp;
  118. }
  119. /*
  120. * Initial console vtermnos for console API usage prior to full console
  121. * initialization. Any vty adapter outside this range will not have usable
  122. * console interfaces but can still be used as a tty device. This has to be
  123. * static because kmalloc will not work during early console init.
  124. */
  125. static struct hv_ops *cons_ops[MAX_NR_HVC_CONSOLES];
  126. static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] =
  127. {[0 ... MAX_NR_HVC_CONSOLES - 1] = -1};
  128. /*
  129. * Console APIs, NOT TTY. These APIs are available immediately when
  130. * hvc_console_setup() finds adapters.
  131. */
  132. static void hvc_console_print(struct console *co, const char *b,
  133. unsigned count)
  134. {
  135. char c[N_OUTBUF] __ALIGNED__;
  136. unsigned i = 0, n = 0;
  137. int r, donecr = 0, index = co->index;
  138. /* Console access attempt outside of acceptable console range. */
  139. if (index >= MAX_NR_HVC_CONSOLES)
  140. return;
  141. /* This console adapter was removed so it is not usable. */
  142. if (vtermnos[index] < 0)
  143. return;
  144. while (count > 0 || i > 0) {
  145. if (count > 0 && i < sizeof(c)) {
  146. if (b[n] == '\n' && !donecr) {
  147. c[i++] = '\r';
  148. donecr = 1;
  149. } else {
  150. c[i++] = b[n++];
  151. donecr = 0;
  152. --count;
  153. }
  154. } else {
  155. r = cons_ops[index]->put_chars(vtermnos[index], c, i);
  156. if (r < 0) {
  157. /* throw away chars on error */
  158. i = 0;
  159. } else if (r > 0) {
  160. i -= r;
  161. if (i > 0)
  162. memmove(c, c+r, i);
  163. }
  164. }
  165. }
  166. }
  167. static struct tty_driver *hvc_console_device(struct console *c, int *index)
  168. {
  169. if (vtermnos[c->index] == -1)
  170. return NULL;
  171. *index = c->index;
  172. return hvc_driver;
  173. }
  174. static int __init hvc_console_setup(struct console *co, char *options)
  175. {
  176. if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES)
  177. return -ENODEV;
  178. if (vtermnos[co->index] == -1)
  179. return -ENODEV;
  180. return 0;
  181. }
  182. static struct console hvc_con_driver = {
  183. .name = "hvc",
  184. .write = hvc_console_print,
  185. .device = hvc_console_device,
  186. .setup = hvc_console_setup,
  187. .flags = CON_PRINTBUFFER,
  188. .index = -1,
  189. };
  190. /*
  191. * Early console initialization. Precedes driver initialization.
  192. *
  193. * (1) we are first, and the user specified another driver
  194. * -- index will remain -1
  195. * (2) we are first and the user specified no driver
  196. * -- index will be set to 0, then we will fail setup.
  197. * (3) we are first and the user specified our driver
  198. * -- index will be set to user specified driver, and we will fail
  199. * (4) we are after driver, and this initcall will register us
  200. * -- if the user didn't specify a driver then the console will match
  201. *
  202. * Note that for cases 2 and 3, we will match later when the io driver
  203. * calls hvc_instantiate() and call register again.
  204. */
  205. static int __init hvc_console_init(void)
  206. {
  207. register_console(&hvc_con_driver);
  208. return 0;
  209. }
  210. console_initcall(hvc_console_init);
  211. /* callback when the kboject ref count reaches zero. */
  212. static void destroy_hvc_struct(struct kref *kref)
  213. {
  214. struct hvc_struct *hp = container_of(kref, struct hvc_struct, kref);
  215. unsigned long flags;
  216. spin_lock(&hvc_structs_lock);
  217. spin_lock_irqsave(&hp->lock, flags);
  218. list_del(&(hp->next));
  219. spin_unlock_irqrestore(&hp->lock, flags);
  220. spin_unlock(&hvc_structs_lock);
  221. kfree(hp);
  222. }
  223. /*
  224. * hvc_instantiate() is an early console discovery method which locates
  225. * consoles * prior to the vio subsystem discovering them. Hotplugged
  226. * vty adapters do NOT get an hvc_instantiate() callback since they
  227. * appear after early console init.
  228. */
  229. int hvc_instantiate(uint32_t vtermno, int index, struct hv_ops *ops)
  230. {
  231. struct hvc_struct *hp;
  232. if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
  233. return -1;
  234. if (vtermnos[index] != -1)
  235. return -1;
  236. /* make sure no no tty has been registered in this index */
  237. hp = hvc_get_by_index(index);
  238. if (hp) {
  239. kref_put(&hp->kref, destroy_hvc_struct);
  240. return -1;
  241. }
  242. vtermnos[index] = vtermno;
  243. cons_ops[index] = ops;
  244. /* reserve all indices up to and including this index */
  245. if (last_hvc < index)
  246. last_hvc = index;
  247. /* if this index is what the user requested, then register
  248. * now (setup won't fail at this point). It's ok to just
  249. * call register again if previously .setup failed.
  250. */
  251. if (index == hvc_con_driver.index)
  252. register_console(&hvc_con_driver);
  253. return 0;
  254. }
  255. /* Wake the sleeping khvcd */
  256. static void hvc_kick(void)
  257. {
  258. hvc_kicked = 1;
  259. wake_up_process(hvc_task);
  260. }
  261. static int hvc_poll(struct hvc_struct *hp);
  262. /*
  263. * NOTE: This API isn't used if the console adapter doesn't support interrupts.
  264. * In this case the console is poll driven.
  265. */
  266. static irqreturn_t hvc_handle_interrupt(int irq, void *dev_instance)
  267. {
  268. /* if hvc_poll request a repoll, then kick the hvcd thread */
  269. if (hvc_poll(dev_instance))
  270. hvc_kick();
  271. return IRQ_HANDLED;
  272. }
  273. static void hvc_unthrottle(struct tty_struct *tty)
  274. {
  275. hvc_kick();
  276. }
  277. /*
  278. * The TTY interface won't be used until after the vio layer has exposed the vty
  279. * adapter to the kernel.
  280. */
  281. static int hvc_open(struct tty_struct *tty, struct file * filp)
  282. {
  283. struct hvc_struct *hp;
  284. unsigned long flags;
  285. int irq = 0;
  286. int rc = 0;
  287. /* Auto increments kref reference if found. */
  288. if (!(hp = hvc_get_by_index(tty->index)))
  289. return -ENODEV;
  290. spin_lock_irqsave(&hp->lock, flags);
  291. /* Check and then increment for fast path open. */
  292. if (hp->count++ > 0) {
  293. spin_unlock_irqrestore(&hp->lock, flags);
  294. hvc_kick();
  295. return 0;
  296. } /* else count == 0 */
  297. tty->driver_data = hp;
  298. tty->low_latency = 1; /* Makes flushes to ldisc synchronous. */
  299. hp->tty = tty;
  300. /* Save for request_irq outside of spin_lock. */
  301. irq = hp->irq;
  302. if (irq)
  303. hp->irq_requested = 1;
  304. spin_unlock_irqrestore(&hp->lock, flags);
  305. /* check error, fallback to non-irq */
  306. if (irq)
  307. rc = request_irq(irq, hvc_handle_interrupt, IRQF_DISABLED, "hvc_console", hp);
  308. /*
  309. * If the request_irq() fails and we return an error. The tty layer
  310. * will call hvc_close() after a failed open but we don't want to clean
  311. * up there so we'll clean up here and clear out the previously set
  312. * tty fields and return the kref reference.
  313. */
  314. if (rc) {
  315. spin_lock_irqsave(&hp->lock, flags);
  316. hp->tty = NULL;
  317. hp->irq_requested = 0;
  318. spin_unlock_irqrestore(&hp->lock, flags);
  319. tty->driver_data = NULL;
  320. kref_put(&hp->kref, destroy_hvc_struct);
  321. printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
  322. }
  323. /* Force wakeup of the polling thread */
  324. hvc_kick();
  325. return rc;
  326. }
  327. static void hvc_close(struct tty_struct *tty, struct file * filp)
  328. {
  329. struct hvc_struct *hp;
  330. int irq = 0;
  331. unsigned long flags;
  332. if (tty_hung_up_p(filp))
  333. return;
  334. /*
  335. * No driver_data means that this close was issued after a failed
  336. * hvc_open by the tty layer's release_dev() function and we can just
  337. * exit cleanly because the kref reference wasn't made.
  338. */
  339. if (!tty->driver_data)
  340. return;
  341. hp = tty->driver_data;
  342. spin_lock_irqsave(&hp->lock, flags);
  343. if (--hp->count == 0) {
  344. if (hp->irq_requested)
  345. irq = hp->irq;
  346. hp->irq_requested = 0;
  347. /* We are done with the tty pointer now. */
  348. hp->tty = NULL;
  349. spin_unlock_irqrestore(&hp->lock, flags);
  350. /*
  351. * Chain calls chars_in_buffer() and returns immediately if
  352. * there is no buffered data otherwise sleeps on a wait queue
  353. * waking periodically to check chars_in_buffer().
  354. */
  355. tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
  356. if (irq)
  357. free_irq(irq, hp);
  358. } else {
  359. if (hp->count < 0)
  360. printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
  361. hp->vtermno, hp->count);
  362. spin_unlock_irqrestore(&hp->lock, flags);
  363. }
  364. kref_put(&hp->kref, destroy_hvc_struct);
  365. }
  366. static void hvc_hangup(struct tty_struct *tty)
  367. {
  368. struct hvc_struct *hp = tty->driver_data;
  369. unsigned long flags;
  370. int irq = 0;
  371. int temp_open_count;
  372. if (!hp)
  373. return;
  374. spin_lock_irqsave(&hp->lock, flags);
  375. /*
  376. * The N_TTY line discipline has problems such that in a close vs
  377. * open->hangup case this can be called after the final close so prevent
  378. * that from happening for now.
  379. */
  380. if (hp->count <= 0) {
  381. spin_unlock_irqrestore(&hp->lock, flags);
  382. return;
  383. }
  384. temp_open_count = hp->count;
  385. hp->count = 0;
  386. hp->n_outbuf = 0;
  387. hp->tty = NULL;
  388. if (hp->irq_requested)
  389. /* Saved for use outside of spin_lock. */
  390. irq = hp->irq;
  391. hp->irq_requested = 0;
  392. spin_unlock_irqrestore(&hp->lock, flags);
  393. if (irq)
  394. free_irq(irq, hp);
  395. while(temp_open_count) {
  396. --temp_open_count;
  397. kref_put(&hp->kref, destroy_hvc_struct);
  398. }
  399. }
  400. /*
  401. * Push buffered characters whether they were just recently buffered or waiting
  402. * on a blocked hypervisor. Call this function with hp->lock held.
  403. */
  404. static void hvc_push(struct hvc_struct *hp)
  405. {
  406. int n;
  407. n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
  408. if (n <= 0) {
  409. if (n == 0) {
  410. hp->do_wakeup = 1;
  411. return;
  412. }
  413. /* throw away output on error; this happens when
  414. there is no session connected to the vterm. */
  415. hp->n_outbuf = 0;
  416. } else
  417. hp->n_outbuf -= n;
  418. if (hp->n_outbuf > 0)
  419. memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
  420. else
  421. hp->do_wakeup = 1;
  422. }
  423. static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
  424. {
  425. struct hvc_struct *hp = tty->driver_data;
  426. unsigned long flags;
  427. int rsize, written = 0;
  428. /* This write was probably executed during a tty close. */
  429. if (!hp)
  430. return -EPIPE;
  431. if (hp->count <= 0)
  432. return -EIO;
  433. spin_lock_irqsave(&hp->lock, flags);
  434. /* Push pending writes */
  435. if (hp->n_outbuf > 0)
  436. hvc_push(hp);
  437. while (count > 0 && (rsize = hp->outbuf_size - hp->n_outbuf) > 0) {
  438. if (rsize > count)
  439. rsize = count;
  440. memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
  441. count -= rsize;
  442. buf += rsize;
  443. hp->n_outbuf += rsize;
  444. written += rsize;
  445. hvc_push(hp);
  446. }
  447. spin_unlock_irqrestore(&hp->lock, flags);
  448. /*
  449. * Racy, but harmless, kick thread if there is still pending data.
  450. */
  451. if (hp->n_outbuf)
  452. hvc_kick();
  453. return written;
  454. }
  455. /*
  456. * This is actually a contract between the driver and the tty layer outlining
  457. * how much write room the driver can guarantee will be sent OR BUFFERED. This
  458. * driver MUST honor the return value.
  459. */
  460. static int hvc_write_room(struct tty_struct *tty)
  461. {
  462. struct hvc_struct *hp = tty->driver_data;
  463. if (!hp)
  464. return -1;
  465. return hp->outbuf_size - hp->n_outbuf;
  466. }
  467. static int hvc_chars_in_buffer(struct tty_struct *tty)
  468. {
  469. struct hvc_struct *hp = tty->driver_data;
  470. if (!hp)
  471. return -1;
  472. return hp->n_outbuf;
  473. }
  474. /*
  475. * timeout will vary between the MIN and MAX values defined here. By default
  476. * and during console activity we will use a default MIN_TIMEOUT of 10. When
  477. * the console is idle, we increase the timeout value on each pass through
  478. * msleep until we reach the max. This may be noticeable as a brief (average
  479. * one second) delay on the console before the console responds to input when
  480. * there has been no input for some time.
  481. */
  482. #define MIN_TIMEOUT (10)
  483. #define MAX_TIMEOUT (2000)
  484. static u32 timeout = MIN_TIMEOUT;
  485. #define HVC_POLL_READ 0x00000001
  486. #define HVC_POLL_WRITE 0x00000002
  487. static int hvc_poll(struct hvc_struct *hp)
  488. {
  489. struct tty_struct *tty;
  490. int i, n, poll_mask = 0;
  491. char buf[N_INBUF] __ALIGNED__;
  492. unsigned long flags;
  493. int read_total = 0;
  494. spin_lock_irqsave(&hp->lock, flags);
  495. /* Push pending writes */
  496. if (hp->n_outbuf > 0)
  497. hvc_push(hp);
  498. /* Reschedule us if still some write pending */
  499. if (hp->n_outbuf > 0)
  500. poll_mask |= HVC_POLL_WRITE;
  501. /* No tty attached, just skip */
  502. tty = 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 interrupt driven and aren't throttled, we always
  509. * request a reschedule
  510. */
  511. if (hp->irq == 0)
  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_con_driver.index) {
  541. /* Handle the SysRq Hack */
  542. /* XXX should support a sequence */
  543. if (buf[i] == '\x0f') { /* ^O */
  544. sysrq_pressed = 1;
  545. continue;
  546. } else if (sysrq_pressed) {
  547. handle_sysrq(buf[i], tty);
  548. sysrq_pressed = 0;
  549. continue;
  550. }
  551. }
  552. #endif /* CONFIG_MAGIC_SYSRQ */
  553. tty_insert_flip_char(tty, buf[i], 0);
  554. }
  555. read_total += n;
  556. }
  557. throttled:
  558. /* Wakeup write queue if necessary */
  559. if (hp->do_wakeup) {
  560. hp->do_wakeup = 0;
  561. tty_wakeup(tty);
  562. }
  563. bail:
  564. spin_unlock_irqrestore(&hp->lock, flags);
  565. if (read_total) {
  566. /* Activity is occurring, so reset the polling backoff value to
  567. a minimum for performance. */
  568. timeout = MIN_TIMEOUT;
  569. tty_flip_buffer_push(tty);
  570. }
  571. return poll_mask;
  572. }
  573. #if defined(CONFIG_XMON) && defined(CONFIG_SMP)
  574. extern cpumask_t cpus_in_xmon;
  575. #else
  576. static const cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  577. #endif
  578. /*
  579. * This kthread is either polling or interrupt driven. This is determined by
  580. * calling hvc_poll() who determines whether a console adapter support
  581. * interrupts.
  582. */
  583. static int khvcd(void *unused)
  584. {
  585. int poll_mask;
  586. struct hvc_struct *hp;
  587. set_freezable();
  588. __set_current_state(TASK_RUNNING);
  589. do {
  590. poll_mask = 0;
  591. hvc_kicked = 0;
  592. try_to_freeze();
  593. wmb();
  594. if (cpus_empty(cpus_in_xmon)) {
  595. spin_lock(&hvc_structs_lock);
  596. list_for_each_entry(hp, &hvc_structs, next) {
  597. poll_mask |= hvc_poll(hp);
  598. }
  599. spin_unlock(&hvc_structs_lock);
  600. } else
  601. poll_mask |= HVC_POLL_READ;
  602. if (hvc_kicked)
  603. continue;
  604. if (poll_mask & HVC_POLL_WRITE) {
  605. yield();
  606. continue;
  607. }
  608. set_current_state(TASK_INTERRUPTIBLE);
  609. if (!hvc_kicked) {
  610. if (poll_mask == 0)
  611. schedule();
  612. else {
  613. if (timeout < MAX_TIMEOUT)
  614. timeout += (timeout >> 6) + 1;
  615. msleep_interruptible(timeout);
  616. }
  617. }
  618. __set_current_state(TASK_RUNNING);
  619. } while (!kthread_should_stop());
  620. return 0;
  621. }
  622. static const struct tty_operations hvc_ops = {
  623. .open = hvc_open,
  624. .close = hvc_close,
  625. .write = hvc_write,
  626. .hangup = hvc_hangup,
  627. .unthrottle = hvc_unthrottle,
  628. .write_room = hvc_write_room,
  629. .chars_in_buffer = hvc_chars_in_buffer,
  630. };
  631. struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq,
  632. struct hv_ops *ops, int outbuf_size)
  633. {
  634. struct hvc_struct *hp;
  635. int i;
  636. /* We wait until a driver actually comes along */
  637. if (!hvc_driver) {
  638. int err = hvc_init();
  639. if (err)
  640. return ERR_PTR(err);
  641. }
  642. hp = kmalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
  643. GFP_KERNEL);
  644. if (!hp)
  645. return ERR_PTR(-ENOMEM);
  646. memset(hp, 0x00, sizeof(*hp));
  647. hp->vtermno = vtermno;
  648. hp->irq = irq;
  649. hp->ops = ops;
  650. hp->outbuf_size = outbuf_size;
  651. hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
  652. kref_init(&hp->kref);
  653. spin_lock_init(&hp->lock);
  654. spin_lock(&hvc_structs_lock);
  655. /*
  656. * find index to use:
  657. * see if this vterm id matches one registered for console.
  658. */
  659. for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
  660. if (vtermnos[i] == hp->vtermno &&
  661. cons_ops[i] == hp->ops)
  662. break;
  663. /* no matching slot, just use a counter */
  664. if (i >= MAX_NR_HVC_CONSOLES)
  665. i = ++last_hvc;
  666. hp->index = i;
  667. list_add_tail(&(hp->next), &hvc_structs);
  668. spin_unlock(&hvc_structs_lock);
  669. return hp;
  670. }
  671. int __devexit hvc_remove(struct hvc_struct *hp)
  672. {
  673. unsigned long flags;
  674. struct tty_struct *tty;
  675. spin_lock_irqsave(&hp->lock, flags);
  676. tty = hp->tty;
  677. if (hp->index < MAX_NR_HVC_CONSOLES)
  678. vtermnos[hp->index] = -1;
  679. /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
  680. spin_unlock_irqrestore(&hp->lock, flags);
  681. /*
  682. * We 'put' the instance that was grabbed when the kref instance
  683. * was initialized using kref_init(). Let the last holder of this
  684. * kref cause it to be removed, which will probably be the tty_hangup
  685. * below.
  686. */
  687. kref_put(&hp->kref, destroy_hvc_struct);
  688. /*
  689. * This function call will auto chain call hvc_hangup. The tty should
  690. * always be valid at this time unless a simultaneous tty close already
  691. * cleaned up the hvc_struct.
  692. */
  693. if (tty)
  694. tty_hangup(tty);
  695. return 0;
  696. }
  697. /* Driver initialization: called as soon as someone uses hvc_alloc(). */
  698. static int hvc_init(void)
  699. {
  700. struct tty_driver *drv;
  701. int err;
  702. /* We need more than hvc_count adapters due to hotplug additions. */
  703. drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
  704. if (!drv) {
  705. err = -ENOMEM;
  706. goto out;
  707. }
  708. drv->owner = THIS_MODULE;
  709. drv->driver_name = "hvc";
  710. drv->name = "hvc";
  711. drv->major = HVC_MAJOR;
  712. drv->minor_start = HVC_MINOR;
  713. drv->type = TTY_DRIVER_TYPE_SYSTEM;
  714. drv->init_termios = tty_std_termios;
  715. drv->flags = TTY_DRIVER_REAL_RAW;
  716. tty_set_operations(drv, &hvc_ops);
  717. /* Always start the kthread because there can be hotplug vty adapters
  718. * added later. */
  719. hvc_task = kthread_run(khvcd, NULL, "khvcd");
  720. if (IS_ERR(hvc_task)) {
  721. printk(KERN_ERR "Couldn't create kthread for console.\n");
  722. err = PTR_ERR(hvc_task);
  723. goto put_tty;
  724. }
  725. err = tty_register_driver(drv);
  726. if (err) {
  727. printk(KERN_ERR "Couldn't register hvc console driver\n");
  728. goto stop_thread;
  729. }
  730. /* FIXME: This mb() seems completely random. Remove it. */
  731. mb();
  732. hvc_driver = drv;
  733. return 0;
  734. put_tty:
  735. put_tty_driver(hvc_driver);
  736. stop_thread:
  737. kthread_stop(hvc_task);
  738. hvc_task = NULL;
  739. out:
  740. return err;
  741. }
  742. /* This isn't particularly necessary due to this being a console driver
  743. * but it is nice to be thorough.
  744. */
  745. static void __exit hvc_exit(void)
  746. {
  747. if (hvc_driver) {
  748. kthread_stop(hvc_task);
  749. tty_unregister_driver(hvc_driver);
  750. /* return tty_struct instances allocated in hvc_init(). */
  751. put_tty_driver(hvc_driver);
  752. unregister_console(&hvc_con_driver);
  753. }
  754. }
  755. module_exit(hvc_exit);