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/compat.h>
31 #include <asm/uaccess.h>
32 #include <asm/termios.h>
33 #include <asm/openpromio.h>
38 extern asmlinkage int compat_sys_ioctl(unsigned int fd, unsigned int cmd,
40 asmlinkage int solaris_ioctl(unsigned int fd, unsigned int cmd, u32 arg);
42 extern int timod_putmsg(unsigned int fd, char __user *ctl_buf, int ctl_len,
43 char __user *data_buf, int data_len, int flags);
44 extern int timod_getmsg(unsigned int fd, char __user *ctl_buf, int ctl_maxlen, int __user *ctl_len,
45 char __user *data_buf, int data_maxlen, int __user *data_len, int *flags);
47 /* termio* stuff {{{ */
49 struct solaris_termios {
57 struct solaris_termio {
66 struct solaris_termiox {
73 static u32 solaris_to_linux_cflag(u32 cflag)
76 if (cflag & 0x200000) {
77 int baud = cflag & 0xf;
80 case 0: baud = B57600; break;
81 case 1: baud = B76800; break;
82 case 2: baud = B115200; break;
83 case 3: baud = B153600; break;
84 case 4: baud = B230400; break;
85 case 5: baud = B307200; break;
86 case 6: baud = B460800; break;
88 cflag |= CBAUDEX | baud;
93 static u32 linux_to_solaris_cflag(u32 cflag)
95 cflag &= ~(CMSPAR | CIBAUD);
96 if (cflag & CBAUDEX) {
97 int baud = cflag & CBAUD;
100 case B57600: baud = 0; break;
101 case B76800: baud = 1; break;
102 case B115200: baud = 2; break;
103 case B153600: baud = 3; break;
104 case B230400: baud = 4; break;
105 case B307200: baud = 5; break;
106 case B460800: baud = 6; break;
107 case B614400: baud = 7; break;
108 case B921600: baud = 8; break;
110 case B1843200: baud = 9; break;
113 cflag |= 0x200000 | baud;
118 static inline int linux_to_solaris_termio(unsigned int fd, unsigned int cmd, u32 arg)
120 struct solaris_termio __user *p = A(arg);
123 ret = sys_ioctl(fd, cmd, (unsigned long)p);
127 if (__get_user (cflag, &p->c_cflag))
129 cflag = linux_to_solaris_cflag(cflag);
130 if (__put_user (cflag, &p->c_cflag))
136 static int solaris_to_linux_termio(unsigned int fd, unsigned int cmd, u32 arg)
139 struct solaris_termio s;
140 mm_segment_t old_fs = get_fs();
142 if (copy_from_user (&s, (struct solaris_termio __user *)A(arg), sizeof(struct solaris_termio)))
144 s.c_cflag = solaris_to_linux_cflag(s.c_cflag);
146 ret = sys_ioctl(fd, cmd, (unsigned long)&s);
151 static inline int linux_to_solaris_termios(unsigned int fd, unsigned int cmd, u32 arg)
154 struct solaris_termios s;
155 mm_segment_t old_fs = get_fs();
158 ret = sys_ioctl(fd, cmd, (unsigned long)&s);
161 struct solaris_termios __user *p = A(arg);
162 if (put_user (s.c_iflag, &p->c_iflag) ||
163 __put_user (s.c_oflag, &p->c_oflag) ||
164 __put_user (linux_to_solaris_cflag(s.c_cflag), &p->c_cflag) ||
165 __put_user (s.c_lflag, &p->c_lflag) ||
166 __copy_to_user (p->c_cc, s.c_cc, 16) ||
167 __clear_user (p->c_cc + 16, 2))
173 static int solaris_to_linux_termios(unsigned int fd, unsigned int cmd, u32 arg)
176 struct solaris_termios s;
177 struct solaris_termios __user *p = A(arg);
178 mm_segment_t old_fs = get_fs();
181 ret = sys_ioctl(fd, TCGETS, (unsigned long)&s);
184 if (put_user (s.c_iflag, &p->c_iflag) ||
185 __put_user (s.c_oflag, &p->c_oflag) ||
186 __put_user (s.c_cflag, &p->c_cflag) ||
187 __put_user (s.c_lflag, &p->c_lflag) ||
188 __copy_from_user (s.c_cc, p->c_cc, 16))
190 s.c_cflag = solaris_to_linux_cflag(s.c_cflag);
192 ret = sys_ioctl(fd, cmd, (unsigned long)&s);
197 static inline int solaris_T(unsigned int fd, unsigned int cmd, u32 arg)
199 switch (cmd & 0xff) {
201 return linux_to_solaris_termio(fd, TCGETA, arg);
203 return solaris_to_linux_termio(fd, TCSETA, arg);
204 case 3: /* TCSETAW */
205 return solaris_to_linux_termio(fd, TCSETAW, arg);
206 case 4: /* TCSETAF */
207 return solaris_to_linux_termio(fd, TCSETAF, arg);
209 return sys_ioctl(fd, TCSBRK, arg);
211 return sys_ioctl(fd, TCXONC, arg);
213 return sys_ioctl(fd, TCFLSH, arg);
214 case 13: /* TCGETS */
215 return linux_to_solaris_termios(fd, TCGETS, arg);
216 case 14: /* TCSETS */
217 return solaris_to_linux_termios(fd, TCSETS, arg);
218 case 15: /* TCSETSW */
219 return solaris_to_linux_termios(fd, TCSETSW, arg);
220 case 16: /* TCSETSF */
221 return solaris_to_linux_termios(fd, TCSETSF, arg);
222 case 103: /* TIOCSWINSZ */
223 return sys_ioctl(fd, TIOCSWINSZ, arg);
224 case 104: /* TIOCGWINSZ */
225 return sys_ioctl(fd, TIOCGWINSZ, arg);
230 static inline int solaris_t(unsigned int fd, unsigned int cmd, u32 arg)
232 switch (cmd & 0xff) {
233 case 20: /* TIOCGPGRP */
234 return sys_ioctl(fd, TIOCGPGRP, arg);
235 case 21: /* TIOCSPGRP */
236 return sys_ioctl(fd, TIOCSPGRP, arg);
243 /* A pseudo STREAMS support {{{ */
246 int cmd, timeout, len;
250 struct solaris_si_sockparams {
256 struct solaris_o_si_udata {
267 struct solaris_si_udata {
276 struct solaris_si_sockparams sockparams;
279 #define SOLARIS_MODULE_TIMOD 0
280 #define SOLARIS_MODULE_SOCKMOD 1
281 #define SOLARIS_MODULE_MAX 2
283 static struct module_info {
285 /* can be expanded further if needed */
286 } module_table[ SOLARIS_MODULE_MAX + 1 ] = {
287 /* the ordering here must match the module numbers above! */
293 static inline int solaris_sockmod(unsigned int fd, unsigned int cmd, u32 arg)
296 /* I wonder which of these tests are superfluous... --patrik */
297 spin_lock(¤t->files->file_lock);
298 if (! current->files->fd[fd] ||
299 ! current->files->fd[fd]->f_dentry ||
300 ! (ino = current->files->fd[fd]->f_dentry->d_inode) ||
302 spin_unlock(¤t->files->file_lock);
305 spin_unlock(¤t->files->file_lock);
307 switch (cmd & 0xff) {
308 case 109: /* SI_SOCKPARAMS */
310 struct solaris_si_sockparams si;
311 if (copy_from_user (&si, A(arg), sizeof(si)))
312 return (EFAULT << 8) | TSYSERR;
314 /* Should we modify socket ino->socket_i.ops and type? */
317 case 110: /* SI_GETUDATA */
319 int etsdusize, servtype;
320 struct solaris_si_udata __user *p = A(arg);
321 switch (SOCKET_I(ino)->type) {
331 if (put_user(16384, &p->tidusize) ||
332 __put_user(sizeof(struct sockaddr), &p->addrsize) ||
333 __put_user(-1, &p->optsize) ||
334 __put_user(etsdusize, &p->etsdusize) ||
335 __put_user(servtype, &p->servtype) ||
336 __put_user(0, &p->so_state) ||
337 __put_user(0, &p->so_options) ||
338 __put_user(16384, &p->tsdusize) ||
339 __put_user(SOCKET_I(ino)->ops->family, &p->sockparams.sp_family) ||
340 __put_user(SOCKET_I(ino)->type, &p->sockparams.sp_type) ||
341 __put_user(SOCKET_I(ino)->ops->family, &p->sockparams.sp_protocol))
342 return (EFAULT << 8) | TSYSERR;
345 case 101: /* O_SI_GETUDATA */
347 int etsdusize, servtype;
348 struct solaris_o_si_udata __user *p = A(arg);
349 switch (SOCKET_I(ino)->type) {
359 if (put_user(16384, &p->tidusize) ||
360 __put_user(sizeof(struct sockaddr), &p->addrsize) ||
361 __put_user(-1, &p->optsize) ||
362 __put_user(etsdusize, &p->etsdusize) ||
363 __put_user(servtype, &p->servtype) ||
364 __put_user(0, &p->so_state) ||
365 __put_user(0, &p->so_options) ||
366 __put_user(16384, &p->tsdusize))
367 return (EFAULT << 8) | TSYSERR;
370 case 102: /* SI_SHUTDOWN */
371 case 103: /* SI_LISTEN */
372 case 104: /* SI_SETMYNAME */
373 case 105: /* SI_SETPEERNAME */
374 case 106: /* SI_GETINTRANSIT */
375 case 107: /* SI_TCL_LINK */
376 case 108: /* SI_TCL_UNLINK */
382 static inline int solaris_timod(unsigned int fd, unsigned int cmd, u32 arg,
383 int len, int __user *len_p)
387 switch (cmd & 0xff) {
388 case 141: /* TI_OPTMGMT */
392 SOLD("TI_OPMGMT entry");
393 ret = timod_putmsg(fd, A(arg), len, NULL, -1, 0);
394 SOLD("timod_putmsg() returned");
396 return (-ret << 8) | TSYSERR;
398 SOLD("calling timod_getmsg()");
399 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
400 SOLD("timod_getmsg() returned");
402 return (-ret << 8) | TSYSERR;
404 if (get_user(prim, (u32 __user *)A(arg)))
405 return (EFAULT << 8) | TSYSERR;
407 if (prim == T_ERROR_ACK) {
409 SOLD("prim is T_ERROR_ACK");
410 if (get_user(tmp, (u32 __user *)A(arg)+3) ||
411 get_user(tmp2, (u32 __user *)A(arg)+2))
412 return (EFAULT << 8) | TSYSERR;
413 return (tmp2 << 8) | tmp;
415 SOLD("TI_OPMGMT return 0");
418 case 142: /* TI_BIND */
422 SOLD("TI_BIND entry");
423 ret = timod_putmsg(fd, A(arg), len, NULL, -1, 0);
424 SOLD("timod_putmsg() returned");
426 return (-ret << 8) | TSYSERR;
427 len = 1024; /* Solaris allows arbitrary return size */
429 SOLD("calling timod_getmsg()");
430 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
431 SOLD("timod_getmsg() returned");
433 return (-ret << 8) | TSYSERR;
435 if (get_user(prim, (u32 __user *)A(arg)))
436 return (EFAULT << 8) | TSYSERR;
438 if (prim == T_ERROR_ACK) {
440 SOLD("prim is T_ERROR_ACK");
441 if (get_user(tmp, (u32 __user *)A(arg)+3) ||
442 get_user(tmp2, (u32 __user *)A(arg)+2))
443 return (EFAULT << 8) | TSYSERR;
444 return (tmp2 << 8) | tmp;
446 SOLD("no ERROR_ACK requested");
447 if (prim != T_OK_ACK)
449 SOLD("OK_ACK requested");
451 SOLD("calling timod_getmsg()");
452 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
453 SOLD("timod_getmsg() returned");
455 return (-ret << 8) | TSYSERR;
456 SOLD("TI_BIND return ok");
459 case 140: /* TI_GETINFO */
460 case 143: /* TI_UNBIND */
461 case 144: /* TI_GETMYNAME */
462 case 145: /* TI_GETPEERNAME */
463 case 146: /* TI_SETMYNAME */
464 case 147: /* TI_SETPEERNAME */
470 static inline int solaris_S(struct file *filp, unsigned int fd, unsigned int cmd, u32 arg)
477 struct sol_socket_struct *sock;
478 struct module_info *mi;
480 ino = filp->f_dentry->d_inode;
483 sock = filp->private_data;
485 printk("solaris_S: NULL private_data\n");
488 if (sock->magic != SOLARIS_SOCKET_MAGIC) {
489 printk("solaris_S: invalid magic\n");
494 switch (cmd & 0xff) {
495 case 1: /* I_NREAD */
499 p = getname (A(arg));
503 for (mi = module_table; mi->name; mi++) {
504 if (strcmp(mi->name, p) == 0) {
506 if (sock->modcount >= MAX_NR_STREAM_MODULES) {
510 m = (sol_module) (mi - module_table);
511 sock->module[sock->modcount++] = m;
520 if (sock->modcount <= 0) return -EINVAL;
526 if (sock->modcount <= 0) return -EINVAL;
527 p = module_table[(unsigned)sock->module[sock->modcount]].name;
528 if (copy_to_user (A(arg), p, strlen(p)))
532 case 5: /* I_FLUSH */
535 if (copy_from_user(&si, A(arg), sizeof(struct strioctl)))
537 /* We ignore what module is actually at the top of stack. */
538 switch ((si.cmd >> 8) & 0xff) {
540 return solaris_sockmod(fd, si.cmd, si.data);
542 return solaris_timod(fd, si.cmd, si.data, si.len,
543 &((struct strioctl __user *)A(arg))->len);
545 return solaris_ioctl(fd, si.cmd, si.data);
547 case 9: /* I_SETSIG */
548 return sys_ioctl(fd, FIOSETOWN, current->pid);
549 case 10: /* I_GETSIG */
552 sys_ioctl(fd, FIOGETOWN, (unsigned long)&ret);
554 if (ret == current->pid) return 0x3ff;
556 case 11: /* I_FIND */
559 p = getname (A(arg));
563 for (i = 0; i < sock->modcount; i++) {
564 unsigned m = sock->module[i];
565 if (strcmp(module_table[m].name, p) == 0) {
573 case 19: /* I_SWROPT */
574 case 32: /* I_SETCLTIME */
580 static inline int solaris_s(unsigned int fd, unsigned int cmd, u32 arg)
582 switch (cmd & 0xff) {
583 case 0: /* SIOCSHIWAT */
584 case 2: /* SIOCSLOWAT */
585 return 0; /* We don't support them */
586 case 1: /* SIOCGHIWAT */
587 case 3: /* SIOCGLOWAT */
588 if (put_user (0, (u32 __user *)A(arg)))
591 case 7: /* SIOCATMARK */
592 return sys_ioctl(fd, SIOCATMARK, arg);
593 case 8: /* SIOCSPGRP */
594 return sys_ioctl(fd, SIOCSPGRP, arg);
595 case 9: /* SIOCGPGRP */
596 return sys_ioctl(fd, SIOCGPGRP, arg);
601 static inline int solaris_r(unsigned int fd, unsigned int cmd, u32 arg)
603 switch (cmd & 0xff) {
604 case 10: /* SIOCADDRT */
605 return compat_sys_ioctl(fd, SIOCADDRT, arg);
606 case 11: /* SIOCDELRT */
607 return compat_sys_ioctl(fd, SIOCDELRT, arg);
612 static inline int solaris_i(unsigned int fd, unsigned int cmd, u32 arg)
614 switch (cmd & 0xff) {
615 case 12: /* SIOCSIFADDR */
616 return compat_sys_ioctl(fd, SIOCSIFADDR, arg);
617 case 13: /* SIOCGIFADDR */
618 return compat_sys_ioctl(fd, SIOCGIFADDR, arg);
619 case 14: /* SIOCSIFDSTADDR */
620 return compat_sys_ioctl(fd, SIOCSIFDSTADDR, arg);
621 case 15: /* SIOCGIFDSTADDR */
622 return compat_sys_ioctl(fd, SIOCGIFDSTADDR, arg);
623 case 16: /* SIOCSIFFLAGS */
624 return compat_sys_ioctl(fd, SIOCSIFFLAGS, arg);
625 case 17: /* SIOCGIFFLAGS */
626 return compat_sys_ioctl(fd, SIOCGIFFLAGS, arg);
627 case 18: /* SIOCSIFMEM */
628 return compat_sys_ioctl(fd, SIOCSIFMEM, arg);
629 case 19: /* SIOCGIFMEM */
630 return compat_sys_ioctl(fd, SIOCGIFMEM, arg);
631 case 20: /* SIOCGIFCONF */
632 return compat_sys_ioctl(fd, SIOCGIFCONF, arg);
633 case 21: /* SIOCSIFMTU */
634 return compat_sys_ioctl(fd, SIOCSIFMTU, arg);
635 case 22: /* SIOCGIFMTU */
636 return compat_sys_ioctl(fd, SIOCGIFMTU, arg);
637 case 23: /* SIOCGIFBRDADDR */
638 return compat_sys_ioctl(fd, SIOCGIFBRDADDR, arg);
639 case 24: /* SIOCSIFBRDADDR */
640 return compat_sys_ioctl(fd, SIOCSIFBRDADDR, arg);
641 case 25: /* SIOCGIFNETMASK */
642 return compat_sys_ioctl(fd, SIOCGIFNETMASK, arg);
643 case 26: /* SIOCSIFNETMASK */
644 return compat_sys_ioctl(fd, SIOCSIFNETMASK, arg);
645 case 27: /* SIOCGIFMETRIC */
646 return compat_sys_ioctl(fd, SIOCGIFMETRIC, arg);
647 case 28: /* SIOCSIFMETRIC */
648 return compat_sys_ioctl(fd, SIOCSIFMETRIC, arg);
649 case 30: /* SIOCSARP */
650 return compat_sys_ioctl(fd, SIOCSARP, arg);
651 case 31: /* SIOCGARP */
652 return compat_sys_ioctl(fd, SIOCGARP, arg);
653 case 32: /* SIOCDARP */
654 return compat_sys_ioctl(fd, SIOCDARP, arg);
655 case 52: /* SIOCGETNAME */
656 case 53: /* SIOCGETPEER */
658 struct sockaddr uaddr;
659 int uaddr_len = sizeof(struct sockaddr), ret;
661 mm_segment_t old_fs = get_fs();
662 int (*sys_socketcall)(int, unsigned long *) =
663 (int (*)(int, unsigned long *))SYS(socketcall);
665 args[0] = fd; args[1] = (long)&uaddr; args[2] = (long)&uaddr_len;
667 ret = sys_socketcall(((cmd & 0xff) == 52) ? SYS_GETSOCKNAME : SYS_GETPEERNAME,
671 if (copy_to_user(A(arg), &uaddr, uaddr_len))
677 case 86: /* SIOCSOCKSYS */
678 return socksys_syscall(fd, arg);
680 case 87: /* SIOCGIFNUM */
682 struct net_device *d;
685 read_lock_bh(&dev_base_lock);
686 for (d = dev_base; d; d = d->next) i++;
687 read_unlock_bh(&dev_base_lock);
689 if (put_user (i, (int __user *)A(arg)))
697 static int solaris_m(unsigned int fd, unsigned int cmd, u32 arg)
701 switch (cmd & 0xff) {
702 case 1: /* MTIOCTOP */
703 ret = sys_ioctl(fd, MTIOCTOP, (unsigned long)&arg);
705 case 2: /* MTIOCGET */
706 ret = sys_ioctl(fd, MTIOCGET, (unsigned long)&arg);
708 case 3: /* MTIOCGETDRIVETYPE */
709 case 4: /* MTIOCPERSISTENT */
710 case 5: /* MTIOCPERSISTENTSTATUS */
711 case 6: /* MTIOCLRERR */
712 case 7: /* MTIOCGUARANTEEDORDER */
713 case 8: /* MTIOCRESERVE */
714 case 9: /* MTIOCRELEASE */
715 case 10: /* MTIOCFORCERESERVE */
716 case 13: /* MTIOCSTATE */
717 case 14: /* MTIOCREADIGNOREILI */
718 case 15: /* MTIOCREADIGNOREEOFS */
719 case 16: /* MTIOCSHORTFMK */
721 ret = -ENOSYS; /* linux doesn't support these */
728 static int solaris_O(unsigned int fd, unsigned int cmd, u32 arg)
732 switch (cmd & 0xff) {
733 case 1: /* OPROMGETOPT */
734 ret = sys_ioctl(fd, OPROMGETOPT, arg);
736 case 2: /* OPROMSETOPT */
737 ret = sys_ioctl(fd, OPROMSETOPT, arg);
739 case 3: /* OPROMNXTOPT */
740 ret = sys_ioctl(fd, OPROMNXTOPT, arg);
742 case 4: /* OPROMSETOPT2 */
743 ret = sys_ioctl(fd, OPROMSETOPT2, arg);
745 case 5: /* OPROMNEXT */
746 ret = sys_ioctl(fd, OPROMNEXT, arg);
748 case 6: /* OPROMCHILD */
749 ret = sys_ioctl(fd, OPROMCHILD, arg);
751 case 7: /* OPROMGETPROP */
752 ret = sys_ioctl(fd, OPROMGETPROP, arg);
754 case 8: /* OPROMNXTPROP */
755 ret = sys_ioctl(fd, OPROMNXTPROP, arg);
757 case 9: /* OPROMU2P */
758 ret = sys_ioctl(fd, OPROMU2P, arg);
760 case 10: /* OPROMGETCONS */
761 ret = sys_ioctl(fd, OPROMGETCONS, arg);
763 case 11: /* OPROMGETFBNAME */
764 ret = sys_ioctl(fd, OPROMGETFBNAME, arg);
766 case 12: /* OPROMGETBOOTARGS */
767 ret = sys_ioctl(fd, OPROMGETBOOTARGS, arg);
769 case 13: /* OPROMGETVERSION */
770 case 14: /* OPROMPATH2DRV */
771 case 15: /* OPROMDEV2PROMNAME */
772 case 16: /* OPROMPROM2DEVNAME */
773 case 17: /* OPROMGETPROPLEN */
783 asmlinkage int solaris_ioctl(unsigned int fd, unsigned int cmd, u32 arg)
794 switch ((cmd >> 8) & 0xff) {
795 case 'S': error = solaris_S(filp, fd, cmd, arg); break;
796 case 'T': error = solaris_T(fd, cmd, arg); break;
797 case 'i': error = solaris_i(fd, cmd, arg); break;
798 case 'r': error = solaris_r(fd, cmd, arg); break;
799 case 's': error = solaris_s(fd, cmd, arg); break;
800 case 't': error = solaris_t(fd, cmd, arg); break;
801 case 'f': error = sys_ioctl(fd, cmd, arg); break;
802 case 'm': error = solaris_m(fd, cmd, arg); break;
803 case 'O': error = solaris_O(fd, cmd, arg); break;
811 if (error == -ENOSYS) {
812 unsigned char c = cmd>>8;
814 if (c < ' ' || c > 126) c = '.';
815 printk("solaris_ioctl: Unknown cmd fd(%d) cmd(%08x '%c') arg(%08x)\n",
816 (int)fd, (unsigned int)cmd, c, (unsigned int)arg);