n_r3964.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. DECLARE_WAITQUEUE(wait, current);
  924. int count;
  925. TRACE_L("read()");
  926. pClient = findClient(pInfo, task_pid(current));
  927. if (pClient) {
  928. pMsg = remove_msg(pInfo, pClient);
  929. if (pMsg == NULL) {
  930. /* no messages available. */
  931. if (file->f_flags & O_NONBLOCK) {
  932. return -EAGAIN;
  933. }
  934. /* block until there is a message: */
  935. add_wait_queue(&pInfo->read_wait, &wait);
  936. repeat:
  937. __set_current_state(TASK_INTERRUPTIBLE);
  938. pMsg = remove_msg(pInfo, pClient);
  939. if (!pMsg && !signal_pending(current)) {
  940. schedule();
  941. goto repeat;
  942. }
  943. __set_current_state(TASK_RUNNING);
  944. remove_wait_queue(&pInfo->read_wait, &wait);
  945. }
  946. /* If we still haven't got a message, we must have been signalled */
  947. if (!pMsg)
  948. return -EINTR;
  949. /* deliver msg to client process: */
  950. theMsg.msg_id = pMsg->msg_id;
  951. theMsg.arg = pMsg->arg;
  952. theMsg.error_code = pMsg->error_code;
  953. count = sizeof(struct r3964_client_message);
  954. kfree(pMsg);
  955. TRACE_M("r3964_read - msg kfree %p", pMsg);
  956. if (copy_to_user(buf, &theMsg, count))
  957. return -EFAULT;
  958. TRACE_PS("read - return %d", count);
  959. return count;
  960. }
  961. return -EPERM;
  962. }
  963. static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
  964. const unsigned char *data, size_t count)
  965. {
  966. struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
  967. struct r3964_block_header *pHeader;
  968. struct r3964_client_info *pClient;
  969. unsigned char *new_data;
  970. TRACE_L("write request, %d characters", count);
  971. /*
  972. * Verify the pointers
  973. */
  974. if (!pInfo)
  975. return -EIO;
  976. /*
  977. * Ensure that the caller does not wish to send too much.
  978. */
  979. if (count > R3964_MTU) {
  980. if (pInfo->flags & R3964_DEBUG) {
  981. TRACE_L(KERN_WARNING "r3964_write: truncating user "
  982. "packet from %u to mtu %d", count, R3964_MTU);
  983. }
  984. count = R3964_MTU;
  985. }
  986. /*
  987. * Allocate a buffer for the data and copy it from the buffer with header prepended
  988. */
  989. new_data = kmalloc(count + sizeof(struct r3964_block_header),
  990. GFP_KERNEL);
  991. TRACE_M("r3964_write - kmalloc %p", new_data);
  992. if (new_data == NULL) {
  993. if (pInfo->flags & R3964_DEBUG) {
  994. printk(KERN_ERR "r3964_write: no memory\n");
  995. }
  996. return -ENOSPC;
  997. }
  998. pHeader = (struct r3964_block_header *)new_data;
  999. pHeader->data = new_data + sizeof(struct r3964_block_header);
  1000. pHeader->length = count;
  1001. pHeader->locks = 0;
  1002. pHeader->owner = NULL;
  1003. pClient = findClient(pInfo, task_pid(current));
  1004. if (pClient) {
  1005. pHeader->owner = pClient;
  1006. }
  1007. memcpy(pHeader->data, data, count); /* We already verified this */
  1008. if (pInfo->flags & R3964_DEBUG) {
  1009. dump_block(pHeader->data, count);
  1010. }
  1011. /*
  1012. * Add buffer to transmit-queue:
  1013. */
  1014. add_tx_queue(pInfo, pHeader);
  1015. trigger_transmit(pInfo);
  1016. return 0;
  1017. }
  1018. static int r3964_ioctl(struct tty_struct *tty, struct file *file,
  1019. unsigned int cmd, unsigned long arg)
  1020. {
  1021. struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
  1022. if (pInfo == NULL)
  1023. return -EINVAL;
  1024. switch (cmd) {
  1025. case R3964_ENABLE_SIGNALS:
  1026. return enable_signals(pInfo, task_pid(current), arg);
  1027. case R3964_SETPRIORITY:
  1028. if (arg < R3964_MASTER || arg > R3964_SLAVE)
  1029. return -EINVAL;
  1030. pInfo->priority = arg & 0xff;
  1031. return 0;
  1032. case R3964_USE_BCC:
  1033. if (arg)
  1034. pInfo->flags |= R3964_BCC;
  1035. else
  1036. pInfo->flags &= ~R3964_BCC;
  1037. return 0;
  1038. case R3964_READ_TELEGRAM:
  1039. return read_telegram(pInfo, task_pid(current),
  1040. (unsigned char __user *)arg);
  1041. default:
  1042. return -ENOIOCTLCMD;
  1043. }
  1044. }
  1045. static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
  1046. {
  1047. TRACE_L("set_termios");
  1048. }
  1049. /* Called without the kernel lock held - fine */
  1050. static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
  1051. struct poll_table_struct *wait)
  1052. {
  1053. struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
  1054. struct r3964_client_info *pClient;
  1055. struct r3964_message *pMsg = NULL;
  1056. unsigned long flags;
  1057. int result = POLLOUT;
  1058. TRACE_L("POLL");
  1059. pClient = findClient(pInfo, task_pid(current));
  1060. if (pClient) {
  1061. poll_wait(file, &pInfo->read_wait, wait);
  1062. spin_lock_irqsave(&pInfo->lock, flags);
  1063. pMsg = pClient->first_msg;
  1064. spin_unlock_irqrestore(&pInfo->lock, flags);
  1065. if (pMsg)
  1066. result |= POLLIN | POLLRDNORM;
  1067. } else {
  1068. result = -EINVAL;
  1069. }
  1070. return result;
  1071. }
  1072. static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1073. char *fp, int count)
  1074. {
  1075. struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
  1076. const unsigned char *p;
  1077. char *f, flags = 0;
  1078. int i;
  1079. for (i = count, p = cp, f = fp; i; i--, p++) {
  1080. if (f)
  1081. flags = *f++;
  1082. if (flags == TTY_NORMAL) {
  1083. receive_char(pInfo, *p);
  1084. } else {
  1085. receive_error(pInfo, flags);
  1086. }
  1087. }
  1088. }
  1089. MODULE_LICENSE("GPL");
  1090. MODULE_ALIAS_LDISC(N_R3964);