hvc_console.c 22 KB

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