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.
31 usb_dev_handle *usb_dev;
35 #define CP2101_UART 0x00
36 #define UART_ENABLE 0x0001
37 #define UART_DISABLE 0x0000
38 #define REQTYPE_HOST_TO_DEVICE 0x41
39 #define REQTYPE_DEVICE_TO_HOST 0xc1
42 cp_usb_gpio_get(struct cp_usb *cp, uint8_t *gpio_get)
44 return usb_control_msg(cp->usb_dev, /* dev */
46 0xff, /* requesttype */
49 (char *) gpio_get, /* bytes */
55 cp_usb_gpio_set(struct cp_usb *cp, uint8_t mask, uint8_t value)
57 uint16_t gpio_set = ((uint16_t) value << 8) | mask;
59 return usb_control_msg(cp->usb_dev, /* dev */
61 0xff, /* requesttype */
70 cp_usb_uart_enable_disable(struct cp_usb *cp, uint16_t enable)
72 return usb_control_msg(cp->usb_dev,
74 REQTYPE_HOST_TO_DEVICE,
86 usb_dev_handle *dev_handle;
87 struct usb_device *dev = NULL;
88 struct usb_bus *bus, *busses;
97 busses = usb_get_busses();
98 for (bus = busses; bus; bus = bus->next) {
99 for (dev = bus->devices; dev; dev = dev->next) {
100 if (dev->descriptor.idVendor == 0x10c4 &&
101 dev->descriptor.idProduct == 0xea60)
108 perror("No CP2103 found");
111 cp = calloc(sizeof(struct cp_usb), 1);
113 dev_handle = usb_open(dev);
114 usb_detach_kernel_driver_np(dev_handle, interface);
115 usb_claim_interface(dev_handle, interface);
116 cp->usb_dev = dev_handle;
117 ret = cp_usb_uart_enable_disable(cp, UART_DISABLE);
119 ret = cp_usb_gpio_set(cp, 0xf, cp->gpio);
120 ret = cp_usb_gpio_get(cp, &gpio);
125 cp_usb_close(struct cp_usb *cp)
127 cp_usb_uart_enable_disable(cp, UART_DISABLE);
128 usb_close(cp->usb_dev);
133 cp_usb_write(struct cp_usb *cp, uint8_t mask, uint8_t value)
138 new_gpio = (cp->gpio & ~mask) | (value & mask);
139 if (new_gpio != cp->gpio) {
140 ret = cp_usb_gpio_set(cp, new_gpio ^ cp->gpio, new_gpio);
148 cp_usb_read(struct cp_usb *cp)
153 ret = cp_usb_gpio_get(cp, &gpio);