00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "pic_usb.h"
00039 #include "memory.h"
00040 #include "config.h"
00041 #include "pic_usb_buffer_mgt.h"
00042
00043
00044 #include "pic_serial.h"
00045
00046
00047
00048
00049
00050 usb_state_type usb_state = st_POWERED;
00051
00052 setup_data_packet usb_sdp;
00053
00054 uns8 usb_address;
00055 control_mode_type control_mode;
00056 uns16 delivery_bytes_to_send,
00057 delivery_bytes_max_send,
00058 delivery_bytes_sent;
00059 uns8 *delivery_ptr;
00060 uns8 delivery_buffer_size;
00061 uns8 *delivery_buffer;
00062 buffer_descriptor *delivery_bd;
00063
00064 usb_status_type usb_status;
00065
00066 void usb_configure_endpoints() {
00067
00068 #ifdef USB_DEBUG
00069 serial_print_str("Config eps ");
00070 #endif
00071 #ifdef USB_EP1
00072 set_bit (uep1, EPHSHK);
00073 #ifdef USB_EP1_OUT_SIZE
00074 set_bit(uep1, EPOUTEN);
00075 #else
00076 clear_bit(uep1, EPOUTEN);
00077 #endif
00078 #ifdef USB_EP1_IN_SIZE
00079 set_bit(uep1, EPINEN);
00080 #else
00081 clear_bit(uep1, EPINEN);
00082 #endif
00083 set_bit (uep1, EPCONDIS);
00084
00085
00086 #ifdef USB_EP1_IN_SIZE
00087 set_bit(bd1in.stat, DTS);
00088 clear_bit(bd1in.stat, KEN);
00089 clear_bit(bd1in.stat, INCDIS);
00090 clear_bit(bd1in.stat, DTSEN);
00091 clear_bit(bd1in.stat, BSTALL);
00092 clear_bit(bd1in.stat, BC9);
00093 clear_bit(bd1in.stat, BC8);
00094
00095 clear_bit(bd1in.stat, UOWN);
00096 #endif
00097
00098 #ifdef USB_EP1_OUT_SIZE
00099 bd1out.count = USB_EP1_OUT_SIZE;
00100 bd1out.addr = USB_EP1_OUT_ADDR;
00101
00102 clear_bit(bd1out.stat, DTS);
00103 clear_bit(bd1out.stat, KEN);
00104 clear_bit(bd1out.stat, INCDIS);
00105 clear_bit(bd1out.stat, DTSEN);
00106 clear_bit(bd1out.stat, BSTALL);
00107 clear_bit(bd1out.stat, BC9);
00108 clear_bit(bd1out.stat, BC8);
00109 set_bit (bd1out.stat, UOWN);
00110
00111 #endif
00112
00113
00114 #endif
00115
00116 #ifdef USB_EP2
00117 set_bit (uep2, EPHSHK);
00118 #ifdef USB_EP2_OUT_SIZE
00119 set_bit(uep2, EPOUTEN);
00120 #else
00121 clear_bit(uep2, EPOUTEN);
00122 #endif
00123 #ifdef USB_EP2_IN_SIZE
00124 set_bit(uep2, EPINEN);
00125 #else
00126 clear_bit(uep2, EPINEN);
00127 #endif
00128 set_bit (uep2, EPCONDIS);
00129
00130
00131 #ifdef USB_EP2_IN_SIZE
00132
00133 set_bit(bd2in.stat, DTS);
00134 clear_bit(bd2in.stat, KEN);
00135 clear_bit(bd2in.stat, INCDIS);
00136 clear_bit(bd2in.stat, DTSEN);
00137 clear_bit(bd2in.stat, BSTALL);
00138 clear_bit(bd2in.stat, BC9);
00139 clear_bit(bd2in.stat, BC8);
00140
00141 clear_bit(bd2in.stat, UOWN);
00142 #endif
00143
00144 #ifdef USB_EP2_OUT_SIZE
00145 bd2out.count = USB_EP2_OUT_SIZE;
00146 bd2out.addr = USB_EP2_OUT_ADDR;
00147
00148 clear_bit(bd2out.stat, DTS);
00149 clear_bit(bd2out.stat, KEN);
00150 clear_bit(bd2out.stat, INCDIS);
00151 clear_bit(bd2out.stat, DTSEN);
00152 clear_bit(bd2out.stat, BSTALL);
00153 clear_bit(bd2out.stat, BC9);
00154 clear_bit(bd2out.stat, BC8);
00155 set_bit (bd2out.stat, UOWN);
00156 #endif
00157 #endif
00158
00159 #ifdef USB_EP3
00160 set_bit (uep3, EPHSHK);
00161 #ifdef USB_EP3_OUT_SIZE
00162 set_bit(uep3, EPOUTEN);
00163 #else
00164 clear_bit(uep3, EPOUTEN);
00165 #endif
00166 #ifdef USB_EP3_IN_SIZE
00167 set_bit(uep3, EPINEN);
00168 #else
00169 clear_bit(uep3, EPINEN);
00170 #endif
00171 set_bit (uep3, EPCONDIS);
00172
00173 #ifdef USB_EP3_IN_SIZE
00174 set_bit(bd3in.stat, DTS);
00175 clear_bit(bd3in.stat, KEN);
00176 clear_bit(bd3in.stat, INCDIS);
00177 clear_bit(bd3in.stat, DTSEN);
00178 clear_bit(bd3in.stat, BSTALL);
00179 clear_bit(bd3in.stat, BC9);
00180 clear_bit(bd3in.stat, BC8);
00181
00182 clear_bit(bd3in.stat, UOWN);
00183 #endif
00184
00185 #ifdef USB_EP3_OUT_SIZE
00186 bd3out.count = USB_EP3_OUT_SIZE;
00187 bd3out.addr = USB_EP3_OUT_ADDR;
00188
00189 clear_bit(bd3out.stat, DTS);
00190 clear_bit(bd3out.stat, KEN);
00191 clear_bit(bd3out.stat, INCDIS);
00192 clear_bit(bd3out.stat, DTSEN);
00193 clear_bit(bd3out.stat, BSTALL);
00194 clear_bit(bd3out.stat, BC9);
00195 clear_bit(bd3out.stat, BC8);
00196 set_bit (bd3out.stat, UOWN);
00197 #endif
00198
00199 #endif
00200
00201 }
00202
00203 void usb_stall_ep0() {
00204 set_bit(bd0in.stat, BSTALL);
00205 set_bit(bd0in.stat, UOWN);
00206
00207
00208
00209 }
00210
00211 void usb_send_data(uns8 ep, uns8 *data, uns8 send_count, bit first) {
00212 uns8 count;
00213 buffer_descriptor *bd;
00214 uns8 *buffer;
00215
00216
00217 #ifdef USB_DEBUG
00218 serial_print_str("Send:EP");
00219 serial_print_int(ep);
00220 serial_putc(' ');
00221 #endif
00222
00223 buffer = ep_in_buffer_location[ep];
00224
00225 bd = ep_in_bd_location[ep];
00226
00227 if (test_bit(bd->stat, UOWN)) {
00228 #ifdef USB_DEBUG
00229 serial_print_str(" !Adon't own it! ");
00230 #endif
00231 return;
00232 }
00233
00234 count = 0;
00235 while ((count < send_count)) {
00236 buffer[count] = data[count];
00237 count++;
00238 }
00239
00240
00241 bd->count = count;
00242 bd->addr = (uns16)buffer;
00243 if (first) {
00244 clear_bit(bd->stat, DTS);
00245 }
00246
00247 toggle_bit(bd->stat, DTS);
00248 clear_bit(bd->stat, KEN);
00249 clear_bit(bd->stat, INCDIS);
00250 set_bit (bd->stat, DTSEN);
00251 clear_bit(bd->stat, BSTALL);
00252 clear_bit(bd->stat, BC9);
00253 clear_bit(bd->stat, BC8);
00254
00255 set_bit (bd->stat, UOWN);
00256 }
00257
00258
00259
00260 void usb_send_data_chunk() {
00261
00262 uns8 count;
00263 uns8 now = 0;
00264
00265 if (test_bit(bd0in.stat, UOWN)) {
00266 #ifdef USB_DEBUG
00267 serial_print_str(" !Bdon't own it! ");
00268 #endif
00269 return;
00270 }
00271
00272 count = 0;
00273 while ((count < delivery_buffer_size) &&
00274 (delivery_bytes_sent < delivery_bytes_to_send) &&
00275 (delivery_bytes_sent < delivery_bytes_max_send)) {
00276 delivery_buffer[count] = *delivery_ptr;
00277 delivery_ptr++;
00278 delivery_bytes_sent++;
00279 count++;
00280 now++;
00281 }
00282 #ifdef USB_DEBUG
00283 serial_print_str(" wrote ");
00284 serial_print_int(now);
00285 #endif
00286 if ((count < delivery_buffer_size) &&
00287 ((delivery_bytes_sent == delivery_bytes_max_send) ||
00288 (delivery_bytes_sent == delivery_bytes_to_send))) {
00289 #ifdef USB_DEBUG_x
00290 serial_print_str(" c=");
00291 serial_print_int(count);
00292 serial_print_str(" dbsz=");
00293 serial_print_int(delivery_buffer_size);
00294
00295 serial_print_str(" dbs=");
00296 serial_print_int(delivery_bytes_sent);
00297 serial_print_str(" dbms=");
00298 serial_print_int(delivery_bytes_max_send);
00299 serial_print_str(" dbts=");
00300 serial_print_int(delivery_bytes_to_send);
00301 #endif
00302 if (control_mode != cm_CTRL_WRITE_SENDING_STATUS) {
00303 control_mode = cm_CTRL_READ_AWAITING_STATUS;
00304 #ifdef USB_DEBUG_HIGH
00305 serial_print_str(" st2READWAITSTAT ");
00306 #endif
00307 }
00308 }
00309
00310
00311 bd0in.count = count;
00312 bd0in.addr = (uns16)&buffer_0_in;
00313
00314
00315 toggle_bit(bd0in.stat, DTS);
00316
00317 clear_bit(bd0in.stat, KEN);
00318 clear_bit(bd0in.stat, INCDIS);
00319 set_bit (bd0in.stat, DTSEN);
00320 clear_bit(bd0in.stat, BSTALL);
00321 clear_bit(bd0in.stat, BC9);
00322 clear_bit(bd0in.stat, BC8);
00323
00324 set_bit (bd0in.stat, UOWN);
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 }
00339
00340
00341 void usb_send_empty_data_pkt() {
00342 delivery_buffer_size = USB_EP0_IN_SIZE;
00343 delivery_bd = &bd0in;
00344 delivery_buffer = &buffer_0_in;
00345 delivery_bytes_sent = 0;
00346 delivery_bytes_to_send = 0;
00347 delivery_bytes_max_send = 0;
00348 delivery_ptr = (uns8 *) 0;
00349 clear_bit(bd0in.stat, DTS);
00350 usb_send_data_chunk();
00351 }
00352
00353 void usb_stall_on_in() {
00354
00355 clear_bit(bd0in.stat, DTS);
00356
00357 clear_bit(bd0in.stat, KEN);
00358 clear_bit(bd0in.stat, INCDIS);
00359 clear_bit(bd0in.stat, DTSEN);
00360 set_bit(bd0in.stat, BSTALL);
00361 clear_bit(bd0in.stat, BC9);
00362 clear_bit(bd0in.stat, BC8);
00363
00364 set_bit (bd0in.stat, UOWN);
00365 }
00366
00367
00368 uns8 buffer_byte;
00369
00370 void usb_send_one_byte(uns8 data) {
00371 delivery_buffer_size = USB_EP0_IN_SIZE;
00372 delivery_bytes_sent = 0;
00373 delivery_bytes_to_send = 1;
00374 delivery_bytes_max_send = 0;
00375 buffer_byte = data;
00376 delivery_ptr = (uns8 *) &buffer_byte;
00377 delivery_bd = &bd0in;
00378 delivery_buffer = &buffer_0_in;
00379
00380 clear_bit(bd0in.stat, DTS);
00381 usb_send_data_chunk();
00382 }
00383
00384 void usb_prime_ep0_out_e() {
00385
00386 bd0out_e.count = USB_EP0_OUT_E_SIZE;
00387 bd0out_e.addr = USB_EP0_OUT_E_ADDR;
00388
00389 clear_bit(bd0out_e.stat, DTS);
00390 clear_bit(bd0out_e.stat, KEN);
00391 clear_bit(bd0out_e.stat, INCDIS);
00393 clear_bit(bd0out_e.stat, DTSEN);
00394 clear_bit(bd0out_e.stat, BSTALL);
00395 clear_bit(bd0out_e.stat, BC9);
00396 clear_bit(bd0out_e.stat, BC8);
00397
00398 set_bit (bd0out_e.stat, UOWN);
00399
00400 #ifdef USB_DEBUG
00401 serial_putc('P');
00402 serial_putc('E');
00403 #endif
00404 }
00405
00406 void usb_prime_ep0_out_o() {
00407
00408 bd0out_o.count = USB_EP0_OUT_O_SIZE;
00409 bd0out_o.addr = USB_EP0_OUT_O_ADDR;
00410
00411 clear_bit(bd0out_o.stat, DTS);
00412 clear_bit(bd0out_o.stat, KEN);
00413 clear_bit(bd0out_o.stat, INCDIS);
00415 clear_bit(bd0out_o.stat, DTSEN);
00416 clear_bit(bd0out_o.stat, BSTALL);
00417 clear_bit(bd0out_o.stat, BC9);
00418 clear_bit(bd0out_o.stat, BC8);
00419
00420 set_bit (bd0out_o.stat, UOWN);
00421
00422 #ifdef USB_DEBUG
00423 serial_putc('P');
00424 serial_putc('O');
00425 #endif
00426 }
00427
00428 void usb_handle_standard_request(setup_data_packet sdp) {
00429
00430 switch (sdp.bRequest) {
00431 case req_Get_Descriptor:
00432 #ifdef USB_DEBUG
00433 serial_print_str(" GD: ");
00434 #endif
00435 uns8 descriptor_type = sdp.wValue >> 8;
00436 uns8 descriptor_num = sdp.wValue & 0xff;
00437 #ifdef USB_DEBUG
00438 serial_print_int(descriptor_type);
00439 #endif
00440 usb_get_descriptor_callback(descriptor_type, descriptor_num, &delivery_ptr, &delivery_bytes_to_send);
00441 if (delivery_ptr != 0) {
00442 control_mode = cm_CTRL_READ_DATA_STAGE;
00443 delivery_bytes_max_send = sdp.wLength;
00444 delivery_bytes_sent = 0;
00445 delivery_buffer_size = USB_EP0_IN_SIZE;
00446 delivery_bd = &bd0in;
00447 delivery_buffer = (uns8 *)USB_EP0_IN_ADDR;
00448 clear_bit(bd0in.stat, DTS);
00449 usb_send_data_chunk();
00450 } else {
00451 #ifdef USB_DEBUG
00452 serial_print_str(" <stall> ");
00453 #endif
00454 usb_stall_ep0();
00455 }
00456
00457 break;
00458 case req_Set_Address:
00459 usb_address = sdp.wValue & 0xff;
00460 #ifdef USB_DEBUG
00461 serial_print_str(" SA:");
00462 serial_print_int_hex(usb_address);
00463 #endif
00464 usb_status = us_SET_ADDRESS;
00465
00466
00467 usb_send_status_ack();
00468 control_mode = cm_CTRL_WRITE_SENDING_STATUS;
00469 break;
00470 case req_Set_Configuration:
00471 #ifdef USB_DEBUG
00472 serial_print_str(" SC: ");
00473 #endif
00474
00475
00476 usb_configure_endpoints();
00477
00478 #ifdef USB_CALLBACK_ON_DEVICE_CONFIGURED
00479 usb_device_configured_callback();
00480 #endif
00481
00482
00483
00484
00485 usb_send_status_ack();
00486 control_mode = cm_CTRL_WRITE_SENDING_STATUS;
00487
00488 usb_state = st_CONFIGURED;
00489
00490
00491
00492
00493
00494 break;
00495 case req_Get_Interface:
00496 #ifdef USB_DEBUG
00497 serial_print_str(" GI ");
00498 #endif
00499 control_mode = cm_CTRL_READ_DATA_STAGE;
00500 usb_send_one_byte(1);
00501 case req_Get_Status:
00502
00503 #ifdef USB_DEBUG
00504 serial_print_str(" GS ");
00505 #endif
00506
00507 #ifdef USB_SELF_POWERED
00508 usb_send_one_byte(1);
00509 #else
00510 usb_send_one_byte(0);
00511 #endif
00512 break;
00513 default:
00514 #ifdef USB_DEBUG
00515 serial_print_str(" ??SR ");
00516 serial_print_int(sdp.bRequest);
00517 #endif
00518 break;
00519
00520 }
00521 }
00522
00523
00524 void usb_handle_transaction(uns8 stat) {
00525
00526 uns8 end_point, pid;
00527 bit even;
00528 uns8 count;
00529
00530 even = !test_bit(stat, PPBI);
00531 end_point = stat >> 3;
00532
00533 if (test_bit(stat, DIR)) {
00534 #ifdef USB_DEBUG
00535 serial_print_str("\nDI[");
00536 for (count=0; count < 8; count++) {
00537 serial_print_int_hex(buffer_0_in[count]);
00538 serial_putc(']');
00539 }
00540 #endif
00541 pid = (bd0in.stat >> 2) & 0x0f;
00542
00543 } else {
00544 if (even) {
00545 pid = (bd0out_e.stat >> 2) & 0x0f;
00546 } else {
00547 pid = (bd0out_o.stat >> 2) & 0x0f;
00548 }
00549 #ifdef USB_DEBUG
00550 serial_print_str("\nDO ");
00551 if (even) {
00552 for (count=0; count < 8; count++) {
00553 serial_print_int_hex(buffer_0_out_e[count]);
00554 serial_putc(' ');
00555 }
00556 } else {
00557 for (count=0; count < 8; count++) {
00558 serial_print_int_hex(buffer_0_out_o[count]);
00559 serial_putc(' ');
00560 }
00561 }
00562
00563 #endif
00564
00565 }
00566 #ifdef USB_DEBUG
00567 if (end_point != 0) {
00568 serial_putc('E');
00569 serial_print_int_hex(end_point);
00570 serial_print_spc();
00571 }
00572 #endif
00573
00574
00575
00576 if (end_point == 0) {
00577 if (!(test_bit(stat, DIR))) {
00578 #ifdef USB_DEBUG_HIGH
00579 if (even) {
00580 serial_print_str(" (e) ");
00581 } else {
00582 serial_print_str(" (o) ");
00583 }
00584 #endif
00585 }
00586 #ifdef USB _DEBUG_HIGH
00587 serial_print_str(" bytes: ");
00588 if (test_bit(stat, DIR)) {
00589 serial_print_int(bd0in.count);
00590 } else {
00591 if (even) {
00592 serial_print_int(bd0out_e.count);
00593 } else {
00594 serial_print_int(bd0out_o.count);
00595 }
00596 }
00597 #endif
00598 if (pid == pid_SETUP) {
00599
00600 #ifdef USB_DEBUG
00601 serial_print_str(" Setup ");
00602 #endif
00603 if (even) {
00604
00605
00606
00607
00608 memcpy( (void*)&usb_sdp, (void *)&buffer_0_out_e, 8);
00609 usb_prime_ep0_out_e();
00610 } else {
00611
00612
00613
00614
00615 memcpy( (void*)&usb_sdp, (void *)&buffer_0_out_o, 8);
00616 usb_prime_ep0_out_o();
00617 }
00618
00619
00620
00621
00622
00623 clear_bit (bd0in.stat, UOWN);
00624
00625 #ifdef USB_DEBUG
00626
00627 serial_print_str("RT= ");
00628 serial_print_int_hex(usb_sdp.bmRequestType);
00629 serial_print_str(" rq=");
00630 serial_print_int_hex(usb_sdp.bRequest);
00631 serial_print_str(" Va=");
00632 serial_print_int_hex_16bit(usb_sdp.wValue);
00633 serial_print_str(" In=");
00634 serial_print_int_hex_16bit(usb_sdp.wIndex);
00635 serial_print_str(" Ln=");
00636 serial_print_int_hex_16bit(usb_sdp.wLength);
00637
00638
00639
00640 serial_putc(' ');
00641 if (test_bit(usb_sdp.bmRequestType, DATA_STAGE_DIR)) {
00642 serial_print_str(" Data=IN");
00643 } else {
00644 serial_print_str(" Data=OUT/NO");
00645 }
00646 #endif
00647
00648
00649
00650
00651
00652 if (!test_bit(usb_sdp.bmRequestType, REQUEST_TYPE1) &&
00653 !test_bit(usb_sdp.bmRequestType, REQUEST_TYPE0)) {
00654
00655 if ((usb_sdp.bmRequestType & 0b00011111) == 0) {
00656
00657 } else if ((usb_sdp.bmRequestType & 0b00011111) == 1) {
00658
00659 } else if ((usb_sdp.bmRequestType & 0b00011111) == 0b00011) {
00660
00661 }
00662 #ifdef USB_DEBUG
00663 serial_print_str(" std ");
00664 #endif
00665 usb_handle_standard_request(usb_sdp);
00666 } else if (!test_bit(usb_sdp.bmRequestType, REQUEST_TYPE1) &&
00667 test_bit(usb_sdp.bmRequestType, REQUEST_TYPE0)) {
00668 #ifdef USB_DEBUG
00669 serial_print_str(" class ");
00670 #endif
00671 #ifdef USB_CALLBACK_ON_CLASS_CTRL
00672 usb_handle_class_request_callback(usb_sdp);
00673 #endif
00674 } else {
00675
00676
00677
00678 #ifdef USB_DEBUG
00679 serial_print_str(" ??req t=");
00680 serial_print_int(usb_sdp.bmRequestType);
00681 serial_putc(' ');
00682 #endif
00683 }
00684 clear_bit(ucon, PKTDIS);
00685
00686 } else if (pid == pid_IN) {
00687 #ifdef USB_DEBUG
00688 serial_print_str(" IN ");
00689 #endif
00690 if (control_mode == cm_CTRL_READ_DATA_STAGE) {
00691
00692 usb_send_data_chunk();
00693 } else if (control_mode == cm_CTRL_WRITE_SENDING_STATUS) {
00694
00695 control_mode = cm_IDLE;
00696 if (usb_status == us_SET_ADDRESS) {
00697 #ifdef USB_DEBUG
00698 serial_print_str(" addr to ");
00699 serial_print_int(uaddr);
00700 #endif
00701 usb_state = st_ADDRESS;
00702 uaddr = usb_address;
00703 usb_status = us_IDLE;
00704 }
00705 #ifdef USB_DEBUG
00706 serial_print_str(" ----\n");
00707 #endif
00708 } else if (control_mode == cm_CTRL_READ_AWAITING_STATUS) {
00709
00710 #ifdef USB_DEBUG
00711 serial_print_str(" last read, waiting status");
00712 #endif
00714 //usb_prime_ep0_out(); // !! NEW
00715
00716 nop();
00717 } else if (control_mode == cm_CTRL_READ_DATA_STAGE_CLASS) {
00718 #ifdef USB_DEBUG
00719 serial_print_str(" ctrl read data stage class - more to come? ");
00720 #endif
00721
00722 #ifdef USB_CALLBACK_ON_CLASS_CTRL
00723 usb_handle_class_ctrl_read_callback();
00724 #else
00725 nop();
00726 #endif
00727 } else {
00728 #ifdef USB_DEBUG
00729 serial_print_str(" ?? cm=");
00730 serial_print_int((uns8)control_mode);
00731 serial_print_spc();
00732 #else
00733 nop();
00734 #endif
00735
00736 }
00737 } else if (pid == pid_ACK) {
00738 #ifdef USB_DEBUG
00739 serial_print_str("****A\n");
00740 #endif
00741 if (control_mode == cm_CTRL_READ_DATA_STAGE) {
00742 #ifdef USB_DEBUG
00743 serial_print_str(" &2 ");
00744 #endif
00745 usb_send_data_chunk();
00746 } else if (control_mode == cm_CTRL_READ_DATA_STAGE_CLASS) {
00747 #ifdef USB_CALLBACK_ON_CLASS_CTRL
00748 usb_handle_class_ctrl_read_callback();
00749 #else
00750 nop();
00751 #endif
00752 } else if (control_mode == cm_CTRL_WRITE_SENDING_STATUS) {
00753 #ifdef USB_DEBUG
00754 serial_print_str(" st sent ");
00755 #endif
00756 control_mode = cm_IDLE;
00757 } else if (control_mode == cm_CTRL_READ_AWAITING_STATUS) {
00758 #ifdef USB_DEBUG
00759 serial_print_str(" now what? ");
00760 #endif
00761 control_mode = cm_IDLE;
00762 }
00763
00764
00765 } else if (pid == pid_OUT) {
00766 uns8 count;
00767 uns8 *buffer;
00768
00769 if (even) {
00770 count = bd0out_e.count;
00771 buffer = &buffer_0_out_e;
00772 usb_prime_ep0_out_e();
00773 } else {
00774 count = bd0out_o.count;
00775 buffer = &buffer_0_out_o;
00776 usb_prime_ep0_out_o();
00777 }
00778
00779
00780
00781 if (control_mode == cm_CTRL_READ_AWAITING_STATUS) {
00782 #ifdef USB_DEBUG
00783 serial_print_str(" ----\n");
00784 #endif
00785 control_mode = cm_IDLE;
00786 } else if (control_mode == cm_CTRL_WRITE_DATA_STAGE_CLASS) {
00787 #ifdef USB_CALLBACK_ON_CLASS_CTRL
00788 usb_handle_class_ctrl_write_callback(buffer, count);
00789
00790
00791 #else
00792 nop();
00793 #endif
00794 } else if (control_mode == cm_CTRL_READ_DATA_STAGE) {
00795
00796 control_mode = cm_IDLE;
00797 } else {
00798
00799 #ifdef USB_DEBUG
00800 serial_print_str("??unk pid_OUT ");
00801 serial_print_int((uns8)control_mode);
00802 #endif
00803 }
00804
00805 } else {
00806 #ifdef USB_DEBUG
00807 serial_print_str(" UKPID = ");
00808 serial_print_int(pid);
00809 #endif
00810 }
00811
00812 } else {
00813 buffer_descriptor *bd;
00814 if (test_bit(stat, DIR)) {
00815
00816 #ifdef USB_EP_DATA_CALLBACK
00817 bd = ep_in_bd_location[end_point];
00818 usb_ep_data_in_callback(end_point, bd->count);
00819 #else
00820 nop();
00821 #endif
00822 } else {
00823
00824 bd = ep_out_bd_location[end_point];
00825
00826 #ifdef USB_EP_DATA_CALLBACK
00827 usb_ep_data_out_callback(end_point, ep_out_buffer_location[end_point],
00828 bd->count);
00829 #endif
00830
00831 bd->count = ep_out_buffer_size[end_point];
00832 bd->addr = (uns16)ep_out_buffer_location[end_point];
00833
00834
00835
00836 clear_bit(bd->stat, DTS);
00837 clear_bit(bd->stat, KEN);
00838 clear_bit(bd->stat, INCDIS);
00839 clear_bit(bd->stat, DTSEN);
00840 clear_bit(bd->stat, BSTALL);
00841 clear_bit(bd->stat, BC9);
00842 clear_bit(bd->stat, BC8);
00843 set_bit (bd->stat, UOWN);
00844
00845
00846 }
00847 }
00848
00849
00850
00851 }
00852
00853 void usb_handle_reset() {
00854 usb_address = 0;
00855
00856
00857 control_mode = cm_IDLE;
00858 usb_status = us_IDLE;
00859
00860
00861 clear_bit(uir, TRNIF);
00862 clear_bit(uir, TRNIF);
00863 clear_bit(uir, TRNIF);
00864 clear_bit(uir, TRNIF);
00865
00866
00867
00868
00869
00870 bd0out_e.count = USB_EP0_OUT_E_SIZE;
00871 bd0out_e.addr = USB_EP0_OUT_E_ADDR;
00872
00873 clear_bit(bd0out_e.stat, DTS);
00874 clear_bit(bd0out_e.stat, KEN);
00875 clear_bit(bd0out_e.stat, INCDIS);
00876 clear_bit (bd0out_e.stat, DTSEN);
00877 clear_bit(bd0out_e.stat, BSTALL);
00878 clear_bit(bd0out_e.stat, BC9);
00879 clear_bit(bd0out_e.stat, BC8);
00880
00881 set_bit (bd0out_e.stat, UOWN);
00882
00883
00884 bd0out_o.count = USB_EP0_OUT_O_SIZE;
00885 bd0out_o.addr = USB_EP0_OUT_O_ADDR;
00886
00887 clear_bit(bd0out_o.stat, DTS);
00888 clear_bit(bd0out_o.stat, KEN);
00889 clear_bit(bd0out_o.stat, INCDIS);
00890 clear_bit (bd0out_o.stat, DTSEN);
00891 clear_bit(bd0out_o.stat, BSTALL);
00892 clear_bit(bd0out_o.stat, BC9);
00893 clear_bit(bd0out_o.stat, BC8);
00894
00895 set_bit (bd0out_o.stat, UOWN);
00896
00897
00898
00899
00900
00901 bd0in.count = USB_EP0_IN_SIZE;
00902 bd0in.addr = USB_EP0_IN_ADDR;
00903 clear_bit(bd0in.stat, DTS);
00904 clear_bit(bd0in.stat, KEN);
00905 clear_bit(bd0in.stat, INCDIS);
00906 clear_bit(bd0in.stat, BSTALL);
00907 clear_bit(bd0in.stat, BC9);
00908 clear_bit(bd0in.stat, BC8);
00909
00910 clear_bit(bd0in.stat, UOWN);
00911
00912 #ifdef USB_DEBUG
00913 serial_print_str("\nR ");
00914 serial_print_int_hex(uir);
00915 #endif
00916 }
00917
00918 void usb_handle_stall() {
00919 #ifdef USB_DEBUG
00920 serial_print_str(" U:Stall ");
00921 #endif
00922 clear_bit(bd0in.stat, UOWN);
00923 clear_bit(bd0in.stat, BSTALL);
00924 }
00925
00926
00927
00928 void usb_handle_isr() {
00929
00930 while (test_bit(pir2, USBIF)) {
00931
00932 while (test_bit(uir, TRNIF)) {
00933 uns8 stat = ustat;
00934 clear_bit(uir, TRNIF);
00935 usb_handle_transaction(stat);
00936 }
00937
00938 if (test_bit(uir, URSTIF)) {
00939 usb_handle_reset();
00940 clear_bit(uir, URSTIF);
00941 }
00942
00943 if (test_bit(uir, STALLIF)) {
00944 usb_handle_stall();
00945 clear_bit(uir, STALLIF);
00946 }
00947 if (test_bit(uir, SOFIF)) {
00948 #ifdef USB_CALLBACK_ON_SOF
00949 #ifdef ufrm
00950 usb_SOF_callback(ufrm);
00951 #else
00952 usb_SOF_callback(ufrmh << 8 | ufrml);
00953 #endif
00954 clear_bit(uir, SOFIF);
00955 #endif
00956 }
00957
00958 clear_bit(pir2, USBIF);
00959 }
00960 }
00961
00962 void turn_usb_ints_on() {
00963
00964 set_bit(uie, STALLIE);
00965 set_bit(uie, TRNIE);
00966 set_bit(uie, URSTIE);
00967 set_bit(pie2, USBIE);
00968 #ifdef USB_CALLBACK_ON_SOF
00969 set_bit(uie, SOFIE);
00970 #endif
00971 }
00972
00973 void usb_setup() {
00974
00975 usb_state = st_POWERED;
00976
00977
00978 #ifdef UTRDIS
00979 clear_bit(ucfg, UTRDIS);
00980 #endif
00981 set_bit (ucfg, FSEN);
00982 set_bit (ucfg, UPUEN);
00983
00984 clear_bit(ucfg, PPB1);
00985 set_bit(ucfg, PPB0);
00986
00987
00988 set_bit(ucon, PPBRST);
00989 clear_bit(ucon, PPBRST);
00990
00991
00992
00993 set_bit(uep0, EPHSHK);
00994 set_bit(uep0, EPOUTEN);
00995 set_bit(uep0, EPINEN);
00996 clear_bit(uep0, EPCONDIS);
00997
00998
00999
01000
01001 ep_out_bd_location[0] = &bd0out_e;
01002 #if USB_HIGHEST_EP >= 1
01003 ep_out_bd_location[1] = &bd1out;
01004 #endif
01005 #if USB_HIGHEST_EP >= 2
01006 ep_out_bd_location[2] = &bd2out;
01007 #endif
01008 #if USB_HIGHEST_EP >= 3
01009 ep_out_bd_location[3] = &bd3out;
01010 #endif
01011 #if USB_HIGHEST_EP >= 4
01012 ep_out_bd_location[4] = &bd4out;
01013 #endif
01014
01015 ep_in_bd_location[0] = &bd0in;
01016 #if USB_HIGHEST_EP >= 1
01017 ep_in_bd_location[1] = &bd1in;
01018 #endif
01019 #if USB_HIGHEST_EP >= 2
01020 ep_in_bd_location[2] = &bd2in;
01021 #endif
01022 #if USB_HIGHEST_EP >= 3
01023 ep_in_bd_location[3] = &bd3in;
01024 #endif
01025 #if USB_HIGHEST_EP >= 4
01026 ep_in_bd_location[4] = &bd4in;
01027 #endif
01028
01029
01030 }
01031
01032
01033 void usb_enable_module() {
01034 uir = 0;
01035 set_bit(ucon, USBEN);
01036 usb_state = st_DEFAULT;
01037 }
01038
01039 usb_state_type usb_get_state() {
01040 return usb_state;
01041 }