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>
31 #include <net/net_namespace.h>
33 #include <asm/uaccess.h>
34 #include <asm/termios.h>
35 #include <asm/openpromio.h>
40 extern asmlinkage int compat_sys_ioctl(unsigned int fd, unsigned int cmd,
42 asmlinkage int solaris_ioctl(unsigned int fd, unsigned int cmd, u32 arg);
44 extern int timod_putmsg(unsigned int fd, char __user *ctl_buf, int ctl_len,
45 char __user *data_buf, int data_len, int flags);
46 extern int timod_getmsg(unsigned int fd, char __user *ctl_buf, int ctl_maxlen, int __user *ctl_len,
47 char __user *data_buf, int data_maxlen, int __user *data_len, int *flags);
49 /* termio* stuff {{{ */
51 struct solaris_termios {
59 struct solaris_termio {
68 struct solaris_termiox {
75 static u32 solaris_to_linux_cflag(u32 cflag)
78 if (cflag & 0x200000) {
79 int baud = cflag & 0xf;
82 case 0: baud = B57600; break;
83 case 1: baud = B76800; break;
84 case 2: baud = B115200; break;
85 case 3: baud = B153600; break;
86 case 4: baud = B230400; break;
87 case 5: baud = B307200; break;
88 case 6: baud = B460800; break;
90 cflag |= CBAUDEX | baud;
95 static u32 linux_to_solaris_cflag(u32 cflag)
97 cflag &= ~(CMSPAR | CIBAUD);
98 if (cflag & CBAUDEX) {
99 int baud = cflag & CBAUD;
102 case B57600: baud = 0; break;
103 case B76800: baud = 1; break;
104 case B115200: baud = 2; break;
105 case B153600: baud = 3; break;
106 case B230400: baud = 4; break;
107 case B307200: baud = 5; break;
108 case B460800: baud = 6; break;
109 case B614400: baud = 7; break;
110 case B921600: baud = 8; break;
112 case B1843200: baud = 9; break;
115 cflag |= 0x200000 | baud;
120 static inline int linux_to_solaris_termio(unsigned int fd, unsigned int cmd, u32 arg)
122 struct solaris_termio __user *p = A(arg);
125 ret = sys_ioctl(fd, cmd, (unsigned long)p);
129 if (__get_user (cflag, &p->c_cflag))
131 cflag = linux_to_solaris_cflag(cflag);
132 if (__put_user (cflag, &p->c_cflag))
138 static int solaris_to_linux_termio(unsigned int fd, unsigned int cmd, u32 arg)
141 struct solaris_termio s;
142 mm_segment_t old_fs = get_fs();
144 if (copy_from_user (&s, (struct solaris_termio __user *)A(arg), sizeof(struct solaris_termio)))
146 s.c_cflag = solaris_to_linux_cflag(s.c_cflag);
148 ret = sys_ioctl(fd, cmd, (unsigned long)&s);
153 static inline int linux_to_solaris_termios(unsigned int fd, unsigned int cmd, u32 arg)
156 struct solaris_termios s;
157 mm_segment_t old_fs = get_fs();
160 ret = sys_ioctl(fd, cmd, (unsigned long)&s);
163 struct solaris_termios __user *p = A(arg);
164 if (put_user (s.c_iflag, &p->c_iflag) ||
165 __put_user (s.c_oflag, &p->c_oflag) ||
166 __put_user (linux_to_solaris_cflag(s.c_cflag), &p->c_cflag) ||
167 __put_user (s.c_lflag, &p->c_lflag) ||
168 __copy_to_user (p->c_cc, s.c_cc, 16) ||
169 __clear_user (p->c_cc + 16, 2))
175 static int solaris_to_linux_termios(unsigned int fd, unsigned int cmd, u32 arg)
178 struct solaris_termios s;
179 struct solaris_termios __user *p = A(arg);
180 mm_segment_t old_fs = get_fs();
183 ret = sys_ioctl(fd, TCGETS, (unsigned long)&s);
186 if (put_user (s.c_iflag, &p->c_iflag) ||
187 __put_user (s.c_oflag, &p->c_oflag) ||
188 __put_user (s.c_cflag, &p->c_cflag) ||
189 __put_user (s.c_lflag, &p->c_lflag) ||
190 __copy_from_user (s.c_cc, p->c_cc, 16))
192 s.c_cflag = solaris_to_linux_cflag(s.c_cflag);
194 ret = sys_ioctl(fd, cmd, (unsigned long)&s);
199 static inline int solaris_T(unsigned int fd, unsigned int cmd, u32 arg)
201 switch (cmd & 0xff) {
203 return linux_to_solaris_termio(fd, TCGETA, arg);
205 return solaris_to_linux_termio(fd, TCSETA, arg);
206 case 3: /* TCSETAW */
207 return solaris_to_linux_termio(fd, TCSETAW, arg);
208 case 4: /* TCSETAF */
209 return solaris_to_linux_termio(fd, TCSETAF, arg);
211 return sys_ioctl(fd, TCSBRK, arg);
213 return sys_ioctl(fd, TCXONC, arg);
215 return sys_ioctl(fd, TCFLSH, arg);
216 case 13: /* TCGETS */
217 return linux_to_solaris_termios(fd, TCGETS, arg);
218 case 14: /* TCSETS */
219 return solaris_to_linux_termios(fd, TCSETS, arg);
220 case 15: /* TCSETSW */
221 return solaris_to_linux_termios(fd, TCSETSW, arg);
222 case 16: /* TCSETSF */
223 return solaris_to_linux_termios(fd, TCSETSF, arg);
224 case 103: /* TIOCSWINSZ */
225 return sys_ioctl(fd, TIOCSWINSZ, arg);
226 case 104: /* TIOCGWINSZ */
227 return sys_ioctl(fd, TIOCGWINSZ, arg);
232 static inline int solaris_t(unsigned int fd, unsigned int cmd, u32 arg)
234 switch (cmd & 0xff) {
235 case 20: /* TIOCGPGRP */
236 return sys_ioctl(fd, TIOCGPGRP, arg);
237 case 21: /* TIOCSPGRP */
238 return sys_ioctl(fd, TIOCSPGRP, arg);
245 /* A pseudo STREAMS support {{{ */
248 int cmd, timeout, len;
252 struct solaris_si_sockparams {
258 struct solaris_o_si_udata {
269 struct solaris_si_udata {
278 struct solaris_si_sockparams sockparams;
281 #define SOLARIS_MODULE_TIMOD 0
282 #define SOLARIS_MODULE_SOCKMOD 1
283 #define SOLARIS_MODULE_MAX 2
285 static struct module_info {
287 /* can be expanded further if needed */
288 } module_table[ SOLARIS_MODULE_MAX + 1 ] = {
289 /* the ordering here must match the module numbers above! */
295 static inline int solaris_sockmod(unsigned int fd, unsigned int cmd, u32 arg)
299 /* I wonder which of these tests are superfluous... --patrik */
301 fdt = files_fdtable(current->files);
303 ! fdt->fd[fd]->f_path.dentry ||
304 ! (ino = fdt->fd[fd]->f_path.dentry->d_inode) ||
305 ! S_ISSOCK(ino->i_mode)) {
311 switch (cmd & 0xff) {
312 case 109: /* SI_SOCKPARAMS */
314 struct solaris_si_sockparams si;
315 if (copy_from_user (&si, A(arg), sizeof(si)))
316 return (EFAULT << 8) | TSYSERR;
318 /* Should we modify socket ino->socket_i.ops and type? */
321 case 110: /* SI_GETUDATA */
323 int etsdusize, servtype;
324 struct solaris_si_udata __user *p = A(arg);
325 switch (SOCKET_I(ino)->type) {
335 if (put_user(16384, &p->tidusize) ||
336 __put_user(sizeof(struct sockaddr), &p->addrsize) ||
337 __put_user(-1, &p->optsize) ||
338 __put_user(etsdusize, &p->etsdusize) ||
339 __put_user(servtype, &p->servtype) ||
340 __put_user(0, &p->so_state) ||
341 __put_user(0, &p->so_options) ||
342 __put_user(16384, &p->tsdusize) ||
343 __put_user(SOCKET_I(ino)->ops->family, &p->sockparams.sp_family) ||
344 __put_user(SOCKET_I(ino)->type, &p->sockparams.sp_type) ||
345 __put_user(SOCKET_I(ino)->ops->family, &p->sockparams.sp_protocol))
346 return (EFAULT << 8) | TSYSERR;
349 case 101: /* O_SI_GETUDATA */
351 int etsdusize, servtype;
352 struct solaris_o_si_udata __user *p = A(arg);
353 switch (SOCKET_I(ino)->type) {
363 if (put_user(16384, &p->tidusize) ||
364 __put_user(sizeof(struct sockaddr), &p->addrsize) ||
365 __put_user(-1, &p->optsize) ||
366 __put_user(etsdusize, &p->etsdusize) ||
367 __put_user(servtype, &p->servtype) ||
368 __put_user(0, &p->so_state) ||
369 __put_user(0, &p->so_options) ||
370 __put_user(16384, &p->tsdusize))
371 return (EFAULT << 8) | TSYSERR;
374 case 102: /* SI_SHUTDOWN */
375 case 103: /* SI_LISTEN */
376 case 104: /* SI_SETMYNAME */
377 case 105: /* SI_SETPEERNAME */
378 case 106: /* SI_GETINTRANSIT */
379 case 107: /* SI_TCL_LINK */
380 case 108: /* SI_TCL_UNLINK */
386 static inline int solaris_timod(unsigned int fd, unsigned int cmd, u32 arg,
387 int len, int __user *len_p)
391 switch (cmd & 0xff) {
392 case 141: /* TI_OPTMGMT */
396 SOLD("TI_OPMGMT entry");
397 ret = timod_putmsg(fd, A(arg), len, NULL, -1, 0);
398 SOLD("timod_putmsg() returned");
400 return (-ret << 8) | TSYSERR;
402 SOLD("calling timod_getmsg()");
403 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
404 SOLD("timod_getmsg() returned");
406 return (-ret << 8) | TSYSERR;
408 if (get_user(prim, (u32 __user *)A(arg)))
409 return (EFAULT << 8) | TSYSERR;
411 if (prim == T_ERROR_ACK) {
413 SOLD("prim is T_ERROR_ACK");
414 if (get_user(tmp, (u32 __user *)A(arg)+3) ||
415 get_user(tmp2, (u32 __user *)A(arg)+2))
416 return (EFAULT << 8) | TSYSERR;
417 return (tmp2 << 8) | tmp;
419 SOLD("TI_OPMGMT return 0");
422 case 142: /* TI_BIND */
426 SOLD("TI_BIND entry");
427 ret = timod_putmsg(fd, A(arg), len, NULL, -1, 0);
428 SOLD("timod_putmsg() returned");
430 return (-ret << 8) | TSYSERR;
431 len = 1024; /* Solaris allows arbitrary return size */
433 SOLD("calling timod_getmsg()");
434 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
435 SOLD("timod_getmsg() returned");
437 return (-ret << 8) | TSYSERR;
439 if (get_user(prim, (u32 __user *)A(arg)))
440 return (EFAULT << 8) | TSYSERR;
442 if (prim == T_ERROR_ACK) {
444 SOLD("prim is T_ERROR_ACK");
445 if (get_user(tmp, (u32 __user *)A(arg)+3) ||
446 get_user(tmp2, (u32 __user *)A(arg)+2))
447 return (EFAULT << 8) | TSYSERR;
448 return (tmp2 << 8) | tmp;
450 SOLD("no ERROR_ACK requested");
451 if (prim != T_OK_ACK)
453 SOLD("OK_ACK requested");
455 SOLD("calling timod_getmsg()");
456 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
457 SOLD("timod_getmsg() returned");
459 return (-ret << 8) | TSYSERR;
460 SOLD("TI_BIND return ok");
463 case 140: /* TI_GETINFO */
464 case 143: /* TI_UNBIND */
465 case 144: /* TI_GETMYNAME */
466 case 145: /* TI_GETPEERNAME */
467 case 146: /* TI_SETMYNAME */
468 case 147: /* TI_SETPEERNAME */
474 static inline int solaris_S(struct file *filp, unsigned int fd, unsigned int cmd, u32 arg)
481 struct sol_socket_struct *sock;
482 struct module_info *mi;
484 ino = filp->f_path.dentry->d_inode;
485 if (!S_ISSOCK(ino->i_mode))
487 sock = filp->private_data;
489 printk("solaris_S: NULL private_data\n");
492 if (sock->magic != SOLARIS_SOCKET_MAGIC) {
493 printk("solaris_S: invalid magic\n");
498 switch (cmd & 0xff) {
499 case 1: /* I_NREAD */
503 p = getname (A(arg));
507 for (mi = module_table; mi->name; mi++) {
508 if (strcmp(mi->name, p) == 0) {
510 if (sock->modcount >= MAX_NR_STREAM_MODULES) {
514 m = (sol_module) (mi - module_table);
515 sock->module[sock->modcount++] = m;
524 if (sock->modcount <= 0) return -EINVAL;
530 if (sock->modcount <= 0) return -EINVAL;
531 p = module_table[(unsigned)sock->module[sock->modcount]].name;
532 if (copy_to_user (A(arg), p, strlen(p)))
536 case 5: /* I_FLUSH */
539 if (copy_from_user(&si, A(arg), sizeof(struct strioctl)))
541 /* We ignore what module is actually at the top of stack. */
542 switch ((si.cmd >> 8) & 0xff) {
544 return solaris_sockmod(fd, si.cmd, si.data);
546 return solaris_timod(fd, si.cmd, si.data, si.len,
547 &((struct strioctl __user *)A(arg))->len);
549 return solaris_ioctl(fd, si.cmd, si.data);
551 case 9: /* I_SETSIG */
552 return sys_ioctl(fd, FIOSETOWN, current->pid);
553 case 10: /* I_GETSIG */
556 sys_ioctl(fd, FIOGETOWN, (unsigned long)&ret);
558 if (ret == current->pid) return 0x3ff;
560 case 11: /* I_FIND */
563 p = getname (A(arg));
567 for (i = 0; i < sock->modcount; i++) {
568 unsigned m = sock->module[i];
569 if (strcmp(module_table[m].name, p) == 0) {
577 case 19: /* I_SWROPT */
578 case 32: /* I_SETCLTIME */
584 static inline int solaris_s(unsigned int fd, unsigned int cmd, u32 arg)
586 switch (cmd & 0xff) {
587 case 0: /* SIOCSHIWAT */
588 case 2: /* SIOCSLOWAT */
589 return 0; /* We don't support them */
590 case 1: /* SIOCGHIWAT */
591 case 3: /* SIOCGLOWAT */
592 if (put_user (0, (u32 __user *)A(arg)))
595 case 7: /* SIOCATMARK */
596 return sys_ioctl(fd, SIOCATMARK, arg);
597 case 8: /* SIOCSPGRP */
598 return sys_ioctl(fd, SIOCSPGRP, arg);
599 case 9: /* SIOCGPGRP */
600 return sys_ioctl(fd, SIOCGPGRP, arg);
605 static inline int solaris_r(unsigned int fd, unsigned int cmd, u32 arg)
607 switch (cmd & 0xff) {
608 case 10: /* SIOCADDRT */
609 return compat_sys_ioctl(fd, SIOCADDRT, arg);
610 case 11: /* SIOCDELRT */
611 return compat_sys_ioctl(fd, SIOCDELRT, arg);
616 static inline int solaris_i(unsigned int fd, unsigned int cmd, u32 arg)
618 switch (cmd & 0xff) {
619 case 12: /* SIOCSIFADDR */
620 return compat_sys_ioctl(fd, SIOCSIFADDR, arg);
621 case 13: /* SIOCGIFADDR */
622 return compat_sys_ioctl(fd, SIOCGIFADDR, arg);
623 case 14: /* SIOCSIFDSTADDR */
624 return compat_sys_ioctl(fd, SIOCSIFDSTADDR, arg);
625 case 15: /* SIOCGIFDSTADDR */
626 return compat_sys_ioctl(fd, SIOCGIFDSTADDR, arg);
627 case 16: /* SIOCSIFFLAGS */
628 return compat_sys_ioctl(fd, SIOCSIFFLAGS, arg);
629 case 17: /* SIOCGIFFLAGS */
630 return compat_sys_ioctl(fd, SIOCGIFFLAGS, arg);
631 case 18: /* SIOCSIFMEM */
632 return compat_sys_ioctl(fd, SIOCSIFMEM, arg);
633 case 19: /* SIOCGIFMEM */
634 return compat_sys_ioctl(fd, SIOCGIFMEM, arg);
635 case 20: /* SIOCGIFCONF */
636 return compat_sys_ioctl(fd, SIOCGIFCONF, arg);
637 case 21: /* SIOCSIFMTU */
638 return compat_sys_ioctl(fd, SIOCSIFMTU, arg);
639 case 22: /* SIOCGIFMTU */
640 return compat_sys_ioctl(fd, SIOCGIFMTU, arg);
641 case 23: /* SIOCGIFBRDADDR */
642 return compat_sys_ioctl(fd, SIOCGIFBRDADDR, arg);
643 case 24: /* SIOCSIFBRDADDR */
644 return compat_sys_ioctl(fd, SIOCSIFBRDADDR, arg);
645 case 25: /* SIOCGIFNETMASK */
646 return compat_sys_ioctl(fd, SIOCGIFNETMASK, arg);
647 case 26: /* SIOCSIFNETMASK */
648 return compat_sys_ioctl(fd, SIOCSIFNETMASK, arg);
649 case 27: /* SIOCGIFMETRIC */
650 return compat_sys_ioctl(fd, SIOCGIFMETRIC, arg);
651 case 28: /* SIOCSIFMETRIC */
652 return compat_sys_ioctl(fd, SIOCSIFMETRIC, arg);
653 case 30: /* SIOCSARP */
654 return compat_sys_ioctl(fd, SIOCSARP, arg);
655 case 31: /* SIOCGARP */
656 return compat_sys_ioctl(fd, SIOCGARP, arg);
657 case 32: /* SIOCDARP */
658 return compat_sys_ioctl(fd, SIOCDARP, arg);
659 case 52: /* SIOCGETNAME */
660 case 53: /* SIOCGETPEER */
662 struct sockaddr uaddr;
663 int uaddr_len = sizeof(struct sockaddr), ret;
665 mm_segment_t old_fs = get_fs();
666 int (*sys_socketcall)(int, unsigned long *) =
667 (int (*)(int, unsigned long *))SYS(socketcall);
669 args[0] = fd; args[1] = (long)&uaddr; args[2] = (long)&uaddr_len;
671 ret = sys_socketcall(((cmd & 0xff) == 52) ? SYS_GETSOCKNAME : SYS_GETPEERNAME,
675 if (copy_to_user(A(arg), &uaddr, uaddr_len))
681 case 86: /* SIOCSOCKSYS */
682 return socksys_syscall(fd, arg);
684 case 87: /* SIOCGIFNUM */
686 struct net_device *d;
689 read_lock_bh(&dev_base_lock);
690 for_each_netdev(&init_net, d)
692 read_unlock_bh(&dev_base_lock);
694 if (put_user (i, (int __user *)A(arg)))
702 static int solaris_m(unsigned int fd, unsigned int cmd, u32 arg)
706 switch (cmd & 0xff) {
707 case 1: /* MTIOCTOP */
708 ret = sys_ioctl(fd, MTIOCTOP, (unsigned long)&arg);
710 case 2: /* MTIOCGET */
711 ret = sys_ioctl(fd, MTIOCGET, (unsigned long)&arg);
713 case 3: /* MTIOCGETDRIVETYPE */
714 case 4: /* MTIOCPERSISTENT */
715 case 5: /* MTIOCPERSISTENTSTATUS */
716 case 6: /* MTIOCLRERR */
717 case 7: /* MTIOCGUARANTEEDORDER */
718 case 8: /* MTIOCRESERVE */
719 case 9: /* MTIOCRELEASE */
720 case 10: /* MTIOCFORCERESERVE */
721 case 13: /* MTIOCSTATE */
722 case 14: /* MTIOCREADIGNOREILI */
723 case 15: /* MTIOCREADIGNOREEOFS */
724 case 16: /* MTIOCSHORTFMK */
726 ret = -ENOSYS; /* linux doesn't support these */
733 static int solaris_O(unsigned int fd, unsigned int cmd, u32 arg)
737 switch (cmd & 0xff) {
738 case 1: /* OPROMGETOPT */
739 ret = sys_ioctl(fd, OPROMGETOPT, arg);
741 case 2: /* OPROMSETOPT */
742 ret = sys_ioctl(fd, OPROMSETOPT, arg);
744 case 3: /* OPROMNXTOPT */
745 ret = sys_ioctl(fd, OPROMNXTOPT, arg);
747 case 4: /* OPROMSETOPT2 */
748 ret = sys_ioctl(fd, OPROMSETOPT2, arg);
750 case 5: /* OPROMNEXT */
751 ret = sys_ioctl(fd, OPROMNEXT, arg);
753 case 6: /* OPROMCHILD */
754 ret = sys_ioctl(fd, OPROMCHILD, arg);
756 case 7: /* OPROMGETPROP */
757 ret = sys_ioctl(fd, OPROMGETPROP, arg);
759 case 8: /* OPROMNXTPROP */
760 ret = sys_ioctl(fd, OPROMNXTPROP, arg);
762 case 9: /* OPROMU2P */
763 ret = sys_ioctl(fd, OPROMU2P, arg);
765 case 10: /* OPROMGETCONS */
766 ret = sys_ioctl(fd, OPROMGETCONS, arg);
768 case 11: /* OPROMGETFBNAME */
769 ret = sys_ioctl(fd, OPROMGETFBNAME, arg);
771 case 12: /* OPROMGETBOOTARGS */
772 ret = sys_ioctl(fd, OPROMGETBOOTARGS, arg);
774 case 13: /* OPROMGETVERSION */
775 case 14: /* OPROMPATH2DRV */
776 case 15: /* OPROMDEV2PROMNAME */
777 case 16: /* OPROMPROM2DEVNAME */
778 case 17: /* OPROMGETPROPLEN */
788 asmlinkage int solaris_ioctl(unsigned int fd, unsigned int cmd, u32 arg)
799 switch ((cmd >> 8) & 0xff) {
800 case 'S': error = solaris_S(filp, fd, cmd, arg); break;
801 case 'T': error = solaris_T(fd, cmd, arg); break;
802 case 'i': error = solaris_i(fd, cmd, arg); break;
803 case 'r': error = solaris_r(fd, cmd, arg); break;
804 case 's': error = solaris_s(fd, cmd, arg); break;
805 case 't': error = solaris_t(fd, cmd, arg); break;
806 case 'f': error = sys_ioctl(fd, cmd, arg); break;
807 case 'm': error = solaris_m(fd, cmd, arg); break;
808 case 'O': error = solaris_O(fd, cmd, arg); break;
816 if (error == -ENOSYS) {
817 unsigned char c = cmd>>8;
819 if (c < ' ' || c > 126) c = '.';
820 printk("solaris_ioctl: Unknown cmd fd(%d) cmd(%08x '%c') arg(%08x)\n",
821 (int)fd, (unsigned int)cmd, c, (unsigned int)arg);