n_r3964.c 31 KB

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