common.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/serial.h>
  19. #include <grub/usbserial.h>
  20. #include <grub/dl.h>
  21. GRUB_MOD_LICENSE ("GPLv3+");
  22. void
  23. grub_usbserial_fini (struct grub_serial_port *port)
  24. {
  25. port->usbdev->config[port->configno].interf[port->interfno].detach_hook = 0;
  26. port->usbdev->config[port->configno].interf[port->interfno].attached = 0;
  27. }
  28. void
  29. grub_usbserial_detach (grub_usb_device_t usbdev, int configno, int interfno)
  30. {
  31. static struct grub_serial_port *port;
  32. port = usbdev->config[configno].interf[interfno].detach_data;
  33. grub_serial_unregister (port);
  34. }
  35. static int usbnum = 0;
  36. int
  37. grub_usbserial_attach (grub_usb_device_t usbdev, int configno, int interfno,
  38. struct grub_serial_driver *driver, int in_endp,
  39. int out_endp)
  40. {
  41. struct grub_serial_port *port;
  42. int j;
  43. struct grub_usb_desc_if *interf;
  44. grub_usb_err_t err = GRUB_USB_ERR_NONE;
  45. interf = usbdev->config[configno].interf[interfno].descif;
  46. port = grub_zalloc (sizeof (*port));
  47. if (!port)
  48. {
  49. grub_print_error ();
  50. return 0;
  51. }
  52. port->name = grub_xasprintf ("usb%d", usbnum++);
  53. if (!port->name)
  54. {
  55. grub_free (port);
  56. grub_print_error ();
  57. return 0;
  58. }
  59. port->usbdev = usbdev;
  60. port->driver = driver;
  61. for (j = 0; j < interf->endpointcnt; j++)
  62. {
  63. struct grub_usb_desc_endp *endp;
  64. endp = &usbdev->config[0].interf[interfno].descendp[j];
  65. if ((endp->endp_addr & 128) && (endp->attrib & 3) == 2
  66. && (in_endp == GRUB_USB_SERIAL_ENDPOINT_LAST_MATCHING
  67. || in_endp == endp->endp_addr))
  68. {
  69. /* Bulk IN endpoint. */
  70. port->in_endp = endp;
  71. }
  72. else if (!(endp->endp_addr & 128) && (endp->attrib & 3) == 2
  73. && (out_endp == GRUB_USB_SERIAL_ENDPOINT_LAST_MATCHING
  74. || out_endp == endp->endp_addr))
  75. {
  76. /* Bulk OUT endpoint. */
  77. port->out_endp = endp;
  78. }
  79. }
  80. /* Configure device */
  81. if (port->out_endp && port->in_endp)
  82. err = grub_usb_set_configuration (usbdev, configno + 1);
  83. if (!port->out_endp || !port->in_endp || err)
  84. {
  85. grub_free (port->name);
  86. grub_free (port);
  87. return 0;
  88. }
  89. port->configno = configno;
  90. port->interfno = interfno;
  91. grub_serial_config_defaults (port);
  92. grub_serial_register (port);
  93. port->usbdev->config[port->configno].interf[port->interfno].detach_hook
  94. = grub_usbserial_detach;
  95. port->usbdev->config[port->configno].interf[port->interfno].detach_data
  96. = port;
  97. return 1;
  98. }
  99. int
  100. grub_usbserial_fetch (struct grub_serial_port *port, grub_size_t header_size)
  101. {
  102. grub_usb_err_t err;
  103. grub_size_t actual;
  104. if (port->bufstart < port->bufend)
  105. return port->buf[port->bufstart++];
  106. err = grub_usb_bulk_read_extended (port->usbdev, port->in_endp,
  107. sizeof (port->buf), port->buf, 10,
  108. &actual);
  109. if (err != GRUB_USB_ERR_NONE)
  110. return -1;
  111. port->bufstart = header_size;
  112. port->bufend = actual;
  113. if (port->bufstart >= port->bufend)
  114. return -1;
  115. return port->buf[port->bufstart++];
  116. }