hvc_console.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 <asm/hvconsole.h>
  42. #include <asm/vio.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. int irq_requested;
  78. int irq;
  79. struct list_head next;
  80. struct kobject kobj; /* ref count & hvc_struct lifetime */
  81. struct vio_dev *vdev;
  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 uint32_t vtermnos[MAX_NR_HVC_CONSOLES];
  127. /* Used for accounting purposes */
  128. static int num_vterms = 0;
  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. *index = c->index;
  170. return hvc_driver;
  171. }
  172. static int __init hvc_console_setup(struct console *co, char *options)
  173. {
  174. return 0;
  175. }
  176. struct console hvc_con_driver = {
  177. .name = "hvc",
  178. .write = hvc_console_print,
  179. .device = hvc_console_device,
  180. .setup = hvc_console_setup,
  181. .flags = CON_PRINTBUFFER,
  182. .index = -1,
  183. };
  184. /* Early console initialization. Preceeds driver initialization. */
  185. static int __init hvc_console_init(void)
  186. {
  187. int i;
  188. for (i=0; i<MAX_NR_HVC_CONSOLES; i++)
  189. vtermnos[i] = -1;
  190. num_vterms = hvc_find_vtys();
  191. register_console(&hvc_con_driver);
  192. return 0;
  193. }
  194. console_initcall(hvc_console_init);
  195. /*
  196. * hvc_instantiate() is an early console discovery method which locates
  197. * consoles * prior to the vio subsystem discovering them. Hotplugged
  198. * vty adapters do NOT get an hvc_instantiate() callback since they
  199. * appear after early console init.
  200. */
  201. int hvc_instantiate(uint32_t vtermno, int index)
  202. {
  203. if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
  204. return -1;
  205. if (vtermnos[index] != -1)
  206. return -1;
  207. vtermnos[index] = vtermno;
  208. /* reserve all indices upto and including this index */
  209. if (last_hvc < index)
  210. last_hvc = index;
  211. return 0;
  212. }
  213. /* Wake the sleeping khvcd */
  214. static void hvc_kick(void)
  215. {
  216. hvc_kicked = 1;
  217. wake_up_process(hvc_task);
  218. }
  219. static int hvc_poll(struct hvc_struct *hp);
  220. /*
  221. * NOTE: This API isn't used if the console adapter doesn't support interrupts.
  222. * In this case the console is poll driven.
  223. */
  224. static irqreturn_t hvc_handle_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
  225. {
  226. /* if hvc_poll request a repoll, then kick the hvcd thread */
  227. if (hvc_poll(dev_instance))
  228. hvc_kick();
  229. return IRQ_HANDLED;
  230. }
  231. static void hvc_unthrottle(struct tty_struct *tty)
  232. {
  233. hvc_kick();
  234. }
  235. /*
  236. * The TTY interface won't be used until after the vio layer has exposed the vty
  237. * adapter to the kernel.
  238. */
  239. static int hvc_open(struct tty_struct *tty, struct file * filp)
  240. {
  241. struct hvc_struct *hp;
  242. unsigned long flags;
  243. int irq = NO_IRQ;
  244. int rc = 0;
  245. struct kobject *kobjp;
  246. /* Auto increments kobject reference if found. */
  247. if (!(hp = hvc_get_by_index(tty->index))) {
  248. printk(KERN_WARNING "hvc_console: tty open failed, no vty associated with tty.\n");
  249. return -ENODEV;
  250. }
  251. spin_lock_irqsave(&hp->lock, flags);
  252. /* Check and then increment for fast path open. */
  253. if (hp->count++ > 0) {
  254. spin_unlock_irqrestore(&hp->lock, flags);
  255. hvc_kick();
  256. return 0;
  257. } /* else count == 0 */
  258. tty->driver_data = hp;
  259. hp->tty = tty;
  260. /* Save for request_irq outside of spin_lock. */
  261. irq = hp->irq;
  262. if (irq != NO_IRQ)
  263. hp->irq_requested = 1;
  264. kobjp = &hp->kobj;
  265. spin_unlock_irqrestore(&hp->lock, flags);
  266. /* check error, fallback to non-irq */
  267. if (irq != NO_IRQ)
  268. rc = request_irq(irq, hvc_handle_interrupt, SA_INTERRUPT, "hvc_console", hp);
  269. /*
  270. * If the request_irq() fails and we return an error. The tty layer
  271. * will call hvc_close() after a failed open but we don't want to clean
  272. * up there so we'll clean up here and clear out the previously set
  273. * tty fields and return the kobject reference.
  274. */
  275. if (rc) {
  276. spin_lock_irqsave(&hp->lock, flags);
  277. hp->tty = NULL;
  278. hp->irq_requested = 0;
  279. spin_unlock_irqrestore(&hp->lock, flags);
  280. tty->driver_data = NULL;
  281. kobject_put(kobjp);
  282. printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
  283. }
  284. /* Force wakeup of the polling thread */
  285. hvc_kick();
  286. return rc;
  287. }
  288. static void hvc_close(struct tty_struct *tty, struct file * filp)
  289. {
  290. struct hvc_struct *hp;
  291. struct kobject *kobjp;
  292. int irq = NO_IRQ;
  293. unsigned long flags;
  294. if (tty_hung_up_p(filp))
  295. return;
  296. /*
  297. * No driver_data means that this close was issued after a failed
  298. * hvc_open by the tty layer's release_dev() function and we can just
  299. * exit cleanly because the kobject reference wasn't made.
  300. */
  301. if (!tty->driver_data)
  302. return;
  303. hp = tty->driver_data;
  304. spin_lock_irqsave(&hp->lock, flags);
  305. kobjp = &hp->kobj;
  306. if (--hp->count == 0) {
  307. if (hp->irq_requested)
  308. irq = hp->irq;
  309. hp->irq_requested = 0;
  310. /* We are done with the tty pointer now. */
  311. hp->tty = NULL;
  312. spin_unlock_irqrestore(&hp->lock, flags);
  313. /*
  314. * Chain calls chars_in_buffer() and returns immediately if
  315. * there is no buffered data otherwise sleeps on a wait queue
  316. * waking periodically to check chars_in_buffer().
  317. */
  318. tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
  319. if (irq != NO_IRQ)
  320. free_irq(irq, hp);
  321. } else {
  322. if (hp->count < 0)
  323. printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
  324. hp->vtermno, hp->count);
  325. spin_unlock_irqrestore(&hp->lock, flags);
  326. }
  327. kobject_put(kobjp);
  328. }
  329. static void hvc_hangup(struct tty_struct *tty)
  330. {
  331. struct hvc_struct *hp = tty->driver_data;
  332. unsigned long flags;
  333. int irq = NO_IRQ;
  334. int temp_open_count;
  335. struct kobject *kobjp;
  336. if (!hp)
  337. return;
  338. spin_lock_irqsave(&hp->lock, flags);
  339. /*
  340. * The N_TTY line discipline has problems such that in a close vs
  341. * open->hangup case this can be called after the final close so prevent
  342. * that from happening for now.
  343. */
  344. if (hp->count <= 0) {
  345. spin_unlock_irqrestore(&hp->lock, flags);
  346. return;
  347. }
  348. kobjp = &hp->kobj;
  349. temp_open_count = hp->count;
  350. hp->count = 0;
  351. hp->n_outbuf = 0;
  352. hp->tty = NULL;
  353. if (hp->irq_requested)
  354. /* Saved for use outside of spin_lock. */
  355. irq = hp->irq;
  356. hp->irq_requested = 0;
  357. spin_unlock_irqrestore(&hp->lock, flags);
  358. if (irq != NO_IRQ)
  359. free_irq(irq, hp);
  360. while(temp_open_count) {
  361. --temp_open_count;
  362. kobject_put(kobjp);
  363. }
  364. }
  365. /*
  366. * Push buffered characters whether they were just recently buffered or waiting
  367. * on a blocked hypervisor. Call this function with hp->lock held.
  368. */
  369. static void hvc_push(struct hvc_struct *hp)
  370. {
  371. int n;
  372. n = hvc_put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
  373. if (n <= 0) {
  374. if (n == 0)
  375. return;
  376. /* throw away output on error; this happens when
  377. there is no session connected to the vterm. */
  378. hp->n_outbuf = 0;
  379. } else
  380. hp->n_outbuf -= n;
  381. if (hp->n_outbuf > 0)
  382. memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
  383. else
  384. hp->do_wakeup = 1;
  385. }
  386. static inline int __hvc_write_kernel(struct hvc_struct *hp,
  387. const unsigned char *buf, int count)
  388. {
  389. unsigned long flags;
  390. int rsize, written = 0;
  391. spin_lock_irqsave(&hp->lock, flags);
  392. /* Push pending writes */
  393. if (hp->n_outbuf > 0)
  394. hvc_push(hp);
  395. while (count > 0 && (rsize = N_OUTBUF - hp->n_outbuf) > 0) {
  396. if (rsize > count)
  397. rsize = count;
  398. memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
  399. count -= rsize;
  400. buf += rsize;
  401. hp->n_outbuf += rsize;
  402. written += rsize;
  403. hvc_push(hp);
  404. }
  405. spin_unlock_irqrestore(&hp->lock, flags);
  406. return written;
  407. }
  408. static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
  409. {
  410. struct hvc_struct *hp = tty->driver_data;
  411. int written;
  412. /* This write was probably executed during a tty close. */
  413. if (!hp)
  414. return -EPIPE;
  415. if (hp->count <= 0)
  416. return -EIO;
  417. written = __hvc_write_kernel(hp, buf, count);
  418. /*
  419. * Racy, but harmless, kick thread if there is still pending data.
  420. * There really is nothing wrong with kicking the thread, even if there
  421. * is no buffered data.
  422. */
  423. if (hp->n_outbuf)
  424. hvc_kick();
  425. return written;
  426. }
  427. /*
  428. * This is actually a contract between the driver and the tty layer outlining
  429. * how much write room the driver can guarentee will be sent OR BUFFERED. This
  430. * driver MUST honor the return value.
  431. */
  432. static int hvc_write_room(struct tty_struct *tty)
  433. {
  434. struct hvc_struct *hp = tty->driver_data;
  435. if (!hp)
  436. return -1;
  437. return N_OUTBUF - hp->n_outbuf;
  438. }
  439. static int hvc_chars_in_buffer(struct tty_struct *tty)
  440. {
  441. struct hvc_struct *hp = tty->driver_data;
  442. if (!hp)
  443. return -1;
  444. return hp->n_outbuf;
  445. }
  446. #define HVC_POLL_READ 0x00000001
  447. #define HVC_POLL_WRITE 0x00000002
  448. #define HVC_POLL_QUICK 0x00000004
  449. static int hvc_poll(struct hvc_struct *hp)
  450. {
  451. struct tty_struct *tty;
  452. int i, n, poll_mask = 0;
  453. char buf[N_INBUF] __ALIGNED__;
  454. unsigned long flags;
  455. int read_total = 0;
  456. spin_lock_irqsave(&hp->lock, flags);
  457. /* Push pending writes */
  458. if (hp->n_outbuf > 0)
  459. hvc_push(hp);
  460. /* Reschedule us if still some write pending */
  461. if (hp->n_outbuf > 0)
  462. poll_mask |= HVC_POLL_WRITE;
  463. /* No tty attached, just skip */
  464. tty = hp->tty;
  465. if (tty == NULL)
  466. goto bail;
  467. /* Now check if we can get data (are we throttled ?) */
  468. if (test_bit(TTY_THROTTLED, &tty->flags))
  469. goto throttled;
  470. /* If we aren't interrupt driven and aren't throttled, we always
  471. * request a reschedule
  472. */
  473. if (hp->irq == NO_IRQ)
  474. poll_mask |= HVC_POLL_READ;
  475. /* Read data if any */
  476. for (;;) {
  477. int count = N_INBUF;
  478. if (count > (TTY_FLIPBUF_SIZE - tty->flip.count))
  479. count = TTY_FLIPBUF_SIZE - tty->flip.count;
  480. /* If flip is full, just reschedule a later read */
  481. if (count == 0) {
  482. poll_mask |= HVC_POLL_READ;
  483. break;
  484. }
  485. n = hvc_get_chars(hp->vtermno, buf, count);
  486. if (n <= 0) {
  487. /* Hangup the tty when disconnected from host */
  488. if (n == -EPIPE) {
  489. spin_unlock_irqrestore(&hp->lock, flags);
  490. tty_hangup(tty);
  491. spin_lock_irqsave(&hp->lock, flags);
  492. }
  493. break;
  494. }
  495. for (i = 0; i < n; ++i) {
  496. #ifdef CONFIG_MAGIC_SYSRQ
  497. if (hp->index == hvc_con_driver.index) {
  498. /* Handle the SysRq Hack */
  499. /* XXX should support a sequence */
  500. if (buf[i] == '\x0f') { /* ^O */
  501. sysrq_pressed = 1;
  502. continue;
  503. } else if (sysrq_pressed) {
  504. handle_sysrq(buf[i], NULL, tty);
  505. sysrq_pressed = 0;
  506. continue;
  507. }
  508. }
  509. #endif /* CONFIG_MAGIC_SYSRQ */
  510. tty_insert_flip_char(tty, buf[i], 0);
  511. }
  512. if (tty->flip.count)
  513. tty_schedule_flip(tty);
  514. /*
  515. * Account for the total amount read in one loop, and if above
  516. * 64 bytes, we do a quick schedule loop to let the tty grok
  517. * the data and eventually throttle us.
  518. */
  519. read_total += n;
  520. if (read_total >= 64) {
  521. poll_mask |= HVC_POLL_QUICK;
  522. break;
  523. }
  524. }
  525. throttled:
  526. /* Wakeup write queue if necessary */
  527. if (hp->do_wakeup) {
  528. hp->do_wakeup = 0;
  529. tty_wakeup(tty);
  530. }
  531. bail:
  532. spin_unlock_irqrestore(&hp->lock, flags);
  533. return poll_mask;
  534. }
  535. #if defined(CONFIG_XMON) && defined(CONFIG_SMP)
  536. extern cpumask_t cpus_in_xmon;
  537. #else
  538. static const cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  539. #endif
  540. /*
  541. * This kthread is either polling or interrupt driven. This is determined by
  542. * calling hvc_poll() who determines whether a console adapter support
  543. * interrupts.
  544. */
  545. int khvcd(void *unused)
  546. {
  547. int poll_mask;
  548. struct hvc_struct *hp;
  549. __set_current_state(TASK_RUNNING);
  550. do {
  551. poll_mask = 0;
  552. hvc_kicked = 0;
  553. wmb();
  554. if (cpus_empty(cpus_in_xmon)) {
  555. spin_lock(&hvc_structs_lock);
  556. list_for_each_entry(hp, &hvc_structs, next) {
  557. /*hp = list_entry(node, struct hvc_struct, * next); */
  558. poll_mask |= hvc_poll(hp);
  559. }
  560. spin_unlock(&hvc_structs_lock);
  561. } else
  562. poll_mask |= HVC_POLL_READ;
  563. if (hvc_kicked)
  564. continue;
  565. if (poll_mask & HVC_POLL_QUICK) {
  566. yield();
  567. continue;
  568. }
  569. set_current_state(TASK_INTERRUPTIBLE);
  570. if (!hvc_kicked) {
  571. if (poll_mask == 0)
  572. schedule();
  573. else
  574. msleep_interruptible(TIMEOUT);
  575. }
  576. __set_current_state(TASK_RUNNING);
  577. } while (!kthread_should_stop());
  578. return 0;
  579. }
  580. static struct tty_operations hvc_ops = {
  581. .open = hvc_open,
  582. .close = hvc_close,
  583. .write = hvc_write,
  584. .hangup = hvc_hangup,
  585. .unthrottle = hvc_unthrottle,
  586. .write_room = hvc_write_room,
  587. .chars_in_buffer = hvc_chars_in_buffer,
  588. };
  589. /* callback when the kboject ref count reaches zero. */
  590. static void destroy_hvc_struct(struct kobject *kobj)
  591. {
  592. struct hvc_struct *hp = container_of(kobj, struct hvc_struct, kobj);
  593. unsigned long flags;
  594. spin_lock(&hvc_structs_lock);
  595. spin_lock_irqsave(&hp->lock, flags);
  596. list_del(&(hp->next));
  597. spin_unlock_irqrestore(&hp->lock, flags);
  598. spin_unlock(&hvc_structs_lock);
  599. kfree(hp);
  600. }
  601. static struct kobj_type hvc_kobj_type = {
  602. .release = destroy_hvc_struct,
  603. };
  604. static int __devinit hvc_probe(
  605. struct vio_dev *dev,
  606. const struct vio_device_id *id)
  607. {
  608. struct hvc_struct *hp;
  609. int i;
  610. /* probed with invalid parameters. */
  611. if (!dev || !id)
  612. return -EPERM;
  613. hp = kmalloc(sizeof(*hp), GFP_KERNEL);
  614. if (!hp)
  615. return -ENOMEM;
  616. memset(hp, 0x00, sizeof(*hp));
  617. hp->vtermno = dev->unit_address;
  618. hp->vdev = dev;
  619. hp->vdev->dev.driver_data = hp;
  620. hp->irq = dev->irq;
  621. kobject_init(&hp->kobj);
  622. hp->kobj.ktype = &hvc_kobj_type;
  623. spin_lock_init(&hp->lock);
  624. spin_lock(&hvc_structs_lock);
  625. /*
  626. * find index to use:
  627. * see if this vterm id matches one registered for console.
  628. */
  629. for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
  630. if (vtermnos[i] == hp->vtermno)
  631. break;
  632. /* no matching slot, just use a counter */
  633. if (i >= MAX_NR_HVC_CONSOLES)
  634. i = ++last_hvc;
  635. hp->index = i;
  636. list_add_tail(&(hp->next), &hvc_structs);
  637. spin_unlock(&hvc_structs_lock);
  638. return 0;
  639. }
  640. static int __devexit hvc_remove(struct vio_dev *dev)
  641. {
  642. struct hvc_struct *hp = dev->dev.driver_data;
  643. unsigned long flags;
  644. struct kobject *kobjp;
  645. struct tty_struct *tty;
  646. spin_lock_irqsave(&hp->lock, flags);
  647. tty = hp->tty;
  648. kobjp = &hp->kobj;
  649. if (hp->index < MAX_NR_HVC_CONSOLES)
  650. vtermnos[hp->index] = -1;
  651. /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
  652. spin_unlock_irqrestore(&hp->lock, flags);
  653. /*
  654. * We 'put' the instance that was grabbed when the kobject instance
  655. * was intialized using kobject_init(). Let the last holder of this
  656. * kobject cause it to be removed, which will probably be the tty_hangup
  657. * below.
  658. */
  659. kobject_put(kobjp);
  660. /*
  661. * This function call will auto chain call hvc_hangup. The tty should
  662. * always be valid at this time unless a simultaneous tty close already
  663. * cleaned up the hvc_struct.
  664. */
  665. if (tty)
  666. tty_hangup(tty);
  667. return 0;
  668. }
  669. char hvc_driver_name[] = "hvc_console";
  670. static struct vio_device_id hvc_driver_table[] __devinitdata= {
  671. {"serial", "hvterm1"},
  672. { NULL, }
  673. };
  674. MODULE_DEVICE_TABLE(vio, hvc_driver_table);
  675. static struct vio_driver hvc_vio_driver = {
  676. .name = hvc_driver_name,
  677. .id_table = hvc_driver_table,
  678. .probe = hvc_probe,
  679. .remove = hvc_remove,
  680. };
  681. /* Driver initialization. Follow console initialization. This is where the TTY
  682. * interfaces start to become available. */
  683. int __init hvc_init(void)
  684. {
  685. int rc;
  686. /* We need more than num_vterms adapters due to hotplug additions. */
  687. hvc_driver = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
  688. /* hvc_driver = alloc_tty_driver(num_vterms); */
  689. if (!hvc_driver)
  690. return -ENOMEM;
  691. hvc_driver->owner = THIS_MODULE;
  692. hvc_driver->devfs_name = "hvc/";
  693. hvc_driver->driver_name = "hvc";
  694. hvc_driver->name = "hvc";
  695. hvc_driver->major = HVC_MAJOR;
  696. hvc_driver->minor_start = HVC_MINOR;
  697. hvc_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  698. hvc_driver->init_termios = tty_std_termios;
  699. hvc_driver->flags = TTY_DRIVER_REAL_RAW;
  700. tty_set_operations(hvc_driver, &hvc_ops);
  701. if (tty_register_driver(hvc_driver))
  702. panic("Couldn't register hvc console driver\n");
  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(hvc_driver);
  709. return -EIO;
  710. }
  711. /* Register as a vio device to receive callbacks */
  712. rc = vio_register_driver(&hvc_vio_driver);
  713. return rc;
  714. }
  715. module_init(hvc_init);
  716. /* This isn't particularily necessary due to this being a console driver
  717. * but it is nice to be thorough.
  718. */
  719. static void __exit hvc_exit(void)
  720. {
  721. kthread_stop(hvc_task);
  722. vio_unregister_driver(&hvc_vio_driver);
  723. tty_unregister_driver(hvc_driver);
  724. /* return tty_struct instances allocated in hvc_init(). */
  725. put_tty_driver(hvc_driver);
  726. unregister_console(&hvc_con_driver);
  727. }
  728. module_exit(hvc_exit);