X-Git-Url: http://developer.intra2net.com/git/?a=blobdiff_plain;f=src%2Fftdi.c;h=4c4d31a5c40d6a393d607c98a25256be7c187d1d;hb=8ce71b78305cd61feacde340760f1ddccbd308ea;hp=5bfc27c5419ed3c914bad228d339ad0f67425ab3;hpb=edb82cbfc69aa07daca72790808d759e02272efe;p=libftdi diff --git a/src/ftdi.c b/src/ftdi.c index 5bfc27c..4c4d31a 100644 --- a/src/ftdi.c +++ b/src/ftdi.c @@ -170,6 +170,60 @@ void ftdi_list_free(struct ftdi_device_list **devlist) devlist = NULL; } +/* ftdi_usb_open_dev + + Opens a ftdi device given by a usb_device. + + Return codes: + 0: all fine + -4: unable to open device + -5: unable to claim device + -6: reset failed + -7: set baudrate failed +*/ +int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev) +{ + if (!(ftdi->usb_dev = usb_open(dev))) + ftdi_error_return(-4, "usb_open() failed"); + + if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) { + usb_close (ftdi->usb_dev); + ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!"); + } + + if (ftdi_usb_reset (ftdi) != 0) { + usb_close (ftdi->usb_dev); + ftdi_error_return(-6, "ftdi_usb_reset failed"); + } + + if (ftdi_set_baudrate (ftdi, 9600) != 0) { + usb_close (ftdi->usb_dev); + ftdi_error_return(-7, "set baudrate failed"); + } + + // Try to guess chip type + // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0 + if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200 + && dev->descriptor.iSerialNumber == 0)) + ftdi->type = TYPE_BM; + else if (dev->descriptor.bcdDevice == 0x200) + ftdi->type = TYPE_AM; + else if (dev->descriptor.bcdDevice == 0x500) { + ftdi->type = TYPE_2232C; + if (!ftdi->index) + ftdi->index = INTERFACE_A; + } + + ftdi_error_return(0, "all fine"); +} + +/* ftdi_usb_open + + Opens the first device with a given vendor and product ids. + + Return codes: + See ftdi_usb_open_desc() +*/ int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) { return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL); @@ -408,7 +462,7 @@ static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi, if(ftdi->type == TYPE_2232C) { *index = (unsigned short)(encoded_divisor >> 8); *index &= 0xFF00; - *index |= ftdi->interface; + *index |= ftdi->index; } else *index = (unsigned short)(encoded_divisor >> 16);