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