n_r3964.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /* r3964 linediscipline for linux
  2. *
  3. * -----------------------------------------------------------
  4. * Copyright by
  5. * Philips Automation Projects
  6. * Kassel (Germany)
  7. * http://www.pap-philips.de
  8. * -----------------------------------------------------------
  9. * This software may be used and distributed according to the terms of
  10. * the GNU General Public License, incorporated herein by reference.
  11. *
  12. * Author:
  13. * L. Haag
  14. *
  15. * $Log: n_r3964.c,v $
  16. * Revision 1.10 2001/03/18 13:02:24 dwmw2
  17. * Fix timer usage, use spinlocks properly.
  18. *
  19. * Revision 1.9 2001/03/18 12:52:14 dwmw2
  20. * Merge changes in 2.4.2
  21. *
  22. * Revision 1.8 2000/03/23 14:14:54 dwmw2
  23. * Fix race in sleeping in r3964_read()
  24. *
  25. * Revision 1.7 1999/28/08 11:41:50 dwmw2
  26. * Port to 2.3 kernel
  27. *
  28. * Revision 1.6 1998/09/30 00:40:40 dwmw2
  29. * Fixed compilation on 2.0.x kernels
  30. * Updated to newly registered tty-ldisc number 9
  31. *
  32. * Revision 1.5 1998/09/04 21:57:36 dwmw2
  33. * Signal handling bug fixes, port to 2.1.x.
  34. *
  35. * Revision 1.4 1998/04/02 20:26:59 lhaag
  36. * select, blocking, ...
  37. *
  38. * Revision 1.3 1998/02/12 18:58:43 root
  39. * fixed some memory leaks
  40. * calculation of checksum characters
  41. *
  42. * Revision 1.2 1998/02/07 13:03:34 root
  43. * ioctl read_telegram
  44. *
  45. * Revision 1.1 1998/02/06 19:21:03 root
  46. * Initial revision
  47. *
  48. *
  49. */
  50. #include <linux/module.h>
  51. #include <linux/kernel.h>
  52. #include <linux/sched.h>
  53. #include <linux/types.h>
  54. #include <linux/fcntl.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/ptrace.h>
  57. #include <linux/ioport.h>
  58. #include <linux/in.h>
  59. #include <linux/slab.h>
  60. #include <linux/tty.h>
  61. #include <linux/errno.h>
  62. #include <linux/string.h> /* used in new tty drivers */
  63. #include <linux/signal.h> /* used in new tty drivers */
  64. #include <linux/ioctl.h>
  65. #include <linux/n_r3964.h>
  66. #include <linux/poll.h>
  67. #include <linux/init.h>
  68. #include <asm/uaccess.h>
  69. //#define DEBUG_QUEUE
  70. /* Log successful handshake and protocol operations */
  71. //#define DEBUG_PROTO_S
  72. /* Log handshake and protocol errors: */
  73. //#define DEBUG_PROTO_E
  74. /* Log Linediscipline operations (open, close, read, write...): */
  75. //#define DEBUG_LDISC
  76. /* Log module and memory operations (init, cleanup; kmalloc, kfree): */
  77. //#define DEBUG_MODUL
  78. /* Macro helpers for debug output: */
  79. #define TRACE(format, args...) printk("r3964: " format "\n" , ## args);
  80. #ifdef DEBUG_MODUL
  81. #define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args);
  82. #else
  83. #define TRACE_M(fmt, arg...) /**/
  84. #endif
  85. #ifdef DEBUG_PROTO_S
  86. #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args);
  87. #else
  88. #define TRACE_PS(fmt, arg...) /**/
  89. #endif
  90. #ifdef DEBUG_PROTO_E
  91. #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args);
  92. #else
  93. #define TRACE_PE(fmt, arg...) /**/
  94. #endif
  95. #ifdef DEBUG_LDISC
  96. #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args);
  97. #else
  98. #define TRACE_L(fmt, arg...) /**/
  99. #endif
  100. #ifdef DEBUG_QUEUE
  101. #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args);
  102. #else
  103. #define TRACE_Q(fmt, arg...) /**/
  104. #endif
  105. static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
  106. static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
  107. static void put_char(struct r3964_info *pInfo, unsigned char ch);
  108. static void trigger_transmit(struct r3964_info *pInfo);
  109. static void retry_transmit(struct r3964_info *pInfo);
  110. static void transmit_block(struct r3964_info *pInfo);
  111. static void receive_char(struct r3964_info *pInfo, const unsigned char c);
  112. static void receive_error(struct r3964_info *pInfo, const char flag);
  113. static void on_timeout(unsigned long priv);
  114. static int enable_signals(struct r3964_info *pInfo, pid_t pid, int arg);
  115. static int read_telegram(struct r3964_info *pInfo, pid_t pid, unsigned char __user *buf);
  116. static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
  117. int error_code, struct r3964_block_header *pBlock);
  118. static struct r3964_message* remove_msg(struct r3964_info *pInfo,
  119. struct r3964_client_info *pClient);
  120. static void remove_client_block(struct r3964_info *pInfo,
  121. struct r3964_client_info *pClient);
  122. static int r3964_open(struct tty_struct *tty);
  123. static void r3964_close(struct tty_struct *tty);
  124. static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
  125. unsigned char __user *buf, size_t nr);
  126. static ssize_t r3964_write(struct tty_struct * tty, struct file * file,
  127. const unsigned char * buf, size_t nr);
  128. static int r3964_ioctl(struct tty_struct * tty, struct file * file,
  129. unsigned int cmd, unsigned long arg);
  130. static void r3964_set_termios(struct tty_struct *tty, struct termios * old);
  131. static unsigned int r3964_poll(struct tty_struct * tty, struct file * file,
  132. struct poll_table_struct *wait);
  133. static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  134. char *fp, int count);
  135. static int r3964_receive_room(struct tty_struct *tty);
  136. static struct tty_ldisc tty_ldisc_N_R3964 = {
  137. .owner = THIS_MODULE,
  138. .magic = TTY_LDISC_MAGIC,
  139. .name = "R3964",
  140. .open = r3964_open,
  141. .close = r3964_close,
  142. .read = r3964_read,
  143. .write = r3964_write,
  144. .ioctl = r3964_ioctl,
  145. .set_termios = r3964_set_termios,
  146. .poll = r3964_poll,
  147. .receive_buf = r3964_receive_buf,
  148. .receive_room = r3964_receive_room,
  149. };
  150. static void dump_block(const unsigned char *block, unsigned int length)
  151. {
  152. unsigned int i,j;
  153. char linebuf[16*3+1];
  154. for(i=0;i<length;i+=16)
  155. {
  156. for(j=0;(j<16) && (j+i<length);j++)
  157. {
  158. sprintf(linebuf+3*j,"%02x ",block[i+j]);
  159. }
  160. linebuf[3*j]='\0';
  161. TRACE_PS("%s",linebuf);
  162. }
  163. }
  164. /*************************************************************
  165. * Driver initialisation
  166. *************************************************************/
  167. /*************************************************************
  168. * Module support routines
  169. *************************************************************/
  170. static void __exit r3964_exit(void)
  171. {
  172. int status;
  173. TRACE_M ("cleanup_module()");
  174. status=tty_unregister_ldisc(N_R3964);
  175. if(status!=0)
  176. {
  177. printk(KERN_ERR "r3964: error unregistering linediscipline: %d\n", status);
  178. }
  179. else
  180. {
  181. TRACE_L("linediscipline successfully unregistered");
  182. }
  183. }
  184. static int __init r3964_init(void)
  185. {
  186. int status;
  187. printk ("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
  188. /*
  189. * Register the tty line discipline
  190. */
  191. status = tty_register_ldisc (N_R3964, &tty_ldisc_N_R3964);
  192. if (status == 0)
  193. {
  194. TRACE_L("line discipline %d registered", N_R3964);
  195. TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
  196. tty_ldisc_N_R3964.num);
  197. TRACE_L("open=%x", (int)tty_ldisc_N_R3964.open);
  198. TRACE_L("tty_ldisc_N_R3964 = %x", (int)&tty_ldisc_N_R3964);
  199. }
  200. else
  201. {
  202. printk (KERN_ERR "r3964: error registering line discipline: %d\n", status);
  203. }
  204. return status;
  205. }
  206. module_init(r3964_init);
  207. module_exit(r3964_exit);
  208. /*************************************************************
  209. * Protocol implementation routines
  210. *************************************************************/
  211. static void add_tx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader)
  212. {
  213. unsigned long flags;
  214. spin_lock_irqsave(&pInfo->lock, flags);
  215. pHeader->next = NULL;
  216. if(pInfo->tx_last == NULL)
  217. {
  218. pInfo->tx_first = pInfo->tx_last = pHeader;
  219. }
  220. else
  221. {
  222. pInfo->tx_last->next = pHeader;
  223. pInfo->tx_last = pHeader;
  224. }
  225. spin_unlock_irqrestore(&pInfo->lock, flags);
  226. TRACE_Q("add_tx_queue %x, length %d, tx_first = %x",
  227. (int)pHeader, pHeader->length, (int)pInfo->tx_first );
  228. }
  229. static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
  230. {
  231. struct r3964_block_header *pHeader;
  232. unsigned long flags;
  233. #ifdef DEBUG_QUEUE
  234. struct r3964_block_header *pDump;
  235. #endif
  236. pHeader = pInfo->tx_first;
  237. if(pHeader==NULL)
  238. return;
  239. #ifdef DEBUG_QUEUE
  240. printk("r3964: remove_from_tx_queue: %x, length %d - ",
  241. (int)pHeader, (int)pHeader->length );
  242. for(pDump=pHeader;pDump;pDump=pDump->next)
  243. printk("%x ", (int)pDump);
  244. printk("\n");
  245. #endif
  246. if(pHeader->owner)
  247. {
  248. if(error_code)
  249. {
  250. add_msg(pHeader->owner, R3964_MSG_ACK, 0,
  251. error_code, NULL);
  252. }
  253. else
  254. {
  255. add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
  256. error_code, NULL);
  257. }
  258. wake_up_interruptible (&pInfo->read_wait);
  259. }
  260. spin_lock_irqsave(&pInfo->lock, flags);
  261. pInfo->tx_first = pHeader->next;
  262. if(pInfo->tx_first==NULL)
  263. {
  264. pInfo->tx_last = NULL;
  265. }
  266. spin_unlock_irqrestore(&pInfo->lock, flags);
  267. kfree(pHeader);
  268. TRACE_M("remove_from_tx_queue - kfree %x",(int)pHeader);
  269. TRACE_Q("remove_from_tx_queue: tx_first = %x, tx_last = %x",
  270. (int)pInfo->tx_first, (int)pInfo->tx_last );
  271. }
  272. static void add_rx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader)
  273. {
  274. unsigned long flags;
  275. spin_lock_irqsave(&pInfo->lock, flags);
  276. pHeader->next = NULL;
  277. if(pInfo->rx_last == NULL)
  278. {
  279. pInfo->rx_first = pInfo->rx_last = pHeader;
  280. }
  281. else
  282. {
  283. pInfo->rx_last->next = pHeader;
  284. pInfo->rx_last = pHeader;
  285. }
  286. pInfo->blocks_in_rx_queue++;
  287. spin_unlock_irqrestore(&pInfo->lock, flags);
  288. TRACE_Q("add_rx_queue: %x, length = %d, rx_first = %x, count = %d",
  289. (int)pHeader, pHeader->length,
  290. (int)pInfo->rx_first, pInfo->blocks_in_rx_queue);
  291. }
  292. static void remove_from_rx_queue(struct r3964_info *pInfo,
  293. struct r3964_block_header *pHeader)
  294. {
  295. unsigned long flags;
  296. struct r3964_block_header *pFind;
  297. if(pHeader==NULL)
  298. return;
  299. TRACE_Q("remove_from_rx_queue: rx_first = %x, rx_last = %x, count = %d",
  300. (int)pInfo->rx_first, (int)pInfo->rx_last, pInfo->blocks_in_rx_queue );
  301. TRACE_Q("remove_from_rx_queue: %x, length %d",
  302. (int)pHeader, (int)pHeader->length );
  303. spin_lock_irqsave(&pInfo->lock, flags);
  304. if(pInfo->rx_first == pHeader)
  305. {
  306. /* Remove the first block in the linked list: */
  307. pInfo->rx_first = pHeader->next;
  308. if(pInfo->rx_first==NULL)
  309. {
  310. pInfo->rx_last = NULL;
  311. }
  312. pInfo->blocks_in_rx_queue--;
  313. }
  314. else
  315. {
  316. /* Find block to remove: */
  317. for(pFind=pInfo->rx_first; pFind; pFind=pFind->next)
  318. {
  319. if(pFind->next == pHeader)
  320. {
  321. /* Got it. */
  322. pFind->next = pHeader->next;
  323. pInfo->blocks_in_rx_queue--;
  324. if(pFind->next==NULL)
  325. {
  326. /* Oh, removed the last one! */
  327. pInfo->rx_last = pFind;
  328. }
  329. break;
  330. }
  331. }
  332. }
  333. spin_unlock_irqrestore(&pInfo->lock, flags);
  334. kfree(pHeader);
  335. TRACE_M("remove_from_rx_queue - kfree %x",(int)pHeader);
  336. TRACE_Q("remove_from_rx_queue: rx_first = %x, rx_last = %x, count = %d",
  337. (int)pInfo->rx_first, (int)pInfo->rx_last, pInfo->blocks_in_rx_queue );
  338. }
  339. static void put_char(struct r3964_info *pInfo, unsigned char ch)
  340. {
  341. struct tty_struct *tty = pInfo->tty;
  342. if(tty==NULL)
  343. return;
  344. if(tty->driver->put_char)
  345. {
  346. tty->driver->put_char(tty, ch);
  347. }
  348. pInfo->bcc ^= ch;
  349. }
  350. static void flush(struct r3964_info *pInfo)
  351. {
  352. struct tty_struct *tty = pInfo->tty;
  353. if(tty==NULL)
  354. return;
  355. if(tty->driver->flush_chars)
  356. {
  357. tty->driver->flush_chars(tty);
  358. }
  359. }
  360. static void trigger_transmit(struct r3964_info *pInfo)
  361. {
  362. unsigned long flags;
  363. spin_lock_irqsave(&pInfo->lock, flags);
  364. if((pInfo->state == R3964_IDLE) && (pInfo->tx_first!=NULL))
  365. {
  366. pInfo->state = R3964_TX_REQUEST;
  367. pInfo->nRetry=0;
  368. pInfo->flags &= ~R3964_ERROR;
  369. mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
  370. spin_unlock_irqrestore(&pInfo->lock, flags);
  371. TRACE_PS("trigger_transmit - sent STX");
  372. put_char(pInfo, STX);
  373. flush(pInfo);
  374. pInfo->bcc = 0;
  375. }
  376. else
  377. {
  378. spin_unlock_irqrestore(&pInfo->lock, flags);
  379. }
  380. }
  381. static void retry_transmit(struct r3964_info *pInfo)
  382. {
  383. if(pInfo->nRetry<R3964_MAX_RETRIES)
  384. {
  385. TRACE_PE("transmission failed. Retry #%d",
  386. pInfo->nRetry);
  387. pInfo->bcc = 0;
  388. put_char(pInfo, STX);
  389. flush(pInfo);
  390. pInfo->state = R3964_TX_REQUEST;
  391. pInfo->nRetry++;
  392. mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
  393. }
  394. else
  395. {
  396. TRACE_PE("transmission failed after %d retries",
  397. R3964_MAX_RETRIES);
  398. remove_from_tx_queue(pInfo, R3964_TX_FAIL);
  399. put_char(pInfo, NAK);
  400. flush(pInfo);
  401. pInfo->state = R3964_IDLE;
  402. trigger_transmit(pInfo);
  403. }
  404. }
  405. static void transmit_block(struct r3964_info *pInfo)
  406. {
  407. struct tty_struct *tty = pInfo->tty;
  408. struct r3964_block_header *pBlock = pInfo->tx_first;
  409. int room=0;
  410. if((tty==NULL) || (pBlock==NULL))
  411. {
  412. return;
  413. }
  414. if(tty->driver->write_room)
  415. room=tty->driver->write_room(tty);
  416. TRACE_PS("transmit_block %x, room %d, length %d",
  417. (int)pBlock, room, pBlock->length);
  418. while(pInfo->tx_position < pBlock->length)
  419. {
  420. if(room<2)
  421. break;
  422. if(pBlock->data[pInfo->tx_position]==DLE)
  423. {
  424. /* send additional DLE char: */
  425. put_char(pInfo, DLE);
  426. }
  427. put_char(pInfo, pBlock->data[pInfo->tx_position++]);
  428. room--;
  429. }
  430. if((pInfo->tx_position == pBlock->length) && (room>=3))
  431. {
  432. put_char(pInfo, DLE);
  433. put_char(pInfo, ETX);
  434. if(pInfo->flags & R3964_BCC)
  435. {
  436. put_char(pInfo, pInfo->bcc);
  437. }
  438. pInfo->state = R3964_WAIT_FOR_TX_ACK;
  439. mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
  440. }
  441. flush(pInfo);
  442. }
  443. static void on_receive_block(struct r3964_info *pInfo)
  444. {
  445. unsigned int length;
  446. struct r3964_client_info *pClient;
  447. struct r3964_block_header *pBlock;
  448. length=pInfo->rx_position;
  449. /* compare byte checksum characters: */
  450. if(pInfo->flags & R3964_BCC)
  451. {
  452. if(pInfo->bcc!=pInfo->last_rx)
  453. {
  454. TRACE_PE("checksum error - got %x but expected %x",
  455. pInfo->last_rx, pInfo->bcc);
  456. pInfo->flags |= R3964_CHECKSUM;
  457. }
  458. }
  459. /* check for errors (parity, overrun,...): */
  460. if(pInfo->flags & R3964_ERROR)
  461. {
  462. TRACE_PE("on_receive_block - transmission failed error %x",
  463. pInfo->flags & R3964_ERROR);
  464. put_char(pInfo, NAK);
  465. flush(pInfo);
  466. if(pInfo->nRetry<R3964_MAX_RETRIES)
  467. {
  468. pInfo->state=R3964_WAIT_FOR_RX_REPEAT;
  469. pInfo->nRetry++;
  470. mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
  471. }
  472. else
  473. {
  474. TRACE_PE("on_receive_block - failed after max retries");
  475. pInfo->state=R3964_IDLE;
  476. }
  477. return;
  478. }
  479. /* received block; submit DLE: */
  480. put_char(pInfo, DLE);
  481. flush(pInfo);
  482. del_timer_sync(&pInfo->tmr);
  483. TRACE_PS(" rx success: got %d chars", length);
  484. /* prepare struct r3964_block_header: */
  485. pBlock = kmalloc(length+sizeof(struct r3964_block_header), GFP_KERNEL);
  486. TRACE_M("on_receive_block - kmalloc %x",(int)pBlock);
  487. if(pBlock==NULL)
  488. return;
  489. pBlock->length = length;
  490. pBlock->data = ((unsigned char*)pBlock)+sizeof(struct r3964_block_header);
  491. pBlock->locks = 0;
  492. pBlock->next = NULL;
  493. pBlock->owner = NULL;
  494. memcpy(pBlock->data, pInfo->rx_buf, length);
  495. /* queue block into rx_queue: */
  496. add_rx_queue(pInfo, pBlock);
  497. /* notify attached client processes: */
  498. for(pClient=pInfo->firstClient; pClient; pClient=pClient->next)
  499. {
  500. if(pClient->sig_flags & R3964_SIG_DATA)
  501. {
  502. add_msg(pClient, R3964_MSG_DATA, length, R3964_OK, pBlock);
  503. }
  504. }
  505. wake_up_interruptible (&pInfo->read_wait);
  506. pInfo->state = R3964_IDLE;
  507. trigger_transmit(pInfo);
  508. }
  509. static void receive_char(struct r3964_info *pInfo, const unsigned char c)
  510. {
  511. switch(pInfo->state)
  512. {
  513. case R3964_TX_REQUEST:
  514. if(c==DLE)
  515. {
  516. TRACE_PS("TX_REQUEST - got DLE");
  517. pInfo->state = R3964_TRANSMITTING;
  518. pInfo->tx_position = 0;
  519. transmit_block(pInfo);
  520. }
  521. else if(c==STX)
  522. {
  523. if(pInfo->nRetry==0)
  524. {
  525. TRACE_PE("TX_REQUEST - init conflict");
  526. if(pInfo->priority == R3964_SLAVE)
  527. {
  528. goto start_receiving;
  529. }
  530. }
  531. else
  532. {
  533. TRACE_PE("TX_REQUEST - secondary init conflict!?"
  534. " Switching to SLAVE mode for next rx.");
  535. goto start_receiving;
  536. }
  537. }
  538. else
  539. {
  540. TRACE_PE("TX_REQUEST - char != DLE: %x", c);
  541. retry_transmit(pInfo);
  542. }
  543. break;
  544. case R3964_TRANSMITTING:
  545. if(c==NAK)
  546. {
  547. TRACE_PE("TRANSMITTING - got NAK");
  548. retry_transmit(pInfo);
  549. }
  550. else
  551. {
  552. TRACE_PE("TRANSMITTING - got invalid char");
  553. pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
  554. mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
  555. }
  556. break;
  557. case R3964_WAIT_FOR_TX_ACK:
  558. if(c==DLE)
  559. {
  560. TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
  561. remove_from_tx_queue(pInfo, R3964_OK);
  562. pInfo->state = R3964_IDLE;
  563. trigger_transmit(pInfo);
  564. }
  565. else
  566. {
  567. retry_transmit(pInfo);
  568. }
  569. break;
  570. case R3964_WAIT_FOR_RX_REPEAT:
  571. /* FALLTROUGH */
  572. case R3964_IDLE:
  573. if(c==STX)
  574. {
  575. /* Prevent rx_queue from overflow: */
  576. if(pInfo->blocks_in_rx_queue >= R3964_MAX_BLOCKS_IN_RX_QUEUE)
  577. {
  578. TRACE_PE("IDLE - got STX but no space in rx_queue!");
  579. pInfo->state=R3964_WAIT_FOR_RX_BUF;
  580. mod_timer(&pInfo->tmr, R3964_TO_NO_BUF);
  581. break;
  582. }
  583. start_receiving:
  584. /* Ok, start receiving: */
  585. TRACE_PS("IDLE - got STX");
  586. pInfo->rx_position = 0;
  587. pInfo->last_rx = 0;
  588. pInfo->flags &= ~R3964_ERROR;
  589. pInfo->state=R3964_RECEIVING;
  590. mod_timer(&pInfo->tmr, R3964_TO_ZVZ);
  591. pInfo->nRetry = 0;
  592. put_char(pInfo, DLE);
  593. flush(pInfo);
  594. pInfo->bcc = 0;
  595. }
  596. break;
  597. case R3964_RECEIVING:
  598. if(pInfo->rx_position < RX_BUF_SIZE)
  599. {
  600. pInfo->bcc ^= c;
  601. if(c==DLE)
  602. {
  603. if(pInfo->last_rx==DLE)
  604. {
  605. pInfo->last_rx = 0;
  606. goto char_to_buf;
  607. }
  608. pInfo->last_rx = DLE;
  609. break;
  610. }
  611. else if((c==ETX) && (pInfo->last_rx==DLE))
  612. {
  613. if(pInfo->flags & R3964_BCC)
  614. {
  615. pInfo->state = R3964_WAIT_FOR_BCC;
  616. mod_timer(&pInfo->tmr, R3964_TO_ZVZ);
  617. }
  618. else
  619. {
  620. on_receive_block(pInfo);
  621. }
  622. }
  623. else
  624. {
  625. pInfo->last_rx = c;
  626. char_to_buf:
  627. pInfo->rx_buf[pInfo->rx_position++] = c;
  628. mod_timer(&pInfo->tmr, R3964_TO_ZVZ);
  629. }
  630. }
  631. /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
  632. break;
  633. case R3964_WAIT_FOR_BCC:
  634. pInfo->last_rx = c;
  635. on_receive_block(pInfo);
  636. break;
  637. }
  638. }
  639. static void receive_error(struct r3964_info *pInfo, const char flag)
  640. {
  641. switch (flag)
  642. {
  643. case TTY_NORMAL:
  644. break;
  645. case TTY_BREAK:
  646. TRACE_PE("received break")
  647. pInfo->flags |= R3964_BREAK;
  648. break;
  649. case TTY_PARITY:
  650. TRACE_PE("parity error")
  651. pInfo->flags |= R3964_PARITY;
  652. break;
  653. case TTY_FRAME:
  654. TRACE_PE("frame error")
  655. pInfo->flags |= R3964_FRAME;
  656. break;
  657. case TTY_OVERRUN:
  658. TRACE_PE("frame overrun")
  659. pInfo->flags |= R3964_OVERRUN;
  660. break;
  661. default:
  662. TRACE_PE("receive_error - unknown flag %d", flag);
  663. pInfo->flags |= R3964_UNKNOWN;
  664. break;
  665. }
  666. }
  667. static void on_timeout(unsigned long priv)
  668. {
  669. struct r3964_info *pInfo = (void *)priv;
  670. switch(pInfo->state)
  671. {
  672. case R3964_TX_REQUEST:
  673. TRACE_PE("TX_REQUEST - timeout");
  674. retry_transmit(pInfo);
  675. break;
  676. case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
  677. put_char(pInfo, NAK);
  678. flush(pInfo);
  679. retry_transmit(pInfo);
  680. break;
  681. case R3964_WAIT_FOR_TX_ACK:
  682. TRACE_PE("WAIT_FOR_TX_ACK - timeout");
  683. retry_transmit(pInfo);
  684. break;
  685. case R3964_WAIT_FOR_RX_BUF:
  686. TRACE_PE("WAIT_FOR_RX_BUF - timeout");
  687. put_char(pInfo, NAK);
  688. flush(pInfo);
  689. pInfo->state=R3964_IDLE;
  690. break;
  691. case R3964_RECEIVING:
  692. TRACE_PE("RECEIVING - timeout after %d chars",
  693. pInfo->rx_position);
  694. put_char(pInfo, NAK);
  695. flush(pInfo);
  696. pInfo->state=R3964_IDLE;
  697. break;
  698. case R3964_WAIT_FOR_RX_REPEAT:
  699. TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
  700. pInfo->state=R3964_IDLE;
  701. break;
  702. case R3964_WAIT_FOR_BCC:
  703. TRACE_PE("WAIT_FOR_BCC - timeout");
  704. put_char(pInfo, NAK);
  705. flush(pInfo);
  706. pInfo->state=R3964_IDLE;
  707. break;
  708. }
  709. }
  710. static struct r3964_client_info *findClient(
  711. struct r3964_info *pInfo, pid_t pid)
  712. {
  713. struct r3964_client_info *pClient;
  714. for(pClient=pInfo->firstClient; pClient; pClient=pClient->next)
  715. {
  716. if(pClient->pid == pid)
  717. {
  718. return pClient;
  719. }
  720. }
  721. return NULL;
  722. }
  723. static int enable_signals(struct r3964_info *pInfo, pid_t pid, int arg)
  724. {
  725. struct r3964_client_info *pClient;
  726. struct r3964_client_info **ppClient;
  727. struct r3964_message *pMsg;
  728. if((arg & R3964_SIG_ALL)==0)
  729. {
  730. /* Remove client from client list */
  731. for(ppClient=&pInfo->firstClient; *ppClient; ppClient=&(*ppClient)->next)
  732. {
  733. pClient = *ppClient;
  734. if(pClient->pid == pid)
  735. {
  736. TRACE_PS("removing client %d from client list", pid);
  737. *ppClient = pClient->next;
  738. while(pClient->msg_count)
  739. {
  740. pMsg=remove_msg(pInfo, pClient);
  741. if(pMsg)
  742. {
  743. kfree(pMsg);
  744. TRACE_M("enable_signals - msg kfree %x",(int)pMsg);
  745. }
  746. }
  747. kfree(pClient);
  748. TRACE_M("enable_signals - kfree %x",(int)pClient);
  749. return 0;
  750. }
  751. }
  752. return -EINVAL;
  753. }
  754. else
  755. {
  756. pClient=findClient(pInfo, pid);
  757. if(pClient)
  758. {
  759. /* update signal options */
  760. pClient->sig_flags=arg;
  761. }
  762. else
  763. {
  764. /* add client to client list */
  765. pClient=kmalloc(sizeof(struct r3964_client_info), GFP_KERNEL);
  766. TRACE_M("enable_signals - kmalloc %x",(int)pClient);
  767. if(pClient==NULL)
  768. return -ENOMEM;
  769. TRACE_PS("add client %d to client list", pid);
  770. spin_lock_init(&pClient->lock);
  771. pClient->sig_flags=arg;
  772. pClient->pid = pid;
  773. pClient->next=pInfo->firstClient;
  774. pClient->first_msg = NULL;
  775. pClient->last_msg = NULL;
  776. pClient->next_block_to_read = NULL;
  777. pClient->msg_count = 0;
  778. pInfo->firstClient=pClient;
  779. }
  780. }
  781. return 0;
  782. }
  783. static int read_telegram(struct r3964_info *pInfo, pid_t pid, unsigned char __user *buf)
  784. {
  785. struct r3964_client_info *pClient;
  786. struct r3964_block_header *block;
  787. if(!buf)
  788. {
  789. return -EINVAL;
  790. }
  791. pClient=findClient(pInfo,pid);
  792. if(pClient==NULL)
  793. {
  794. return -EINVAL;
  795. }
  796. block=pClient->next_block_to_read;
  797. if(!block)
  798. {
  799. return 0;
  800. }
  801. else
  802. {
  803. if (copy_to_user (buf, block->data, block->length))
  804. return -EFAULT;
  805. remove_client_block(pInfo, pClient);
  806. return block->length;
  807. }
  808. return -EINVAL;
  809. }
  810. static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
  811. int error_code, struct r3964_block_header *pBlock)
  812. {
  813. struct r3964_message *pMsg;
  814. unsigned long flags;
  815. if(pClient->msg_count<R3964_MAX_MSG_COUNT-1)
  816. {
  817. queue_the_message:
  818. pMsg = kmalloc(sizeof(struct r3964_message), GFP_KERNEL);
  819. TRACE_M("add_msg - kmalloc %x",(int)pMsg);
  820. if(pMsg==NULL) {
  821. return;
  822. }
  823. spin_lock_irqsave(&pClient->lock, flags);
  824. pMsg->msg_id = msg_id;
  825. pMsg->arg = arg;
  826. pMsg->error_code = error_code;
  827. pMsg->block = pBlock;
  828. pMsg->next = NULL;
  829. if(pClient->last_msg==NULL)
  830. {
  831. pClient->first_msg=pClient->last_msg=pMsg;
  832. }
  833. else
  834. {
  835. pClient->last_msg->next = pMsg;
  836. pClient->last_msg=pMsg;
  837. }
  838. pClient->msg_count++;
  839. if(pBlock!=NULL)
  840. {
  841. pBlock->locks++;
  842. }
  843. spin_unlock_irqrestore(&pClient->lock, flags);
  844. }
  845. else
  846. {
  847. if((pClient->last_msg->msg_id == R3964_MSG_ACK)
  848. && (pClient->last_msg->error_code==R3964_OVERFLOW))
  849. {
  850. pClient->last_msg->arg++;
  851. TRACE_PE("add_msg - inc prev OVERFLOW-msg");
  852. }
  853. else
  854. {
  855. msg_id = R3964_MSG_ACK;
  856. arg = 0;
  857. error_code = R3964_OVERFLOW;
  858. pBlock = NULL;
  859. TRACE_PE("add_msg - queue OVERFLOW-msg");
  860. goto queue_the_message;
  861. }
  862. }
  863. /* Send SIGIO signal to client process: */
  864. if(pClient->sig_flags & R3964_USE_SIGIO)
  865. {
  866. kill_proc(pClient->pid, SIGIO, 1);
  867. }
  868. }
  869. static struct r3964_message *remove_msg(struct r3964_info *pInfo,
  870. struct r3964_client_info *pClient)
  871. {
  872. struct r3964_message *pMsg=NULL;
  873. unsigned long flags;
  874. if(pClient->first_msg)
  875. {
  876. spin_lock_irqsave(&pClient->lock, flags);
  877. pMsg = pClient->first_msg;
  878. pClient->first_msg = pMsg->next;
  879. if(pClient->first_msg==NULL)
  880. {
  881. pClient->last_msg = NULL;
  882. }
  883. pClient->msg_count--;
  884. if(pMsg->block)
  885. {
  886. remove_client_block(pInfo, pClient);
  887. pClient->next_block_to_read = pMsg->block;
  888. }
  889. spin_unlock_irqrestore(&pClient->lock, flags);
  890. }
  891. return pMsg;
  892. }
  893. static void remove_client_block(struct r3964_info *pInfo,
  894. struct r3964_client_info *pClient)
  895. {
  896. struct r3964_block_header *block;
  897. TRACE_PS("remove_client_block PID %d", pClient->pid);
  898. block=pClient->next_block_to_read;
  899. if(block)
  900. {
  901. block->locks--;
  902. if(block->locks==0)
  903. {
  904. remove_from_rx_queue(pInfo, block);
  905. }
  906. }
  907. pClient->next_block_to_read = NULL;
  908. }
  909. /*************************************************************
  910. * Line discipline routines
  911. *************************************************************/
  912. static int r3964_open(struct tty_struct *tty)
  913. {
  914. struct r3964_info *pInfo;
  915. TRACE_L("open");
  916. TRACE_L("tty=%x, PID=%d, disc_data=%x",
  917. (int)tty, current->pid, (int)tty->disc_data);
  918. pInfo=kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
  919. TRACE_M("r3964_open - info kmalloc %x",(int)pInfo);
  920. if(!pInfo)
  921. {
  922. printk(KERN_ERR "r3964: failed to alloc info structure\n");
  923. return -ENOMEM;
  924. }
  925. pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
  926. TRACE_M("r3964_open - rx_buf kmalloc %x",(int)pInfo->rx_buf);
  927. if(!pInfo->rx_buf)
  928. {
  929. printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
  930. kfree(pInfo);
  931. TRACE_M("r3964_open - info kfree %x",(int)pInfo);
  932. return -ENOMEM;
  933. }
  934. pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
  935. TRACE_M("r3964_open - tx_buf kmalloc %x",(int)pInfo->tx_buf);
  936. if(!pInfo->tx_buf)
  937. {
  938. printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
  939. kfree(pInfo->rx_buf);
  940. TRACE_M("r3964_open - rx_buf kfree %x",(int)pInfo->rx_buf);
  941. kfree(pInfo);
  942. TRACE_M("r3964_open - info kfree %x",(int)pInfo);
  943. return -ENOMEM;
  944. }
  945. spin_lock_init(&pInfo->lock);
  946. pInfo->tty = tty;
  947. init_waitqueue_head (&pInfo->read_wait);
  948. pInfo->priority = R3964_MASTER;
  949. pInfo->rx_first = pInfo->rx_last = NULL;
  950. pInfo->tx_first = pInfo->tx_last = NULL;
  951. pInfo->rx_position = 0;
  952. pInfo->tx_position = 0;
  953. pInfo->last_rx = 0;
  954. pInfo->blocks_in_rx_queue = 0;
  955. pInfo->firstClient=NULL;
  956. pInfo->state=R3964_IDLE;
  957. pInfo->flags = R3964_DEBUG;
  958. pInfo->nRetry = 0;
  959. tty->disc_data = pInfo;
  960. init_timer(&pInfo->tmr);
  961. pInfo->tmr.data = (unsigned long)pInfo;
  962. pInfo->tmr.function = on_timeout;
  963. return 0;
  964. }
  965. static void r3964_close(struct tty_struct *tty)
  966. {
  967. struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
  968. struct r3964_client_info *pClient, *pNext;
  969. struct r3964_message *pMsg;
  970. struct r3964_block_header *pHeader, *pNextHeader;
  971. unsigned long flags;
  972. TRACE_L("close");
  973. /*
  974. * Make sure that our task queue isn't activated. If it
  975. * is, take it out of the linked list.
  976. */
  977. del_timer_sync(&pInfo->tmr);
  978. /* Remove client-structs and message queues: */
  979. pClient=pInfo->firstClient;
  980. while(pClient)
  981. {
  982. pNext=pClient->next;
  983. while(pClient->msg_count)
  984. {
  985. pMsg=remove_msg(pInfo, pClient);
  986. if(pMsg)
  987. {
  988. kfree(pMsg);
  989. TRACE_M("r3964_close - msg kfree %x",(int)pMsg);
  990. }
  991. }
  992. kfree(pClient);
  993. TRACE_M("r3964_close - client kfree %x",(int)pClient);
  994. pClient=pNext;
  995. }
  996. /* Remove jobs from tx_queue: */
  997. spin_lock_irqsave(&pInfo->lock, flags);
  998. pHeader=pInfo->tx_first;
  999. pInfo->tx_first=pInfo->tx_last=NULL;
  1000. spin_unlock_irqrestore(&pInfo->lock, flags);
  1001. while(pHeader)
  1002. {
  1003. pNextHeader=pHeader->next;
  1004. kfree(pHeader);
  1005. pHeader=pNextHeader;
  1006. }
  1007. /* Free buffers: */
  1008. wake_up_interruptible(&pInfo->read_wait);
  1009. kfree(pInfo->rx_buf);
  1010. TRACE_M("r3964_close - rx_buf kfree %x",(int)pInfo->rx_buf);
  1011. kfree(pInfo->tx_buf);
  1012. TRACE_M("r3964_close - tx_buf kfree %x",(int)pInfo->tx_buf);
  1013. kfree(pInfo);
  1014. TRACE_M("r3964_close - info kfree %x",(int)pInfo);
  1015. }
  1016. static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
  1017. unsigned char __user *buf, size_t nr)
  1018. {
  1019. struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
  1020. struct r3964_client_info *pClient;
  1021. struct r3964_message *pMsg;
  1022. struct r3964_client_message theMsg;
  1023. DECLARE_WAITQUEUE (wait, current);
  1024. int pid = current->pid;
  1025. int count;
  1026. TRACE_L("read()");
  1027. pClient=findClient(pInfo, pid);
  1028. if(pClient)
  1029. {
  1030. pMsg = remove_msg(pInfo, pClient);
  1031. if(pMsg==NULL)
  1032. {
  1033. /* no messages available. */
  1034. if (file->f_flags & O_NONBLOCK)
  1035. {
  1036. return -EAGAIN;
  1037. }
  1038. /* block until there is a message: */
  1039. add_wait_queue(&pInfo->read_wait, &wait);
  1040. repeat:
  1041. current->state = TASK_INTERRUPTIBLE;
  1042. pMsg = remove_msg(pInfo, pClient);
  1043. if (!pMsg && !signal_pending(current))
  1044. {
  1045. schedule();
  1046. goto repeat;
  1047. }
  1048. current->state = TASK_RUNNING;
  1049. remove_wait_queue(&pInfo->read_wait, &wait);
  1050. }
  1051. /* If we still haven't got a message, we must have been signalled */
  1052. if (!pMsg) return -EINTR;
  1053. /* deliver msg to client process: */
  1054. theMsg.msg_id = pMsg->msg_id;
  1055. theMsg.arg = pMsg->arg;
  1056. theMsg.error_code = pMsg->error_code;
  1057. count = sizeof(struct r3964_client_message);
  1058. kfree(pMsg);
  1059. TRACE_M("r3964_read - msg kfree %x",(int)pMsg);
  1060. if (copy_to_user(buf,&theMsg, count))
  1061. return -EFAULT;
  1062. TRACE_PS("read - return %d", count);
  1063. return count;
  1064. }
  1065. return -EPERM;
  1066. }
  1067. static ssize_t r3964_write(struct tty_struct * tty, struct file * file,
  1068. const unsigned char *data, size_t count)
  1069. {
  1070. struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
  1071. struct r3964_block_header *pHeader;
  1072. struct r3964_client_info *pClient;
  1073. unsigned char *new_data;
  1074. int pid;
  1075. TRACE_L("write request, %d characters", count);
  1076. /*
  1077. * Verify the pointers
  1078. */
  1079. if(!pInfo)
  1080. return -EIO;
  1081. /*
  1082. * Ensure that the caller does not wish to send too much.
  1083. */
  1084. if (count > R3964_MTU)
  1085. {
  1086. if (pInfo->flags & R3964_DEBUG)
  1087. {
  1088. TRACE_L (KERN_WARNING
  1089. "r3964_write: truncating user packet "
  1090. "from %u to mtu %d", count, R3964_MTU);
  1091. }
  1092. count = R3964_MTU;
  1093. }
  1094. /*
  1095. * Allocate a buffer for the data and copy it from the buffer with header prepended
  1096. */
  1097. new_data = kmalloc (count+sizeof(struct r3964_block_header), GFP_KERNEL);
  1098. TRACE_M("r3964_write - kmalloc %x",(int)new_data);
  1099. if (new_data == NULL) {
  1100. if (pInfo->flags & R3964_DEBUG)
  1101. {
  1102. printk (KERN_ERR
  1103. "r3964_write: no memory\n");
  1104. }
  1105. return -ENOSPC;
  1106. }
  1107. pHeader = (struct r3964_block_header *)new_data;
  1108. pHeader->data = new_data + sizeof(struct r3964_block_header);
  1109. pHeader->length = count;
  1110. pHeader->locks = 0;
  1111. pHeader->owner = NULL;
  1112. pid=current->pid;
  1113. pClient=findClient(pInfo, pid);
  1114. if(pClient)
  1115. {
  1116. pHeader->owner = pClient;
  1117. }
  1118. memcpy(pHeader->data, data, count); /* We already verified this */
  1119. if(pInfo->flags & R3964_DEBUG)
  1120. {
  1121. dump_block(pHeader->data, count);
  1122. }
  1123. /*
  1124. * Add buffer to transmit-queue:
  1125. */
  1126. add_tx_queue(pInfo, pHeader);
  1127. trigger_transmit(pInfo);
  1128. return 0;
  1129. }
  1130. static int r3964_ioctl(struct tty_struct * tty, struct file * file,
  1131. unsigned int cmd, unsigned long arg)
  1132. {
  1133. struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
  1134. if(pInfo==NULL)
  1135. return -EINVAL;
  1136. switch(cmd)
  1137. {
  1138. case R3964_ENABLE_SIGNALS:
  1139. return enable_signals(pInfo, current->pid, arg);
  1140. case R3964_SETPRIORITY:
  1141. if(arg<R3964_MASTER || arg>R3964_SLAVE)
  1142. return -EINVAL;
  1143. pInfo->priority = arg & 0xff;
  1144. return 0;
  1145. case R3964_USE_BCC:
  1146. if(arg)
  1147. pInfo->flags |= R3964_BCC;
  1148. else
  1149. pInfo->flags &= ~R3964_BCC;
  1150. return 0;
  1151. case R3964_READ_TELEGRAM:
  1152. return read_telegram(pInfo, current->pid, (unsigned char __user *)arg);
  1153. default:
  1154. return -ENOIOCTLCMD;
  1155. }
  1156. }
  1157. static void r3964_set_termios(struct tty_struct *tty, struct termios * old)
  1158. {
  1159. TRACE_L("set_termios");
  1160. }
  1161. /* Called without the kernel lock held - fine */
  1162. static unsigned int r3964_poll(struct tty_struct * tty, struct file * file,
  1163. struct poll_table_struct *wait)
  1164. {
  1165. struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
  1166. int pid=current->pid;
  1167. struct r3964_client_info *pClient;
  1168. struct r3964_message *pMsg=NULL;
  1169. unsigned long flags;
  1170. int result = POLLOUT;
  1171. TRACE_L("POLL");
  1172. pClient=findClient(pInfo,pid);
  1173. if(pClient)
  1174. {
  1175. poll_wait(file, &pInfo->read_wait, wait);
  1176. spin_lock_irqsave(&pInfo->lock, flags);
  1177. pMsg=pClient->first_msg;
  1178. spin_unlock_irqrestore(&pInfo->lock, flags);
  1179. if(pMsg)
  1180. result |= POLLIN | POLLRDNORM;
  1181. }
  1182. else
  1183. {
  1184. result = -EINVAL;
  1185. }
  1186. return result;
  1187. }
  1188. static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1189. char *fp, int count)
  1190. {
  1191. struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
  1192. const unsigned char *p;
  1193. char *f, flags = 0;
  1194. int i;
  1195. for (i=count, p = cp, f = fp; i; i--, p++) {
  1196. if (f)
  1197. flags = *f++;
  1198. if(flags==TTY_NORMAL)
  1199. {
  1200. receive_char(pInfo, *p);
  1201. }
  1202. else
  1203. {
  1204. receive_error(pInfo, flags);
  1205. }
  1206. }
  1207. }
  1208. static int r3964_receive_room(struct tty_struct *tty)
  1209. {
  1210. TRACE_L("receive_room");
  1211. return -1;
  1212. }
  1213. MODULE_LICENSE("GPL");
  1214. MODULE_ALIAS_LDISC(N_R3964);