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)
297 /* I wonder which of these tests are superfluous... --patrik */
298 spin_lock(¤t->files->file_lock);
299 fdt = files_fdtable(current->files);
301 ! fdt->fd[fd]->f_dentry ||
302 ! (ino = fdt->fd[fd]->f_dentry->d_inode) ||
303 ! S_ISSOCK(ino->i_mode)) {
304 spin_unlock(¤t->files->file_lock);
307 spin_unlock(¤t->files->file_lock);
309 switch (cmd & 0xff) {
310 case 109: /* SI_SOCKPARAMS */
312 struct solaris_si_sockparams si;
313 if (copy_from_user (&si, A(arg), sizeof(si)))
314 return (EFAULT << 8) | TSYSERR;
316 /* Should we modify socket ino->socket_i.ops and type? */
319 case 110: /* SI_GETUDATA */
321 int etsdusize, servtype;
322 struct solaris_si_udata __user *p = A(arg);
323 switch (SOCKET_I(ino)->type) {
333 if (put_user(16384, &p->tidusize) ||
334 __put_user(sizeof(struct sockaddr), &p->addrsize) ||
335 __put_user(-1, &p->optsize) ||
336 __put_user(etsdusize, &p->etsdusize) ||
337 __put_user(servtype, &p->servtype) ||
338 __put_user(0, &p->so_state) ||
339 __put_user(0, &p->so_options) ||
340 __put_user(16384, &p->tsdusize) ||
341 __put_user(SOCKET_I(ino)->ops->family, &p->sockparams.sp_family) ||
342 __put_user(SOCKET_I(ino)->type, &p->sockparams.sp_type) ||
343 __put_user(SOCKET_I(ino)->ops->family, &p->sockparams.sp_protocol))
344 return (EFAULT << 8) | TSYSERR;
347 case 101: /* O_SI_GETUDATA */
349 int etsdusize, servtype;
350 struct solaris_o_si_udata __user *p = A(arg);
351 switch (SOCKET_I(ino)->type) {
361 if (put_user(16384, &p->tidusize) ||
362 __put_user(sizeof(struct sockaddr), &p->addrsize) ||
363 __put_user(-1, &p->optsize) ||
364 __put_user(etsdusize, &p->etsdusize) ||
365 __put_user(servtype, &p->servtype) ||
366 __put_user(0, &p->so_state) ||
367 __put_user(0, &p->so_options) ||
368 __put_user(16384, &p->tsdusize))
369 return (EFAULT << 8) | TSYSERR;
372 case 102: /* SI_SHUTDOWN */
373 case 103: /* SI_LISTEN */
374 case 104: /* SI_SETMYNAME */
375 case 105: /* SI_SETPEERNAME */
376 case 106: /* SI_GETINTRANSIT */
377 case 107: /* SI_TCL_LINK */
378 case 108: /* SI_TCL_UNLINK */
384 static inline int solaris_timod(unsigned int fd, unsigned int cmd, u32 arg,
385 int len, int __user *len_p)
389 switch (cmd & 0xff) {
390 case 141: /* TI_OPTMGMT */
394 SOLD("TI_OPMGMT entry");
395 ret = timod_putmsg(fd, A(arg), len, NULL, -1, 0);
396 SOLD("timod_putmsg() returned");
398 return (-ret << 8) | TSYSERR;
400 SOLD("calling timod_getmsg()");
401 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
402 SOLD("timod_getmsg() returned");
404 return (-ret << 8) | TSYSERR;
406 if (get_user(prim, (u32 __user *)A(arg)))
407 return (EFAULT << 8) | TSYSERR;
409 if (prim == T_ERROR_ACK) {
411 SOLD("prim is T_ERROR_ACK");
412 if (get_user(tmp, (u32 __user *)A(arg)+3) ||
413 get_user(tmp2, (u32 __user *)A(arg)+2))
414 return (EFAULT << 8) | TSYSERR;
415 return (tmp2 << 8) | tmp;
417 SOLD("TI_OPMGMT return 0");
420 case 142: /* TI_BIND */
424 SOLD("TI_BIND entry");
425 ret = timod_putmsg(fd, A(arg), len, NULL, -1, 0);
426 SOLD("timod_putmsg() returned");
428 return (-ret << 8) | TSYSERR;
429 len = 1024; /* Solaris allows arbitrary return size */
431 SOLD("calling timod_getmsg()");
432 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
433 SOLD("timod_getmsg() returned");
435 return (-ret << 8) | TSYSERR;
437 if (get_user(prim, (u32 __user *)A(arg)))
438 return (EFAULT << 8) | TSYSERR;
440 if (prim == T_ERROR_ACK) {
442 SOLD("prim is T_ERROR_ACK");
443 if (get_user(tmp, (u32 __user *)A(arg)+3) ||
444 get_user(tmp2, (u32 __user *)A(arg)+2))
445 return (EFAULT << 8) | TSYSERR;
446 return (tmp2 << 8) | tmp;
448 SOLD("no ERROR_ACK requested");
449 if (prim != T_OK_ACK)
451 SOLD("OK_ACK requested");
453 SOLD("calling timod_getmsg()");
454 ret = timod_getmsg(fd, A(arg), len, len_p, NULL, -1, NULL, &i);
455 SOLD("timod_getmsg() returned");
457 return (-ret << 8) | TSYSERR;
458 SOLD("TI_BIND return ok");
461 case 140: /* TI_GETINFO */
462 case 143: /* TI_UNBIND */
463 case 144: /* TI_GETMYNAME */
464 case 145: /* TI_GETPEERNAME */
465 case 146: /* TI_SETMYNAME */
466 case 147: /* TI_SETPEERNAME */
472 static inline int solaris_S(struct file *filp, unsigned int fd, unsigned int cmd, u32 arg)
479 struct sol_socket_struct *sock;
480 struct module_info *mi;
482 ino = filp->f_dentry->d_inode;
483 if (!S_ISSOCK(ino->i_mode))
485 sock = filp->private_data;
487 printk("solaris_S: NULL private_data\n");
490 if (sock->magic != SOLARIS_SOCKET_MAGIC) {
491 printk("solaris_S: invalid magic\n");
496 switch (cmd & 0xff) {
497 case 1: /* I_NREAD */
501 p = getname (A(arg));
505 for (mi = module_table; mi->name; mi++) {
506 if (strcmp(mi->name, p) == 0) {
508 if (sock->modcount >= MAX_NR_STREAM_MODULES) {
512 m = (sol_module) (mi - module_table);
513 sock->module[sock->modcount++] = m;
522 if (sock->modcount <= 0) return -EINVAL;
528 if (sock->modcount <= 0) return -EINVAL;
529 p = module_table[(unsigned)sock->module[sock->modcount]].name;
530 if (copy_to_user (A(arg), p, strlen(p)))
534 case 5: /* I_FLUSH */
537 if (copy_from_user(&si, A(arg), sizeof(struct strioctl)))
539 /* We ignore what module is actually at the top of stack. */
540 switch ((si.cmd >> 8) & 0xff) {
542 return solaris_sockmod(fd, si.cmd, si.data);
544 return solaris_timod(fd, si.cmd, si.data, si.len,
545 &((struct strioctl __user *)A(arg))->len);
547 return solaris_ioctl(fd, si.cmd, si.data);
549 case 9: /* I_SETSIG */
550 return sys_ioctl(fd, FIOSETOWN, current->pid);
551 case 10: /* I_GETSIG */
554 sys_ioctl(fd, FIOGETOWN, (unsigned long)&ret);
556 if (ret == current->pid) return 0x3ff;
558 case 11: /* I_FIND */
561 p = getname (A(arg));
565 for (i = 0; i < sock->modcount; i++) {
566 unsigned m = sock->module[i];
567 if (strcmp(module_table[m].name, p) == 0) {
575 case 19: /* I_SWROPT */
576 case 32: /* I_SETCLTIME */
582 static inline int solaris_s(unsigned int fd, unsigned int cmd, u32 arg)
584 switch (cmd & 0xff) {
585 case 0: /* SIOCSHIWAT */
586 case 2: /* SIOCSLOWAT */
587 return 0; /* We don't support them */
588 case 1: /* SIOCGHIWAT */
589 case 3: /* SIOCGLOWAT */
590 if (put_user (0, (u32 __user *)A(arg)))
593 case 7: /* SIOCATMARK */
594 return sys_ioctl(fd, SIOCATMARK, arg);
595 case 8: /* SIOCSPGRP */
596 return sys_ioctl(fd, SIOCSPGRP, arg);
597 case 9: /* SIOCGPGRP */
598 return sys_ioctl(fd, SIOCGPGRP, arg);
603 static inline int solaris_r(unsigned int fd, unsigned int cmd, u32 arg)
605 switch (cmd & 0xff) {
606 case 10: /* SIOCADDRT */
607 return compat_sys_ioctl(fd, SIOCADDRT, arg);
608 case 11: /* SIOCDELRT */
609 return compat_sys_ioctl(fd, SIOCDELRT, arg);
614 static inline int solaris_i(unsigned int fd, unsigned int cmd, u32 arg)
616 switch (cmd & 0xff) {
617 case 12: /* SIOCSIFADDR */
618 return compat_sys_ioctl(fd, SIOCSIFADDR, arg);
619 case 13: /* SIOCGIFADDR */
620 return compat_sys_ioctl(fd, SIOCGIFADDR, arg);
621 case 14: /* SIOCSIFDSTADDR */
622 return compat_sys_ioctl(fd, SIOCSIFDSTADDR, arg);
623 case 15: /* SIOCGIFDSTADDR */
624 return compat_sys_ioctl(fd, SIOCGIFDSTADDR, arg);
625 case 16: /* SIOCSIFFLAGS */
626 return compat_sys_ioctl(fd, SIOCSIFFLAGS, arg);
627 case 17: /* SIOCGIFFLAGS */
628 return compat_sys_ioctl(fd, SIOCGIFFLAGS, arg);
629 case 18: /* SIOCSIFMEM */
630 return compat_sys_ioctl(fd, SIOCSIFMEM, arg);
631 case 19: /* SIOCGIFMEM */
632 return compat_sys_ioctl(fd, SIOCGIFMEM, arg);
633 case 20: /* SIOCGIFCONF */
634 return compat_sys_ioctl(fd, SIOCGIFCONF, arg);
635 case 21: /* SIOCSIFMTU */
636 return compat_sys_ioctl(fd, SIOCSIFMTU, arg);
637 case 22: /* SIOCGIFMTU */
638 return compat_sys_ioctl(fd, SIOCGIFMTU, arg);
639 case 23: /* SIOCGIFBRDADDR */
640 return compat_sys_ioctl(fd, SIOCGIFBRDADDR, arg);
641 case 24: /* SIOCSIFBRDADDR */
642 return compat_sys_ioctl(fd, SIOCSIFBRDADDR, arg);
643 case 25: /* SIOCGIFNETMASK */
644 return compat_sys_ioctl(fd, SIOCGIFNETMASK, arg);
645 case 26: /* SIOCSIFNETMASK */
646 return compat_sys_ioctl(fd, SIOCSIFNETMASK, arg);
647 case 27: /* SIOCGIFMETRIC */
648 return compat_sys_ioctl(fd, SIOCGIFMETRIC, arg);
649 case 28: /* SIOCSIFMETRIC */
650 return compat_sys_ioctl(fd, SIOCSIFMETRIC, arg);
651 case 30: /* SIOCSARP */
652 return compat_sys_ioctl(fd, SIOCSARP, arg);
653 case 31: /* SIOCGARP */
654 return compat_sys_ioctl(fd, SIOCGARP, arg);
655 case 32: /* SIOCDARP */
656 return compat_sys_ioctl(fd, SIOCDARP, arg);
657 case 52: /* SIOCGETNAME */
658 case 53: /* SIOCGETPEER */
660 struct sockaddr uaddr;
661 int uaddr_len = sizeof(struct sockaddr), ret;
663 mm_segment_t old_fs = get_fs();
664 int (*sys_socketcall)(int, unsigned long *) =
665 (int (*)(int, unsigned long *))SYS(socketcall);
667 args[0] = fd; args[1] = (long)&uaddr; args[2] = (long)&uaddr_len;
669 ret = sys_socketcall(((cmd & 0xff) == 52) ? SYS_GETSOCKNAME : SYS_GETPEERNAME,
673 if (copy_to_user(A(arg), &uaddr, uaddr_len))
679 case 86: /* SIOCSOCKSYS */
680 return socksys_syscall(fd, arg);
682 case 87: /* SIOCGIFNUM */
684 struct net_device *d;
687 read_lock_bh(&dev_base_lock);
688 for (d = dev_base; d; d = d->next) i++;
689 read_unlock_bh(&dev_base_lock);
691 if (put_user (i, (int __user *)A(arg)))
699 static int solaris_m(unsigned int fd, unsigned int cmd, u32 arg)
703 switch (cmd & 0xff) {
704 case 1: /* MTIOCTOP */
705 ret = sys_ioctl(fd, MTIOCTOP, (unsigned long)&arg);
707 case 2: /* MTIOCGET */
708 ret = sys_ioctl(fd, MTIOCGET, (unsigned long)&arg);
710 case 3: /* MTIOCGETDRIVETYPE */
711 case 4: /* MTIOCPERSISTENT */
712 case 5: /* MTIOCPERSISTENTSTATUS */
713 case 6: /* MTIOCLRERR */
714 case 7: /* MTIOCGUARANTEEDORDER */
715 case 8: /* MTIOCRESERVE */
716 case 9: /* MTIOCRELEASE */
717 case 10: /* MTIOCFORCERESERVE */
718 case 13: /* MTIOCSTATE */
719 case 14: /* MTIOCREADIGNOREILI */
720 case 15: /* MTIOCREADIGNOREEOFS */
721 case 16: /* MTIOCSHORTFMK */
723 ret = -ENOSYS; /* linux doesn't support these */
730 static int solaris_O(unsigned int fd, unsigned int cmd, u32 arg)
734 switch (cmd & 0xff) {
735 case 1: /* OPROMGETOPT */
736 ret = sys_ioctl(fd, OPROMGETOPT, arg);
738 case 2: /* OPROMSETOPT */
739 ret = sys_ioctl(fd, OPROMSETOPT, arg);
741 case 3: /* OPROMNXTOPT */
742 ret = sys_ioctl(fd, OPROMNXTOPT, arg);
744 case 4: /* OPROMSETOPT2 */
745 ret = sys_ioctl(fd, OPROMSETOPT2, arg);
747 case 5: /* OPROMNEXT */
748 ret = sys_ioctl(fd, OPROMNEXT, arg);
750 case 6: /* OPROMCHILD */
751 ret = sys_ioctl(fd, OPROMCHILD, arg);
753 case 7: /* OPROMGETPROP */
754 ret = sys_ioctl(fd, OPROMGETPROP, arg);
756 case 8: /* OPROMNXTPROP */
757 ret = sys_ioctl(fd, OPROMNXTPROP, arg);
759 case 9: /* OPROMU2P */
760 ret = sys_ioctl(fd, OPROMU2P, arg);
762 case 10: /* OPROMGETCONS */
763 ret = sys_ioctl(fd, OPROMGETCONS, arg);
765 case 11: /* OPROMGETFBNAME */
766 ret = sys_ioctl(fd, OPROMGETFBNAME, arg);
768 case 12: /* OPROMGETBOOTARGS */
769 ret = sys_ioctl(fd, OPROMGETBOOTARGS, arg);
771 case 13: /* OPROMGETVERSION */
772 case 14: /* OPROMPATH2DRV */
773 case 15: /* OPROMDEV2PROMNAME */
774 case 16: /* OPROMPROM2DEVNAME */
775 case 17: /* OPROMGETPROPLEN */
785 asmlinkage int solaris_ioctl(unsigned int fd, unsigned int cmd, u32 arg)
796 switch ((cmd >> 8) & 0xff) {
797 case 'S': error = solaris_S(filp, fd, cmd, arg); break;
798 case 'T': error = solaris_T(fd, cmd, arg); break;
799 case 'i': error = solaris_i(fd, cmd, arg); break;
800 case 'r': error = solaris_r(fd, cmd, arg); break;
801 case 's': error = solaris_s(fd, cmd, arg); break;
802 case 't': error = solaris_t(fd, cmd, arg); break;
803 case 'f': error = sys_ioctl(fd, cmd, arg); break;
804 case 'm': error = solaris_m(fd, cmd, arg); break;
805 case 'O': error = solaris_O(fd, cmd, arg); break;
813 if (error == -ENOSYS) {
814 unsigned char c = cmd>>8;
816 if (c < ' ' || c > 126) c = '.';
817 printk("solaris_ioctl: Unknown cmd fd(%d) cmd(%08x '%c') arg(%08x)\n",
818 (int)fd, (unsigned int)cmd, c, (unsigned int)arg);