hvc_console.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. #include <asm/vio.h>
  44. #define HVC_MAJOR 229
  45. #define HVC_MINOR 0
  46. #define TIMEOUT (10)
  47. /*
  48. * Wait this long per iteration while trying to push buffered data to the
  49. * hypervisor before allowing the tty to complete a close operation.
  50. */
  51. #define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
  52. /*
  53. * The Linux TTY code does not support dynamic addition of tty derived devices
  54. * so we need to know how many tty devices we might need when space is allocated
  55. * for the tty device. Since this driver supports hotplug of vty adapters we
  56. * need to make sure we have enough allocated.
  57. */
  58. #define HVC_ALLOC_TTY_ADAPTERS 8
  59. #define N_OUTBUF 16
  60. #define N_INBUF 16
  61. #define __ALIGNED__ __attribute__((__aligned__(8)))
  62. static struct tty_driver *hvc_driver;
  63. static struct task_struct *hvc_task;
  64. /* Picks up late kicks after list walk but before schedule() */
  65. static int hvc_kicked;
  66. #ifdef CONFIG_MAGIC_SYSRQ
  67. static int sysrq_pressed;
  68. #endif
  69. struct hvc_struct {
  70. spinlock_t lock;
  71. int index;
  72. struct tty_struct *tty;
  73. unsigned int count;
  74. int do_wakeup;
  75. char outbuf[N_OUTBUF] __ALIGNED__;
  76. int n_outbuf;
  77. uint32_t vtermno;
  78. int irq_requested;
  79. int irq;
  80. struct list_head next;
  81. struct kobject kobj; /* ref count & hvc_struct lifetime */
  82. struct vio_dev *vdev;
  83. };
  84. /* dynamic list of hvc_struct instances */
  85. static struct list_head hvc_structs = LIST_HEAD_INIT(hvc_structs);
  86. /*
  87. * Protect the list of hvc_struct instances from inserts and removals during
  88. * list traversal.
  89. */
  90. static DEFINE_SPINLOCK(hvc_structs_lock);
  91. /*
  92. * This value is used to assign a tty->index value to a hvc_struct based
  93. * upon order of exposure via hvc_probe(), when we can not match it to
  94. * a console canidate registered with hvc_instantiate().
  95. */
  96. static int last_hvc = -1;
  97. /*
  98. * Do not call this function with either the hvc_strucst_lock or the hvc_struct
  99. * lock held. If successful, this function increments the kobject reference
  100. * count against the target hvc_struct so it should be released when finished.
  101. */
  102. struct hvc_struct *hvc_get_by_index(int index)
  103. {
  104. struct hvc_struct *hp;
  105. unsigned long flags;
  106. spin_lock(&hvc_structs_lock);
  107. list_for_each_entry(hp, &hvc_structs, next) {
  108. spin_lock_irqsave(&hp->lock, flags);
  109. if (hp->index == index) {
  110. kobject_get(&hp->kobj);
  111. spin_unlock_irqrestore(&hp->lock, flags);
  112. spin_unlock(&hvc_structs_lock);
  113. return hp;
  114. }
  115. spin_unlock_irqrestore(&hp->lock, flags);
  116. }
  117. hp = NULL;
  118. spin_unlock(&hvc_structs_lock);
  119. return hp;
  120. }
  121. /*
  122. * Initial console vtermnos for console API usage prior to full console
  123. * initialization. Any vty adapter outside this range will not have usable
  124. * console interfaces but can still be used as a tty device. This has to be
  125. * static because kmalloc will not work during early console init.
  126. */
  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;
  138. /* Console access attempt outside of acceptable console range. */
  139. if (co->index >= MAX_NR_HVC_CONSOLES)
  140. return;
  141. /* This console adapter was removed so it is not useable. */
  142. if (vtermnos[co->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 = hvc_put_chars(vtermnos[co->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)
  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. /* 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. /* 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. hp->tty = tty;
  289. /* Save for request_irq outside of spin_lock. */
  290. irq = hp->irq;
  291. if (irq != NO_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 != NO_IRQ)
  297. rc = request_irq(irq, hvc_handle_interrupt, SA_INTERRUPT, "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 = NO_IRQ;
  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 != NO_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 = NO_IRQ;
  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 != NO_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 = hvc_put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
  402. if (n <= 0) {
  403. if (n == 0)
  404. return;
  405. /* throw away output on error; this happens when
  406. there is no session connected to the vterm. */
  407. hp->n_outbuf = 0;
  408. } else
  409. hp->n_outbuf -= n;
  410. if (hp->n_outbuf > 0)
  411. memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
  412. else
  413. hp->do_wakeup = 1;
  414. }
  415. static inline int __hvc_write_kernel(struct hvc_struct *hp,
  416. const unsigned char *buf, int count)
  417. {
  418. unsigned long flags;
  419. int rsize, written = 0;
  420. spin_lock_irqsave(&hp->lock, flags);
  421. /* Push pending writes */
  422. if (hp->n_outbuf > 0)
  423. hvc_push(hp);
  424. while (count > 0 && (rsize = N_OUTBUF - hp->n_outbuf) > 0) {
  425. if (rsize > count)
  426. rsize = count;
  427. memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
  428. count -= rsize;
  429. buf += rsize;
  430. hp->n_outbuf += rsize;
  431. written += rsize;
  432. hvc_push(hp);
  433. }
  434. spin_unlock_irqrestore(&hp->lock, flags);
  435. return written;
  436. }
  437. static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
  438. {
  439. struct hvc_struct *hp = tty->driver_data;
  440. int written;
  441. /* This write was probably executed during a tty close. */
  442. if (!hp)
  443. return -EPIPE;
  444. if (hp->count <= 0)
  445. return -EIO;
  446. written = __hvc_write_kernel(hp, buf, count);
  447. /*
  448. * Racy, but harmless, kick thread if there is still pending data.
  449. * There really is nothing wrong with kicking the thread, even if there
  450. * is no buffered data.
  451. */
  452. if (hp->n_outbuf)
  453. hvc_kick();
  454. return written;
  455. }
  456. /*
  457. * This is actually a contract between the driver and the tty layer outlining
  458. * how much write room the driver can guarentee will be sent OR BUFFERED. This
  459. * driver MUST honor the return value.
  460. */
  461. static int hvc_write_room(struct tty_struct *tty)
  462. {
  463. struct hvc_struct *hp = tty->driver_data;
  464. if (!hp)
  465. return -1;
  466. return N_OUTBUF - hp->n_outbuf;
  467. }
  468. static int hvc_chars_in_buffer(struct tty_struct *tty)
  469. {
  470. struct hvc_struct *hp = tty->driver_data;
  471. if (!hp)
  472. return -1;
  473. return hp->n_outbuf;
  474. }
  475. #define HVC_POLL_READ 0x00000001
  476. #define HVC_POLL_WRITE 0x00000002
  477. #define HVC_POLL_QUICK 0x00000004
  478. static int hvc_poll(struct hvc_struct *hp)
  479. {
  480. struct tty_struct *tty;
  481. int i, n, poll_mask = 0;
  482. char buf[N_INBUF] __ALIGNED__;
  483. unsigned long flags;
  484. int read_total = 0;
  485. spin_lock_irqsave(&hp->lock, flags);
  486. /* Push pending writes */
  487. if (hp->n_outbuf > 0)
  488. hvc_push(hp);
  489. /* Reschedule us if still some write pending */
  490. if (hp->n_outbuf > 0)
  491. poll_mask |= HVC_POLL_WRITE;
  492. /* No tty attached, just skip */
  493. tty = hp->tty;
  494. if (tty == NULL)
  495. goto bail;
  496. /* Now check if we can get data (are we throttled ?) */
  497. if (test_bit(TTY_THROTTLED, &tty->flags))
  498. goto throttled;
  499. /* If we aren't interrupt driven and aren't throttled, we always
  500. * request a reschedule
  501. */
  502. if (hp->irq == NO_IRQ)
  503. poll_mask |= HVC_POLL_READ;
  504. /* Read data if any */
  505. for (;;) {
  506. int count = N_INBUF;
  507. if (count > (TTY_FLIPBUF_SIZE - tty->flip.count))
  508. count = TTY_FLIPBUF_SIZE - tty->flip.count;
  509. /* If flip is full, just reschedule a later read */
  510. if (count == 0) {
  511. poll_mask |= HVC_POLL_READ;
  512. break;
  513. }
  514. n = hvc_get_chars(hp->vtermno, buf, count);
  515. if (n <= 0) {
  516. /* Hangup the tty when disconnected from host */
  517. if (n == -EPIPE) {
  518. spin_unlock_irqrestore(&hp->lock, flags);
  519. tty_hangup(tty);
  520. spin_lock_irqsave(&hp->lock, flags);
  521. }
  522. break;
  523. }
  524. for (i = 0; i < n; ++i) {
  525. #ifdef CONFIG_MAGIC_SYSRQ
  526. if (hp->index == hvc_con_driver.index) {
  527. /* Handle the SysRq Hack */
  528. /* XXX should support a sequence */
  529. if (buf[i] == '\x0f') { /* ^O */
  530. sysrq_pressed = 1;
  531. continue;
  532. } else if (sysrq_pressed) {
  533. handle_sysrq(buf[i], NULL, tty);
  534. sysrq_pressed = 0;
  535. continue;
  536. }
  537. }
  538. #endif /* CONFIG_MAGIC_SYSRQ */
  539. tty_insert_flip_char(tty, buf[i], 0);
  540. }
  541. if (tty->flip.count)
  542. tty_schedule_flip(tty);
  543. /*
  544. * Account for the total amount read in one loop, and if above
  545. * 64 bytes, we do a quick schedule loop to let the tty grok
  546. * the data and eventually throttle us.
  547. */
  548. read_total += n;
  549. if (read_total >= 64) {
  550. poll_mask |= HVC_POLL_QUICK;
  551. break;
  552. }
  553. }
  554. throttled:
  555. /* Wakeup write queue if necessary */
  556. if (hp->do_wakeup) {
  557. hp->do_wakeup = 0;
  558. tty_wakeup(tty);
  559. }
  560. bail:
  561. spin_unlock_irqrestore(&hp->lock, flags);
  562. return poll_mask;
  563. }
  564. #if defined(CONFIG_XMON) && defined(CONFIG_SMP)
  565. extern cpumask_t cpus_in_xmon;
  566. #else
  567. static const cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  568. #endif
  569. /*
  570. * This kthread is either polling or interrupt driven. This is determined by
  571. * calling hvc_poll() who determines whether a console adapter support
  572. * interrupts.
  573. */
  574. int khvcd(void *unused)
  575. {
  576. int poll_mask;
  577. struct hvc_struct *hp;
  578. __set_current_state(TASK_RUNNING);
  579. do {
  580. poll_mask = 0;
  581. hvc_kicked = 0;
  582. wmb();
  583. if (cpus_empty(cpus_in_xmon)) {
  584. spin_lock(&hvc_structs_lock);
  585. list_for_each_entry(hp, &hvc_structs, next) {
  586. poll_mask |= hvc_poll(hp);
  587. }
  588. spin_unlock(&hvc_structs_lock);
  589. } else
  590. poll_mask |= HVC_POLL_READ;
  591. if (hvc_kicked)
  592. continue;
  593. if (poll_mask & HVC_POLL_QUICK) {
  594. yield();
  595. continue;
  596. }
  597. set_current_state(TASK_INTERRUPTIBLE);
  598. if (!hvc_kicked) {
  599. if (poll_mask == 0)
  600. schedule();
  601. else
  602. msleep_interruptible(TIMEOUT);
  603. }
  604. __set_current_state(TASK_RUNNING);
  605. } while (!kthread_should_stop());
  606. return 0;
  607. }
  608. static struct tty_operations hvc_ops = {
  609. .open = hvc_open,
  610. .close = hvc_close,
  611. .write = hvc_write,
  612. .hangup = hvc_hangup,
  613. .unthrottle = hvc_unthrottle,
  614. .write_room = hvc_write_room,
  615. .chars_in_buffer = hvc_chars_in_buffer,
  616. };
  617. /* callback when the kboject ref count reaches zero. */
  618. static void destroy_hvc_struct(struct kobject *kobj)
  619. {
  620. struct hvc_struct *hp = container_of(kobj, struct hvc_struct, kobj);
  621. unsigned long flags;
  622. spin_lock(&hvc_structs_lock);
  623. spin_lock_irqsave(&hp->lock, flags);
  624. list_del(&(hp->next));
  625. spin_unlock_irqrestore(&hp->lock, flags);
  626. spin_unlock(&hvc_structs_lock);
  627. kfree(hp);
  628. }
  629. static struct kobj_type hvc_kobj_type = {
  630. .release = destroy_hvc_struct,
  631. };
  632. static int __devinit hvc_probe(
  633. struct vio_dev *dev,
  634. const struct vio_device_id *id)
  635. {
  636. struct hvc_struct *hp;
  637. int i;
  638. /* probed with invalid parameters. */
  639. if (!dev || !id)
  640. return -EPERM;
  641. hp = kmalloc(sizeof(*hp), GFP_KERNEL);
  642. if (!hp)
  643. return -ENOMEM;
  644. memset(hp, 0x00, sizeof(*hp));
  645. hp->vtermno = dev->unit_address;
  646. hp->vdev = dev;
  647. hp->vdev->dev.driver_data = hp;
  648. hp->irq = dev->irq;
  649. kobject_init(&hp->kobj);
  650. hp->kobj.ktype = &hvc_kobj_type;
  651. spin_lock_init(&hp->lock);
  652. spin_lock(&hvc_structs_lock);
  653. /*
  654. * find index to use:
  655. * see if this vterm id matches one registered for console.
  656. */
  657. for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
  658. if (vtermnos[i] == hp->vtermno)
  659. break;
  660. /* no matching slot, just use a counter */
  661. if (i >= MAX_NR_HVC_CONSOLES)
  662. i = ++last_hvc;
  663. hp->index = i;
  664. list_add_tail(&(hp->next), &hvc_structs);
  665. spin_unlock(&hvc_structs_lock);
  666. return 0;
  667. }
  668. static int __devexit hvc_remove(struct vio_dev *dev)
  669. {
  670. struct hvc_struct *hp = dev->dev.driver_data;
  671. unsigned long flags;
  672. struct kobject *kobjp;
  673. struct tty_struct *tty;
  674. spin_lock_irqsave(&hp->lock, flags);
  675. tty = hp->tty;
  676. kobjp = &hp->kobj;
  677. if (hp->index < MAX_NR_HVC_CONSOLES)
  678. vtermnos[hp->index] = -1;
  679. /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
  680. spin_unlock_irqrestore(&hp->lock, flags);
  681. /*
  682. * We 'put' the instance that was grabbed when the kobject instance
  683. * was intialized using kobject_init(). Let the last holder of this
  684. * kobject cause it to be removed, which will probably be the tty_hangup
  685. * below.
  686. */
  687. kobject_put(kobjp);
  688. /*
  689. * This function call will auto chain call hvc_hangup. The tty should
  690. * always be valid at this time unless a simultaneous tty close already
  691. * cleaned up the hvc_struct.
  692. */
  693. if (tty)
  694. tty_hangup(tty);
  695. return 0;
  696. }
  697. char hvc_driver_name[] = "hvc_console";
  698. static struct vio_device_id hvc_driver_table[] __devinitdata= {
  699. {"serial", "hvterm1"},
  700. { NULL, }
  701. };
  702. MODULE_DEVICE_TABLE(vio, hvc_driver_table);
  703. static struct vio_driver hvc_vio_driver = {
  704. .name = hvc_driver_name,
  705. .id_table = hvc_driver_table,
  706. .probe = hvc_probe,
  707. .remove = hvc_remove,
  708. };
  709. /* Driver initialization. Follow console initialization. This is where the TTY
  710. * interfaces start to become available. */
  711. int __init hvc_init(void)
  712. {
  713. int rc;
  714. /* We need more than hvc_count adapters due to hotplug additions. */
  715. hvc_driver = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
  716. if (!hvc_driver)
  717. return -ENOMEM;
  718. hvc_driver->owner = THIS_MODULE;
  719. hvc_driver->devfs_name = "hvc/";
  720. hvc_driver->driver_name = "hvc";
  721. hvc_driver->name = "hvc";
  722. hvc_driver->major = HVC_MAJOR;
  723. hvc_driver->minor_start = HVC_MINOR;
  724. hvc_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  725. hvc_driver->init_termios = tty_std_termios;
  726. hvc_driver->flags = TTY_DRIVER_REAL_RAW;
  727. tty_set_operations(hvc_driver, &hvc_ops);
  728. if (tty_register_driver(hvc_driver))
  729. panic("Couldn't register hvc console driver\n");
  730. /* Always start the kthread because there can be hotplug vty adapters
  731. * added later. */
  732. hvc_task = kthread_run(khvcd, NULL, "khvcd");
  733. if (IS_ERR(hvc_task)) {
  734. panic("Couldn't create kthread for console.\n");
  735. put_tty_driver(hvc_driver);
  736. return -EIO;
  737. }
  738. /* Register as a vio device to receive callbacks */
  739. rc = vio_register_driver(&hvc_vio_driver);
  740. return rc;
  741. }
  742. module_init(hvc_init);
  743. /* This isn't particularily necessary due to this being a console driver
  744. * but it is nice to be thorough.
  745. */
  746. static void __exit hvc_exit(void)
  747. {
  748. kthread_stop(hvc_task);
  749. vio_unregister_driver(&hvc_vio_driver);
  750. tty_unregister_driver(hvc_driver);
  751. /* return tty_struct instances allocated in hvc_init(). */
  752. put_tty_driver(hvc_driver);
  753. unregister_console(&hvc_con_driver);
  754. }
  755. module_exit(hvc_exit);