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; 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 max_write(MAX_REG_MODE, MAX_MODE_HOST_LOWSPEED); 558 } 559 else 560 { 561 printf("Device is full speed.\n"); 562 max_write(MAX_REG_MODE, MAX_MODE_HOST_FULLSPEED); 563 } 564 565 /* Reset the device. */ 566 567 max_write(MAX_REG_HCTL, MAX_HCTL_BUSRST); 568 return true; 569 } 570 571 return false; 572 } 573 574 /** 575 * Initialise a USB control request setup payload. 576 */ 577 void setup_packet(uint8_t *setup, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, uint16_t length) 578 { 579 setup[0] = request_type; 580 setup[1] = request; 581 setup[2] = value & 0xff; 582 setup[3] = value >> 8; 583 setup[4] = index & 0xff; 584 setup[5] = index >> 8; 585 setup[6] = length & 0xff; 586 setup[7] = length >> 8; 587 } 588 589 uint8_t usb_descriptor_size(uint8_t type) 590 { 591 switch (type) 592 { 593 case USB_DT_DEVICE: return USB_DT_DEVICE_SIZE; 594 case USB_DT_CONFIG: return USB_DT_CONFIG_SIZE; 595 case USB_DT_STRING: return sizeof(struct usb_string_descriptor); 596 case USB_DT_INTERFACE: return USB_DT_INTERFACE_SIZE; 597 case USB_DT_ENDPOINT: return USB_DT_ENDPOINT_SIZE; 598 default: return 0; 599 } 600 } 601 602 /** 603 * Get a descriptor from the device having the given type, value and index. 604 * The descriptor buffer is static and must be copied if it is to be preserved. 605 */ 606 uint8_t *max_get_descriptor(max_device *device, uint8_t type, uint8_t value, uint8_t index) 607 { 608 static uint8_t data[64]; 609 uint8_t len = 64, setup[8]; 610 611 printf("Sending descriptor request to address %d, endpoint 0...\n", device->address); 612 613 max_write(MAX_REG_PERADDR, device->address); 614 615 setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(type) | value, index, 64); 616 max_control(setup); 617 if (!max_control_input(data, &len, device)) 618 { 619 printf("Failed.\n"); 620 return NULL; 621 } 622 max_control_status(true); 623 624 if (len >= usb_descriptor_size(type)) 625 return data; 626 else 627 { 628 printf("Expected %d but received %d.\n", usb_descriptor_size(type), len); 629 return NULL; 630 } 631 } 632 633 /** 634 * Perform initialisation on a device, obtaining the device details and storing 635 * this information in the device structure, returning true if successful and 636 * false otherwise. 637 */ 638 bool max_init_device(max_device *device) 639 { 640 struct usb_device_descriptor *desc; 641 642 printf("Sending control request to address 0, endpoint 0...\n"); 643 644 device->address = 0; 645 646 /* Send a "get descriptor" request for the device descriptor. */ 647 648 desc = (struct usb_device_descriptor *) max_get_descriptor(device, USB_DT_DEVICE, 0, 0); 649 650 if (desc != NULL) 651 { 652 memcpy(&device->desc, desc, sizeof(struct usb_device_descriptor)); 653 device->desc.bcdUSB = le16toh(device->desc.bcdUSB); 654 device->desc.idVendor = le16toh(device->desc.idVendor); 655 device->desc.idProduct = le16toh(device->desc.idProduct); 656 device->desc.bcdDevice = le16toh(device->desc.bcdDevice); 657 658 printf("bLength: %d\n", device->desc.bLength); 659 printf("bDescriptorType: %d\n", device->desc.bDescriptorType); 660 printf("bcdUSB: %04x\n", device->desc.bcdUSB); 661 printf("bDeviceClass: %d\n", device->desc.bDeviceClass); 662 printf("bDeviceSubClass: %d\n", device->desc.bDeviceSubClass); 663 printf("bDeviceProtocol: %d\n", device->desc.bDeviceProtocol); 664 printf("bMaxPacketSize0: %d\n", device->desc.bMaxPacketSize0); 665 printf("idVendor: %04x\n", device->desc.idVendor); 666 printf("idProduct: %04x\n", device->desc.idProduct); 667 printf("bcdDevice: %04x\n", device->desc.bcdDevice); 668 printf("iManufacturer: %d\n", device->desc.iManufacturer); 669 printf("iProduct: %d\n", device->desc.iProduct); 670 printf("iSerialNumber: %d\n", device->desc.iSerialNumber); 671 printf("bNumConfigurations: %d\n", device->desc.bNumConfigurations); 672 return true; 673 } 674 675 return false; 676 } 677 678 /** 679 * Assign a new address to the given device. 680 */ 681 void max_set_address(max_device *device) 682 { 683 uint8_t setup[8]; 684 685 max_write(MAX_REG_PERADDR, 0); 686 687 device->address = next_address++; 688 689 printf("Setting device address to %d...\n", device->address); 690 691 setup_packet(setup, USB_ENDPOINT_OUT, USB_REQ_SET_ADDRESS, device->address, 0, 0); 692 max_control(setup); 693 max_control_status(false); 694 } 695 696 void usb_show_languages(uint8_t *data) 697 { 698 struct usb_string_descriptor *desc = (struct usb_string_descriptor *) data; 699 uint16_t *lang; 700 701 if (data == NULL) 702 return; 703 704 printf("bLength: %d\n", desc->bLength); 705 printf("bDescriptorType: %d\n", desc->bDescriptorType); 706 707 for (lang = desc->wData; lang < (uint16_t *) (data + desc->bLength); lang++) 708 { 709 printf("wLangId: %04x\n", le16toh(*lang)); 710 } 711 } 712 713 void usb_show_configuration(uint8_t *data) 714 { 715 struct usb_config_descriptor *conf = (struct usb_config_descriptor *) data; 716 uint8_t *current, *last, total = le16toh(conf->wTotalLength); 717 struct usb_descriptor_header *desc; 718 struct usb_interface_descriptor *intf; 719 struct usb_endpoint_descriptor *endp; 720 721 if (data == NULL) 722 return; 723 724 printf("bLength: %d\n", conf->bLength); 725 printf("bDescriptorType: %d\n", conf->bDescriptorType); 726 printf("wTotalLength: %d\n", total); 727 printf("bNumInterfaces: %d\n", conf->bNumInterfaces); 728 printf("bConfigurationValue: %d\n", conf->bConfigurationValue); 729 printf("iConfiguration: %d\n", conf->iConfiguration); 730 printf("bmAttributes: %x\n", conf->bmAttributes); 731 printf("MaxPower: %d\n", conf->MaxPower); 732 733 current = ((uint8_t *) conf) + conf->bLength; 734 last = ((uint8_t *) conf) + total; 735 736 while (current < last) 737 { 738 desc = (struct usb_descriptor_header *) current; 739 printf("bLength: %d\n", desc->bLength); 740 printf("bDescriptorType: %d\n", desc->bDescriptorType); 741 742 switch (desc->bDescriptorType) 743 { 744 case USB_DT_INTERFACE: 745 intf = (struct usb_interface_descriptor *) current; 746 printf("bInterfaceNumber: %d\n", intf->bInterfaceNumber); 747 printf("bAlternateSetting: %d\n", intf->bAlternateSetting); 748 printf("bNumEndpoints: %d\n", intf->bNumEndpoints); 749 printf("bInterfaceClass: %x\n", intf->bInterfaceClass); 750 printf("bInterfaceSubClass: %x\n", intf->bInterfaceSubClass); 751 printf("bInterfaceProtocol: %x\n", intf->bInterfaceProtocol); 752 printf("iInterface: %d\n", intf->iInterface); 753 break; 754 755 case USB_DT_ENDPOINT: 756 endp = (struct usb_endpoint_descriptor *) current; 757 printf("bEndpointAddress: %d\n", endp->bEndpointAddress); 758 printf("bmAttributes: %x\n", endp->bmAttributes); 759 printf("wMaxPacketSize: %d\n", le16toh(endp->wMaxPacketSize)); 760 printf("bInterval: %d\n", endp->bInterval); 761 break; 762 763 default: 764 break; 765 } 766 767 if (desc->bLength) 768 current += desc->bLength; 769 else 770 { 771 printf("END (zero length record)\n"); 772 break; 773 } 774 } 775 } 776 777 /** 778 * Handle termination of the process. 779 */ 780 void shutdown(int signum) 781 { 782 printf("Closing...\n"); 783 ubb_close(0); 784 exit(1); 785 } 786 787 int main(int argc, char *argv[]) 788 { 789 uint8_t status = 0, revision = 0; 790 uint16_t count; 791 bool bus_event, data_event, suspended_event, connection_event, frame_event; 792 max_device device; 793 max_devstate devstate = MAX_DEVSTATE_INIT; 794 795 device.in_toggle = false; 796 device.out_toggle = false; 797 798 signal(SIGINT, &shutdown); 799 800 if (ubb_open(0) < 0) { 801 perror("ubb_open"); 802 return 1; 803 } 804 805 ubb_power(1); 806 printf("Power on.\n"); 807 808 OUT(MAX_SS); 809 OUT(MAX_MOSI); 810 OUT(MAX_SCLK); 811 OUT(MAX_RESET); 812 IN(MAX_INT); 813 IN(MAX_MISO); 814 815 /* Initialise SPI. */ 816 /* Set SS# to 1. */ 817 818 SET(MAX_SS); 819 CLR(MAX_MOSI); 820 CLR(MAX_SCLK); 821 SET(MAX_RESET); 822 823 /* Initialise the MAX3421E. */ 824 825 /* Set full-duplex, interrupt signalling. */ 826 827 printf("Setting pin control...\n"); 828 max_write(MAX_REG_PINCTL, MAX_PINCTL_INTLEVEL_LEVEL | MAX_PINCTL_FDUPSPI_FULL); 829 830 chipreset(); 831 printf("Ready? %d\n", wait()); 832 833 /* Check various registers. */ 834 835 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 836 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 837 838 /* Set host mode. */ 839 840 printf("Setting mode...\n"); 841 status = max_write(MAX_REG_MODE, MAX_MODE_HOST_ENABLED); 842 843 printf("Setting INT signalling...\n"); 844 status = max_write(MAX_REG_CPUCTL, MAX_CPUCTL_IE); 845 846 printf("Setting event signalling...\n"); 847 status = max_write(MAX_REG_HIEN, MAX_HIEN_CONDETIE | MAX_HIEN_FRAMEIE | MAX_HIEN_BUSEVENTIE); 848 849 /* Check various registers. */ 850 851 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 852 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 853 printf("IE: %x\n", max_read(MAX_REG_HIEN, &status)); 854 printf("CPU: %x\n", max_read(MAX_REG_CPUCTL, &status)); 855 printf("Pin: %x\n", max_read(MAX_REG_PINCTL, &status)); 856 printf("USBIRQ: %x\n", max_read(MAX_REG_USBIRQ, &status)); 857 printf("USBIE: %x\n", max_read(MAX_REG_USBIEN, &status)); 858 859 /* Read from the REVISION register. */ 860 861 printf("Reading...\n"); 862 revision = max_read(MAX_REG_REVISION, &status); 863 printf("Revision = %x\n", revision); 864 865 /* Sample the bus for any devices that are already connected. */ 866 867 samplebus(); 868 869 if (devicechanged()) 870 { 871 devstate = MAX_DEVSTATE_CONNECTED; 872 } 873 874 /* Enter the main processing loop. */ 875 876 for (count = 0; count <= 65535; count++) 877 { 878 if (!PIN(MAX_INT)) 879 { 880 /* Obtain interrupt conditions. */ 881 882 status = max_read(MAX_REG_HIRQ, NULL); 883 884 bus_event = status & MAX_HIRQ_BUSEVENTIRQ; 885 data_event = status & MAX_HIRQ_RCVDAVIRQ; 886 suspended_event = status & MAX_HIRQ_SUSDNIRQ; 887 connection_event = status & MAX_HIRQ_CONDETIRQ; 888 frame_event = status & MAX_HIRQ_FRAMEIRQ; 889 890 #ifdef DEBUG 891 if (bus_event) printf("Bus "); 892 if (data_event) printf("Data "); 893 if (suspended_event) printf("Suspended "); 894 if (connection_event) printf("Connection "); 895 if (frame_event) printf("Frame "); 896 printf("\n"); 897 #endif 898 899 /* Acknowledge the interrupts. */ 900 901 max_write(MAX_REG_HIRQ, status); 902 903 /* Detect device connection/disconnection. */ 904 905 if ((devstate == MAX_DEVSTATE_INIT) && connection_event && devicechanged()) 906 { 907 devstate = MAX_DEVSTATE_CONNECTED; 908 printf("CONNECTED\n"); 909 } 910 911 /* Handle device reset initiation. */ 912 913 else if ((devstate == MAX_DEVSTATE_CONNECTED) && bus_event) 914 { 915 max_write(MAX_REG_MODE, max_read(MAX_REG_MODE, NULL)); 916 devstate = MAX_DEVSTATE_RESET; 917 printf("RESET\n"); 918 } 919 920 /* Handle device reset completion, getting device details and 921 initiating a second reset. */ 922 923 else if ((devstate == MAX_DEVSTATE_RESET) && frame_event && max_can_send(&status)) 924 { 925 if (!max_init_device(&device)) 926 printf("FAILED: RESET -> READY\n"); 927 else 928 { 929 max_set_address(&device); 930 usb_show_languages(max_get_descriptor(&device, USB_DT_STRING, 0, 0)); 931 usb_show_configuration(max_get_descriptor(&device, USB_DT_CONFIG, 0, 0)); 932 devstate = MAX_DEVSTATE_READY; 933 printf("READY\n"); 934 } 935 } 936 937 /* Handle device disconnection. */ 938 939 else if ((devstate != MAX_DEVSTATE_INIT) && connection_event) 940 { 941 devstate = MAX_DEVSTATE_INIT; 942 printf("INIT\n"); 943 } 944 } 945 } 946 947 printf("Closing...\n"); 948 ubb_close(0); 949 950 return 0; 951 }