pc300_tty.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. /*
  2. * pc300_tty.c Cyclades-PC300(tm) TTY Driver.
  3. *
  4. * Author: Regina Kodato <reginak@cyclades.com>
  5. *
  6. * Copyright: (c) 1999-2002 Cyclades Corp.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * $Log: pc300_tty.c,v $
  14. * Revision 3.7 2002/03/07 14:17:09 henrique
  15. * License data fixed
  16. *
  17. * Revision 3.6 2001/12/10 12:29:42 regina
  18. * Fix the MLPPP bug
  19. *
  20. * Revision 3.5 2001/10/31 11:20:05 regina
  21. * automatic pppd starts
  22. *
  23. * Revision 3.4 2001/08/06 12:01:51 regina
  24. * problem in DSR_DE bit
  25. *
  26. * Revision 3.3 2001/07/26 22:58:41 regina
  27. * update EDA value
  28. *
  29. * Revision 3.2 2001/07/12 13:11:20 regina
  30. * bug fix - DCD-OFF in pc300 tty driver
  31. *
  32. * DMA transmission bug fix
  33. *
  34. * Revision 3.1 2001/06/22 13:13:02 regina
  35. * MLPPP implementation
  36. *
  37. */
  38. #include <linux/module.h>
  39. #include <linux/kernel.h>
  40. #include <linux/errno.h>
  41. #include <linux/string.h>
  42. #include <linux/init.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/slab.h>
  46. #include <linux/if.h>
  47. #include <linux/skbuff.h>
  48. /* TTY includes */
  49. #include <linux/tty.h>
  50. #include <linux/tty_flip.h>
  51. #include <linux/serial.h>
  52. #include <asm/io.h>
  53. #include <asm/uaccess.h>
  54. #include "pc300.h"
  55. /* defines and macros */
  56. /* TTY Global definitions */
  57. #define CPC_TTY_NPORTS 8 /* maximum number of the sync tty connections */
  58. #define CPC_TTY_MAJOR CYCLADES_MAJOR
  59. #define CPC_TTY_MINOR_START 240 /* minor of the first PC300 interface */
  60. #define CPC_TTY_MAX_MTU 2000
  61. /* tty interface state */
  62. #define CPC_TTY_ST_IDLE 0
  63. #define CPC_TTY_ST_INIT 1 /* configured with MLPPP and up */
  64. #define CPC_TTY_ST_OPEN 2 /* opened by application */
  65. #define CPC_TTY_LOCK(card,flags)\
  66. do {\
  67. spin_lock_irqsave(&card->card_lock, flags); \
  68. } while (0)
  69. #define CPC_TTY_UNLOCK(card,flags) \
  70. do {\
  71. spin_unlock_irqrestore(&card->card_lock, flags); \
  72. } while (0)
  73. //#define CPC_TTY_DBG(format,a...) printk(format,##a)
  74. #define CPC_TTY_DBG(format,a...)
  75. /* data structures */
  76. typedef struct _st_cpc_rx_buf {
  77. struct _st_cpc_rx_buf *next;
  78. int size;
  79. unsigned char data[1];
  80. } st_cpc_rx_buf;
  81. struct st_cpc_rx_list {
  82. st_cpc_rx_buf *first;
  83. st_cpc_rx_buf *last;
  84. };
  85. typedef struct _st_cpc_tty_area {
  86. int state; /* state of the TTY interface */
  87. int num_open;
  88. unsigned int tty_minor; /* minor this interface */
  89. volatile struct st_cpc_rx_list buf_rx; /* ptr. to reception buffer */
  90. unsigned char* buf_tx; /* ptr. to transmission buffer */
  91. pc300dev_t* pc300dev; /* ptr. to info struct in PC300 driver */
  92. unsigned char name[20]; /* interf. name + "-tty" */
  93. struct tty_struct *tty;
  94. struct work_struct tty_tx_work; /* tx work - tx interrupt */
  95. struct work_struct tty_rx_work; /* rx work - rx interrupt */
  96. } st_cpc_tty_area;
  97. /* TTY data structures */
  98. static struct tty_driver serial_drv;
  99. /* local variables */
  100. static st_cpc_tty_area cpc_tty_area[CPC_TTY_NPORTS];
  101. static int cpc_tty_cnt = 0; /* number of intrfaces configured with MLPPP */
  102. static int cpc_tty_unreg_flag = 0;
  103. /* TTY functions prototype */
  104. static int cpc_tty_open(struct tty_struct *tty, struct file *flip);
  105. static void cpc_tty_close(struct tty_struct *tty, struct file *flip);
  106. static int cpc_tty_write(struct tty_struct *tty, const unsigned char *buf, int count);
  107. static int cpc_tty_write_room(struct tty_struct *tty);
  108. static int cpc_tty_chars_in_buffer(struct tty_struct *tty);
  109. static void cpc_tty_flush_buffer(struct tty_struct *tty);
  110. static void cpc_tty_hangup(struct tty_struct *tty);
  111. static void cpc_tty_rx_work(struct work_struct *work);
  112. static void cpc_tty_tx_work(struct work_struct *work);
  113. static int cpc_tty_send_to_card(pc300dev_t *dev,void *buf, int len);
  114. static void cpc_tty_trace(pc300dev_t *dev, char* buf, int len, char rxtx);
  115. static void cpc_tty_signal_off(pc300dev_t *pc300dev, unsigned char);
  116. static void cpc_tty_signal_on(pc300dev_t *pc300dev, unsigned char);
  117. static int pc300_tiocmset(struct tty_struct *, struct file *,
  118. unsigned int, unsigned int);
  119. static int pc300_tiocmget(struct tty_struct *, struct file *);
  120. /* functions called by PC300 driver */
  121. void cpc_tty_init(pc300dev_t *dev);
  122. void cpc_tty_unregister_service(pc300dev_t *pc300dev);
  123. void cpc_tty_receive(pc300dev_t *pc300dev);
  124. void cpc_tty_trigger_poll(pc300dev_t *pc300dev);
  125. void cpc_tty_reset_var(void);
  126. /*
  127. * PC300 TTY clear "signal"
  128. */
  129. static void cpc_tty_signal_off(pc300dev_t *pc300dev, unsigned char signal)
  130. {
  131. pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
  132. pc300_t *card = (pc300_t *) pc300chan->card;
  133. int ch = pc300chan->channel;
  134. unsigned long flags;
  135. CPC_TTY_DBG("%s-tty: Clear signal %x\n",
  136. pc300dev->dev->name, signal);
  137. CPC_TTY_LOCK(card, flags);
  138. cpc_writeb(card->hw.scabase + M_REG(CTL,ch),
  139. cpc_readb(card->hw.scabase+M_REG(CTL,ch))& signal);
  140. CPC_TTY_UNLOCK(card,flags);
  141. }
  142. /*
  143. * PC300 TTY set "signal" to ON
  144. */
  145. static void cpc_tty_signal_on(pc300dev_t *pc300dev, unsigned char signal)
  146. {
  147. pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
  148. pc300_t *card = (pc300_t *) pc300chan->card;
  149. int ch = pc300chan->channel;
  150. unsigned long flags;
  151. CPC_TTY_DBG("%s-tty: Set signal %x\n",
  152. pc300dev->dev->name, signal);
  153. CPC_TTY_LOCK(card, flags);
  154. cpc_writeb(card->hw.scabase + M_REG(CTL,ch),
  155. cpc_readb(card->hw.scabase+M_REG(CTL,ch))& ~signal);
  156. CPC_TTY_UNLOCK(card,flags);
  157. }
  158. static const struct tty_operations pc300_ops = {
  159. .open = cpc_tty_open,
  160. .close = cpc_tty_close,
  161. .write = cpc_tty_write,
  162. .write_room = cpc_tty_write_room,
  163. .chars_in_buffer = cpc_tty_chars_in_buffer,
  164. .tiocmset = pc300_tiocmset,
  165. .tiocmget = pc300_tiocmget,
  166. .flush_buffer = cpc_tty_flush_buffer,
  167. .hangup = cpc_tty_hangup,
  168. };
  169. /*
  170. * PC300 TTY initialization routine
  171. *
  172. * This routine is called by the PC300 driver during board configuration
  173. * (ioctl=SIOCSP300CONF). At this point the adapter is completely
  174. * initialized.
  175. * o verify kernel version (only 2.4.x)
  176. * o register TTY driver
  177. * o init cpc_tty_area struct
  178. */
  179. void cpc_tty_init(pc300dev_t *pc300dev)
  180. {
  181. unsigned long port;
  182. int aux;
  183. st_cpc_tty_area * cpc_tty;
  184. /* hdlcX - X=interface number */
  185. port = pc300dev->dev->name[4] - '0';
  186. if (port >= CPC_TTY_NPORTS) {
  187. printk("%s-tty: invalid interface selected (0-%i): %li",
  188. pc300dev->dev->name,
  189. CPC_TTY_NPORTS-1,port);
  190. return;
  191. }
  192. if (cpc_tty_cnt == 0) { /* first TTY connection -> register driver */
  193. CPC_TTY_DBG("%s-tty: driver init, major:%i, minor range:%i=%i\n",
  194. pc300dev->dev->name,
  195. CPC_TTY_MAJOR, CPC_TTY_MINOR_START,
  196. CPC_TTY_MINOR_START+CPC_TTY_NPORTS);
  197. /* initialize tty driver struct */
  198. memset(&serial_drv,0,sizeof(struct tty_driver));
  199. serial_drv.magic = TTY_DRIVER_MAGIC;
  200. serial_drv.owner = THIS_MODULE;
  201. serial_drv.driver_name = "pc300_tty";
  202. serial_drv.name = "ttyCP";
  203. serial_drv.major = CPC_TTY_MAJOR;
  204. serial_drv.minor_start = CPC_TTY_MINOR_START;
  205. serial_drv.num = CPC_TTY_NPORTS;
  206. serial_drv.type = TTY_DRIVER_TYPE_SERIAL;
  207. serial_drv.subtype = SERIAL_TYPE_NORMAL;
  208. serial_drv.init_termios = tty_std_termios;
  209. serial_drv.init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
  210. serial_drv.flags = TTY_DRIVER_REAL_RAW;
  211. /* interface routines from the upper tty layer to the tty driver */
  212. tty_set_operations(&serial_drv, &pc300_ops);
  213. /* register the TTY driver */
  214. if (tty_register_driver(&serial_drv)) {
  215. printk("%s-tty: Failed to register serial driver! ",
  216. pc300dev->dev->name);
  217. return;
  218. }
  219. memset((void *)cpc_tty_area, 0,
  220. sizeof(st_cpc_tty_area) * CPC_TTY_NPORTS);
  221. }
  222. cpc_tty = &cpc_tty_area[port];
  223. if (cpc_tty->state != CPC_TTY_ST_IDLE) {
  224. CPC_TTY_DBG("%s-tty: TTY port %i, already in use.\n",
  225. pc300dev->dev->name, port);
  226. return;
  227. }
  228. cpc_tty_cnt++;
  229. cpc_tty->state = CPC_TTY_ST_INIT;
  230. cpc_tty->num_open= 0;
  231. cpc_tty->tty_minor = port + CPC_TTY_MINOR_START;
  232. cpc_tty->pc300dev = pc300dev;
  233. INIT_WORK(&cpc_tty->tty_tx_work, cpc_tty_tx_work);
  234. INIT_WORK(&cpc_tty->tty_rx_work, cpc_tty_rx_work);
  235. cpc_tty->buf_rx.first = cpc_tty->buf_rx.last = NULL;
  236. pc300dev->cpc_tty = (void *)cpc_tty;
  237. aux = strlen(pc300dev->dev->name);
  238. memcpy(cpc_tty->name, pc300dev->dev->name, aux);
  239. memcpy(&cpc_tty->name[aux], "-tty", 5);
  240. cpc_open(pc300dev->dev);
  241. cpc_tty_signal_off(pc300dev, CTL_DTR);
  242. CPC_TTY_DBG("%s: Initializing TTY Sync Driver, tty major#%d minor#%i\n",
  243. cpc_tty->name,CPC_TTY_MAJOR,cpc_tty->tty_minor);
  244. return;
  245. }
  246. /*
  247. * PC300 TTY OPEN routine
  248. *
  249. * This routine is called by the tty driver to open the interface
  250. * o verify minor
  251. * o allocate buffer to Rx and Tx
  252. */
  253. static int cpc_tty_open(struct tty_struct *tty, struct file *flip)
  254. {
  255. int port ;
  256. st_cpc_tty_area *cpc_tty;
  257. if (!tty) {
  258. return -ENODEV;
  259. }
  260. port = tty->index;
  261. if ((port < 0) || (port >= CPC_TTY_NPORTS)){
  262. CPC_TTY_DBG("pc300_tty: open invalid port %d\n", port);
  263. return -ENODEV;
  264. }
  265. cpc_tty = &cpc_tty_area[port];
  266. if (cpc_tty->state == CPC_TTY_ST_IDLE){
  267. CPC_TTY_DBG("%s: open - invalid interface, port=%d\n",
  268. cpc_tty->name, tty->index);
  269. return -ENODEV;
  270. }
  271. if (cpc_tty->num_open == 0) { /* first open of this tty */
  272. if (!cpc_tty_area[port].buf_tx){
  273. cpc_tty_area[port].buf_tx = kmalloc(CPC_TTY_MAX_MTU,GFP_KERNEL);
  274. if (!cpc_tty_area[port].buf_tx) {
  275. CPC_TTY_DBG("%s: error in memory allocation\n",cpc_tty->name);
  276. return -ENOMEM;
  277. }
  278. }
  279. if (cpc_tty_area[port].buf_rx.first) {
  280. unsigned char * aux;
  281. while (cpc_tty_area[port].buf_rx.first) {
  282. aux = (unsigned char *)cpc_tty_area[port].buf_rx.first;
  283. cpc_tty_area[port].buf_rx.first = cpc_tty_area[port].buf_rx.first->next;
  284. kfree(aux);
  285. }
  286. cpc_tty_area[port].buf_rx.first = NULL;
  287. cpc_tty_area[port].buf_rx.last = NULL;
  288. }
  289. cpc_tty_area[port].state = CPC_TTY_ST_OPEN;
  290. cpc_tty_area[port].tty = tty;
  291. tty->driver_data = &cpc_tty_area[port];
  292. cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
  293. }
  294. cpc_tty->num_open++;
  295. CPC_TTY_DBG("%s: opening TTY driver\n", cpc_tty->name);
  296. /* avisar driver PC300 */
  297. return 0;
  298. }
  299. /*
  300. * PC300 TTY CLOSE routine
  301. *
  302. * This routine is called by the tty driver to close the interface
  303. * o call close channel in PC300 driver (cpc_closech)
  304. * o free Rx and Tx buffers
  305. */
  306. static void cpc_tty_close(struct tty_struct *tty, struct file *flip)
  307. {
  308. st_cpc_tty_area *cpc_tty;
  309. unsigned long flags;
  310. int res;
  311. if (!tty || !tty->driver_data ) {
  312. CPC_TTY_DBG("hdlx-tty: no TTY in close \n");
  313. return;
  314. }
  315. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  316. if ((cpc_tty->tty != tty)|| (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  317. CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
  318. return;
  319. }
  320. if (!cpc_tty->num_open) {
  321. CPC_TTY_DBG("%s: TTY is closed\n",cpc_tty->name);
  322. return;
  323. }
  324. if (--cpc_tty->num_open > 0) {
  325. CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
  326. return;
  327. }
  328. cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
  329. CPC_TTY_LOCK(cpc_tty->pc300dev->chan->card, flags); /* lock irq */
  330. cpc_tty->tty = NULL;
  331. cpc_tty->state = CPC_TTY_ST_INIT;
  332. CPC_TTY_UNLOCK(cpc_tty->pc300dev->chan->card, flags); /* unlock irq */
  333. if (cpc_tty->buf_rx.first) {
  334. unsigned char * aux;
  335. while (cpc_tty->buf_rx.first) {
  336. aux = (unsigned char *)cpc_tty->buf_rx.first;
  337. cpc_tty->buf_rx.first = cpc_tty->buf_rx.first->next;
  338. kfree(aux);
  339. }
  340. cpc_tty->buf_rx.first = NULL;
  341. cpc_tty->buf_rx.last = NULL;
  342. }
  343. kfree(cpc_tty->buf_tx);
  344. cpc_tty->buf_tx = NULL;
  345. CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
  346. if (!serial_drv.refcount && cpc_tty_unreg_flag) {
  347. cpc_tty_unreg_flag = 0;
  348. CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
  349. if ((res=tty_unregister_driver(&serial_drv))) {
  350. CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
  351. cpc_tty->name,res);
  352. }
  353. }
  354. return;
  355. }
  356. /*
  357. * PC300 TTY WRITE routine
  358. *
  359. * This routine is called by the tty driver to write a series of characters
  360. * to the tty device. The characters may come from user or kernel space.
  361. * o verify the DCD signal
  362. * o send characters to board and start the transmission
  363. */
  364. static int cpc_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  365. {
  366. st_cpc_tty_area *cpc_tty;
  367. pc300ch_t *pc300chan;
  368. pc300_t *card;
  369. int ch;
  370. unsigned long flags;
  371. struct net_device_stats *stats;
  372. if (!tty || !tty->driver_data ) {
  373. CPC_TTY_DBG("hdlcX-tty: no TTY in write\n");
  374. return -ENODEV;
  375. }
  376. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  377. if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  378. CPC_TTY_DBG("%s: TTY is not opened\n", cpc_tty->name);
  379. return -ENODEV;
  380. }
  381. if (count > CPC_TTY_MAX_MTU) {
  382. CPC_TTY_DBG("%s: count is invalid\n",cpc_tty->name);
  383. return -EINVAL; /* frame too big */
  384. }
  385. CPC_TTY_DBG("%s: cpc_tty_write data len=%i\n",cpc_tty->name,count);
  386. pc300chan = (pc300ch_t *)((pc300dev_t*)cpc_tty->pc300dev)->chan;
  387. stats = &cpc_tty->pc300dev->dev->stats;
  388. card = (pc300_t *) pc300chan->card;
  389. ch = pc300chan->channel;
  390. /* verify DCD signal*/
  391. if (cpc_readb(card->hw.scabase + M_REG(ST3,ch)) & ST3_DCD) {
  392. /* DCD is OFF */
  393. CPC_TTY_DBG("%s : DCD is OFF\n", cpc_tty->name);
  394. stats->tx_errors++;
  395. stats->tx_carrier_errors++;
  396. CPC_TTY_LOCK(card, flags);
  397. cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_BUF_CLR);
  398. if (card->hw.type == PC300_TE) {
  399. cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
  400. cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) &
  401. ~(CPLD_REG2_FALC_LED1 << (2 *ch)));
  402. }
  403. CPC_TTY_UNLOCK(card, flags);
  404. return -EINVAL;
  405. }
  406. if (cpc_tty_send_to_card(cpc_tty->pc300dev, (void*)buf, count)) {
  407. /* failed to send */
  408. CPC_TTY_DBG("%s: trasmition error\n", cpc_tty->name);
  409. return 0;
  410. }
  411. return count;
  412. }
  413. /*
  414. * PC300 TTY Write Room routine
  415. *
  416. * This routine returns the numbers of characteres the tty driver will accept
  417. * for queuing to be written.
  418. * o return MTU
  419. */
  420. static int cpc_tty_write_room(struct tty_struct *tty)
  421. {
  422. st_cpc_tty_area *cpc_tty;
  423. if (!tty || !tty->driver_data ) {
  424. CPC_TTY_DBG("hdlcX-tty: no TTY to write room\n");
  425. return -ENODEV;
  426. }
  427. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  428. if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  429. CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
  430. return -ENODEV;
  431. }
  432. CPC_TTY_DBG("%s: write room\n",cpc_tty->name);
  433. return CPC_TTY_MAX_MTU;
  434. }
  435. /*
  436. * PC300 TTY chars in buffer routine
  437. *
  438. * This routine returns the chars number in the transmission buffer
  439. * o returns 0
  440. */
  441. static int cpc_tty_chars_in_buffer(struct tty_struct *tty)
  442. {
  443. st_cpc_tty_area *cpc_tty;
  444. if (!tty || !tty->driver_data ) {
  445. CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
  446. return -ENODEV;
  447. }
  448. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  449. if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  450. CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
  451. return -ENODEV;
  452. }
  453. return(0);
  454. }
  455. static int pc300_tiocmset(struct tty_struct *tty, struct file *file,
  456. unsigned int set, unsigned int clear)
  457. {
  458. st_cpc_tty_area *cpc_tty;
  459. CPC_TTY_DBG("%s: set:%x clear:%x\n", __func__, set, clear);
  460. if (!tty || !tty->driver_data ) {
  461. CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
  462. return -ENODEV;
  463. }
  464. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  465. if (set & TIOCM_RTS)
  466. cpc_tty_signal_on(cpc_tty->pc300dev, CTL_RTS);
  467. if (set & TIOCM_DTR)
  468. cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
  469. if (clear & TIOCM_RTS)
  470. cpc_tty_signal_off(cpc_tty->pc300dev, CTL_RTS);
  471. if (clear & TIOCM_DTR)
  472. cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
  473. return 0;
  474. }
  475. static int pc300_tiocmget(struct tty_struct *tty, struct file *file)
  476. {
  477. unsigned int result;
  478. unsigned char status;
  479. unsigned long flags;
  480. st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  481. pc300dev_t *pc300dev = cpc_tty->pc300dev;
  482. pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
  483. pc300_t *card = (pc300_t *) pc300chan->card;
  484. int ch = pc300chan->channel;
  485. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  486. CPC_TTY_DBG("%s-tty: tiocmget\n",
  487. ((struct net_device*)(pc300dev->hdlc))->name);
  488. CPC_TTY_LOCK(card, flags);
  489. status = cpc_readb(card->hw.scabase+M_REG(CTL,ch));
  490. CPC_TTY_UNLOCK(card,flags);
  491. result = ((status & CTL_DTR) ? TIOCM_DTR : 0) |
  492. ((status & CTL_RTS) ? TIOCM_RTS : 0);
  493. return result;
  494. }
  495. /*
  496. * PC300 TTY Flush Buffer routine
  497. *
  498. * This routine resets the transmission buffer
  499. */
  500. static void cpc_tty_flush_buffer(struct tty_struct *tty)
  501. {
  502. st_cpc_tty_area *cpc_tty;
  503. if (!tty || !tty->driver_data ) {
  504. CPC_TTY_DBG("hdlcX-tty: no TTY to flush buffer\n");
  505. return;
  506. }
  507. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  508. if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  509. CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
  510. return;
  511. }
  512. CPC_TTY_DBG("%s: call wake_up_interruptible\n",cpc_tty->name);
  513. tty_wakeup(tty);
  514. return;
  515. }
  516. /*
  517. * PC300 TTY Hangup routine
  518. *
  519. * This routine is called by the tty driver to hangup the interface
  520. * o clear DTR signal
  521. */
  522. static void cpc_tty_hangup(struct tty_struct *tty)
  523. {
  524. st_cpc_tty_area *cpc_tty;
  525. int res;
  526. if (!tty || !tty->driver_data ) {
  527. CPC_TTY_DBG("hdlcX-tty: no TTY to hangup\n");
  528. return ;
  529. }
  530. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  531. if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  532. CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
  533. return ;
  534. }
  535. if (!serial_drv.refcount && cpc_tty_unreg_flag) {
  536. cpc_tty_unreg_flag = 0;
  537. CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
  538. if ((res=tty_unregister_driver(&serial_drv))) {
  539. CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
  540. cpc_tty->name,res);
  541. }
  542. }
  543. cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
  544. }
  545. /*
  546. * PC300 TTY RX work routine
  547. * This routine treats RX work
  548. * o verify read buffer
  549. * o call the line disc. read
  550. * o free memory
  551. */
  552. static void cpc_tty_rx_work(struct work_struct *work)
  553. {
  554. st_cpc_tty_area *cpc_tty;
  555. unsigned long port;
  556. int i, j;
  557. volatile st_cpc_rx_buf *buf;
  558. char flags=0,flg_rx=1;
  559. struct tty_ldisc *ld;
  560. if (cpc_tty_cnt == 0) return;
  561. for (i=0; (i < 4) && flg_rx ; i++) {
  562. flg_rx = 0;
  563. cpc_tty = container_of(work, st_cpc_tty_area, tty_rx_work);
  564. port = cpc_tty - cpc_tty_area;
  565. for (j=0; j < CPC_TTY_NPORTS; j++) {
  566. cpc_tty = &cpc_tty_area[port];
  567. if ((buf=cpc_tty->buf_rx.first) != NULL) {
  568. if (cpc_tty->tty) {
  569. ld = tty_ldisc_ref(cpc_tty->tty);
  570. if (ld) {
  571. if (ld->ops->receive_buf) {
  572. CPC_TTY_DBG("%s: call line disc. receive_buf\n",cpc_tty->name);
  573. ld->ops->receive_buf(cpc_tty->tty, (char *)(buf->data), &flags, buf->size);
  574. }
  575. tty_ldisc_deref(ld);
  576. }
  577. }
  578. cpc_tty->buf_rx.first = cpc_tty->buf_rx.first->next;
  579. kfree((void *)buf);
  580. buf = cpc_tty->buf_rx.first;
  581. flg_rx = 1;
  582. }
  583. if (++port == CPC_TTY_NPORTS) port = 0;
  584. }
  585. }
  586. }
  587. /*
  588. * PC300 TTY RX work routine
  589. *
  590. * This routine treats RX interrupt.
  591. * o read all frames in card
  592. * o verify the frame size
  593. * o read the frame in rx buffer
  594. */
  595. static void cpc_tty_rx_disc_frame(pc300ch_t *pc300chan)
  596. {
  597. volatile pcsca_bd_t __iomem * ptdescr;
  598. volatile unsigned char status;
  599. pc300_t *card = (pc300_t *)pc300chan->card;
  600. int ch = pc300chan->channel;
  601. /* dma buf read */
  602. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase +
  603. RX_BD_ADDR(ch, pc300chan->rx_first_bd));
  604. while (pc300chan->rx_first_bd != pc300chan->rx_last_bd) {
  605. status = cpc_readb(&ptdescr->status);
  606. cpc_writeb(&ptdescr->status, 0);
  607. cpc_writeb(&ptdescr->len, 0);
  608. pc300chan->rx_first_bd = (pc300chan->rx_first_bd + 1) &
  609. (N_DMA_RX_BUF - 1);
  610. if (status & DST_EOM) {
  611. break; /* end of message */
  612. }
  613. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase + cpc_readl(&ptdescr->next));
  614. }
  615. }
  616. void cpc_tty_receive(pc300dev_t *pc300dev)
  617. {
  618. st_cpc_tty_area *cpc_tty;
  619. pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
  620. pc300_t *card = (pc300_t *)pc300chan->card;
  621. int ch = pc300chan->channel;
  622. volatile pcsca_bd_t __iomem * ptdescr;
  623. struct net_device_stats *stats = &pc300dev->dev->stats;
  624. int rx_len, rx_aux;
  625. volatile unsigned char status;
  626. unsigned short first_bd = pc300chan->rx_first_bd;
  627. st_cpc_rx_buf *new = NULL;
  628. unsigned char dsr_rx;
  629. if (pc300dev->cpc_tty == NULL) {
  630. return;
  631. }
  632. dsr_rx = cpc_readb(card->hw.scabase + DSR_RX(ch));
  633. cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty;
  634. while (1) {
  635. rx_len = 0;
  636. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase + RX_BD_ADDR(ch, first_bd));
  637. while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
  638. rx_len += cpc_readw(&ptdescr->len);
  639. first_bd = (first_bd + 1) & (N_DMA_RX_BUF - 1);
  640. if (status & DST_EOM) {
  641. break;
  642. }
  643. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase+cpc_readl(&ptdescr->next));
  644. }
  645. if (!rx_len) {
  646. if (dsr_rx & DSR_BOF) {
  647. /* update EDA */
  648. cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch),
  649. RX_BD_ADDR(ch, pc300chan->rx_last_bd));
  650. }
  651. kfree(new);
  652. return;
  653. }
  654. if (rx_len > CPC_TTY_MAX_MTU) {
  655. /* Free RX descriptors */
  656. CPC_TTY_DBG("%s: frame size is invalid.\n",cpc_tty->name);
  657. stats->rx_errors++;
  658. stats->rx_frame_errors++;
  659. cpc_tty_rx_disc_frame(pc300chan);
  660. continue;
  661. }
  662. new = kmalloc(rx_len + sizeof(st_cpc_rx_buf), GFP_ATOMIC);
  663. if (!new) {
  664. cpc_tty_rx_disc_frame(pc300chan);
  665. continue;
  666. }
  667. /* dma buf read */
  668. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase +
  669. RX_BD_ADDR(ch, pc300chan->rx_first_bd));
  670. rx_len = 0; /* counter frame size */
  671. while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
  672. rx_aux = cpc_readw(&ptdescr->len);
  673. if ((status & (DST_OVR | DST_CRC | DST_RBIT | DST_SHRT | DST_ABT))
  674. || (rx_aux > BD_DEF_LEN)) {
  675. CPC_TTY_DBG("%s: reception error\n", cpc_tty->name);
  676. stats->rx_errors++;
  677. if (status & DST_OVR) {
  678. stats->rx_fifo_errors++;
  679. }
  680. if (status & DST_CRC) {
  681. stats->rx_crc_errors++;
  682. }
  683. if ((status & (DST_RBIT | DST_SHRT | DST_ABT)) ||
  684. (rx_aux > BD_DEF_LEN)) {
  685. stats->rx_frame_errors++;
  686. }
  687. /* discard remainig descriptors used by the bad frame */
  688. CPC_TTY_DBG("%s: reception error - discard descriptors",
  689. cpc_tty->name);
  690. cpc_tty_rx_disc_frame(pc300chan);
  691. rx_len = 0;
  692. kfree(new);
  693. new = NULL;
  694. break; /* read next frame - while(1) */
  695. }
  696. if (cpc_tty->state != CPC_TTY_ST_OPEN) {
  697. /* Free RX descriptors */
  698. cpc_tty_rx_disc_frame(pc300chan);
  699. stats->rx_dropped++;
  700. rx_len = 0;
  701. kfree(new);
  702. new = NULL;
  703. break; /* read next frame - while(1) */
  704. }
  705. /* read the segment of the frame */
  706. if (rx_aux != 0) {
  707. memcpy_fromio((new->data + rx_len),
  708. (void __iomem *)(card->hw.rambase +
  709. cpc_readl(&ptdescr->ptbuf)), rx_aux);
  710. rx_len += rx_aux;
  711. }
  712. cpc_writeb(&ptdescr->status,0);
  713. cpc_writeb(&ptdescr->len, 0);
  714. pc300chan->rx_first_bd = (pc300chan->rx_first_bd + 1) &
  715. (N_DMA_RX_BUF -1);
  716. if (status & DST_EOM)break;
  717. ptdescr = (pcsca_bd_t __iomem *) (card->hw.rambase +
  718. cpc_readl(&ptdescr->next));
  719. }
  720. /* update pointer */
  721. pc300chan->rx_last_bd = (pc300chan->rx_first_bd - 1) &
  722. (N_DMA_RX_BUF - 1) ;
  723. if (!(dsr_rx & DSR_BOF)) {
  724. /* update EDA */
  725. cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch),
  726. RX_BD_ADDR(ch, pc300chan->rx_last_bd));
  727. }
  728. if (rx_len != 0) {
  729. stats->rx_bytes += rx_len;
  730. if (pc300dev->trace_on) {
  731. cpc_tty_trace(pc300dev, new->data,rx_len, 'R');
  732. }
  733. new->size = rx_len;
  734. new->next = NULL;
  735. if (cpc_tty->buf_rx.first == NULL) {
  736. cpc_tty->buf_rx.first = new;
  737. cpc_tty->buf_rx.last = new;
  738. } else {
  739. cpc_tty->buf_rx.last->next = new;
  740. cpc_tty->buf_rx.last = new;
  741. }
  742. schedule_work(&(cpc_tty->tty_rx_work));
  743. stats->rx_packets++;
  744. }
  745. }
  746. }
  747. /*
  748. * PC300 TTY TX work routine
  749. *
  750. * This routine treats TX interrupt.
  751. * o if need call line discipline wakeup
  752. * o call wake_up_interruptible
  753. */
  754. static void cpc_tty_tx_work(struct work_struct *work)
  755. {
  756. st_cpc_tty_area *cpc_tty =
  757. container_of(work, st_cpc_tty_area, tty_tx_work);
  758. struct tty_struct *tty;
  759. CPC_TTY_DBG("%s: cpc_tty_tx_work init\n",cpc_tty->name);
  760. if ((tty = cpc_tty->tty) == NULL) {
  761. CPC_TTY_DBG("%s: the interface is not opened\n",cpc_tty->name);
  762. return;
  763. }
  764. tty_wakeup(tty);
  765. }
  766. /*
  767. * PC300 TTY send to card routine
  768. *
  769. * This routine send data to card.
  770. * o clear descriptors
  771. * o write data to DMA buffers
  772. * o start the transmission
  773. */
  774. static int cpc_tty_send_to_card(pc300dev_t *dev,void* buf, int len)
  775. {
  776. pc300ch_t *chan = (pc300ch_t *)dev->chan;
  777. pc300_t *card = (pc300_t *)chan->card;
  778. int ch = chan->channel;
  779. struct net_device_stats *stats = &dev->dev->stats;
  780. unsigned long flags;
  781. volatile pcsca_bd_t __iomem *ptdescr;
  782. int i, nchar;
  783. int tosend = len;
  784. int nbuf = ((len - 1)/BD_DEF_LEN) + 1;
  785. unsigned char *pdata=buf;
  786. CPC_TTY_DBG("%s:cpc_tty_send_to_cars len=%i",
  787. (st_cpc_tty_area *)dev->cpc_tty->name,len);
  788. if (nbuf >= card->chan[ch].nfree_tx_bd) {
  789. return 1;
  790. }
  791. /* write buffer to DMA buffers */
  792. CPC_TTY_DBG("%s: call dma_buf_write\n",
  793. (st_cpc_tty_area *)dev->cpc_tty->name);
  794. for (i = 0 ; i < nbuf ; i++) {
  795. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase +
  796. TX_BD_ADDR(ch, card->chan[ch].tx_next_bd));
  797. nchar = (BD_DEF_LEN > tosend) ? tosend : BD_DEF_LEN;
  798. if (cpc_readb(&ptdescr->status) & DST_OSB) {
  799. memcpy_toio((void __iomem *)(card->hw.rambase +
  800. cpc_readl(&ptdescr->ptbuf)),
  801. &pdata[len - tosend],
  802. nchar);
  803. card->chan[ch].nfree_tx_bd--;
  804. if ((i + 1) == nbuf) {
  805. /* This must be the last BD to be used */
  806. cpc_writeb(&ptdescr->status, DST_EOM);
  807. } else {
  808. cpc_writeb(&ptdescr->status, 0);
  809. }
  810. cpc_writew(&ptdescr->len, nchar);
  811. } else {
  812. CPC_TTY_DBG("%s: error in dma_buf_write\n",
  813. (st_cpc_tty_area *)dev->cpc_tty->name);
  814. stats->tx_dropped++;
  815. return 1;
  816. }
  817. tosend -= nchar;
  818. card->chan[ch].tx_next_bd =
  819. (card->chan[ch].tx_next_bd + 1) & (N_DMA_TX_BUF - 1);
  820. }
  821. if (dev->trace_on) {
  822. cpc_tty_trace(dev, buf, len,'T');
  823. }
  824. /* start transmission */
  825. CPC_TTY_DBG("%s: start transmission\n",
  826. (st_cpc_tty_area *)dev->cpc_tty->name);
  827. CPC_TTY_LOCK(card, flags);
  828. cpc_writeb(card->hw.scabase + DTX_REG(EDAL, ch),
  829. TX_BD_ADDR(ch, chan->tx_next_bd));
  830. cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_ENA);
  831. cpc_writeb(card->hw.scabase + DSR_TX(ch), DSR_DE);
  832. if (card->hw.type == PC300_TE) {
  833. cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
  834. cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) |
  835. (CPLD_REG2_FALC_LED1 << (2 * ch)));
  836. }
  837. CPC_TTY_UNLOCK(card, flags);
  838. return 0;
  839. }
  840. /*
  841. * PC300 TTY trace routine
  842. *
  843. * This routine send trace of connection to application.
  844. * o clear descriptors
  845. * o write data to DMA buffers
  846. * o start the transmission
  847. */
  848. static void cpc_tty_trace(pc300dev_t *dev, char* buf, int len, char rxtx)
  849. {
  850. struct sk_buff *skb;
  851. if ((skb = dev_alloc_skb(10 + len)) == NULL) {
  852. /* out of memory */
  853. CPC_TTY_DBG("%s: tty_trace - out of memory\n", dev->dev->name);
  854. return;
  855. }
  856. skb_put (skb, 10 + len);
  857. skb->dev = dev->dev;
  858. skb->protocol = htons(ETH_P_CUST);
  859. skb_reset_mac_header(skb);
  860. skb->pkt_type = PACKET_HOST;
  861. skb->len = 10 + len;
  862. skb_copy_to_linear_data(skb, dev->dev->name, 5);
  863. skb->data[5] = '[';
  864. skb->data[6] = rxtx;
  865. skb->data[7] = ']';
  866. skb->data[8] = ':';
  867. skb->data[9] = ' ';
  868. skb_copy_to_linear_data_offset(skb, 10, buf, len);
  869. netif_rx(skb);
  870. }
  871. /*
  872. * PC300 TTY unregister service routine
  873. *
  874. * This routine unregister one interface.
  875. */
  876. void cpc_tty_unregister_service(pc300dev_t *pc300dev)
  877. {
  878. st_cpc_tty_area *cpc_tty;
  879. ulong flags;
  880. int res;
  881. if ((cpc_tty= (st_cpc_tty_area *) pc300dev->cpc_tty) == NULL) {
  882. CPC_TTY_DBG("%s: interface is not TTY\n", pc300dev->dev->name);
  883. return;
  884. }
  885. CPC_TTY_DBG("%s: cpc_tty_unregister_service", cpc_tty->name);
  886. if (cpc_tty->pc300dev != pc300dev) {
  887. CPC_TTY_DBG("%s: invalid tty ptr=%s\n",
  888. pc300dev->dev->name, cpc_tty->name);
  889. return;
  890. }
  891. if (--cpc_tty_cnt == 0) {
  892. if (serial_drv.refcount) {
  893. CPC_TTY_DBG("%s: unregister is not possible, refcount=%d",
  894. cpc_tty->name, serial_drv.refcount);
  895. cpc_tty_cnt++;
  896. cpc_tty_unreg_flag = 1;
  897. return;
  898. } else {
  899. CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
  900. if ((res=tty_unregister_driver(&serial_drv))) {
  901. CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
  902. cpc_tty->name,res);
  903. }
  904. }
  905. }
  906. CPC_TTY_LOCK(pc300dev->chan->card,flags);
  907. cpc_tty->tty = NULL;
  908. CPC_TTY_UNLOCK(pc300dev->chan->card, flags);
  909. cpc_tty->tty_minor = 0;
  910. cpc_tty->state = CPC_TTY_ST_IDLE;
  911. }
  912. /*
  913. * PC300 TTY trigger poll routine
  914. * This routine is called by pc300driver to treats Tx interrupt.
  915. */
  916. void cpc_tty_trigger_poll(pc300dev_t *pc300dev)
  917. {
  918. st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty;
  919. if (!cpc_tty) {
  920. return;
  921. }
  922. schedule_work(&(cpc_tty->tty_tx_work));
  923. }
  924. /*
  925. * PC300 TTY reset var routine
  926. * This routine is called by pc300driver to init the TTY area.
  927. */
  928. void cpc_tty_reset_var(void)
  929. {
  930. int i ;
  931. CPC_TTY_DBG("hdlcX-tty: reset variables\n");
  932. /* reset the tty_driver structure - serial_drv */
  933. memset(&serial_drv, 0, sizeof(struct tty_driver));
  934. for (i=0; i < CPC_TTY_NPORTS; i++){
  935. memset(&cpc_tty_area[i],0, sizeof(st_cpc_tty_area));
  936. }
  937. }