simserial.c 25 KB

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