1 /* $Id: ioctl.c,v 1.17 2002/02/08 03:57:14 davem Exp $
2 * ioctl.c: Solaris ioctl emulation.
4 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5 * Copyright (C) 1997,1998 Patrik Rak (prak3264@ss1000.ms.mff.cuni.cz)
7 * Streams & timod emulation based on code
8 * Copyright (C) 1995, 1996 Mike Jagdis (jaggy@purplet.demon.co.uk)
10 * 1999-08-19 Implemented solaris 'm' (mag tape) and
11 * 'O' (openprom) ioctls, by Jason Rappleye
12 * (rappleye@ccr.buffalo.edu)
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/syscalls.h>
21 #include <linux/ioctl.h>
23 #include <linux/file.h>
24 #include <linux/netdevice.h>
25 #include <linux/mtio.h>
26 #include <linux/time.h>
27 #include <linux/rcupdate.h>
28 #include <linux/compat.h>
32 #include <asm/uaccess.h>
33 #include <asm/termios.h>
34 #include <asm/openpromio.h>
39 extern asmlinkage int compat_sys_ioctl(unsigned int fd, unsigned int cmd,
41 asmlinkage int solaris_ioctl(unsigned int fd, unsigned int cmd, u32 arg);
43 extern int timod_putmsg(unsigned int fd, char __user *ctl_buf, int ctl_len,
44 char __user *data_buf, int data_len, int flags);
45 extern int timod_getmsg(unsigned int fd, char __user *ctl_buf, int ctl_maxlen, int __user *ctl_len,
46 char __user *data_buf, int data_maxlen, int __user *data_len, int *flags);
48 /* termio* stuff {{{ */
50 struct solaris_termios {
58 struct solaris_termio {
67 struct solaris_termiox {
74 static u32 solaris_to_linux_cflag(u32 cflag)
77 if (cflag & 0x200000) {
78 int baud = cflag & 0xf;
81 case 0: baud = B57600; break;
82 case 1: baud = B76800; break;
83 case 2: baud = B115200; break;
84 case 3: baud = B153600; break;
85 case 4: baud = B230400; break;
86 case 5: baud = B307200; break;
87 case 6: baud = B460800; break;
89 cflag |= CBAUDEX | baud;
94 static u32 linux_to_solaris_cflag(u32 cflag)
96 cflag &= ~(CMSPAR | CIBAUD);
97 if (cflag & CBAUDEX) {
98 int baud = cflag & CBAUD;
101 case B57600: baud = 0; break;
102 case B76800: baud = 1; break;
103 case B115200: baud = 2; break;
104 case B153600: baud = 3; break;
105 case B230400: baud = 4; break;
106 case B307200: baud = 5; break;
107 case B460800: baud = 6; break;
108 case B614400: baud = 7; break;
109 case B921600: baud = 8; break;
111 case B1843200: baud = 9; break;
114 cflag |= 0x200000 | baud;
119 static inline int linux_to_solaris_termio(unsigned int fd, unsigned int cmd, u32 arg)
121 struct solaris_termio __user *p = A(arg);
124 ret = sys_ioctl(fd, cmd, (unsigned long)p);
128 if (__get_user (cflag, &p->c_cflag))
130 cflag = linux_to_solaris_cflag(cflag);
131 if (__put_user (cflag, &p->c_cflag))
137 static int solaris_to_linux_termio(unsigned int fd, unsigned int cmd, u32 arg)
140 struct solaris_termio s;
141 mm_segment_t old_fs = get_fs();
143 if (copy_from_user (&s, (struct solaris_termio __user *)A(arg), sizeof(struct solaris_termio)))
145 s.c_cflag = solaris_to_linux_cflag(s.c_cflag);
147 ret = sys_ioctl(fd, cmd, (unsigned long)&s);
152 static inline int linux_to_solaris_termios(unsigned int fd, unsigned int cmd, u32 arg)
155 struct solaris_termios s;
156 mm_segment_t old_fs = get_fs();
159 ret = sys_ioctl(fd, cmd, (unsigned long)&s);
162 struct solaris_termios __user *p = A(arg);
163 if (put_user (s.c_iflag, &p->c_iflag) ||
164 __put_user (s.c_oflag, &p->c_oflag) ||
165 __put_user (linux_to_solaris_cflag(s.c_cflag), &p->c_cflag) ||
166 __put_user (s.c_lflag, &p->c_lflag) ||
167 __copy_to_user (p->c_cc, s.c_cc, 16) ||
168 __clear_user (p->c_cc + 16, 2))
174 static int solaris_to_linux_termios(unsigned int fd, unsigned int cmd, u32 arg)
177 struct solaris_termios s;
178 struct solaris_termios __user *p = A(arg);
179 mm_segment_t old_fs = get_fs();
182 ret = sys_ioctl(fd, TCGETS, (unsigned long)&s);
185 if (put_user (s.c_iflag, &p->c_iflag) ||
186 __put_user (s.c_oflag, &p->c_oflag) ||
187 __put_user (s.c_cflag, &p->c_cflag) ||
188 __put_user (s.c_lflag, &p->c_lflag) ||
189 __copy_from_user (s.c_cc, p->c_cc, 16))
191 s.c_cflag = solaris_to_linux_cflag(s.c_cflag);
193 ret = sys_ioctl(fd, cmd, (unsigned long)&s);
198 static inline int solaris_T(unsigned int fd, unsigned int cmd, u32 arg)
200 switch (cmd & 0xff) {
202 return linux_to_solaris_termio(fd, TCGETA, arg);
204 return solaris_to_linux_termio(fd, TCSETA, arg);
205 case 3: /* TCSETAW */
206 return solaris_to_linux_termio(fd, TCSETAW, arg);
207 case 4: /* TCSETAF */
208 return solaris_to_linux_termio(fd, TCSETAF, arg);
210 return sys_ioctl(fd, TCSBRK, arg);
212 return sys_ioctl(fd, TCXONC, arg);
214 return sys_ioctl(fd, TCFLSH, arg);
215 case 13: /* TCGETS */
216 return linux_to_solaris_termios(fd, TCGETS, arg);
217 case 14: /* TCSETS */
218 return solaris_to_linux_termios(fd, TCSETS, arg);
219 case 15: /* TCSETSW */
220 return solaris_to_linux_termios(fd, TCSETSW, arg);
221 case 16: /* TCSETSF */
222 return solaris_to_linux_termios(fd, TCSETSF, arg);
223 case 103: /* TIOCSWINSZ */
224 return sys_ioctl(fd, TIOCSWINSZ, arg);
225 case 104: /* TIOCGWINSZ */
226 return sys_ioctl(fd, TIOCGWINSZ, arg);
231 static inline int solaris_t(unsigned int fd, unsigned int cmd, u32 arg)
233 switch (cmd & 0xff) {
234 case 20: /* TIOCGPGRP */
235 return sys_ioctl(fd, TIOCGPGRP, arg);
236 case 21: /* TIOCSPGRP */
237 return sys_ioctl(fd, TIOCSPGRP, arg);
244 /* A pseudo STREAMS support {{{ */
247 int cmd, timeout, len;
251 struct solaris_si_sockparams {
257 struct solaris_o_si_udata {
268 struct solaris_si_udata {
277 struct solaris_si_sockparams sockparams;
280 #define SOLARIS_MODULE_TIMOD 0
281 #define SOLARIS_MODULE_SOCKMOD 1
282 #define SOLARIS_MODULE_MAX 2
284 static struct module_info {
286 /* can be expanded further if needed */
287 } module_table[ SOLARIS_MODULE_MAX + 1 ] = {
288 /* the ordering here must match the module numbers above! */
294 static inline int solaris_sockmod(unsigned int fd, unsigned int cmd, u32 arg)
298 /* I wonder which of these tests are superfluous... --patrik */
300 fdt = files_fdtable(current->files);
302 ! fdt->fd[fd]->f_path.dentry ||
303 ! (ino = fdt->fd[fd]->f_path.dentry->d_inode) ||
304 ! S_ISSOCK(ino->i_mode)) {
310 switch (cmd & 0xff) {
311 case 109: /* SI_SOCKPARAMS */
313 struct solaris_si_sockparams si;
314 if (copy_from_user (&si, A(arg), sizeof(si)))
315 return (EFAULT << 8) | TSYSERR;
317 /* Should we modify socket ino->socket_i.ops and type? */
320 case 110: /* SI_GETUDATA */
322 int etsdusize, servtype;
323 struct solaris_si_udata __user *p = A(arg);
324 switch (SOCKET_I(ino)->type) {
334 if (put_user(16384, &p->tidusize) ||
335 __put_user(sizeof(struct sockaddr), &p->addrsize) ||
336 __put_user(-1, &p->optsize) ||
337 __put_user(etsdusize, &p->etsdusize) ||
338 __put_user(servtype, &p->servtype) ||
339 __put_user(0, &p->so_state) ||
340 __put_user(0, &p->so_options) ||
341 __put_user(16384, &p->tsdusize) ||
342 __put_user(SOCKET_I(ino)->ops->family, &p->sockparams.sp_family) ||
343 __put_user(SOCKET_I(ino)->type, &p->sockparams.sp_type) ||
344 __put_user(SOCKET_I(ino)->ops->family, &p->sockparams.sp_protocol))
345 return (EFAULT << 8) | TSYSERR;
348 case 101: /* O_SI_GETUDATA */
350 int etsdusize, servtype;
351 struct solaris_o_si_udata __user *p = A(arg);
352 switch (SOCKET_I(ino)->type) {
362 if (put_user(16384, &p->tidusize) ||
363 __put_user(sizeof(struct sockaddr), &p->addrsize) ||
364 __put_user(-1, &p->optsize) ||
365 __put_user(etsdusize, &p->etsdusize) ||
366 __put_user(servtype, &p->servtype) ||
367 __put_user(0, &p->so_state) ||
368 __put_user(0, &p->so_options) ||
369 __put_user(16384, &p->tsdusize))
370 return (EFAULT << 8) | TSYSERR;
373 case 102: /* SI_SHUTDOWN */
374 case 103: /* SI_LISTEN */
375 case 104: /* SI_SETMYNAME */
376 case 105: /* SI_SETPEERNAME */
377 case 106: /* SI_GETINTRANSIT */
378 case 107: /* SI_TCL_LINK */
379 case 108: /* SI_TCL_UNLINK */
385 static inline int solaris_timod(unsigned int fd, unsigned int cmd, u32 arg,
386 int len, int __user *len_p)
390 switch (cmd & 0xff) {
391 case 141: /* TI_OPTMGMT */
395 SOLD("TI_OPMGMT entry");
396 ret = timod_putmsg(fd, A(arg), len, NULL, -1, 0);
397 SOLD("timod_putmsg() returned");
399 return (-ret << 8) | TSYSERR;
401 SOLD("calling timod_getmsg()");
402 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
403 SOLD("timod_getmsg() returned");
405 return (-ret << 8) | TSYSERR;
407 if (get_user(prim, (u32 __user *)A(arg)))
408 return (EFAULT << 8) | TSYSERR;
410 if (prim == T_ERROR_ACK) {
412 SOLD("prim is T_ERROR_ACK");
413 if (get_user(tmp, (u32 __user *)A(arg)+3) ||
414 get_user(tmp2, (u32 __user *)A(arg)+2))
415 return (EFAULT << 8) | TSYSERR;
416 return (tmp2 << 8) | tmp;
418 SOLD("TI_OPMGMT return 0");
421 case 142: /* TI_BIND */
425 SOLD("TI_BIND entry");
426 ret = timod_putmsg(fd, A(arg), len, NULL, -1, 0);
427 SOLD("timod_putmsg() returned");
429 return (-ret << 8) | TSYSERR;
430 len = 1024; /* Solaris allows arbitrary return size */
432 SOLD("calling timod_getmsg()");
433 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
434 SOLD("timod_getmsg() returned");
436 return (-ret << 8) | TSYSERR;
438 if (get_user(prim, (u32 __user *)A(arg)))
439 return (EFAULT << 8) | TSYSERR;
441 if (prim == T_ERROR_ACK) {
443 SOLD("prim is T_ERROR_ACK");
444 if (get_user(tmp, (u32 __user *)A(arg)+3) ||
445 get_user(tmp2, (u32 __user *)A(arg)+2))
446 return (EFAULT << 8) | TSYSERR;
447 return (tmp2 << 8) | tmp;
449 SOLD("no ERROR_ACK requested");
450 if (prim != T_OK_ACK)
452 SOLD("OK_ACK requested");
454 SOLD("calling timod_getmsg()");
455 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
456 SOLD("timod_getmsg() returned");
458 return (-ret << 8) | TSYSERR;
459 SOLD("TI_BIND return ok");
462 case 140: /* TI_GETINFO */
463 case 143: /* TI_UNBIND */
464 case 144: /* TI_GETMYNAME */
465 case 145: /* TI_GETPEERNAME */
466 case 146: /* TI_SETMYNAME */
467 case 147: /* TI_SETPEERNAME */
473 static inline int solaris_S(struct file *filp, unsigned int fd, unsigned int cmd, u32 arg)
480 struct sol_socket_struct *sock;
481 struct module_info *mi;
483 ino = filp->f_path.dentry->d_inode;
484 if (!S_ISSOCK(ino->i_mode))
486 sock = filp->private_data;
488 printk("solaris_S: NULL private_data\n");
491 if (sock->magic != SOLARIS_SOCKET_MAGIC) {
492 printk("solaris_S: invalid magic\n");
497 switch (cmd & 0xff) {
498 case 1: /* I_NREAD */
502 p = getname (A(arg));
506 for (mi = module_table; mi->name; mi++) {
507 if (strcmp(mi->name, p) == 0) {
509 if (sock->modcount >= MAX_NR_STREAM_MODULES) {
513 m = (sol_module) (mi - module_table);
514 sock->module[sock->modcount++] = m;
523 if (sock->modcount <= 0) return -EINVAL;
529 if (sock->modcount <= 0) return -EINVAL;
530 p = module_table[(unsigned)sock->module[sock->modcount]].name;
531 if (copy_to_user (A(arg), p, strlen(p)))
535 case 5: /* I_FLUSH */
538 if (copy_from_user(&si, A(arg), sizeof(struct strioctl)))
540 /* We ignore what module is actually at the top of stack. */
541 switch ((si.cmd >> 8) & 0xff) {
543 return solaris_sockmod(fd, si.cmd, si.data);
545 return solaris_timod(fd, si.cmd, si.data, si.len,
546 &((struct strioctl __user *)A(arg))->len);
548 return solaris_ioctl(fd, si.cmd, si.data);
550 case 9: /* I_SETSIG */
551 return sys_ioctl(fd, FIOSETOWN, current->pid);
552 case 10: /* I_GETSIG */
555 sys_ioctl(fd, FIOGETOWN, (unsigned long)&ret);
557 if (ret == current->pid) return 0x3ff;
559 case 11: /* I_FIND */
562 p = getname (A(arg));
566 for (i = 0; i < sock->modcount; i++) {
567 unsigned m = sock->module[i];
568 if (strcmp(module_table[m].name, p) == 0) {
576 case 19: /* I_SWROPT */
577 case 32: /* I_SETCLTIME */
583 static inline int solaris_s(unsigned int fd, unsigned int cmd, u32 arg)
585 switch (cmd & 0xff) {
586 case 0: /* SIOCSHIWAT */
587 case 2: /* SIOCSLOWAT */
588 return 0; /* We don't support them */
589 case 1: /* SIOCGHIWAT */
590 case 3: /* SIOCGLOWAT */
591 if (put_user (0, (u32 __user *)A(arg)))
594 case 7: /* SIOCATMARK */
595 return sys_ioctl(fd, SIOCATMARK, arg);
596 case 8: /* SIOCSPGRP */
597 return sys_ioctl(fd, SIOCSPGRP, arg);
598 case 9: /* SIOCGPGRP */
599 return sys_ioctl(fd, SIOCGPGRP, arg);
604 static inline int solaris_r(unsigned int fd, unsigned int cmd, u32 arg)
606 switch (cmd & 0xff) {
607 case 10: /* SIOCADDRT */
608 return compat_sys_ioctl(fd, SIOCADDRT, arg);
609 case 11: /* SIOCDELRT */
610 return compat_sys_ioctl(fd, SIOCDELRT, arg);
615 static inline int solaris_i(unsigned int fd, unsigned int cmd, u32 arg)
617 switch (cmd & 0xff) {
618 case 12: /* SIOCSIFADDR */
619 return compat_sys_ioctl(fd, SIOCSIFADDR, arg);
620 case 13: /* SIOCGIFADDR */
621 return compat_sys_ioctl(fd, SIOCGIFADDR, arg);
622 case 14: /* SIOCSIFDSTADDR */
623 return compat_sys_ioctl(fd, SIOCSIFDSTADDR, arg);
624 case 15: /* SIOCGIFDSTADDR */
625 return compat_sys_ioctl(fd, SIOCGIFDSTADDR, arg);
626 case 16: /* SIOCSIFFLAGS */
627 return compat_sys_ioctl(fd, SIOCSIFFLAGS, arg);
628 case 17: /* SIOCGIFFLAGS */
629 return compat_sys_ioctl(fd, SIOCGIFFLAGS, arg);
630 case 18: /* SIOCSIFMEM */
631 return compat_sys_ioctl(fd, SIOCSIFMEM, arg);
632 case 19: /* SIOCGIFMEM */
633 return compat_sys_ioctl(fd, SIOCGIFMEM, arg);
634 case 20: /* SIOCGIFCONF */
635 return compat_sys_ioctl(fd, SIOCGIFCONF, arg);
636 case 21: /* SIOCSIFMTU */
637 return compat_sys_ioctl(fd, SIOCSIFMTU, arg);
638 case 22: /* SIOCGIFMTU */
639 return compat_sys_ioctl(fd, SIOCGIFMTU, arg);
640 case 23: /* SIOCGIFBRDADDR */
641 return compat_sys_ioctl(fd, SIOCGIFBRDADDR, arg);
642 case 24: /* SIOCSIFBRDADDR */
643 return compat_sys_ioctl(fd, SIOCSIFBRDADDR, arg);
644 case 25: /* SIOCGIFNETMASK */
645 return compat_sys_ioctl(fd, SIOCGIFNETMASK, arg);
646 case 26: /* SIOCSIFNETMASK */
647 return compat_sys_ioctl(fd, SIOCSIFNETMASK, arg);
648 case 27: /* SIOCGIFMETRIC */
649 return compat_sys_ioctl(fd, SIOCGIFMETRIC, arg);
650 case 28: /* SIOCSIFMETRIC */
651 return compat_sys_ioctl(fd, SIOCSIFMETRIC, arg);
652 case 30: /* SIOCSARP */
653 return compat_sys_ioctl(fd, SIOCSARP, arg);
654 case 31: /* SIOCGARP */
655 return compat_sys_ioctl(fd, SIOCGARP, arg);
656 case 32: /* SIOCDARP */
657 return compat_sys_ioctl(fd, SIOCDARP, arg);
658 case 52: /* SIOCGETNAME */
659 case 53: /* SIOCGETPEER */
661 struct sockaddr uaddr;
662 int uaddr_len = sizeof(struct sockaddr), ret;
664 mm_segment_t old_fs = get_fs();
665 int (*sys_socketcall)(int, unsigned long *) =
666 (int (*)(int, unsigned long *))SYS(socketcall);
668 args[0] = fd; args[1] = (long)&uaddr; args[2] = (long)&uaddr_len;
670 ret = sys_socketcall(((cmd & 0xff) == 52) ? SYS_GETSOCKNAME : SYS_GETPEERNAME,
674 if (copy_to_user(A(arg), &uaddr, uaddr_len))
680 case 86: /* SIOCSOCKSYS */
681 return socksys_syscall(fd, arg);
683 case 87: /* SIOCGIFNUM */
685 struct net_device *d;
688 read_lock_bh(&dev_base_lock);
691 read_unlock_bh(&dev_base_lock);
693 if (put_user (i, (int __user *)A(arg)))
701 static int solaris_m(unsigned int fd, unsigned int cmd, u32 arg)
705 switch (cmd & 0xff) {
706 case 1: /* MTIOCTOP */
707 ret = sys_ioctl(fd, MTIOCTOP, (unsigned long)&arg);
709 case 2: /* MTIOCGET */
710 ret = sys_ioctl(fd, MTIOCGET, (unsigned long)&arg);
712 case 3: /* MTIOCGETDRIVETYPE */
713 case 4: /* MTIOCPERSISTENT */
714 case 5: /* MTIOCPERSISTENTSTATUS */
715 case 6: /* MTIOCLRERR */
716 case 7: /* MTIOCGUARANTEEDORDER */
717 case 8: /* MTIOCRESERVE */
718 case 9: /* MTIOCRELEASE */
719 case 10: /* MTIOCFORCERESERVE */
720 case 13: /* MTIOCSTATE */
721 case 14: /* MTIOCREADIGNOREILI */
722 case 15: /* MTIOCREADIGNOREEOFS */
723 case 16: /* MTIOCSHORTFMK */
725 ret = -ENOSYS; /* linux doesn't support these */
732 static int solaris_O(unsigned int fd, unsigned int cmd, u32 arg)
736 switch (cmd & 0xff) {
737 case 1: /* OPROMGETOPT */
738 ret = sys_ioctl(fd, OPROMGETOPT, arg);
740 case 2: /* OPROMSETOPT */
741 ret = sys_ioctl(fd, OPROMSETOPT, arg);
743 case 3: /* OPROMNXTOPT */
744 ret = sys_ioctl(fd, OPROMNXTOPT, arg);
746 case 4: /* OPROMSETOPT2 */
747 ret = sys_ioctl(fd, OPROMSETOPT2, arg);
749 case 5: /* OPROMNEXT */
750 ret = sys_ioctl(fd, OPROMNEXT, arg);
752 case 6: /* OPROMCHILD */
753 ret = sys_ioctl(fd, OPROMCHILD, arg);
755 case 7: /* OPROMGETPROP */
756 ret = sys_ioctl(fd, OPROMGETPROP, arg);
758 case 8: /* OPROMNXTPROP */
759 ret = sys_ioctl(fd, OPROMNXTPROP, arg);
761 case 9: /* OPROMU2P */
762 ret = sys_ioctl(fd, OPROMU2P, arg);
764 case 10: /* OPROMGETCONS */
765 ret = sys_ioctl(fd, OPROMGETCONS, arg);
767 case 11: /* OPROMGETFBNAME */
768 ret = sys_ioctl(fd, OPROMGETFBNAME, arg);
770 case 12: /* OPROMGETBOOTARGS */
771 ret = sys_ioctl(fd, OPROMGETBOOTARGS, arg);
773 case 13: /* OPROMGETVERSION */
774 case 14: /* OPROMPATH2DRV */
775 case 15: /* OPROMDEV2PROMNAME */
776 case 16: /* OPROMPROM2DEVNAME */
777 case 17: /* OPROMGETPROPLEN */
787 asmlinkage int solaris_ioctl(unsigned int fd, unsigned int cmd, u32 arg)
798 switch ((cmd >> 8) & 0xff) {
799 case 'S': error = solaris_S(filp, fd, cmd, arg); break;
800 case 'T': error = solaris_T(fd, cmd, arg); break;
801 case 'i': error = solaris_i(fd, cmd, arg); break;
802 case 'r': error = solaris_r(fd, cmd, arg); break;
803 case 's': error = solaris_s(fd, cmd, arg); break;
804 case 't': error = solaris_t(fd, cmd, arg); break;
805 case 'f': error = sys_ioctl(fd, cmd, arg); break;
806 case 'm': error = solaris_m(fd, cmd, arg); break;
807 case 'O': error = solaris_O(fd, cmd, arg); break;
815 if (error == -ENOSYS) {
816 unsigned char c = cmd>>8;
818 if (c < ' ' || c > 126) c = '.';
819 printk("solaris_ioctl: Unknown cmd fd(%d) cmd(%08x '%c') arg(%08x)\n",
820 (int)fd, (unsigned int)cmd, c, (unsigned int)arg);