Full support for Ginger Console
[linux-ginger.git] / net / irda / irnet / irnet_ppp.c
blob7dea882dbb750649b10616f00dcb9bb68697ec64
1 /*
2 * IrNET protocol module : Synchronous PPP over an IrDA socket.
4 * Jean II - HPL `00 - <jt@hpl.hp.com>
6 * This file implement the PPP interface and /dev/irnet character device.
7 * The PPP interface hook to the ppp_generic module, handle all our
8 * relationship to the PPP code in the kernel (and by extension to pppd),
9 * and exchange PPP frames with this module (send/receive).
10 * The /dev/irnet device is used primarily for 2 functions :
11 * 1) as a stub for pppd (the ppp daemon), so that we can appropriately
12 * generate PPP sessions (we pretend we are a tty).
13 * 2) as a control channel (write commands, read events)
16 #include <linux/sched.h>
17 #include <linux/smp_lock.h>
18 #include "irnet_ppp.h" /* Private header */
19 /* Please put other headers in irnet.h - Thanks */
21 /* Generic PPP callbacks (to call us) */
22 static struct ppp_channel_ops irnet_ppp_ops = {
23 .start_xmit = ppp_irnet_send,
24 .ioctl = ppp_irnet_ioctl
27 /************************* CONTROL CHANNEL *************************/
29 * When a pppd instance is not active on /dev/irnet, it acts as a control
30 * channel.
31 * Writing allow to set up the IrDA destination of the IrNET channel,
32 * and any application may be read events happening in IrNET...
35 /*------------------------------------------------------------------*/
37 * Write is used to send a command to configure a IrNET channel
38 * before it is open by pppd. The syntax is : "command argument"
39 * Currently there is only two defined commands :
40 * o name : set the requested IrDA nickname of the IrNET peer.
41 * o addr : set the requested IrDA address of the IrNET peer.
42 * Note : the code is crude, but effective...
44 static inline ssize_t
45 irnet_ctrl_write(irnet_socket * ap,
46 const char __user *buf,
47 size_t count)
49 char command[IRNET_MAX_COMMAND];
50 char * start; /* Current command being processed */
51 char * next; /* Next command to process */
52 int length; /* Length of current command */
54 DENTER(CTRL_TRACE, "(ap=0x%p, count=%Zd)\n", ap, count);
56 /* Check for overflow... */
57 DABORT(count >= IRNET_MAX_COMMAND, -ENOMEM,
58 CTRL_ERROR, "Too much data !!!\n");
60 /* Get the data in the driver */
61 if(copy_from_user(command, buf, count))
63 DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
64 return -EFAULT;
67 /* Safe terminate the string */
68 command[count] = '\0';
69 DEBUG(CTRL_INFO, "Command line received is ``%s'' (%Zd).\n",
70 command, count);
72 /* Check every commands in the command line */
73 next = command;
74 while(next != NULL)
76 /* Look at the next command */
77 start = next;
79 /* Scrap whitespaces before the command */
80 while(isspace(*start))
81 start++;
83 /* ',' is our command separator */
84 next = strchr(start, ',');
85 if(next)
87 *next = '\0'; /* Terminate command */
88 length = next - start; /* Length */
89 next++; /* Skip the '\0' */
91 else
92 length = strlen(start);
94 DEBUG(CTRL_INFO, "Found command ``%s'' (%d).\n", start, length);
96 /* Check if we recognised one of the known command
97 * We can't use "switch" with strings, so hack with "continue" */
99 /* First command : name -> Requested IrDA nickname */
100 if(!strncmp(start, "name", 4))
102 /* Copy the name only if is included and not "any" */
103 if((length > 5) && (strcmp(start + 5, "any")))
105 /* Strip out trailing whitespaces */
106 while(isspace(start[length - 1]))
107 length--;
109 /* Copy the name for later reuse */
110 memcpy(ap->rname, start + 5, length - 5);
111 ap->rname[length - 5] = '\0';
113 else
114 ap->rname[0] = '\0';
115 DEBUG(CTRL_INFO, "Got rname = ``%s''\n", ap->rname);
117 /* Restart the loop */
118 continue;
121 /* Second command : addr, daddr -> Requested IrDA destination address
122 * Also process : saddr -> Requested IrDA source address */
123 if((!strncmp(start, "addr", 4)) ||
124 (!strncmp(start, "daddr", 5)) ||
125 (!strncmp(start, "saddr", 5)))
127 __u32 addr = DEV_ADDR_ANY;
129 /* Copy the address only if is included and not "any" */
130 if((length > 5) && (strcmp(start + 5, "any")))
132 char * begp = start + 5;
133 char * endp;
135 /* Scrap whitespaces before the command */
136 while(isspace(*begp))
137 begp++;
139 /* Convert argument to a number (last arg is the base) */
140 addr = simple_strtoul(begp, &endp, 16);
141 /* Has it worked ? (endp should be start + length) */
142 DABORT(endp <= (start + 5), -EINVAL,
143 CTRL_ERROR, "Invalid address.\n");
145 /* Which type of address ? */
146 if(start[0] == 's')
148 /* Save it */
149 ap->rsaddr = addr;
150 DEBUG(CTRL_INFO, "Got rsaddr = %08x\n", ap->rsaddr);
152 else
154 /* Save it */
155 ap->rdaddr = addr;
156 DEBUG(CTRL_INFO, "Got rdaddr = %08x\n", ap->rdaddr);
159 /* Restart the loop */
160 continue;
163 /* Other possible command : connect N (number of retries) */
165 /* No command matched -> Failed... */
166 DABORT(1, -EINVAL, CTRL_ERROR, "Not a recognised IrNET command.\n");
169 /* Success : we have parsed all commands successfully */
170 return(count);
173 #ifdef INITIAL_DISCOVERY
174 /*------------------------------------------------------------------*/
176 * Function irnet_get_discovery_log (self)
178 * Query the content on the discovery log if not done
180 * This function query the current content of the discovery log
181 * at the startup of the event channel and save it in the internal struct.
183 static void
184 irnet_get_discovery_log(irnet_socket * ap)
186 __u16 mask = irlmp_service_to_hint(S_LAN);
188 /* Ask IrLMP for the current discovery log */
189 ap->discoveries = irlmp_get_discoveries(&ap->disco_number, mask,
190 DISCOVERY_DEFAULT_SLOTS);
192 /* Check if the we got some results */
193 if(ap->discoveries == NULL)
194 ap->disco_number = -1;
196 DEBUG(CTRL_INFO, "Got the log (0x%p), size is %d\n",
197 ap->discoveries, ap->disco_number);
200 /*------------------------------------------------------------------*/
202 * Function irnet_read_discovery_log (self, event)
204 * Read the content on the discovery log
206 * This function dump the current content of the discovery log
207 * at the startup of the event channel.
208 * Return 1 if wrote an event on the control channel...
210 * State of the ap->disco_XXX variables :
211 * Socket creation : discoveries = NULL ; disco_index = 0 ; disco_number = 0
212 * While reading : discoveries = ptr ; disco_index = X ; disco_number = Y
213 * After reading : discoveries = NULL ; disco_index = Y ; disco_number = -1
215 static inline int
216 irnet_read_discovery_log(irnet_socket * ap,
217 char * event)
219 int done_event = 0;
221 DENTER(CTRL_TRACE, "(ap=0x%p, event=0x%p)\n",
222 ap, event);
224 /* Test if we have some work to do or we have already finished */
225 if(ap->disco_number == -1)
227 DEBUG(CTRL_INFO, "Already done\n");
228 return 0;
231 /* Test if it's the first time and therefore we need to get the log */
232 if(ap->discoveries == NULL)
233 irnet_get_discovery_log(ap);
235 /* Check if we have more item to dump */
236 if(ap->disco_index < ap->disco_number)
238 /* Write an event */
239 sprintf(event, "Found %08x (%s) behind %08x {hints %02X-%02X}\n",
240 ap->discoveries[ap->disco_index].daddr,
241 ap->discoveries[ap->disco_index].info,
242 ap->discoveries[ap->disco_index].saddr,
243 ap->discoveries[ap->disco_index].hints[0],
244 ap->discoveries[ap->disco_index].hints[1]);
245 DEBUG(CTRL_INFO, "Writing discovery %d : %s\n",
246 ap->disco_index, ap->discoveries[ap->disco_index].info);
248 /* We have an event */
249 done_event = 1;
250 /* Next discovery */
251 ap->disco_index++;
254 /* Check if we have done the last item */
255 if(ap->disco_index >= ap->disco_number)
257 /* No more items : remove the log and signal termination */
258 DEBUG(CTRL_INFO, "Cleaning up log (0x%p)\n",
259 ap->discoveries);
260 if(ap->discoveries != NULL)
262 /* Cleanup our copy of the discovery log */
263 kfree(ap->discoveries);
264 ap->discoveries = NULL;
266 ap->disco_number = -1;
269 return done_event;
271 #endif /* INITIAL_DISCOVERY */
273 /*------------------------------------------------------------------*/
275 * Read is used to get IrNET events
277 static inline ssize_t
278 irnet_ctrl_read(irnet_socket * ap,
279 struct file * file,
280 char __user * buf,
281 size_t count)
283 DECLARE_WAITQUEUE(wait, current);
284 char event[64]; /* Max event is 61 char */
285 ssize_t ret = 0;
287 DENTER(CTRL_TRACE, "(ap=0x%p, count=%Zd)\n", ap, count);
289 /* Check if we can write an event out in one go */
290 DABORT(count < sizeof(event), -EOVERFLOW, CTRL_ERROR, "Buffer to small.\n");
292 #ifdef INITIAL_DISCOVERY
293 /* Check if we have read the log */
294 if(irnet_read_discovery_log(ap, event))
296 /* We have an event !!! Copy it to the user */
297 if(copy_to_user(buf, event, strlen(event)))
299 DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
300 return -EFAULT;
303 DEXIT(CTRL_TRACE, "\n");
304 return(strlen(event));
306 #endif /* INITIAL_DISCOVERY */
308 /* Put ourselves on the wait queue to be woken up */
309 add_wait_queue(&irnet_events.rwait, &wait);
310 current->state = TASK_INTERRUPTIBLE;
311 for(;;)
313 /* If there is unread events */
314 ret = 0;
315 if(ap->event_index != irnet_events.index)
316 break;
317 ret = -EAGAIN;
318 if(file->f_flags & O_NONBLOCK)
319 break;
320 ret = -ERESTARTSYS;
321 if(signal_pending(current))
322 break;
323 /* Yield and wait to be woken up */
324 schedule();
326 current->state = TASK_RUNNING;
327 remove_wait_queue(&irnet_events.rwait, &wait);
329 /* Did we got it ? */
330 if(ret != 0)
332 /* No, return the error code */
333 DEXIT(CTRL_TRACE, " - ret %Zd\n", ret);
334 return ret;
337 /* Which event is it ? */
338 switch(irnet_events.log[ap->event_index].event)
340 case IRNET_DISCOVER:
341 sprintf(event, "Discovered %08x (%s) behind %08x {hints %02X-%02X}\n",
342 irnet_events.log[ap->event_index].daddr,
343 irnet_events.log[ap->event_index].name,
344 irnet_events.log[ap->event_index].saddr,
345 irnet_events.log[ap->event_index].hints.byte[0],
346 irnet_events.log[ap->event_index].hints.byte[1]);
347 break;
348 case IRNET_EXPIRE:
349 sprintf(event, "Expired %08x (%s) behind %08x {hints %02X-%02X}\n",
350 irnet_events.log[ap->event_index].daddr,
351 irnet_events.log[ap->event_index].name,
352 irnet_events.log[ap->event_index].saddr,
353 irnet_events.log[ap->event_index].hints.byte[0],
354 irnet_events.log[ap->event_index].hints.byte[1]);
355 break;
356 case IRNET_CONNECT_TO:
357 sprintf(event, "Connected to %08x (%s) on ppp%d\n",
358 irnet_events.log[ap->event_index].daddr,
359 irnet_events.log[ap->event_index].name,
360 irnet_events.log[ap->event_index].unit);
361 break;
362 case IRNET_CONNECT_FROM:
363 sprintf(event, "Connection from %08x (%s) on ppp%d\n",
364 irnet_events.log[ap->event_index].daddr,
365 irnet_events.log[ap->event_index].name,
366 irnet_events.log[ap->event_index].unit);
367 break;
368 case IRNET_REQUEST_FROM:
369 sprintf(event, "Request from %08x (%s) behind %08x\n",
370 irnet_events.log[ap->event_index].daddr,
371 irnet_events.log[ap->event_index].name,
372 irnet_events.log[ap->event_index].saddr);
373 break;
374 case IRNET_NOANSWER_FROM:
375 sprintf(event, "No-answer from %08x (%s) on ppp%d\n",
376 irnet_events.log[ap->event_index].daddr,
377 irnet_events.log[ap->event_index].name,
378 irnet_events.log[ap->event_index].unit);
379 break;
380 case IRNET_BLOCKED_LINK:
381 sprintf(event, "Blocked link with %08x (%s) on ppp%d\n",
382 irnet_events.log[ap->event_index].daddr,
383 irnet_events.log[ap->event_index].name,
384 irnet_events.log[ap->event_index].unit);
385 break;
386 case IRNET_DISCONNECT_FROM:
387 sprintf(event, "Disconnection from %08x (%s) on ppp%d\n",
388 irnet_events.log[ap->event_index].daddr,
389 irnet_events.log[ap->event_index].name,
390 irnet_events.log[ap->event_index].unit);
391 break;
392 case IRNET_DISCONNECT_TO:
393 sprintf(event, "Disconnected to %08x (%s)\n",
394 irnet_events.log[ap->event_index].daddr,
395 irnet_events.log[ap->event_index].name);
396 break;
397 default:
398 sprintf(event, "Bug\n");
400 /* Increment our event index */
401 ap->event_index = (ap->event_index + 1) % IRNET_MAX_EVENTS;
403 DEBUG(CTRL_INFO, "Event is :%s", event);
405 /* Copy it to the user */
406 if(copy_to_user(buf, event, strlen(event)))
408 DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
409 return -EFAULT;
412 DEXIT(CTRL_TRACE, "\n");
413 return(strlen(event));
416 /*------------------------------------------------------------------*/
418 * Poll : called when someone do a select on /dev/irnet.
419 * Just check if there are new events...
421 static inline unsigned int
422 irnet_ctrl_poll(irnet_socket * ap,
423 struct file * file,
424 poll_table * wait)
426 unsigned int mask;
428 DENTER(CTRL_TRACE, "(ap=0x%p)\n", ap);
430 poll_wait(file, &irnet_events.rwait, wait);
431 mask = POLLOUT | POLLWRNORM;
432 /* If there is unread events */
433 if(ap->event_index != irnet_events.index)
434 mask |= POLLIN | POLLRDNORM;
435 #ifdef INITIAL_DISCOVERY
436 if(ap->disco_number != -1)
438 /* Test if it's the first time and therefore we need to get the log */
439 if(ap->discoveries == NULL)
440 irnet_get_discovery_log(ap);
441 /* Recheck */
442 if(ap->disco_number != -1)
443 mask |= POLLIN | POLLRDNORM;
445 #endif /* INITIAL_DISCOVERY */
447 DEXIT(CTRL_TRACE, " - mask=0x%X\n", mask);
448 return mask;
452 /*********************** FILESYSTEM CALLBACKS ***********************/
454 * Implement the usual open, read, write functions that will be called
455 * by the file system when some action is performed on /dev/irnet.
456 * Most of those actions will in fact be performed by "pppd" or
457 * the control channel, we just act as a redirector...
460 /*------------------------------------------------------------------*/
462 * Open : when somebody open /dev/irnet
463 * We basically create a new instance of irnet and initialise it.
465 static int
466 dev_irnet_open(struct inode * inode,
467 struct file * file)
469 struct irnet_socket * ap;
470 int err;
472 DENTER(FS_TRACE, "(file=0x%p)\n", file);
474 #ifdef SECURE_DEVIRNET
475 /* This could (should?) be enforced by the permissions on /dev/irnet. */
476 if(!capable(CAP_NET_ADMIN))
477 return -EPERM;
478 #endif /* SECURE_DEVIRNET */
480 /* Allocate a private structure for this IrNET instance */
481 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
482 DABORT(ap == NULL, -ENOMEM, FS_ERROR, "Can't allocate struct irnet...\n");
484 lock_kernel();
485 /* initialize the irnet structure */
486 ap->file = file;
488 /* PPP channel setup */
489 ap->ppp_open = 0;
490 ap->chan.private = ap;
491 ap->chan.ops = &irnet_ppp_ops;
492 ap->chan.mtu = (2048 - TTP_MAX_HEADER - 2 - PPP_HDRLEN);
493 ap->chan.hdrlen = 2 + TTP_MAX_HEADER; /* for A/C + Max IrDA hdr */
494 /* PPP parameters */
495 ap->mru = (2048 - TTP_MAX_HEADER - 2 - PPP_HDRLEN);
496 ap->xaccm[0] = ~0U;
497 ap->xaccm[3] = 0x60000000U;
498 ap->raccm = ~0U;
500 /* Setup the IrDA part... */
501 err = irda_irnet_create(ap);
502 if(err)
504 DERROR(FS_ERROR, "Can't setup IrDA link...\n");
505 kfree(ap);
506 unlock_kernel();
507 return err;
510 /* For the control channel */
511 ap->event_index = irnet_events.index; /* Cancel all past events */
513 /* Put our stuff where we will be able to find it later */
514 file->private_data = ap;
516 DEXIT(FS_TRACE, " - ap=0x%p\n", ap);
517 unlock_kernel();
518 return 0;
522 /*------------------------------------------------------------------*/
524 * Close : when somebody close /dev/irnet
525 * Destroy the instance of /dev/irnet
527 static int
528 dev_irnet_close(struct inode * inode,
529 struct file * file)
531 irnet_socket * ap = (struct irnet_socket *) file->private_data;
533 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
534 file, ap);
535 DABORT(ap == NULL, 0, FS_ERROR, "ap is NULL !!!\n");
537 /* Detach ourselves */
538 file->private_data = NULL;
540 /* Close IrDA stuff */
541 irda_irnet_destroy(ap);
543 /* Disconnect from the generic PPP layer if not already done */
544 if(ap->ppp_open)
546 DERROR(FS_ERROR, "Channel still registered - deregistering !\n");
547 ap->ppp_open = 0;
548 ppp_unregister_channel(&ap->chan);
551 kfree(ap);
553 DEXIT(FS_TRACE, "\n");
554 return 0;
557 /*------------------------------------------------------------------*/
559 * Write does nothing.
560 * (we receive packet from ppp_generic through ppp_irnet_send())
562 static ssize_t
563 dev_irnet_write(struct file * file,
564 const char __user *buf,
565 size_t count,
566 loff_t * ppos)
568 irnet_socket * ap = (struct irnet_socket *) file->private_data;
570 DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
571 file, ap, count);
572 DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
574 /* If we are connected to ppp_generic, let it handle the job */
575 if(ap->ppp_open)
576 return -EAGAIN;
577 else
578 return irnet_ctrl_write(ap, buf, count);
581 /*------------------------------------------------------------------*/
583 * Read doesn't do much either.
584 * (pppd poll us, but ultimately reads through /dev/ppp)
586 static ssize_t
587 dev_irnet_read(struct file * file,
588 char __user * buf,
589 size_t count,
590 loff_t * ppos)
592 irnet_socket * ap = (struct irnet_socket *) file->private_data;
594 DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
595 file, ap, count);
596 DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
598 /* If we are connected to ppp_generic, let it handle the job */
599 if(ap->ppp_open)
600 return -EAGAIN;
601 else
602 return irnet_ctrl_read(ap, file, buf, count);
605 /*------------------------------------------------------------------*/
607 * Poll : called when someone do a select on /dev/irnet
609 static unsigned int
610 dev_irnet_poll(struct file * file,
611 poll_table * wait)
613 irnet_socket * ap = (struct irnet_socket *) file->private_data;
614 unsigned int mask;
616 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
617 file, ap);
619 mask = POLLOUT | POLLWRNORM;
620 DABORT(ap == NULL, mask, FS_ERROR, "ap is NULL !!!\n");
622 /* If we are connected to ppp_generic, let it handle the job */
623 if(!ap->ppp_open)
624 mask |= irnet_ctrl_poll(ap, file, wait);
626 DEXIT(FS_TRACE, " - mask=0x%X\n", mask);
627 return(mask);
630 /*------------------------------------------------------------------*/
632 * IOCtl : Called when someone does some ioctls on /dev/irnet
633 * This is the way pppd configure us and control us while the PPP
634 * instance is active.
636 static long
637 dev_irnet_ioctl(
638 struct file * file,
639 unsigned int cmd,
640 unsigned long arg)
642 irnet_socket * ap = (struct irnet_socket *) file->private_data;
643 int err;
644 int val;
645 void __user *argp = (void __user *)arg;
647 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p, cmd=0x%X)\n",
648 file, ap, cmd);
650 /* Basic checks... */
651 DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
652 #ifdef SECURE_DEVIRNET
653 if(!capable(CAP_NET_ADMIN))
654 return -EPERM;
655 #endif /* SECURE_DEVIRNET */
657 err = -EFAULT;
658 switch(cmd)
660 /* Set discipline (should be N_SYNC_PPP or N_TTY) */
661 case TIOCSETD:
662 if(get_user(val, (int __user *)argp))
663 break;
664 if((val == N_SYNC_PPP) || (val == N_PPP))
666 DEBUG(FS_INFO, "Entering PPP discipline.\n");
667 /* PPP channel setup (ap->chan in configued in dev_irnet_open())*/
668 lock_kernel();
669 err = ppp_register_channel(&ap->chan);
670 if(err == 0)
672 /* Our ppp side is active */
673 ap->ppp_open = 1;
675 DEBUG(FS_INFO, "Trying to establish a connection.\n");
676 /* Setup the IrDA link now - may fail... */
677 irda_irnet_connect(ap);
679 else
680 DERROR(FS_ERROR, "Can't setup PPP channel...\n");
681 unlock_kernel();
683 else
685 /* In theory, should be N_TTY */
686 DEBUG(FS_INFO, "Exiting PPP discipline.\n");
687 /* Disconnect from the generic PPP layer */
688 lock_kernel();
689 if(ap->ppp_open)
691 ap->ppp_open = 0;
692 ppp_unregister_channel(&ap->chan);
694 else
695 DERROR(FS_ERROR, "Channel not registered !\n");
696 err = 0;
697 unlock_kernel();
699 break;
701 /* Query PPP channel and unit number */
702 case PPPIOCGCHAN:
703 if(ap->ppp_open && !put_user(ppp_channel_index(&ap->chan),
704 (int __user *)argp))
705 err = 0;
706 break;
707 case PPPIOCGUNIT:
708 lock_kernel();
709 if(ap->ppp_open && !put_user(ppp_unit_number(&ap->chan),
710 (int __user *)argp))
711 err = 0;
712 break;
714 /* All these ioctls can be passed both directly and from ppp_generic,
715 * so we just deal with them in one place...
717 case PPPIOCGFLAGS:
718 case PPPIOCSFLAGS:
719 case PPPIOCGASYNCMAP:
720 case PPPIOCSASYNCMAP:
721 case PPPIOCGRASYNCMAP:
722 case PPPIOCSRASYNCMAP:
723 case PPPIOCGXASYNCMAP:
724 case PPPIOCSXASYNCMAP:
725 case PPPIOCGMRU:
726 case PPPIOCSMRU:
727 DEBUG(FS_INFO, "Standard PPP ioctl.\n");
728 if(!capable(CAP_NET_ADMIN))
729 err = -EPERM;
730 else {
731 lock_kernel();
732 err = ppp_irnet_ioctl(&ap->chan, cmd, arg);
733 unlock_kernel();
735 break;
737 /* TTY IOCTLs : Pretend that we are a tty, to keep pppd happy */
738 /* Get termios */
739 case TCGETS:
740 DEBUG(FS_INFO, "Get termios.\n");
741 lock_kernel();
742 #ifndef TCGETS2
743 if(!kernel_termios_to_user_termios((struct termios __user *)argp, &ap->termios))
744 err = 0;
745 #else
746 if(kernel_termios_to_user_termios_1((struct termios __user *)argp, &ap->termios))
747 err = 0;
748 #endif
749 unlock_kernel();
750 break;
751 /* Set termios */
752 case TCSETSF:
753 DEBUG(FS_INFO, "Set termios.\n");
754 lock_kernel();
755 #ifndef TCGETS2
756 if(!user_termios_to_kernel_termios(&ap->termios, (struct termios __user *)argp))
757 err = 0;
758 #else
759 if(!user_termios_to_kernel_termios_1(&ap->termios, (struct termios __user *)argp))
760 err = 0;
761 #endif
762 unlock_kernel();
763 break;
765 /* Set DTR/RTS */
766 case TIOCMBIS:
767 case TIOCMBIC:
768 /* Set exclusive/non-exclusive mode */
769 case TIOCEXCL:
770 case TIOCNXCL:
771 DEBUG(FS_INFO, "TTY compatibility.\n");
772 err = 0;
773 break;
775 case TCGETA:
776 DEBUG(FS_INFO, "TCGETA\n");
777 break;
779 case TCFLSH:
780 DEBUG(FS_INFO, "TCFLSH\n");
781 /* Note : this will flush buffers in PPP, so it *must* be done
782 * We should also worry that we don't accept junk here and that
783 * we get rid of our own buffers */
784 #ifdef FLUSH_TO_PPP
785 lock_kernel();
786 ppp_output_wakeup(&ap->chan);
787 unlock_kernel();
788 #endif /* FLUSH_TO_PPP */
789 err = 0;
790 break;
792 case FIONREAD:
793 DEBUG(FS_INFO, "FIONREAD\n");
794 val = 0;
795 if(put_user(val, (int __user *)argp))
796 break;
797 err = 0;
798 break;
800 default:
801 DERROR(FS_ERROR, "Unsupported ioctl (0x%X)\n", cmd);
802 err = -ENOTTY;
805 DEXIT(FS_TRACE, " - err = 0x%X\n", err);
806 return err;
809 /************************** PPP CALLBACKS **************************/
811 * This are the functions that the generic PPP driver in the kernel
812 * will call to communicate to us.
815 /*------------------------------------------------------------------*/
817 * Prepare the ppp frame for transmission over the IrDA socket.
818 * We make sure that the header space is enough, and we change ppp header
819 * according to flags passed by pppd.
820 * This is not a callback, but just a helper function used in ppp_irnet_send()
822 static inline struct sk_buff *
823 irnet_prepare_skb(irnet_socket * ap,
824 struct sk_buff * skb)
826 unsigned char * data;
827 int proto; /* PPP protocol */
828 int islcp; /* Protocol == LCP */
829 int needaddr; /* Need PPP address */
831 DENTER(PPP_TRACE, "(ap=0x%p, skb=0x%p)\n",
832 ap, skb);
834 /* Extract PPP protocol from the frame */
835 data = skb->data;
836 proto = (data[0] << 8) + data[1];
838 /* LCP packets with codes between 1 (configure-request)
839 * and 7 (code-reject) must be sent as though no options
840 * have been negotiated. */
841 islcp = (proto == PPP_LCP) && (1 <= data[2]) && (data[2] <= 7);
843 /* compress protocol field if option enabled */
844 if((data[0] == 0) && (ap->flags & SC_COMP_PROT) && (!islcp))
845 skb_pull(skb,1);
847 /* Check if we need address/control fields */
848 needaddr = 2*((ap->flags & SC_COMP_AC) == 0 || islcp);
850 /* Is the skb headroom large enough to contain all IrDA-headers? */
851 if((skb_headroom(skb) < (ap->max_header_size + needaddr)) ||
852 (skb_shared(skb)))
854 struct sk_buff * new_skb;
856 DEBUG(PPP_INFO, "Reallocating skb\n");
858 /* Create a new skb */
859 new_skb = skb_realloc_headroom(skb, ap->max_header_size + needaddr);
861 /* We have to free the original skb anyway */
862 dev_kfree_skb(skb);
864 /* Did the realloc succeed ? */
865 DABORT(new_skb == NULL, NULL, PPP_ERROR, "Could not realloc skb\n");
867 /* Use the new skb instead */
868 skb = new_skb;
871 /* prepend address/control fields if necessary */
872 if(needaddr)
874 skb_push(skb, 2);
875 skb->data[0] = PPP_ALLSTATIONS;
876 skb->data[1] = PPP_UI;
879 DEXIT(PPP_TRACE, "\n");
881 return skb;
884 /*------------------------------------------------------------------*/
886 * Send a packet to the peer over the IrTTP connection.
887 * Returns 1 iff the packet was accepted.
888 * Returns 0 iff packet was not consumed.
889 * If the packet was not accepted, we will call ppp_output_wakeup
890 * at some later time to reactivate flow control in ppp_generic.
892 static int
893 ppp_irnet_send(struct ppp_channel * chan,
894 struct sk_buff * skb)
896 irnet_socket * self = (struct irnet_socket *) chan->private;
897 int ret;
899 DENTER(PPP_TRACE, "(channel=0x%p, ap/self=0x%p)\n",
900 chan, self);
902 /* Check if things are somewhat valid... */
903 DASSERT(self != NULL, 0, PPP_ERROR, "Self is NULL !!!\n");
905 /* Check if we are connected */
906 if(!(test_bit(0, &self->ttp_open)))
908 #ifdef CONNECT_IN_SEND
909 /* Let's try to connect one more time... */
910 /* Note : we won't be connected after this call, but we should be
911 * ready for next packet... */
912 /* If we are already connecting, this will fail */
913 irda_irnet_connect(self);
914 #endif /* CONNECT_IN_SEND */
916 DEBUG(PPP_INFO, "IrTTP not ready ! (%ld-%ld)\n",
917 self->ttp_open, self->ttp_connect);
919 /* Note : we can either drop the packet or block the packet.
921 * Blocking the packet allow us a better connection time,
922 * because by calling ppp_output_wakeup() we can have
923 * ppp_generic resending the LCP request immediately to us,
924 * rather than waiting for one of pppd periodic transmission of
925 * LCP request.
927 * On the other hand, if we block all packet, all those periodic
928 * transmissions of pppd accumulate in ppp_generic, creating a
929 * backlog of LCP request. When we eventually connect later on,
930 * we have to transmit all this backlog before we can connect
931 * proper (if we don't timeout before).
933 * The current strategy is as follow :
934 * While we are attempting to connect, we block packets to get
935 * a better connection time.
936 * If we fail to connect, we drain the queue and start dropping packets
938 #ifdef BLOCK_WHEN_CONNECT
939 /* If we are attempting to connect */
940 if(test_bit(0, &self->ttp_connect))
942 /* Blocking packet, ppp_generic will retry later */
943 return 0;
945 #endif /* BLOCK_WHEN_CONNECT */
947 /* Dropping packet, pppd will retry later */
948 dev_kfree_skb(skb);
949 return 1;
952 /* Check if the queue can accept any packet, otherwise block */
953 if(self->tx_flow != FLOW_START)
954 DRETURN(0, PPP_INFO, "IrTTP queue full (%d skbs)...\n",
955 skb_queue_len(&self->tsap->tx_queue));
957 /* Prepare ppp frame for transmission */
958 skb = irnet_prepare_skb(self, skb);
959 DABORT(skb == NULL, 1, PPP_ERROR, "Prepare skb for Tx failed.\n");
961 /* Send the packet to IrTTP */
962 ret = irttp_data_request(self->tsap, skb);
963 if(ret < 0)
966 * > IrTTPs tx queue is full, so we just have to
967 * > drop the frame! You might think that we should
968 * > just return -1 and don't deallocate the frame,
969 * > but that is dangerous since it's possible that
970 * > we have replaced the original skb with a new
971 * > one with larger headroom, and that would really
972 * > confuse do_dev_queue_xmit() in dev.c! I have
973 * > tried :-) DB
974 * Correction : we verify the flow control above (self->tx_flow),
975 * so we come here only if IrTTP doesn't like the packet (empty,
976 * too large, IrTTP not connected). In those rare cases, it's ok
977 * to drop it, we don't want to see it here again...
978 * Jean II
980 DERROR(PPP_ERROR, "IrTTP doesn't like this packet !!! (0x%X)\n", ret);
981 /* irttp_data_request already free the packet */
984 DEXIT(PPP_TRACE, "\n");
985 return 1; /* Packet has been consumed */
988 /*------------------------------------------------------------------*/
990 * Take care of the ioctls that ppp_generic doesn't want to deal with...
991 * Note : we are also called from dev_irnet_ioctl().
993 static int
994 ppp_irnet_ioctl(struct ppp_channel * chan,
995 unsigned int cmd,
996 unsigned long arg)
998 irnet_socket * ap = (struct irnet_socket *) chan->private;
999 int err;
1000 int val;
1001 u32 accm[8];
1002 void __user *argp = (void __user *)arg;
1004 DENTER(PPP_TRACE, "(channel=0x%p, ap=0x%p, cmd=0x%X)\n",
1005 chan, ap, cmd);
1007 /* Basic checks... */
1008 DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
1010 err = -EFAULT;
1011 switch(cmd)
1013 /* PPP flags */
1014 case PPPIOCGFLAGS:
1015 val = ap->flags | ap->rbits;
1016 if(put_user(val, (int __user *) argp))
1017 break;
1018 err = 0;
1019 break;
1020 case PPPIOCSFLAGS:
1021 if(get_user(val, (int __user *) argp))
1022 break;
1023 ap->flags = val & ~SC_RCV_BITS;
1024 ap->rbits = val & SC_RCV_BITS;
1025 err = 0;
1026 break;
1028 /* Async map stuff - all dummy to please pppd */
1029 case PPPIOCGASYNCMAP:
1030 if(put_user(ap->xaccm[0], (u32 __user *) argp))
1031 break;
1032 err = 0;
1033 break;
1034 case PPPIOCSASYNCMAP:
1035 if(get_user(ap->xaccm[0], (u32 __user *) argp))
1036 break;
1037 err = 0;
1038 break;
1039 case PPPIOCGRASYNCMAP:
1040 if(put_user(ap->raccm, (u32 __user *) argp))
1041 break;
1042 err = 0;
1043 break;
1044 case PPPIOCSRASYNCMAP:
1045 if(get_user(ap->raccm, (u32 __user *) argp))
1046 break;
1047 err = 0;
1048 break;
1049 case PPPIOCGXASYNCMAP:
1050 if(copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
1051 break;
1052 err = 0;
1053 break;
1054 case PPPIOCSXASYNCMAP:
1055 if(copy_from_user(accm, argp, sizeof(accm)))
1056 break;
1057 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
1058 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
1059 memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
1060 err = 0;
1061 break;
1063 /* Max PPP frame size */
1064 case PPPIOCGMRU:
1065 if(put_user(ap->mru, (int __user *) argp))
1066 break;
1067 err = 0;
1068 break;
1069 case PPPIOCSMRU:
1070 if(get_user(val, (int __user *) argp))
1071 break;
1072 if(val < PPP_MRU)
1073 val = PPP_MRU;
1074 ap->mru = val;
1075 err = 0;
1076 break;
1078 default:
1079 DEBUG(PPP_INFO, "Unsupported ioctl (0x%X)\n", cmd);
1080 err = -ENOIOCTLCMD;
1083 DEXIT(PPP_TRACE, " - err = 0x%X\n", err);
1084 return err;
1087 /************************** INITIALISATION **************************/
1089 * Module initialisation and all that jazz...
1092 /*------------------------------------------------------------------*/
1094 * Hook our device callbacks in the filesystem, to connect our code
1095 * to /dev/irnet
1097 static inline int __init
1098 ppp_irnet_init(void)
1100 int err = 0;
1102 DENTER(MODULE_TRACE, "()\n");
1104 /* Allocate ourselves as a minor in the misc range */
1105 err = misc_register(&irnet_misc_device);
1107 DEXIT(MODULE_TRACE, "\n");
1108 return err;
1111 /*------------------------------------------------------------------*/
1113 * Cleanup at exit...
1115 static inline void __exit
1116 ppp_irnet_cleanup(void)
1118 DENTER(MODULE_TRACE, "()\n");
1120 /* De-allocate /dev/irnet minor in misc range */
1121 misc_deregister(&irnet_misc_device);
1123 DEXIT(MODULE_TRACE, "\n");
1126 /*------------------------------------------------------------------*/
1128 * Module main entry point
1130 static int __init
1131 irnet_init(void)
1133 int err;
1135 /* Initialise both parts... */
1136 err = irda_irnet_init();
1137 if(!err)
1138 err = ppp_irnet_init();
1139 return err;
1142 /*------------------------------------------------------------------*/
1144 * Module exit
1146 static void __exit
1147 irnet_cleanup(void)
1149 irda_irnet_cleanup();
1150 ppp_irnet_cleanup();
1153 /*------------------------------------------------------------------*/
1155 * Module magic
1157 module_init(irnet_init);
1158 module_exit(irnet_cleanup);
1159 MODULE_AUTHOR("Jean Tourrilhes <jt@hpl.hp.com>");
1160 MODULE_DESCRIPTION("IrNET : Synchronous PPP over IrDA");
1161 MODULE_LICENSE("GPL");
1162 MODULE_ALIAS_CHARDEV(10, 187);