aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lufa/Demos/DualRole/ClassDriver/MouseHostDevice/HostFunctions.c
blob: 8e602add1bbd45f88bd37c5f4cade8af8d7459ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
             LUFA Library
     Copyright (C) Dean Camera, 2017.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaims all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  Host Mode USB Mouse functionality for the MouseHostDevice demo. This file contains the Host mode
 *  USB Mouse related code of the demo and is responsible for all the Host mode Mouse functionality.
 */

#include "HostFunctions.h"

/** LUFA HID Class driver interface configuration and state information. This structure is
 *  passed to all HID Class driver functions, so that multiple instances of the same class
 *  within a device can be differentiated from one another.
 */
USB_ClassInfo_HID_Host_t Mouse_HID_Host_Interface =
	{
		.Config =
			{
				.DataINPipe             =
					{
						.Address        = (PIPE_DIR_IN  | 1),
						.Banks          = 1,
					},
				.DataINPipe             =
					{
						.Address        = (PIPE_DIR_OUT | 2),
						.Banks          = 1,
					},
				.HIDInterfaceProtocol   = HID_CSCP_MouseBootProtocol,
			},
	};


/** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
 *  starts the library USB task to begin the enumeration and USB management process.
 */
void EVENT_USB_Host_DeviceAttached(void)
{
	puts_P(PSTR("Device Attached.\r\n"));
	LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
}

/** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
 *  stops the library USB task management process.
 */
void EVENT_USB_Host_DeviceUnattached(void)
{
	puts_P(PSTR("\r\nDevice Unattached.\r\n"));
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
}

/** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
 *  enumerated by the host and is now ready to be used by the application.
 */
void EVENT_USB_Host_DeviceEnumerationComplete(void)
{
	LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);

	uint16_t ConfigDescriptorSize;
	uint8_t  ConfigDescriptorData[512];

	if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
	                                       sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
	{
		printf("Error Retrieving Configuration Descriptor.\r\n");
		LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
		return;
	}

	if (HID_Host_ConfigurePipes(&Mouse_HID_Host_Interface,
	                            ConfigDescriptorSize, ConfigDescriptorData) != HID_ENUMERROR_NoError)
	{
		printf("Attached Device Not a Valid Mouse.\r\n");
		LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
		return;
	}

	if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
	{
		printf("Error Setting Device Configuration.\r\n");
		LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
		return;
	}

	if (HID_Host_SetBootProtocol(&Mouse_HID_Host_Interface) != HOST_SENDCONTROL_Successful)
	{
		printf("Could not Set Boot Protocol Mode.\r\n");
		LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
		return;
	}

	printf("Mouse Enumerated.\r\n");
	LEDs_SetAllLEDs(LEDMASK_USB_READY);
}

/** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
{
	USB_Disable();

	printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
	                         " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);

	LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
	for(;;);
}

/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
 *  enumerating an attached USB device.
 */
void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
                                            const uint8_t SubErrorCode)
{
	printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
	                         " -- Error Code %d\r\n"
	                         " -- Sub Error Code %d\r\n"
	                         " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);

	LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
}

/** Host USB management task. This task handles the control of USB Mice while in USB Host mode,
 *  setting up the appropriate data pipes and processing reports from the attached device.
 */
