n_r3964.c 31 KB

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