summaryrefslogtreecommitdiff
path: root/class/printer
diff options
context:
space:
mode:
authorsakumisu <[email protected]>2022-05-14 21:37:43 +0800
committersakumisu <[email protected]>2022-05-14 21:42:39 +0800
commit2ecde84a4ece1859f7d9e6c26f5c2001680b71e2 (patch)
tree0ba3a72cb36000e52ef3f96463d314627a8b4d53 /class/printer
parent8e0e772992125498e525ee12025dee65b7ca5afa (diff)
add initial printer、mtp、template class driver
Diffstat (limited to 'class/printer')
-rw-r--r--class/printer/usb_printer.h14
-rw-r--r--class/printer/usbd_printer.c91
-rw-r--r--class/printer/usbd_printer.h38
-rw-r--r--class/printer/usbh_printer.c156
-rw-r--r--class/printer/usbh_printer.h44
5 files changed, 343 insertions, 0 deletions
diff --git a/class/printer/usb_printer.h b/class/printer/usb_printer.h
new file mode 100644
index 00000000..8471d76a
--- /dev/null
+++ b/class/printer/usb_printer.h
@@ -0,0 +1,14 @@
+#ifndef _USB_PRINTER_H
+#define _USB_PRINTER_H
+
+#define PRINTER_SUBCLASS 0x01U
+
+#define PRINTER_REQUEST_GET_DEVICE_ID 0x00U
+#define PRINTER_REQUEST_GET_PORT_SATTUS 0x01U
+#define PRINTER_REQUEST_SOFT_RESET 0x02U
+
+#define PRINTER_STATUS_NO_ERROR 0x00U
+#define PRINTER_STATUS_SELECTED 0x08U
+#define PRINTER_STATUS_PAPER_EMPTY 0x10U
+
+#endif \ No newline at end of file
diff --git a/class/printer/usbd_printer.c b/class/printer/usbd_printer.c
new file mode 100644
index 00000000..fae89532
--- /dev/null
+++ b/class/printer/usbd_printer.c
@@ -0,0 +1,91 @@
+/**
+ * @file usbd_printer.h
+ * @brief
+ *
+ * Copyright (c) 2022 sakumisu
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+#include "usbd_core.h"
+#include "usbd_printer.h"
+
+/* Device data structure */
+struct printer_cfg_priv {
+ uint8_t *device_id;
+ uint8_t port_status;
+} usbd_printer_cfg;
+
+/**
+ * @brief Handler called for Class requests not handled by the USB stack.
+ *
+ * @param setup Information about the request to execute.
+ * @param len Size of the buffer.
+ * @param data Buffer containing the request result.
+ *
+ * @return 0 on success, negative errno code on fail.
+ */
+static int printer_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
+{
+ USB_LOG_DBG("Printer Class request: "
+ "bRequest 0x%02x\r\n",
+ setup->bRequest);
+
+ switch (setup->bRequest) {
+ case PRINTER_REQUEST_GET_DEVICE_ID:
+
+ break;
+ case PRINTER_REQUEST_GET_PORT_SATTUS:
+
+ break;
+ case PRINTER_REQUEST_SOFT_RESET:
+
+ break;
+ default:
+ USB_LOG_WRN("Unhandled Printer Class bRequest 0x%02x\r\n", setup->bRequest);
+ return -1;
+ }
+
+ return 0;
+}
+
+static void printer_notify_handler(uint8_t event, void *arg)
+{
+ switch (event) {
+ case USBD_EVENT_RESET:
+ break;
+
+ default:
+ break;
+ }
+}
+
+void usbd_printer_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
+{
+ static usbd_class_t *last_class = NULL;
+
+ if (last_class != devclass) {
+ last_class = devclass;
+ usbd_class_register(devclass);
+ }
+
+ intf->class_handler = printer_class_request_handler;
+ intf->custom_handler = NULL;
+ intf->vendor_handler = NULL;
+ intf->notify_handler = printer_notify_handler;
+
+ usbd_class_add_interface(devclass, intf);
+}
diff --git a/class/printer/usbd_printer.h b/class/printer/usbd_printer.h
new file mode 100644
index 00000000..c243609e
--- /dev/null
+++ b/class/printer/usbd_printer.h
@@ -0,0 +1,38 @@
+/**
+ * @file usbd_printer.h
+ * @brief
+ *
+ * Copyright (c) 2022 sakumisu
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+#ifndef _USBD_PRINTER_H
+#define _USBD_PRINTER_H
+
+#include "usb_printer.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void usbd_printer_add_interface(usbd_class_t *devclass, usbd_interface_t *intf);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif \ No newline at end of file
diff --git a/class/printer/usbh_printer.c b/class/printer/usbh_printer.c
new file mode 100644
index 00000000..b64da8d8
--- /dev/null
+++ b/class/printer/usbh_printer.c
@@ -0,0 +1,156 @@
+/**
+ * @file usbh_printer.c
+ * @brief
+ *
+ * Copyright (c) 2022 sakumisu
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+#include "usbh_core.h"
+#include "usbh_printer.h"
+
+#define DEV_FORMAT "/dev/printer"
+
+static int usbh_printer_get_device_id(struct usbh_printer *printer_class, uint8_t *buffer)
+{
+ struct usb_setup_packet *setup = printer_class->hport->setup;
+ int ret;
+
+ setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
+ setup->bRequest = PRINTER_REQUEST_GET_DEVICE_ID;
+ setup->wValue = 0;
+ setup->wIndex = printer_class->intf;
+ setup->wLength = 256;
+
+ return usbh_control_transfer(printer_class->hport->ep0, setup, buffer);
+}
+
+static int usbh_printer_get_port_status(struct usbh_printer *printer_class, uint8_t *buffer)
+{
+ struct usb_setup_packet *setup = printer_class->hport->setup;
+ int ret;
+
+ setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
+ setup->bRequest = PRINTER_REQUEST_GET_PORT_SATTUS;
+ setup->wValue = 0;
+ setup->wIndex = printer_class->intf;
+ setup->wLength = 1;
+
+ return usbh_control_transfer(printer_class->hport->ep0, setup, buffer);
+}
+
+static int usbh_printer_soft_reset(struct usbh_printer *printer_class)
+{
+ struct usb_setup_packet *setup = printer_class->hport->setup;
+ int ret;
+
+ setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
+ setup->bRequest = PRINTER_REQUEST_SOFT_RESET;
+ setup->wValue = 0;
+ setup->wIndex = printer_class->intf;
+ setup->wLength = 0;
+
+ return usbh_control_transfer(printer_class->hport->ep0, setup, NULL);
+}
+
+static int usbh_printer_connect(struct usbh_hubport *hport, uint8_t intf)
+{
+ struct usbh_endpoint_cfg ep_cfg = { 0 };
+ struct usb_endpoint_descriptor *ep_desc;
+ int ret;
+
+ struct usbh_printer *printer_class = usb_malloc(sizeof(struct usbh_printer));
+ if (printer_class == NULL) {
+ USB_LOG_ERR("Fail to alloc printer_class\r\n");
+ return -ENOMEM;
+ }
+
+ memset(printer_class, 0, sizeof(struct usbh_printer));
+
+ printer_class->hport = hport;
+ printer_class->intf = intf;
+
+ hport->config.intf[intf].priv = printer_class;
+ strncpy(hport->config.intf[intf].devname, DEV_FORMAT, CONFIG_USBHOST_DEV_NAMELEN);
+
+ for (uint8_t i = 0; i < hport->config.intf[intf + 1].intf_desc.bNumEndpoints; i++) {
+ ep_desc = &hport->config.intf[intf + 1].ep[i].ep_desc;
+
+ ep_cfg.ep_addr = ep_desc->bEndpointAddress;
+ ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
+ ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
+ ep_cfg.ep_interval = ep_desc->bInterval;
+ ep_cfg.hport = hport;
+ if (ep_desc->bEndpointAddress & 0x80) {
+ usbh_ep_alloc(&printer_class->bulkin, &ep_cfg);
+ } else {
+ usbh_ep_alloc(&printer_class->bulkout, &ep_cfg);
+ }
+ }
+
+ // uint8_t *device_id = usb_iomalloc(256);
+ // ret = usbh_printer_get_device_id(printer_class, device_id);
+ return ret;
+}
+
+static int usbh_printer_disconnect(struct usbh_hubport *hport, uint8_t intf)
+{
+ int ret = 0;
+
+ struct usbh_printer *printer_class = (struct usbh_printer *)hport->config.intf[intf].priv;
+
+ if (printer_class) {
+ if (printer_class->bulkin) {
+ ret = usb_ep_cancel(printer_class->bulkin);
+ if (ret < 0) {
+ }
+ usbh_ep_free(printer_class->bulkin);
+ }
+
+ if (printer_class->bulkout) {
+ ret = usb_ep_cancel(printer_class->bulkout);
+ if (ret < 0) {
+ }
+ usbh_ep_free(printer_class->bulkout);
+ }
+
+ usb_free(printer_class);
+
+ USB_LOG_INFO("Unregister Printer Class:%s\r\n", hport->config.intf[intf].devname);
+ memset(hport->config.intf[intf].devname, 0, CONFIG_USBHOST_DEV_NAMELEN);
+
+ hport->config.intf[intf].priv = NULL;
+ }
+
+ return ret;
+}
+
+static const struct usbh_class_driver printer_class_driver = {
+ .driver_name = "printer",
+ .connect = usbh_printer_connect,
+ .disconnect = usbh_printer_disconnect
+};
+
+CLASS_INFO_DEFINE const struct usbh_class_info printer_class_info = {
+ .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
+ .class = USB_DEVICE_CLASS_PRINTER,
+ .subclass = PRINTER_SUBCLASS,
+ .protocol = 0x00,
+ .vid = 0x00,
+ .pid = 0x00,
+ .class_driver = &printer_class_driver
+};
diff --git a/class/printer/usbh_printer.h b/class/printer/usbh_printer.h
new file mode 100644
index 00000000..4c41bfb3
--- /dev/null
+++ b/class/printer/usbh_printer.h
@@ -0,0 +1,44 @@
+/**
+ * @file usbh_printer.h
+ * @brief
+ *
+ * Copyright (c) 2022 sakumisu
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+#ifndef _USBH_PRINTER_H
+#define _USBH_PRINTER_H
+
+#include "usb_printer.h"
+
+struct usbh_printer {
+ struct usbh_hubport *hport;
+
+ uint8_t intf; /* interface number */
+ usbh_epinfo_t bulkin; /* BULK IN endpoint */
+ usbh_epinfo_t bulkout; /* BULK OUT endpoint */
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif \ No newline at end of file