MdePkg, MdeModulePkg: Remove EFI_ABORTED from I2C protocols per PI spec 1.4.
[mirror_edk2.git] / MdeModulePkg / Bus / I2c / I2cDxe / I2cBus.c
CommitLineData
43e543bc
EL
1/** @file\r
2 This file implements I2C IO Protocol which enables the user to manipulate a single \r
3 I2C device independent of the host controller and I2C design.\r
4\r
1d956501 5 Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>\r
43e543bc
EL
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "I2cDxe.h"\r
17\r
18//\r
19// EFI_DRIVER_BINDING_PROTOCOL instance\r
20//\r
21EFI_DRIVER_BINDING_PROTOCOL gI2cBusDriverBinding = {\r
22 I2cBusDriverSupported,\r
23 I2cBusDriverStart,\r
24 I2cBusDriverStop,\r
25 0x10,\r
26 NULL,\r
27 NULL\r
28};\r
29\r
30//\r
31// Template for I2C Bus Child Device.\r
32//\r
33I2C_DEVICE_CONTEXT gI2cDeviceContextTemplate = {\r
34 I2C_DEVICE_SIGNATURE,\r
35 NULL,\r
36 { // I2cIo Protocol\r
37 I2cBusQueueRequest, // QueueRequest\r
38 NULL, // DeviceGuid\r
39 0, // DeviceIndex\r
40 0, // HardwareRevision\r
41 NULL // I2cControllerCapabilities\r
42 },\r
43 NULL, // DevicePath\r
44 NULL, // I2cDevice\r
45 NULL, // I2cBusContext\r
46};\r
47\r
48//\r
49// Template for controller device path node.\r
50//\r
51CONTROLLER_DEVICE_PATH gControllerDevicePathTemplate = {\r
52 {\r
53 HARDWARE_DEVICE_PATH,\r
54 HW_CONTROLLER_DP,\r
55 {\r
56 (UINT8) (sizeof (CONTROLLER_DEVICE_PATH)),\r
57 (UINT8) ((sizeof (CONTROLLER_DEVICE_PATH)) >> 8)\r
58 }\r
59 },\r
60 0\r
61};\r
62\r
63//\r
64// Template for vendor device path node.\r
65//\r
66VENDOR_DEVICE_PATH gVendorDevicePathTemplate = {\r
67 {\r
68 HARDWARE_DEVICE_PATH,\r
69 HW_VENDOR_DP,\r
70 {\r
71 (UINT8) (sizeof (VENDOR_DEVICE_PATH)),\r
72 (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)\r
73 }\r
74 },\r
75 { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }}\r
76};\r
77\r
78//\r
79// Driver name table \r
80//\r
81GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mI2cBusDriverNameTable[] = {\r
82 { "eng;en", (CHAR16 *) L"I2C Bus Driver" },\r
83 { NULL , NULL }\r
84};\r
85\r
86//\r
87// EFI Component Name Protocol\r
88//\r
89GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gI2cBusComponentName = {\r
90 (EFI_COMPONENT_NAME_GET_DRIVER_NAME) I2cBusComponentNameGetDriverName,\r
91 (EFI_COMPONENT_NAME_GET_CONTROLLER_NAME) I2cBusComponentNameGetControllerName,\r
92 "eng"\r
93};\r
94\r
95//\r
96// EFI Component Name 2 Protocol\r
97//\r
98GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gI2cBusComponentName2 = {\r
99 I2cBusComponentNameGetDriverName,\r
100 I2cBusComponentNameGetControllerName,\r
101 "en"\r
102};\r
103\r
104/**\r
105 Retrieves a Unicode string that is the user readable name of the driver.\r
106\r
107 This function retrieves the user readable name of a driver in the form of a\r
108 Unicode string. If the driver specified by This has a user readable name in\r
109 the language specified by Language, then a pointer to the driver name is\r
110 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified\r
111 by This does not support the language specified by Language,\r
112 then EFI_UNSUPPORTED is returned.\r
113\r
114 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or\r
115 EFI_COMPONENT_NAME_PROTOCOL instance.\r
116\r
117 @param Language[in] A pointer to a Null-terminated ASCII string\r
118 array indicating the language. This is the\r
119 language of the driver name that the caller is\r
120 requesting, and it must match one of the\r
121 languages specified in SupportedLanguages. The\r
122 number of languages supported by a driver is up\r
123 to the driver writer. Language is specified\r
124 in RFC 4646 or ISO 639-2 language code format.\r
125\r
126 @param DriverName[out] A pointer to the Unicode string to return.\r
127 This Unicode string is the name of the\r
128 driver specified by This in the language\r
129 specified by Language.\r
130\r
131 @retval EFI_SUCCESS The Unicode string for the Driver specified by\r
132 This and the language specified by Language was\r
133 returned in DriverName.\r
134\r
135 @retval EFI_INVALID_PARAMETER Language is NULL.\r
136\r
137 @retval EFI_INVALID_PARAMETER DriverName is NULL.\r
138\r
139 @retval EFI_UNSUPPORTED The driver specified by This does not support\r
140 the language specified by Language.\r
141\r
142**/\r
143EFI_STATUS\r
144EFIAPI\r
145I2cBusComponentNameGetDriverName (\r
146 IN EFI_COMPONENT_NAME2_PROTOCOL *This,\r
147 IN CHAR8 *Language,\r
148 OUT CHAR16 **DriverName\r
149 )\r
150{\r
151 return LookupUnicodeString2 (\r
152 Language,\r
153 This->SupportedLanguages,\r
154 mI2cBusDriverNameTable,\r
155 DriverName,\r
156 (BOOLEAN)(This != &gI2cBusComponentName2)\r
157 );\r
158}\r
159\r
160/**\r
161 Retrieves a Unicode string that is the user readable name of the controller\r
162 that is being managed by a driver.\r
163\r
164 This function retrieves the user readable name of the controller specified by\r
165 ControllerHandle and ChildHandle in the form of a Unicode string. If the\r
166 driver specified by This has a user readable name in the language specified by\r
167 Language, then a pointer to the controller name is returned in ControllerName,\r
168 and EFI_SUCCESS is returned. If the driver specified by This is not currently\r
169 managing the controller specified by ControllerHandle and ChildHandle,\r
170 then EFI_UNSUPPORTED is returned. If the driver specified by This does not\r
171 support the language specified by Language, then EFI_UNSUPPORTED is returned.\r
172\r
173 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or\r
174 EFI_COMPONENT_NAME_PROTOCOL instance.\r
175\r
176 @param ControllerHandle[in] The handle of a controller that the driver\r
177 specified by This is managing. This handle\r
178 specifies the controller whose name is to be\r
179 returned.\r
180\r
181 @param ChildHandle[in] The handle of the child controller to retrieve\r
182 the name of. This is an optional parameter that\r
183 may be NULL. It will be NULL for device\r
184 drivers. It will also be NULL for a bus drivers\r
185 that wish to retrieve the name of the bus\r
186 controller. It will not be NULL for a bus\r
187 driver that wishes to retrieve the name of a\r
188 child controller.\r
189\r
190 @param Language[in] A pointer to a Null-terminated ASCII string\r
191 array indicating the language. This is the\r
192 language of the driver name that the caller is\r
193 requesting, and it must match one of the\r
194 languages specified in SupportedLanguages. The\r
195 number of languages supported by a driver is up\r
196 to the driver writer. Language is specified in\r
197 RFC 4646 or ISO 639-2 language code format.\r
198\r
199 @param ControllerName[out] A pointer to the Unicode string to return.\r
200 This Unicode string is the name of the\r
201 controller specified by ControllerHandle and\r
202 ChildHandle in the language specified by\r
203 Language from the point of view of the driver\r
204 specified by This.\r
205\r
206 @retval EFI_SUCCESS The Unicode string for the user readable name in\r
207 the language specified by Language for the\r
208 driver specified by This was returned in\r
209 DriverName.\r
210\r
211 @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.\r
212\r
213 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid\r
214 EFI_HANDLE.\r
215\r
216 @retval EFI_INVALID_PARAMETER Language is NULL.\r
217\r
218 @retval EFI_INVALID_PARAMETER ControllerName is NULL.\r
219\r
220 @retval EFI_UNSUPPORTED The driver specified by This is not currently\r
221 managing the controller specified by\r
222 ControllerHandle and ChildHandle.\r
223\r
224 @retval EFI_UNSUPPORTED The driver specified by This does not support\r
225 the language specified by Language.\r
226\r
227**/\r
228EFI_STATUS\r
229EFIAPI\r
230I2cBusComponentNameGetControllerName (\r
231 IN EFI_COMPONENT_NAME2_PROTOCOL *This,\r
232 IN EFI_HANDLE ControllerHandle,\r
233 IN EFI_HANDLE ChildHandle OPTIONAL,\r
234 IN CHAR8 *Language,\r
235 OUT CHAR16 **ControllerName\r
236 )\r
237{\r
238 return EFI_UNSUPPORTED;\r
239}\r
240\r
241/**\r
242 Check if the child of I2C controller has been created.\r
243\r
244 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance. \r
245 @param[in] Controller I2C controller handle.\r
246 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.\r
247 @param[in] RemainingHasControllerNode Indicate if RemainingDevicePath contains CONTROLLER_DEVICE_PATH.\r
248 @param[in] RemainingControllerNumber Controller number in CONTROLLER_DEVICE_PATH.\r
249 \r
250 @retval EFI_SUCCESS The child of I2C controller is not created.\r
251 @retval Others The child of I2C controller has been created or other errors happen.\r
252\r
253**/\r
254EFI_STATUS\r
255CheckRemainingDevicePath (\r
256 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
257 IN EFI_HANDLE Controller,\r
258 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath,\r
259 IN BOOLEAN RemainingHasControllerNode,\r
260 IN UINT32 RemainingControllerNumber\r
261 )\r
262{\r
263 EFI_STATUS Status;\r
264 EFI_DEVICE_PATH_PROTOCOL *SystemDevicePath;\r
265 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
266 UINTN EntryCount;\r
267 UINTN Index;\r
268 BOOLEAN SystemHasControllerNode;\r
269 UINT32 SystemControllerNumber; \r
270\r
271 SystemHasControllerNode = FALSE;\r
272 SystemControllerNumber = 0;\r
273 \r
274 Status = gBS->OpenProtocolInformation (\r
275 Controller,\r
276 &gEfiI2cHostProtocolGuid,\r
277 &OpenInfoBuffer,\r
278 &EntryCount\r
279 );\r
280 if (EFI_ERROR (Status)) {\r
281 return Status;\r
282 }\r
283 \r
284 for (Index = 0; Index < EntryCount; Index++) {\r
285 if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
286 Status = gBS->OpenProtocol (\r
287 OpenInfoBuffer[Index].ControllerHandle,\r
288 &gEfiDevicePathProtocolGuid,\r
289 (VOID **) &SystemDevicePath,\r
290 This->DriverBindingHandle,\r
291 Controller,\r
292 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
293 );\r
294 if (!EFI_ERROR (Status)) {\r
295 //\r
296 // Find vendor device path node and compare\r
297 // \r
298 while (!IsDevicePathEnd (SystemDevicePath)) {\r
299 if ((DevicePathType (SystemDevicePath) == HARDWARE_DEVICE_PATH) &&\r
300 (DevicePathSubType (SystemDevicePath) == HW_VENDOR_DP)) {\r
301 //\r
302 // Check if vendor device path is same between system device path and remaining device path\r
303 //\r
304 if (CompareMem (SystemDevicePath, RemainingDevicePath, sizeof (VENDOR_DEVICE_PATH)) == 0) {\r
305 //\r
306 // Get controller node appended after vendor node\r
307 //\r
308 SystemDevicePath = NextDevicePathNode (SystemDevicePath);\r
309 if ((DevicePathType (SystemDevicePath) == HARDWARE_DEVICE_PATH) &&\r
310 (DevicePathSubType (SystemDevicePath) == HW_CONTROLLER_DP)) {\r
311 SystemHasControllerNode = TRUE;\r
312 SystemControllerNumber = ((CONTROLLER_DEVICE_PATH *) SystemDevicePath)->ControllerNumber;\r
313 } else {\r
314 SystemHasControllerNode = FALSE;\r
315 SystemControllerNumber = 0;\r
316 }\r
317 if (((SystemHasControllerNode) && (!RemainingHasControllerNode) && (SystemControllerNumber == 0)) ||\r
318 ((!SystemHasControllerNode) && (RemainingHasControllerNode) && (RemainingControllerNumber == 0)) ||\r
319 ((SystemHasControllerNode) && (RemainingHasControllerNode) && (SystemControllerNumber == RemainingControllerNumber)) ||\r
320 ((!SystemHasControllerNode) && (!RemainingHasControllerNode))) {\r
321 DEBUG ((EFI_D_ERROR, "This I2C device has been already started.\n"));\r
322 Status = EFI_UNSUPPORTED;\r
323 break;\r
324 }\r
325 }\r
326 }\r
327 SystemDevicePath = NextDevicePathNode (SystemDevicePath);\r
328 }\r
329 if (EFI_ERROR (Status)) {\r
330 break;\r
331 }\r
332 }\r
333 }\r
334 }\r
335 FreePool (OpenInfoBuffer);\r
336 return Status;\r
337}\r
338\r
339/**\r
340 Tests to see if this driver supports a given controller. If a child device is provided,\r
341 it further tests to see if this driver supports creating a handle for the specified child device.\r
342\r
343 This function checks to see if the driver specified by This supports the device specified by\r
344 ControllerHandle. Drivers will typically use the device path attached to\r
345 ControllerHandle and/or the services from the bus I/O abstraction attached to\r
346 ControllerHandle to determine if the driver supports ControllerHandle. This function\r
347 may be called many times during platform initialization. In order to reduce boot times, the tests\r
348 performed by this function must be very small, and take as little time as possible to execute. This\r
349 function must not change the state of any hardware devices, and this function must be aware that the\r
350 device specified by ControllerHandle may already be managed by the same driver or a\r
351 different driver. This function must match its calls to AllocatePages() with FreePages(),\r
352 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
353 Since ControllerHandle may have been previously started by the same driver, if a protocol is\r
354 already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
355 to guarantee the state of ControllerHandle is not modified by this function.\r
356\r
357 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
358 @param[in] ControllerHandle The handle of the controller to test. This handle\r
359 must support a protocol interface that supplies\r
360 an I/O abstraction to the driver.\r
361 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
362 parameter is ignored by device drivers, and is optional for bus\r
363 drivers. For bus drivers, if this parameter is not NULL, then\r
364 the bus driver must determine if the bus controller specified\r
365 by ControllerHandle and the child controller specified\r
366 by RemainingDevicePath are both supported by this\r
367 bus driver.\r
368\r
369 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
370 RemainingDevicePath is supported by the driver specified by This.\r
371 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
372 RemainingDevicePath is already being managed by the driver\r
373 specified by This.\r
374 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
375 RemainingDevicePath is already being managed by a different\r
376 driver or an application that requires exclusive access.\r
377 Currently not implemented.\r
378 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
379 RemainingDevicePath is not supported by the driver specified by This.\r
380**/\r
381EFI_STATUS\r
382EFIAPI\r
383I2cBusDriverSupported (\r
384 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
385 IN EFI_HANDLE Controller,\r
386 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
387 )\r
388{\r
389 EFI_STATUS Status;\r
390 EFI_I2C_ENUMERATE_PROTOCOL *I2cEnumerate;\r
391 EFI_I2C_HOST_PROTOCOL *I2cHost;\r
392 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
393 EFI_DEVICE_PATH_PROTOCOL *DevPathNode;\r
394 BOOLEAN RemainingHasControllerNode;\r
395 UINT32 RemainingControllerNumber;\r
396\r
397 RemainingHasControllerNode = FALSE;\r
398 RemainingControllerNumber = 0;\r
399\r
400 //\r
401 // Determine if the I2c Enumerate Protocol is available\r
402 //\r
403 Status = gBS->OpenProtocol (\r
404 Controller,\r
405 &gEfiI2cEnumerateProtocolGuid,\r
406 (VOID **) &I2cEnumerate,\r
407 This->DriverBindingHandle,\r
408 Controller,\r
409 EFI_OPEN_PROTOCOL_BY_DRIVER\r
410 );\r
411 if ((EFI_ERROR (Status)) && (Status != EFI_ALREADY_STARTED)) {\r
412 return Status;\r
413 }\r
414\r
415 if (!EFI_ERROR (Status)) {\r
416 gBS->CloseProtocol (\r
417 Controller,\r
418 &gEfiI2cEnumerateProtocolGuid,\r
419 This->DriverBindingHandle,\r
420 Controller\r
421 );\r
422 }\r
423\r
424 Status = gBS->OpenProtocol (\r
425 Controller,\r
426 &gEfiDevicePathProtocolGuid,\r
427 (VOID **) &ParentDevicePath,\r
428 This->DriverBindingHandle,\r
429 Controller,\r
430 EFI_OPEN_PROTOCOL_BY_DRIVER\r
431 );\r
432\r
433 if ((EFI_ERROR (Status)) && (Status != EFI_ALREADY_STARTED)) {\r
434 return Status;\r
435 }\r
436\r
437 if (!EFI_ERROR (Status)) {\r
438 gBS->CloseProtocol (\r
439 Controller,\r
440 &gEfiDevicePathProtocolGuid,\r
441 This->DriverBindingHandle,\r
442 Controller\r
443 );\r
444 }\r
445\r
446 if ((RemainingDevicePath != NULL) && !IsDevicePathEnd (RemainingDevicePath)) {\r
447 //\r
448 // Check if the first node of RemainingDevicePath is a hardware vendor device path\r
449 //\r
450 if ((DevicePathType (RemainingDevicePath) != HARDWARE_DEVICE_PATH) ||\r
451 (DevicePathSubType (RemainingDevicePath) != HW_VENDOR_DP)) {\r
452 return EFI_UNSUPPORTED;\r
453 }\r
454 //\r
455 // Check if the second node of RemainingDevicePath is a controller node\r
456 //\r
457 DevPathNode = NextDevicePathNode (RemainingDevicePath);\r
458 if (!IsDevicePathEnd (DevPathNode)) {\r
459 if ((DevicePathType (DevPathNode) != HARDWARE_DEVICE_PATH) ||\r
460 (DevicePathSubType (DevPathNode) != HW_CONTROLLER_DP)) {\r
461 return EFI_UNSUPPORTED;\r
462 } else {\r
463 RemainingHasControllerNode = TRUE;\r
464 RemainingControllerNumber = ((CONTROLLER_DEVICE_PATH *) DevPathNode)->ControllerNumber;\r
465 }\r
466 }\r
467 }\r
468\r
469 //\r
470 // Determine if the I2C Host Protocol is available\r
471 //\r
472 Status = gBS->OpenProtocol ( \r
473 Controller,\r
474 &gEfiI2cHostProtocolGuid,\r
475 (VOID **) &I2cHost,\r
476 This->DriverBindingHandle,\r
477 Controller,\r
478 EFI_OPEN_PROTOCOL_BY_DRIVER\r
479 );\r
480\r
481 if (!EFI_ERROR (Status)) {\r
482 gBS->CloseProtocol (\r
483 Controller,\r
484 &gEfiI2cHostProtocolGuid,\r
485 This->DriverBindingHandle,\r
486 Controller\r
487 );\r
488 }\r
489\r
490\r
491 if (Status == EFI_ALREADY_STARTED) {\r
492 if ((RemainingDevicePath == NULL) || \r
493 ((RemainingDevicePath != NULL) && IsDevicePathEnd (RemainingDevicePath))) {\r
494 //\r
495 // If RemainingDevicePath is NULL or is the End of Device Path Node, return EFI_SUCCESS.\r
496 //\r
497 Status = EFI_SUCCESS;\r
498 } else {\r
499 //\r
500 // Test if the child with the RemainingDevicePath has already been created.\r
501 // \r
502 Status = CheckRemainingDevicePath (\r
503 This,\r
504 Controller,\r
505 RemainingDevicePath,\r
506 RemainingHasControllerNode,\r
507 RemainingControllerNumber\r
508 );\r
509 }\r
510 }\r
511\r
512 return Status;\r
513}\r
514\r
515/**\r
516 Starts a device controller or a bus controller.\r
517\r
518 The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
519 As a result, much of the error checking on the parameters to Start() has been moved into this\r
520 common boot service. It is legal to call Start() from other locations,\r
521 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
522 1. ControllerHandle must be a valid EFI_HANDLE.\r
523 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
524 EFI_DEVICE_PATH_PROTOCOL.\r
525 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
526 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
527\r
528 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
529 @param[in] ControllerHandle The handle of the controller to start. This handle\r
530 must support a protocol interface that supplies\r
531 an I/O abstraction to the driver.\r
532 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
533 parameter is ignored by device drivers, and is optional for bus\r
534 drivers. For a bus driver, if this parameter is NULL, then handles\r
535 for all the children of Controller are created by this driver.\r
536 If this parameter is not NULL and the first Device Path Node is\r
537 not the End of Device Path Node, then only the handle for the\r
538 child device specified by the first Device Path Node of\r
539 RemainingDevicePath is created by this driver.\r
540 If the first Device Path Node of RemainingDevicePath is\r
541 the End of Device Path Node, no child handle is created by this\r
542 driver.\r
543\r
544 @retval EFI_SUCCESS The device was started.\r
545 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
546 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
547 @retval Others The driver failded to start the device.\r
548\r
549**/\r
550EFI_STATUS\r
551EFIAPI\r
552I2cBusDriverStart (\r
553 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
554 IN EFI_HANDLE Controller,\r
555 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
556 )\r
557{\r
558 EFI_I2C_ENUMERATE_PROTOCOL *I2cEnumerate;\r
559 EFI_I2C_HOST_PROTOCOL *I2cHost;\r
560 I2C_BUS_CONTEXT *I2cBusContext;\r
561 EFI_STATUS Status;\r
562 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
563\r
564 I2cBusContext = NULL;\r
565 ParentDevicePath = NULL;\r
566 I2cEnumerate = NULL;\r
567 I2cHost = NULL;\r
568\r
569 //\r
570 // Determine if the I2C controller is available\r
571 //\r
572 Status = gBS->OpenProtocol (\r
573 Controller,\r
574 &gEfiI2cHostProtocolGuid,\r
575 (VOID**)&I2cHost,\r
576 This->DriverBindingHandle,\r
577 Controller,\r
578 EFI_OPEN_PROTOCOL_BY_DRIVER\r
579 );\r
580 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
581 DEBUG ((EFI_D_ERROR, "I2cBus: open I2C host error, Status = %r\n", Status));\r
582 return Status;\r
583 }\r
584\r
585 if (Status == EFI_ALREADY_STARTED) {\r
586 Status = gBS->OpenProtocol (\r
587 Controller,\r
588 &gEfiCallerIdGuid,\r
589 (VOID **) &I2cBusContext,\r
590 This->DriverBindingHandle,\r
591 Controller,\r
592 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
593 );\r
594 if (EFI_ERROR (Status)) {\r
595 DEBUG ((EFI_D_ERROR, "I2cBus: open private protocol error, Status = %r.\n", Status));\r
596 return Status;\r
597 }\r
598 }\r
599\r
600 //\r
601 // Get the I2C bus enumeration API\r
602 //\r
603 Status = gBS->OpenProtocol (\r
604 Controller,\r
605 &gEfiI2cEnumerateProtocolGuid,\r
606 (VOID**)&I2cEnumerate,\r
607 This->DriverBindingHandle,\r
608 Controller,\r
609 EFI_OPEN_PROTOCOL_BY_DRIVER\r
610 );\r
611 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
612 DEBUG ((EFI_D_ERROR, "I2cBus: open I2C enumerate error, Status = %r\n", Status));\r
613 goto Error;\r
614 }\r
615\r
616 Status = gBS->OpenProtocol (\r
617 Controller,\r
618 &gEfiDevicePathProtocolGuid,\r
619 (VOID **) &ParentDevicePath,\r
620 This->DriverBindingHandle,\r
621 Controller,\r
622 EFI_OPEN_PROTOCOL_BY_DRIVER\r
623 );\r
624 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
625 DEBUG ((EFI_D_ERROR, "I2cBus: open device path error, Status = %r\n", Status));\r
626 goto Error;\r
627 }\r
628\r
629 if ((RemainingDevicePath != NULL) && IsDevicePathEnd (RemainingDevicePath)) {\r
630 //\r
631 // If RemainingDevicePath is the End of Device Path Node,\r
632 // don't create any child device and return EFI_SUCESS\r
633 //\r
634 return EFI_SUCCESS;\r
635 }\r
636\r
637 //\r
638 // Allocate the buffer for I2C_BUS_CONTEXT if it is not allocated before.\r
639 //\r
640 if (I2cBusContext == NULL) {\r
641 //\r
642 // Allocate the I2C context structure for the current I2C controller\r
643 //\r
644 I2cBusContext = AllocateZeroPool (sizeof (I2C_BUS_CONTEXT));\r
645 if (I2cBusContext == NULL) {\r
646 DEBUG ((EFI_D_ERROR, "I2cBus: there is no enough memory to allocate.\n"));\r
647 Status = EFI_OUT_OF_RESOURCES;\r
648 goto Error;\r
649 }\r
650 \r
651 /*\r
652 +----------------+\r
653 .->| I2C_BUS_CONTEXT|<----- This file Protocol (gEfiCallerIdGuid) installed on I2C Controller handle\r
654 | +----------------+\r
655 |\r
656 | +----------------------------+\r
657 | | I2C_DEVICE_CONTEXT |\r
658 `--| |\r
659 | |\r
660 | I2C IO Protocol Structure | <----- I2C IO Protocol\r
661 | |\r
662 +----------------------------+\r
663 \r
664 */\r
665 I2cBusContext->I2cHost = I2cHost;\r
666 I2cBusContext->I2cEnumerate = I2cEnumerate;\r
667 //\r
668 // Parent controller used to create children\r
669 //\r
670 I2cBusContext->Controller = Controller;\r
671 //\r
672 // Parent controller device path used to create children device path\r
673 //\r
674 I2cBusContext->ParentDevicePath = ParentDevicePath;\r
675 \r
676 I2cBusContext->DriverBindingHandle = This->DriverBindingHandle;\r
677 \r
678 Status = gBS->InstallMultipleProtocolInterfaces (\r
679 &Controller,\r
680 &gEfiCallerIdGuid,\r
681 I2cBusContext,\r
682 NULL\r
683 );\r
684 if (EFI_ERROR (Status)) {\r
685 DEBUG ((EFI_D_ERROR, "I2cBus: install private protocol error, Status = %r.\n", Status));\r
686 goto Error;\r
687 }\r
688 }\r
689\r
690 //\r
691 // Start the driver\r
692 //\r
693 Status = RegisterI2cDevice (I2cBusContext, Controller, RemainingDevicePath);\r
694\r
695 return Status;\r
696\r
697Error:\r
698 if (EFI_ERROR (Status)) {\r
699 DEBUG ((EFI_D_ERROR, "I2cBus: Start() function failed, Status = %r\n", Status));\r
700 if (ParentDevicePath != NULL) {\r
701 gBS->CloseProtocol ( \r
702 Controller,\r
703 &gEfiDevicePathProtocolGuid,\r
704 This->DriverBindingHandle,\r
705 Controller\r
706 );\r
707 }\r
708\r
709 if (I2cHost != NULL) {\r
710 gBS->CloseProtocol (\r
711 Controller,\r
712 &gEfiI2cHostProtocolGuid,\r
713 This->DriverBindingHandle,\r
714 Controller\r
715 );\r
716 }\r
717\r
718 if (I2cEnumerate != NULL) {\r
719 gBS->CloseProtocol ( \r
720 Controller,\r
721 &gEfiI2cEnumerateProtocolGuid,\r
722 This->DriverBindingHandle,\r
723 Controller\r
724 );\r
725 }\r
726 \r
727 if (I2cBusContext != NULL) {\r
728 Status = gBS->UninstallMultipleProtocolInterfaces (\r
729 &Controller,\r
730 gEfiCallerIdGuid,\r
731 I2cBusContext,\r
732 NULL\r
733 );\r
734 FreePool (I2cBusContext);\r
735 }\r
736 }\r
737\r
738 //\r
739 // Return the operation status.\r
740 //\r
741 return Status;\r
742}\r
743\r
744\r
745/**\r
746 Stops a device controller or a bus controller.\r
747\r
748 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
749 As a result, much of the error checking on the parameters to Stop() has been moved\r
750 into this common boot service. It is legal to call Stop() from other locations,\r
751 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
752 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
753 same driver's Start() function.\r
754 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
755 EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
756 Start() function, and the Start() function must have called OpenProtocol() on\r
757 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
758\r
759 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
760 @param[in] ControllerHandle A handle to the device being stopped. The handle must\r
761 support a bus specific I/O protocol for the driver\r
762 to use to stop the device.\r
763 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
764 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
765 if NumberOfChildren is 0.\r
766\r
767 @retval EFI_SUCCESS The device was stopped.\r
768 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
769\r
770**/\r
771EFI_STATUS\r
772EFIAPI\r
773I2cBusDriverStop (\r
774 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
775 IN EFI_HANDLE Controller,\r
776 IN UINTN NumberOfChildren,\r
777 IN EFI_HANDLE *ChildHandleBuffer\r
778 )\r
779{\r
780 I2C_BUS_CONTEXT *I2cBusContext;\r
781 EFI_STATUS Status;\r
782 BOOLEAN AllChildrenStopped;\r
783 UINTN Index;\r
784\r
785 if (NumberOfChildren == 0) {\r
786 gBS->CloseProtocol ( \r
787 Controller,\r
788 &gEfiDevicePathProtocolGuid,\r
789 This->DriverBindingHandle,\r
790 Controller\r
791 );\r
792\r
793 gBS->CloseProtocol (\r
794 Controller,\r
795 &gEfiI2cHostProtocolGuid,\r
796 This->DriverBindingHandle,\r
797 Controller\r
798 );\r
799\r
800 gBS->CloseProtocol (\r
801 Controller,\r
802 &gEfiI2cEnumerateProtocolGuid,\r
803 This->DriverBindingHandle,\r
804 Controller\r
805 );\r
806\r
807 Status = gBS->OpenProtocol (\r
808 Controller,\r
809 &gEfiCallerIdGuid,\r
810 (VOID **) &I2cBusContext,\r
811 This->DriverBindingHandle,\r
812 Controller,\r
813 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
814 );\r
815 if (!EFI_ERROR (Status)) {\r
816 gBS->UninstallMultipleProtocolInterfaces (\r
817 Controller,\r
818 &gEfiCallerIdGuid,\r
819 I2cBusContext,\r
820 NULL\r
821 );\r
822 //\r
823 // No more child now, free bus context data.\r
824 //\r
825 FreePool (I2cBusContext);\r
826 }\r
827 return Status;\r
828 }\r
829\r
830 AllChildrenStopped = TRUE;\r
831\r
832 for (Index = 0; Index < NumberOfChildren; Index++) {\r
833\r
834 Status = UnRegisterI2cDevice (This, Controller, ChildHandleBuffer[Index]);\r
835 if (EFI_ERROR (Status)) {\r
836 AllChildrenStopped = FALSE;\r
837 }\r
838 }\r
839\r
840 if (!AllChildrenStopped) {\r
841 return EFI_DEVICE_ERROR;\r
842 }\r
843 return EFI_SUCCESS;\r
844}\r
845\r
846/**\r
847 Enumerate the I2C bus\r
848\r
849 This routine walks the platform specific data describing the\r
850 I2C bus to create the I2C devices where driver GUIDs were\r
851 specified.\r
852\r
853 @param[in] I2cBusContext Address of an I2C_BUS_CONTEXT structure\r
854 @param[in] Controller Handle to the controller\r
855 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.\r
856\r
857 @retval EFI_SUCCESS The bus is successfully configured\r
858\r
859**/\r
860EFI_STATUS\r
861RegisterI2cDevice (\r
862 IN I2C_BUS_CONTEXT *I2cBusContext,\r
863 IN EFI_HANDLE Controller,\r
864 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
865 )\r
866{\r
867 I2C_DEVICE_CONTEXT *I2cDeviceContext;\r
868 EFI_STATUS Status;\r
869 CONST EFI_I2C_DEVICE *Device;\r
870 CONST EFI_I2C_DEVICE *TempDevice;\r
871 UINT32 RemainingPathDeviceIndex;\r
872 EFI_DEVICE_PATH_PROTOCOL *DevPathNode;\r
873 BOOLEAN BuildControllerNode;\r
874 UINTN Count;\r
875\r
876 Status = EFI_SUCCESS;\r
877 BuildControllerNode = TRUE;\r
878\r
879 //\r
880 // Default DeviceIndex\r
881 //\r
882 RemainingPathDeviceIndex = 0;\r
883 \r
884 //\r
885 // Determine the controller number in Controller Node Device Path when RemainingDevicePath is not NULL.\r
886 //\r
887 if (RemainingDevicePath != NULL) {\r
888 //\r
889 // Check if there is a controller node appended after vendor node\r
890 // \r
891 DevPathNode = NextDevicePathNode (RemainingDevicePath);\r
892 if ((DevicePathType (DevPathNode) == HARDWARE_DEVICE_PATH) &&\r
893 (DevicePathSubType(DevPathNode) == HW_CONTROLLER_DP)) {\r
894 //\r
895 // RemainingDevicePath != NULL and RemainingDevicePath contains Controller Node,\r
896 // add Controller Node to Device Path on child handle.\r
897 //\r
898 RemainingPathDeviceIndex = ((CONTROLLER_DEVICE_PATH *) DevPathNode)->ControllerNumber;\r
899 } else {\r
900 //\r
901 // RemainingDevicePath != NULL and RemainingDevicePath does not contain Controller Node,\r
902 // do not add controller node to Device Path on child handle.\r
903 //\r
904 BuildControllerNode = FALSE;\r
905 }\r
906 }\r
907\r
908 //\r
909 // Walk the list of I2C devices on this bus\r
910 //\r
911 Device = NULL;\r
912 while (TRUE) {\r
913 //\r
914 // Get the next I2C device\r
915 //\r
916 Status = I2cBusContext->I2cEnumerate->Enumerate (I2cBusContext->I2cEnumerate, &Device);\r
917 if (EFI_ERROR (Status) || Device == NULL) {\r
918 if (RemainingDevicePath != NULL) {\r
919 Status = EFI_NOT_FOUND;\r
920 } else {\r
921 Status = EFI_SUCCESS;\r
922 }\r
923 break;\r
924 }\r
925\r
926 //\r
927 // Determine if the device info is valid\r
928 //\r
929 if ((Device->DeviceGuid == NULL) || (Device->SlaveAddressCount == 0) || (Device->SlaveAddressArray == NULL)) {\r
930 DEBUG ((EFI_D_ERROR, "Invalid EFI_I2C_DEVICE reported by I2c Enumerate protocol.\n"));\r
931 continue;\r
932 }\r
933\r
934 if (RemainingDevicePath == NULL) {\r
935 if (Device->DeviceIndex == 0) {\r
936 //\r
937 // Determine if the controller node is necessary when controller number is zero in I2C device\r
938 //\r
939 TempDevice = NULL;\r
940 Count = 0;\r
941 while (TRUE) {\r
942 //\r
943 // Get the next I2C device\r
944 //\r
945 Status = I2cBusContext->I2cEnumerate->Enumerate (I2cBusContext->I2cEnumerate, &TempDevice);\r
946 if (EFI_ERROR (Status) || TempDevice == NULL) {\r
947 Status = EFI_SUCCESS;\r
948 break;\r
949 }\r
950 if (CompareGuid (Device->DeviceGuid, TempDevice->DeviceGuid)) {\r
951 Count++;\r
952 }\r
953 }\r
954 if (Count == 1) {\r
955 //\r
956 // RemainingDevicePath == NULL and only DeviceIndex 0 is present on the I2C bus,\r
957 // do not add Controller Node to Device Path on child handle.\r
958 //\r
959 BuildControllerNode = FALSE;\r
960 }\r
961 }\r
962 } else {\r
963 //\r
964 // Find I2C device reported in Remaining Device Path\r
965 //\r
966 if ((!CompareGuid (&((VENDOR_DEVICE_PATH *)RemainingDevicePath)->Guid, Device->DeviceGuid)) ||\r
967 (RemainingPathDeviceIndex != Device->DeviceIndex)) {\r
968 continue; \r
969 }\r
970 }\r
971\r
972 //\r
973 // Build the device context for current I2C device.\r
974 //\r
975 I2cDeviceContext = NULL;\r
976 I2cDeviceContext = AllocateCopyPool (sizeof (I2C_DEVICE_CONTEXT), &gI2cDeviceContextTemplate);\r
977 ASSERT (I2cDeviceContext != NULL);\r
978 if (I2cDeviceContext == NULL) {\r
979 continue;\r
980 }\r
981\r
982 //\r
983 // Initialize the specific device context\r
984 //\r
985 I2cDeviceContext->I2cBusContext = I2cBusContext;\r
986 I2cDeviceContext->I2cDevice = Device;\r
987 I2cDeviceContext->I2cIo.DeviceGuid = Device->DeviceGuid;\r
988 I2cDeviceContext->I2cIo.DeviceIndex = Device->DeviceIndex;\r
989 I2cDeviceContext->I2cIo.HardwareRevision = Device->HardwareRevision;\r
990 I2cDeviceContext->I2cIo.I2cControllerCapabilities = I2cBusContext->I2cHost->I2cControllerCapabilities;\r
991\r
992 //\r
993 // Build the device path\r
994 //\r
995 Status = I2cBusDevicePathAppend (I2cDeviceContext, BuildControllerNode);\r
996 ASSERT_EFI_ERROR (Status);\r
997 if (EFI_ERROR (Status)) {\r
998 continue;\r
999 }\r
1000\r
1001 //\r
1002 // Install the protocol\r
1003 //\r
1004 Status = gBS->InstallMultipleProtocolInterfaces (\r
1005 &I2cDeviceContext->Handle,\r
1006 &gEfiI2cIoProtocolGuid,\r
1007 &I2cDeviceContext->I2cIo,\r
1008 &gEfiDevicePathProtocolGuid,\r
1009 I2cDeviceContext->DevicePath,\r
1010 NULL );\r
1011 if (EFI_ERROR (Status)) {\r
1012 //\r
1013 // Free resources for this I2C device\r
1014 //\r
1015 ReleaseI2cDeviceContext (I2cDeviceContext);\r
1016 continue;\r
1017 }\r
1018 \r
1019 //\r
1020 // Create the child handle\r
1021 //\r
1022 Status = gBS->OpenProtocol (\r
1023 Controller,\r
1024 &gEfiI2cHostProtocolGuid,\r
1025 (VOID **) &I2cBusContext->I2cHost,\r
1026 I2cBusContext->DriverBindingHandle,\r
1027 I2cDeviceContext->Handle,\r
1028 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
1029 );\r
1030 if (EFI_ERROR (Status)) {\r
1031 Status = gBS->UninstallMultipleProtocolInterfaces (\r
1032 I2cDeviceContext->Handle,\r
1033 &gEfiDevicePathProtocolGuid,\r
1034 I2cDeviceContext->DevicePath,\r
1035 &gEfiI2cIoProtocolGuid,\r
1036 &I2cDeviceContext->I2cIo,\r
1037 NULL\r
1038 );\r
1039 //\r
1040 // Free resources for this I2C device\r
1041 //\r
1042 ReleaseI2cDeviceContext (I2cDeviceContext);\r
1043 continue; \r
1044 }\r
1045\r
1046 if (RemainingDevicePath != NULL) {\r
1047 //\r
1048 // Child has been created successfully\r
1049 //\r
1050 break;\r
1051 }\r
1052 }\r
1053\r
1054 return Status;\r
1055}\r
1056\r
1057\r
1058/**\r
1059 Queue an I2C transaction for execution on the I2C device.\r
1060\r
1061 This routine must be called at or below TPL_NOTIFY. For synchronous\r
1062 requests this routine must be called at or below TPL_CALLBACK.\r
1063\r
1064 This routine queues an I2C transaction to the I2C controller for\r
1065 execution on the I2C bus.\r
1066\r
1067 When Event is NULL, QueueRequest() operates synchronously and returns\r
1068 the I2C completion status as its return value.\r
1069\r
1070 When Event is not NULL, QueueRequest() synchronously returns EFI_SUCCESS\r
1071 indicating that the asynchronous I2C transaction was queued. The values\r
1072 above are returned in the buffer pointed to by I2cStatus upon the\r
1073 completion of the I2C transaction when I2cStatus is not NULL.\r
1074\r
1075 The upper layer driver writer provides the following to the platform\r
1076 vendor:\r
1077 \r
1078 1. Vendor specific GUID for the I2C part\r
1079 2. Guidance on proper construction of the slave address array when the\r
1080 I2C device uses more than one slave address. The I2C bus protocol\r
1081 uses the SlaveAddressIndex to perform relative to physical address\r
1082 translation to access the blocks of hardware within the I2C device.\r
1083\r
1084 @param[in] This Pointer to an EFI_I2C_IO_PROTOCOL structure.\r
1085 @param[in] SlaveAddressIndex Index value into an array of slave addresses\r
1086 for the I2C device. The values in the array\r
1087 are specified by the board designer, with the\r
1088 third party I2C device driver writer providing\r
1089 the slave address order.\r
1090\r
1091 For devices that have a single slave address,\r
1092 this value must be zero. If the I2C device\r
1093 uses more than one slave address then the\r
1094 third party (upper level) I2C driver writer\r
1095 needs to specify the order of entries in the\r
1096 slave address array.\r
1097\r
1098 \ref ThirdPartyI2cDrivers "Third Party I2C\r
1099 Drivers" section in I2cMaster.h.\r
1100 @param[in] Event Event to signal for asynchronous transactions,\r
1101 NULL for synchronous transactions\r
1102 @param[in] RequestPacket Pointer to an EFI_I2C_REQUEST_PACKET structure\r
1103 describing the I2C transaction\r
1104 @param[out] I2cStatus Optional buffer to receive the I2C transaction\r
1105 completion status\r
1106\r
1107 @retval EFI_SUCCESS The asynchronous transaction was successfully\r
1108 queued when Event is not NULL.\r
1109 @retval EFI_SUCCESS The transaction completed successfully when\r
1110 Event is NULL.\r
43e543bc
EL
1111 @retval EFI_BAD_BUFFER_SIZE The RequestPacket->LengthInBytes value is too\r
1112 large.\r
1113 @retval EFI_DEVICE_ERROR There was an I2C error (NACK) during the\r
1114 transaction.\r
1115 @retval EFI_INVALID_PARAMETER RequestPacket is NULL\r
43e543bc
EL
1116 @retval EFI_NO_MAPPING The EFI_I2C_HOST_PROTOCOL could not set the\r
1117 bus configuration required to access this I2C\r
1118 device.\r
1119 @retval EFI_NO_RESPONSE The I2C device is not responding to the slave\r
1120 address selected by SlaveAddressIndex.\r
1121 EFI_DEVICE_ERROR will be returned if the\r
1122 controller cannot distinguish when the NACK\r
1123 occurred.\r
1124 @retval EFI_OUT_OF_RESOURCES Insufficient memory for I2C transaction\r
1125 @retval EFI_UNSUPPORTED The controller does not support the requested\r
1126 transaction.\r
1127\r
1128**/\r
1129EFI_STATUS\r
1130EFIAPI\r
1131I2cBusQueueRequest (\r
1132 IN CONST EFI_I2C_IO_PROTOCOL *This,\r
1133 IN UINTN SlaveAddressIndex,\r
1134 IN EFI_EVENT Event OPTIONAL,\r
1135 IN EFI_I2C_REQUEST_PACKET *RequestPacket,\r
1136 OUT EFI_STATUS *I2cStatus OPTIONAL\r
1137 )\r
1138{\r
1139 CONST EFI_I2C_DEVICE *I2cDevice;\r
1140 I2C_BUS_CONTEXT *I2cBusContext;\r
1141 CONST EFI_I2C_HOST_PROTOCOL *I2cHost;\r
1142 I2C_DEVICE_CONTEXT *I2cDeviceContext;\r
1143 EFI_STATUS Status;\r
1144\r
1145 if (RequestPacket == NULL) {\r
1146 return EFI_INVALID_PARAMETER;\r
1147 }\r
1148\r
1149 //\r
1150 // Validate the I2C slave index\r
1151 //\r
1152 I2cDeviceContext = I2C_DEVICE_CONTEXT_FROM_PROTOCOL (This);\r
1153 I2cDevice = I2cDeviceContext->I2cDevice;\r
1154 if ( SlaveAddressIndex >= I2cDevice->SlaveAddressCount ) {\r
1155 return EFI_INVALID_PARAMETER;\r
1156 }\r
1157\r
1158 //\r
1159 // Locate the I2c Host Protocol to queue request\r
1160 //\r
1161 I2cBusContext = I2cDeviceContext->I2cBusContext;\r
1162 I2cHost = I2cBusContext->I2cHost;\r
1163\r
1164 //\r
1165 // Start the I2C operation\r
1166 //\r
1167 Status = I2cHost->QueueRequest (\r
1168 I2cHost,\r
1169 I2cDevice->I2cBusConfiguration,\r
1170 I2cDevice->SlaveAddressArray [SlaveAddressIndex],\r
1171 Event,\r
1172 RequestPacket,\r
1173 I2cStatus\r
1174 );\r
1175\r
1176 return Status;\r
1177}\r
1178\r
1179/**\r
1180 Release all the resources allocated for the I2C device.\r
1181\r
1182 This function releases all the resources allocated for the I2C device.\r
1183\r
1184 @param I2cDeviceContext The I2C child device involved for the operation.\r
1185\r
1186**/\r
1187VOID\r
1188ReleaseI2cDeviceContext (\r
1189 IN I2C_DEVICE_CONTEXT *I2cDeviceContext\r
1190 )\r
1191{\r
1192 if (I2cDeviceContext == NULL) {\r
1193 return;\r
1194 }\r
1195 \r
1196 if (I2cDeviceContext->DevicePath != NULL) {\r
1197 FreePool (I2cDeviceContext->DevicePath);\r
1198 }\r
1199\r
1200 FreePool (I2cDeviceContext);\r
1201}\r
1202\r
1203/**\r
1204 Unregister an I2C device.\r
1205\r
1206 This function removes the protocols installed on the controller handle and\r
1207 frees the resources allocated for the I2C device.\r
1208\r
1209 @param This The pointer to EFI_DRIVER_BINDING_PROTOCOL instance.\r
1210 @param Controller The controller handle of the I2C device.\r
1211 @param Handle The child handle.\r
1212\r
1213 @retval EFI_SUCCESS The I2C device is successfully unregistered.\r
1214 @return Others Some error occurs when unregistering the I2C device.\r
1215\r
1216**/\r
1217EFI_STATUS\r
1218UnRegisterI2cDevice (\r
1219 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
1220 IN EFI_HANDLE Controller,\r
1221 IN EFI_HANDLE Handle\r
1222 )\r
1223{\r
1224 EFI_STATUS Status;\r
1225 I2C_DEVICE_CONTEXT *I2cDeviceContext;\r
1226 EFI_I2C_IO_PROTOCOL *I2cIo;\r
1227 EFI_I2C_HOST_PROTOCOL *I2cHost;\r
1228\r
1229 I2cIo = NULL;\r
1230\r
1231 Status = gBS->OpenProtocol (\r
1232 Handle,\r
1233 &gEfiI2cIoProtocolGuid,\r
1234 (VOID **) &I2cIo,\r
1235 This->DriverBindingHandle,\r
1236 Controller,\r
1237 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1238 );\r
1239 if (EFI_ERROR (Status)) {\r
1240 return Status;\r
1241 }\r
1242\r
1243 //\r
1244 // Get I2c device context data.\r
1245 //\r
1246 I2cDeviceContext = I2C_DEVICE_CONTEXT_FROM_PROTOCOL (I2cIo);\r
1247\r
1248 //\r
1249 // Close the child handle\r
1250 //\r
1251 gBS->CloseProtocol (\r
1252 Controller,\r
1253 &gEfiI2cHostProtocolGuid,\r
1254 This->DriverBindingHandle,\r
1255 Handle\r
1256 );\r
1257\r
1258 //\r
1259 // The I2C Bus driver installs the I2C Io and Device Path Protocol in the DriverBindingStart().\r
1260 // Here should uninstall them.\r
1261 //\r
1262 Status = gBS->UninstallMultipleProtocolInterfaces (\r
1263 Handle,\r
1264 &gEfiDevicePathProtocolGuid,\r
1265 I2cDeviceContext->DevicePath,\r
1266 &gEfiI2cIoProtocolGuid,\r
1267 &I2cDeviceContext->I2cIo,\r
1268 NULL\r
1269 );\r
1270\r
1271 if (EFI_ERROR (Status)) {\r
1272 //\r
1273 // Keep parent and child relationship\r
1274 //\r
1275 gBS->OpenProtocol (\r
1276 Controller,\r
1277 &gEfiI2cHostProtocolGuid,\r
1278 (VOID **) &I2cHost,\r
1279 This->DriverBindingHandle,\r
1280 Handle,\r
1281 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
1282 );\r
1283 return Status;\r
1284 }\r
1285 \r
1286 //\r
1287 // Free resources for this I2C device\r
1288 //\r
1289 ReleaseI2cDeviceContext (I2cDeviceContext);\r
1290 \r
1291 return EFI_SUCCESS;\r
1292}\r
1293\r
1294/**\r
1295 Create a path for the I2C device\r
1296\r
1297 Append the I2C slave path to the I2C master controller path.\r
1298\r
1299 @param[in] I2cDeviceContext Address of an I2C_DEVICE_CONTEXT structure.\r
1300 @param[in] BuildControllerNode Flag to build controller node in device path.\r
1301\r
1302 @retval EFI_SUCCESS The I2C device path is built successfully.\r
1303 @return Others It is failed to built device path.\r
1304\r
1305**/\r
1306EFI_STATUS\r
1307I2cBusDevicePathAppend (\r
1308 IN I2C_DEVICE_CONTEXT *I2cDeviceContext,\r
1309 IN BOOLEAN BuildControllerNode\r
1310 )\r
1311{\r
1312 EFI_DEVICE_PATH_PROTOCOL *PreviousDevicePath;\r
1313 \r
1314 PreviousDevicePath = NULL;\r
1315\r
1316 //\r
1317 // Build vendor device path\r
1318 // \r
1319 CopyMem (&gVendorDevicePathTemplate.Guid, I2cDeviceContext->I2cDevice->DeviceGuid, sizeof (EFI_GUID));\r
1320 I2cDeviceContext->DevicePath = AppendDevicePathNode (\r
1321 I2cDeviceContext->I2cBusContext->ParentDevicePath,\r
1322 (EFI_DEVICE_PATH_PROTOCOL *) &gVendorDevicePathTemplate\r
1323 );\r
1324 ASSERT (I2cDeviceContext->DevicePath != NULL);\r
1325 if (I2cDeviceContext->DevicePath == NULL) {\r
1326 return EFI_OUT_OF_RESOURCES;\r
1327 }\r
1328 \r
1329 if ((BuildControllerNode) && (I2cDeviceContext->DevicePath != NULL)) {\r
1330 //\r
1331 // Build the final I2C device path with controller node\r
1332 //\r
1333 PreviousDevicePath = I2cDeviceContext->DevicePath;\r
1334 gControllerDevicePathTemplate.ControllerNumber = I2cDeviceContext->I2cDevice->DeviceIndex; \r
1335 I2cDeviceContext->DevicePath = AppendDevicePathNode (\r
1336 I2cDeviceContext->DevicePath,\r
1337 (EFI_DEVICE_PATH_PROTOCOL *) &gControllerDevicePathTemplate\r
1338 );\r
1339 gBS->FreePool (PreviousDevicePath);\r
1340 ASSERT (I2cDeviceContext->DevicePath != NULL);\r
1341 if (I2cDeviceContext->DevicePath == NULL) {\r
1342 return EFI_OUT_OF_RESOURCES;\r
1343 }\r
1344 }\r
1345\r
1346 return EFI_SUCCESS;\r
1347}\r
1348\r
1349/**\r
1350 The user entry point for the I2C bus module. The user code starts with\r
1351 this function.\r
1352\r
1353 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
1354 @param[in] SystemTable A pointer to the EFI System Table.\r
1355\r
1356 @retval EFI_SUCCESS The entry point is executed successfully.\r
1357 @retval other Some error occurs when executing this entry point.\r
1358\r
1359**/\r
1360EFI_STATUS\r
1361EFIAPI\r
1362InitializeI2cBus(\r
1363 IN EFI_HANDLE ImageHandle,\r
1364 IN EFI_SYSTEM_TABLE *SystemTable\r
1365 )\r
1366{\r
1367 EFI_STATUS Status;\r
1368\r
1369 //\r
1370 // Install driver model protocol(s).\r
1371 //\r
1372 Status = EfiLibInstallDriverBindingComponentName2 (\r
1373 ImageHandle,\r
1374 SystemTable,\r
1375 &gI2cBusDriverBinding,\r
1376 NULL,\r
1377 &gI2cBusComponentName,\r
1378 &gI2cBusComponentName2\r
1379 );\r
1380 ASSERT_EFI_ERROR (Status);\r
1381\r
1382 \r
1383 return Status;\r
1384}\r
1385\r
1386/**\r
1387 This is the unload handle for I2C bus module.\r
1388\r
1389 Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r
1390 Uninstall all the protocols installed in the driver entry point.\r
1391\r
1392 @param[in] ImageHandle The drivers' driver image.\r
1393\r
1394 @retval EFI_SUCCESS The image is unloaded.\r
1395 @retval Others Failed to unload the image.\r
1396\r
1397**/\r
1398EFI_STATUS\r
1399EFIAPI\r
1400I2cBusUnload (\r
1401 IN EFI_HANDLE ImageHandle\r
1402 )\r
1403{\r
1404 EFI_STATUS Status;\r
1405 EFI_HANDLE *DeviceHandleBuffer;\r
1406 UINTN DeviceHandleCount;\r
1407 UINTN Index;\r
6f497f8f
EL
1408 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;\r
1409 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
43e543bc
EL
1410\r
1411 //\r
1412 // Get the list of all I2C Controller handles in the handle database.\r
1413 // If there is an error getting the list, then the unload\r
1414 // operation fails.\r
1415 //\r
1416 Status = gBS->LocateHandleBuffer (\r
1417 ByProtocol,\r
1418 &gEfiI2cHostProtocolGuid,\r
1419 NULL,\r
1420 &DeviceHandleCount,\r
1421 &DeviceHandleBuffer\r
1422 );\r
1423\r
2fa77862
EL
1424 if (!EFI_ERROR (Status)) {\r
1425 //\r
1426 // Disconnect the driver specified by Driver BindingHandle from all\r
1427 // the devices in the handle database.\r
1428 //\r
1429 for (Index = 0; Index < DeviceHandleCount; Index++) {\r
1430 Status = gBS->DisconnectController (\r
1431 DeviceHandleBuffer[Index],\r
1432 gI2cBusDriverBinding.DriverBindingHandle,\r
1433 NULL\r
1434 );\r
1435 if (EFI_ERROR (Status)) {\r
1436 goto Done;\r
1437 }\r
43e543bc
EL
1438 }\r
1439 }\r
1440\r
1441 //\r
1442 // Uninstall all the protocols installed in the driver entry point\r
1443 //\r
1444 Status = gBS->UninstallMultipleProtocolInterfaces (\r
1445 gI2cBusDriverBinding.DriverBindingHandle,\r
1446 &gEfiDriverBindingProtocolGuid,\r
1447 &gI2cBusDriverBinding,\r
43e543bc
EL
1448 NULL\r
1449 );\r
1450 ASSERT_EFI_ERROR (Status);\r
1451\r
6f497f8f
EL
1452 //\r
1453 // Note we have to one by one uninstall the following protocols.\r
1454 // It's because some of them are optionally installed based on\r
1455 // the following PCD settings.\r
1456 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnosticsDisable\r
1457 // gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable\r
1458 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnostics2Disable\r
1459 // gEfiMdePkgTokenSpaceGuid.PcdComponentName2Disable\r
1460 //\r
1461 Status = gBS->HandleProtocol (\r
1462 gI2cBusDriverBinding.DriverBindingHandle,\r
1463 &gEfiComponentNameProtocolGuid,\r
1464 (VOID **) &ComponentName\r
1465 );\r
1466 if (!EFI_ERROR (Status)) {\r
1467 gBS->UninstallProtocolInterface (\r
1468 gI2cBusDriverBinding.DriverBindingHandle,\r
1469 &gEfiComponentNameProtocolGuid,\r
1470 ComponentName\r
1471 );\r
1472 }\r
1473\r
1474 Status = gBS->HandleProtocol (\r
1475 gI2cBusDriverBinding.DriverBindingHandle,\r
1476 &gEfiComponentName2ProtocolGuid,\r
1477 (VOID **) &ComponentName2\r
1478 );\r
1479 if (!EFI_ERROR (Status)) {\r
1480 gBS->UninstallProtocolInterface (\r
1481 gI2cBusDriverBinding.DriverBindingHandle,\r
1482 &gEfiComponentName2ProtocolGuid,\r
1483 ComponentName2\r
1484 );\r
1485 }\r
1486\r
1487 Status = EFI_SUCCESS;\r
1488\r
43e543bc
EL
1489Done:\r
1490 //\r
1491 // Free the buffer containing the list of handles from the handle database\r
1492 //\r
1493 if (DeviceHandleBuffer != NULL) {\r
1494 gBS->FreePool (DeviceHandleBuffer);\r
1495 }\r
1496\r
1497 return Status;\r
1498}\r