jsm_tty.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /************************************************************************
  2. * Copyright 2003 Digi International (www.digi.com)
  3. *
  4. * Copyright (C) 2004 IBM Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
  13. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14. * PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 * Temple Place - Suite 330, Boston,
  19. * MA 02111-1307, USA.
  20. *
  21. * Contact Information:
  22. * Scott H Kilau <Scott_Kilau@digi.com>
  23. * Ananda Venkatarman <mansarov@us.ibm.com>
  24. * Modifications:
  25. * 01/19/06: changed jsm_input routine to use the dynamically allocated
  26. * tty_buffer changes. Contributors: Scott Kilau and Ananda V.
  27. ***********************************************************************/
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/serial_reg.h>
  31. #include <linux/delay.h> /* For udelay */
  32. #include <linux/pci.h>
  33. #include "jsm.h"
  34. static DECLARE_BITMAP(linemap, MAXLINES);
  35. static void jsm_carrier(struct jsm_channel *ch);
  36. static inline int jsm_get_mstat(struct jsm_channel *ch)
  37. {
  38. unsigned char mstat;
  39. unsigned result;
  40. jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "start\n");
  41. mstat = (ch->ch_mostat | ch->ch_mistat);
  42. result = 0;
  43. if (mstat & UART_MCR_DTR)
  44. result |= TIOCM_DTR;
  45. if (mstat & UART_MCR_RTS)
  46. result |= TIOCM_RTS;
  47. if (mstat & UART_MSR_CTS)
  48. result |= TIOCM_CTS;
  49. if (mstat & UART_MSR_DSR)
  50. result |= TIOCM_DSR;
  51. if (mstat & UART_MSR_RI)
  52. result |= TIOCM_RI;
  53. if (mstat & UART_MSR_DCD)
  54. result |= TIOCM_CD;
  55. jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
  56. return result;
  57. }
  58. static unsigned int jsm_tty_tx_empty(struct uart_port *port)
  59. {
  60. return TIOCSER_TEMT;
  61. }
  62. /*
  63. * Return modem signals to ld.
  64. */
  65. static unsigned int jsm_tty_get_mctrl(struct uart_port *port)
  66. {
  67. int result;
  68. struct jsm_channel *channel = (struct jsm_channel *)port;
  69. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
  70. result = jsm_get_mstat(channel);
  71. if (result < 0)
  72. return -ENXIO;
  73. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
  74. return result;
  75. }
  76. /*
  77. * jsm_set_modem_info()
  78. *
  79. * Set modem signals, called by ld.
  80. */
  81. static void jsm_tty_set_mctrl(struct uart_port *port, unsigned int mctrl)
  82. {
  83. struct jsm_channel *channel = (struct jsm_channel *)port;
  84. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
  85. if (mctrl & TIOCM_RTS)
  86. channel->ch_mostat |= UART_MCR_RTS;
  87. else
  88. channel->ch_mostat &= ~UART_MCR_RTS;
  89. if (mctrl & TIOCM_DTR)
  90. channel->ch_mostat |= UART_MCR_DTR;
  91. else
  92. channel->ch_mostat &= ~UART_MCR_DTR;
  93. channel->ch_bd->bd_ops->assert_modem_signals(channel);
  94. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
  95. udelay(10);
  96. }
  97. static void jsm_tty_start_tx(struct uart_port *port)
  98. {
  99. struct jsm_channel *channel = (struct jsm_channel *)port;
  100. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
  101. channel->ch_flags &= ~(CH_STOP);
  102. jsm_tty_write(port);
  103. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
  104. }
  105. static void jsm_tty_stop_tx(struct uart_port *port)
  106. {
  107. struct jsm_channel *channel = (struct jsm_channel *)port;
  108. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
  109. channel->ch_flags |= (CH_STOP);
  110. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
  111. }
  112. static void jsm_tty_send_xchar(struct uart_port *port, char ch)
  113. {
  114. unsigned long lock_flags;
  115. struct jsm_channel *channel = (struct jsm_channel *)port;
  116. struct ktermios *termios;
  117. spin_lock_irqsave(&port->lock, lock_flags);
  118. termios = port->state->port.tty->termios;
  119. if (ch == termios->c_cc[VSTART])
  120. channel->ch_bd->bd_ops->send_start_character(channel);
  121. if (ch == termios->c_cc[VSTOP])
  122. channel->ch_bd->bd_ops->send_stop_character(channel);
  123. spin_unlock_irqrestore(&port->lock, lock_flags);
  124. }
  125. static void jsm_tty_stop_rx(struct uart_port *port)
  126. {
  127. struct jsm_channel *channel = (struct jsm_channel *)port;
  128. channel->ch_bd->bd_ops->disable_receiver(channel);
  129. }
  130. static void jsm_tty_enable_ms(struct uart_port *port)
  131. {
  132. /* Nothing needed */
  133. }
  134. static void jsm_tty_break(struct uart_port *port, int break_state)
  135. {
  136. unsigned long lock_flags;
  137. struct jsm_channel *channel = (struct jsm_channel *)port;
  138. spin_lock_irqsave(&port->lock, lock_flags);
  139. if (break_state == -1)
  140. channel->ch_bd->bd_ops->send_break(channel);
  141. else
  142. channel->ch_bd->bd_ops->clear_break(channel, 0);
  143. spin_unlock_irqrestore(&port->lock, lock_flags);
  144. }
  145. static int jsm_tty_open(struct uart_port *port)
  146. {
  147. struct jsm_board *brd;
  148. struct jsm_channel *channel = (struct jsm_channel *)port;
  149. struct ktermios *termios;
  150. /* Get board pointer from our array of majors we have allocated */
  151. brd = channel->ch_bd;
  152. /*
  153. * Allocate channel buffers for read/write/error.
  154. * Set flag, so we don't get trounced on.
  155. */
  156. channel->ch_flags |= (CH_OPENING);
  157. /* Drop locks, as malloc with GFP_KERNEL can sleep */
  158. if (!channel->ch_rqueue) {
  159. channel->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
  160. if (!channel->ch_rqueue) {
  161. jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
  162. "unable to allocate read queue buf");
  163. return -ENOMEM;
  164. }
  165. }
  166. if (!channel->ch_equeue) {
  167. channel->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
  168. if (!channel->ch_equeue) {
  169. jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
  170. "unable to allocate error queue buf");
  171. return -ENOMEM;
  172. }
  173. }
  174. if (!channel->ch_wqueue) {
  175. channel->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
  176. if (!channel->ch_wqueue) {
  177. jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
  178. "unable to allocate write queue buf");
  179. return -ENOMEM;
  180. }
  181. }
  182. channel->ch_flags &= ~(CH_OPENING);
  183. /*
  184. * Initialize if neither terminal is open.
  185. */
  186. jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev,
  187. "jsm_open: initializing channel in open...\n");
  188. /*
  189. * Flush input queues.
  190. */
  191. channel->ch_r_head = channel->ch_r_tail = 0;
  192. channel->ch_e_head = channel->ch_e_tail = 0;
  193. channel->ch_w_head = channel->ch_w_tail = 0;
  194. brd->bd_ops->flush_uart_write(channel);
  195. brd->bd_ops->flush_uart_read(channel);
  196. channel->ch_flags = 0;
  197. channel->ch_cached_lsr = 0;
  198. channel->ch_stops_sent = 0;
  199. termios = port->state->port.tty->termios;
  200. channel->ch_c_cflag = termios->c_cflag;
  201. channel->ch_c_iflag = termios->c_iflag;
  202. channel->ch_c_oflag = termios->c_oflag;
  203. channel->ch_c_lflag = termios->c_lflag;
  204. channel->ch_startc = termios->c_cc[VSTART];
  205. channel->ch_stopc = termios->c_cc[VSTOP];
  206. /* Tell UART to init itself */
  207. brd->bd_ops->uart_init(channel);
  208. /*
  209. * Run param in case we changed anything
  210. */
  211. brd->bd_ops->param(channel);
  212. jsm_carrier(channel);
  213. channel->ch_open_count++;
  214. jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev, "finish\n");
  215. return 0;
  216. }
  217. static void jsm_tty_close(struct uart_port *port)
  218. {
  219. struct jsm_board *bd;
  220. struct ktermios *ts;
  221. struct jsm_channel *channel = (struct jsm_channel *)port;
  222. jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "start\n");
  223. bd = channel->ch_bd;
  224. ts = port->state->port.tty->termios;
  225. channel->ch_flags &= ~(CH_STOPI);
  226. channel->ch_open_count--;
  227. /*
  228. * If we have HUPCL set, lower DTR and RTS
  229. */
  230. if (channel->ch_c_cflag & HUPCL) {
  231. jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev,
  232. "Close. HUPCL set, dropping DTR/RTS\n");
  233. /* Drop RTS/DTR */
  234. channel->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
  235. bd->bd_ops->assert_modem_signals(channel);
  236. }
  237. /* Turn off UART interrupts for this port */
  238. channel->ch_bd->bd_ops->uart_off(channel);
  239. jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "finish\n");
  240. }
  241. static void jsm_tty_set_termios(struct uart_port *port,
  242. struct ktermios *termios,
  243. struct ktermios *old_termios)
  244. {
  245. unsigned long lock_flags;
  246. struct jsm_channel *channel = (struct jsm_channel *)port;
  247. spin_lock_irqsave(&port->lock, lock_flags);
  248. channel->ch_c_cflag = termios->c_cflag;
  249. channel->ch_c_iflag = termios->c_iflag;
  250. channel->ch_c_oflag = termios->c_oflag;
  251. channel->ch_c_lflag = termios->c_lflag;
  252. channel->ch_startc = termios->c_cc[VSTART];
  253. channel->ch_stopc = termios->c_cc[VSTOP];
  254. channel->ch_bd->bd_ops->param(channel);
  255. jsm_carrier(channel);
  256. spin_unlock_irqrestore(&port->lock, lock_flags);
  257. }
  258. static const char *jsm_tty_type(struct uart_port *port)
  259. {
  260. return "jsm";
  261. }
  262. static void jsm_tty_release_port(struct uart_port *port)
  263. {
  264. }
  265. static int jsm_tty_request_port(struct uart_port *port)
  266. {
  267. return 0;
  268. }
  269. static void jsm_config_port(struct uart_port *port, int flags)
  270. {
  271. port->type = PORT_JSM;
  272. }
  273. static struct uart_ops jsm_ops = {
  274. .tx_empty = jsm_tty_tx_empty,
  275. .set_mctrl = jsm_tty_set_mctrl,
  276. .get_mctrl = jsm_tty_get_mctrl,
  277. .stop_tx = jsm_tty_stop_tx,
  278. .start_tx = jsm_tty_start_tx,
  279. .send_xchar = jsm_tty_send_xchar,
  280. .stop_rx = jsm_tty_stop_rx,
  281. .enable_ms = jsm_tty_enable_ms,
  282. .break_ctl = jsm_tty_break,
  283. .startup = jsm_tty_open,
  284. .shutdown = jsm_tty_close,
  285. .set_termios = jsm_tty_set_termios,
  286. .type = jsm_tty_type,
  287. .release_port = jsm_tty_release_port,
  288. .request_port = jsm_tty_request_port,
  289. .config_port = jsm_config_port,
  290. };
  291. /*
  292. * jsm_tty_init()
  293. *
  294. * Init the tty subsystem. Called once per board after board has been
  295. * downloaded and init'ed.
  296. */
  297. int __devinit jsm_tty_init(struct jsm_board *brd)
  298. {
  299. int i;
  300. void __iomem *vaddr;
  301. struct jsm_channel *ch;
  302. if (!brd)
  303. return -ENXIO;
  304. jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
  305. /*
  306. * Initialize board structure elements.
  307. */
  308. brd->nasync = brd->maxports;
  309. /*
  310. * Allocate channel memory that might not have been allocated
  311. * when the driver was first loaded.
  312. */
  313. for (i = 0; i < brd->nasync; i++) {
  314. if (!brd->channels[i]) {
  315. /*
  316. * Okay to malloc with GFP_KERNEL, we are not at
  317. * interrupt context, and there are no locks held.
  318. */
  319. brd->channels[i] = kzalloc(sizeof(struct jsm_channel), GFP_KERNEL);
  320. if (!brd->channels[i]) {
  321. jsm_printk(CORE, ERR, &brd->pci_dev,
  322. "%s:%d Unable to allocate memory for channel struct\n",
  323. __FILE__, __LINE__);
  324. }
  325. }
  326. }
  327. ch = brd->channels[0];
  328. vaddr = brd->re_map_membase;
  329. /* Set up channel variables */
  330. for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
  331. if (!brd->channels[i])
  332. continue;
  333. spin_lock_init(&ch->ch_lock);
  334. if (brd->bd_uart_offset == 0x200)
  335. ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
  336. ch->ch_bd = brd;
  337. ch->ch_portnum = i;
  338. /* .25 second delay */
  339. ch->ch_close_delay = 250;
  340. init_waitqueue_head(&ch->ch_flags_wait);
  341. }
  342. jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
  343. return 0;
  344. }
  345. int jsm_uart_port_init(struct jsm_board *brd)
  346. {
  347. int i, rc;
  348. unsigned int line;
  349. struct jsm_channel *ch;
  350. if (!brd)
  351. return -ENXIO;
  352. jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
  353. /*
  354. * Initialize board structure elements.
  355. */
  356. brd->nasync = brd->maxports;
  357. /* Set up channel variables */
  358. for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
  359. if (!brd->channels[i])
  360. continue;
  361. brd->channels[i]->uart_port.irq = brd->irq;
  362. brd->channels[i]->uart_port.uartclk = 14745600;
  363. brd->channels[i]->uart_port.type = PORT_JSM;
  364. brd->channels[i]->uart_port.iotype = UPIO_MEM;
  365. brd->channels[i]->uart_port.membase = brd->re_map_membase;
  366. brd->channels[i]->uart_port.fifosize = 16;
  367. brd->channels[i]->uart_port.ops = &jsm_ops;
  368. line = find_first_zero_bit(linemap, MAXLINES);
  369. if (line >= MAXLINES) {
  370. printk(KERN_INFO "jsm: linemap is full, added device failed\n");
  371. continue;
  372. } else
  373. set_bit(line, linemap);
  374. brd->channels[i]->uart_port.line = line;
  375. rc = uart_add_one_port (&jsm_uart_driver, &brd->channels[i]->uart_port);
  376. if (rc){
  377. printk(KERN_INFO "jsm: Port %d failed. Aborting...\n", i);
  378. return rc;
  379. }
  380. else
  381. printk(KERN_INFO "jsm: Port %d added\n", i);
  382. }
  383. jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
  384. return 0;
  385. }
  386. int jsm_remove_uart_port(struct jsm_board *brd)
  387. {
  388. int i;
  389. struct jsm_channel *ch;
  390. if (!brd)
  391. return -ENXIO;
  392. jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
  393. /*
  394. * Initialize board structure elements.
  395. */
  396. brd->nasync = brd->maxports;
  397. /* Set up channel variables */
  398. for (i = 0; i < brd->nasync; i++) {
  399. if (!brd->channels[i])
  400. continue;
  401. ch = brd->channels[i];
  402. clear_bit(ch->uart_port.line, linemap);
  403. uart_remove_one_port(&jsm_uart_driver, &brd->channels[i]->uart_port);
  404. }
  405. jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
  406. return 0;
  407. }
  408. void jsm_input(struct jsm_channel *ch)
  409. {
  410. struct jsm_board *bd;
  411. struct tty_struct *tp;
  412. u32 rmask;
  413. u16 head;
  414. u16 tail;
  415. int data_len;
  416. unsigned long lock_flags;
  417. int len = 0;
  418. int n = 0;
  419. int s = 0;
  420. int i = 0;
  421. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
  422. if (!ch)
  423. return;
  424. tp = ch->uart_port.state->port.tty;
  425. bd = ch->ch_bd;
  426. if(!bd)
  427. return;
  428. spin_lock_irqsave(&ch->ch_lock, lock_flags);
  429. /*
  430. *Figure the number of characters in the buffer.
  431. *Exit immediately if none.
  432. */
  433. rmask = RQUEUEMASK;
  434. head = ch->ch_r_head & rmask;
  435. tail = ch->ch_r_tail & rmask;
  436. data_len = (head - tail) & rmask;
  437. if (data_len == 0) {
  438. spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
  439. return;
  440. }
  441. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
  442. /*
  443. *If the device is not open, or CREAD is off, flush
  444. *input data and return immediately.
  445. */
  446. if (!tp ||
  447. !(tp->termios->c_cflag & CREAD) ) {
  448. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
  449. "input. dropping %d bytes on port %d...\n", data_len, ch->ch_portnum);
  450. ch->ch_r_head = tail;
  451. /* Force queue flow control to be released, if needed */
  452. jsm_check_queue_flow_control(ch);
  453. spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
  454. return;
  455. }
  456. /*
  457. * If we are throttled, simply don't read any data.
  458. */
  459. if (ch->ch_flags & CH_STOPI) {
  460. spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
  461. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
  462. "Port %d throttled, not reading any data. head: %x tail: %x\n",
  463. ch->ch_portnum, head, tail);
  464. return;
  465. }
  466. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start 2\n");
  467. if (data_len <= 0) {
  468. spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
  469. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "jsm_input 1\n");
  470. return;
  471. }
  472. len = tty_buffer_request_room(tp, data_len);
  473. n = len;
  474. /*
  475. * n now contains the most amount of data we can copy,
  476. * bounded either by the flip buffer size or the amount
  477. * of data the card actually has pending...
  478. */
  479. while (n) {
  480. s = ((head >= tail) ? head : RQUEUESIZE) - tail;
  481. s = min(s, n);
  482. if (s <= 0)
  483. break;
  484. /*
  485. * If conditions are such that ld needs to see all
  486. * UART errors, we will have to walk each character
  487. * and error byte and send them to the buffer one at
  488. * a time.
  489. */
  490. if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
  491. for (i = 0; i < s; i++) {
  492. /*
  493. * Give the Linux ld the flags in the
  494. * format it likes.
  495. */
  496. if (*(ch->ch_equeue +tail +i) & UART_LSR_BI)
  497. tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_BREAK);
  498. else if (*(ch->ch_equeue +tail +i) & UART_LSR_PE)
  499. tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_PARITY);
  500. else if (*(ch->ch_equeue +tail +i) & UART_LSR_FE)
  501. tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_FRAME);
  502. else
  503. tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_NORMAL);
  504. }
  505. } else {
  506. tty_insert_flip_string(tp, ch->ch_rqueue + tail, s) ;
  507. }
  508. tail += s;
  509. n -= s;
  510. /* Flip queue if needed */
  511. tail &= rmask;
  512. }
  513. ch->ch_r_tail = tail & rmask;
  514. ch->ch_e_tail = tail & rmask;
  515. jsm_check_queue_flow_control(ch);
  516. spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
  517. /* Tell the tty layer its okay to "eat" the data now */
  518. tty_flip_buffer_push(tp);
  519. jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
  520. }
  521. static void jsm_carrier(struct jsm_channel *ch)
  522. {
  523. struct jsm_board *bd;
  524. int virt_carrier = 0;
  525. int phys_carrier = 0;
  526. jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev, "start\n");
  527. if (!ch)
  528. return;
  529. bd = ch->ch_bd;
  530. if (!bd)
  531. return;
  532. if (ch->ch_mistat & UART_MSR_DCD) {
  533. jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
  534. "mistat: %x D_CD: %x\n", ch->ch_mistat, ch->ch_mistat & UART_MSR_DCD);
  535. phys_carrier = 1;
  536. }
  537. if (ch->ch_c_cflag & CLOCAL)
  538. virt_carrier = 1;
  539. jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
  540. "DCD: physical: %d virt: %d\n", phys_carrier, virt_carrier);
  541. /*
  542. * Test for a VIRTUAL carrier transition to HIGH.
  543. */
  544. if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
  545. /*
  546. * When carrier rises, wake any threads waiting
  547. * for carrier in the open routine.
  548. */
  549. jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
  550. "carrier: virt DCD rose\n");
  551. if (waitqueue_active(&(ch->ch_flags_wait)))
  552. wake_up_interruptible(&ch->ch_flags_wait);
  553. }
  554. /*
  555. * Test for a PHYSICAL carrier transition to HIGH.
  556. */
  557. if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
  558. /*
  559. * When carrier rises, wake any threads waiting
  560. * for carrier in the open routine.
  561. */
  562. jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
  563. "carrier: physical DCD rose\n");
  564. if (waitqueue_active(&(ch->ch_flags_wait)))
  565. wake_up_interruptible(&ch->ch_flags_wait);
  566. }
  567. /*
  568. * Test for a PHYSICAL transition to low, so long as we aren't
  569. * currently ignoring physical transitions (which is what "virtual
  570. * carrier" indicates).
  571. *
  572. * The transition of the virtual carrier to low really doesn't
  573. * matter... it really only means "ignore carrier state", not
  574. * "make pretend that carrier is there".
  575. */
  576. if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0)
  577. && (phys_carrier == 0)) {
  578. /*
  579. * When carrier drops:
  580. *
  581. * Drop carrier on all open units.
  582. *
  583. * Flush queues, waking up any task waiting in the
  584. * line discipline.
  585. *
  586. * Send a hangup to the control terminal.
  587. *
  588. * Enable all select calls.
  589. */
  590. if (waitqueue_active(&(ch->ch_flags_wait)))
  591. wake_up_interruptible(&ch->ch_flags_wait);
  592. }
  593. /*
  594. * Make sure that our cached values reflect the current reality.
  595. */
  596. if (virt_carrier == 1)
  597. ch->ch_flags |= CH_FCAR;
  598. else
  599. ch->ch_flags &= ~CH_FCAR;
  600. if (phys_carrier == 1)
  601. ch->ch_flags |= CH_CD;
  602. else
  603. ch->ch_flags &= ~CH_CD;
  604. }
  605. void jsm_check_queue_flow_control(struct jsm_channel *ch)
  606. {
  607. struct board_ops *bd_ops = ch->ch_bd->bd_ops;
  608. int qleft;
  609. /* Store how much space we have left in the queue */
  610. if ((qleft = ch->ch_r_tail - ch->ch_r_head - 1) < 0)
  611. qleft += RQUEUEMASK + 1;
  612. /*
  613. * Check to see if we should enforce flow control on our queue because
  614. * the ld (or user) isn't reading data out of our queue fast enuf.
  615. *
  616. * NOTE: This is done based on what the current flow control of the
  617. * port is set for.
  618. *
  619. * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
  620. * This will cause the UART's FIFO to back up, and force
  621. * the RTS signal to be dropped.
  622. * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
  623. * the other side, in hopes it will stop sending data to us.
  624. * 3) NONE - Nothing we can do. We will simply drop any extra data
  625. * that gets sent into us when the queue fills up.
  626. */
  627. if (qleft < 256) {
  628. /* HWFLOW */
  629. if (ch->ch_c_cflag & CRTSCTS) {
  630. if(!(ch->ch_flags & CH_RECEIVER_OFF)) {
  631. bd_ops->disable_receiver(ch);
  632. ch->ch_flags |= (CH_RECEIVER_OFF);
  633. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
  634. "Internal queue hit hilevel mark (%d)! Turning off interrupts.\n",
  635. qleft);
  636. }
  637. }
  638. /* SWFLOW */
  639. else if (ch->ch_c_iflag & IXOFF) {
  640. if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
  641. bd_ops->send_stop_character(ch);
  642. ch->ch_stops_sent++;
  643. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
  644. "Sending stop char! Times sent: %x\n", ch->ch_stops_sent);
  645. }
  646. }
  647. }
  648. /*
  649. * Check to see if we should unenforce flow control because
  650. * ld (or user) finally read enuf data out of our queue.
  651. *
  652. * NOTE: This is done based on what the current flow control of the
  653. * port is set for.
  654. *
  655. * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
  656. * This will cause the UART's FIFO to raise RTS back up,
  657. * which will allow the other side to start sending data again.
  658. * 2) SWFLOW (IXOFF) - Send a start character to
  659. * the other side, so it will start sending data to us again.
  660. * 3) NONE - Do nothing. Since we didn't do anything to turn off the
  661. * other side, we don't need to do anything now.
  662. */
  663. if (qleft > (RQUEUESIZE / 2)) {
  664. /* HWFLOW */
  665. if (ch->ch_c_cflag & CRTSCTS) {
  666. if (ch->ch_flags & CH_RECEIVER_OFF) {
  667. bd_ops->enable_receiver(ch);
  668. ch->ch_flags &= ~(CH_RECEIVER_OFF);
  669. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
  670. "Internal queue hit lowlevel mark (%d)! Turning on interrupts.\n",
  671. qleft);
  672. }
  673. }
  674. /* SWFLOW */
  675. else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
  676. ch->ch_stops_sent = 0;
  677. bd_ops->send_start_character(ch);
  678. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "Sending start char!\n");
  679. }
  680. }
  681. }
  682. /*
  683. * jsm_tty_write()
  684. *
  685. * Take data from the user or kernel and send it out to the FEP.
  686. * In here exists all the Transparent Print magic as well.
  687. */
  688. int jsm_tty_write(struct uart_port *port)
  689. {
  690. int bufcount;
  691. int data_count = 0,data_count1 =0;
  692. u16 head;
  693. u16 tail;
  694. u16 tmask;
  695. u32 remain;
  696. int temp_tail = port->state->xmit.tail;
  697. struct jsm_channel *channel = (struct jsm_channel *)port;
  698. tmask = WQUEUEMASK;
  699. head = (channel->ch_w_head) & tmask;
  700. tail = (channel->ch_w_tail) & tmask;
  701. if ((bufcount = tail - head - 1) < 0)
  702. bufcount += WQUEUESIZE;
  703. bufcount = min(bufcount, 56);
  704. remain = WQUEUESIZE - head;
  705. data_count = 0;
  706. if (bufcount >= remain) {
  707. bufcount -= remain;
  708. while ((port->state->xmit.head != temp_tail) &&
  709. (data_count < remain)) {
  710. channel->ch_wqueue[head++] =
  711. port->state->xmit.buf[temp_tail];
  712. temp_tail++;
  713. temp_tail &= (UART_XMIT_SIZE - 1);
  714. data_count++;
  715. }
  716. if (data_count == remain) head = 0;
  717. }
  718. data_count1 = 0;
  719. if (bufcount > 0) {
  720. remain = bufcount;
  721. while ((port->state->xmit.head != temp_tail) &&
  722. (data_count1 < remain)) {
  723. channel->ch_wqueue[head++] =
  724. port->state->xmit.buf[temp_tail];
  725. temp_tail++;
  726. temp_tail &= (UART_XMIT_SIZE - 1);
  727. data_count1++;
  728. }
  729. }
  730. port->state->xmit.tail = temp_tail;
  731. data_count += data_count1;
  732. if (data_count) {
  733. head &= tmask;
  734. channel->ch_w_head = head;
  735. }
  736. if (data_count) {
  737. channel->ch_bd->bd_ops->copy_data_from_queue_to_uart(channel);
  738. }
  739. return data_count;
  740. }