aboutsummaryrefslogtreecommitdiff
path: root/lowlevel.c
blob: c5c407ccfb666bc737e1d2b902acde4a3885ea1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
 * NXT bootstrap interface; low-level USB functions.
 *
 * Copyright 2006 David Anderson <david.anderson@calixo.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <usb.h>

#include "lowlevel.h"

const struct
{
  int vendor_id;
  int product_id;
} nxt_usb_ids[N_FIRMWARES] = {
  { 0x03EB, 0x6124 }, /* SAM-BA */
  { 0x0694, 0x0002 }, /* LEGO   */
  { 0x0694, 0xFF00 }  /* NXTOS  */
};

struct nxt_t
{
  struct usb_device *dev;
  struct usb_dev_handle *hdl;
  nxt_firmware firmware;
  int interface;
};

nxt_error_t
nxt_init(nxt_t **nxt)
{
  usb_init();
  *nxt = calloc(1, sizeof(**nxt));

  return NXT_OK;
}

nxt_error_t
nxt_find(nxt_t *nxt)
{
  struct usb_bus *busses, *bus;

  usb_find_busses();
  usb_find_devices();

  busses = usb_get_busses();

  for (bus = busses; bus != NULL; bus = bus->next)
    {
      struct usb_device *dev;

      for (dev = bus->devices; dev != NULL; dev = dev->next)
        {
          int i;

          for (i = 0; i < N_FIRMWARES; i++)
            if (dev->descriptor.idVendor == nxt_usb_ids[i].vendor_id &&
                dev->descriptor.idProduct == nxt_usb_ids[i].product_id)
              {
                nxt->dev = dev;
                nxt->firmware = i;
                return NXT_OK;
              }
        }
    }

  return NXT_NOT_PRESENT;
}

nxt_error_t
nxt_open(nxt_t *nxt, int interface)
{
  int ret;

  nxt->hdl = usb_open(nxt->dev);

  // Try to detach driver, but ignore errors, only implemented on Linux
  usb_detach_kernel_driver_np(nxt->hdl, interface);

  ret = usb_set_configuration(nxt->hdl, 1);
  if (ret < 0)
    {
      usb_close(nxt->hdl);
      return NXT_CONFIGURATION_ERROR;
    }

  ret = usb_claim_interface(nxt->hdl, interface);
  if (ret < 0)
    {
      usb_close(nxt->hdl);
      return NXT_IN_USE;
    }

  nxt->interface = interface;

  return NXT_OK;
}

nxt_error_t
nxt_close(nxt_t *nxt)
{
  usb_release_interface(nxt->hdl, nxt->interface);
  usb_close(nxt->hdl);
  free(nxt);

  return NXT_OK;
}

int
nxt_is_firmware(nxt_t *nxt, nxt_firmware fw)
{
  return (nxt->firmware == fw);
}

nxt_error_t
nxt_send_buf(nxt_t *nxt, char *buf, int len)
{
  int ret = usb_bulk_write(nxt->hdl, 0x1, buf, len, 0);
  if (ret < 0)
    return NXT_USB_WRITE_ERROR;

  return NXT_OK;
}

nxt_error_t
nxt_send_str(nxt_t *nxt, char *str)
{
  return nxt_send_buf(nxt, str, strlen(str));
}

nxt_error_t
nxt_recv_buf(nxt_t *nxt, char *buf, int len)
{
  int ret = usb_bulk_read(nxt->hdl, 0x82, buf, len, 0);
  if (ret < 0)
    return NXT_USB_READ_ERROR;

  return NXT_OK;
}