hvc_console.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 <asm/uaccess.h>
  41. #include "hvc_console.h"
  42. #define HVC_MAJOR 229
  43. #define HVC_MINOR 0
  44. #define TIMEOUT (10)
  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. #ifdef CONFIG_MAGIC_SYSRQ
  63. static int sysrq_pressed;
  64. #endif
  65. struct hvc_struct {
  66. spinlock_t lock;
  67. int index;
  68. struct tty_struct *tty;
  69. unsigned int count;
  70. int do_wakeup;
  71. char *outbuf;
  72. int outbuf_size;
  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)
  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. return -ENODEV;
  278. spin_lock_irqsave(&hp->lock, flags);
  279. /* Check and then increment for fast path open. */
  280. if (hp->count++ > 0) {
  281. spin_unlock_irqrestore(&hp->lock, flags);
  282. hvc_kick();
  283. return 0;
  284. } /* else count == 0 */
  285. tty->driver_data = hp;
  286. tty->low_latency = 1; /* Makes flushes to ldisc synchronous. */
  287. hp->tty = tty;
  288. /* Save for request_irq outside of spin_lock. */
  289. irq = hp->irq;
  290. if (irq != NO_IRQ)
  291. hp->irq_requested = 1;
  292. kobjp = &hp->kobj;
  293. spin_unlock_irqrestore(&hp->lock, flags);
  294. /* check error, fallback to non-irq */
  295. if (irq != NO_IRQ)
  296. rc = request_irq(irq, hvc_handle_interrupt, IRQF_DISABLED, "hvc_console", hp);
  297. /*
  298. * If the request_irq() fails and we return an error. The tty layer
  299. * will call hvc_close() after a failed open but we don't want to clean
  300. * up there so we'll clean up here and clear out the previously set
  301. * tty fields and return the kobject reference.
  302. */
  303. if (rc) {
  304. spin_lock_irqsave(&hp->lock, flags);
  305. hp->tty = NULL;
  306. hp->irq_requested = 0;
  307. spin_unlock_irqrestore(&hp->lock, flags);
  308. tty->driver_data = NULL;
  309. kobject_put(kobjp);
  310. printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
  311. }
  312. /* Force wakeup of the polling thread */
  313. hvc_kick();
  314. return rc;
  315. }
  316. static void hvc_close(struct tty_struct *tty, struct file * filp)
  317. {
  318. struct hvc_struct *hp;
  319. struct kobject *kobjp;
  320. int irq = NO_IRQ;
  321. unsigned long flags;
  322. if (tty_hung_up_p(filp))
  323. return;
  324. /*
  325. * No driver_data means that this close was issued after a failed
  326. * hvc_open by the tty layer's release_dev() function and we can just
  327. * exit cleanly because the kobject reference wasn't made.
  328. */
  329. if (!tty->driver_data)
  330. return;
  331. hp = tty->driver_data;
  332. spin_lock_irqsave(&hp->lock, flags);
  333. kobjp = &hp->kobj;
  334. if (--hp->count == 0) {
  335. if (hp->irq_requested)
  336. irq = hp->irq;
  337. hp->irq_requested = 0;
  338. /* We are done with the tty pointer now. */
  339. hp->tty = NULL;
  340. spin_unlock_irqrestore(&hp->lock, flags);
  341. /*
  342. * Chain calls chars_in_buffer() and returns immediately if
  343. * there is no buffered data otherwise sleeps on a wait queue
  344. * waking periodically to check chars_in_buffer().
  345. */
  346. tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
  347. if (irq != NO_IRQ)
  348. free_irq(irq, hp);
  349. } else {
  350. if (hp->count < 0)
  351. printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
  352. hp->vtermno, hp->count);
  353. spin_unlock_irqrestore(&hp->lock, flags);
  354. }
  355. kobject_put(kobjp);
  356. }
  357. static void hvc_hangup(struct tty_struct *tty)
  358. {
  359. struct hvc_struct *hp = tty->driver_data;
  360. unsigned long flags;
  361. int irq = NO_IRQ;
  362. int temp_open_count;
  363. struct kobject *kobjp;
  364. if (!hp)
  365. return;
  366. spin_lock_irqsave(&hp->lock, flags);
  367. /*
  368. * The N_TTY line discipline has problems such that in a close vs
  369. * open->hangup case this can be called after the final close so prevent
  370. * that from happening for now.
  371. */
  372. if (hp->count <= 0) {
  373. spin_unlock_irqrestore(&hp->lock, flags);
  374. return;
  375. }
  376. kobjp = &hp->kobj;
  377. temp_open_count = hp->count;
  378. hp->count = 0;
  379. hp->n_outbuf = 0;
  380. hp->tty = NULL;
  381. if (hp->irq_requested)
  382. /* Saved for use outside of spin_lock. */
  383. irq = hp->irq;
  384. hp->irq_requested = 0;
  385. spin_unlock_irqrestore(&hp->lock, flags);
  386. if (irq != NO_IRQ)
  387. free_irq(irq, hp);
  388. while(temp_open_count) {
  389. --temp_open_count;
  390. kobject_put(kobjp);
  391. }
  392. }
  393. /*
  394. * Push buffered characters whether they were just recently buffered or waiting
  395. * on a blocked hypervisor. Call this function with hp->lock held.
  396. */
  397. static void hvc_push(struct hvc_struct *hp)
  398. {
  399. int n;
  400. n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
  401. if (n <= 0) {
  402. if (n == 0) {
  403. hp->do_wakeup = 1;
  404. return;
  405. }
  406. /* throw away output on error; this happens when
  407. there is no session connected to the vterm. */
  408. hp->n_outbuf = 0;
  409. } else
  410. hp->n_outbuf -= n;
  411. if (hp->n_outbuf > 0)
  412. memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
  413. else
  414. hp->do_wakeup = 1;
  415. }
  416. static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
  417. {
  418. struct hvc_struct *hp = tty->driver_data;
  419. unsigned long flags;
  420. int rsize, written = 0;
  421. /* This write was probably executed during a tty close. */
  422. if (!hp)
  423. return -EPIPE;
  424. if (hp->count <= 0)
  425. return -EIO;
  426. spin_lock_irqsave(&hp->lock, flags);
  427. /* Push pending writes */
  428. if (hp->n_outbuf > 0)
  429. hvc_push(hp);
  430. while (count > 0 && (rsize = hp->outbuf_size - hp->n_outbuf) > 0) {
  431. if (rsize > count)
  432. rsize = count;
  433. memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
  434. count -= rsize;
  435. buf += rsize;
  436. hp->n_outbuf += rsize;
  437. written += rsize;
  438. hvc_push(hp);
  439. }
  440. spin_unlock_irqrestore(&hp->lock, flags);
  441. /*
  442. * Racy, but harmless, kick thread if there is still pending data.
  443. */
  444. if (hp->n_outbuf)
  445. hvc_kick();
  446. return written;
  447. }
  448. /*
  449. * This is actually a contract between the driver and the tty layer outlining
  450. * how much write room the driver can guarentee will be sent OR BUFFERED. This
  451. * driver MUST honor the return value.
  452. */
  453. static int hvc_write_room(struct tty_struct *tty)
  454. {
  455. struct hvc_struct *hp = tty->driver_data;
  456. if (!hp)
  457. return -1;
  458. return hp->outbuf_size - hp->n_outbuf;
  459. }
  460. static int hvc_chars_in_buffer(struct tty_struct *tty)
  461. {
  462. struct hvc_struct *hp = tty->driver_data;
  463. if (!hp)
  464. return -1;
  465. return hp->n_outbuf;
  466. }
  467. #define HVC_POLL_READ 0x00000001
  468. #define HVC_POLL_WRITE 0x00000002
  469. static int hvc_poll(struct hvc_struct *hp)
  470. {
  471. struct tty_struct *tty;
  472. int i, n, poll_mask = 0;
  473. char buf[N_INBUF] __ALIGNED__;
  474. unsigned long flags;
  475. int read_total = 0;
  476. spin_lock_irqsave(&hp->lock, flags);
  477. /* Push pending writes */
  478. if (hp->n_outbuf > 0)
  479. hvc_push(hp);
  480. /* Reschedule us if still some write pending */
  481. if (hp->n_outbuf > 0)
  482. poll_mask |= HVC_POLL_WRITE;
  483. /* No tty attached, just skip */
  484. tty = hp->tty;
  485. if (tty == NULL)
  486. goto bail;
  487. /* Now check if we can get data (are we throttled ?) */
  488. if (test_bit(TTY_THROTTLED, &tty->flags))
  489. goto throttled;
  490. /* If we aren't interrupt driven and aren't throttled, we always
  491. * request a reschedule
  492. */
  493. if (hp->irq == NO_IRQ)
  494. poll_mask |= HVC_POLL_READ;
  495. /* Read data if any */
  496. for (;;) {
  497. int count = tty_buffer_request_room(tty, N_INBUF);
  498. /* If flip is full, just reschedule a later read */
  499. if (count == 0) {
  500. poll_mask |= HVC_POLL_READ;
  501. break;
  502. }
  503. n = hp->ops->get_chars(hp->vtermno, buf, count);
  504. if (n <= 0) {
  505. /* Hangup the tty when disconnected from host */
  506. if (n == -EPIPE) {
  507. spin_unlock_irqrestore(&hp->lock, flags);
  508. tty_hangup(tty);
  509. spin_lock_irqsave(&hp->lock, flags);
  510. } else if ( n == -EAGAIN ) {
  511. /*
  512. * Some back-ends can only ensure a certain min
  513. * num of bytes read, which may be > 'count'.
  514. * Let the tty clear the flip buff to make room.
  515. */
  516. poll_mask |= HVC_POLL_READ;
  517. }
  518. break;
  519. }
  520. for (i = 0; i < n; ++i) {
  521. #ifdef CONFIG_MAGIC_SYSRQ
  522. if (hp->index == hvc_con_driver.index) {
  523. /* Handle the SysRq Hack */
  524. /* XXX should support a sequence */
  525. if (buf[i] == '\x0f') { /* ^O */
  526. sysrq_pressed = 1;
  527. continue;
  528. } else if (sysrq_pressed) {
  529. handle_sysrq(buf[i], tty);
  530. sysrq_pressed = 0;
  531. continue;
  532. }
  533. }
  534. #endif /* CONFIG_MAGIC_SYSRQ */
  535. tty_insert_flip_char(tty, buf[i], 0);
  536. }
  537. read_total += n;
  538. }
  539. throttled:
  540. /* Wakeup write queue if necessary */
  541. if (hp->do_wakeup) {
  542. hp->do_wakeup = 0;
  543. tty_wakeup(tty);
  544. }
  545. bail:
  546. spin_unlock_irqrestore(&hp->lock, flags);
  547. if (read_total)
  548. tty_flip_buffer_push(tty);
  549. return poll_mask;
  550. }
  551. #if defined(CONFIG_XMON) && defined(CONFIG_SMP)
  552. extern cpumask_t cpus_in_xmon;
  553. #else
  554. static const cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  555. #endif
  556. /*
  557. * This kthread is either polling or interrupt driven. This is determined by
  558. * calling hvc_poll() who determines whether a console adapter support
  559. * interrupts.
  560. */
  561. int khvcd(void *unused)
  562. {
  563. int poll_mask;
  564. struct hvc_struct *hp;
  565. __set_current_state(TASK_RUNNING);
  566. do {
  567. poll_mask = 0;
  568. hvc_kicked = 0;
  569. try_to_freeze();
  570. wmb();
  571. if (cpus_empty(cpus_in_xmon)) {
  572. spin_lock(&hvc_structs_lock);
  573. list_for_each_entry(hp, &hvc_structs, next) {
  574. poll_mask |= hvc_poll(hp);
  575. }
  576. spin_unlock(&hvc_structs_lock);
  577. } else
  578. poll_mask |= HVC_POLL_READ;
  579. if (hvc_kicked)
  580. continue;
  581. if (poll_mask & HVC_POLL_WRITE) {
  582. yield();
  583. continue;
  584. }
  585. set_current_state(TASK_INTERRUPTIBLE);
  586. if (!hvc_kicked) {
  587. if (poll_mask == 0)
  588. schedule();
  589. else
  590. msleep_interruptible(TIMEOUT);
  591. }
  592. __set_current_state(TASK_RUNNING);
  593. } while (!kthread_should_stop());
  594. return 0;
  595. }
  596. static const struct tty_operations hvc_ops = {
  597. .open = hvc_open,
  598. .close = hvc_close,
  599. .write = hvc_write,
  600. .hangup = hvc_hangup,
  601. .unthrottle = hvc_unthrottle,
  602. .write_room = hvc_write_room,
  603. .chars_in_buffer = hvc_chars_in_buffer,
  604. };
  605. /* callback when the kboject ref count reaches zero. */
  606. static void destroy_hvc_struct(struct kobject *kobj)
  607. {
  608. struct hvc_struct *hp = container_of(kobj, struct hvc_struct, kobj);
  609. unsigned long flags;
  610. spin_lock(&hvc_structs_lock);
  611. spin_lock_irqsave(&hp->lock, flags);
  612. list_del(&(hp->next));
  613. spin_unlock_irqrestore(&hp->lock, flags);
  614. spin_unlock(&hvc_structs_lock);
  615. kfree(hp);
  616. }
  617. static struct kobj_type hvc_kobj_type = {
  618. .release = destroy_hvc_struct,
  619. };
  620. struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq,
  621. struct hv_ops *ops, int outbuf_size)
  622. {
  623. struct hvc_struct *hp;
  624. int i;
  625. hp = kmalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
  626. GFP_KERNEL);
  627. if (!hp)
  628. return ERR_PTR(-ENOMEM);
  629. memset(hp, 0x00, sizeof(*hp));
  630. hp->vtermno = vtermno;
  631. hp->irq = irq;
  632. hp->ops = ops;
  633. hp->outbuf_size = outbuf_size;
  634. hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
  635. kobject_init(&hp->kobj);
  636. hp->kobj.ktype = &hvc_kobj_type;
  637. spin_lock_init(&hp->lock);
  638. spin_lock(&hvc_structs_lock);
  639. /*
  640. * find index to use:
  641. * see if this vterm id matches one registered for console.
  642. */
  643. for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
  644. if (vtermnos[i] == hp->vtermno &&
  645. cons_ops[i] == hp->ops)
  646. break;
  647. /* no matching slot, just use a counter */
  648. if (i >= MAX_NR_HVC_CONSOLES)
  649. i = ++last_hvc;
  650. hp->index = i;
  651. list_add_tail(&(hp->next), &hvc_structs);
  652. spin_unlock(&hvc_structs_lock);
  653. return hp;
  654. }
  655. EXPORT_SYMBOL(hvc_alloc);
  656. int __devexit hvc_remove(struct hvc_struct *hp)
  657. {
  658. unsigned long flags;
  659. struct kobject *kobjp;
  660. struct tty_struct *tty;
  661. spin_lock_irqsave(&hp->lock, flags);
  662. tty = hp->tty;
  663. kobjp = &hp->kobj;
  664. if (hp->index < MAX_NR_HVC_CONSOLES)
  665. vtermnos[hp->index] = -1;
  666. /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
  667. spin_unlock_irqrestore(&hp->lock, flags);
  668. /*
  669. * We 'put' the instance that was grabbed when the kobject instance
  670. * was intialized using kobject_init(). Let the last holder of this
  671. * kobject cause it to be removed, which will probably be the tty_hangup
  672. * below.
  673. */
  674. kobject_put(kobjp);
  675. /*
  676. * This function call will auto chain call hvc_hangup. The tty should
  677. * always be valid at this time unless a simultaneous tty close already
  678. * cleaned up the hvc_struct.
  679. */
  680. if (tty)
  681. tty_hangup(tty);
  682. return 0;
  683. }
  684. EXPORT_SYMBOL(hvc_remove);
  685. /* Driver initialization. Follow console initialization. This is where the TTY
  686. * interfaces start to become available. */
  687. int __init hvc_init(void)
  688. {
  689. struct tty_driver *drv;
  690. /* We need more than hvc_count adapters due to hotplug additions. */
  691. drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
  692. if (!drv)
  693. return -ENOMEM;
  694. drv->owner = THIS_MODULE;
  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);