1 /* 2 * Copyright (c) 2007-2008 by Michael Lotz 3 * Heavily based on the original usb_serial driver which is: 4 * 5 * Copyright (c) 2003 by Siarzhuk Zharski <imker@gmx.li> 6 * Distributed under the terms of the MIT License. 7 */ 8 #include "ACM.h" 9 #include "Driver.h" 10 11 12 ACMDevice::ACMDevice(usb_device device, uint16 vendorID, uint16 productID, 13 const char *description) 14 : SerialDevice(device, vendorID, productID, description) 15 { 16 } 17 18 19 status_t 20 ACMDevice::AddDevice(const usb_configuration_info *config) 21 { 22 TRACE_FUNCALLS("> ACMDevice::AddDevice(0x%08x, 0x%08x)\n", this, config); 23 24 status_t status = ENODEV; 25 uint8 masterIndex = 0; 26 uint8 slaveIndex = 0; 27 usb_cdc_cm_functional_descriptor* cmDesc = NULL; 28 usb_cdc_union_functional_descriptor* unionDesc = NULL; 29 30 // Search ACM Communication Interface 31 for (size_t i = 0; i < config->interface_count && status < B_OK; i++) { 32 usb_interface_info *interface = config->interface[i].active; 33 usb_interface_descriptor *descriptor = interface->descr; 34 if (descriptor->interface_class != USB_CDC_COMMUNICATION_INTERFACE_CLASS 35 || descriptor->interface_subclass != USB_CDC_COMMUNICATION_INTERFACE_ACM_SUBCLASS) 36 continue; 37 38 // Found ACM Communication Interface! 39 // Get functional descriptors of some interest, if any 40 for (size_t j = 0; j < interface->generic_count; j++) { 41 usb_generic_descriptor *generic = &interface->generic[j]->generic; 42 switch (generic->data[0]) { 43 case USB_CDC_CM_FUNCTIONAL_DESCRIPTOR: 44 cmDesc = (usb_cdc_cm_functional_descriptor*)generic; 45 break; 46 47 case USB_CDC_ACM_FUNCTIONAL_DESCRIPTOR: 48 break; 49 50 case USB_CDC_UNION_FUNCTIONAL_DESCRIPTOR: 51 unionDesc = (usb_cdc_union_functional_descriptor*)generic; 52 break; 53 } 54 } 55 56 masterIndex = unionDesc ? unionDesc->master_interface : i; 57 slaveIndex = cmDesc ? cmDesc->data_interface 58 : unionDesc ? unionDesc->slave_interfaces[0] : 0; 59 60 TRACE("ACM device found on configuration #%d: master itf: %d, " 61 "slave/data itf: %d\n", config->descr->configuration, 62 masterIndex, slaveIndex); 63 64 // Some ACM USB devices report the wrong unions which rightfully 65 // breaks probing. Some drivers keep a list of these devices, 66 // for now we just assume identical indexes are wrong. 67 if (masterIndex == slaveIndex) { 68 TRACE_ALWAYS("Command interface matches data interface, " 69 "assuming broken union quirk!\n"); 70 masterIndex = 0; 71 slaveIndex = 1; 72 } 73 74 status = B_OK; 75 break; 76 } 77 78 if (status == B_OK && masterIndex < config->interface_count) { 79 // check that the indicated master interface fits our need 80 usb_interface_info *interface = config->interface[masterIndex].active; 81 usb_interface_descriptor *descriptor = interface->descr; 82 if ((descriptor->interface_class == USB_CDC_COMMUNICATION_INTERFACE_CLASS 83 || descriptor->interface_class == USB_CDC_DATA_INTERFACE_CLASS) 84 && interface->endpoint_count >= 1) { 85 SetControlPipe(interface->endpoint[0].handle); 86 } else { 87 TRACE("Indicated command interface doesn't fit our needs!\n"); 88 status = ENODEV; 89 } 90 } 91 92 if (status == B_OK && slaveIndex < config->interface_count) { 93 // check that the indicated slave interface fits our need 94 usb_interface_info *interface = config->interface[slaveIndex].active; 95 usb_interface_descriptor *descriptor = interface->descr; 96 if (descriptor->interface_class == USB_CDC_DATA_INTERFACE_CLASS 97 && interface->endpoint_count >= 2) { 98 if (!(interface->endpoint[0].descr->endpoint_address & USB_ENDPOINT_ADDR_DIR_IN)) 99 SetWritePipe(interface->endpoint[0].handle); 100 else 101 SetReadPipe(interface->endpoint[0].handle); 102 103 if (interface->endpoint[1].descr->endpoint_address & USB_ENDPOINT_ADDR_DIR_IN) 104 SetReadPipe(interface->endpoint[1].handle); 105 else 106 SetWritePipe(interface->endpoint[1].handle); 107 } else { 108 TRACE("Indicated data interface doesn't fit our needs!\n"); 109 status = ENODEV; 110 } 111 } 112 113 TRACE_FUNCRET("< ACMDevice::AddDevice() returns: 0x%08x\n", status); 114 return status; 115 } 116 117 118 status_t 119 ACMDevice::SetLineCoding(usb_cdc_line_coding *lineCoding) 120 { 121 TRACE_FUNCALLS("> ACMDevice::SetLineCoding(0x%08x, {%d, 0x%02x, 0x%02x, 0x%02x})\n", 122 this, lineCoding->speed, lineCoding->stopbits, lineCoding->parity, 123 lineCoding->databits); 124 125 size_t length = 0; 126 status_t status = gUSBModule->send_request(Device(), 127 USB_REQTYPE_CLASS | USB_REQTYPE_INTERFACE_OUT, 128 USB_CDC_SET_LINE_CODING, 0, 0, 129 sizeof(usb_cdc_line_coding), 130 lineCoding, &length); 131 132 TRACE_FUNCRET("< ACMDevice::SetLineCoding() returns: 0x%08x\n", status); 133 return status; 134 } 135 136 137 status_t 138 ACMDevice::SetControlLineState(uint16 state) 139 { 140 TRACE_FUNCALLS("> ACMDevice::SetControlLineState(0x%08x, 0x%04x)\n", this, state); 141 142 size_t length = 0; 143 status_t status = gUSBModule->send_request(Device(), 144 USB_REQTYPE_CLASS | USB_REQTYPE_INTERFACE_OUT, 145 USB_CDC_SET_CONTROL_LINE_STATE, state, 0, 0, NULL, &length); 146 147 TRACE_FUNCRET("< ACMDevice::SetControlLineState() returns: 0x%08x\n", status); 148 return status; 149 } 150