jsm_tty.c 25 KB

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