Added new project makefile template to the library and moved board driver stub files...
[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,
40 uint16_t ConfigDescriptorSize,
41 void* DeviceConfigDescriptor)
42 {
43 uint8_t FoundEndpoints = 0;
44
45 memset(&PRNTInterfaceInfo->State, 0x00, sizeof(PRNTInterfaceInfo->State));
46
47 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor) != DTYPE_Configuration)
48 return PRNT_ENUMERROR_InvalidConfigDescriptor;
49
50 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &DeviceConfigDescriptor,
51 DCOMP_PRNT_NextPRNTInterface) != DESCRIPTOR_SEARCH_COMP_Found)
52 {
53 return PRNT_ENUMERROR_NoPrinterInterfaceFound;
54 }
55
56 USB_Descriptor_Interface_t* PrinterInterface = DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Interface_t);
57
58 PRNTInterfaceInfo->State.InterfaceNumber = PrinterInterface->InterfaceNumber;
59 PRNTInterfaceInfo->State.AlternateSetting = PrinterInterface->AlternateSetting;
60
61 while (FoundEndpoints != (PRNT_FOUND_DATAPIPE_IN | PRNT_FOUND_DATAPIPE_OUT))
62 {
63 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &DeviceConfigDescriptor,
64 DCOMP_PRNT_NextPRNTInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
65 {
66 return PRNT_ENUMERROR_EndpointsNotFound;
67 }
68
69 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Endpoint_t);
70
71 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
72 {
73 Pipe_ConfigurePipe(PRNTInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_IN,
74 EndpointData->EndpointAddress, EndpointData->EndpointSize,
75 PRNTInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
76 PRNTInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
77
78 FoundEndpoints |= PRNT_FOUND_DATAPIPE_IN;
79 }
80 else
81 {
82 Pipe_ConfigurePipe(PRNTInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_OUT,
83 EndpointData->EndpointAddress, EndpointData->EndpointSize,
84 PRNTInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
85 PRNTInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
86
87 FoundEndpoints |= PRNT_FOUND_DATAPIPE_OUT;
88 }
89 }
90
91 PRNTInterfaceInfo->State.IsActive = true;
92 return PRNT_ENUMERROR_NoError;
93 }
94
95 static uint8_t DCOMP_PRNT_NextPRNTInterface(void* CurrentDescriptor)
96 {
97 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
98 {
99 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == PRINTER_CLASS) &&
100 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == PRINTER_SUBCLASS) &&
101 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == PRINTER_PROTOCOL))
102 {
103 return DESCRIPTOR_SEARCH_Found;
104 }
105 }
106
107 return DESCRIPTOR_SEARCH_NotFound;
108 }
109
110 static uint8_t DCOMP_PRNT_NextPRNTInterfaceEndpoint(void* CurrentDescriptor)
111 {
112 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
113 {
114 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
115 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
116
117 if (EndpointType == EP_TYPE_BULK)
118 return DESCRIPTOR_SEARCH_Found;
119 }
120 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
121 {
122 return DESCRIPTOR_SEARCH_Fail;
123 }
124
125 return DESCRIPTOR_SEARCH_NotFound;
126 }
127
128 uint8_t PRNT_Host_SetBidirectionalMode(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
129 {
130 if (PRNTInterfaceInfo->State.AlternateSetting)
131 {
132 uint8_t ErrorCode;
133
134 USB_ControlRequest = (USB_Request_Header_t)
135 {
136 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),
137 .bRequest = REQ_SetInterface,
138 .wValue = PRNTInterfaceInfo->State.AlternateSetting,
139 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
140 .wLength = 0,
141 };
142
143 Pipe_SelectPipe(PIPE_CONTROLPIPE);
144
145 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
146 return ErrorCode;
147 }
148
149 return HOST_SENDCONTROL_Successful;
150 }
151
152 uint8_t PRNT_Host_GetPortStatus(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
153 uint8_t* const PortStatus)
154 {
155 USB_ControlRequest = (USB_Request_Header_t)
156 {
157 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
158 .bRequest = REQ_GetPortStatus,
159 .wValue = 0,
160 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
161 .wLength = sizeof(uint8_t),
162 };
163
164 Pipe_SelectPipe(PIPE_CONTROLPIPE);
165
166 return USB_Host_SendControlRequest(PortStatus);
167 }
168
169 uint8_t PRNT_Host_SoftReset(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
170 {
171 USB_ControlRequest = (USB_Request_Header_t)
172 {
173 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
174 .bRequest = REQ_SoftReset,
175 .wValue = 0,
176 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
177 .wLength = 0,
178 };
179
180 Pipe_SelectPipe(PIPE_CONTROLPIPE);
181
182 return USB_Host_SendControlRequest(NULL);
183 }
184
185 uint8_t PRNT_Host_SendString(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
186 void* Buffer,
187 const uint16_t Length)
188 {
189 uint8_t ErrorCode;
190
191 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
192 return PIPE_RWSTREAM_DeviceDisconnected;
193
194 Pipe_SelectPipe(PRNTInterfaceInfo->Config.DataOUTPipeNumber);
195 Pipe_Unfreeze();
196
197 if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, Length, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
198 return ErrorCode;
199
200 Pipe_ClearOUT();
201
202 ErrorCode = Pipe_WaitUntilReady();
203
204 Pipe_Freeze();
205
206 return ErrorCode;
207 }
208
209 uint16_t PRNT_Host_BytesReceived(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
210 {
211 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
212 return 0;
213
214 Pipe_SelectPipe(PRNTInterfaceInfo->Config.DataINPipeNumber);
215 Pipe_Unfreeze();
216
217 if (Pipe_IsINReceived())
218 {
219 if (!(Pipe_BytesInPipe()))
220 {
221 Pipe_ClearIN();
222 Pipe_Freeze();
223 return 0;
224 }
225 else
226 {
227 Pipe_Freeze();
228 return Pipe_BytesInPipe();
229 }
230 }
231 else
232 {
233 Pipe_Freeze();
234
235 return 0;
236 }
237 }
238
239 int16_t PRNT_Host_ReceiveByte(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
240 {
241 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
242 return PIPE_RWSTREAM_DeviceDisconnected;
243
244 int16_t ReceivedByte = -1;
245
246 Pipe_SelectPipe(PRNTInterfaceInfo->Config.DataINPipeNumber);
247 Pipe_Unfreeze();
248
249 if (Pipe_IsINReceived())
250 {
251 if (Pipe_BytesInPipe())
252 ReceivedByte = Pipe_Read_Byte();
253
254 if (!(Pipe_BytesInPipe()))
255 Pipe_ClearIN();
256 }
257
258 Pipe_Freeze();
259
260 return ReceivedByte;
261 }
262
263 uint8_t PRNT_Host_GetDeviceID(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
264 char* const DeviceIDString,
265 const uint16_t BufferSize)
266 {
267 uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
268 uint16_t DeviceIDStringLength = 0;
269
270 USB_ControlRequest = (USB_Request_Header_t)
271 {
272 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
273 .bRequest = REQ_GetDeviceID,
274 .wValue = 0,
275 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
276 .wLength = sizeof(DeviceIDStringLength),
277 };
278
279 Pipe_SelectPipe(PIPE_CONTROLPIPE);
280
281 if ((ErrorCode = USB_Host_SendControlRequest(&DeviceIDStringLength)) != HOST_SENDCONTROL_Successful)
282 return ErrorCode;
283
284 if (!(DeviceIDStringLength))
285 {
286 DeviceIDString[0] = 0x00;
287 return HOST_SENDCONTROL_Successful;
288 }
289
290 DeviceIDStringLength = SwapEndian_16(DeviceIDStringLength);
291
292 if (DeviceIDStringLength > BufferSize)
293 DeviceIDStringLength = BufferSize;
294
295 USB_ControlRequest.wLength = DeviceIDStringLength;
296
297 if ((ErrorCode = USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)
298 return ErrorCode;
299
300 memmove(&DeviceIDString[0], &DeviceIDString[2], DeviceIDStringLength - 2);
301
302 DeviceIDString[DeviceIDStringLength - 2] = 0x00;
303
304 return HOST_SENDCONTROL_Successful;
305 }
306
307 #endif