count = 1193182 / count; 
count = CLOCK_TICK_RATE / count; 
int vt_ioctl(struct tty_struct *tty, struct file * file,	     unsigned int cmd, unsigned long arg){	struct vc_data *vc = (struct vc_data *)tty->driver_data;	struct console_font_op op;	/* used in multiple places here */	struct kbd_struct * kbd;	unsigned int console;	unsigned char ucval;	void __user *up = (void __user *)arg;	int i, perm;		console = vc->vc_num;	if (!vc_cons_allocated(console)) 	/* impossible? */		return -ENOIOCTLCMD;	/*	 * To have permissions to do most of the vt ioctls, we either have	 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.	 */	perm = 0;	if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))		perm = 1; 	kbd = kbd_table + console;	switch (cmd) {	case KIOCSOUND:		if (!perm)			return -EPERM;		if (arg)			arg = 1193182 / arg;		kd_mksound(arg, 0);		return 0;	case KDMKTONE:		if (!perm)			return -EPERM;	{		unsigned int ticks, count;				/*		 * Generate the tone for the appropriate number of ticks.		 * If the time is zero, turn off sound ourselves.		 */		ticks = HZ * ((arg >> 16) & 0xffff) / 1000;		count = ticks ? (arg & 0xffff) : 0;		if (count)			count = 1193182 / count;		kd_mksound(count, ticks);		return 0;	}	case KDGKBTYPE:		/*		 * this is naive.		 */		ucval = KB_101;		goto setchar;		/*		 * These cannot be implemented on any machine that implements		 * ioperm() in user level (such as Alpha PCs) or not at all.		 *		 * XXX: you should never use these, just call ioperm directly..		 */#ifdef CONFIG_X86	case KDADDIO:	case KDDELIO:		/*		 * KDADDIO and KDDELIO may be able to add ports beyond what		 * we reject here, but to be safe...		 */		if (arg < GPFIRST || arg > GPLAST)			return -EINVAL;		return sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;	case KDENABIO:	case KDDISABIO:		return sys_ioperm(GPFIRST, GPNUM,				  (cmd == KDENABIO)) ? -ENXIO : 0;#endif	/* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */			case KDKBDREP:	{		struct kbd_repeat kbrep;		int err;				if (!capable(CAP_SYS_TTY_CONFIG))			return -EPERM;		if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat)))			return -EFAULT;		err = kbd_rate(&kbrep);		if (err)			return err;		if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))			return -EFAULT;		return 0;	}	case KDSETMODE:		/*		 * currently, setting the mode from KD_TEXT to KD_GRAPHICS		 * doesn't do a whole lot. i'm not sure if it should do any		 * restoration of modes or what...		 *		 * XXX It should at least call into the driver, fbdev's definitely		 * need to restore their engine state. --BenH		 */		if (!perm)			return -EPERM;		switch (arg) {		case KD_GRAPHICS:			break;		case KD_TEXT0:		case KD_TEXT1:			arg = KD_TEXT;		case KD_TEXT:			break;		default:			return -EINVAL;		}		if (vc->vc_mode == (unsigned char) arg)			return 0;		vc->vc_mode = (unsigned char) arg;		if (console != fg_console)			return 0;		/*		 * explicitly blank/unblank the screen if switching modes		 */		acquire_console_sem();		if (arg == KD_TEXT)			do_unblank_screen(1);		else			do_blank_screen(1);		release_console_sem();		return 0;	case KDGETMODE:		ucval = vc->vc_mode;		goto setint;	case KDMAPDISP:	case KDUNMAPDISP:		/*		 * these work like a combination of mmap and KDENABIO.		 * this could be easily finished.		 */		return -EINVAL;	case KDSKBMODE:		if (!perm)			return -EPERM;		switch(arg) {		  case K_RAW:			kbd->kbdmode = VC_RAW;			break;		  case K_MEDIUMRAW:			kbd->kbdmode = VC_MEDIUMRAW;			break;		  case K_XLATE:			kbd->kbdmode = VC_XLATE;			compute_shiftstate();			break;		  case K_UNICODE:			kbd->kbdmode = VC_UNICODE;			compute_shiftstate();			break;		  default:			return -EINVAL;		}		tty_ldisc_flush(tty);		return 0;	case KDGKBMODE:		ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :				 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :				 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :				 K_XLATE);		goto setint;	/* this could be folded into KDSKBMODE, but for compatibility	   reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */	case KDSKBMETA:		switch(arg) {		  case K_METABIT:			clr_vc_kbd_mode(kbd, VC_META);			break;		  case K_ESCPREFIX:			set_vc_kbd_mode(kbd, VC_META);			break;		  default:			return -EINVAL;		}		return 0;	case KDGKBMETA:		ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);	setint:		return put_user(ucval, (int __user *)arg); 	case KDGETKEYCODE:	case KDSETKEYCODE:		if(!capable(CAP_SYS_TTY_CONFIG))			perm=0;		return do_kbkeycode_ioctl(cmd, up, perm);	case KDGKBENT:	case KDSKBENT:		return do_kdsk_ioctl(cmd, up, perm, kbd);	case KDGKBSENT:	case KDSKBSENT:		return do_kdgkb_ioctl(cmd, up, perm);	case KDGKBDIACR:	{		struct kbdiacrs __user *a = up;		if (put_user(accent_table_size, &a->kb_cnt))			return -EFAULT;		if (copy_to_user(a->kbdiacr, accent_table, accent_table_size*sizeof(struct kbdiacr)))			return -EFAULT;		return 0;	}	case KDSKBDIACR:	{		struct kbdiacrs __user *a = up;		unsigned int ct;		if (!perm)			return -EPERM;		if (get_user(ct,&a->kb_cnt))			return -EFAULT;		if (ct >= MAX_DIACR)			return -EINVAL;		accent_table_size = ct;		if (copy_from_user(accent_table, a->kbdiacr, ct*sizeof(struct kbdiacr)))			return -EFAULT;		return 0;	}	/* the ioctls below read/set the flags usually shown in the leds */	/* don't use them - they will go away without warning */	case KDGKBLED:		ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);		goto setchar;	case KDSKBLED:		if (!perm)			return -EPERM;		if (arg & ~0x77)			return -EINVAL;		kbd->ledflagstate = (arg & 7);		kbd->default_ledflagstate = ((arg >> 4) & 7);		set_leds();		return 0;	/* the ioctls below only set the lights, not the functions */	/* for those, see KDGKBLED and KDSKBLED above */	case KDGETLED:		ucval = getledstate();	setchar:		return put_user(ucval, (char __user *)arg);	case KDSETLED:		if (!perm)		  return -EPERM;		setledstate(kbd, arg);		return 0;	/*	 * A process can indicate its willingness to accept signals	 * generated by pressing an appropriate key combination.	 * Thus, one can have a daemon that e.g. spawns a new console	 * upon a keypress and then changes to it.	 * See also the kbrequest field of inittab(5).	 */	case KDSIGACCEPT:	{		extern int spawnpid, spawnsig;		if (!perm || !capable(CAP_KILL))		  return -EPERM;		if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)		  return -EINVAL;		spawnpid = current->pid;		spawnsig = arg;		return 0;	}	case VT_SETMODE:	{		struct vt_mode tmp;		if (!perm)			return -EPERM;		if (copy_from_user(&tmp, up, sizeof(struct vt_mode)))			return -EFAULT;		if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)			return -EINVAL;		acquire_console_sem();		vc->vt_mode = tmp;		/* the frsig is ignored, so we set it to 0 */		vc->vt_mode.frsig = 0;		vc->vt_pid = current->pid;		/* no switch is required -- saw@shade.msu.ru */		vc->vt_newvt = -1;		release_console_sem();		return 0;	}	case VT_GETMODE:	{		struct vt_mode tmp;		int rc;		acquire_console_sem();		memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));		release_console_sem();		rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));		return rc ? -EFAULT : 0;	}	/*	 * Returns global vt state. Note that VT 0 is always open, since	 * it's an alias for the current VT, and people can't use it here.	 * We cannot return state for more than 16 VTs, since v_state is short.	 */	case VT_GETSTATE:	{		struct vt_stat __user *vtstat = up;		unsigned short state, mask;		if (put_user(fg_console + 1, &vtstat->v_active))			return -EFAULT;		state = 1;	/* /dev/tty0 is always open */		for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; ++i, mask <<= 1)			if (VT_IS_IN_USE(i))				state |= mask;		return put_user(state, &vtstat->v_state);	}	/*	 * Returns the first available (non-opened) console.	 */	case VT_OPENQRY:		for (i = 0; i < MAX_NR_CONSOLES; ++i)			if (! VT_IS_IN_USE(i))				break;		ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;		goto setint;		 	/*	 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,	 * with num >= 1 (switches to vt 0, our console, are not allowed, just	 * to preserve sanity).	 */	case VT_ACTIVATE:		if (!perm)			return -EPERM;		if (arg == 0 || arg > MAX_NR_CONSOLES)			return -ENXIO;		arg--;		acquire_console_sem();		i = vc_allocate(arg);		release_console_sem();		if (i)			return i;		set_console(arg);		return 0;	/*	 * wait until the specified VT has been activated	 */	case VT_WAITACTIVE:		if (!perm)			return -EPERM;		if (arg == 0 || arg > MAX_NR_CONSOLES)			return -ENXIO;		return vt_waitactive(arg-1);	/*	 * If a vt is under process control, the kernel will not switch to it	 * immediately, but postpone the operation until the process calls this	 * ioctl, allowing the switch to complete.	 *	 * According to the X sources this is the behavior:	 *	0:	pending switch-from not OK	 *	1:	pending switch-from OK	 *	2:	completed switch-to OK	 */	case VT_RELDISP:		if (!perm)			return -EPERM;		if (vc->vt_mode.mode != VT_PROCESS)			return -EINVAL;		/*		 * Switching-from response		 */		if (vc->vt_newvt >= 0) {			if (arg == 0)				/*				 * Switch disallowed, so forget we were trying				 * to do it.				 */				vc->vt_newvt = -1;			else {				/*				 * The current vt has been released, so				 * complete the switch.				 */				int newvt;				acquire_console_sem();				newvt = vc->vt_newvt;				vc->vt_newvt = -1;				i = vc_allocate(newvt);				if (i) {					release_console_sem();					return i;				}				/*				 * When we actually do the console switch,				 * make sure we are atomic with respect to				 * other console switches..				 */				complete_change_console(vc_cons[newvt].d);				release_console_sem();			}		}		/*		 * Switched-to response		 */		else		{			/*			 * If it's just an ACK, ignore it			 */			if (arg != VT_ACKACQ)				return -EINVAL;		}		return 0;	 /*	  * Disallocate memory associated to VT (but leave VT1)	  */	 case VT_DISALLOCATE:		if (arg > MAX_NR_CONSOLES)			return -ENXIO;		if (arg == 0) {		    /* disallocate all unused consoles, but leave 0 */			acquire_console_sem();			for (i=1; i<MAX_NR_CONSOLES; i++)				if (! VT_BUSY(i))					vc_disallocate(i);			release_console_sem();		} else {			/* disallocate a single console, if possible */			arg--;			if (VT_BUSY(arg))				return -EBUSY;			if (arg) {			      /* leave 0 */				acquire_console_sem();				vc_disallocate(arg);				release_console_sem();			}		}		return 0;	case VT_RESIZE:	{		struct vt_sizes __user *vtsizes = up;		ushort ll,cc;		if (!perm)			return -EPERM;		if (get_user(ll, &vtsizes->v_rows) ||		    get_user(cc, &vtsizes->v_cols))			return -EFAULT;		for (i = 0; i < MAX_NR_CONSOLES; i++) {			acquire_console_sem();			vc_resize(vc_cons[i].d, cc, ll);			release_console_sem();		}		return 0;	}	case VT_RESIZEX:	{		struct vt_consize __user *vtconsize = up;		ushort ll,cc,vlin,clin,vcol,ccol;		if (!perm)			return -EPERM;		if (!access_ok(VERIFY_READ, vtconsize,				sizeof(struct vt_consize)))			return -EFAULT;		__get_user(ll, &vtconsize->v_rows);		__get_user(cc, &vtconsize->v_cols);		__get_user(vlin, &vtconsize->v_vlin);		__get_user(clin, &vtconsize->v_clin);		__get_user(vcol, &vtconsize->v_vcol);		__get_user(ccol, &vtconsize->v_ccol);		vlin = vlin ? vlin : vc->vc_scan_lines;		if (clin) {			if (ll) {				if (ll != vlin/clin)					return -EINVAL; /* Parameters don't add up */			} else 				ll = vlin/clin;		}		if (vcol && ccol) {			if (cc) {				if (cc != vcol/ccol)					return -EINVAL;			} else				cc = vcol/ccol;		}		if (clin > 32)			return -EINVAL;		    		for (i = 0; i < MAX_NR_CONSOLES; i++) {			if (!vc_cons[i].d)				continue;			acquire_console_sem();			if (vlin)				vc_cons[i].d->vc_scan_lines = vlin;			if (clin)				vc_cons[i].d->vc_font.height = clin;			vc_resize(vc_cons[i].d, cc, ll);			release_console_sem();		}  		return 0;	}	case PIO_FONT: {		if (!perm)			return -EPERM;		op.op = KD_FONT_OP_SET;		op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC;	/* Compatibility */		op.width = 8;		op.height = 0;		op.charcount = 256;		op.data = up;		return con_font_op(vc_cons[fg_console].d, &op);	}	case GIO_FONT: {		op.op = KD_FONT_OP_GET;		op.flags = KD_FONT_FLAG_OLD;		op.width = 8;		op.height = 32;		op.charcount = 256;		op.data = up;		return con_font_op(vc_cons[fg_console].d, &op);	}	case PIO_CMAP:                if (!perm)			return -EPERM;                return con_set_cmap(up);	case GIO_CMAP:                return con_get_cmap(up);	case PIO_FONTX:	case GIO_FONTX:		return do_fontx_ioctl(cmd, up, perm, &op);	case PIO_FONTRESET:	{		if (!perm)			return -EPERM;#ifdef BROKEN_GRAPHICS_PROGRAMS		/* With BROKEN_GRAPHICS_PROGRAMS defined, the default		   font is not saved. */		return -ENOSYS;#else		{		op.op = KD_FONT_OP_SET_DEFAULT;		op.data = NULL;		i = con_font_op(vc_cons[fg_console].d, &op);		if (i)			return i;		con_set_default_unimap(vc_cons[fg_console].d);		return 0;		}#endif	}	case KDFONTOP: {		if (copy_from_user(&op, up, sizeof(op)))			return -EFAULT;		if (!perm && op.op != KD_FONT_OP_GET)			return -EPERM;		i = con_font_op(vc, &op);		if (i) return i;		if (copy_to_user(up, &op, sizeof(op)))			return -EFAULT;		return 0;	}	case PIO_SCRNMAP:		if (!perm)			return -EPERM;		return con_set_trans_old(up);	case GIO_SCRNMAP:		return con_get_trans_old(up);	case PIO_UNISCRNMAP:		if (!perm)			return -EPERM;		return con_set_trans_new(up);	case GIO_UNISCRNMAP:		return con_get_trans_new(up);	case PIO_UNIMAPCLR:	      { struct unimapinit ui;		if (!perm)			return -EPERM;		i = copy_from_user(&ui, up, sizeof(struct unimapinit));		if (i) return -EFAULT;		con_clear_unimap(vc, &ui);		return 0;	      }	case PIO_UNIMAP:	case GIO_UNIMAP:		return do_unimap_ioctl(cmd, up, perm, vc);	case VT_LOCKSWITCH:		if (!capable(CAP_SYS_TTY_CONFIG))		   return -EPERM;		vt_dont_switch = 1;		return 0;	case VT_UNLOCKSWITCH:		if (!capable(CAP_SYS_TTY_CONFIG))		   return -EPERM;		vt_dont_switch = 0;		return 0;	default:		return -ENOIOCTLCMD;	}}
10457	/local/tlutelli/issta_data/temp/c/2005_temp/2005/10457/bcc8ca09920755520ba8a1e2d9f72fe8ff892643/vt_ioctl.c/clean/drivers/char/vt_ioctl.c