2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 * libusb interface to the GPIO pins on a CP2103.
22 * Various magic constants came from the cp210x driver published by silabs.
30 usb_dev_handle *usb_dev;
34 #define CP2101_UART 0x00
35 #define UART_ENABLE 0x0001
36 #define UART_DISABLE 0x0000
37 #define REQTYPE_HOST_TO_DEVICE 0x41
38 #define REQTYPE_DEVICE_TO_HOST 0xc1
41 cp_usb_gpio_get(struct cp_usb *cp, uint8_t *gpio_get)
43 return usb_control_msg(cp->usb_dev, /* dev */
45 0xff, /* requesttype */
48 (char *) gpio_get, /* bytes */
54 cp_usb_gpio_set(struct cp_usb *cp, uint8_t mask, uint8_t value)
56 uint16_t gpio_set = ((uint16_t) value << 8) | mask;
58 return usb_control_msg(cp->usb_dev, /* dev */
60 0xff, /* requesttype */
69 cp_usb_uart_enable_disable(struct cp_usb *cp, uint16_t enable)
71 return usb_control_msg(cp->usb_dev,
73 REQTYPE_HOST_TO_DEVICE,
85 usb_dev_handle *dev_handle;
86 struct usb_device *dev = NULL;
87 struct usb_bus *bus, *busses;
96 busses = usb_get_busses();
97 for (bus = busses; bus; bus = bus->next) {
98 for (dev = bus->devices; dev; dev = dev->next) {
99 if (dev->descriptor.idVendor == 0x10c4 &&
100 dev->descriptor.idProduct == 0xea60)
107 perror("No CP2103 found");
110 cp = calloc(sizeof(struct cp_usb), 1);
112 dev_handle = usb_open(dev);
113 usb_detach_kernel_driver_np(dev_handle, interface);
114 usb_claim_interface(dev_handle, interface);
115 cp->usb_dev = dev_handle;
116 ret = cp_usb_uart_enable_disable(cp, UART_DISABLE);
118 ret = cp_usb_gpio_set(cp, 0xf, cp->gpio);
119 ret = cp_usb_gpio_get(cp, &gpio);
124 cp_usb_close(struct cp_usb *cp)
126 cp_usb_uart_enable_disable(cp, UART_DISABLE);
127 usb_close(cp->usb_dev);
132 cp_usb_write(struct cp_usb *cp, uint8_t mask, uint8_t value)
137 new_gpio = (cp->gpio & ~mask) | (value & mask);
138 if (new_gpio != cp->gpio) {
139 ret = cp_usb_gpio_set(cp, new_gpio ^ cp->gpio, new_gpio);
147 cp_usb_read(struct cp_usb *cp)
152 ret = cp_usb_gpio_get(cp, &gpio);