pc300_tty.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. st_cpc_tty_area cpc_tty_area[CPC_TTY_NPORTS];
  102. int cpc_tty_cnt=0; /* number of intrfaces configured with MLPPP */
  103. 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. int pc300_tiocmset(struct tty_struct *, struct file *,
  119. unsigned int, unsigned int);
  120. 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. if (cpc_tty->buf_tx) {
  342. kfree(cpc_tty->buf_tx);
  343. cpc_tty->buf_tx = NULL;
  344. }
  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 = hdlc_stats(((pc300dev_t*)cpc_tty->pc300dev)->dev);
  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. 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", __FUNCTION__, 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. 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(void * data)
  553. {
  554. unsigned long port;
  555. int i, j;
  556. st_cpc_tty_area *cpc_tty;
  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. port = (unsigned long)data;
  564. for (j=0; j < CPC_TTY_NPORTS; j++) {
  565. cpc_tty = &cpc_tty_area[port];
  566. if ((buf=cpc_tty->buf_rx.first) != 0) {
  567. if(cpc_tty->tty) {
  568. ld = tty_ldisc_ref(cpc_tty->tty);
  569. if(ld) {
  570. if (ld->receive_buf) {
  571. CPC_TTY_DBG("%s: call line disc. receive_buf\n",cpc_tty->name);
  572. ld->receive_buf(cpc_tty->tty, (char *)(buf->data), &flags, buf->size);
  573. }
  574. tty_ldisc_deref(ld);
  575. }
  576. }
  577. cpc_tty->buf_rx.first = cpc_tty->buf_rx.first->next;
  578. kfree((unsigned char *)buf);
  579. buf = cpc_tty->buf_rx.first;
  580. flg_rx = 1;
  581. }
  582. if (++port == CPC_TTY_NPORTS) port = 0;
  583. }
  584. }
  585. }
  586. /*
  587. * PC300 TTY RX work routine
  588. *
  589. * This routine treats RX interrupt.
  590. * o read all frames in card
  591. * o verify the frame size
  592. * o read the frame in rx buffer
  593. */
  594. static void cpc_tty_rx_disc_frame(pc300ch_t *pc300chan)
  595. {
  596. volatile pcsca_bd_t __iomem * ptdescr;
  597. volatile unsigned char status;
  598. pc300_t *card = (pc300_t *)pc300chan->card;
  599. int ch = pc300chan->channel;
  600. /* dma buf read */
  601. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase +
  602. RX_BD_ADDR(ch, pc300chan->rx_first_bd));
  603. while (pc300chan->rx_first_bd != pc300chan->rx_last_bd) {
  604. status = cpc_readb(&ptdescr->status);
  605. cpc_writeb(&ptdescr->status, 0);
  606. cpc_writeb(&ptdescr->len, 0);
  607. pc300chan->rx_first_bd = (pc300chan->rx_first_bd + 1) &
  608. (N_DMA_RX_BUF - 1);
  609. if (status & DST_EOM) {
  610. break; /* end of message */
  611. }
  612. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase + cpc_readl(&ptdescr->next));
  613. }
  614. }
  615. void cpc_tty_receive(pc300dev_t *pc300dev)
  616. {
  617. st_cpc_tty_area *cpc_tty;
  618. pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
  619. pc300_t *card = (pc300_t *)pc300chan->card;
  620. int ch = pc300chan->channel;
  621. volatile pcsca_bd_t __iomem * ptdescr;
  622. struct net_device_stats *stats = hdlc_stats(pc300dev->dev);
  623. int rx_len, rx_aux;
  624. volatile unsigned char status;
  625. unsigned short first_bd = pc300chan->rx_first_bd;
  626. st_cpc_rx_buf *new=NULL;
  627. unsigned char dsr_rx;
  628. if (pc300dev->cpc_tty == NULL) {
  629. return;
  630. }
  631. dsr_rx = cpc_readb(card->hw.scabase + DSR_RX(ch));
  632. cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty;
  633. while (1) {
  634. rx_len = 0;
  635. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase + RX_BD_ADDR(ch, first_bd));
  636. while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
  637. rx_len += cpc_readw(&ptdescr->len);
  638. first_bd = (first_bd + 1) & (N_DMA_RX_BUF - 1);
  639. if (status & DST_EOM) {
  640. break;
  641. }
  642. ptdescr=(pcsca_bd_t __iomem *)(card->hw.rambase+cpc_readl(&ptdescr->next));
  643. }
  644. if (!rx_len) {
  645. if (dsr_rx & DSR_BOF) {
  646. /* update EDA */
  647. cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch),
  648. RX_BD_ADDR(ch, pc300chan->rx_last_bd));
  649. }
  650. if (new) {
  651. kfree(new);
  652. new = NULL;
  653. }
  654. return;
  655. }
  656. if (rx_len > CPC_TTY_MAX_MTU) {
  657. /* Free RX descriptors */
  658. CPC_TTY_DBG("%s: frame size is invalid.\n",cpc_tty->name);
  659. stats->rx_errors++;
  660. stats->rx_frame_errors++;
  661. cpc_tty_rx_disc_frame(pc300chan);
  662. continue;
  663. }
  664. new = (st_cpc_rx_buf *) kmalloc(rx_len + sizeof(st_cpc_rx_buf), GFP_ATOMIC);
  665. if (new == 0) {
  666. cpc_tty_rx_disc_frame(pc300chan);
  667. continue;
  668. }
  669. /* dma buf read */
  670. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase +
  671. RX_BD_ADDR(ch, pc300chan->rx_first_bd));
  672. rx_len = 0; /* counter frame size */
  673. while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
  674. rx_aux = cpc_readw(&ptdescr->len);
  675. if ((status & (DST_OVR | DST_CRC | DST_RBIT | DST_SHRT | DST_ABT))
  676. || (rx_aux > BD_DEF_LEN)) {
  677. CPC_TTY_DBG("%s: reception error\n", cpc_tty->name);
  678. stats->rx_errors++;
  679. if (status & DST_OVR) {
  680. stats->rx_fifo_errors++;
  681. }
  682. if (status & DST_CRC) {
  683. stats->rx_crc_errors++;
  684. }
  685. if ((status & (DST_RBIT | DST_SHRT | DST_ABT)) ||
  686. (rx_aux > BD_DEF_LEN)) {
  687. stats->rx_frame_errors++;
  688. }
  689. /* discard remainig descriptors used by the bad frame */
  690. CPC_TTY_DBG("%s: reception error - discard descriptors",
  691. cpc_tty->name);
  692. cpc_tty_rx_disc_frame(pc300chan);
  693. rx_len = 0;
  694. kfree(new);
  695. new = NULL;
  696. break; /* read next frame - while(1) */
  697. }
  698. if (cpc_tty->state != CPC_TTY_ST_OPEN) {
  699. /* Free RX descriptors */
  700. cpc_tty_rx_disc_frame(pc300chan);
  701. stats->rx_dropped++;
  702. rx_len = 0;
  703. kfree(new);
  704. new = NULL;
  705. break; /* read next frame - while(1) */
  706. }
  707. /* read the segment of the frame */
  708. if (rx_aux != 0) {
  709. memcpy_fromio((new->data + rx_len),
  710. (void __iomem *)(card->hw.rambase +
  711. cpc_readl(&ptdescr->ptbuf)), rx_aux);
  712. rx_len += rx_aux;
  713. }
  714. cpc_writeb(&ptdescr->status,0);
  715. cpc_writeb(&ptdescr->len, 0);
  716. pc300chan->rx_first_bd = (pc300chan->rx_first_bd + 1) &
  717. (N_DMA_RX_BUF -1);
  718. if (status & DST_EOM)break;
  719. ptdescr = (pcsca_bd_t __iomem *) (card->hw.rambase +
  720. cpc_readl(&ptdescr->next));
  721. }
  722. /* update pointer */
  723. pc300chan->rx_last_bd = (pc300chan->rx_first_bd - 1) &
  724. (N_DMA_RX_BUF - 1) ;
  725. if (!(dsr_rx & DSR_BOF)) {
  726. /* update EDA */
  727. cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch),
  728. RX_BD_ADDR(ch, pc300chan->rx_last_bd));
  729. }
  730. if (rx_len != 0) {
  731. stats->rx_bytes += rx_len;
  732. if (pc300dev->trace_on) {
  733. cpc_tty_trace(pc300dev, new->data,rx_len, 'R');
  734. }
  735. new->size = rx_len;
  736. new->next = NULL;
  737. if (cpc_tty->buf_rx.first == 0) {
  738. cpc_tty->buf_rx.first = new;
  739. cpc_tty->buf_rx.last = new;
  740. } else {
  741. cpc_tty->buf_rx.last->next = new;
  742. cpc_tty->buf_rx.last = new;
  743. }
  744. schedule_work(&(cpc_tty->tty_rx_work));
  745. stats->rx_packets++;
  746. }
  747. }
  748. }
  749. /*
  750. * PC300 TTY TX work routine
  751. *
  752. * This routine treats TX interrupt.
  753. * o if need call line discipline wakeup
  754. * o call wake_up_interruptible
  755. */
  756. static void cpc_tty_tx_work(void *data)
  757. {
  758. st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *) data;
  759. struct tty_struct *tty;
  760. CPC_TTY_DBG("%s: cpc_tty_tx_work init\n",cpc_tty->name);
  761. if ((tty = cpc_tty->tty) == 0) {
  762. CPC_TTY_DBG("%s: the interface is not opened\n",cpc_tty->name);
  763. return;
  764. }
  765. tty_wakeup(tty);
  766. }
  767. /*
  768. * PC300 TTY send to card routine
  769. *
  770. * This routine send data to card.
  771. * o clear descriptors
  772. * o write data to DMA buffers
  773. * o start the transmission
  774. */
  775. static int cpc_tty_send_to_card(pc300dev_t *dev,void* buf, int len)
  776. {
  777. pc300ch_t *chan = (pc300ch_t *)dev->chan;
  778. pc300_t *card = (pc300_t *)chan->card;
  779. int ch = chan->channel;
  780. struct net_device_stats *stats = hdlc_stats(dev->dev);
  781. unsigned long flags;
  782. volatile pcsca_bd_t __iomem *ptdescr;
  783. int i, nchar;
  784. int tosend = len;
  785. int nbuf = ((len - 1)/BD_DEF_LEN) + 1;
  786. unsigned char *pdata=buf;
  787. CPC_TTY_DBG("%s:cpc_tty_send_to_cars len=%i",
  788. (st_cpc_tty_area *)dev->cpc_tty->name,len);
  789. if (nbuf >= card->chan[ch].nfree_tx_bd) {
  790. return 1;
  791. }
  792. /* write buffer to DMA buffers */
  793. CPC_TTY_DBG("%s: call dma_buf_write\n",
  794. (st_cpc_tty_area *)dev->cpc_tty->name);
  795. for (i = 0 ; i < nbuf ; i++) {
  796. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase +
  797. TX_BD_ADDR(ch, card->chan[ch].tx_next_bd));
  798. nchar = (BD_DEF_LEN > tosend) ? tosend : BD_DEF_LEN;
  799. if (cpc_readb(&ptdescr->status) & DST_OSB) {
  800. memcpy_toio((void __iomem *)(card->hw.rambase +
  801. cpc_readl(&ptdescr->ptbuf)),
  802. &pdata[len - tosend],
  803. nchar);
  804. card->chan[ch].nfree_tx_bd--;
  805. if ((i + 1) == nbuf) {
  806. /* This must be the last BD to be used */
  807. cpc_writeb(&ptdescr->status, DST_EOM);
  808. } else {
  809. cpc_writeb(&ptdescr->status, 0);
  810. }
  811. cpc_writew(&ptdescr->len, nchar);
  812. } else {
  813. CPC_TTY_DBG("%s: error in dma_buf_write\n",
  814. (st_cpc_tty_area *)dev->cpc_tty->name);
  815. stats->tx_dropped++;
  816. return 1;
  817. }
  818. tosend -= nchar;
  819. card->chan[ch].tx_next_bd =
  820. (card->chan[ch].tx_next_bd + 1) & (N_DMA_TX_BUF - 1);
  821. }
  822. if (dev->trace_on) {
  823. cpc_tty_trace(dev, buf, len,'T');
  824. }
  825. /* start transmission */
  826. CPC_TTY_DBG("%s: start transmission\n",
  827. (st_cpc_tty_area *)dev->cpc_tty->name);
  828. CPC_TTY_LOCK(card, flags);
  829. cpc_writeb(card->hw.scabase + DTX_REG(EDAL, ch),
  830. TX_BD_ADDR(ch, chan->tx_next_bd));
  831. cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_ENA);
  832. cpc_writeb(card->hw.scabase + DSR_TX(ch), DSR_DE);
  833. if (card->hw.type == PC300_TE) {
  834. cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
  835. cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) |
  836. (CPLD_REG2_FALC_LED1 << (2 * ch)));
  837. }
  838. CPC_TTY_UNLOCK(card, flags);
  839. return 0;
  840. }
  841. /*
  842. * PC300 TTY trace routine
  843. *
  844. * This routine send trace of connection to application.
  845. * o clear descriptors
  846. * o write data to DMA buffers
  847. * o start the transmission
  848. */
  849. static void cpc_tty_trace(pc300dev_t *dev, char* buf, int len, char rxtx)
  850. {
  851. struct sk_buff *skb;
  852. if ((skb = dev_alloc_skb(10 + len)) == NULL) {
  853. /* out of memory */
  854. CPC_TTY_DBG("%s: tty_trace - out of memory\n", dev->dev->name);
  855. return;
  856. }
  857. skb_put (skb, 10 + len);
  858. skb->dev = dev->dev;
  859. skb->protocol = htons(ETH_P_CUST);
  860. skb->mac.raw = skb->data;
  861. skb->pkt_type = PACKET_HOST;
  862. skb->len = 10 + len;
  863. memcpy(skb->data,dev->dev->name,5);
  864. skb->data[5] = '[';
  865. skb->data[6] = rxtx;
  866. skb->data[7] = ']';
  867. skb->data[8] = ':';
  868. skb->data[9] = ' ';
  869. memcpy(&skb->data[10], buf, len);
  870. netif_rx(skb);
  871. }
  872. /*
  873. * PC300 TTY unregister service routine
  874. *
  875. * This routine unregister one interface.
  876. */
  877. void cpc_tty_unregister_service(pc300dev_t *pc300dev)
  878. {
  879. st_cpc_tty_area *cpc_tty;
  880. ulong flags;
  881. int res;
  882. if ((cpc_tty= (st_cpc_tty_area *) pc300dev->cpc_tty) == 0) {
  883. CPC_TTY_DBG("%s: interface is not TTY\n", pc300dev->dev->name);
  884. return;
  885. }
  886. CPC_TTY_DBG("%s: cpc_tty_unregister_service", cpc_tty->name);
  887. if (cpc_tty->pc300dev != pc300dev) {
  888. CPC_TTY_DBG("%s: invalid tty ptr=%s\n",
  889. pc300dev->dev->name, cpc_tty->name);
  890. return;
  891. }
  892. if (--cpc_tty_cnt == 0) {
  893. if (serial_drv.refcount) {
  894. CPC_TTY_DBG("%s: unregister is not possible, refcount=%d",
  895. cpc_tty->name, serial_drv.refcount);
  896. cpc_tty_cnt++;
  897. cpc_tty_unreg_flag = 1;
  898. return;
  899. } else {
  900. CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
  901. if ((res=tty_unregister_driver(&serial_drv))) {
  902. CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
  903. cpc_tty->name,res);
  904. }
  905. }
  906. }
  907. CPC_TTY_LOCK(pc300dev->chan->card,flags);
  908. cpc_tty->tty = NULL;
  909. CPC_TTY_UNLOCK(pc300dev->chan->card, flags);
  910. cpc_tty->tty_minor = 0;
  911. cpc_tty->state = CPC_TTY_ST_IDLE;
  912. }
  913. /*
  914. * PC300 TTY trigger poll routine
  915. * This routine is called by pc300driver to treats Tx interrupt.
  916. */
  917. void cpc_tty_trigger_poll(pc300dev_t *pc300dev)
  918. {
  919. st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty;
  920. if (!cpc_tty) {
  921. return;
  922. }
  923. schedule_work(&(cpc_tty->tty_tx_work));
  924. }
  925. /*
  926. * PC300 TTY reset var routine
  927. * This routine is called by pc300driver to init the TTY area.
  928. */
  929. void cpc_tty_reset_var(void)
  930. {
  931. int i ;
  932. CPC_TTY_DBG("hdlcX-tty: reset variables\n");
  933. /* reset the tty_driver structure - serial_drv */
  934. memset(&serial_drv, 0, sizeof(struct tty_driver));
  935. for (i=0; i < CPC_TTY_NPORTS; i++){
  936. memset(&cpc_tty_area[i],0, sizeof(st_cpc_tty_area));
  937. }
  938. }