blob: 6b918b80f73e6af785eeb34fdefa27e75ca32780 [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>
61#include <linux/tty.h>
62#include <linux/errno.h>
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080063#include <linux/string.h> /* used in new tty drivers */
64#include <linux/signal.h> /* used in new tty drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/ioctl.h>
66#include <linux/n_r3964.h>
67#include <linux/poll.h>
68#include <linux/init.h>
69#include <asm/uaccess.h>
70
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080071/*#define DEBUG_QUEUE*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/* Log successful handshake and protocol operations */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080074/*#define DEBUG_PROTO_S*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* Log handshake and protocol errors: */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080077/*#define DEBUG_PROTO_E*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* Log Linediscipline operations (open, close, read, write...): */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080080/*#define DEBUG_LDISC*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* Log module and memory operations (init, cleanup; kmalloc, kfree): */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080083/*#define DEBUG_MODUL*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* Macro helpers for debug output: */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080086#define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#ifdef DEBUG_MODUL
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080089#define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#else
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080091#define TRACE_M(fmt, arg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#ifdef DEBUG_PROTO_S
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080094#define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#else
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080096#define TRACE_PS(fmt, arg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#ifdef DEBUG_PROTO_E
Jiri Slaby5e07e1c2007-02-10 01:45:09 -080099#define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#else
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800101#define TRACE_PE(fmt, arg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#ifdef DEBUG_LDISC
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800104#define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#else
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800106#define TRACE_L(fmt, arg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#ifdef DEBUG_QUEUE
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800109#define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#else
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800111#define TRACE_Q(fmt, arg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
114static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
115static void put_char(struct r3964_info *pInfo, unsigned char ch);
116static void trigger_transmit(struct r3964_info *pInfo);
117static void retry_transmit(struct r3964_info *pInfo);
118static void transmit_block(struct r3964_info *pInfo);
119static void receive_char(struct r3964_info *pInfo, const unsigned char c);
120static void receive_error(struct r3964_info *pInfo, const char flag);
121static void on_timeout(unsigned long priv);
Eric W. Biederman3cec5562006-12-13 00:35:10 -0800122static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg);
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800123static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
124 unsigned char __user * buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800126 int error_code, struct r3964_block_header *pBlock);
127static struct r3964_message *remove_msg(struct r3964_info *pInfo,
128 struct r3964_client_info *pClient);
129static void remove_client_block(struct r3964_info *pInfo,
130 struct r3964_client_info *pClient);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800132static int r3964_open(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static void r3964_close(struct tty_struct *tty);
134static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800135 unsigned char __user * buf, size_t nr);
136static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
137 const unsigned char *buf, size_t nr);
138static int r3964_ioctl(struct tty_struct *tty, struct file *file,
139 unsigned int cmd, unsigned long arg);
140static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
141static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
142 struct poll_table_struct *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800144 char *fp, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146static struct tty_ldisc tty_ldisc_N_R3964 = {
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800147 .owner = THIS_MODULE,
148 .magic = TTY_LDISC_MAGIC,
149 .name = "R3964",
150 .open = r3964_open,
151 .close = r3964_close,
152 .read = r3964_read,
153 .write = r3964_write,
154 .ioctl = r3964_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .set_termios = r3964_set_termios,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800156 .poll = r3964_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .receive_buf = r3964_receive_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static void dump_block(const unsigned char *block, unsigned int length)
161{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800162 unsigned int i, j;
163 char linebuf[16 * 3 + 1];
164
165 for (i = 0; i < length; i += 16) {
166 for (j = 0; (j < 16) && (j + i < length); j++) {
167 sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
168 }
169 linebuf[3 * j] = '\0';
170 TRACE_PS("%s", linebuf);
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*************************************************************
175 * Driver initialisation
176 *************************************************************/
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*************************************************************
179 * Module support routines
180 *************************************************************/
181
182static void __exit r3964_exit(void)
183{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800184 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800186 TRACE_M("cleanup_module()");
187
188 status = tty_unregister_ldisc(N_R3964);
189
190 if (status != 0) {
191 printk(KERN_ERR "r3964: error unregistering linediscipline: "
192 "%d\n", status);
193 } else {
194 TRACE_L("linediscipline successfully unregistered");
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198static int __init r3964_init(void)
199{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800200 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800202 printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
203
204 /*
205 * Register the tty line discipline
206 */
207
208 status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
209 if (status == 0) {
210 TRACE_L("line discipline %d registered", N_R3964);
211 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
212 tty_ldisc_N_R3964.num);
213 TRACE_L("open=%p", tty_ldisc_N_R3964.open);
214 TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
215 } else {
216 printk(KERN_ERR "r3964: error registering line discipline: "
217 "%d\n", status);
218 }
219 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222module_init(r3964_init);
223module_exit(r3964_exit);
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*************************************************************
226 * Protocol implementation routines
227 *************************************************************/
228
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800229static void add_tx_queue(struct r3964_info *pInfo,
230 struct r3964_block_header *pHeader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800232 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800234 spin_lock_irqsave(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800236 pHeader->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800238 if (pInfo->tx_last == NULL) {
239 pInfo->tx_first = pInfo->tx_last = pHeader;
240 } else {
241 pInfo->tx_last->next = pHeader;
242 pInfo->tx_last = pHeader;
243 }
244
245 spin_unlock_irqrestore(&pInfo->lock, flags);
246
247 TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
248 pHeader, pHeader->length, pInfo->tx_first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
252{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800253 struct r3964_block_header *pHeader;
254 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#ifdef DEBUG_QUEUE
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800256 struct r3964_block_header *pDump;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#endif
258
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800259 pHeader = pInfo->tx_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800261 if (pHeader == NULL)
262 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800264#ifdef DEBUG_QUEUE
265 printk("r3964: remove_from_tx_queue: %p, length %u - ",
266 pHeader, pHeader->length);
267 for (pDump = pHeader; pDump; pDump = pDump->next)
268 printk("%p ", pDump);
269 printk("\n");
270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800272 if (pHeader->owner) {
273 if (error_code) {
274 add_msg(pHeader->owner, R3964_MSG_ACK, 0,
275 error_code, NULL);
276 } else {
277 add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
278 error_code, NULL);
279 }
280 wake_up_interruptible(&pInfo->read_wait);
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800283 spin_lock_irqsave(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800285 pInfo->tx_first = pHeader->next;
286 if (pInfo->tx_first == NULL) {
287 pInfo->tx_last = NULL;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800290 spin_unlock_irqrestore(&pInfo->lock, flags);
291
292 kfree(pHeader);
293 TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
294
295 TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
296 pInfo->tx_first, pInfo->tx_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800299static void add_rx_queue(struct r3964_info *pInfo,
300 struct r3964_block_header *pHeader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800302 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800304 spin_lock_irqsave(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800306 pHeader->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800308 if (pInfo->rx_last == NULL) {
309 pInfo->rx_first = pInfo->rx_last = pHeader;
310 } else {
311 pInfo->rx_last->next = pHeader;
312 pInfo->rx_last = pHeader;
313 }
314 pInfo->blocks_in_rx_queue++;
315
316 spin_unlock_irqrestore(&pInfo->lock, flags);
317
318 TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
319 pHeader, pHeader->length,
320 pInfo->rx_first, pInfo->blocks_in_rx_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323static void remove_from_rx_queue(struct r3964_info *pInfo,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800324 struct r3964_block_header *pHeader)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800326 unsigned long flags;
327 struct r3964_block_header *pFind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800329 if (pHeader == NULL)
330 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800332 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
333 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
334 TRACE_Q("remove_from_rx_queue: %p, length %u",
335 pHeader, pHeader->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800337 spin_lock_irqsave(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800339 if (pInfo->rx_first == pHeader) {
340 /* Remove the first block in the linked list: */
341 pInfo->rx_first = pHeader->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800343 if (pInfo->rx_first == NULL) {
344 pInfo->rx_last = NULL;
345 }
346 pInfo->blocks_in_rx_queue--;
347 } else {
348 /* Find block to remove: */
349 for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
350 if (pFind->next == pHeader) {
351 /* Got it. */
352 pFind->next = pHeader->next;
353 pInfo->blocks_in_rx_queue--;
354 if (pFind->next == NULL) {
355 /* Oh, removed the last one! */
356 pInfo->rx_last = pFind;
357 }
358 break;
359 }
360 }
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800363 spin_unlock_irqrestore(&pInfo->lock, flags);
364
365 kfree(pHeader);
366 TRACE_M("remove_from_rx_queue - kfree %p", pHeader);
367
368 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
369 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static void put_char(struct r3964_info *pInfo, unsigned char ch)
373{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800374 struct tty_struct *tty = pInfo->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800376 if (tty == NULL)
377 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800379 if (tty->driver->put_char) {
380 tty->driver->put_char(tty, ch);
381 }
382 pInfo->bcc ^= ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385static void flush(struct r3964_info *pInfo)
386{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800387 struct tty_struct *tty = pInfo->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800389 if (tty == NULL)
390 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800392 if (tty->driver->flush_chars) {
393 tty->driver->flush_chars(tty);
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397static void trigger_transmit(struct r3964_info *pInfo)
398{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800399 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800401 spin_lock_irqsave(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800403 if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
404 pInfo->state = R3964_TX_REQUEST;
405 pInfo->nRetry = 0;
406 pInfo->flags &= ~R3964_ERROR;
407 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800409 spin_unlock_irqrestore(&pInfo->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800411 TRACE_PS("trigger_transmit - sent STX");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800413 put_char(pInfo, STX);
414 flush(pInfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800416 pInfo->bcc = 0;
417 } else {
418 spin_unlock_irqrestore(&pInfo->lock, flags);
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422static void retry_transmit(struct r3964_info *pInfo)
423{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800424 if (pInfo->nRetry < R3964_MAX_RETRIES) {
425 TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
426 pInfo->bcc = 0;
427 put_char(pInfo, STX);
428 flush(pInfo);
429 pInfo->state = R3964_TX_REQUEST;
430 pInfo->nRetry++;
431 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
432 } else {
433 TRACE_PE("transmission failed after %d retries",
434 R3964_MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800436 remove_from_tx_queue(pInfo, R3964_TX_FAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800438 put_char(pInfo, NAK);
439 flush(pInfo);
440 pInfo->state = R3964_IDLE;
441
442 trigger_transmit(pInfo);
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446static void transmit_block(struct r3964_info *pInfo)
447{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800448 struct tty_struct *tty = pInfo->tty;
449 struct r3964_block_header *pBlock = pInfo->tx_first;
450 int room = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800452 if ((tty == NULL) || (pBlock == NULL)) {
453 return;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800456 if (tty->driver->write_room)
457 room = tty->driver->write_room(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800459 TRACE_PS("transmit_block %p, room %d, length %d",
460 pBlock, room, pBlock->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800462 while (pInfo->tx_position < pBlock->length) {
463 if (room < 2)
464 break;
465
466 if (pBlock->data[pInfo->tx_position] == DLE) {
467 /* send additional DLE char: */
468 put_char(pInfo, DLE);
469 }
470 put_char(pInfo, pBlock->data[pInfo->tx_position++]);
471
472 room--;
473 }
474
475 if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
476 put_char(pInfo, DLE);
477 put_char(pInfo, ETX);
478 if (pInfo->flags & R3964_BCC) {
479 put_char(pInfo, pInfo->bcc);
480 }
481 pInfo->state = R3964_WAIT_FOR_TX_ACK;
482 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
483 }
484 flush(pInfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487static void on_receive_block(struct r3964_info *pInfo)
488{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800489 unsigned int length;
490 struct r3964_client_info *pClient;
491 struct r3964_block_header *pBlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800493 length = pInfo->rx_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800495 /* compare byte checksum characters: */
496 if (pInfo->flags & R3964_BCC) {
497 if (pInfo->bcc != pInfo->last_rx) {
498 TRACE_PE("checksum error - got %x but expected %x",
499 pInfo->last_rx, pInfo->bcc);
500 pInfo->flags |= R3964_CHECKSUM;
501 }
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800504 /* check for errors (parity, overrun,...): */
505 if (pInfo->flags & R3964_ERROR) {
506 TRACE_PE("on_receive_block - transmission failed error %x",
507 pInfo->flags & R3964_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800509 put_char(pInfo, NAK);
510 flush(pInfo);
511 if (pInfo->nRetry < R3964_MAX_RETRIES) {
512 pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
513 pInfo->nRetry++;
514 mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
515 } else {
516 TRACE_PE("on_receive_block - failed after max retries");
517 pInfo->state = R3964_IDLE;
518 }
519 return;
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800522 /* received block; submit DLE: */
523 put_char(pInfo, DLE);
524 flush(pInfo);
525 del_timer_sync(&pInfo->tmr);
526 TRACE_PS(" rx success: got %d chars", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800528 /* prepare struct r3964_block_header: */
529 pBlock = kmalloc(length + sizeof(struct r3964_block_header),
530 GFP_KERNEL);
531 TRACE_M("on_receive_block - kmalloc %p", pBlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800533 if (pBlock == NULL)
534 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800536 pBlock->length = length;
537 pBlock->data = ((unsigned char *)pBlock) +
538 sizeof(struct r3964_block_header);
539 pBlock->locks = 0;
540 pBlock->next = NULL;
541 pBlock->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800543 memcpy(pBlock->data, pInfo->rx_buf, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800545 /* queue block into rx_queue: */
546 add_rx_queue(pInfo, pBlock);
547
548 /* notify attached client processes: */
549 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
550 if (pClient->sig_flags & R3964_SIG_DATA) {
551 add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
552 pBlock);
553 }
554 }
555 wake_up_interruptible(&pInfo->read_wait);
556
557 pInfo->state = R3964_IDLE;
558
559 trigger_transmit(pInfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562static void receive_char(struct r3964_info *pInfo, const unsigned char c)
563{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800564 switch (pInfo->state) {
565 case R3964_TX_REQUEST:
566 if (c == DLE) {
567 TRACE_PS("TX_REQUEST - got DLE");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800569 pInfo->state = R3964_TRANSMITTING;
570 pInfo->tx_position = 0;
571
572 transmit_block(pInfo);
573 } else if (c == STX) {
574 if (pInfo->nRetry == 0) {
575 TRACE_PE("TX_REQUEST - init conflict");
576 if (pInfo->priority == R3964_SLAVE) {
577 goto start_receiving;
578 }
579 } else {
580 TRACE_PE("TX_REQUEST - secondary init "
581 "conflict!? Switching to SLAVE mode "
582 "for next rx.");
583 goto start_receiving;
584 }
585 } else {
586 TRACE_PE("TX_REQUEST - char != DLE: %x", c);
587 retry_transmit(pInfo);
588 }
589 break;
590 case R3964_TRANSMITTING:
591 if (c == NAK) {
592 TRACE_PE("TRANSMITTING - got NAK");
593 retry_transmit(pInfo);
594 } else {
595 TRACE_PE("TRANSMITTING - got invalid char");
596
597 pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
598 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
599 }
600 break;
601 case R3964_WAIT_FOR_TX_ACK:
602 if (c == DLE) {
603 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
604 remove_from_tx_queue(pInfo, R3964_OK);
605
606 pInfo->state = R3964_IDLE;
607 trigger_transmit(pInfo);
608 } else {
609 retry_transmit(pInfo);
610 }
611 break;
612 case R3964_WAIT_FOR_RX_REPEAT:
613 /* FALLTROUGH */
614 case R3964_IDLE:
615 if (c == STX) {
616 /* Prevent rx_queue from overflow: */
617 if (pInfo->blocks_in_rx_queue >=
618 R3964_MAX_BLOCKS_IN_RX_QUEUE) {
619 TRACE_PE("IDLE - got STX but no space in "
620 "rx_queue!");
621 pInfo->state = R3964_WAIT_FOR_RX_BUF;
622 mod_timer(&pInfo->tmr,
623 jiffies + R3964_TO_NO_BUF);
624 break;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626start_receiving:
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800627 /* Ok, start receiving: */
628 TRACE_PS("IDLE - got STX");
629 pInfo->rx_position = 0;
630 pInfo->last_rx = 0;
631 pInfo->flags &= ~R3964_ERROR;
632 pInfo->state = R3964_RECEIVING;
633 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
634 pInfo->nRetry = 0;
635 put_char(pInfo, DLE);
636 flush(pInfo);
637 pInfo->bcc = 0;
638 }
639 break;
640 case R3964_RECEIVING:
641 if (pInfo->rx_position < RX_BUF_SIZE) {
642 pInfo->bcc ^= c;
643
644 if (c == DLE) {
645 if (pInfo->last_rx == DLE) {
646 pInfo->last_rx = 0;
647 goto char_to_buf;
648 }
649 pInfo->last_rx = DLE;
650 break;
651 } else if ((c == ETX) && (pInfo->last_rx == DLE)) {
652 if (pInfo->flags & R3964_BCC) {
653 pInfo->state = R3964_WAIT_FOR_BCC;
654 mod_timer(&pInfo->tmr,
655 jiffies + R3964_TO_ZVZ);
656 } else {
657 on_receive_block(pInfo);
658 }
659 } else {
660 pInfo->last_rx = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661char_to_buf:
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800662 pInfo->rx_buf[pInfo->rx_position++] = c;
663 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
664 }
665 }
666 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
667 break;
668 case R3964_WAIT_FOR_BCC:
669 pInfo->last_rx = c;
670 on_receive_block(pInfo);
671 break;
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675static void receive_error(struct r3964_info *pInfo, const char flag)
676{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800677 switch (flag) {
678 case TTY_NORMAL:
679 break;
680 case TTY_BREAK:
681 TRACE_PE("received break");
682 pInfo->flags |= R3964_BREAK;
683 break;
684 case TTY_PARITY:
685 TRACE_PE("parity error");
686 pInfo->flags |= R3964_PARITY;
687 break;
688 case TTY_FRAME:
689 TRACE_PE("frame error");
690 pInfo->flags |= R3964_FRAME;
691 break;
692 case TTY_OVERRUN:
693 TRACE_PE("frame overrun");
694 pInfo->flags |= R3964_OVERRUN;
695 break;
696 default:
697 TRACE_PE("receive_error - unknown flag %d", flag);
698 pInfo->flags |= R3964_UNKNOWN;
699 break;
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703static void on_timeout(unsigned long priv)
704{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800705 struct r3964_info *pInfo = (void *)priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800707 switch (pInfo->state) {
708 case R3964_TX_REQUEST:
709 TRACE_PE("TX_REQUEST - timeout");
710 retry_transmit(pInfo);
711 break;
712 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
713 put_char(pInfo, NAK);
714 flush(pInfo);
715 retry_transmit(pInfo);
716 break;
717 case R3964_WAIT_FOR_TX_ACK:
718 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
719 retry_transmit(pInfo);
720 break;
721 case R3964_WAIT_FOR_RX_BUF:
722 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
723 put_char(pInfo, NAK);
724 flush(pInfo);
725 pInfo->state = R3964_IDLE;
726 break;
727 case R3964_RECEIVING:
728 TRACE_PE("RECEIVING - timeout after %d chars",
729 pInfo->rx_position);
730 put_char(pInfo, NAK);
731 flush(pInfo);
732 pInfo->state = R3964_IDLE;
733 break;
734 case R3964_WAIT_FOR_RX_REPEAT:
735 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
736 pInfo->state = R3964_IDLE;
737 break;
738 case R3964_WAIT_FOR_BCC:
739 TRACE_PE("WAIT_FOR_BCC - timeout");
740 put_char(pInfo, NAK);
741 flush(pInfo);
742 pInfo->state = R3964_IDLE;
743 break;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800747static struct r3964_client_info *findClient(struct r3964_info *pInfo,
748 struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800750 struct r3964_client_info *pClient;
751
752 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
753 if (pClient->pid == pid) {
754 return pClient;
755 }
756 }
757 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Eric W. Biederman3cec5562006-12-13 00:35:10 -0800760static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800762 struct r3964_client_info *pClient;
763 struct r3964_client_info **ppClient;
764 struct r3964_message *pMsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800766 if ((arg & R3964_SIG_ALL) == 0) {
767 /* Remove client from client list */
768 for (ppClient = &pInfo->firstClient; *ppClient;
769 ppClient = &(*ppClient)->next) {
770 pClient = *ppClient;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800772 if (pClient->pid == pid) {
773 TRACE_PS("removing client %d from client list",
774 pid_nr(pid));
775 *ppClient = pClient->next;
776 while (pClient->msg_count) {
777 pMsg = remove_msg(pInfo, pClient);
778 if (pMsg) {
779 kfree(pMsg);
780 TRACE_M("enable_signals - msg "
781 "kfree %p", pMsg);
782 }
783 }
784 put_pid(pClient->pid);
785 kfree(pClient);
786 TRACE_M("enable_signals - kfree %p", pClient);
787 return 0;
788 }
789 }
790 return -EINVAL;
791 } else {
792 pClient = findClient(pInfo, pid);
793 if (pClient) {
794 /* update signal options */
795 pClient->sig_flags = arg;
796 } else {
797 /* add client to client list */
798 pClient = kmalloc(sizeof(struct r3964_client_info),
799 GFP_KERNEL);
800 TRACE_M("enable_signals - kmalloc %p", pClient);
801 if (pClient == NULL)
802 return -ENOMEM;
803
804 TRACE_PS("add client %d to client list", pid_nr(pid));
805 spin_lock_init(&pClient->lock);
806 pClient->sig_flags = arg;
807 pClient->pid = get_pid(pid);
808 pClient->next = pInfo->firstClient;
809 pClient->first_msg = NULL;
810 pClient->last_msg = NULL;
811 pClient->next_block_to_read = NULL;
812 pClient->msg_count = 0;
813 pInfo->firstClient = pClient;
814 }
815 }
816
817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800820static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
821 unsigned char __user * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800823 struct r3964_client_info *pClient;
824 struct r3964_block_header *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800826 if (!buf) {
827 return -EINVAL;
828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800830 pClient = findClient(pInfo, pid);
831 if (pClient == NULL) {
832 return -EINVAL;
833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800835 block = pClient->next_block_to_read;
836 if (!block) {
837 return 0;
838 } else {
839 if (copy_to_user(buf, block->data, block->length))
840 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800842 remove_client_block(pInfo, pClient);
843 return block->length;
844 }
845
846 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800850 int error_code, struct r3964_block_header *pBlock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800852 struct r3964_message *pMsg;
853 unsigned long flags;
854
855 if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856queue_the_message:
857
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800858 pMsg = kmalloc(sizeof(struct r3964_message),
859 error_code ? GFP_ATOMIC : GFP_KERNEL);
860 TRACE_M("add_msg - kmalloc %p", pMsg);
861 if (pMsg == NULL) {
862 return;
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800865 spin_lock_irqsave(&pClient->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800867 pMsg->msg_id = msg_id;
868 pMsg->arg = arg;
869 pMsg->error_code = error_code;
870 pMsg->block = pBlock;
871 pMsg->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800873 if (pClient->last_msg == NULL) {
874 pClient->first_msg = pClient->last_msg = pMsg;
875 } else {
876 pClient->last_msg->next = pMsg;
877 pClient->last_msg = pMsg;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800880 pClient->msg_count++;
881
882 if (pBlock != NULL) {
883 pBlock->locks++;
884 }
885 spin_unlock_irqrestore(&pClient->lock, flags);
886 } else {
887 if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
888 && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
889 pClient->last_msg->arg++;
890 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
891 } else {
892 msg_id = R3964_MSG_ACK;
893 arg = 0;
894 error_code = R3964_OVERFLOW;
895 pBlock = NULL;
896 TRACE_PE("add_msg - queue OVERFLOW-msg");
897 goto queue_the_message;
898 }
899 }
900 /* Send SIGIO signal to client process: */
901 if (pClient->sig_flags & R3964_USE_SIGIO) {
902 kill_pid(pClient->pid, SIGIO, 1);
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906static struct r3964_message *remove_msg(struct r3964_info *pInfo,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800907 struct r3964_client_info *pClient)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800909 struct r3964_message *pMsg = NULL;
910 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800912 if (pClient->first_msg) {
913 spin_lock_irqsave(&pClient->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800915 pMsg = pClient->first_msg;
916 pClient->first_msg = pMsg->next;
917 if (pClient->first_msg == NULL) {
918 pClient->last_msg = NULL;
919 }
920
921 pClient->msg_count--;
922 if (pMsg->block) {
923 remove_client_block(pInfo, pClient);
924 pClient->next_block_to_read = pMsg->block;
925 }
926 spin_unlock_irqrestore(&pClient->lock, flags);
927 }
928 return pMsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800931static void remove_client_block(struct r3964_info *pInfo,
932 struct r3964_client_info *pClient)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800934 struct r3964_block_header *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800936 TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800938 block = pClient->next_block_to_read;
939 if (block) {
940 block->locks--;
941 if (block->locks == 0) {
942 remove_from_rx_queue(pInfo, block);
943 }
944 }
945 pClient->next_block_to_read = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948/*************************************************************
949 * Line discipline routines
950 *************************************************************/
951
952static int r3964_open(struct tty_struct *tty)
953{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800954 struct r3964_info *pInfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800956 TRACE_L("open");
957 TRACE_L("tty=%p, PID=%d, disc_data=%p",
958 tty, current->pid, tty->disc_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800960 pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
961 TRACE_M("r3964_open - info kmalloc %p", pInfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800963 if (!pInfo) {
964 printk(KERN_ERR "r3964: failed to alloc info structure\n");
965 return -ENOMEM;
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800968 pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
969 TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800971 if (!pInfo->rx_buf) {
972 printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
973 kfree(pInfo);
974 TRACE_M("r3964_open - info kfree %p", pInfo);
975 return -ENOMEM;
976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800978 pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
979 TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Jiri Slaby5e07e1c2007-02-10 01:45:09 -0800981 if (!pInfo->tx_buf) {
982 printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
983 kfree(pInfo->rx_buf);
984 TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
985 kfree(pInfo);
986 TRACE_M("r3964_open - info kfree %p", pInfo);
987 return -ENOMEM;
988 }
989
990 spin_lock_init(&pInfo->lock);
991 pInfo->tty = tty;
992 init_waitqueue_head(&pInfo->read_wait);
993 pInfo->priority = R3964_MASTER;
994 pInfo->rx_first = pInfo->rx_last = NULL;
995 pInfo->tx_first = pInfo->tx_last = NULL;
996 pInfo->rx_position = 0;
997 pInfo->tx_position = 0;
998 pInfo->last_rx = 0;
999 pInfo->blocks_in_rx_queue = 0;
1000 pInfo->firstClient = NULL;
1001 pInfo->state = R3964_IDLE;
1002 pInfo->flags = R3964_DEBUG;
1003 pInfo->nRetry = 0;
1004
1005 tty->disc_data = pInfo;
1006 tty->receive_room = 65536;
1007
Jiri Slaby40565f12007-02-12 00:52:31 -08001008 setup_timer(&pInfo->tmr, on_timeout, (unsigned long)pInfo);
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001009
1010 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013static void r3964_close(struct tty_struct *tty)
1014{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001015 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1016 struct r3964_client_info *pClient, *pNext;
1017 struct r3964_message *pMsg;
1018 struct r3964_block_header *pHeader, *pNextHeader;
1019 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001021 TRACE_L("close");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001023 /*
1024 * Make sure that our task queue isn't activated. If it
1025 * is, take it out of the linked list.
1026 */
1027 del_timer_sync(&pInfo->tmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001029 /* Remove client-structs and message queues: */
1030 pClient = pInfo->firstClient;
1031 while (pClient) {
1032 pNext = pClient->next;
1033 while (pClient->msg_count) {
1034 pMsg = remove_msg(pInfo, pClient);
1035 if (pMsg) {
1036 kfree(pMsg);
1037 TRACE_M("r3964_close - msg kfree %p", pMsg);
1038 }
1039 }
1040 put_pid(pClient->pid);
1041 kfree(pClient);
1042 TRACE_M("r3964_close - client kfree %p", pClient);
1043 pClient = pNext;
1044 }
1045 /* Remove jobs from tx_queue: */
1046 spin_lock_irqsave(&pInfo->lock, flags);
1047 pHeader = pInfo->tx_first;
1048 pInfo->tx_first = pInfo->tx_last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 spin_unlock_irqrestore(&pInfo->lock, flags);
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001050
1051 while (pHeader) {
1052 pNextHeader = pHeader->next;
1053 kfree(pHeader);
1054 pHeader = pNextHeader;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001057 /* Free buffers: */
1058 wake_up_interruptible(&pInfo->read_wait);
1059 kfree(pInfo->rx_buf);
1060 TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
1061 kfree(pInfo->tx_buf);
1062 TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
1063 kfree(pInfo);
1064 TRACE_M("r3964_close - info kfree %p", pInfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001068 unsigned char __user * buf, size_t nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001070 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1071 struct r3964_client_info *pClient;
1072 struct r3964_message *pMsg;
1073 struct r3964_client_message theMsg;
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001074 int count;
1075
1076 TRACE_L("read()");
1077
1078 pClient = findClient(pInfo, task_pid(current));
1079 if (pClient) {
1080 pMsg = remove_msg(pInfo, pClient);
1081 if (pMsg == NULL) {
1082 /* no messages available. */
1083 if (file->f_flags & O_NONBLOCK) {
1084 return -EAGAIN;
1085 }
1086 /* block until there is a message: */
Jiri Slaby6defec12007-07-15 23:40:20 -07001087 wait_event_interruptible(pInfo->read_wait,
1088 (pMsg = remove_msg(pInfo, pClient)));
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001091 /* If we still haven't got a message, we must have been signalled */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001093 if (!pMsg)
1094 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001096 /* deliver msg to client process: */
1097 theMsg.msg_id = pMsg->msg_id;
1098 theMsg.arg = pMsg->arg;
1099 theMsg.error_code = pMsg->error_code;
1100 count = sizeof(struct r3964_client_message);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001102 kfree(pMsg);
1103 TRACE_M("r3964_read - msg kfree %p", pMsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001105 if (copy_to_user(buf, &theMsg, count))
1106 return -EFAULT;
1107
1108 TRACE_PS("read - return %d", count);
1109 return count;
1110 }
1111 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001114static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 const unsigned char *data, size_t count)
1116{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001117 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1118 struct r3964_block_header *pHeader;
1119 struct r3964_client_info *pClient;
1120 unsigned char *new_data;
1121
1122 TRACE_L("write request, %d characters", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123/*
1124 * Verify the pointers
1125 */
1126
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001127 if (!pInfo)
1128 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130/*
1131 * Ensure that the caller does not wish to send too much.
1132 */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001133 if (count > R3964_MTU) {
1134 if (pInfo->flags & R3964_DEBUG) {
1135 TRACE_L(KERN_WARNING "r3964_write: truncating user "
1136 "packet from %u to mtu %d", count, R3964_MTU);
1137 }
1138 count = R3964_MTU;
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/*
1141 * Allocate a buffer for the data and copy it from the buffer with header prepended
1142 */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001143 new_data = kmalloc(count + sizeof(struct r3964_block_header),
1144 GFP_KERNEL);
1145 TRACE_M("r3964_write - kmalloc %p", new_data);
1146 if (new_data == NULL) {
1147 if (pInfo->flags & R3964_DEBUG) {
1148 printk(KERN_ERR "r3964_write: no memory\n");
1149 }
1150 return -ENOSPC;
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001153 pHeader = (struct r3964_block_header *)new_data;
1154 pHeader->data = new_data + sizeof(struct r3964_block_header);
1155 pHeader->length = count;
1156 pHeader->locks = 0;
1157 pHeader->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001159 pClient = findClient(pInfo, task_pid(current));
1160 if (pClient) {
1161 pHeader->owner = pClient;
1162 }
1163
1164 memcpy(pHeader->data, data, count); /* We already verified this */
1165
1166 if (pInfo->flags & R3964_DEBUG) {
1167 dump_block(pHeader->data, count);
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170/*
1171 * Add buffer to transmit-queue:
1172 */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001173 add_tx_queue(pInfo, pHeader);
1174 trigger_transmit(pInfo);
1175
1176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001179static int r3964_ioctl(struct tty_struct *tty, struct file *file,
1180 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001182 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1183 if (pInfo == NULL)
1184 return -EINVAL;
1185 switch (cmd) {
1186 case R3964_ENABLE_SIGNALS:
1187 return enable_signals(pInfo, task_pid(current), arg);
1188 case R3964_SETPRIORITY:
1189 if (arg < R3964_MASTER || arg > R3964_SLAVE)
1190 return -EINVAL;
1191 pInfo->priority = arg & 0xff;
1192 return 0;
1193 case R3964_USE_BCC:
1194 if (arg)
1195 pInfo->flags |= R3964_BCC;
1196 else
1197 pInfo->flags &= ~R3964_BCC;
1198 return 0;
1199 case R3964_READ_TELEGRAM:
1200 return read_telegram(pInfo, task_pid(current),
1201 (unsigned char __user *)arg);
1202 default:
1203 return -ENOIOCTLCMD;
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001207static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001209 TRACE_L("set_termios");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
1212/* Called without the kernel lock held - fine */
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001213static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
1214 struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001216 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1217 struct r3964_client_info *pClient;
1218 struct r3964_message *pMsg = NULL;
1219 unsigned long flags;
1220 int result = POLLOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001222 TRACE_L("POLL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001224 pClient = findClient(pInfo, task_pid(current));
1225 if (pClient) {
1226 poll_wait(file, &pInfo->read_wait, wait);
1227 spin_lock_irqsave(&pInfo->lock, flags);
1228 pMsg = pClient->first_msg;
1229 spin_unlock_irqrestore(&pInfo->lock, flags);
1230 if (pMsg)
1231 result |= POLLIN | POLLRDNORM;
1232 } else {
1233 result = -EINVAL;
1234 }
1235 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001239 char *fp, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001241 struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
1242 const unsigned char *p;
1243 char *f, flags = 0;
1244 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Jiri Slaby5e07e1c2007-02-10 01:45:09 -08001246 for (i = count, p = cp, f = fp; i; i--, p++) {
1247 if (f)
1248 flags = *f++;
1249 if (flags == TTY_NORMAL) {
1250 receive_char(pInfo, *p);
1251 } else {
1252 receive_error(pInfo, flags);
1253 }
1254
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258MODULE_LICENSE("GPL");
1259MODULE_ALIAS_LDISC(N_R3964);