usbh_printer.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @file usbh_printer.c
  3. * @brief
  4. *
  5. * Copyright (c) 2022 sakumisu
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #include "usbh_core.h"
  24. #include "usbh_printer.h"
  25. #define DEV_FORMAT "/dev/printer"
  26. static int usbh_printer_get_device_id(struct usbh_printer *printer_class, uint8_t *buffer)
  27. {
  28. struct usb_setup_packet *setup = printer_class->hport->setup;
  29. int ret;
  30. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  31. setup->bRequest = PRINTER_REQUEST_GET_DEVICE_ID;
  32. setup->wValue = 0;
  33. setup->wIndex = printer_class->intf;
  34. setup->wLength = 256;
  35. return usbh_control_transfer(printer_class->hport->ep0, setup, buffer);
  36. }
  37. static int usbh_printer_get_port_status(struct usbh_printer *printer_class, uint8_t *buffer)
  38. {
  39. struct usb_setup_packet *setup = printer_class->hport->setup;
  40. int ret;
  41. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  42. setup->bRequest = PRINTER_REQUEST_GET_PORT_SATTUS;
  43. setup->wValue = 0;
  44. setup->wIndex = printer_class->intf;
  45. setup->wLength = 1;
  46. return usbh_control_transfer(printer_class->hport->ep0, setup, buffer);
  47. }
  48. static int usbh_printer_soft_reset(struct usbh_printer *printer_class)
  49. {
  50. struct usb_setup_packet *setup = printer_class->hport->setup;
  51. int ret;
  52. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  53. setup->bRequest = PRINTER_REQUEST_SOFT_RESET;
  54. setup->wValue = 0;
  55. setup->wIndex = printer_class->intf;
  56. setup->wLength = 0;
  57. return usbh_control_transfer(printer_class->hport->ep0, setup, NULL);
  58. }
  59. static int usbh_printer_connect(struct usbh_hubport *hport, uint8_t intf)
  60. {
  61. struct usbh_endpoint_cfg ep_cfg = { 0 };
  62. struct usb_endpoint_descriptor *ep_desc;
  63. int ret;
  64. struct usbh_printer *printer_class = usb_malloc(sizeof(struct usbh_printer));
  65. if (printer_class == NULL) {
  66. USB_LOG_ERR("Fail to alloc printer_class\r\n");
  67. return -ENOMEM;
  68. }
  69. memset(printer_class, 0, sizeof(struct usbh_printer));
  70. printer_class->hport = hport;
  71. printer_class->intf = intf;
  72. hport->config.intf[intf].priv = printer_class;
  73. for (uint8_t i = 0; i < hport->config.intf[intf + 1].intf_desc.bNumEndpoints; i++) {
  74. ep_desc = &hport->config.intf[intf + 1].ep[i].ep_desc;
  75. ep_cfg.ep_addr = ep_desc->bEndpointAddress;
  76. ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
  77. ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
  78. ep_cfg.ep_interval = ep_desc->bInterval;
  79. ep_cfg.hport = hport;
  80. if (ep_desc->bEndpointAddress & 0x80) {
  81. usbh_ep_alloc(&printer_class->bulkin, &ep_cfg);
  82. } else {
  83. usbh_ep_alloc(&printer_class->bulkout, &ep_cfg);
  84. }
  85. }
  86. // uint8_t *device_id = usb_iomalloc(256);
  87. // ret = usbh_printer_get_device_id(printer_class, device_id);
  88. strncpy(hport->config.intf[intf].devname, DEV_FORMAT, CONFIG_USBHOST_DEV_NAMELEN);
  89. USB_LOG_INFO("Register Printer Class:%s\r\n", hport->config.intf[intf].devname);
  90. return ret;
  91. }
  92. static int usbh_printer_disconnect(struct usbh_hubport *hport, uint8_t intf)
  93. {
  94. int ret = 0;
  95. struct usbh_printer *printer_class = (struct usbh_printer *)hport->config.intf[intf].priv;
  96. if (printer_class) {
  97. if (printer_class->bulkin) {
  98. ret = usb_ep_cancel(printer_class->bulkin);
  99. if (ret < 0) {
  100. }
  101. usbh_ep_free(printer_class->bulkin);
  102. }
  103. if (printer_class->bulkout) {
  104. ret = usb_ep_cancel(printer_class->bulkout);
  105. if (ret < 0) {
  106. }
  107. usbh_ep_free(printer_class->bulkout);
  108. }
  109. usb_free(printer_class);
  110. if (hport->config.intf[intf].devname[0] != '\0')
  111. USB_LOG_INFO("Unregister Printer Class:%s\r\n", hport->config.intf[intf].devname);
  112. memset(hport->config.intf[intf].devname, 0, CONFIG_USBHOST_DEV_NAMELEN);
  113. hport->config.intf[intf].priv = NULL;
  114. }
  115. return ret;
  116. }
  117. static const struct usbh_class_driver printer_class_driver = {
  118. .driver_name = "printer",
  119. .connect = usbh_printer_connect,
  120. .disconnect = usbh_printer_disconnect
  121. };
  122. CLASS_INFO_DEFINE const struct usbh_class_info printer_class_info = {
  123. .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
  124. .class = USB_DEVICE_CLASS_PRINTER,
  125. .subclass = PRINTER_SUBCLASS,
  126. .protocol = 0x00,
  127. .vid = 0x00,
  128. .pid = 0x00,
  129. .class_driver = &printer_class_driver
  130. };