1 /* 2 * Ben NanoNote and Arduino USB Host shield communication. 3 * 4 * Copyright (C) 2013 Paul Boddie 5 * 6 * SPI functions derived from those in lib/atben.c by Werner Almesberger: 7 * 8 * Copyright (C) 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, 37 MAX_DEVSTATE_START, 38 MAX_DEVSTATE_RESET, 39 MAX_DEVSTATE_INSPECTED, 40 MAX_DEVSTATE_RESET_AGAIN, 41 MAX_DEVSTATE_ADDRESSING, 42 MAX_DEVSTATE_READY 43 } max_devstate; 44 45 /* Device details. */ 46 47 typedef struct 48 { 49 bool in_toggle, out_toggle; 50 struct usb_device_descriptor desc; 51 uint8_t address, max_packet_size; 52 } max_device; 53 54 static uint16_t next_address = 1; 55 56 /* Pin assignments: 57 * 58 * Sniffer UBB Shield 59 * ------- ---- ------ 60 * DAT2 DAT2 9 (INT) 61 * CD DAT3 10 (SS) 62 * CMD CMD 7 (RESET) 63 * VCC VDD VIN 64 * CLK CLK 13 (SCLK) 65 * GND GND GND 66 * DAT0 DAT0 11 (MOSI) 67 * DAT1 DAT1 12 (MISO) 68 * 8 (GPX) (not assigned) 69 */ 70 71 #define MAX_RESET UBB_CMD 72 #define MAX_SCLK UBB_CLK 73 #define MAX_MOSI UBB_DAT0 74 #define MAX_MISO UBB_DAT1 75 #define MAX_INT UBB_DAT2 76 #define MAX_SS UBB_DAT3 77 78 /* MAX3421E definitions. */ 79 80 #define MAX_REG_READ 0x00 81 #define MAX_REG_WRITE 0x02 82 83 #define MAX_REG_RCVFIFO 1 84 #define MAX_REG_SNDFIFO 2 85 #define MAX_REG_SUDFIFO 4 86 #define MAX_REG_RCVBC 6 87 #define MAX_REG_SNDBC 7 88 #define MAX_REG_USBIRQ 13 89 #define MAX_REG_USBIEN 14 90 #define MAX_REG_USBCTL 15 91 #define MAX_REG_CPUCTL 16 92 #define MAX_REG_PINCTL 17 93 #define MAX_REG_REVISION 18 94 #define MAX_REG_HIRQ 25 95 #define MAX_REG_HIEN 26 96 #define MAX_REG_MODE 27 97 #define MAX_REG_PERADDR 28 98 #define MAX_REG_HCTL 29 99 #define MAX_REG_HXFR 30 100 #define MAX_REG_HRSL 31 101 102 #define MAX_USBIRQ_OSCOKIRQ 1 103 #define MAX_USBIRQ_NOVBUSIRQ 32 104 #define MAX_USBIRQ_VBUSIRQ 64 105 106 #define MAX_USBCTL_PWRDOWN 16 107 #define MAX_USBCTL_CHIPRES 32 108 109 #define MAX_CPUCTL_IE 1 110 111 #define MAX_PINCTL_POSINT_LOW 0 112 #define MAX_PINCTL_POSINT_HIGH 4 113 #define MAX_PINCTL_INTLEVEL_EDGE 0 114 #define MAX_PINCTL_INTLEVEL_LEVEL 8 115 #define MAX_PINCTL_FDUPSPI_HALF 0 116 #define MAX_PINCTL_FDUPSPI_FULL 16 117 118 #define MAX_HIRQ_BUSEVENTIRQ 1 119 #define MAX_HIRQ_RWUIRQ 2 120 #define MAX_HIRQ_RCVDAVIRQ 4 121 #define MAX_HIRQ_SNDBAVIRQ 8 122 #define MAX_HIRQ_SUSDNIRQ 16 123 #define MAX_HIRQ_CONDETIRQ 32 124 #define MAX_HIRQ_FRAMEIRQ 64 125 #define MAX_HIRQ_HXFRDNIRQ 128 126 127 #define MAX_HIEN_BUSEVENTIE 1 128 #define MAX_HIEN_CONDETIE 32 129 #define MAX_HIEN_FRAMEIE 64 130 131 #define MAX_MODE_PERIPHERAL 0 132 #define MAX_MODE_HOST 1 133 #define MAX_MODE_LOWSPEED 2 134 #define MAX_MODE_SOFKAENAB 8 135 #define MAX_MODE_SEPIRQ_OFF 0 136 #define MAX_MODE_SEPIRQ_ON 16 137 #define MAX_MODE_DMPULLDN 64 138 #define MAX_MODE_DPPULLDN 128 139 140 #define MAX_MODE_HOST_ENABLED (MAX_MODE_HOST | MAX_MODE_SEPIRQ_OFF | MAX_MODE_DMPULLDN | MAX_MODE_DPPULLDN) 141 #define MAX_MODE_HOST_ACTIVE (MAX_MODE_HOST_ENABLED | MAX_MODE_SOFKAENAB) 142 #define MAX_MODE_HOST_FULLSPEED MAX_MODE_HOST_ACTIVE 143 #define MAX_MODE_HOST_LOWSPEED (MAX_MODE_HOST_ACTIVE | MAX_MODE_LOWSPEED) 144 145 #define MAX_HCTL_BUSRST 1 146 #define MAX_HCTL_SAMPLEBUS 4 147 #define MAX_HCTL_RCVTOG0 16 148 #define MAX_HCTL_RCVTOG1 32 149 #define MAX_HCTL_SNDTOG0 64 150 #define MAX_HCTL_SNDTOG1 128 151 152 #define MAX_HXFR_SETUP 16 153 #define MAX_HXFR_OUTNIN 32 154 #define MAX_HXFR_HS 128 155 156 #define MAX_HRSL_JSTATUS 128 157 #define MAX_HRSL_KSTATUS 64 158 #define MAX_HRSL_SNDTOGRD 32 159 #define MAX_HRSL_RCVTOGRD 16 160 #define MAX_HRSL_HRSLT 15 161 162 #define max_reg(n) ((uint8_t) (n << 3)) 163 #define max_reg_read(n) (max_reg(n) | MAX_REG_READ) 164 #define max_reg_write(n) (max_reg(n) | MAX_REG_WRITE) 165 166 #define usb_descriptor_type(n) ((uint16_t) (n << 8)) 167 168 void spi_begin() 169 { 170 CLR(MAX_SS); 171 } 172 173 void spi_end() 174 { 175 SET(MAX_SS); 176 } 177 178 /** 179 * Send the given value via MOSI while receiving a value via MISO. 180 * This requires full-duplex SPI and will produce a status value for the first 181 * value sent (the command). 182 */ 183 uint8_t spi_sendrecv(uint8_t v) 184 { 185 uint8_t result = 0; 186 uint8_t mask; 187 188 for (mask = 0x80; mask; mask >>= 1) 189 { 190 if (v & mask) 191 { 192 #ifdef DEBUG 193 printf("1"); 194 #endif 195 SET(MAX_MOSI); 196 } 197 else 198 { 199 #ifdef DEBUG 200 printf("0"); 201 #endif 202 CLR(MAX_MOSI); 203 } 204 205 /* Wait for stable output signal. */ 206 207 SET(MAX_SCLK); 208 209 if (PIN(MAX_MISO)) 210 result |= mask; 211 212 CLR(MAX_SCLK); 213 } 214 215 #ifdef DEBUG 216 printf("\n"); 217 #endif 218 return result; 219 } 220 221 uint8_t max_read(uint8_t reg, uint8_t *status) 222 { 223 uint8_t result = 0, tmpstatus = 0; 224 225 tmpstatus = 0; 226 227 spi_begin(); 228 tmpstatus = spi_sendrecv(max_reg_read(reg)); 229 result = spi_sendrecv(0); 230 spi_end(); 231 232 if (status != NULL) 233 *status = tmpstatus; 234 235 return result; 236 } 237 238 uint8_t max_write(uint8_t reg, uint8_t value) 239 { 240 uint8_t status = 0; 241 242 spi_begin(); 243 status = spi_sendrecv(max_reg_write(reg)); 244 spi_sendrecv(value); 245 spi_end(); 246 247 return status; 248 } 249 250 /** 251 * Return whether data can be sent. 252 */ 253 bool max_can_send(uint8_t *status) 254 { 255 if (status == NULL) 256 return max_read(MAX_REG_HIRQ, NULL) & MAX_HIRQ_SNDBAVIRQ; 257 else 258 return *status & MAX_HIRQ_SNDBAVIRQ; 259 } 260 261 /** 262 * Set the sending data toggle. 263 */ 264 void max_set_send_toggle(bool toggle) 265 { 266 max_write(MAX_REG_HCTL, toggle ? MAX_HCTL_SNDTOG1 : MAX_HCTL_SNDTOG0); 267 } 268 269 /** 270 * Return the sending data toggle. 271 */ 272 bool max_get_send_toggle() 273 { 274 return (max_read(MAX_REG_HRSL, NULL) & MAX_HRSL_SNDTOGRD) != 0; 275 } 276 277 /** 278 * Set the receiving data toggle. 279 */ 280 void max_set_recv_toggle(bool toggle) 281 { 282 max_write(MAX_REG_HCTL, toggle ? MAX_HCTL_RCVTOG1 : MAX_HCTL_RCVTOG0); 283 } 284 285 /** 286 * Return the receiving data toggle. 287 */ 288 bool max_get_recv_toggle() 289 { 290 return (max_read(MAX_REG_HRSL, NULL) & MAX_HRSL_RCVTOGRD) != 0; 291 } 292 293 /** 294 * Wait for handshake/timeout after a transfer. 295 */ 296 uint8_t max_wait_transfer(uint8_t status) 297 { 298 while (!(status & MAX_HIRQ_HXFRDNIRQ)) 299 { 300 status = max_read(MAX_REG_HIRQ, NULL); 301 } 302 303 max_write(MAX_REG_HIRQ, MAX_HIRQ_HXFRDNIRQ); 304 return max_read(MAX_REG_HIRQ, NULL); 305 } 306 307 /** 308 * Write the given data to the FIFO. 309 */ 310 void max_write_fifo(uint8_t endpoint, uint8_t *data, uint8_t len) 311 { 312 uint8_t count; 313 314 for (count = 0; count < len; count++) 315 { 316 max_write(endpoint ? MAX_REG_SNDFIFO : MAX_REG_SUDFIFO, data[count]); 317 } 318 319 if (endpoint) 320 max_write(MAX_REG_SNDBC, len); 321 } 322 323 /** 324 * Read the data from the FIFO. 325 */ 326 void max_read_fifo(uint8_t **data, uint8_t *len, uint8_t *datalimit) 327 { 328 uint8_t count, received = max_read(MAX_REG_RCVBC, NULL); 329 printf("Received %d bytes.\n", received); 330 331 *len += received; 332 333 for (count = 0; (count < received) && (*data < datalimit); count++) 334 { 335 *((*data)++) = max_read(MAX_REG_RCVFIFO, NULL); 336 } 337 } 338 339 /** 340 * Send a request to the given endpoint, using the supplied data payload with 341 * the given length, indicating the preserved toggle state of the endpoint 342 * (which will be updated). 343 */ 344 uint8_t max_send_packet(uint8_t endpoint, uint8_t *data, uint8_t len, bool *toggle) 345 { 346 uint8_t status, hrsl = 0; 347 348 max_write_fifo(endpoint, data, len); 349 350 max_set_send_toggle(*toggle); 351 352 /* Initiate the transfer. */ 353 354 do 355 { 356 status = max_write(MAX_REG_HXFR, endpoint | MAX_HXFR_OUTNIN); 357 status = max_wait_transfer(status); 358 359 /* Test for usable data. */ 360 361 if (status & MAX_HIRQ_SNDBAVIRQ) 362 { 363 hrsl = max_read(MAX_REG_HRSL, &status); 364 365 if (!(hrsl & MAX_HRSL_HRSLT)) 366 break; 367 } 368 } 369 while (true); 370 371 *toggle = max_get_send_toggle(); 372 373 return status; 374 } 375 376 /** 377 * Make a request for data from the given endpoint, collecting it in the 378 * supplied buffer, updating the given length of data received, and recording 379 * the data within the given limit, also indicating the preserved toggle state 380 * of the endpoint (which will be updated). 381 */ 382 bool max_recv_packet(uint8_t endpoint, uint8_t **data, uint8_t *received, uint8_t *datalimit, bool *toggle) 383 { 384 uint8_t status, hrsl = 0; 385 uint16_t attempt = 1024; 386 387 max_set_recv_toggle(*toggle); 388 389 /* Initiate the transfer. */ 390 391 do 392 { 393 status = max_write(MAX_REG_HXFR, endpoint); 394 status = max_wait_transfer(status); 395 396 /* Test for usable data. */ 397 398 hrsl = max_read(MAX_REG_HRSL, &status); 399 400 attempt--; 401 } 402 while ((hrsl & MAX_HRSL_HRSLT) && attempt); 403 404 if (!attempt) 405 { 406 printf("HRSL is %x\n", hrsl); 407 return false; 408 } 409 410 while (status & MAX_HIRQ_RCVDAVIRQ) 411 { 412 max_read_fifo(data, received, datalimit); 413 414 /* Indicate that all data has been read. */ 415 416 max_write(MAX_REG_HIRQ, MAX_HIRQ_RCVDAVIRQ); 417 status = max_read(MAX_REG_HIRQ, NULL); 418 } 419 420 *toggle = max_get_recv_toggle(); 421 422 return true; 423 } 424 425 /** 426 * Make a request for data from the given endpoint, collecting it in the 427 * supplied buffer, updating the given length of data received, and also 428 * indicating the preserved toggle state of the endpoint (which will be updated). 429 */ 430 bool max_recv(uint8_t endpoint, uint8_t *data, uint8_t *len, bool *toggle) 431 { 432 uint8_t *datalimit = data + *len; 433 434 *len = 0; 435 436 while (max_recv_packet(endpoint, &data, len, datalimit, toggle)) 437 { 438 if (data == datalimit) 439 return true; 440 } 441 442 return false; 443 } 444 445 /** 446 * Send a control request consisting of the given setup data. 447 */ 448 uint8_t max_control(uint8_t *setup) 449 { 450 uint8_t status, hrsl; 451 452 max_write_fifo(0, setup, 8); 453 454 /* Initiate the transfer. */ 455 456 do 457 { 458 status = max_write(MAX_REG_HXFR, MAX_HXFR_SETUP); 459 status = max_wait_transfer(status); 460 hrsl = max_read(MAX_REG_HRSL, &status); 461 } 462 while (hrsl & MAX_HRSL_HRSLT); 463 464 return status; 465 } 466 467 bool max_control_input(uint8_t *data, uint8_t *len, max_device *device) 468 { 469 device->in_toggle = true; 470 return max_recv(0, data, len, &device->in_toggle); 471 } 472 473 /** 474 * Perform a status transaction as part of a larger control transaction. 475 * The out parameter is used to indicate the kind of status transfer to be 476 * performed and should be the inverse of the control transfer direction. 477 */ 478 uint8_t max_control_status(bool out) 479 { 480 uint8_t status, hrsl; 481 482 do 483 { 484 status = max_write(MAX_REG_HXFR, MAX_HXFR_HS | (out ? MAX_HXFR_OUTNIN : 0)); 485 status = max_wait_transfer(status); 486 hrsl = max_read(MAX_REG_HRSL, &status); 487 } 488 while (hrsl & MAX_HRSL_HRSLT); 489 490 return status; 491 } 492 493 void chipreset() 494 { 495 printf("Resetting...\n"); 496 max_write(MAX_REG_USBCTL, MAX_USBCTL_CHIPRES); 497 498 printf("Clearing the reset...\n"); 499 max_write(MAX_REG_USBCTL, 0); 500 } 501 502 uint8_t check() 503 { 504 uint8_t oscillator; 505 506 oscillator = max_read(MAX_REG_USBIRQ, NULL); 507 508 return (oscillator & ~(MAX_USBIRQ_NOVBUSIRQ | MAX_USBIRQ_VBUSIRQ)) == MAX_USBIRQ_OSCOKIRQ; 509 } 510 511 uint8_t wait() 512 { 513 uint16_t timeout = 1024; 514 515 /* Wait for the oscillator before performing USB activity. */ 516 517 printf("Waiting...\n"); 518 519 while ((timeout > 0) && (!check())) 520 { 521 timeout--; 522 } 523 524 printf("Iterations remaining: %d\n", timeout); 525 526 return timeout; 527 } 528 529 /** 530 * Return whether the bus is ready to be sampled. The application note claims 531 * that the SAMPLEBUS bit is cleared, but this does not seem to be the case. 532 */ 533 uint8_t samplebusready() 534 { 535 uint8_t result; 536 537 result = max_read(MAX_REG_HCTL, NULL); 538 return result & MAX_HCTL_SAMPLEBUS; 539 } 540 541 void samplebus() 542 { 543 max_write(MAX_REG_HCTL, MAX_HCTL_SAMPLEBUS); 544 while (!samplebusready()); 545 } 546 547 /** 548 * Handle the connection or disconnection of a device, returning true if the 549 * device is now connected or false otherwise. If the device is connected, a 550 * bus reset is performed. 551 */ 552 bool devicechanged() 553 { 554 uint8_t hrsl, mode; 555 556 hrsl = max_read(MAX_REG_HRSL, NULL); 557 mode = max_read(MAX_REG_MODE, NULL); 558 559 if ((hrsl & MAX_HRSL_JSTATUS) && (hrsl & MAX_HRSL_KSTATUS)) 560 { 561 printf("Bad device status.\n"); 562 } 563 else if (!(hrsl & MAX_HRSL_JSTATUS) && !(hrsl & MAX_HRSL_KSTATUS)) 564 { 565 printf("Device disconnected.\n"); 566 max_write(MAX_REG_MODE, MAX_MODE_HOST_ENABLED); 567 } 568 else 569 { 570 printf("Device connected.\n"); 571 572 /* Low speed device when J and lowspeed have the same level. 573 Since J and K should have opposing levels, K can be tested when 574 lowspeed is low. */ 575 576 if (((hrsl & MAX_HRSL_JSTATUS) && (mode & MAX_MODE_LOWSPEED)) || 577 ((hrsl & MAX_HRSL_KSTATUS) && !(mode & MAX_MODE_LOWSPEED))) 578 { 579 printf("Device is low speed.\n"); 580 if (max_read(MAX_REG_MODE, NULL) != MAX_MODE_HOST_LOWSPEED) 581 max_write(MAX_REG_MODE, MAX_MODE_HOST_LOWSPEED); 582 } 583 else 584 { 585 printf("Device is full speed.\n"); 586 if (max_read(MAX_REG_MODE, NULL) != MAX_MODE_HOST_FULLSPEED) 587 max_write(MAX_REG_MODE, MAX_MODE_HOST_FULLSPEED); 588 } 589 590 return true; 591 } 592 593 return false; 594 } 595 596 /** 597 * Initialise a USB control request setup payload. 598 */ 599 void setup_packet(uint8_t *setup, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, uint16_t length) 600 { 601 setup[0] = request_type; 602 setup[1] = request; 603 setup[2] = value & 0xff; 604 setup[3] = value >> 8; 605 setup[4] = index & 0xff; 606 setup[5] = index >> 8; 607 setup[6] = length & 0xff; 608 setup[7] = length >> 8; 609 } 610 611 uint8_t usb_descriptor_size(uint8_t type) 612 { 613 switch (type) 614 { 615 case USB_DT_DEVICE: return USB_DT_DEVICE_SIZE; 616 case USB_DT_CONFIG: return USB_DT_CONFIG_SIZE; 617 case USB_DT_STRING: return sizeof(struct usb_string_descriptor); 618 case USB_DT_INTERFACE: return USB_DT_INTERFACE_SIZE; 619 case USB_DT_ENDPOINT: return USB_DT_ENDPOINT_SIZE; 620 default: return 0; 621 } 622 } 623 624 /** 625 * Obtain a device descriptor with usable structure members. 626 */ 627 struct usb_device_descriptor *usb_get_device_descriptor(uint8_t *data) 628 { 629 struct usb_device_descriptor *desc = (struct usb_device_descriptor *) data; 630 desc->bcdUSB = le16toh(desc->bcdUSB); 631 desc->idVendor = le16toh(desc->idVendor); 632 desc->idProduct = le16toh(desc->idProduct); 633 desc->bcdDevice = le16toh(desc->bcdDevice); 634 return desc; 635 } 636 637 /** 638 * Obtain a configuration descriptor with usable structure members. 639 */ 640 struct usb_config_descriptor *usb_get_config_descriptor(uint8_t *data) 641 { 642 struct usb_config_descriptor *conf = (struct usb_config_descriptor *) data; 643 conf->wTotalLength = le16toh(conf->wTotalLength); 644 return conf; 645 } 646 647 /** 648 * Obtain an endpoint descriptor with usable structure members. 649 */ 650 struct usb_endpoint_descriptor *usb_get_endpoint_descriptor(uint8_t *data) 651 { 652 struct usb_endpoint_descriptor *endp = (struct usb_endpoint_descriptor *) data; 653 endp->wMaxPacketSize = le16toh(endp->wMaxPacketSize); 654 return endp; 655 } 656 657 /** 658 * Get a descriptor from the device having the given type, value, index and length. 659 * The descriptor buffer is static and must be copied if it is to be preserved. 660 */ 661 uint8_t *max_get_descriptor_with_length(max_device *device, uint8_t type, uint8_t value, uint8_t index, uint8_t length) 662 { 663 static uint8_t data[1024]; 664 uint8_t len = length, setup[8]; 665 666 setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(type) | value, index, length); 667 668 max_control(setup); 669 if (!max_control_input(data, &len, device)) 670 { 671 printf("Failed.\n"); 672 return NULL; 673 } 674 max_control_status(true); 675 676 if (len >= length) 677 return data; 678 else 679 { 680 printf("Expected %d but received %d.\n", length, len); 681 return NULL; 682 } 683 } 684 685 /** 686 * Get a descriptor from the device having the given type, value, index and 687 * length. If the length is given as zero, the descriptor header will first be 688 * received and then the full descriptor will be retrieved. 689 * The descriptor buffer is static and must be copied if it is to be preserved. 690 */ 691 uint8_t *max_get_descriptor(max_device *device, uint8_t type, uint8_t value, uint8_t index, uint8_t length) 692 { 693 uint8_t *data; 694 695 printf("Sending descriptor request to address %d, endpoint 0...\n", device->address); 696 697 max_write(MAX_REG_PERADDR, device->address); 698 699 if (!length) 700 { 701 data = max_get_descriptor_with_length(device, type, value, index, usb_descriptor_size(type)); 702 if (data == NULL) 703 return NULL; 704 705 if (type == USB_DT_CONFIG) 706 length = usb_get_config_descriptor(data)->wTotalLength; 707 else 708 length = ((struct usb_descriptor_header *) data)->bLength; 709 710 if (length == usb_descriptor_size(type)) 711 return data; 712 } 713 714 return max_get_descriptor_with_length(device, type, value, index, length); 715 } 716 717 bool usb_init_device(max_device *device, uint8_t *data) 718 { 719 struct usb_device_descriptor *desc; 720 721 if (data == NULL) 722 return false; 723 724 desc = usb_get_device_descriptor(data); 725 memcpy(&device->desc, desc, sizeof(struct usb_device_descriptor)); 726 return true; 727 } 728 729 /** 730 * Perform initialisation on a device, obtaining the device details and storing 731 * this information in the device structure, returning true if successful and 732 * false otherwise. 733 */ 734 bool max_init_device(max_device *device) 735 { 736 uint8_t *data; 737 738 printf("Sending control request to address 0, endpoint 0...\n"); 739 740 device->address = 0; 741 742 /* Send a "get descriptor" request for the device descriptor. */ 743 744 data = max_get_descriptor(device, USB_DT_DEVICE, 0, 0, 8); 745 746 if (data != NULL) 747 { 748 usb_init_device(device, data); 749 device->max_packet_size = device->desc.bMaxPacketSize0; 750 751 /* Reset the device. */ 752 753 max_write(MAX_REG_HCTL, MAX_HCTL_BUSRST); 754 return true; 755 } 756 757 return false; 758 } 759 760 /** 761 * Assign a new address to the given device. 762 */ 763 void max_set_address(max_device *device) 764 { 765 uint8_t setup[8]; 766 767 max_write(MAX_REG_PERADDR, 0); 768 769 device->address = next_address++; 770 771 printf("Setting device address to %d...\n", device->address); 772 773 setup_packet(setup, USB_ENDPOINT_OUT, USB_REQ_SET_ADDRESS, device->address, 0, 0); 774 max_control(setup); 775 max_control_status(false); 776 } 777 778 /** 779 * Set the configuration of the given device. 780 */ 781 void max_set_configuration(max_device *device, uint8_t config) 782 { 783 uint8_t setup[8]; 784 785 max_write(MAX_REG_PERADDR, device->address); 786 787 setup_packet(setup, USB_ENDPOINT_OUT, USB_REQ_SET_CONFIGURATION, config, 0, 0); 788 max_control(setup); 789 max_control_status(false); 790 } 791 792 /** 793 * Return a string representation of an endpoint address. 794 */ 795 const char *usb_endpoint_address_str(uint8_t bEndpointAddress) 796 { 797 static char output[] = "255 (127 OUT)"; 798 799 sprintf(output, "%03d (%03d %3s)", bEndpointAddress, 800 bEndpointAddress & ~USB_ENDPOINT_IN, 801 bEndpointAddress & USB_ENDPOINT_IN ? "IN" : "OUT"); 802 803 return output; 804 } 805 806 /** 807 * Return a string representation of endpoint attributes. 808 */ 809 const char *usb_endpoint_attributes_str(uint8_t bmAttributes) 810 { 811 switch (bmAttributes & USB_ENDPOINT_TYPE_MASK) 812 { 813 case USB_ENDPOINT_TYPE_CONTROL: 814 return "CONTROL"; 815 816 case USB_ENDPOINT_TYPE_ISOCHRONOUS: 817 return "ISO"; 818 819 case USB_ENDPOINT_TYPE_BULK: 820 return "BULK"; 821 822 case USB_ENDPOINT_TYPE_INTERRUPT: 823 return "INT"; 824 825 default: 826 return ""; 827 } 828 } 829 830 void usb_show_device(max_device *device) 831 { 832 printf("bLength: %d\n", device->desc.bLength); 833 printf("bDescriptorType: %d\n", device->desc.bDescriptorType); 834 printf("bcdUSB: %04x\n", device->desc.bcdUSB); 835 printf("bDeviceClass: %d\n", device->desc.bDeviceClass); 836 printf("bDeviceSubClass: %d\n", device->desc.bDeviceSubClass); 837 printf("bDeviceProtocol: %d\n", device->desc.bDeviceProtocol); 838 printf("bMaxPacketSize0: %d\n", device->desc.bMaxPacketSize0); 839 printf("idVendor: %04x\n", device->desc.idVendor); 840 printf("idProduct: %04x\n", device->desc.idProduct); 841 printf("bcdDevice: %04x\n", device->desc.bcdDevice); 842 printf("iManufacturer: %d\n", device->desc.iManufacturer); 843 printf("iProduct: %d\n", device->desc.iProduct); 844 printf("iSerialNumber: %d\n", device->desc.iSerialNumber); 845 printf("bNumConfigurations: %d\n", device->desc.bNumConfigurations); 846 } 847 848 void usb_show_languages(uint8_t *data) 849 { 850 struct usb_string_descriptor *desc = (struct usb_string_descriptor *) data; 851 uint16_t *lang; 852 853 if (data == NULL) 854 return; 855 856 printf("bLength: %d\n", desc->bLength); 857 printf("bDescriptorType: %d\n", desc->bDescriptorType); 858 859 for (lang = desc->wData; lang < (uint16_t *) (data + desc->bLength); lang++) 860 { 861 printf("wLangId: %04x\n", le16toh(*lang)); 862 } 863 } 864 865 uint8_t *usb_descriptor_start(struct usb_config_descriptor *conf) 866 { 867 return ((uint8_t *) conf) + conf->bLength; 868 } 869 870 uint8_t *usb_descriptor_end(struct usb_config_descriptor *conf) 871 { 872 return ((uint8_t *) conf) + conf->wTotalLength; 873 } 874 875 uint8_t *usb_descriptor_next(uint8_t *current) 876 { 877 struct usb_descriptor_header *desc = (struct usb_descriptor_header *) current; 878 if (desc->bLength) 879 return current + desc->bLength; 880 else 881 return NULL; 882 } 883 884 void usb_show_configuration(struct usb_config_descriptor *conf) 885 { 886 printf("bLength: %d\n", conf->bLength); 887 printf("bDescriptorType: %d\n", conf->bDescriptorType); 888 printf("wTotalLength: %d\n", conf->wTotalLength); 889 printf("bNumInterfaces: %d\n", conf->bNumInterfaces); 890 printf("bConfigurationValue: %d\n", conf->bConfigurationValue); 891 printf("iConfiguration: %d\n", conf->iConfiguration); 892 printf("bmAttributes: %x\n", conf->bmAttributes); 893 printf("MaxPower: %d\n", conf->MaxPower); 894 } 895 896 void usb_show_interface(struct usb_interface_descriptor *intf) 897 { 898 printf("bInterfaceNumber: %d\n", intf->bInterfaceNumber); 899 printf("bAlternateSetting: %d\n", intf->bAlternateSetting); 900 printf("bNumEndpoints: %d\n", intf->bNumEndpoints); 901 printf("bInterfaceClass: %x\n", intf->bInterfaceClass); 902 printf("bInterfaceSubClass: %x\n", intf->bInterfaceSubClass); 903 printf("bInterfaceProtocol: %x\n", intf->bInterfaceProtocol); 904 printf("iInterface: %d\n", intf->iInterface); 905 } 906 907 void usb_show_endpoint(struct usb_endpoint_descriptor *endp) 908 { 909 printf("bEndpointAddress: %s\n", usb_endpoint_address_str(endp->bEndpointAddress)); 910 printf("bmAttributes: %s\n", usb_endpoint_attributes_str(endp->bmAttributes)); 911 printf("wMaxPacketSize: %d\n", endp->wMaxPacketSize); 912 printf("bInterval: %d\n", endp->bInterval); 913 } 914 915 void usb_show_configuration_full(uint8_t *data) 916 { 917 struct usb_config_descriptor *conf; 918 struct usb_descriptor_header *desc; 919 struct usb_interface_descriptor *intf; 920 struct usb_endpoint_descriptor *endp; 921 uint8_t *current, *last; 922 923 if (data == NULL) 924 return; 925 926 conf = usb_get_config_descriptor(data); 927 current = usb_descriptor_start(conf); 928 last = usb_descriptor_end(conf); 929 930 while (current < last) 931 { 932 desc = (struct usb_descriptor_header *) current; 933 printf("bLength: %d\n", desc->bLength); 934 printf("bDescriptorType: %d\n", desc->bDescriptorType); 935 936 switch (desc->bDescriptorType) 937 { 938 case USB_DT_INTERFACE: 939 intf = (struct usb_interface_descriptor *) current; 940 usb_show_interface(intf); 941 break; 942 943 case USB_DT_ENDPOINT: 944 endp = usb_get_endpoint_descriptor(current); 945 usb_show_endpoint(endp); 946 break; 947 948 default: 949 break; 950 } 951 952 current = usb_descriptor_next(current); 953 954 if (current == NULL) 955 { 956 printf("END (zero length record)\n"); 957 break; 958 } 959 } 960 } 961 962 void usb_show_configurations(max_device *device) 963 { 964 uint8_t n; 965 966 for (n = 0; n < device->desc.bNumConfigurations; n++) 967 { 968 usb_show_configuration_full(max_get_descriptor(device, USB_DT_CONFIG, n, 0, 0)); 969 } 970 } 971 972 bool usb_configuration_has_interface(uint8_t *data, uint8_t bInterfaceClass, uint8_t bInterfaceProtocol) 973 { 974 struct usb_config_descriptor *conf; 975 struct usb_descriptor_header *desc; 976 struct usb_interface_descriptor *intf; 977 uint8_t *current, *last; 978 979 if (data == NULL) 980 return false; 981 982 conf = usb_get_config_descriptor(data); 983 current = usb_descriptor_start(conf); 984 last = usb_descriptor_end(conf); 985 986 while ((current != NULL) && (current < last)) 987 { 988 desc = (struct usb_descriptor_header *) current; 989 switch (desc->bDescriptorType) 990 { 991 case USB_DT_INTERFACE: 992 intf = (struct usb_interface_descriptor *) current; 993 if ((intf->bInterfaceClass == bInterfaceClass) && (intf->bInterfaceProtocol == bInterfaceProtocol)) 994 return true; 995 break; 996 997 default: 998 break; 999 } 1000 1001 current = usb_descriptor_next(current); 1002 } 1003 1004 return false; 1005 } 1006 1007 bool usb_configure_device(max_device *device) 1008 { 1009 uint8_t n, *data; 1010 1011 for (n = 0; n < device->desc.bNumConfigurations; n++) 1012 { 1013 data = max_get_descriptor(device, USB_DT_CONFIG, n, 0, 0); 1014 1015 /* USB HID mouse. */ 1016 1017 if (usb_configuration_has_interface(data, 3, 2)) 1018 { 1019 return true; 1020 } 1021 } 1022 1023 return false; 1024 } 1025 1026 /** 1027 * Handle termination of the process. 1028 */ 1029 void shutdown(int signum) 1030 { 1031 printf("Closing...\n"); 1032 ubb_close(0); 1033 exit(1); 1034 } 1035 1036 int main(int argc, char *argv[]) 1037 { 1038 uint8_t status = 0, revision = 0, framecount; 1039 uint16_t count; 1040 bool bus_event, data_event, suspended_event, connection_event, frame_event; 1041 max_device device; 1042 max_devstate devstate = MAX_DEVSTATE_INIT; 1043 1044 device.in_toggle = false; 1045 device.out_toggle = false; 1046 device.max_packet_size = 8; 1047 1048 signal(SIGINT, &shutdown); 1049 1050 if (ubb_open(0) < 0) { 1051 perror("ubb_open"); 1052 return 1; 1053 } 1054 1055 ubb_power(1); 1056 printf("Power on.\n"); 1057 1058 OUT(MAX_SS); 1059 OUT(MAX_MOSI); 1060 OUT(MAX_SCLK); 1061 OUT(MAX_RESET); 1062 IN(MAX_INT); 1063 IN(MAX_MISO); 1064 1065 /* Initialise SPI. */ 1066 /* Set SS# to 1. */ 1067 1068 SET(MAX_SS); 1069 CLR(MAX_MOSI); 1070 CLR(MAX_SCLK); 1071 SET(MAX_RESET); 1072 1073 /* Initialise the MAX3421E. */ 1074 1075 /* Set full-duplex, interrupt signalling. */ 1076 1077 printf("Setting pin control...\n"); 1078 max_write(MAX_REG_PINCTL, MAX_PINCTL_INTLEVEL_LEVEL | MAX_PINCTL_FDUPSPI_FULL); 1079 1080 chipreset(); 1081 printf("Ready? %d\n", wait()); 1082 1083 /* Check various registers. */ 1084 1085 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 1086 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 1087 1088 /* Set host mode. */ 1089 1090 printf("Setting mode...\n"); 1091 status = max_write(MAX_REG_MODE, MAX_MODE_HOST_ENABLED); 1092 1093 printf("Setting INT signalling...\n"); 1094 status = max_write(MAX_REG_CPUCTL, MAX_CPUCTL_IE); 1095 1096 printf("Setting event signalling...\n"); 1097 status = max_write(MAX_REG_HIEN, MAX_HIEN_CONDETIE | MAX_HIEN_FRAMEIE | MAX_HIEN_BUSEVENTIE); 1098 1099 /* Check various registers. */ 1100 1101 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 1102 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 1103 printf("IE: %x\n", max_read(MAX_REG_HIEN, &status)); 1104 printf("CPU: %x\n", max_read(MAX_REG_CPUCTL, &status)); 1105 printf("Pin: %x\n", max_read(MAX_REG_PINCTL, &status)); 1106 printf("USBIRQ: %x\n", max_read(MAX_REG_USBIRQ, &status)); 1107 printf("USBIE: %x\n", max_read(MAX_REG_USBIEN, &status)); 1108 1109 /* Read from the REVISION register. */ 1110 1111 printf("Reading...\n"); 1112 revision = max_read(MAX_REG_REVISION, &status); 1113 printf("Revision = %x\n", revision); 1114 1115 /* Sample the bus for any devices that are already connected. */ 1116 1117 samplebus(); 1118 1119 if (devicechanged()) 1120 { 1121 devstate = MAX_DEVSTATE_CONNECTED; 1122 framecount = 200; 1123 } 1124 1125 /* Enter the main processing loop. */ 1126 1127 for (count = 0; count <= 65535; count++) 1128 { 1129 if (!PIN(MAX_INT)) 1130 { 1131 /* Obtain interrupt conditions. */ 1132 1133 status = max_read(MAX_REG_HIRQ, NULL); 1134 1135 bus_event = status & MAX_HIRQ_BUSEVENTIRQ; 1136 data_event = status & MAX_HIRQ_RCVDAVIRQ; 1137 suspended_event = status & MAX_HIRQ_SUSDNIRQ; 1138 connection_event = status & MAX_HIRQ_CONDETIRQ; 1139 frame_event = status & MAX_HIRQ_FRAMEIRQ; 1140 1141 #ifdef DEBUG_EVENTS 1142 if (bus_event) printf("Bus "); 1143 if (data_event) printf("Data "); 1144 if (suspended_event) printf("Suspended "); 1145 if (connection_event) printf("Connection "); 1146 if (frame_event) printf("Frame "); 1147 printf("\n"); 1148 #endif 1149 1150 /* Acknowledge the interrupts. */ 1151 1152 max_write(MAX_REG_HIRQ, status); 1153 1154 /* Detect device connection/disconnection. */ 1155 1156 if ((devstate == MAX_DEVSTATE_INIT) && connection_event && devicechanged()) 1157 { 1158 devstate = MAX_DEVSTATE_CONNECTED; 1159 framecount = 200; 1160 printf("CONNECTED\n"); 1161 } 1162 1163 /* Handle device connection. */ 1164 1165 else if ((devstate == MAX_DEVSTATE_CONNECTED) && frame_event && !(--framecount)) 1166 { 1167 /* Reset the device. */ 1168 1169 max_write(MAX_REG_HCTL, MAX_HCTL_BUSRST); 1170 devstate = MAX_DEVSTATE_START; 1171 printf("START\n"); 1172 } 1173 1174 /* Handle device reset initiation. */ 1175 1176 else if ((devstate == MAX_DEVSTATE_START) && bus_event) 1177 { 1178 devstate = MAX_DEVSTATE_RESET; 1179 framecount = 200; 1180 printf("RESET\n"); 1181 } 1182 1183 /* Handle device reset completion, getting device details and 1184 resetting the device again. */ 1185 1186 else if ((devstate == MAX_DEVSTATE_RESET) && frame_event && !(--framecount)) 1187 { 1188 if (!max_init_device(&device)) 1189 printf("FAILED: RESET -> INSPECTED\n"); 1190 else 1191 { 1192 devstate = MAX_DEVSTATE_INSPECTED; 1193 printf("INSPECTED\n"); 1194 } 1195 } 1196 1197 /* Handle second reset initiation. */ 1198 1199 else if ((devstate == MAX_DEVSTATE_INSPECTED) && bus_event) 1200 { 1201 devstate = MAX_DEVSTATE_RESET_AGAIN; 1202 framecount = 200; 1203 printf("RESET_AGAIN\n"); 1204 } 1205 1206 /* Handle second reset completion, setting the address. */ 1207 1208 else if ((devstate == MAX_DEVSTATE_RESET_AGAIN) && frame_event && !(--framecount)) 1209 { 1210 max_set_address(&device); 1211 devstate = MAX_DEVSTATE_ADDRESSING; 1212 framecount = 30; 1213 printf("ADDRESSING\n"); 1214 } 1215 1216 /* Post-addressing. */ 1217 1218 else if ((devstate == MAX_DEVSTATE_ADDRESSING) && frame_event && !(--framecount)) 1219 { 1220 if (usb_init_device(&device, max_get_descriptor(&device, USB_DT_DEVICE, 0, 0, usb_descriptor_size(USB_DT_DEVICE)))) 1221 usb_show_device(&device); 1222 else 1223 printf("DEVICE?\n"); 1224 1225 usb_show_languages(max_get_descriptor(&device, USB_DT_STRING, 0, 0, 0)); 1226 usb_show_configurations(&device); 1227 devstate = MAX_DEVSTATE_READY; 1228 printf("READY\n"); 1229 1230 /* Detect supported interfaces and configure devices to use them. */ 1231 1232 /* usb_configure_device(&device); */ 1233 } 1234 1235 /* Handle device disconnection. */ 1236 1237 else if ((devstate != MAX_DEVSTATE_INIT) && (devstate != MAX_DEVSTATE_CONNECTED) 1238 && connection_event && !devicechanged()) 1239 { 1240 devstate = MAX_DEVSTATE_INIT; 1241 printf("INIT\n"); 1242 } 1243 } 1244 } 1245 1246 printf("Closing...\n"); 1247 ubb_close(0); 1248 1249 return 0; 1250 }