8 #include <sys/select.h>
12 #include <linux/usbdevice_fs.h>
13 #include <linux/usb/ch9.h>
15 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
25 /* return 1 if founf, 0 if not, < 0 on error */
26 static int find_device(struct teensy_dev *dev,
27 uint16_t vendor, uint16_t product)
29 const char path_root[] = "/dev/bus/usb";
31 struct usb_descriptor_header hdr;
32 struct usb_device_descriptor d;
33 struct usb_config_descriptor c;
34 struct usb_interface_descriptor i;
35 struct usb_endpoint_descriptor e;
36 char space[0x100]; /* enough? */
38 char path_bus[256], path_dev[256];
39 struct dirent *ent, *ent_bus;
40 DIR *dir = NULL, *dir_bus = NULL;
46 memset(dev, 0xff, sizeof(*dev));
48 dir = opendir(path_root);
54 for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
55 /* should be a number like 000 */
56 if (sscanf(ent->d_name, "%03d", &num) != 1)
59 snprintf(path_bus, sizeof(path_bus), "%s/%s",
60 path_root, ent->d_name);
62 dir_bus = opendir(path_bus);
66 ent_bus = readdir(dir_bus);
67 for (; ent_bus != NULL; ent_bus = readdir(dir_bus)) {
68 if (sscanf(ent->d_name, "%03d", &num) != 1)
71 snprintf(path_dev, sizeof(path_dev), "%s/%s/%s",
72 path_root, ent->d_name, ent_bus->d_name);
74 fd = open(path_dev, O_RDWR);
78 ret = read(fd, &desc.d, sizeof(desc.d));
79 if (ret != sizeof(desc.d)) {
80 fprintf(stderr, "desc read: %d/%zd: ", ret, sizeof(desc.d));
85 if (desc.d.bDescriptorType != USB_DT_DEVICE) {
86 fprintf(stderr, "%s: bad DT: 0x%02x\n",
87 path_dev, desc.d.bDescriptorType);
91 if (desc.d.idVendor == vendor && desc.d.idProduct == product)
108 if (desc.d.bNumConfigurations != 1) {
109 fprintf(stderr, "unexpected bNumConfigurations: %u\n",
110 desc.d.bNumConfigurations);
114 /* walk through all descriptors */
117 ret = read(fd, &desc.hdr, sizeof(desc.hdr));
120 if (ret != sizeof(desc.hdr)) {
121 fprintf(stderr, "desc.hdr read: %d/%zd: ", ret, sizeof(desc.hdr));
126 ret = (int)lseek(fd, -sizeof(desc.hdr), SEEK_CUR);
132 ret = read(fd, &desc, desc.hdr.bLength);
133 if (ret != desc.hdr.bLength) {
134 fprintf(stderr, "desc read: %d/%u: ", ret, desc.hdr.bLength);
139 switch (desc.hdr.bDescriptorType) {
141 if (desc.c.bNumInterfaces != 2) {
142 fprintf(stderr, "unexpected bNumInterfaces: %u\n",
143 desc.c.bNumInterfaces);
148 case USB_DT_INTERFACE:
149 if (desc.i.bInterfaceClass != USB_CLASS_HID
150 || desc.i.bInterfaceSubClass != 0
151 || desc.i.bInterfaceProtocol != 0) {
152 fprintf(stderr, "unexpected interface %x:%x:%x\n",
153 desc.i.bInterfaceClass, desc.i.bInterfaceSubClass,
154 desc.i.bInterfaceProtocol);
157 if (desc.i.bNumEndpoints != 2) {
158 fprintf(stderr, "unexpected bNumEndpoints: %u\n",
159 desc.i.bNumEndpoints);
165 case USB_DT_ENDPOINT:
166 if (iface < 0 || iface >= ARRAY_SIZE(dev->ifaces)) {
167 fprintf(stderr, "bad iface: %d\n", iface);
170 if (desc.e.wMaxPacketSize != 64 && desc.e.wMaxPacketSize != 32) {
171 fprintf(stderr, "iface %d, EP %02x: "
172 "unexpected wMaxPacketSize: %u\n",
173 iface, desc.e.bEndpointAddress, desc.e.wMaxPacketSize);
176 if (desc.e.bEndpointAddress & 0x80)
177 dev->ifaces[iface].ep_in = desc.e.bEndpointAddress; // & 0x7F;
179 dev->ifaces[iface].ep_out = desc.e.bEndpointAddress;
187 fprintf(stderr, "skipping desc 0x%02x\n",
188 desc.hdr.bDescriptorType);
193 /* claim interfaces */
194 for (iface = 0; iface < ARRAY_SIZE(dev->ifaces); iface++) {
195 struct usbdevfs_ioctl usbio;
197 if (dev->ifaces[iface].ep_in == -1) {
198 fprintf(stderr, "missing ep_in, iface: %d\n", iface);
201 if (dev->ifaces[iface].ep_out == -1) {
202 fprintf(stderr, "missing ep_out, iface: %d\n", iface);
206 /* disconnect default driver */
207 memset(&usbio, 0, sizeof(usbio));
209 usbio.ioctl_code = USBDEVFS_DISCONNECT;
210 ret = ioctl(fd, USBDEVFS_IOCTL, &usbio);
211 if (ret != 0 && errno != ENODATA)
212 perror("USBDEVFS_DISCONNECT");
214 ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &iface);
216 perror("USBDEVFS_CLAIMINTERFACE");
241 int main(int argc, char *argv[])
243 struct teensy_dev dev;
244 struct usbdevfs_urb urb[URB_CNT];
245 struct usbdevfs_urb *reaped_urb;
246 char buf_dbg[64 + 1], buf_in[64];
249 int data_in_sent = 0;
258 ret = find_device(&dev, 0x16C0, 0x0486);
264 printf("waiting for device..\n");
277 memset(&urb[URB_DATA_IN], 0, sizeof(urb[URB_DATA_IN]));
278 urb[URB_DATA_IN].type = USBDEVFS_URB_TYPE_INTERRUPT;
279 urb[URB_DATA_IN].endpoint = dev.ifaces[0].ep_in;
280 urb[URB_DATA_IN].buffer = buf_in;
281 urb[URB_DATA_IN].buffer_length = sizeof(buf_in);
283 ret = ioctl(dev.fd, USBDEVFS_SUBMITURB, &urb[URB_DATA_IN]);
285 perror("USBDEVFS_SUBMITURB URB_DATA_IN");
291 memset(&urb[URB_DBG_IN], 0, sizeof(urb[URB_DBG_IN]));
292 urb[URB_DBG_IN].type = USBDEVFS_URB_TYPE_INTERRUPT;
293 urb[URB_DBG_IN].endpoint = dev.ifaces[1].ep_in;
294 urb[URB_DBG_IN].buffer = buf_dbg;
295 urb[URB_DBG_IN].buffer_length = sizeof(buf_dbg) - 1;
297 ret = ioctl(dev.fd, USBDEVFS_SUBMITURB, &urb[URB_DBG_IN]);
299 perror("USBDEVFS_SUBMITURB URB_DBG_IN");
306 FD_SET(dev.fd, &wfds);
308 ret = select(dev.fd + 1, NULL, &wfds, NULL, NULL);
314 if (FD_ISSET(dev.fd, &wfds)) {
316 ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb);
320 perror("USBDEVFS_REAPURB");
324 if (reaped_urb != NULL && reaped_urb->status != 0) {
325 errno = -reaped_urb->status;
326 perror("urb status");
327 if (reaped_urb->status == -EILSEQ) {
328 /* this is usually a sign of disconnect.. */
334 if (reaped_urb == &urb[URB_DATA_IN]) {
338 else if (reaped_urb == &urb[URB_DBG_IN]) {
340 buf_dbg[reaped_urb->actual_length] = 0;
341 printf("%s", buf_dbg);
345 fprintf(stderr, "reaped unknown urb? %p\n", reaped_urb);
358 // vim: ts=2:sw=2:expandtab