Added new SCSI_ASENSE_NOT_READY_TO_READY_CHANGE constant to the Mass Storage class...
[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_SendData(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
186 void* PrinterCommands,
187 const uint16_t CommandSize)
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(PrinterCommands, CommandSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
198 return ErrorCode;
199
200 Pipe_ClearOUT();
201 while (!(Pipe_IsOUTReady()))
202 {
203 if (USB_HostState == HOST_STATE_Unattached)
204 return PIPE_RWSTREAM_DeviceDisconnected;
205 }
206
207 Pipe_Freeze();
208
209 return PIPE_RWSTREAM_NoError;
210 }
211
212 uint8_t PRNT_Host_GetDeviceID(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
213 char* const DeviceIDString,
214 const uint16_t BufferSize)
215 {
216 uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
217 uint16_t DeviceIDStringLength = 0;
218
219 USB_ControlRequest = (USB_Request_Header_t)
220 {
221 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
222 .bRequest = REQ_GetDeviceID,
223 .wValue = 0,
224 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
225 .wLength = sizeof(DeviceIDStringLength),
226 };
227
228 Pipe_SelectPipe(PIPE_CONTROLPIPE);
229
230 if ((ErrorCode = USB_Host_SendControlRequest(&DeviceIDStringLength)) != HOST_SENDCONTROL_Successful)
231 return ErrorCode;
232
233 if (!(DeviceIDStringLength))
234 {
235 DeviceIDString[0] = 0x00;
236 return HOST_SENDCONTROL_Successful;
237 }
238
239 DeviceIDStringLength = SwapEndian_16(DeviceIDStringLength);
240
241 if (DeviceIDStringLength > BufferSize)
242 DeviceIDStringLength = BufferSize;
243
244 USB_ControlRequest.wLength = DeviceIDStringLength;
245
246 if ((ErrorCode = USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)
247 return ErrorCode;
248
249 memmove(&DeviceIDString[0], &DeviceIDString[2], DeviceIDStringLength - 2);
250
251 DeviceIDString[DeviceIDStringLength - 2] = 0x00;
252
253 return HOST_SENDCONTROL_Successful;
254 }
255
256 #endif