The Pedigree Project  0.1
FtdiSerialDevice.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "FtdiSerialDevice.h"
21 #include "modules/system/usb/UsbConstants.h"
22 #include "modules/system/usb/UsbDevice.h"
23 #include "pedigree/kernel/Log.h"
24 #include "pedigree/kernel/utilities/PointerGuard.h"
25 #include "pedigree/kernel/utilities/Vector.h"
26 #include "pedigree/kernel/utilities/new"
27 
28 #define FTDI_BAUD_RATE 9600
29 
30 static uint8_t nSubdivisors[8] = {0, 3, 2, 4, 1, 5, 6, 7};
31 
32 FtdiSerialDevice::FtdiSerialDevice(UsbDevice *dev)
33  : UsbDevice(dev), Serial(), m_pInEndpoint(0), m_pOutEndpoint(0)
34 {
35 }
36 
37 FtdiSerialDevice::~FtdiSerialDevice()
38 {
39 }
40 
42 {
43  // Reset the device
44  controlRequest(UsbRequestType::Vendor, 0, 0, 0);
45 
46  // Calculate the divisor and subdivisor for the baud rate
47  uint16_t nDivisor = (48000000 / 2) / FTDI_BAUD_RATE,
48  nSubdivisor = nSubdivisors[nDivisor % 8];
49  nDivisor /= 8;
50 
51  // Set the divisor and subdivisor (0x4138 / 0x00 for 9600)
52  controlRequest(
53  UsbRequestType::Vendor, 3, (nSubdivisor & 3) << 14 | nDivisor,
54  nSubdivisor >> 2);
55 
56  // Get the in and out endpoints
57  for (size_t i = 0; i < m_pInterface->endpointList.count(); i++)
58  {
59  Endpoint *pEndpoint = m_pInterface->endpointList[i];
60  if (!m_pInEndpoint && (pEndpoint->nTransferType == Endpoint::Bulk) &&
61  pEndpoint->bIn)
62  m_pInEndpoint = pEndpoint;
63  if (!m_pOutEndpoint && (pEndpoint->nTransferType == Endpoint::Bulk) &&
64  pEndpoint->bOut)
65  m_pOutEndpoint = pEndpoint;
66  if (m_pInEndpoint && m_pOutEndpoint)
67  break;
68  }
69 
70  if (!m_pInEndpoint)
71  {
72  ERROR("USB: FTDI: No IN endpoint");
73  return;
74  }
75 
76  if (!m_pOutEndpoint)
77  {
78  ERROR("USB: FTDI: No OUT endpoint");
79  return;
80  }
81 
82  m_UsbState = HasDriver;
83 }
84 
85 char FtdiSerialDevice::read()
86 {
87  char *pChar = new char(0);
88  PointerGuard<char> guard(pChar);
89  syncIn(m_pInEndpoint, reinterpret_cast<uintptr_t>(pChar), 1);
90  return *pChar;
91 }
92 
93 void FtdiSerialDevice::write(char c)
94 {
95  char *pChar = new char(c);
96  PointerGuard<char> guard(pChar);
97  syncOut(m_pOutEndpoint, reinterpret_cast<uintptr_t>(pChar), 1);
98 }
virtual void initialiseDriver()
Implemented by the driver class, initialises driver-specific stuff.
#define ERROR(text)
Definition: Log.h:82