1 /* 2 * Ben NanoNote and Arduino USB Host shield communication. 3 * 4 * Copyright 2013 Paul Boddie 5 * 6 * SPI functions derived from those in lib/atben.c by Werner Almesberger: 7 * 8 * Copyright 2010-2011 Werner Almesberger 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 */ 15 16 #include <ubb/ubb.h> 17 #include <stdio.h> 18 #include <signal.h> 19 #include <stdlib.h> 20 #include <usb.h> 21 #include <unistd.h> 22 #include <string.h> 23 #include <endian.h> 24 25 /* Found in Python's asdl.h. */ 26 27 #ifndef __cplusplus 28 typedef enum {false, true} bool; 29 #endif 30 31 /* Initialisation states. */ 32 33 typedef enum 34 { 35 MAX_DEVSTATE_INIT = 0, 36 MAX_DEVSTATE_CONNECTED = 1, 37 MAX_DEVSTATE_RESET = 2, 38 MAX_DEVSTATE_READY = 3 39 } max_devstate; 40 41 /* Device details. */ 42 43 typedef struct 44 { 45 bool in_toggle, out_toggle; 46 struct usb_device_descriptor desc; 47 uint8_t address, max_packet_size; 48 } max_device; 49 50 static uint16_t next_address = 1; 51 52 /* Pin assignments: 53 * 54 * Sniffer UBB Shield 55 * ------- ---- ------ 56 * DAT2 DAT2 9 (INT) 57 * CD DAT3 10 (SS) 58 * CMD CMD 7 (RESET) 59 * VCC VDD VIN 60 * CLK CLK 13 (SCLK) 61 * GND GND GND 62 * DAT0 DAT0 11 (MOSI) 63 * DAT1 DAT1 12 (MISO) 64 * 8 (GPX) (not assigned) 65 */ 66 67 #define MAX_RESET UBB_CMD 68 #define MAX_SCLK UBB_CLK 69 #define MAX_MOSI UBB_DAT0 70 #define MAX_MISO UBB_DAT1 71 #define MAX_INT UBB_DAT2 72 #define MAX_SS UBB_DAT3 73 74 /* MAX3421E definitions. */ 75 76 #define MAX_REG_READ 0x00 77 #define MAX_REG_WRITE 0x02 78 79 #define MAX_REG_RCVFIFO 1 80 #define MAX_REG_SNDFIFO 2 81 #define MAX_REG_SUDFIFO 4 82 #define MAX_REG_RCVBC 6 83 #define MAX_REG_SNDBC 7 84 #define MAX_REG_USBIRQ 13 85 #define MAX_REG_USBIEN 14 86 #define MAX_REG_USBCTL 15 87 #define MAX_REG_CPUCTL 16 88 #define MAX_REG_PINCTL 17 89 #define MAX_REG_REVISION 18 90 #define MAX_REG_HIRQ 25 91 #define MAX_REG_HIEN 26 92 #define MAX_REG_MODE 27 93 #define MAX_REG_PERADDR 28 94 #define MAX_REG_HCTL 29 95 #define MAX_REG_HXFR 30 96 #define MAX_REG_HRSL 31 97 98 #define MAX_USBIRQ_OSCOKIRQ 1 99 #define MAX_USBIRQ_NOVBUSIRQ 32 100 #define MAX_USBIRQ_VBUSIRQ 64 101 102 #define MAX_USBCTL_PWRDOWN 16 103 #define MAX_USBCTL_CHIPRES 32 104 105 #define MAX_CPUCTL_IE 1 106 107 #define MAX_PINCTL_POSINT_LOW 0 108 #define MAX_PINCTL_POSINT_HIGH 4 109 #define MAX_PINCTL_INTLEVEL_EDGE 0 110 #define MAX_PINCTL_INTLEVEL_LEVEL 8 111 #define MAX_PINCTL_FDUPSPI_HALF 0 112 #define MAX_PINCTL_FDUPSPI_FULL 16 113 114 #define MAX_HIRQ_BUSEVENTIRQ 1 115 #define MAX_HIRQ_RWUIRQ 2 116 #define MAX_HIRQ_RCVDAVIRQ 4 117 #define MAX_HIRQ_SNDBAVIRQ 8 118 #define MAX_HIRQ_SUSDNIRQ 16 119 #define MAX_HIRQ_CONDETIRQ 32 120 #define MAX_HIRQ_FRAMEIRQ 64 121 #define MAX_HIRQ_HXFRDNIRQ 128 122 123 #define MAX_HIEN_BUSEVENTIE 1 124 #define MAX_HIEN_CONDETIE 32 125 #define MAX_HIEN_FRAMEIE 64 126 127 #define MAX_MODE_PERIPHERAL 0 128 #define MAX_MODE_HOST 1 129 #define MAX_MODE_LOWSPEED 2 130 #define MAX_MODE_SOFKAENAB 8 131 #define MAX_MODE_SEPIRQ_OFF 0 132 #define MAX_MODE_SEPIRQ_ON 16 133 #define MAX_MODE_DMPULLDN 64 134 #define MAX_MODE_DPPULLDN 128 135 136 #define MAX_MODE_HOST_ENABLED (MAX_MODE_HOST | MAX_MODE_SEPIRQ_OFF | MAX_MODE_DMPULLDN | MAX_MODE_DPPULLDN) 137 #define MAX_MODE_HOST_ACTIVE (MAX_MODE_HOST_ENABLED | MAX_MODE_SOFKAENAB) 138 #define MAX_MODE_HOST_FULLSPEED MAX_MODE_HOST_ACTIVE 139 #define MAX_MODE_HOST_LOWSPEED (MAX_MODE_HOST_ACTIVE | MAX_MODE_LOWSPEED) 140 141 #define MAX_HCTL_BUSRST 1 142 #define MAX_HCTL_SAMPLEBUS 4 143 #define MAX_HCTL_RCVTOG0 16 144 #define MAX_HCTL_RCVTOG1 32 145 #define MAX_HCTL_SNDTOG0 64 146 #define MAX_HCTL_SNDTOG1 128 147 148 #define MAX_HXFR_SETUP 16 149 #define MAX_HXFR_OUTNIN 32 150 #define MAX_HXFR_HS 128 151 152 #define MAX_HRSL_JSTATUS 128 153 #define MAX_HRSL_KSTATUS 64 154 #define MAX_HRSL_SNDTOGRD 32 155 #define MAX_HRSL_RCVTOGRD 16 156 #define MAX_HRSL_HRSLT 15 157 158 #define max_reg(n) ((uint8_t) (n << 3)) 159 #define max_reg_read(n) (max_reg(n) | MAX_REG_READ) 160 #define max_reg_write(n) (max_reg(n) | MAX_REG_WRITE) 161 162 #define usb_descriptor_type(n) ((uint16_t) (n << 8)) 163 164 void spi_begin() 165 { 166 CLR(MAX_SS); 167 } 168 169 void spi_end() 170 { 171 SET(MAX_SS); 172 } 173 174 /** 175 * Send the given value via MOSI while receiving a value via MISO. 176 * This requires full-duplex SPI and will produce a status value for the first 177 * value sent (the command). 178 */ 179 uint8_t spi_sendrecv(uint8_t v) 180 { 181 uint8_t result = 0; 182 uint8_t mask; 183 184 for (mask = 0x80; mask; mask >>= 1) 185 { 186 if (v & mask) 187 { 188 #ifdef DEBUG 189 printf("1"); 190 #endif 191 SET(MAX_MOSI); 192 } 193 else 194 { 195 #ifdef DEBUG 196 printf("0"); 197 #endif 198 CLR(MAX_MOSI); 199 } 200 201 /* Wait for stable output signal. */ 202 203 SET(MAX_SCLK); 204 205 if (PIN(MAX_MISO)) 206 result |= mask; 207 208 CLR(MAX_SCLK); 209 } 210 211 #ifdef DEBUG 212 printf("\n"); 213 #endif 214 return result; 215 } 216 217 uint8_t max_read(uint8_t reg, uint8_t *status) 218 { 219 uint8_t result = 0, tmpstatus = 0; 220 221 tmpstatus = 0; 222 223 spi_begin(); 224 tmpstatus = spi_sendrecv(max_reg_read(reg)); 225 result = spi_sendrecv(0); 226 spi_end(); 227 228 if (status != NULL) 229 *status = tmpstatus; 230 231 return result; 232 } 233 234 uint8_t max_write(uint8_t reg, uint8_t value) 235 { 236 uint8_t status = 0; 237 238 spi_begin(); 239 status = spi_sendrecv(max_reg_write(reg)); 240 spi_sendrecv(value); 241 spi_end(); 242 243 return status; 244 } 245 246 /** 247 * Return whether data can be sent. 248 */ 249 bool max_can_send(uint8_t *status) 250 { 251 if (status == NULL) 252 return max_read(MAX_REG_HIRQ, NULL) & MAX_HIRQ_SNDBAVIRQ; 253 else 254 return *status & MAX_HIRQ_SNDBAVIRQ; 255 } 256 257 /** 258 * Set the sending data toggle. 259 */ 260 void max_set_send_toggle(bool toggle) 261 { 262 max_write(MAX_REG_HCTL, toggle ? MAX_HCTL_SNDTOG1 : MAX_HCTL_SNDTOG0); 263 } 264 265 /** 266 * Return the sending data toggle. 267 */ 268 bool max_get_send_toggle() 269 { 270 return (max_read(MAX_REG_HRSL, NULL) & MAX_HRSL_SNDTOGRD) != 0; 271 } 272 273 /** 274 * Set the receiving data toggle. 275 */ 276 void max_set_recv_toggle(bool toggle) 277 { 278 max_write(MAX_REG_HCTL, toggle ? MAX_HCTL_RCVTOG1 : MAX_HCTL_RCVTOG0); 279 } 280 281 /** 282 * Return the receiving data toggle. 283 */ 284 bool max_get_recv_toggle() 285 { 286 return (max_read(MAX_REG_HRSL, NULL) & MAX_HRSL_RCVTOGRD) != 0; 287 } 288 289 /** 290 * Wait for handshake/timeout after a transfer. 291 */ 292 uint8_t max_wait_transfer(uint8_t status) 293 { 294 while (!(status & MAX_HIRQ_HXFRDNIRQ)) 295 { 296 status = max_read(MAX_REG_HIRQ, NULL); 297 } 298 299 max_write(MAX_REG_HIRQ, MAX_HIRQ_HXFRDNIRQ); 300 return max_read(MAX_REG_HIRQ, NULL); 301 } 302 303 /** 304 * Write the given data to the FIFO. 305 */ 306 void max_write_fifo(uint8_t endpoint, uint8_t *data, uint8_t len) 307 { 308 uint8_t count; 309 310 for (count = 0; count < len; count++) 311 { 312 max_write(endpoint ? MAX_REG_SNDFIFO : MAX_REG_SUDFIFO, data[count]); 313 } 314 315 if (endpoint) 316 max_write(MAX_REG_SNDBC, len); 317 } 318 319 /** 320 * Read the data from the FIFO. 321 */ 322 void max_read_fifo(uint8_t **data, uint8_t *len, uint8_t *datalimit) 323 { 324 uint8_t count, received = max_read(MAX_REG_RCVBC, NULL); 325 printf("Received %d bytes.\n", received); 326 327 *len += received; 328 329 for (count = 0; (count < received) && (*data < datalimit); count++) 330 { 331 *((*data)++) = max_read(MAX_REG_RCVFIFO, NULL); 332 } 333 } 334 335 /** 336 * Send a request to the given endpoint, using the supplied data payload with 337 * the given length, indicating the preserved toggle state of the endpoint 338 * (which will be updated). 339 */ 340 uint8_t max_send(uint8_t endpoint, uint8_t *data, uint8_t len, bool *toggle) 341 { 342 uint8_t status, hrsl = 0; 343 344 max_write_fifo(endpoint, data, len); 345 346 max_set_send_toggle(*toggle); 347 348 /* Initiate the transfer. */ 349 350 do 351 { 352 status = max_write(MAX_REG_HXFR, endpoint | MAX_HXFR_OUTNIN); 353 status = max_wait_transfer(status); 354 355 /* Test for usable data. */ 356 357 if (status & MAX_HIRQ_SNDBAVIRQ) 358 { 359 hrsl = max_read(MAX_REG_HRSL, &status); 360 361 if (!(hrsl & MAX_HRSL_HRSLT)) 362 break; 363 } 364 } 365 while (true); 366 367 *toggle = max_get_send_toggle(); 368 369 return status; 370 } 371 372 /** 373 * Make a request for data from the given endpoint, collecting it in the 374 * supplied buffer with the given length, indicating the preserved toggle state 375 * of the endpoint (which will be updated). The length will be updated to 376 * indicate the total length of the received data. 377 */ 378 bool max_recv(uint8_t endpoint, uint8_t *data, uint8_t *len, bool *toggle) 379 { 380 uint8_t *datalimit = data + *len; 381 uint8_t status, hrsl = 0; 382 uint16_t attempt = 1024; 383 384 max_set_recv_toggle(*toggle); 385 386 /* Initiate the transfer. */ 387 388 do 389 { 390 status = max_write(MAX_REG_HXFR, endpoint); 391 status = max_wait_transfer(status); 392 393 /* Test for usable data. */ 394 395 hrsl = max_read(MAX_REG_HRSL, &status); 396 397 attempt--; 398 } 399 while ((hrsl & MAX_HRSL_HRSLT) && attempt); 400 401 if (!attempt) 402 { 403 printf("HRSL is %x\n", hrsl); 404 return false; 405 } 406 407 while (status & MAX_HIRQ_RCVDAVIRQ) 408 { 409 max_read_fifo(&data, len, datalimit); 410 411 /* Indicate that all data has been read. */ 412 413 max_write(MAX_REG_HIRQ, MAX_HIRQ_RCVDAVIRQ); 414 status = max_read(MAX_REG_HIRQ, NULL); 415 } 416 417 *toggle = max_get_recv_toggle(); 418 419 return true; 420 } 421 422 /** 423 * Send a control request consisting of the given setup data. 424 */ 425 uint8_t max_control(uint8_t *setup) 426 { 427 uint8_t status, hrsl; 428 429 max_write_fifo(0, setup, 8); 430 431 /* Initiate the transfer. */ 432 433 do 434 { 435 status = max_write(MAX_REG_HXFR, MAX_HXFR_SETUP); 436 status = max_wait_transfer(status); 437 hrsl = max_read(MAX_REG_HRSL, &status); 438 } 439 while (hrsl & MAX_HRSL_HRSLT); 440 441 return status; 442 } 443 444 bool max_control_input(uint8_t *data, uint8_t *len, max_device *device) 445 { 446 device->in_toggle = true; 447 return max_recv(0, data, len, &device->in_toggle); 448 } 449 450 /** 451 * Perform a status transaction as part of a larger control transaction. 452 * The out parameter is used to indicate the kind of status transfer to be 453 * performed and should be the inverse of the control transfer direction. 454 */ 455 uint8_t max_control_status(bool out) 456 { 457 uint8_t status, hrsl; 458 459 do 460 { 461 status = max_write(MAX_REG_HXFR, MAX_HXFR_HS | (out ? MAX_HXFR_OUTNIN : 0)); 462 status = max_wait_transfer(status); 463 hrsl = max_read(MAX_REG_HRSL, &status); 464 } 465 while (hrsl & MAX_HRSL_HRSLT); 466 467 return status; 468 } 469 470 void chipreset() 471 { 472 printf("Resetting...\n"); 473 max_write(MAX_REG_USBCTL, MAX_USBCTL_CHIPRES); 474 475 printf("Clearing the reset...\n"); 476 max_write(MAX_REG_USBCTL, 0); 477 } 478 479 uint8_t check() 480 { 481 uint8_t oscillator; 482 483 oscillator = max_read(MAX_REG_USBIRQ, NULL); 484 485 return (oscillator & ~(MAX_USBIRQ_NOVBUSIRQ | MAX_USBIRQ_VBUSIRQ)) == MAX_USBIRQ_OSCOKIRQ; 486 } 487 488 uint8_t wait() 489 { 490 uint16_t timeout = 1024; 491 492 /* Wait for the oscillator before performing USB activity. */ 493 494 printf("Waiting...\n"); 495 496 while ((timeout > 0) && (!check())) 497 { 498 timeout--; 499 } 500 501 printf("Iterations remaining: %d\n", timeout); 502 503 return timeout; 504 } 505 506 /** 507 * Return whether the bus is ready to be sampled. The application note claims 508 * that the SAMPLEBUS bit is cleared, but this does not seem to be the case. 509 */ 510 uint8_t samplebusready() 511 { 512 uint8_t result; 513 514 result = max_read(MAX_REG_HCTL, NULL); 515 return result & MAX_HCTL_SAMPLEBUS; 516 } 517 518 void samplebus() 519 { 520 max_write(MAX_REG_HCTL, MAX_HCTL_SAMPLEBUS); 521 while (!samplebusready()); 522 } 523 524 /** 525 * Handle the connection or disconnection of a device, returning true if the 526 * device is now connected or false otherwise. If the device is connected, a 527 * bus reset is performed. 528 */ 529 bool devicechanged() 530 { 531 uint8_t hrsl, mode; 532 533 hrsl = max_read(MAX_REG_HRSL, NULL); 534 mode = max_read(MAX_REG_MODE, NULL); 535 536 if ((hrsl & MAX_HRSL_JSTATUS) && (hrsl & MAX_HRSL_KSTATUS)) 537 { 538 printf("Bad device status.\n"); 539 } 540 else if (!(hrsl & MAX_HRSL_JSTATUS) && !(hrsl & MAX_HRSL_KSTATUS)) 541 { 542 printf("Device disconnected.\n"); 543 max_write(MAX_REG_MODE, MAX_MODE_HOST_ENABLED); 544 } 545 else 546 { 547 printf("Device connected.\n"); 548 549 /* Low speed device when J and lowspeed have the same level. 550 Since J and K should have opposing levels, K can be tested when 551 lowspeed is low. */ 552 553 if (((hrsl & MAX_HRSL_JSTATUS) && (mode & MAX_MODE_LOWSPEED)) || 554 ((hrsl & MAX_HRSL_KSTATUS) && !(mode & MAX_MODE_LOWSPEED))) 555 { 556 printf("Device is low speed.\n"); 557 if (max_read(MAX_REG_MODE, NULL) != MAX_MODE_HOST_LOWSPEED) 558 max_write(MAX_REG_MODE, MAX_MODE_HOST_LOWSPEED); 559 } 560 else 561 { 562 printf("Device is full speed.\n"); 563 if (max_read(MAX_REG_MODE, NULL) != MAX_MODE_HOST_FULLSPEED) 564 max_write(MAX_REG_MODE, MAX_MODE_HOST_FULLSPEED); 565 } 566 567 /* Reset the device. */ 568 569 max_write(MAX_REG_HCTL, MAX_HCTL_BUSRST); 570 return true; 571 } 572 573 return false; 574 } 575 576 /** 577 * Initialise a USB control request setup payload. 578 */ 579 void setup_packet(uint8_t *setup, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, uint16_t length) 580 { 581 setup[0] = request_type; 582 setup[1] = request; 583 setup[2] = value & 0xff; 584 setup[3] = value >> 8; 585 setup[4] = index & 0xff; 586 setup[5] = index >> 8; 587 setup[6] = length & 0xff; 588 setup[7] = length >> 8; 589 } 590 591 uint8_t usb_descriptor_size(uint8_t type) 592 { 593 switch (type) 594 { 595 case USB_DT_DEVICE: return USB_DT_DEVICE_SIZE; 596 case USB_DT_CONFIG: return USB_DT_CONFIG_SIZE; 597 case USB_DT_STRING: return sizeof(struct usb_string_descriptor); 598 case USB_DT_INTERFACE: return USB_DT_INTERFACE_SIZE; 599 case USB_DT_ENDPOINT: return USB_DT_ENDPOINT_SIZE; 600 default: return 0; 601 } 602 } 603 604 /** 605 * Get a descriptor from the device having the given type, value and index. 606 * The descriptor buffer is static and must be copied if it is to be preserved. 607 */ 608 uint8_t *max_get_descriptor(max_device *device, uint8_t type, uint8_t value, uint8_t index) 609 { 610 static uint8_t data[64]; 611 uint8_t len = 64, setup[8]; 612 613 printf("Sending descriptor request to address %d, endpoint 0...\n", device->address); 614 615 max_write(MAX_REG_PERADDR, device->address); 616 617 setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(type) | value, index, 64); 618 max_control(setup); 619 if (!max_control_input(data, &len, device)) 620 { 621 printf("Failed.\n"); 622 return NULL; 623 } 624 max_control_status(true); 625 626 if (len >= usb_descriptor_size(type)) 627 return data; 628 else 629 { 630 printf("Expected %d but received %d.\n", usb_descriptor_size(type), len); 631 return NULL; 632 } 633 } 634 635 /** 636 * Perform initialisation on a device, obtaining the device details and storing 637 * this information in the device structure, returning true if successful and 638 * false otherwise. 639 */ 640 bool max_init_device(max_device *device) 641 { 642 struct usb_device_descriptor *desc; 643 644 printf("Sending control request to address 0, endpoint 0...\n"); 645 646 device->address = 0; 647 648 /* Send a "get descriptor" request for the device descriptor. */ 649 650 desc = (struct usb_device_descriptor *) max_get_descriptor(device, USB_DT_DEVICE, 0, 0); 651 652 if (desc != NULL) 653 { 654 memcpy(&device->desc, desc, sizeof(struct usb_device_descriptor)); 655 device->desc.bcdUSB = le16toh(device->desc.bcdUSB); 656 device->desc.idVendor = le16toh(device->desc.idVendor); 657 device->desc.idProduct = le16toh(device->desc.idProduct); 658 device->desc.bcdDevice = le16toh(device->desc.bcdDevice); 659 660 printf("bLength: %d\n", device->desc.bLength); 661 printf("bDescriptorType: %d\n", device->desc.bDescriptorType); 662 printf("bcdUSB: %04x\n", device->desc.bcdUSB); 663 printf("bDeviceClass: %d\n", device->desc.bDeviceClass); 664 printf("bDeviceSubClass: %d\n", device->desc.bDeviceSubClass); 665 printf("bDeviceProtocol: %d\n", device->desc.bDeviceProtocol); 666 printf("bMaxPacketSize0: %d\n", device->desc.bMaxPacketSize0); 667 printf("idVendor: %04x\n", device->desc.idVendor); 668 printf("idProduct: %04x\n", device->desc.idProduct); 669 printf("bcdDevice: %04x\n", device->desc.bcdDevice); 670 printf("iManufacturer: %d\n", device->desc.iManufacturer); 671 printf("iProduct: %d\n", device->desc.iProduct); 672 printf("iSerialNumber: %d\n", device->desc.iSerialNumber); 673 printf("bNumConfigurations: %d\n", device->desc.bNumConfigurations); 674 675 device->max_packet_size = device->desc.bMaxPacketSize0; 676 return true; 677 } 678 679 return false; 680 } 681 682 /** 683 * Assign a new address to the given device. 684 */ 685 void max_set_address(max_device *device) 686 { 687 uint8_t setup[8]; 688 689 max_write(MAX_REG_PERADDR, 0); 690 691 device->address = next_address++; 692 693 printf("Setting device address to %d...\n", device->address); 694 695 setup_packet(setup, USB_ENDPOINT_OUT, USB_REQ_SET_ADDRESS, device->address, 0, 0); 696 max_control(setup); 697 max_control_status(false); 698 } 699 700 void usb_show_languages(uint8_t *data) 701 { 702 struct usb_string_descriptor *desc = (struct usb_string_descriptor *) data; 703 uint16_t *lang; 704 705 if (data == NULL) 706 return; 707 708 printf("bLength: %d\n", desc->bLength); 709 printf("bDescriptorType: %d\n", desc->bDescriptorType); 710 711 for (lang = desc->wData; lang < (uint16_t *) (data + desc->bLength); lang++) 712 { 713 printf("wLangId: %04x\n", le16toh(*lang)); 714 } 715 } 716 717 void usb_show_configuration(uint8_t *data) 718 { 719 struct usb_config_descriptor *conf = (struct usb_config_descriptor *) data; 720 uint8_t *current, *last, total = le16toh(conf->wTotalLength); 721 struct usb_descriptor_header *desc; 722 struct usb_interface_descriptor *intf; 723 struct usb_endpoint_descriptor *endp; 724 725 if (data == NULL) 726 return; 727 728 printf("bLength: %d\n", conf->bLength); 729 printf("bDescriptorType: %d\n", conf->bDescriptorType); 730 printf("wTotalLength: %d\n", total); 731 printf("bNumInterfaces: %d\n", conf->bNumInterfaces); 732 printf("bConfigurationValue: %d\n", conf->bConfigurationValue); 733 printf("iConfiguration: %d\n", conf->iConfiguration); 734 printf("bmAttributes: %x\n", conf->bmAttributes); 735 printf("MaxPower: %d\n", conf->MaxPower); 736 737 current = ((uint8_t *) conf) + conf->bLength; 738 last = ((uint8_t *) conf) + total; 739 740 while (current < last) 741 { 742 desc = (struct usb_descriptor_header *) current; 743 printf("bLength: %d\n", desc->bLength); 744 printf("bDescriptorType: %d\n", desc->bDescriptorType); 745 746 switch (desc->bDescriptorType) 747 { 748 case USB_DT_INTERFACE: 749 intf = (struct usb_interface_descriptor *) current; 750 printf("bInterfaceNumber: %d\n", intf->bInterfaceNumber); 751 printf("bAlternateSetting: %d\n", intf->bAlternateSetting); 752 printf("bNumEndpoints: %d\n", intf->bNumEndpoints); 753 printf("bInterfaceClass: %x\n", intf->bInterfaceClass); 754 printf("bInterfaceSubClass: %x\n", intf->bInterfaceSubClass); 755 printf("bInterfaceProtocol: %x\n", intf->bInterfaceProtocol); 756 printf("iInterface: %d\n", intf->iInterface); 757 break; 758 759 case USB_DT_ENDPOINT: 760 endp = (struct usb_endpoint_descriptor *) current; 761 printf("bEndpointAddress: %d\n", endp->bEndpointAddress); 762 printf("bmAttributes: %x\n", endp->bmAttributes); 763 printf("wMaxPacketSize: %d\n", le16toh(endp->wMaxPacketSize)); 764 printf("bInterval: %d\n", endp->bInterval); 765 break; 766 767 default: 768 break; 769 } 770 771 if (desc->bLength) 772 current += desc->bLength; 773 else 774 { 775 printf("END (zero length record)\n"); 776 break; 777 } 778 } 779 } 780 781 /** 782 * Handle termination of the process. 783 */ 784 void shutdown(int signum) 785 { 786 printf("Closing...\n"); 787 ubb_close(0); 788 exit(1); 789 } 790 791 int main(int argc, char *argv[]) 792 { 793 uint8_t status = 0, revision = 0; 794 uint16_t count; 795 bool bus_event, data_event, suspended_event, connection_event, frame_event; 796 max_device device; 797 max_devstate devstate = MAX_DEVSTATE_INIT; 798 799 device.in_toggle = false; 800 device.out_toggle = false; 801 device.max_packet_size = 8; 802 803 signal(SIGINT, &shutdown); 804 805 if (ubb_open(0) < 0) { 806 perror("ubb_open"); 807 return 1; 808 } 809 810 ubb_power(1); 811 printf("Power on.\n"); 812 813 OUT(MAX_SS); 814 OUT(MAX_MOSI); 815 OUT(MAX_SCLK); 816 OUT(MAX_RESET); 817 IN(MAX_INT); 818 IN(MAX_MISO); 819 820 /* Initialise SPI. */ 821 /* Set SS# to 1. */ 822 823 SET(MAX_SS); 824 CLR(MAX_MOSI); 825 CLR(MAX_SCLK); 826 SET(MAX_RESET); 827 828 /* Initialise the MAX3421E. */ 829 830 /* Set full-duplex, interrupt signalling. */ 831 832 printf("Setting pin control...\n"); 833 max_write(MAX_REG_PINCTL, MAX_PINCTL_INTLEVEL_LEVEL | MAX_PINCTL_FDUPSPI_FULL); 834 835 chipreset(); 836 printf("Ready? %d\n", wait()); 837 838 /* Check various registers. */ 839 840 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 841 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 842 843 /* Set host mode. */ 844 845 printf("Setting mode...\n"); 846 status = max_write(MAX_REG_MODE, MAX_MODE_HOST_ENABLED); 847 848 printf("Setting INT signalling...\n"); 849 status = max_write(MAX_REG_CPUCTL, MAX_CPUCTL_IE); 850 851 printf("Setting event signalling...\n"); 852 status = max_write(MAX_REG_HIEN, MAX_HIEN_CONDETIE | MAX_HIEN_FRAMEIE | MAX_HIEN_BUSEVENTIE); 853 854 /* Check various registers. */ 855 856 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 857 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 858 printf("IE: %x\n", max_read(MAX_REG_HIEN, &status)); 859 printf("CPU: %x\n", max_read(MAX_REG_CPUCTL, &status)); 860 printf("Pin: %x\n", max_read(MAX_REG_PINCTL, &status)); 861 printf("USBIRQ: %x\n", max_read(MAX_REG_USBIRQ, &status)); 862 printf("USBIE: %x\n", max_read(MAX_REG_USBIEN, &status)); 863 864 /* Read from the REVISION register. */ 865 866 printf("Reading...\n"); 867 revision = max_read(MAX_REG_REVISION, &status); 868 printf("Revision = %x\n", revision); 869 870 /* Sample the bus for any devices that are already connected. */ 871 872 samplebus(); 873 874 if (devicechanged()) 875 { 876 devstate = MAX_DEVSTATE_CONNECTED; 877 } 878 879 /* Enter the main processing loop. */ 880 881 for (count = 0; count <= 65535; count++) 882 { 883 if (!PIN(MAX_INT)) 884 { 885 /* Obtain interrupt conditions. */ 886 887 status = max_read(MAX_REG_HIRQ, NULL); 888 889 bus_event = status & MAX_HIRQ_BUSEVENTIRQ; 890 data_event = status & MAX_HIRQ_RCVDAVIRQ; 891 suspended_event = status & MAX_HIRQ_SUSDNIRQ; 892 connection_event = status & MAX_HIRQ_CONDETIRQ; 893 frame_event = status & MAX_HIRQ_FRAMEIRQ; 894 895 #ifdef DEBUG 896 if (bus_event) printf("Bus "); 897 if (data_event) printf("Data "); 898 if (suspended_event) printf("Suspended "); 899 if (connection_event) printf("Connection "); 900 if (frame_event) printf("Frame "); 901 printf("\n"); 902 #endif 903 904 /* Acknowledge the interrupts. */ 905 906 max_write(MAX_REG_HIRQ, status); 907 908 /* Detect device connection/disconnection. */ 909 910 if ((devstate == MAX_DEVSTATE_INIT) && connection_event && devicechanged()) 911 { 912 devstate = MAX_DEVSTATE_CONNECTED; 913 printf("CONNECTED\n"); 914 } 915 916 /* Handle device reset initiation. */ 917 918 else if ((devstate == MAX_DEVSTATE_CONNECTED) && bus_event) 919 { 920 max_write(MAX_REG_MODE, max_read(MAX_REG_MODE, NULL)); 921 devstate = MAX_DEVSTATE_RESET; 922 printf("RESET\n"); 923 } 924 925 /* Handle device reset completion, getting device details and 926 initiating a second reset. */ 927 928 else if ((devstate == MAX_DEVSTATE_RESET) && frame_event && max_can_send(&status)) 929 { 930 if (!max_init_device(&device)) 931 printf("FAILED: RESET -> READY\n"); 932 else 933 { 934 max_set_address(&device); 935 usb_show_languages(max_get_descriptor(&device, USB_DT_STRING, 0, 0)); 936 usb_show_configuration(max_get_descriptor(&device, USB_DT_CONFIG, 0, 0)); 937 devstate = MAX_DEVSTATE_READY; 938 printf("READY\n"); 939 } 940 } 941 942 /* Handle device disconnection. */ 943 944 else if ((devstate != MAX_DEVSTATE_INIT) && connection_event && !devicechanged()) 945 { 946 devstate = MAX_DEVSTATE_INIT; 947 printf("INIT\n"); 948 } 949 } 950 } 951 952 printf("Closing...\n"); 953 ubb_close(0); 954 955 return 0; 956 }