pc300_tty.c 29 KB

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