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