hvc_console.c 20 KB

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