simserial.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * Simulated Serial Driver (fake serial)
  3. *
  4. * This driver is mostly used for bringup purposes and will go away.
  5. * It has a strong dependency on the system console. All outputs
  6. * are rerouted to the same facility as the one used by printk which, in our
  7. * case means sys_sim.c console (goes via the simulator). The code hereafter
  8. * is completely leveraged from the serial.c driver.
  9. *
  10. * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co
  11. * Stephane Eranian <eranian@hpl.hp.com>
  12. * David Mosberger-Tang <davidm@hpl.hp.com>
  13. *
  14. * 02/04/00 D. Mosberger Merged in serial.c bug fixes in rs_close().
  15. * 02/25/00 D. Mosberger Synced up with 2.3.99pre-5 version of serial.c.
  16. * 07/30/02 D. Mosberger Replace sti()/cli() with explicit spinlocks & local irq masking
  17. */
  18. #include <linux/init.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/major.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/mm.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/slab.h>
  28. #include <linux/capability.h>
  29. #include <linux/circ_buf.h>
  30. #include <linux/console.h>
  31. #include <linux/module.h>
  32. #include <linux/serial.h>
  33. #include <linux/sysrq.h>
  34. #include <asm/irq.h>
  35. #include <asm/hpsim.h>
  36. #include <asm/hw_irq.h>
  37. #include <asm/uaccess.h>
  38. #include "hpsim_ssc.h"
  39. #undef SIMSERIAL_DEBUG /* define this to get some debug information */
  40. #define KEYBOARD_INTR 3 /* must match with simulator! */
  41. #define NR_PORTS 1 /* only one port for now */
  42. struct serial_state {
  43. struct tty_port port;
  44. struct circ_buf xmit;
  45. int irq;
  46. int x_char;
  47. };
  48. static char *serial_name = "SimSerial driver";
  49. static char *serial_version = "0.6";
  50. static struct serial_state rs_table[NR_PORTS];
  51. struct tty_driver *hp_simserial_driver;
  52. static struct console *console;
  53. extern struct console *console_drivers; /* from kernel/printk.c */
  54. static void receive_chars(struct tty_struct *tty)
  55. {
  56. unsigned char ch;
  57. static unsigned char seen_esc = 0;
  58. while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
  59. if ( ch == 27 && seen_esc == 0 ) {
  60. seen_esc = 1;
  61. continue;
  62. } else {
  63. if ( seen_esc==1 && ch == 'O' ) {
  64. seen_esc = 2;
  65. continue;
  66. } else if ( seen_esc == 2 ) {
  67. if ( ch == 'P' ) /* F1 */
  68. show_state();
  69. #ifdef CONFIG_MAGIC_SYSRQ
  70. if ( ch == 'S' ) { /* F4 */
  71. do
  72. ch = ia64_ssc(0, 0, 0, 0,
  73. SSC_GETCHAR);
  74. while (!ch);
  75. handle_sysrq(ch);
  76. }
  77. #endif
  78. seen_esc = 0;
  79. continue;
  80. }
  81. }
  82. seen_esc = 0;
  83. if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
  84. break;
  85. }
  86. tty_flip_buffer_push(tty);
  87. }
  88. /*
  89. * This is the serial driver's interrupt routine for a single port
  90. */
  91. static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
  92. {
  93. struct serial_state *info = dev_id;
  94. struct tty_struct *tty = tty_port_tty_get(&info->port);
  95. if (!tty) {
  96. printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
  97. return IRQ_NONE;
  98. }
  99. /*
  100. * pretty simple in our case, because we only get interrupts
  101. * on inbound traffic
  102. */
  103. receive_chars(tty);
  104. tty_kref_put(tty);
  105. return IRQ_HANDLED;
  106. }
  107. /*
  108. * -------------------------------------------------------------------
  109. * Here ends the serial interrupt routines.
  110. * -------------------------------------------------------------------
  111. */
  112. static int rs_put_char(struct tty_struct *tty, unsigned char ch)
  113. {
  114. struct serial_state *info = tty->driver_data;
  115. unsigned long flags;
  116. if (!tty || !info->xmit.buf)
  117. return 0;
  118. local_irq_save(flags);
  119. if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
  120. local_irq_restore(flags);
  121. return 0;
  122. }
  123. info->xmit.buf[info->xmit.head] = ch;
  124. info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
  125. local_irq_restore(flags);
  126. return 1;
  127. }
  128. static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
  129. int *intr_done)
  130. {
  131. int count;
  132. unsigned long flags;
  133. local_irq_save(flags);
  134. if (info->x_char) {
  135. char c = info->x_char;
  136. console->write(console, &c, 1);
  137. info->x_char = 0;
  138. goto out;
  139. }
  140. if (info->xmit.head == info->xmit.tail || tty->stopped ||
  141. tty->hw_stopped) {
  142. #ifdef SIMSERIAL_DEBUG
  143. printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
  144. info->xmit.head, info->xmit.tail, tty->stopped);
  145. #endif
  146. goto out;
  147. }
  148. /*
  149. * We removed the loop and try to do it in to chunks. We need
  150. * 2 operations maximum because it's a ring buffer.
  151. *
  152. * First from current to tail if possible.
  153. * Then from the beginning of the buffer until necessary
  154. */
  155. count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
  156. SERIAL_XMIT_SIZE - info->xmit.tail);
  157. console->write(console, info->xmit.buf+info->xmit.tail, count);
  158. info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
  159. /*
  160. * We have more at the beginning of the buffer
  161. */
  162. count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  163. if (count) {
  164. console->write(console, info->xmit.buf, count);
  165. info->xmit.tail += count;
  166. }
  167. out:
  168. local_irq_restore(flags);
  169. }
  170. static void rs_flush_chars(struct tty_struct *tty)
  171. {
  172. struct serial_state *info = tty->driver_data;
  173. if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
  174. !info->xmit.buf)
  175. return;
  176. transmit_chars(tty, info, NULL);
  177. }
  178. static int rs_write(struct tty_struct * tty,
  179. const unsigned char *buf, int count)
  180. {
  181. struct serial_state *info = tty->driver_data;
  182. int c, ret = 0;
  183. unsigned long flags;
  184. if (!tty || !info->xmit.buf)
  185. return 0;
  186. local_irq_save(flags);
  187. while (1) {
  188. c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  189. if (count < c)
  190. c = count;
  191. if (c <= 0) {
  192. break;
  193. }
  194. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  195. info->xmit.head = ((info->xmit.head + c) &
  196. (SERIAL_XMIT_SIZE-1));
  197. buf += c;
  198. count -= c;
  199. ret += c;
  200. }
  201. local_irq_restore(flags);
  202. /*
  203. * Hey, we transmit directly from here in our case
  204. */
  205. if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
  206. && !tty->stopped && !tty->hw_stopped) {
  207. transmit_chars(tty, info, NULL);
  208. }
  209. return ret;
  210. }
  211. static int rs_write_room(struct tty_struct *tty)
  212. {
  213. struct serial_state *info = tty->driver_data;
  214. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  215. }
  216. static int rs_chars_in_buffer(struct tty_struct *tty)
  217. {
  218. struct serial_state *info = tty->driver_data;
  219. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  220. }
  221. static void rs_flush_buffer(struct tty_struct *tty)
  222. {
  223. struct serial_state *info = tty->driver_data;
  224. unsigned long flags;
  225. local_irq_save(flags);
  226. info->xmit.head = info->xmit.tail = 0;
  227. local_irq_restore(flags);
  228. tty_wakeup(tty);
  229. }
  230. /*
  231. * This function is used to send a high-priority XON/XOFF character to
  232. * the device
  233. */
  234. static void rs_send_xchar(struct tty_struct *tty, char ch)
  235. {
  236. struct serial_state *info = tty->driver_data;
  237. info->x_char = ch;
  238. if (ch) {
  239. /*
  240. * I guess we could call console->write() directly but
  241. * let's do that for now.
  242. */
  243. transmit_chars(tty, info, NULL);
  244. }
  245. }
  246. /*
  247. * ------------------------------------------------------------
  248. * rs_throttle()
  249. *
  250. * This routine is called by the upper-layer tty layer to signal that
  251. * incoming characters should be throttled.
  252. * ------------------------------------------------------------
  253. */
  254. static void rs_throttle(struct tty_struct * tty)
  255. {
  256. if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
  257. printk(KERN_INFO "simrs_throttle called\n");
  258. }
  259. static void rs_unthrottle(struct tty_struct * tty)
  260. {
  261. struct serial_state *info = tty->driver_data;
  262. if (I_IXOFF(tty)) {
  263. if (info->x_char)
  264. info->x_char = 0;
  265. else
  266. rs_send_xchar(tty, START_CHAR(tty));
  267. }
  268. printk(KERN_INFO "simrs_unthrottle called\n");
  269. }
  270. static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  271. {
  272. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  273. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  274. (cmd != TIOCMIWAIT)) {
  275. if (tty->flags & (1 << TTY_IO_ERROR))
  276. return -EIO;
  277. }
  278. switch (cmd) {
  279. case TIOCGSERIAL:
  280. printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
  281. return 0;
  282. case TIOCSSERIAL:
  283. printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
  284. return 0;
  285. case TIOCSERCONFIG:
  286. printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
  287. return -EINVAL;
  288. case TIOCSERGETLSR: /* Get line status register */
  289. printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
  290. return -EINVAL;
  291. case TIOCSERGSTRUCT:
  292. printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
  293. #if 0
  294. if (copy_to_user((struct async_struct *) arg,
  295. info, sizeof(struct async_struct)))
  296. return -EFAULT;
  297. #endif
  298. return 0;
  299. /*
  300. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  301. * - mask passed in arg for lines of interest
  302. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  303. * Caller should use TIOCGICOUNT to see which one it was
  304. */
  305. case TIOCMIWAIT:
  306. printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
  307. return 0;
  308. case TIOCSERGWILD:
  309. case TIOCSERSWILD:
  310. /* "setserial -W" is called in Debian boot */
  311. printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
  312. return 0;
  313. default:
  314. return -ENOIOCTLCMD;
  315. }
  316. return 0;
  317. }
  318. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  319. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  320. {
  321. /* Handle turning off CRTSCTS */
  322. if ((old_termios->c_cflag & CRTSCTS) &&
  323. !(tty->termios->c_cflag & CRTSCTS)) {
  324. tty->hw_stopped = 0;
  325. }
  326. }
  327. /*
  328. * This routine will shutdown a serial port; interrupts are disabled, and
  329. * DTR is dropped if the hangup on close termio flag is on.
  330. */
  331. static void shutdown(struct tty_port *port)
  332. {
  333. struct serial_state *info = container_of(port, struct serial_state,
  334. port);
  335. unsigned long flags;
  336. local_irq_save(flags);
  337. {
  338. if (info->irq)
  339. free_irq(info->irq, info);
  340. if (info->xmit.buf) {
  341. free_page((unsigned long) info->xmit.buf);
  342. info->xmit.buf = NULL;
  343. }
  344. }
  345. local_irq_restore(flags);
  346. }
  347. static void rs_close(struct tty_struct *tty, struct file * filp)
  348. {
  349. struct serial_state *info = tty->driver_data;
  350. tty_port_close(&info->port, tty, filp);
  351. }
  352. static void rs_hangup(struct tty_struct *tty)
  353. {
  354. struct serial_state *info = tty->driver_data;
  355. rs_flush_buffer(tty);
  356. tty_port_hangup(&info->port);
  357. }
  358. static int activate(struct tty_port *port, struct tty_struct *tty)
  359. {
  360. struct serial_state *state = container_of(port, struct serial_state,
  361. port);
  362. unsigned long flags;
  363. int retval=0;
  364. unsigned long page;
  365. page = get_zeroed_page(GFP_KERNEL);
  366. if (!page)
  367. return -ENOMEM;
  368. local_irq_save(flags);
  369. if (state->xmit.buf)
  370. free_page(page);
  371. else
  372. state->xmit.buf = (unsigned char *) page;
  373. /*
  374. * Allocate the IRQ if necessary
  375. */
  376. if (state->irq) {
  377. retval = request_irq(state->irq, rs_interrupt_single, 0,
  378. "simserial", state);
  379. if (retval)
  380. goto errout;
  381. }
  382. state->xmit.head = state->xmit.tail = 0;
  383. /*
  384. * Set up the tty->alt_speed kludge
  385. */
  386. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  387. tty->alt_speed = 57600;
  388. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  389. tty->alt_speed = 115200;
  390. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  391. tty->alt_speed = 230400;
  392. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  393. tty->alt_speed = 460800;
  394. errout:
  395. local_irq_restore(flags);
  396. return retval;
  397. }
  398. /*
  399. * This routine is called whenever a serial port is opened. It
  400. * enables interrupts for a serial port, linking in its async structure into
  401. * the IRQ chain. It also performs the serial-specific
  402. * initialization for the tty structure.
  403. */
  404. static int rs_open(struct tty_struct *tty, struct file * filp)
  405. {
  406. struct serial_state *info = rs_table + tty->index;
  407. struct tty_port *port = &info->port;
  408. tty->driver_data = info;
  409. tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  410. /*
  411. * figure out which console to use (should be one already)
  412. */
  413. console = console_drivers;
  414. while (console) {
  415. if ((console->flags & CON_ENABLED) && console->write) break;
  416. console = console->next;
  417. }
  418. return tty_port_open(port, tty, filp);
  419. }
  420. /*
  421. * /proc fs routines....
  422. */
  423. static int rs_proc_show(struct seq_file *m, void *v)
  424. {
  425. int i;
  426. seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
  427. for (i = 0; i < NR_PORTS; i++)
  428. seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
  429. i, rs_table[i].irq);
  430. return 0;
  431. }
  432. static int rs_proc_open(struct inode *inode, struct file *file)
  433. {
  434. return single_open(file, rs_proc_show, NULL);
  435. }
  436. static const struct file_operations rs_proc_fops = {
  437. .owner = THIS_MODULE,
  438. .open = rs_proc_open,
  439. .read = seq_read,
  440. .llseek = seq_lseek,
  441. .release = single_release,
  442. };
  443. /*
  444. * ---------------------------------------------------------------------
  445. * rs_init() and friends
  446. *
  447. * rs_init() is called at boot-time to initialize the serial driver.
  448. * ---------------------------------------------------------------------
  449. */
  450. /*
  451. * This routine prints out the appropriate serial driver version
  452. * number, and identifies which options were configured into this
  453. * driver.
  454. */
  455. static inline void show_serial_version(void)
  456. {
  457. printk(KERN_INFO "%s version %s with", serial_name, serial_version);
  458. printk(KERN_INFO " no serial options enabled\n");
  459. }
  460. static const struct tty_operations hp_ops = {
  461. .open = rs_open,
  462. .close = rs_close,
  463. .write = rs_write,
  464. .put_char = rs_put_char,
  465. .flush_chars = rs_flush_chars,
  466. .write_room = rs_write_room,
  467. .chars_in_buffer = rs_chars_in_buffer,
  468. .flush_buffer = rs_flush_buffer,
  469. .ioctl = rs_ioctl,
  470. .throttle = rs_throttle,
  471. .unthrottle = rs_unthrottle,
  472. .send_xchar = rs_send_xchar,
  473. .set_termios = rs_set_termios,
  474. .hangup = rs_hangup,
  475. .proc_fops = &rs_proc_fops,
  476. };
  477. static const struct tty_port_operations hp_port_ops = {
  478. .activate = activate,
  479. .shutdown = shutdown,
  480. };
  481. /*
  482. * The serial driver boot-time initialization code!
  483. */
  484. static int __init simrs_init(void)
  485. {
  486. struct serial_state *state;
  487. int retval;
  488. if (!ia64_platform_is("hpsim"))
  489. return -ENODEV;
  490. hp_simserial_driver = alloc_tty_driver(NR_PORTS);
  491. if (!hp_simserial_driver)
  492. return -ENOMEM;
  493. show_serial_version();
  494. /* Initialize the tty_driver structure */
  495. hp_simserial_driver->driver_name = "simserial";
  496. hp_simserial_driver->name = "ttyS";
  497. hp_simserial_driver->major = TTY_MAJOR;
  498. hp_simserial_driver->minor_start = 64;
  499. hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  500. hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
  501. hp_simserial_driver->init_termios = tty_std_termios;
  502. hp_simserial_driver->init_termios.c_cflag =
  503. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  504. hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
  505. tty_set_operations(hp_simserial_driver, &hp_ops);
  506. /*
  507. * Let's have a little bit of fun !
  508. */
  509. state = rs_table;
  510. tty_port_init(&state->port);
  511. state->port.ops = &hp_port_ops;
  512. state->port.close_delay = 0; /* XXX really 0? */
  513. retval = hpsim_get_irq(KEYBOARD_INTR);
  514. if (retval < 0) {
  515. printk(KERN_ERR "%s: out of interrupt vectors!\n",
  516. __func__);
  517. goto err_free_tty;
  518. }
  519. state->irq = retval;
  520. /* the port is imaginary */
  521. printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
  522. retval = tty_register_driver(hp_simserial_driver);
  523. if (retval) {
  524. printk(KERN_ERR "Couldn't register simserial driver\n");
  525. goto err_free_tty;
  526. }
  527. return 0;
  528. err_free_tty:
  529. put_tty_driver(hp_simserial_driver);
  530. return retval;
  531. }
  532. #ifndef MODULE
  533. __initcall(simrs_init);
  534. #endif