void MouseHost_Task(void)
{
	if (USB_HostState != HOST_STATE_Configured)
	  return;

	if (HID_Host_IsReportReceived(&Mouse_HID_Host_Interface))
	{
		uint8_t LEDMask  = LEDS_NO_LEDS;

		USB_MouseReport_Data_t MouseReport;
		HID_Host_ReceiveReport(&Mouse_HID_Host_Interface, &MouseReport);

		printf_P(PSTR("dX:%2d dY:%2d Button:%d\r\n"), MouseReport.X,
		                                              MouseReport.Y,
		                                              MouseReport.Button);
		if (MouseReport.X > 0)
		  LEDMask |= LEDS_LED1;
		else if (MouseReport.X < 0)
		  LEDMask |= LEDS_LED2;

		if (MouseReport.Y > 0)
		  LEDMask |= LEDS_LED3;
		else if (MouseReport.Y < 0)
		  LEDMask |= LEDS_LED4;

		if (MouseReport.Button)
		  LEDMask  = LEDS_ALL_LEDS;

		LEDs_SetAllLEDs(LEDMask);
	}
}
span class="p">] = 0; acm_accepted_boot_policy_name = polname; } } custom_param("ssidref", set_dom0_ssidref); int acm_set_policy_reference(u8 *buf, u32 buf_size) { char *name = (char *)(buf + sizeof(struct acm_policy_reference_buffer)); struct acm_policy_reference_buffer *pr = (struct acm_policy_reference_buffer *)buf; if (acm_accepted_boot_policy_name != NULL) { if (strcmp(acm_accepted_boot_policy_name, name)) { printk("Policy's name '%s' is not the expected one '%s'.\n", name, acm_accepted_boot_policy_name); return ACM_ERROR; } } acm_bin_pol.policy_reference_name = (char *)xmalloc_array(u8, be32_to_cpu(pr->len)); if (!acm_bin_pol.policy_reference_name) return -ENOMEM; strlcpy(acm_bin_pol.policy_reference_name, name, be32_to_cpu(pr->len)); printk("%s: Activating policy %s\n", __func__, acm_bin_pol.policy_reference_name); return 0; } int acm_dump_policy_reference(u8 *buf, u32 buf_size) { struct acm_policy_reference_buffer *pr_buf = (struct acm_policy_reference_buffer *)buf; int ret = sizeof(struct acm_policy_reference_buffer) + strlen(acm_bin_pol.policy_reference_name) + 1; ret = (ret + 7) & ~7; if (buf_size < ret) return -EINVAL; memset(buf, 0, ret); pr_buf->len = cpu_to_be32(strlen(acm_bin_pol.policy_reference_name) + 1); /* including stringend '\0' */ strlcpy((char *)(buf + sizeof(struct acm_policy_reference_buffer)), acm_bin_pol.policy_reference_name, be32_to_cpu(pr_buf->len)); return ret; } int acm_init_binary_policy(u32 policy_code) { int ret = ACM_OK; acm_bin_pol.primary_policy_code = (policy_code & 0x0f); acm_bin_pol.secondary_policy_code = (policy_code >> 4) & 0x0f; write_lock(&acm_bin_pol_rwlock); /* set primary policy component */ switch ((policy_code) & 0x0f) { case ACM_CHINESE_WALL_POLICY: acm_init_chwall_policy(); acm_bin_pol.primary_policy_code = ACM_CHINESE_WALL_POLICY; acm_primary_ops = &acm_chinesewall_ops; break; case ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY: acm_init_ste_policy(); acm_bin_pol.primary_policy_code = ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY; acm_primary_ops = &acm_simple_type_enforcement_ops; break; case ACM_NULL_POLICY: acm_bin_pol.primary_policy_code = ACM_NULL_POLICY; acm_primary_ops = &acm_null_ops; break; default: /* Unknown policy not allowed primary */ ret = -EINVAL; goto out; } /* secondary policy component part */ switch ((policy_code) >> 4) { case ACM_NULL_POLICY: acm_bin_pol.secondary_policy_code = ACM_NULL_POLICY; acm_secondary_ops = &acm_null_ops; break; case ACM_CHINESE_WALL_POLICY: if (acm_bin_pol.primary_policy_code == ACM_CHINESE_WALL_POLICY) { /* not a valid combination */ ret = -EINVAL; goto out; } acm_init_chwall_policy(); acm_bin_pol.secondary_policy_code = ACM_CHINESE_WALL_POLICY; acm_secondary_ops = &acm_chinesewall_ops; break; case ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY: if (acm_bin_pol.primary_policy_code == ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY) { /* not a valid combination */ ret = -EINVAL; goto out; } acm_init_ste_policy(); acm_bin_pol.secondary_policy_code = ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY; acm_secondary_ops = &acm_simple_type_enforcement_ops; break; default: ret = -EINVAL; goto out; } out: write_unlock(&acm_bin_pol_rwlock); return ret; } int acm_is_policy(char *buf, unsigned long len) { struct acm_policy_buffer *pol; if (buf == NULL || len < sizeof(struct acm_policy_buffer)) return 0; pol = (struct acm_policy_buffer *)buf; return be32_to_cpu(pol->magic) == ACM_MAGIC; } static int acm_setup(char *policy_start, unsigned long policy_len, int is_bootpolicy) { int rc = ACM_OK; struct acm_policy_buffer *pol; if (policy_start == NULL || policy_len < sizeof(struct acm_policy_buffer)) return rc; pol = (struct acm_policy_buffer *)policy_start; if (be32_to_cpu(pol->magic) != ACM_MAGIC) return rc; rc = do_acm_set_policy((void *)policy_start, (u32)policy_len, is_bootpolicy, NULL, NULL, NULL); if (rc == ACM_OK) { printkd("Policy len 0x%lx, start at %p.\n",policy_len,policy_start); } else { printk("Invalid policy.\n"); /* load default policy later */ acm_active_security_policy = ACM_POLICY_UNDEFINED; } return rc; } int acm_init(void) { int ret = ACM_OK; printk("ACM-XSM: Initializing.\n"); /* first try to load the boot policy (uses its own locks) */ acm_setup(policy_buffer, policy_size, 1); /* a user-provided policy may have any name; only matched during boot */ acm_accepted_boot_policy_name = NULL; if (acm_active_security_policy != ACM_POLICY_UNDEFINED) { printk("%s: Enforcing %s boot policy.\n", __func__, ACM_POLICY_NAME(acm_active_security_policy)); goto out; } /* else continue with the minimal hardcoded default startup policy */ printk("%s: Loading default policy (%s).\n", __func__, ACM_POLICY_NAME(ACM_DEFAULT_SECURITY_POLICY)); /* (re-)set dom-0 ssidref to default */ dom0_ste_ssidref = dom0_chwall_ssidref = 0x0001; if (acm_init_binary_policy(ACM_DEFAULT_SECURITY_POLICY)) { ret = -EINVAL; goto out; } acm_active_security_policy = ACM_DEFAULT_SECURITY_POLICY; if (acm_active_security_policy != ACM_NULL_POLICY) acm_bin_pol.policy_reference_name = "DEFAULT"; else acm_bin_pol.policy_reference_name = "NULL"; out: if (ret != ACM_OK) { printk("%s: Error initializing policies.\n", __func__); /* here one could imagine a clean panic */ return -EINVAL; } if (register_xsm(&acm_xsm_ops)) panic("ACM-XSM: Unable to register with XSM.\n"); return ret; } xsm_initcall(acm_init); int acm_init_domain_ssid(struct domain *subj, ssidref_t ssidref) { struct acm_ssid_domain *ssid; int ret1, ret2; if ((ssid = xmalloc(struct acm_ssid_domain)) == NULL) { return ACM_INIT_SSID_ERROR; } INIT_LIST_HEAD(&ssid->node); ssid->datatype = ACM_DATATYPE_domain; ssid->subject = subj; ssid->domainid = subj->domain_id; ssid->primary_ssid = NULL; ssid->secondary_ssid = NULL; if (acm_active_security_policy != ACM_NULL_POLICY) ssid->ssidref = ssidref; else ssid->ssidref = ACM_DEFAULT_SSID; subj->ssid = ssid; /* now fill in primary and secondary parts; we only get here through hooks */ if (acm_primary_ops->init_domain_ssid != NULL) ret1 = acm_primary_ops->init_domain_ssid(&(ssid->primary_ssid), ssidref); else ret1 = ACM_OK; if (acm_secondary_ops->init_domain_ssid != NULL) ret2 = acm_secondary_ops->init_domain_ssid(&(ssid->secondary_ssid), ssidref); else ret2 = ACM_OK; if ((ret1 != ACM_OK) || (ret2 != ACM_OK)) { printk("%s: ERROR instantiating individual ssids for domain 0x%02x.\n", __func__, subj->domain_id); acm_free_domain_ssid(subj); return ACM_INIT_SSID_ERROR; } printkd("%s: assigned domain %x the ssidref=%x.\n", __func__, subj->domain_id, ssid->ssidref); return ACM_OK; } void acm_free_domain_ssid(struct domain *d) { struct acm_ssid_domain *ssid = d->ssid; /* domain is already gone, just ssid is left */ if (ssid == NULL) return; ssid->subject = NULL; if (acm_primary_ops->free_domain_ssid != NULL) /* null policy */ acm_primary_ops->free_domain_ssid(ssid->primary_ssid); ssid->primary_ssid = NULL; if (acm_secondary_ops->free_domain_ssid != NULL) acm_secondary_ops->free_domain_ssid(ssid->secondary_ssid); ssid->secondary_ssid = NULL; xfree(ssid); d->ssid = NULL; printkd("%s: Freed individual domain ssid (domain=%02x).\n", __func__, id); } /* * Local variables: * mode: C * c-set-style: "BSD" * c-basic-offset: 4 * tab-width: 4 * indent-tabs-mode: nil * End: */