FtdiUsbSerialDriver.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580
  1. /** @file
  2. USB Serial Driver that manages USB to Serial and produces Serial IO Protocol.
  3. Copyright (c) 2004 - 2013, Intel Corporation. All rights reserved.
  4. Portions Copyright 2012 Ashley DeSimone
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. //
  8. // Tested with VEND_ID 0x0403, DEVICE_ID 0x6001
  9. //
  10. // Driver starts the device with the following values:
  11. // 115200, No parity, 8 data bits, 1 stop bit, No Flow control
  12. //
  13. #include "FtdiUsbSerialDriver.h"
  14. //
  15. // Table of supported devices. This is the device information that this
  16. // driver was developed with. Add other FTDI devices as needed.
  17. //
  18. USB_DEVICE gUSBDeviceList[] = {
  19. {VID_FTDI, DID_FTDI_FT232},
  20. {0,0}
  21. };
  22. //
  23. // USB Serial Driver Global Variables
  24. //
  25. EFI_DRIVER_BINDING_PROTOCOL gUsbSerialDriverBinding = {
  26. UsbSerialDriverBindingSupported,
  27. UsbSerialDriverBindingStart,
  28. UsbSerialDriverBindingStop,
  29. 0xa,
  30. NULL,
  31. NULL
  32. };
  33. //
  34. // Table with the nearest power of 2 for the numbers 0-15
  35. //
  36. UINT8 gRoundedPowersOf2[16] = { 0, 2, 2, 4, 4, 4, 8, 8, 8, 8, 8, 8, 16, 16, 16, 16 };
  37. /**
  38. Check to see if the device path node is the Flow control node
  39. @param[in] FlowControl The device path node to be checked
  40. @retval TRUE It is the flow control node
  41. @retval FALSE It is not the flow control node
  42. **/
  43. BOOLEAN
  44. IsUartFlowControlNode (
  45. IN UART_FLOW_CONTROL_DEVICE_PATH *FlowControl
  46. )
  47. {
  48. return (BOOLEAN) (
  49. (DevicePathType (FlowControl) == MESSAGING_DEVICE_PATH) &&
  50. (DevicePathSubType (FlowControl) == MSG_VENDOR_DP) &&
  51. (CompareGuid (&FlowControl->Guid, &gEfiUartDevicePathGuid))
  52. );
  53. }
  54. /**
  55. Checks the device path to see if it contains flow control.
  56. @param[in] DevicePath The device path to be checked
  57. @retval TRUE It contains flow control
  58. @retval FALSE It does not contain flow control
  59. **/
  60. BOOLEAN
  61. ContainsFlowControl (
  62. IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
  63. )
  64. {
  65. while (!IsDevicePathEnd (DevicePath)) {
  66. if (IsUartFlowControlNode ((UART_FLOW_CONTROL_DEVICE_PATH *) DevicePath)) {
  67. return TRUE;
  68. }
  69. DevicePath = NextDevicePathNode (DevicePath);
  70. }
  71. return FALSE;
  72. }
  73. /**
  74. Transfer the data between the device and host.
  75. This function transfers the data between the device and host.
  76. BOT transfer is composed of three phases: Command, Data, and Status.
  77. This is the Data phase.
  78. @param UsbBot[in] The USB BOT device
  79. @param DataDir[in] The direction of the data
  80. @param Data[in, out] The buffer to hold data
  81. @param TransLen[in, out] The expected length of the data
  82. @param Timeout[in] The time to wait the command to complete
  83. @retval EFI_SUCCESS The data is transferred
  84. @retval EFI_SUCCESS No data to transfer
  85. @retval EFI_NOT_READY The device return NAK to the transfer
  86. @retval Others Failed to transfer data
  87. **/
  88. EFI_STATUS
  89. UsbSerialDataTransfer (
  90. IN USB_SER_DEV *UsbBot,
  91. IN EFI_USB_DATA_DIRECTION DataDir,
  92. IN OUT VOID *Data,
  93. IN OUT UINTN *TransLen,
  94. IN UINT32 Timeout
  95. )
  96. {
  97. EFI_USB_ENDPOINT_DESCRIPTOR *Endpoint;
  98. EFI_STATUS Status;
  99. UINT32 Result;
  100. //
  101. // If no data to transfer, just return EFI_SUCCESS.
  102. //
  103. if ((DataDir == EfiUsbNoData) || (*TransLen == 0)) {
  104. return EFI_SUCCESS;
  105. }
  106. //
  107. // Select the endpoint then issue the transfer
  108. //
  109. if (DataDir == EfiUsbDataIn) {
  110. Endpoint = &UsbBot->InEndpointDescriptor;
  111. } else {
  112. Endpoint = &UsbBot->OutEndpointDescriptor;
  113. }
  114. Result = 0;
  115. Status = UsbBot->UsbIo->UsbBulkTransfer (
  116. UsbBot->UsbIo,
  117. Endpoint->EndpointAddress,
  118. Data,
  119. TransLen,
  120. Timeout,
  121. &Result
  122. );
  123. if (EFI_ERROR (Status)) {
  124. if (USB_IS_ERROR (Result, EFI_USB_ERR_NAK)) {
  125. Status = EFI_NOT_READY;
  126. } else {
  127. UsbBot->Shutdown = TRUE; // Fixes infinite loop in older EFI
  128. }
  129. return Status;
  130. }
  131. return Status;
  132. }
  133. /**
  134. Sets the status values of the Usb Serial Device.
  135. @param UsbSerialDevice[in] Handle to the Usb Serial Device to set the status
  136. for
  137. @param StatusBuffer[in] Buffer holding the status values
  138. @retval EFI_SUCCESS The status values were read and set correctly
  139. **/
  140. EFI_STATUS
  141. EFIAPI
  142. SetStatusInternal (
  143. IN USB_SER_DEV *UsbSerialDevice,
  144. IN UINT8 *StatusBuffer
  145. )
  146. {
  147. UINT8 Msr;
  148. Msr = (StatusBuffer[0] & MSR_MASK);
  149. //
  150. // set the Status values to disabled
  151. //
  152. UsbSerialDevice->StatusValues.CtsState = FALSE;
  153. UsbSerialDevice->StatusValues.DsrState = FALSE;
  154. UsbSerialDevice->StatusValues.RiState = FALSE;
  155. UsbSerialDevice->StatusValues.SdState = FALSE;
  156. //
  157. // Check the values from the status buffer and set the appropriate status
  158. // values to enabled
  159. //
  160. if ((Msr & CTS_MASK) == CTS_MASK) {
  161. UsbSerialDevice->StatusValues.CtsState = TRUE;
  162. }
  163. if ((Msr & DSR_MASK) == DSR_MASK) {
  164. UsbSerialDevice->StatusValues.DsrState = TRUE;
  165. }
  166. if ((Msr & RI_MASK) == RI_MASK) {
  167. UsbSerialDevice->StatusValues.RiState = TRUE;
  168. }
  169. if ((Msr & SD_MASK) == SD_MASK) {
  170. UsbSerialDevice->StatusValues.SdState = TRUE;
  171. }
  172. return EFI_SUCCESS;
  173. }
  174. /**
  175. Initiates a read operation on the Usb Serial Device.
  176. @param UsbSerialDevice[in] Handle to the USB device to read
  177. @param BufferSize[in, out] On input, the size of the Buffer. On output,
  178. the amount of data returned in Buffer.
  179. Setting this to zero will initiate a read
  180. and store all data returned in the internal
  181. buffer.
  182. @param Buffer [out] The buffer to return the data into.
  183. @retval EFI_SUCCESS The data was read.
  184. @retval EFI_DEVICE_ERROR The device reported an error.
  185. @retval EFI_TIMEOUT The data write was stopped due to a timeout.
  186. **/
  187. EFI_STATUS
  188. EFIAPI
  189. ReadDataFromUsb (
  190. IN USB_SER_DEV *UsbSerialDevice,
  191. IN OUT UINTN *BufferSize,
  192. OUT VOID *Buffer
  193. )
  194. {
  195. EFI_STATUS Status;
  196. UINTN ReadBufferSize;
  197. UINT8 *ReadBuffer;
  198. UINTN Index;
  199. EFI_TPL Tpl;
  200. UINT8 StatusBuffer[2]; // buffer to store the status bytes
  201. ReadBufferSize = 512;
  202. ReadBuffer = &(UsbSerialDevice->ReadBuffer[0]);
  203. if (UsbSerialDevice->Shutdown) {
  204. return EFI_DEVICE_ERROR;
  205. }
  206. Tpl = gBS->RaiseTPL (TPL_NOTIFY);
  207. Status = UsbSerialDataTransfer (
  208. UsbSerialDevice,
  209. EfiUsbDataIn,
  210. ReadBuffer,
  211. &ReadBufferSize,
  212. FTDI_TIMEOUT*2 //Padded because timers won't be exactly aligned
  213. );
  214. if (EFI_ERROR (Status)) {
  215. gBS->RestoreTPL (Tpl);
  216. if (Status == EFI_TIMEOUT) {
  217. return EFI_TIMEOUT;
  218. } else {
  219. return EFI_DEVICE_ERROR;
  220. }
  221. }
  222. //
  223. // Store the status bytes in the status buffer
  224. //
  225. for (Index = 0; Index < 2; Index++) {//only the first 2 bytes are status bytes
  226. StatusBuffer[Index] = ReadBuffer[Index];
  227. }
  228. //
  229. // update the statusvalue field of the usbserialdevice
  230. //
  231. Status = SetStatusInternal (UsbSerialDevice, StatusBuffer);
  232. if (Status != EFI_SUCCESS) {
  233. }
  234. //
  235. // Store the read data in the read buffer, start at 2 to ignore status bytes
  236. //
  237. for (Index = 2; Index < ReadBufferSize; Index++) {
  238. if (((UsbSerialDevice->DataBufferTail + 1) % SW_FIFO_DEPTH) == UsbSerialDevice->DataBufferHead) {
  239. break;
  240. }
  241. if (ReadBuffer[Index] == 0x00) {
  242. //
  243. // This is null, do not add
  244. //
  245. } else {
  246. UsbSerialDevice->DataBuffer[UsbSerialDevice->DataBufferTail] = ReadBuffer[Index];
  247. UsbSerialDevice->DataBufferTail = (UsbSerialDevice->DataBufferTail + 1) % SW_FIFO_DEPTH;
  248. }
  249. }
  250. //
  251. // Read characters out of the buffer to satisfy caller's request.
  252. //
  253. for (Index = 0; Index < *BufferSize; Index++) {
  254. if (UsbSerialDevice->DataBufferHead == UsbSerialDevice->DataBufferTail) {
  255. break;
  256. }
  257. //
  258. // Still have characters in the buffer to return
  259. //
  260. ((UINT8 *)Buffer)[Index] = UsbSerialDevice->DataBuffer[UsbSerialDevice->DataBufferHead];
  261. UsbSerialDevice->DataBufferHead = (UsbSerialDevice->DataBufferHead + 1) % SW_FIFO_DEPTH;
  262. }
  263. //
  264. // Return actual number of bytes returned.
  265. //
  266. *BufferSize = Index;
  267. gBS->RestoreTPL (Tpl);
  268. return EFI_SUCCESS;
  269. }
  270. /**
  271. Sets the initial status values of the Usb Serial Device by reading the status
  272. bytes from the device.
  273. @param UsbSerialDevice[in] Handle to the Usb Serial Device that needs its
  274. initial status values set
  275. @retval EFI_SUCCESS The status bytes were read successfully and the
  276. initial status values were set correctly
  277. @retval EFI_TIMEOUT The read of the status bytes was stopped due to a
  278. timeout
  279. @retval EFI_DEVICE_ERROR The device reported an error during the read of
  280. the status bytes
  281. **/
  282. EFI_STATUS
  283. EFIAPI
  284. SetInitialStatus (
  285. IN USB_SER_DEV *UsbSerialDevice
  286. )
  287. {
  288. EFI_STATUS Status;
  289. UINTN BufferSize;
  290. EFI_TPL Tpl;
  291. UINT8 StatusBuffer[2];
  292. Status = EFI_UNSUPPORTED;
  293. BufferSize = sizeof (StatusBuffer);
  294. if (UsbSerialDevice->Shutdown) {
  295. return EFI_DEVICE_ERROR;
  296. }
  297. Tpl = gBS->RaiseTPL (TPL_NOTIFY);
  298. Status = UsbSerialDataTransfer (
  299. UsbSerialDevice,
  300. EfiUsbDataIn,
  301. StatusBuffer,
  302. &BufferSize,
  303. 40 //Slightly more than 2x the FTDI polling frequency to make sure that data will be returned
  304. );
  305. Status = SetStatusInternal (UsbSerialDevice, StatusBuffer);
  306. gBS->RestoreTPL (Tpl);
  307. return Status;
  308. }
  309. /**
  310. UsbSerialDriverCheckInput.
  311. attempts to read data in from the device periodically, stores any read data
  312. and updates the control attributes.
  313. @param Event[in]
  314. @param Context[in]....The current instance of the USB serial device
  315. **/
  316. VOID
  317. EFIAPI
  318. UsbSerialDriverCheckInput (
  319. IN EFI_EVENT Event,
  320. IN VOID *Context
  321. )
  322. {
  323. UINTN BufferSize;
  324. USB_SER_DEV *UsbSerialDevice;
  325. UsbSerialDevice = (USB_SER_DEV*)Context;
  326. if (UsbSerialDevice->DataBufferHead == UsbSerialDevice->DataBufferTail) {
  327. //
  328. // Data buffer is empty, try to read from device
  329. //
  330. BufferSize = 0;
  331. ReadDataFromUsb (UsbSerialDevice, &BufferSize, NULL);
  332. if (UsbSerialDevice->DataBufferHead == UsbSerialDevice->DataBufferTail) {
  333. //
  334. // Data buffer still has no data, set the EFI_SERIAL_INPUT_BUFFER_EMPTY
  335. // flag
  336. //
  337. UsbSerialDevice->ControlBits |= EFI_SERIAL_INPUT_BUFFER_EMPTY;
  338. } else {
  339. //
  340. // Read has returned some data, clear the EFI_SERIAL_INPUT_BUFFER_EMPTY
  341. // flag
  342. //
  343. UsbSerialDevice->ControlBits &= ~(EFI_SERIAL_INPUT_BUFFER_EMPTY);
  344. }
  345. } else {
  346. //
  347. // Data buffer has data, no read attempt required
  348. //
  349. UsbSerialDevice->ControlBits &= ~(EFI_SERIAL_INPUT_BUFFER_EMPTY);
  350. }
  351. }
  352. /**
  353. Encodes the baud rate into the format expected by the Ftdi device.
  354. @param BaudRate[in] The baudrate to be set on the device
  355. @param EncodedBaudRate[out] The baud rate encoded in the format
  356. expected by the Ftdi device
  357. @return EFI_SUCCESS Baudrate encoding was calculated
  358. successfully
  359. @return EFI_INVALID_PARAMETER An invalid value of BaudRate was received
  360. **/
  361. EFI_STATUS
  362. EFIAPI
  363. EncodeBaudRateForFtdi (
  364. IN UINT64 BaudRate,
  365. OUT UINT16 *EncodedBaudRate
  366. )
  367. {
  368. UINT32 Divisor;
  369. UINT32 AdjustedFrequency;
  370. UINT16 Result;
  371. //
  372. // Check to make sure we won't get an integer overflow
  373. //
  374. if ((BaudRate < 178) || ( BaudRate > ((FTDI_UART_FREQUENCY * 100) / 97))) {
  375. return EFI_INVALID_PARAMETER;
  376. }
  377. //
  378. // Baud Rates of 2000000 and 3000000 are special cases
  379. //
  380. if ((BaudRate >= FTDI_SPECIAL_CASE_300_MIN) && (BaudRate <= FTDI_SPECIAL_CASE_300_MAX)) {
  381. *EncodedBaudRate = 0;
  382. return EFI_SUCCESS;
  383. }
  384. if ((BaudRate >= FTDI_SPECIAL_CASE_200_MIN) && (BaudRate <= FTDI_SPECIAL_CASE_200_MAX)) {
  385. *EncodedBaudRate = 1;
  386. return EFI_SUCCESS;
  387. }
  388. //
  389. // Compute divisor
  390. //
  391. Divisor = (FTDI_UART_FREQUENCY << 4) / (UINT32)BaudRate;
  392. //
  393. // Round the last 4 bits to the nearest power of 2
  394. //
  395. Divisor = (Divisor & ~(0xF)) + (gRoundedPowersOf2[Divisor & 0xF]);
  396. //
  397. // Check to make sure computed divisor is within
  398. // the min and max that FTDI controller will accept
  399. //
  400. if (Divisor < FTDI_MIN_DIVISOR) {
  401. Divisor = FTDI_MIN_DIVISOR;
  402. } else if (Divisor > FTDI_MAX_DIVISOR) {
  403. Divisor = FTDI_MAX_DIVISOR;
  404. }
  405. //
  406. // Check to make sure the frequency that the FTDI chip will need to
  407. // generate to attain the requested Baud Rate is within 3% of the
  408. // 3MHz clock frequency that the FTDI chip runs at.
  409. //
  410. // (3MHz * 1600) / 103 = 46601941
  411. // (3MHz * 1600) / 97 = 49484536
  412. //
  413. AdjustedFrequency = (((UINT32)BaudRate) * Divisor);
  414. if ((AdjustedFrequency < FTDI_MIN_FREQUENCY) || (AdjustedFrequency > FTDI_MAX_FREQUENCY)) {
  415. return EFI_INVALID_PARAMETER;
  416. }
  417. //
  418. // Encode the Divisor into the format FTDI expects
  419. //
  420. Result = (UINT16)(Divisor >> 4);
  421. if ((Divisor & 0x8) != 0) {
  422. Result |= 0x4000;
  423. } else if ((Divisor & 0x4) != 0) {
  424. Result |= 0x8000;
  425. } else if ((Divisor & 0x2) != 0) {
  426. Result |= 0xC000;
  427. }
  428. *EncodedBaudRate = Result;
  429. return EFI_SUCCESS;
  430. }
  431. /**
  432. Uses USB I/O to check whether the device is a USB Serial device.
  433. @param UsbIo[in] Pointer to a USB I/O protocol instance.
  434. @retval TRUE Device is a USB Serial device.
  435. @retval FALSE Device is a not USB Serial device.
  436. **/
  437. BOOLEAN
  438. IsUsbSerial (
  439. IN EFI_USB_IO_PROTOCOL *UsbIo
  440. )
  441. {
  442. EFI_STATUS Status;
  443. EFI_USB_DEVICE_DESCRIPTOR DeviceDescriptor;
  444. CHAR16 *StrMfg;
  445. BOOLEAN Found;
  446. UINT32 Index;
  447. //
  448. // Get the default device descriptor
  449. //
  450. Status = UsbIo->UsbGetDeviceDescriptor (
  451. UsbIo,
  452. &DeviceDescriptor
  453. );
  454. if (EFI_ERROR (Status)) {
  455. return FALSE;
  456. }
  457. Found = FALSE;
  458. Index = 0;
  459. while (gUSBDeviceList[Index].VendorId != 0 &&
  460. gUSBDeviceList[Index].DeviceId != 0 &&
  461. !Found ) {
  462. if (DeviceDescriptor.IdProduct == gUSBDeviceList[Index].DeviceId &&
  463. DeviceDescriptor.IdVendor == gUSBDeviceList[Index].VendorId ){
  464. //
  465. // Checks to see if a string descriptor can be pulled from the device in
  466. // the selected language. If not False is returned indicating that this
  467. // is not a Usb Serial Device that can be managegd by this driver
  468. //
  469. StrMfg = NULL;
  470. Status = UsbIo->UsbGetStringDescriptor (
  471. UsbIo,
  472. USB_US_LANG_ID, // LANGID selector, should make this
  473. // more robust to verify lang support
  474. // for device
  475. DeviceDescriptor.StrManufacturer,
  476. &StrMfg
  477. );
  478. if (StrMfg != NULL) {
  479. FreePool (StrMfg);
  480. }
  481. if (EFI_ERROR (Status)) {
  482. return FALSE;
  483. }
  484. return TRUE;
  485. }
  486. Index++;
  487. }
  488. return FALSE;
  489. }
  490. /**
  491. Internal function that sets the Data Bits, Stop Bits and Parity values on the
  492. Usb Serial Device with a single usb control transfer.
  493. @param UsbIo[in] Usb Io Protocol instance pointer
  494. @param DataBits[in] The data bits value to be set on the Usb
  495. Serial Device
  496. @param Parity[in] The parity type that will be set on the Usb
  497. Serial Device
  498. @param StopBits[in] The stop bits type that will be set on the
  499. Usb Serial Device
  500. @param LastSettings[in] A pointer to the Usb Serial Device's
  501. PREVIOUS_ATTRIBUTES item
  502. @retval EFI_SUCCESS The data items were correctly set on the
  503. USB Serial Device
  504. @retval EFI_INVALID_PARAMETER An invalid data parameter or an invalid
  505. combination or parameters was used
  506. @retval EFI_DEVICE_ERROR The device is not functioning correctly and
  507. the data values were unable to be set
  508. **/
  509. EFI_STATUS
  510. EFIAPI
  511. SetDataInternal (
  512. IN EFI_USB_IO_PROTOCOL *UsbIo,
  513. IN UINT8 DataBits,
  514. IN EFI_PARITY_TYPE Parity,
  515. IN EFI_STOP_BITS_TYPE StopBits,
  516. IN PREVIOUS_ATTRIBUTES *LastSettings
  517. )
  518. {
  519. EFI_STATUS Status;
  520. EFI_USB_DEVICE_REQUEST DevReq;
  521. UINT32 ReturnValue;
  522. UINT8 ConfigurationValue;
  523. //
  524. // Since data bits settings of 6,7,8 cannot be set with a stop bits setting of
  525. // 1.5 check to see if this happens when the values of last settings are used
  526. //
  527. if ((DataBits == 0) && (StopBits == OneFiveStopBits)) {
  528. if ((LastSettings->DataBits == 6) || (LastSettings->DataBits == 7) || (LastSettings->DataBits == 8)) {
  529. return EFI_INVALID_PARAMETER;
  530. }
  531. } else if ((StopBits == DefaultStopBits) && ((DataBits == 6) || (DataBits == 7) || (DataBits == 8))) {
  532. if (LastSettings->StopBits == OneFiveStopBits) {
  533. return EFI_INVALID_PARAMETER;
  534. }
  535. } else if ((DataBits == 0) && (StopBits == DefaultStopBits)) {
  536. if (LastSettings->StopBits == OneFiveStopBits) {
  537. if ((LastSettings->DataBits == 6) || (LastSettings->DataBits == 7) || (LastSettings->DataBits == 8)) {
  538. return EFI_INVALID_PARAMETER;
  539. }
  540. }
  541. }
  542. //
  543. // set the DevReq.Value for the usb control transfer to the correct value
  544. // based on the seleceted number of data bits if there is an invalid number of
  545. // data bits requested return EFI_INVALID_PARAMETER
  546. //
  547. if (((DataBits < 5 ) || (DataBits > 8)) && (DataBits != 0)) {
  548. return EFI_INVALID_PARAMETER;
  549. }
  550. if (DataBits == 0) {
  551. //
  552. // use the value of LastDataBits
  553. //
  554. DevReq.Value = SET_DATA_BITS (LastSettings->DataBits);
  555. } else {
  556. //
  557. // use the value of DataBits
  558. //
  559. DevReq.Value = SET_DATA_BITS (DataBits);
  560. }
  561. //
  562. // Set Parity
  563. //
  564. if (Parity == DefaultParity) {
  565. Parity = LastSettings->Parity;
  566. }
  567. if (Parity == NoParity) {
  568. DevReq.Value |= SET_PARITY_NONE;
  569. } else if (Parity == EvenParity) {
  570. DevReq.Value |= SET_PARITY_EVEN;
  571. } else if (Parity == OddParity){
  572. DevReq.Value |= SET_PARITY_ODD;
  573. } else if (Parity == MarkParity) {
  574. DevReq.Value |= SET_PARITY_MARK;
  575. } else if (Parity == SpaceParity) {
  576. DevReq.Value |= SET_PARITY_SPACE;
  577. }
  578. //
  579. // Set Stop Bits
  580. //
  581. if (StopBits == DefaultStopBits) {
  582. StopBits = LastSettings->StopBits;
  583. }
  584. if (StopBits == OneStopBit) {
  585. DevReq.Value |= SET_STOP_BITS_1;
  586. } else if (StopBits == OneFiveStopBits) {
  587. DevReq.Value |= SET_STOP_BITS_15;
  588. } else if (StopBits == TwoStopBits) {
  589. DevReq.Value |= SET_STOP_BITS_2;
  590. }
  591. //
  592. // set the rest of the DevReq parameters and perform the usb control transfer
  593. // to set the data bits on the device
  594. //
  595. DevReq.Request = FTDI_COMMAND_SET_DATA;
  596. DevReq.RequestType = USB_REQ_TYPE_VENDOR;
  597. DevReq.Index = FTDI_PORT_IDENTIFIER;
  598. DevReq.Length = 0; // indicates that there is no data phase in this request
  599. Status = UsbIo->UsbControlTransfer (
  600. UsbIo,
  601. &DevReq,
  602. EfiUsbDataOut,
  603. WDR_SHORT_TIMEOUT,
  604. &ConfigurationValue,
  605. 1,
  606. &ReturnValue
  607. );
  608. if (EFI_ERROR (Status)) {
  609. goto StatusError;
  610. }
  611. return Status;
  612. StatusError:
  613. if ((Status != EFI_INVALID_PARAMETER) || (Status != EFI_DEVICE_ERROR)) {
  614. return EFI_DEVICE_ERROR;
  615. } else {
  616. return Status;
  617. }
  618. }
  619. /**
  620. Internal function that sets the baudrate on the Usb Serial Device.
  621. @param UsbIo[in] Usb Io Protocol instance pointer
  622. @param BaudRate[in] The baudrate value to be set on the device.
  623. If this value is 0 the value of LastBaudRate
  624. will be used instead
  625. @param LastBaudRate[in] The baud rate value that was previously set
  626. on the Usb Serial Device
  627. @retval EFI_SUCCESS The baudrate was set succesfully
  628. @retval EFI_INVALID_PARAMETER An invalid baudrate was used
  629. @retval EFI_DEVICE_ERROR The device is not functioning correctly and
  630. the baudrate was unable to be set
  631. **/
  632. EFI_STATUS
  633. EFIAPI
  634. SetBaudRateInternal (
  635. IN EFI_USB_IO_PROTOCOL *UsbIo,
  636. IN UINT64 BaudRate,
  637. IN UINT64 LastBaudRate
  638. )
  639. {
  640. EFI_STATUS Status;
  641. EFI_USB_DEVICE_REQUEST DevReq;
  642. UINT32 ReturnValue;
  643. UINT8 ConfigurationValue;
  644. UINT16 EncodedBaudRate;
  645. EFI_TPL Tpl;
  646. Tpl = gBS->RaiseTPL(TPL_NOTIFY);
  647. //
  648. // set the value of DevReq.Value based on the value of BaudRate
  649. // if 0 is selected as baud rate use the value of LastBaudRate
  650. //
  651. if (BaudRate == 0) {
  652. Status = EncodeBaudRateForFtdi (LastBaudRate, &EncodedBaudRate);
  653. if (EFI_ERROR (Status)) {
  654. gBS->RestoreTPL (Tpl);
  655. //
  656. // EncodeBaudRateForFtdi returns EFI_INVALID_PARAMETER when not
  657. // succesfull
  658. //
  659. return Status;
  660. }
  661. DevReq.Value = EncodedBaudRate;
  662. } else {
  663. Status = EncodeBaudRateForFtdi (BaudRate, &EncodedBaudRate);
  664. if (EFI_ERROR (Status)) {
  665. gBS->RestoreTPL (Tpl);
  666. //
  667. // EncodeBaudRateForFtdi returns EFI_INVALID_PARAMETER when not
  668. // successfull
  669. //
  670. return Status;
  671. }
  672. DevReq.Value = EncodedBaudRate;
  673. }
  674. //
  675. // set the remaining parameters of DevReq and perform the usb control transfer
  676. // to set the device
  677. //
  678. DevReq.Request = FTDI_COMMAND_SET_BAUDRATE;
  679. DevReq.RequestType = USB_REQ_TYPE_VENDOR;
  680. DevReq.Index = FTDI_PORT_IDENTIFIER;
  681. DevReq.Length = 0; // indicates that there is no data phase in this request
  682. Status = UsbIo->UsbControlTransfer (
  683. UsbIo,
  684. &DevReq,
  685. EfiUsbDataOut,
  686. WDR_SHORT_TIMEOUT,
  687. &ConfigurationValue,
  688. 1,
  689. &ReturnValue
  690. );
  691. if (EFI_ERROR (Status)) {
  692. goto StatusError;
  693. }
  694. gBS->RestoreTPL (Tpl);
  695. return Status;
  696. StatusError:
  697. gBS->RestoreTPL (Tpl);
  698. if ((Status != EFI_INVALID_PARAMETER) || (Status != EFI_DEVICE_ERROR)) {
  699. return EFI_DEVICE_ERROR;
  700. } else {
  701. return Status;
  702. }
  703. }
  704. /**
  705. Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
  706. data bits, and stop bits on a serial device.
  707. @param UsbSerialDevice[in] Pointer to the current instance of the USB Serial
  708. Device.
  709. @param BaudRate[in] The requested baud rate. A BaudRate value of 0
  710. will use the device's default interface speed.
  711. @param ReveiveFifoDepth[in] The requested depth of the FIFO on the receive
  712. side of the serial interface. A ReceiveFifoDepth
  713. value of 0 will use the device's default FIFO
  714. depth.
  715. @param Timeout[in] The requested time out for a single character in
  716. microseconds.This timeout applies to both the
  717. transmit and receive side of the interface.A
  718. Timeout value of 0 will use the device's default
  719. time out value.
  720. @param Parity[in] The type of parity to use on this serial device.
  721. A Parity value of DefaultParity will use the
  722. device's default parity value.
  723. @param DataBits[in] The number of data bits to use on the serial
  724. device. A DataBits value of 0 will use the
  725. device's default data bit setting.
  726. @param StopBits[in] The number of stop bits to use on this serial
  727. device. A StopBits value of DefaultStopBits will
  728. use the device's default number of stop bits.
  729. @retval EFI_SUCCESS The attributes were set
  730. @retval EFI_DEVICE_ERROR The attributes were not able to be set
  731. **/
  732. EFI_STATUS
  733. EFIAPI
  734. SetAttributesInternal (
  735. IN USB_SER_DEV *UsbSerialDevice,
  736. IN UINT64 BaudRate,
  737. IN UINT32 ReceiveFifoDepth,
  738. IN UINT32 Timeout,
  739. IN EFI_PARITY_TYPE Parity,
  740. IN UINT8 DataBits,
  741. IN EFI_STOP_BITS_TYPE StopBits
  742. )
  743. {
  744. EFI_STATUS Status;
  745. EFI_TPL Tpl;
  746. UART_DEVICE_PATH *Uart;
  747. EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
  748. Status = EFI_UNSUPPORTED;
  749. Tpl = gBS->RaiseTPL(TPL_NOTIFY);
  750. Uart = NULL;
  751. //
  752. // check for invalid combinations of parameters
  753. //
  754. if (((DataBits >= 6) && (DataBits <= 8)) && (StopBits == OneFiveStopBits)) {
  755. return EFI_INVALID_PARAMETER;
  756. }
  757. //
  758. // set data bits, parity and stop bits
  759. //
  760. Status = SetDataInternal (
  761. UsbSerialDevice->UsbIo,
  762. DataBits,
  763. Parity,
  764. StopBits,
  765. &(UsbSerialDevice->LastSettings)
  766. );
  767. if (EFI_ERROR (Status)) {
  768. goto StatusError;
  769. }
  770. //
  771. // set baudrate
  772. //
  773. Status = SetBaudRateInternal (
  774. UsbSerialDevice->UsbIo,
  775. BaudRate,
  776. UsbSerialDevice->LastSettings.BaudRate
  777. );
  778. if (EFI_ERROR (Status)){
  779. goto StatusError;
  780. }
  781. //
  782. // update the values of UsbSerialDevice->LastSettings and UsbSerialDevice->SerialIo.Mode
  783. //
  784. if (BaudRate == 0) {
  785. UsbSerialDevice->LastSettings.BaudRate = UsbSerialDevice->LastSettings.BaudRate;
  786. UsbSerialDevice->SerialIo.Mode->BaudRate = UsbSerialDevice->LastSettings.BaudRate;
  787. } else {
  788. UsbSerialDevice->LastSettings.BaudRate = BaudRate;
  789. UsbSerialDevice->SerialIo.Mode->BaudRate = BaudRate;
  790. }
  791. UsbSerialDevice->LastSettings.Timeout = FTDI_TIMEOUT;
  792. UsbSerialDevice->LastSettings.ReceiveFifoDepth = FTDI_MAX_RECEIVE_FIFO_DEPTH;
  793. if (Parity == DefaultParity) {
  794. UsbSerialDevice->LastSettings.Parity = UsbSerialDevice->LastSettings.Parity;
  795. UsbSerialDevice->SerialIo.Mode->Parity = UsbSerialDevice->LastSettings.Parity;
  796. } else {
  797. UsbSerialDevice->LastSettings.Parity = Parity;
  798. UsbSerialDevice->SerialIo.Mode->Parity = Parity;
  799. }
  800. if (DataBits == 0) {
  801. UsbSerialDevice->LastSettings.DataBits = UsbSerialDevice->LastSettings.DataBits;
  802. UsbSerialDevice->SerialIo.Mode->DataBits = UsbSerialDevice->LastSettings.DataBits;
  803. } else {
  804. UsbSerialDevice->LastSettings.DataBits = DataBits;
  805. UsbSerialDevice->SerialIo.Mode->DataBits = DataBits;
  806. }
  807. if (StopBits == DefaultStopBits) {
  808. UsbSerialDevice->LastSettings.StopBits = UsbSerialDevice->LastSettings.StopBits;
  809. UsbSerialDevice->SerialIo.Mode->StopBits = UsbSerialDevice->LastSettings.StopBits;
  810. } else {
  811. UsbSerialDevice->LastSettings.StopBits = StopBits;
  812. UsbSerialDevice->SerialIo.Mode->StopBits = StopBits;
  813. }
  814. //
  815. // See if the device path node has changed
  816. //
  817. if (UsbSerialDevice->UartDevicePath.BaudRate == BaudRate &&
  818. UsbSerialDevice->UartDevicePath.DataBits == DataBits &&
  819. UsbSerialDevice->UartDevicePath.StopBits == StopBits &&
  820. UsbSerialDevice->UartDevicePath.Parity == Parity
  821. ) {
  822. gBS->RestoreTPL (Tpl);
  823. return EFI_SUCCESS;
  824. }
  825. //
  826. // Update the device path
  827. //
  828. UsbSerialDevice->UartDevicePath.BaudRate = BaudRate;
  829. UsbSerialDevice->UartDevicePath.DataBits = DataBits;
  830. UsbSerialDevice->UartDevicePath.StopBits = (UINT8) StopBits;
  831. UsbSerialDevice->UartDevicePath.Parity = (UINT8) Parity;
  832. Status = EFI_SUCCESS;
  833. if (UsbSerialDevice->ControllerHandle != NULL) {
  834. RemainingDevicePath = UsbSerialDevice->DevicePath;
  835. while (!IsDevicePathEnd (RemainingDevicePath)) {
  836. Uart = (UART_DEVICE_PATH *) NextDevicePathNode (RemainingDevicePath);
  837. if (Uart->Header.Type == MESSAGING_DEVICE_PATH &&
  838. Uart->Header.SubType == MSG_UART_DP &&
  839. sizeof (UART_DEVICE_PATH) == DevicePathNodeLength ((EFI_DEVICE_PATH *) Uart)) {
  840. Uart->BaudRate = BaudRate;
  841. Uart->DataBits = DataBits;
  842. Uart->StopBits = (UINT8)StopBits;
  843. Uart->Parity = (UINT8) Parity;
  844. break;
  845. }
  846. RemainingDevicePath = NextDevicePathNode (RemainingDevicePath);
  847. }
  848. }
  849. gBS->RestoreTPL (Tpl);
  850. return Status;
  851. StatusError:
  852. gBS->RestoreTPL (Tpl);
  853. if ((Status != EFI_INVALID_PARAMETER) || (Status != EFI_DEVICE_ERROR)) {
  854. return EFI_DEVICE_ERROR;
  855. } else {
  856. return Status;
  857. }
  858. }
  859. /**
  860. Internal function that performs a Usb Control Transfer to set the flow control
  861. on the Usb Serial Device.
  862. @param UsbIo[in] Usb Io Protocol instance pointer
  863. @param FlowControlEnable[in] Data on the Enable/Disable status of Flow
  864. Control on the Usb Serial Device
  865. @retval EFI_SUCCESS The flow control was set on the Usb Serial
  866. device
  867. @retval EFI_INVALID_PARAMETER An invalid flow control value was used
  868. @retval EFI_EFI_UNSUPPORTED The operation is not supported
  869. @retval EFI_DEVICE_ERROR The device is not functioning correctly
  870. **/
  871. EFI_STATUS
  872. EFIAPI
  873. SetFlowControlInternal (
  874. IN EFI_USB_IO_PROTOCOL *UsbIo,
  875. IN BOOLEAN FlowControlEnable
  876. )
  877. {
  878. EFI_STATUS Status;
  879. EFI_USB_DEVICE_REQUEST DevReq;
  880. UINT32 ReturnValue;
  881. UINT8 ConfigurationValue;
  882. //
  883. // set DevReq.Value based on the value of FlowControlEnable
  884. //
  885. if (!FlowControlEnable) {
  886. DevReq.Value = NO_FLOW_CTRL;
  887. }
  888. if (FlowControlEnable) {
  889. DevReq.Value = XON_XOFF_CTRL;
  890. }
  891. //
  892. // set the remaining DevReq parameters and perform the usb control transfer to
  893. // set the flow control on the device
  894. //
  895. DevReq.Request = FTDI_COMMAND_SET_FLOW_CTRL;
  896. DevReq.RequestType = USB_REQ_TYPE_VENDOR;
  897. DevReq.Index = FTDI_PORT_IDENTIFIER;
  898. DevReq.Length = 0; // indicates that this transfer has no data phase
  899. Status = UsbIo->UsbControlTransfer (
  900. UsbIo,
  901. &DevReq,
  902. EfiUsbDataOut,
  903. WDR_TIMEOUT,
  904. &ConfigurationValue,
  905. 1,
  906. &ReturnValue
  907. );
  908. if (EFI_ERROR (Status)) {
  909. goto StatusError;
  910. }
  911. return Status;
  912. StatusError:
  913. if ((Status != EFI_INVALID_PARAMETER) ||
  914. (Status != EFI_DEVICE_ERROR) ||
  915. (Status != EFI_UNSUPPORTED) ) {
  916. return EFI_DEVICE_ERROR;
  917. } else {
  918. return Status;
  919. }
  920. }
  921. /**
  922. Internal function that performs a Usb Control Transfer to set the Dtr value on
  923. the Usb Serial Device.
  924. @param UsbIo[in] Usb Io Protocol instance pointer
  925. @param DtrEnable[in] Data on the Enable/Disable status of the
  926. Dtr for the Usb Serial Device
  927. @retval EFI_SUCCESS The Dtr value was set on the Usb Serial
  928. Device
  929. @retval EFI_INVALID_PARAMETER An invalid Dtr value was used
  930. @retval EFI_UNSUPPORTED The operation is not supported
  931. @retval EFI_DEVICE_ERROR The device is not functioning correctly
  932. **/
  933. EFI_STATUS
  934. EFIAPI
  935. SetDtrInternal (
  936. IN EFI_USB_IO_PROTOCOL *UsbIo,
  937. IN BOOLEAN DtrEnable
  938. )
  939. {
  940. EFI_STATUS Status;
  941. EFI_USB_DEVICE_REQUEST DevReq;
  942. UINT32 ReturnValue;
  943. UINT8 ConfigurationValue;
  944. //
  945. // set the value of DevReq.Value based on the value of DtrEnable
  946. //
  947. if (!DtrEnable) {
  948. DevReq.Value = SET_DTR_LOW;
  949. }
  950. if (DtrEnable) {
  951. DevReq.Value = SET_DTR_HIGH;
  952. }
  953. //
  954. // set the remaining attributes of DevReq and perform the usb control transfer
  955. // to set the device
  956. //
  957. DevReq.Request = FTDI_COMMAND_MODEM_CTRL;
  958. DevReq.RequestType = USB_REQ_TYPE_VENDOR;
  959. DevReq.Index = FTDI_PORT_IDENTIFIER;
  960. DevReq.Length = 0; // indicates that there is no data phase in this transfer
  961. Status = UsbIo->UsbControlTransfer (
  962. UsbIo,
  963. &DevReq,
  964. EfiUsbDataOut,
  965. WDR_TIMEOUT,
  966. &ConfigurationValue,
  967. 1,
  968. &ReturnValue
  969. );
  970. if (EFI_ERROR (Status)) {
  971. goto StatusError;
  972. }
  973. return Status;
  974. StatusError:
  975. if ((Status != EFI_INVALID_PARAMETER) ||
  976. (Status != EFI_DEVICE_ERROR) ||
  977. (Status != EFI_UNSUPPORTED) ) {
  978. return EFI_DEVICE_ERROR;
  979. } else {
  980. return Status;
  981. }
  982. }
  983. /**
  984. Internal function that performs a Usb Control Transfer to set the Dtr value on
  985. the Usb Serial Device.
  986. @param UsbIo[in] Usb Io Protocol instance pointer
  987. @param RtsEnable[in] Data on the Enable/Disable status of the
  988. Rts for the Usb Serial Device
  989. @retval EFI_SUCCESS The Rts value was set on the Usb Serial
  990. Device
  991. @retval EFI_INVALID_PARAMETER An invalid Rts value was used
  992. @retval EFI_UNSUPPORTED The operation is not supported
  993. @retval EFI_DEVICE_ERROR The device is not functioning correctly
  994. **/
  995. EFI_STATUS
  996. EFIAPI
  997. SetRtsInternal (
  998. IN EFI_USB_IO_PROTOCOL *UsbIo,
  999. IN BOOLEAN RtsEnable
  1000. )
  1001. {
  1002. EFI_STATUS Status;
  1003. EFI_USB_DEVICE_REQUEST DevReq;
  1004. UINT32 ReturnValue;
  1005. UINT8 ConfigurationValue;
  1006. //
  1007. // set DevReq.Value based on the value of RtsEnable
  1008. //
  1009. if (!RtsEnable) {
  1010. DevReq.Value = SET_RTS_LOW;
  1011. }
  1012. if (RtsEnable) {
  1013. DevReq.Value = SET_RTS_HIGH;
  1014. }
  1015. //
  1016. // set the remaining parameters of DevReq and perform the usb control transfer
  1017. // to set the values on the device
  1018. //
  1019. DevReq.Request = FTDI_COMMAND_MODEM_CTRL;
  1020. DevReq.RequestType = USB_REQ_TYPE_VENDOR;
  1021. DevReq.Index = FTDI_PORT_IDENTIFIER;
  1022. DevReq.Length = 0; // indicates that there is no data phase in this request
  1023. Status = UsbIo->UsbControlTransfer (
  1024. UsbIo,
  1025. &DevReq,
  1026. EfiUsbDataOut,
  1027. WDR_TIMEOUT,
  1028. &ConfigurationValue,
  1029. 1,
  1030. &ReturnValue
  1031. );
  1032. if (EFI_ERROR (Status)) {
  1033. goto StatusError;
  1034. }
  1035. return Status;
  1036. StatusError:
  1037. if ((Status != EFI_INVALID_PARAMETER) ||
  1038. (Status != EFI_DEVICE_ERROR) ||
  1039. (Status != EFI_UNSUPPORTED) ) {
  1040. return EFI_DEVICE_ERROR;
  1041. } else {
  1042. return Status;
  1043. }
  1044. }
  1045. /**
  1046. Internal function that checks for valid control values and sets the control
  1047. bits on the Usb Serial Device.
  1048. @param UsbSerialDevice[in] Handle to the Usb Serial Device whose
  1049. control bits are being set
  1050. @param Control[in] The control value passed to the function
  1051. that contains the values of the control
  1052. bits that are being set
  1053. @retval EFI_SUCCESS The control bits were set on the Usb Serial
  1054. Device
  1055. @retval EFI_INVALID_PARAMETER An invalid control value was encountered
  1056. @retval EFI_EFI_UNSUPPORTED The operation is not supported
  1057. @retval EFI_DEVICE_ERROR The device is not functioning correctly
  1058. **/
  1059. EFI_STATUS
  1060. EFIAPI
  1061. SetControlBitsInternal (
  1062. IN USB_SER_DEV *UsbSerialDevice,
  1063. IN CONTROL_BITS *Control
  1064. )
  1065. {
  1066. EFI_STATUS Status;
  1067. UART_FLOW_CONTROL_DEVICE_PATH *FlowControl;
  1068. EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
  1069. //
  1070. // check for invalid control parameters hardware and software loopback enabled
  1071. // must always be set to FALSE
  1072. //
  1073. Control->HardwareLoopBack = FALSE;
  1074. Control->SoftwareLoopBack = FALSE;
  1075. //
  1076. // set hardware flow control
  1077. //
  1078. Status = SetFlowControlInternal (
  1079. UsbSerialDevice->UsbIo,
  1080. Control->HardwareFlowControl
  1081. );
  1082. if (EFI_ERROR (Status)) {
  1083. goto StatusError;
  1084. }
  1085. //
  1086. // set Dtr state
  1087. //
  1088. Status = SetDtrInternal (UsbSerialDevice->UsbIo, Control->DtrState);
  1089. if (EFI_ERROR (Status)) {
  1090. goto StatusError;
  1091. }
  1092. //
  1093. // set Rts state
  1094. //
  1095. Status = SetRtsInternal (UsbSerialDevice->UsbIo, Control->RtsState);
  1096. if (EFI_ERROR (Status)){
  1097. goto StatusError;
  1098. }
  1099. //
  1100. // update the remaining control values for UsbSerialDevice->ControlValues
  1101. //
  1102. UsbSerialDevice->ControlValues.DtrState = Control->DtrState;
  1103. UsbSerialDevice->ControlValues.RtsState = Control->RtsState;
  1104. UsbSerialDevice->ControlValues.HardwareFlowControl = Control->HardwareFlowControl;
  1105. UsbSerialDevice->ControlValues.HardwareLoopBack = FALSE;
  1106. UsbSerialDevice->ControlValues.SoftwareLoopBack = FALSE;
  1107. Status = EFI_SUCCESS;
  1108. //
  1109. // Update the device path to have the correct flow control values
  1110. //
  1111. if (UsbSerialDevice->ControllerHandle != NULL) {
  1112. RemainingDevicePath = UsbSerialDevice->DevicePath;
  1113. while (!IsDevicePathEnd (RemainingDevicePath)) {
  1114. FlowControl = (UART_FLOW_CONTROL_DEVICE_PATH *) NextDevicePathNode (RemainingDevicePath);
  1115. if (FlowControl->Header.Type == MESSAGING_DEVICE_PATH &&
  1116. FlowControl->Header.SubType == MSG_VENDOR_DP &&
  1117. sizeof (UART_FLOW_CONTROL_DEVICE_PATH) == DevicePathNodeLength ((EFI_DEVICE_PATH *) FlowControl)){
  1118. if (UsbSerialDevice->ControlValues.HardwareFlowControl == TRUE) {
  1119. FlowControl->FlowControlMap = UART_FLOW_CONTROL_HARDWARE;
  1120. } else if (UsbSerialDevice->ControlValues.HardwareFlowControl == FALSE) {
  1121. FlowControl->FlowControlMap = 0;
  1122. }
  1123. break;
  1124. }
  1125. RemainingDevicePath = NextDevicePathNode (RemainingDevicePath);
  1126. }
  1127. }
  1128. return Status;
  1129. StatusError:
  1130. if ((Status != EFI_INVALID_PARAMETER) ||
  1131. (Status != EFI_DEVICE_ERROR) ||
  1132. (Status != EFI_UNSUPPORTED) ) {
  1133. return EFI_DEVICE_ERROR;
  1134. } else {
  1135. return Status;
  1136. }
  1137. }
  1138. /**
  1139. Internal function that calculates the Control value used by GetControlBits()
  1140. based on the status and control values of the Usb Serial Device.
  1141. @param UsbSerialDevice[in] Handle to the Usb Serial Devie whose status
  1142. and control values are being used to set
  1143. Control
  1144. @param Control[out] On output the formated value of Control
  1145. that has been calculated based on the
  1146. control and status values of the Usb Serial
  1147. Device
  1148. @retval EFI_SUCCESS The value of Control was successfully
  1149. calculated
  1150. **/
  1151. EFI_STATUS
  1152. EFIAPI
  1153. GetControlBitsInternal (
  1154. IN USB_SER_DEV *UsbSerialDevice,
  1155. OUT UINT32 *Control
  1156. )
  1157. {
  1158. *Control = 0;
  1159. //
  1160. // Check the values of UsbSerialDevice->Status Values and modify control
  1161. // accordingly these values correspond to the modem status register
  1162. //
  1163. if (UsbSerialDevice->StatusValues.CtsState) {
  1164. *Control |= EFI_SERIAL_CLEAR_TO_SEND;
  1165. }
  1166. if (UsbSerialDevice->StatusValues.DsrState) {
  1167. *Control |= EFI_SERIAL_DATA_SET_READY;
  1168. }
  1169. if (UsbSerialDevice->StatusValues.RiState) {
  1170. *Control |= EFI_SERIAL_RING_INDICATE;
  1171. }
  1172. if (UsbSerialDevice->StatusValues.SdState) {
  1173. *Control |= EFI_SERIAL_CARRIER_DETECT;
  1174. }
  1175. //
  1176. // check the values of UsbSerialDevice->ControlValues and modify control
  1177. // accordingly these values correspond to the values of the Modem Control
  1178. // Register
  1179. //
  1180. if (UsbSerialDevice->ControlValues.DtrState) {
  1181. *Control |= EFI_SERIAL_DATA_TERMINAL_READY;
  1182. }
  1183. if (UsbSerialDevice->ControlValues.RtsState) {
  1184. *Control |= EFI_SERIAL_REQUEST_TO_SEND;
  1185. }
  1186. if (UsbSerialDevice->ControlValues.HardwareLoopBack) {
  1187. *Control |= EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE;
  1188. }
  1189. if (UsbSerialDevice->ControlValues.HardwareFlowControl) {
  1190. *Control |= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;
  1191. }
  1192. //
  1193. // check if the buffer is empty since only one is being used if it is empty
  1194. // set both the receive and transmit buffers to empty
  1195. //
  1196. if (UsbSerialDevice->DataBufferHead == UsbSerialDevice->DataBufferTail) {
  1197. *Control |= EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
  1198. *Control |= EFI_SERIAL_INPUT_BUFFER_EMPTY;
  1199. }
  1200. //
  1201. // check for software loopback enable in UsbSerialDevice->ControlValues
  1202. //
  1203. if (UsbSerialDevice->ControlValues.SoftwareLoopBack) {
  1204. *Control |= EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE;
  1205. }
  1206. return EFI_SUCCESS;
  1207. }
  1208. /**
  1209. Resets the USB Serial Device
  1210. This function is the internal method for resetting the device and is called by
  1211. SerialReset()
  1212. @param UsbSerialDevice[in] A pointer to the USB Serial device
  1213. @retval EFI_SUCCESS The device was reset
  1214. @retval EFI_DEVICE_ERROR The device could not be reset
  1215. **/
  1216. EFI_STATUS
  1217. EFIAPI
  1218. ResetInternal (
  1219. IN USB_SER_DEV *UsbSerialDevice
  1220. )
  1221. {
  1222. EFI_STATUS Status;
  1223. EFI_USB_DEVICE_REQUEST DevReq;
  1224. UINT8 ConfigurationValue;
  1225. UINT32 ReturnValue;
  1226. DevReq.Request = FTDI_COMMAND_RESET_PORT;
  1227. DevReq.RequestType = USB_REQ_TYPE_VENDOR;
  1228. DevReq.Value = RESET_PORT_PURGE_RX;
  1229. DevReq.Index = FTDI_PORT_IDENTIFIER;
  1230. DevReq.Length = 0; //indicates that there is not data phase in this request
  1231. Status = UsbSerialDevice->UsbIo->UsbControlTransfer (
  1232. UsbSerialDevice->UsbIo,
  1233. &DevReq,
  1234. EfiUsbDataIn,
  1235. WDR_TIMEOUT,
  1236. &ConfigurationValue,
  1237. 1,
  1238. &ReturnValue
  1239. );
  1240. if (EFI_ERROR (Status)) {
  1241. return EFI_DEVICE_ERROR;
  1242. }
  1243. DevReq.Request = FTDI_COMMAND_RESET_PORT;
  1244. DevReq.RequestType = USB_REQ_TYPE_VENDOR;
  1245. DevReq.Value = RESET_PORT_PURGE_TX;
  1246. DevReq.Index = FTDI_PORT_IDENTIFIER;
  1247. DevReq.Length = 0; //indicates that there is no data phase in this request
  1248. Status = UsbSerialDevice->UsbIo->UsbControlTransfer (
  1249. UsbSerialDevice->UsbIo,
  1250. &DevReq,
  1251. EfiUsbDataIn,
  1252. WDR_TIMEOUT,
  1253. &ConfigurationValue,
  1254. 1,
  1255. &ReturnValue
  1256. );
  1257. if (EFI_ERROR (Status)) {
  1258. return EFI_DEVICE_ERROR;
  1259. }
  1260. return Status;
  1261. }
  1262. /**
  1263. Entrypoint of USB Serial Driver.
  1264. This function is the entrypoint of USB Serial Driver. It installs
  1265. Driver Binding Protocols together with Component Name Protocols.
  1266. @param ImageHandle[in] The firmware allocated handle for the EFI image.
  1267. @param SystemTable[in] A pointer to the EFI System Table.
  1268. @retval EFI_SUCCESS The entry point is executed successfully.
  1269. **/
  1270. EFI_STATUS
  1271. EFIAPI
  1272. FtdiUsbSerialEntryPoint (
  1273. IN EFI_HANDLE ImageHandle,
  1274. IN EFI_SYSTEM_TABLE *SystemTable
  1275. )
  1276. {
  1277. EFI_STATUS Status;
  1278. Status = EfiLibInstallDriverBindingComponentName2 (
  1279. ImageHandle,
  1280. SystemTable,
  1281. &gUsbSerialDriverBinding,
  1282. ImageHandle,
  1283. &gUsbSerialComponentName,
  1284. &gUsbSerialComponentName2
  1285. );
  1286. ASSERT_EFI_ERROR (Status);
  1287. return EFI_SUCCESS;
  1288. }
  1289. /**
  1290. Unload function for the Usb Serial Driver.
  1291. @param ImageHandle[in] The allocated handle for the EFI image
  1292. @retval EFI_SUCCESS The driver was unloaded successfully
  1293. **/
  1294. EFI_STATUS
  1295. EFIAPI
  1296. FtdiUsbSerialUnload (
  1297. IN EFI_HANDLE ImageHandle
  1298. )
  1299. {
  1300. EFI_STATUS Status;
  1301. EFI_HANDLE *HandleBuffer;
  1302. UINTN HandleCount;
  1303. UINTN Index;
  1304. //
  1305. // Retrieve all handles in the handle database
  1306. //
  1307. Status = gBS->LocateHandleBuffer (
  1308. AllHandles,
  1309. NULL,
  1310. NULL,
  1311. &HandleCount,
  1312. &HandleBuffer
  1313. );
  1314. if (EFI_ERROR (Status)) {
  1315. return Status;
  1316. }
  1317. //
  1318. // Disconnect the driver from the handles in the handle database
  1319. //
  1320. for (Index = 0; Index < HandleCount; Index++) {
  1321. Status = gBS->DisconnectController (
  1322. HandleBuffer[Index],
  1323. gImageHandle,
  1324. NULL
  1325. );
  1326. }
  1327. //
  1328. // Free the handle array
  1329. //
  1330. FreePool (HandleBuffer);
  1331. //
  1332. // Uninstall protocols installed by the driver in its entrypoint
  1333. //
  1334. Status = gBS->UninstallMultipleProtocolInterfaces (
  1335. ImageHandle,
  1336. &gEfiDriverBindingProtocolGuid,
  1337. &gUsbSerialDriverBinding,
  1338. &gEfiComponentNameProtocolGuid,
  1339. &gUsbSerialComponentName,
  1340. &gEfiComponentName2ProtocolGuid,
  1341. &gUsbSerialComponentName2,
  1342. NULL
  1343. );
  1344. if (EFI_ERROR (Status)) {
  1345. return Status;
  1346. }
  1347. return EFI_SUCCESS;
  1348. }
  1349. /**
  1350. Check whether USB Serial driver supports this device.
  1351. @param This[in] The USB Serial driver binding protocol.
  1352. @param Controller[in] The controller handle to check.
  1353. @param RemainingDevicePath[in] The remaining device path.
  1354. @retval EFI_SUCCESS The driver supports this controller.
  1355. @retval other This device isn't supported.
  1356. **/
  1357. EFI_STATUS
  1358. EFIAPI
  1359. UsbSerialDriverBindingSupported (
  1360. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  1361. IN EFI_HANDLE Controller,
  1362. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  1363. )
  1364. {
  1365. EFI_STATUS Status;
  1366. EFI_USB_IO_PROTOCOL *UsbIo;
  1367. UART_DEVICE_PATH *UartNode;
  1368. UART_FLOW_CONTROL_DEVICE_PATH *FlowControlNode;
  1369. UINTN Index;
  1370. UINTN EntryCount;
  1371. EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;
  1372. BOOLEAN HasFlowControl;
  1373. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  1374. EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
  1375. if (RemainingDevicePath != NULL) {
  1376. if (!IsDevicePathEnd (RemainingDevicePath)) {
  1377. Status = EFI_UNSUPPORTED;
  1378. UartNode = (UART_DEVICE_PATH *) NextDevicePathNode (RemainingDevicePath);
  1379. if (UartNode->Header.Type != MESSAGING_DEVICE_PATH ||
  1380. UartNode->Header.SubType != MSG_UART_DP ||
  1381. sizeof (UART_DEVICE_PATH) != DevicePathNodeLength ((EFI_DEVICE_PATH *) UartNode)) {
  1382. goto Error;
  1383. }
  1384. FlowControlNode = (UART_FLOW_CONTROL_DEVICE_PATH *) NextDevicePathNode (UartNode);
  1385. if ((ReadUnaligned32 (&FlowControlNode->FlowControlMap) & ~UART_FLOW_CONTROL_HARDWARE) != 0) {
  1386. goto Error;
  1387. }
  1388. }
  1389. }
  1390. //
  1391. // Check if USB I/O Protocol is attached on the controller handle.
  1392. //
  1393. Status = gBS->OpenProtocol (
  1394. Controller,
  1395. &gEfiUsbIoProtocolGuid,
  1396. (VOID **) &UsbIo,
  1397. This->DriverBindingHandle,
  1398. Controller,
  1399. EFI_OPEN_PROTOCOL_BY_DRIVER
  1400. );
  1401. if (Status == EFI_ALREADY_STARTED) {
  1402. if (RemainingDevicePath == NULL || IsDevicePathEnd (RemainingDevicePath)) {
  1403. return EFI_SUCCESS;
  1404. }
  1405. Status = gBS->OpenProtocolInformation (
  1406. Controller,
  1407. &gEfiUsbIoProtocolGuid,
  1408. &OpenInfoBuffer,
  1409. &EntryCount
  1410. );
  1411. if (EFI_ERROR (Status)) {
  1412. return Status;
  1413. }
  1414. for (Index = 0; Index < EntryCount; Index++) {
  1415. if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
  1416. Status = gBS->OpenProtocol (
  1417. OpenInfoBuffer[Index].ControllerHandle,
  1418. &gEfiDevicePathProtocolGuid,
  1419. (VOID **) &DevicePath,
  1420. This->DriverBindingHandle,
  1421. Controller,
  1422. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  1423. );
  1424. if (!EFI_ERROR (Status)) {
  1425. HasFlowControl = ContainsFlowControl (RemainingDevicePath);
  1426. if (HasFlowControl ^ ContainsFlowControl (DevicePath)) {
  1427. Status = EFI_UNSUPPORTED;
  1428. }
  1429. }
  1430. break;
  1431. }
  1432. }
  1433. FreePool (OpenInfoBuffer);
  1434. return Status;
  1435. }
  1436. if (EFI_ERROR (Status)) {
  1437. return Status;
  1438. }
  1439. gBS->CloseProtocol (
  1440. Controller,
  1441. &gEfiUsbIoProtocolGuid,
  1442. This->DriverBindingHandle,
  1443. Controller
  1444. );
  1445. Status = gBS->OpenProtocol (
  1446. Controller,
  1447. &gEfiDevicePathProtocolGuid,
  1448. (VOID **) &ParentDevicePath,
  1449. This->DriverBindingHandle,
  1450. Controller,
  1451. EFI_OPEN_PROTOCOL_BY_DRIVER
  1452. );
  1453. if (Status == EFI_ALREADY_STARTED) {
  1454. return EFI_SUCCESS;
  1455. }
  1456. if (EFI_ERROR (Status)) {
  1457. return Status;
  1458. }
  1459. //
  1460. // Use the USB I/O Protocol interface to check whether Controller is
  1461. // a USB Serial device that can be managed by this driver.
  1462. //
  1463. Status = EFI_SUCCESS;
  1464. if (!IsUsbSerial (UsbIo)) {
  1465. Status = EFI_UNSUPPORTED;
  1466. goto Error;
  1467. }
  1468. Error:
  1469. gBS->CloseProtocol (
  1470. Controller,
  1471. &gEfiDevicePathProtocolGuid,
  1472. This->DriverBindingHandle,
  1473. Controller
  1474. );
  1475. return Status;
  1476. }
  1477. /**
  1478. Starts the USB Serial device with this driver.
  1479. This function produces initializes the USB Serial device and
  1480. produces the Serial IO Protocol.
  1481. @param This[in] The USB Serial driver binding instance.
  1482. @param Controller[in] Handle of device to bind driver to.
  1483. @param RemainingDevicePath[in] Optional parameter use to pick a specific
  1484. child device to start.
  1485. @retval EFI_SUCCESS The controller is controlled by the usb USB
  1486. Serial driver.
  1487. @retval EFI_UNSUPPORTED No interrupt endpoint can be found.
  1488. @retval Other This controller cannot be started.
  1489. **/
  1490. EFI_STATUS
  1491. EFIAPI
  1492. UsbSerialDriverBindingStart (
  1493. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  1494. IN EFI_HANDLE Controller,
  1495. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  1496. )
  1497. {
  1498. EFI_STATUS Status;
  1499. EFI_USB_IO_PROTOCOL *UsbIo;
  1500. USB_SER_DEV *UsbSerialDevice;
  1501. UINT8 EndpointNumber;
  1502. EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;
  1503. UINT8 Index;
  1504. BOOLEAN FoundIn;
  1505. BOOLEAN FoundOut;
  1506. EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
  1507. EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;
  1508. UINTN EntryCount;
  1509. EFI_SERIAL_IO_PROTOCOL *SerialIo;
  1510. UART_DEVICE_PATH *Uart;
  1511. UART_FLOW_CONTROL_DEVICE_PATH *FlowControl;
  1512. UINT32 Control;
  1513. EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
  1514. UsbSerialDevice = AllocateZeroPool (sizeof (USB_SER_DEV));
  1515. ASSERT (UsbSerialDevice != NULL);
  1516. //
  1517. // Get the Parent Device path
  1518. //
  1519. Status = gBS->OpenProtocol (
  1520. Controller,
  1521. &gEfiDevicePathProtocolGuid,
  1522. (VOID **) &ParentDevicePath,
  1523. This->DriverBindingHandle,
  1524. Controller,
  1525. EFI_OPEN_PROTOCOL_BY_DRIVER
  1526. );
  1527. if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
  1528. goto ErrorExit1;
  1529. }
  1530. //
  1531. // Open USB I/O Protocol
  1532. //
  1533. Status = gBS->OpenProtocol (
  1534. Controller,
  1535. &gEfiUsbIoProtocolGuid,
  1536. (VOID **) &UsbIo,
  1537. This->DriverBindingHandle,
  1538. Controller,
  1539. EFI_OPEN_PROTOCOL_BY_DRIVER
  1540. );
  1541. if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
  1542. goto ErrorExit1;
  1543. }
  1544. if (Status == EFI_ALREADY_STARTED) {
  1545. if (RemainingDevicePath == NULL || IsDevicePathEnd (RemainingDevicePath)) {
  1546. FreePool (UsbSerialDevice);
  1547. return EFI_SUCCESS;
  1548. }
  1549. //
  1550. // Check to see if a child handle exists
  1551. //
  1552. Status = gBS->OpenProtocolInformation (
  1553. Controller,
  1554. &gEfiSerialIoProtocolGuid,
  1555. &OpenInfoBuffer,
  1556. &EntryCount
  1557. );
  1558. if (EFI_ERROR (Status)) {
  1559. goto ErrorExit1;
  1560. }
  1561. Status = EFI_ALREADY_STARTED;
  1562. for (Index = 0; Index < EntryCount; Index++) {
  1563. if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
  1564. Status = gBS->OpenProtocol (
  1565. OpenInfoBuffer[Index].ControllerHandle,
  1566. &gEfiSerialIoProtocolGuid,
  1567. (VOID **) &SerialIo,
  1568. This->DriverBindingHandle,
  1569. Controller,
  1570. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  1571. );
  1572. if (EFI_ERROR (Status)) {
  1573. }
  1574. if (!EFI_ERROR (Status)) {
  1575. Uart = (UART_DEVICE_PATH *) RemainingDevicePath;
  1576. Status = SerialIo->SetAttributes (
  1577. SerialIo,
  1578. Uart->BaudRate,
  1579. SerialIo->Mode->ReceiveFifoDepth,
  1580. SerialIo->Mode->Timeout,
  1581. (EFI_PARITY_TYPE) Uart->Parity,
  1582. Uart->DataBits,
  1583. (EFI_STOP_BITS_TYPE) Uart->StopBits
  1584. );
  1585. FlowControl = (UART_FLOW_CONTROL_DEVICE_PATH *) NextDevicePathNode (Uart);
  1586. if (!EFI_ERROR (Status) && IsUartFlowControlNode (FlowControl)) {
  1587. Status = SerialIo->GetControl (
  1588. SerialIo,
  1589. &Control
  1590. );
  1591. if (!EFI_ERROR (Status)) {
  1592. if (ReadUnaligned32 (&FlowControl->FlowControlMap) == UART_FLOW_CONTROL_HARDWARE) {
  1593. Control |= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;
  1594. } else {
  1595. Control &= ~EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;
  1596. }
  1597. //
  1598. // Clear bits that are not allowed to be passed to SetControl
  1599. //
  1600. Control &= (EFI_SERIAL_REQUEST_TO_SEND |
  1601. EFI_SERIAL_DATA_TERMINAL_READY |
  1602. EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE |
  1603. EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE |
  1604. EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE);
  1605. Status = SerialIo->SetControl (SerialIo, Control);
  1606. }
  1607. }
  1608. }
  1609. break;
  1610. }
  1611. }
  1612. FreePool (OpenInfoBuffer);
  1613. return Status;
  1614. }
  1615. if (RemainingDevicePath != NULL) {
  1616. if (IsDevicePathEnd (RemainingDevicePath)) {
  1617. return EFI_SUCCESS;
  1618. }
  1619. }
  1620. UsbSerialDevice->UsbIo = UsbIo;
  1621. //
  1622. // Get interface & endpoint descriptor
  1623. //
  1624. UsbIo->UsbGetInterfaceDescriptor (
  1625. UsbIo,
  1626. &UsbSerialDevice->InterfaceDescriptor
  1627. );
  1628. EndpointNumber = UsbSerialDevice->InterfaceDescriptor.NumEndpoints;
  1629. //
  1630. // Traverse endpoints to find the IN and OUT endpoints that will send and
  1631. // receive data.
  1632. //
  1633. FoundIn = FALSE;
  1634. FoundOut = FALSE;
  1635. for (Index = 0; Index < EndpointNumber; Index++) {
  1636. Status = UsbIo->UsbGetEndpointDescriptor (
  1637. UsbIo,
  1638. Index,
  1639. &EndpointDescriptor
  1640. );
  1641. if (EFI_ERROR (Status)) {
  1642. return Status;
  1643. }
  1644. if (EndpointDescriptor.EndpointAddress == FTDI_ENDPOINT_ADDRESS_OUT) {
  1645. //
  1646. // Set the Out endpoint device
  1647. //
  1648. CopyMem (
  1649. &UsbSerialDevice->OutEndpointDescriptor,
  1650. &EndpointDescriptor,
  1651. sizeof(EndpointDescriptor)
  1652. );
  1653. FoundOut = TRUE;
  1654. }
  1655. if (EndpointDescriptor.EndpointAddress == FTDI_ENDPOINT_ADDRESS_IN) {
  1656. //
  1657. // Set the In endpoint device
  1658. //
  1659. CopyMem (
  1660. &UsbSerialDevice->InEndpointDescriptor,
  1661. &EndpointDescriptor,
  1662. sizeof(EndpointDescriptor)
  1663. );
  1664. FoundIn = TRUE;
  1665. }
  1666. }
  1667. if (!FoundIn || !FoundOut) {
  1668. //
  1669. // No interrupt endpoint found, then return unsupported.
  1670. //
  1671. Status = EFI_UNSUPPORTED;
  1672. goto ErrorExit;
  1673. }
  1674. //
  1675. // set the initial values of UsbSerialDevice->LastSettings to the default
  1676. // values
  1677. //
  1678. UsbSerialDevice->LastSettings.BaudRate = 115200;
  1679. UsbSerialDevice->LastSettings.DataBits = 8;
  1680. UsbSerialDevice->LastSettings.Parity = NoParity;
  1681. UsbSerialDevice->LastSettings.ReceiveFifoDepth = FTDI_MAX_RECEIVE_FIFO_DEPTH;
  1682. UsbSerialDevice->LastSettings.StopBits = OneStopBit;
  1683. UsbSerialDevice->LastSettings.Timeout = FTDI_TIMEOUT;
  1684. //
  1685. // set the initial values of UsbSerialDevice->ControlValues
  1686. //
  1687. UsbSerialDevice->ControlValues.DtrState = FALSE;
  1688. UsbSerialDevice->ControlValues.RtsState = FALSE;
  1689. UsbSerialDevice->ControlValues.HardwareFlowControl = FALSE;
  1690. UsbSerialDevice->ControlValues.HardwareLoopBack = FALSE;
  1691. UsbSerialDevice->ControlValues.SoftwareLoopBack = FALSE;
  1692. //
  1693. // set the values of UsbSerialDevice->UartDevicePath
  1694. //
  1695. UsbSerialDevice->UartDevicePath.Header.Type = MESSAGING_DEVICE_PATH;
  1696. UsbSerialDevice->UartDevicePath.Header.SubType = MSG_UART_DP;
  1697. UsbSerialDevice->UartDevicePath.Header.Length[0] = (UINT8) (sizeof (UART_DEVICE_PATH));
  1698. UsbSerialDevice->UartDevicePath.Header.Length[1] = (UINT8) ((sizeof (UART_DEVICE_PATH)) >> 8);
  1699. //
  1700. // set the values of UsbSerialDevice->FlowControlDevicePath
  1701. UsbSerialDevice->FlowControlDevicePath.Header.Type = MESSAGING_DEVICE_PATH;
  1702. UsbSerialDevice->FlowControlDevicePath.Header.SubType = MSG_VENDOR_DP;
  1703. UsbSerialDevice->FlowControlDevicePath.Header.Length[0] = (UINT8) (sizeof (UART_FLOW_CONTROL_DEVICE_PATH));
  1704. UsbSerialDevice->FlowControlDevicePath.Header.Length[1] = (UINT8) ((sizeof (UART_FLOW_CONTROL_DEVICE_PATH)) >> 8);
  1705. UsbSerialDevice->FlowControlDevicePath.FlowControlMap = 0;
  1706. Status = SetAttributesInternal (
  1707. UsbSerialDevice,
  1708. UsbSerialDevice->LastSettings.BaudRate,
  1709. UsbSerialDevice->LastSettings.ReceiveFifoDepth,
  1710. UsbSerialDevice->LastSettings.Timeout,
  1711. UsbSerialDevice->LastSettings.Parity,
  1712. UsbSerialDevice->LastSettings.DataBits,
  1713. UsbSerialDevice->LastSettings.StopBits
  1714. );
  1715. ASSERT_EFI_ERROR (Status);
  1716. Status = SetControlBitsInternal (
  1717. UsbSerialDevice,
  1718. &(UsbSerialDevice->ControlValues)
  1719. );
  1720. ASSERT_EFI_ERROR (Status);
  1721. //
  1722. // Publish Serial GUID and protocol
  1723. //
  1724. UsbSerialDevice->Signature = USB_SER_DEV_SIGNATURE;
  1725. UsbSerialDevice->SerialIo.Reset = SerialReset;
  1726. UsbSerialDevice->SerialIo.SetControl = SetControlBits;
  1727. UsbSerialDevice->SerialIo.SetAttributes = SetAttributes;
  1728. UsbSerialDevice->SerialIo.GetControl = GetControlBits;
  1729. UsbSerialDevice->SerialIo.Read = ReadSerialIo;
  1730. UsbSerialDevice->SerialIo.Write = WriteSerialIo;
  1731. //
  1732. // Set the static Serial IO modes that will display when running
  1733. // "sermode" within the UEFI shell.
  1734. //
  1735. UsbSerialDevice->SerialIo.Mode->Timeout = 0;
  1736. UsbSerialDevice->SerialIo.Mode->BaudRate = 115200;
  1737. UsbSerialDevice->SerialIo.Mode->DataBits = 8;
  1738. UsbSerialDevice->SerialIo.Mode->Parity = 1;
  1739. UsbSerialDevice->SerialIo.Mode->StopBits = 1;
  1740. UsbSerialDevice->ParentDevicePath = ParentDevicePath;
  1741. UsbSerialDevice->ControllerHandle = NULL;
  1742. FlowControl = NULL;
  1743. //
  1744. // Allocate space for the receive buffer
  1745. //
  1746. UsbSerialDevice->DataBuffer = AllocateZeroPool (SW_FIFO_DEPTH);
  1747. //
  1748. // Initialize data buffer pointers.
  1749. // Head==Tail = true means buffer is empty.
  1750. //
  1751. UsbSerialDevice->DataBufferHead = 0;
  1752. UsbSerialDevice->DataBufferTail = 0;
  1753. UsbSerialDevice->ControllerNameTable = NULL;
  1754. AddUnicodeString2 (
  1755. "eng",
  1756. gUsbSerialComponentName.SupportedLanguages,
  1757. &UsbSerialDevice->ControllerNameTable,
  1758. L"FTDI USB Serial Adapter",
  1759. TRUE
  1760. );
  1761. AddUnicodeString2 (
  1762. "en",
  1763. gUsbSerialComponentName2.SupportedLanguages,
  1764. &UsbSerialDevice->ControllerNameTable,
  1765. L"FTDI USB Serial Adapter",
  1766. FALSE
  1767. );
  1768. Status = SetInitialStatus (UsbSerialDevice);
  1769. ASSERT_EFI_ERROR (Status);
  1770. //
  1771. // Create a polling loop to check for input
  1772. //
  1773. gBS->CreateEvent (
  1774. EVT_TIMER | EVT_NOTIFY_SIGNAL,
  1775. TPL_CALLBACK,
  1776. UsbSerialDriverCheckInput,
  1777. UsbSerialDevice,
  1778. &(UsbSerialDevice->PollingLoop)
  1779. );
  1780. //
  1781. // add code to set trigger time based on baud rate
  1782. // setting to 0.5s for now
  1783. //
  1784. gBS->SetTimer (
  1785. UsbSerialDevice->PollingLoop,
  1786. TimerPeriodic,
  1787. EFI_TIMER_PERIOD_MILLISECONDS (500)
  1788. );
  1789. //
  1790. // Check if the remaining device path is null. If it is not null change the settings
  1791. // of the device to match those on the device path
  1792. //
  1793. if (RemainingDevicePath != NULL) {
  1794. CopyMem (
  1795. &UsbSerialDevice->UartDevicePath,
  1796. RemainingDevicePath,
  1797. sizeof (UART_DEVICE_PATH)
  1798. );
  1799. FlowControl = (UART_FLOW_CONTROL_DEVICE_PATH *) NextDevicePathNode (RemainingDevicePath);
  1800. if (IsUartFlowControlNode (FlowControl)) {
  1801. UsbSerialDevice->FlowControlDevicePath.FlowControlMap = ReadUnaligned32 (&FlowControl->FlowControlMap);
  1802. } else {
  1803. FlowControl = NULL;
  1804. }
  1805. }
  1806. //
  1807. // Build the device path by appending the UART node to the parent device path
  1808. //
  1809. UsbSerialDevice->DevicePath = AppendDevicePathNode (
  1810. ParentDevicePath,
  1811. (EFI_DEVICE_PATH_PROTOCOL *) &UsbSerialDevice->UartDevicePath
  1812. );
  1813. //
  1814. // Continue building the device path by appending the flow control node
  1815. //
  1816. TempDevicePath = UsbSerialDevice->DevicePath;
  1817. UsbSerialDevice->DevicePath = AppendDevicePathNode (
  1818. TempDevicePath,
  1819. (EFI_DEVICE_PATH_PROTOCOL *) &UsbSerialDevice->FlowControlDevicePath
  1820. );
  1821. FreePool (TempDevicePath);
  1822. if (UsbSerialDevice->DevicePath == NULL) {
  1823. Status = EFI_OUT_OF_RESOURCES;
  1824. goto ErrorExit;
  1825. }
  1826. //
  1827. // Install protocol interfaces for the device
  1828. //
  1829. Status = gBS->InstallMultipleProtocolInterfaces (
  1830. &UsbSerialDevice->ControllerHandle,
  1831. &gEfiDevicePathProtocolGuid,
  1832. UsbSerialDevice->DevicePath,
  1833. &gEfiSerialIoProtocolGuid,
  1834. &UsbSerialDevice->SerialIo,
  1835. NULL
  1836. );
  1837. if (EFI_ERROR (Status)){
  1838. goto ErrorExit;
  1839. }
  1840. //
  1841. // Open for child device
  1842. //
  1843. Status = gBS->OpenProtocol (
  1844. Controller,
  1845. &gEfiUsbIoProtocolGuid,
  1846. (VOID **) &UsbIo,
  1847. This->DriverBindingHandle,
  1848. UsbSerialDevice->ControllerHandle,
  1849. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  1850. );
  1851. UsbSerialDevice->Shutdown = FALSE;
  1852. return EFI_SUCCESS;
  1853. ErrorExit:
  1854. //
  1855. // Error handler
  1856. //
  1857. Status = gBS->UninstallMultipleProtocolInterfaces (
  1858. Controller,
  1859. &gEfiSerialIoProtocolGuid,
  1860. &UsbSerialDevice->SerialIo,
  1861. NULL
  1862. );
  1863. if (EFI_ERROR (Status)) {
  1864. goto ErrorExit1;
  1865. }
  1866. FreePool (UsbSerialDevice->DataBuffer);
  1867. FreePool (UsbSerialDevice);
  1868. UsbSerialDevice = NULL;
  1869. gBS->CloseProtocol (
  1870. Controller,
  1871. &gEfiUsbIoProtocolGuid,
  1872. This->DriverBindingHandle,
  1873. Controller
  1874. );
  1875. ErrorExit1:
  1876. return Status;
  1877. }
  1878. /**
  1879. Stop the USB Serial device handled by this driver.
  1880. @param This[in] The USB Serial driver binding protocol.
  1881. @param Controller[in] The controller to release.
  1882. @param NumberOfChildren[in] The number of handles in ChildHandleBuffer.
  1883. @param ChildHandleBuffer[in] The array of child handle.
  1884. @retval EFI_SUCCESS The device was stopped.
  1885. @retval EFI_UNSUPPORTED Serial IO Protocol is not installed on
  1886. Controller.
  1887. @retval EFI_DEVICE_ERROR The device could not be stopped due to a
  1888. device error.
  1889. @retval Others Fail to uninstall protocols attached on the
  1890. device.
  1891. **/
  1892. EFI_STATUS
  1893. EFIAPI
  1894. UsbSerialDriverBindingStop (
  1895. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  1896. IN EFI_HANDLE Controller,
  1897. IN UINTN NumberOfChildren,
  1898. IN EFI_HANDLE *ChildHandleBuffer
  1899. )
  1900. {
  1901. EFI_STATUS Status;
  1902. EFI_SERIAL_IO_PROTOCOL *SerialIo;
  1903. EFI_USB_IO_PROTOCOL *UsbIo;
  1904. USB_SER_DEV *UsbSerialDevice;
  1905. UINTN Index;
  1906. BOOLEAN AllChildrenStopped;
  1907. Status = EFI_SUCCESS;
  1908. UsbSerialDevice = NULL;
  1909. if (NumberOfChildren == 0) {
  1910. //
  1911. // Close the driver
  1912. //
  1913. Status = gBS->CloseProtocol (
  1914. Controller,
  1915. &gEfiUsbIoProtocolGuid,
  1916. This->DriverBindingHandle,
  1917. Controller
  1918. );
  1919. Status = gBS->CloseProtocol (
  1920. Controller,
  1921. &gEfiDevicePathProtocolGuid,
  1922. This->DriverBindingHandle,
  1923. Controller
  1924. );
  1925. return Status;
  1926. }
  1927. AllChildrenStopped = TRUE;
  1928. for (Index = 0; Index < NumberOfChildren ;Index++) {
  1929. Status = gBS->OpenProtocol (
  1930. ChildHandleBuffer[Index],
  1931. &gEfiSerialIoProtocolGuid,
  1932. (VOID **) &SerialIo,
  1933. This->DriverBindingHandle,
  1934. Controller,
  1935. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  1936. );
  1937. if (Status == EFI_SUCCESS) {//!EFI_ERROR (Status)) {
  1938. UsbSerialDevice = USB_SER_DEV_FROM_THIS (SerialIo);
  1939. Status = gBS->CloseProtocol (
  1940. Controller,
  1941. &gEfiUsbIoProtocolGuid,
  1942. This->DriverBindingHandle,
  1943. ChildHandleBuffer[Index]
  1944. );
  1945. Status = gBS->UninstallMultipleProtocolInterfaces (
  1946. ChildHandleBuffer[Index],
  1947. &gEfiDevicePathProtocolGuid,
  1948. UsbSerialDevice->DevicePath,
  1949. &gEfiSerialIoProtocolGuid,
  1950. &UsbSerialDevice->SerialIo,
  1951. NULL
  1952. );
  1953. if (EFI_ERROR (Status)) {
  1954. gBS->OpenProtocol (
  1955. Controller,
  1956. &gEfiUsbIoProtocolGuid,
  1957. (VOID **) &UsbIo,
  1958. This->DriverBindingHandle,
  1959. ChildHandleBuffer[Index],
  1960. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  1961. );
  1962. } else {
  1963. if (UsbSerialDevice->DevicePath != NULL) {
  1964. gBS->FreePool (UsbSerialDevice->DevicePath);
  1965. }
  1966. gBS->SetTimer (
  1967. UsbSerialDevice->PollingLoop,
  1968. TimerCancel,
  1969. 0
  1970. );
  1971. gBS->CloseEvent (UsbSerialDevice->PollingLoop);
  1972. UsbSerialDevice->Shutdown = TRUE;
  1973. FreeUnicodeStringTable (UsbSerialDevice->ControllerNameTable);
  1974. FreePool (UsbSerialDevice->DataBuffer);
  1975. FreePool (UsbSerialDevice);
  1976. }
  1977. }
  1978. if (EFI_ERROR (Status)) {
  1979. AllChildrenStopped = FALSE;
  1980. }
  1981. }
  1982. if (!AllChildrenStopped) {
  1983. return EFI_DEVICE_ERROR;
  1984. }
  1985. return EFI_SUCCESS;
  1986. }
  1987. //
  1988. // Serial IO Member Functions
  1989. //
  1990. /**
  1991. Reset the serial device.
  1992. @param This[in] Protocol instance pointer.
  1993. @retval EFI_SUCCESS The device was reset.
  1994. @retval EFI_DEVICE_ERROR The serial device could not be reset.
  1995. **/
  1996. EFI_STATUS
  1997. EFIAPI
  1998. SerialReset (
  1999. IN EFI_SERIAL_IO_PROTOCOL *This
  2000. )
  2001. {
  2002. EFI_STATUS Status;
  2003. USB_SER_DEV *UsbSerialDevice;
  2004. UsbSerialDevice = USB_SER_DEV_FROM_THIS (This);
  2005. Status = ResetInternal (UsbSerialDevice);
  2006. if (EFI_ERROR (Status)){
  2007. return EFI_DEVICE_ERROR;
  2008. }
  2009. return Status;
  2010. }
  2011. /**
  2012. Set the control bits on a serial device.
  2013. @param This[in] Protocol instance pointer.
  2014. @param Control[in] Set the bits of Control that are settable.
  2015. @retval EFI_SUCCESS The new control bits were set on the serial device.
  2016. @retval EFI_UNSUPPORTED The serial device does not support this operation.
  2017. @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
  2018. **/
  2019. EFI_STATUS
  2020. EFIAPI
  2021. SetControlBits (
  2022. IN EFI_SERIAL_IO_PROTOCOL *This,
  2023. IN UINT32 Control
  2024. )
  2025. {
  2026. EFI_STATUS Status;
  2027. USB_SER_DEV *UsbSerialDevice;
  2028. CONTROL_BITS ControlBits;
  2029. UsbSerialDevice = USB_SER_DEV_FROM_THIS (This);
  2030. //
  2031. // check for invalid control parameters
  2032. //
  2033. if ((Control & (~(EFI_SERIAL_REQUEST_TO_SEND |
  2034. EFI_SERIAL_DATA_TERMINAL_READY |
  2035. EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE |
  2036. EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE |
  2037. EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE))) != 0 ) {
  2038. return EFI_UNSUPPORTED;
  2039. }
  2040. //
  2041. // check the control parameters and set the correct setting for
  2042. // the paramerts of ControlBits
  2043. // both loopback enables are always set to FALSE
  2044. //
  2045. ControlBits.HardwareLoopBack = FALSE;
  2046. ControlBits.SoftwareLoopBack = FALSE;
  2047. //
  2048. // check for hardware flow control
  2049. //
  2050. if ((Control & EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE) == EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE) {
  2051. ControlBits.HardwareFlowControl = TRUE;
  2052. } else {
  2053. ControlBits.HardwareFlowControl = FALSE;
  2054. }
  2055. //
  2056. // check for DTR enabled
  2057. //
  2058. if ((Control & EFI_SERIAL_DATA_TERMINAL_READY) == EFI_SERIAL_DATA_TERMINAL_READY) {
  2059. ControlBits.DtrState = TRUE;
  2060. } else {
  2061. ControlBits.DtrState = FALSE;
  2062. }
  2063. //
  2064. // check for RTS enabled
  2065. //
  2066. if ((Control & EFI_SERIAL_REQUEST_TO_SEND) == EFI_SERIAL_REQUEST_TO_SEND) {
  2067. ControlBits.RtsState = TRUE;
  2068. } else {
  2069. ControlBits.RtsState = FALSE;
  2070. }
  2071. //
  2072. // set the control values with a call to SetControlBitsInternal()
  2073. //
  2074. Status = SetControlBitsInternal (UsbSerialDevice, &ControlBits);
  2075. return Status;
  2076. }
  2077. /**
  2078. calls SetAttributesInternal() to set the baud rate, receive FIFO depth,
  2079. transmit/receive time out, parity, data buts, and stop bits on a serial
  2080. device.
  2081. @param This[in] Protocol instance pointer.
  2082. @param BaudRate[in] The requested baud rate. A BaudRate value of 0
  2083. will use the device's default interface speed.
  2084. @param ReveiveFifoDepth[in] The requested depth of the FIFO on the receive
  2085. side of the serial interface. A ReceiveFifoDepth
  2086. value of 0 will use the device's default FIFO
  2087. depth.
  2088. @param Timeout[in] The requested time out for a single character in
  2089. microseconds.This timeout applies to both the
  2090. transmit and receive side of the interface. A
  2091. Timeout value of 0 will use the device's default
  2092. time out value.
  2093. @param Parity[in] The type of parity to use on this serial device.
  2094. A Parity value of DefaultParity will use the
  2095. device's default parity value.
  2096. @param DataBits[in] The number of data bits to use on the serial
  2097. device. A DataBit vaule of 0 will use the
  2098. device's default data bit setting.
  2099. @param StopBits[in] The number of stop bits to use on this serial
  2100. device. A StopBits value of DefaultStopBits will
  2101. use the device's default number of stop bits.
  2102. @retval EFI_SUCCESS The attributes were set
  2103. @retval EFI_DEVICE_ERROR The attributes were not able to be
  2104. **/
  2105. EFI_STATUS
  2106. EFIAPI
  2107. SetAttributes (
  2108. IN EFI_SERIAL_IO_PROTOCOL *This,
  2109. IN UINT64 BaudRate,
  2110. IN UINT32 ReceiveFifoDepth,
  2111. IN UINT32 Timeout,
  2112. IN EFI_PARITY_TYPE Parity,
  2113. IN UINT8 DataBits,
  2114. IN EFI_STOP_BITS_TYPE StopBits
  2115. )
  2116. {
  2117. EFI_STATUS Status;
  2118. USB_SER_DEV *UsbSerialDevice;
  2119. UsbSerialDevice = USB_SER_DEV_FROM_THIS (This);
  2120. Status = SetAttributesInternal (
  2121. UsbSerialDevice,
  2122. BaudRate,
  2123. ReceiveFifoDepth,
  2124. Timeout,
  2125. Parity,
  2126. DataBits,
  2127. StopBits
  2128. );
  2129. if (EFI_ERROR (Status)) {
  2130. return Status;
  2131. }
  2132. return Status;
  2133. }
  2134. /**
  2135. Retrieves the status of the control bits on a serial device.
  2136. @param This[in] Protocol instance pointer.
  2137. @param Control[out] A pointer to return the current Control signals
  2138. from the serial device.
  2139. @retval EFI_SUCCESS The control bits were read from the serial
  2140. device.
  2141. @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
  2142. **/
  2143. EFI_STATUS
  2144. EFIAPI
  2145. GetControlBits (
  2146. IN EFI_SERIAL_IO_PROTOCOL *This,
  2147. OUT UINT32 *Control
  2148. )
  2149. {
  2150. USB_SER_DEV *UsbSerialDevice;
  2151. EFI_STATUS Status;
  2152. UsbSerialDevice = USB_SER_DEV_FROM_THIS (This);
  2153. *Control = 0;
  2154. Status = GetControlBitsInternal (UsbSerialDevice, Control);
  2155. if (EFI_ERROR (Status)) {
  2156. return EFI_DEVICE_ERROR;
  2157. }
  2158. return Status;
  2159. }
  2160. /**
  2161. Reads data from a serial device.
  2162. @param This[in] Protocol instance pointer.
  2163. @param BufferSize[in, out] On input, the size of the Buffer. On output,
  2164. the amount of data returned in Buffer.
  2165. @param Buffer[out] The buffer to return the data into.
  2166. @retval EFI_SUCCESS The data was read.
  2167. @retval EFI_DEVICE_ERROR The device reported an error.
  2168. @retval EFI_TIMEOUT The data write was stopped due to a timeout.
  2169. **/
  2170. EFI_STATUS
  2171. EFIAPI
  2172. ReadSerialIo (
  2173. IN EFI_SERIAL_IO_PROTOCOL *This,
  2174. IN OUT UINTN *BufferSize,
  2175. OUT VOID *Buffer
  2176. )
  2177. {
  2178. UINTN Index;
  2179. UINTN RemainingCallerBufferSize;
  2180. USB_SER_DEV *UsbSerialDevice;
  2181. EFI_STATUS Status;
  2182. if (*BufferSize == 0) {
  2183. return EFI_SUCCESS;
  2184. }
  2185. if (Buffer == NULL) {
  2186. return EFI_DEVICE_ERROR;
  2187. }
  2188. Status = EFI_SUCCESS;
  2189. UsbSerialDevice = USB_SER_DEV_FROM_THIS (This);
  2190. //
  2191. // Clear out any data that we already have in our internal buffer
  2192. //
  2193. for (Index = 0; Index < *BufferSize; Index++) {
  2194. if (UsbSerialDevice->DataBufferHead == UsbSerialDevice->DataBufferTail) {
  2195. break;
  2196. }
  2197. //
  2198. // Still have characters in the buffer to return
  2199. //
  2200. ((UINT8 *)Buffer)[Index] = UsbSerialDevice->DataBuffer[UsbSerialDevice->DataBufferHead];
  2201. UsbSerialDevice->DataBufferHead = (UsbSerialDevice->DataBufferHead + 1) % SW_FIFO_DEPTH;
  2202. }
  2203. //
  2204. // If we haven't filled the caller's buffer using data that we already had on
  2205. // hand We need to generate an additional USB request to try and fill the
  2206. // caller's buffer
  2207. //
  2208. if (Index != *BufferSize) {
  2209. RemainingCallerBufferSize = *BufferSize - Index;
  2210. Status = ReadDataFromUsb (
  2211. UsbSerialDevice,
  2212. &RemainingCallerBufferSize,
  2213. (VOID *)(((CHAR8 *)Buffer) + Index)
  2214. );
  2215. if (!EFI_ERROR (Status)) {
  2216. *BufferSize = RemainingCallerBufferSize + Index;
  2217. } else {
  2218. *BufferSize = Index;
  2219. }
  2220. }
  2221. if (UsbSerialDevice->DataBufferHead == UsbSerialDevice->DataBufferTail) {
  2222. //
  2223. // Data buffer has no data, set the EFI_SERIAL_INPUT_BUFFER_EMPTY flag
  2224. //
  2225. UsbSerialDevice->ControlBits |= EFI_SERIAL_INPUT_BUFFER_EMPTY;
  2226. } else {
  2227. //
  2228. // There is some leftover data, clear EFI_SERIAL_INPUT_BUFFER_EMPTY flag
  2229. //
  2230. UsbSerialDevice->ControlBits &= ~(EFI_SERIAL_INPUT_BUFFER_EMPTY);
  2231. }
  2232. return Status;
  2233. }
  2234. /**
  2235. Writes data to a serial device.
  2236. @param This[in] Protocol instance pointer.
  2237. @param BufferSize[in, out] On input, the size of the Buffer. On output,
  2238. the amount of data actually written.
  2239. @param Buffer[in] The buffer of data to write
  2240. @retval EFI_SUCCESS The data was written.
  2241. @retval EFI_DEVICE_ERROR The device reported an error.
  2242. @retval EFI_TIMEOUT The data write was stopped due to a timeout.
  2243. **/
  2244. EFI_STATUS
  2245. EFIAPI
  2246. WriteSerialIo (
  2247. IN EFI_SERIAL_IO_PROTOCOL *This,
  2248. IN OUT UINTN *BufferSize,
  2249. IN VOID *Buffer
  2250. )
  2251. {
  2252. EFI_STATUS Status;
  2253. USB_SER_DEV *UsbSerialDevice;
  2254. EFI_TPL Tpl;
  2255. UsbSerialDevice = USB_SER_DEV_FROM_THIS (This);
  2256. if (UsbSerialDevice->Shutdown) {
  2257. return EFI_DEVICE_ERROR;
  2258. }
  2259. Tpl = gBS->RaiseTPL (TPL_NOTIFY);
  2260. Status = UsbSerialDataTransfer (
  2261. UsbSerialDevice,
  2262. EfiUsbDataOut,
  2263. Buffer,
  2264. BufferSize,
  2265. FTDI_TIMEOUT
  2266. );
  2267. gBS->RestoreTPL (Tpl);
  2268. if (EFI_ERROR (Status)) {
  2269. if (Status == EFI_TIMEOUT){
  2270. return Status;
  2271. } else {
  2272. return EFI_DEVICE_ERROR;
  2273. }
  2274. }
  2275. return EFI_SUCCESS;
  2276. }