n_r3964.c 31 KB

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