| 1 | /*************************************************************************** |
| 2 | ftdi.c - description |
| 3 | ------------------- |
| 4 | begin : Fri Apr 4 2003 |
| 5 | copyright : (C) 2003-2011 by Intra2net AG and the libftdi developers |
| 6 | email : opensource@intra2net.com |
| 7 | ***************************************************************************/ |
| 8 | |
| 9 | /*************************************************************************** |
| 10 | * * |
| 11 | * This program is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU Lesser General Public License * |
| 13 | * version 2.1 as published by the Free Software Foundation; * |
| 14 | * * |
| 15 | ***************************************************************************/ |
| 16 | |
| 17 | /** |
| 18 | \mainpage libftdi API documentation |
| 19 | |
| 20 | Library to talk to FTDI chips. You find the latest versions of libftdi at |
| 21 | http://www.intra2net.com/en/developer/libftdi/ |
| 22 | |
| 23 | The library is easy to use. Have a look at this short example: |
| 24 | \include simple.c |
| 25 | |
| 26 | More examples can be found in the "examples" directory. |
| 27 | */ |
| 28 | /** \addtogroup libftdi */ |
| 29 | /* @{ */ |
| 30 | |
| 31 | #include <libusb.h> |
| 32 | #include <string.h> |
| 33 | #include <errno.h> |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | |
| 37 | #include "ftdi.h" |
| 38 | |
| 39 | #define ftdi_error_return(code, str) do { \ |
| 40 | ftdi->error_str = str; \ |
| 41 | return code; \ |
| 42 | } while(0); |
| 43 | |
| 44 | #define ftdi_error_return_free_device_list(code, str, devs) do { \ |
| 45 | libusb_free_device_list(devs,1); \ |
| 46 | ftdi->error_str = str; \ |
| 47 | return code; \ |
| 48 | } while(0); |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | Internal function to close usb device pointer. |
| 53 | Sets ftdi->usb_dev to NULL. |
| 54 | \internal |
| 55 | |
| 56 | \param ftdi pointer to ftdi_context |
| 57 | |
| 58 | \retval none |
| 59 | */ |
| 60 | static void ftdi_usb_close_internal (struct ftdi_context *ftdi) |
| 61 | { |
| 62 | if (ftdi && ftdi->usb_dev) |
| 63 | { |
| 64 | libusb_close (ftdi->usb_dev); |
| 65 | ftdi->usb_dev = NULL; |
| 66 | if(ftdi->eeprom) |
| 67 | ftdi->eeprom->initialized_for_connected_device = 0; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | Initializes a ftdi_context. |
| 73 | |
| 74 | \param ftdi pointer to ftdi_context |
| 75 | |
| 76 | \retval 0: all fine |
| 77 | \retval -1: couldn't allocate read buffer |
| 78 | \retval -2: couldn't allocate struct buffer |
| 79 | \retval -3: libusb_init() failed |
| 80 | |
| 81 | \remark This should be called before all functions |
| 82 | */ |
| 83 | int ftdi_init(struct ftdi_context *ftdi) |
| 84 | { |
| 85 | struct ftdi_eeprom* eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom)); |
| 86 | ftdi->usb_ctx = NULL; |
| 87 | ftdi->usb_dev = NULL; |
| 88 | ftdi->usb_read_timeout = 5000; |
| 89 | ftdi->usb_write_timeout = 5000; |
| 90 | |
| 91 | ftdi->type = TYPE_BM; /* chip type */ |
| 92 | ftdi->baudrate = -1; |
| 93 | ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */ |
| 94 | |
| 95 | ftdi->readbuffer = NULL; |
| 96 | ftdi->readbuffer_offset = 0; |
| 97 | ftdi->readbuffer_remaining = 0; |
| 98 | ftdi->writebuffer_chunksize = 4096; |
| 99 | ftdi->max_packet_size = 0; |
| 100 | ftdi->error_str = NULL; |
| 101 | ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE; |
| 102 | |
| 103 | if (libusb_init(&ftdi->usb_ctx) < 0) |
| 104 | ftdi_error_return(-3, "libusb_init() failed"); |
| 105 | |
| 106 | ftdi_set_interface(ftdi, INTERFACE_ANY); |
| 107 | ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */ |
| 108 | |
| 109 | if (eeprom == 0) |
| 110 | ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom"); |
| 111 | memset(eeprom, 0, sizeof(struct ftdi_eeprom)); |
| 112 | ftdi->eeprom = eeprom; |
| 113 | |
| 114 | /* All fine. Now allocate the readbuffer */ |
| 115 | return ftdi_read_data_set_chunksize(ftdi, 4096); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | Allocate and initialize a new ftdi_context |
| 120 | |
| 121 | \return a pointer to a new ftdi_context, or NULL on failure |
| 122 | */ |
| 123 | struct ftdi_context *ftdi_new(void) |
| 124 | { |
| 125 | struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context)); |
| 126 | |
| 127 | if (ftdi == NULL) |
| 128 | { |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | if (ftdi_init(ftdi) != 0) |
| 133 | { |
| 134 | free(ftdi); |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | return ftdi; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | Open selected channels on a chip, otherwise use first channel. |
| 143 | |
| 144 | \param ftdi pointer to ftdi_context |
| 145 | \param interface Interface to use for FT2232C/2232H/4232H chips. |
| 146 | |
| 147 | \retval 0: all fine |
| 148 | \retval -1: unknown interface |
| 149 | \retval -2: USB device unavailable |
| 150 | */ |
| 151 | int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface) |
| 152 | { |
| 153 | if (ftdi == NULL) |
| 154 | ftdi_error_return(-2, "USB device unavailable"); |
| 155 | |
| 156 | switch (interface) |
| 157 | { |
| 158 | case INTERFACE_ANY: |
| 159 | case INTERFACE_A: |
| 160 | ftdi->interface = 0; |
| 161 | ftdi->index = INTERFACE_A; |
| 162 | ftdi->in_ep = 0x02; |
| 163 | ftdi->out_ep = 0x81; |
| 164 | break; |
| 165 | case INTERFACE_B: |
| 166 | ftdi->interface = 1; |
| 167 | ftdi->index = INTERFACE_B; |
| 168 | ftdi->in_ep = 0x04; |
| 169 | ftdi->out_ep = 0x83; |
| 170 | break; |
| 171 | case INTERFACE_C: |
| 172 | ftdi->interface = 2; |
| 173 | ftdi->index = INTERFACE_C; |
| 174 | ftdi->in_ep = 0x06; |
| 175 | ftdi->out_ep = 0x85; |
| 176 | break; |
| 177 | case INTERFACE_D: |
| 178 | ftdi->interface = 3; |
| 179 | ftdi->index = INTERFACE_D; |
| 180 | ftdi->in_ep = 0x08; |
| 181 | ftdi->out_ep = 0x87; |
| 182 | break; |
| 183 | default: |
| 184 | ftdi_error_return(-1, "Unknown interface"); |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | Deinitializes a ftdi_context. |
| 191 | |
| 192 | \param ftdi pointer to ftdi_context |
| 193 | */ |
| 194 | void ftdi_deinit(struct ftdi_context *ftdi) |
| 195 | { |
| 196 | if (ftdi == NULL) |
| 197 | return; |
| 198 | |
| 199 | ftdi_usb_close_internal (ftdi); |
| 200 | |
| 201 | if (ftdi->readbuffer != NULL) |
| 202 | { |
| 203 | free(ftdi->readbuffer); |
| 204 | ftdi->readbuffer = NULL; |
| 205 | } |
| 206 | |
| 207 | if (ftdi->eeprom != NULL) |
| 208 | { |
| 209 | if (ftdi->eeprom->manufacturer != 0) |
| 210 | { |
| 211 | free(ftdi->eeprom->manufacturer); |
| 212 | ftdi->eeprom->manufacturer = 0; |
| 213 | } |
| 214 | if (ftdi->eeprom->product != 0) |
| 215 | { |
| 216 | free(ftdi->eeprom->product); |
| 217 | ftdi->eeprom->product = 0; |
| 218 | } |
| 219 | if (ftdi->eeprom->serial != 0) |
| 220 | { |
| 221 | free(ftdi->eeprom->serial); |
| 222 | ftdi->eeprom->serial = 0; |
| 223 | } |
| 224 | free(ftdi->eeprom); |
| 225 | ftdi->eeprom = NULL; |
| 226 | } |
| 227 | |
| 228 | if (ftdi->usb_ctx) |
| 229 | { |
| 230 | libusb_exit(ftdi->usb_ctx); |
| 231 | ftdi->usb_ctx = NULL; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | Deinitialize and free an ftdi_context. |
| 237 | |
| 238 | \param ftdi pointer to ftdi_context |
| 239 | */ |
| 240 | void ftdi_free(struct ftdi_context *ftdi) |
| 241 | { |
| 242 | ftdi_deinit(ftdi); |
| 243 | free(ftdi); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | Use an already open libusb device. |
| 248 | |
| 249 | \param ftdi pointer to ftdi_context |
| 250 | \param usb libusb libusb_device_handle to use |
| 251 | */ |
| 252 | void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb) |
| 253 | { |
| 254 | if (ftdi == NULL) |
| 255 | return; |
| 256 | |
| 257 | ftdi->usb_dev = usb; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | /** |
| 262 | Finds all ftdi devices with given VID:PID on the usb bus. Creates a new |
| 263 | ftdi_device_list which needs to be deallocated by ftdi_list_free() after |
| 264 | use. With VID:PID 0:0, search for the default devices |
| 265 | (0x403:0x6001, 0x403:0x6010, 0x403:0x6011, 0x403:0x6014) |
| 266 | |
| 267 | \param ftdi pointer to ftdi_context |
| 268 | \param devlist Pointer where to store list of found devices |
| 269 | \param vendor Vendor ID to search for |
| 270 | \param product Product ID to search for |
| 271 | |
| 272 | \retval >0: number of devices found |
| 273 | \retval -3: out of memory |
| 274 | \retval -5: libusb_get_device_list() failed |
| 275 | \retval -6: libusb_get_device_descriptor() failed |
| 276 | */ |
| 277 | int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product) |
| 278 | { |
| 279 | struct ftdi_device_list **curdev; |
| 280 | libusb_device *dev; |
| 281 | libusb_device **devs; |
| 282 | int count = 0; |
| 283 | int i = 0; |
| 284 | |
| 285 | if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0) |
| 286 | ftdi_error_return(-5, "libusb_get_device_list() failed"); |
| 287 | |
| 288 | curdev = devlist; |
| 289 | *curdev = NULL; |
| 290 | |
| 291 | while ((dev = devs[i++]) != NULL) |
| 292 | { |
| 293 | struct libusb_device_descriptor desc; |
| 294 | |
| 295 | if (libusb_get_device_descriptor(dev, &desc) < 0) |
| 296 | ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs); |
| 297 | |
| 298 | if (((vendor != 0 && product != 0) && |
| 299 | desc.idVendor == vendor && desc.idProduct == product) || |
| 300 | ((vendor == 0 && product == 0) && |
| 301 | (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010 |
| 302 | || desc.idProduct == 0x6011 || desc.idProduct == 0x6014))) |
| 303 | { |
| 304 | *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list)); |
| 305 | if (!*curdev) |
| 306 | ftdi_error_return_free_device_list(-3, "out of memory", devs); |
| 307 | |
| 308 | (*curdev)->next = NULL; |
| 309 | (*curdev)->dev = dev; |
| 310 | libusb_ref_device(dev); |
| 311 | curdev = &(*curdev)->next; |
| 312 | count++; |
| 313 | } |
| 314 | } |
| 315 | libusb_free_device_list(devs,1); |
| 316 | return count; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | Frees a usb device list. |
| 321 | |
| 322 | \param devlist USB device list created by ftdi_usb_find_all() |
| 323 | */ |
| 324 | void ftdi_list_free(struct ftdi_device_list **devlist) |
| 325 | { |
| 326 | struct ftdi_device_list *curdev, *next; |
| 327 | |
| 328 | for (curdev = *devlist; curdev != NULL;) |
| 329 | { |
| 330 | next = curdev->next; |
| 331 | libusb_unref_device(curdev->dev); |
| 332 | free(curdev); |
| 333 | curdev = next; |
| 334 | } |
| 335 | |
| 336 | *devlist = NULL; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | Frees a usb device list. |
| 341 | |
| 342 | \param devlist USB device list created by ftdi_usb_find_all() |
| 343 | */ |
| 344 | void ftdi_list_free2(struct ftdi_device_list *devlist) |
| 345 | { |
| 346 | ftdi_list_free(&devlist); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | Return device ID strings from the usb device. |
| 351 | |
| 352 | The parameters manufacturer, description and serial may be NULL |
| 353 | or pointer to buffers to store the fetched strings. |
| 354 | |
| 355 | \note Use this function only in combination with ftdi_usb_find_all() |
| 356 | as it closes the internal "usb_dev" after use. |
| 357 | |
| 358 | \param ftdi pointer to ftdi_context |
| 359 | \param dev libusb usb_dev to use |
| 360 | \param manufacturer Store manufacturer string here if not NULL |
| 361 | \param mnf_len Buffer size of manufacturer string |
| 362 | \param description Store product description string here if not NULL |
| 363 | \param desc_len Buffer size of product description string |
| 364 | \param serial Store serial string here if not NULL |
| 365 | \param serial_len Buffer size of serial string |
| 366 | |
| 367 | \retval 0: all fine |
| 368 | \retval -1: wrong arguments |
| 369 | \retval -4: unable to open device |
| 370 | \retval -7: get product manufacturer failed |
| 371 | \retval -8: get product description failed |
| 372 | \retval -9: get serial number failed |
| 373 | \retval -11: libusb_get_device_descriptor() failed |
| 374 | */ |
| 375 | int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev, |
| 376 | char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len) |
| 377 | { |
| 378 | struct libusb_device_descriptor desc; |
| 379 | |
| 380 | if ((ftdi==NULL) || (dev==NULL)) |
| 381 | return -1; |
| 382 | |
| 383 | if (libusb_open(dev, &ftdi->usb_dev) < 0) |
| 384 | ftdi_error_return(-4, "libusb_open() failed"); |
| 385 | |
| 386 | if (libusb_get_device_descriptor(dev, &desc) < 0) |
| 387 | ftdi_error_return(-11, "libusb_get_device_descriptor() failed"); |
| 388 | |
| 389 | if (manufacturer != NULL) |
| 390 | { |
| 391 | if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0) |
| 392 | { |
| 393 | ftdi_usb_close_internal (ftdi); |
| 394 | ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed"); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if (description != NULL) |
| 399 | { |
| 400 | if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0) |
| 401 | { |
| 402 | ftdi_usb_close_internal (ftdi); |
| 403 | ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed"); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | if (serial != NULL) |
| 408 | { |
| 409 | if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0) |
| 410 | { |
| 411 | ftdi_usb_close_internal (ftdi); |
| 412 | ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed"); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | ftdi_usb_close_internal (ftdi); |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Internal function to determine the maximum packet size. |
| 423 | * \param ftdi pointer to ftdi_context |
| 424 | * \param dev libusb usb_dev to use |
| 425 | * \retval Maximum packet size for this device |
| 426 | */ |
| 427 | static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev) |
| 428 | { |
| 429 | struct libusb_device_descriptor desc; |
| 430 | struct libusb_config_descriptor *config0; |
| 431 | unsigned int packet_size; |
| 432 | |
| 433 | // Sanity check |
| 434 | if (ftdi == NULL || dev == NULL) |
| 435 | return 64; |
| 436 | |
| 437 | // Determine maximum packet size. Init with default value. |
| 438 | // New hi-speed devices from FTDI use a packet size of 512 bytes |
| 439 | // but could be connected to a normal speed USB hub -> 64 bytes packet size. |
| 440 | if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H ) |
| 441 | packet_size = 512; |
| 442 | else |
| 443 | packet_size = 64; |
| 444 | |
| 445 | if (libusb_get_device_descriptor(dev, &desc) < 0) |
| 446 | return packet_size; |
| 447 | |
| 448 | if (libusb_get_config_descriptor(dev, 0, &config0) < 0) |
| 449 | return packet_size; |
| 450 | |
| 451 | if (desc.bNumConfigurations > 0) |
| 452 | { |
| 453 | if (ftdi->interface < config0->bNumInterfaces) |
| 454 | { |
| 455 | struct libusb_interface interface = config0->interface[ftdi->interface]; |
| 456 | if (interface.num_altsetting > 0) |
| 457 | { |
| 458 | struct libusb_interface_descriptor descriptor = interface.altsetting[0]; |
| 459 | if (descriptor.bNumEndpoints > 0) |
| 460 | { |
| 461 | packet_size = descriptor.endpoint[0].wMaxPacketSize; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | libusb_free_config_descriptor (config0); |
| 468 | return packet_size; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | Opens a ftdi device given by an usb_device. |
| 473 | |
| 474 | \param ftdi pointer to ftdi_context |
| 475 | \param dev libusb usb_dev to use |
| 476 | |
| 477 | \retval 0: all fine |
| 478 | \retval -3: unable to config device |
| 479 | \retval -4: unable to open device |
| 480 | \retval -5: unable to claim device |
| 481 | \retval -6: reset failed |
| 482 | \retval -7: set baudrate failed |
| 483 | \retval -8: ftdi context invalid |
| 484 | \retval -9: libusb_get_device_descriptor() failed |
| 485 | \retval -10: libusb_get_config_descriptor() failed |
| 486 | \retval -11: libusb_detach_kernel_driver() failed |
| 487 | \retval -12: libusb_get_configuration() failed |
| 488 | */ |
| 489 | int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev) |
| 490 | { |
| 491 | struct libusb_device_descriptor desc; |
| 492 | struct libusb_config_descriptor *config0; |
| 493 | int cfg, cfg0, detach_errno = 0; |
| 494 | |
| 495 | if (ftdi == NULL) |
| 496 | ftdi_error_return(-8, "ftdi context invalid"); |
| 497 | |
| 498 | if (libusb_open(dev, &ftdi->usb_dev) < 0) |
| 499 | ftdi_error_return(-4, "libusb_open() failed"); |
| 500 | |
| 501 | if (libusb_get_device_descriptor(dev, &desc) < 0) |
| 502 | ftdi_error_return(-9, "libusb_get_device_descriptor() failed"); |
| 503 | |
| 504 | if (libusb_get_config_descriptor(dev, 0, &config0) < 0) |
| 505 | ftdi_error_return(-10, "libusb_get_config_descriptor() failed"); |
| 506 | cfg0 = config0->bConfigurationValue; |
| 507 | libusb_free_config_descriptor (config0); |
| 508 | |
| 509 | // Try to detach ftdi_sio kernel module. |
| 510 | // |
| 511 | // The return code is kept in a separate variable and only parsed |
| 512 | // if usb_set_configuration() or usb_claim_interface() fails as the |
| 513 | // detach operation might be denied and everything still works fine. |
| 514 | // Likely scenario is a static ftdi_sio kernel module. |
| 515 | if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE) |
| 516 | { |
| 517 | if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0) |
| 518 | detach_errno = errno; |
| 519 | } |
| 520 | |
| 521 | if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0) |
| 522 | ftdi_error_return(-12, "libusb_get_configuration () failed"); |
| 523 | // set configuration (needed especially for windows) |
| 524 | // tolerate EBUSY: one device with one configuration, but two interfaces |
| 525 | // and libftdi sessions to both interfaces (e.g. FT2232) |
| 526 | if (desc.bNumConfigurations > 0 && cfg != cfg0) |
| 527 | { |
| 528 | if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0) |
| 529 | { |
| 530 | ftdi_usb_close_internal (ftdi); |
| 531 | if (detach_errno == EPERM) |
| 532 | { |
| 533 | ftdi_error_return(-8, "inappropriate permissions on device!"); |
| 534 | } |
| 535 | else |
| 536 | { |
| 537 | ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use"); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0) |
| 543 | { |
| 544 | ftdi_usb_close_internal (ftdi); |
| 545 | if (detach_errno == EPERM) |
| 546 | { |
| 547 | ftdi_error_return(-8, "inappropriate permissions on device!"); |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use"); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (ftdi_usb_reset (ftdi) != 0) |
| 556 | { |
| 557 | ftdi_usb_close_internal (ftdi); |
| 558 | ftdi_error_return(-6, "ftdi_usb_reset failed"); |
| 559 | } |
| 560 | |
| 561 | // Try to guess chip type |
| 562 | // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0 |
| 563 | if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200 |
| 564 | && desc.iSerialNumber == 0)) |
| 565 | ftdi->type = TYPE_BM; |
| 566 | else if (desc.bcdDevice == 0x200) |
| 567 | ftdi->type = TYPE_AM; |
| 568 | else if (desc.bcdDevice == 0x500) |
| 569 | ftdi->type = TYPE_2232C; |
| 570 | else if (desc.bcdDevice == 0x600) |
| 571 | ftdi->type = TYPE_R; |
| 572 | else if (desc.bcdDevice == 0x700) |
| 573 | ftdi->type = TYPE_2232H; |
| 574 | else if (desc.bcdDevice == 0x800) |
| 575 | ftdi->type = TYPE_4232H; |
| 576 | else if (desc.bcdDevice == 0x900) |
| 577 | ftdi->type = TYPE_232H; |
| 578 | |
| 579 | // Determine maximum packet size |
| 580 | ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev); |
| 581 | |
| 582 | if (ftdi_set_baudrate (ftdi, 9600) != 0) |
| 583 | { |
| 584 | ftdi_usb_close_internal (ftdi); |
| 585 | ftdi_error_return(-7, "set baudrate failed"); |
| 586 | } |
| 587 | |
| 588 | ftdi_error_return(0, "all fine"); |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | Opens the first device with a given vendor and product ids. |
| 593 | |
| 594 | \param ftdi pointer to ftdi_context |
| 595 | \param vendor Vendor ID |
| 596 | \param product Product ID |
| 597 | |
| 598 | \retval same as ftdi_usb_open_desc() |
| 599 | */ |
| 600 | int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) |
| 601 | { |
| 602 | return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | Opens the first device with a given, vendor id, product id, |
| 607 | description and serial. |
| 608 | |
| 609 | \param ftdi pointer to ftdi_context |
| 610 | \param vendor Vendor ID |
| 611 | \param product Product ID |
| 612 | \param description Description to search for. Use NULL if not needed. |
| 613 | \param serial Serial to search for. Use NULL if not needed. |
| 614 | |
| 615 | \retval 0: all fine |
| 616 | \retval -3: usb device not found |
| 617 | \retval -4: unable to open device |
| 618 | \retval -5: unable to claim device |
| 619 | \retval -6: reset failed |
| 620 | \retval -7: set baudrate failed |
| 621 | \retval -8: get product description failed |
| 622 | \retval -9: get serial number failed |
| 623 | \retval -12: libusb_get_device_list() failed |
| 624 | \retval -13: libusb_get_device_descriptor() failed |
| 625 | */ |
| 626 | int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, |
| 627 | const char* description, const char* serial) |
| 628 | { |
| 629 | return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0); |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | Opens the index-th device with a given, vendor id, product id, |
| 634 | description and serial. |
| 635 | |
| 636 | \param ftdi pointer to ftdi_context |
| 637 | \param vendor Vendor ID |
| 638 | \param product Product ID |
| 639 | \param description Description to search for. Use NULL if not needed. |
| 640 | \param serial Serial to search for. Use NULL if not needed. |
| 641 | \param index Number of matching device to open if there are more than one, starts with 0. |
| 642 | |
| 643 | \retval 0: all fine |
| 644 | \retval -1: usb_find_busses() failed |
| 645 | \retval -2: usb_find_devices() failed |
| 646 | \retval -3: usb device not found |
| 647 | \retval -4: unable to open device |
| 648 | \retval -5: unable to claim device |
| 649 | \retval -6: reset failed |
| 650 | \retval -7: set baudrate failed |
| 651 | \retval -8: get product description failed |
| 652 | \retval -9: get serial number failed |
| 653 | \retval -10: unable to close device |
| 654 | \retval -11: ftdi context invalid |
| 655 | */ |
| 656 | int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product, |
| 657 | const char* description, const char* serial, unsigned int index) |
| 658 | { |
| 659 | libusb_device *dev; |
| 660 | libusb_device **devs; |
| 661 | char string[256]; |
| 662 | int i = 0; |
| 663 | |
| 664 | if (ftdi == NULL) |
| 665 | ftdi_error_return(-11, "ftdi context invalid"); |
| 666 | |
| 667 | if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0) |
| 668 | ftdi_error_return(-12, "libusb_get_device_list() failed"); |
| 669 | |
| 670 | while ((dev = devs[i++]) != NULL) |
| 671 | { |
| 672 | struct libusb_device_descriptor desc; |
| 673 | int res; |
| 674 | |
| 675 | if (libusb_get_device_descriptor(dev, &desc) < 0) |
| 676 | ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs); |
| 677 | |
| 678 | if (desc.idVendor == vendor && desc.idProduct == product) |
| 679 | { |
| 680 | if (libusb_open(dev, &ftdi->usb_dev) < 0) |
| 681 | ftdi_error_return_free_device_list(-4, "usb_open() failed", devs); |
| 682 | |
| 683 | if (description != NULL) |
| 684 | { |
| 685 | if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0) |
| 686 | { |
| 687 | ftdi_usb_close_internal (ftdi); |
| 688 | ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs); |
| 689 | } |
| 690 | if (strncmp(string, description, sizeof(string)) != 0) |
| 691 | { |
| 692 | ftdi_usb_close_internal (ftdi); |
| 693 | continue; |
| 694 | } |
| 695 | } |
| 696 | if (serial != NULL) |
| 697 | { |
| 698 | if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0) |
| 699 | { |
| 700 | ftdi_usb_close_internal (ftdi); |
| 701 | ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs); |
| 702 | } |
| 703 | if (strncmp(string, serial, sizeof(string)) != 0) |
| 704 | { |
| 705 | ftdi_usb_close_internal (ftdi); |
| 706 | continue; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | ftdi_usb_close_internal (ftdi); |
| 711 | |
| 712 | if (index > 0) |
| 713 | { |
| 714 | index--; |
| 715 | continue; |
| 716 | } |
| 717 | |
| 718 | res = ftdi_usb_open_dev(ftdi, dev); |
| 719 | libusb_free_device_list(devs,1); |
| 720 | return res; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | // device not found |
| 725 | ftdi_error_return_free_device_list(-3, "device not found", devs); |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | Opens the ftdi-device described by a description-string. |
| 730 | Intended to be used for parsing a device-description given as commandline argument. |
| 731 | |
| 732 | \param ftdi pointer to ftdi_context |
| 733 | \param description NULL-terminated description-string, using this format: |
| 734 | \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/) |
| 735 | \li <tt>i:\<vendor>:\<product></tt> first device with given vendor and product id, ids can be decimal, octal (preceded by "0") or hex (preceded by "0x") |
| 736 | \li <tt>i:\<vendor>:\<product>:\<index></tt> as above with index being the number of the device (starting with 0) if there are more than one |
| 737 | \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string |
| 738 | |
| 739 | \note The description format may be extended in later versions. |
| 740 | |
| 741 | \retval 0: all fine |
| 742 | \retval -2: libusb_get_device_list() failed |
| 743 | \retval -3: usb device not found |
| 744 | \retval -4: unable to open device |
| 745 | \retval -5: unable to claim device |
| 746 | \retval -6: reset failed |
| 747 | \retval -7: set baudrate failed |
| 748 | \retval -8: get product description failed |
| 749 | \retval -9: get serial number failed |
| 750 | \retval -10: unable to close device |
| 751 | \retval -11: illegal description format |
| 752 | \retval -12: ftdi context invalid |
| 753 | */ |
| 754 | int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description) |
| 755 | { |
| 756 | if (ftdi == NULL) |
| 757 | ftdi_error_return(-12, "ftdi context invalid"); |
| 758 | |
| 759 | if (description[0] == 0 || description[1] != ':') |
| 760 | ftdi_error_return(-11, "illegal description format"); |
| 761 | |
| 762 | if (description[0] == 'd') |
| 763 | { |
| 764 | libusb_device *dev; |
| 765 | libusb_device **devs; |
| 766 | unsigned int bus_number, device_address; |
| 767 | int i = 0; |
| 768 | |
| 769 | if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0) |
| 770 | ftdi_error_return(-2, "libusb_get_device_list() failed"); |
| 771 | |
| 772 | /* XXX: This doesn't handle symlinks/odd paths/etc... */ |
| 773 | if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2) |
| 774 | ftdi_error_return_free_device_list(-11, "illegal description format", devs); |
| 775 | |
| 776 | while ((dev = devs[i++]) != NULL) |
| 777 | { |
| 778 | int ret; |
| 779 | if (bus_number == libusb_get_bus_number (dev) |
| 780 | && device_address == libusb_get_device_address (dev)) |
| 781 | { |
| 782 | ret = ftdi_usb_open_dev(ftdi, dev); |
| 783 | libusb_free_device_list(devs,1); |
| 784 | return ret; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | // device not found |
| 789 | ftdi_error_return_free_device_list(-3, "device not found", devs); |
| 790 | } |
| 791 | else if (description[0] == 'i' || description[0] == 's') |
| 792 | { |
| 793 | unsigned int vendor; |
| 794 | unsigned int product; |
| 795 | unsigned int index=0; |
| 796 | const char *serial=NULL; |
| 797 | const char *startp, *endp; |
| 798 | |
| 799 | errno=0; |
| 800 | startp=description+2; |
| 801 | vendor=strtoul((char*)startp,(char**)&endp,0); |
| 802 | if (*endp != ':' || endp == startp || errno != 0) |
| 803 | ftdi_error_return(-11, "illegal description format"); |
| 804 | |
| 805 | startp=endp+1; |
| 806 | product=strtoul((char*)startp,(char**)&endp,0); |
| 807 | if (endp == startp || errno != 0) |
| 808 | ftdi_error_return(-11, "illegal description format"); |
| 809 | |
| 810 | if (description[0] == 'i' && *endp != 0) |
| 811 | { |
| 812 | /* optional index field in i-mode */ |
| 813 | if (*endp != ':') |
| 814 | ftdi_error_return(-11, "illegal description format"); |
| 815 | |
| 816 | startp=endp+1; |
| 817 | index=strtoul((char*)startp,(char**)&endp,0); |
| 818 | if (*endp != 0 || endp == startp || errno != 0) |
| 819 | ftdi_error_return(-11, "illegal description format"); |
| 820 | } |
| 821 | if (description[0] == 's') |
| 822 | { |
| 823 | if (*endp != ':') |
| 824 | ftdi_error_return(-11, "illegal description format"); |
| 825 | |
| 826 | /* rest of the description is the serial */ |
| 827 | serial=endp+1; |
| 828 | } |
| 829 | |
| 830 | return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index); |
| 831 | } |
| 832 | else |
| 833 | { |
| 834 | ftdi_error_return(-11, "illegal description format"); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | Resets the ftdi device. |
| 840 | |
| 841 | \param ftdi pointer to ftdi_context |
| 842 | |
| 843 | \retval 0: all fine |
| 844 | \retval -1: FTDI reset failed |
| 845 | \retval -2: USB device unavailable |
| 846 | */ |
| 847 | int ftdi_usb_reset(struct ftdi_context *ftdi) |
| 848 | { |
| 849 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 850 | ftdi_error_return(-2, "USB device unavailable"); |
| 851 | |
| 852 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 853 | SIO_RESET_REQUEST, SIO_RESET_SIO, |
| 854 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 855 | ftdi_error_return(-1,"FTDI reset failed"); |
| 856 | |
| 857 | // Invalidate data in the readbuffer |
| 858 | ftdi->readbuffer_offset = 0; |
| 859 | ftdi->readbuffer_remaining = 0; |
| 860 | |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | Clears the read buffer on the chip and the internal read buffer. |
| 866 | |
| 867 | \param ftdi pointer to ftdi_context |
| 868 | |
| 869 | \retval 0: all fine |
| 870 | \retval -1: read buffer purge failed |
| 871 | \retval -2: USB device unavailable |
| 872 | */ |
| 873 | int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi) |
| 874 | { |
| 875 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 876 | ftdi_error_return(-2, "USB device unavailable"); |
| 877 | |
| 878 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 879 | SIO_RESET_REQUEST, SIO_RESET_PURGE_RX, |
| 880 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 881 | ftdi_error_return(-1, "FTDI purge of RX buffer failed"); |
| 882 | |
| 883 | // Invalidate data in the readbuffer |
| 884 | ftdi->readbuffer_offset = 0; |
| 885 | ftdi->readbuffer_remaining = 0; |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | Clears the write buffer on the chip. |
| 892 | |
| 893 | \param ftdi pointer to ftdi_context |
| 894 | |
| 895 | \retval 0: all fine |
| 896 | \retval -1: write buffer purge failed |
| 897 | \retval -2: USB device unavailable |
| 898 | */ |
| 899 | int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi) |
| 900 | { |
| 901 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 902 | ftdi_error_return(-2, "USB device unavailable"); |
| 903 | |
| 904 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 905 | SIO_RESET_REQUEST, SIO_RESET_PURGE_TX, |
| 906 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 907 | ftdi_error_return(-1, "FTDI purge of TX buffer failed"); |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | Clears the buffers on the chip and the internal read buffer. |
| 914 | |
| 915 | \param ftdi pointer to ftdi_context |
| 916 | |
| 917 | \retval 0: all fine |
| 918 | \retval -1: read buffer purge failed |
| 919 | \retval -2: write buffer purge failed |
| 920 | \retval -3: USB device unavailable |
| 921 | */ |
| 922 | int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) |
| 923 | { |
| 924 | int result; |
| 925 | |
| 926 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 927 | ftdi_error_return(-3, "USB device unavailable"); |
| 928 | |
| 929 | result = ftdi_usb_purge_rx_buffer(ftdi); |
| 930 | if (result < 0) |
| 931 | return -1; |
| 932 | |
| 933 | result = ftdi_usb_purge_tx_buffer(ftdi); |
| 934 | if (result < 0) |
| 935 | return -2; |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | |
| 941 | |
| 942 | /** |
| 943 | Closes the ftdi device. Call ftdi_deinit() if you're cleaning up. |
| 944 | |
| 945 | \param ftdi pointer to ftdi_context |
| 946 | |
| 947 | \retval 0: all fine |
| 948 | \retval -1: usb_release failed |
| 949 | \retval -3: ftdi context invalid |
| 950 | */ |
| 951 | int ftdi_usb_close(struct ftdi_context *ftdi) |
| 952 | { |
| 953 | int rtn = 0; |
| 954 | |
| 955 | if (ftdi == NULL) |
| 956 | ftdi_error_return(-3, "ftdi context invalid"); |
| 957 | |
| 958 | if (ftdi->usb_dev != NULL) |
| 959 | if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0) |
| 960 | rtn = -1; |
| 961 | |
| 962 | ftdi_usb_close_internal (ftdi); |
| 963 | |
| 964 | return rtn; |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | ftdi_convert_baudrate returns nearest supported baud rate to that requested. |
| 969 | Function is only used internally |
| 970 | \internal |
| 971 | */ |
| 972 | static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi, |
| 973 | unsigned short *value, unsigned short *index) |
| 974 | { |
| 975 | static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1}; |
| 976 | static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3}; |
| 977 | static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7}; |
| 978 | int divisor, best_divisor, best_baud, best_baud_diff; |
| 979 | unsigned long encoded_divisor; |
| 980 | int i; |
| 981 | |
| 982 | if (baudrate <= 0) |
| 983 | { |
| 984 | // Return error |
| 985 | return -1; |
| 986 | } |
| 987 | |
| 988 | divisor = 24000000 / baudrate; |
| 989 | |
| 990 | if (ftdi->type == TYPE_AM) |
| 991 | { |
| 992 | // Round down to supported fraction (AM only) |
| 993 | divisor -= am_adjust_dn[divisor & 7]; |
| 994 | } |
| 995 | |
| 996 | // Try this divisor and the one above it (because division rounds down) |
| 997 | best_divisor = 0; |
| 998 | best_baud = 0; |
| 999 | best_baud_diff = 0; |
| 1000 | for (i = 0; i < 2; i++) |
| 1001 | { |
| 1002 | int try_divisor = divisor + i; |
| 1003 | int baud_estimate; |
| 1004 | int baud_diff; |
| 1005 | |
| 1006 | // Round up to supported divisor value |
| 1007 | if (try_divisor <= 8) |
| 1008 | { |
| 1009 | // Round up to minimum supported divisor |
| 1010 | try_divisor = 8; |
| 1011 | } |
| 1012 | else if (ftdi->type != TYPE_AM && try_divisor < 12) |
| 1013 | { |
| 1014 | // BM doesn't support divisors 9 through 11 inclusive |
| 1015 | try_divisor = 12; |
| 1016 | } |
| 1017 | else if (divisor < 16) |
| 1018 | { |
| 1019 | // AM doesn't support divisors 9 through 15 inclusive |
| 1020 | try_divisor = 16; |
| 1021 | } |
| 1022 | else |
| 1023 | { |
| 1024 | if (ftdi->type == TYPE_AM) |
| 1025 | { |
| 1026 | // Round up to supported fraction (AM only) |
| 1027 | try_divisor += am_adjust_up[try_divisor & 7]; |
| 1028 | if (try_divisor > 0x1FFF8) |
| 1029 | { |
| 1030 | // Round down to maximum supported divisor value (for AM) |
| 1031 | try_divisor = 0x1FFF8; |
| 1032 | } |
| 1033 | } |
| 1034 | else |
| 1035 | { |
| 1036 | if (try_divisor > 0x1FFFF) |
| 1037 | { |
| 1038 | // Round down to maximum supported divisor value (for BM) |
| 1039 | try_divisor = 0x1FFFF; |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | // Get estimated baud rate (to nearest integer) |
| 1044 | baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor; |
| 1045 | // Get absolute difference from requested baud rate |
| 1046 | if (baud_estimate < baudrate) |
| 1047 | { |
| 1048 | baud_diff = baudrate - baud_estimate; |
| 1049 | } |
| 1050 | else |
| 1051 | { |
| 1052 | baud_diff = baud_estimate - baudrate; |
| 1053 | } |
| 1054 | if (i == 0 || baud_diff < best_baud_diff) |
| 1055 | { |
| 1056 | // Closest to requested baud rate so far |
| 1057 | best_divisor = try_divisor; |
| 1058 | best_baud = baud_estimate; |
| 1059 | best_baud_diff = baud_diff; |
| 1060 | if (baud_diff == 0) |
| 1061 | { |
| 1062 | // Spot on! No point trying |
| 1063 | break; |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | // Encode the best divisor value |
| 1068 | encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14); |
| 1069 | // Deal with special cases for encoded value |
| 1070 | if (encoded_divisor == 1) |
| 1071 | { |
| 1072 | encoded_divisor = 0; // 3000000 baud |
| 1073 | } |
| 1074 | else if (encoded_divisor == 0x4001) |
| 1075 | { |
| 1076 | encoded_divisor = 1; // 2000000 baud (BM only) |
| 1077 | } |
| 1078 | // Split into "value" and "index" values |
| 1079 | *value = (unsigned short)(encoded_divisor & 0xFFFF); |
| 1080 | if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H ) |
| 1081 | { |
| 1082 | *index = (unsigned short)(encoded_divisor >> 8); |
| 1083 | *index &= 0xFF00; |
| 1084 | *index |= ftdi->index; |
| 1085 | } |
| 1086 | else |
| 1087 | *index = (unsigned short)(encoded_divisor >> 16); |
| 1088 | |
| 1089 | // Return the nearest baud rate |
| 1090 | return best_baud; |
| 1091 | } |
| 1092 | |
| 1093 | /** |
| 1094 | Sets the chip baud rate |
| 1095 | |
| 1096 | \param ftdi pointer to ftdi_context |
| 1097 | \param baudrate baud rate to set |
| 1098 | |
| 1099 | \retval 0: all fine |
| 1100 | \retval -1: invalid baudrate |
| 1101 | \retval -2: setting baudrate failed |
| 1102 | \retval -3: USB device unavailable |
| 1103 | */ |
| 1104 | int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) |
| 1105 | { |
| 1106 | unsigned short value, index; |
| 1107 | int actual_baudrate; |
| 1108 | |
| 1109 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1110 | ftdi_error_return(-3, "USB device unavailable"); |
| 1111 | |
| 1112 | if (ftdi->bitbang_enabled) |
| 1113 | { |
| 1114 | baudrate = baudrate*4; |
| 1115 | } |
| 1116 | |
| 1117 | actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index); |
| 1118 | if (actual_baudrate <= 0) |
| 1119 | ftdi_error_return (-1, "Silly baudrate <= 0."); |
| 1120 | |
| 1121 | // Check within tolerance (about 5%) |
| 1122 | if ((actual_baudrate * 2 < baudrate /* Catch overflows */ ) |
| 1123 | || ((actual_baudrate < baudrate) |
| 1124 | ? (actual_baudrate * 21 < baudrate * 20) |
| 1125 | : (baudrate * 21 < actual_baudrate * 20))) |
| 1126 | ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4"); |
| 1127 | |
| 1128 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 1129 | SIO_SET_BAUDRATE_REQUEST, value, |
| 1130 | index, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 1131 | ftdi_error_return (-2, "Setting new baudrate failed"); |
| 1132 | |
| 1133 | ftdi->baudrate = baudrate; |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | /** |
| 1138 | Set (RS232) line characteristics. |
| 1139 | The break type can only be set via ftdi_set_line_property2() |
| 1140 | and defaults to "off". |
| 1141 | |
| 1142 | \param ftdi pointer to ftdi_context |
| 1143 | \param bits Number of bits |
| 1144 | \param sbit Number of stop bits |
| 1145 | \param parity Parity mode |
| 1146 | |
| 1147 | \retval 0: all fine |
| 1148 | \retval -1: Setting line property failed |
| 1149 | */ |
| 1150 | int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits, |
| 1151 | enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity) |
| 1152 | { |
| 1153 | return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF); |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | Set (RS232) line characteristics |
| 1158 | |
| 1159 | \param ftdi pointer to ftdi_context |
| 1160 | \param bits Number of bits |
| 1161 | \param sbit Number of stop bits |
| 1162 | \param parity Parity mode |
| 1163 | \param break_type Break type |
| 1164 | |
| 1165 | \retval 0: all fine |
| 1166 | \retval -1: Setting line property failed |
| 1167 | \retval -2: USB device unavailable |
| 1168 | */ |
| 1169 | int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits, |
| 1170 | enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity, |
| 1171 | enum ftdi_break_type break_type) |
| 1172 | { |
| 1173 | unsigned short value = bits; |
| 1174 | |
| 1175 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1176 | ftdi_error_return(-2, "USB device unavailable"); |
| 1177 | |
| 1178 | switch (parity) |
| 1179 | { |
| 1180 | case NONE: |
| 1181 | value |= (0x00 << 8); |
| 1182 | break; |
| 1183 | case ODD: |
| 1184 | value |= (0x01 << 8); |
| 1185 | break; |
| 1186 | case EVEN: |
| 1187 | value |= (0x02 << 8); |
| 1188 | break; |
| 1189 | case MARK: |
| 1190 | value |= (0x03 << 8); |
| 1191 | break; |
| 1192 | case SPACE: |
| 1193 | value |= (0x04 << 8); |
| 1194 | break; |
| 1195 | } |
| 1196 | |
| 1197 | switch (sbit) |
| 1198 | { |
| 1199 | case STOP_BIT_1: |
| 1200 | value |= (0x00 << 11); |
| 1201 | break; |
| 1202 | case STOP_BIT_15: |
| 1203 | value |= (0x01 << 11); |
| 1204 | break; |
| 1205 | case STOP_BIT_2: |
| 1206 | value |= (0x02 << 11); |
| 1207 | break; |
| 1208 | } |
| 1209 | |
| 1210 | switch (break_type) |
| 1211 | { |
| 1212 | case BREAK_OFF: |
| 1213 | value |= (0x00 << 14); |
| 1214 | break; |
| 1215 | case BREAK_ON: |
| 1216 | value |= (0x01 << 14); |
| 1217 | break; |
| 1218 | } |
| 1219 | |
| 1220 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 1221 | SIO_SET_DATA_REQUEST, value, |
| 1222 | ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 1223 | ftdi_error_return (-1, "Setting new line property failed"); |
| 1224 | |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
| 1228 | /** |
| 1229 | Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip |
| 1230 | |
| 1231 | \param ftdi pointer to ftdi_context |
| 1232 | \param buf Buffer with the data |
| 1233 | \param size Size of the buffer |
| 1234 | |
| 1235 | \retval -666: USB device unavailable |
| 1236 | \retval <0: error code from usb_bulk_write() |
| 1237 | \retval >0: number of bytes written |
| 1238 | */ |
| 1239 | int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) |
| 1240 | { |
| 1241 | int offset = 0; |
| 1242 | int actual_length; |
| 1243 | |
| 1244 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1245 | ftdi_error_return(-666, "USB device unavailable"); |
| 1246 | |
| 1247 | while (offset < size) |
| 1248 | { |
| 1249 | int write_size = ftdi->writebuffer_chunksize; |
| 1250 | |
| 1251 | if (offset+write_size > size) |
| 1252 | write_size = size-offset; |
| 1253 | |
| 1254 | if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0) |
| 1255 | ftdi_error_return(-1, "usb bulk write failed"); |
| 1256 | |
| 1257 | offset += actual_length; |
| 1258 | } |
| 1259 | |
| 1260 | return offset; |
| 1261 | } |
| 1262 | |
| 1263 | static void ftdi_read_data_cb(struct libusb_transfer *transfer) |
| 1264 | { |
| 1265 | struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data; |
| 1266 | struct ftdi_context *ftdi = tc->ftdi; |
| 1267 | int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret; |
| 1268 | |
| 1269 | packet_size = ftdi->max_packet_size; |
| 1270 | |
| 1271 | actual_length = transfer->actual_length; |
| 1272 | |
| 1273 | if (actual_length > 2) |
| 1274 | { |
| 1275 | // skip FTDI status bytes. |
| 1276 | // Maybe stored in the future to enable modem use |
| 1277 | num_of_chunks = actual_length / packet_size; |
| 1278 | chunk_remains = actual_length % packet_size; |
| 1279 | //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset); |
| 1280 | |
| 1281 | ftdi->readbuffer_offset += 2; |
| 1282 | actual_length -= 2; |
| 1283 | |
| 1284 | if (actual_length > packet_size - 2) |
| 1285 | { |
| 1286 | for (i = 1; i < num_of_chunks; i++) |
| 1287 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i, |
| 1288 | ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i, |
| 1289 | packet_size - 2); |
| 1290 | if (chunk_remains > 2) |
| 1291 | { |
| 1292 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i, |
| 1293 | ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i, |
| 1294 | chunk_remains-2); |
| 1295 | actual_length -= 2*num_of_chunks; |
| 1296 | } |
| 1297 | else |
| 1298 | actual_length -= 2*(num_of_chunks-1)+chunk_remains; |
| 1299 | } |
| 1300 | |
| 1301 | if (actual_length > 0) |
| 1302 | { |
| 1303 | // data still fits in buf? |
| 1304 | if (tc->offset + actual_length <= tc->size) |
| 1305 | { |
| 1306 | memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length); |
| 1307 | //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]); |
| 1308 | tc->offset += actual_length; |
| 1309 | |
| 1310 | ftdi->readbuffer_offset = 0; |
| 1311 | ftdi->readbuffer_remaining = 0; |
| 1312 | |
| 1313 | /* Did we read exactly the right amount of bytes? */ |
| 1314 | if (tc->offset == tc->size) |
| 1315 | { |
| 1316 | //printf("read_data exact rem %d offset %d\n", |
| 1317 | //ftdi->readbuffer_remaining, offset); |
| 1318 | tc->completed = 1; |
| 1319 | return; |
| 1320 | } |
| 1321 | } |
| 1322 | else |
| 1323 | { |
| 1324 | // only copy part of the data or size <= readbuffer_chunksize |
| 1325 | int part_size = tc->size - tc->offset; |
| 1326 | memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size); |
| 1327 | tc->offset += part_size; |
| 1328 | |
| 1329 | ftdi->readbuffer_offset += part_size; |
| 1330 | ftdi->readbuffer_remaining = actual_length - part_size; |
| 1331 | |
| 1332 | /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n", |
| 1333 | part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */ |
| 1334 | tc->completed = 1; |
| 1335 | return; |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | ret = libusb_submit_transfer (transfer); |
| 1340 | if (ret < 0) |
| 1341 | tc->completed = 1; |
| 1342 | } |
| 1343 | |
| 1344 | |
| 1345 | static void ftdi_write_data_cb(struct libusb_transfer *transfer) |
| 1346 | { |
| 1347 | struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data; |
| 1348 | struct ftdi_context *ftdi = tc->ftdi; |
| 1349 | |
| 1350 | tc->offset += transfer->actual_length; |
| 1351 | |
| 1352 | if (tc->offset == tc->size) |
| 1353 | { |
| 1354 | tc->completed = 1; |
| 1355 | } |
| 1356 | else |
| 1357 | { |
| 1358 | int write_size = ftdi->writebuffer_chunksize; |
| 1359 | int ret; |
| 1360 | |
| 1361 | if (tc->offset + write_size > tc->size) |
| 1362 | write_size = tc->size - tc->offset; |
| 1363 | |
| 1364 | transfer->length = write_size; |
| 1365 | transfer->buffer = tc->buf + tc->offset; |
| 1366 | ret = libusb_submit_transfer (transfer); |
| 1367 | if (ret < 0) |
| 1368 | tc->completed = 1; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | |
| 1373 | /** |
| 1374 | Writes data to the chip. Does not wait for completion of the transfer |
| 1375 | nor does it make sure that the transfer was successful. |
| 1376 | |
| 1377 | Use libusb 1.0 asynchronous API. |
| 1378 | |
| 1379 | \param ftdi pointer to ftdi_context |
| 1380 | \param buf Buffer with the data |
| 1381 | \param size Size of the buffer |
| 1382 | |
| 1383 | \retval NULL: Some error happens when submit transfer |
| 1384 | \retval !NULL: Pointer to a ftdi_transfer_control |
| 1385 | */ |
| 1386 | |
| 1387 | struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size) |
| 1388 | { |
| 1389 | struct ftdi_transfer_control *tc; |
| 1390 | struct libusb_transfer *transfer; |
| 1391 | int write_size, ret; |
| 1392 | |
| 1393 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1394 | return NULL; |
| 1395 | |
| 1396 | tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc)); |
| 1397 | if (!tc) |
| 1398 | return NULL; |
| 1399 | |
| 1400 | transfer = libusb_alloc_transfer(0); |
| 1401 | if (!transfer) |
| 1402 | { |
| 1403 | free(tc); |
| 1404 | return NULL; |
| 1405 | } |
| 1406 | |
| 1407 | tc->ftdi = ftdi; |
| 1408 | tc->completed = 0; |
| 1409 | tc->buf = buf; |
| 1410 | tc->size = size; |
| 1411 | tc->offset = 0; |
| 1412 | |
| 1413 | if (size < ftdi->writebuffer_chunksize) |
| 1414 | write_size = size; |
| 1415 | else |
| 1416 | write_size = ftdi->writebuffer_chunksize; |
| 1417 | |
| 1418 | libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf, |
| 1419 | write_size, ftdi_write_data_cb, tc, |
| 1420 | ftdi->usb_write_timeout); |
| 1421 | transfer->type = LIBUSB_TRANSFER_TYPE_BULK; |
| 1422 | |
| 1423 | ret = libusb_submit_transfer(transfer); |
| 1424 | if (ret < 0) |
| 1425 | { |
| 1426 | libusb_free_transfer(transfer); |
| 1427 | free(tc); |
| 1428 | return NULL; |
| 1429 | } |
| 1430 | tc->transfer = transfer; |
| 1431 | |
| 1432 | return tc; |
| 1433 | } |
| 1434 | |
| 1435 | /** |
| 1436 | Reads data from the chip. Does not wait for completion of the transfer |
| 1437 | nor does it make sure that the transfer was successful. |
| 1438 | |
| 1439 | Use libusb 1.0 asynchronous API. |
| 1440 | |
| 1441 | \param ftdi pointer to ftdi_context |
| 1442 | \param buf Buffer with the data |
| 1443 | \param size Size of the buffer |
| 1444 | |
| 1445 | \retval NULL: Some error happens when submit transfer |
| 1446 | \retval !NULL: Pointer to a ftdi_transfer_control |
| 1447 | */ |
| 1448 | |
| 1449 | struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size) |
| 1450 | { |
| 1451 | struct ftdi_transfer_control *tc; |
| 1452 | struct libusb_transfer *transfer; |
| 1453 | int ret; |
| 1454 | |
| 1455 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1456 | return NULL; |
| 1457 | |
| 1458 | tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc)); |
| 1459 | if (!tc) |
| 1460 | return NULL; |
| 1461 | |
| 1462 | tc->ftdi = ftdi; |
| 1463 | tc->buf = buf; |
| 1464 | tc->size = size; |
| 1465 | |
| 1466 | if (size <= ftdi->readbuffer_remaining) |
| 1467 | { |
| 1468 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size); |
| 1469 | |
| 1470 | // Fix offsets |
| 1471 | ftdi->readbuffer_remaining -= size; |
| 1472 | ftdi->readbuffer_offset += size; |
| 1473 | |
| 1474 | /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */ |
| 1475 | |
| 1476 | tc->completed = 1; |
| 1477 | tc->offset = size; |
| 1478 | tc->transfer = NULL; |
| 1479 | return tc; |
| 1480 | } |
| 1481 | |
| 1482 | tc->completed = 0; |
| 1483 | if (ftdi->readbuffer_remaining != 0) |
| 1484 | { |
| 1485 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining); |
| 1486 | |
| 1487 | tc->offset = ftdi->readbuffer_remaining; |
| 1488 | } |
| 1489 | else |
| 1490 | tc->offset = 0; |
| 1491 | |
| 1492 | transfer = libusb_alloc_transfer(0); |
| 1493 | if (!transfer) |
| 1494 | { |
| 1495 | free (tc); |
| 1496 | return NULL; |
| 1497 | } |
| 1498 | |
| 1499 | ftdi->readbuffer_remaining = 0; |
| 1500 | ftdi->readbuffer_offset = 0; |
| 1501 | |
| 1502 | libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi_read_data_cb, tc, ftdi->usb_read_timeout); |
| 1503 | transfer->type = LIBUSB_TRANSFER_TYPE_BULK; |
| 1504 | |
| 1505 | ret = libusb_submit_transfer(transfer); |
| 1506 | if (ret < 0) |
| 1507 | { |
| 1508 | libusb_free_transfer(transfer); |
| 1509 | free (tc); |
| 1510 | return NULL; |
| 1511 | } |
| 1512 | tc->transfer = transfer; |
| 1513 | |
| 1514 | return tc; |
| 1515 | } |
| 1516 | |
| 1517 | /** |
| 1518 | Wait for completion of the transfer. |
| 1519 | |
| 1520 | Use libusb 1.0 asynchronous API. |
| 1521 | |
| 1522 | \param tc pointer to ftdi_transfer_control |
| 1523 | |
| 1524 | \retval < 0: Some error happens |
| 1525 | \retval >= 0: Data size transferred |
| 1526 | */ |
| 1527 | |
| 1528 | int ftdi_transfer_data_done(struct ftdi_transfer_control *tc) |
| 1529 | { |
| 1530 | int ret; |
| 1531 | |
| 1532 | while (!tc->completed) |
| 1533 | { |
| 1534 | ret = libusb_handle_events(tc->ftdi->usb_ctx); |
| 1535 | if (ret < 0) |
| 1536 | { |
| 1537 | if (ret == LIBUSB_ERROR_INTERRUPTED) |
| 1538 | continue; |
| 1539 | libusb_cancel_transfer(tc->transfer); |
| 1540 | while (!tc->completed) |
| 1541 | if (libusb_handle_events(tc->ftdi->usb_ctx) < 0) |
| 1542 | break; |
| 1543 | libusb_free_transfer(tc->transfer); |
| 1544 | free (tc); |
| 1545 | return ret; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | ret = tc->offset; |
| 1550 | /** |
| 1551 | * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)" |
| 1552 | * at ftdi_read_data_submit(). Therefore, we need to check it here. |
| 1553 | **/ |
| 1554 | if (tc->transfer) |
| 1555 | { |
| 1556 | if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED) |
| 1557 | ret = -1; |
| 1558 | libusb_free_transfer(tc->transfer); |
| 1559 | } |
| 1560 | free(tc); |
| 1561 | return ret; |
| 1562 | } |
| 1563 | |
| 1564 | /** |
| 1565 | Configure write buffer chunk size. |
| 1566 | Default is 4096. |
| 1567 | |
| 1568 | \param ftdi pointer to ftdi_context |
| 1569 | \param chunksize Chunk size |
| 1570 | |
| 1571 | \retval 0: all fine |
| 1572 | \retval -1: ftdi context invalid |
| 1573 | */ |
| 1574 | int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
| 1575 | { |
| 1576 | if (ftdi == NULL) |
| 1577 | ftdi_error_return(-1, "ftdi context invalid"); |
| 1578 | |
| 1579 | ftdi->writebuffer_chunksize = chunksize; |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | /** |
| 1584 | Get write buffer chunk size. |
| 1585 | |
| 1586 | \param ftdi pointer to ftdi_context |
| 1587 | \param chunksize Pointer to store chunk size in |
| 1588 | |
| 1589 | \retval 0: all fine |
| 1590 | \retval -1: ftdi context invalid |
| 1591 | */ |
| 1592 | int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
| 1593 | { |
| 1594 | if (ftdi == NULL) |
| 1595 | ftdi_error_return(-1, "ftdi context invalid"); |
| 1596 | |
| 1597 | *chunksize = ftdi->writebuffer_chunksize; |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | /** |
| 1602 | Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip. |
| 1603 | |
| 1604 | Automatically strips the two modem status bytes transfered during every read. |
| 1605 | |
| 1606 | \param ftdi pointer to ftdi_context |
| 1607 | \param buf Buffer to store data in |
| 1608 | \param size Size of the buffer |
| 1609 | |
| 1610 | \retval -666: USB device unavailable |
| 1611 | \retval <0: error code from libusb_bulk_transfer() |
| 1612 | \retval 0: no data was available |
| 1613 | \retval >0: number of bytes read |
| 1614 | |
| 1615 | */ |
| 1616 | int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) |
| 1617 | { |
| 1618 | int offset = 0, ret, i, num_of_chunks, chunk_remains; |
| 1619 | int packet_size = ftdi->max_packet_size; |
| 1620 | int actual_length = 1; |
| 1621 | |
| 1622 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1623 | ftdi_error_return(-666, "USB device unavailable"); |
| 1624 | |
| 1625 | // Packet size sanity check (avoid division by zero) |
| 1626 | if (packet_size == 0) |
| 1627 | ftdi_error_return(-1, "max_packet_size is bogus (zero)"); |
| 1628 | |
| 1629 | // everything we want is still in the readbuffer? |
| 1630 | if (size <= ftdi->readbuffer_remaining) |
| 1631 | { |
| 1632 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size); |
| 1633 | |
| 1634 | // Fix offsets |
| 1635 | ftdi->readbuffer_remaining -= size; |
| 1636 | ftdi->readbuffer_offset += size; |
| 1637 | |
| 1638 | /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */ |
| 1639 | |
| 1640 | return size; |
| 1641 | } |
| 1642 | // something still in the readbuffer, but not enough to satisfy 'size'? |
| 1643 | if (ftdi->readbuffer_remaining != 0) |
| 1644 | { |
| 1645 | memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining); |
| 1646 | |
| 1647 | // Fix offset |
| 1648 | offset += ftdi->readbuffer_remaining; |
| 1649 | } |
| 1650 | // do the actual USB read |
| 1651 | while (offset < size && actual_length > 0) |
| 1652 | { |
| 1653 | ftdi->readbuffer_remaining = 0; |
| 1654 | ftdi->readbuffer_offset = 0; |
| 1655 | /* returns how much received */ |
| 1656 | ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout); |
| 1657 | if (ret < 0) |
| 1658 | ftdi_error_return(ret, "usb bulk read failed"); |
| 1659 | |
| 1660 | if (actual_length > 2) |
| 1661 | { |
| 1662 | // skip FTDI status bytes. |
| 1663 | // Maybe stored in the future to enable modem use |
| 1664 | num_of_chunks = actual_length / packet_size; |
| 1665 | chunk_remains = actual_length % packet_size; |
| 1666 | //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset); |
| 1667 | |
| 1668 | ftdi->readbuffer_offset += 2; |
| 1669 | actual_length -= 2; |
| 1670 | |
| 1671 | if (actual_length > packet_size - 2) |
| 1672 | { |
| 1673 | for (i = 1; i < num_of_chunks; i++) |
| 1674 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i, |
| 1675 | ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i, |
| 1676 | packet_size - 2); |
| 1677 | if (chunk_remains > 2) |
| 1678 | { |
| 1679 | memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i, |
| 1680 | ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i, |
| 1681 | chunk_remains-2); |
| 1682 | actual_length -= 2*num_of_chunks; |
| 1683 | } |
| 1684 | else |
| 1685 | actual_length -= 2*(num_of_chunks-1)+chunk_remains; |
| 1686 | } |
| 1687 | } |
| 1688 | else if (actual_length <= 2) |
| 1689 | { |
| 1690 | // no more data to read? |
| 1691 | return offset; |
| 1692 | } |
| 1693 | if (actual_length > 0) |
| 1694 | { |
| 1695 | // data still fits in buf? |
| 1696 | if (offset+actual_length <= size) |
| 1697 | { |
| 1698 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length); |
| 1699 | //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]); |
| 1700 | offset += actual_length; |
| 1701 | |
| 1702 | /* Did we read exactly the right amount of bytes? */ |
| 1703 | if (offset == size) |
| 1704 | //printf("read_data exact rem %d offset %d\n", |
| 1705 | //ftdi->readbuffer_remaining, offset); |
| 1706 | return offset; |
| 1707 | } |
| 1708 | else |
| 1709 | { |
| 1710 | // only copy part of the data or size <= readbuffer_chunksize |
| 1711 | int part_size = size-offset; |
| 1712 | memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size); |
| 1713 | |
| 1714 | ftdi->readbuffer_offset += part_size; |
| 1715 | ftdi->readbuffer_remaining = actual_length-part_size; |
| 1716 | offset += part_size; |
| 1717 | |
| 1718 | /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n", |
| 1719 | part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */ |
| 1720 | |
| 1721 | return offset; |
| 1722 | } |
| 1723 | } |
| 1724 | } |
| 1725 | // never reached |
| 1726 | return -127; |
| 1727 | } |
| 1728 | |
| 1729 | /** |
| 1730 | Configure read buffer chunk size. |
| 1731 | Default is 4096. |
| 1732 | |
| 1733 | Automatically reallocates the buffer. |
| 1734 | |
| 1735 | \param ftdi pointer to ftdi_context |
| 1736 | \param chunksize Chunk size |
| 1737 | |
| 1738 | \retval 0: all fine |
| 1739 | \retval -1: ftdi context invalid |
| 1740 | */ |
| 1741 | int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) |
| 1742 | { |
| 1743 | unsigned char *new_buf; |
| 1744 | |
| 1745 | if (ftdi == NULL) |
| 1746 | ftdi_error_return(-1, "ftdi context invalid"); |
| 1747 | |
| 1748 | // Invalidate all remaining data |
| 1749 | ftdi->readbuffer_offset = 0; |
| 1750 | ftdi->readbuffer_remaining = 0; |
| 1751 | #ifdef __linux__ |
| 1752 | /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH, |
| 1753 | which is defined in libusb-1.0. Otherwise, each USB read request will |
| 1754 | be divided into multiple URBs. This will cause issues on Linux kernel |
| 1755 | older than 2.6.32. */ |
| 1756 | if (chunksize > 16384) |
| 1757 | chunksize = 16384; |
| 1758 | #endif |
| 1759 | |
| 1760 | if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) |
| 1761 | ftdi_error_return(-1, "out of memory for readbuffer"); |
| 1762 | |
| 1763 | ftdi->readbuffer = new_buf; |
| 1764 | ftdi->readbuffer_chunksize = chunksize; |
| 1765 | |
| 1766 | return 0; |
| 1767 | } |
| 1768 | |
| 1769 | /** |
| 1770 | Get read buffer chunk size. |
| 1771 | |
| 1772 | \param ftdi pointer to ftdi_context |
| 1773 | \param chunksize Pointer to store chunk size in |
| 1774 | |
| 1775 | \retval 0: all fine |
| 1776 | \retval -1: FTDI context invalid |
| 1777 | */ |
| 1778 | int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) |
| 1779 | { |
| 1780 | if (ftdi == NULL) |
| 1781 | ftdi_error_return(-1, "FTDI context invalid"); |
| 1782 | |
| 1783 | *chunksize = ftdi->readbuffer_chunksize; |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |
| 1787 | |
| 1788 | /** |
| 1789 | Enable bitbang mode. |
| 1790 | |
| 1791 | \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead |
| 1792 | |
| 1793 | \param ftdi pointer to ftdi_context |
| 1794 | \param bitmask Bitmask to configure lines. |
| 1795 | HIGH/ON value configures a line as output. |
| 1796 | |
| 1797 | \retval 0: all fine |
| 1798 | \retval -1: can't enable bitbang mode |
| 1799 | \retval -2: USB device unavailable |
| 1800 | */ |
| 1801 | int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) |
| 1802 | { |
| 1803 | unsigned short usb_val; |
| 1804 | |
| 1805 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1806 | ftdi_error_return(-2, "USB device unavailable"); |
| 1807 | |
| 1808 | usb_val = bitmask; // low byte: bitmask |
| 1809 | /* FT2232C: Set bitbang_mode to 2 to enable SPI */ |
| 1810 | usb_val |= (ftdi->bitbang_mode << 8); |
| 1811 | |
| 1812 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 1813 | SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, |
| 1814 | NULL, 0, ftdi->usb_write_timeout) < 0) |
| 1815 | ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?"); |
| 1816 | |
| 1817 | ftdi->bitbang_enabled = 1; |
| 1818 | return 0; |
| 1819 | } |
| 1820 | |
| 1821 | /** |
| 1822 | Disable bitbang mode. |
| 1823 | |
| 1824 | \param ftdi pointer to ftdi_context |
| 1825 | |
| 1826 | \retval 0: all fine |
| 1827 | \retval -1: can't disable bitbang mode |
| 1828 | \retval -2: USB device unavailable |
| 1829 | */ |
| 1830 | int ftdi_disable_bitbang(struct ftdi_context *ftdi) |
| 1831 | { |
| 1832 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1833 | ftdi_error_return(-2, "USB device unavailable"); |
| 1834 | |
| 1835 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 1836 | ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?"); |
| 1837 | |
| 1838 | ftdi->bitbang_enabled = 0; |
| 1839 | return 0; |
| 1840 | } |
| 1841 | |
| 1842 | /** |
| 1843 | Enable/disable bitbang modes. |
| 1844 | |
| 1845 | \param ftdi pointer to ftdi_context |
| 1846 | \param bitmask Bitmask to configure lines. |
| 1847 | HIGH/ON value configures a line as output. |
| 1848 | \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode |
| 1849 | |
| 1850 | \retval 0: all fine |
| 1851 | \retval -1: can't enable bitbang mode |
| 1852 | \retval -2: USB device unavailable |
| 1853 | */ |
| 1854 | int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode) |
| 1855 | { |
| 1856 | unsigned short usb_val; |
| 1857 | |
| 1858 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1859 | ftdi_error_return(-2, "USB device unavailable"); |
| 1860 | |
| 1861 | usb_val = bitmask; // low byte: bitmask |
| 1862 | usb_val |= (mode << 8); |
| 1863 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 1864 | ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?"); |
| 1865 | |
| 1866 | ftdi->bitbang_mode = mode; |
| 1867 | ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1; |
| 1868 | return 0; |
| 1869 | } |
| 1870 | |
| 1871 | /** |
| 1872 | Directly read pin state, circumventing the read buffer. Useful for bitbang mode. |
| 1873 | |
| 1874 | \param ftdi pointer to ftdi_context |
| 1875 | \param pins Pointer to store pins into |
| 1876 | |
| 1877 | \retval 0: all fine |
| 1878 | \retval -1: read pins failed |
| 1879 | \retval -2: USB device unavailable |
| 1880 | */ |
| 1881 | int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) |
| 1882 | { |
| 1883 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1884 | ftdi_error_return(-2, "USB device unavailable"); |
| 1885 | |
| 1886 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_PINS_REQUEST, 0, ftdi->index, (unsigned char *)pins, 1, ftdi->usb_read_timeout) != 1) |
| 1887 | ftdi_error_return(-1, "read pins failed"); |
| 1888 | |
| 1889 | return 0; |
| 1890 | } |
| 1891 | |
| 1892 | /** |
| 1893 | Set latency timer |
| 1894 | |
| 1895 | The FTDI chip keeps data in the internal buffer for a specific |
| 1896 | amount of time if the buffer is not full yet to decrease |
| 1897 | load on the usb bus. |
| 1898 | |
| 1899 | \param ftdi pointer to ftdi_context |
| 1900 | \param latency Value between 1 and 255 |
| 1901 | |
| 1902 | \retval 0: all fine |
| 1903 | \retval -1: latency out of range |
| 1904 | \retval -2: unable to set latency timer |
| 1905 | \retval -3: USB device unavailable |
| 1906 | */ |
| 1907 | int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) |
| 1908 | { |
| 1909 | unsigned short usb_val; |
| 1910 | |
| 1911 | if (latency < 1) |
| 1912 | ftdi_error_return(-1, "latency out of range. Only valid for 1-255"); |
| 1913 | |
| 1914 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1915 | ftdi_error_return(-3, "USB device unavailable"); |
| 1916 | |
| 1917 | usb_val = latency; |
| 1918 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_LATENCY_TIMER_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 1919 | ftdi_error_return(-2, "unable to set latency timer"); |
| 1920 | |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
| 1924 | /** |
| 1925 | Get latency timer |
| 1926 | |
| 1927 | \param ftdi pointer to ftdi_context |
| 1928 | \param latency Pointer to store latency value in |
| 1929 | |
| 1930 | \retval 0: all fine |
| 1931 | \retval -1: unable to get latency timer |
| 1932 | \retval -2: USB device unavailable |
| 1933 | */ |
| 1934 | int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) |
| 1935 | { |
| 1936 | unsigned short usb_val; |
| 1937 | |
| 1938 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1939 | ftdi_error_return(-2, "USB device unavailable"); |
| 1940 | |
| 1941 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_GET_LATENCY_TIMER_REQUEST, 0, ftdi->index, (unsigned char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) |
| 1942 | ftdi_error_return(-1, "reading latency timer failed"); |
| 1943 | |
| 1944 | *latency = (unsigned char)usb_val; |
| 1945 | return 0; |
| 1946 | } |
| 1947 | |
| 1948 | /** |
| 1949 | Poll modem status information |
| 1950 | |
| 1951 | This function allows the retrieve the two status bytes of the device. |
| 1952 | The device sends these bytes also as a header for each read access |
| 1953 | where they are discarded by ftdi_read_data(). The chip generates |
| 1954 | the two stripped status bytes in the absence of data every 40 ms. |
| 1955 | |
| 1956 | Layout of the first byte: |
| 1957 | - B0..B3 - must be 0 |
| 1958 | - B4 Clear to send (CTS) |
| 1959 | 0 = inactive |
| 1960 | 1 = active |
| 1961 | - B5 Data set ready (DTS) |
| 1962 | 0 = inactive |
| 1963 | 1 = active |
| 1964 | - B6 Ring indicator (RI) |
| 1965 | 0 = inactive |
| 1966 | 1 = active |
| 1967 | - B7 Receive line signal detect (RLSD) |
| 1968 | 0 = inactive |
| 1969 | 1 = active |
| 1970 | |
| 1971 | Layout of the second byte: |
| 1972 | - B0 Data ready (DR) |
| 1973 | - B1 Overrun error (OE) |
| 1974 | - B2 Parity error (PE) |
| 1975 | - B3 Framing error (FE) |
| 1976 | - B4 Break interrupt (BI) |
| 1977 | - B5 Transmitter holding register (THRE) |
| 1978 | - B6 Transmitter empty (TEMT) |
| 1979 | - B7 Error in RCVR FIFO |
| 1980 | |
| 1981 | \param ftdi pointer to ftdi_context |
| 1982 | \param status Pointer to store status information in. Must be two bytes. |
| 1983 | |
| 1984 | \retval 0: all fine |
| 1985 | \retval -1: unable to retrieve status information |
| 1986 | \retval -2: USB device unavailable |
| 1987 | */ |
| 1988 | int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status) |
| 1989 | { |
| 1990 | char usb_val[2]; |
| 1991 | |
| 1992 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 1993 | ftdi_error_return(-2, "USB device unavailable"); |
| 1994 | |
| 1995 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, (unsigned char *)usb_val, 2, ftdi->usb_read_timeout) != 2) |
| 1996 | ftdi_error_return(-1, "getting modem status failed"); |
| 1997 | |
| 1998 | *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF); |
| 1999 | |
| 2000 | return 0; |
| 2001 | } |
| 2002 | |
| 2003 | /** |
| 2004 | Set flowcontrol for ftdi chip |
| 2005 | |
| 2006 | \param ftdi pointer to ftdi_context |
| 2007 | \param flowctrl flow control to use. should be |
| 2008 | SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS |
| 2009 | |
| 2010 | \retval 0: all fine |
| 2011 | \retval -1: set flow control failed |
| 2012 | \retval -2: USB device unavailable |
| 2013 | */ |
| 2014 | int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl) |
| 2015 | { |
| 2016 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 2017 | ftdi_error_return(-2, "USB device unavailable"); |
| 2018 | |
| 2019 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 2020 | SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index), |
| 2021 | NULL, 0, ftdi->usb_write_timeout) < 0) |
| 2022 | ftdi_error_return(-1, "set flow control failed"); |
| 2023 | |
| 2024 | return 0; |
| 2025 | } |
| 2026 | |
| 2027 | /** |
| 2028 | Set dtr line |
| 2029 | |
| 2030 | \param ftdi pointer to ftdi_context |
| 2031 | \param state state to set line to (1 or 0) |
| 2032 | |
| 2033 | \retval 0: all fine |
| 2034 | \retval -1: set dtr failed |
| 2035 | \retval -2: USB device unavailable |
| 2036 | */ |
| 2037 | int ftdi_setdtr(struct ftdi_context *ftdi, int state) |
| 2038 | { |
| 2039 | unsigned short usb_val; |
| 2040 | |
| 2041 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 2042 | ftdi_error_return(-2, "USB device unavailable"); |
| 2043 | |
| 2044 | if (state) |
| 2045 | usb_val = SIO_SET_DTR_HIGH; |
| 2046 | else |
| 2047 | usb_val = SIO_SET_DTR_LOW; |
| 2048 | |
| 2049 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 2050 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index, |
| 2051 | NULL, 0, ftdi->usb_write_timeout) < 0) |
| 2052 | ftdi_error_return(-1, "set dtr failed"); |
| 2053 | |
| 2054 | return 0; |
| 2055 | } |
| 2056 | |
| 2057 | /** |
| 2058 | Set rts line |
| 2059 | |
| 2060 | \param ftdi pointer to ftdi_context |
| 2061 | \param state state to set line to (1 or 0) |
| 2062 | |
| 2063 | \retval 0: all fine |
| 2064 | \retval -1: set rts failed |
| 2065 | \retval -2: USB device unavailable |
| 2066 | */ |
| 2067 | int ftdi_setrts(struct ftdi_context *ftdi, int state) |
| 2068 | { |
| 2069 | unsigned short usb_val; |
| 2070 | |
| 2071 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 2072 | ftdi_error_return(-2, "USB device unavailable"); |
| 2073 | |
| 2074 | if (state) |
| 2075 | usb_val = SIO_SET_RTS_HIGH; |
| 2076 | else |
| 2077 | usb_val = SIO_SET_RTS_LOW; |
| 2078 | |
| 2079 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 2080 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index, |
| 2081 | NULL, 0, ftdi->usb_write_timeout) < 0) |
| 2082 | ftdi_error_return(-1, "set of rts failed"); |
| 2083 | |
| 2084 | return 0; |
| 2085 | } |
| 2086 | |
| 2087 | /** |
| 2088 | Set dtr and rts line in one pass |
| 2089 | |
| 2090 | \param ftdi pointer to ftdi_context |
| 2091 | \param dtr DTR state to set line to (1 or 0) |
| 2092 | \param rts RTS state to set line to (1 or 0) |
| 2093 | |
| 2094 | \retval 0: all fine |
| 2095 | \retval -1: set dtr/rts failed |
| 2096 | \retval -2: USB device unavailable |
| 2097 | */ |
| 2098 | int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts) |
| 2099 | { |
| 2100 | unsigned short usb_val; |
| 2101 | |
| 2102 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 2103 | ftdi_error_return(-2, "USB device unavailable"); |
| 2104 | |
| 2105 | if (dtr) |
| 2106 | usb_val = SIO_SET_DTR_HIGH; |
| 2107 | else |
| 2108 | usb_val = SIO_SET_DTR_LOW; |
| 2109 | |
| 2110 | if (rts) |
| 2111 | usb_val |= SIO_SET_RTS_HIGH; |
| 2112 | else |
| 2113 | usb_val |= SIO_SET_RTS_LOW; |
| 2114 | |
| 2115 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 2116 | SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index, |
| 2117 | NULL, 0, ftdi->usb_write_timeout) < 0) |
| 2118 | ftdi_error_return(-1, "set of rts/dtr failed"); |
| 2119 | |
| 2120 | return 0; |
| 2121 | } |
| 2122 | |
| 2123 | /** |
| 2124 | Set the special event character |
| 2125 | |
| 2126 | \param ftdi pointer to ftdi_context |
| 2127 | \param eventch Event character |
| 2128 | \param enable 0 to disable the event character, non-zero otherwise |
| 2129 | |
| 2130 | \retval 0: all fine |
| 2131 | \retval -1: unable to set event character |
| 2132 | \retval -2: USB device unavailable |
| 2133 | */ |
| 2134 | int ftdi_set_event_char(struct ftdi_context *ftdi, |
| 2135 | unsigned char eventch, unsigned char enable) |
| 2136 | { |
| 2137 | unsigned short usb_val; |
| 2138 | |
| 2139 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 2140 | ftdi_error_return(-2, "USB device unavailable"); |
| 2141 | |
| 2142 | usb_val = eventch; |
| 2143 | if (enable) |
| 2144 | usb_val |= 1 << 8; |
| 2145 | |
| 2146 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_EVENT_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 2147 | ftdi_error_return(-1, "setting event character failed"); |
| 2148 | |
| 2149 | return 0; |
| 2150 | } |
| 2151 | |
| 2152 | /** |
| 2153 | Set error character |
| 2154 | |
| 2155 | \param ftdi pointer to ftdi_context |
| 2156 | \param errorch Error character |
| 2157 | \param enable 0 to disable the error character, non-zero otherwise |
| 2158 | |
| 2159 | \retval 0: all fine |
| 2160 | \retval -1: unable to set error character |
| 2161 | \retval -2: USB device unavailable |
| 2162 | */ |
| 2163 | int ftdi_set_error_char(struct ftdi_context *ftdi, |
| 2164 | unsigned char errorch, unsigned char enable) |
| 2165 | { |
| 2166 | unsigned short usb_val; |
| 2167 | |
| 2168 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 2169 | ftdi_error_return(-2, "USB device unavailable"); |
| 2170 | |
| 2171 | usb_val = errorch; |
| 2172 | if (enable) |
| 2173 | usb_val |= 1 << 8; |
| 2174 | |
| 2175 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_ERROR_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 2176 | ftdi_error_return(-1, "setting error character failed"); |
| 2177 | |
| 2178 | return 0; |
| 2179 | } |
| 2180 | |
| 2181 | /** |
| 2182 | Init eeprom with default values for the connected device |
| 2183 | \param ftdi pointer to ftdi_context |
| 2184 | \param manufacturer String to use as Manufacturer |
| 2185 | \param product String to use as Product description |
| 2186 | \param serial String to use as Serial number description |
| 2187 | |
| 2188 | \retval 0: all fine |
| 2189 | \retval -1: No struct ftdi_context |
| 2190 | \retval -2: No struct ftdi_eeprom |
| 2191 | \retval -3: No connected device or device not yet opened |
| 2192 | */ |
| 2193 | int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer, |
| 2194 | char * product, char * serial) |
| 2195 | { |
| 2196 | struct ftdi_eeprom *eeprom; |
| 2197 | |
| 2198 | if (ftdi == NULL) |
| 2199 | ftdi_error_return(-1, "No struct ftdi_context"); |
| 2200 | |
| 2201 | if (ftdi->eeprom == NULL) |
| 2202 | ftdi_error_return(-2,"No struct ftdi_eeprom"); |
| 2203 | |
| 2204 | eeprom = ftdi->eeprom; |
| 2205 | memset(eeprom, 0, sizeof(struct ftdi_eeprom)); |
| 2206 | |
| 2207 | if (ftdi->usb_dev == NULL) |
| 2208 | ftdi_error_return(-3, "No connected device or device not yet opened"); |
| 2209 | |
| 2210 | eeprom->vendor_id = 0x0403; |
| 2211 | eeprom->use_serial = USE_SERIAL_NUM; |
| 2212 | if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) || |
| 2213 | (ftdi->type == TYPE_R)) |
| 2214 | eeprom->product_id = 0x6001; |
| 2215 | else if (ftdi->type == TYPE_4232H) |
| 2216 | eeprom->product_id = 0x6011; |
| 2217 | else if (ftdi->type == TYPE_232H) |
| 2218 | eeprom->product_id = 0x6014; |
| 2219 | else |
| 2220 | eeprom->product_id = 0x6010; |
| 2221 | if (ftdi->type == TYPE_AM) |
| 2222 | eeprom->usb_version = 0x0101; |
| 2223 | else |
| 2224 | eeprom->usb_version = 0x0200; |
| 2225 | eeprom->max_power = 100; |
| 2226 | |
| 2227 | if (eeprom->manufacturer) |
| 2228 | free (eeprom->manufacturer); |
| 2229 | eeprom->manufacturer = NULL; |
| 2230 | if (manufacturer) |
| 2231 | { |
| 2232 | eeprom->manufacturer = malloc(strlen(manufacturer)+1); |
| 2233 | if (eeprom->manufacturer) |
| 2234 | strcpy(eeprom->manufacturer, manufacturer); |
| 2235 | } |
| 2236 | |
| 2237 | if (eeprom->product) |
| 2238 | free (eeprom->product); |
| 2239 | eeprom->product = NULL; |
| 2240 | if(product) |
| 2241 | { |
| 2242 | eeprom->product = malloc(strlen(product)+1); |
| 2243 | if (eeprom->product) |
| 2244 | strcpy(eeprom->product, product); |
| 2245 | } |
| 2246 | |
| 2247 | if (eeprom->serial) |
| 2248 | free (eeprom->serial); |
| 2249 | eeprom->serial = NULL; |
| 2250 | if (serial) |
| 2251 | { |
| 2252 | eeprom->serial = malloc(strlen(serial)+1); |
| 2253 | if (eeprom->serial) |
| 2254 | strcpy(eeprom->serial, serial); |
| 2255 | } |
| 2256 | |
| 2257 | |
| 2258 | if (ftdi->type == TYPE_R) |
| 2259 | { |
| 2260 | eeprom->max_power = 90; |
| 2261 | eeprom->size = 0x80; |
| 2262 | eeprom->cbus_function[0] = CBUS_TXLED; |
| 2263 | eeprom->cbus_function[1] = CBUS_RXLED; |
| 2264 | eeprom->cbus_function[2] = CBUS_TXDEN; |
| 2265 | eeprom->cbus_function[3] = CBUS_PWREN; |
| 2266 | eeprom->cbus_function[4] = CBUS_SLEEP; |
| 2267 | } |
| 2268 | else |
| 2269 | { |
| 2270 | if(ftdi->type == TYPE_232H) |
| 2271 | { |
| 2272 | int i; |
| 2273 | for (i=0; i<10; i++) |
| 2274 | eeprom->cbus_function[i] = CBUSH_TRISTATE; |
| 2275 | } |
| 2276 | eeprom->size = -1; |
| 2277 | } |
| 2278 | eeprom->initialized_for_connected_device = 1; |
| 2279 | return 0; |
| 2280 | } |
| 2281 | /*FTD2XX doesn't check for values not fitting in the ACBUS Signal oprtions*/ |
| 2282 | void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output) |
| 2283 | { |
| 2284 | int i; |
| 2285 | for(i=0; i<5;i++) |
| 2286 | { |
| 2287 | int mode_low, mode_high; |
| 2288 | if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5) |
| 2289 | mode_low = CBUSH_TRISTATE; |
| 2290 | else |
| 2291 | mode_low = eeprom->cbus_function[2*i]; |
| 2292 | if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5) |
| 2293 | mode_high = CBUSH_TRISTATE; |
| 2294 | else |
| 2295 | mode_high = eeprom->cbus_function[2*i]; |
| 2296 | |
| 2297 | output[0x18+i] = mode_high <<4 | mode_low; |
| 2298 | } |
| 2299 | } |
| 2300 | /* Return the bits for the encoded EEPROM Structure of a requested Mode |
| 2301 | * |
| 2302 | */ |
| 2303 | static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip) |
| 2304 | { |
| 2305 | switch (chip) |
| 2306 | { |
| 2307 | case TYPE_2232H: |
| 2308 | case TYPE_2232C: |
| 2309 | { |
| 2310 | switch (type) |
| 2311 | { |
| 2312 | case CHANNEL_IS_UART: return 0; |
| 2313 | case CHANNEL_IS_FIFO: return 0x01; |
| 2314 | case CHANNEL_IS_OPTO: return 0x02; |
| 2315 | case CHANNEL_IS_CPU : return 0x04; |
| 2316 | default: return 0; |
| 2317 | } |
| 2318 | } |
| 2319 | case TYPE_232H: |
| 2320 | { |
| 2321 | switch (type) |
| 2322 | { |
| 2323 | case CHANNEL_IS_UART : return 0; |
| 2324 | case CHANNEL_IS_FIFO : return 0x01; |
| 2325 | case CHANNEL_IS_OPTO : return 0x02; |
| 2326 | case CHANNEL_IS_CPU : return 0x04; |
| 2327 | case CHANNEL_IS_FT1284 : return 0x08; |
| 2328 | default: return 0; |
| 2329 | } |
| 2330 | } |
| 2331 | default: return 0; |
| 2332 | } |
| 2333 | return 0; |
| 2334 | } |
| 2335 | |
| 2336 | /** |
| 2337 | Build binary buffer from ftdi_eeprom structure. |
| 2338 | Output is suitable for ftdi_write_eeprom(). |
| 2339 | |
| 2340 | \param ftdi pointer to ftdi_context |
| 2341 | |
| 2342 | \retval >=0: size of eeprom user area in bytes |
| 2343 | \retval -1: eeprom size (128 bytes) exceeded by custom strings |
| 2344 | \retval -2: Invalid eeprom or ftdi pointer |
| 2345 | \retval -3: Invalid cbus function setting (FIXME: Not in the code?) |
| 2346 | \retval -4: Chip doesn't support invert (FIXME: Not in the code?) |
| 2347 | \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?) |
| 2348 | \retval -6: No connected EEPROM or EEPROM Type unknown |
| 2349 | */ |
| 2350 | int ftdi_eeprom_build(struct ftdi_context *ftdi) |
| 2351 | { |
| 2352 | unsigned char i, j, eeprom_size_mask; |
| 2353 | unsigned short checksum, value; |
| 2354 | unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0; |
| 2355 | int user_area_size; |
| 2356 | struct ftdi_eeprom *eeprom; |
| 2357 | unsigned char * output; |
| 2358 | |
| 2359 | if (ftdi == NULL) |
| 2360 | ftdi_error_return(-2,"No context"); |
| 2361 | if (ftdi->eeprom == NULL) |
| 2362 | ftdi_error_return(-2,"No eeprom structure"); |
| 2363 | |
| 2364 | eeprom= ftdi->eeprom; |
| 2365 | output = eeprom->buf; |
| 2366 | |
| 2367 | if (eeprom->chip == -1) |
| 2368 | ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown"); |
| 2369 | |
| 2370 | if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66)) |
| 2371 | eeprom->size = 0x100; |
| 2372 | else |
| 2373 | eeprom->size = 0x80; |
| 2374 | |
| 2375 | if (eeprom->manufacturer != NULL) |
| 2376 | manufacturer_size = strlen(eeprom->manufacturer); |
| 2377 | if (eeprom->product != NULL) |
| 2378 | product_size = strlen(eeprom->product); |
| 2379 | if (eeprom->serial != NULL) |
| 2380 | serial_size = strlen(eeprom->serial); |
| 2381 | |
| 2382 | // eeprom size check |
| 2383 | switch (ftdi->type) |
| 2384 | { |
| 2385 | case TYPE_AM: |
| 2386 | case TYPE_BM: |
| 2387 | user_area_size = 96; // base size for strings (total of 48 characters) |
| 2388 | break; |
| 2389 | case TYPE_2232C: |
| 2390 | user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff |
| 2391 | break; |
| 2392 | case TYPE_R: |
| 2393 | user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff |
| 2394 | break; |
| 2395 | case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff |
| 2396 | case TYPE_4232H: |
| 2397 | user_area_size = 86; |
| 2398 | break; |
| 2399 | case TYPE_232H: |
| 2400 | user_area_size = 80; |
| 2401 | break; |
| 2402 | default: |
| 2403 | user_area_size = 0; |
| 2404 | break; |
| 2405 | } |
| 2406 | user_area_size -= (manufacturer_size + product_size + serial_size) * 2; |
| 2407 | |
| 2408 | if (user_area_size < 0) |
| 2409 | ftdi_error_return(-1,"eeprom size exceeded"); |
| 2410 | |
| 2411 | // empty eeprom |
| 2412 | memset (ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE); |
| 2413 | |
| 2414 | // Bytes and Bits set for all Types |
| 2415 | |
| 2416 | // Addr 02: Vendor ID |
| 2417 | output[0x02] = eeprom->vendor_id; |
| 2418 | output[0x03] = eeprom->vendor_id >> 8; |
| 2419 | |
| 2420 | // Addr 04: Product ID |
| 2421 | output[0x04] = eeprom->product_id; |
| 2422 | output[0x05] = eeprom->product_id >> 8; |
| 2423 | |
| 2424 | // Addr 06: Device release number (0400h for BM features) |
| 2425 | output[0x06] = 0x00; |
| 2426 | switch (ftdi->type) |
| 2427 | { |
| 2428 | case TYPE_AM: |
| 2429 | output[0x07] = 0x02; |
| 2430 | break; |
| 2431 | case TYPE_BM: |
| 2432 | output[0x07] = 0x04; |
| 2433 | break; |
| 2434 | case TYPE_2232C: |
| 2435 | output[0x07] = 0x05; |
| 2436 | break; |
| 2437 | case TYPE_R: |
| 2438 | output[0x07] = 0x06; |
| 2439 | break; |
| 2440 | case TYPE_2232H: |
| 2441 | output[0x07] = 0x07; |
| 2442 | break; |
| 2443 | case TYPE_4232H: |
| 2444 | output[0x07] = 0x08; |
| 2445 | break; |
| 2446 | case TYPE_232H: |
| 2447 | output[0x07] = 0x09; |
| 2448 | break; |
| 2449 | default: |
| 2450 | output[0x07] = 0x00; |
| 2451 | } |
| 2452 | |
| 2453 | // Addr 08: Config descriptor |
| 2454 | // Bit 7: always 1 |
| 2455 | // Bit 6: 1 if this device is self powered, 0 if bus powered |
| 2456 | // Bit 5: 1 if this device uses remote wakeup |
| 2457 | // Bit 4-0: reserved - 0 |
| 2458 | j = 0x80; |
| 2459 | if (eeprom->self_powered == 1) |
| 2460 | j |= 0x40; |
| 2461 | if (eeprom->remote_wakeup == 1) |
| 2462 | j |= 0x20; |
| 2463 | output[0x08] = j; |
| 2464 | |
| 2465 | // Addr 09: Max power consumption: max power = value * 2 mA |
| 2466 | output[0x09] = eeprom->max_power>>1; |
| 2467 | |
| 2468 | if (ftdi->type != TYPE_AM) |
| 2469 | { |
| 2470 | // Addr 0A: Chip configuration |
| 2471 | // Bit 7: 0 - reserved |
| 2472 | // Bit 6: 0 - reserved |
| 2473 | // Bit 5: 0 - reserved |
| 2474 | // Bit 4: 1 - Change USB version |
| 2475 | // Bit 3: 1 - Use the serial number string |
| 2476 | // Bit 2: 1 - Enable suspend pull downs for lower power |
| 2477 | // Bit 1: 1 - Out EndPoint is Isochronous |
| 2478 | // Bit 0: 1 - In EndPoint is Isochronous |
| 2479 | // |
| 2480 | j = 0; |
| 2481 | if (eeprom->in_is_isochronous == 1) |
| 2482 | j = j | 1; |
| 2483 | if (eeprom->out_is_isochronous == 1) |
| 2484 | j = j | 2; |
| 2485 | output[0x0A] = j; |
| 2486 | } |
| 2487 | |
| 2488 | // Dynamic content |
| 2489 | // Strings start at 0x94 (TYPE_AM, TYPE_BM) |
| 2490 | // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H) |
| 2491 | // 0xa0 (TYPE_232H) |
| 2492 | i = 0; |
| 2493 | switch (ftdi->type) |
| 2494 | { |
| 2495 | case TYPE_232H: |
| 2496 | i += 2; |
| 2497 | case TYPE_2232H: |
| 2498 | case TYPE_4232H: |
| 2499 | i += 2; |
| 2500 | case TYPE_R: |
| 2501 | i += 2; |
| 2502 | case TYPE_2232C: |
| 2503 | i += 2; |
| 2504 | case TYPE_AM: |
| 2505 | case TYPE_BM: |
| 2506 | i += 0x94; |
| 2507 | } |
| 2508 | /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */ |
| 2509 | eeprom_size_mask = eeprom->size -1; |
| 2510 | |
| 2511 | // Addr 0E: Offset of the manufacturer string + 0x80, calculated later |
| 2512 | // Addr 0F: Length of manufacturer string |
| 2513 | // Output manufacturer |
| 2514 | output[0x0E] = i; // calculate offset |
| 2515 | output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++; |
| 2516 | output[i & eeprom_size_mask] = 0x03, i++; // type: string |
| 2517 | for (j = 0; j < manufacturer_size; j++) |
| 2518 | { |
| 2519 | output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++; |
| 2520 | output[i & eeprom_size_mask] = 0x00, i++; |
| 2521 | } |
| 2522 | output[0x0F] = manufacturer_size*2 + 2; |
| 2523 | |
| 2524 | // Addr 10: Offset of the product string + 0x80, calculated later |
| 2525 | // Addr 11: Length of product string |
| 2526 | output[0x10] = i | 0x80; // calculate offset |
| 2527 | output[i & eeprom_size_mask] = product_size*2 + 2, i++; |
| 2528 | output[i & eeprom_size_mask] = 0x03, i++; |
| 2529 | for (j = 0; j < product_size; j++) |
| 2530 | { |
| 2531 | output[i & eeprom_size_mask] = eeprom->product[j], i++; |
| 2532 | output[i & eeprom_size_mask] = 0x00, i++; |
| 2533 | } |
| 2534 | output[0x11] = product_size*2 + 2; |
| 2535 | |
| 2536 | // Addr 12: Offset of the serial string + 0x80, calculated later |
| 2537 | // Addr 13: Length of serial string |
| 2538 | output[0x12] = i | 0x80; // calculate offset |
| 2539 | output[i & eeprom_size_mask] = serial_size*2 + 2, i++; |
| 2540 | output[i & eeprom_size_mask] = 0x03, i++; |
| 2541 | for (j = 0; j < serial_size; j++) |
| 2542 | { |
| 2543 | output[i & eeprom_size_mask] = eeprom->serial[j], i++; |
| 2544 | output[i & eeprom_size_mask] = 0x00, i++; |
| 2545 | } |
| 2546 | |
| 2547 | // Legacy port name and PnP fields for FT2232 and newer chips |
| 2548 | if (ftdi->type > TYPE_BM) |
| 2549 | { |
| 2550 | output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */ |
| 2551 | i++; |
| 2552 | output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */ |
| 2553 | i++; |
| 2554 | output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */ |
| 2555 | i++; |
| 2556 | } |
| 2557 | |
| 2558 | output[0x13] = serial_size*2 + 2; |
| 2559 | |
| 2560 | if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */ |
| 2561 | { |
| 2562 | if (eeprom->use_serial == USE_SERIAL_NUM ) |
| 2563 | output[0x0A] |= USE_SERIAL_NUM; |
| 2564 | else |
| 2565 | output[0x0A] &= ~USE_SERIAL_NUM; |
| 2566 | } |
| 2567 | |
| 2568 | /* Bytes and Bits specific to (some) types |
| 2569 | Write linear, as this allows easier fixing*/ |
| 2570 | switch (ftdi->type) |
| 2571 | { |
| 2572 | case TYPE_AM: |
| 2573 | break; |
| 2574 | case TYPE_BM: |
| 2575 | output[0x0C] = eeprom->usb_version & 0xff; |
| 2576 | output[0x0D] = (eeprom->usb_version>>8) & 0xff; |
| 2577 | if (eeprom->use_usb_version == USE_USB_VERSION_BIT) |
| 2578 | output[0x0A] |= USE_USB_VERSION_BIT; |
| 2579 | else |
| 2580 | output[0x0A] &= ~USE_USB_VERSION_BIT; |
| 2581 | |
| 2582 | break; |
| 2583 | case TYPE_2232C: |
| 2584 | |
| 2585 | output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C); |
| 2586 | if ( eeprom->channel_a_driver == DRIVER_VCP) |
| 2587 | output[0x00] |= DRIVER_VCP; |
| 2588 | else |
| 2589 | output[0x00] &= ~DRIVER_VCP; |
| 2590 | |
| 2591 | if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE) |
| 2592 | output[0x00] |= HIGH_CURRENT_DRIVE; |
| 2593 | else |
| 2594 | output[0x00] &= ~HIGH_CURRENT_DRIVE; |
| 2595 | |
| 2596 | output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C); |
| 2597 | if ( eeprom->channel_b_driver == DRIVER_VCP) |
| 2598 | output[0x01] |= DRIVER_VCP; |
| 2599 | else |
| 2600 | output[0x01] &= ~DRIVER_VCP; |
| 2601 | |
| 2602 | if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE) |
| 2603 | output[0x01] |= HIGH_CURRENT_DRIVE; |
| 2604 | else |
| 2605 | output[0x01] &= ~HIGH_CURRENT_DRIVE; |
| 2606 | |
| 2607 | if (eeprom->in_is_isochronous == 1) |
| 2608 | output[0x0A] |= 0x1; |
| 2609 | else |
| 2610 | output[0x0A] &= ~0x1; |
| 2611 | if (eeprom->out_is_isochronous == 1) |
| 2612 | output[0x0A] |= 0x2; |
| 2613 | else |
| 2614 | output[0x0A] &= ~0x2; |
| 2615 | if (eeprom->suspend_pull_downs == 1) |
| 2616 | output[0x0A] |= 0x4; |
| 2617 | else |
| 2618 | output[0x0A] &= ~0x4; |
| 2619 | if (eeprom->use_usb_version == USE_USB_VERSION_BIT) |
| 2620 | output[0x0A] |= USE_USB_VERSION_BIT; |
| 2621 | else |
| 2622 | output[0x0A] &= ~USE_USB_VERSION_BIT; |
| 2623 | |
| 2624 | output[0x0C] = eeprom->usb_version & 0xff; |
| 2625 | output[0x0D] = (eeprom->usb_version>>8) & 0xff; |
| 2626 | output[0x14] = eeprom->chip; |
| 2627 | break; |
| 2628 | case TYPE_R: |
| 2629 | if (eeprom->high_current == HIGH_CURRENT_DRIVE_R) |
| 2630 | output[0x00] |= HIGH_CURRENT_DRIVE_R; |
| 2631 | output[0x01] = 0x40; /* Hard coded Endpoint Size*/ |
| 2632 | |
| 2633 | if (eeprom->suspend_pull_downs == 1) |
| 2634 | output[0x0A] |= 0x4; |
| 2635 | else |
| 2636 | output[0x0A] &= ~0x4; |
| 2637 | output[0x0B] = eeprom->invert; |
| 2638 | output[0x0C] = eeprom->usb_version & 0xff; |
| 2639 | output[0x0D] = (eeprom->usb_version>>8) & 0xff; |
| 2640 | |
| 2641 | if (eeprom->cbus_function[0] > CBUS_BB) |
| 2642 | output[0x14] = CBUS_TXLED; |
| 2643 | else |
| 2644 | output[0x14] = eeprom->cbus_function[0]; |
| 2645 | |
| 2646 | if (eeprom->cbus_function[1] > CBUS_BB) |
| 2647 | output[0x14] |= CBUS_RXLED<<4; |
| 2648 | else |
| 2649 | output[0x14] |= eeprom->cbus_function[1]<<4; |
| 2650 | |
| 2651 | if (eeprom->cbus_function[2] > CBUS_BB) |
| 2652 | output[0x15] = CBUS_TXDEN; |
| 2653 | else |
| 2654 | output[0x15] = eeprom->cbus_function[2]; |
| 2655 | |
| 2656 | if (eeprom->cbus_function[3] > CBUS_BB) |
| 2657 | output[0x15] |= CBUS_PWREN<<4; |
| 2658 | else |
| 2659 | output[0x15] |= eeprom->cbus_function[3]<<4; |
| 2660 | |
| 2661 | if (eeprom->cbus_function[4] > CBUS_CLK6) |
| 2662 | output[0x16] = CBUS_SLEEP; |
| 2663 | else |
| 2664 | output[0x16] = eeprom->cbus_function[4]; |
| 2665 | break; |
| 2666 | case TYPE_2232H: |
| 2667 | output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H); |
| 2668 | if ( eeprom->channel_a_driver == DRIVER_VCP) |
| 2669 | output[0x00] |= DRIVER_VCP; |
| 2670 | else |
| 2671 | output[0x00] &= ~DRIVER_VCP; |
| 2672 | |
| 2673 | output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H); |
| 2674 | if ( eeprom->channel_b_driver == DRIVER_VCP) |
| 2675 | output[0x01] |= DRIVER_VCP; |
| 2676 | else |
| 2677 | output[0x01] &= ~DRIVER_VCP; |
| 2678 | if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT) |
| 2679 | output[0x01] |= SUSPEND_DBUS7_BIT; |
| 2680 | else |
| 2681 | output[0x01] &= ~SUSPEND_DBUS7_BIT; |
| 2682 | |
| 2683 | if (eeprom->suspend_pull_downs == 1) |
| 2684 | output[0x0A] |= 0x4; |
| 2685 | else |
| 2686 | output[0x0A] &= ~0x4; |
| 2687 | |
| 2688 | if (eeprom->group0_drive > DRIVE_16MA) |
| 2689 | output[0x0c] |= DRIVE_16MA; |
| 2690 | else |
| 2691 | output[0x0c] |= eeprom->group0_drive; |
| 2692 | if (eeprom->group0_schmitt == IS_SCHMITT) |
| 2693 | output[0x0c] |= IS_SCHMITT; |
| 2694 | if (eeprom->group0_slew == SLOW_SLEW) |
| 2695 | output[0x0c] |= SLOW_SLEW; |
| 2696 | |
| 2697 | if (eeprom->group1_drive > DRIVE_16MA) |
| 2698 | output[0x0c] |= DRIVE_16MA<<4; |
| 2699 | else |
| 2700 | output[0x0c] |= eeprom->group1_drive<<4; |
| 2701 | if (eeprom->group1_schmitt == IS_SCHMITT) |
| 2702 | output[0x0c] |= IS_SCHMITT<<4; |
| 2703 | if (eeprom->group1_slew == SLOW_SLEW) |
| 2704 | output[0x0c] |= SLOW_SLEW<<4; |
| 2705 | |
| 2706 | if (eeprom->group2_drive > DRIVE_16MA) |
| 2707 | output[0x0d] |= DRIVE_16MA; |
| 2708 | else |
| 2709 | output[0x0d] |= eeprom->group2_drive; |
| 2710 | if (eeprom->group2_schmitt == IS_SCHMITT) |
| 2711 | output[0x0d] |= IS_SCHMITT; |
| 2712 | if (eeprom->group2_slew == SLOW_SLEW) |
| 2713 | output[0x0d] |= SLOW_SLEW; |
| 2714 | |
| 2715 | if (eeprom->group3_drive > DRIVE_16MA) |
| 2716 | output[0x0d] |= DRIVE_16MA<<4; |
| 2717 | else |
| 2718 | output[0x0d] |= eeprom->group3_drive<<4; |
| 2719 | if (eeprom->group3_schmitt == IS_SCHMITT) |
| 2720 | output[0x0d] |= IS_SCHMITT<<4; |
| 2721 | if (eeprom->group3_slew == SLOW_SLEW) |
| 2722 | output[0x0d] |= SLOW_SLEW<<4; |
| 2723 | |
| 2724 | output[0x18] = eeprom->chip; |
| 2725 | |
| 2726 | break; |
| 2727 | case TYPE_4232H: |
| 2728 | output[0x18] = eeprom->chip; |
| 2729 | fprintf(stderr,"FIXME: Build FT4232H specific EEPROM settings\n"); |
| 2730 | break; |
| 2731 | case TYPE_232H: |
| 2732 | output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H); |
| 2733 | if ( eeprom->channel_a_driver == DRIVER_VCP) |
| 2734 | output[0x00] |= DRIVER_VCPH; |
| 2735 | else |
| 2736 | output[0x00] &= ~DRIVER_VCPH; |
| 2737 | if (eeprom->powersave) |
| 2738 | output[0x01] |= POWER_SAVE_DISABLE_H; |
| 2739 | else |
| 2740 | output[0x01] &= ~POWER_SAVE_DISABLE_H; |
| 2741 | if (eeprom->clock_polarity) |
| 2742 | output[0x01] |= FT1284_CLK_IDLE_STATE; |
| 2743 | else |
| 2744 | output[0x01] &= ~FT1284_CLK_IDLE_STATE; |
| 2745 | if (eeprom->data_order) |
| 2746 | output[0x01] |= FT1284_DATA_LSB; |
| 2747 | else |
| 2748 | output[0x01] &= ~FT1284_DATA_LSB; |
| 2749 | if (eeprom->flow_control) |
| 2750 | output[0x01] |= FT1284_FLOW_CONTROL; |
| 2751 | else |
| 2752 | output[0x01] &= ~FT1284_FLOW_CONTROL; |
| 2753 | if (eeprom->group0_drive > DRIVE_16MA) |
| 2754 | output[0x0c] |= DRIVE_16MA; |
| 2755 | else |
| 2756 | output[0x0c] |= eeprom->group0_drive; |
| 2757 | if (eeprom->group0_schmitt == IS_SCHMITT) |
| 2758 | output[0x0c] |= IS_SCHMITT; |
| 2759 | if (eeprom->group0_slew == SLOW_SLEW) |
| 2760 | output[0x0c] |= SLOW_SLEW; |
| 2761 | |
| 2762 | if (eeprom->group1_drive > DRIVE_16MA) |
| 2763 | output[0x0d] |= DRIVE_16MA; |
| 2764 | else |
| 2765 | output[0x0d] |= eeprom->group1_drive; |
| 2766 | if (eeprom->group1_schmitt == IS_SCHMITT) |
| 2767 | output[0x0d] |= IS_SCHMITT; |
| 2768 | if (eeprom->group1_slew == SLOW_SLEW) |
| 2769 | output[0x0d] |= SLOW_SLEW; |
| 2770 | |
| 2771 | set_ft232h_cbus(eeprom, output); |
| 2772 | |
| 2773 | output[0x1e] = eeprom->chip; |
| 2774 | fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n"); |
| 2775 | break; |
| 2776 | |
| 2777 | } |
| 2778 | |
| 2779 | // calculate checksum |
| 2780 | checksum = 0xAAAA; |
| 2781 | |
| 2782 | for (i = 0; i < eeprom->size/2-1; i++) |
| 2783 | { |
| 2784 | value = output[i*2]; |
| 2785 | value += output[(i*2)+1] << 8; |
| 2786 | |
| 2787 | checksum = value^checksum; |
| 2788 | checksum = (checksum << 1) | (checksum >> 15); |
| 2789 | } |
| 2790 | |
| 2791 | output[eeprom->size-2] = checksum; |
| 2792 | output[eeprom->size-1] = checksum >> 8; |
| 2793 | |
| 2794 | return user_area_size; |
| 2795 | } |
| 2796 | /* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted |
| 2797 | * EEPROM structure |
| 2798 | * |
| 2799 | * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we |
| 2800 | */ |
| 2801 | static unsigned char bit2type(unsigned char bits) |
| 2802 | { |
| 2803 | switch (bits) |
| 2804 | { |
| 2805 | case 0: return CHANNEL_IS_UART; |
| 2806 | case 1: return CHANNEL_IS_FIFO; |
| 2807 | case 2: return CHANNEL_IS_OPTO; |
| 2808 | case 4: return CHANNEL_IS_CPU; |
| 2809 | case 8: return CHANNEL_IS_FT1284; |
| 2810 | default: |
| 2811 | fprintf(stderr," Unexpected value %d for Hardware Interface type\n", |
| 2812 | bits); |
| 2813 | } |
| 2814 | return 0; |
| 2815 | } |
| 2816 | /** |
| 2817 | Decode binary EEPROM image into an ftdi_eeprom structure. |
| 2818 | |
| 2819 | \param ftdi pointer to ftdi_context |
| 2820 | \param verbose Decode EEPROM on stdout |
| 2821 | |
| 2822 | \retval 0: all fine |
| 2823 | \retval -1: something went wrong |
| 2824 | |
| 2825 | FIXME: How to pass size? How to handle size field in ftdi_eeprom? |
| 2826 | FIXME: Strings are malloc'ed here and should be freed somewhere |
| 2827 | */ |
| 2828 | int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose) |
| 2829 | { |
| 2830 | unsigned char i, j; |
| 2831 | unsigned short checksum, eeprom_checksum, value; |
| 2832 | unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0; |
| 2833 | int eeprom_size; |
| 2834 | struct ftdi_eeprom *eeprom; |
| 2835 | unsigned char *buf = ftdi->eeprom->buf; |
| 2836 | int release; |
| 2837 | |
| 2838 | if (ftdi == NULL) |
| 2839 | ftdi_error_return(-1,"No context"); |
| 2840 | if (ftdi->eeprom == NULL) |
| 2841 | ftdi_error_return(-1,"No eeprom structure"); |
| 2842 | |
| 2843 | eeprom = ftdi->eeprom; |
| 2844 | eeprom_size = eeprom->size; |
| 2845 | |
| 2846 | // Addr 02: Vendor ID |
| 2847 | eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8); |
| 2848 | |
| 2849 | // Addr 04: Product ID |
| 2850 | eeprom->product_id = buf[0x04] + (buf[0x05] << 8); |
| 2851 | |
| 2852 | release = buf[0x06] + (buf[0x07]<<8); |
| 2853 | |
| 2854 | // Addr 08: Config descriptor |
| 2855 | // Bit 7: always 1 |
| 2856 | // Bit 6: 1 if this device is self powered, 0 if bus powered |
| 2857 | // Bit 5: 1 if this device uses remote wakeup |
| 2858 | eeprom->self_powered = buf[0x08] & 0x40; |
| 2859 | eeprom->remote_wakeup = buf[0x08] & 0x20; |
| 2860 | |
| 2861 | // Addr 09: Max power consumption: max power = value * 2 mA |
| 2862 | eeprom->max_power = buf[0x09]; |
| 2863 | |
| 2864 | // Addr 0A: Chip configuration |
| 2865 | // Bit 7: 0 - reserved |
| 2866 | // Bit 6: 0 - reserved |
| 2867 | // Bit 5: 0 - reserved |
| 2868 | // Bit 4: 1 - Change USB version on BM and 2232C |
| 2869 | // Bit 3: 1 - Use the serial number string |
| 2870 | // Bit 2: 1 - Enable suspend pull downs for lower power |
| 2871 | // Bit 1: 1 - Out EndPoint is Isochronous |
| 2872 | // Bit 0: 1 - In EndPoint is Isochronous |
| 2873 | // |
| 2874 | eeprom->in_is_isochronous = buf[0x0A]&0x01; |
| 2875 | eeprom->out_is_isochronous = buf[0x0A]&0x02; |
| 2876 | eeprom->suspend_pull_downs = buf[0x0A]&0x04; |
| 2877 | eeprom->use_serial = buf[0x0A] & USE_SERIAL_NUM; |
| 2878 | eeprom->use_usb_version = buf[0x0A] & USE_USB_VERSION_BIT; |
| 2879 | |
| 2880 | // Addr 0C: USB version low byte when 0x0A |
| 2881 | // Addr 0D: USB version high byte when 0x0A |
| 2882 | eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8); |
| 2883 | |
| 2884 | // Addr 0E: Offset of the manufacturer string + 0x80, calculated later |
| 2885 | // Addr 0F: Length of manufacturer string |
| 2886 | manufacturer_size = buf[0x0F]/2; |
| 2887 | if (eeprom->manufacturer) |
| 2888 | free(eeprom->manufacturer); |
| 2889 | if (manufacturer_size > 0) |
| 2890 | { |
| 2891 | eeprom->manufacturer = malloc(manufacturer_size); |
| 2892 | if (eeprom->manufacturer) |
| 2893 | { |
| 2894 | // Decode manufacturer |
| 2895 | i = buf[0x0E] & (eeprom_size -1); // offset |
| 2896 | for (j=0;j<manufacturer_size-1;j++) |
| 2897 | { |
| 2898 | eeprom->manufacturer[j] = buf[2*j+i+2]; |
| 2899 | } |
| 2900 | eeprom->manufacturer[j] = '\0'; |
| 2901 | } |
| 2902 | } |
| 2903 | else eeprom->manufacturer = NULL; |
| 2904 | |
| 2905 | // Addr 10: Offset of the product string + 0x80, calculated later |
| 2906 | // Addr 11: Length of product string |
| 2907 | if (eeprom->product) |
| 2908 | free(eeprom->product); |
| 2909 | product_size = buf[0x11]/2; |
| 2910 | if (product_size > 0) |
| 2911 | { |
| 2912 | eeprom->product = malloc(product_size); |
| 2913 | if (eeprom->product) |
| 2914 | { |
| 2915 | // Decode product name |
| 2916 | i = buf[0x10] & (eeprom_size -1); // offset |
| 2917 | for (j=0;j<product_size-1;j++) |
| 2918 | { |
| 2919 | eeprom->product[j] = buf[2*j+i+2]; |
| 2920 | } |
| 2921 | eeprom->product[j] = '\0'; |
| 2922 | } |
| 2923 | } |
| 2924 | else eeprom->product = NULL; |
| 2925 | |
| 2926 | // Addr 12: Offset of the serial string + 0x80, calculated later |
| 2927 | // Addr 13: Length of serial string |
| 2928 | if (eeprom->serial) |
| 2929 | free(eeprom->serial); |
| 2930 | serial_size = buf[0x13]/2; |
| 2931 | if (serial_size > 0) |
| 2932 | { |
| 2933 | eeprom->serial = malloc(serial_size); |
| 2934 | if (eeprom->serial) |
| 2935 | { |
| 2936 | // Decode serial |
| 2937 | i = buf[0x12] & (eeprom_size -1); // offset |
| 2938 | for (j=0;j<serial_size-1;j++) |
| 2939 | { |
| 2940 | eeprom->serial[j] = buf[2*j+i+2]; |
| 2941 | } |
| 2942 | eeprom->serial[j] = '\0'; |
| 2943 | } |
| 2944 | } |
| 2945 | else eeprom->serial = NULL; |
| 2946 | |
| 2947 | // verify checksum |
| 2948 | checksum = 0xAAAA; |
| 2949 | |
| 2950 | for (i = 0; i < eeprom_size/2-1; i++) |
| 2951 | { |
| 2952 | value = buf[i*2]; |
| 2953 | value += buf[(i*2)+1] << 8; |
| 2954 | |
| 2955 | checksum = value^checksum; |
| 2956 | checksum = (checksum << 1) | (checksum >> 15); |
| 2957 | } |
| 2958 | |
| 2959 | eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8); |
| 2960 | |
| 2961 | if (eeprom_checksum != checksum) |
| 2962 | { |
| 2963 | fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum); |
| 2964 | ftdi_error_return(-1,"EEPROM checksum error"); |
| 2965 | } |
| 2966 | |
| 2967 | eeprom->channel_a_type = 0; |
| 2968 | if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM)) |
| 2969 | { |
| 2970 | eeprom->chip = -1; |
| 2971 | } |
| 2972 | else if (ftdi->type == TYPE_2232C) |
| 2973 | { |
| 2974 | eeprom->channel_a_type = bit2type(buf[0x00] & 0x7); |
| 2975 | eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP; |
| 2976 | eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE; |
| 2977 | eeprom->channel_b_type = buf[0x01] & 0x7; |
| 2978 | eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP; |
| 2979 | eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE; |
| 2980 | eeprom->chip = buf[0x14]; |
| 2981 | } |
| 2982 | else if (ftdi->type == TYPE_R) |
| 2983 | { |
| 2984 | /* TYPE_R flags D2XX, not VCP as all others*/ |
| 2985 | eeprom->channel_a_driver = (~buf[0x00]) & DRIVER_VCP; |
| 2986 | eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R; |
| 2987 | if ( (buf[0x01]&0x40) != 0x40) |
| 2988 | fprintf(stderr, |
| 2989 | "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size." |
| 2990 | " If this happened with the\n" |
| 2991 | " EEPROM programmed by FTDI tools, please report " |
| 2992 | "to libftdi@developer.intra2net.com\n"); |
| 2993 | |
| 2994 | eeprom->chip = buf[0x16]; |
| 2995 | // Addr 0B: Invert data lines |
| 2996 | // Works only on FT232R, not FT245R, but no way to distinguish |
| 2997 | eeprom->invert = buf[0x0B]; |
| 2998 | // Addr 14: CBUS function: CBUS0, CBUS1 |
| 2999 | // Addr 15: CBUS function: CBUS2, CBUS3 |
| 3000 | // Addr 16: CBUS function: CBUS5 |
| 3001 | eeprom->cbus_function[0] = buf[0x14] & 0x0f; |
| 3002 | eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f; |
| 3003 | eeprom->cbus_function[2] = buf[0x15] & 0x0f; |
| 3004 | eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f; |
| 3005 | eeprom->cbus_function[4] = buf[0x16] & 0x0f; |
| 3006 | } |
| 3007 | else if ((ftdi->type == TYPE_2232H) ||(ftdi->type == TYPE_4232H)) |
| 3008 | { |
| 3009 | eeprom->channel_a_type = bit2type(buf[0x00] & 0x7); |
| 3010 | eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP; |
| 3011 | eeprom->channel_b_type = bit2type(buf[0x01] & 0x7); |
| 3012 | eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP; |
| 3013 | |
| 3014 | if (ftdi->type == TYPE_2232H) |
| 3015 | eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT; |
| 3016 | |
| 3017 | eeprom->chip = buf[0x18]; |
| 3018 | eeprom->group0_drive = buf[0x0c] & DRIVE_16MA; |
| 3019 | eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT; |
| 3020 | eeprom->group0_slew = buf[0x0c] & SLOW_SLEW; |
| 3021 | eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3; |
| 3022 | eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT; |
| 3023 | eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW; |
| 3024 | eeprom->group2_drive = buf[0x0d] & DRIVE_16MA; |
| 3025 | eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT; |
| 3026 | eeprom->group2_slew = buf[0x0d] & SLOW_SLEW; |
| 3027 | eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA; |
| 3028 | eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT; |
| 3029 | eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW; |
| 3030 | } |
| 3031 | else if (ftdi->type == TYPE_232H) |
| 3032 | { |
| 3033 | int i; |
| 3034 | |
| 3035 | eeprom->channel_a_type = buf[0x00] & 0xf; |
| 3036 | eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0; |
| 3037 | eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE; |
| 3038 | eeprom->data_order = buf[0x01] & FT1284_DATA_LSB; |
| 3039 | eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL; |
| 3040 | eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H; |
| 3041 | eeprom->group0_drive = buf[0x0c] & DRIVE_16MA; |
| 3042 | eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT; |
| 3043 | eeprom->group0_slew = buf[0x0c] & SLOW_SLEW; |
| 3044 | eeprom->group1_drive = buf[0x0d] & DRIVE_16MA; |
| 3045 | eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT; |
| 3046 | eeprom->group1_slew = buf[0x0d] & SLOW_SLEW; |
| 3047 | |
| 3048 | for(i=0; i<5; i++) |
| 3049 | { |
| 3050 | eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f; |
| 3051 | eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f; |
| 3052 | } |
| 3053 | eeprom->chip = buf[0x1e]; |
| 3054 | /*FIXME: Decipher more values*/ |
| 3055 | } |
| 3056 | |
| 3057 | if (verbose) |
| 3058 | { |
| 3059 | char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"}; |
| 3060 | fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id); |
| 3061 | fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id); |
| 3062 | fprintf(stdout, "Release: 0x%04x\n",release); |
| 3063 | |
| 3064 | if (eeprom->self_powered) |
| 3065 | fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n"); |
| 3066 | else |
| 3067 | fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power * 2, |
| 3068 | (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n"); |
| 3069 | if (eeprom->manufacturer) |
| 3070 | fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer); |
| 3071 | if (eeprom->product) |
| 3072 | fprintf(stdout, "Product: %s\n",eeprom->product); |
| 3073 | if (eeprom->serial) |
| 3074 | fprintf(stdout, "Serial: %s\n",eeprom->serial); |
| 3075 | fprintf(stdout, "Checksum : %04x\n", checksum); |
| 3076 | if (ftdi->type == TYPE_R) |
| 3077 | fprintf(stdout, "Internal EEPROM\n"); |
| 3078 | else if (eeprom->chip >= 0x46) |
| 3079 | fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip); |
| 3080 | if (eeprom->suspend_dbus7) |
| 3081 | fprintf(stdout, "Suspend on DBUS7\n"); |
| 3082 | if (eeprom->suspend_pull_downs) |
| 3083 | fprintf(stdout, "Pull IO pins low during suspend\n"); |
| 3084 | if(eeprom->powersave) |
| 3085 | { |
| 3086 | if(ftdi->type >= TYPE_232H) |
| 3087 | fprintf(stdout,"Enter low power state on ACBUS7\n"); |
| 3088 | } |
| 3089 | if (eeprom->remote_wakeup) |
| 3090 | fprintf(stdout, "Enable Remote Wake Up\n"); |
| 3091 | fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1); |
| 3092 | if (ftdi->type >= TYPE_2232C) |
| 3093 | fprintf(stdout,"Channel A has Mode %s%s%s\n", |
| 3094 | channel_mode[eeprom->channel_a_type], |
| 3095 | (eeprom->channel_a_driver)?" VCP":"", |
| 3096 | (eeprom->high_current_a)?" High Current IO":""); |
| 3097 | if (ftdi->type >= TYPE_232H) |
| 3098 | { |
| 3099 | fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n", |
| 3100 | (eeprom->clock_polarity)?"HIGH":"LOW", |
| 3101 | (eeprom->data_order)?"LSB":"MSB", |
| 3102 | (eeprom->flow_control)?"":"No "); |
| 3103 | } |
| 3104 | if ((ftdi->type >= TYPE_2232C) && (ftdi->type != TYPE_R) && (ftdi->type != TYPE_232H)) |
| 3105 | fprintf(stdout,"Channel B has Mode %s%s%s\n", |
| 3106 | channel_mode[eeprom->channel_b_type], |
| 3107 | (eeprom->channel_b_driver)?" VCP":"", |
| 3108 | (eeprom->high_current_b)?" High Current IO":""); |
| 3109 | if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) && |
| 3110 | eeprom->use_usb_version == USE_USB_VERSION_BIT) |
| 3111 | fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version); |
| 3112 | |
| 3113 | if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H)) |
| 3114 | { |
| 3115 | fprintf(stdout,"%s has %d mA drive%s%s\n", |
| 3116 | (ftdi->type == TYPE_2232H)?"AL":"A", |
| 3117 | (eeprom->group0_drive+1) *4, |
| 3118 | (eeprom->group0_schmitt)?" Schmitt Input":"", |
| 3119 | (eeprom->group0_slew)?" Slow Slew":""); |
| 3120 | fprintf(stdout,"%s has %d mA drive%s%s\n", |
| 3121 | (ftdi->type == TYPE_2232H)?"AH":"B", |
| 3122 | (eeprom->group1_drive+1) *4, |
| 3123 | (eeprom->group1_schmitt)?" Schmitt Input":"", |
| 3124 | (eeprom->group1_slew)?" Slow Slew":""); |
| 3125 | fprintf(stdout,"%s has %d mA drive%s%s\n", |
| 3126 | (ftdi->type == TYPE_2232H)?"BL":"C", |
| 3127 | (eeprom->group2_drive+1) *4, |
| 3128 | (eeprom->group2_schmitt)?" Schmitt Input":"", |
| 3129 | (eeprom->group2_slew)?" Slow Slew":""); |
| 3130 | fprintf(stdout,"%s has %d mA drive%s%s\n", |
| 3131 | (ftdi->type == TYPE_2232H)?"BH":"D", |
| 3132 | (eeprom->group3_drive+1) *4, |
| 3133 | (eeprom->group3_schmitt)?" Schmitt Input":"", |
| 3134 | (eeprom->group3_slew)?" Slow Slew":""); |
| 3135 | } |
| 3136 | else if (ftdi->type == TYPE_232H) |
| 3137 | { |
| 3138 | int i; |
| 3139 | char *cbush_mux[] = {"TRISTATE","RXLED","TXLED", "TXRXLED","PWREN", |
| 3140 | "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN", |
| 3141 | "CLK30","CLK15","CLK7_5" |
| 3142 | }; |
| 3143 | fprintf(stdout,"ACBUS has %d mA drive%s%s\n", |
| 3144 | (eeprom->group0_drive+1) *4, |
| 3145 | (eeprom->group0_schmitt)?" Schmitt Input":"", |
| 3146 | (eeprom->group0_slew)?" Slow Slew":""); |
| 3147 | fprintf(stdout,"ADBUS has %d mA drive%s%s\n", |
| 3148 | (eeprom->group1_drive+1) *4, |
| 3149 | (eeprom->group1_schmitt)?" Schmitt Input":"", |
| 3150 | (eeprom->group1_slew)?" Slow Slew":""); |
| 3151 | for (i=0; i<10; i++) |
| 3152 | { |
| 3153 | if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 ) |
| 3154 | fprintf(stdout,"C%d Function: %s\n", i, |
| 3155 | cbush_mux[eeprom->cbus_function[i]]); |
| 3156 | } |
| 3157 | |
| 3158 | } |
| 3159 | |
| 3160 | if (ftdi->type == TYPE_R) |
| 3161 | { |
| 3162 | char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED", |
| 3163 | "SLEEP","CLK48","CLK24","CLK12","CLK6", |
| 3164 | "IOMODE","BB_WR","BB_RD" |
| 3165 | }; |
| 3166 | char *cbus_BB[] = {"RXF","TXE","RD", "WR"}; |
| 3167 | |
| 3168 | if (eeprom->invert) |
| 3169 | { |
| 3170 | char *r_bits[] = {"TXD","RXD","RTS", "CTS","DTR","DSR","DCD","RI"}; |
| 3171 | fprintf(stdout,"Inverted bits:"); |
| 3172 | for (i=0; i<8; i++) |
| 3173 | if ((eeprom->invert & (1<<i)) == (1<<i)) |
| 3174 | fprintf(stdout," %s",r_bits[i]); |
| 3175 | fprintf(stdout,"\n"); |
| 3176 | } |
| 3177 | for (i=0; i<5; i++) |
| 3178 | { |
| 3179 | if (eeprom->cbus_function[i]<CBUS_BB) |
| 3180 | fprintf(stdout,"C%d Function: %s\n", i, |
| 3181 | cbus_mux[eeprom->cbus_function[i]]); |
| 3182 | else |
| 3183 | { |
| 3184 | if (i < 4) |
| 3185 | /* Running MPROG show that C0..3 have fixed function Synchronous |
| 3186 | Bit Bang mode */ |
| 3187 | fprintf(stdout,"C%d BB Function: %s\n", i, |
| 3188 | cbus_BB[i]); |
| 3189 | else |
| 3190 | fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n"); |
| 3191 | } |
| 3192 | } |
| 3193 | } |
| 3194 | } |
| 3195 | return 0; |
| 3196 | } |
| 3197 | |
| 3198 | /** |
| 3199 | Get a value from the decoded EEPROM structure |
| 3200 | |
| 3201 | \param ftdi pointer to ftdi_context |
| 3202 | \param value_name Enum of the value to query |
| 3203 | \param value Pointer to store read value |
| 3204 | |
| 3205 | \retval 0: all fine |
| 3206 | \retval -1: Value doesn't exist |
| 3207 | */ |
| 3208 | int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value) |
| 3209 | { |
| 3210 | switch (value_name) |
| 3211 | { |
| 3212 | case VENDOR_ID: |
| 3213 | *value = ftdi->eeprom->vendor_id; |
| 3214 | break; |
| 3215 | case PRODUCT_ID: |
| 3216 | *value = ftdi->eeprom->product_id; |
| 3217 | break; |
| 3218 | case SELF_POWERED: |
| 3219 | *value = ftdi->eeprom->self_powered; |
| 3220 | break; |
| 3221 | case REMOTE_WAKEUP: |
| 3222 | *value = ftdi->eeprom->remote_wakeup; |
| 3223 | break; |
| 3224 | case IS_NOT_PNP: |
| 3225 | *value = ftdi->eeprom->is_not_pnp; |
| 3226 | break; |
| 3227 | case SUSPEND_DBUS7: |
| 3228 | *value = ftdi->eeprom->suspend_dbus7; |
| 3229 | break; |
| 3230 | case IN_IS_ISOCHRONOUS: |
| 3231 | *value = ftdi->eeprom->in_is_isochronous; |
| 3232 | break; |
| 3233 | case OUT_IS_ISOCHRONOUS: |
| 3234 | *value = ftdi->eeprom->out_is_isochronous; |
| 3235 | break; |
| 3236 | case SUSPEND_PULL_DOWNS: |
| 3237 | *value = ftdi->eeprom->suspend_pull_downs; |
| 3238 | break; |
| 3239 | case USE_SERIAL: |
| 3240 | *value = ftdi->eeprom->use_serial; |
| 3241 | break; |
| 3242 | case USB_VERSION: |
| 3243 | *value = ftdi->eeprom->usb_version; |
| 3244 | break; |
| 3245 | case USE_USB_VERSION: |
| 3246 | *value = ftdi->eeprom->use_usb_version; |
| 3247 | break; |
| 3248 | case MAX_POWER: |
| 3249 | *value = ftdi->eeprom->max_power; |
| 3250 | break; |
| 3251 | case CHANNEL_A_TYPE: |
| 3252 | *value = ftdi->eeprom->channel_a_type; |
| 3253 | break; |
| 3254 | case CHANNEL_B_TYPE: |
| 3255 | *value = ftdi->eeprom->channel_b_type; |
| 3256 | break; |
| 3257 | case CHANNEL_A_DRIVER: |
| 3258 | *value = ftdi->eeprom->channel_a_driver; |
| 3259 | break; |
| 3260 | case CHANNEL_B_DRIVER: |
| 3261 | *value = ftdi->eeprom->channel_b_driver; |
| 3262 | break; |
| 3263 | case CBUS_FUNCTION_0: |
| 3264 | *value = ftdi->eeprom->cbus_function[0]; |
| 3265 | break; |
| 3266 | case CBUS_FUNCTION_1: |
| 3267 | *value = ftdi->eeprom->cbus_function[1]; |
| 3268 | break; |
| 3269 | case CBUS_FUNCTION_2: |
| 3270 | *value = ftdi->eeprom->cbus_function[2]; |
| 3271 | break; |
| 3272 | case CBUS_FUNCTION_3: |
| 3273 | *value = ftdi->eeprom->cbus_function[3]; |
| 3274 | break; |
| 3275 | case CBUS_FUNCTION_4: |
| 3276 | *value = ftdi->eeprom->cbus_function[4]; |
| 3277 | break; |
| 3278 | case CBUS_FUNCTION_5: |
| 3279 | *value = ftdi->eeprom->cbus_function[5]; |
| 3280 | break; |
| 3281 | case CBUS_FUNCTION_6: |
| 3282 | *value = ftdi->eeprom->cbus_function[6]; |
| 3283 | break; |
| 3284 | case CBUS_FUNCTION_7: |
| 3285 | *value = ftdi->eeprom->cbus_function[7]; |
| 3286 | break; |
| 3287 | case CBUS_FUNCTION_8: |
| 3288 | *value = ftdi->eeprom->cbus_function[8]; |
| 3289 | break; |
| 3290 | case CBUS_FUNCTION_9: |
| 3291 | *value = ftdi->eeprom->cbus_function[8]; |
| 3292 | break; |
| 3293 | case HIGH_CURRENT: |
| 3294 | *value = ftdi->eeprom->high_current; |
| 3295 | break; |
| 3296 | case HIGH_CURRENT_A: |
| 3297 | *value = ftdi->eeprom->high_current_a; |
| 3298 | break; |
| 3299 | case HIGH_CURRENT_B: |
| 3300 | *value = ftdi->eeprom->high_current_b; |
| 3301 | break; |
| 3302 | case INVERT: |
| 3303 | *value = ftdi->eeprom->invert; |
| 3304 | break; |
| 3305 | case GROUP0_DRIVE: |
| 3306 | *value = ftdi->eeprom->group0_drive; |
| 3307 | break; |
| 3308 | case GROUP0_SCHMITT: |
| 3309 | *value = ftdi->eeprom->group0_schmitt; |
| 3310 | break; |
| 3311 | case GROUP0_SLEW: |
| 3312 | *value = ftdi->eeprom->group0_slew; |
| 3313 | break; |
| 3314 | case GROUP1_DRIVE: |
| 3315 | *value = ftdi->eeprom->group1_drive; |
| 3316 | break; |
| 3317 | case GROUP1_SCHMITT: |
| 3318 | *value = ftdi->eeprom->group1_schmitt; |
| 3319 | break; |
| 3320 | case GROUP1_SLEW: |
| 3321 | *value = ftdi->eeprom->group1_slew; |
| 3322 | break; |
| 3323 | case GROUP2_DRIVE: |
| 3324 | *value = ftdi->eeprom->group2_drive; |
| 3325 | break; |
| 3326 | case GROUP2_SCHMITT: |
| 3327 | *value = ftdi->eeprom->group2_schmitt; |
| 3328 | break; |
| 3329 | case GROUP2_SLEW: |
| 3330 | *value = ftdi->eeprom->group2_slew; |
| 3331 | break; |
| 3332 | case GROUP3_DRIVE: |
| 3333 | *value = ftdi->eeprom->group3_drive; |
| 3334 | break; |
| 3335 | case GROUP3_SCHMITT: |
| 3336 | *value = ftdi->eeprom->group3_schmitt; |
| 3337 | break; |
| 3338 | case GROUP3_SLEW: |
| 3339 | *value = ftdi->eeprom->group3_slew; |
| 3340 | break; |
| 3341 | case POWER_SAVE: |
| 3342 | *value = ftdi->eeprom->powersave; |
| 3343 | break; |
| 3344 | case CLOCK_POLARITY: |
| 3345 | *value = ftdi->eeprom->clock_polarity; |
| 3346 | break; |
| 3347 | case DATA_ORDER: |
| 3348 | *value = ftdi->eeprom->data_order; |
| 3349 | break; |
| 3350 | case FLOW_CONTROL: |
| 3351 | *value = ftdi->eeprom->flow_control; |
| 3352 | break; |
| 3353 | case CHIP_TYPE: |
| 3354 | *value = ftdi->eeprom->chip; |
| 3355 | break; |
| 3356 | case CHIP_SIZE: |
| 3357 | *value = ftdi->eeprom->size; |
| 3358 | break; |
| 3359 | default: |
| 3360 | ftdi_error_return(-1, "Request for unknown EEPROM value"); |
| 3361 | } |
| 3362 | return 0; |
| 3363 | } |
| 3364 | |
| 3365 | /** |
| 3366 | Set a value in the decoded EEPROM Structure |
| 3367 | No parameter checking is performed |
| 3368 | |
| 3369 | \param ftdi pointer to ftdi_context |
| 3370 | \param value_name Enum of the value to set |
| 3371 | \param value to set |
| 3372 | |
| 3373 | \retval 0: all fine |
| 3374 | \retval -1: Value doesn't exist |
| 3375 | \retval -2: Value not user settable |
| 3376 | */ |
| 3377 | int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value) |
| 3378 | { |
| 3379 | switch (value_name) |
| 3380 | { |
| 3381 | case VENDOR_ID: |
| 3382 | ftdi->eeprom->vendor_id = value; |
| 3383 | break; |
| 3384 | case PRODUCT_ID: |
| 3385 | ftdi->eeprom->product_id = value; |
| 3386 | break; |
| 3387 | case SELF_POWERED: |
| 3388 | ftdi->eeprom->self_powered = value; |
| 3389 | break; |
| 3390 | case REMOTE_WAKEUP: |
| 3391 | ftdi->eeprom->remote_wakeup = value; |
| 3392 | break; |
| 3393 | case IS_NOT_PNP: |
| 3394 | ftdi->eeprom->is_not_pnp = value; |
| 3395 | break; |
| 3396 | case SUSPEND_DBUS7: |
| 3397 | ftdi->eeprom->suspend_dbus7 = value; |
| 3398 | break; |
| 3399 | case IN_IS_ISOCHRONOUS: |
| 3400 | ftdi->eeprom->in_is_isochronous = value; |
| 3401 | break; |
| 3402 | case OUT_IS_ISOCHRONOUS: |
| 3403 | ftdi->eeprom->out_is_isochronous = value; |
| 3404 | break; |
| 3405 | case SUSPEND_PULL_DOWNS: |
| 3406 | ftdi->eeprom->suspend_pull_downs = value; |
| 3407 | break; |
| 3408 | case USE_SERIAL: |
| 3409 | ftdi->eeprom->use_serial = value; |
| 3410 | break; |
| 3411 | case USB_VERSION: |
| 3412 | ftdi->eeprom->usb_version = value; |
| 3413 | break; |
| 3414 | case USE_USB_VERSION: |
| 3415 | ftdi->eeprom->use_usb_version = value; |
| 3416 | break; |
| 3417 | case MAX_POWER: |
| 3418 | ftdi->eeprom->max_power = value; |
| 3419 | break; |
| 3420 | case CHANNEL_A_TYPE: |
| 3421 | ftdi->eeprom->channel_a_type = value; |
| 3422 | break; |
| 3423 | case CHANNEL_B_TYPE: |
| 3424 | ftdi->eeprom->channel_b_type = value; |
| 3425 | break; |
| 3426 | case CHANNEL_A_DRIVER: |
| 3427 | ftdi->eeprom->channel_a_driver = value; |
| 3428 | break; |
| 3429 | case CHANNEL_B_DRIVER: |
| 3430 | ftdi->eeprom->channel_b_driver = value; |
| 3431 | break; |
| 3432 | case CBUS_FUNCTION_0: |
| 3433 | ftdi->eeprom->cbus_function[0] = value; |
| 3434 | break; |
| 3435 | case CBUS_FUNCTION_1: |
| 3436 | ftdi->eeprom->cbus_function[1] = value; |
| 3437 | break; |
| 3438 | case CBUS_FUNCTION_2: |
| 3439 | ftdi->eeprom->cbus_function[2] = value; |
| 3440 | break; |
| 3441 | case CBUS_FUNCTION_3: |
| 3442 | ftdi->eeprom->cbus_function[3] = value; |
| 3443 | break; |
| 3444 | case CBUS_FUNCTION_4: |
| 3445 | ftdi->eeprom->cbus_function[4] = value; |
| 3446 | break; |
| 3447 | case CBUS_FUNCTION_5: |
| 3448 | ftdi->eeprom->cbus_function[5] = value; |
| 3449 | break; |
| 3450 | case CBUS_FUNCTION_6: |
| 3451 | ftdi->eeprom->cbus_function[6] = value; |
| 3452 | break; |
| 3453 | case CBUS_FUNCTION_7: |
| 3454 | ftdi->eeprom->cbus_function[7] = value; |
| 3455 | break; |
| 3456 | case CBUS_FUNCTION_8: |
| 3457 | ftdi->eeprom->cbus_function[8] = value; |
| 3458 | break; |
| 3459 | case CBUS_FUNCTION_9: |
| 3460 | ftdi->eeprom->cbus_function[9] = value; |
| 3461 | break; |
| 3462 | case HIGH_CURRENT: |
| 3463 | ftdi->eeprom->high_current = value; |
| 3464 | break; |
| 3465 | case HIGH_CURRENT_A: |
| 3466 | ftdi->eeprom->high_current_a = value; |
| 3467 | break; |
| 3468 | case HIGH_CURRENT_B: |
| 3469 | ftdi->eeprom->high_current_b = value; |
| 3470 | break; |
| 3471 | case INVERT: |
| 3472 | ftdi->eeprom->invert = value; |
| 3473 | break; |
| 3474 | case GROUP0_DRIVE: |
| 3475 | ftdi->eeprom->group0_drive = value; |
| 3476 | break; |
| 3477 | case GROUP0_SCHMITT: |
| 3478 | ftdi->eeprom->group0_schmitt = value; |
| 3479 | break; |
| 3480 | case GROUP0_SLEW: |
| 3481 | ftdi->eeprom->group0_slew = value; |
| 3482 | break; |
| 3483 | case GROUP1_DRIVE: |
| 3484 | ftdi->eeprom->group1_drive = value; |
| 3485 | break; |
| 3486 | case GROUP1_SCHMITT: |
| 3487 | ftdi->eeprom->group1_schmitt = value; |
| 3488 | break; |
| 3489 | case GROUP1_SLEW: |
| 3490 | ftdi->eeprom->group1_slew = value; |
| 3491 | break; |
| 3492 | case GROUP2_DRIVE: |
| 3493 | ftdi->eeprom->group2_drive = value; |
| 3494 | break; |
| 3495 | case GROUP2_SCHMITT: |
| 3496 | ftdi->eeprom->group2_schmitt = value; |
| 3497 | break; |
| 3498 | case GROUP2_SLEW: |
| 3499 | ftdi->eeprom->group2_slew = value; |
| 3500 | break; |
| 3501 | case GROUP3_DRIVE: |
| 3502 | ftdi->eeprom->group3_drive = value; |
| 3503 | break; |
| 3504 | case GROUP3_SCHMITT: |
| 3505 | ftdi->eeprom->group3_schmitt = value; |
| 3506 | break; |
| 3507 | case GROUP3_SLEW: |
| 3508 | ftdi->eeprom->group3_slew = value; |
| 3509 | break; |
| 3510 | case CHIP_TYPE: |
| 3511 | ftdi->eeprom->chip = value; |
| 3512 | break; |
| 3513 | case POWER_SAVE: |
| 3514 | ftdi->eeprom->powersave = value; |
| 3515 | break; |
| 3516 | case CLOCK_POLARITY: |
| 3517 | ftdi->eeprom->clock_polarity = value; |
| 3518 | break; |
| 3519 | case DATA_ORDER: |
| 3520 | ftdi->eeprom->data_order = value; |
| 3521 | break; |
| 3522 | case FLOW_CONTROL: |
| 3523 | ftdi->eeprom->flow_control = value; |
| 3524 | break; |
| 3525 | case CHIP_SIZE: |
| 3526 | ftdi_error_return(-2, "EEPROM Value can't be changed"); |
| 3527 | default : |
| 3528 | ftdi_error_return(-1, "Request to unknown EEPROM value"); |
| 3529 | } |
| 3530 | return 0; |
| 3531 | } |
| 3532 | |
| 3533 | /** Get the read-only buffer to the binary EEPROM content |
| 3534 | |
| 3535 | \param ftdi pointer to ftdi_context |
| 3536 | \param buf buffer to receive EEPROM content |
| 3537 | \param size Size of receiving buffer |
| 3538 | |
| 3539 | \retval 0: All fine |
| 3540 | \retval -1: struct ftdi_contxt or ftdi_eeprom missing |
| 3541 | \retval -2: Not enough room to store eeprom |
| 3542 | */ |
| 3543 | int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size) |
| 3544 | { |
| 3545 | if (!ftdi || !(ftdi->eeprom)) |
| 3546 | ftdi_error_return(-1, "No appropriate structure"); |
| 3547 | |
| 3548 | if (!buf || size < ftdi->eeprom->size) |
| 3549 | ftdi_error_return(-1, "Not enough room to store eeprom"); |
| 3550 | |
| 3551 | // Only copy up to FTDI_MAX_EEPROM_SIZE bytes |
| 3552 | if (size > FTDI_MAX_EEPROM_SIZE) |
| 3553 | size = FTDI_MAX_EEPROM_SIZE; |
| 3554 | |
| 3555 | memcpy(buf, ftdi->eeprom->buf, size); |
| 3556 | |
| 3557 | return 0; |
| 3558 | } |
| 3559 | |
| 3560 | /** Set the EEPROM content from the user-supplied prefilled buffer |
| 3561 | |
| 3562 | \param ftdi pointer to ftdi_context |
| 3563 | \param buf buffer to read EEPROM content |
| 3564 | \param size Size of buffer |
| 3565 | |
| 3566 | \retval 0: All fine |
| 3567 | \retval -1: struct ftdi_contxt or ftdi_eeprom of buf missing |
| 3568 | */ |
| 3569 | int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size) |
| 3570 | { |
| 3571 | if (!ftdi || !(ftdi->eeprom) || !buf) |
| 3572 | ftdi_error_return(-1, "No appropriate structure"); |
| 3573 | |
| 3574 | // Only copy up to FTDI_MAX_EEPROM_SIZE bytes |
| 3575 | if (size > FTDI_MAX_EEPROM_SIZE) |
| 3576 | size = FTDI_MAX_EEPROM_SIZE; |
| 3577 | |
| 3578 | memcpy(ftdi->eeprom->buf, buf, size); |
| 3579 | |
| 3580 | return 0; |
| 3581 | } |
| 3582 | |
| 3583 | /** |
| 3584 | Read eeprom location |
| 3585 | |
| 3586 | \param ftdi pointer to ftdi_context |
| 3587 | \param eeprom_addr Address of eeprom location to be read |
| 3588 | \param eeprom_val Pointer to store read eeprom location |
| 3589 | |
| 3590 | \retval 0: all fine |
| 3591 | \retval -1: read failed |
| 3592 | \retval -2: USB device unavailable |
| 3593 | */ |
| 3594 | int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val) |
| 3595 | { |
| 3596 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 3597 | ftdi_error_return(-2, "USB device unavailable"); |
| 3598 | |
| 3599 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (unsigned char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2) |
| 3600 | ftdi_error_return(-1, "reading eeprom failed"); |
| 3601 | |
| 3602 | return 0; |
| 3603 | } |
| 3604 | |
| 3605 | /** |
| 3606 | Read eeprom |
| 3607 | |
| 3608 | \param ftdi pointer to ftdi_context |
| 3609 | |
| 3610 | \retval 0: all fine |
| 3611 | \retval -1: read failed |
| 3612 | \retval -2: USB device unavailable |
| 3613 | */ |
| 3614 | int ftdi_read_eeprom(struct ftdi_context *ftdi) |
| 3615 | { |
| 3616 | int i; |
| 3617 | unsigned char *buf; |
| 3618 | |
| 3619 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 3620 | ftdi_error_return(-2, "USB device unavailable"); |
| 3621 | buf = ftdi->eeprom->buf; |
| 3622 | |
| 3623 | for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++) |
| 3624 | { |
| 3625 | if (libusb_control_transfer( |
| 3626 | ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i, |
| 3627 | buf+(i*2), 2, ftdi->usb_read_timeout) != 2) |
| 3628 | ftdi_error_return(-1, "reading eeprom failed"); |
| 3629 | } |
| 3630 | |
| 3631 | if (ftdi->type == TYPE_R) |
| 3632 | ftdi->eeprom->size = 0x80; |
| 3633 | /* Guesses size of eeprom by comparing halves |
| 3634 | - will not work with blank eeprom */ |
| 3635 | else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1)) |
| 3636 | ftdi->eeprom->size = -1; |
| 3637 | else if (memcmp(buf,&buf[0x80],0x80) == 0) |
| 3638 | ftdi->eeprom->size = 0x80; |
| 3639 | else if (memcmp(buf,&buf[0x40],0x40) == 0) |
| 3640 | ftdi->eeprom->size = 0x40; |
| 3641 | else |
| 3642 | ftdi->eeprom->size = 0x100; |
| 3643 | return 0; |
| 3644 | } |
| 3645 | |
| 3646 | /* |
| 3647 | ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID |
| 3648 | Function is only used internally |
| 3649 | \internal |
| 3650 | */ |
| 3651 | static unsigned char ftdi_read_chipid_shift(unsigned char value) |
| 3652 | { |
| 3653 | return ((value & 1) << 1) | |
| 3654 | ((value & 2) << 5) | |
| 3655 | ((value & 4) >> 2) | |
| 3656 | ((value & 8) << 4) | |
| 3657 | ((value & 16) >> 1) | |
| 3658 | ((value & 32) >> 1) | |
| 3659 | ((value & 64) >> 4) | |
| 3660 | ((value & 128) >> 2); |
| 3661 | } |
| 3662 | |
| 3663 | /** |
| 3664 | Read the FTDIChip-ID from R-type devices |
| 3665 | |
| 3666 | \param ftdi pointer to ftdi_context |
| 3667 | \param chipid Pointer to store FTDIChip-ID |
| 3668 | |
| 3669 | \retval 0: all fine |
| 3670 | \retval -1: read failed |
| 3671 | \retval -2: USB device unavailable |
| 3672 | */ |
| 3673 | int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid) |
| 3674 | { |
| 3675 | unsigned int a = 0, b = 0; |
| 3676 | |
| 3677 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 3678 | ftdi_error_return(-2, "USB device unavailable"); |
| 3679 | |
| 3680 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x43, (unsigned char *)&a, 2, ftdi->usb_read_timeout) == 2) |
| 3681 | { |
| 3682 | a = a << 8 | a >> 8; |
| 3683 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x44, (unsigned char *)&b, 2, ftdi->usb_read_timeout) == 2) |
| 3684 | { |
| 3685 | b = b << 8 | b >> 8; |
| 3686 | a = (a << 16) | (b & 0xFFFF); |
| 3687 | a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8 |
| 3688 | | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24; |
| 3689 | *chipid = a ^ 0xa5f0f7d1; |
| 3690 | return 0; |
| 3691 | } |
| 3692 | } |
| 3693 | |
| 3694 | ftdi_error_return(-1, "read of FTDIChip-ID failed"); |
| 3695 | } |
| 3696 | |
| 3697 | /** |
| 3698 | Write eeprom location |
| 3699 | |
| 3700 | \param ftdi pointer to ftdi_context |
| 3701 | \param eeprom_addr Address of eeprom location to be written |
| 3702 | \param eeprom_val Value to be written |
| 3703 | |
| 3704 | \retval 0: all fine |
| 3705 | \retval -1: write failed |
| 3706 | \retval -2: USB device unavailable |
| 3707 | \retval -3: Invalid access to checksum protected area below 0x80 |
| 3708 | \retval -4: Device can't access unprotected area |
| 3709 | \retval -5: Reading chip type failed |
| 3710 | */ |
| 3711 | int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, |
| 3712 | unsigned short eeprom_val) |
| 3713 | { |
| 3714 | int chip_type_location; |
| 3715 | unsigned short chip_type; |
| 3716 | |
| 3717 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 3718 | ftdi_error_return(-2, "USB device unavailable"); |
| 3719 | |
| 3720 | if (eeprom_addr <0x80) |
| 3721 | ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80"); |
| 3722 | |
| 3723 | |
| 3724 | switch (ftdi->type) |
| 3725 | { |
| 3726 | case TYPE_BM: |
| 3727 | case TYPE_2232C: |
| 3728 | chip_type_location = 0x14; |
| 3729 | break; |
| 3730 | case TYPE_2232H: |
| 3731 | case TYPE_4232H: |
| 3732 | chip_type_location = 0x18; |
| 3733 | break; |
| 3734 | case TYPE_232H: |
| 3735 | chip_type_location = 0x1e; |
| 3736 | break; |
| 3737 | default: |
| 3738 | ftdi_error_return(-4, "Device can't access unprotected area"); |
| 3739 | } |
| 3740 | |
| 3741 | if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type)) |
| 3742 | ftdi_error_return(-5, "Reading failed failed"); |
| 3743 | fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type); |
| 3744 | if ((chip_type & 0xff) != 0x66) |
| 3745 | { |
| 3746 | ftdi_error_return(-6, "EEPROM is not of 93x66"); |
| 3747 | } |
| 3748 | |
| 3749 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 3750 | SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr, |
| 3751 | NULL, 0, ftdi->usb_write_timeout) != 0) |
| 3752 | ftdi_error_return(-1, "unable to write eeprom"); |
| 3753 | |
| 3754 | return 0; |
| 3755 | } |
| 3756 | |
| 3757 | /** |
| 3758 | Write eeprom |
| 3759 | |
| 3760 | \param ftdi pointer to ftdi_context |
| 3761 | |
| 3762 | \retval 0: all fine |
| 3763 | \retval -1: read failed |
| 3764 | \retval -2: USB device unavailable |
| 3765 | \retval -3: EEPROM not initialized for the connected device; |
| 3766 | */ |
| 3767 | int ftdi_write_eeprom(struct ftdi_context *ftdi) |
| 3768 | { |
| 3769 | unsigned short usb_val, status; |
| 3770 | int i, ret; |
| 3771 | unsigned char *eeprom; |
| 3772 | |
| 3773 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 3774 | ftdi_error_return(-2, "USB device unavailable"); |
| 3775 | |
| 3776 | if(ftdi->eeprom->initialized_for_connected_device == 0) |
| 3777 | ftdi_error_return(-3, "EEPROM not initialized for the connected device"); |
| 3778 | |
| 3779 | eeprom = ftdi->eeprom->buf; |
| 3780 | |
| 3781 | /* These commands were traced while running MProg */ |
| 3782 | if ((ret = ftdi_usb_reset(ftdi)) != 0) |
| 3783 | return ret; |
| 3784 | if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0) |
| 3785 | return ret; |
| 3786 | if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0) |
| 3787 | return ret; |
| 3788 | |
| 3789 | for (i = 0; i < ftdi->eeprom->size/2; i++) |
| 3790 | { |
| 3791 | usb_val = eeprom[i*2]; |
| 3792 | usb_val += eeprom[(i*2)+1] << 8; |
| 3793 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 3794 | SIO_WRITE_EEPROM_REQUEST, usb_val, i, |
| 3795 | NULL, 0, ftdi->usb_write_timeout) < 0) |
| 3796 | ftdi_error_return(-1, "unable to write eeprom"); |
| 3797 | } |
| 3798 | |
| 3799 | return 0; |
| 3800 | } |
| 3801 | |
| 3802 | /** |
| 3803 | Erase eeprom |
| 3804 | |
| 3805 | This is not supported on FT232R/FT245R according to the MProg manual from FTDI. |
| 3806 | |
| 3807 | \param ftdi pointer to ftdi_context |
| 3808 | |
| 3809 | \retval 0: all fine |
| 3810 | \retval -1: erase failed |
| 3811 | \retval -2: USB device unavailable |
| 3812 | \retval -3: Writing magic failed |
| 3813 | \retval -4: Read EEPROM failed |
| 3814 | \retval -5: Unexpected EEPROM value |
| 3815 | */ |
| 3816 | #define MAGIC 0x55aa |
| 3817 | int ftdi_erase_eeprom(struct ftdi_context *ftdi) |
| 3818 | { |
| 3819 | unsigned short eeprom_value; |
| 3820 | if (ftdi == NULL || ftdi->usb_dev == NULL) |
| 3821 | ftdi_error_return(-2, "USB device unavailable"); |
| 3822 | |
| 3823 | if (ftdi->type == TYPE_R) |
| 3824 | { |
| 3825 | ftdi->eeprom->chip = 0; |
| 3826 | return 0; |
| 3827 | } |
| 3828 | |
| 3829 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, |
| 3830 | 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 3831 | ftdi_error_return(-1, "unable to erase eeprom"); |
| 3832 | |
| 3833 | |
| 3834 | /* detect chip type by writing 0x55AA as magic at word position 0xc0 |
| 3835 | Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40 |
| 3836 | Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80 |
| 3837 | Chip is 93x66 if magic is only read at word position 0xc0*/ |
| 3838 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, |
| 3839 | SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0, |
| 3840 | NULL, 0, ftdi->usb_write_timeout) != 0) |
| 3841 | ftdi_error_return(-3, "Writing magic failed"); |
| 3842 | if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value)) |
| 3843 | ftdi_error_return(-4, "Reading failed failed"); |
| 3844 | if (eeprom_value == MAGIC) |
| 3845 | { |
| 3846 | ftdi->eeprom->chip = 0x46; |
| 3847 | } |
| 3848 | else |
| 3849 | { |
| 3850 | if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value)) |
| 3851 | ftdi_error_return(-4, "Reading failed failed"); |
| 3852 | if (eeprom_value == MAGIC) |
| 3853 | ftdi->eeprom->chip = 0x56; |
| 3854 | else |
| 3855 | { |
| 3856 | if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value)) |
| 3857 | ftdi_error_return(-4, "Reading failed failed"); |
| 3858 | if (eeprom_value == MAGIC) |
| 3859 | ftdi->eeprom->chip = 0x66; |
| 3860 | else |
| 3861 | { |
| 3862 | ftdi->eeprom->chip = -1; |
| 3863 | } |
| 3864 | } |
| 3865 | } |
| 3866 | if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, |
| 3867 | 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0) |
| 3868 | ftdi_error_return(-1, "unable to erase eeprom"); |
| 3869 | return 0; |
| 3870 | } |
| 3871 | |
| 3872 | /** |
| 3873 | Get string representation for last error code |
| 3874 | |
| 3875 | \param ftdi pointer to ftdi_context |
| 3876 | |
| 3877 | \retval Pointer to error string |
| 3878 | */ |
| 3879 | char *ftdi_get_error_string (struct ftdi_context *ftdi) |
| 3880 | { |
| 3881 | if (ftdi == NULL) |
| 3882 | return ""; |
| 3883 | |
| 3884 | return ftdi->error_str; |
| 3885 | } |
| 3886 | |
| 3887 | /* @} end of doxygen libftdi group */ |