simserial.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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/config.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/sched.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/major.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/mm.h>
  27. #include <linux/slab.h>
  28. #include <linux/capability.h>
  29. #include <linux/console.h>
  30. #include <linux/module.h>
  31. #include <linux/serial.h>
  32. #include <linux/serialP.h>
  33. #include <linux/sysrq.h>
  34. #include <asm/irq.h>
  35. #include <asm/hw_irq.h>
  36. #include <asm/uaccess.h>
  37. #ifdef CONFIG_KDB
  38. # include <linux/kdb.h>
  39. #endif
  40. #undef SIMSERIAL_DEBUG /* define this to get some debug information */
  41. #define KEYBOARD_INTR 3 /* must match with simulator! */
  42. #define NR_PORTS 1 /* only one port for now */
  43. #define SERIAL_INLINE 1
  44. #ifdef SERIAL_INLINE
  45. #define _INLINE_ inline
  46. #endif
  47. #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
  48. #define SSC_GETCHAR 21
  49. extern long ia64_ssc (long, long, long, long, int);
  50. extern void ia64_ssc_connect_irq (long intr, long irq);
  51. static char *serial_name = "SimSerial driver";
  52. static char *serial_version = "0.6";
  53. /*
  54. * This has been extracted from asm/serial.h. We need one eventually but
  55. * I don't know exactly what we're going to put in it so just fake one
  56. * for now.
  57. */
  58. #define BASE_BAUD ( 1843200 / 16 )
  59. #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
  60. /*
  61. * Most of the values here are meaningless to this particular driver.
  62. * However some values must be preserved for the code (leveraged from serial.c
  63. * to work correctly).
  64. * port must not be 0
  65. * type must not be UNKNOWN
  66. * So I picked arbitrary (guess from where?) values instead
  67. */
  68. static struct serial_state rs_table[NR_PORTS]={
  69. /* UART CLK PORT IRQ FLAGS */
  70. { 0, BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS,0,PORT_16550 } /* ttyS0 */
  71. };
  72. /*
  73. * Just for the fun of it !
  74. */
  75. static struct serial_uart_config uart_config[] = {
  76. { "unknown", 1, 0 },
  77. { "8250", 1, 0 },
  78. { "16450", 1, 0 },
  79. { "16550", 1, 0 },
  80. { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
  81. { "cirrus", 1, 0 },
  82. { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
  83. { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
  84. UART_STARTECH },
  85. { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
  86. { 0, 0}
  87. };
  88. struct tty_driver *hp_simserial_driver;
  89. static struct async_struct *IRQ_ports[NR_IRQS];
  90. static struct console *console;
  91. static unsigned char *tmp_buf;
  92. extern struct console *console_drivers; /* from kernel/printk.c */
  93. /*
  94. * ------------------------------------------------------------
  95. * rs_stop() and rs_start()
  96. *
  97. * This routines are called before setting or resetting tty->stopped.
  98. * They enable or disable transmitter interrupts, as necessary.
  99. * ------------------------------------------------------------
  100. */
  101. static void rs_stop(struct tty_struct *tty)
  102. {
  103. #ifdef SIMSERIAL_DEBUG
  104. printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
  105. tty->stopped, tty->hw_stopped, tty->flow_stopped);
  106. #endif
  107. }
  108. static void rs_start(struct tty_struct *tty)
  109. {
  110. #ifdef SIMSERIAL_DEBUG
  111. printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
  112. tty->stopped, tty->hw_stopped, tty->flow_stopped);
  113. #endif
  114. }
  115. static void receive_chars(struct tty_struct *tty, struct pt_regs *regs)
  116. {
  117. unsigned char ch;
  118. static unsigned char seen_esc = 0;
  119. while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
  120. if ( ch == 27 && seen_esc == 0 ) {
  121. seen_esc = 1;
  122. continue;
  123. } else {
  124. if ( seen_esc==1 && ch == 'O' ) {
  125. seen_esc = 2;
  126. continue;
  127. } else if ( seen_esc == 2 ) {
  128. if ( ch == 'P' ) /* F1 */
  129. show_state();
  130. #ifdef CONFIG_MAGIC_SYSRQ
  131. if ( ch == 'S' ) { /* F4 */
  132. do
  133. ch = ia64_ssc(0, 0, 0, 0,
  134. SSC_GETCHAR);
  135. while (!ch);
  136. handle_sysrq(ch, regs, NULL);
  137. }
  138. #endif
  139. seen_esc = 0;
  140. continue;
  141. }
  142. }
  143. seen_esc = 0;
  144. if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
  145. break;
  146. }
  147. tty_flip_buffer_push(tty);
  148. }
  149. /*
  150. * This is the serial driver's interrupt routine for a single port
  151. */
  152. static irqreturn_t rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
  153. {
  154. struct async_struct * info;
  155. /*
  156. * I don't know exactly why they don't use the dev_id opaque data
  157. * pointer instead of this extra lookup table
  158. */
  159. info = IRQ_ports[irq];
  160. if (!info || !info->tty) {
  161. printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
  162. return IRQ_NONE;
  163. }
  164. /*
  165. * pretty simple in our case, because we only get interrupts
  166. * on inbound traffic
  167. */
  168. receive_chars(info->tty, regs);
  169. return IRQ_HANDLED;
  170. }
  171. /*
  172. * -------------------------------------------------------------------
  173. * Here ends the serial interrupt routines.
  174. * -------------------------------------------------------------------
  175. */
  176. #if 0
  177. /*
  178. * not really used in our situation so keep them commented out for now
  179. */
  180. static DECLARE_TASK_QUEUE(tq_serial); /* used to be at the top of the file */
  181. static void do_serial_bh(void)
  182. {
  183. run_task_queue(&tq_serial);
  184. printk(KERN_ERR "do_serial_bh: called\n");
  185. }
  186. #endif
  187. static void do_softint(void *private_)
  188. {
  189. printk(KERN_ERR "simserial: do_softint called\n");
  190. }
  191. static void rs_put_char(struct tty_struct *tty, unsigned char ch)
  192. {
  193. struct async_struct *info = (struct async_struct *)tty->driver_data;
  194. unsigned long flags;
  195. if (!tty || !info->xmit.buf) return;
  196. local_irq_save(flags);
  197. if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
  198. local_irq_restore(flags);
  199. return;
  200. }
  201. info->xmit.buf[info->xmit.head] = ch;
  202. info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
  203. local_irq_restore(flags);
  204. }
  205. static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
  206. {
  207. int count;
  208. unsigned long flags;
  209. local_irq_save(flags);
  210. if (info->x_char) {
  211. char c = info->x_char;
  212. console->write(console, &c, 1);
  213. info->state->icount.tx++;
  214. info->x_char = 0;
  215. goto out;
  216. }
  217. if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
  218. #ifdef SIMSERIAL_DEBUG
  219. printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
  220. info->xmit.head, info->xmit.tail, info->tty->stopped);
  221. #endif
  222. goto out;
  223. }
  224. /*
  225. * We removed the loop and try to do it in to chunks. We need
  226. * 2 operations maximum because it's a ring buffer.
  227. *
  228. * First from current to tail if possible.
  229. * Then from the beginning of the buffer until necessary
  230. */
  231. count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
  232. SERIAL_XMIT_SIZE - info->xmit.tail);
  233. console->write(console, info->xmit.buf+info->xmit.tail, count);
  234. info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
  235. /*
  236. * We have more at the beginning of the buffer
  237. */
  238. count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  239. if (count) {
  240. console->write(console, info->xmit.buf, count);
  241. info->xmit.tail += count;
  242. }
  243. out:
  244. local_irq_restore(flags);
  245. }
  246. static void rs_flush_chars(struct tty_struct *tty)
  247. {
  248. struct async_struct *info = (struct async_struct *)tty->driver_data;
  249. if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
  250. !info->xmit.buf)
  251. return;
  252. transmit_chars(info, NULL);
  253. }
  254. static int rs_write(struct tty_struct * tty,
  255. const unsigned char *buf, int count)
  256. {
  257. int c, ret = 0;
  258. struct async_struct *info = (struct async_struct *)tty->driver_data;
  259. unsigned long flags;
  260. if (!tty || !info->xmit.buf || !tmp_buf) return 0;
  261. local_irq_save(flags);
  262. while (1) {
  263. c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  264. if (count < c)
  265. c = count;
  266. if (c <= 0) {
  267. break;
  268. }
  269. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  270. info->xmit.head = ((info->xmit.head + c) &
  271. (SERIAL_XMIT_SIZE-1));
  272. buf += c;
  273. count -= c;
  274. ret += c;
  275. }
  276. local_irq_restore(flags);
  277. /*
  278. * Hey, we transmit directly from here in our case
  279. */
  280. if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
  281. && !tty->stopped && !tty->hw_stopped) {
  282. transmit_chars(info, NULL);
  283. }
  284. return ret;
  285. }
  286. static int rs_write_room(struct tty_struct *tty)
  287. {
  288. struct async_struct *info = (struct async_struct *)tty->driver_data;
  289. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  290. }
  291. static int rs_chars_in_buffer(struct tty_struct *tty)
  292. {
  293. struct async_struct *info = (struct async_struct *)tty->driver_data;
  294. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  295. }
  296. static void rs_flush_buffer(struct tty_struct *tty)
  297. {
  298. struct async_struct *info = (struct async_struct *)tty->driver_data;
  299. unsigned long flags;
  300. local_irq_save(flags);
  301. info->xmit.head = info->xmit.tail = 0;
  302. local_irq_restore(flags);
  303. wake_up_interruptible(&tty->write_wait);
  304. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  305. tty->ldisc.write_wakeup)
  306. (tty->ldisc.write_wakeup)(tty);
  307. }
  308. /*
  309. * This function is used to send a high-priority XON/XOFF character to
  310. * the device
  311. */
  312. static void rs_send_xchar(struct tty_struct *tty, char ch)
  313. {
  314. struct async_struct *info = (struct async_struct *)tty->driver_data;
  315. info->x_char = ch;
  316. if (ch) {
  317. /*
  318. * I guess we could call console->write() directly but
  319. * let's do that for now.
  320. */
  321. transmit_chars(info, NULL);
  322. }
  323. }
  324. /*
  325. * ------------------------------------------------------------
  326. * rs_throttle()
  327. *
  328. * This routine is called by the upper-layer tty layer to signal that
  329. * incoming characters should be throttled.
  330. * ------------------------------------------------------------
  331. */
  332. static void rs_throttle(struct tty_struct * tty)
  333. {
  334. if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
  335. printk(KERN_INFO "simrs_throttle called\n");
  336. }
  337. static void rs_unthrottle(struct tty_struct * tty)
  338. {
  339. struct async_struct *info = (struct async_struct *)tty->driver_data;
  340. if (I_IXOFF(tty)) {
  341. if (info->x_char)
  342. info->x_char = 0;
  343. else
  344. rs_send_xchar(tty, START_CHAR(tty));
  345. }
  346. printk(KERN_INFO "simrs_unthrottle called\n");
  347. }
  348. /*
  349. * rs_break() --- routine which turns the break handling on or off
  350. */
  351. static void rs_break(struct tty_struct *tty, int break_state)
  352. {
  353. }
  354. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  355. unsigned int cmd, unsigned long arg)
  356. {
  357. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  358. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  359. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  360. if (tty->flags & (1 << TTY_IO_ERROR))
  361. return -EIO;
  362. }
  363. switch (cmd) {
  364. case TIOCMGET:
  365. printk(KERN_INFO "rs_ioctl: TIOCMGET called\n");
  366. return -EINVAL;
  367. case TIOCMBIS:
  368. case TIOCMBIC:
  369. case TIOCMSET:
  370. printk(KERN_INFO "rs_ioctl: TIOCMBIS/BIC/SET called\n");
  371. return -EINVAL;
  372. case TIOCGSERIAL:
  373. printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
  374. return 0;
  375. case TIOCSSERIAL:
  376. printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
  377. return 0;
  378. case TIOCSERCONFIG:
  379. printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
  380. return -EINVAL;
  381. case TIOCSERGETLSR: /* Get line status register */
  382. printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
  383. return -EINVAL;
  384. case TIOCSERGSTRUCT:
  385. printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
  386. #if 0
  387. if (copy_to_user((struct async_struct *) arg,
  388. info, sizeof(struct async_struct)))
  389. return -EFAULT;
  390. #endif
  391. return 0;
  392. /*
  393. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  394. * - mask passed in arg for lines of interest
  395. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  396. * Caller should use TIOCGICOUNT to see which one it was
  397. */
  398. case TIOCMIWAIT:
  399. printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
  400. return 0;
  401. /*
  402. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  403. * Return: write counters to the user passed counter struct
  404. * NB: both 1->0 and 0->1 transitions are counted except for
  405. * RI where only 0->1 is counted.
  406. */
  407. case TIOCGICOUNT:
  408. printk(KERN_INFO "rs_ioctl: TIOCGICOUNT called\n");
  409. return 0;
  410. case TIOCSERGWILD:
  411. case TIOCSERSWILD:
  412. /* "setserial -W" is called in Debian boot */
  413. printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
  414. return 0;
  415. default:
  416. return -ENOIOCTLCMD;
  417. }
  418. return 0;
  419. }
  420. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  421. static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
  422. {
  423. unsigned int cflag = tty->termios->c_cflag;
  424. if ( (cflag == old_termios->c_cflag)
  425. && ( RELEVANT_IFLAG(tty->termios->c_iflag)
  426. == RELEVANT_IFLAG(old_termios->c_iflag)))
  427. return;
  428. /* Handle turning off CRTSCTS */
  429. if ((old_termios->c_cflag & CRTSCTS) &&
  430. !(tty->termios->c_cflag & CRTSCTS)) {
  431. tty->hw_stopped = 0;
  432. rs_start(tty);
  433. }
  434. }
  435. /*
  436. * This routine will shutdown a serial port; interrupts are disabled, and
  437. * DTR is dropped if the hangup on close termio flag is on.
  438. */
  439. static void shutdown(struct async_struct * info)
  440. {
  441. unsigned long flags;
  442. struct serial_state *state;
  443. int retval;
  444. if (!(info->flags & ASYNC_INITIALIZED)) return;
  445. state = info->state;
  446. #ifdef SIMSERIAL_DEBUG
  447. printk("Shutting down serial port %d (irq %d)....", info->line,
  448. state->irq);
  449. #endif
  450. local_irq_save(flags);
  451. {
  452. /*
  453. * First unlink the serial port from the IRQ chain...
  454. */
  455. if (info->next_port)
  456. info->next_port->prev_port = info->prev_port;
  457. if (info->prev_port)
  458. info->prev_port->next_port = info->next_port;
  459. else
  460. IRQ_ports[state->irq] = info->next_port;
  461. /*
  462. * Free the IRQ, if necessary
  463. */
  464. if (state->irq && (!IRQ_ports[state->irq] ||
  465. !IRQ_ports[state->irq]->next_port)) {
  466. if (IRQ_ports[state->irq]) {
  467. free_irq(state->irq, NULL);
  468. retval = request_irq(state->irq, rs_interrupt_single,
  469. IRQ_T(info), "serial", NULL);
  470. if (retval)
  471. printk(KERN_ERR "serial shutdown: request_irq: error %d"
  472. " Couldn't reacquire IRQ.\n", retval);
  473. } else
  474. free_irq(state->irq, NULL);
  475. }
  476. if (info->xmit.buf) {
  477. free_page((unsigned long) info->xmit.buf);
  478. info->xmit.buf = 0;
  479. }
  480. if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
  481. info->flags &= ~ASYNC_INITIALIZED;
  482. }
  483. local_irq_restore(flags);
  484. }
  485. /*
  486. * ------------------------------------------------------------
  487. * rs_close()
  488. *
  489. * This routine is called when the serial port gets closed. First, we
  490. * wait for the last remaining data to be sent. Then, we unlink its
  491. * async structure from the interrupt chain if necessary, and we free
  492. * that IRQ if nothing is left in the chain.
  493. * ------------------------------------------------------------
  494. */
  495. static void rs_close(struct tty_struct *tty, struct file * filp)
  496. {
  497. struct async_struct * info = (struct async_struct *)tty->driver_data;
  498. struct serial_state *state;
  499. unsigned long flags;
  500. if (!info ) return;
  501. state = info->state;
  502. local_irq_save(flags);
  503. if (tty_hung_up_p(filp)) {
  504. #ifdef SIMSERIAL_DEBUG
  505. printk("rs_close: hung_up\n");
  506. #endif
  507. local_irq_restore(flags);
  508. return;
  509. }
  510. #ifdef SIMSERIAL_DEBUG
  511. printk("rs_close ttys%d, count = %d\n", info->line, state->count);
  512. #endif
  513. if ((tty->count == 1) && (state->count != 1)) {
  514. /*
  515. * Uh, oh. tty->count is 1, which means that the tty
  516. * structure will be freed. state->count should always
  517. * be one in these conditions. If it's greater than
  518. * one, we've got real problems, since it means the
  519. * serial port won't be shutdown.
  520. */
  521. printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
  522. "state->count is %d\n", state->count);
  523. state->count = 1;
  524. }
  525. if (--state->count < 0) {
  526. printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
  527. info->line, state->count);
  528. state->count = 0;
  529. }
  530. if (state->count) {
  531. local_irq_restore(flags);
  532. return;
  533. }
  534. info->flags |= ASYNC_CLOSING;
  535. local_irq_restore(flags);
  536. /*
  537. * Now we wait for the transmit buffer to clear; and we notify
  538. * the line discipline to only process XON/XOFF characters.
  539. */
  540. shutdown(info);
  541. if (tty->driver->flush_buffer) tty->driver->flush_buffer(tty);
  542. if (tty->ldisc.flush_buffer) tty->ldisc.flush_buffer(tty);
  543. info->event = 0;
  544. info->tty = 0;
  545. if (info->blocked_open) {
  546. if (info->close_delay)
  547. schedule_timeout_interruptible(info->close_delay);
  548. wake_up_interruptible(&info->open_wait);
  549. }
  550. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  551. wake_up_interruptible(&info->close_wait);
  552. }
  553. /*
  554. * rs_wait_until_sent() --- wait until the transmitter is empty
  555. */
  556. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  557. {
  558. }
  559. /*
  560. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  561. */
  562. static void rs_hangup(struct tty_struct *tty)
  563. {
  564. struct async_struct * info = (struct async_struct *)tty->driver_data;
  565. struct serial_state *state = info->state;
  566. #ifdef SIMSERIAL_DEBUG
  567. printk("rs_hangup: called\n");
  568. #endif
  569. state = info->state;
  570. rs_flush_buffer(tty);
  571. if (info->flags & ASYNC_CLOSING)
  572. return;
  573. shutdown(info);
  574. info->event = 0;
  575. state->count = 0;
  576. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  577. info->tty = 0;
  578. wake_up_interruptible(&info->open_wait);
  579. }
  580. static int get_async_struct(int line, struct async_struct **ret_info)
  581. {
  582. struct async_struct *info;
  583. struct serial_state *sstate;
  584. sstate = rs_table + line;
  585. sstate->count++;
  586. if (sstate->info) {
  587. *ret_info = sstate->info;
  588. return 0;
  589. }
  590. info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
  591. if (!info) {
  592. sstate->count--;
  593. return -ENOMEM;
  594. }
  595. memset(info, 0, sizeof(struct async_struct));
  596. init_waitqueue_head(&info->open_wait);
  597. init_waitqueue_head(&info->close_wait);
  598. init_waitqueue_head(&info->delta_msr_wait);
  599. info->magic = SERIAL_MAGIC;
  600. info->port = sstate->port;
  601. info->flags = sstate->flags;
  602. info->xmit_fifo_size = sstate->xmit_fifo_size;
  603. info->line = line;
  604. INIT_WORK(&info->work, do_softint, info);
  605. info->state = sstate;
  606. if (sstate->info) {
  607. kfree(info);
  608. *ret_info = sstate->info;
  609. return 0;
  610. }
  611. *ret_info = sstate->info = info;
  612. return 0;
  613. }
  614. static int
  615. startup(struct async_struct *info)
  616. {
  617. unsigned long flags;
  618. int retval=0;
  619. irqreturn_t (*handler)(int, void *, struct pt_regs *);
  620. struct serial_state *state= info->state;
  621. unsigned long page;
  622. page = get_zeroed_page(GFP_KERNEL);
  623. if (!page)
  624. return -ENOMEM;
  625. local_irq_save(flags);
  626. if (info->flags & ASYNC_INITIALIZED) {
  627. free_page(page);
  628. goto errout;
  629. }
  630. if (!state->port || !state->type) {
  631. if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
  632. free_page(page);
  633. goto errout;
  634. }
  635. if (info->xmit.buf)
  636. free_page(page);
  637. else
  638. info->xmit.buf = (unsigned char *) page;
  639. #ifdef SIMSERIAL_DEBUG
  640. printk("startup: ttys%d (irq %d)...", info->line, state->irq);
  641. #endif
  642. /*
  643. * Allocate the IRQ if necessary
  644. */
  645. if (state->irq && (!IRQ_ports[state->irq] ||
  646. !IRQ_ports[state->irq]->next_port)) {
  647. if (IRQ_ports[state->irq]) {
  648. retval = -EBUSY;
  649. goto errout;
  650. } else
  651. handler = rs_interrupt_single;
  652. retval = request_irq(state->irq, handler, IRQ_T(info), "simserial", NULL);
  653. if (retval) {
  654. if (capable(CAP_SYS_ADMIN)) {
  655. if (info->tty)
  656. set_bit(TTY_IO_ERROR,
  657. &info->tty->flags);
  658. retval = 0;
  659. }
  660. goto errout;
  661. }
  662. }
  663. /*
  664. * Insert serial port into IRQ chain.
  665. */
  666. info->prev_port = 0;
  667. info->next_port = IRQ_ports[state->irq];
  668. if (info->next_port)
  669. info->next_port->prev_port = info;
  670. IRQ_ports[state->irq] = info;
  671. if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
  672. info->xmit.head = info->xmit.tail = 0;
  673. #if 0
  674. /*
  675. * Set up serial timers...
  676. */
  677. timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
  678. timer_active |= 1 << RS_TIMER;
  679. #endif
  680. /*
  681. * Set up the tty->alt_speed kludge
  682. */
  683. if (info->tty) {
  684. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  685. info->tty->alt_speed = 57600;
  686. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  687. info->tty->alt_speed = 115200;
  688. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  689. info->tty->alt_speed = 230400;
  690. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  691. info->tty->alt_speed = 460800;
  692. }
  693. info->flags |= ASYNC_INITIALIZED;
  694. local_irq_restore(flags);
  695. return 0;
  696. errout:
  697. local_irq_restore(flags);
  698. return retval;
  699. }
  700. /*
  701. * This routine is called whenever a serial port is opened. It
  702. * enables interrupts for a serial port, linking in its async structure into
  703. * the IRQ chain. It also performs the serial-specific
  704. * initialization for the tty structure.
  705. */
  706. static int rs_open(struct tty_struct *tty, struct file * filp)
  707. {
  708. struct async_struct *info;
  709. int retval, line;
  710. unsigned long page;
  711. line = tty->index;
  712. if ((line < 0) || (line >= NR_PORTS))
  713. return -ENODEV;
  714. retval = get_async_struct(line, &info);
  715. if (retval)
  716. return retval;
  717. tty->driver_data = info;
  718. info->tty = tty;
  719. #ifdef SIMSERIAL_DEBUG
  720. printk("rs_open %s, count = %d\n", tty->name, info->state->count);
  721. #endif
  722. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  723. if (!tmp_buf) {
  724. page = get_zeroed_page(GFP_KERNEL);
  725. if (!page)
  726. return -ENOMEM;
  727. if (tmp_buf)
  728. free_page(page);
  729. else
  730. tmp_buf = (unsigned char *) page;
  731. }
  732. /*
  733. * If the port is the middle of closing, bail out now
  734. */
  735. if (tty_hung_up_p(filp) ||
  736. (info->flags & ASYNC_CLOSING)) {
  737. if (info->flags & ASYNC_CLOSING)
  738. interruptible_sleep_on(&info->close_wait);
  739. #ifdef SERIAL_DO_RESTART
  740. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  741. -EAGAIN : -ERESTARTSYS);
  742. #else
  743. return -EAGAIN;
  744. #endif
  745. }
  746. /*
  747. * Start up serial port
  748. */
  749. retval = startup(info);
  750. if (retval) {
  751. return retval;
  752. }
  753. /*
  754. * figure out which console to use (should be one already)
  755. */
  756. console = console_drivers;
  757. while (console) {
  758. if ((console->flags & CON_ENABLED) && console->write) break;
  759. console = console->next;
  760. }
  761. #ifdef SIMSERIAL_DEBUG
  762. printk("rs_open ttys%d successful\n", info->line);
  763. #endif
  764. return 0;
  765. }
  766. /*
  767. * /proc fs routines....
  768. */
  769. static inline int line_info(char *buf, struct serial_state *state)
  770. {
  771. return sprintf(buf, "%d: uart:%s port:%lX irq:%d\n",
  772. state->line, uart_config[state->type].name,
  773. state->port, state->irq);
  774. }
  775. static int rs_read_proc(char *page, char **start, off_t off, int count,
  776. int *eof, void *data)
  777. {
  778. int i, len = 0, l;
  779. off_t begin = 0;
  780. len += sprintf(page, "simserinfo:1.0 driver:%s\n", serial_version);
  781. for (i = 0; i < NR_PORTS && len < 4000; i++) {
  782. l = line_info(page + len, &rs_table[i]);
  783. len += l;
  784. if (len+begin > off+count)
  785. goto done;
  786. if (len+begin < off) {
  787. begin += len;
  788. len = 0;
  789. }
  790. }
  791. *eof = 1;
  792. done:
  793. if (off >= len+begin)
  794. return 0;
  795. *start = page + (begin-off);
  796. return ((count < begin+len-off) ? count : begin+len-off);
  797. }
  798. /*
  799. * ---------------------------------------------------------------------
  800. * rs_init() and friends
  801. *
  802. * rs_init() is called at boot-time to initialize the serial driver.
  803. * ---------------------------------------------------------------------
  804. */
  805. /*
  806. * This routine prints out the appropriate serial driver version
  807. * number, and identifies which options were configured into this
  808. * driver.
  809. */
  810. static inline void show_serial_version(void)
  811. {
  812. printk(KERN_INFO "%s version %s with", serial_name, serial_version);
  813. printk(KERN_INFO " no serial options enabled\n");
  814. }
  815. static struct tty_operations hp_ops = {
  816. .open = rs_open,
  817. .close = rs_close,
  818. .write = rs_write,
  819. .put_char = rs_put_char,
  820. .flush_chars = rs_flush_chars,
  821. .write_room = rs_write_room,
  822. .chars_in_buffer = rs_chars_in_buffer,
  823. .flush_buffer = rs_flush_buffer,
  824. .ioctl = rs_ioctl,
  825. .throttle = rs_throttle,
  826. .unthrottle = rs_unthrottle,
  827. .send_xchar = rs_send_xchar,
  828. .set_termios = rs_set_termios,
  829. .stop = rs_stop,
  830. .start = rs_start,
  831. .hangup = rs_hangup,
  832. .break_ctl = rs_break,
  833. .wait_until_sent = rs_wait_until_sent,
  834. .read_proc = rs_read_proc,
  835. };
  836. /*
  837. * The serial driver boot-time initialization code!
  838. */
  839. static int __init
  840. simrs_init (void)
  841. {
  842. int i, rc;
  843. struct serial_state *state;
  844. if (!ia64_platform_is("hpsim"))
  845. return -ENODEV;
  846. hp_simserial_driver = alloc_tty_driver(1);
  847. if (!hp_simserial_driver)
  848. return -ENOMEM;
  849. show_serial_version();
  850. /* Initialize the tty_driver structure */
  851. hp_simserial_driver->owner = THIS_MODULE;
  852. hp_simserial_driver->driver_name = "simserial";
  853. hp_simserial_driver->name = "ttyS";
  854. hp_simserial_driver->major = TTY_MAJOR;
  855. hp_simserial_driver->minor_start = 64;
  856. hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  857. hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
  858. hp_simserial_driver->init_termios = tty_std_termios;
  859. hp_simserial_driver->init_termios.c_cflag =
  860. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  861. hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
  862. tty_set_operations(hp_simserial_driver, &hp_ops);
  863. /*
  864. * Let's have a little bit of fun !
  865. */
  866. for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
  867. if (state->type == PORT_UNKNOWN) continue;
  868. if (!state->irq) {
  869. if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
  870. panic("%s: out of interrupt vectors!\n",
  871. __FUNCTION__);
  872. state->irq = rc;
  873. ia64_ssc_connect_irq(KEYBOARD_INTR, state->irq);
  874. }
  875. printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
  876. state->line,
  877. state->port, state->irq,
  878. uart_config[state->type].name);
  879. }
  880. if (tty_register_driver(hp_simserial_driver))
  881. panic("Couldn't register simserial driver\n");
  882. return 0;
  883. }
  884. #ifndef MODULE
  885. __initcall(simrs_init);
  886. #endif