simserial.c 25 KB

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