n_r3964.c 31 KB

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