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.
22 #define CP2101_UART 0x00
23 #define UART_ENABLE 0x0001
24 #define UART_DISABLE 0x0000
25 #define REQTYPE_HOST_TO_DEVICE 0x41
26 #define REQTYPE_DEVICE_TO_HOST 0xc1
29 cp_usb_gpio_get(struct ccdbg *dbg, uint8_t *gpio_get)
31 return usb_control_msg(dbg->usb_dev, /* dev */
33 0xff, /* requesttype */
36 (char *) gpio_get, /* bytes */
42 cp_usb_gpio_set(struct ccdbg *dbg, uint8_t mask, uint8_t value)
44 uint16_t gpio_set = ((uint16_t) value << 8) | mask;
46 return usb_control_msg(dbg->usb_dev, /* dev */
48 0xff, /* requesttype */
57 cp_usb_uart_enable_disable(struct ccdbg *dbg, uint16_t enable)
59 return usb_control_msg(dbg->usb_dev,
61 REQTYPE_HOST_TO_DEVICE,
70 cp_usb_init(struct ccdbg *dbg)
72 usb_dev_handle *dev_handle;
73 struct usb_device *dev;
74 struct usb_bus *bus, *busses;
83 busses = usb_get_busses();
84 for (bus = busses; bus; bus = bus->next) {
85 for (dev = bus->devices; dev; dev = dev->next) {
86 if (dev->descriptor.idVendor == 0x10c4 &&
87 dev->descriptor.idProduct == 0xea60)
94 perror("No CP2103 found\n");
98 dev_handle = usb_open(dev);
99 usb_detach_kernel_driver_np(dev_handle, interface);
100 usb_claim_interface(dev_handle, interface);
101 dbg->usb_dev = dev_handle;
102 ret = cp_usb_uart_enable_disable(dbg, UART_DISABLE);
104 ret = cp_usb_gpio_set(dbg, 0xf, dbg->gpio);
105 ret = cp_usb_gpio_get(dbg, &gpio);
109 cp_usb_fini(struct ccdbg *dbg)
111 cp_usb_uart_enable_disable(dbg, UART_DISABLE);
112 usb_close(dbg->usb_dev);
116 cp_usb_write(struct ccdbg *dbg, uint8_t mask, uint8_t value)
121 new_gpio = (dbg->gpio & ~mask) | (value & mask);
122 if (new_gpio != dbg->gpio) {
123 ret = cp_usb_gpio_set(dbg, new_gpio ^ dbg->gpio, new_gpio);
126 dbg->gpio = new_gpio;
131 cp_usb_read(struct ccdbg *dbg)
136 ret = cp_usb_gpio_get(dbg, &gpio);