hvc_console.c 20 KB

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