ppp_hdlc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * Copyright (C) 2012 by egnite GmbH
  3. * Copyright (C) 2008 by Szemzo András
  4. * Copyright (C) 2003-2004 by egnite Software GmbH
  5. * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
  6. * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
  7. * Internet Initiative Japan, Inc (IIJ)
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file dev/ppp_hdlc.c
  38. * \brief Generic AHDLC driver for PPP.
  39. *
  40. * \verbatim
  41. * $Id$
  42. * \endverbatim
  43. */
  44. #include <dev/uart.h>
  45. #include <dev/ahdlc.h>
  46. #include <dev/ppp.h>
  47. #include <sys/event.h>
  48. #include <sys/timer.h>
  49. #include <sys/thread.h>
  50. #include <net/if_var.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <io.h>
  54. #include <fcntl.h>
  55. #include <dev/ppp_hdlc.h>
  56. /*!
  57. * \brief AHDLC receiver thread stack size.
  58. */
  59. #ifndef NUT_THREAD_AHDLCRXSTACK
  60. #define NUT_THREAD_AHDLCRXSTACK 1024
  61. #endif
  62. #define RXTHREADSTACK ((NUT_THREAD_AHDLCRXSTACK) * (NUT_THREAD_STACK_MULT) + (NUT_THREAD_STACK_ADD))
  63. /*!
  64. * \brief Check character against Async-Control-Character-Map.
  65. *
  66. * Checks the 32-bit ACCM to see if the byte needs un-escaping
  67. */
  68. #define IN_ACC_MAP(c, m) (( ((uint8_t) (c)) < 0x20) && ((m) & (1UL << (c))) != 0)
  69. /*
  70. * FCS lookup table.
  71. */
  72. static const uint16_t fcstab[256] = {
  73. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  74. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  75. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  76. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  77. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  78. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  79. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  80. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  81. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  82. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  83. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  84. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  85. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  86. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  87. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  88. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  89. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  90. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  91. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  92. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  93. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  94. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  95. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  96. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  97. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  98. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  99. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  100. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  101. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  102. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  103. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  104. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  105. };
  106. typedef struct _PPPHDLC_DCB PPPHDLC_DCB;
  107. struct _PPPHDLC_DCB {
  108. /* ! \brief Downlink device. */
  109. int dcb_fd;
  110. /*! \brief HDLC mode change event queue.
  111. */
  112. HANDLE dcb_mode_evt;
  113. /*! \brief Peer's Async-Control-Character-Map.
  114. */
  115. uint32_t dcb_rx_accm;
  116. /*! \brief Our Async-Control-Character-Map.
  117. */
  118. uint32_t dcb_tx_accm;
  119. /*! \brief Maximum receive and transmit MRU.
  120. */
  121. uint_fast16_t dcb_mru;
  122. };
  123. static PPPHDLC_DCB dcb_ppp0;
  124. static PPPHDLC_DCB dcb_ppp1;
  125. /*!
  126. * \brief Send byte to physical device.
  127. *
  128. * \return 0 on success, -1 in case of any errors.
  129. */
  130. static int INLINE PppHdlcSendByte(int fd, uint8_t ch, uint8_t flush)
  131. {
  132. if (_write(fd, &ch, 1) != 1)
  133. return -1;
  134. return 0;
  135. }
  136. /*
  137. * Characters are properly escaped and checksum is updated.
  138. *
  139. * \return 0 on success, -1 in case of any errors.
  140. */
  141. static int PppHdlcSend(PPPHDLC_DCB *dcb, const uint8_t *data, uint16_t len, uint16_t *txfcs)
  142. {
  143. uint16_t tbx;
  144. register uint16_t fcs;
  145. uint8_t ch;
  146. if (txfcs)
  147. fcs = *txfcs;
  148. else
  149. fcs = 0;
  150. while (len) {
  151. tbx = (uint16_t) ((uint8_t) fcs ^ *data);
  152. fcs >>= 8;
  153. fcs ^= fcstab[tbx];
  154. if (IN_ACC_MAP(*data, dcb->dcb_tx_accm) || *data == AHDLC_FLAG || *data == AHDLC_ESCAPE) {
  155. ch = AHDLC_ESCAPE;
  156. if (PppHdlcSendByte(dcb->dcb_fd, AHDLC_ESCAPE, 0))
  157. return -1;
  158. if (PppHdlcSendByte(dcb->dcb_fd, *data ^ AHDLC_TRANS, 0))
  159. if (_write(dcb->dcb_fd, &ch, 1) != 1)
  160. return -1;
  161. } else if (PppHdlcSendByte(dcb->dcb_fd, *data, 0))
  162. return -1;
  163. data++;
  164. len--;
  165. }
  166. if (txfcs)
  167. *txfcs = fcs;
  168. return 0;
  169. }
  170. /*!
  171. * \brief Send HDLC frame.
  172. *
  173. * \param dev Identifies the device to use.
  174. * \param nb Network buffer structure containing the packet to be sent.
  175. * The structure must have been allocated by a previous
  176. * call NutNetBufAlloc().
  177. *
  178. * \return 0 on success, -1 in case of any errors.
  179. */
  180. static int PppHdlcOutput(NUTDEVICE *dev, NETBUF *nb)
  181. {
  182. uint16_t txfcs;
  183. PPPHDLC_DCB *dcb = dev->dev_dcb;
  184. uint_fast16_t sz;
  185. /*
  186. * Calculate the number of bytes to be send. Do not
  187. * send packets larger than transmit mru.
  188. */
  189. sz = nb->nb_dl.sz + nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz;
  190. if (sz > dcb->dcb_mru) {
  191. return -1;
  192. }
  193. /* Send the starting flag. */
  194. PppHdlcSendByte(dcb->dcb_fd, AHDLC_FLAG, 0);
  195. /* Initialize the checksum and send the NETBUF. */
  196. txfcs = AHDLC_INITFCS;
  197. if (PppHdlcSend(dcb, nb->nb_dl.vp, nb->nb_dl.sz, &txfcs))
  198. return -1;
  199. if (PppHdlcSend(dcb, nb->nb_nw.vp, nb->nb_nw.sz, &txfcs))
  200. return -1;
  201. if (PppHdlcSend(dcb, nb->nb_tp.vp, nb->nb_tp.sz, &txfcs))
  202. return -1;
  203. if (PppHdlcSend(dcb, nb->nb_ap.vp, nb->nb_ap.sz, &txfcs))
  204. return -1;
  205. /* Send the checksum and the final flag. */
  206. txfcs ^= 0xffff;
  207. if (PppHdlcSend(dcb, (uint8_t *) & txfcs, 2, 0))
  208. return -1;
  209. PppHdlcSendByte(dcb->dcb_fd, AHDLC_FLAG, 1);
  210. return 0;
  211. }
  212. /*! \fn PppHdlcReceive(void *arg)
  213. * \brief Asynchronous HDLC receiver thread.
  214. *
  215. * Running at high priority.
  216. */
  217. THREAD(PppHdlcReceive, arg)
  218. {
  219. NUTDEVICE *dev = (NUTDEVICE *) arg;
  220. PPPHDLC_DCB *dcb = (PPPHDLC_DCB *) dev->dev_dcb;
  221. NUTDEVICE *netdev = (NUTDEVICE *) dev->dev_icb;
  222. IFNET *ifn = (IFNET *) netdev->dev_icb;
  223. uint_fast8_t ch;
  224. uint8_t *rd_buf;
  225. uint8_t *rd_ptr;
  226. uint8_t *rx_buf;
  227. uint8_t *rx_ptr;
  228. static const int rd_siz = 64;
  229. int rd_len = 0;
  230. uint16_t rx_fcs = AHDLC_INITFCS;
  231. int rx_cnt = 0;
  232. uint_fast8_t inframe = 0;
  233. uint_fast8_t escaped = 0;
  234. uint32_t tmo = 1000;
  235. rd_buf = malloc(rd_siz);
  236. rd_ptr = rd_buf;
  237. _ioctl(dcb->dcb_fd, UART_SETREADTIMEOUT, &tmo);
  238. dev->dev_type = IFTYP_NET;
  239. dcb->dcb_mru = ifn->if_mtu;
  240. rx_buf = malloc(dcb->dcb_mru + 2);
  241. rx_ptr = rx_buf;
  242. /* Signal the link driver that we are up. */
  243. ifn->if_send = PppHdlcOutput;
  244. netdev->dev_ioctl(netdev, LCP_LOWERUP, 0);
  245. NutEventPost(&dcb->dcb_mode_evt);
  246. for (;;) {
  247. /* Read next character from downlink. */
  248. while (rd_len == 0) {
  249. rd_ptr = rd_buf;
  250. rd_len = _read(dcb->dcb_fd, rd_buf, rd_siz);
  251. /* Check, if the net interface is still attached. */
  252. if (dev->dev_icb == NULL) {
  253. rd_len = -1;
  254. }
  255. }
  256. if (rd_len < 0) {
  257. /* Device error. */
  258. break;
  259. }
  260. ch = *rd_ptr++;
  261. rd_len--;
  262. if (inframe) {
  263. if (ch != AHDLC_FLAG) {
  264. if (ch == AHDLC_ESCAPE) {
  265. /* Escape next character. */
  266. escaped = 1;
  267. /* Jump to get next character. */
  268. continue;
  269. }
  270. if (escaped) {
  271. /* Last character was escape. */
  272. ch ^= AHDLC_TRANS;
  273. escaped = 0;
  274. }
  275. if (rx_cnt++ < dcb->dcb_mru + 2) {
  276. /* Update calculated checksum and store character in buffer. */
  277. uint16_t tbx = (uint16_t) ((uint8_t) rx_fcs ^ ch);
  278. rx_fcs >>= 8;
  279. rx_fcs ^= fcstab[tbx];
  280. *rx_ptr++ = ch;
  281. } else {
  282. /* Size overflow, skip this frame. */
  283. inframe = 0;
  284. }
  285. /* Jump to get next character. */
  286. continue;
  287. }
  288. /* Frame flag received, check length and checksum. */
  289. if (rx_cnt >= 2 && rx_fcs == AHDLC_GOODFCS) {
  290. NETBUF *nb;
  291. /* Frame is valid. Create a NETBUF and pass it to the
  292. network specific receive handler. */
  293. rx_cnt -= 2;
  294. nb = NutNetBufAlloc(NULL, NBAF_DATALINK, rx_cnt);
  295. if (nb) {
  296. memcpy(nb->nb_dl.vp, rx_buf, rx_cnt);
  297. (*ifn->if_recv) (netdev, nb);
  298. }
  299. }
  300. }
  301. /* If frame flag is received, re-sync frame processing. */
  302. if (ch == AHDLC_FLAG) {
  303. inframe = 1;
  304. escaped = 0;
  305. rx_ptr = rx_buf;
  306. rx_cnt = 0;
  307. rx_fcs = AHDLC_INITFCS;
  308. }
  309. }
  310. /* Signal the link driver that we are down. */
  311. netdev->dev_ioctl(netdev, LCP_LOWERDOWN, 0);
  312. dev->dev_type = IFTYP_CHAR;
  313. NutEventPost(&dcb->dcb_mode_evt);
  314. NutThreadExit();
  315. for (;;);
  316. }
  317. /*
  318. * \brief Attach or detach the network interface.
  319. *
  320. * \param dev PPP device.
  321. * \param netdev Network device.
  322. *
  323. * \return 0 on success, -1 otherwise.
  324. */
  325. static int PppHdlcAttach(NUTDEVICE *dev, NUTDEVICE *netdev)
  326. {
  327. PPPHDLC_DCB *dcb = (PPPHDLC_DCB *) dev->dev_dcb;
  328. if (netdev) {
  329. if (dev->dev_icb) {
  330. return -1;
  331. }
  332. dev->dev_icb = netdev;
  333. if (NutThreadCreate("ppphdlc", PppHdlcReceive, dev, RXTHREADSTACK) == NULL) {
  334. return -1;
  335. }
  336. }
  337. else if (dev->dev_icb == NULL) {
  338. return 0;
  339. }
  340. else {
  341. dev->dev_icb = NULL;
  342. }
  343. return NutEventWait(&dcb->dcb_mode_evt, 2000);
  344. }
  345. /*!
  346. * \brief Perform device specific control functions.
  347. *
  348. * \param dev Identifies the device that receives the device-control
  349. * function.
  350. * \param req Requested control function. The following functions are
  351. * handled by this device:
  352. * - \ref HDLC_SETIFNET
  353. * - \ref HDLC_GETIFNET
  354. * All other functions will be passed to the UART driver.
  355. * \param conf Points to a buffer that contains any data required for
  356. * the given control function or receives data from that
  357. * function.
  358. *
  359. * \return 0 on success, -1 otherwise.
  360. */
  361. static int PppHdlcIoCtl(NUTDEVICE *dev, int req, void *conf)
  362. {
  363. int rc = 0;
  364. PPPHDLC_DCB *dcb;
  365. dcb = (PPPHDLC_DCB *) dev->dev_dcb;
  366. switch (req) {
  367. case HDLC_SETIFNET:
  368. rc = PppHdlcAttach(dev, *(NUTDEVICE **) conf);
  369. break;
  370. case HDLC_GETIFNET:
  371. *(NUTDEVICE **)conf = (NUTDEVICE *) dev->dev_icb;
  372. break;
  373. case HDLC_SETTXACCM:
  374. dcb->dcb_tx_accm = *((uint32_t *) conf);
  375. break;
  376. case HDLC_GETTXACCM:
  377. *((uint32_t *) conf) = dcb->dcb_tx_accm;
  378. break;
  379. default:
  380. rc = _ioctl(((PPPHDLC_DCB *) (dev->dev_dcb))->dcb_fd, req, conf);
  381. break;
  382. }
  383. return rc;
  384. }
  385. /*!
  386. * \brief Initialize asynchronous HDLC device.
  387. *
  388. * This function will be called during device registration.
  389. *
  390. * \param dev Identifies the device to initialize.
  391. *
  392. * \return 0 on success, -1 otherwise.
  393. */
  394. static int PppHdlcInit(NUTDEVICE * dev)
  395. {
  396. return 0;
  397. }
  398. /*!
  399. * \brief Open the asynchronous HDLC device.
  400. *
  401. * This function is called internally when opening the PPP driver and
  402. * will in turn open the physical UART device. Therefore it is required,
  403. * that the UART driver has been registered before opening the PPP device.
  404. *
  405. * \param dev Pointer to the \ref NUTDEVICE structure.
  406. * \param name Ignored, should point to an empty string.
  407. * \param mode Operation mode, should be set to \ref _O_RDWR or-ed with
  408. * \ref _O_BINARY.
  409. * \param acc Ignored, should be zero.
  410. *
  411. * \return Pointer to a NUTFILE structure if successful or NUTFILE_EOF otherwise.
  412. */
  413. static NUTFILE *PppHdlcOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  414. {
  415. PPPHDLC_DCB *dcb;
  416. NUTFILE *nfp;
  417. dcb = (PPPHDLC_DCB *) dev->dev_dcb;
  418. /* Open the physical device. */
  419. dcb->dcb_fd = _open(dev->dev_name + 1, mode);
  420. if (dcb->dcb_fd == -1) {
  421. return NUTFILE_EOF;
  422. }
  423. /* Set transmit ACCM prior to negotiation. */
  424. dcb->dcb_tx_accm = 0xffffffff;
  425. /* Allocate a file structure to return. */
  426. nfp = calloc(1, sizeof(*nfp));
  427. if (nfp == NULL) {
  428. return NUTFILE_EOF;
  429. }
  430. nfp->nf_dev = dev;
  431. return nfp;
  432. }
  433. /*!
  434. * \brief Close the asynchronous HDLC device.
  435. *
  436. * This function is called internally when closing the PPP driver. It
  437. * will detach the network interface and close the UART device driver.
  438. *
  439. * \param nfp Pointer to a _NUTFILE structure, obtained by a previous call
  440. * to PppHdlcOpen().
  441. *
  442. * \return 0 on success or -1 otherwise.
  443. */
  444. static int PppHdlcClose(NUTFILE * nfp)
  445. {
  446. int rc;
  447. PppHdlcAttach(nfp->nf_dev, NULL);
  448. rc = _close(((PPPHDLC_DCB *) (nfp->nf_dev->dev_dcb))->dcb_fd);
  449. free(nfp);
  450. return rc;
  451. }
  452. /*!
  453. * \brief Read raw data from the asynchronous HDLC device.
  454. *
  455. * This call is simply passed to the UART driver.
  456. *
  457. * \param nfp Pointer to a \ref _NUTFILE structure, obtained by a
  458. * previous call to PppHdlcOpen().
  459. * \param buffer Pointer to the buffer that receives the data.
  460. * \param size Maximum number of bytes to read.
  461. *
  462. * \return The number of bytes read, which may be less than the number
  463. * of bytes specified. A return value of -1 indicates an error,
  464. * while zero is returned in case of a timeout.
  465. */
  466. static int PppHdlcRead(NUTFILE *nfp, void *buffer, int size)
  467. {
  468. return _read(((PPPHDLC_DCB *) (nfp->nf_dev->dev_dcb))->dcb_fd, buffer, size);
  469. }
  470. /*!
  471. * \brief Write raw data to the asynchronous HDLC device.
  472. *
  473. * This call is simply passed to the UART driver.
  474. *
  475. * \param nfp Pointer to a \ref _NUTFILE structure, obtained by a
  476. * previous call to PppHdlcOpen().
  477. * \param buffer Pointer the data to write.
  478. * \param len Number of data bytes to write.
  479. *
  480. * \return The number of bytes written, which may be less than the number
  481. * of bytes specified if a timeout occured. A return value of -1
  482. * indicates an error.
  483. */
  484. static int PppHdlcWrite(NUTFILE *nfp, const void *buffer, int len)
  485. {
  486. return _write(((PPPHDLC_DCB *) (nfp->nf_dev->dev_dcb))->dcb_fd, buffer, len);
  487. }
  488. NUTDEVICE devPppHdlc0 = {
  489. NULL, /* Pointer to next device, dev_next. */
  490. { 'l', 'u', 'a', 'r', 't', '0', 0, 0, 0 }, /* Hardware device name, dev_name. */
  491. IFTYP_CHAR, /* Type of device, dev_type. */
  492. 0, /* Base address, dev_base (not used). */
  493. 0, /* First interrupt number, dev_irq (not used). */
  494. NULL, /* Interface control block, dev_icb. */
  495. &dcb_ppp0, /* Driver control block, dev_dcb. */
  496. PppHdlcInit, /* Driver initialization routine, dev_init. */
  497. PppHdlcIoCtl, /* Driver specific control function, dev_ioctl. */
  498. PppHdlcRead, /* Read from device, dev_read. */
  499. PppHdlcWrite, /* Write to device, dev_write. */
  500. #ifdef __HARVARD_ARCH__
  501. NULL, /* Write data from program space to device, dev_write_P. */
  502. #endif
  503. PppHdlcOpen, /* Open a device or file, dev_open. */
  504. PppHdlcClose, /* Close a device or file, dev_close. */
  505. NULL, /* Request file size, dev_size. */
  506. NULL, /* Select function, optional, not yet implemented */
  507. };
  508. NUTDEVICE devPppHdlc1 = {
  509. NULL, /* Pointer to next device, dev_next. */
  510. { 'l', 'u', 'a', 'r', 't', '1', 0, 0, 0 }, /* Hardware device name, dev_name. */
  511. IFTYP_CHAR, /* Type of device, dev_type. */
  512. 0, /* Base address, dev_base (not used). */
  513. 0, /* First interrupt number, dev_irq (not used). */
  514. NULL, /* Interface control block, dev_icb. */
  515. &dcb_ppp1, /* Driver control block, dev_dcb. */
  516. PppHdlcInit, /* Driver initialization routine, dev_init. */
  517. PppHdlcIoCtl, /* Driver specific control function, dev_ioctl. */
  518. PppHdlcRead, /* Read from device, dev_read. */
  519. PppHdlcWrite, /* Write to device, dev_write. */
  520. #ifdef __HARVARD_ARCH__
  521. NULL, /* Write data from program space to device, dev_write_P. */
  522. #endif
  523. PppHdlcOpen, /* Open a device or file, dev_open. */
  524. PppHdlcClose, /* Close a device or file, dev_close. */
  525. NULL, /* Request file size, dev_size. */
  526. NULL, /* Select function, optional, not yet implemented */
  527. };