hvc_console.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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 <asm/hvconsole.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. * The Linux TTY code does not support dynamic addition of tty derived devices
  53. * so we need to know how many tty devices we might need when space is allocated
  54. * for the tty device. Since this driver supports hotplug of vty adapters we
  55. * need to make sure we have enough allocated.
  56. */
  57. #define HVC_ALLOC_TTY_ADAPTERS 8
  58. #define N_OUTBUF 16
  59. #define N_INBUF 16
  60. #define __ALIGNED__ __attribute__((__aligned__(8)))
  61. static struct tty_driver *hvc_driver;
  62. static struct task_struct *hvc_task;
  63. /* Picks up late kicks after list walk but before schedule() */
  64. static int hvc_kicked;
  65. #ifdef CONFIG_MAGIC_SYSRQ
  66. static int sysrq_pressed;
  67. #endif
  68. struct hvc_struct {
  69. spinlock_t lock;
  70. int index;
  71. struct tty_struct *tty;
  72. unsigned int count;
  73. int do_wakeup;
  74. char outbuf[N_OUTBUF] __ALIGNED__;
  75. int n_outbuf;
  76. uint32_t vtermno;
  77. struct hv_ops *ops;
  78. int irq_requested;
  79. int irq;
  80. struct list_head next;
  81. struct kobject kobj; /* ref count & hvc_struct lifetime */
  82. };
  83. /* dynamic list of hvc_struct instances */
  84. static struct list_head hvc_structs = LIST_HEAD_INIT(hvc_structs);
  85. /*
  86. * Protect the list of hvc_struct instances from inserts and removals during
  87. * list traversal.
  88. */
  89. static DEFINE_SPINLOCK(hvc_structs_lock);
  90. /*
  91. * This value is used to assign a tty->index value to a hvc_struct based
  92. * upon order of exposure via hvc_probe(), when we can not match it to
  93. * a console canidate registered with hvc_instantiate().
  94. */
  95. static int last_hvc = -1;
  96. /*
  97. * Do not call this function with either the hvc_strucst_lock or the hvc_struct
  98. * lock held. If successful, this function increments the kobject reference
  99. * count against the target hvc_struct so it should be released when finished.
  100. */
  101. struct hvc_struct *hvc_get_by_index(int index)
  102. {
  103. struct hvc_struct *hp;
  104. unsigned long flags;
  105. spin_lock(&hvc_structs_lock);
  106. list_for_each_entry(hp, &hvc_structs, next) {
  107. spin_lock_irqsave(&hp->lock, flags);
  108. if (hp->index == index) {
  109. kobject_get(&hp->kobj);
  110. spin_unlock_irqrestore(&hp->lock, flags);
  111. spin_unlock(&hvc_structs_lock);
  112. return hp;
  113. }
  114. spin_unlock_irqrestore(&hp->lock, flags);
  115. }
  116. hp = NULL;
  117. spin_unlock(&hvc_structs_lock);
  118. return hp;
  119. }
  120. /*
  121. * Initial console vtermnos for console API usage prior to full console
  122. * initialization. Any vty adapter outside this range will not have usable
  123. * console interfaces but can still be used as a tty device. This has to be
  124. * static because kmalloc will not work during early console init.
  125. */
  126. static struct hv_ops *cons_ops[MAX_NR_HVC_CONSOLES];
  127. static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] =
  128. {[0 ... MAX_NR_HVC_CONSOLES - 1] = -1};
  129. /*
  130. * Console APIs, NOT TTY. These APIs are available immediately when
  131. * hvc_console_setup() finds adapters.
  132. */
  133. void hvc_console_print(struct console *co, const char *b, unsigned count)
  134. {
  135. char c[16] __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 useable. */
  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. 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. Preceeds 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 registerd 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 upto 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. EXPORT_SYMBOL(hvc_instantiate);
  244. /* Wake the sleeping khvcd */
  245. static void hvc_kick(void)
  246. {
  247. hvc_kicked = 1;
  248. wake_up_process(hvc_task);
  249. }
  250. static int hvc_poll(struct hvc_struct *hp);
  251. /*
  252. * NOTE: This API isn't used if the console adapter doesn't support interrupts.
  253. * In this case the console is poll driven.
  254. */
  255. static irqreturn_t hvc_handle_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
  256. {
  257. /* if hvc_poll request a repoll, then kick the hvcd thread */
  258. if (hvc_poll(dev_instance))
  259. hvc_kick();
  260. return IRQ_HANDLED;
  261. }
  262. static void hvc_unthrottle(struct tty_struct *tty)
  263. {
  264. hvc_kick();
  265. }
  266. /*
  267. * The TTY interface won't be used until after the vio layer has exposed the vty
  268. * adapter to the kernel.
  269. */
  270. static int hvc_open(struct tty_struct *tty, struct file * filp)
  271. {
  272. struct hvc_struct *hp;
  273. unsigned long flags;
  274. int irq = NO_IRQ;
  275. int rc = 0;
  276. struct kobject *kobjp;
  277. /* Auto increments kobject reference if found. */
  278. if (!(hp = hvc_get_by_index(tty->index))) {
  279. printk(KERN_WARNING "hvc_console: tty open failed, no vty associated with tty.\n");
  280. return -ENODEV;
  281. }
  282. spin_lock_irqsave(&hp->lock, flags);
  283. /* Check and then increment for fast path open. */
  284. if (hp->count++ > 0) {
  285. spin_unlock_irqrestore(&hp->lock, flags);
  286. hvc_kick();
  287. return 0;
  288. } /* else count == 0 */
  289. tty->driver_data = hp;
  290. hp->tty = tty;
  291. /* Save for request_irq outside of spin_lock. */
  292. irq = hp->irq;
  293. if (irq != NO_IRQ)
  294. hp->irq_requested = 1;
  295. kobjp = &hp->kobj;
  296. spin_unlock_irqrestore(&hp->lock, flags);
  297. /* check error, fallback to non-irq */
  298. if (irq != NO_IRQ)
  299. rc = request_irq(irq, hvc_handle_interrupt, SA_INTERRUPT, "hvc_console", hp);
  300. /*
  301. * If the request_irq() fails and we return an error. The tty layer
  302. * will call hvc_close() after a failed open but we don't want to clean
  303. * up there so we'll clean up here and clear out the previously set
  304. * tty fields and return the kobject reference.
  305. */
  306. if (rc) {
  307. spin_lock_irqsave(&hp->lock, flags);
  308. hp->tty = NULL;
  309. hp->irq_requested = 0;
  310. spin_unlock_irqrestore(&hp->lock, flags);
  311. tty->driver_data = NULL;
  312. kobject_put(kobjp);
  313. printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
  314. }
  315. /* Force wakeup of the polling thread */
  316. hvc_kick();
  317. return rc;
  318. }
  319. static void hvc_close(struct tty_struct *tty, struct file * filp)
  320. {
  321. struct hvc_struct *hp;
  322. struct kobject *kobjp;
  323. int irq = NO_IRQ;
  324. unsigned long flags;
  325. if (tty_hung_up_p(filp))
  326. return;
  327. /*
  328. * No driver_data means that this close was issued after a failed
  329. * hvc_open by the tty layer's release_dev() function and we can just
  330. * exit cleanly because the kobject reference wasn't made.
  331. */
  332. if (!tty->driver_data)
  333. return;
  334. hp = tty->driver_data;
  335. spin_lock_irqsave(&hp->lock, flags);
  336. kobjp = &hp->kobj;
  337. if (--hp->count == 0) {
  338. if (hp->irq_requested)
  339. irq = hp->irq;
  340. hp->irq_requested = 0;
  341. /* We are done with the tty pointer now. */
  342. hp->tty = NULL;
  343. spin_unlock_irqrestore(&hp->lock, flags);
  344. /*
  345. * Chain calls chars_in_buffer() and returns immediately if
  346. * there is no buffered data otherwise sleeps on a wait queue
  347. * waking periodically to check chars_in_buffer().
  348. */
  349. tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
  350. if (irq != NO_IRQ)
  351. free_irq(irq, hp);
  352. } else {
  353. if (hp->count < 0)
  354. printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
  355. hp->vtermno, hp->count);
  356. spin_unlock_irqrestore(&hp->lock, flags);
  357. }
  358. kobject_put(kobjp);
  359. }
  360. static void hvc_hangup(struct tty_struct *tty)
  361. {
  362. struct hvc_struct *hp = tty->driver_data;
  363. unsigned long flags;
  364. int irq = NO_IRQ;
  365. int temp_open_count;
  366. struct kobject *kobjp;
  367. if (!hp)
  368. return;
  369. spin_lock_irqsave(&hp->lock, flags);
  370. /*
  371. * The N_TTY line discipline has problems such that in a close vs
  372. * open->hangup case this can be called after the final close so prevent
  373. * that from happening for now.
  374. */
  375. if (hp->count <= 0) {
  376. spin_unlock_irqrestore(&hp->lock, flags);
  377. return;
  378. }
  379. kobjp = &hp->kobj;
  380. temp_open_count = hp->count;
  381. hp->count = 0;
  382. hp->n_outbuf = 0;
  383. hp->tty = NULL;
  384. if (hp->irq_requested)
  385. /* Saved for use outside of spin_lock. */
  386. irq = hp->irq;
  387. hp->irq_requested = 0;
  388. spin_unlock_irqrestore(&hp->lock, flags);
  389. if (irq != NO_IRQ)
  390. free_irq(irq, hp);
  391. while(temp_open_count) {
  392. --temp_open_count;
  393. kobject_put(kobjp);
  394. }
  395. }
  396. /*
  397. * Push buffered characters whether they were just recently buffered or waiting
  398. * on a blocked hypervisor. Call this function with hp->lock held.
  399. */
  400. static void hvc_push(struct hvc_struct *hp)
  401. {
  402. int n;
  403. n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
  404. if (n <= 0) {
  405. if (n == 0)
  406. return;
  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 inline int __hvc_write_kernel(struct hvc_struct *hp,
  418. const unsigned char *buf, int count)
  419. {
  420. unsigned long flags;
  421. int rsize, written = 0;
  422. spin_lock_irqsave(&hp->lock, flags);
  423. /* Push pending writes */
  424. if (hp->n_outbuf > 0)
  425. hvc_push(hp);
  426. while (count > 0 && (rsize = N_OUTBUF - hp->n_outbuf) > 0) {
  427. if (rsize > count)
  428. rsize = count;
  429. memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
  430. count -= rsize;
  431. buf += rsize;
  432. hp->n_outbuf += rsize;
  433. written += rsize;
  434. hvc_push(hp);
  435. }
  436. spin_unlock_irqrestore(&hp->lock, flags);
  437. return written;
  438. }
  439. static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
  440. {
  441. struct hvc_struct *hp = tty->driver_data;
  442. int written;
  443. /* This write was probably executed during a tty close. */
  444. if (!hp)
  445. return -EPIPE;
  446. if (hp->count <= 0)
  447. return -EIO;
  448. written = __hvc_write_kernel(hp, buf, count);
  449. /*
  450. * Racy, but harmless, kick thread if there is still pending data.
  451. * There really is nothing wrong with kicking the thread, even if there
  452. * is no buffered data.
  453. */
  454. if (hp->n_outbuf)
  455. hvc_kick();
  456. return written;
  457. }
  458. /*
  459. * This is actually a contract between the driver and the tty layer outlining
  460. * how much write room the driver can guarentee will be sent OR BUFFERED. This
  461. * driver MUST honor the return value.
  462. */
  463. static int hvc_write_room(struct tty_struct *tty)
  464. {
  465. struct hvc_struct *hp = tty->driver_data;
  466. if (!hp)
  467. return -1;
  468. return N_OUTBUF - hp->n_outbuf;
  469. }
  470. static int hvc_chars_in_buffer(struct tty_struct *tty)
  471. {
  472. struct hvc_struct *hp = tty->driver_data;
  473. if (!hp)
  474. return -1;
  475. return hp->n_outbuf;
  476. }
  477. #define HVC_POLL_READ 0x00000001
  478. #define HVC_POLL_WRITE 0x00000002
  479. #define HVC_POLL_QUICK 0x00000004
  480. static int hvc_poll(struct hvc_struct *hp)
  481. {
  482. struct tty_struct *tty;
  483. int i, n, poll_mask = 0;
  484. char buf[N_INBUF] __ALIGNED__;
  485. unsigned long flags;
  486. int read_total = 0;
  487. spin_lock_irqsave(&hp->lock, flags);
  488. /* Push pending writes */
  489. if (hp->n_outbuf > 0)
  490. hvc_push(hp);
  491. /* Reschedule us if still some write pending */
  492. if (hp->n_outbuf > 0)
  493. poll_mask |= HVC_POLL_WRITE;
  494. /* No tty attached, just skip */
  495. tty = hp->tty;
  496. if (tty == NULL)
  497. goto bail;
  498. /* Now check if we can get data (are we throttled ?) */
  499. if (test_bit(TTY_THROTTLED, &tty->flags))
  500. goto throttled;
  501. /* If we aren't interrupt driven and aren't throttled, we always
  502. * request a reschedule
  503. */
  504. if (hp->irq == NO_IRQ)
  505. poll_mask |= HVC_POLL_READ;
  506. /* Read data if any */
  507. for (;;) {
  508. int count = N_INBUF;
  509. if (count > (TTY_FLIPBUF_SIZE - tty->flip.count))
  510. count = TTY_FLIPBUF_SIZE - tty->flip.count;
  511. /* If flip is full, just reschedule a later read */
  512. if (count == 0) {
  513. poll_mask |= HVC_POLL_READ;
  514. break;
  515. }
  516. n = hp->ops->get_chars(hp->vtermno, buf, count);
  517. if (n <= 0) {
  518. /* Hangup the tty when disconnected from host */
  519. if (n == -EPIPE) {
  520. spin_unlock_irqrestore(&hp->lock, flags);
  521. tty_hangup(tty);
  522. spin_lock_irqsave(&hp->lock, flags);
  523. }
  524. break;
  525. }
  526. for (i = 0; i < n; ++i) {
  527. #ifdef CONFIG_MAGIC_SYSRQ
  528. if (hp->index == hvc_con_driver.index) {
  529. /* Handle the SysRq Hack */
  530. /* XXX should support a sequence */
  531. if (buf[i] == '\x0f') { /* ^O */
  532. sysrq_pressed = 1;
  533. continue;
  534. } else if (sysrq_pressed) {
  535. handle_sysrq(buf[i], NULL, tty);
  536. sysrq_pressed = 0;
  537. continue;
  538. }
  539. }
  540. #endif /* CONFIG_MAGIC_SYSRQ */
  541. tty_insert_flip_char(tty, buf[i], 0);
  542. }
  543. if (tty->flip.count)
  544. tty_schedule_flip(tty);
  545. /*
  546. * Account for the total amount read in one loop, and if above
  547. * 64 bytes, we do a quick schedule loop to let the tty grok
  548. * the data and eventually throttle us.
  549. */
  550. read_total += n;
  551. if (read_total >= 64) {
  552. poll_mask |= HVC_POLL_QUICK;
  553. break;
  554. }
  555. }
  556. throttled:
  557. /* Wakeup write queue if necessary */
  558. if (hp->do_wakeup) {
  559. hp->do_wakeup = 0;
  560. tty_wakeup(tty);
  561. }
  562. bail:
  563. spin_unlock_irqrestore(&hp->lock, flags);
  564. return poll_mask;
  565. }
  566. #if defined(CONFIG_XMON) && defined(CONFIG_SMP)
  567. extern cpumask_t cpus_in_xmon;
  568. #else
  569. static const cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  570. #endif
  571. /*
  572. * This kthread is either polling or interrupt driven. This is determined by
  573. * calling hvc_poll() who determines whether a console adapter support
  574. * interrupts.
  575. */
  576. int khvcd(void *unused)
  577. {
  578. int poll_mask;
  579. struct hvc_struct *hp;
  580. __set_current_state(TASK_RUNNING);
  581. do {
  582. poll_mask = 0;
  583. hvc_kicked = 0;
  584. wmb();
  585. if (cpus_empty(cpus_in_xmon)) {
  586. spin_lock(&hvc_structs_lock);
  587. list_for_each_entry(hp, &hvc_structs, next) {
  588. poll_mask |= hvc_poll(hp);
  589. }
  590. spin_unlock(&hvc_structs_lock);
  591. } else
  592. poll_mask |= HVC_POLL_READ;
  593. if (hvc_kicked)
  594. continue;
  595. if (poll_mask & HVC_POLL_QUICK) {
  596. yield();
  597. continue;
  598. }
  599. set_current_state(TASK_INTERRUPTIBLE);
  600. if (!hvc_kicked) {
  601. if (poll_mask == 0)
  602. schedule();
  603. else
  604. msleep_interruptible(TIMEOUT);
  605. }
  606. __set_current_state(TASK_RUNNING);
  607. } while (!kthread_should_stop());
  608. return 0;
  609. }
  610. static struct tty_operations hvc_ops = {
  611. .open = hvc_open,
  612. .close = hvc_close,
  613. .write = hvc_write,
  614. .hangup = hvc_hangup,
  615. .unthrottle = hvc_unthrottle,
  616. .write_room = hvc_write_room,
  617. .chars_in_buffer = hvc_chars_in_buffer,
  618. };
  619. /* callback when the kboject ref count reaches zero. */
  620. static void destroy_hvc_struct(struct kobject *kobj)
  621. {
  622. struct hvc_struct *hp = container_of(kobj, struct hvc_struct, kobj);
  623. unsigned long flags;
  624. spin_lock(&hvc_structs_lock);
  625. spin_lock_irqsave(&hp->lock, flags);
  626. list_del(&(hp->next));
  627. spin_unlock_irqrestore(&hp->lock, flags);
  628. spin_unlock(&hvc_structs_lock);
  629. kfree(hp);
  630. }
  631. static struct kobj_type hvc_kobj_type = {
  632. .release = destroy_hvc_struct,
  633. };
  634. struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq,
  635. struct hv_ops *ops)
  636. {
  637. struct hvc_struct *hp;
  638. int i;
  639. hp = kmalloc(sizeof(*hp), GFP_KERNEL);
  640. if (!hp)
  641. return ERR_PTR(-ENOMEM);
  642. memset(hp, 0x00, sizeof(*hp));
  643. hp->vtermno = vtermno;
  644. hp->irq = irq;
  645. hp->ops = ops;
  646. kobject_init(&hp->kobj);
  647. hp->kobj.ktype = &hvc_kobj_type;
  648. spin_lock_init(&hp->lock);
  649. spin_lock(&hvc_structs_lock);
  650. /*
  651. * find index to use:
  652. * see if this vterm id matches one registered for console.
  653. */
  654. for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
  655. if (vtermnos[i] == hp->vtermno)
  656. break;
  657. /* no matching slot, just use a counter */
  658. if (i >= MAX_NR_HVC_CONSOLES)
  659. i = ++last_hvc;
  660. hp->index = i;
  661. list_add_tail(&(hp->next), &hvc_structs);
  662. spin_unlock(&hvc_structs_lock);
  663. return hp;
  664. }
  665. EXPORT_SYMBOL(hvc_alloc);
  666. int __devexit hvc_remove(struct hvc_struct *hp)
  667. {
  668. unsigned long flags;
  669. struct kobject *kobjp;
  670. struct tty_struct *tty;
  671. spin_lock_irqsave(&hp->lock, flags);
  672. tty = hp->tty;
  673. kobjp = &hp->kobj;
  674. if (hp->index < MAX_NR_HVC_CONSOLES)
  675. vtermnos[hp->index] = -1;
  676. /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
  677. spin_unlock_irqrestore(&hp->lock, flags);
  678. /*
  679. * We 'put' the instance that was grabbed when the kobject instance
  680. * was intialized using kobject_init(). Let the last holder of this
  681. * kobject cause it to be removed, which will probably be the tty_hangup
  682. * below.
  683. */
  684. kobject_put(kobjp);
  685. /*
  686. * This function call will auto chain call hvc_hangup. The tty should
  687. * always be valid at this time unless a simultaneous tty close already
  688. * cleaned up the hvc_struct.
  689. */
  690. if (tty)
  691. tty_hangup(tty);
  692. return 0;
  693. }
  694. EXPORT_SYMBOL(hvc_remove);
  695. /* Driver initialization. Follow console initialization. This is where the TTY
  696. * interfaces start to become available. */
  697. int __init hvc_init(void)
  698. {
  699. /* We need more than hvc_count adapters due to hotplug additions. */
  700. hvc_driver = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
  701. if (!hvc_driver)
  702. return -ENOMEM;
  703. hvc_driver->owner = THIS_MODULE;
  704. hvc_driver->devfs_name = "hvc/";
  705. hvc_driver->driver_name = "hvc";
  706. hvc_driver->name = "hvc";
  707. hvc_driver->major = HVC_MAJOR;
  708. hvc_driver->minor_start = HVC_MINOR;
  709. hvc_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  710. hvc_driver->init_termios = tty_std_termios;
  711. hvc_driver->flags = TTY_DRIVER_REAL_RAW;
  712. tty_set_operations(hvc_driver, &hvc_ops);
  713. if (tty_register_driver(hvc_driver))
  714. panic("Couldn't register hvc console driver\n");
  715. /* Always start the kthread because there can be hotplug vty adapters
  716. * added later. */
  717. hvc_task = kthread_run(khvcd, NULL, "khvcd");
  718. if (IS_ERR(hvc_task)) {
  719. panic("Couldn't create kthread for console.\n");
  720. put_tty_driver(hvc_driver);
  721. return -EIO;
  722. }
  723. return 0;
  724. }
  725. module_init(hvc_init);
  726. /* This isn't particularily necessary due to this being a console driver
  727. * but it is nice to be thorough.
  728. */
  729. static void __exit hvc_exit(void)
  730. {
  731. kthread_stop(hvc_task);
  732. tty_unregister_driver(hvc_driver);
  733. /* return tty_struct instances allocated in hvc_init(). */
  734. put_tty_driver(hvc_driver);
  735. unregister_console(&hvc_con_driver);
  736. }
  737. module_exit(hvc_exit);