hvc_console.c 21 KB

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