Add new ReportType parameter to the HID class driver device callback and host report...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / Printer.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
34
35 #define __INCLUDE_FROM_PRINTER_CLASS_HOST_C
36 #define __INCLUDE_FROM_PRINTER_DRIVER
37 #include "Printer.h"
38
39 uint8_t PRNT_Host_ConfigurePipes(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo, uint16_t ConfigDescriptorSize,
40 void* DeviceConfigDescriptor)
41 {
42 uint8_t FoundEndpoints = 0;
43
44 memset(&PRNTInterfaceInfo->State, 0x00, sizeof(PRNTInterfaceInfo->State));
45
46 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor) != DTYPE_Configuration)
47 return PRNT_ENUMERROR_InvalidConfigDescriptor;
48
49 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &DeviceConfigDescriptor,
50 DCOMP_PRNT_NextPRNTInterface) != DESCRIPTOR_SEARCH_COMP_Found)
51 {
52 return PRNT_ENUMERROR_NoPrinterInterfaceFound;
53 }
54
55 USB_Descriptor_Interface_t* PrinterInterface = DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Interface_t);
56
57 PRNTInterfaceInfo->State.InterfaceNumber = PrinterInterface->InterfaceNumber;
58 PRNTInterfaceInfo->State.AlternateSetting = PrinterInterface->AlternateSetting;
59
60 while (FoundEndpoints != (PRNT_FOUND_DATAPIPE_IN | PRNT_FOUND_DATAPIPE_OUT))
61 {
62 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &DeviceConfigDescriptor,
63 DCOMP_PRNT_NextPRNTInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
64 {
65 return PRNT_ENUMERROR_EndpointsNotFound;
66 }
67
68 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Endpoint_t);
69
70 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
71 {
72 Pipe_ConfigurePipe(PRNTInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_IN,
73 EndpointData->EndpointAddress, EndpointData->EndpointSize,
74 PRNTInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
75 PRNTInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
76
77 FoundEndpoints |= PRNT_FOUND_DATAPIPE_IN;
78 }
79 else
80 {
81 Pipe_ConfigurePipe(PRNTInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_OUT,
82 EndpointData->EndpointAddress, EndpointData->EndpointSize,
83 PRNTInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
84 PRNTInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
85
86 FoundEndpoints |= PRNT_FOUND_DATAPIPE_OUT;
87 }
88 }
89
90 PRNTInterfaceInfo->State.IsActive = true;
91 return PRNT_ENUMERROR_NoError;
92 }
93
94 static uint8_t DCOMP_PRNT_NextPRNTInterface(void* CurrentDescriptor)
95 {
96 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
97 {
98 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == PRINTER_CLASS) &&
99 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == PRINTER_SUBCLASS) &&
100 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == PRINTER_PROTOCOL))
101 {
102 return DESCRIPTOR_SEARCH_Found;
103 }
104 }
105
106 return DESCRIPTOR_SEARCH_NotFound;
107 }
108
109 static uint8_t DCOMP_PRNT_NextPRNTInterfaceEndpoint(void* CurrentDescriptor)
110 {
111 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
112 {
113 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
114 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
115
116 if (EndpointType == EP_TYPE_BULK)
117 return DESCRIPTOR_SEARCH_Found;
118 }
119 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
120 {
121 return DESCRIPTOR_SEARCH_Fail;
122 }
123
124 return DESCRIPTOR_SEARCH_NotFound;
125 }
126
127 uint8_t PRNT_Host_SetBidirectionalMode(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
128 {
129 if (PRNTInterfaceInfo->State.AlternateSetting)
130 {
131 uint8_t ErrorCode;
132
133 USB_ControlRequest = (USB_Request_Header_t)
134 {
135 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),
136 .bRequest = REQ_SetInterface,
137 .wValue = PRNTInterfaceInfo->State.AlternateSetting,
138 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
139 .wLength = 0,
140 };
141
142 Pipe_SelectPipe(PIPE_CONTROLPIPE);
143
144 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
145 return ErrorCode;
146 }
147
148 return HOST_SENDCONTROL_Successful;
149 }
150
151 uint8_t PRNT_Host_GetPortStatus(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo, uint8_t* const PortStatus)
152 {
153 USB_ControlRequest = (USB_Request_Header_t)
154 {
155 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
156 .bRequest = REQ_GetPortStatus,
157 .wValue = 0,
158 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
159 .wLength = sizeof(uint8_t),
160 };
161
162 Pipe_SelectPipe(PIPE_CONTROLPIPE);
163
164 return USB_Host_SendControlRequest(PortStatus);
165 }
166
167 uint8_t PRNT_Host_SoftReset(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
168 {
169 USB_ControlRequest = (USB_Request_Header_t)
170 {
171 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
172 .bRequest = REQ_SoftReset,
173 .wValue = 0,
174 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
175 .wLength = 0,
176 };
177
178 Pipe_SelectPipe(PIPE_CONTROLPIPE);
179
180 return USB_Host_SendControlRequest(NULL);
181 }
182
183 uint8_t PRNT_Host_SendData(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo, void* PrinterCommands, uint16_t CommandSize)
184 {
185 uint8_t ErrorCode;
186
187 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
188 return PIPE_RWSTREAM_DeviceDisconnected;
189
190 Pipe_SelectPipe(PRNTInterfaceInfo->Config.DataOUTPipeNumber);
191 Pipe_Unfreeze();
192
193 if ((ErrorCode = Pipe_Write_Stream_LE(PrinterCommands, CommandSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
194 return ErrorCode;
195
196 Pipe_ClearOUT();
197 while (!(Pipe_IsOUTReady()))
198 {
199 if (USB_HostState == HOST_STATE_Unattached)
200 return PIPE_RWSTREAM_DeviceDisconnected;
201 }
202
203 Pipe_Freeze();
204
205 return PIPE_RWSTREAM_NoError;
206 }
207
208 uint8_t PRNT_Host_GetDeviceID(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo, char* DeviceIDString, uint16_t BufferSize)
209 {
210 uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
211 uint16_t DeviceIDStringLength = 0;
212
213 USB_ControlRequest = (USB_Request_Header_t)
214 {
215 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
216 .bRequest = REQ_GetDeviceID,
217 .wValue = 0,
218 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
219 .wLength = sizeof(DeviceIDStringLength),
220 };
221
222 Pipe_SelectPipe(PIPE_CONTROLPIPE);
223
224 if ((ErrorCode = USB_Host_SendControlRequest(&DeviceIDStringLength)) != HOST_SENDCONTROL_Successful)
225 return ErrorCode;
226
227 if (!(DeviceIDStringLength))
228 {
229 DeviceIDString[0] = 0x00;
230 return HOST_SENDCONTROL_Successful;
231 }
232
233 DeviceIDStringLength = SwapEndian_16(DeviceIDStringLength);
234
235 if (DeviceIDStringLength > BufferSize)
236 DeviceIDStringLength = BufferSize;
237
238 USB_ControlRequest.wLength = DeviceIDStringLength;
239
240 if ((ErrorCode = USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)
241 return ErrorCode;
242
243 memmove(&DeviceIDString[0], &DeviceIDString[2], DeviceIDStringLength - 2);
244
245 DeviceIDString[DeviceIDStringLength - 2] = 0x00;
246
247 return HOST_SENDCONTROL_Successful;
248 }
249
250 #endif