n_r3964.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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),
  817. error_code?GFP_ATOMIC:GFP_KERNEL);
  818. TRACE_M("add_msg - kmalloc %p",pMsg);
  819. if(pMsg==NULL) {
  820. return;
  821. }
  822. spin_lock_irqsave(&pClient->lock, flags);
  823. pMsg->msg_id = msg_id;
  824. pMsg->arg = arg;
  825. pMsg->error_code = error_code;
  826. pMsg->block = pBlock;
  827. pMsg->next = NULL;
  828. if(pClient->last_msg==NULL)
  829. {
  830. pClient->first_msg=pClient->last_msg=pMsg;
  831. }
  832. else
  833. {
  834. pClient->last_msg->next = pMsg;
  835. pClient->last_msg=pMsg;
  836. }
  837. pClient->msg_count++;
  838. if(pBlock!=NULL)
  839. {
  840. pBlock->locks++;
  841. }
  842. spin_unlock_irqrestore(&pClient->lock, flags);
  843. }
  844. else
  845. {
  846. if((pClient->last_msg->msg_id == R3964_MSG_ACK)
  847. && (pClient->last_msg->error_code==R3964_OVERFLOW))
  848. {
  849. pClient->last_msg->arg++;
  850. TRACE_PE("add_msg - inc prev OVERFLOW-msg");
  851. }
  852. else
  853. {
  854. msg_id = R3964_MSG_ACK;
  855. arg = 0;
  856. error_code = R3964_OVERFLOW;
  857. pBlock = NULL;
  858. TRACE_PE("add_msg - queue OVERFLOW-msg");
  859. goto queue_the_message;
  860. }
  861. }
  862. /* Send SIGIO signal to client process: */
  863. if(pClient->sig_flags & R3964_USE_SIGIO)
  864. {
  865. kill_proc(pClient->pid, SIGIO, 1);
  866. }
  867. }
  868. static struct r3964_message *remove_msg(struct r3964_info *pInfo,
  869. struct r3964_client_info *pClient)
  870. {
  871. struct r3964_message *pMsg=NULL;
  872. unsigned long flags;
  873. if(pClient->first_msg)
  874. {
  875. spin_lock_irqsave(&pClient->lock, flags);
  876. pMsg = pClient->first_msg;
  877. pClient->first_msg = pMsg->next;
  878. if(pClient->first_msg==NULL)
  879. {
  880. pClient->last_msg = NULL;
  881. }
  882. pClient->msg_count--;
  883. if(pMsg->block)
  884. {
  885. remove_client_block(pInfo, pClient);
  886. pClient->next_block_to_read = pMsg->block;
  887. }
  888. spin_unlock_irqrestore(&pClient->lock, flags);
  889. }
  890. return pMsg;
  891. }
  892. static void remove_client_block(struct r3964_info *pInfo,
  893. struct r3964_client_info *pClient)
  894. {
  895. struct r3964_block_header *block;
  896. TRACE_PS("remove_client_block PID %d", pClient->pid);
  897. block=pClient->next_block_to_read;
  898. if(block)
  899. {
  900. block->locks--;
  901. if(block->locks==0)
  902. {
  903. remove_from_rx_queue(pInfo, block);
  904. }
  905. }
  906. pClient->next_block_to_read = NULL;
  907. }
  908. /*************************************************************
  909. * Line discipline routines
  910. *************************************************************/
  911. static int r3964_open(struct tty_struct *tty)
  912. {
  913. struct r3964_info *pInfo;
  914. TRACE_L("open");
  915. TRACE_L("tty=%p, PID=%d, disc_data=%p",
  916. tty, current->pid, tty->disc_data);
  917. pInfo=kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
  918. TRACE_M("r3964_open - info kmalloc %p",pInfo);
  919. if(!pInfo)
  920. {
  921. printk(KERN_ERR "r3964: failed to alloc info structure\n");
  922. return -ENOMEM;
  923. }
  924. pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
  925. TRACE_M("r3964_open - rx_buf kmalloc %p",pInfo->rx_buf);
  926. if(!pInfo->rx_buf)
  927. {
  928. printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
  929. kfree(pInfo);
  930. TRACE_M("r3964_open - info kfree %p",pInfo);
  931. return -ENOMEM;
  932. }
  933. pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
  934. TRACE_M("r3964_open - tx_buf kmalloc %p",pInfo->tx_buf);
  935. if(!pInfo->tx_buf)
  936. {
  937. printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
  938. kfree(pInfo->rx_buf);
  939. TRACE_M("r3964_open - rx_buf kfree %p",pInfo->rx_buf);
  940. kfree(pInfo);
  941. TRACE_M("r3964_open - info kfree %p",pInfo);
  942. return -ENOMEM;
  943. }
  944. spin_lock_init(&pInfo->lock);
  945. pInfo->tty = tty;
  946. init_waitqueue_head (&pInfo->read_wait);
  947. pInfo->priority = R3964_MASTER;
  948. pInfo->rx_first = pInfo->rx_last = NULL;
  949. pInfo->tx_first = pInfo->tx_last = NULL;
  950. pInfo->rx_position = 0;
  951. pInfo->tx_position = 0;
  952. pInfo->last_rx = 0;
  953. pInfo->blocks_in_rx_queue = 0;
  954. pInfo->firstClient=NULL;
  955. pInfo->state=R3964_IDLE;
  956. pInfo->flags = R3964_DEBUG;
  957. pInfo->nRetry = 0;
  958. tty->disc_data = pInfo;
  959. tty->receive_room = 65536;
  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 %p",pMsg);
  990. }
  991. }
  992. kfree(pClient);
  993. TRACE_M("r3964_close - client kfree %p",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 %p",pInfo->rx_buf);
  1011. kfree(pInfo->tx_buf);
  1012. TRACE_M("r3964_close - tx_buf kfree %p",pInfo->tx_buf);
  1013. kfree(pInfo);
  1014. TRACE_M("r3964_close - info kfree %p",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 %p",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 %p",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. MODULE_LICENSE("GPL");
  1209. MODULE_ALIAS_LDISC(N_R3964);