Be doubly-certain that the incomming CDC class driver's endpoint/pipe is flushed...
[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 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
33
34 #define INCLUDE_FROM_PRINTER_CLASS_HOST_C
35 #include "Printer.h"
36
37 uint8_t PRNT_Host_ConfigurePipes(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo, uint16_t ConfigDescriptorSize,
38 void* DeviceConfigDescriptor)
39 {
40 uint8_t FoundEndpoints = 0;
41
42 memset(&PRNTInterfaceInfo->State, 0x00, sizeof(PRNTInterfaceInfo->State));
43
44 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor) != DTYPE_Configuration)
45 return PRNT_ENUMERROR_InvalidConfigDescriptor;
46
47 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &DeviceConfigDescriptor,
48 DComp_NextPRNTInterface) != DESCRIPTOR_SEARCH_COMP_Found)
49 {
50 return PRNT_ENUMERROR_NoPrinterInterfaceFound;
51 }
52
53 USB_Descriptor_Interface_t* PrinterInterface = DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Interface_t);
54
55 PRNTInterfaceInfo->State.InterfaceNumber = PrinterInterface->InterfaceNumber;
56 PRNTInterfaceInfo->State.AlternateSetting = PrinterInterface->AlternateSetting;
57
58 while (FoundEndpoints != (PRNT_FOUND_DATAPIPE_IN | PRNT_FOUND_DATAPIPE_OUT))
59 {
60 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &DeviceConfigDescriptor,
61 DComp_NextPRNTInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
62 {
63 return PRNT_ENUMERROR_EndpointsNotFound;
64 }
65
66 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Endpoint_t);
67
68 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
69 {
70 Pipe_ConfigurePipe(PRNTInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_IN,
71 EndpointData->EndpointAddress, EndpointData->EndpointSize,
72 PRNTInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
73 PRNTInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
74
75 FoundEndpoints |= PRNT_FOUND_DATAPIPE_IN;
76 }
77 else
78 {
79 Pipe_ConfigurePipe(PRNTInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_OUT,
80 EndpointData->EndpointAddress, EndpointData->EndpointSize,
81 PRNTInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
82 PRNTInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
83
84 FoundEndpoints |= PRNT_FOUND_DATAPIPE_OUT;
85 }
86 }
87
88 PRNTInterfaceInfo->State.IsActive = true;
89 return PRNT_ENUMERROR_NoError;
90 }
91
92 static uint8_t DComp_NextPRNTInterface(void* CurrentDescriptor)
93 {
94 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
95 {
96 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == PRINTER_CLASS) &&
97 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == PRINTER_SUBCLASS) &&
98 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == PRINTER_PROTOCOL))
99 {
100 return DESCRIPTOR_SEARCH_Found;
101 }
102 }
103
104 return DESCRIPTOR_SEARCH_NotFound;
105 }
106
107 static uint8_t DComp_NextPRNTInterfaceEndpoint(void* CurrentDescriptor)
108 {
109 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
110 {
111 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
112 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
113
114 if (EndpointType == EP_TYPE_BULK)
115 return DESCRIPTOR_SEARCH_Found;
116 }
117 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
118 {
119 return DESCRIPTOR_SEARCH_Fail;
120 }
121
122 return DESCRIPTOR_SEARCH_NotFound;
123 }
124
125 uint8_t PRNT_Host_SetBidirectionalMode(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
126 {
127 if (PRNTInterfaceInfo->State.AlternateSetting)
128 {
129 uint8_t ErrorCode;
130
131 USB_ControlRequest = (USB_Request_Header_t)
132 {
133 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),
134 .bRequest = REQ_SetInterface,
135 .wValue = PRNTInterfaceInfo->State.AlternateSetting,
136 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
137 .wLength = 0,
138 };
139
140 Pipe_SelectPipe(PIPE_CONTROLPIPE);
141
142 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
143 return ErrorCode;
144 }
145
146 return HOST_SENDCONTROL_Successful;
147 }
148
149 uint8_t PRNT_Host_GetPortStatus(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo, uint8_t* const PortStatus)
150 {
151 USB_ControlRequest = (USB_Request_Header_t)
152 {
153 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
154 .bRequest = REQ_GetPortStatus,
155 .wValue = 0,
156 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
157 .wLength = sizeof(uint8_t),
158 };
159
160 Pipe_SelectPipe(PIPE_CONTROLPIPE);
161
162 return USB_Host_SendControlRequest(PortStatus);
163 }
164
165 uint8_t PRNT_Host_SoftReset(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
166 {
167 USB_ControlRequest = (USB_Request_Header_t)
168 {
169 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
170 .bRequest = REQ_SoftReset,
171 .wValue = 0,
172 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
173 .wLength = 0,
174 };
175
176 Pipe_SelectPipe(PIPE_CONTROLPIPE);
177
178 return USB_Host_SendControlRequest(NULL);
179 }
180
181 uint8_t PRNT_Host_SendData(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo, void* PrinterCommands, uint16_t CommandSize)
182 {
183 uint8_t ErrorCode;
184
185 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
186 return PIPE_RWSTREAM_DeviceDisconnected;
187
188 Pipe_SelectPipe(PRNTInterfaceInfo->Config.DataOUTPipeNumber);
189 Pipe_Unfreeze();
190
191 if ((ErrorCode = Pipe_Write_Stream_LE(PrinterCommands, CommandSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
192 return ErrorCode;
193
194 Pipe_ClearOUT();
195 while (!(Pipe_IsOUTReady()))
196 {
197 if (USB_HostState == HOST_STATE_Unattached)
198 return PIPE_RWSTREAM_DeviceDisconnected;
199 }
200
201 Pipe_Freeze();
202
203 return PIPE_RWSTREAM_NoError;
204 }
205
206 uint8_t PRNT_Host_GetDeviceID(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo, char* DeviceIDString, uint16_t BufferSize)
207 {
208 uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
209 uint16_t DeviceIDStringLength = 0;
210
211 USB_ControlRequest = (USB_Request_Header_t)
212 {
213 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
214 .bRequest = REQ_GetDeviceID,
215 .wValue = 0,
216 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
217 .wLength = sizeof(DeviceIDStringLength),
218 };
219
220 Pipe_SelectPipe(PIPE_CONTROLPIPE);
221
222 if ((ErrorCode = USB_Host_SendControlRequest(&DeviceIDStringLength)) != HOST_SENDCONTROL_Successful)
223 return ErrorCode;
224
225 if (!(DeviceIDStringLength))
226 {
227 DeviceIDString[0] = 0x00;
228 return HOST_SENDCONTROL_Successful;
229 }
230
231 DeviceIDStringLength = SwapEndian_16(DeviceIDStringLength);
232
233 if (DeviceIDStringLength > BufferSize)
234 DeviceIDStringLength = BufferSize;
235
236 USB_ControlRequest.wLength = DeviceIDStringLength;
237
238 if ((ErrorCode = USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)
239 return ErrorCode;
240
241 memmove(&DeviceIDString[0], &DeviceIDString[2], DeviceIDStringLength - 2);
242
243 DeviceIDString[DeviceIDStringLength - 2] = 0x00;
244
245 return HOST_SENDCONTROL_Successful;
246 }
247
248 #endif