PageRenderTime 122ms CodeModel.GetById 22ms RepoModel.GetById 2ms app.codeStats 0ms

/drivers/staging/sbe-2t3e3/netdev.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 142 lines | 108 code | 23 blank | 11 comment | 14 complexity | 30ecbdc68c5b1f644e0d1d8b15323f4d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * SBE 2T3E3 synchronous serial card driver for Linux
  3. *
  4. * Copyright (C) 2009-2010 Krzysztof Halasa <khc@pm.waw.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation.
  9. *
  10. * This code is based on a driver written by SBE Inc.
  11. */
  12. #include <linux/capability.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/pci.h>
  18. #include <linux/hdlc.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/interrupt.h>
  21. #include "2t3e3.h"
  22. int t3e3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  23. {
  24. struct channel *sc = dev_to_priv(dev);
  25. int cmd_2t3e3, len, rlen;
  26. t3e3_param_t param;
  27. t3e3_resp_t resp;
  28. void *data = ifr->ifr_data + sizeof(cmd_2t3e3) + sizeof(len);
  29. if (cmd == SIOCWANDEV)
  30. return hdlc_ioctl(dev, ifr, cmd);
  31. if (!capable(CAP_SYS_ADMIN))
  32. return -EPERM;
  33. if (cmd != SIOCDEVPRIVATE + 15)
  34. return -EINVAL;
  35. if (copy_from_user(&cmd_2t3e3, ifr->ifr_data, sizeof(cmd_2t3e3)))
  36. return -EFAULT;
  37. if (copy_from_user(&len, ifr->ifr_data + sizeof(cmd_2t3e3), sizeof(len)))
  38. return -EFAULT;
  39. if (len > sizeof(param))
  40. return -EFAULT;
  41. if (len)
  42. if (copy_from_user(&param, data, len))
  43. return -EFAULT;
  44. t3e3_if_config(sc, cmd_2t3e3, (char *)&param, &resp, &rlen);
  45. if (rlen)
  46. if (copy_to_user(data, &resp, rlen))
  47. return -EFAULT;
  48. return 0;
  49. }
  50. static struct net_device_stats* t3e3_get_stats(struct net_device *dev)
  51. {
  52. struct net_device_stats *nstats = &dev->stats;
  53. struct channel *sc = dev_to_priv(dev);
  54. t3e3_stats_t *stats = &sc->s;
  55. memset(nstats, 0, sizeof(struct net_device_stats));
  56. nstats->rx_packets = stats->in_packets;
  57. nstats->tx_packets = stats->out_packets;
  58. nstats->rx_bytes = stats->in_bytes;
  59. nstats->tx_bytes = stats->out_bytes;
  60. nstats->rx_errors = stats->in_errors;
  61. nstats->tx_errors = stats->out_errors;
  62. nstats->rx_crc_errors = stats->in_error_crc;
  63. nstats->rx_dropped = stats->in_dropped;
  64. nstats->tx_dropped = stats->out_dropped;
  65. nstats->tx_carrier_errors = stats->out_error_lost_carr +
  66. stats->out_error_no_carr;
  67. return nstats;
  68. }
  69. int t3e3_open(struct net_device *dev)
  70. {
  71. struct channel *sc = dev_to_priv(dev);
  72. int ret = hdlc_open(dev);
  73. if (ret)
  74. return ret;
  75. sc->r.flags |= SBE_2T3E3_FLAG_NETWORK_UP;
  76. dc_start(dev_to_priv(dev));
  77. netif_start_queue(dev);
  78. try_module_get(THIS_MODULE);
  79. return 0;
  80. }
  81. int t3e3_close(struct net_device *dev)
  82. {
  83. struct channel *sc = dev_to_priv(dev);
  84. hdlc_close(dev);
  85. netif_stop_queue(dev);
  86. dc_stop(sc);
  87. sc->r.flags &= ~SBE_2T3E3_FLAG_NETWORK_UP;
  88. module_put(THIS_MODULE);
  89. return 0;
  90. }
  91. static int t3e3_attach(struct net_device *dev, unsigned short foo1,
  92. unsigned short foo2)
  93. {
  94. return 0;
  95. }
  96. static const struct net_device_ops t3e3_ops = {
  97. .ndo_open = t3e3_open,
  98. .ndo_stop = t3e3_close,
  99. .ndo_change_mtu = hdlc_change_mtu,
  100. .ndo_start_xmit = hdlc_start_xmit,
  101. .ndo_do_ioctl = t3e3_ioctl,
  102. .ndo_get_stats = t3e3_get_stats,
  103. };
  104. int setup_device(struct net_device *dev, struct channel *sc)
  105. {
  106. hdlc_device *hdlc = dev_to_hdlc(dev);
  107. int retval;
  108. dev->base_addr = pci_resource_start(sc->pdev, 0);
  109. dev->irq = sc->pdev->irq;
  110. dev->netdev_ops = &t3e3_ops;
  111. dev->tx_queue_len = 100;
  112. hdlc->xmit = t3e3_if_start_xmit;
  113. hdlc->attach = t3e3_attach;
  114. if ((retval = register_hdlc_device(dev))) {
  115. dev_err(&sc->pdev->dev, "error registering HDLC device\n");
  116. return retval;
  117. }
  118. return 0;
  119. }