n_r3964.c 31 KB

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