blob: a98290d7a2c581b1add26df5fcede3d484e9cbd6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
51#include <linux/module.h>
52#include <linux/kernel.h>
53#include <linux/sched.h>
54#include <linux/types.h>
55#include <linux/fcntl.h>
56#include <linux/interrupt.h>
57#include <linux/ptrace.h>
58#include <linux/ioport.h>
59#include <linux/in.h>
60#include <linux/slab.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040061#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/tty.h>
63#include <linux/errno.h>
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080064#include <linux/string.h> /* used in new tty drivers */
65#include <linux/signal.h> /* used in new tty drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/ioctl.h>
67#include <linux/n_r3964.h>
68#include <linux/poll.h>
69#include <linux/init.h>
70#include <asm/uaccess.h>
71
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080072/*#define DEBUG_QUEUE*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/* Log successful handshake and protocol operations */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080075/*#define DEBUG_PROTO_S*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/* Log handshake and protocol errors: */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080078/*#define DEBUG_PROTO_E*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* Log Linediscipline operations (open, close, read, write...): */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080081/*#define DEBUG_LDISC*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/* Log module and memory operations (init, cleanup; kmalloc, kfree): */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080084/*#define DEBUG_MODUL*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* Macro helpers for debug output: */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080087#define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#ifdef DEBUG_MODUL
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080090#define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#else
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080092#define TRACE_M(fmt, arg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#ifdef DEBUG_PROTO_S
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080095#define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#else
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080097#define TRACE_PS(fmt, arg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#ifdef DEBUG_PROTO_E
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800100#define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#else
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800102#define TRACE_PE(fmt, arg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#ifdef DEBUG_LDISC
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800105#define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#else
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800107#define TRACE_L(fmt, arg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#ifdef DEBUG_QUEUE
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800110#define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#else
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800112#define TRACE_Q(fmt, arg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
115static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
116static void put_char(struct r3964_info *pInfo, unsigned char ch);
117static void trigger_transmit(struct r3964_info *pInfo);
118static void retry_transmit(struct r3964_info *pInfo);
119static void transmit_block(struct r3964_info *pInfo);
120static void receive_char(struct r3964_info *pInfo, const unsigned char c);
121static void receive_error(struct r3964_info *pInfo, const char flag);
122static void on_timeout(unsigned long priv);
Eric W. Biederman3cec5562006-12-13 00:35:10 -0800123static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg);
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800124static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
125 unsigned char __user * buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800127 int error_code, struct r3964_block_header *pBlock);
128static struct r3964_message *remove_msg(struct r3964_info *pInfo,
129 struct r3964_client_info *pClient);
130static void remove_client_block(struct r3964_info *pInfo,
131 struct r3964_client_info *pClient);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800133static int r3964_open(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static void r3964_close(struct tty_struct *tty);
135static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800136 unsigned char __user * buf, size_t nr);
137static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
138 const unsigned char *buf, size_t nr);
139static int r3964_ioctl(struct tty_struct *tty, struct file *file,
140 unsigned int cmd, unsigned long arg);
141static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
142static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
143 struct poll_table_struct *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800145 char *fp, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Alan Coxa352def2008-07-16 21:53:12 +0100147static struct tty_ldisc_ops tty_ldisc_N_R3964 = {
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800148 .owner = THIS_MODULE,
149 .magic = TTY_LDISC_MAGIC,
150 .name = "R3964",
151 .open = r3964_open,
152 .close = r3964_close,
153 .read = r3964_read,
154 .write = r3964_write,
155 .ioctl = r3964_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 .set_termios = r3964_set_termios,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800157 .poll = r3964_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .receive_buf = r3964_receive_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static void dump_block(const unsigned char *block, unsigned int length)
162{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800163 unsigned int i, j;
164 char linebuf[16 * 3 + 1];
165
166 for (i = 0; i < length; i += 16) {
167 for (j = 0; (j < 16) && (j + i < length); j++) {
168 sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
169 }
170 linebuf[3 * j] = '\0';
171 TRACE_PS("%s", linebuf);
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*************************************************************
176 * Driver initialisation
177 *************************************************************/
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*************************************************************
180 * Module support routines
181 *************************************************************/
182
183static void __exit r3964_exit(void)
184{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800185 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800187 TRACE_M("cleanup_module()");
188
189 status = tty_unregister_ldisc(N_R3964);
190
191 if (status != 0) {
192 printk(KERN_ERR "r3964: error unregistering linediscipline: "
193 "%d\n", status);
194 } else {
195 TRACE_L("linediscipline successfully unregistered");
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199static int __init r3964_init(void)
200{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800201 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800203 printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
204
205 /*
206 * Register the tty line discipline
207 */
208
209 status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
210 if (status == 0) {
211 TRACE_L("line discipline %d registered", N_R3964);
212 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
213 tty_ldisc_N_R3964.num);
214 TRACE_L("open=%p", tty_ldisc_N_R3964.open);
215 TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
216 } else {
217 printk(KERN_ERR "r3964: error registering line discipline: "
218 "%d\n", status);
219 }
220 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223module_init(r3964_init);
224module_exit(r3964_exit);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/*************************************************************
227 * Protocol implementation routines
228 *************************************************************/
229
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800230static void add_tx_queue(struct r3964_info *pInfo,
231 struct r3964_block_header *pHeader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800233 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800235 spin_lock_irqsave(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800237 pHeader->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800239 if (pInfo->tx_last == NULL) {
240 pInfo->tx_first = pInfo->tx_last = pHeader;
241 } else {
242 pInfo->tx_last->next = pHeader;
243 pInfo->tx_last = pHeader;
244 }
245
246 spin_unlock_irqrestore(&pInfo->lock, flags);
247
248 TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
249 pHeader, pHeader->length, pInfo->tx_first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
253{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800254 struct r3964_block_header *pHeader;
255 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#ifdef DEBUG_QUEUE
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800257 struct r3964_block_header *pDump;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#endif
259
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800260 pHeader = pInfo->tx_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800262 if (pHeader == NULL)
263 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800265#ifdef DEBUG_QUEUE
266 printk("r3964: remove_from_tx_queue: %p, length %u - ",
267 pHeader, pHeader->length);
268 for (pDump = pHeader; pDump; pDump = pDump->next)
269 printk("%p ", pDump);
270 printk("\n");
271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800273 if (pHeader->owner) {
274 if (error_code) {
275 add_msg(pHeader->owner, R3964_MSG_ACK, 0,
276 error_code, NULL);
277 } else {
278 add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
279 error_code, NULL);
280 }
281 wake_up_interruptible(&pInfo->read_wait);
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800284 spin_lock_irqsave(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800286 pInfo->tx_first = pHeader->next;
287 if (pInfo->tx_first == NULL) {
288 pInfo->tx_last = NULL;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800291 spin_unlock_irqrestore(&pInfo->lock, flags);
292
293 kfree(pHeader);
294 TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
295
296 TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
297 pInfo->tx_first, pInfo->tx_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800300static void add_rx_queue(struct r3964_info *pInfo,
301 struct r3964_block_header *pHeader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800303 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800305 spin_lock_irqsave(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800307 pHeader->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800309 if (pInfo->rx_last == NULL) {
310 pInfo->rx_first = pInfo->rx_last = pHeader;
311 } else {
312 pInfo->rx_last->next = pHeader;
313 pInfo->rx_last = pHeader;
314 }
315 pInfo->blocks_in_rx_queue++;
316
317 spin_unlock_irqrestore(&pInfo->lock, flags);
318
319 TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
320 pHeader, pHeader->length,
321 pInfo->rx_first, pInfo->blocks_in_rx_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324static void remove_from_rx_queue(struct r3964_info *pInfo,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800325 struct r3964_block_header *pHeader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800327 unsigned long flags;
328 struct r3964_block_header *pFind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800330 if (pHeader == NULL)
331 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800333 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
334 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
335 TRACE_Q("remove_from_rx_queue: %p, length %u",
336 pHeader, pHeader->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800338 spin_lock_irqsave(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800340 if (pInfo->rx_first == pHeader) {
341 /* Remove the first block in the linked list: */
342 pInfo->rx_first = pHeader->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800344 if (pInfo->rx_first == NULL) {
345 pInfo->rx_last = NULL;
346 }
347 pInfo->blocks_in_rx_queue--;
348 } else {
349 /* Find block to remove: */
350 for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
351 if (pFind->next == pHeader) {
352 /* Got it. */
353 pFind->next = pHeader->next;
354 pInfo->blocks_in_rx_queue--;
355 if (pFind->next == NULL) {
356 /* Oh, removed the last one! */
357 pInfo->rx_last = pFind;
358 }
359 break;
360 }
361 }
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800364 spin_unlock_irqrestore(&pInfo->lock, flags);
365
366 kfree(pHeader);
367 TRACE_M("remove_from_rx_queue - kfree %p", pHeader);
368
369 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
370 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373static void put_char(struct r3964_info *pInfo, unsigned char ch)
374{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800375 struct tty_struct *tty = pInfo->tty;
Alan Coxf34d7a52008-04-30 00:54:13 -0700376 /* FIXME: put_char should not be called from an IRQ */
Alan Cox51383f62008-10-13 10:44:57 +0100377 tty_put_char(tty, ch);
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800378 pInfo->bcc ^= ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381static void flush(struct r3964_info *pInfo)
382{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800383 struct tty_struct *tty = pInfo->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Alan Coxf34d7a52008-04-30 00:54:13 -0700385 if (tty == NULL || tty->ops->flush_chars == NULL)
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800386 return;
Alan Coxf34d7a52008-04-30 00:54:13 -0700387 tty->ops->flush_chars(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390static void trigger_transmit(struct r3964_info *pInfo)
391{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800392 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800394 spin_lock_irqsave(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800396 if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
397 pInfo->state = R3964_TX_REQUEST;
398 pInfo->nRetry = 0;
399 pInfo->flags &= ~R3964_ERROR;
400 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800402 spin_unlock_irqrestore(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800404 TRACE_PS("trigger_transmit - sent STX");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800406 put_char(pInfo, STX);
407 flush(pInfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800409 pInfo->bcc = 0;
410 } else {
411 spin_unlock_irqrestore(&pInfo->lock, flags);
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415static void retry_transmit(struct r3964_info *pInfo)
416{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800417 if (pInfo->nRetry < R3964_MAX_RETRIES) {
418 TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
419 pInfo->bcc = 0;
420 put_char(pInfo, STX);
421 flush(pInfo);
422 pInfo->state = R3964_TX_REQUEST;
423 pInfo->nRetry++;
424 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
425 } else {
426 TRACE_PE("transmission failed after %d retries",
427 R3964_MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800429 remove_from_tx_queue(pInfo, R3964_TX_FAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800431 put_char(pInfo, NAK);
432 flush(pInfo);
433 pInfo->state = R3964_IDLE;
434
435 trigger_transmit(pInfo);
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static void transmit_block(struct r3964_info *pInfo)
440{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800441 struct tty_struct *tty = pInfo->tty;
442 struct r3964_block_header *pBlock = pInfo->tx_first;
443 int room = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Alan Coxf34d7a52008-04-30 00:54:13 -0700445 if (tty == NULL || pBlock == NULL) {
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800446 return;
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Alan Coxf34d7a52008-04-30 00:54:13 -0700449 room = tty_write_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800451 TRACE_PS("transmit_block %p, room %d, length %d",
452 pBlock, room, pBlock->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800454 while (pInfo->tx_position < pBlock->length) {
455 if (room < 2)
456 break;
457
458 if (pBlock->data[pInfo->tx_position] == DLE) {
459 /* send additional DLE char: */
460 put_char(pInfo, DLE);
461 }
462 put_char(pInfo, pBlock->data[pInfo->tx_position++]);
463
464 room--;
465 }
466
467 if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
468 put_char(pInfo, DLE);
469 put_char(pInfo, ETX);
470 if (pInfo->flags & R3964_BCC) {
471 put_char(pInfo, pInfo->bcc);
472 }
473 pInfo->state = R3964_WAIT_FOR_TX_ACK;
474 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
475 }
476 flush(pInfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479static void on_receive_block(struct r3964_info *pInfo)
480{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800481 unsigned int length;
482 struct r3964_client_info *pClient;
483 struct r3964_block_header *pBlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800485 length = pInfo->rx_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800487 /* compare byte checksum characters: */
488 if (pInfo->flags & R3964_BCC) {
489 if (pInfo->bcc != pInfo->last_rx) {
490 TRACE_PE("checksum error - got %x but expected %x",
491 pInfo->last_rx, pInfo->bcc);
492 pInfo->flags |= R3964_CHECKSUM;
493 }
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800496 /* check for errors (parity, overrun,...): */
497 if (pInfo->flags & R3964_ERROR) {
498 TRACE_PE("on_receive_block - transmission failed error %x",
499 pInfo->flags & R3964_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800501 put_char(pInfo, NAK);
502 flush(pInfo);
503 if (pInfo->nRetry < R3964_MAX_RETRIES) {
504 pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
505 pInfo->nRetry++;
506 mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
507 } else {
508 TRACE_PE("on_receive_block - failed after max retries");
509 pInfo->state = R3964_IDLE;
510 }
511 return;
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800514 /* received block; submit DLE: */
515 put_char(pInfo, DLE);
516 flush(pInfo);
517 del_timer_sync(&pInfo->tmr);
518 TRACE_PS(" rx success: got %d chars", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800520 /* prepare struct r3964_block_header: */
521 pBlock = kmalloc(length + sizeof(struct r3964_block_header),
522 GFP_KERNEL);
523 TRACE_M("on_receive_block - kmalloc %p", pBlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800525 if (pBlock == NULL)
526 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800528 pBlock->length = length;
529 pBlock->data = ((unsigned char *)pBlock) +
530 sizeof(struct r3964_block_header);
531 pBlock->locks = 0;
532 pBlock->next = NULL;
533 pBlock->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800535 memcpy(pBlock->data, pInfo->rx_buf, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800537 /* queue block into rx_queue: */
538 add_rx_queue(pInfo, pBlock);
539
540 /* notify attached client processes: */
541 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
542 if (pClient->sig_flags & R3964_SIG_DATA) {
543 add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
544 pBlock);
545 }
546 }
547 wake_up_interruptible(&pInfo->read_wait);
548
549 pInfo->state = R3964_IDLE;
550
551 trigger_transmit(pInfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static void receive_char(struct r3964_info *pInfo, const unsigned char c)
555{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800556 switch (pInfo->state) {
557 case R3964_TX_REQUEST:
558 if (c == DLE) {
559 TRACE_PS("TX_REQUEST - got DLE");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800561 pInfo->state = R3964_TRANSMITTING;
562 pInfo->tx_position = 0;
563
564 transmit_block(pInfo);
565 } else if (c == STX) {
566 if (pInfo->nRetry == 0) {
567 TRACE_PE("TX_REQUEST - init conflict");
568 if (pInfo->priority == R3964_SLAVE) {
569 goto start_receiving;
570 }
571 } else {
572 TRACE_PE("TX_REQUEST - secondary init "
573 "conflict!? Switching to SLAVE mode "
574 "for next rx.");
575 goto start_receiving;
576 }
577 } else {
578 TRACE_PE("TX_REQUEST - char != DLE: %x", c);
579 retry_transmit(pInfo);
580 }
581 break;
582 case R3964_TRANSMITTING:
583 if (c == NAK) {
584 TRACE_PE("TRANSMITTING - got NAK");
585 retry_transmit(pInfo);
586 } else {
587 TRACE_PE("TRANSMITTING - got invalid char");
588
589 pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
590 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
591 }
592 break;
593 case R3964_WAIT_FOR_TX_ACK:
594 if (c == DLE) {
595 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
596 remove_from_tx_queue(pInfo, R3964_OK);
597
598 pInfo->state = R3964_IDLE;
599 trigger_transmit(pInfo);
600 } else {
601 retry_transmit(pInfo);
602 }
603 break;
604 case R3964_WAIT_FOR_RX_REPEAT:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200605 /* FALLTHROUGH */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800606 case R3964_IDLE:
607 if (c == STX) {
608 /* Prevent rx_queue from overflow: */
609 if (pInfo->blocks_in_rx_queue >=
610 R3964_MAX_BLOCKS_IN_RX_QUEUE) {
611 TRACE_PE("IDLE - got STX but no space in "
612 "rx_queue!");
613 pInfo->state = R3964_WAIT_FOR_RX_BUF;
614 mod_timer(&pInfo->tmr,
615 jiffies + R3964_TO_NO_BUF);
616 break;
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618start_receiving:
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800619 /* Ok, start receiving: */
620 TRACE_PS("IDLE - got STX");
621 pInfo->rx_position = 0;
622 pInfo->last_rx = 0;
623 pInfo->flags &= ~R3964_ERROR;
624 pInfo->state = R3964_RECEIVING;
625 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
626 pInfo->nRetry = 0;
627 put_char(pInfo, DLE);
628 flush(pInfo);
629 pInfo->bcc = 0;
630 }
631 break;
632 case R3964_RECEIVING:
633 if (pInfo->rx_position < RX_BUF_SIZE) {
634 pInfo->bcc ^= c;
635
636 if (c == DLE) {
637 if (pInfo->last_rx == DLE) {
638 pInfo->last_rx = 0;
639 goto char_to_buf;
640 }
641 pInfo->last_rx = DLE;
642 break;
643 } else if ((c == ETX) && (pInfo->last_rx == DLE)) {
644 if (pInfo->flags & R3964_BCC) {
645 pInfo->state = R3964_WAIT_FOR_BCC;
646 mod_timer(&pInfo->tmr,
647 jiffies + R3964_TO_ZVZ);
648 } else {
649 on_receive_block(pInfo);
650 }
651 } else {
652 pInfo->last_rx = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653char_to_buf:
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800654 pInfo->rx_buf[pInfo->rx_position++] = c;
655 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
656 }
657 }
658 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
659 break;
660 case R3964_WAIT_FOR_BCC:
661 pInfo->last_rx = c;
662 on_receive_block(pInfo);
663 break;
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667static void receive_error(struct r3964_info *pInfo, const char flag)
668{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800669 switch (flag) {
670 case TTY_NORMAL:
671 break;
672 case TTY_BREAK:
673 TRACE_PE("received break");
674 pInfo->flags |= R3964_BREAK;
675 break;
676 case TTY_PARITY:
677 TRACE_PE("parity error");
678 pInfo->flags |= R3964_PARITY;
679 break;
680 case TTY_FRAME:
681 TRACE_PE("frame error");
682 pInfo->flags |= R3964_FRAME;
683 break;
684 case TTY_OVERRUN:
685 TRACE_PE("frame overrun");
686 pInfo->flags |= R3964_OVERRUN;
687 break;
688 default:
689 TRACE_PE("receive_error - unknown flag %d", flag);
690 pInfo->flags |= R3964_UNKNOWN;
691 break;
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695static void on_timeout(unsigned long priv)
696{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800697 struct r3964_info *pInfo = (void *)priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800699 switch (pInfo->state) {
700 case R3964_TX_REQUEST:
701 TRACE_PE("TX_REQUEST - timeout");
702 retry_transmit(pInfo);
703 break;
704 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
705 put_char(pInfo, NAK);
706 flush(pInfo);
707 retry_transmit(pInfo);
708 break;
709 case R3964_WAIT_FOR_TX_ACK:
710 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
711 retry_transmit(pInfo);
712 break;
713 case R3964_WAIT_FOR_RX_BUF:
714 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
715 put_char(pInfo, NAK);
716 flush(pInfo);
717 pInfo->state = R3964_IDLE;
718 break;
719 case R3964_RECEIVING:
720 TRACE_PE("RECEIVING - timeout after %d chars",
721 pInfo->rx_position);
722 put_char(pInfo, NAK);
723 flush(pInfo);
724 pInfo->state = R3964_IDLE;
725 break;
726 case R3964_WAIT_FOR_RX_REPEAT:
727 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
728 pInfo->state = R3964_IDLE;
729 break;
730 case R3964_WAIT_FOR_BCC:
731 TRACE_PE("WAIT_FOR_BCC - timeout");
732 put_char(pInfo, NAK);
733 flush(pInfo);
734 pInfo->state = R3964_IDLE;
735 break;
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800739static struct r3964_client_info *findClient(struct r3964_info *pInfo,
740 struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800742 struct r3964_client_info *pClient;
743
744 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
745 if (pClient->pid == pid) {
746 return pClient;
747 }
748 }
749 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Eric W. Biederman3cec5562006-12-13 00:35:10 -0800752static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800754 struct r3964_client_info *pClient;
755 struct r3964_client_info **ppClient;
756 struct r3964_message *pMsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800758 if ((arg & R3964_SIG_ALL) == 0) {
759 /* Remove client from client list */
760 for (ppClient = &pInfo->firstClient; *ppClient;
761 ppClient = &(*ppClient)->next) {
762 pClient = *ppClient;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800764 if (pClient->pid == pid) {
765 TRACE_PS("removing client %d from client list",
766 pid_nr(pid));
767 *ppClient = pClient->next;
768 while (pClient->msg_count) {
769 pMsg = remove_msg(pInfo, pClient);
770 if (pMsg) {
771 kfree(pMsg);
772 TRACE_M("enable_signals - msg "
773 "kfree %p", pMsg);
774 }
775 }
776 put_pid(pClient->pid);
777 kfree(pClient);
778 TRACE_M("enable_signals - kfree %p", pClient);
779 return 0;
780 }
781 }
782 return -EINVAL;
783 } else {
784 pClient = findClient(pInfo, pid);
785 if (pClient) {
786 /* update signal options */
787 pClient->sig_flags = arg;
788 } else {
789 /* add client to client list */
790 pClient = kmalloc(sizeof(struct r3964_client_info),
791 GFP_KERNEL);
792 TRACE_M("enable_signals - kmalloc %p", pClient);
793 if (pClient == NULL)
794 return -ENOMEM;
795
796 TRACE_PS("add client %d to client list", pid_nr(pid));
797 spin_lock_init(&pClient->lock);
798 pClient->sig_flags = arg;
799 pClient->pid = get_pid(pid);
800 pClient->next = pInfo->firstClient;
801 pClient->first_msg = NULL;
802 pClient->last_msg = NULL;
803 pClient->next_block_to_read = NULL;
804 pClient->msg_count = 0;
805 pInfo->firstClient = pClient;
806 }
807 }
808
809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800812static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
813 unsigned char __user * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800815 struct r3964_client_info *pClient;
816 struct r3964_block_header *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800818 if (!buf) {
819 return -EINVAL;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800822 pClient = findClient(pInfo, pid);
823 if (pClient == NULL) {
824 return -EINVAL;
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800827 block = pClient->next_block_to_read;
828 if (!block) {
829 return 0;
830 } else {
831 if (copy_to_user(buf, block->data, block->length))
832 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800834 remove_client_block(pInfo, pClient);
835 return block->length;
836 }
837
838 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800842 int error_code, struct r3964_block_header *pBlock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800844 struct r3964_message *pMsg;
845 unsigned long flags;
846
847 if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848queue_the_message:
849
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800850 pMsg = kmalloc(sizeof(struct r3964_message),
851 error_code ? GFP_ATOMIC : GFP_KERNEL);
852 TRACE_M("add_msg - kmalloc %p", pMsg);
853 if (pMsg == NULL) {
854 return;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800857 spin_lock_irqsave(&pClient->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800859 pMsg->msg_id = msg_id;
860 pMsg->arg = arg;
861 pMsg->error_code = error_code;
862 pMsg->block = pBlock;
863 pMsg->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800865 if (pClient->last_msg == NULL) {
866 pClient->first_msg = pClient->last_msg = pMsg;
867 } else {
868 pClient->last_msg->next = pMsg;
869 pClient->last_msg = pMsg;
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800872 pClient->msg_count++;
873
874 if (pBlock != NULL) {
875 pBlock->locks++;
876 }
877 spin_unlock_irqrestore(&pClient->lock, flags);
878 } else {
879 if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
880 && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
881 pClient->last_msg->arg++;
882 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
883 } else {
884 msg_id = R3964_MSG_ACK;
885 arg = 0;
886 error_code = R3964_OVERFLOW;
887 pBlock = NULL;
888 TRACE_PE("add_msg - queue OVERFLOW-msg");
889 goto queue_the_message;
890 }
891 }
892 /* Send SIGIO signal to client process: */
893 if (pClient->sig_flags & R3964_USE_SIGIO) {
894 kill_pid(pClient->pid, SIGIO, 1);
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
898static struct r3964_message *remove_msg(struct r3964_info *pInfo,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800899 struct r3964_client_info *pClient)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800901 struct r3964_message *pMsg = NULL;
902 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800904 if (pClient->first_msg) {
905 spin_lock_irqsave(&pClient->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800907 pMsg = pClient->first_msg;
908 pClient->first_msg = pMsg->next;
909 if (pClient->first_msg == NULL) {
910 pClient->last_msg = NULL;
911 }
912
913 pClient->msg_count--;
914 if (pMsg->block) {
915 remove_client_block(pInfo, pClient);
916 pClient->next_block_to_read = pMsg->block;
917 }
918 spin_unlock_irqrestore(&pClient->lock, flags);
919 }
920 return pMsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800923static void remove_client_block(struct r3964_info *pInfo,
924 struct r3964_client_info *pClient)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800926 struct r3964_block_header *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800928 TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800930 block = pClient->next_block_to_read;
931 if (block) {
932 block->locks--;
933 if (block->locks == 0) {
934 remove_from_rx_queue(pInfo, block);
935 }
936 }
937 pClient->next_block_to_read = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/*************************************************************
941 * Line discipline routines
942 *************************************************************/
943
944static int r3964_open(struct tty_struct *tty)
945{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800946 struct r3964_info *pInfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800948 TRACE_L("open");
949 TRACE_L("tty=%p, PID=%d, disc_data=%p",
950 tty, current->pid, tty->disc_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800952 pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
953 TRACE_M("r3964_open - info kmalloc %p", pInfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800955 if (!pInfo) {
956 printk(KERN_ERR "r3964: failed to alloc info structure\n");
957 return -ENOMEM;
958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800960 pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
961 TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800963 if (!pInfo->rx_buf) {
964 printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
965 kfree(pInfo);
966 TRACE_M("r3964_open - info kfree %p", pInfo);
967 return -ENOMEM;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800970 pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
971 TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800973 if (!pInfo->tx_buf) {
974 printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
975 kfree(pInfo->rx_buf);
976 TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
977 kfree(pInfo);
978 TRACE_M("r3964_open - info kfree %p", pInfo);
979 return -ENOMEM;
980 }
981
982 spin_lock_init(&pInfo->lock);
983 pInfo->tty = tty;
984 init_waitqueue_head(&pInfo->read_wait);
985 pInfo->priority = R3964_MASTER;
986 pInfo->rx_first = pInfo->rx_last = NULL;
987 pInfo->tx_first = pInfo->tx_last = NULL;
988 pInfo->rx_position = 0;
989 pInfo->tx_position = 0;
990 pInfo->last_rx = 0;
991 pInfo->blocks_in_rx_queue = 0;
992 pInfo->firstClient = NULL;
993 pInfo->state = R3964_IDLE;
994 pInfo->flags = R3964_DEBUG;
995 pInfo->nRetry = 0;
996
997 tty->disc_data = pInfo;
998 tty->receive_room = 65536;
999
Jiri Slaby40565f12007-02-12 00:52:31 -08001000 setup_timer(&pInfo->tmr, on_timeout, (unsigned long)pInfo);
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001001
1002 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
1005static void r3964_close(struct tty_struct *tty)
1006{
Alan Coxc9f19e92009-01-02 13:47:26 +00001007 struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001008 struct r3964_client_info *pClient, *pNext;
1009 struct r3964_message *pMsg;
1010 struct r3964_block_header *pHeader, *pNextHeader;
1011 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001013 TRACE_L("close");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001015 /*
1016 * Make sure that our task queue isn't activated. If it
1017 * is, take it out of the linked list.
1018 */
1019 del_timer_sync(&pInfo->tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001021 /* Remove client-structs and message queues: */
1022 pClient = pInfo->firstClient;
1023 while (pClient) {
1024 pNext = pClient->next;
1025 while (pClient->msg_count) {
1026 pMsg = remove_msg(pInfo, pClient);
1027 if (pMsg) {
1028 kfree(pMsg);
1029 TRACE_M("r3964_close - msg kfree %p", pMsg);
1030 }
1031 }
1032 put_pid(pClient->pid);
1033 kfree(pClient);
1034 TRACE_M("r3964_close - client kfree %p", pClient);
1035 pClient = pNext;
1036 }
1037 /* Remove jobs from tx_queue: */
1038 spin_lock_irqsave(&pInfo->lock, flags);
1039 pHeader = pInfo->tx_first;
1040 pInfo->tx_first = pInfo->tx_last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 spin_unlock_irqrestore(&pInfo->lock, flags);
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001042
1043 while (pHeader) {
1044 pNextHeader = pHeader->next;
1045 kfree(pHeader);
1046 pHeader = pNextHeader;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001049 /* Free buffers: */
1050 wake_up_interruptible(&pInfo->read_wait);
1051 kfree(pInfo->rx_buf);
1052 TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
1053 kfree(pInfo->tx_buf);
1054 TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
1055 kfree(pInfo);
1056 TRACE_M("r3964_close - info kfree %p", pInfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001060 unsigned char __user * buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Alan Coxc9f19e92009-01-02 13:47:26 +00001062 struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001063 struct r3964_client_info *pClient;
1064 struct r3964_message *pMsg;
1065 struct r3964_client_message theMsg;
Jiri Slabyeca41042009-06-22 18:42:03 +01001066 int ret;
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001067
1068 TRACE_L("read()");
1069
Arnd Bergmannec79d602010-06-01 22:53:01 +02001070 tty_lock();
Alan Cox04f378b2008-04-30 00:53:29 -07001071
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001072 pClient = findClient(pInfo, task_pid(current));
1073 if (pClient) {
1074 pMsg = remove_msg(pInfo, pClient);
1075 if (pMsg == NULL) {
1076 /* no messages available. */
1077 if (file->f_flags & O_NONBLOCK) {
Jiri Slabyeca41042009-06-22 18:42:03 +01001078 ret = -EAGAIN;
1079 goto unlock;
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001080 }
1081 /* block until there is a message: */
Arnd Bergmannbe1bc282010-06-01 22:53:05 +02001082 wait_event_interruptible_tty(pInfo->read_wait,
Jiri Slaby6defec12007-07-15 23:40:20 -07001083 (pMsg = remove_msg(pInfo, pClient)));
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001086 /* If we still haven't got a message, we must have been signalled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Alan Cox04f378b2008-04-30 00:53:29 -07001088 if (!pMsg) {
Jiri Slabyeca41042009-06-22 18:42:03 +01001089 ret = -EINTR;
1090 goto unlock;
Alan Cox04f378b2008-04-30 00:53:29 -07001091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001093 /* deliver msg to client process: */
1094 theMsg.msg_id = pMsg->msg_id;
1095 theMsg.arg = pMsg->arg;
1096 theMsg.error_code = pMsg->error_code;
Jiri Slabyeca41042009-06-22 18:42:03 +01001097 ret = sizeof(struct r3964_client_message);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001099 kfree(pMsg);
1100 TRACE_M("r3964_read - msg kfree %p", pMsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Jiri Slabyeca41042009-06-22 18:42:03 +01001102 if (copy_to_user(buf, &theMsg, ret)) {
1103 ret = -EFAULT;
1104 goto unlock;
Alan Cox04f378b2008-04-30 00:53:29 -07001105 }
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001106
Jiri Slabyeca41042009-06-22 18:42:03 +01001107 TRACE_PS("read - return %d", ret);
1108 goto unlock;
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001109 }
Jiri Slabyeca41042009-06-22 18:42:03 +01001110 ret = -EPERM;
1111unlock:
Arnd Bergmannec79d602010-06-01 22:53:01 +02001112 tty_unlock();
Jiri Slabyeca41042009-06-22 18:42:03 +01001113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001116static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 const unsigned char *data, size_t count)
1118{
Alan Coxc9f19e92009-01-02 13:47:26 +00001119 struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001120 struct r3964_block_header *pHeader;
1121 struct r3964_client_info *pClient;
1122 unsigned char *new_data;
1123
1124 TRACE_L("write request, %d characters", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/*
1126 * Verify the pointers
1127 */
1128
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001129 if (!pInfo)
1130 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132/*
1133 * Ensure that the caller does not wish to send too much.
1134 */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001135 if (count > R3964_MTU) {
1136 if (pInfo->flags & R3964_DEBUG) {
1137 TRACE_L(KERN_WARNING "r3964_write: truncating user "
1138 "packet from %u to mtu %d", count, R3964_MTU);
1139 }
1140 count = R3964_MTU;
1141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142/*
1143 * Allocate a buffer for the data and copy it from the buffer with header prepended
1144 */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001145 new_data = kmalloc(count + sizeof(struct r3964_block_header),
1146 GFP_KERNEL);
1147 TRACE_M("r3964_write - kmalloc %p", new_data);
1148 if (new_data == NULL) {
1149 if (pInfo->flags & R3964_DEBUG) {
1150 printk(KERN_ERR "r3964_write: no memory\n");
1151 }
1152 return -ENOSPC;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001155 pHeader = (struct r3964_block_header *)new_data;
1156 pHeader->data = new_data + sizeof(struct r3964_block_header);
1157 pHeader->length = count;
1158 pHeader->locks = 0;
1159 pHeader->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Arnd Bergmannec79d602010-06-01 22:53:01 +02001161 tty_lock();
Alan Cox04f378b2008-04-30 00:53:29 -07001162
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001163 pClient = findClient(pInfo, task_pid(current));
1164 if (pClient) {
1165 pHeader->owner = pClient;
1166 }
1167
1168 memcpy(pHeader->data, data, count); /* We already verified this */
1169
1170 if (pInfo->flags & R3964_DEBUG) {
1171 dump_block(pHeader->data, count);
1172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174/*
1175 * Add buffer to transmit-queue:
1176 */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001177 add_tx_queue(pInfo, pHeader);
1178 trigger_transmit(pInfo);
1179
Arnd Bergmannec79d602010-06-01 22:53:01 +02001180 tty_unlock();
Alan Cox04f378b2008-04-30 00:53:29 -07001181
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001185static int r3964_ioctl(struct tty_struct *tty, struct file *file,
1186 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Alan Coxc9f19e92009-01-02 13:47:26 +00001188 struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001189 if (pInfo == NULL)
1190 return -EINVAL;
1191 switch (cmd) {
1192 case R3964_ENABLE_SIGNALS:
1193 return enable_signals(pInfo, task_pid(current), arg);
1194 case R3964_SETPRIORITY:
1195 if (arg < R3964_MASTER || arg > R3964_SLAVE)
1196 return -EINVAL;
1197 pInfo->priority = arg & 0xff;
1198 return 0;
1199 case R3964_USE_BCC:
1200 if (arg)
1201 pInfo->flags |= R3964_BCC;
1202 else
1203 pInfo->flags &= ~R3964_BCC;
1204 return 0;
1205 case R3964_READ_TELEGRAM:
1206 return read_telegram(pInfo, task_pid(current),
1207 (unsigned char __user *)arg);
1208 default:
1209 return -ENOIOCTLCMD;
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001213static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001215 TRACE_L("set_termios");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218/* Called without the kernel lock held - fine */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001219static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
1220 struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Alan Coxc9f19e92009-01-02 13:47:26 +00001222 struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001223 struct r3964_client_info *pClient;
1224 struct r3964_message *pMsg = NULL;
1225 unsigned long flags;
1226 int result = POLLOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001228 TRACE_L("POLL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001230 pClient = findClient(pInfo, task_pid(current));
1231 if (pClient) {
1232 poll_wait(file, &pInfo->read_wait, wait);
1233 spin_lock_irqsave(&pInfo->lock, flags);
1234 pMsg = pClient->first_msg;
1235 spin_unlock_irqrestore(&pInfo->lock, flags);
1236 if (pMsg)
1237 result |= POLLIN | POLLRDNORM;
1238 } else {
1239 result = -EINVAL;
1240 }
1241 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001245 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Alan Coxc9f19e92009-01-02 13:47:26 +00001247 struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001248 const unsigned char *p;
1249 char *f, flags = 0;
1250 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001252 for (i = count, p = cp, f = fp; i; i--, p++) {
1253 if (f)
1254 flags = *f++;
1255 if (flags == TTY_NORMAL) {
1256 receive_char(pInfo, *p);
1257 } else {
1258 receive_error(pInfo, flags);
1259 }
1260
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264MODULE_LICENSE("GPL");
1265MODULE_ALIAS_LDISC(N_R3964);