pc300_tty.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. /*
  159. * PC300 TTY initialization routine
  160. *
  161. * This routine is called by the PC300 driver during board configuration
  162. * (ioctl=SIOCSP300CONF). At this point the adapter is completely
  163. * initialized.
  164. * o verify kernel version (only 2.4.x)
  165. * o register TTY driver
  166. * o init cpc_tty_area struct
  167. */
  168. void cpc_tty_init(pc300dev_t *pc300dev)
  169. {
  170. unsigned long port;
  171. int aux;
  172. st_cpc_tty_area * cpc_tty;
  173. /* hdlcX - X=interface number */
  174. port = pc300dev->dev->name[4] - '0';
  175. if (port >= CPC_TTY_NPORTS) {
  176. printk("%s-tty: invalid interface selected (0-%i): %li",
  177. pc300dev->dev->name,
  178. CPC_TTY_NPORTS-1,port);
  179. return;
  180. }
  181. if (cpc_tty_cnt == 0) { /* first TTY connection -> register driver */
  182. CPC_TTY_DBG("%s-tty: driver init, major:%i, minor range:%i=%i\n",
  183. pc300dev->dev->name,
  184. CPC_TTY_MAJOR, CPC_TTY_MINOR_START,
  185. CPC_TTY_MINOR_START+CPC_TTY_NPORTS);
  186. /* initialize tty driver struct */
  187. memset(&serial_drv,0,sizeof(struct tty_driver));
  188. serial_drv.magic = TTY_DRIVER_MAGIC;
  189. serial_drv.owner = THIS_MODULE;
  190. serial_drv.driver_name = "pc300_tty";
  191. serial_drv.name = "ttyCP";
  192. serial_drv.major = CPC_TTY_MAJOR;
  193. serial_drv.minor_start = CPC_TTY_MINOR_START;
  194. serial_drv.num = CPC_TTY_NPORTS;
  195. serial_drv.type = TTY_DRIVER_TYPE_SERIAL;
  196. serial_drv.subtype = SERIAL_TYPE_NORMAL;
  197. serial_drv.init_termios = tty_std_termios;
  198. serial_drv.init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
  199. serial_drv.flags = TTY_DRIVER_REAL_RAW;
  200. /* interface routines from the upper tty layer to the tty driver */
  201. serial_drv.open = cpc_tty_open;
  202. serial_drv.close = cpc_tty_close;
  203. serial_drv.write = cpc_tty_write;
  204. serial_drv.write_room = cpc_tty_write_room;
  205. serial_drv.chars_in_buffer = cpc_tty_chars_in_buffer;
  206. serial_drv.tiocmset = pc300_tiocmset;
  207. serial_drv.tiocmget = pc300_tiocmget;
  208. serial_drv.flush_buffer = cpc_tty_flush_buffer;
  209. serial_drv.hangup = cpc_tty_hangup;
  210. /* register the TTY driver */
  211. if (tty_register_driver(&serial_drv)) {
  212. printk("%s-tty: Failed to register serial driver! ",
  213. pc300dev->dev->name);
  214. return;
  215. }
  216. memset((void *)cpc_tty_area, 0,
  217. sizeof(st_cpc_tty_area) * CPC_TTY_NPORTS);
  218. }
  219. cpc_tty = &cpc_tty_area[port];
  220. if (cpc_tty->state != CPC_TTY_ST_IDLE) {
  221. CPC_TTY_DBG("%s-tty: TTY port %i, already in use.\n",
  222. pc300dev->dev->name, port);
  223. return;
  224. }
  225. cpc_tty_cnt++;
  226. cpc_tty->state = CPC_TTY_ST_INIT;
  227. cpc_tty->num_open= 0;
  228. cpc_tty->tty_minor = port + CPC_TTY_MINOR_START;
  229. cpc_tty->pc300dev = pc300dev;
  230. INIT_WORK(&cpc_tty->tty_tx_work, cpc_tty_tx_work);
  231. INIT_WORK(&cpc_tty->tty_rx_work, cpc_tty_rx_work);
  232. cpc_tty->buf_rx.first = cpc_tty->buf_rx.last = NULL;
  233. pc300dev->cpc_tty = (void *)cpc_tty;
  234. aux = strlen(pc300dev->dev->name);
  235. memcpy(cpc_tty->name, pc300dev->dev->name, aux);
  236. memcpy(&cpc_tty->name[aux], "-tty", 5);
  237. cpc_open(pc300dev->dev);
  238. cpc_tty_signal_off(pc300dev, CTL_DTR);
  239. CPC_TTY_DBG("%s: Initializing TTY Sync Driver, tty major#%d minor#%i\n",
  240. cpc_tty->name,CPC_TTY_MAJOR,cpc_tty->tty_minor);
  241. return;
  242. }
  243. /*
  244. * PC300 TTY OPEN routine
  245. *
  246. * This routine is called by the tty driver to open the interface
  247. * o verify minor
  248. * o allocate buffer to Rx and Tx
  249. */
  250. static int cpc_tty_open(struct tty_struct *tty, struct file *flip)
  251. {
  252. int port ;
  253. st_cpc_tty_area *cpc_tty;
  254. if (!tty) {
  255. return -ENODEV;
  256. }
  257. port = tty->index;
  258. if ((port < 0) || (port >= CPC_TTY_NPORTS)){
  259. CPC_TTY_DBG("pc300_tty: open invalid port %d\n", port);
  260. return -ENODEV;
  261. }
  262. cpc_tty = &cpc_tty_area[port];
  263. if (cpc_tty->state == CPC_TTY_ST_IDLE){
  264. CPC_TTY_DBG("%s: open - invalid interface, port=%d\n",
  265. cpc_tty->name, tty->index);
  266. return -ENODEV;
  267. }
  268. if (cpc_tty->num_open == 0) { /* first open of this tty */
  269. if (!cpc_tty_area[port].buf_tx){
  270. cpc_tty_area[port].buf_tx = kmalloc(CPC_TTY_MAX_MTU,GFP_KERNEL);
  271. if (cpc_tty_area[port].buf_tx == 0){
  272. CPC_TTY_DBG("%s: error in memory allocation\n",cpc_tty->name);
  273. return -ENOMEM;
  274. }
  275. }
  276. if (cpc_tty_area[port].buf_rx.first) {
  277. unsigned char * aux;
  278. while (cpc_tty_area[port].buf_rx.first) {
  279. aux = (unsigned char *)cpc_tty_area[port].buf_rx.first;
  280. cpc_tty_area[port].buf_rx.first = cpc_tty_area[port].buf_rx.first->next;
  281. kfree(aux);
  282. }
  283. cpc_tty_area[port].buf_rx.first = NULL;
  284. cpc_tty_area[port].buf_rx.last = NULL;
  285. }
  286. cpc_tty_area[port].state = CPC_TTY_ST_OPEN;
  287. cpc_tty_area[port].tty = tty;
  288. tty->driver_data = &cpc_tty_area[port];
  289. cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
  290. }
  291. cpc_tty->num_open++;
  292. CPC_TTY_DBG("%s: opening TTY driver\n", cpc_tty->name);
  293. /* avisar driver PC300 */
  294. return 0;
  295. }
  296. /*
  297. * PC300 TTY CLOSE routine
  298. *
  299. * This routine is called by the tty driver to close the interface
  300. * o call close channel in PC300 driver (cpc_closech)
  301. * o free Rx and Tx buffers
  302. */
  303. static void cpc_tty_close(struct tty_struct *tty, struct file *flip)
  304. {
  305. st_cpc_tty_area *cpc_tty;
  306. unsigned long flags;
  307. int res;
  308. if (!tty || !tty->driver_data ) {
  309. CPC_TTY_DBG("hdlx-tty: no TTY in close \n");
  310. return;
  311. }
  312. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  313. if ((cpc_tty->tty != tty)|| (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  314. CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
  315. return;
  316. }
  317. if (!cpc_tty->num_open) {
  318. CPC_TTY_DBG("%s: TTY is closed\n",cpc_tty->name);
  319. return;
  320. }
  321. if (--cpc_tty->num_open > 0) {
  322. CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
  323. return;
  324. }
  325. cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
  326. CPC_TTY_LOCK(cpc_tty->pc300dev->chan->card, flags); /* lock irq */
  327. cpc_tty->tty = NULL;
  328. cpc_tty->state = CPC_TTY_ST_INIT;
  329. CPC_TTY_UNLOCK(cpc_tty->pc300dev->chan->card, flags); /* unlock irq */
  330. if (cpc_tty->buf_rx.first) {
  331. unsigned char * aux;
  332. while (cpc_tty->buf_rx.first) {
  333. aux = (unsigned char *)cpc_tty->buf_rx.first;
  334. cpc_tty->buf_rx.first = cpc_tty->buf_rx.first->next;
  335. kfree(aux);
  336. }
  337. cpc_tty->buf_rx.first = NULL;
  338. cpc_tty->buf_rx.last = NULL;
  339. }
  340. kfree(cpc_tty->buf_tx);
  341. cpc_tty->buf_tx = NULL;
  342. CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
  343. if (!serial_drv.refcount && cpc_tty_unreg_flag) {
  344. cpc_tty_unreg_flag = 0;
  345. CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
  346. if ((res=tty_unregister_driver(&serial_drv))) {
  347. CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
  348. cpc_tty->name,res);
  349. }
  350. }
  351. return;
  352. }
  353. /*
  354. * PC300 TTY WRITE routine
  355. *
  356. * This routine is called by the tty driver to write a series of characters
  357. * to the tty device. The characters may come from user or kernel space.
  358. * o verify the DCD signal
  359. * o send characters to board and start the transmission
  360. */
  361. static int cpc_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  362. {
  363. st_cpc_tty_area *cpc_tty;
  364. pc300ch_t *pc300chan;
  365. pc300_t *card;
  366. int ch;
  367. unsigned long flags;
  368. struct net_device_stats *stats;
  369. if (!tty || !tty->driver_data ) {
  370. CPC_TTY_DBG("hdlcX-tty: no TTY in write\n");
  371. return -ENODEV;
  372. }
  373. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  374. if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  375. CPC_TTY_DBG("%s: TTY is not opened\n", cpc_tty->name);
  376. return -ENODEV;
  377. }
  378. if (count > CPC_TTY_MAX_MTU) {
  379. CPC_TTY_DBG("%s: count is invalid\n",cpc_tty->name);
  380. return -EINVAL; /* frame too big */
  381. }
  382. CPC_TTY_DBG("%s: cpc_tty_write data len=%i\n",cpc_tty->name,count);
  383. pc300chan = (pc300ch_t *)((pc300dev_t*)cpc_tty->pc300dev)->chan;
  384. stats = hdlc_stats(((pc300dev_t*)cpc_tty->pc300dev)->dev);
  385. card = (pc300_t *) pc300chan->card;
  386. ch = pc300chan->channel;
  387. /* verify DCD signal*/
  388. if (cpc_readb(card->hw.scabase + M_REG(ST3,ch)) & ST3_DCD) {
  389. /* DCD is OFF */
  390. CPC_TTY_DBG("%s : DCD is OFF\n", cpc_tty->name);
  391. stats->tx_errors++;
  392. stats->tx_carrier_errors++;
  393. CPC_TTY_LOCK(card, flags);
  394. cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_BUF_CLR);
  395. if (card->hw.type == PC300_TE) {
  396. cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
  397. cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) &
  398. ~(CPLD_REG2_FALC_LED1 << (2 *ch)));
  399. }
  400. CPC_TTY_UNLOCK(card, flags);
  401. return -EINVAL;
  402. }
  403. if (cpc_tty_send_to_card(cpc_tty->pc300dev, (void*)buf, count)) {
  404. /* failed to send */
  405. CPC_TTY_DBG("%s: trasmition error\n", cpc_tty->name);
  406. return 0;
  407. }
  408. return count;
  409. }
  410. /*
  411. * PC300 TTY Write Room routine
  412. *
  413. * This routine returns the numbers of characteres the tty driver will accept
  414. * for queuing to be written.
  415. * o return MTU
  416. */
  417. static int cpc_tty_write_room(struct tty_struct *tty)
  418. {
  419. st_cpc_tty_area *cpc_tty;
  420. if (!tty || !tty->driver_data ) {
  421. CPC_TTY_DBG("hdlcX-tty: no TTY to write room\n");
  422. return -ENODEV;
  423. }
  424. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  425. if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  426. CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
  427. return -ENODEV;
  428. }
  429. CPC_TTY_DBG("%s: write room\n",cpc_tty->name);
  430. return CPC_TTY_MAX_MTU;
  431. }
  432. /*
  433. * PC300 TTY chars in buffer routine
  434. *
  435. * This routine returns the chars number in the transmission buffer
  436. * o returns 0
  437. */
  438. static int cpc_tty_chars_in_buffer(struct tty_struct *tty)
  439. {
  440. st_cpc_tty_area *cpc_tty;
  441. if (!tty || !tty->driver_data ) {
  442. CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
  443. return -ENODEV;
  444. }
  445. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  446. if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  447. CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
  448. return -ENODEV;
  449. }
  450. return(0);
  451. }
  452. static int pc300_tiocmset(struct tty_struct *tty, struct file *file,
  453. unsigned int set, unsigned int clear)
  454. {
  455. st_cpc_tty_area *cpc_tty;
  456. CPC_TTY_DBG("%s: set:%x clear:%x\n", __FUNCTION__, set, clear);
  457. if (!tty || !tty->driver_data ) {
  458. CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
  459. return -ENODEV;
  460. }
  461. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  462. if (set & TIOCM_RTS)
  463. cpc_tty_signal_on(cpc_tty->pc300dev, CTL_RTS);
  464. if (set & TIOCM_DTR)
  465. cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
  466. if (clear & TIOCM_RTS)
  467. cpc_tty_signal_off(cpc_tty->pc300dev, CTL_RTS);
  468. if (clear & TIOCM_DTR)
  469. cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
  470. return 0;
  471. }
  472. static int pc300_tiocmget(struct tty_struct *tty, struct file *file)
  473. {
  474. unsigned int result;
  475. unsigned char status;
  476. unsigned long flags;
  477. st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  478. pc300dev_t *pc300dev = cpc_tty->pc300dev;
  479. pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
  480. pc300_t *card = (pc300_t *) pc300chan->card;
  481. int ch = pc300chan->channel;
  482. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  483. CPC_TTY_DBG("%s-tty: tiocmget\n",
  484. ((struct net_device*)(pc300dev->hdlc))->name);
  485. CPC_TTY_LOCK(card, flags);
  486. status = cpc_readb(card->hw.scabase+M_REG(CTL,ch));
  487. CPC_TTY_UNLOCK(card,flags);
  488. result = ((status & CTL_DTR) ? TIOCM_DTR : 0) |
  489. ((status & CTL_RTS) ? TIOCM_RTS : 0);
  490. return result;
  491. }
  492. /*
  493. * PC300 TTY Flush Buffer routine
  494. *
  495. * This routine resets the transmission buffer
  496. */
  497. static void cpc_tty_flush_buffer(struct tty_struct *tty)
  498. {
  499. st_cpc_tty_area *cpc_tty;
  500. if (!tty || !tty->driver_data ) {
  501. CPC_TTY_DBG("hdlcX-tty: no TTY to flush buffer\n");
  502. return;
  503. }
  504. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  505. if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  506. CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
  507. return;
  508. }
  509. CPC_TTY_DBG("%s: call wake_up_interruptible\n",cpc_tty->name);
  510. tty_wakeup(tty);
  511. return;
  512. }
  513. /*
  514. * PC300 TTY Hangup routine
  515. *
  516. * This routine is called by the tty driver to hangup the interface
  517. * o clear DTR signal
  518. */
  519. static void cpc_tty_hangup(struct tty_struct *tty)
  520. {
  521. st_cpc_tty_area *cpc_tty;
  522. int res;
  523. if (!tty || !tty->driver_data ) {
  524. CPC_TTY_DBG("hdlcX-tty: no TTY to hangup\n");
  525. return ;
  526. }
  527. cpc_tty = (st_cpc_tty_area *) tty->driver_data;
  528. if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
  529. CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
  530. return ;
  531. }
  532. if (!serial_drv.refcount && cpc_tty_unreg_flag) {
  533. cpc_tty_unreg_flag = 0;
  534. CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
  535. if ((res=tty_unregister_driver(&serial_drv))) {
  536. CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
  537. cpc_tty->name,res);
  538. }
  539. }
  540. cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
  541. }
  542. /*
  543. * PC300 TTY RX work routine
  544. * This routine treats RX work
  545. * o verify read buffer
  546. * o call the line disc. read
  547. * o free memory
  548. */
  549. static void cpc_tty_rx_work(struct work_struct *work)
  550. {
  551. st_cpc_tty_area *cpc_tty;
  552. unsigned long port;
  553. int i, j;
  554. volatile st_cpc_rx_buf *buf;
  555. char flags=0,flg_rx=1;
  556. struct tty_ldisc *ld;
  557. if (cpc_tty_cnt == 0) return;
  558. for (i=0; (i < 4) && flg_rx ; i++) {
  559. flg_rx = 0;
  560. cpc_tty = container_of(work, st_cpc_tty_area, tty_rx_work);
  561. port = cpc_tty - cpc_tty_area;
  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 = 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(struct work_struct *work)
  752. {
  753. st_cpc_tty_area *cpc_tty =
  754. container_of(work, st_cpc_tty_area, tty_tx_work);
  755. struct tty_struct *tty;
  756. CPC_TTY_DBG("%s: cpc_tty_tx_work init\n",cpc_tty->name);
  757. if ((tty = cpc_tty->tty) == 0) {
  758. CPC_TTY_DBG("%s: the interface is not opened\n",cpc_tty->name);
  759. return;
  760. }
  761. tty_wakeup(tty);
  762. }
  763. /*
  764. * PC300 TTY send to card routine
  765. *
  766. * This routine send data to card.
  767. * o clear descriptors
  768. * o write data to DMA buffers
  769. * o start the transmission
  770. */
  771. static int cpc_tty_send_to_card(pc300dev_t *dev,void* buf, int len)
  772. {
  773. pc300ch_t *chan = (pc300ch_t *)dev->chan;
  774. pc300_t *card = (pc300_t *)chan->card;
  775. int ch = chan->channel;
  776. struct net_device_stats *stats = hdlc_stats(dev->dev);
  777. unsigned long flags;
  778. volatile pcsca_bd_t __iomem *ptdescr;
  779. int i, nchar;
  780. int tosend = len;
  781. int nbuf = ((len - 1)/BD_DEF_LEN) + 1;
  782. unsigned char *pdata=buf;
  783. CPC_TTY_DBG("%s:cpc_tty_send_to_cars len=%i",
  784. (st_cpc_tty_area *)dev->cpc_tty->name,len);
  785. if (nbuf >= card->chan[ch].nfree_tx_bd) {
  786. return 1;
  787. }
  788. /* write buffer to DMA buffers */
  789. CPC_TTY_DBG("%s: call dma_buf_write\n",
  790. (st_cpc_tty_area *)dev->cpc_tty->name);
  791. for (i = 0 ; i < nbuf ; i++) {
  792. ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase +
  793. TX_BD_ADDR(ch, card->chan[ch].tx_next_bd));
  794. nchar = (BD_DEF_LEN > tosend) ? tosend : BD_DEF_LEN;
  795. if (cpc_readb(&ptdescr->status) & DST_OSB) {
  796. memcpy_toio((void __iomem *)(card->hw.rambase +
  797. cpc_readl(&ptdescr->ptbuf)),
  798. &pdata[len - tosend],
  799. nchar);
  800. card->chan[ch].nfree_tx_bd--;
  801. if ((i + 1) == nbuf) {
  802. /* This must be the last BD to be used */
  803. cpc_writeb(&ptdescr->status, DST_EOM);
  804. } else {
  805. cpc_writeb(&ptdescr->status, 0);
  806. }
  807. cpc_writew(&ptdescr->len, nchar);
  808. } else {
  809. CPC_TTY_DBG("%s: error in dma_buf_write\n",
  810. (st_cpc_tty_area *)dev->cpc_tty->name);
  811. stats->tx_dropped++;
  812. return 1;
  813. }
  814. tosend -= nchar;
  815. card->chan[ch].tx_next_bd =
  816. (card->chan[ch].tx_next_bd + 1) & (N_DMA_TX_BUF - 1);
  817. }
  818. if (dev->trace_on) {
  819. cpc_tty_trace(dev, buf, len,'T');
  820. }
  821. /* start transmission */
  822. CPC_TTY_DBG("%s: start transmission\n",
  823. (st_cpc_tty_area *)dev->cpc_tty->name);
  824. CPC_TTY_LOCK(card, flags);
  825. cpc_writeb(card->hw.scabase + DTX_REG(EDAL, ch),
  826. TX_BD_ADDR(ch, chan->tx_next_bd));
  827. cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_ENA);
  828. cpc_writeb(card->hw.scabase + DSR_TX(ch), DSR_DE);
  829. if (card->hw.type == PC300_TE) {
  830. cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
  831. cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) |
  832. (CPLD_REG2_FALC_LED1 << (2 * ch)));
  833. }
  834. CPC_TTY_UNLOCK(card, flags);
  835. return 0;
  836. }
  837. /*
  838. * PC300 TTY trace routine
  839. *
  840. * This routine send trace of connection to application.
  841. * o clear descriptors
  842. * o write data to DMA buffers
  843. * o start the transmission
  844. */
  845. static void cpc_tty_trace(pc300dev_t *dev, char* buf, int len, char rxtx)
  846. {
  847. struct sk_buff *skb;
  848. if ((skb = dev_alloc_skb(10 + len)) == NULL) {
  849. /* out of memory */
  850. CPC_TTY_DBG("%s: tty_trace - out of memory\n", dev->dev->name);
  851. return;
  852. }
  853. skb_put (skb, 10 + len);
  854. skb->dev = dev->dev;
  855. skb->protocol = htons(ETH_P_CUST);
  856. skb_reset_mac_header(skb);
  857. skb->pkt_type = PACKET_HOST;
  858. skb->len = 10 + len;
  859. skb_copy_to_linear_data(skb, dev->dev->name, 5);
  860. skb->data[5] = '[';
  861. skb->data[6] = rxtx;
  862. skb->data[7] = ']';
  863. skb->data[8] = ':';
  864. skb->data[9] = ' ';
  865. skb_copy_to_linear_data_offset(skb, 10, buf, len);
  866. netif_rx(skb);
  867. }
  868. /*
  869. * PC300 TTY unregister service routine
  870. *
  871. * This routine unregister one interface.
  872. */
  873. void cpc_tty_unregister_service(pc300dev_t *pc300dev)
  874. {
  875. st_cpc_tty_area *cpc_tty;
  876. ulong flags;
  877. int res;
  878. if ((cpc_tty= (st_cpc_tty_area *) pc300dev->cpc_tty) == 0) {
  879. CPC_TTY_DBG("%s: interface is not TTY\n", pc300dev->dev->name);
  880. return;
  881. }
  882. CPC_TTY_DBG("%s: cpc_tty_unregister_service", cpc_tty->name);
  883. if (cpc_tty->pc300dev != pc300dev) {
  884. CPC_TTY_DBG("%s: invalid tty ptr=%s\n",
  885. pc300dev->dev->name, cpc_tty->name);
  886. return;
  887. }
  888. if (--cpc_tty_cnt == 0) {
  889. if (serial_drv.refcount) {
  890. CPC_TTY_DBG("%s: unregister is not possible, refcount=%d",
  891. cpc_tty->name, serial_drv.refcount);
  892. cpc_tty_cnt++;
  893. cpc_tty_unreg_flag = 1;
  894. return;
  895. } else {
  896. CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
  897. if ((res=tty_unregister_driver(&serial_drv))) {
  898. CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
  899. cpc_tty->name,res);
  900. }
  901. }
  902. }
  903. CPC_TTY_LOCK(pc300dev->chan->card,flags);
  904. cpc_tty->tty = NULL;
  905. CPC_TTY_UNLOCK(pc300dev->chan->card, flags);
  906. cpc_tty->tty_minor = 0;
  907. cpc_tty->state = CPC_TTY_ST_IDLE;
  908. }
  909. /*
  910. * PC300 TTY trigger poll routine
  911. * This routine is called by pc300driver to treats Tx interrupt.
  912. */
  913. void cpc_tty_trigger_poll(pc300dev_t *pc300dev)
  914. {
  915. st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty;
  916. if (!cpc_tty) {
  917. return;
  918. }
  919. schedule_work(&(cpc_tty->tty_tx_work));
  920. }
  921. /*
  922. * PC300 TTY reset var routine
  923. * This routine is called by pc300driver to init the TTY area.
  924. */
  925. void cpc_tty_reset_var(void)
  926. {
  927. int i ;
  928. CPC_TTY_DBG("hdlcX-tty: reset variables\n");
  929. /* reset the tty_driver structure - serial_drv */
  930. memset(&serial_drv, 0, sizeof(struct tty_driver));
  931. for (i=0; i < CPC_TTY_NPORTS; i++){
  932. memset(&cpc_tty_area[i],0, sizeof(st_cpc_tty_area));
  933. }
  934. }