simserial.c 25 KB

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