n_r3964.c 31 KB

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