pc300_tty.c 29 KB

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