hvc_console.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. #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;
  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 canidate registered with hvc_instantiate().
  93. */
  94. static int last_hvc = -1;
  95. /*
  96. * Do not call this function with either the hvc_strucst_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. 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. void hvc_console_print(struct console *co, const char *b, unsigned count)
  133. {
  134. char c[N_OUTBUF] __ALIGNED__;
  135. unsigned i = 0, n = 0;
  136. int r, donecr = 0, index = co->index;
  137. /* Console access attempt outside of acceptable console range. */
  138. if (index >= MAX_NR_HVC_CONSOLES)
  139. return;
  140. /* This console adapter was removed so it is not useable. */
  141. if (vtermnos[index] < 0)
  142. return;
  143. while (count > 0 || i > 0) {
  144. if (count > 0 && i < sizeof(c)) {
  145. if (b[n] == '\n' && !donecr) {
  146. c[i++] = '\r';
  147. donecr = 1;
  148. } else {
  149. c[i++] = b[n++];
  150. donecr = 0;
  151. --count;
  152. }
  153. } else {
  154. r = cons_ops[index]->put_chars(vtermnos[index], c, i);
  155. if (r < 0) {
  156. /* throw away chars on error */
  157. i = 0;
  158. } else if (r > 0) {
  159. i -= r;
  160. if (i > 0)
  161. memmove(c, c+r, i);
  162. }
  163. }
  164. }
  165. }
  166. static struct tty_driver *hvc_console_device(struct console *c, int *index)
  167. {
  168. if (vtermnos[c->index] == -1)
  169. return NULL;
  170. *index = c->index;
  171. return hvc_driver;
  172. }
  173. static int __init hvc_console_setup(struct console *co, char *options)
  174. {
  175. if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES)
  176. return -ENODEV;
  177. if (vtermnos[co->index] == -1)
  178. return -ENODEV;
  179. return 0;
  180. }
  181. struct console hvc_con_driver = {
  182. .name = "hvc",
  183. .write = hvc_console_print,
  184. .device = hvc_console_device,
  185. .setup = hvc_console_setup,
  186. .flags = CON_PRINTBUFFER,
  187. .index = -1,
  188. };
  189. /*
  190. * Early console initialization. Preceeds driver initialization.
  191. *
  192. * (1) we are first, and the user specified another driver
  193. * -- index will remain -1
  194. * (2) we are first and the user specified no driver
  195. * -- index will be set to 0, then we will fail setup.
  196. * (3) we are first and the user specified our driver
  197. * -- index will be set to user specified driver, and we will fail
  198. * (4) we are after driver, and this initcall will register us
  199. * -- if the user didn't specify a driver then the console will match
  200. *
  201. * Note that for cases 2 and 3, we will match later when the io driver
  202. * calls hvc_instantiate() and call register again.
  203. */
  204. static int __init hvc_console_init(void)
  205. {
  206. register_console(&hvc_con_driver);
  207. return 0;
  208. }
  209. console_initcall(hvc_console_init);
  210. /*
  211. * hvc_instantiate() is an early console discovery method which locates
  212. * consoles * prior to the vio subsystem discovering them. Hotplugged
  213. * vty adapters do NOT get an hvc_instantiate() callback since they
  214. * appear after early console init.
  215. */
  216. int hvc_instantiate(uint32_t vtermno, int index, struct hv_ops *ops)
  217. {
  218. struct hvc_struct *hp;
  219. if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
  220. return -1;
  221. if (vtermnos[index] != -1)
  222. return -1;
  223. /* make sure no no tty has been registerd in this index */
  224. hp = hvc_get_by_index(index);
  225. if (hp) {
  226. kobject_put(&hp->kobj);
  227. return -1;
  228. }
  229. vtermnos[index] = vtermno;
  230. cons_ops[index] = ops;
  231. /* reserve all indices upto and including this index */
  232. if (last_hvc < index)
  233. last_hvc = index;
  234. /* if this index is what the user requested, then register
  235. * now (setup won't fail at this point). It's ok to just
  236. * call register again if previously .setup failed.
  237. */
  238. if (index == hvc_con_driver.index)
  239. register_console(&hvc_con_driver);
  240. return 0;
  241. }
  242. EXPORT_SYMBOL(hvc_instantiate);
  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 guarentee 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. #define HVC_POLL_READ 0x00000001
  469. #define HVC_POLL_WRITE 0x00000002
  470. static int hvc_poll(struct hvc_struct *hp)
  471. {
  472. struct tty_struct *tty;
  473. int i, n, poll_mask = 0;
  474. char buf[N_INBUF] __ALIGNED__;
  475. unsigned long flags;
  476. int read_total = 0;
  477. spin_lock_irqsave(&hp->lock, flags);
  478. /* Push pending writes */
  479. if (hp->n_outbuf > 0)
  480. hvc_push(hp);
  481. /* Reschedule us if still some write pending */
  482. if (hp->n_outbuf > 0)
  483. poll_mask |= HVC_POLL_WRITE;
  484. /* No tty attached, just skip */
  485. tty = hp->tty;
  486. if (tty == NULL)
  487. goto bail;
  488. /* Now check if we can get data (are we throttled ?) */
  489. if (test_bit(TTY_THROTTLED, &tty->flags))
  490. goto throttled;
  491. /* If we aren't interrupt driven and aren't throttled, we always
  492. * request a reschedule
  493. */
  494. if (hp->irq == 0)
  495. poll_mask |= HVC_POLL_READ;
  496. /* Read data if any */
  497. for (;;) {
  498. int count = tty_buffer_request_room(tty, N_INBUF);
  499. /* If flip is full, just reschedule a later read */
  500. if (count == 0) {
  501. poll_mask |= HVC_POLL_READ;
  502. break;
  503. }
  504. n = hp->ops->get_chars(hp->vtermno, buf, count);
  505. if (n <= 0) {
  506. /* Hangup the tty when disconnected from host */
  507. if (n == -EPIPE) {
  508. spin_unlock_irqrestore(&hp->lock, flags);
  509. tty_hangup(tty);
  510. spin_lock_irqsave(&hp->lock, flags);
  511. } else if ( n == -EAGAIN ) {
  512. /*
  513. * Some back-ends can only ensure a certain min
  514. * num of bytes read, which may be > 'count'.
  515. * Let the tty clear the flip buff to make room.
  516. */
  517. poll_mask |= HVC_POLL_READ;
  518. }
  519. break;
  520. }
  521. for (i = 0; i < n; ++i) {
  522. #ifdef CONFIG_MAGIC_SYSRQ
  523. if (hp->index == hvc_con_driver.index) {
  524. /* Handle the SysRq Hack */
  525. /* XXX should support a sequence */
  526. if (buf[i] == '\x0f') { /* ^O */
  527. sysrq_pressed = 1;
  528. continue;
  529. } else if (sysrq_pressed) {
  530. handle_sysrq(buf[i], tty);
  531. sysrq_pressed = 0;
  532. continue;
  533. }
  534. }
  535. #endif /* CONFIG_MAGIC_SYSRQ */
  536. tty_insert_flip_char(tty, buf[i], 0);
  537. }
  538. read_total += n;
  539. }
  540. throttled:
  541. /* Wakeup write queue if necessary */
  542. if (hp->do_wakeup) {
  543. hp->do_wakeup = 0;
  544. tty_wakeup(tty);
  545. }
  546. bail:
  547. spin_unlock_irqrestore(&hp->lock, flags);
  548. if (read_total)
  549. tty_flip_buffer_push(tty);
  550. return poll_mask;
  551. }
  552. #if defined(CONFIG_XMON) && defined(CONFIG_SMP)
  553. extern cpumask_t cpus_in_xmon;
  554. #else
  555. static const cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  556. #endif
  557. /*
  558. * This kthread is either polling or interrupt driven. This is determined by
  559. * calling hvc_poll() who determines whether a console adapter support
  560. * interrupts.
  561. */
  562. int khvcd(void *unused)
  563. {
  564. int poll_mask;
  565. struct hvc_struct *hp;
  566. __set_current_state(TASK_RUNNING);
  567. do {
  568. poll_mask = 0;
  569. hvc_kicked = 0;
  570. try_to_freeze();
  571. wmb();
  572. if (cpus_empty(cpus_in_xmon)) {
  573. spin_lock(&hvc_structs_lock);
  574. list_for_each_entry(hp, &hvc_structs, next) {
  575. poll_mask |= hvc_poll(hp);
  576. }
  577. spin_unlock(&hvc_structs_lock);
  578. } else
  579. poll_mask |= HVC_POLL_READ;
  580. if (hvc_kicked)
  581. continue;
  582. if (poll_mask & HVC_POLL_WRITE) {
  583. yield();
  584. continue;
  585. }
  586. set_current_state(TASK_INTERRUPTIBLE);
  587. if (!hvc_kicked) {
  588. if (poll_mask == 0)
  589. schedule();
  590. else
  591. msleep_interruptible(TIMEOUT);
  592. }
  593. __set_current_state(TASK_RUNNING);
  594. } while (!kthread_should_stop());
  595. return 0;
  596. }
  597. static const struct tty_operations hvc_ops = {
  598. .open = hvc_open,
  599. .close = hvc_close,
  600. .write = hvc_write,
  601. .hangup = hvc_hangup,
  602. .unthrottle = hvc_unthrottle,
  603. .write_room = hvc_write_room,
  604. .chars_in_buffer = hvc_chars_in_buffer,
  605. };
  606. /* callback when the kboject ref count reaches zero. */
  607. static void destroy_hvc_struct(struct kobject *kobj)
  608. {
  609. struct hvc_struct *hp = container_of(kobj, struct hvc_struct, kobj);
  610. unsigned long flags;
  611. spin_lock(&hvc_structs_lock);
  612. spin_lock_irqsave(&hp->lock, flags);
  613. list_del(&(hp->next));
  614. spin_unlock_irqrestore(&hp->lock, flags);
  615. spin_unlock(&hvc_structs_lock);
  616. kfree(hp);
  617. }
  618. static struct kobj_type hvc_kobj_type = {
  619. .release = destroy_hvc_struct,
  620. };
  621. struct hvc_struct __devinit *hvc_alloc(uint32_t vtermno, int irq,
  622. struct hv_ops *ops, int outbuf_size)
  623. {
  624. struct hvc_struct *hp;
  625. int i;
  626. hp = kmalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
  627. 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. hp->outbuf_size = outbuf_size;
  635. hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
  636. kobject_init(&hp->kobj);
  637. hp->kobj.ktype = &hvc_kobj_type;
  638. spin_lock_init(&hp->lock);
  639. spin_lock(&hvc_structs_lock);
  640. /*
  641. * find index to use:
  642. * see if this vterm id matches one registered for console.
  643. */
  644. for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
  645. if (vtermnos[i] == hp->vtermno &&
  646. cons_ops[i] == hp->ops)
  647. break;
  648. /* no matching slot, just use a counter */
  649. if (i >= MAX_NR_HVC_CONSOLES)
  650. i = ++last_hvc;
  651. hp->index = i;
  652. list_add_tail(&(hp->next), &hvc_structs);
  653. spin_unlock(&hvc_structs_lock);
  654. return hp;
  655. }
  656. EXPORT_SYMBOL(hvc_alloc);
  657. int __devexit hvc_remove(struct hvc_struct *hp)
  658. {
  659. unsigned long flags;
  660. struct kobject *kobjp;
  661. struct tty_struct *tty;
  662. spin_lock_irqsave(&hp->lock, flags);
  663. tty = hp->tty;
  664. kobjp = &hp->kobj;
  665. if (hp->index < MAX_NR_HVC_CONSOLES)
  666. vtermnos[hp->index] = -1;
  667. /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
  668. spin_unlock_irqrestore(&hp->lock, flags);
  669. /*
  670. * We 'put' the instance that was grabbed when the kobject instance
  671. * was intialized using kobject_init(). Let the last holder of this
  672. * kobject cause it to be removed, which will probably be the tty_hangup
  673. * below.
  674. */
  675. kobject_put(kobjp);
  676. /*
  677. * This function call will auto chain call hvc_hangup. The tty should
  678. * always be valid at this time unless a simultaneous tty close already
  679. * cleaned up the hvc_struct.
  680. */
  681. if (tty)
  682. tty_hangup(tty);
  683. return 0;
  684. }
  685. EXPORT_SYMBOL(hvc_remove);
  686. /* Driver initialization. Follow console initialization. This is where the TTY
  687. * interfaces start to become available. */
  688. int __init hvc_init(void)
  689. {
  690. struct tty_driver *drv;
  691. /* We need more than hvc_count adapters due to hotplug additions. */
  692. drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
  693. if (!drv)
  694. return -ENOMEM;
  695. drv->owner = THIS_MODULE;
  696. drv->driver_name = "hvc";
  697. drv->name = "hvc";
  698. drv->major = HVC_MAJOR;
  699. drv->minor_start = HVC_MINOR;
  700. drv->type = TTY_DRIVER_TYPE_SYSTEM;
  701. drv->init_termios = tty_std_termios;
  702. drv->flags = TTY_DRIVER_REAL_RAW;
  703. tty_set_operations(drv, &hvc_ops);
  704. /* Always start the kthread because there can be hotplug vty adapters
  705. * added later. */
  706. hvc_task = kthread_run(khvcd, NULL, "khvcd");
  707. if (IS_ERR(hvc_task)) {
  708. panic("Couldn't create kthread for console.\n");
  709. put_tty_driver(drv);
  710. return -EIO;
  711. }
  712. if (tty_register_driver(drv))
  713. panic("Couldn't register hvc console driver\n");
  714. mb();
  715. hvc_driver = drv;
  716. return 0;
  717. }
  718. module_init(hvc_init);
  719. /* This isn't particularily necessary due to this being a console driver
  720. * but it is nice to be thorough.
  721. */
  722. static void __exit hvc_exit(void)
  723. {
  724. kthread_stop(hvc_task);
  725. tty_unregister_driver(hvc_driver);
  726. /* return tty_struct instances allocated in hvc_init(). */
  727. put_tty_driver(hvc_driver);
  728. unregister_console(&hvc_con_driver);
  729. }
  730. module_exit(hvc_exit);