2 * NXT bootstrap interface; low-level USB functions.
4 * Copyright 2006 David Anderson <david.anderson@calixo.net>
5 * Modified to work with lejos NXJ by Lawrie Griffiths (lawrie.griffiths@ntlwworld.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 VENDOR_ATMEL = 0x03EB,
38 PRODUCT_SAMBA = 0x6124
42 struct usb_device *dev;
43 struct usb_dev_handle *hdl;
48 nxt_error_t nxt_init(nxt_t **nxt)
51 *nxt = calloc(1, sizeof(**nxt));
57 nxt_error_t nxt_find(nxt_t *nxt)
59 struct usb_bus *busses, *bus;
63 busses = usb_get_busses();
64 for (bus = busses; bus != NULL; bus = bus->next)
66 struct usb_device *dev;
68 for (dev = bus->devices; dev != NULL; dev = dev->next)
70 if (dev->descriptor.idVendor == VENDOR_ATMEL &&
71 dev->descriptor.idProduct == PRODUCT_SAMBA)
74 nxt->is_in_reset_mode = 1;
77 else if (dev->descriptor.idVendor == VENDOR_LEGO &&
78 dev->descriptor.idProduct == PRODUCT_NXT)
86 return NXT_NOT_PRESENT;
96 nxt->hdl = usb_open(nxt->dev);
98 ret = usb_set_configuration(nxt->hdl, 1);
103 return NXT_CONFIGURATION_ERROR;
106 ret = usb_claim_interface(nxt->hdl, 1);
115 nxt_send_str(nxt, "N#");
116 nxt_recv_buf(nxt, buf, 2);
117 if (memcmp(buf, "\n\r", 2) != 0)
119 usb_release_interface(nxt->hdl, 1);
121 return NXT_HANDSHAKE_FAILED;
129 nxt_close(nxt_t *nxt)
131 usb_release_interface(nxt->hdl, 1);
138 // Version of open that works with lejos NXJ firmware.
139 // Uses interface zero, and does not send samba N# message
141 nxt_open0(nxt_t *nxt)
145 nxt->hdl = usb_open(nxt->dev);
147 ret = usb_set_configuration(nxt->hdl, 1);
152 return NXT_CONFIGURATION_ERROR;
155 ret = usb_claim_interface(nxt->hdl, 0);
166 // Version of close that uses interface 0
168 nxt_close0(nxt_t *nxt)
170 usb_release_interface(nxt->hdl, 0);
178 nxt_in_reset_mode(nxt_t *nxt)
180 return nxt->is_in_reset_mode;
183 // Timeout set to 10 seconds for lejos NXJ
185 nxt_send_buf(nxt_t *nxt, char *buf, int len)
187 int ret = usb_bulk_write(nxt->hdl, 0x1, buf, len, 10000);
189 return NXT_USB_WRITE_ERROR;
196 nxt_send_str(nxt_t *nxt, char *str)
198 return nxt_send_buf(nxt, str, strlen(str));
203 nxt_recv_buf(nxt_t *nxt, char *buf, int len)
205 // set timeout to 10 seconds to allow time for defrag
206 int ret = usb_bulk_read(nxt->hdl, 0x82, buf, len, 10000);
208 return NXT_USB_READ_ERROR;