be2net: swap only first 2 fields of mcc_wrb
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / irda / irnet / irnet_ppp.c
blob156020d138b507685633feda14fa747d11c90538
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 start = skip_spaces(start);
82 /* ',' is our command separator */
83 next = strchr(start, ',');
84 if(next)
86 *next = '\0'; /* Terminate command */
87 length = next - start; /* Length */
88 next++; /* Skip the '\0' */
90 else
91 length = strlen(start);
93 DEBUG(CTRL_INFO, "Found command ``%s'' (%d).\n", start, length);
95 /* Check if we recognised one of the known command
96 * We can't use "switch" with strings, so hack with "continue" */
98 /* First command : name -> Requested IrDA nickname */
99 if(!strncmp(start, "name", 4))
101 /* Copy the name only if is included and not "any" */
102 if((length > 5) && (strcmp(start + 5, "any")))
104 /* Strip out trailing whitespaces */
105 while(isspace(start[length - 1]))
106 length--;
108 /* Copy the name for later reuse */
109 memcpy(ap->rname, start + 5, length - 5);
110 ap->rname[length - 5] = '\0';
112 else
113 ap->rname[0] = '\0';
114 DEBUG(CTRL_INFO, "Got rname = ``%s''\n", ap->rname);
116 /* Restart the loop */
117 continue;
120 /* Second command : addr, daddr -> Requested IrDA destination address
121 * Also process : saddr -> Requested IrDA source address */
122 if((!strncmp(start, "addr", 4)) ||
123 (!strncmp(start, "daddr", 5)) ||
124 (!strncmp(start, "saddr", 5)))
126 __u32 addr = DEV_ADDR_ANY;
128 /* Copy the address only if is included and not "any" */
129 if((length > 5) && (strcmp(start + 5, "any")))
131 char * begp = start + 5;
132 char * endp;
134 /* Scrap whitespaces before the command */
135 begp = skip_spaces(begp);
137 /* Convert argument to a number (last arg is the base) */
138 addr = simple_strtoul(begp, &endp, 16);
139 /* Has it worked ? (endp should be start + length) */
140 DABORT(endp <= (start + 5), -EINVAL,
141 CTRL_ERROR, "Invalid address.\n");
143 /* Which type of address ? */
144 if(start[0] == 's')
146 /* Save it */
147 ap->rsaddr = addr;
148 DEBUG(CTRL_INFO, "Got rsaddr = %08x\n", ap->rsaddr);
150 else
152 /* Save it */
153 ap->rdaddr = addr;
154 DEBUG(CTRL_INFO, "Got rdaddr = %08x\n", ap->rdaddr);
157 /* Restart the loop */
158 continue;
161 /* Other possible command : connect N (number of retries) */
163 /* No command matched -> Failed... */
164 DABORT(1, -EINVAL, CTRL_ERROR, "Not a recognised IrNET command.\n");
167 /* Success : we have parsed all commands successfully */
168 return(count);
171 #ifdef INITIAL_DISCOVERY
172 /*------------------------------------------------------------------*/
174 * Function irnet_get_discovery_log (self)
176 * Query the content on the discovery log if not done
178 * This function query the current content of the discovery log
179 * at the startup of the event channel and save it in the internal struct.
181 static void
182 irnet_get_discovery_log(irnet_socket * ap)
184 __u16 mask = irlmp_service_to_hint(S_LAN);
186 /* Ask IrLMP for the current discovery log */
187 ap->discoveries = irlmp_get_discoveries(&ap->disco_number, mask,
188 DISCOVERY_DEFAULT_SLOTS);
190 /* Check if the we got some results */
191 if(ap->discoveries == NULL)
192 ap->disco_number = -1;
194 DEBUG(CTRL_INFO, "Got the log (0x%p), size is %d\n",
195 ap->discoveries, ap->disco_number);
198 /*------------------------------------------------------------------*/
200 * Function irnet_read_discovery_log (self, event)
202 * Read the content on the discovery log
204 * This function dump the current content of the discovery log
205 * at the startup of the event channel.
206 * Return 1 if wrote an event on the control channel...
208 * State of the ap->disco_XXX variables :
209 * Socket creation : discoveries = NULL ; disco_index = 0 ; disco_number = 0
210 * While reading : discoveries = ptr ; disco_index = X ; disco_number = Y
211 * After reading : discoveries = NULL ; disco_index = Y ; disco_number = -1
213 static inline int
214 irnet_read_discovery_log(irnet_socket * ap,
215 char * event)
217 int done_event = 0;
219 DENTER(CTRL_TRACE, "(ap=0x%p, event=0x%p)\n",
220 ap, event);
222 /* Test if we have some work to do or we have already finished */
223 if(ap->disco_number == -1)
225 DEBUG(CTRL_INFO, "Already done\n");
226 return 0;
229 /* Test if it's the first time and therefore we need to get the log */
230 if(ap->discoveries == NULL)
231 irnet_get_discovery_log(ap);
233 /* Check if we have more item to dump */
234 if(ap->disco_index < ap->disco_number)
236 /* Write an event */
237 sprintf(event, "Found %08x (%s) behind %08x {hints %02X-%02X}\n",
238 ap->discoveries[ap->disco_index].daddr,
239 ap->discoveries[ap->disco_index].info,
240 ap->discoveries[ap->disco_index].saddr,
241 ap->discoveries[ap->disco_index].hints[0],
242 ap->discoveries[ap->disco_index].hints[1]);
243 DEBUG(CTRL_INFO, "Writing discovery %d : %s\n",
244 ap->disco_index, ap->discoveries[ap->disco_index].info);
246 /* We have an event */
247 done_event = 1;
248 /* Next discovery */
249 ap->disco_index++;
252 /* Check if we have done the last item */
253 if(ap->disco_index >= ap->disco_number)
255 /* No more items : remove the log and signal termination */
256 DEBUG(CTRL_INFO, "Cleaning up log (0x%p)\n",
257 ap->discoveries);
258 if(ap->discoveries != NULL)
260 /* Cleanup our copy of the discovery log */
261 kfree(ap->discoveries);
262 ap->discoveries = NULL;
264 ap->disco_number = -1;
267 return done_event;
269 #endif /* INITIAL_DISCOVERY */
271 /*------------------------------------------------------------------*/
273 * Read is used to get IrNET events
275 static inline ssize_t
276 irnet_ctrl_read(irnet_socket * ap,
277 struct file * file,
278 char __user * buf,
279 size_t count)
281 DECLARE_WAITQUEUE(wait, current);
282 char event[64]; /* Max event is 61 char */
283 ssize_t ret = 0;
285 DENTER(CTRL_TRACE, "(ap=0x%p, count=%Zd)\n", ap, count);
287 /* Check if we can write an event out in one go */
288 DABORT(count < sizeof(event), -EOVERFLOW, CTRL_ERROR, "Buffer to small.\n");
290 #ifdef INITIAL_DISCOVERY
291 /* Check if we have read the log */
292 if(irnet_read_discovery_log(ap, event))
294 /* We have an event !!! Copy it to the user */
295 if(copy_to_user(buf, event, strlen(event)))
297 DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
298 return -EFAULT;
301 DEXIT(CTRL_TRACE, "\n");
302 return(strlen(event));
304 #endif /* INITIAL_DISCOVERY */
306 /* Put ourselves on the wait queue to be woken up */
307 add_wait_queue(&irnet_events.rwait, &wait);
308 current->state = TASK_INTERRUPTIBLE;
309 for(;;)
311 /* If there is unread events */
312 ret = 0;
313 if(ap->event_index != irnet_events.index)
314 break;
315 ret = -EAGAIN;
316 if(file->f_flags & O_NONBLOCK)
317 break;
318 ret = -ERESTARTSYS;
319 if(signal_pending(current))
320 break;
321 /* Yield and wait to be woken up */
322 schedule();
324 current->state = TASK_RUNNING;
325 remove_wait_queue(&irnet_events.rwait, &wait);
327 /* Did we got it ? */
328 if(ret != 0)
330 /* No, return the error code */
331 DEXIT(CTRL_TRACE, " - ret %Zd\n", ret);
332 return ret;
335 /* Which event is it ? */
336 switch(irnet_events.log[ap->event_index].event)
338 case IRNET_DISCOVER:
339 sprintf(event, "Discovered %08x (%s) behind %08x {hints %02X-%02X}\n",
340 irnet_events.log[ap->event_index].daddr,
341 irnet_events.log[ap->event_index].name,
342 irnet_events.log[ap->event_index].saddr,
343 irnet_events.log[ap->event_index].hints.byte[0],
344 irnet_events.log[ap->event_index].hints.byte[1]);
345 break;
346 case IRNET_EXPIRE:
347 sprintf(event, "Expired %08x (%s) behind %08x {hints %02X-%02X}\n",
348 irnet_events.log[ap->event_index].daddr,
349 irnet_events.log[ap->event_index].name,
350 irnet_events.log[ap->event_index].saddr,
351 irnet_events.log[ap->event_index].hints.byte[0],
352 irnet_events.log[ap->event_index].hints.byte[1]);
353 break;
354 case IRNET_CONNECT_TO:
355 sprintf(event, "Connected to %08x (%s) on ppp%d\n",
356 irnet_events.log[ap->event_index].daddr,
357 irnet_events.log[ap->event_index].name,
358 irnet_events.log[ap->event_index].unit);
359 break;
360 case IRNET_CONNECT_FROM:
361 sprintf(event, "Connection from %08x (%s) on ppp%d\n",
362 irnet_events.log[ap->event_index].daddr,
363 irnet_events.log[ap->event_index].name,
364 irnet_events.log[ap->event_index].unit);
365 break;
366 case IRNET_REQUEST_FROM:
367 sprintf(event, "Request from %08x (%s) behind %08x\n",
368 irnet_events.log[ap->event_index].daddr,
369 irnet_events.log[ap->event_index].name,
370 irnet_events.log[ap->event_index].saddr);
371 break;
372 case IRNET_NOANSWER_FROM:
373 sprintf(event, "No-answer from %08x (%s) on ppp%d\n",
374 irnet_events.log[ap->event_index].daddr,
375 irnet_events.log[ap->event_index].name,
376 irnet_events.log[ap->event_index].unit);
377 break;
378 case IRNET_BLOCKED_LINK:
379 sprintf(event, "Blocked link with %08x (%s) on ppp%d\n",
380 irnet_events.log[ap->event_index].daddr,
381 irnet_events.log[ap->event_index].name,
382 irnet_events.log[ap->event_index].unit);
383 break;
384 case IRNET_DISCONNECT_FROM:
385 sprintf(event, "Disconnection from %08x (%s) on ppp%d\n",
386 irnet_events.log[ap->event_index].daddr,
387 irnet_events.log[ap->event_index].name,
388 irnet_events.log[ap->event_index].unit);
389 break;
390 case IRNET_DISCONNECT_TO:
391 sprintf(event, "Disconnected to %08x (%s)\n",
392 irnet_events.log[ap->event_index].daddr,
393 irnet_events.log[ap->event_index].name);
394 break;
395 default:
396 sprintf(event, "Bug\n");
398 /* Increment our event index */
399 ap->event_index = (ap->event_index + 1) % IRNET_MAX_EVENTS;
401 DEBUG(CTRL_INFO, "Event is :%s", event);
403 /* Copy it to the user */
404 if(copy_to_user(buf, event, strlen(event)))
406 DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
407 return -EFAULT;
410 DEXIT(CTRL_TRACE, "\n");
411 return(strlen(event));
414 /*------------------------------------------------------------------*/
416 * Poll : called when someone do a select on /dev/irnet.
417 * Just check if there are new events...
419 static inline unsigned int
420 irnet_ctrl_poll(irnet_socket * ap,
421 struct file * file,
422 poll_table * wait)
424 unsigned int mask;
426 DENTER(CTRL_TRACE, "(ap=0x%p)\n", ap);
428 poll_wait(file, &irnet_events.rwait, wait);
429 mask = POLLOUT | POLLWRNORM;
430 /* If there is unread events */
431 if(ap->event_index != irnet_events.index)
432 mask |= POLLIN | POLLRDNORM;
433 #ifdef INITIAL_DISCOVERY
434 if(ap->disco_number != -1)
436 /* Test if it's the first time and therefore we need to get the log */
437 if(ap->discoveries == NULL)
438 irnet_get_discovery_log(ap);
439 /* Recheck */
440 if(ap->disco_number != -1)
441 mask |= POLLIN | POLLRDNORM;
443 #endif /* INITIAL_DISCOVERY */
445 DEXIT(CTRL_TRACE, " - mask=0x%X\n", mask);
446 return mask;
450 /*********************** FILESYSTEM CALLBACKS ***********************/
452 * Implement the usual open, read, write functions that will be called
453 * by the file system when some action is performed on /dev/irnet.
454 * Most of those actions will in fact be performed by "pppd" or
455 * the control channel, we just act as a redirector...
458 /*------------------------------------------------------------------*/
460 * Open : when somebody open /dev/irnet
461 * We basically create a new instance of irnet and initialise it.
463 static int
464 dev_irnet_open(struct inode * inode,
465 struct file * file)
467 struct irnet_socket * ap;
468 int err;
470 DENTER(FS_TRACE, "(file=0x%p)\n", file);
472 #ifdef SECURE_DEVIRNET
473 /* This could (should?) be enforced by the permissions on /dev/irnet. */
474 if(!capable(CAP_NET_ADMIN))
475 return -EPERM;
476 #endif /* SECURE_DEVIRNET */
478 /* Allocate a private structure for this IrNET instance */
479 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
480 DABORT(ap == NULL, -ENOMEM, FS_ERROR, "Can't allocate struct irnet...\n");
482 lock_kernel();
483 /* initialize the irnet structure */
484 ap->file = file;
486 /* PPP channel setup */
487 ap->ppp_open = 0;
488 ap->chan.private = ap;
489 ap->chan.ops = &irnet_ppp_ops;
490 ap->chan.mtu = (2048 - TTP_MAX_HEADER - 2 - PPP_HDRLEN);
491 ap->chan.hdrlen = 2 + TTP_MAX_HEADER; /* for A/C + Max IrDA hdr */
492 /* PPP parameters */
493 ap->mru = (2048 - TTP_MAX_HEADER - 2 - PPP_HDRLEN);
494 ap->xaccm[0] = ~0U;
495 ap->xaccm[3] = 0x60000000U;
496 ap->raccm = ~0U;
498 /* Setup the IrDA part... */
499 err = irda_irnet_create(ap);
500 if(err)
502 DERROR(FS_ERROR, "Can't setup IrDA link...\n");
503 kfree(ap);
504 unlock_kernel();
505 return err;
508 /* For the control channel */
509 ap->event_index = irnet_events.index; /* Cancel all past events */
511 /* Put our stuff where we will be able to find it later */
512 file->private_data = ap;
514 DEXIT(FS_TRACE, " - ap=0x%p\n", ap);
515 unlock_kernel();
516 return 0;
520 /*------------------------------------------------------------------*/
522 * Close : when somebody close /dev/irnet
523 * Destroy the instance of /dev/irnet
525 static int
526 dev_irnet_close(struct inode * inode,
527 struct file * file)
529 irnet_socket * ap = (struct irnet_socket *) file->private_data;
531 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
532 file, ap);
533 DABORT(ap == NULL, 0, FS_ERROR, "ap is NULL !!!\n");
535 /* Detach ourselves */
536 file->private_data = NULL;
538 /* Close IrDA stuff */
539 irda_irnet_destroy(ap);
541 /* Disconnect from the generic PPP layer if not already done */
542 if(ap->ppp_open)
544 DERROR(FS_ERROR, "Channel still registered - deregistering !\n");
545 ap->ppp_open = 0;
546 ppp_unregister_channel(&ap->chan);
549 kfree(ap);
551 DEXIT(FS_TRACE, "\n");
552 return 0;
555 /*------------------------------------------------------------------*/
557 * Write does nothing.
558 * (we receive packet from ppp_generic through ppp_irnet_send())
560 static ssize_t
561 dev_irnet_write(struct file * file,
562 const char __user *buf,
563 size_t count,
564 loff_t * ppos)
566 irnet_socket * ap = (struct irnet_socket *) file->private_data;
568 DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
569 file, ap, count);
570 DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
572 /* If we are connected to ppp_generic, let it handle the job */
573 if(ap->ppp_open)
574 return -EAGAIN;
575 else
576 return irnet_ctrl_write(ap, buf, count);
579 /*------------------------------------------------------------------*/
581 * Read doesn't do much either.
582 * (pppd poll us, but ultimately reads through /dev/ppp)
584 static ssize_t
585 dev_irnet_read(struct file * file,
586 char __user * buf,
587 size_t count,
588 loff_t * ppos)
590 irnet_socket * ap = (struct irnet_socket *) file->private_data;
592 DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
593 file, ap, count);
594 DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
596 /* If we are connected to ppp_generic, let it handle the job */
597 if(ap->ppp_open)
598 return -EAGAIN;
599 else
600 return irnet_ctrl_read(ap, file, buf, count);
603 /*------------------------------------------------------------------*/
605 * Poll : called when someone do a select on /dev/irnet
607 static unsigned int
608 dev_irnet_poll(struct file * file,
609 poll_table * wait)
611 irnet_socket * ap = (struct irnet_socket *) file->private_data;
612 unsigned int mask;
614 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
615 file, ap);
617 mask = POLLOUT | POLLWRNORM;
618 DABORT(ap == NULL, mask, FS_ERROR, "ap is NULL !!!\n");
620 /* If we are connected to ppp_generic, let it handle the job */
621 if(!ap->ppp_open)
622 mask |= irnet_ctrl_poll(ap, file, wait);
624 DEXIT(FS_TRACE, " - mask=0x%X\n", mask);
625 return(mask);
628 /*------------------------------------------------------------------*/
630 * IOCtl : Called when someone does some ioctls on /dev/irnet
631 * This is the way pppd configure us and control us while the PPP
632 * instance is active.
634 static long
635 dev_irnet_ioctl(
636 struct file * file,
637 unsigned int cmd,
638 unsigned long arg)
640 irnet_socket * ap = (struct irnet_socket *) file->private_data;
641 int err;
642 int val;
643 void __user *argp = (void __user *)arg;
645 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p, cmd=0x%X)\n",
646 file, ap, cmd);
648 /* Basic checks... */
649 DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
650 #ifdef SECURE_DEVIRNET
651 if(!capable(CAP_NET_ADMIN))
652 return -EPERM;
653 #endif /* SECURE_DEVIRNET */
655 err = -EFAULT;
656 switch(cmd)
658 /* Set discipline (should be N_SYNC_PPP or N_TTY) */
659 case TIOCSETD:
660 if(get_user(val, (int __user *)argp))
661 break;
662 if((val == N_SYNC_PPP) || (val == N_PPP))
664 DEBUG(FS_INFO, "Entering PPP discipline.\n");
665 /* PPP channel setup (ap->chan in configued in dev_irnet_open())*/
666 lock_kernel();
667 err = ppp_register_channel(&ap->chan);
668 if(err == 0)
670 /* Our ppp side is active */
671 ap->ppp_open = 1;
673 DEBUG(FS_INFO, "Trying to establish a connection.\n");
674 /* Setup the IrDA link now - may fail... */
675 irda_irnet_connect(ap);
677 else
678 DERROR(FS_ERROR, "Can't setup PPP channel...\n");
679 unlock_kernel();
681 else
683 /* In theory, should be N_TTY */
684 DEBUG(FS_INFO, "Exiting PPP discipline.\n");
685 /* Disconnect from the generic PPP layer */
686 lock_kernel();
687 if(ap->ppp_open)
689 ap->ppp_open = 0;
690 ppp_unregister_channel(&ap->chan);
692 else
693 DERROR(FS_ERROR, "Channel not registered !\n");
694 err = 0;
695 unlock_kernel();
697 break;
699 /* Query PPP channel and unit number */
700 case PPPIOCGCHAN:
701 if(ap->ppp_open && !put_user(ppp_channel_index(&ap->chan),
702 (int __user *)argp))
703 err = 0;
704 break;
705 case PPPIOCGUNIT:
706 lock_kernel();
707 if(ap->ppp_open && !put_user(ppp_unit_number(&ap->chan),
708 (int __user *)argp))
709 err = 0;
710 break;
712 /* All these ioctls can be passed both directly and from ppp_generic,
713 * so we just deal with them in one place...
715 case PPPIOCGFLAGS:
716 case PPPIOCSFLAGS:
717 case PPPIOCGASYNCMAP:
718 case PPPIOCSASYNCMAP:
719 case PPPIOCGRASYNCMAP:
720 case PPPIOCSRASYNCMAP:
721 case PPPIOCGXASYNCMAP:
722 case PPPIOCSXASYNCMAP:
723 case PPPIOCGMRU:
724 case PPPIOCSMRU:
725 DEBUG(FS_INFO, "Standard PPP ioctl.\n");
726 if(!capable(CAP_NET_ADMIN))
727 err = -EPERM;
728 else {
729 lock_kernel();
730 err = ppp_irnet_ioctl(&ap->chan, cmd, arg);
731 unlock_kernel();
733 break;
735 /* TTY IOCTLs : Pretend that we are a tty, to keep pppd happy */
736 /* Get termios */
737 case TCGETS:
738 DEBUG(FS_INFO, "Get termios.\n");
739 lock_kernel();
740 #ifndef TCGETS2
741 if(!kernel_termios_to_user_termios((struct termios __user *)argp, &ap->termios))
742 err = 0;
743 #else
744 if(kernel_termios_to_user_termios_1((struct termios __user *)argp, &ap->termios))
745 err = 0;
746 #endif
747 unlock_kernel();
748 break;
749 /* Set termios */
750 case TCSETSF:
751 DEBUG(FS_INFO, "Set termios.\n");
752 lock_kernel();
753 #ifndef TCGETS2
754 if(!user_termios_to_kernel_termios(&ap->termios, (struct termios __user *)argp))
755 err = 0;
756 #else
757 if(!user_termios_to_kernel_termios_1(&ap->termios, (struct termios __user *)argp))
758 err = 0;
759 #endif
760 unlock_kernel();
761 break;
763 /* Set DTR/RTS */
764 case TIOCMBIS:
765 case TIOCMBIC:
766 /* Set exclusive/non-exclusive mode */
767 case TIOCEXCL:
768 case TIOCNXCL:
769 DEBUG(FS_INFO, "TTY compatibility.\n");
770 err = 0;
771 break;
773 case TCGETA:
774 DEBUG(FS_INFO, "TCGETA\n");
775 break;
777 case TCFLSH:
778 DEBUG(FS_INFO, "TCFLSH\n");
779 /* Note : this will flush buffers in PPP, so it *must* be done
780 * We should also worry that we don't accept junk here and that
781 * we get rid of our own buffers */
782 #ifdef FLUSH_TO_PPP
783 lock_kernel();
784 ppp_output_wakeup(&ap->chan);
785 unlock_kernel();
786 #endif /* FLUSH_TO_PPP */
787 err = 0;
788 break;
790 case FIONREAD:
791 DEBUG(FS_INFO, "FIONREAD\n");
792 val = 0;
793 if(put_user(val, (int __user *)argp))
794 break;
795 err = 0;
796 break;
798 default:
799 DERROR(FS_ERROR, "Unsupported ioctl (0x%X)\n", cmd);
800 err = -ENOTTY;
803 DEXIT(FS_TRACE, " - err = 0x%X\n", err);
804 return err;
807 /************************** PPP CALLBACKS **************************/
809 * This are the functions that the generic PPP driver in the kernel
810 * will call to communicate to us.
813 /*------------------------------------------------------------------*/
815 * Prepare the ppp frame for transmission over the IrDA socket.
816 * We make sure that the header space is enough, and we change ppp header
817 * according to flags passed by pppd.
818 * This is not a callback, but just a helper function used in ppp_irnet_send()
820 static inline struct sk_buff *
821 irnet_prepare_skb(irnet_socket * ap,
822 struct sk_buff * skb)
824 unsigned char * data;
825 int proto; /* PPP protocol */
826 int islcp; /* Protocol == LCP */
827 int needaddr; /* Need PPP address */
829 DENTER(PPP_TRACE, "(ap=0x%p, skb=0x%p)\n",
830 ap, skb);
832 /* Extract PPP protocol from the frame */
833 data = skb->data;
834 proto = (data[0] << 8) + data[1];
836 /* LCP packets with codes between 1 (configure-request)
837 * and 7 (code-reject) must be sent as though no options
838 * have been negotiated. */
839 islcp = (proto == PPP_LCP) && (1 <= data[2]) && (data[2] <= 7);
841 /* compress protocol field if option enabled */
842 if((data[0] == 0) && (ap->flags & SC_COMP_PROT) && (!islcp))
843 skb_pull(skb,1);
845 /* Check if we need address/control fields */
846 needaddr = 2*((ap->flags & SC_COMP_AC) == 0 || islcp);
848 /* Is the skb headroom large enough to contain all IrDA-headers? */
849 if((skb_headroom(skb) < (ap->max_header_size + needaddr)) ||
850 (skb_shared(skb)))
852 struct sk_buff * new_skb;
854 DEBUG(PPP_INFO, "Reallocating skb\n");
856 /* Create a new skb */
857 new_skb = skb_realloc_headroom(skb, ap->max_header_size + needaddr);
859 /* We have to free the original skb anyway */
860 dev_kfree_skb(skb);
862 /* Did the realloc succeed ? */
863 DABORT(new_skb == NULL, NULL, PPP_ERROR, "Could not realloc skb\n");
865 /* Use the new skb instead */
866 skb = new_skb;
869 /* prepend address/control fields if necessary */
870 if(needaddr)
872 skb_push(skb, 2);
873 skb->data[0] = PPP_ALLSTATIONS;
874 skb->data[1] = PPP_UI;
877 DEXIT(PPP_TRACE, "\n");
879 return skb;
882 /*------------------------------------------------------------------*/
884 * Send a packet to the peer over the IrTTP connection.
885 * Returns 1 iff the packet was accepted.
886 * Returns 0 iff packet was not consumed.
887 * If the packet was not accepted, we will call ppp_output_wakeup
888 * at some later time to reactivate flow control in ppp_generic.
890 static int
891 ppp_irnet_send(struct ppp_channel * chan,
892 struct sk_buff * skb)
894 irnet_socket * self = (struct irnet_socket *) chan->private;
895 int ret;
897 DENTER(PPP_TRACE, "(channel=0x%p, ap/self=0x%p)\n",
898 chan, self);
900 /* Check if things are somewhat valid... */
901 DASSERT(self != NULL, 0, PPP_ERROR, "Self is NULL !!!\n");
903 /* Check if we are connected */
904 if(!(test_bit(0, &self->ttp_open)))
906 #ifdef CONNECT_IN_SEND
907 /* Let's try to connect one more time... */
908 /* Note : we won't be connected after this call, but we should be
909 * ready for next packet... */
910 /* If we are already connecting, this will fail */
911 irda_irnet_connect(self);
912 #endif /* CONNECT_IN_SEND */
914 DEBUG(PPP_INFO, "IrTTP not ready ! (%ld-%ld)\n",
915 self->ttp_open, self->ttp_connect);
917 /* Note : we can either drop the packet or block the packet.
919 * Blocking the packet allow us a better connection time,
920 * because by calling ppp_output_wakeup() we can have
921 * ppp_generic resending the LCP request immediately to us,
922 * rather than waiting for one of pppd periodic transmission of
923 * LCP request.
925 * On the other hand, if we block all packet, all those periodic
926 * transmissions of pppd accumulate in ppp_generic, creating a
927 * backlog of LCP request. When we eventually connect later on,
928 * we have to transmit all this backlog before we can connect
929 * proper (if we don't timeout before).
931 * The current strategy is as follow :
932 * While we are attempting to connect, we block packets to get
933 * a better connection time.
934 * If we fail to connect, we drain the queue and start dropping packets
936 #ifdef BLOCK_WHEN_CONNECT
937 /* If we are attempting to connect */
938 if(test_bit(0, &self->ttp_connect))
940 /* Blocking packet, ppp_generic will retry later */
941 return 0;
943 #endif /* BLOCK_WHEN_CONNECT */
945 /* Dropping packet, pppd will retry later */
946 dev_kfree_skb(skb);
947 return 1;
950 /* Check if the queue can accept any packet, otherwise block */
951 if(self->tx_flow != FLOW_START)
952 DRETURN(0, PPP_INFO, "IrTTP queue full (%d skbs)...\n",
953 skb_queue_len(&self->tsap->tx_queue));
955 /* Prepare ppp frame for transmission */
956 skb = irnet_prepare_skb(self, skb);
957 DABORT(skb == NULL, 1, PPP_ERROR, "Prepare skb for Tx failed.\n");
959 /* Send the packet to IrTTP */
960 ret = irttp_data_request(self->tsap, skb);
961 if(ret < 0)
964 * > IrTTPs tx queue is full, so we just have to
965 * > drop the frame! You might think that we should
966 * > just return -1 and don't deallocate the frame,
967 * > but that is dangerous since it's possible that
968 * > we have replaced the original skb with a new
969 * > one with larger headroom, and that would really
970 * > confuse do_dev_queue_xmit() in dev.c! I have
971 * > tried :-) DB
972 * Correction : we verify the flow control above (self->tx_flow),
973 * so we come here only if IrTTP doesn't like the packet (empty,
974 * too large, IrTTP not connected). In those rare cases, it's ok
975 * to drop it, we don't want to see it here again...
976 * Jean II
978 DERROR(PPP_ERROR, "IrTTP doesn't like this packet !!! (0x%X)\n", ret);
979 /* irttp_data_request already free the packet */
982 DEXIT(PPP_TRACE, "\n");
983 return 1; /* Packet has been consumed */
986 /*------------------------------------------------------------------*/
988 * Take care of the ioctls that ppp_generic doesn't want to deal with...
989 * Note : we are also called from dev_irnet_ioctl().
991 static int
992 ppp_irnet_ioctl(struct ppp_channel * chan,
993 unsigned int cmd,
994 unsigned long arg)
996 irnet_socket * ap = (struct irnet_socket *) chan->private;
997 int err;
998 int val;
999 u32 accm[8];
1000 void __user *argp = (void __user *)arg;
1002 DENTER(PPP_TRACE, "(channel=0x%p, ap=0x%p, cmd=0x%X)\n",
1003 chan, ap, cmd);
1005 /* Basic checks... */
1006 DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
1008 err = -EFAULT;
1009 switch(cmd)
1011 /* PPP flags */
1012 case PPPIOCGFLAGS:
1013 val = ap->flags | ap->rbits;
1014 if(put_user(val, (int __user *) argp))
1015 break;
1016 err = 0;
1017 break;
1018 case PPPIOCSFLAGS:
1019 if(get_user(val, (int __user *) argp))
1020 break;
1021 ap->flags = val & ~SC_RCV_BITS;
1022 ap->rbits = val & SC_RCV_BITS;
1023 err = 0;
1024 break;
1026 /* Async map stuff - all dummy to please pppd */
1027 case PPPIOCGASYNCMAP:
1028 if(put_user(ap->xaccm[0], (u32 __user *) argp))
1029 break;
1030 err = 0;
1031 break;
1032 case PPPIOCSASYNCMAP:
1033 if(get_user(ap->xaccm[0], (u32 __user *) argp))
1034 break;
1035 err = 0;
1036 break;
1037 case PPPIOCGRASYNCMAP:
1038 if(put_user(ap->raccm, (u32 __user *) argp))
1039 break;
1040 err = 0;
1041 break;
1042 case PPPIOCSRASYNCMAP:
1043 if(get_user(ap->raccm, (u32 __user *) argp))
1044 break;
1045 err = 0;
1046 break;
1047 case PPPIOCGXASYNCMAP:
1048 if(copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
1049 break;
1050 err = 0;
1051 break;
1052 case PPPIOCSXASYNCMAP:
1053 if(copy_from_user(accm, argp, sizeof(accm)))
1054 break;
1055 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
1056 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
1057 memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
1058 err = 0;
1059 break;
1061 /* Max PPP frame size */
1062 case PPPIOCGMRU:
1063 if(put_user(ap->mru, (int __user *) argp))
1064 break;
1065 err = 0;
1066 break;
1067 case PPPIOCSMRU:
1068 if(get_user(val, (int __user *) argp))
1069 break;
1070 if(val < PPP_MRU)
1071 val = PPP_MRU;
1072 ap->mru = val;
1073 err = 0;
1074 break;
1076 default:
1077 DEBUG(PPP_INFO, "Unsupported ioctl (0x%X)\n", cmd);
1078 err = -ENOIOCTLCMD;
1081 DEXIT(PPP_TRACE, " - err = 0x%X\n", err);
1082 return err;
1085 /************************** INITIALISATION **************************/
1087 * Module initialisation and all that jazz...
1090 /*------------------------------------------------------------------*/
1092 * Hook our device callbacks in the filesystem, to connect our code
1093 * to /dev/irnet
1095 static inline int __init
1096 ppp_irnet_init(void)
1098 int err = 0;
1100 DENTER(MODULE_TRACE, "()\n");
1102 /* Allocate ourselves as a minor in the misc range */
1103 err = misc_register(&irnet_misc_device);
1105 DEXIT(MODULE_TRACE, "\n");
1106 return err;
1109 /*------------------------------------------------------------------*/
1111 * Cleanup at exit...
1113 static inline void __exit
1114 ppp_irnet_cleanup(void)
1116 DENTER(MODULE_TRACE, "()\n");
1118 /* De-allocate /dev/irnet minor in misc range */
1119 misc_deregister(&irnet_misc_device);
1121 DEXIT(MODULE_TRACE, "\n");
1124 /*------------------------------------------------------------------*/
1126 * Module main entry point
1128 static int __init
1129 irnet_init(void)
1131 int err;
1133 /* Initialise both parts... */
1134 err = irda_irnet_init();
1135 if(!err)
1136 err = ppp_irnet_init();
1137 return err;
1140 /*------------------------------------------------------------------*/
1142 * Module exit
1144 static void __exit
1145 irnet_cleanup(void)
1147 irda_irnet_cleanup();
1148 ppp_irnet_cleanup();
1151 /*------------------------------------------------------------------*/
1153 * Module magic
1155 module_init(irnet_init);
1156 module_exit(irnet_cleanup);
1157 MODULE_AUTHOR("Jean Tourrilhes <jt@hpl.hp.com>");
1158 MODULE_DESCRIPTION("IrNET : Synchronous PPP over IrDA");
1159 MODULE_LICENSE("GPL");
1160 MODULE_ALIAS_CHARDEV(10, 187);