• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "usbh_core.h"
7 #include "usbh_mtp.h"
8 
9 #define DEV_FORMAT "/dev/mtp"
10 
usbh_mtp_connect(struct usbh_hubport * hport,uint8_t intf)11 static int usbh_mtp_connect(struct usbh_hubport *hport, uint8_t intf)
12 {
13     struct usbh_endpoint_cfg ep_cfg = { 0 };
14     struct usb_endpoint_descriptor *ep_desc;
15     int ret;
16 
17     struct usbh_mtp *mtp_class = usb_malloc(sizeof(struct usbh_mtp));
18     if (mtp_class == NULL) {
19         USB_LOG_ERR("Fail to alloc mtp_class\r\n");
20         return -ENOMEM;
21     }
22 
23     memset(mtp_class, 0, sizeof(struct usbh_mtp));
24     mtp_class->hport = hport;
25     mtp_class->intf = intf;
26 
27     hport->config.intf[intf].priv = mtp_class;
28 
29 #ifdef CONFIG_USBHOST_MTP_NOTIFY
30     ep_desc = &hport->config.intf[intf].altsetting[0].ep[0].ep_desc;
31     usbh_hport_activate_epx(&mtp_class->intin, hport, ep_desc);
32 #endif
33     for (uint8_t i = 0; i < hport->config.intf[intf + 1].altsetting[0].intf_desc.bNumEndpoints; i++) {
34         ep_desc = &hport->config.intf[intf + 1].altsetting[0].ep[i].ep_desc;
35 
36         if (ep_desc->bEndpointAddress & 0x80) {
37             usbh_hport_activate_epx(&mtp_class->bulkin, hport, ep_desc);
38         } else {
39             usbh_hport_activate_epx(&mtp_class->bulkout, hport, ep_desc);
40         }
41     }
42 
43     strncpy(hport->config.intf[intf].devname, DEV_FORMAT, CONFIG_USBHOST_DEV_NAMELEN);
44 
45     USB_LOG_INFO("Register MTP Class:%s\r\n", hport->config.intf[intf].devname);
46 
47     return ret;
48 }
49 
usbh_mtp_disconnect(struct usbh_hubport * hport,uint8_t intf)50 static int usbh_mtp_disconnect(struct usbh_hubport *hport, uint8_t intf)
51 {
52     int ret = 0;
53 
54     struct usbh_mtp *mtp_class = (struct usbh_mtp *)hport->config.intf[intf].priv;
55 
56     if (mtp_class) {
57         if (mtp_class->bulkin) {
58             usbh_pipe_free(mtp_class->bulkin);
59         }
60 
61         if (mtp_class->bulkout) {
62             usbh_pipe_free(mtp_class->bulkout);
63         }
64 
65         if (hport->config.intf[intf].devname[0] != '\0') {
66             USB_LOG_INFO("Unregister MTP Class:%s\r\n", hport->config.intf[intf].devname);
67         }
68 
69         memset(mtp_class, 0, sizeof(struct usbh_mtp));
70         usb_free(mtp_class);
71     }
72 
73     return ret;
74 }
75 
76 static const struct usbh_class_driver mtp_class_driver = {
77     .driver_name = "mtp",
78     .connect = usbh_mtp_connect,
79     .disconnect = usbh_mtp_disconnect
80 };
81 
82 CLASS_INFO_DEFINE const struct usbh_class_info mtp_class_info = {
83     .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
84     .class = USB_MTP_CLASS,
85     .subclass = USB_MTP_SUB_CLASS,
86     .protocol = USB_MTP_PROTOCOL,
87     .vid = 0x00,
88     .pid = 0x00,
89     .class_driver = &mtp_class_driver
90 };
91