atkbd.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,002 行 · 第 1/2 页

C
1,002
字号
			 * of ID response.			 */			clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);			atkbd->cmdcnt = 0;		}		wait_event_interruptible_timeout(atkbd->wait,				!test_bit(ATKBD_FLAG_CMD, &atkbd->flags), timeout);	}	if (param)		for (i = 0; i < receive; i++)			param[i] = atkbd->cmdbuf[(receive - 1) - i];	if (atkbd->cmdcnt && (command != ATKBD_CMD_RESET_BAT || atkbd->cmdcnt != 1))		goto out;	rc = 0;out:	clear_bit(ATKBD_FLAG_CMD, &atkbd->flags);	clear_bit(ATKBD_FLAG_CMD1, &atkbd->flags);	up(&atkbd->cmd_sem);	return rc;}/* * atkbd_execute_scheduled_command() sends a command, previously scheduled by * atkbd_schedule_command(), to the keyboard. */static void atkbd_execute_scheduled_command(void *data){	struct atkbd_work *atkbd_work = data;	atkbd_command(atkbd_work->atkbd, atkbd_work->param, atkbd_work->command);	kfree(atkbd_work);}/* * atkbd_schedule_command() allows to schedule delayed execution of a keyboard * command and can be used to issue a command from an interrupt or softirq * context. */static int atkbd_schedule_command(struct atkbd *atkbd, unsigned char *param, int command){	struct atkbd_work *atkbd_work;	int send = (command >> 12) & 0xf;	int receive = (command >> 8) & 0xf;	if (!test_bit(ATKBD_FLAG_ENABLED, &atkbd->flags))		return -1;	if (!(atkbd_work = kmalloc(sizeof(struct atkbd_work) + max(send, receive), GFP_ATOMIC)))		return -1;	memset(atkbd_work, 0, sizeof(struct atkbd_work));	atkbd_work->atkbd = atkbd;	atkbd_work->command = command;	memcpy(atkbd_work->param, param, send);	INIT_WORK(&atkbd_work->work, atkbd_execute_scheduled_command, atkbd_work);	if (!schedule_work(&atkbd_work->work)) {		kfree(atkbd_work);		return -1;	}	return 0;}/* * Event callback from the input module. Events that change the state of * the hardware are processed here. */static int atkbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value){	struct atkbd *atkbd = dev->private;	const short period[32] =		{ 33,  37,  42,  46,  50,  54,  58,  63,  67,  75,  83,  92, 100, 109, 116, 125,		 133, 149, 167, 182, 200, 217, 232, 250, 270, 303, 333, 370, 400, 435, 470, 500 };	const short delay[4] =		{ 250, 500, 750, 1000 };	unsigned char param[2];	int i, j;	if (!atkbd->write)		return -1;	switch (type) {		case EV_LED:			param[0] = (test_bit(LED_SCROLLL, dev->led) ? 1 : 0)			         | (test_bit(LED_NUML,    dev->led) ? 2 : 0)			         | (test_bit(LED_CAPSL,   dev->led) ? 4 : 0);		        atkbd_schedule_command(atkbd, param, ATKBD_CMD_SETLEDS);			if (atkbd->extra) {				param[0] = 0;				param[1] = (test_bit(LED_COMPOSE, dev->led) ? 0x01 : 0)					 | (test_bit(LED_SLEEP,   dev->led) ? 0x02 : 0)					 | (test_bit(LED_SUSPEND, dev->led) ? 0x04 : 0)				         | (test_bit(LED_MISC,    dev->led) ? 0x10 : 0)				         | (test_bit(LED_MUTE,    dev->led) ? 0x20 : 0);				atkbd_schedule_command(atkbd, param, ATKBD_CMD_EX_SETLEDS);			}			return 0;		case EV_REP:			if (atkbd_softrepeat) return 0;			i = j = 0;			while (i < 32 && period[i] < dev->rep[REP_PERIOD]) i++;			while (j < 4 && delay[j] < dev->rep[REP_DELAY]) j++;			dev->rep[REP_PERIOD] = period[i];			dev->rep[REP_DELAY] = delay[j];			param[0] = i | (j << 5);			atkbd_schedule_command(atkbd, param, ATKBD_CMD_SETREP);			return 0;	}	return -1;}/* * atkbd_probe() probes for an AT keyboard on a serio port. */static int atkbd_probe(struct atkbd *atkbd){	unsigned char param[2];/* * Some systems, where the bit-twiddling when testing the io-lines of the * controller may confuse the keyboard need a full reset of the keyboard. On * these systems the BIOS also usually doesn't do it for us. */	if (atkbd_reset)		if (atkbd_command(atkbd, NULL, ATKBD_CMD_RESET_BAT))			printk(KERN_WARNING "atkbd.c: keyboard reset failed on %s\n", atkbd->serio->phys);/* * Then we check the keyboard ID. We should get 0xab83 under normal conditions. * Some keyboards report different values, but the first byte is always 0xab or * 0xac. Some old AT keyboards don't report anything. If a mouse is connected, this * should make sure we don't try to set the LEDs on it. */	param[0] = param[1] = 0xa5;	/* initialize with invalid values */	if (atkbd_command(atkbd, param, ATKBD_CMD_GETID)) {/* * If the get ID command failed, we check if we can at least set the LEDs on * the keyboard. This should work on every keyboard out there. It also turns * the LEDs off, which we want anyway. */		param[0] = 0;		if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))			return -1;		atkbd->id = 0xabba;		return 0;	}	if (param[0] != 0xab && param[0] != 0xac)		return -1;	atkbd->id = (param[0] << 8) | param[1];	if (atkbd->id == 0xaca1 && atkbd->translated) {		printk(KERN_ERR "atkbd.c: NCD terminal keyboards are only supported on non-translating\n");		printk(KERN_ERR "atkbd.c: controllers. Use i8042.direct=1 to disable translation.\n");		return -1;	}	return 0;}/* * atkbd_set_3 checks if a keyboard has a working Set 3 support, and * sets it into that. Unfortunately there are keyboards that can be switched * to Set 3, but don't work well in that (BTC Multimedia ...) */static int atkbd_set_3(struct atkbd *atkbd){	unsigned char param[2];/* * For known special keyboards we can go ahead and set the correct set. * We check for NCD PS/2 Sun, NorthGate OmniKey 101 and * IBM RapidAccess / IBM EzButton / Chicony KBP-8993 keyboards. */	if (atkbd->translated)		return 2;	if (atkbd->id == 0xaca1) {		param[0] = 3;		atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET);		return 3;	}	if (atkbd_extra) {		param[0] = 0x71;		if (!atkbd_command(atkbd, param, ATKBD_CMD_EX_ENABLE)) {			atkbd->extra = 1;			return 2;		}	}	if (atkbd_set != 3)		return 2;	if (!atkbd_command(atkbd, param, ATKBD_CMD_OK_GETID)) {		atkbd->id = param[0] << 8 | param[1];		return 2;	}	param[0] = 3;	if (atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET))		return 2;	param[0] = 0;	if (atkbd_command(atkbd, param, ATKBD_CMD_GSCANSET))		return 2;	if (param[0] != 3) {		param[0] = 2;		if (atkbd_command(atkbd, param, ATKBD_CMD_SSCANSET))		return 2;	}	atkbd_command(atkbd, param, ATKBD_CMD_SETALL_MBR);	return 3;}static int atkbd_enable(struct atkbd *atkbd){	unsigned char param[1];/* * Set the LEDs to a defined state. */	param[0] = 0;	if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))		return -1;/* * Set autorepeat to fastest possible. */	param[0] = 0;	if (atkbd_command(atkbd, param, ATKBD_CMD_SETREP))		return -1;/* * Enable the keyboard to receive keystrokes. */	if (atkbd_command(atkbd, NULL, ATKBD_CMD_ENABLE)) {		printk(KERN_ERR "atkbd.c: Failed to enable keyboard on %s\n",			atkbd->serio->phys);		return -1;	}	return 0;}/* * atkbd_cleanup() restores the keyboard state so that BIOS is happy after a * reboot. */static void atkbd_cleanup(struct serio *serio){	struct atkbd *atkbd = serio->private;	atkbd_command(atkbd, NULL, ATKBD_CMD_RESET_BAT);}/* * atkbd_disconnect() closes and frees. */static void atkbd_disconnect(struct serio *serio){	struct atkbd *atkbd = serio->private;	clear_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);	synchronize_kernel();	flush_scheduled_work();	input_unregister_device(&atkbd->dev);	serio_close(serio);	kfree(atkbd);}/* * atkbd_connect() is called when the serio module finds and interface * that isn't handled yet by an appropriate device driver. We check if * there is an AT keyboard out there and if yes, we register ourselves * to the input module. */static void atkbd_connect(struct serio *serio, struct serio_driver *drv){	struct atkbd *atkbd;	int i;	if (!(atkbd = kmalloc(sizeof(struct atkbd), GFP_KERNEL)))		return;	memset(atkbd, 0, sizeof(struct atkbd));	init_MUTEX(&atkbd->cmd_sem);	init_waitqueue_head(&atkbd->wait);	switch (serio->type & SERIO_TYPE) {		case SERIO_8042_XL:			atkbd->translated = 1;		case SERIO_8042:			if (serio->write)				atkbd->write = 1;			break;		case SERIO_RS232:			if ((serio->type & SERIO_PROTO) == SERIO_PS2SER)				break;		default:			kfree(atkbd);			return;	}	if (!atkbd->write)		atkbd_softrepeat = 1;	if (atkbd_softrepeat)		atkbd_softraw = 1;	if (atkbd->write) {		atkbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP) | BIT(EV_MSC);		atkbd->dev.ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL);	} else  atkbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REP) | BIT(EV_MSC);	atkbd->dev.mscbit[0] = atkbd_softraw ? BIT(MSC_SCAN) : BIT(MSC_RAW) | BIT(MSC_SCAN);	if (!atkbd_softrepeat) {		atkbd->dev.rep[REP_DELAY] = 250;		atkbd->dev.rep[REP_PERIOD] = 33;	} else atkbd_softraw = 1;	atkbd->serio = serio;	init_input_dev(&atkbd->dev);	atkbd->dev.keycode = atkbd->keycode;	atkbd->dev.keycodesize = sizeof(unsigned char);	atkbd->dev.keycodemax = ARRAY_SIZE(atkbd_set2_keycode);	atkbd->dev.event = atkbd_event;	atkbd->dev.private = atkbd;	serio->private = atkbd;	if (serio_open(serio, drv)) {		kfree(atkbd);		return;	}	if (atkbd->write) {		if (atkbd_probe(atkbd)) {			serio_close(serio);			serio->private = NULL;			kfree(atkbd);			return;		}		atkbd->set = atkbd_set_3(atkbd);		atkbd_enable(atkbd);	} else {		atkbd->set = 2;		atkbd->id = 0xab00;	}	if (atkbd->extra) {		atkbd->dev.ledbit[0] |= BIT(LED_COMPOSE) | BIT(LED_SUSPEND) | BIT(LED_SLEEP) | BIT(LED_MUTE) | BIT(LED_MISC);		sprintf(atkbd->name, "AT Set 2 Extra keyboard");	} else		sprintf(atkbd->name, "AT %s Set %d keyboard",			atkbd->translated ? "Translated" : "Raw", atkbd->set);	sprintf(atkbd->phys, "%s/input0", serio->phys);	if (atkbd_scroll) {		for (i = 0; i < 5; i++)			atkbd_set2_keycode[atkbd_scroll_keys[i][1]] = atkbd_scroll_keys[i][0];		atkbd->dev.evbit[0] |= BIT(EV_REL);		atkbd->dev.relbit[0] = BIT(REL_WHEEL);		set_bit(BTN_MIDDLE, atkbd->dev.keybit);	}	if (atkbd->translated) {		for (i = 0; i < 128; i++) {			atkbd->keycode[i] = atkbd_set2_keycode[atkbd_unxlate_table[i]];			atkbd->keycode[i | 0x80] = atkbd_set2_keycode[atkbd_unxlate_table[i] | 0x80];		}	} else if (atkbd->set == 3) {		memcpy(atkbd->keycode, atkbd_set3_keycode, sizeof(atkbd->keycode));	} else {		memcpy(atkbd->keycode, atkbd_set2_keycode, sizeof(atkbd->keycode));	}	atkbd->dev.name = atkbd->name;	atkbd->dev.phys = atkbd->phys;	atkbd->dev.id.bustype = BUS_I8042;	atkbd->dev.id.vendor = 0x0001;	atkbd->dev.id.product = atkbd->translated ? 1 : atkbd->set;	atkbd->dev.id.version = atkbd->id;	for (i = 0; i < 512; i++)		if (atkbd->keycode[i] && atkbd->keycode[i] < ATKBD_SPECIAL)			set_bit(atkbd->keycode[i], atkbd->dev.keybit);	input_register_device(&atkbd->dev);	set_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);	printk(KERN_INFO "input: %s on %s\n", atkbd->name, serio->phys);}/* * atkbd_reconnect() tries to restore keyboard into a sane state and is * most likely called on resume. */static int atkbd_reconnect(struct serio *serio){	struct atkbd *atkbd = serio->private;	struct serio_driver *drv = serio->drv;	unsigned char param[1];	if (!drv) {		printk(KERN_DEBUG "atkbd: reconnect request, but serio is disconnected, ignoring...\n");		return -1;	}	if (atkbd->write) {		param[0] = (test_bit(LED_SCROLLL, atkbd->dev.led) ? 1 : 0)		         | (test_bit(LED_NUML,    atkbd->dev.led) ? 2 : 0) 		         | (test_bit(LED_CAPSL,   atkbd->dev.led) ? 4 : 0);		if (atkbd_probe(atkbd))			return -1;		if (atkbd->set != atkbd_set_3(atkbd))			return -1;		atkbd_enable(atkbd);		if (atkbd_command(atkbd, param, ATKBD_CMD_SETLEDS))			return -1;	}	set_bit(ATKBD_FLAG_ENABLED, &atkbd->flags);	return 0;}static struct serio_driver atkbd_drv = {	.driver		= {		.name	= "atkbd",	},	.description	= DRIVER_DESC,	.interrupt	= atkbd_interrupt,	.connect	= atkbd_connect,	.reconnect	= atkbd_reconnect,	.disconnect	= atkbd_disconnect,	.cleanup	= atkbd_cleanup,};int __init atkbd_init(void){	serio_register_driver(&atkbd_drv);	return 0;}void __exit atkbd_exit(void){	serio_unregister_driver(&atkbd_drv);}module_init(atkbd_init);module_exit(atkbd_exit);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?