n_r3964.c 35 KB

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