simserial.c 25 KB

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