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